micro-utils/coreutils/wc.c
2023-12-02 13:15:48 +03:00

132 lines
2.0 KiB
C

#include <fcntl.h>
#include <stdio.h>
#include <errno.h>
#include <ctype.h>
#include <unistd.h>
#include <string.h>
#include "config.h"
/* cmd arguments l - lines c - bytes w - words */
unsigned int l_flag;
unsigned int c_flag;
unsigned int w_flag;
unsigned int words;
unsigned int bytes;
unsigned int lines;
/* Total */
unsigned int twords;
unsigned int tbytes;
unsigned int tlines;
void count(const int fd) {
char buf[BUF_SIZE + 1];
off_t n = 0;
int in_word = 1;
while ((n = read(fd, buf, sizeof(buf))) > 0) {
bytes += n;
for (ssize_t i = 0; i < n; i++) {
if (buf[i] == '\n')
lines++;
if (isspace(buf[i]))
in_word = 1;
else {
if (in_word)
words++;
in_word = 0;
}
}
}
tbytes += bytes;
twords += words;
tlines += lines;
}
void print_count(const char *path, unsigned int plines, unsigned int pwords, unsigned int pbytes) {
if (l_flag)
printf("%7u ", plines);
if (w_flag)
printf(" %7u", pwords);
if (c_flag)
printf(" %7u", pbytes);
printf(" %s\n", path);
}
int main(int argc, char **argv) {
int opt;
while ((opt = getopt(argc, argv, "lcw")) != -1) {
switch (opt) {
case 'l':
l_flag = 1;
break;
case 'c':
c_flag = 1;
break;
case 'w':
w_flag = 1;
break;
default:
printf("wc [file1 file2...]\n\t[-l Lines] [-c Bytes]\n\t[-w Words]\n");
return 0;
}
}
argv += optind;
argc -= optind;
if (!w_flag && !l_flag && !c_flag) {
w_flag = 1;
l_flag = 1;
c_flag = 1;
}
if (argc == 0) {
count(STDIN_FILENO);
print_count("", lines, words, bytes);
return 0;
}
for (int i = 0; i < argc; i++) {
if (argv[i][0] == '-') {
count(STDIN_FILENO);
print_count("-", lines, words, bytes);
}
else {
int fd = open(argv[i], O_RDONLY);
if (fd < 0) {
fprintf(stderr, "wc: %s\n", strerror(errno));
return 1;
}
count(fd);
if (i == argc - 1)
print_count("total", tlines, twords, tbytes);
else
print_count(argv[i], lines, words, bytes);
close(fd);
}
words = bytes = lines = 0;
}
return 0;
}