home *** CD-ROM | disk | FTP | other *** search
/ OS/2 Shareware BBS: 5 Edit / 05-Edit.zip / most423.zip / dir.c < prev    next >
C/C++ Source or Header  |  1997-03-05  |  3KB  |  134 lines

  1. /* directory functions-- mainly unix stuff, needs work for VMS */
  2. #ifndef VMS
  3. #include <sys/types.h>
  4. #ifdef __EMX__
  5. # include <dirent.h>
  6. #else
  7. # include <sys/dir.h>
  8. #endif
  9. #include <unistd.h>
  10. #endif
  11.  
  12. #include <stdlib.h>
  13.  
  14. #include "buffer.h"
  15. #include "window.h"
  16. #include "file.h"
  17. #include "externs.h"
  18.  
  19. void get_cdir(char *dir)
  20. {
  21. #ifndef VMS
  22.     int i;
  23.     getwd(dir);
  24.     i = strlen(dir); dir[i] = '/'; dir[i] = '\0';
  25. #else
  26.     getcwd(dir,80);
  27. #endif
  28. }
  29.  
  30.  
  31. #ifndef VMS
  32. /* This is a cheap routine-- should be replaced!!!! */
  33. void bubble_sort()
  34. {
  35.     unsigned char *this, *next, *save;
  36.     char tmp[80], *t;
  37.     int change;
  38.  
  39.     change = 1;
  40.     while(change)
  41.       {
  42.           next = this = BEG;
  43.           while((next < EOB) && (*next++ != 0));
  44.           if (next == EOB) return;
  45.           change = 0;
  46.           while(next < EOB)
  47.             {
  48.                 if (strcmp((char *)this,(char *) next) > 0)
  49.                   {
  50.                       change = 1;
  51.                       strcpy(tmp,(char *) this);
  52.                       t = tmp;
  53.                       while(*next != 0) *this++ = *next++;
  54.                       *this++ = 0;
  55.                       save = this;
  56.                       /* now next points at a null */
  57.                       next++;
  58.                       while(*t != 0) *this++ = *t++;
  59.                       *this = 0;
  60.                       this = save;
  61.                   }
  62.                 else
  63.                   {
  64.                       this = next;
  65.                       while(next < EOB && *next++ != 0);
  66.                   }
  67.             }
  68.       }
  69. }
  70.  
  71. int get_dir(char *dir)
  72. {
  73.     DIR *dirp;
  74.     struct dirent *dp;
  75.     char *p;
  76.     unsigned char *pos;
  77.     int dsize, len, size = 4096, i;
  78.  
  79.     if (*dir == 0)
  80.       dirp = opendir(".");
  81.     else
  82.       dirp = opendir(dir);
  83.  
  84.     if (dirp == NULL)
  85.       {
  86.           message("Unable to open directory.",1);
  87.           return(0);
  88.       }
  89.  
  90.  
  91.     BEG = (unsigned char *) MALLOC(size);
  92.     strcpy(BUF->file,dir);
  93.     pos = BEG;
  94.     dsize = size;
  95.     EOB = BEG + size;
  96.  
  97.     for(dp = readdir(dirp); dp != NULL; dp = readdir(dirp))
  98.       {
  99.           p = dp->d_name;
  100.           len = dp->d_namlen;
  101.           if (pos + len >= EOB - 1)
  102.             {
  103.                 size += dsize;
  104.                 BEG = (unsigned char *) REALLOC(BEG,size);
  105.  
  106.                 EOB = BEG + size;
  107.                 pos = EOB - dsize;
  108.             }
  109.           strcpy(pos,p);
  110.           pos += len;
  111.           /*           *pos++ = '\n'; */
  112.           *pos++ = '\0';
  113.       }
  114.  
  115.     BUF->end = EOB = pos;
  116.     BUF->beg = BEG;
  117.     closedir(dirp);
  118.     bubble_sort();
  119.     i = 0;
  120.     for (pos = BEG; pos < EOB; pos++)
  121.       if (*pos == '\0')
  122.         {
  123.             if (i = !i, i) *pos = '\t'; else *pos = '\n';
  124.         }
  125.  
  126.  
  127.    NUM_LINES = count_lines(BEG,EOB);
  128.    BUF->fd = -1;
  129.    return(1);
  130. }
  131. #endif
  132.  
  133.  
  134.