1 |
#ifndef lint |
2 |
static const char RCSid[] = "$Id: lam.c,v 1.13 2014/02/11 17:05:00 greg Exp $"; |
3 |
#endif |
4 |
/* |
5 |
* lam.c - simple program to laminate files. |
6 |
* |
7 |
* 7/14/88 Greg Ward |
8 |
*/ |
9 |
|
10 |
#include <stdlib.h> |
11 |
#include <string.h> |
12 |
#include <stdio.h> |
13 |
#include <ctype.h> |
14 |
|
15 |
#include "platform.h" |
16 |
#include "rtprocess.h" |
17 |
|
18 |
#define MAXFILE 512 /* maximum number of files */ |
19 |
|
20 |
#define MAXLINE 65536 /* maximum input line */ |
21 |
|
22 |
FILE *input[MAXFILE]; |
23 |
int bytsiz[MAXFILE]; |
24 |
char *tabc[MAXFILE]; |
25 |
int nfiles; |
26 |
|
27 |
char buf[MAXLINE]; |
28 |
|
29 |
int |
30 |
main(argc, argv) |
31 |
int argc; |
32 |
char *argv[]; |
33 |
{ |
34 |
int unbuff = 0; |
35 |
int binout = 0; |
36 |
int i; |
37 |
char *curtab; |
38 |
int curbytes; |
39 |
int puteol; |
40 |
|
41 |
curtab = "\t"; |
42 |
curbytes = 0; |
43 |
nfiles = 0; |
44 |
for (i = 1; i < argc; i++) { |
45 |
if (argv[i][0] == '-') { |
46 |
switch (argv[i][1]) { |
47 |
case 't': |
48 |
curtab = argv[i]+2; |
49 |
break; |
50 |
case 'u': |
51 |
unbuff = !unbuff; |
52 |
break; |
53 |
case 'i': |
54 |
switch (argv[i][2]) { |
55 |
case 'f': |
56 |
curbytes = sizeof(float); |
57 |
break; |
58 |
case 'd': |
59 |
curbytes = sizeof(double); |
60 |
break; |
61 |
case 'i': |
62 |
curbytes = sizeof(int); |
63 |
break; |
64 |
case 'w': |
65 |
curbytes = 2; |
66 |
break; |
67 |
case 'b': |
68 |
curbytes = 1; |
69 |
break; |
70 |
case 'a': |
71 |
curbytes = argv[i][3] ? -1 : 0; |
72 |
break; |
73 |
default: |
74 |
goto badopt; |
75 |
} |
76 |
if (isdigit(argv[i][3])) |
77 |
curbytes *= atoi(argv[i]+3); |
78 |
if (abs(curbytes) > MAXLINE) { |
79 |
fputs(argv[0], stderr); |
80 |
fputs(": input size too big\n", stderr); |
81 |
exit(1); |
82 |
} |
83 |
if (curbytes) { |
84 |
curtab = ""; |
85 |
++binout; |
86 |
} |
87 |
break; |
88 |
case '\0': |
89 |
tabc[nfiles] = curtab; |
90 |
input[nfiles] = stdin; |
91 |
if (curbytes > 0) |
92 |
SET_FILE_BINARY(input[nfiles]); |
93 |
else |
94 |
curbytes = -curbytes; |
95 |
bytsiz[nfiles++] = curbytes; |
96 |
break; |
97 |
badopt:; |
98 |
default: |
99 |
fputs(argv[0], stderr); |
100 |
fputs(": bad option\n", stderr); |
101 |
exit(1); |
102 |
} |
103 |
} else if (argv[i][0] == '!') { |
104 |
tabc[nfiles] = curtab; |
105 |
if ((input[nfiles] = popen(argv[i]+1, "r")) == NULL) { |
106 |
fputs(argv[i], stderr); |
107 |
fputs(": cannot start command\n", stderr); |
108 |
exit(1); |
109 |
} |
110 |
if (curbytes > 0) |
111 |
SET_FILE_BINARY(input[nfiles]); |
112 |
else |
113 |
curbytes = -curbytes; |
114 |
bytsiz[nfiles++] = curbytes; |
115 |
} else { |
116 |
tabc[nfiles] = curtab; |
117 |
if ((input[nfiles] = fopen(argv[i], "r")) == NULL) { |
118 |
fputs(argv[i], stderr); |
119 |
fputs(": cannot open file\n", stderr); |
120 |
exit(1); |
121 |
} |
122 |
if (curbytes > 0) |
123 |
SET_FILE_BINARY(input[nfiles]); |
124 |
else |
125 |
curbytes = -curbytes; |
126 |
bytsiz[nfiles++] = curbytes; |
127 |
} |
128 |
if (nfiles >= MAXFILE) { |
129 |
fputs(argv[0], stderr); |
130 |
fputs(": too many input streams\n", stderr); |
131 |
exit(1); |
132 |
} |
133 |
} |
134 |
if (binout) /* binary output? */ |
135 |
SET_FILE_BINARY(stdout); |
136 |
#ifdef getc_unlocked /* avoid lock/unlock overhead */ |
137 |
for (i = nfiles; i--; ) |
138 |
flockfile(input[i]); |
139 |
flockfile(stdout); |
140 |
#endif |
141 |
puteol = 0; /* check for ASCII output */ |
142 |
for (i = nfiles; i--; ) |
143 |
if (!bytsiz[i] || isprint(tabc[i][0]) || tabc[i][0] == '\t') { |
144 |
puteol++; |
145 |
break; |
146 |
} |
147 |
for ( ; ; ) { /* main loop */ |
148 |
for (i = 0; i < nfiles; i++) { |
149 |
if (bytsiz[i]) { /* binary file */ |
150 |
if (fread(buf, bytsiz[i], 1, input[i]) < 1) |
151 |
break; |
152 |
if (i) |
153 |
fputs(tabc[i], stdout); |
154 |
fwrite(buf, bytsiz[i], 1, stdout); |
155 |
} else { |
156 |
if (fgets(buf, MAXLINE, input[i]) == NULL) |
157 |
break; |
158 |
if (i) |
159 |
fputs(tabc[i], stdout); |
160 |
buf[strlen(buf)-1] = '\0'; |
161 |
fputs(buf, stdout); |
162 |
} |
163 |
} |
164 |
if (i < nfiles) |
165 |
break; |
166 |
if (puteol) |
167 |
putchar('\n'); |
168 |
if (unbuff) |
169 |
fflush(stdout); |
170 |
} |
171 |
return(0); |
172 |
} |