home *** CD-ROM | disk | FTP | other *** search
/ The Datafile PD-CD 4 / DATAFILE_PDCD4.iso / unix / unixtools / util / c / dirscan < prev    next >
Encoding:
Text File  |  1992-07-21  |  1.7 KB  |  114 lines

  1. /* C.Dirscan: Get filenames for a wildcarded value */
  2.  
  3. /* Use as:
  4.  *
  5.  *    for (file = dirscan(wild); file; file = dirscan(0))
  6.  *        ...
  7.  *
  8.  * Specifying an argument other than 0 restarts the scan with the
  9.  * new wildcard value.
  10.  */ 
  11.  
  12. #include <stdio.h>
  13. #include <stdlib.h>
  14. #include <string.h>
  15. #include "kernel.h"
  16.  
  17. #include "utils.h"
  18.  
  19. char *dirscan (const char *f)
  20. {
  21.     _kernel_swi_regs regs;
  22.     _kernel_oserror *err;
  23.  
  24.     static int ptr;
  25.     static int len;
  26.     static int last_notwild;
  27.     static char *file;
  28.     static char *dir;
  29.     static char *name;
  30.     static char path[255];
  31.     static char res[255];
  32.  
  33.     if (f && *f)
  34.     {
  35.         strcpy (path, f);
  36.  
  37.         /* Try for a directory prefix */
  38.         file = strrchr (path, '.');
  39.  
  40.         /* If not, try for a filing system prefix */
  41.         if (file == NULL)
  42.         {
  43.             if (*path == '-')
  44.                 file = strchr (path + 1, '-');
  45.             else
  46.                 file = strchr (path, ':');
  47.         }
  48.  
  49.         if (file)
  50.         {
  51.             len = file - path + 1;
  52.             strncpy (res, path, len);
  53.  
  54.             name = &res[len];
  55.             len = 255 - len;
  56.  
  57.             dir = path;
  58.             *file++ = '\0';
  59.         }
  60.         else
  61.         {
  62.             name = res;
  63.             len = 255;
  64.  
  65.             file = path;
  66.             dir = "";
  67.         }
  68.  
  69.         /* Is the file name wildcarded? */
  70.         if (strchr (file, '*') || strchr (file, '#'))
  71.             last_notwild = 0;
  72.         else
  73.         {
  74.             strcpy (name, file);
  75.             last_notwild = 1;
  76.             return res;
  77.         }
  78.  
  79.         ptr = 0;
  80.     }
  81.  
  82.     /* If we're looking for more from a non-wild name, no luck */
  83.     if (last_notwild)
  84.         return 0;
  85.  
  86.     regs.r[0] = 9;
  87.     regs.r[1] = (int) dir;
  88.     regs.r[2] = (int) name;
  89.     regs.r[4] = ptr;
  90.     regs.r[5] = len;
  91.     regs.r[6] = (int) file;
  92.  
  93.     do
  94.     {
  95.         regs.r[3] = 1;
  96.         err = _kernel_swi (0x0C, ®s, ®s);
  97.  
  98.         if (err)
  99.         {
  100.             fprintf (stderr, "*** Error (%d) - %s\n",
  101.                  err->errnum, err->errmess);
  102.             exit (1);
  103.         }
  104.  
  105.     } while (regs.r[3] == 0 && regs.r[4] >= 0);
  106.  
  107.     ptr = regs.r[4];
  108.  
  109.     if (ptr < 0)
  110.         return 0;
  111.  
  112.     return res;
  113. }
  114.