Advertisement
2607

s21_cat.c

Dec 5th, 2021
1,649
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C 4.16 KB | None | 0 0
  1. #include <stdio.h>
  2. #include <stdlib.h>
  3. #include <string.h>
  4. #include <unistd.h>
  5.  
  6. void open_file(char** argv, int flag_b, int flag_e, int flag_n, int flag_s,
  7.                int flag_t, int flag_T, int flag_E);
  8. int s21_cat(int flag_b, int flag_e, int flag_n, int flag_s,
  9.             int flag_t, int flag_T, int flag_E, FILE* fp, int count);
  10. int flag_to_int(char* flags, char flag);
  11.  
  12. int flag_to_int(char* flags, char flag) {
  13.     int tmp = 0;
  14.     for (int i = 0; i < strlen(flags); i++) {
  15.         if (flags[i] == flag) {
  16.             tmp = 1;
  17.             break;
  18.         }
  19.     }
  20.     return tmp;
  21. }
  22.  
  23. int s21_cat(int flag_b, int flag_e, int flag_n, int flag_s,
  24.             int flag_t, int flag_T, int flag_E, FILE* fp, int count) {
  25.     int current, prev = '\n';
  26.     int temp = 0;
  27.     while ((current = getc(fp)) != EOF) {
  28.         if (flag_s) {
  29.             if (current == '\n') temp++;
  30.             if (current != '\n') temp = 0;
  31.         }
  32.         if (flag_b && temp < 3) {
  33.             if ((prev == '\n' && !(current == '\n')) || count == 1) {
  34.                 printf("%6d\t", count);
  35.                 count++;
  36.             }
  37.         }
  38.         if (flag_n && !flag_b && temp < 3) {
  39.             if (prev == '\n' || count == 1) {
  40.                 printf("%6d\t", count);
  41.                 count++;
  42.             }
  43.         }
  44.         if (flag_E || flag_e && temp < 3) {
  45.             if ((flag_t || flag_e) && current >= 0 && current < 32 && current != '\n' && current != '\t') {
  46.                 printf("^%c", 64 + current);
  47.             } else if ((flag_t || flag_T) && current == '\t') {
  48.                 printf("^I");
  49.             } else if (current != '\n') {
  50.                 printf("%c", current);
  51.             }
  52.             if (current == '\n') {
  53.                 printf("$\n");
  54.             }
  55.             if (current != '\n') temp = 0;
  56.         } else if (flag_T || flag_t && temp < 3) {
  57.             if (!flag_e && flag_t && current >= 0 && current < 32 && current != '\n' && current != '\t') {
  58.                 printf("^%c", 64 + current);
  59.             } else if (current != '\t' && current != '\n') {
  60.                 printf("%c", current);
  61.             } else if (!flag_e && !flag_E && current == '\n') {
  62.                 printf("\n");
  63.             }
  64.             if (current == '\t') {
  65.                 printf("^I");
  66.             }
  67.             if (current != '\n') temp = 0;
  68.         } else {
  69.             if (!((flag_e || flag_E) && current == '\n') && temp < 3) {
  70.                 putchar(current);
  71.             }
  72.         }
  73.         prev = current;
  74.     }
  75.     return count;
  76. }
  77.  
  78. void open_file(char** argv, int flag_b, int flag_e, int flag_n,
  79.                int flag_s, int flag_t, int flag_T, int flag_E) {
  80.     FILE* fp;
  81.     argv++;
  82.     int result = 1;
  83.     while (*argv) {
  84.         if (*argv[0] == '-') {
  85.             argv++;
  86.         } else {
  87.             if ((fp = fopen(*argv, "r")) == NULL) {
  88.                 argv++;
  89.             } else {
  90.                 result = s21_cat(flag_b, flag_e, flag_n, flag_s, flag_t, flag_T, flag_E, fp, result);
  91.                 argv++;
  92.             }
  93.         }
  94.     }
  95. }
  96.  
  97. int main(int argc, char** argv) {
  98.     int flag_b = 0, flag_e = 0, flag_n = 0, flag_s = 0, flag_t = 0, flag_E = 0, flag_T = 0;
  99.     int i = 0;
  100.     while (i < argc) {
  101.         if (argv[i][0] == '-' && argv[i][1] != '-') {
  102.             flag_b = flag_b == 1 ? 1 : flag_to_int(argv[i], 'b');
  103.             flag_e = flag_e == 1 ? 1 : flag_to_int(argv[i], 'e');
  104.             flag_n = flag_n == 1 ? 1 : flag_to_int(argv[i], 'n');
  105.             flag_s = flag_s == 1 ? 1 : flag_to_int(argv[i], 's');
  106.             flag_t = flag_t == 1 ? 1 : flag_to_int(argv[i], 't');
  107.             flag_E = flag_E == 1 ? 1 : flag_to_int(argv[i], 'E');
  108.             flag_T = flag_T == 1 ? 1 : flag_to_int(argv[i], 'T');
  109.         }
  110.         if (argv[i][0] == '-' && argv[i][1] == '-') {
  111.             if (strcmp(&argv[i][0], "--number") == 0) flag_n = 1;
  112.             if (strcmp(&argv[i][0], "--number-nonblank") == 0) flag_b = 1;
  113.             if (strcmp(&argv[i][0], "--squeeze-blank") == 0) flag_s = 1;
  114.         }
  115.         i++;
  116.     }
  117.     open_file(argv, flag_b, flag_e, flag_n, flag_s, flag_t, flag_T, flag_E);
  118.     return 0;
  119. }
  120.  
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement