Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #include <regex.h>
- #include <stdio.h>
- #include <stdlib.h>
- #include <string.h>
- void spam_error(char* filename, int flag_s);
- void open_file(int flag_i, int flag_v, int flag_c, int flag_l, int flag_n, int flag_b,
- int flag_h, int flag_s, int flag_o, int flag_e, int flag_f, char** argv,
- int file_numbers, char* find_str);
- void s21_grep(int flag_i, int flag_v, int flag_c, int flag_l, int flag_n, int flag_b,
- int flag_h, int flag_s, int flag_o, int file_numbers, char* find_str,
- FILE* fp, char* filename);
- int flag_to_int(char* flags, char flag);
- void outputFlagC(int flag_c, int flag_v, int count_c, int count_vc, int flag_h,
- char* filename, int file_numbers, int flag_l);
- void* s21_to_lower(const char* str);
- void* s21_to_upper(const char* str);
- char* s21_strcat(char* dest, const char* src);
- char* s21_strcpy(char* dest, const char* src);
- char* convert_for_char(unsigned int num);
- void s21_grep_core(int flag_b, int flag_n, int flag_c, int flag_l, int count, size_t count_b, char* line);
- // Support Function
- char* s21_strcat(char* dest, const char* src) {
- size_t i, j;
- for (i = 0; dest[i] != '\0'; i++) {
- }
- for (j = 0; src[j] != '\0'; j++)
- dest[i + j] = src[j];
- dest[i + j] = '\0';
- return dest;
- }
- char* s21_strcpy(char* dest, const char* src) {
- char* tmp = dest;
- for (; *src; src++, dest++)
- *dest = *src;
- *dest = 0;
- return tmp;
- }
- void* s21_to_lower(const char* string) {
- short isError = 1;
- char* pString = calloc((strlen(string) + 1), sizeof(char));
- if (pString) {
- s21_strcpy(pString, string);
- for (char* p = (char*)pString; *p != '\0'; p++) {
- if (*p >= 'A' && *p <= 'Z') {
- *p += 32;
- }
- }
- isError = 0;
- }
- return (isError) ? NULL : (void*)pString;
- }
- void* s21_to_upper(const char* string) {
- short isError = 1;
- char* pString = calloc((strlen(string) + 1), sizeof(char));
- if (pString) {
- s21_strcpy(pString, string);
- for (char* p = (char*)pString; *p != '\0'; p++) {
- if (*p >= 'a' && *p <= 'z') {
- *p -= 32;
- }
- }
- isError = 0;
- }
- return (isError) ? NULL : (void*)pString;
- }
- void spam_error(char* filename, int flag_s) {
- if (!flag_s) {
- printf("grep: %s: No such file or directory\n", filename);
- }
- }
- 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;
- }
- char* convert_for_char(unsigned int num) {
- static char buffer[2];
- char* ptr;
- ptr = &buffer[1];
- *ptr = '\0';
- *--ptr = (char)num;
- return (ptr);
- }
- void open_file(int flag_i, int flag_v, int flag_c, int flag_l, int flag_n, int flag_b,
- int flag_h, int flag_s, int flag_o, int flag_e, int flag_f,
- char** argv, int file_numbers, char* find_str) {
- FILE* fp;
- char* filename;
- int find = 0;
- argv++;
- while (*argv) {
- if (flag_to_int(*argv, '-')) {
- if (flag_to_int(*argv, 'e') || flag_to_int(*argv, 'f'))
- argv++;
- argv++;
- } else {
- if (find == 0 && flag_e == 0 && flag_f == 0) {
- find++;
- argv++;
- } else if ((fp = fopen(*argv, "r")) == NULL) {
- spam_error(*argv, flag_s);
- argv++;
- } else {
- filename = *argv++;
- s21_grep(flag_i, flag_v, flag_c, flag_l, flag_n, flag_b,
- flag_h, flag_s, flag_o, file_numbers, find_str, fp, filename);
- fclose(fp);
- }
- }
- }
- }
- void s21_grep_core(int flag_b, int flag_n, int flag_c, int flag_l, int count, size_t count_b, char* line) {
- if (flag_b && !flag_n && !flag_c && !flag_l) {
- printf("%zu:", count_b);
- }
- if (flag_n && !flag_c && !flag_l) {
- printf("%d:", count);
- if (flag_b) {
- printf("%zu:", count_b);
- }
- printf("%s", line);
- } else if (!flag_c && !flag_l) {
- printf("%s", line);
- }
- }
- void outputFlagL(int flag_l, int flag_v, int flag_c, int count_c,
- int count_vc, int flag_h, char* filename, int file_numbers) {
- if (flag_l && !flag_v) {
- if (count_c != 0)
- printf("%s\n", filename);
- } else if (flag_l && flag_v) {
- if (count_vc != 0)
- printf("%s\n", filename);
- }
- }
- void s21_grep(int flag_i, int flag_v, int flag_c, int flag_l, int flag_n, int flag_b,
- int flag_h, int flag_s, int flag_o, int file_numbers, char* find_str,
- FILE* fp, char* filename) {
- regex_t regex;
- char* line = NULL;
- size_t len = 0, count_b = 0, nmatch = 2, read;
- int count = 1, count_c = 0, count_vc = 0, reti;
- regcomp(®ex, find_str, REG_EXTENDED);
- regmatch_t pmatch[2];
- while ((read = getline(&line, &len, fp)) != EOF) {
- size_t count_b_ex = strlen(line);
- char* line_tmp = calloc(strlen(line), sizeof(char));
- if (line[strlen(line)] == '\n' && strlen(line) == 1)
- s21_strcat(line, "\n");
- if (line[strlen(line) - 1] != '\n')
- s21_strcat(line, "\n");
- if (flag_i) {
- s21_strcpy(line_tmp, line);
- char* line_tmp2 = s21_to_lower(line_tmp);
- char* line_tmp3 = s21_to_upper(line_tmp);
- int reti_tmp;
- reti_tmp = regexec(®ex, line_tmp3, nmatch, pmatch, 0);
- reti = regexec(®ex, line_tmp2, nmatch,
- pmatch, 0);
- reti = reti && reti_tmp;
- free(line_tmp2);
- free(line_tmp3);
- } else {
- reti = regexec(®ex, line, nmatch, pmatch, 0);
- }
- if (flag_o && !flag_v && !reti) {
- if (strlen(line) != 1) {
- char* str_tmp = calloc(strlen(line) + 1, sizeof(char));
- snprintf(str_tmp, strlen(line) + 1, "%c%s", '\t', line);
- snprintf(line, strlen(line), "%.*s", (int)(pmatch[1].rm_eo - pmatch[1].rm_so),
- &str_tmp[pmatch[1].rm_so + 1]);
- s21_strcat(line, "\n");
- }
- }
- if (file_numbers > 1 && !flag_h && !reti && !flag_v && !flag_c && !flag_l)
- printf("%s:", filename);
- if (file_numbers > 1 && !flag_h && reti && flag_v && !flag_c && !flag_l)
- printf("%s:", filename);
- if (!reti && !flag_v) {
- s21_grep_core(flag_b, flag_n, flag_c, flag_l, count, count_b, line);
- count_c++;
- } else if (flag_v && reti) {
- s21_grep_core(flag_b, flag_n, flag_c, flag_l, count, count_b, line);
- count_vc++;
- }
- count++;
- count_b += count_b_ex;
- if (line_tmp) free(line_tmp);
- }
- outputFlagL(flag_l, flag_v, flag_c, count_c, count_vc, flag_h, filename, file_numbers);
- if (line) free(line);
- regfree(®ex);
- }
- int main(int argc, char** argv) {
- char* find_str = calloc(1000, sizeof(char));
- int i = 1, file_numbers = 0, find = 0;
- int flag_e = 0, flag_f = 0, flag_i = 0, flag_v = 0, flag_c = 0, flag_l = 0,
- flag_n = 0, flag_b = 0, flag_h = 0, flag_s = 0, flag_o = 0;
- s21_strcat(find_str, convert_for_char('('));
- while (i < argc) {
- if (argv[i][0] == '-') {
- flag_i = flag_i == 1 ? 1 : flag_to_int(argv[i], 'i');
- flag_v = flag_v == 1 ? 1 : flag_to_int(argv[i], 'v');
- flag_c = flag_c == 1 ? 1 : flag_to_int(argv[i], 'c');
- flag_l = flag_l == 1 ? 1 : flag_to_int(argv[i], 'l');
- flag_n = flag_n == 1 ? 1 : flag_to_int(argv[i], 'n');
- flag_b = flag_b == 1 ? 1 : flag_to_int(argv[i], 'b');
- flag_h = flag_h == 1 ? 1 : flag_to_int(argv[i], 'h');
- flag_s = flag_s == 1 ? 1 : flag_to_int(argv[i], 's');
- flag_o = flag_o == 1 ? 1 : flag_to_int(argv[i], 'o');
- if (flag_to_int(argv[i], 'e')) {
- flag_e++;
- if (flag_e == 1 && flag_f == 0) {
- s21_strcpy(find_str, convert_for_char('('));
- s21_strcat(find_str, argv[i + 1]);
- } else {
- if (flag_e > 0 || flag_f > 0) {
- s21_strcat(find_str, convert_for_char('|'));
- s21_strcat(find_str, argv[i + 1]);
- }
- }
- i++;
- }
- if (flag_to_int(argv[i], 'f')) {
- FILE* fp;
- fp = fopen(argv[i + 1], "r");
- if (fp == NULL) exit(EXIT_FAILURE);
- flag_f++;
- char current, prev = 'z';
- if (flag_e > 0 && flag_f > 0) {
- s21_strcat(find_str, convert_for_char('|'));
- }
- while ((current = getc(fp)) != EOF) {
- if (prev == '\n' && current != '\n') {
- s21_strcat(find_str, convert_for_char('|'));
- }
- if (current != '\n') {
- s21_strcat(find_str, convert_for_char(current));
- }
- if (prev == '\n' && current == '\n') {
- s21_strcat(find_str, convert_for_char('|'));
- s21_strcat(find_str, "\n");
- }
- prev = current;
- }
- fclose(fp);
- i++;
- }
- } else {
- if (flag_e == 0 && flag_f == 0 && find == 0) {
- find++;
- s21_strcat(find_str, argv[i]);
- } else {
- file_numbers++;
- }
- }
- i++;
- }
- if (find_str) s21_strcat(find_str, convert_for_char(')'));
- open_file(flag_i, flag_v, flag_c, flag_l, flag_n, flag_b,
- flag_h, flag_s, flag_o, flag_e, flag_f, argv, file_numbers, find_str);
- free(find_str);
- return 0;
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement