Recebendo de stdin e vários arquivos como argumentos
This commit is contained in:
parent
d4e47bf489
commit
588ea34c6b
1 changed files with 45 additions and 18 deletions
33
wc.c
33
wc.c
|
@ -2,10 +2,19 @@
|
||||||
#include <stdlib.h>
|
#include <stdlib.h>
|
||||||
#include <string.h>
|
#include <string.h>
|
||||||
|
|
||||||
int main(int argc, char **argv) {
|
int intlen(int number) {
|
||||||
|
// return ceil(log10(number+1));
|
||||||
|
char buffer[10];
|
||||||
|
sprintf(buffer, "%d", number);
|
||||||
|
return strlen(buffer);
|
||||||
|
}
|
||||||
|
|
||||||
|
void wc(char **list, int count) {
|
||||||
char buffer[BUFSIZ];
|
char buffer[BUFSIZ];
|
||||||
|
int clines = 0, cwords = 0, cbytes = 0;
|
||||||
|
for (int i = 1; i < count; i++) {
|
||||||
int lines = 0, words = 0, bytes = 0;
|
int lines = 0, words = 0, bytes = 0;
|
||||||
FILE *stream = fopen(argv[1], "r");
|
FILE *stream = fopen(list[i], "r");
|
||||||
if (stream == NULL) {
|
if (stream == NULL) {
|
||||||
perror("Erro ao abrir o arquivo");
|
perror("Erro ao abrir o arquivo");
|
||||||
exit(EXIT_FAILURE);
|
exit(EXIT_FAILURE);
|
||||||
|
@ -21,7 +30,25 @@ int main(int argc, char **argv) {
|
||||||
/* lines */
|
/* lines */
|
||||||
lines++;
|
lines++;
|
||||||
}
|
}
|
||||||
printf("%d %d %d %s\n", lines, words, bytes, argv[1]);
|
if (strcmp(list[i], "/dev/stdin") == 0)
|
||||||
|
list[i] = "";
|
||||||
|
clines += lines;
|
||||||
|
cwords += words;
|
||||||
|
cbytes += bytes;
|
||||||
|
printf("%d %d %d %s\n", lines, words, bytes, list[i]);
|
||||||
fclose(stream);
|
fclose(stream);
|
||||||
|
}
|
||||||
|
if (count > 2)
|
||||||
|
printf("%d %d %d total\n", clines, cwords, cbytes);
|
||||||
|
}
|
||||||
|
|
||||||
|
int main(int argc, char **argv) {
|
||||||
|
char *list[2];
|
||||||
|
if (argc < 2) {
|
||||||
|
list[1] = "/dev/stdin";
|
||||||
|
wc(list, 2);
|
||||||
|
} else {
|
||||||
|
wc(argv, argc);
|
||||||
|
}
|
||||||
return EXIT_SUCCESS;
|
return EXIT_SUCCESS;
|
||||||
}
|
}
|
||||||
|
|
Loading…
Add table
Reference in a new issue