-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmycat.c
50 lines (41 loc) · 826 Bytes
/
mycat.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
#include <stdio.h>
#include <string.h>
#define BUFSIZE 1000
char buffer[BUFSIZE+1];
int lineno = 0;
void cat(FILE* f, int lines) {
fgets(buffer, BUFSIZE, f);
while(!feof(f)) {
if(lines) {
printf("%6d ", ++lineno);
}
printf("%s", buffer);
fgets(buffer, BUFSIZE, f);
}
}
int main(int argc, char* argv[]) {
FILE* f;
int i;
int lines = 0;
int wasfile = 0;
int input = 1;
for(i = 1; i < argc; i++) {
if(strcmp(argv[i], "-n") == 0) { lines = 1; }
}
if(strcmp(argv[1], "--") == 0) { input = 0; }
for(i = 1; i < argc; i++) {
if(input){
if(argv[i][0] == '-') { cat(stdin, lines); }
}
f = fopen(argv[i], "r");
if(f != NULL) {
wasfile = 1;
cat(f, lines);
fclose(f);
}
}
if(!wasfile) {
cat(stdin, lines);
}
return 0;
}