Project

General

Profile

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

sevan, 08/01/2016 06:04 PM

 
1
diff --git a/bin/cat/cat.1 b/bin/cat/cat.1
2
index 861d62e..6a3a5d6 100644
3
--- a/bin/cat/cat.1
4
+++ b/bin/cat/cat.1
5
@@ -57,18 +57,6 @@ is a single dash
6
 or absent,
7
 .Nm
8
 reads from the standard input.
9
-If
10
-.Ar file
11
-is a
12
-.Ux
13
-domain socket,
14
-.Nm
15
-connects to it and then reads it until
16
-.Dv EOF .
17
-This complements the
18
-.Ux
19
-domain binding capability available in
20
-.Xr inetd 8 .
21
 .Pp
22
 The options are as follows:
23
 .Bl -tag -width indent
24
diff --git a/bin/cat/cat.c b/bin/cat/cat.c
25
index a2aff71..c58fb0c 100644
26
--- a/bin/cat/cat.c
27
+++ b/bin/cat/cat.c
28
@@ -37,17 +37,11 @@
29
 
30
 #include <sys/param.h>
31
 #include <sys/stat.h>
32
-#ifndef NO_UDOM_SUPPORT
33
-#include <sys/socket.h>
34
-#include <sys/un.h>
35
-#include <errno.h>
36
-#endif
37
 
38
 #include <ctype.h>
39
 #include <err.h>
40
 #include <fcntl.h>
41
 #include <locale.h>
42
-#include <stddef.h>
43
 #include <stdio.h>
44
 #include <stdlib.h>
45
 #include <string.h>
46
@@ -56,13 +50,10 @@
47
 static int bflag, eflag, nflag, sflag, tflag, vflag, rval;
48
 static const char *filename;
49
 
50
-static void	scanfiles(char **, int);
51
-static void	cook_cat(FILE *);
52
-static void	raw_cat(int);
53
-
54
-#ifndef NO_UDOM_SUPPORT
55
-static int	udom_open(const char *, int);
56
-#endif
57
+void	cook_args(char *argv[]);
58
+void	cook_buf(FILE *);
59
+void	raw_args(char *argv[]);
60
+void	raw_cat(int);
61
 
62
 static void
63
 usage(void)
64
@@ -109,63 +100,42 @@ main(int argc, char **argv)
65
 	argv += optind;
66
 
67
 	if (bflag || eflag || nflag || sflag || tflag || vflag)
68
-		scanfiles(argv, 1);
69
+		cook_args(argv);
70
 	else
71
-		scanfiles(argv, 0);
72
+		raw_args(argv);
73
 	if (fclose(stdout))
74
 		err(1, "stdout");
75
 	exit(rval);
76
 	/* NOTREACHED */
77
 }
78
 
79
-static void
80
-scanfiles(char **argv, int cooked)
81
+void
82
+cook_args(char **argv)
83
 {
84
-	char *path;
85
 	FILE *fp;
86
-	int fd;
87
-	int i;
88
 
89
-	i = 0;
90
-
91
-	while ((path = argv[i]) != NULL || i == 0) {
92
-		if (path == NULL || strcmp(path, "-") == 0) {
93
-			filename = "stdin";
94
-			fd = STDIN_FILENO;
95
-		} else {
96
-			filename = path;
97
-			fd = open(path, O_RDONLY);
98
-#ifndef NO_UDOM_SUPPORT
99
-			if (fd < 0 && errno == EOPNOTSUPP)
100
-				fd = udom_open(path, O_RDONLY);
101
-#endif
102
-		}
103
-		if (fd < 0) {
104
-			warn("%s", path);
105
-			rval = 1;
106
-		} else if (cooked) {
107
-			if (fd == STDIN_FILENO) {
108
-				cook_cat(stdin);
109
-			} else {
110
-				fp = fdopen(fd, "r");
111
-				if (fp == NULL)
112
-					err(1, "%s", path);
113
-				cook_cat(fp);
114
-				fclose(fp);
115
+	fp = stdin;
116
+	filename = "stdin";
117
+	do {
118
+		if (*argv) {
119
+			if (!strcmp(*argv, "-"))
120
+				fp = stdin;
121
+			else if ((fp = fopen(*argv, "r")) == NULL) {
122
+				warn("%s", *argv);
123
+				rval = 1;
124
+				++argv;
125
+				continue;
126
 			}
127
-		} else {
128
-			raw_cat(fd);
129
-			if (fd != STDIN_FILENO)
130
-				close(fd);
131
+			filename = *argv++;
132
 		}
133
-		if (path == NULL)
134
-			break;
135
-		++i;
136
-	}
137
+		cook_buf(fp);
138
+		if (fp != stdin)
139
+			(void)fclose(fp);
140
+	} while (*argv);
141
 }
142
 
143
-static void
144
-cook_cat(FILE *fp)
145
+void
146
+cook_buf(FILE *fp)
147
 {
148
 	int ch, gobble, line, prev;
149
 
150
@@ -231,7 +201,32 @@ cook_cat(FILE *fp)
151
 		err(1, "stdout");
152
 }
153
 
154
-static void
155
+void
156
+raw_args(char **argv)
157
+{
158
+	int fd;
159
+
160
+	fd = fileno(stdin);
161
+	filename = "stdin";
162
+	do {
163
+		if (*argv) {
164
+			if (!strcmp(*argv, "-"))
165
+				fd = fileno(stdin);
166
+			else if ((fd = open(*argv, O_RDONLY, 0)) < 0) {
167
+				warn("%s", *argv);
168
+				rval = 1;
169
+				++argv;
170
+				continue;
171
+			}
172
+			filename = *argv++;
173
+		}
174
+		raw_cat(fd);
175
+		if (fd != fileno(stdin))
176
+			(void)close(fd);
177
+	} while (*argv);
178
+};
179
+
180
+void
181
 raw_cat(int rfd)
182
 {
183
 	int off;
184
@@ -276,55 +271,3 @@ raw_cat(int rfd)
185
 		rval = 1;
186
 	}
187
 }
188
-
189
-#ifndef NO_UDOM_SUPPORT
190
-
191
-static int
192
-udom_open(const char *path, int flags)
193
-{
194
-	struct sockaddr_un sou;
195
-	int fd;
196
-	unsigned int len;
197
-
198
-	bzero(&sou, sizeof(sou));
199
-
200
-	/*
201
-	 * Construct the unix domain socket address and attempt to connect
202
-	 */
203
-	fd = socket(AF_UNIX, SOCK_STREAM, 0);
204
-	if (fd >= 0) {
205
-		sou.sun_family = AF_UNIX;
206
-		if ((len = strlcpy(sou.sun_path, path,
207
-		    sizeof(sou.sun_path))) >= sizeof(sou.sun_path)) {
208
-			errno = ENAMETOOLONG;
209
-			return (-1);
210
-		}
211
-		len = offsetof(struct sockaddr_un, sun_path[len+1]);
212
-
213
-		if (connect(fd, (void *)&sou, len) < 0) {
214
-			close(fd);
215
-			fd = -1;
216
-		}
217
-	}
218
-
219
-	/*
220
-	 * handle the open flags by shutting down appropriate directions
221
-	 */
222
-	if (fd >= 0) {
223
-		switch (flags & O_ACCMODE) {
224
-		case O_RDONLY:
225
-			if (shutdown(fd, SHUT_WR) == -1)
226
-				warn(NULL);
227
-			break;
228
-		case O_WRONLY:
229
-			if (shutdown(fd, SHUT_RD) == -1)
230
-				warn(NULL);
231
-			break;
232
-		default:
233
-			break;
234
-		}
235
-	}
236
-	return(fd);
237
-}
238
-
239
-#endif
    (1-1/1)