home *** CD-ROM | disk | FTP | other *** search
/ OS/2 Shareware BBS: 10 Tools / 10-Tools.zip / BRIK.ZIP / TURBOC.C < prev    next >
C/C++ Source or Header  |  1989-03-10  |  8KB  |  241 lines

  1. /* nextfile.c */
  2. /* ::[[ @(#) turboc.c 1.4 89/03/10 19:10:15 ]]:: */
  3. #ifndef LINT
  4. static char sccsid[]="::[[ @(#) turboc.c 1.4 89/03/10 19:10:15 ]]::";
  5. #endif
  6.  
  7. /*
  8. This file is used only for MS-DOS & Turbo C.
  9. */
  10.  
  11. /*
  12. Checksum: 2196257943      (check or update this with "brik")
  13. */
  14.  
  15. /*
  16. nextfile() is a general wildcard expansion function that may be used
  17. with other programs.  Usage instructions are below.  It does not
  18. simply expand wildcards in an entire argument list.  Instead, it is
  19. called in a loop as described below, and returns one matching
  20. filename each time it is called.
  21.  
  22. These functions are for the SMALL MEMORY MODEL ONLY.
  23. */
  24.  
  25. #include "assert.h"
  26. #include "brik.h"
  27.  
  28. #define  FMAX  2        /* Number of different filename patterns */
  29. #define  PATHSIZE 200   /* Size of MS-DOS pathname */
  30. #define  NULL  0
  31.  
  32. #ifdef ANSIPROTO
  33. char *strtcpy (char *, char *);
  34. int strlen (char *);
  35. char *strcpy (char *, char *);
  36. #endif
  37.  
  38.  
  39. /* Structure definitions for MS-DOS software interrupt intdos() */
  40.  
  41. struct WORD_REGISTERS {
  42.    unsigned int ax, bx, cx, dx, si, di, carry, flags;
  43. };
  44.  
  45. /* byte registers */
  46.  
  47. struct BYTE_REGISTERS {
  48.    unsigned char al, ah, bl, bh, cl, ch, dl, dh;
  49. };
  50.  
  51. union REGS {
  52.    struct WORD_REGISTERS x;
  53.    struct BYTE_REGISTERS h;
  54. };
  55.  
  56. int intdos (union REGS *, union REGS *);
  57.  
  58. /*
  59. format of disk transfer address after MS-DOS calls FindFirst and
  60. FindNext
  61. */
  62. struct dta_t {
  63.    char junk[22];
  64.    int time;
  65.    int date;
  66.    long size;
  67.    char fname[13];
  68.    char just_in_case[4];   /* in case MS-DOS writes too much */
  69. };
  70.  
  71. void setdta (struct dta_t *);
  72. void fcbpath (struct dta_t *, char *, char *);
  73.  
  74. /*******************/
  75. /*
  76. nextfile() returns the name of the next source file matching a filespec.
  77.  
  78. INPUT
  79.    what: A flag specifying what to do.  If "what" is 0, nextfile()
  80.       initializes itself.  If "what" is 1, nextfile() returns the next
  81.       matching filename.
  82.    filespec:  The filespec, usually containing wildcard characters, that
  83.       specifies which files are needed.  If "what" is 0, filespec must be
  84.       the filespec for which matching filenames are needed.  If "what" is 1,
  85.       nextfile() does not use "filespec" and "filespec" should be NULL to
  86.       avoid an assertion error during debugging.
  87.    fileset:  nextfile() can keep track of more than one set of filespecs.
  88.       The fileset specifies which filespec is being matched and therefore
  89.       which set of files is being considered.  "fileset" can be in the
  90.       range 0:FMAX.  Initialization of one fileset does not affect the
  91.       other filesets.
  92.  
  93. OUTPUT
  94.    IF what == 0 THEN
  95.       return value is NULL
  96.    ELSE IF what == 1 THEN
  97.       IF a matching filename is found THEN
  98.          return value is pointer to matching filename including supplied path
  99.       ELSE
  100.          IF at least one file matched previously but no more match THEN
  101.             return value is NULL
  102.          ELSE IF supplied filespec never matched any filename THEN
  103.             IF this is the first call with what == 1 THEN
  104.                return value is pointer to original filespec
  105.             ELSE
  106.                return value is NULL
  107.             END IF
  108.          END IF
  109.       END IF
  110.    END IF
  111.  
  112. NOTE
  113.  
  114.    Initialization done when "what"=0 is not dependent on the correctness
  115.    of the supplied filespec but simply initializes internal variables
  116.    and makes a local copy of the supplied filespec.  If the supplied
  117.    filespec was illegal, the only effect is that the first time that
  118.    nextfile() is called with "what"=1, it will return the original
  119.    filespec instead of a matching filename.  That the filespec was
  120.    illegal will become obvious when the caller attempts to open the
  121.    returned filename for input/output and the open attempt fails.
  122.  
  123. USAGE HINTS
  124.  
  125. nextfile() can be used in the following manner:
  126.  
  127.       char *filespec;                  -- will point to filespec
  128.       char *this_file;                 -- will point to matching filename
  129.       filespec = parse_command_line(); -- may contain wildcards
  130.       FILE *stream;
  131.  
  132.       nextfile (0, filespec, 0);          -- initialize fileset 0
  133.       while ((this_file = nextfile(1, (char *) NULL, 0)) != NULL) {
  134.          stream = fopen (this_file, "whatever");
  135.          if (stream == NULL)
  136.             printf ("could not open %s\n", this_file);
  137.          else
  138.             perform_operations (stream);
  139.       }
  140. */
  141.  
  142. char *nextfile (what, filespec, fileset)
  143. int what;                        /* whether to initialize or match      */
  144. register char *filespec;         /* filespec to match if initializing   */
  145. register int fileset;            /* which set of files                  */
  146. {
  147.    static struct dta_t new_dta [FMAX+1];     /* our own private dta        */
  148.    static int first_time [FMAX+1];
  149.    static char pathholder [FMAX+1][PATHSIZE]; /* holds a pathname to return */
  150.    static char saved_fspec [FMAX+1][PATHSIZE];/* our own copy of filespec   */
  151.    union REGS regs;
  152.  
  153.    assert(fileset >= 0 && fileset <= FMAX);
  154.    if (what == 0) {
  155.       assert(filespec != NULL);
  156.       strcpy (saved_fspec[fileset], filespec);  /* save the filespec */
  157.       first_time[fileset] = 1;
  158.       return ((char *) NULL);
  159.    }
  160.  
  161.    setdta (&new_dta[fileset]);   /* set new dta -- our very own */
  162.    assert(what == 1);
  163.    assert(filespec == NULL);
  164.    assert(first_time[fileset] == 0 || first_time[fileset] == 1);
  165.  
  166.    if (first_time[fileset]) {             /* first time -- initialize etc. */
  167.       /* find first matching file */
  168.       regs.h.ah = 0x4e;                   /* FindFirst MS-DOS call    */
  169.       regs.x.dx = (unsigned int) saved_fspec[fileset]; /* filespec to match */
  170.       regs.x.cx = 0;                      /* search attributes       */
  171.       intdos (®s, ®s);
  172.    } else {
  173.       /* find next matching file */
  174.       regs.h.ah = 0x4f;                   /* FindNext MS-DOS call     */
  175.       intdos (®s, ®s);
  176.    }
  177.  
  178.    if (regs.x.carry != 0) {            /* if error status                  */
  179.       if (first_time[fileset]) {       /*   if file never matched then     */
  180.          first_time[fileset] = 0;
  181.          return (saved_fspec[fileset]);/*      return original filespec    */
  182.       } else {                         /*   else                           */
  183.          first_time[fileset] = 0;      /*                                  */
  184.          return ((char *) NULL);         /*      return (NULL) for no more   */
  185.       }
  186.    } else {                                        /* a file matched */
  187.       first_time[fileset] = 0;
  188.       /* add path info  */
  189.       fcbpath (&new_dta[fileset], saved_fspec[fileset], pathholder[fileset]);
  190.       return (pathholder[fileset]);                /* matching path  */
  191.    }
  192. } /* nextfile */
  193.  
  194. /*******************/
  195. /* This function sets the dta to a new dta */
  196. void setdta (dta)
  197. struct dta_t *dta;
  198. {
  199.    union REGS regs;
  200.    regs.h.ah = 0x1a;                /* SetDTA Call       */
  201.    regs.x.dx = (unsigned int) dta;  /* new DTA address   */
  202.    intdos (®s, ®s);
  203. }
  204.  
  205. /*******************/
  206. /*
  207. fcbpath() accepts a pointer to the Disk Transfer Area, a character
  208. pointer to a pathname that may contain wildcards, and a character
  209. pointer to a buffer.  It copies into the buffer the path prefix from
  210. the pathname and the filename prefix from the DTA so that it forms a
  211. complete path.
  212. */
  213.  
  214. void fcbpath (dta, old_path, new_path)
  215. struct dta_t *dta;
  216. char *old_path;
  217. register char *new_path;
  218. {
  219.    register int i;
  220.    int length, start_pos;
  221.  
  222.    strcpy(new_path, old_path);               /* copy the whole thing first */
  223.    length = strlen(new_path);
  224.    i = length - 1;                           /* i points to end of path */
  225.    while (i >= 0 && new_path[i] != '/' && new_path[i] != '\\' && new_path[i] != ':')
  226.       i--;
  227.    /* either we found a "/", "\", or ":", or we reached the beginning of
  228.       the name.  In any case, i points to the last character of the
  229.       path part. */
  230.    start_pos = i + 1;
  231.    for (i = 0; i < 13; i++)
  232.       new_path[start_pos+i] = dta->fname[i];
  233.    new_path[start_pos+13] = '\0';
  234. }
  235. /* -- END OF nextfile() and related functions -- */
  236.  
  237. extern unsigned _stklen = 10000;
  238.  
  239. #include <conio.h>
  240. brktst() { kbhit(); }      /* test for user interrupt */
  241.