|
|
1.1 root 1: #ifndef lint
2: static char *sccsid = "@(#)cat.c 4.6 (Berkeley) 8/11/83";
3: #endif
4:
5: /*
6: * Concatenate files.
7: */
8:
9: #include <stdio.h>
10: #include <sys/types.h>
11: #include <sys/stat.h>
12:
13: char stdbuf[BUFSIZ];
14: int bflg, eflg, nflg, sflg, tflg, vflg;
15: int spaced, col, lno, inline;
16:
17: main(argc, argv)
18: char **argv;
19: {
20: int fflg = 0;
21: register FILE *fi;
22: register c;
23: int dev, ino = -1;
24: struct stat statb;
25:
26: lno = 1;
27: setbuf(stdout, stdbuf);
28: for( ; argc>1 && argv[1][0]=='-'; argc--,argv++) {
29: switch(argv[1][1]) {
30: case 0:
31: break;
32: case 'u':
33: setbuf(stdout, (char *)NULL);
34: continue;
35: case 'n':
36: nflg++;
37: continue;
38: case 'b':
39: bflg++;
40: nflg++;
41: continue;
42: case 'v':
43: vflg++;
44: continue;
45: case 's':
46: sflg++;
47: continue;
48: case 'e':
49: eflg++;
50: vflg++;
51: continue;
52: case 't':
53: tflg++;
54: vflg++;
55: continue;
56: }
57: break;
58: }
59: if (fstat(fileno(stdout), &statb) == 0) {
60: statb.st_mode &= S_IFMT;
61: if (statb.st_mode!=S_IFCHR && statb.st_mode!=S_IFBLK) {
62: dev = statb.st_dev;
63: ino = statb.st_ino;
64: }
65: }
66: if (argc < 2) {
67: argc = 2;
68: fflg++;
69: }
70: while (--argc > 0) {
71: if (fflg || (*++argv)[0]=='-' && (*argv)[1]=='\0')
72: fi = stdin;
73: else {
74: if ((fi = fopen(*argv, "r")) == NULL) {
75: perror(*argv);
76: continue;
77: }
78: }
79: if (fstat(fileno(fi), &statb) == 0) {
80: if ((statb.st_mode & S_IFMT) == S_IFREG &&
81: statb.st_dev==dev && statb.st_ino==ino) {
82: fprintf(stderr, "cat: input %s is output\n",
83: fflg?"-": *argv);
84: fclose(fi);
85: continue;
86: }
87: }
88: if (nflg||sflg||vflg)
89: copyopt(fi);
90: else {
91: while ((c = getc(fi)) != EOF)
92: putchar(c);
93: }
94: if (fi!=stdin)
95: fclose(fi);
96: }
97: if (ferror(stdout))
98: fprintf(stderr, "cat: output write error\n");
99: return(0);
100: }
101:
102: copyopt(f)
103: register FILE *f;
104: {
105: register int c;
106:
107: top:
108: c = getc(f);
109: if (c == EOF)
110: return;
111: if (c == '\n') {
112: if (inline == 0) {
113: if (sflg && spaced)
114: goto top;
115: spaced = 1;
116: }
117: if (nflg && bflg==0 && inline == 0)
118: printf("%6d\t", lno++);
119: if (eflg)
120: putchar('$');
121: putchar('\n');
122: inline = 0;
123: goto top;
124: }
125: if (nflg && inline == 0)
126: printf("%6d\t", lno++);
127: inline = 1;
128: if (vflg) {
129: if (tflg==0 && c == '\t')
130: putchar(c);
131: else {
132: if (c > 0177) {
133: printf("M-");
134: c &= 0177;
135: }
136: if (c < ' ')
137: printf("^%c", c+'@');
138: else if (c == 0177)
139: printf("^?");
140: else
141: putchar(c);
142: }
143: } else
144: putchar(c);
145: spaced = 0;
146: goto top;
147: }
This archive runs on limited infrastructure. Preserving old code on modern bandwidth. Automated agents are requested to crawl responsibly.