home
***
CD-ROM
|
disk
|
FTP
|
other
***
search
/
RBBS in a Box Volume 1 #2
/
RBBS_vol1_no2.iso
/
add2
/
fc_v003.exe
/
WILDCARD.C
< prev
Wrap
Text File
|
1989-04-24
|
4KB
|
128 lines
/****************************************************************************
*
* WILDCARD.C
*
* Wildcard matching routines ...
*
* By: Ronald C. Bieber
*
***************************************************************************/
#include <stdio.h>
#include <dos.h>
#include <string.h>
#include <ctype.h>
#define REST '*'
#define ANY '?'
#define CMP(x, y) (toupper((x)) == toupper((y)) ? 1 : 0)
struct nmblk { /* filename structure used by wildcard */
char node[8];
char ext[3];
};
/******************************************************************************
*
* Name: parsname
* Synopsis: void parsname(str, pattern);
* struct nmblk *str; structure to hold name
* char *pattern; name to store in nmblk
*
* Description: parses the filename given in *pattern and stores it
* in the structure nmblk, seperating the name and
* extension.
*
****************************************************************************/
int parsname(str, pattern)
struct nmblk *str;
char *pattern;
{
int rest = -1,
cmp;
if (strchr(pattern, '.') == NULL) {
strcpy(str->node, pattern);
str->ext[0] = '*';
str->ext[1] = 0;
return(0);
}
while (++rest <= strlen(pattern))
if (pattern[rest] == '.')
cmp = rest;
for (rest = 0; rest < cmp; rest++)
str->node[rest] = pattern[rest];
str->node[rest] = 0; /* append NULL to end */
rest = 0;
cmp++;
while(pattern[cmp + rest] != 0)
str->ext[rest] = pattern[cmp + rest++];
str->ext[rest] = 0; /* append NULL to end */
}
/******************************************************************************
*
* Name: matchwc
* Synopsis: int matchwc(first, second);
* char *first; first string to compare
* char *second; 2nd string to compare
*
* Description: This function does the actual wildcard matching.
*
* Returns: True if a match was found, false if not.
*
****************************************************************************/
int matchwc (first, second)
char *first;
char *second;
{
char *p,
*l,
c;
int i = -1;
p = first;
l = second;
while ( (c = p[++i]) != NULL || l[i] != NULL) {
switch(c) {
case ANY: break;
case REST: return(1);
default: if (!CMP(l[i], p[i]))
return(0);
}
}
return(1);
}
/******************************************************************************
*
* Name: wildcards
* Synopsis: int wildcards(pattern, to);
* char *pattern; name to match (*, ? included)
* char *to; filename to match it to
*
* Description: User called function to match wildcard characters
* with a "for-real" type filename.
*
* Returns: True if a match was found, false if not.
****************************************************************************/
int wildcards(pattern, to)
char pattern[14];
char to[14];
{
struct nmblk first,
second;
parsname(&first, pattern);
parsname(&second, to);
if (matchwc(first.node, second.node) && matchwc(first.ext, second.ext))
return(1);
else
return(0);
}