Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- /*
- Jaren Rowan
- COMP 322/L
- Project 5
- */
- #include <stdlib.h>
- #include <stdio.h>
- #include <sys/types.h>
- #include <string.h>
- #include <errno.h>
- #include <dirent.h>
- #include <limits.h>
- static void
- listdir (const char * dir_name){
- DIR * dir;
- /* Open the directory specified by "dir_name". */
- dir = opendir (dir_name);
- /* Check it was opened. */
- if (! dir){
- fprintf (stderr, "Cannot open directory '%s': %s\n", dir_name, strerror (errno));
- exit (EXIT_FAILURE);
- }
- while (1){
- struct dirent * entry;
- const char * d_name;
- /* "Readdir" gets subsequent entries from "d". */
- entry = readdir (dir);
- if (! entry) {
- /* There are no more entries in this directory, so break
- out of the while loop. */
- break;
- }
- d_name = entry->d_name;
- if(entry->d_type & DT_DIR) {
- /* Check that the directory is not "d" or d's parent. */
- if(strcmp (d_name, "..") != 0 && strcmp (d_name, ".") != 0) {
- int path_length;
- char path[PATH_MAX];
- path_length = snprintf (path, PATH_MAX,"%s/%s", dir_name, d_name);
- printf ("%s\n", d_name);
- if (path_length >= PATH_MAX) {
- fprintf (stderr, "Path length has got too long.\n");
- exit (EXIT_FAILURE);
- }
- /* Recursively call "listdir" with the next. */
- listdir (path);
- }
- }
- }
- /* After going through all the entries, close the directory. */
- if (closedir (dir)) {
- fprintf (stderr, "Could not close '%s': %s\n",dir_name, strerror (errno));
- exit (EXIT_FAILURE);
- }
- }
- int main(int argc, char **argv) {
- int x = 1;
- if (argc == 1)
- listdir(".");
- while (++x <= argc){
- listdir(argv[x-1]);
- }
- return 0;
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement