c - Displaying File Permissions in UNIX -
i trying type c program display user's permissions files typed on command line. test it, typed pathname of file in main function. shows error saying "couldn't stat comp_sci/project_1/test.c"
i've checked directory , contain file named test.c think error in code.
# include <stdio.h> # include <stdlib.h> # include <sys/types.h> # include <sys/stat.h> int filedata(const char *pathname) { // call filedata function parameter pathname // use octarray determining if permission bits set static short octarray[9] = { 0400, 0200, 0100, 0040, 0020, 0010, 0004, 0002, 0001 }; // mnemonic codes file permission, 10 characters long because of terminating null static char perms[10] = "rwxrwxrwx"; struct stat statbuf; // statbuf name of our stat structure char descrip[10]; // descrip array of chars of length 10 int j; if (stat(pathname, &statbuf) == -1) { fprintf(stderr, "couldn't stat %s\n", pathname); return (-1); } // put permissions readable form (j = 0; j < 9; j++) { // test whether permission set using bitwise , if (statbuf.st_mode & octarray[j]) { descrip[j] = perms[j]; } else { descrip[j] = '-'; } } // end for-loop descrip[9] = '\0'; // make sure have string // display file information printf("\nfile %s :\n", pathname); //printf("\nsize %ld bytes\n", statbuf.st_size); //printf("user_id %d, group_id %d\n\n", statbuf.st_uid, statbuf.st_gid); printf("permissions: %s\n", descrip); return (0); } int main (int argc, char *argv[]) { filedata ("comp_sci/project_1/test.c"); return(0); }
Comments
Post a Comment