int main(int argc, char **argv){ int opt; while ((opt = getopt(argc, argv, "a")) != -1){ switch(opt){ case 'a': hidden_files_flag = true; break; default: printf("No such option as %c", opt); break; } } DIR *dir = opendir("."); struct dirent *entry; while ((entry = readdir(dir)) != NULL) { printf("%s\n", entry->d_name); } stat("./", &file_stat); printf("%d\n", file_stat.st_size); printf("%d\n", file_stat.st_uid); print_file_type(file_stat.st_mode); struct passwd *pwd = getpwuid(file_stat.st_uid); printf("%d\n", pwd->pw_name); printf("%d\n", file_stat.st_mtime); return 0; }
void display_file_info(const char *filename) { // Local parameters int i; // Loop index char *token, *prev_token; // For string tokenizing struct stat buf; if (lstat(filename, &buf) < 0) { fprintf(stderr, "stat error: %s\n", strerror(errno)); return; } // Print permissions print_permissions(&buf); printf("\t"); // Print the number of links to the file printf("%lu\t", (size_t) buf.st_nlink); // Print the user id of the file struct passwd *pwd = getpwuid(buf.st_uid); printf("%s\t", pwd->pw_name); // Print the group id of the file struct group *grp = getgrgid(buf.st_gid); printf("%s\t", grp->gr_name); // Print the size of the file printf("%lu\t", (size_t) buf.st_size); // Print the time of last modification char buffer[MAX_STRING_SIZE]; strftime(buffer, MAX_STRING_SIZE, "%b %d %H:%M", localtime(&(buf.st_mtime))); printf("%s\t", buffer); // Duplicate filename contents for (i = 0; i < strlen(filename); ++i) { buffer[i] = filename[i]; } buffer[ strlen(filename) ] = 0; // Get the last '/' delimeted string; the local filename token = strtok(buffer, "/"), prev_token = 0; while ((token = strtok(0, "/")) != 0) { prev_token = token; } printf("%s", prev_token); // Determine the type of file print_file_type(&buf); if (S_ISLNK(buf.st_mode)) { printf(" -> "); // Zero buffer contents int i; char buffer[MAX_STRING_SIZE]; for (i = 0; i < MAX_STRING_SIZE; ++i) { buffer[i] = '\0'; } readlink(filename, buffer, MAX_STRING_SIZE); printf("%s", buffer); } printf("\n"); }