lsflib/src/getdir.c
changeset 6 c3dc3eb3b541
child 9 c3fecc82ade6
equal deleted inserted replaced
5:d752cbe8208e 6:c3dc3eb3b541
       
     1 /*
       
     2  *  $Id: getdir.c 94 2008-04-05 01:27:30Z mbroeker $
       
     3  * $URL: http://localhost/svn/c/lsflib/trunk/src/getdir.c $
       
     4  *
       
     5  */
       
     6 
       
     7 #include <stdio.h>
       
     8 #include <stdlib.h>
       
     9 #include <dirent.h>
       
    10 #include <sys/types.h>
       
    11 #include <sys/stat.h>
       
    12 #include <unistd.h>
       
    13 #include <string.h>
       
    14 
       
    15 #include <lsf.h>
       
    16 
       
    17 int isDir (char *filename)
       
    18 {
       
    19     struct stat buf;
       
    20 
       
    21     if (!stat (filename, &buf))
       
    22         return (S_ISDIR (buf.st_mode));
       
    23 
       
    24     return 0;
       
    25 }
       
    26 
       
    27 int isFile (char *filename)
       
    28 {
       
    29     struct stat buf;
       
    30 
       
    31     if (!stat (filename, &buf))
       
    32         return (S_ISREG (buf.st_mode));
       
    33 
       
    34     return 0;
       
    35 }
       
    36 
       
    37 void getdir (char *root, int recursive)
       
    38 {
       
    39     struct dirent *entry;
       
    40     DIR *directory;
       
    41     char *list;
       
    42 
       
    43     if (!isDir (root))
       
    44         return;
       
    45 
       
    46     if ((directory = opendir (root)) == NULL) {
       
    47         perror (root);
       
    48         return;
       
    49     }
       
    50 
       
    51     while ((entry = readdir (directory))) {
       
    52         list = (char *)malloc (strlen (root) + 1 + strlen (entry->d_name) + 1);
       
    53 
       
    54         if (list == NULL) {
       
    55             perror ("malloc");
       
    56             return;
       
    57         }
       
    58 
       
    59         sprintf (list, "%s/%s", root, entry->d_name);
       
    60 
       
    61         if (!strcmp (entry->d_name, "."))
       
    62             continue;
       
    63         if (!strcmp (entry->d_name, ".."))
       
    64             continue;
       
    65         if (isDir (list)) {
       
    66             printf ("Directory: %s\n", list);
       
    67             if (recursive)
       
    68                 getdir (list, recursive);
       
    69             continue;
       
    70         }
       
    71 
       
    72         printf ("%s\n", list);
       
    73     }
       
    74 
       
    75     free (list);
       
    76     closedir (directory);
       
    77 }