home *** CD-ROM | disk | FTP | other *** search
/ Usenet 1994 January / usenetsourcesnewsgroupsinfomagicjanuary1994.iso / sources / unix / volume22 / nn6.4 / part19 / dir.c next >
Encoding:
C/C++ Source or Header  |  1990-06-07  |  3.5 KB  |  193 lines

  1. /*
  2.  *    (c) Copyright 1990, Kim Fabricius Storm.  All rights reserved.
  3.  *
  4.  *    Directory access.
  5.  *     read file names in directory 'dir' starting with 'prefix'
  6.  */
  7.  
  8. #include "config.h"
  9. #include "articles.h"
  10. #include "dir.h"
  11.  
  12. static char dir_path[FILENAME], *dir_tail;
  13.  
  14. #ifdef HAVE_DIRECTORY
  15.  
  16. static string_marker str_mark;
  17. static char **completions = NULL;
  18. static char **comp_iterator;
  19. static char **comp_help;
  20.  
  21. /*
  22.  * list_directory scans the directory twice; first time to find out how
  23.  * many matches there are, and second time to save the names, after
  24.  * sufficient memory have been allocated to store it all.
  25.  */
  26.  
  27. static sort_directory(f1, f2)        /* Used by [Dqsort */
  28.     register char **f1;
  29.     register char **f2;
  30. {
  31.     return strcmp(*f1, *f2);
  32. }
  33.  
  34. list_directory(dir, prefix)
  35. char *dir, *prefix;
  36. {
  37.     DIR *dirp;
  38.     register Direntry *dp;
  39.     register char *cp;
  40.     register char **comp;
  41.     int pflen = strlen(prefix);
  42.     unsigned count = 0, comp_length = 0;
  43.  
  44.     if ((dirp = opendir(dir)) == NULL)
  45.     return 0;            /* tough luck */
  46.  
  47.     mark_str(&str_mark);
  48.  
  49.     while ((dp = readdir(dirp)) != NULL) {
  50.     cp = dp->d_name;
  51. #ifdef FAKED_DIRECTORY
  52.     if (dp->d_ino == 0) continue;
  53.     cp[14] = NUL;
  54. #endif
  55.     if (*cp == '.' && (cp[1] == '\0' || (cp[1] == '.' && cp[2] == '\0')))
  56.         continue;
  57.     if (pflen && strncmp(prefix, cp, pflen)) continue;
  58.     if (count == comp_length) {
  59.         comp_length += 100;
  60.         completions = resizeobj(completions, char *, comp_length + 1);
  61.         comp = completions + count;
  62.     }
  63.     strcpy(*comp++ = alloc_str(strlen(cp)), cp);
  64.     count++;
  65.     }
  66.     closedir(dirp);
  67.     if (count == 0) {
  68.     release_str(&str_mark);
  69.     return 0;
  70.     }
  71.  
  72.     quicksort(completions, count, char *, sort_directory);
  73.     *comp = (char *)0;
  74.     comp_iterator = completions;
  75.     comp_help = completions;
  76.  
  77.     dir_tail = dir_path;
  78.     while (*dir_tail++ = *dir++);
  79.     dir_tail[-1] = '/';
  80.  
  81.     return 1;
  82. }
  83.  
  84. next_directory(buffer, add_slash)
  85. register char *buffer;
  86. int add_slash;
  87. {
  88.     if (*comp_iterator != NULL) {
  89.     strcpy(buffer, *comp_iterator);
  90.  
  91.     if (add_slash) {
  92.         strcpy(dir_tail, *comp_iterator);
  93.         if (file_exist(dir_path, "d"))
  94.         strcat(buffer, "/");
  95.     }
  96.     comp_iterator++;
  97.     return 1;
  98.     }
  99.     close_directory();
  100.     return 0;
  101. }
  102.  
  103. compl_help_directory()
  104. {
  105.     list_completion((char *)NULL);
  106.  
  107.     if (*comp_help == NULL) comp_help = completions;
  108.     while (*comp_help && list_completion(*comp_help))
  109.         comp_help++;
  110.  
  111.     fl;
  112.     return 1;
  113. }
  114.  
  115. close_directory()
  116. {
  117.     if (completions) {
  118.     release_str(&str_mark);
  119.     freeobj(completions);
  120.     completions = NULL;
  121.     }
  122. }
  123.  
  124. #else
  125.  
  126. static FILE *dirf;
  127. static int prefix_lgt;
  128.  
  129. list_directory(dir, prefix)
  130. char *dir, *prefix;
  131. {
  132.     if (prefix[0])
  133.     sprintf(dir_path, "cd %s && echo %s* 2>/dev/null", dir, prefix);
  134.     else
  135.     sprintf(dir_path, "cd %s && ls 2>/dev/null", dir);
  136.     prefix_lgt = strlen(prefix);
  137.  
  138.     if ((dirf = popen(dir_path, "r")) == NULL) return 0;
  139.  
  140.     dir_tail = dir_path;
  141.     while (*dir_tail++ = *dir++);
  142.     dir_tail[-1] = '/';
  143.  
  144.     return 1;
  145. }
  146.  
  147. next_directory(buffer, add_slash)
  148. char *buffer;
  149. int add_slash;
  150. {
  151.     register char *cp;
  152.     register int c;
  153.  
  154.     cp = buffer;
  155.     while ((c = getc(dirf)) != EOF && (c != SP) && (c != NL))
  156.     *cp++ = c;
  157.  
  158.     if (cp != buffer) {
  159.     *cp = NUL;
  160.     if (strcmp(buffer + prefix_lgt, "*")) {
  161.  
  162.         if (!add_slash) return 1;
  163.  
  164.         strcpy(dir_tail, buffer);
  165.             if (file_exist(dir_path, "d")) {
  166.         *cp++ = '/';
  167.         *cp = NUL;
  168.         }
  169.  
  170.         return 1;
  171.     }
  172.  
  173.     }
  174.  
  175.     close_directory();
  176.     return 0;
  177. }
  178.  
  179. compl_help_directory()
  180. {
  181.     return 0;
  182. }
  183.  
  184. close_directory()
  185. {
  186.     if (dirf) {
  187.     pclose(dirf);
  188.     dirf = NULL;
  189.     }
  190. }
  191. #endif /* HAVE_DIRECTORY */
  192.  
  193.