k9core/src/wc.c

37 lines
514 B
C
Raw Normal View History

2020-06-16 16:52:17 +00:00
#include <stdio.h>
2020-07-28 10:46:55 +00:00
#include <ctype.h>
2020-06-16 16:52:17 +00:00
2020-06-16 19:28:56 +00:00
void
wc(FILE *file)
2020-06-16 16:52:17 +00:00
{
char c;
2020-06-16 19:28:56 +00:00
int newlines, spaces, bytes = 0;
newlines = spaces = bytes = 0;
while((c = fgetc(file)) > 0)
2020-06-16 16:52:17 +00:00
{
bytes++;
if(c == '\n')
newlines++;
2020-07-28 10:46:55 +00:00
if(isspace(c))
2020-06-16 16:52:17 +00:00
spaces++;
}
printf("%i %i %i\n",newlines,spaces,bytes);
2020-06-16 19:28:56 +00:00
fclose(file);
}
int
main(int argc, char *argv[])
{
if (argc == 1)
wc(stdin);
else
{
for(int i = 1; i<argc;i++)
{
wc(fopen(argv[i],"r"));
}
}
2020-06-16 16:52:17 +00:00
return 0;
}