Помогите найти утечку памяти на си

не могу найти утечку памяти, free вроде везде стоят.

#include <regex.h>
#include <stdlib.h>
#include <string.h>
#include <unistd.h>

struct flags {
  int _e;
  int _v;
  int _i;
  int _n;
  int _c;
  int _l;
  int _h;
  int _s;
  int _o;
  int _f;
  int E;
  int R;
  int S;
  int _count;
  int ct_file;
  char ptxt[100][1024];
};

int check_file(char *file) {
  FILE *file_x;
  file_x = fopen(file, "r");
  if (file_x == NULL) {
    return 0;
  }
  fclose(file_x);
  return 1;
}

int read_file(struct flags *fl) {
  FILE *file_x;
  size_t len;
  char *line = NULL;
  file_x = fopen(optarg, "r");
  if (file_x != NULL) {
    while ((getline(&line, &len, file_x)) != EOF) {
      if (line[0] == '\n')
        fl->E = 1;
      if (line[strlen(line) - 1] == '\n')
        line[strlen(line) - 1] = '\0';
      strcpy(fl->ptxt[fl->_count++], line);
    }
    if (line)
      free(line);
    fclose(file_x);
  } else {
    return 0;
  }
  return 1;
}

int pars(int argz, char **argx, struct flags *fl) {
  char z;
  while ((z = getopt(argz, argx, "e:ivclonhsf:")) != -1) {
    switch (z) {
    case 'e':
      fl->_e = 1;
      strcpy(fl->ptxt[fl->_count++], optarg);
      break;
    case 'i':
      fl->_i = 1;
      break;
    case 'v':
      fl->_v = 1;
      break;
    case 'c':
      fl->_c = 1;
      break;
    case 'l':
      fl->_l = 1;
      break;
    case 'n':
      fl->_n = 1;
      break;
    case 'h':
      fl->_h = 1;
      fl->ct_file = 0;
      break;
    case 's':
      fl->_s = 1;
      break;
    case 'f':
      fl->_f = 1;
      read_file(fl);
      break;
    case 'o':
      fl->_o = 1;
      break;
    case '?':
      fprintf(stderr,
              "usage: grep [-abcDEFGHhIiJLlmnOoqRSsUVvwxZ] [-A num] [-B num] "
              "[-C[num]]");
      return 1;
    default:
      break;
    }
  }
  for (int i = 1; i < argz; i++) {
    if (argx[i][0] != '-' && !fl->_f && !fl->_e) {
      strcpy(fl->ptxt[fl->_count++], argx[i]);
      break;
    }
  }
  return 0;
}

void without_flags(char *file) {
  FILE *file_x;
  file_x = fopen(file, "r");
  if (file_x != NULL) {
    int c;
    while ((c = fgetc(file_x)) != EOF)
      printf("%c", c);
    fclose(file_x);
  }
}

void argzAfterFl(int argz, char **argx, struct flags *fl) {
  int z = 0, i = 0;
  if (argx[1][0] != '-')
    fl->S = i = 2;
  else
    fl->S = i = 3;
  while (i < argz) {
    z++;
    i++;
  }
  fl->ct_file = z;
}

int read_pr(char *file, struct flags fl) {
  if (fl.E) {
    without_flags(file);
    return 1;
  }
  int dot = fl.ct_file;
  if (fl._l) {
    fl._n = 0;
    fl._o = 0;
  }
  if (fl._c) {
    fl._n = 0;
    fl._o = 0;
  }
  if (fl._v)
    fl._o = 0;
  FILE *file_x;
  file_x = fopen(file, "r");
  size_t len = 0;
  regex_t regex;
  regmatch_t math[1];
  char *line = NULL;
  if (file_x != NULL) {
    int count_n = 0, count_c = 0;
    while ((getline(&line, &len, file_x)) != -1) {
      count_n++;
      int d = 0;
      for (int k = 0; k < fl._count; k++) {
        int err;
        int rite = regcomp(&regex, fl.ptxt[k], (fl._i) ? REG_ICASE : 0);
        fl.R = rite;
        if ((err = regexec(&regex, line, 0, NULL, 0)) == fl._v) {
          if (line[strlen(line) - 1] == '\n')
            line[strlen(line) - 1] = '\0';
          count_c++;
          d = 1;
        }
        regfree(&regex);
      }
      if ((!fl._c && !fl._l) && d) {
        if (dot > 1)
          printf("%s:", file);
        if (fl._n)
          printf("%d:", count_n);
        if (fl._o) {
          char *buff = line;
          for (int l = 0; l < fl._count; l++) {
            regcomp(&regex, fl.ptxt[l], (fl._i) ? REG_ICASE : 0);
            while ((regexec(&regex, buff, 1, math, 0)) == 0) {
              for (int x = (int)math[0].rm_so; x < (int)math[0].rm_eo; x++) {
                printf("%c", buff[x]);
              }
              buff += (int)math[0].rm_eo;
              printf("\n");
              regfree(&regex);
            }
          }
        } else {
          printf("%s\n", line);
        }
      }
    }
    if (fl._l && count_c)
      printf("%s\n", file);
    if (fl._c && dot > 1)
      printf("%s:%d\n", file, count_c);
    else if (fl._c && dot < 2)
      printf("%d\n", count_c);
    fclose(file_x);
    free(line);
  } else {
    if (!fl._s) {
      fprintf(stderr, "s21_grep: %s: No such file or directory\n", file);
    }
  }
  return 0;
}

int main(int argz, char **argx) {
  struct flags flag = {0};
  argzAfterFl(argz, argx, &flag);
  int i = flag.S;
  if (!pars(argz, argx, &flag)) {
    while (i < argz) {
      if (flag._f) {
        if (argx[i][0] != '-' && check_file(argx[i]))
          read_pr(argx[i], flag);
      } else if (argx[i][0] != '-') {
        read_pr(argx[i], flag);
      }
      i++;
    }
  }
  return 0;
}`

Ответы (0 шт):