#include #include // perror #include // opendir #include // opendir #include // stat #include // stat #include // strcpy, strcat using namespace std; void DoDir(string dirname); void DoFile(char * filename); bool IsDir(string dirname); int main(int argc, char * argv[]) { int i; if (argc > 1) { for (i=1;id_name); DoFile(buf); } if (-1 == closedir(dir)) { perror("closedir: "); } } cout << endl; return; } void PrintBit(bool flag, char a, char b) { if (flag) { cout << a; } else { cout << b; } return; } void DoPerms(mode_t m) { cout <<"\tPerm : "; PrintBit(S_IRUSR & m, 'r','-'); PrintBit(S_IWUSR & m, 'w','-'); PrintBit(S_IXUSR & m, 'x','-'); PrintBit(S_IRGRP & m, 'r','-'); PrintBit(S_IWGRP & m, 'w','-'); PrintBit(S_IXGRP & m, 'x','-'); PrintBit(S_IROTH & m, 'r','-'); PrintBit(S_IWOTH & m, 'w','-'); PrintBit(S_IXOTH & m, 'x','-'); cout << endl; } void DoType(mode_t m) { cout << "\tType : "; if (S_ISREG(m) ) { cout << "Regular File"; } else if (S_ISDIR(m)) { cout << "Directory"; } else if (S_ISCHR(m)) { cout << "Character Device"; } else if (S_ISBLK(m)) { cout << "Block Device"; } else if (S_ISFIFO(m)) { cout << "FIFO"; } else if (S_ISLNK(m)) { cout << "Symbolic Link"; } else if (S_ISSOCK(m)) { cout << "Socket"; } else { cout << "UNKNOWN"; } cout << endl; } void DoFile(char * filename){ struct stat fileInfo; if (-1 == lstat(filename, &fileInfo)) { perror("Stat"); cout << "Could not stat " << filename << endl; return; } cout << "Name : " << filename<< endl; DoType(fileInfo.st_mode); DoPerms(fileInfo.st_mode); cout << "\tSize: " << fileInfo . st_size << endl; cout << "\tOwner: " << fileInfo . st_uid << endl; cout << "\tGroup: " << fileInfo . st_gid << endl; cout << "\tLinks: " << fileInfo . st_nlink << endl; cout << "\tInode: " << fileInfo . st_ino << endl; cout << endl; return; }