home *** CD-ROM | disk | FTP | other *** search
- /* C.Dirscan: Get filenames for a wildcarded value */
-
- /* Use as:
- *
- * for (file = dirscan(wild); file; file = dirscan(0))
- * ...
- *
- * Specifying an argument other than 0 restarts the scan with the
- * new wildcard value.
- */
-
- #include <stdio.h>
- #include <stdlib.h>
- #include <string.h>
- #include "kernel.h"
-
- #include "utils.h"
-
- char *dirscan (const char *f)
- {
- _kernel_swi_regs regs;
- _kernel_oserror *err;
-
- static int ptr;
- static int len;
- static int last_notwild;
- static char *file;
- static char *dir;
- static char *name;
- static char path[255];
- static char res[255];
-
- if (f && *f)
- {
- strcpy (path, f);
-
- /* Try for a directory prefix */
- file = strrchr (path, '.');
-
- /* If not, try for a filing system prefix */
- if (file == NULL)
- {
- if (*path == '-')
- file = strchr (path + 1, '-');
- else
- file = strchr (path, ':');
- }
-
- if (file)
- {
- len = file - path + 1;
- strncpy (res, path, len);
-
- name = &res[len];
- len = 255 - len;
-
- dir = path;
- *file++ = '\0';
- }
- else
- {
- name = res;
- len = 255;
-
- file = path;
- dir = "";
- }
-
- /* Is the file name wildcarded? */
- if (strchr (file, '*') || strchr (file, '#'))
- last_notwild = 0;
- else
- {
- strcpy (name, file);
- last_notwild = 1;
- return res;
- }
-
- ptr = 0;
- }
-
- /* If we're looking for more from a non-wild name, no luck */
- if (last_notwild)
- return 0;
-
- regs.r[0] = 9;
- regs.r[1] = (int) dir;
- regs.r[2] = (int) name;
- regs.r[4] = ptr;
- regs.r[5] = len;
- regs.r[6] = (int) file;
-
- do
- {
- regs.r[3] = 1;
- err = _kernel_swi (0x0C, ®s, ®s);
-
- if (err)
- {
- fprintf (stderr, "*** Error (%d) - %s\n",
- err->errnum, err->errmess);
- exit (1);
- }
-
- } while (regs.r[3] == 0 && regs.r[4] >= 0);
-
- ptr = regs.r[4];
-
- if (ptr < 0)
- return 0;
-
- return res;
- }
-