home *** CD-ROM | disk | FTP | other *** search
/ Frozen Fish 1: Amiga / FrozenFish-Apr94.iso / bbs / alib / d1xx / d109 / uupc.lha / UUpc / Source / scandir.c < prev    next >
C/C++ Source or Header  |  1987-10-28  |  3KB  |  105 lines

  1. /*
  2.  *    scandir - a SysV library routine that simulates the
  3.  *          BSD 4.3 version.
  4.  *
  5.  *    returns:
  6.  *        -1 for failures
  7.  *        # of files found if successful
  8.  *
  9.  *    Produces a list of files which are contained in the
  10.  *    directory "dirname".
  11.  *
  12.  *    The list is returned through "namelist" which is an
  13.  *    array of pointers to (struct direct).  This list of
  14.  *    pointers are malloc'ed and should be freed when
  15.  *    done with.
  16.  *
  17.  *    The passed function "select" is passed a (struct direct)
  18.  *    pointer for each directory entry.  If "select" returns
  19.  *    non-zero the entry is included in "namelist", otherwise
  20.  *    it is not.  If "select" is NULL, all entries are returned.
  21.  *
  22.  *    The function "compar" is passed a pair of pointers to
  23.  *    (struct direct) and is used to sort "namelist" via qsort.
  24.  *    If "compar" is NULL, the names are unsorted.
  25.  *
  26.  *    This source is public domain and the author claims no
  27.  *    rights to it.  It may be copied, spindled, or mutilated.
  28.  *
  29.  *    R.J. Esposito - Bell of Pennsylvania
  30.  *
  31.  */
  32.  
  33. #include <stdio.h>
  34. /*#include <sys/types.h>*/
  35. #include <ndir.h>
  36.  
  37. int
  38. scandir(dirname, namelist, select, compar)
  39. char        *dirname;
  40. struct direct    *(*namelist[]);
  41. int        (*select)();
  42. int        (*compar)();
  43. {
  44.     DIR        *dfp;
  45.     struct direct    *dp;
  46.     register int    ii, nf;
  47.     char        *malloc();
  48.  
  49.     if ((dfp = opendir(dirname)) == NULL)    /* can't open directory */
  50.         return(-1);
  51.  
  52.     nf = 0;
  53.     while ((dp = readdir(dfp)) != NULL)    /* read thru direcetory */
  54.         if (select == NULL || (*select)(dp))
  55.             nf++;
  56.  
  57.     if (!nf)                /* nothing found */
  58.         return(0);
  59.  
  60.     /* malloc memory for the namelist array */
  61.  
  62.     *namelist = (struct direct **)malloc((nf+1)*sizeof(struct direct *));
  63.     if (*namelist == NULL) {
  64.         fprintf(stderr, "scandir: out of memory\n");
  65.         return(-1);
  66.     }
  67.  
  68.     for (ii = 0; ii < nf; ii++) {
  69.         (*namelist)[ii] = (struct direct *)malloc(sizeof(struct direct));
  70.         if ((*namelist)[ii] == NULL) {
  71.             fprintf(stderr, "scandir: out of memory\n");
  72.             return(-1);
  73.         }
  74.     }
  75.  
  76.     /* now re-read the directory loading up the namelist array */
  77.  
  78.     closedir( dfp );
  79.     if ((dfp = opendir(dirname)) == NULL)    /* can't open directory */
  80.         return(-1);
  81.  
  82.     (*namelist)[ii] = 0;
  83.     /*seekdir(dfp, 0L);*/
  84.     ii = 0;
  85.  
  86.     while ((dp = readdir(dfp)) != NULL) {
  87.         if (select == NULL || (*select)(dp)) {
  88.             (*namelist)[ii]->d_ino = dp->d_ino;
  89.             (*namelist)[ii]->d_reclen = dp->d_reclen;
  90.             (*namelist)[ii]->d_namlen = dp->d_namlen;
  91.             strcpy((*namelist)[ii]->d_name, dp->d_name);
  92.             ii++;
  93.         }
  94.     }
  95.  
  96.     closedir(dfp);
  97. #ifdef test
  98.     if (compar != NULL)        /* sort the list if required */
  99.         qsort((char **)*namelist, nf, sizeof(struct direct *), compar);
  100. #endif
  101.  
  102.     return(nf);
  103. }
  104.  
  105.