wc.c 2.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475
  1. /* SPDX-License-Identifier: BSD-3-Clause */
  2. /*
  3. * Copyright (C) 2022, 2023 Ferass El Hafidi <vitali64pmemail@protonmail.com>
  4. */
  5. #include <unistd.h>
  6. #include <stdio.h>
  7. #include <stdlib.h>
  8. #include <stdint.h>
  9. #include <errno.h>
  10. #include <string.h>
  11. #define REQ_PRINT_USAGE /* Require print_usage() from ../common/common.h */
  12. #define REQ_ERRPRINT /* Require errprint() from ../common/common.h */
  13. #define DESCRIPTION "Word, line, and byte/character count."
  14. #define OPERANDS "[-clwm] [file...]"
  15. #include "../common/common.h"
  16. int main(int argc, char *const argv[]) {
  17. int argument, length;
  18. FILE *file;
  19. size_t len_getd = 4096;
  20. char *line = NULL, *argv0 = strdup(argv[0]);
  21. char param[256];
  22. ssize_t bytes = 0, newlines = 0, words = 0,
  23. total_bytes = 0, total_newlines = 0, total_words = 0;
  24. if (argc < 2)
  25. return print_usage(argv[0], DESCRIPTION, OPERANDS, VERSION);
  26. while ((argument = getopt(argc, argv, "clmw")) != -1) {
  27. if (argument == '?') {
  28. print_usage(argv[0], DESCRIPTION, OPERANDS, VERSION);
  29. return 1;
  30. }
  31. param[(uint8_t)argument] = argument;
  32. } argc -= optind; argv += optind;
  33. if (!param['l'] && !param['m'] && !param['c'] && !param['w']) {
  34. param['l'] = 'l';
  35. param['m'] = 'm';
  36. param['c'] = 'c';
  37. param['w'] = 'w';
  38. }
  39. for (int i = 0; i != argc; i++) {
  40. words = 0; bytes = 0; newlines = 0;
  41. file = fopen(argv[i], "r");
  42. if (errno) return errprint(argv0, argv[i], errno);
  43. while ((length = getline(&line, &len_getd, file)) != -1) {
  44. if (param['l']) newlines++;
  45. if (param['c'] || param['m']) bytes += length;
  46. } rewind(file); /* XXX: Won't work with standard input */
  47. if (errno) return errprint(argv0, argv[i], errno);
  48. if (param['w']) while ((length = getdelim(&line, &len_getd, (int)' ', file)) != -1) {
  49. words++;
  50. }
  51. if (errno) return errprint(argv0, argv[i], errno);
  52. if (param['l']) printf("%zu ", newlines);
  53. if (param['w']) printf("%zu ", words);
  54. if (param['m'] || param['c']) printf("%zu ", bytes);
  55. printf("%s\n", argv[i]);
  56. fclose(file);
  57. if (errno) return errprint(argv0, argv[i], errno);
  58. total_bytes += bytes;
  59. total_words += words;
  60. total_newlines += newlines;
  61. }
  62. if (total_bytes != bytes) {
  63. if (param['l']) printf("%zu ", total_newlines);
  64. if (param['w']) printf("%zu ", total_words);
  65. if (param['m'] || param['c']) printf("%zu ", total_bytes);
  66. printf("total\n");
  67. }
  68. free(line);
  69. if (errno) return errprint(argv0, NULL, errno);
  70. return 0;
  71. }