Project

General

Profile

Submit #2933 ยป patch-dfbsd-cat-nosocket.txt

sevan, 08/01/2016 06:04 PM

 
diff --git a/bin/cat/cat.1 b/bin/cat/cat.1
index 861d62e..6a3a5d6 100644
--- a/bin/cat/cat.1
+++ b/bin/cat/cat.1
@@ -57,18 +57,6 @@ is a single dash
or absent,
.Nm
reads from the standard input.
-If
-.Ar file
-is a
-.Ux
-domain socket,
-.Nm
-connects to it and then reads it until
-.Dv EOF .
-This complements the
-.Ux
-domain binding capability available in
-.Xr inetd 8 .
.Pp
The options are as follows:
.Bl -tag -width indent
diff --git a/bin/cat/cat.c b/bin/cat/cat.c
index a2aff71..c58fb0c 100644
--- a/bin/cat/cat.c
+++ b/bin/cat/cat.c
@@ -37,17 +37,11 @@
#include <sys/param.h>
#include <sys/stat.h>
-#ifndef NO_UDOM_SUPPORT
-#include <sys/socket.h>
-#include <sys/un.h>
-#include <errno.h>
-#endif
#include <ctype.h>
#include <err.h>
#include <fcntl.h>
#include <locale.h>
-#include <stddef.h>
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
@@ -56,13 +50,10 @@
static int bflag, eflag, nflag, sflag, tflag, vflag, rval;
static const char *filename;
-static void scanfiles(char **, int);
-static void cook_cat(FILE *);
-static void raw_cat(int);
-
-#ifndef NO_UDOM_SUPPORT
-static int udom_open(const char *, int);
-#endif
+void cook_args(char *argv[]);
+void cook_buf(FILE *);
+void raw_args(char *argv[]);
+void raw_cat(int);
static void
usage(void)
@@ -109,63 +100,42 @@ main(int argc, char **argv)
argv += optind;
if (bflag || eflag || nflag || sflag || tflag || vflag)
- scanfiles(argv, 1);
+ cook_args(argv);
else
- scanfiles(argv, 0);
+ raw_args(argv);
if (fclose(stdout))
err(1, "stdout");
exit(rval);
/* NOTREACHED */
}
-static void
-scanfiles(char **argv, int cooked)
+void
+cook_args(char **argv)
{
- char *path;
FILE *fp;
- int fd;
- int i;
- i = 0;
-
- while ((path = argv[i]) != NULL || i == 0) {
- if (path == NULL || strcmp(path, "-") == 0) {
- filename = "stdin";
- fd = STDIN_FILENO;
- } else {
- filename = path;
- fd = open(path, O_RDONLY);
-#ifndef NO_UDOM_SUPPORT
- if (fd < 0 && errno == EOPNOTSUPP)
- fd = udom_open(path, O_RDONLY);
-#endif
- }
- if (fd < 0) {
- warn("%s", path);
- rval = 1;
- } else if (cooked) {
- if (fd == STDIN_FILENO) {
- cook_cat(stdin);
- } else {
- fp = fdopen(fd, "r");
- if (fp == NULL)
- err(1, "%s", path);
- cook_cat(fp);
- fclose(fp);
+ fp = stdin;
+ filename = "stdin";
+ do {
+ if (*argv) {
+ if (!strcmp(*argv, "-"))
+ fp = stdin;
+ else if ((fp = fopen(*argv, "r")) == NULL) {
+ warn("%s", *argv);
+ rval = 1;
+ ++argv;
+ continue;
}
- } else {
- raw_cat(fd);
- if (fd != STDIN_FILENO)
- close(fd);
+ filename = *argv++;
}
- if (path == NULL)
- break;
- ++i;
- }
+ cook_buf(fp);
+ if (fp != stdin)
+ (void)fclose(fp);
+ } while (*argv);
}
-static void
-cook_cat(FILE *fp)
+void
+cook_buf(FILE *fp)
{
int ch, gobble, line, prev;
@@ -231,7 +201,32 @@ cook_cat(FILE *fp)
err(1, "stdout");
}
-static void
+void
+raw_args(char **argv)
+{
+ int fd;
+
+ fd = fileno(stdin);
+ filename = "stdin";
+ do {
+ if (*argv) {
+ if (!strcmp(*argv, "-"))
+ fd = fileno(stdin);
+ else if ((fd = open(*argv, O_RDONLY, 0)) < 0) {
+ warn("%s", *argv);
+ rval = 1;
+ ++argv;
+ continue;
+ }
+ filename = *argv++;
+ }
+ raw_cat(fd);
+ if (fd != fileno(stdin))
+ (void)close(fd);
+ } while (*argv);
+};
+
+void
raw_cat(int rfd)
{
int off;
@@ -276,55 +271,3 @@ raw_cat(int rfd)
rval = 1;
}
}
-
-#ifndef NO_UDOM_SUPPORT
-
-static int
-udom_open(const char *path, int flags)
-{
- struct sockaddr_un sou;
- int fd;
- unsigned int len;
-
- bzero(&sou, sizeof(sou));
-
- /*
- * Construct the unix domain socket address and attempt to connect
- */
- fd = socket(AF_UNIX, SOCK_STREAM, 0);
- if (fd >= 0) {
- sou.sun_family = AF_UNIX;
- if ((len = strlcpy(sou.sun_path, path,
- sizeof(sou.sun_path))) >= sizeof(sou.sun_path)) {
- errno = ENAMETOOLONG;
- return (-1);
- }
- len = offsetof(struct sockaddr_un, sun_path[len+1]);
-
- if (connect(fd, (void *)&sou, len) < 0) {
- close(fd);
- fd = -1;
- }
- }
-
- /*
- * handle the open flags by shutting down appropriate directions
- */
- if (fd >= 0) {
- switch (flags & O_ACCMODE) {
- case O_RDONLY:
- if (shutdown(fd, SHUT_WR) == -1)
- warn(NULL);
- break;
- case O_WRONLY:
- if (shutdown(fd, SHUT_RD) == -1)
- warn(NULL);
- break;
- default:
- break;
- }
- }
- return(fd);
-}
-
-#endif
    (1-1/1)