// An example showing how to use stat and // extract information about the file type // and access permissions #include #include #include #include #include #include int main(int argc, char* argv[]){ struct stat s; if( argc < 2){ fprintf(stderr,"Usage...\n"); return 1; } //man 2 stat for system call //man 3 stat for struct if(stat(argv[1],&s) < 0){ perror(""); return 1; } printf("size: %ld\n", s.st_size ); printf("mode %u %o\n",s.st_mode, s.st_mode); //man 7 inode for more details if (s.st_mode & S_IRUSR){ printf("Owner can read\n"); } else { printf("Owner can't read\n"); } if (s.st_mode & S_IXOTH){ printf("Others can execute\n"); } else { printf("Others can't execute\n"); } if(S_ISREG(s.st_mode)){ printf("This is a regular file\n"); } else { printf("This is not a regular file\n"); } if(S_ISDIR(s.st_mode)){ printf("This is a dir\n"); } else { printf("This is not a dir\n"); } if(S_ISLNK(s.st_mode)){ printf("This is a symbolic link\n"); } else { printf("This is not a symbolic link\n"); } return 0; }