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