#include #include #include #include #include #include #include #include int main(int argc, char* argv[]) { struct dirent **files; struct stat mystat; struct passwd pwd; struct passwd *pwdp; struct group grp; struct group *grpp; struct tm time; ssize_t linker; int i, sum, j; char* buffer[256]; char path[256]; char newpath[256]; char* linked[256]; int dir_count; for (j = 1; j <= argc; j++) { getcwd(path, sizeof(path)); if (argc > 1) { strcat(path, "/"); strcat(path, argv[j]); // keeping the recieved path } strcat(path, "/"); dir_count = scandir(path, &files, NULL, alphasort); sum = 0; printf("\ndir: %s \n", path); for (i = 0; i < dir_count; i++) { strcat(newpath, path); strcat(newpath, files[i]->d_name); printf("\n %s ", newpath); if (lstat(files[i]->d_name, &mystat) == 0) { sum += mystat.st_blocks; } else { perror("stat() error"); } strcpy(newpath, ""); } printf("total: %d\n", sum / 2); for (i = 0; i < dir_count; i++) { strcat(newpath, path); strcat(newpath, files[i]->d_name); if (lstat(newpath, &mystat) == 0) { if (S_ISDIR(mystat.st_mode)) printf("d"); else if (S_ISLNK(mystat.st_mode)) printf("l"); else if (S_ISREG(mystat.st_mode)) printf("-"); if ((mystat.st_mode & S_IRUSR) == S_IRUSR) printf("r"); else printf("-"); if ((mystat.st_mode & S_IWUSR) == S_IWUSR) printf("w"); else printf("-"); if ((mystat.st_mode & S_IXUSR) == S_IXUSR) printf("x"); else printf("-"); if ((mystat.st_mode & S_IRGRP)== S_IRGRP) printf("r"); else printf("-"); if ((mystat.st_mode & S_IWGRP)== S_IWGRP) printf("w"); else printf("-"); if ((mystat.st_mode & S_IXGRP)== S_IXGRP) printf("x"); else printf("-"); if ((mystat.st_mode & S_IROTH)== S_IROTH) printf("r"); else printf("-"); if ((mystat.st_mode & S_IWOTH)== S_IWOTH) printf("w"); else printf("-"); if ((mystat.st_mode & S_IXOTH)== S_IXOTH) printf("x"); else printf("-"); printf("%3d ", mystat.st_nlink); getpwuid_r(mystat.st_uid, &pwd, buffer, sizeof(buffer), &pwdp); getgrgid_r(mystat.st_gid, &grp, buffer, sizeof(buffer), &grpp); printf("%.5s ", pwd.pw_name); printf("%5s ", grp.gr_name); printf("%d ", mystat.st_size); localtime_r(&mystat.st_mtime, &time); strftime(buffer, sizeof(buffer), "%F %T", &time); printf("%s ", buffer); printf("%s", files[i]->d_name); if (S_ISLNK(mystat.st_mode)) { linker = readlink(files[i]->d_name, linked, sizeof(linked)); printf(" -> %s", linked); } printf("\n"); } else { perror("stat() error"); exit(1); } strcpy(newpath, ""); } strcpy(path, ""); } }