forked from abysamross/C_NIX_Programming
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathcnt.c
89 lines (62 loc) · 1.19 KB
/
cnt.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
#include <stdio.h>
#include <stdarg.h>
#include <unistd.h>
#include "mheader.h"
int main(int argc, char* argv[]) {
int opt;
int cflag = 0;
int lflag = 0;
FILE* fp = NULL;
char* sname = NULL;
while ((opt = getopt(argc, argv, "-lc")) != -1) {
switch (opt) {
case 'c':
cflag = 1;
break;
case 'l':
lflag = 1;
break;
case '?':
mprint(stderr, "Usage: %s -l|-c [<stream name>]\n", argv[0]);
goto error;
default:
if (sname) {
mprint(stderr, "Usage: %s -l|-c [<stream name>]\nInvalid argument argv[%d]: %s\n", argv[0], optind-1, optarg);
goto error;
}
else
sname = optarg;
break;
}
}
if(!lflag && !cflag && optind <= 2) {
mprint(stderr, "Usage: %s -l|-c [<stream name>]\n", argv[0]);
goto error;
}
if (sname) {
if (checkAccess(sname))
goto error;
if (openStream(sname, &fp))
goto error;
if (cflag)
charcount(1, fp);
fseek(fp, 0, SEEK_SET);
if (lflag)
linecount(1, fp);
if (closeStream(sname, fp))
goto error;
} else {
if (cflag)
charcount(0);
else if (lflag)
linecount(0);
/*
* if(lflag)
* freopen("/dev/tty", "r", stdin);
*}
*/
}
return 0;
error:
return 1;
}