2020-06-16 16:52:17 +00:00
|
|
|
#include <stdio.h>
|
2020-07-28 10:46:55 +00:00
|
|
|
#include <ctype.h>
|
2020-07-28 12:26:08 +00:00
|
|
|
#include <getopt.h>
|
|
|
|
#include <stdlib.h>
|
2020-06-16 16:52:17 +00:00
|
|
|
|
2020-08-23 08:58:30 +00:00
|
|
|
int show_lines, show_words, show_bytes;
|
|
|
|
struct wc_values data;
|
2020-07-29 20:49:00 +00:00
|
|
|
|
2020-07-28 12:26:08 +00:00
|
|
|
struct wc_values
|
|
|
|
{
|
|
|
|
int lines;
|
|
|
|
int bytes;
|
|
|
|
int words;
|
|
|
|
};
|
|
|
|
|
|
|
|
struct wc_values
|
2020-06-16 19:28:56 +00:00
|
|
|
wc(FILE *file)
|
2020-06-16 16:52:17 +00:00
|
|
|
{
|
2020-07-28 12:26:08 +00:00
|
|
|
if(file == NULL)
|
|
|
|
{
|
2020-12-05 14:26:36 +00:00
|
|
|
fprintf(stderr,"error opening file\n");
|
2020-07-28 12:26:08 +00:00
|
|
|
}
|
|
|
|
struct wc_values foobar;
|
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++;
|
|
|
|
}
|
2020-07-28 12:26:08 +00:00
|
|
|
foobar.bytes = bytes;
|
|
|
|
foobar.lines = newlines;
|
|
|
|
foobar.words = spaces;
|
|
|
|
|
2020-06-16 19:28:56 +00:00
|
|
|
fclose(file);
|
2020-07-28 12:26:08 +00:00
|
|
|
return foobar;
|
2020-06-16 19:28:56 +00:00
|
|
|
}
|
2020-08-23 08:58:30 +00:00
|
|
|
void
|
2020-08-28 22:20:17 +00:00
|
|
|
print_values(const char*filename)
|
2020-08-23 08:58:30 +00:00
|
|
|
{
|
|
|
|
if(show_bytes && show_lines && show_words)
|
|
|
|
printf("%i %i %i",
|
|
|
|
data.lines,
|
|
|
|
data.words,
|
|
|
|
data.bytes);
|
|
|
|
else
|
|
|
|
{
|
|
|
|
if(!show_lines)
|
|
|
|
printf("%i ",data.lines);
|
|
|
|
if(!show_words)
|
|
|
|
printf("%i ",data.words);
|
|
|
|
if(!show_bytes)
|
|
|
|
printf("%i ",data.bytes);
|
|
|
|
}
|
2020-08-28 22:20:17 +00:00
|
|
|
printf(" %s\n",filename);
|
2020-08-23 08:58:30 +00:00
|
|
|
}
|
2020-06-16 19:28:56 +00:00
|
|
|
int
|
|
|
|
main(int argc, char *argv[])
|
|
|
|
{
|
2020-07-29 20:49:00 +00:00
|
|
|
int c;
|
2020-08-23 08:58:30 +00:00
|
|
|
|
2020-07-30 12:07:24 +00:00
|
|
|
show_lines = show_words = show_bytes = 1;
|
2020-07-29 20:49:00 +00:00
|
|
|
/* Process arguments */
|
|
|
|
|
2020-08-23 09:01:36 +00:00
|
|
|
while((c = getopt(argc,argv,"lwcm")) > 0)
|
2020-07-29 20:49:00 +00:00
|
|
|
{
|
|
|
|
switch(c)
|
2020-07-30 11:59:34 +00:00
|
|
|
{
|
|
|
|
case 'l':
|
2020-07-30 12:07:24 +00:00
|
|
|
show_lines = 0;
|
2020-07-30 11:59:34 +00:00
|
|
|
break;
|
|
|
|
case 'w':
|
2020-07-30 12:07:24 +00:00
|
|
|
show_words = 0;
|
2020-07-30 11:59:34 +00:00
|
|
|
break;
|
|
|
|
case 'c':
|
2020-08-24 00:31:35 +00:00
|
|
|
case 'm':
|
2020-07-30 12:07:24 +00:00
|
|
|
show_bytes = 0;
|
2020-08-24 00:31:35 +00:00
|
|
|
break;
|
2020-08-23 09:01:36 +00:00
|
|
|
|
2020-07-30 11:59:34 +00:00
|
|
|
}
|
2020-07-29 20:49:00 +00:00
|
|
|
}
|
2020-08-23 08:58:30 +00:00
|
|
|
|
|
|
|
if(optind == argc)
|
2020-06-16 19:28:56 +00:00
|
|
|
{
|
2020-08-23 08:58:30 +00:00
|
|
|
data = wc(stdin);
|
2020-08-28 22:20:17 +00:00
|
|
|
print_values("stdin");
|
2020-06-16 19:28:56 +00:00
|
|
|
}
|
2020-08-23 08:58:30 +00:00
|
|
|
else for(int i = optind; i<argc; i++)
|
|
|
|
{
|
2020-12-05 14:26:36 +00:00
|
|
|
if(argv[i][0] == '-' && argv[i][1] == '\0')
|
2020-08-24 00:31:35 +00:00
|
|
|
data = wc(stdin);
|
|
|
|
else
|
|
|
|
data = wc(fopen(argv[i],"r"));
|
2020-08-28 22:20:17 +00:00
|
|
|
print_values(argv[i]);
|
2020-08-23 08:58:30 +00:00
|
|
|
}
|
2020-06-16 16:52:17 +00:00
|
|
|
return 0;
|
|
|
|
}
|