home
***
CD-ROM
|
disk
|
FTP
|
other
***
search
/
DOS/V Power Report 1997 December
/
VPR9712A.ISO
/
OLS
/
OS2
/
LHA2P205
/
LHA2P205.LZH
/
lha2-2.05pre
/
source.lzh
/
src
/
match.c
< prev
next >
Wrap
C/C++ Source or Header
|
1996-02-25
|
4KB
|
215 lines
/*
* match.c --- match filenames
* Copyright (C) 1988-1992, Haruyasu YOSHIZAKI
* Copyright (C) 1991-1995, Satoshi HIRAMATSU (OS/2 HPFS version)
*
* $Log$
*/
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <ctype.h>
#include "port2.h"
#include "typedef.h"
#include "lh.h"
#include "errmes.h"
struct pat *pbuf;
static struct pat *pnxt, *plst;
/* initialize pattern pointers */
void
initpat(void)
{
pbuf = plst = e_malloc(sizeof(struct pat *));
pbuf -> nxt = NULL;
}
/* regist file matching pattern */
void
regpat(char *pattern, char *bdir)
{
char *p;
p = getfilename(pattern);
pnxt = e_malloc(strlen(pattern) + sizeof(struct pat));
pnxt -> nxt = NULL;
pnxt -> bdir = bdir;
pnxt -> cnt = 0;
strcpy(pnxt -> pname, pattern);
pnxt -> fname = pnxt -> pname + (p - pattern);
plst = plst -> nxt = pnxt;
}
/*
* regist base directory
*
* bdir : base directory
* return : saved address
*/
char *
regbdir(char *bdir)
{
char *p;
p = e_malloc(strlen(bdir) + 1);
strcpy(p, bdir);
return p;
}
/*
* match filename to pattern including wild cards
*
* pname : filename in the pattern
* fname : filename given
*/
char *
matchfname(char *pname, char *fname)
{
static char dummy[] = " ";
for(; *pname; pname++)
{
switch(*pname)
{
#ifndef __SUPPORT_HPFS__
case '+': /* We can use '+' for filename on OS/2 HPFS. */
#else
case '\xff': /* special case */
#endif
while((uchar)*fname != DELIM && *fname)
fname++;
break;
case '*':
while(*fname != '.' && (uchar)*fname != DELIM && *fname)
fname++;
break;
case '?':
if(*fname != '.' && (uchar)*fname != DELIM && *fname)
fname++;
break;
case '.':
if((uchar)*fname == DELIM || *fname == '\0')
break;
default:
if(iskanji(*pname))
{
if(*pname != *fname)
return dummy;
if(*++pname != *++fname)
return dummy;
}
else if((*pname != *fname) &&
(flg_i == 1 || toupper(*pname) != toupper(*fname)))
return dummy; /* not NULL */
fname++;
}
}
return fname;
}
/*
* match pathname to pattern
*
* path : pathname
* return : it's base directory
*/
char *
matchpat(char *path)
{
struct pat *pt;
char *p, *r;
char *file, *q;
char c;
file = getfilename(path);
for(pt = pbuf -> nxt; pt != NULL; pt = pt -> nxt)
{
p = pt -> pname;
switch(flg_r)
{
case 0:
if(pt -> fname == p && flg_p == 0)
q = matchfname(p, file);
else
q = matchfname(p, path);
if(*q)
q = NULL;
break;
case 1:
c = *(r = pt -> fname);
*r = '\0';
q = matchfname(p, path);
*r = c;
if(q)
{
q = matchfname(pt -> fname, file);
if(*q)
q = NULL;
}
break;
case 2:
q = matchfname(p, path);
if(*q && (uchar)*q != DELIM)
q = NULL;
break;
}
if(q)
{
(pt -> cnt)++;
return (pt -> bdir);
}
}
return NULL;
}
/*
* whether pattern was used or not
*
* return 0 : all pattern was used
* 1 : some pattern not used
*/
int
tstpat(void)
{
int cnt;
struct pat *pt;
cnt = 0;
for(pt = pbuf -> nxt; pt != NULL; pt = pt -> nxt)
cnt += pt -> cnt;
if(cnt == 0) /* no file matched */
error(NOFILEERR, NULL);
cnt = 0;
for(pt = pbuf -> nxt; pt != NULL; pt = pt -> nxt)
if(pt -> cnt == 0) /* if any work name was not used */
{
strcpy(work, pt -> pname);
if(cnt == 0)
eprintf("\n");
cnt++;
eprintf("%s : '%s'\n", NOMATCHERR, work);
errorlevel = 1; /* display warning */
}
return errorlevel;
}