home *** CD-ROM | disk | FTP | other *** search
/ Microsoft Programmer's Library 1.3 / Microsoft-Programers-Library-v1.3.iso / sampcode / prof_c / 08file / ls_dirx.c next >
Encoding:
C/C++ Source or Header  |  1988-08-11  |  2.2 KB  |  97 lines

  1. /*
  2.  *    ls_dirx -- expand the contents of a directory using
  3.  *    the DOS first/next matching file functions
  4.  */
  5.  
  6. #define DEBUG
  7.  
  8. #include <stdio.h>
  9. #include <stdlib.h>
  10. #include <string.h>
  11. #include <malloc.h>
  12. #include <dos.h>
  13. #include <direct.h>
  14. #include <signal.h>
  15. #include <search.h>
  16. #include <local\std.h>
  17. #include <local\doslib.h>
  18. #include "ls.h"
  19.  
  20. #define NFILES    1024
  21.  
  22. extern int Recursive;
  23. extern int Longlist;
  24. extern int Multicol;
  25. extern int Hidden;
  26.  
  27. int
  28. ls_dirx(pname, namep)
  29. char *pname;
  30. char *namep;
  31. {
  32.     int status = 0;            /* function return value */
  33.     int n;                /* number of items found */
  34.     int fileattr;            /* attributes of file-matching */
  35.     struct DTA buf;            /* disk transfer area */
  36.     struct OUTBUF *bp, *bq;        /* output buffer pointers */
  37.     char path[MAXPATH + 1];        /* working path string */
  38.  
  39.     extern void setdta(char *);
  40.     extern int first_fm(char *, int);
  41.     extern int next_fm();
  42.     extern int ls_fcomp(struct OUTBUF *, struct OUTBUF *);
  43.     extern char last_ch(char *);
  44.  
  45.     /* allocate a buffer */
  46.     bp = bq = (struct OUTBUF *)malloc(NFILES * sizeof(struct OUTBUF));
  47.     if (bp == NULL)
  48.         fatal(pname, "Out of memory");
  49.  
  50.     /* form name for directory search */
  51.     strcpy(path, namep);
  52.     if (last_ch(path) != '\\')
  53.         strcat(path, "\\");
  54.     strcat(path, "*.*");
  55.  
  56.     /* list the files found */
  57.     n = 0;
  58.     /* establish a private DTA */
  59.     setdta((char *)&buf);
  60.     /* select file attributes */
  61.     if (Hidden)
  62.         fileattr = SUBDIR | HIDDEN | SYSTEM | READONLY;
  63.     else
  64.         fileattr = SUBDIR;
  65.     if (first_fm(path, fileattr) == 0) {
  66.         /* add file or directory to the buffer */
  67.         do {
  68.             if (!Hidden && buf.d_fname[0] == '.')
  69.                 continue;
  70.             bq->o_name = strdup(buf.d_fname);
  71.             bq->o_mode = buf.d_attr;
  72.             bq->o_size = buf.d_fsize;
  73.             bq->o_date = buf.d_mdate;
  74.             bq->o_time = buf.d_mtime;
  75.             ++bq;
  76.             ++n;
  77.             setdta((char *)&buf);    /* reset to our DTA */
  78.         } while (next_fm() == 0);
  79.  
  80.         if (n > 0) {
  81.             /* got some -- sort and list them */
  82.             qsort(bp, n, sizeof(struct OUTBUF), ls_fcomp);
  83.             if (Longlist)
  84.                 ls_long(bp, n);
  85.             else if (Multicol)
  86.                 ls_multi(bp, n);
  87.             else
  88.                 ls_single(bp, n);
  89.         }
  90.     }
  91.     else 
  92.         ++status;
  93.     free(bp);
  94.  
  95.     return (status);
  96. }
  97.