xref: /titanic_52/usr/src/cmd/cat/cat.c (revision ba4e3c84e6b9390bbf7df80b5f1d11dec34cc525)
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 2005 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 (S_ISREG(statp->st_mode) && S_ISREG(outp->st_mode)) {
432 			bufferp = (char *)buf;
433 			buffsize = SMALLFILESIZE;
434 		} else {
435 			if (obsize)
436 				/*
437 				 * common case, use output blksize
438 				 */
439 				buffsize = obsize;
440 			else if (ibsize)
441 				buffsize = ibsize;
442 			else
443 				buffsize = (long)BUFSIZ;
444 
445 			if (buffsize <= SMALLFILESIZE) {
446 				bufferp = (char *)buf;
447 			} else if ((bufferp =
448 			    malloc((size_t)buffsize)) == NULL) {
449 				perror(gettext("cat: no memory"));
450 				return (1);
451 			}
452 		}
453 
454 		/*
455 		 * While not end of file, copy blocks to stdout.
456 		 */
457 		while ((nitems = read(fi_desc, bufferp, (size_t)buffsize)) >
458 		    0) {
459 			offset = 0;
460 			/*
461 			 * Note that on some systems (V7), very large writes
462 			 * to a pipe return less than the requested size of
463 			 * the write.  In this case, multiple writes are
464 			 * required.
465 			 */
466 			do {
467 				nwritten = write(1, bufferp+offset,
468 					(size_t)nitems);
469 				if (nwritten < 0) {
470 					if (!silent) {
471 						if (nwritten == -1)
472 							nwritten = 0l;
473 						(void) fprintf(stderr, gettext(\
474 "cat: output error (%d/%d characters written)\n"), nwritten, nitems);
475 						perror("");
476 					}
477 					if (bufferp != (char *)buf)
478 						free(bufferp);
479 					return (2);
480 				}
481 				offset += nwritten;
482 			} while ((nitems -= nwritten) > 0);
483 		}
484 		if (bufferp != (char *)buf)
485 			free(bufferp);
486 		if (nitems < 0) {
487 			(void) fprintf(stderr,
488 			    gettext("cat: input error on %s: "), filenm);
489 			perror("");
490 		}
491 	}
492 
493 	return (0);
494 }
495 
496 static int
497 vncat(fi)
498 	FILE *fi;
499 {
500 	int c;
501 	int	lno;
502 	int	boln;	/* = 1 if at beginning of line */
503 			/* = 0 otherwise */
504 	wchar_t	wc;
505 	int	len, n;
506 	unsigned char	*p1, *p2;
507 
508 	lno = 1;
509 	boln = 1;
510 	p1 = p2 = buf;
511 	for (;;) {
512 		if (p1 >= p2) {
513 			p1 = buf;
514 			if ((len = fread(p1, 1, BUFSIZ, fi)) <= 0)
515 				break;
516 			p2 = p1 + len;
517 		}
518 		c = *p1++;
519 
520 		/*
521 		 * Display newlines as "$<newline>"
522 		 * if visi_newline set
523 		 */
524 		if (c == '\n') {
525 			if (nflg && boln && !bflg)
526 				(void) printf("%6d\t", lno++);
527 			boln = 1;
528 
529 			if (visi_mode && visi_newline)
530 				(void) putchar('$');
531 			(void) putchar(c);
532 			continue;
533 		}
534 
535 		if (nflg && boln)
536 			(void) printf("%6d\t", lno++);
537 		boln = 0;
538 
539 		/*
540 		 * For non-printable and non-cntrl chars,
541 		 * use the "M-x" notation.
542 		 */
543 
544 		if (isascii(c)) {
545 			if (isprint(c) || visi_mode == 0) {
546 				(void) putchar(c);
547 				continue;
548 			}
549 
550 			/*
551 			 * For non-printable ascii characters.
552 			 */
553 
554 			if (iscntrl(c)) {
555 				/* For cntrl characters. */
556 				if ((c == '\t') || (c == '\f')) {
557 					/*
558 					 * Display tab as "^I" if visi_tab set
559 					 */
560 					if (visi_mode && visi_tab) {
561 						(void) putchar('^');
562 						(void) putchar(c^0100);
563 					} else
564 						(void) putchar(c);
565 					continue;
566 				}
567 				(void) putchar('^');
568 				(void) putchar(c^0100);
569 				continue;
570 			}
571 			continue;
572 		}
573 
574 		/*
575 		 * For non-ascii characters.
576 		 */
577 		p1--;
578 		if ((len = (p2 - p1)) < MB_LEN_MAX) {
579 			for (n = 0; n < len; n++)
580 				buf[n] = *p1++;
581 			p1 = buf;
582 			p2 = p1 + n;
583 			if ((len = fread(p2, 1, BUFSIZ - n, fi)) > 0)
584 				p2 += len;
585 		}
586 
587 		if ((len = (p2 - p1)) > MB_LEN_MAX)
588 			len = MB_LEN_MAX;
589 
590 		if ((len = mbtowc(&wc, (char *)p1, len)) > 0) {
591 			if (iswprint(wc) || visi_mode == 0) {
592 				(void) putwchar(wc);
593 				p1 += len;
594 				continue;
595 			}
596 		}
597 
598 		(void) putchar('M');
599 		(void) putchar('-');
600 		c -= 0200;
601 
602 		if (isprint(c)) {
603 			(void) putchar(c);
604 		}
605 
606 		/* For non-printable characters. */
607 		if (iscntrl(c)) {
608 			/* For cntrl characters. */
609 			if ((c == '\t') || (c == '\f')) {
610 				/*
611 				 * Display tab as "^I" if visi_tab set
612 				 */
613 				if (visi_mode && visi_tab) {
614 					(void) putchar('^');
615 					(void) putchar(c^0100);
616 				} else
617 					(void) putchar(c);
618 			} else {
619 				(void) putchar('^');
620 				(void) putchar(c^0100);
621 			}
622 		}
623 		p1++;
624 	}
625 	return (0);
626 }
627