home *** CD-ROM | disk | FTP | other *** search
/ The CDPD Public Domain Collection for CDTV 4 / CDPD_IV.bin / networking / uucp / amigauucpsrc / lib / getfiles.c < prev    next >
Encoding:
C/C++ Source or Header  |  1994-06-29  |  1.5 KB  |  88 lines

  1.  
  2. /*
  3.  *  GETFILES.C
  4.  */
  5.  
  6. #include <stddef.h>
  7. #include <stdio.h>
  8. #include <stdlib.h>
  9. #include <string.h>
  10. #include "getfiles.h"
  11. #include "ndir.h"
  12. #include "version.h"
  13. #include "config.h"
  14.  
  15. IDENT(".01");
  16.  
  17. Prototype dir_list *getfiles(const char *, int, int (*)(char *), int (*)(dir_list *, dir_list *));
  18.  
  19. dir_list *
  20. getfiles(dir, gap, sel, cmp)
  21. const char *dir;
  22. int gap;
  23. int (*sel)(char *);
  24. int (*cmp)(dir_list *, dir_list *);
  25. {
  26.     register DIR *dirp;
  27.     register struct direct *dp;
  28.     register dir_list *this;
  29.     dir_list *first = NULL;    /*  assignment avoids warning, not reqd */
  30.  
  31.     if ((dirp = opendir(dir)) == NULL)
  32.     return NULL;
  33.  
  34.     this = (dir_list *)&first;
  35.     while (dp = readdir(dirp)) {
  36.     if (sel == NULL || (*sel)(dp->d_name)) {
  37.         this->next = malloc(offsetof(dir_list, name[0])
  38.                 + gap
  39.                 + strlen(dp->d_name)
  40.                 + 1
  41.                    );
  42.         if ((this = this->next) == NULL)
  43.         break;
  44.         strcpy(this->name + gap, dp->d_name);
  45.     }
  46.     }
  47.     this->next = NULL;
  48.     closedir(dirp);
  49.  
  50.     if (cmp == NULL)
  51.     return first;
  52.     return list_sort(first, (int (*)(void *, void *))cmp);
  53. }
  54.  
  55.  
  56. #ifdef TEST
  57.  
  58. sel(a)
  59. char *a;
  60. {
  61.     while (*a != '\0')
  62.         if (*a++ == '.' && *a == 'o')
  63.             return 1;
  64.     return 0;
  65. }
  66.  
  67. cmp(a, b)
  68. dir_list *a, *b;
  69. {
  70.     return strcmp(a->name, b->name);
  71. }
  72.  
  73. main()
  74. {
  75.     register dir_list *p, *q;
  76.  
  77.     p = getfiles("", 0, (void *)NULL, (void *)NULL);
  78.     while (p)
  79.         puts(p->name), q = p->next, free(p), p = q;
  80.  
  81.     p = getfiles("", sel, cmp);
  82.     while (p)
  83.         puts(p->name), q = p->next, free(p), p = q;
  84. }
  85.  
  86. #endif
  87.  
  88.