Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #include <stdio.h>
- #include <stdlib.h>
- #include <string.h>
- #include <unistd.h>
- void open_file(char** argv, int flag_b, int flag_e, int flag_n, int flag_s,
- int flag_t, int flag_T, int flag_E);
- int s21_cat(int flag_b, int flag_e, int flag_n, int flag_s,
- int flag_t, int flag_T, int flag_E, FILE* fp, int count);
- int flag_to_int(char* flags, char flag);
- int flag_to_int(char* flags, char flag) {
- int tmp = 0;
- for (int i = 0; i < strlen(flags); i++) {
- if (flags[i] == flag) {
- tmp = 1;
- break;
- }
- }
- return tmp;
- }
- int s21_cat(int flag_b, int flag_e, int flag_n, int flag_s,
- int flag_t, int flag_T, int flag_E, FILE* fp, int count) {
- int current, prev = '\n';
- int temp = 0;
- while ((current = getc(fp)) != EOF) {
- if (flag_s) {
- if (current == '\n') temp++;
- if (current != '\n') temp = 0;
- }
- if (flag_b && temp < 3) {
- if ((prev == '\n' && !(current == '\n')) || count == 1) {
- printf("%6d\t", count);
- count++;
- }
- }
- if (flag_n && !flag_b && temp < 3) {
- if (prev == '\n' || count == 1) {
- printf("%6d\t", count);
- count++;
- }
- }
- if (flag_E || flag_e && temp < 3) {
- if ((flag_t || flag_e) && current >= 0 && current < 32 && current != '\n' && current != '\t') {
- printf("^%c", 64 + current);
- } else if ((flag_t || flag_T) && current == '\t') {
- printf("^I");
- } else if (current != '\n') {
- printf("%c", current);
- }
- if (current == '\n') {
- printf("$\n");
- }
- if (current != '\n') temp = 0;
- } else if (flag_T || flag_t && temp < 3) {
- if (!flag_e && flag_t && current >= 0 && current < 32 && current != '\n' && current != '\t') {
- printf("^%c", 64 + current);
- } else if (current != '\t' && current != '\n') {
- printf("%c", current);
- } else if (!flag_e && !flag_E && current == '\n') {
- printf("\n");
- }
- if (current == '\t') {
- printf("^I");
- }
- if (current != '\n') temp = 0;
- } else {
- if (!((flag_e || flag_E) && current == '\n') && temp < 3) {
- putchar(current);
- }
- }
- prev = current;
- }
- return count;
- }
- void open_file(char** argv, int flag_b, int flag_e, int flag_n,
- int flag_s, int flag_t, int flag_T, int flag_E) {
- FILE* fp;
- argv++;
- int result = 1;
- while (*argv) {
- if (*argv[0] == '-') {
- argv++;
- } else {
- if ((fp = fopen(*argv, "r")) == NULL) {
- argv++;
- } else {
- result = s21_cat(flag_b, flag_e, flag_n, flag_s, flag_t, flag_T, flag_E, fp, result);
- argv++;
- }
- }
- }
- }
- int main(int argc, char** argv) {
- int flag_b = 0, flag_e = 0, flag_n = 0, flag_s = 0, flag_t = 0, flag_E = 0, flag_T = 0;
- int i = 0;
- while (i < argc) {
- if (argv[i][0] == '-' && argv[i][1] != '-') {
- flag_b = flag_b == 1 ? 1 : flag_to_int(argv[i], 'b');
- flag_e = flag_e == 1 ? 1 : flag_to_int(argv[i], 'e');
- flag_n = flag_n == 1 ? 1 : flag_to_int(argv[i], 'n');
- flag_s = flag_s == 1 ? 1 : flag_to_int(argv[i], 's');
- flag_t = flag_t == 1 ? 1 : flag_to_int(argv[i], 't');
- flag_E = flag_E == 1 ? 1 : flag_to_int(argv[i], 'E');
- flag_T = flag_T == 1 ? 1 : flag_to_int(argv[i], 'T');
- }
- if (argv[i][0] == '-' && argv[i][1] == '-') {
- if (strcmp(&argv[i][0], "--number") == 0) flag_n = 1;
- if (strcmp(&argv[i][0], "--number-nonblank") == 0) flag_b = 1;
- if (strcmp(&argv[i][0], "--squeeze-blank") == 0) flag_s = 1;
- }
- i++;
- }
- open_file(argv, flag_b, flag_e, flag_n, flag_s, flag_t, flag_T, flag_E);
- return 0;
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement