-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathcounter.c
113 lines (94 loc) · 1.9 KB
/
counter.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
#include <stdlib.h>
#include <stdio.h>
#include <stdarg.h>
#include "common.h"
/* Current file counters: chars, words, lines */
count_t ccount;
count_t wcount;
count_t lcount;
/* Totals counters: chars, words, lines */
count_t total_ccount = 0;
count_t total_wcount = 0;
count_t total_lcount = 0;
#define COUNT(c) \
ccount++; \
if ((c) == '\n') \
lcount++;
/* Print error message and exit with error status. If PERR is not 0,
display current errno status. */
static void
error_print (int perr, char *fmt, va_list ap)
{
vfprintf (stderr, fmt, ap);
if (perr)
perror (" ");
else
fprintf (stderr, "\n");
exit (1);
}
/* Print error message and exit with error status. */
static void
errf (char *fmt, ...)
{
va_list ap;
va_start (ap, fmt);
error_print (0, fmt, ap);
va_end (ap);
}
/* Print error message followed by errno status and exit
with error code. */
static void
perrf (char *fmt, ...)
{
va_list ap;
va_start (ap, fmt);
error_print (1, fmt, ap);
va_end (ap);
}
/* Return true if C is a valid word constituent */
static int
isword (unsigned char c)
{
return isalpha (c);
}
/* Get next word from the input stream. Return 0 on end
of file or error condition. Return 1 otherwise. */
int
getword (FILE *fp)
{
int c;
int word = 0;
if (feof (fp))
return 0;
while ((c = getc (fp)) != EOF)
{
if (isword (c))
{
wcount++;
break;
}
COUNT (c);
}
for (; c != EOF; c = getc (fp))
{
COUNT (c);
if (!isword (c))
break;
}
return c != EOF;
}
/* Process file FILE. */
int counter (char *file)
{
FILE *fp = fopen (file, "r");
if (!fp)
perrf ("cannot open file `%s'", file);
ccount = wcount = lcount = 0;
while (getword (fp))
;
fclose (fp);
total_ccount += ccount;
total_wcount += wcount;
total_lcount += lcount;
return (lcount);
}