|
|
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) {
1.1.1.2 ! root 71: if (fflg || (*++argv)[0]=='-' && (*argv)[1]=='\0') {
1.1 root 72: fi = stdin;
1.1.1.2 ! root 73: }
1.1 root 74: else {
75: if ((fi = fopen(*argv, "r")) == NULL) {
76: perror(*argv);
77: continue;
78: }
79: }
80: if (fstat(fileno(fi), &statb) == 0) {
81: if ((statb.st_mode & S_IFMT) == S_IFREG &&
82: statb.st_dev==dev && statb.st_ino==ino) {
83: fprintf(stderr, "cat: input %s is output\n",
84: fflg?"-": *argv);
85: fclose(fi);
86: continue;
87: }
88: }
89: if (nflg||sflg||vflg)
90: copyopt(fi);
91: else {
92: while ((c = getc(fi)) != EOF)
93: putchar(c);
94: }
95: if (fi!=stdin)
96: fclose(fi);
1.1.1.2 ! root 97: else rewind(stdin);
1.1 root 98: }
99: if (ferror(stdout))
100: fprintf(stderr, "cat: output write error\n");
101: return(0);
102: }
103:
104: copyopt(f)
105: register FILE *f;
106: {
107: register int c;
108:
109: top:
110: c = getc(f);
111: if (c == EOF)
112: return;
113: if (c == '\n') {
114: if (inline == 0) {
115: if (sflg && spaced)
116: goto top;
117: spaced = 1;
118: }
119: if (nflg && bflg==0 && inline == 0)
120: printf("%6d\t", lno++);
121: if (eflg)
122: putchar('$');
123: putchar('\n');
124: inline = 0;
125: goto top;
126: }
127: if (nflg && inline == 0)
128: printf("%6d\t", lno++);
129: inline = 1;
130: if (vflg) {
131: if (tflg==0 && c == '\t')
132: putchar(c);
133: else {
134: if (c > 0177) {
135: printf("M-");
136: c &= 0177;
137: }
138: if (c < ' ')
139: printf("^%c", c+'@');
140: else if (c == 0177)
141: printf("^?");
142: else
143: putchar(c);
144: }
145: } else
146: putchar(c);
147: spaced = 0;
148: goto top;
149: }
This archive runs on limited infrastructure. Preserving old code on modern bandwidth. Automated agents are requested to crawl responsibly.