|
|
1.1 root 1: /*
2: ** Concatenate files.
3: */
4:
5: #include <stdio.h>
6: #include <sys/types.h>
7: #include <sys/stat.h>
8:
9: /* character type table */
10:
11: #define PLAIN 0
12: #define CONTRL 1
13: #define TAB 2
14: #define BACKSP 3
15:
16: char ctype[128] = {
17: CONTRL, CONTRL, CONTRL, CONTRL, CONTRL, CONTRL, CONTRL, CONTRL,
18: CONTRL, TAB, PLAIN, CONTRL, TAB, CONTRL, CONTRL, CONTRL,
19: CONTRL, CONTRL, CONTRL, CONTRL, CONTRL, CONTRL, CONTRL, CONTRL,
20: CONTRL, CONTRL, CONTRL, CONTRL, CONTRL, CONTRL, CONTRL, CONTRL,
21: PLAIN, PLAIN, PLAIN, PLAIN, PLAIN, PLAIN, PLAIN, PLAIN,
22: PLAIN, PLAIN, PLAIN, PLAIN, PLAIN, PLAIN, PLAIN, PLAIN,
23: PLAIN, PLAIN, PLAIN, PLAIN, PLAIN, PLAIN, PLAIN, PLAIN,
24: PLAIN, PLAIN, PLAIN, PLAIN, PLAIN, PLAIN, PLAIN, PLAIN,
25: PLAIN, PLAIN, PLAIN, PLAIN, PLAIN, PLAIN, PLAIN, PLAIN,
26: PLAIN, PLAIN, PLAIN, PLAIN, PLAIN, PLAIN, PLAIN, PLAIN,
27: PLAIN, PLAIN, PLAIN, PLAIN, PLAIN, PLAIN, PLAIN, PLAIN,
28: PLAIN, PLAIN, PLAIN, PLAIN, PLAIN, PLAIN, PLAIN, PLAIN,
29: PLAIN, PLAIN, PLAIN, PLAIN, PLAIN, PLAIN, PLAIN, PLAIN,
30: PLAIN, PLAIN, PLAIN, PLAIN, PLAIN, PLAIN, PLAIN, PLAIN,
31: PLAIN, PLAIN, PLAIN, PLAIN, PLAIN, PLAIN, PLAIN, PLAIN,
32: PLAIN, PLAIN, PLAIN, PLAIN, PLAIN, PLAIN, PLAIN, CONTRL,
33: };
34: char stdbuf[BUFSIZ];
35: main(argc, argv)
36: char **argv;
37: {
38: register FILE *fi;
39: register c;
40: int fflg = 0;
41: int silent = 0;
42: int status = 0;
43: int tabs = 0;
44: int dev, ino = -1;
45: struct stat statb;
46:
47: setbuf(stdout, stdbuf);
48: for( ; argc>1 && argv[1][0]=='-'; argc--,argv++) {
49: switch(argv[1][1]) {
50: case 0:
51: break;
52: case 'u':
53: setbuf(stdout, (char *)NULL);
54: continue;
55: case 's':
56: silent++;
57: continue;
58: case 't':
59: tabs++;
60: continue;
61: }
62: break;
63: }
64: if(fstat(fileno(stdout), &statb) < 0) {
65: if(!silent)
66: fprintf(stderr, "cat: Cannot stat stdout\n");
67: exit(2);
68: }
69: statb.st_mode &= S_IFMT;
70: if (statb.st_mode!=S_IFCHR && statb.st_mode!=S_IFBLK) {
71: dev = statb.st_dev;
72: ino = statb.st_ino;
73: }
74: if (argc < 2) {
75: argc = 2;
76: fflg++;
77: }
78: while (--argc > 0) {
79: if ((*++argv)[0]=='-' && (*argv)[1]=='\0' || fflg)
80: fi = stdin;
81: else {
82: if ((fi = fopen(*argv, "r")) == NULL) {
83: if (!silent)
84: fprintf(stderr, "cat: cannot open %s\n", *argv);
85: status = 2;
86: continue;
87: }
88: }
89: if(fstat(fileno(fi), &statb) < 0) {
90: if(!silent)
91: fprintf(stderr, "cat: cannot stat %s\n", *argv);
92: status = 2;
93: continue;
94: }
95: if (statb.st_dev==dev && statb.st_ino==ino) {
96: if(!silent)
97: fprintf(stderr, "cat: input %s is output\n",
98: fflg?"-": *argv);
99: fclose(fi);
100: status = 2;
101: continue;
102: }
103: while ((c = getc(fi)) != EOF) {
104: if (c & 0200) {
105: putchar('M');
106: putchar('-');
107: c-= 0200;
108: }
109:
110: switch(ctype[c]) {
111: case PLAIN:
112: putchar(c);
113: break;
114: case TAB:
115: if (tabs) {
116: putchar(c);
117: } else {
118: putchar('^');
119: putchar (c^0100);
120: }
121: break;
122: case CONTRL:
123: putchar('^');
124: putchar(c^0100);
125: break;
126: }
127: }
128: if (fi!=stdin)
129: fclose(fi);
130: }
131: exit(status);
132: }
This archive runs on limited infrastructure. Preserving old code on modern bandwidth. Automated agents are requested to crawl responsibly.