1: /*************************************************************************
   2:  * This program is copyright (C) 1985, 1986 by Jonathan Payne.  It is    *
   3:  * provided to you without charge for use only on a licensed Unix        *
   4:  * system.  You may copy JOVE provided that this notice is included with *
   5:  * the copy.  You may not sell copies of this program or versions        *
   6:  * modified for use on microcomputer systems, unless the copies are      *
   7:  * included with a Unix system distribution and the source is provided.  *
   8:  *************************************************************************/
   9: 
  10: #include "jove.h"
  11: #include <sys/stat.h>
  12: #include <sys/dir.h>
  13: 
  14: #ifdef F_COMPLETION
  15: 
  16: #ifdef BSD4_2
  17: 
  18: #define DIRSIZE(entry)  DIRSIZ(entry)
  19: 
  20: #else
  21: 
  22: #define DIRSIZE(entry)  (min(strlen(entry->d_name), DIRSIZ))
  23: 
  24: typedef struct {
  25:     int d_fd;       /* File descriptor for this directory */
  26: } DIR;
  27: 
  28: DIR *
  29: opendir(dir)
  30: char    *dir;
  31: {
  32:     DIR *dp = (DIR *) malloc(sizeof *dp);
  33:     struct stat stbuf;
  34: 
  35:     if ((dp->d_fd = open(dir, 0)) == -1)
  36:         return 0;
  37:     if ((fstat(dp->d_fd, &stbuf) == -1) || !(stbuf.st_mode & S_IFDIR)) {
  38:         closedir(dp);
  39:         return 0;   /* this isn't a directory! */
  40:     }
  41:     return dp;
  42: }
  43: 
  44: closedir(dp)
  45: DIR *dp;
  46: {
  47:     (void) close(dp->d_fd);
  48:     free((char *) dp);
  49: }
  50: 
  51: struct direct *
  52: readdir(dp)
  53: DIR *dp;
  54: {
  55:     static struct direct    dir;
  56: 
  57:     do
  58:         if (read(dp->d_fd, &dir, sizeof dir) != sizeof dir)
  59:             return 0;
  60:     while (dir.d_ino == 0);
  61: 
  62:     return &dir;
  63: }
  64: 
  65: #endif BSD4_2
  66: 
  67: /* Scandir returns the number of entries or -1 if the directory cannoot
  68:    be opened or malloc fails. */
  69: 
  70: scandir(dir, nmptr, qualify, sorter)
  71: char    *dir;
  72: char    ***nmptr;
  73: int (*qualify)();
  74: int (*sorter)();
  75: {
  76:     DIR *dirp;
  77:     struct direct   *entry;
  78:     char    **ourarray;
  79:     unsigned int    nalloc = 10,
  80:             nentries = 0;
  81: 
  82:     if ((dirp = opendir(dir)) == 0)
  83:         return -1;
  84:     if ((ourarray = (char **) malloc(nalloc * sizeof (char *))) == 0)
  85: memfail:    complain("[Malloc failed: cannot scandir]");
  86:     while ((entry = readdir(dirp)) != 0) {
  87:         if (qualify != 0 && (*qualify)(entry->d_name) == 0)
  88:             continue;
  89:         if (nentries == nalloc) {
  90:             ourarray = (char **) realloc((char *) ourarray, (nalloc += 10) * sizeof (char *));
  91:             if (ourarray == 0)
  92:                 goto memfail;
  93:         }
  94:         ourarray[nentries] = (char *) malloc(DIRSIZE(entry) + 1);
  95:         null_ncpy(ourarray[nentries], entry->d_name, (int) DIRSIZE(entry));
  96:         nentries++;
  97:     }
  98:     closedir(dirp);
  99:     if ((nentries + 1) != nalloc)
 100:         ourarray = (char **) realloc((char *) ourarray,
 101:                     ((nentries + 1) * sizeof (char *)));
 102:     if (sorter != 0)
 103:         qsort((char *) ourarray, nentries, sizeof (char **), sorter);
 104:     *nmptr = ourarray;
 105:     ourarray[nentries] = 0;     /* guaranteed 0 pointer */
 106: 
 107:     return nentries;
 108: }
 109: 
 110: freedir(nmptr, nentries)
 111: char    ***nmptr;
 112: {
 113:     char    **ourarray = *nmptr;
 114: 
 115:     while (--nentries >= 0)
 116:         free(*ourarray++);
 117:     free((char *) *nmptr);
 118:     *nmptr = 0;
 119: }
 120: 
 121: alphacomp(a, b)
 122: char    **a,
 123:     **b;
 124: {
 125:     return strcmp(*a, *b);
 126: }
 127: 
 128: #endif

Defined functions

alphacomp defined in line 121; used 2 times
closedir defined in line 44; used 2 times
freedir defined in line 110; used 1 times
opendir defined in line 28; used 1 times
  • in line 82
readdir defined in line 51; used 1 times
  • in line 86
scandir defined in line 70; used 1 times

Defined macros

DIRSIZE defined in line 22; used 2 times
Last modified: 1986-03-28
Generated: 2016-12-26
Generated by src2html V0.67
page hit count: 962
Valid CSS Valid XHTML 1.0 Strict