-
Notifications
You must be signed in to change notification settings - Fork 38
Expand file tree
/
Copy pathwc-l-stdio.c
More file actions
48 lines (42 loc) · 802 Bytes
/
wc-l-stdio.c
File metadata and controls
48 lines (42 loc) · 802 Bytes
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
#include <stdio.h>
#include <stdlib.h>
static void do_wc_l(FILE *f);
int
main(int argc, char *argv[])
{
if (argc == 1) {
do_wc_l(stdin);
} else {
int i;
for (i = 1; i < argc; i++) {
FILE *f;
f = fopen(argv[i], "r");
if (!f) {
perror(argv[i]);
exit(1);
}
do_wc_l(f);
fclose(f);
}
}
exit(0);
}
static void
do_wc_l(FILE *f)
{
unsigned long n;
int c;
int prev = '\n'; /* '\n' is for empty file */
n = 0;
while ((c = getc(f)) != EOF) {
if (c == '\n') {
n++;
}
prev = c;
}
if (prev != '\n') {
/* missing '\n' at the end of file */
n++;
}
printf("%lu\n", n);
}