Advertisement
2607

cat_s21_cat.c

Dec 5th, 2021
992
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C 4.09 KB | None | 0 0
  1. define _GNU_SOURCE
  2.  
  3. #include <stdio.h>
  4. #include <stdlib.h>
  5. #include <errno.h>
  6. #include <string.h>
  7. #include <getopt.h>
  8. #include "s21_cat.h"
  9.  
  10. int main(int argc, char* argv[]) {
  11.     const char* short_opt = "benstvTE";
  12.     const struct option long_opt[] = {
  13.         {"number-nonblank", no_argument, NULL, 'b'},
  14.         {"number", no_argument, NULL, 'n'},
  15.         {"squeeze-blank", no_argument, NULL, 's'},
  16.         {NULL, 0, NULL, 0}
  17.     };
  18.     int index_opt = -1;
  19.  
  20.     int error = 0;
  21.     struct opts cat_opt = {0, 0, 0, 0, 0, 0};
  22.  
  23.     for (int opt; (opt = getopt_long(argc, argv, short_opt, long_opt, &index_opt)) != -1;) {
  24.         // printf("opt: %c\n", opt);
  25.         switch (opt) {
  26.             case 'b':
  27.                 cat_opt.b = 1;
  28.             break;
  29.             case 'e':
  30.             case 'E':
  31.                 cat_opt.e = 1;
  32.                 cat_opt.v = 1;
  33.             break;
  34.             case 'n':
  35.                 cat_opt.n = 1;
  36.             break;
  37.             case 's':
  38.                 cat_opt.s = 1;
  39.             break;
  40.             case 't':
  41.             case 'T':
  42.                 cat_opt.t = 1;
  43.                 cat_opt.v = 1;
  44.             break;
  45.             case 'v':
  46.                 cat_opt.v = 1;
  47.             break;
  48.             case '?':
  49.                 fprintf(stderr, "usage: cat [options] [file ...]\n");
  50.                 error = 1;
  51.             break;
  52.         }
  53.     }
  54.     if (!error) {
  55.         // printf("-b: %d\n", cat_opt.b);
  56.         // printf("-e: %d\n", cat_opt.e);
  57.         // printf("-n: %d\n", cat_opt.n);
  58.         // printf("-s: %d\n", cat_opt.s);
  59.         // printf("-t: %d\n", cat_opt.t);
  60.         for (int index = optind; index < argc; index++) {
  61.             // printf ("Non-option argument %s\n", argv[index]);
  62.             if (output(argv[index], cat_opt)) {
  63.                 fprintf(stderr, "%s: %s: %s\n", CAT, argv[index], strerror(errno));
  64.                 error = 1;
  65.             }
  66.         }
  67.     }
  68.     return error;
  69. }
  70.  
  71. int output(const char* cat_file, struct opts cat_opt) {
  72.     int error = 0;
  73.     int ch;
  74.     FILE* fp;
  75.  
  76.     fp = fopen(cat_file, "rt");
  77.     if (fp && (ch = getc(fp)) != -1) {
  78. #if defined(__APPLE__)
  79.         size_t num = 1;
  80. #else
  81.         static size_t num = 1;
  82. #endif
  83.         int new_line = 1;
  84.         int exclude_line = 0;
  85.         while (!feof(fp) && !ferror(fp)) {
  86.             if (ch != EOF) {
  87.                 if ((cat_opt.b && new_line && ch !='\n') || (cat_opt.n && new_line && !cat_opt.b)) {
  88.                     printf("%*zu\t", WIDTH, num);
  89.                     num++;
  90.                     new_line = 0;
  91.                 }
  92.                 if (ch == '\n') {
  93.                     new_line = 1;
  94.                     if (cat_opt.e && exclude_line < 2) {
  95.                         printf("$");
  96.                     }
  97.                     if (cat_opt.s) {
  98.                         exclude_line++;
  99.                     }
  100.                 } else {
  101.                     exclude_line = 0;
  102.                 }
  103.                 if (exclude_line > 2) {
  104.                     exclude_line = 0;
  105.                 } else {
  106.                     if (cat_opt.v) {
  107.                         if (ch < 9 || (ch > 10 && ch < 32)) {
  108.                             printf("^%c", ch + 64);
  109.                         } else if (cat_opt.t && ch == '\t') {
  110.                             printf("^I");
  111.                         } else if (ch == 127) {
  112.                             printf("^?");
  113.                         } else if (ch > 127 && ch < 160) {
  114.                             printf("M-^%c", ch - 64);
  115. #if defined(__linux__)
  116.                         } else if (ch > 159 && ch < 255) {
  117.                             printf("M-%c", ch - 128);
  118.                         } else if (ch == 255) {
  119.                             printf("M-^?");
  120. #endif
  121.                         } else {
  122.                             putchar(ch);
  123.                         }
  124.                     } else {
  125.                         putchar(ch);
  126.                     }
  127.                 }
  128.                 ch = getc(fp);
  129.             }
  130.         }
  131.         fclose(fp);
  132.     } else {
  133.         error = 1;
  134.     }
  135.     return error;
  136. }
  137.  
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement