home
***
CD-ROM
|
disk
|
FTP
|
other
***
search
/
Black Box 4
/
BlackBox.cdr
/
progc
/
c_all592.arj
/
TI397.ASC
< prev
next >
Wrap
Text File
|
1992-02-25
|
7KB
|
331 lines
PRODUCT : TURBO C NUMBER : 397
VERSION : 1.0
OS : PC-DOS
DATE : November 20, 1987 PAGE : 1/5
TITLE : EXPANDING COMMAND LINE WILDCARDS
It is sometimes desirable to expand the DOS wildcards '*' and '?'
used in directory searches. The Turbo C functions, "findfirst"
and "findnext", accept wildcards in pathnames and are the most
straightforward way to search a directory. Following is a brief
description of each:
int findfirst (char *pathname, struct ffblk *f, int attrib);
findfirst accepts a string for the path or filename to be
searched and a file attribute to search for (0 = exists). If a
file is found which matches the requirements, findfirst enters
the information for that file into the ffblk variable (defined in
dir.h) and returns 0. If an error occurs, or no file is found,
-1 is returned and the global variable errno is set to one of the
following:
ENOENT path or filename not found.
ENMFILE no more files.
int findnext (struct ffblk *f);
findnext is used to fetch subsequent files which match the path
or filename and attribute given in findfirst. The ffblk variable
is the same block filled in by findfirst. The values returned
are the same.
These functions require the dir.h file to be included.
The following function, _setargv, illustrates the use of these
functions. It replaces the standard runtime library routine of
the same name and can be linked into existing code which uses the
standard arguments argv and argc. To do so, first compile to an
.OBJ file. This can be done either by selecting the compile to
.OBJ option from the compile menu in the Integrated Environment,
or by typing the following at the command line:
tcc -mX -c -I\turboc\include -L\turboc\lib setargv.c
where X is the memory model you are using. The object file can
then be linked with a main file (also compiled to an .obj), with
the following command:
tlink c0X mainprog setargv, mainprog,, emu mathX cX
PRODUCT : TURBO C NUMBER : 397
VERSION : 1.0
OS : PC-DOS
DATE : November 20, 1987 PAGE : 2/5
TITLE : EXPANDING COMMAND LINE WILDCARDS
or:
tcc -mX -I\turboc\include -L\turboc\lib mainprog.c setargv.obj
The same result can be achieved with the Integrated Environment
using the following .PRJ file:
mainprog
setargv.obj
Following is the code for _setargv:
/*
* SETARGV.C
*/
#include <dir.h>
#include <dos.h>
#include <string.h>
#include <alloc.h>
extern unsigned _psp; /* psp segment */
extern int __argc; /* argc */
extern char **__argv; /* argv */
extern unsigned _envseg; /* environment segment */
extern unsigned _envLng; /* program name offset in environment */
/*--------------------------------------------------------------
* _setargv - a replacement for the _setargv in cX.lib
* this version expands commandline wildcards
*------------------------------------------------------------*/
void _setargv()
{
unsigned pspcmd = 0x0081; /* command line offset in psp. */
char far *cmdline; /* pointer to the psp command line */
char far *startp; /* begining of a command line argument */
char far *endp; /* end of a command line argument */
char *s;
int v = 1,
is_wildcard = 0,
foundall, i = 0,
done = 0;
struct ffblk fblk; /* structure used by findfirst/findnext */
PRODUCT : TURBO C NUMBER : 397
VERSION : 1.0
OS : PC-DOS
DATE : November 20, 1987 PAGE : 3/5
TITLE : EXPANDING COMMAND LINE WILDCARDS
__argc = 1;
cmdline = (char far *) MK_FP (_psp,pspcmd);
while (*cmdline == 32) ++cmdline;/* skip spaces */
startp = cmdline;
done = *cmdline == 13;
while (!done) /* get value for argc */
{
endp = startp + 1;
while (32 < *endp) ++endp;
if (*endp == 32)
*endp = '\0'; /* replace space with null */
s = (char *) malloc ((1 + endp - startp) * sizeof(char));
i = 0;
while (startp <= endp)
{
if ((*startp == '*') || (*startp == '?'))
is_wildcard = 1;
s[i++] = *startp++;
}
if (*endp == 13)
s[i - 1] = '\0';
if (is_wildcard) /* expand wild cards */
{
i = 0;
foundall = findfirst(s,&fblk,0);
while (!foundall)
{
++__argc;
foundall = findnext(&fblk);
}
is_wildcard = 0;
}
else ++__argc;
free (s);
if (*endp == 13)
done = 1;
else
{
while (*startp == 32) ++startp;
done = *startp == 13;
}
}
PRODUCT : TURBO C NUMBER : 397
VERSION : 1.0
OS : PC-DOS
DATE : November 20, 1987 PAGE : 4/5
TITLE : EXPANDING COMMAND LINE WILDCARDS
/*
allocate space for pointers in argv
*/
__argv = (char **) malloc (__argc * sizeof(char *));
done = __argc == 1;
startp = cmdline;
while (!done) /* copy arguments into argv */
{
endp = startp + 1;
while (*endp > 32) ++endp;
s = (char *) malloc ((1 + endp - startp) * sizeof(char));
i = 0;
is_wildcard = 0;
while (startp <= endp)
{
if ((*startp == '*') || (*startp == '?'))
is_wildcard = 1;
s[i++] = *startp++;
}
if (*endp == 13) s[i - 1] = '\0';
if (is_wildcard)
{
foundall = findfirst (s, &fblk, 0);
while (!foundall)
{
__argv[v++] = strdup (fblk.ff_name);
foundall = findnext (&fblk);
}
free(s);
}
else __argv[v++] = strupr (s);
if (*endp == 13)
done = 1;
else
{
while (*startp == 32)
++startp;
done = *startp == 13;
}
}
PRODUCT : TURBO C NUMBER : 397
VERSION : 1.0
OS : PC-DOS
DATE : November 20, 1987 PAGE : 5/5
TITLE : EXPANDING COMMAND LINE WILDCARDS
/*
copy program name into argv[0]
*/
startp = (char far *) MK_FP(_envseg, _envLng + 2);
endp = startp + 1;
while (*endp++ != '\0');
__argv[0] = (char *) malloc ((endp - startp) * sizeof(char));
v = 0;
while (startp != endp)
__argv[0][v++] = *startp++;
}