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