diff options
[SP/P1] Solved all exercises
1 files changed, 29 insertions, 0 deletions
| -rw-r--r-- | Year 3/System programming/Practicum1/wc.c | 29 |
diff --git a/Year 3/System programming/Practicum1/wc.c b/Year 3/System programming/Practicum1/wc.c new file mode 100644 index 0000000..fc0cee5 --- /dev/null +++ b/Year 3/System programming/Practicum1/wc.c @@ -0,0 +1,29 @@ +#include <sys/types.h> +#include <sys/stat.h> +#include <fcntl.h> +#include <unistd.h> +#include <stdio.h> + +int main(int argc, char *argv[]) { + int f = open(argv[1], O_RDONLY); + if (f == -1) return 1; + + char buf; + int prevNotSpace = 1; + int bytes = 0, newlines = 0, words = 0; + while (read(f, &buf, 1)) { + bytes++; + if (buf == '\n') { + newlines++; + words++; + } + if (buf == ' ' || buf == '\t') { + words += prevNotSpace; + prevNotSpace = 0; + } + else prevNotSpace = 1; + } + + close(f); + printf("%d %d %d %s\n", newlines, words, bytes, argv[1]); +} |
