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