#include #include #include #include #include #include #include using namespace std; void TellAboutTime(string label, timespec theTime){ cout << "\t" << label <<" " << asctime(localtime(&theTime.tv_sec)); cout << "\t\tand " << theTime.tv_nsec << " nanoseconds " << endl; return; } void TellAboutUser(uid_t uid) { struct passwd * pwd; cout << "\tThe user id is " << uid << endl; pwd = getpwuid(uid); if (pwd == NULL) { perror("getpwuid"); } else { cout << "\t\tThat is " << pwd->pw_name << endl; } return; } void TellAboutGroup(gid_t gid){ cout << "\tThe group id is " << gid << endl; return; } void TellPerms(mode_t mode) { // also S_ISGID, S_ISVTX if (S_ISUID & mode) { cout << "\t\t\tsetuid" << endl; } // also S_IxGRP, S_IxOTH if (S_IRUSR & mode) { cout << "\t\t\tuser read" << endl; } if (S_IWUSR & mode) { cout << "\t\t\tuser write" << endl; } if (S_IXUSR & mode) { cout << "\t\t\tuser execute" << endl; } } void TellAboutMode(mode_t mode) { // way 1 to find file type. if (S_ISREG(mode) ) { cout << "\tThis is a regular file " << endl; } else if (S_ISDIR(mode) ) { cout << "\tThis is a directory " << endl; } // way 2 to find file type. switch (mode & S_IFMT) { case S_IFREG : cout << "\t\tRegular File" << endl; break; case S_IFDIR : cout << "\t\tDirectory File" << endl; break; case S_IFCHR : cout << "\t\tCharacter Device" << endl; break; case S_IFBLK : cout << "\t\tBlock Device" << endl; break; case S_IFIFO : cout << "\t\tPipe" << endl; break; case S_IFSOCK : cout << "\t\tSocket" << endl; break; case S_IFLNK : cout << "\t\tSymbolic Link" << endl; break; } TellPerms(mode); return; } void TellAbout(char * file) { int rv; struct stat fileInfo; rv = lstat(file, &fileInfo); if (rv != 0) { cerr << file << endl; perror("stat failed "); } else { cout << file << " has the following properties " << endl; cout << "\tIt is on device " << major(fileInfo.st_dev) << "." << minor(fileInfo.st_dev) << endl; cout << "\tThe inode is " << fileInfo.st_ino << endl; cout << "\tThis file has " << fileInfo.st_nlink << " links " << endl; cout << "\tFile Size " << fileInfo.st_size << " bytes or " << fileInfo.st_size/1014 << "K" << endl; TellAboutMode(fileInfo.st_mode); TellAboutUser(fileInfo.st_uid); TellAboutGroup(fileInfo.st_gid); TellAboutTime("Access Time", fileInfo.st_atim); TellAboutTime("Modification Time", fileInfo.st_mtim); TellAboutTime("Change Time", fileInfo.st_ctim); } cout << endl; return; } int main(int argc, char * argv[]) { int i; for(i=1;i