#include <stdio.h>
#include <stdlib.h>


// TODO: Implement a helper named check_permissions that matches the prototype below.
int check_permissions(char *check_perms, char *req_perms) {
    int i;
    for (i = 0; i < 9; i++) {
        if (req_perms[i] != check_perms[i] && req_perms[i] != '-') {
            return 1;
        }
    }
    return 0;
}


int main(int argc, char** argv) {
    if (!(argc == 2 || argc == 3)) {
        fprintf(stderr, "USAGE: count_large size [permissions]\n");
        return 1;
    }

    // TODO: Process command line arguments.
    int cutoff = strtol(argv[1], NULL, 10);
    int total = 0;
    int success;
    int current_size;

    scanf("%*[^\n]\n");//Get past & ignore the Total (first) line


    if (argc == 3) { //Check permissions version
        char *permissions = malloc(sizeof(char) * 9);
        permissions = argv[2];
        char *current_perm = malloc(sizeof(char) * 9);

        success = scanf("%*1s%9s %*d %*s %*s %d %*[^\n]\n", current_perm, &current_size);
        while (success > -1) {
            if (current_size > cutoff && check_permissions(current_perm, permissions) == 0) {
                total++;
            }
            success = scanf("%*s %*d %*s %*s %d %*[^\n]\n", &current_size);
        }

        free(permissions);
        free(current_perm);
    }
    else {//Regular version
        //Do one check first, so that they are synced properly with EOF check
        success = scanf("%*s %*d %*s %*s %d %*[^\n]\n", &current_size);
        while (success > -1) {
            if (current_size > cutoff) {
                total++;
            }
            success = scanf("%*s %*d %*s %*s %d %*[^\n]\n", &current_size);
        }
    }

    printf("%d\n", total);

    return 0;
}

the intended behavior is to avoid segfaults