home *** CD-ROM | disk | FTP | other *** search
/ ProfitPress Mega CDROM2 …eeware (MSDOS)(1992)(Eng) / ProfitPress-MegaCDROM2.B6I / PROG / PASCAL / MISCTI10.ZIP / TI397.ASC < prev    next >
Encoding:
Text File  |  1988-05-02  |  5.4 KB  |  193 lines

  1.  
  2. It is sometimes desirable to expand the DOS wildcards '*' and '?'
  3. used in directory searches.  The Turbo C functions, "findfirst"
  4. and "findnext", accept wildcards in pathnames and are the most
  5. straightforward way to search a directory.  Following is a brief
  6. description of each:
  7.  
  8. int findfirst (char *pathname, struct ffblk *f, int attrib);
  9.  
  10. findfirst accepts a string for the path or filename to be
  11. searched and a file attribute to search for (0 = exists).  If a
  12. file is found which matches the requirements, findfirst enters
  13. the information for that file into the ffblk variable (defined in
  14. dir.h) and returns 0.  If an error occurs, or no file is found,
  15. -1 is returned and the global variable errno is set to one of the
  16. following:
  17.  
  18.           ENOENT   path or filename not found.
  19.           ENMFILE  no more files.
  20.  
  21. int findnext (struct ffblk *f);
  22.  
  23. findnext is used to fetch subsequent files which match the path
  24. or filename and attribute given in findfirst.  The ffblk variable
  25. is the same block filled in by findfirst.  The values returned
  26. are the same.
  27.  
  28. These functions require the dir.h file to be included.
  29.  
  30. The following function, _setargv, illustrates the use of these
  31. functions.  It replaces the standard runtime library routine of
  32. the same name and can be linked into existing code which uses the
  33. standard arguments argv and argc.  To do so, first compile to an
  34. .OBJ file.  This can be done either by selecting the compile to
  35. .OBJ option from the compile menu in the Integrated Environment,
  36. or by typing the following at the command line:
  37.  
  38. tcc -mX -c -I\turboc\include -L\turboc\lib setargv.c
  39.  
  40. where X is the memory model you are using.  The object file can
  41. then be linked with a main file (also compiled to an .obj), with
  42. the following command:
  43.  
  44. tlink c0X mainprog setargv, mainprog,, emu mathX cX
  45.  
  46.                         or:
  47.  
  48. tcc -mX -I\turboc\include -L\turboc\lib mainprog.c setargv.obj
  49.  
  50. The same result can be achieved with the Integrated Environment
  51. using the following .PRJ file:
  52.  
  53. mainprog
  54. setargv.obj
  55.  
  56. Following is the code for _setargv:
  57.  
  58. /*
  59.  *  SETARGV.C
  60.  */
  61.  
  62. #include <dir.h>
  63. #include <dos.h>
  64. #include <string.h>
  65. #include <alloc.h>
  66.  
  67. extern unsigned _psp;    /* psp segment */
  68. extern int __argc;       /* argc */
  69. extern char **__argv;    /* argv */
  70. extern unsigned _envseg; /* environment segment */
  71. extern unsigned _envLng; /* program name offset in environment */
  72.  
  73. /*--------------------------------------------------------------
  74.  * _setargv - a replacement for the _setargv in cX.lib
  75.  *            this version expands commandline wildcards
  76.  *------------------------------------------------------------*/
  77. void _setargv()
  78. {
  79.   unsigned pspcmd = 0x0081;  /* command line offset in psp. */
  80.   char far *cmdline;         /* pointer to the psp command line
  81. */
  82.   char far *startp;          /* begining of a command line
  83. argument */
  84.   char far *endp;            /* end of a command line argument */
  85.   char *s;
  86.   int v = 1, 
  87.       is_wildcard = 0, 
  88.          foundall, i = 0, 
  89.          done = 0;
  90.   struct ffblk fblk;         /* structure used by
  91. findfirst/findnext */
  92.  
  93.   __argc = 1;
  94.   cmdline = (char far *) MK_FP (_psp,pspcmd);
  95.   while (*cmdline == 32) ++cmdline;/* skip spaces */
  96.   startp = cmdline;
  97.   done = *cmdline == 13;
  98.   
  99.   while (!done)              /* get value for argc */
  100.   {
  101.     endp = startp + 1;
  102.     while (32 < *endp) ++endp;
  103.     if (*endp == 32)
  104.       *endp = '\0';  /* replace space with null */
  105.     s = (char *) malloc ((1 + endp - startp) * sizeof(char));
  106.     i = 0;
  107.     while (startp <= endp)
  108.     {
  109.       if ((*startp == '*') || (*startp == '?'))
  110.         is_wildcard = 1;
  111.       s[i++] = *startp++;
  112.     }
  113.     if (*endp == 13)
  114.       s[i - 1] = '\0';
  115.     if (is_wildcard) /* expand wild cards */
  116.     {
  117.       i = 0;
  118.       foundall = findfirst(s,&fblk,0);
  119.       while (!foundall)
  120.       {
  121.         ++__argc;
  122.         foundall = findnext(&fblk);
  123.       }
  124.       is_wildcard = 0;
  125.     }
  126.     else ++__argc;
  127.     free (s);
  128.     if (*endp == 13)
  129.       done = 1;
  130.     else
  131.     {
  132.       while (*startp == 32) ++startp;
  133.       done = *startp == 13;
  134.     }
  135.   }
  136.   
  137.   /*
  138.      allocate space for pointers in argv 
  139.   */
  140.   
  141.   __argv = (char **) malloc (__argc * sizeof(char *));
  142.   done = __argc == 1;
  143.   startp = cmdline;
  144.   
  145.   while (!done)             /* copy arguments into argv */
  146.   {
  147.     endp = startp + 1;
  148.     while (*endp > 32) ++endp;
  149.     s = (char *) malloc ((1 + endp - startp) * sizeof(char));
  150.     i = 0;
  151.     is_wildcard = 0;
  152.     while (startp <= endp)
  153.     {
  154.       if ((*startp == '*') || (*startp == '?'))
  155.         is_wildcard = 1;
  156.       s[i++] = *startp++;
  157.     }
  158.     if (*endp == 13) s[i - 1] = '\0';
  159.     if (is_wildcard)
  160.     {
  161.       foundall = findfirst (s, &fblk, 0);
  162.       while (!foundall)
  163.       {
  164.         __argv[v++] = strdup (fblk.ff_name);
  165.         foundall = findnext (&fblk);
  166.       }
  167.       free(s);
  168.     }
  169.     else __argv[v++] = strupr (s);
  170.     if (*endp == 13)
  171.       done = 1;
  172.     else
  173.     {
  174.       while (*startp == 32)
  175.         ++startp;
  176.       done = *startp == 13;
  177.     }
  178.   }
  179.   
  180.   /* 
  181.     copy program name into argv[0]
  182.   */
  183.   
  184.   startp = (char far *) MK_FP(_envseg, _envLng + 2);
  185.   endp = startp + 1;
  186.   while (*endp++ != '\0');
  187.   __argv[0] = (char *) malloc ((endp - startp) * sizeof(char));
  188.   v = 0;
  189.   while (startp != endp)
  190.     __argv[0][v++] = *startp++;
  191. }
  192.  
  193.