xref: /titanic_51/usr/src/cmd/cat/cat.c (revision 602ca9ea8f9ce0933f0944601cc5d230e91a950d)
1 /*
2  * CDDL HEADER START
3  *
4  * The contents of this file are subject to the terms of the
5  * Common Development and Distribution License (the "License").
6  * You may not use this file except in compliance with the License.
7  *
8  * You can obtain a copy of the license at usr/src/OPENSOLARIS.LICENSE
9  * or http://www.opensolaris.org/os/licensing.
10  * See the License for the specific language governing permissions
11  * and limitations under the License.
12  *
13  * When distributing Covered Code, include this CDDL HEADER in each
14  * file and include the License file at usr/src/OPENSOLARIS.LICENSE.
15  * If applicable, add the following below this CDDL HEADER, with the
16  * fields enclosed by brackets "[]" replaced with your own identifying
17  * information: Portions Copyright [yyyy] [name of copyright owner]
18  *
19  * CDDL HEADER END
20  */
21 /*	Copyright (c) 1984, 1986, 1987, 1988, 1989 AT&T	*/
22 /*	  All Rights Reserved  	*/
23 
24 
25 /*
26  * Copyright 2007 Sun Microsystems, Inc.  All rights reserved.
27  * Use is subject to license terms.
28  */
29 
30 #pragma ident	"%Z%%M%	%I%	%E% SMI"
31 
32 /*
33  *	Concatenate files.
34  */
35 
36 #include	<stdio.h>
37 #include	<stdlib.h>
38 #include	<ctype.h>
39 #include	<sys/types.h>
40 #include	<sys/stat.h>
41 #include	<locale.h>
42 #include	<unistd.h>
43 #include	<sys/mman.h>
44 #include	<errno.h>
45 #include	<string.h>
46 
47 #include	<widec.h>
48 #include	<wctype.h>
49 #include	<limits.h>
50 #include	<libintl.h>
51 #define	IDENTICAL(A, B)	(A.st_dev == B.st_dev && A.st_ino == B.st_ino)
52 
53 #define	MAXMAPSIZE	(8*1024*1024)	/* map at most 8MB */
54 #define	SMALLFILESIZE	(32*1024)	/* don't use mmap on little files */
55 
56 static int vncat(FILE *);
57 static int cat(FILE *, struct stat *, struct stat *, char *);
58 
59 static int	silent = 0;		/* s flag */
60 static int	visi_mode = 0;		/* v flag */
61 static int	visi_tab = 0;		/* t flag */
62 static int	visi_newline = 0;	/* e flag */
63 static int	bflg = 0;		/* b flag */
64 static int	nflg = 0;		/* n flag */
65 static long	ibsize;
66 static long	obsize;
67 static unsigned	char	buf[SMALLFILESIZE];
68 
69 
70 int
71 main(int argc, char **argv)
72 {
73 	FILE *fi;
74 	int c;
75 	extern	int optind;
76 	int	errflg = 0;
77 	int	stdinflg = 0;
78 	int	status = 0;
79 	int	estatus = 0;
80 	struct stat source, target;
81 
82 	(void) setlocale(LC_ALL, "");
83 #if !defined(TEXT_DOMAIN)	/* Should be defined by cc -D */
84 #define	TEXT_DOMAIN "SYS_TEST"	/* Use this only if it weren't */
85 #endif
86 	(void) textdomain(TEXT_DOMAIN);
87 
88 #ifdef STANDALONE
89 	/*
90 	 * If the first argument is NULL,
91 	 * discard arguments until we find cat.
92 	 */
93 	if (argv[0][0] == '\0')
94 		argc = getargv("cat", &argv, 0);
95 #endif
96 
97 	/*
98 	 * Process the options for cat.
99 	 */
100 
101 	while ((c = getopt(argc, argv, "usvtebn")) != EOF) {
102 		switch (c) {
103 
104 		case 'u':
105 
106 			/*
107 			 * If not standalone, set stdout to
108 			 * completely unbuffered I/O when
109 			 * the 'u' option is used.
110 			 */
111 
112 #ifndef	STANDALONE
113 			setbuf(stdout, (char *)NULL);
114 #endif
115 			continue;
116 
117 		case 's':
118 
119 			/*
120 			 * The 's' option requests silent mode
121 			 * where no messages are written.
122 			 */
123 
124 			silent++;
125 			continue;
126 
127 		case 'v':
128 
129 			/*
130 			 * The 'v' option requests that non-printing
131 			 * characters (with the exception of newlines,
132 			 * form-feeds, and tabs) be displayed visibly.
133 			 *
134 			 * Control characters are printed as "^x".
135 			 * DEL characters are printed as "^?".
136 			 * Non-printable  and non-contrlol characters with the
137 			 * 8th bit set are printed as "M-x".
138 			 */
139 
140 			visi_mode++;
141 			continue;
142 
143 		case 't':
144 
145 			/*
146 			 * When in visi_mode, this option causes tabs
147 			 * to be displayed as "^I".
148 			 */
149 
150 			visi_tab++;
151 			continue;
152 
153 		case 'e':
154 
155 			/*
156 			 * When in visi_mode, this option causes newlines
157 			 * and form-feeds to be displayed as "$" at the end
158 			 * of the line prior to the newline.
159 			 */
160 
161 			visi_newline++;
162 			continue;
163 
164 		case 'b':
165 
166 			/*
167 			 * Precede each line output with its line number,
168 			 * but omit the line numbers from blank lines.
169 			 */
170 
171 			bflg++;
172 			nflg++;
173 			continue;
174 
175 		case 'n':
176 
177 			/*
178 			 * Precede each line output with its line number.
179 			 */
180 
181 			nflg++;
182 			continue;
183 
184 		case '?':
185 			errflg++;
186 			break;
187 		}
188 		break;
189 	}
190 
191 	if (errflg) {
192 		if (!silent)
193 			(void) fprintf(stderr,
194 			    gettext("usage: cat [ -usvtebn ] [-|file] ...\n"));
195 		exit(2);
196 	}
197 
198 	/*
199 	 * Stat stdout to be sure it is defined.
200 	 */
201 
202 	if (fstat(fileno(stdout), &target) < 0) {
203 		if (!silent)
204 			(void) fprintf(stderr,
205 			    gettext("cat: Cannot stat stdout\n"));
206 		exit(2);
207 	}
208 	obsize = target.st_blksize;
209 
210 	/*
211 	 * If no arguments given, then use stdin for input.
212 	 */
213 
214 	if (optind == argc) {
215 		argc++;
216 		stdinflg++;
217 	}
218 
219 	/*
220 	 * Process each remaining argument,
221 	 * unless there is an error with stdout.
222 	 */
223 
224 
225 	for (argv = &argv[optind];
226 	    optind < argc && !ferror(stdout); optind++, argv++) {
227 
228 		/*
229 		 * If the argument was '-' or there were no files
230 		 * specified, take the input from stdin.
231 		 */
232 
233 		if (stdinflg ||
234 		    ((*argv)[0] == '-' && (*argv)[1] == '\0'))
235 			fi = stdin;
236 		else {
237 			/*
238 			 * Attempt to open each specified file.
239 			 */
240 
241 			if ((fi = fopen(*argv, "r")) == NULL) {
242 				if (!silent)
243 				    (void) fprintf(stderr,
244 				    gettext("cat: cannot open %s: %s\n"),
245 					    *argv, strerror(errno));
246 				status = 2;
247 				continue;
248 			}
249 		}
250 
251 		/*
252 		 * Stat source to make sure it is defined.
253 		 */
254 
255 		if (fstat(fileno(fi), &source) < 0) {
256 			if (!silent)
257 				(void) fprintf(stderr,
258 				    gettext("cat: cannot stat %s: %s\n"),
259 				    (stdinflg) ? "-" : *argv, strerror(errno));
260 			status = 2;
261 			continue;
262 		}
263 
264 
265 		/*
266 		 * If the source is not a character special file, socket or a
267 		 * block special file, make sure it is not identical
268 		 * to the target.
269 		 */
270 
271 		if (!S_ISCHR(target.st_mode) &&
272 		    !S_ISBLK(target.st_mode) &&
273 		    !S_ISSOCK(target.st_mode) &&
274 		    IDENTICAL(target, source)) {
275 			if (!silent)
276 			    (void) fprintf(stderr,
277 			    gettext("cat: input/output files '%s' identical\n"),
278 				stdinflg?"-": *argv);
279 			if (fclose(fi) != 0)
280 				(void) fprintf(stderr,
281 				    gettext("cat: close error: %s\n"),
282 				    strerror(errno));
283 			status = 2;
284 			continue;
285 		}
286 		ibsize = source.st_blksize;
287 
288 		/*
289 		 * If in visible mode and/or nflg, use vncat;
290 		 * otherwise, use cat.
291 		 */
292 
293 		if (visi_mode || nflg)
294 			estatus = vncat(fi);
295 		else
296 			estatus = cat(fi, &source, &target,
297 			    fi != stdin ? *argv : "standard input");
298 
299 		if (estatus)
300 			status = estatus;
301 
302 		/*
303 		 * If the input is not stdin, close the source file.
304 		 */
305 
306 		if (fi != stdin) {
307 			if (fclose(fi) != 0)
308 				if (!silent)
309 					(void) fprintf(stderr,
310 					    gettext("cat: close error: %s\n"),
311 					    strerror(errno));
312 		}
313 	}
314 
315 	/*
316 	 * Display any error with stdout operations.
317 	 */
318 
319 	if (fclose(stdout) != 0) {
320 		if (!silent)
321 			perror(gettext("cat: close error"));
322 		status = 2;
323 	}
324 	return (status);
325 }
326 
327 
328 
329 static int
330 cat(FILE *fi, struct stat *statp, struct stat *outp, char *filenm)
331 {
332 	int nitems;
333 	int nwritten;
334 	int offset;
335 	int fi_desc;
336 	long buffsize;
337 	char *bufferp;
338 	off_t mapsize, munmapsize;
339 	off_t filesize;
340 	off_t mapoffset;
341 
342 	fi_desc = fileno(fi);
343 	if (S_ISREG(statp->st_mode) && (lseek(fi_desc, (off_t)0, SEEK_CUR)
344 	    == 0) && (statp->st_size > SMALLFILESIZE)) {
345 		mapsize = (off_t)MAXMAPSIZE;
346 		if (statp->st_size < mapsize)
347 			mapsize = statp->st_size;
348 		munmapsize = mapsize;
349 
350 		/*
351 		 * Mmap time!
352 		 */
353 		bufferp = mmap((caddr_t)NULL, (size_t)mapsize, PROT_READ,
354 			MAP_SHARED, fi_desc, (off_t)0);
355 		if (bufferp == (caddr_t)-1)
356 			mapsize = 0;	/* I guess we can't mmap today */
357 	} else
358 		mapsize = 0;		/* can't mmap non-regular files */
359 
360 	if (mapsize != 0) {
361 		int	read_error = 0;
362 		char	x;
363 
364 		/*
365 		 * NFS V2 will let root open a file it does not have permission
366 		 * to read. This read() is here to make sure that the access
367 		 * time on the input file will be updated. The VSC tests for
368 		 * cat do this:
369 		 *	cat file > /dev/null
370 		 * In this case the write()/mmap() pair will not read the file
371 		 * and the access time will not be updated.
372 		 */
373 
374 		if (read(fi_desc, &x, 1) == -1)
375 			read_error = 1;
376 		mapoffset = 0;
377 		filesize = statp->st_size;
378 		for (;;) {
379 			/*
380 			 * Note that on some systems (V7), very large writes to
381 			 * a pipe return less than the requested size of the
382 			 * write.  In this case, multiple writes are required.
383 			 */
384 			offset = 0;
385 			nitems = (int)mapsize;
386 			do {
387 				if ((nwritten = write(fileno(stdout),
388 				    &bufferp[offset], (size_t)nitems)) < 0) {
389 					if (!silent) {
390 						if (read_error == 1)
391 							(void) fprintf(
392 							    stderr, gettext(
393 							    "cat: cannot read "
394 							    "%s: "), filenm);
395 						else
396 							(void) fprintf(
397 							stderr, gettext(
398 							"cat: write error: "));
399 						perror("");
400 					}
401 					(void) munmap(bufferp,
402 						(size_t)munmapsize);
403 					(void) lseek(fi_desc, (off_t)mapoffset,
404 					    SEEK_SET);
405 					return (2);
406 				}
407 				offset += nwritten;
408 			} while ((nitems -= nwritten) > 0);
409 
410 			filesize -= mapsize;
411 			mapoffset += mapsize;
412 			if (filesize == 0)
413 				break;
414 			if (filesize < mapsize)
415 				mapsize = filesize;
416 			if (mmap(bufferp, (size_t)mapsize, PROT_READ,
417 			    MAP_SHARED|MAP_FIXED, fi_desc,
418 			    mapoffset) == (caddr_t)-1) {
419 				if (!silent)
420 					perror(gettext("cat: mmap error"));
421 				(void) munmap(bufferp, (size_t)munmapsize);
422 				(void) lseek(fi_desc, (off_t)mapoffset,
423 					SEEK_SET);
424 				return (1);
425 			}
426 		}
427 		/*
428 		 * Move the file pointer past what we read. Shell scripts
429 		 * rely on cat to do this, so that successive commands in
430 		 * the script won't re-read the same data.
431 		 */
432 		(void) lseek(fi_desc, (off_t)mapoffset, SEEK_SET);
433 		(void) munmap(bufferp, (size_t)munmapsize);
434 	} else {
435 		if (S_ISREG(statp->st_mode) && S_ISREG(outp->st_mode)) {
436 			bufferp = (char *)buf;
437 			buffsize = SMALLFILESIZE;
438 		} else {
439 			if (obsize)
440 				/*
441 				 * common case, use output blksize
442 				 */
443 				buffsize = obsize;
444 			else if (ibsize)
445 				buffsize = ibsize;
446 			else
447 				buffsize = (long)BUFSIZ;
448 
449 			if (buffsize <= SMALLFILESIZE) {
450 				bufferp = (char *)buf;
451 			} else if ((bufferp =
452 			    malloc((size_t)buffsize)) == NULL) {
453 				perror(gettext("cat: no memory"));
454 				return (1);
455 			}
456 		}
457 
458 		/*
459 		 * While not end of file, copy blocks to stdout.
460 		 */
461 		while ((nitems = read(fi_desc, bufferp, (size_t)buffsize)) >
462 		    0) {
463 			offset = 0;
464 			/*
465 			 * Note that on some systems (V7), very large writes
466 			 * to a pipe return less than the requested size of
467 			 * the write.  In this case, multiple writes are
468 			 * required.
469 			 */
470 			do {
471 				nwritten = write(1, bufferp+offset,
472 					(size_t)nitems);
473 				if (nwritten < 0) {
474 					if (!silent) {
475 						if (nwritten == -1)
476 							nwritten = 0l;
477 						(void) fprintf(stderr, gettext(\
478 "cat: output error (%d/%d characters written)\n"), nwritten, nitems);
479 						perror("");
480 					}
481 					if (bufferp != (char *)buf)
482 						free(bufferp);
483 					return (2);
484 				}
485 				offset += nwritten;
486 			} while ((nitems -= nwritten) > 0);
487 		}
488 		if (bufferp != (char *)buf)
489 			free(bufferp);
490 		if (nitems < 0) {
491 			(void) fprintf(stderr,
492 			    gettext("cat: input error on %s: "), filenm);
493 			perror("");
494 		}
495 	}
496 
497 	return (0);
498 }
499 
500 static int
501 vncat(fi)
502 	FILE *fi;
503 {
504 	int c;
505 	int	lno;
506 	int	boln;	/* = 1 if at beginning of line */
507 			/* = 0 otherwise */
508 	wchar_t	wc;
509 	int	len, n;
510 	unsigned char	*p1, *p2;
511 
512 	lno = 1;
513 	boln = 1;
514 	p1 = p2 = buf;
515 	for (;;) {
516 		if (p1 >= p2) {
517 			p1 = buf;
518 			if ((len = fread(p1, 1, BUFSIZ, fi)) <= 0)
519 				break;
520 			p2 = p1 + len;
521 		}
522 		c = *p1++;
523 
524 		/*
525 		 * Display newlines as "$<newline>"
526 		 * if visi_newline set
527 		 */
528 		if (c == '\n') {
529 			if (nflg && boln && !bflg)
530 				(void) printf("%6d\t", lno++);
531 			boln = 1;
532 
533 			if (visi_mode && visi_newline)
534 				(void) putchar('$');
535 			(void) putchar(c);
536 			continue;
537 		}
538 
539 		if (nflg && boln)
540 			(void) printf("%6d\t", lno++);
541 		boln = 0;
542 
543 		/*
544 		 * For non-printable and non-cntrl chars,
545 		 * use the "M-x" notation.
546 		 */
547 
548 		if (isascii(c)) {
549 			if (isprint(c) || visi_mode == 0) {
550 				(void) putchar(c);
551 				continue;
552 			}
553 
554 			/*
555 			 * For non-printable ascii characters.
556 			 */
557 
558 			if (iscntrl(c)) {
559 				/* For cntrl characters. */
560 				if ((c == '\t') || (c == '\f')) {
561 					/*
562 					 * Display tab as "^I" if visi_tab set
563 					 */
564 					if (visi_mode && visi_tab) {
565 						(void) putchar('^');
566 						(void) putchar(c^0100);
567 					} else
568 						(void) putchar(c);
569 					continue;
570 				}
571 				(void) putchar('^');
572 				(void) putchar(c^0100);
573 				continue;
574 			}
575 			continue;
576 		}
577 
578 		/*
579 		 * For non-ascii characters.
580 		 */
581 		p1--;
582 		if ((len = (p2 - p1)) < MB_LEN_MAX) {
583 			for (n = 0; n < len; n++)
584 				buf[n] = *p1++;
585 			p1 = buf;
586 			p2 = p1 + n;
587 			if ((len = fread(p2, 1, BUFSIZ - n, fi)) > 0)
588 				p2 += len;
589 		}
590 
591 		if ((len = (p2 - p1)) > MB_LEN_MAX)
592 			len = MB_LEN_MAX;
593 
594 		if ((len = mbtowc(&wc, (char *)p1, len)) > 0) {
595 			if (iswprint(wc) || visi_mode == 0) {
596 				(void) putwchar(wc);
597 				p1 += len;
598 				continue;
599 			}
600 		}
601 
602 		(void) putchar('M');
603 		(void) putchar('-');
604 		c -= 0200;
605 
606 		if (isprint(c)) {
607 			(void) putchar(c);
608 		}
609 
610 		/* For non-printable characters. */
611 		if (iscntrl(c)) {
612 			/* For cntrl characters. */
613 			if ((c == '\t') || (c == '\f')) {
614 				/*
615 				 * Display tab as "^I" if visi_tab set
616 				 */
617 				if (visi_mode && visi_tab) {
618 					(void) putchar('^');
619 					(void) putchar(c^0100);
620 				} else
621 					(void) putchar(c);
622 			} else {
623 				(void) putchar('^');
624 				(void) putchar(c^0100);
625 			}
626 		}
627 		p1++;
628 	}
629 	return (0);
630 }
631