-
Notifications
You must be signed in to change notification settings - Fork 3
/
cat0.c
158 lines (139 loc) · 2.76 KB
/
cat0.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
/* --------------------------------- cat -------------------------------- */
#include <stdio.h>
#include <string.h>
#define PC 1
#define ANSI 1
#if PC
#include <dos.h>
#include <dir.h>
#include <io.h>
#include <process.h>
#endif
#define TRACE 0
#if PC
struct ffblk ffblk;
char drive[MAXDRIVE];
char dir[MAXDIR];
char file[MAXFILE];
char ext[MAXEXT];
int flag;
#else
#define MAXPATH 4096
#endif
char *parmv;
char *parms;
char *parmm;
LONGX parml;
char *parmmc;
int lastchar='\n';
int done;
#if ANSI
void concat(FILE *fp);
#else
void concat();
#endif
void concat(fp)
FILE *fp;
{
int c;
LONGX countl;
countl=parml;
while ((c=fgetc(fp)) != EOF) {
if (c == '\n')
countl=parml;
else
if (!countl) {
putchar('\n');
countl=parml;
}
putchar(c); lastchar=c;
countl--;
}
}
void main(argc,argv)
int argc;
char *argv[];
{
int iarg;
char *argvp;
char path[MAXPATH];
char filename[MAXPATH];
FILE *fp;
char *p;
parmv=parms=parmm=parmmc=NULL; parml=999999999L;
if (argc == 1) {
concat(stdin);
exit(0);
}
for (iarg=1; iarg < argc; iarg++) {
argvp=argv[iarg];
#if TRACE
fprintf(stderr,"+++arg '%s'\n",argvp);
#endif
if (*argvp == '-') {
if (*(argvp+1) == 'l') {
sscanf(argvp+2,"%ld",&parml);
fprintf(stderr,"+++ f=%ld bytes\n",parml);
continue;
}
for (p=argvp+1; *p; p++)
switch (*p) {
case 'v':
parmv=p;
break;
case 's':
parms=p;
break;
case 'm':
parmm=p;
break;
case 'c':
parmmc=p;
break;
default:
fprintf(stderr,"*** invalid option: %s\n",p);
}
continue; /* next argv */
}
strcpy(path,argvp);
#if TRACE
fprintf(stderr,"+++path '%s'\n",path);
#endif
#if PC
flag=fnsplit(argvp,drive,dir,file,ext);
if (parms || TRACE)
fprintf(stderr,"+s+ drive=%s, dir=%s, file=%s, ext=%s\n",drive,dir,file,ext);
done=findfirst(path,&ffblk,0xFF);
while (!done) {
if (ffblk.ff_attrib & FA_ARCH) {
strcpy(filename,drive);
strcat(filename,dir);
strcat(filename,ffblk.ff_name);
#else
strcpy(filename,path);
#endif
if ((fp=fopen(filename,"r")) != NULL ) {
if (parmv || TRACE) fprintf(stderr,
#if PC
"+v+% 10ld bytes in %s\n",ffblk.ff_fsize,filename);
#else
"+v+ %s\n",filename);
#endif
if (parmm || parmmc || TRACE) {
if (lastchar != '\n') putchar('\n');
if (parmmc) printf("\f\n");
printf( "/******************************************************\n");
printf( "%s\n",filename);
printf( "******************************************************/\n");
}
concat(fp);
fclose(fp);
}
#if PC
}
done=findnext(&ffblk);
}
#endif
}
exit(0);
}