home
***
CD-ROM
|
disk
|
FTP
|
other
***
search
/
DOS/V Power Report 1997 March
/
VPR9703A.ISO
/
VPR_DATA
/
DOGA
/
SOURCES
/
REND.LZH
/
REND
/
DIR.C
< prev
next >
Wrap
C/C++ Source or Header
|
1996-07-16
|
6KB
|
306 lines
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <ctype.h>
#ifdef __BORLANDC__
#include <malloc.h>
#include <memory.h>
#include <alloc.h>
#endif
#define MAXARGS 1024
#define MAXARGBUF 4096
#define MAXINDBUF 1024
/*
proto -s dir.c > temp
*/
static int _extendargs( int, char*[], int, char*[] );
static int extendwild(char *, int, char*[]);
static char *dfind( char*, int );
static char *dnext( void );
static char *argbuf = NULL ;
static int argremain;
static char *args[MAXARGS] ;
static char *curargbuf ;
static int nargs ;
int extendargs( argcp, argvp, extn, ext )
int *argcp ;
char **argvp[] ;
int extn ;
char *ext[] ;
{
#if !defined(DJ) && !defined(UNIX)
int stat ;
nargs = 1 ;
args[0] = **argvp ;
if (argbuf == NULL) {
argbuf = malloc(MAXARGBUF);
argremain = MAXARGBUF;
}
curargbuf = argbuf ;
stat = _extendargs( *argcp, *argvp, extn, ext );
*argcp = nargs ;
*argvp = args ;
return( stat );
#else
int i = 1, nargc = 1, j;
int stat ;
char *p;
if (argbuf == NULL) {
argbuf = malloc(MAXARGBUF);
argremain = MAXARGBUF;
}
curargbuf = argbuf ;
args[0] = **argvp ;
nargs = 1;
stat = _extendargs( *argcp, *argvp, extn, ext );
for (i = 1; i < nargs; ++i) {
p = strrchr( args[i], '.' );
if ( args[i][0] == '-' || args[i][0] == '/'
|| p == NULL ) {
args[nargc++] = args[i];
} else {
for( j = 0 ; j < extn ; ++j ) {
if ( strcmpi( p, ext[j] ) == 0 )
break ;
}
if ( j < extn )
{
args[nargc++] = args[i];
}
}
}
#if 0
for (i = 0; i < nargc; ++i) {
printf("(%d)=%s\n", i, args[i]);
}
#endif
*argcp = nargc ;
*argvp = args ;
return stat;
#endif
}
static int _extendargs( argc, argv, extn, ext )
int argc ;
char *argv[] ;
int extn ;
char *ext[] ;
{
int i;
char *cargv;
for( i = 1 ; i < argc ; ++i )
{
cargv = argv[i] ;
if ( nargs >= MAXARGS )
return( -1 );
if ( cargv[0] == '@' || cargv[0] == '#')
{
FILE *fp;
char str[MAXINDBUF], *sp, *ep;
if ((fp = fopen(cargv+1, "r")) != NULL) {
while (fgets(str,MAXINDBUF-1,fp) != NULL) {
sp = str;
while (*sp) {
while (*sp && isspace(*sp)){
sp++;
}
if (*sp == 0) {
break;
}
for (ep = sp; *ep && !isspace(*ep); ++ep)
;
if (*ep == 0) {
ep[1] = 0;
}
*ep = 0;
if ( strchr( sp, '*' ) != NULL || strchr( sp, '?' ) != NULL ) {
extendwild(sp, extn, ext);
} else {
int l;
l = strlen(sp)+1;
if (argremain < l) {
argbuf = malloc(MAXARGBUF);
curargbuf = argbuf;
argremain = MAXARGBUF;
}
argremain -= l+1;
strcpy(curargbuf, sp);
args[nargs++] = curargbuf ;
curargbuf += l+1;
}
sp = ep+1;
}
}
}
}
else if ( cargv[0] == '-' || cargv[0] == '/' )
{
args[nargs++] = cargv ;
}
else
{
if ( strchr( cargv, '*' ) != NULL || strchr( cargv, '?' ) != NULL ) {
extendwild(cargv, extn, ext);
} else {
args[nargs++] = cargv ;
}
}
}
return( 0 );
}
static int extendwild(cargv, extn, ext)
char *cargv;
int extn;
char *ext[];
{
int j, len ;
char *file, *p ;
file = dfind( cargv, 0x20 );
p = strrchr( cargv, '\\' );
if ( p == NULL )
p = strchr( cargv, ':' );
if ( p != NULL )
*(p+1) = '\0' ;
else
cargv = "" ;
while( file != NULL )
{
if ( nargs >= MAXARGS )
return( -1 );
p = strrchr( file, '.' );
if ( p != NULL )
{
for( j = 0 ; j < extn ; ++j )
{
if ( strcmpi( p, ext[j] ) == 0 )
break ;
}
if ( j < extn )
{
len = strlen( cargv ) + strlen( file ) + 1 ;
#if 0
if ( curargbuf + len >= argbuf + MAXARGBUF )
return( -1 );
#else
if (argremain < len) {
argbuf = malloc(MAXARGBUF);
curargbuf = argbuf;
argremain = MAXARGBUF;
}
#endif
strcpy( curargbuf, cargv );
strcat( curargbuf, file );
args[nargs++] = curargbuf ;
curargbuf += len ;
argremain -= len;
}
}
file = dnext();
}
return 0;
}
/*
* directry search
*/
#ifdef X68000
#include <doslib.h>
static struct FILBUF buf ;
#endif
#ifdef MSC
#include <dos.h>
static union REGS in, out ;
static struct SREGS seg ;
static char *dta ;
#endif
#if defined(DJ) || defined(__BORLANDC__)
#include <dir.h>
static struct ffblk buf;
#endif
static char *dfind( name, attr )
char *name ;
int attr ;
{
#ifdef X68000
if ( FILES( &buf, (UBYTE*)name, attr ) != 0 )
return( NULL );
return( (char *)buf.name );
#endif /* XC */
#ifdef MSC
segread( &seg );
/* DTA アドレスの読み出し */
in.h.ah = 0x2F ;
intdosx( &in, &out, &seg );
FP_SEG( dta ) = seg.es ;
FP_OFF( dta ) = out.x.bx ;
/* ディレクトリサーチ */
in.h.ah = 0x4E ;
in.x.dx = FP_OFF( name );
in.x.cx = attr ;
seg.ds = FP_SEG( name );
intdosx( &in, &out, &seg );
if( out.x.cflag )
return( NULL );
return( dta+0x1E );
#endif /* MSC */
#if defined(DJ) || defined(__BORLANDC__)
if ( findfirst( name, &buf, attr ) != 0 )
return( NULL );
return( buf.ff_name );
#endif
}
static char *dnext()
{
#ifdef X68000
if ( NFILES( &buf ) != 0 )
return( NULL );
return( (char *)buf.name );
#endif /* XC */
#ifdef MSC
in.h.ah = 0x4F ;
intdos( &in, &out );
if( out.x.cflag )
return( NULL );
return( dta+0x1E );
#endif /* MSC */
#if defined(DJ) || defined(__BORLANDC__)
if (findnext(&buf) != 0)
return NULL;
return buf.ff_name;
#endif
}