/* This file is part of the sample code and exercises * used by the class "Advanced Programming in the UNIX * Environment" taught by Jan Schaumann * at Stevens Institute of * Technology. * * This file is in the public domain. * * You don't have to, but if you feel like * acknowledging where you got this code, you may * reference me by name, email address, or point * people to the course website: * https://stevens.netmeister.org/631/ */ /* * simple-ls.c * Extremely low-power ls clone. * ./simple-ls . */ #include #include #include #include #include #include #include #include #include void printOwner(char *name, struct stat sb) { struct passwd *pwd; pwd = getpwuid(sb.st_uid); printf("%s (%s)\n", name, pwd->pw_name); } int main(int argc, char **argv) { DIR *dp; struct dirent *dirp; if (argc != 2) { fprintf(stderr, "usage: %s dir_name\n", argv[0]); exit(1); } if ((dp = opendir(argv[1])) == NULL) { fprintf(stderr, "can't open '%s'\n", argv[1]); exit(1); } if (chdir(argv[1]) == -1) { fprintf(stderr, "can't chdir to '%s': %s\n", argv[1], strerror(errno)); exit(EXIT_FAILURE); } while ((dirp = readdir(dp)) != NULL) { struct stat sb; if (stat(dirp->d_name, &sb) == -1) { fprintf(stderr, "Can't stat %s: %s\n", dirp->d_name, strerror(errno)); continue; } printOwner(dirp->d_name, sb); } closedir(dp); return(0); }