home *** CD-ROM | disk | FTP | other *** search
/ The Pier Shareware 6 / The_Pier_Shareware_Number_6_(The_Pier_Exchange)_(1995).iso / 024 / psi110g.zip / LXDIR.C < prev    next >
C/C++ Source or Header  |  1994-04-17  |  3KB  |  99 lines

  1. /*
  2.  * Simulate DOS findfirst()/findnext().  We do this rather than rewriting
  3.  * the original code both (a) for simplicity and (b) because we also need to
  4.  * handle globbing.
  5.  */
  6.   
  7. #include <dirent.h>
  8. #include <sys/types.h>
  9. #include <sys/stat.h>
  10.   
  11. #define DIRUTIL_INTERNAL    /* so we get the hidden members */
  12. #include "global.h"
  13. #include "dirutil.h"
  14.   
  15. int
  16. findnext(fbp)
  17. struct ffblk *fbp;
  18. {
  19.     char statname[1024];
  20.     struct stat statbuf;
  21.     struct dirent *fp;
  22.     char *snp;
  23.   
  24.     strcpy(statname, fbp->ddir);
  25.     strcat(statname, "/");
  26.     for (snp = statname; *snp; snp++)
  27.         ;
  28.     for (;;)
  29.     {
  30.         if (!(fp = readdir(fbp->dp)))
  31.         {
  32.             closedir(fbp->dp);
  33.             free(fbp->ddir);
  34.             free(fbp->dpat);
  35.             return -1;      /* no more matches */
  36.         }
  37.         if (!fp->d_ino)
  38.             continue;       /* deleted file */
  39.         if (fp->d_name[0] == '.' && !(fbp->ff_attrib & FA_HIDDEN))
  40.             continue;       /* skip dot-files unless FA_HIDDEN */
  41.         if (!wildmat(fp->d_name, fbp->dpat, (char **) 0))
  42.             continue;       /* name doesn't match glob */
  43.         strcpy(snp, fp->d_name);
  44.         if (stat(statname, &statbuf) == -1)
  45.             continue;       /* file disappeared??? */
  46.         switch (statbuf.st_mode & S_IFMT)
  47.         {
  48.             case S_IFREG:       /* ordinary file */
  49.             case S_IFIFO:       /* allow a FIFO to masquerade as a file */
  50.             case 0:         /* *ix anachronism */
  51.                 fbp->ff_attrib = FA_NORMAL;
  52.                 break;
  53.             case S_IFDIR:       /* directory */
  54.                 if (!(fbp->ff_sattrib & FA_DIREC))
  55.                     continue;
  56.                 fbp->ff_attrib = FA_DIREC;
  57.                 break;
  58.             default:        /* device/name(Xenix)/etc. */
  59.                 if (!(fbp->ff_sattrib & FA_SYSTEM))
  60.                     continue;
  61.                 fbp->ff_attrib = FA_SYSTEM;
  62.                 break;
  63.         }
  64.         strcpy(fbp->ff_name, fp->d_name);
  65.         fbp->ff_fsize = (fbp->ff_attrib == FA_SYSTEM? 0: statbuf.st_size);
  66.         fbp->ff_ftime = *localtime(&statbuf.st_mtime);
  67.         return 0;
  68.     }
  69. }
  70.   
  71. int
  72. findfirst(name, fbp, attr)
  73. char *name;
  74. struct ffblk *fbp;
  75. int attr;
  76. {
  77.     register char *np;
  78.     char nbuf[1024];
  79.   
  80.     strcpy(nbuf, name);
  81.     for (np = nbuf; *np; np++)
  82.         ;
  83.     if (np == nbuf || np[-1] == '/')
  84.     {
  85.         strcat(nbuf, "*");
  86.         np++;
  87.     }
  88.     while (np != nbuf && *--np != '/')
  89.         ;
  90.     if (*np == '/')
  91.         *np++ = '\0';
  92.     fbp->ddir = strdup((np == nbuf? ".": nbuf));
  93.     fbp->dpat = strdup(np);
  94.     fbp->ff_attrib = attr;
  95.     if (!(fbp->dp = opendir(fbp->ddir)))
  96.         return -1;
  97.     return findnext(fbp);
  98. }
  99.