home *** CD-ROM | disk | FTP | other *** search
/ Simtel MSDOS 1992 June / SIMTEL_0692.cdr / msdos / dirutl / tools.arc / WILD.C < prev   
Text File  |  1987-09-23  |  4KB  |  138 lines

  1. /*
  2.  * Wildcard expansion for within C programs
  3.  */
  4.  
  5. #include <stdio.h>              /* Standard I/O definitions */
  6. #include <ctype.h>              /* Character type macros */
  7. #include <dos.h>                /* Msdos definitions */
  8. #include <errno.h>              /* error codes */
  9. #include <string.h>
  10. #include <stdlib.h>
  11. #include <search.h>
  12. #include <malloc.h>
  13.  
  14. #define SIZE(x)         (sizeof(x)/sizeof(x[0]))
  15. #define A_RONLY         0x01    /* Read only file */
  16. #define A_HIDDEN        0x02    /* Hidden file */
  17. #define A_SYSTEM        0x04    /* System file */
  18. #define A_DIRECTORY     0x10    /* Directory file */
  19. #define A_ARCHIVE       0x20    /* Archive bit */
  20. #define FNLEN           80      /* Maximum filename length */
  21. #define MAXFILES        512     /* Max number of files/directory */
  22.  
  23. static char ID[]="## wild.c 1.0 K.van Houten 230987 ##";
  24. struct date {
  25. unsigned    d_sec:      5;      /* Time, 2 second intervals */
  26. unsigned    d_min:      6;      /* Time, minutes */
  27. unsigned    d_hour:     5;      /* Time, hours */
  28. unsigned    d_day:      5;      /* Date, day of month */
  29. unsigned    d_month:    4;      /* Date, month of year */
  30. unsigned    d_year:     7;      /* Date, year since 1980 */
  31. };
  32. struct find {
  33.     char    fnd_dosinfo[21];    /* Reserved for dos */
  34.     char    fnd_attr;           /* File attribute */
  35.     struct date fnd_date;       /* Date structure */
  36.     long    fnd_size;           /* File size */
  37.     char    fnd_name[13];       /* File name less path */
  38. };
  39. char   *strrchr ();
  40. int     qscmp ();
  41. void findf();
  42. void findn();
  43. char **wild();
  44.  
  45. static int tmp;
  46. static struct find f;              /* Used to return data from msdos */
  47. static union REGS r, o;            /* Contains register values for intcall() */
  48.  
  49. char **
  50. wild(file)
  51. char *file;
  52. {
  53.     int i,j,count;
  54.     char **fnd;
  55.  
  56.     fnd = (char **)malloc(MAXFILES * sizeof(char *));
  57.     if (fnd == NULL) {
  58.         printf("wildcard expansion out of mem\n");
  59.         exit(2);
  60.     }
  61.  
  62.     r.h.ah = 0x1a;              /* SET DISK TRANSFER ADDRESS FUNCTION */
  63.     r.x.dx = (int) & f;
  64.     tmp = intdos (&r, &o);
  65.     count = 0;
  66.  
  67.     findf (file);
  68.     if (o.x.cflag) {
  69.         return(NULL);
  70.     }
  71.     for (;;) {
  72.         if (o.x.cflag) {
  73.             break;
  74.         }
  75.         if (f.fnd_attr & A_DIRECTORY) {
  76.             if (f.fnd_name[0] == '.')
  77.                goto next;
  78.         }
  79.         if ((fnd[count] = malloc(15)) == NULL) {
  80.               printf("wildcard expansion out of memory\n");
  81.               exit(2);
  82.         }
  83.         strcpy(fnd[count],strlwr(f.fnd_name));
  84.         count++;
  85. next:   findn ();
  86.      }
  87.      if (count == 0) {
  88.         return(NULL);
  89.     }
  90.     qsort ((char *)fnd, count, sizeof (char *), qscmp);
  91.  
  92.     fnd[count] = NULL;
  93.     return(fnd);
  94. }
  95.  
  96.  
  97. /*
  98.  * Find first entry, return info in find structure.
  99.  */
  100. void
  101. findf (file)
  102. char *file;
  103. {
  104.     r.h.ah = 0x4e;              /* FINDFIRST function */
  105.     r.x.cx = A_HIDDEN | A_SYSTEM | A_DIRECTORY |
  106.         A_RONLY | A_ARCHIVE;
  107.     r.x.dx = (int) file;
  108.     tmp = intdos (&r, &o);      /* Find first */
  109. }
  110.  
  111. /*
  112.  * Find next entry
  113.  */
  114. void
  115. findn ()
  116. {
  117.     r.h.ah = 0x4f;              /* Now use FINDNEXT function */
  118.     tmp = intdos (&r, &o);      /* Find next */
  119. }
  120.  
  121.  
  122. /*
  123.  * Qsort compare routine
  124.  * files sort alphabetically.
  125.  * This routine is called by qsort and passed two pointers to
  126.  * find structure pointers.
  127.  */
  128. qscmp (p1, p2)
  129. char **p1;
  130. char **p2;
  131. {
  132.     register char   *f1 = *p1;
  133.     register char   *f2 = *p2;
  134.     register int    cmp;
  135.  
  136.     return (strcmp (f1, f2));
  137. }
  138.