What the fuck is this

This commit is contained in:
qorg11 2020-07-28 14:26:08 +02:00
parent c0935a1b07
commit 371c5f0803
No known key found for this signature in database
GPG Key ID: 343FC20A4ACA62B9
1 changed files with 44 additions and 8 deletions

View File

@ -1,9 +1,24 @@
#include <stdio.h>
#include <ctype.h>
#include <getopt.h>
#include <stdlib.h>
void
struct wc_values
{
int lines;
int bytes;
int words;
};
struct wc_values
wc(FILE *file)
{
if(file == NULL)
{
fprintf(stderr,"error\n");
exit(1);
}
struct wc_values foobar;
char c;
int newlines, spaces, bytes = 0;
newlines = spaces = bytes = 0;
@ -15,22 +30,43 @@ wc(FILE *file)
if(isspace(c))
spaces++;
}
printf("%i %i %i\n",newlines,spaces,bytes);
foobar.bytes = bytes;
foobar.lines = newlines;
foobar.words = spaces;
// printf("%i %i %i\n",newlines,spaces,bytes);
fclose(file);
return foobar;
}
int
main(int argc, char *argv[])
{
int c = getopt(argc, argv, "clw");
struct wc_values data;
if (argc == 1)
wc(stdin);
data = wc(stdin);
else
{
for(int i = 1; i<argc;i++)
{
wc(fopen(argv[i],"r"));
}
for(int i = optind; i<argc;i++)
{
data = wc(fopen(argv[i],"r"));
switch(c)
{
case 'c':
printf("%i\n",data.bytes);
break;
case 'l':
printf("%i\n",data.lines);
break;
case 'w':
printf("%i\n",data.words);
break;
default:
printf("%i %i %i\n",data.lines,data.words,data.bytes);
}
}
}
return 0;
}