-
Notifications
You must be signed in to change notification settings - Fork 9
/
Copy pathcatg.c
175 lines (148 loc) · 3.95 KB
/
catg.c
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
/* catg.c version 1.3; B D McKay, Jan 2013 */
#define USAGE "catg [-xv] [infile]..."
#define HELPTEXT \
" Copy files to stdout with all but the first header removed.\n\
\n\
-x Don't write a header.\n\
In the absence of -x, a header is written if\n\
there is one in the first input file.\n\
\n\
-v Summarize to stderr.\n"
#include "gtools.h"
/***********************************************************************/
static FILE*
openfile_head(char *filename, char **header)
/* Open the file for reading and return a FILE*. If open fails, or
the header is malformed, write a message and return NULL. Set
*header to point to the header string (statically allocated).
A filename of NULL or "-" means stdin. */
{
int c,i;
char *actname;
FILE *f;
DYNALLSTAT(char,head,head_sz);
if (filename == NULL || strcmp(filename,"-") == 0)
{
f = stdin;
actname = "stdin";
}
else
{
f = fopen(filename,"r");
actname = filename;
}
if (f == NULL)
{
fprintf(stderr,">E catg: can't open file %s\n",actname);
return NULL;
}
DYNALLOC1(char,head,head_sz,100,"catg");
c = getc(f);
if (c == '>')
{
i = 0;
head[i++] = (char)c;
c = getc(f);
if (c != '>')
{
fprintf(stderr,">E catg: bad header in %s\n",actname);
fclose(f);
return NULL;
}
head[i++] = (char)c;
do
{
c = getc(f);
if (c == EOF)
{
fprintf(stderr,">E catg: bad header in %s\n",actname);
fclose(f);
return NULL;
}
if (i >= head_sz-1)
DYNREALLOC(char,head,head_sz,head_sz+100,"catg");
head[i++] = (char)c;
}
while (c != '<' || head[i-2] != '<');
head[i] = '\0';
}
else
{
ungetc(c,f);
head[0] = '\0';
}
*header = head;
return f;
}
/***********************************************************************/
int
main(int argc, char *argv[])
{
char *infilename,*head;
FILE *infile;
int nfiles,i,j;
char *arg,sw;
boolean vswitch,xswitch;
boolean badargs;
char buff[1024];
size_t nr;
DYNALLSTAT(char*,filename,filename_sz);
HELP; PUTVERSION;
DYNALLOC1(char*,filename,filename_sz,200,"catg");
xswitch = vswitch = FALSE;
nfiles = 0;
badargs = FALSE;
for (j = 1; !badargs && j < argc; ++j)
{
arg = argv[j];
if (arg[0] == '-' && arg[1] != '\0')
{
++arg;
while (*arg != '\0')
{
sw = *arg++;
SWBOOLEAN('v',vswitch)
else SWBOOLEAN('x',xswitch)
else badargs = TRUE;
}
}
else
{
if (nfiles >= filename_sz)
DYNREALLOC(char*,filename,filename_sz,
filename_sz+200,"catg");
filename[nfiles++] = arg;
}
}
if (badargs)
{
fprintf(stderr,">E Usage: %s\n",USAGE);
GETHELP;
exit(1);
}
for (i = 0; i < (nfiles == 0 ? 1 : nfiles); ++i)
{
infilename = (nfiles == 0 ? NULL : filename[i]);
infile = openfile_head(infilename,&head);
if (infile == NULL) ABORT("catg");
if (i == 0 && !xswitch) fprintf(stdout,"%s",head);
while ((nr = fread(buff,1,1024,infile)) > 0)
{
fwrite(buff,1,nr,stdout);
if (ferror(stdout))
{
fprintf(stderr,">E catg: error in writing to stdout\n");
ABORT("catg");
}
}
if (ferror(infile))
{
fprintf(stderr,">E catg: error in reading from %s\n",
infilename == NULL ? "stdin" : infilename);
ABORT("catg");
}
fclose(infile);
}
if (vswitch) { }
exit(0);
}