#include #include #include #include #include #include #include #include #include #include #include #include #include static long unsigned int total_size; int sum(const char *fpath, const struct stat *sb, int typeflag) { total_size += sb->st_size; return 0; } void print_perms(mode_t perms) { printf( (perms & S_IRUSR) ? "r" : "-" ); printf( (perms & S_IWUSR) ? "w" : "-" ); printf( (perms & S_IXUSR) ? "x" : "-" ); printf( (perms & S_IRGRP) ? "r" : "-" ); printf( (perms & S_IWGRP) ? "w" : "-" ); printf( (perms & S_IXGRP) ? "x" : "-" ); printf( (perms & S_IROTH) ? "r" : "-" ); printf( (perms & S_IWOTH) ? "w" : "-" ); printf( (perms & S_IXOTH) ? "x" : "-" ); } int main(int argc, char *argv[]) { total_size = 0; char *dirname = "./"; if(argc > 1) dirname = argv[1]; DIR *dp = opendir(dirname); struct dirent *dirp; struct group *grp; struct passwd *pwd; struct tm *tm; struct stat statbuf; char datestring[256]; while((dirp = readdir(dp)) != NULL) { if(!strcmp(dirp->d_name, ".") || !strcmp(dirp->d_name, "..")) continue; stat(dirp->d_name, &statbuf); printf(S_ISDIR(statbuf.st_mode) ? "d" : "-"); print_perms(statbuf.st_mode); printf(" %4d", (int)statbuf.st_nlink); if( (pwd = getpwuid(statbuf.st_uid)) != NULL) printf(" %-8.8s", pwd->pw_name); else printf(" %-8d", statbuf.st_uid); if( (grp = getgrgid(statbuf.st_gid)) != NULL) printf(" %-8.8s", grp->gr_name); else printf(" %-8d", statbuf.st_gid); if(S_ISDIR(statbuf.st_mode)) { ftw(dirp->d_name, &sum, 1); printf(" %9jd", (intmax_t)total_size); } else printf(" %9jd", (intmax_t)statbuf.st_size); tm = localtime(&statbuf.st_mtime); strftime(datestring, sizeof(datestring), nl_langinfo(D_T_FMT), tm); printf(" %s %s\n", datestring, dirp->d_name); } return 1; }