Computer-Operating-Systems/ProblemSet2/walker.c
void walk_through(char *fn){
    DIR* directory;
    if ((directory = opendir(fn)) == NULL){
        fprintf(stderr, "opendir failed for %s\n", fn);
        exit(1);
    }

    char path[1025];
    char symlink[1025];
    struct dirent* entry;
    struct stat info;

    //print information on fn
    if (stat(fn, &info) != 0){
        fprintf(stderr, "stat error for %s: %s\n", fn, strerror(errno));
        exit(1);
    }

    if (init_vol_num == -5){            //using -5 in case info.st_dev returns -1 
        init_vol_num = info.st_dev;
    }

    display_directory(fn, info);
    

    //process other files
    while (directory){
        errno = 0;
        if ((entry = readdir(directory)) == NULL){
            if (errno != 0)
                fprintf(stderr, "readdir error for %s: %s\n", path, strerror(errno));
            break;
        }

        strcpy(path, fn);
        strcat(path, "/");
        strcat(path, entry->d_name);
        if (strcmp(entry->d_name, ".") != 0 && strcmp(entry->d_name, "..") != 0){
            
            if (lstat(path, &info) != 0){
                fprintf(stderr, "lstat error for %s: %s\n", path, strerror(errno));
                return;
            }

            if (S_ISDIR(info.st_mode)){
                if (optx == 1 && init_vol_num != info.st_dev)
                    fprintf(stderr, "note: not crossing mount point at %s\n", path);
                else
                    walk_through(path);
            }

            else
                display_directory(path, info);
        }
    }

    if (closedir(directory) < 0){
        fprintf(stderr, "closedir error for %s: %s\n", path, strerror(errno));
        exit(1);
    }
}
//walk through the directory, last four arguments of the function are used to hold the optargs
walk through the directory, last four arguments of the function are used to hold the optargs
walk through the directory last four arguments of the function are used to hold the optargs
void walk_through(char *fn){
    DIR* directory;
    if ((directory = opendir(fn)) == NULL){
        fprintf(stderr, "opendir failed for %s\n", fn);
        exit(1);
    }
    char path[1025];
    char symlink[1025];
    struct dirent* entry;
    struct stat info;
    if (stat(fn, &info) != 0){
        fprintf(stderr, "stat error for %s: %s\n", fn, strerror(errno));
        exit(1);
    }
    if (init_vol_num == -5){            
        init_vol_num = info.st_dev;
    }
    display_directory(fn, info);
    while (directory){
        errno = 0;
        if ((entry = readdir(directory)) == NULL){
            if (errno != 0)
                fprintf(stderr, "readdir error for %s: %s\n", path, strerror(errno));
            break;
        }
        strcpy(path, fn);
        strcat(path, "/");
        strcat(path, entry->d_name);
        if (strcmp(entry->d_name, ".") != 0 && strcmp(entry->d_name, "..") != 0){
            if (lstat(path, &info) != 0){
                fprintf(stderr, "lstat error for %s: %s\n", path, strerror(errno));
                return;
            }
            if (S_ISDIR(info.st_mode)){
                if (optx == 1 && init_vol_num != info.st_dev)
                    fprintf(stderr, "note: not crossing mount point at %s\n", path);
                else
                    walk_through(path);
            }
            else
                display_directory(path, info);
        }
    }
    if (closedir(directory) < 0){
        fprintf(stderr, "closedir error for %s: %s\n", path, strerror(errno));
        exit(1);
    }
}
void walk_through ( char * fn ) { DIR * directory ; if ( ( directory = opendir ( fn ) ) == NULL ) { fprintf ( stderr , " \n " , fn ) ; exit ( 1 ) ; } char path [ 1025 ] ; char symlink [ 1025 ] ; struct dirent * entry ; struct stat info ; if ( stat ( fn , & info ) != 0 ) { fprintf ( stderr , " \n " , fn , strerror ( errno ) ) ; exit ( 1 ) ; } if ( init_vol_num == -5 ) { init_vol_num = info . st_dev ; } display_directory ( fn , info ) ; while ( directory ) { errno = 0 ; if ( ( entry = readdir ( directory ) ) == NULL ) { if ( errno != 0 ) fprintf ( stderr , " \n " , path , strerror ( errno ) ) ; break ; } strcpy ( path , fn ) ; strcat ( path , " " ) ; strcat ( path , entry -> d_name ) ; if ( strcmp ( entry -> d_name , " " ) != 0 && strcmp ( entry -> d_name , " " ) != 0 ) { if ( lstat ( path , & info ) != 0 ) { fprintf ( stderr , " \n " , path , strerror ( errno ) ) ; return ; } if ( S_ISDIR ( info . st_mode ) ) { if ( optx == 1 && init_vol_num != info . st_dev ) fprintf ( stderr , " \n " , path ) ; else walk_through ( path ) ; } else display_directory ( path , info ) ; } } if ( closedir ( directory ) < 0 ) { fprintf ( stderr , " \n " , path , strerror ( errno ) ) ; exit ( 1 ) ; } }
walk through the directory, last four arguments of the function are used to hold the optargs
walk through the directory last four arguments of the function are used to hold the optargs
//print information on fn //using -5 in case info.st_dev returns -1  //process other files