home *** CD-ROM | disk | FTP | other *** search
/ Garbo / Garbo.cdr / pc / source / pct.arc / XDIR.C < prev    next >
Text File  |  1986-03-29  |  4KB  |  147 lines

  1.  /* xdir.c:     fill structures with extended file info */
  2.  
  3. #include <stdio.h>
  4. #include <ctype.h>
  5. #include <dos.h>
  6. #include <xdir.h>
  7.  
  8. static struct DTA dta;
  9. static struct reg r;
  10.  
  11. int ffirst(), fnext(), xdir(), getdrv();
  12. char *strlower(), *rindex(), *index();
  13.  
  14. int ffirst(s,attr)
  15. char *s;
  16. unsigned attr;
  17. {
  18.     /* ..Set DTA.. */
  19.     r.r_ax = SETDTA;
  20.     ptoreg(dsreg,r.r_dx,r.r_ds,&dta);
  21.     intcall(&r,&r,DOSINT);
  22.  
  23.     /* ..Get first match.. */
  24.     r.r_ax = NFFIRST;
  25.     r.r_cx = attr;
  26.     ptoreg(dsreg,r.r_dx,r.r_ds,s);
  27.     intcall(&r,&r,DOSINT);
  28.  
  29.     return (r.r_flags & F_CF) == 0;
  30. }
  31.  
  32. int fnext()
  33. {
  34.     /* ..Find next match.. */
  35.     r.r_ax = NFNEXT;
  36.     intcall(&r,&r,DOSINT);
  37.  
  38.     return (r.r_flags & F_CF) == 0;
  39. }
  40.  
  41. int xdir(argc,argv,xfiles,maxf,find_attr)
  42. int argc, maxf, find_attr;
  43. char *argv[];
  44. struct file_info *xfiles[];
  45. {
  46.     register i, j;
  47.     int status;
  48.     char path[64];
  49.  
  50.     i = 0;      /* ..File count.. */
  51.     for (j = 1; j < argc; ++j)
  52.     {
  53.         getpath(argv[j],path);
  54.         status = ffirst(argv[j],find_attr);
  55.         /* ..Non-existent files are ignored.. */
  56.         for ( ; status && i < maxf; status = fnext(), ++i)
  57.         {
  58.             xfiles[i] = (struct file_info *) malloc(sizeof(struct file_info));
  59.             if (xfiles[i] == NULL)
  60.             {
  61.                 fputs("not enough memory\n",stderr);
  62.                 exit(1);
  63.             }
  64.             xfiles[i]->name = (char *) malloc(strlen(dta.name)+
  65.                                               strlen(path)+1);
  66.             if (xfiles[i]->name == NULL)
  67.             {
  68.                 fputs("not enough memory\n",stderr);
  69.                 exit(1);
  70.             }
  71.             strcpy(xfiles[i]->name,path);
  72.             strcat(xfiles[i]->name,strlower(dta.name,dta.name));
  73.             xfiles[i]->attr = dta.attribute;
  74.             xfiles[i]->size = dta.size;
  75.             xfiles[i]->mon = dta.month;
  76.             xfiles[i]->day = dta.day;
  77.             xfiles[i]->year = dta.year + 80;
  78.             xfiles[i]->hour = dta.hour;
  79.             xfiles[i]->min = dta.min;        
  80.         }
  81.     }
  82.     return i;
  83. }
  84.  
  85. getpath(f,p)
  86. register char *f, *p;
  87. {
  88.     register char *ptr;
  89.     int drive;
  90.     char dir[64];
  91.  
  92.     /* ..Get drive.. */
  93.     if ((ptr = index(f,':')) != NULL)
  94.     {
  95.         drive = tolower(*f) - 'a';  /* ..Drive ID.. */
  96.         *p++ = tolower(*f++);       /* ..Drive letter.. */
  97.         *p++ = *f++;                /* ':' */
  98.     }
  99.     else
  100.     {
  101.         drive = getdrv();           /* ..Default drive ID.. */
  102.         *p++ = 'a' + drive;
  103.         *p++ = ':';
  104.     }
  105.  
  106.  
  107.     /* ..Get directory.. */
  108.     if (*f != '\\' && *f != '/')
  109.     {
  110.         /* ..Get default directory (starting from root).. */
  111.         getdir(drive+1,dir);
  112.         *p++ = '\\';
  113.         ptr = dir;
  114.         while (*ptr)
  115.             *p++ = tolower(*ptr++);
  116.     if (strlen(dir) > 0)
  117.             *p++ = '\\';
  118.     }
  119.  
  120.     /* ..Copy specified directory.. */
  121.     if ((ptr = rindex(f,'\\')) != NULL)
  122.         while (f <= ptr)
  123.             *p++ = tolower(*f++);
  124.     else if ((ptr = rindex(f,'/')) != NULL)
  125.         while (f <= ptr)
  126.             *p++ = tolower(*f++);
  127.  
  128.     *p = '\0';
  129. }
  130.  
  131. int getdrv()
  132. {
  133.     r.r_ax = GETDISK;
  134.     intcall(&r,&r,DOSINT);
  135.     return r.r_ax & 0x00ff;
  136. }
  137.  
  138. getdir(drive,dir)
  139. int drive;
  140. char *dir;
  141. {
  142.     r.r_ax = GETCDIR;
  143.     r.r_dx = drive;
  144.     ptoreg(dsreg,r.r_si,r.r_ds,dir);
  145.     intcall(&r,&r,DOSINT);
  146. }
  147.