Computer-Operating-Systems/ProblemSet2/walker.c
void display_directory(char *path_name, struct stat info){
    char symlink[1025];
    char uxbit = '-';
    char gxbit = '-';
    char oxbit = '-';

    struct passwd *pswd;
    struct group *group;
    if ((pswd = getpwuid(info.st_uid)) == NULL){
        fprintf(stderr, "getpwuid() error for %s\n", path_name);
        exit(1);
    }
    if ((group = getgrgid(info.st_gid)) == NULL){
        fprintf(stderr, "getgrgid() error for %s\n", path_name);
        exit(1);
    }

    //check if username was 'user' or 'uid' and then address the flag
    if (user_id == 0){
        if (strcmp(username, "") != 0  && strcmp(username, pswd->pw_name) != 0)
            return;
    }
    else {
        if (user_id != info.st_uid)
            return;
    }

    //check how to treat mtime and then address the flag
    time_t now = time(0);
    if (mtime >= 0){
        if (now - info.st_mtime <= mtime)
            return;
    }
    else {
        if (now - info.st_mtime >= mtime * -1)
            return;
    }

    //address the -l flag if provided
    if (optl == 1 && !S_ISLNK(info.st_mode)){
        return;
    }

    
    if (S_ISLNK(info.st_mode)){
        int r;
        if ((r = readlink(path_name, symlink, 1025)) < 0){
            fprintf(stderr, "readlink error for %s\n", path_name);
            exit(1);
        }
        
        symlink[r] = '\0';

        if (optl == 1) {
            struct stat syminfo;
            struct stat targinfo;
            if (stat(symlink, &syminfo) >= 0 && stat(targetlink, &targinfo) >= 0){
                if (syminfo.st_ino != targinfo.st_ino)
                    return;
            }
            else
                return;
        }
    }


    //set the executable permissions for everyone
    if ((info.st_mode & S_IXUSR) && (info.st_mode & S_ISUID))
        uxbit = 's';
    else if (info.st_mode & S_ISUID)
        uxbit = 'S';
    else if (info.st_mode & S_IXUSR)
        uxbit = 'x';

    if ((info.st_mode & S_IXGRP) && (info.st_mode & S_ISGID))
        gxbit = 's';
    else if (info.st_mode & S_ISGID)
        gxbit = 'S';
    else if (info.st_mode & S_IXGRP)
        gxbit = 'x';

    if ((info.st_mode & S_IXOTH) && (info.st_mode & S_ISVTX))
        oxbit = 't';
    else if (info.st_mode & S_ISVTX)
        oxbit = 'T';
    else if (info.st_mode & S_IXOTH)
        oxbit = 'x';


    //handle all printing
    printf("%x/",       info.st_dev);
    printf("%-8llu ",   info.st_ino);
    
    if (S_ISBLK(info.st_mode))
        printf("b");
    else if (S_ISCHR(info.st_mode))
        printf("c");
    else if (S_ISLNK(info.st_mode))
        printf("l");
    else if (S_ISFIFO(info.st_mode))
        printf("p");
    else if (S_ISSOCK(info.st_mode))
        printf("s");
    else
        printf((S_ISDIR(info.st_mode))  ? "d" : "-");

    printf( (info.st_mode & S_IRUSR) ? "r" : "-");
    printf( (info.st_mode & S_IWUSR) ? "w" : "-");
    printf("%c",        uxbit );
    printf( (info.st_mode & S_IRGRP) ? "r" : "-");
    printf( (info.st_mode & S_IWGRP) ? "w" : "-");
    printf("%c",        gxbit );
    printf( (info.st_mode & S_IROTH) ? "r" : "-");
    printf( (info.st_mode & S_IWOTH) ? "w" : "-");
    printf("%c",        oxbit );
    printf(" %4d",      info.st_nlink);
    printf(" %-10s",    pswd->pw_name);
    printf(" %-8s",    group->gr_name);
    

    if (S_ISBLK(info.st_mode) || S_ISCHR(info.st_mode))
        printf(" %10x",     info.st_rdev);
    else
        printf(" %10llu",   info.st_size);


    //display time
    char date[20];
    strftime(date, 20, "%Y-%m-%d %H:%M", localtime(&(info.st_mtime)));
    printf(" %s",       date);
    printf(" %s",       path_name);

    if (S_ISLNK(info.st_mode))
        printf(" -> %s", symlink);

    printf("\n");
}
//display the directory given the path name and stat information
display the directory given the path name and stat information
display the directory given the path name and stat information
void display_directory(char *path_name, struct stat info){
    char symlink[1025];
    char uxbit = '-';
    char gxbit = '-';
    char oxbit = '-';
    struct passwd *pswd;
    struct group *group;
    if ((pswd = getpwuid(info.st_uid)) == NULL){
        fprintf(stderr, "getpwuid() error for %s\n", path_name);
        exit(1);
    }
    if ((group = getgrgid(info.st_gid)) == NULL){
        fprintf(stderr, "getgrgid() error for %s\n", path_name);
        exit(1);
    }
    if (user_id == 0){
        if (strcmp(username, "") != 0  && strcmp(username, pswd->pw_name) != 0)
            return;
    }
    else {
        if (user_id != info.st_uid)
            return;
    }
    time_t now = time(0);
    if (mtime >= 0){
        if (now - info.st_mtime <= mtime)
            return;
    }
    else {
        if (now - info.st_mtime >= mtime * -1)
            return;
    }
    if (optl == 1 && !S_ISLNK(info.st_mode)){
        return;
    }
    if (S_ISLNK(info.st_mode)){
        int r;
        if ((r = readlink(path_name, symlink, 1025)) < 0){
            fprintf(stderr, "readlink error for %s\n", path_name);
            exit(1);
        }
        symlink[r] = '\0';
        if (optl == 1) {
            struct stat syminfo;
            struct stat targinfo;
            if (stat(symlink, &syminfo) >= 0 && stat(targetlink, &targinfo) >= 0){
                if (syminfo.st_ino != targinfo.st_ino)
                    return;
            }
            else
                return;
        }
    }
    if ((info.st_mode & S_IXUSR) && (info.st_mode & S_ISUID))
        uxbit = 's';
    else if (info.st_mode & S_ISUID)
        uxbit = 'S';
    else if (info.st_mode & S_IXUSR)
        uxbit = 'x';
    if ((info.st_mode & S_IXGRP) && (info.st_mode & S_ISGID))
        gxbit = 's';
    else if (info.st_mode & S_ISGID)
        gxbit = 'S';
    else if (info.st_mode & S_IXGRP)
        gxbit = 'x';
    if ((info.st_mode & S_IXOTH) && (info.st_mode & S_ISVTX))
        oxbit = 't';
    else if (info.st_mode & S_ISVTX)
        oxbit = 'T';
    else if (info.st_mode & S_IXOTH)
        oxbit = 'x';
    printf("%x/",       info.st_dev);
    printf("%-8llu ",   info.st_ino);
    if (S_ISBLK(info.st_mode))
        printf("b");
    else if (S_ISCHR(info.st_mode))
        printf("c");
    else if (S_ISLNK(info.st_mode))
        printf("l");
    else if (S_ISFIFO(info.st_mode))
        printf("p");
    else if (S_ISSOCK(info.st_mode))
        printf("s");
    else
        printf((S_ISDIR(info.st_mode))  ? "d" : "-");
    printf( (info.st_mode & S_IRUSR) ? "r" : "-");
    printf( (info.st_mode & S_IWUSR) ? "w" : "-");
    printf("%c",        uxbit );
    printf( (info.st_mode & S_IRGRP) ? "r" : "-");
    printf( (info.st_mode & S_IWGRP) ? "w" : "-");
    printf("%c",        gxbit );
    printf( (info.st_mode & S_IROTH) ? "r" : "-");
    printf( (info.st_mode & S_IWOTH) ? "w" : "-");
    printf("%c",        oxbit );
    printf(" %4d",      info.st_nlink);
    printf(" %-10s",    pswd->pw_name);
    printf(" %-8s",    group->gr_name);
    if (S_ISBLK(info.st_mode) || S_ISCHR(info.st_mode))
        printf(" %10x",     info.st_rdev);
    else
        printf(" %10llu",   info.st_size);
    char date[20];
    strftime(date, 20, "%Y-%m-%d %H:%M", localtime(&(info.st_mtime)));
    printf(" %s",       date);
    printf(" %s",       path_name);
    if (S_ISLNK(info.st_mode))
        printf(" -> %s", symlink);
    printf("\n");
}
void display_directory ( char * path_name , struct stat info ) { char symlink [ 1025 ] ; char uxbit = ' ' ; char gxbit = ' ' ; char oxbit = ' ' ; struct passwd * pswd ; struct group * group ; if ( ( pswd = getpwuid ( info . st_uid ) ) == NULL ) { fprintf ( stderr , " \n " , path_name ) ; exit ( 1 ) ; } if ( ( group = getgrgid ( info . st_gid ) ) == NULL ) { fprintf ( stderr , " \n " , path_name ) ; exit ( 1 ) ; } if ( user_id == 0 ) { if ( strcmp ( username , " " ) != 0 && strcmp ( username , pswd -> pw_name ) != 0 ) return ; } else { if ( user_id != info . st_uid ) return ; } time_t now = time ( 0 ) ; if ( mtime >= 0 ) { if ( now - info . st_mtime <= mtime ) return ; } else { if ( now - info . st_mtime >= mtime * -1 ) return ; } if ( optl == 1 && ! S_ISLNK ( info . st_mode ) ) { return ; } if ( S_ISLNK ( info . st_mode ) ) { int r ; if ( ( r = readlink ( path_name , symlink , 1025 ) ) < 0 ) { fprintf ( stderr , " \n " , path_name ) ; exit ( 1 ) ; } symlink [ r ] = ' \0 ' ; if ( optl == 1 ) { struct stat syminfo ; struct stat targinfo ; if ( stat ( symlink , & syminfo ) >= 0 && stat ( targetlink , & targinfo ) >= 0 ) { if ( syminfo . st_ino != targinfo . st_ino ) return ; } else return ; } } if ( ( info . st_mode & S_IXUSR ) && ( info . st_mode & S_ISUID ) ) uxbit = ' ' ; else if ( info . st_mode & S_ISUID ) uxbit = ' ' ; else if ( info . st_mode & S_IXUSR ) uxbit = ' ' ; if ( ( info . st_mode & S_IXGRP ) && ( info . st_mode & S_ISGID ) ) gxbit = ' ' ; else if ( info . st_mode & S_ISGID ) gxbit = ' ' ; else if ( info . st_mode & S_IXGRP ) gxbit = ' ' ; if ( ( info . st_mode & S_IXOTH ) && ( info . st_mode & S_ISVTX ) ) oxbit = ' ' ; else if ( info . st_mode & S_ISVTX ) oxbit = ' ' ; else if ( info . st_mode & S_IXOTH ) oxbit = ' ' ; printf ( " " , info . st_dev ) ; printf ( " " , info . st_ino ) ; if ( S_ISBLK ( info . st_mode ) ) printf ( " " ) ; else if ( S_ISCHR ( info . st_mode ) ) printf ( " " ) ; else if ( S_ISLNK ( info . st_mode ) ) printf ( " " ) ; else if ( S_ISFIFO ( info . st_mode ) ) printf ( " " ) ; else if ( S_ISSOCK ( info . st_mode ) ) printf ( " " ) ; else printf ( ( S_ISDIR ( info . st_mode ) ) ? " " : " " ) ; printf ( ( info . st_mode & S_IRUSR ) ? " " : " " ) ; printf ( ( info . st_mode & S_IWUSR ) ? " " : " " ) ; printf ( " " , uxbit ) ; printf ( ( info . st_mode & S_IRGRP ) ? " " : " " ) ; printf ( ( info . st_mode & S_IWGRP ) ? " " : " " ) ; printf ( " " , gxbit ) ; printf ( ( info . st_mode & S_IROTH ) ? " " : " " ) ; printf ( ( info . st_mode & S_IWOTH ) ? " " : " " ) ; printf ( " " , oxbit ) ; printf ( " " , info . st_nlink ) ; printf ( " " , pswd -> pw_name ) ; printf ( " " , group -> gr_name ) ; if ( S_ISBLK ( info . st_mode ) || S_ISCHR ( info . st_mode ) ) printf ( " " , info . st_rdev ) ; else printf ( " " , info . st_size ) ; char date [ 20 ] ; strftime ( date , 20 , " " , localtime ( & ( info . st_mtime ) ) ) ; printf ( " " , date ) ; printf ( " " , path_name ) ; if ( S_ISLNK ( info . st_mode ) ) printf ( " " , symlink ) ; printf ( " \n " ) ; }
display the directory given the path name and stat information
display the directory given the path name and stat information
//check if username was 'user' or 'uid' and then address the flag //check how to treat mtime and then address the flag //address the -l flag if provided //set the executable permissions for everyone //handle all printing //display time