#include <errno.h> #include <terrno.h> char **wildcard2(expr) char *expr; int gmatch(str, term) char *str, *expr; int notwild(term) char *term;
wildcard2 expands file wildcard expressions. Expressions can involve mulitple terms. Each expression term with wildcard characters is checked for match. Terms not involving wildcard characters are not checked for match.
Wildcards currently supported are *, ?, [...], and ~. ~ refers to the user's home directory; ~username refers to the home directory for username. Home directories are found by consulting /etc/passwd. Wildcard characters cannot be escaped, using \\ or quotes.
wildcard2 allocates memory for list using UNIX malloc and returns a pointer to the list. If the list references n files, then the list contains an array of n +1 pointers to filenames, that last of which is NULL, followed by a heap of null terminated filenames. When the application is finished with the list, it should free the list using free.
If some terms involve wildterms, but there are no matching files for any of these terms, wildcard2 fails and returns NULL. This is consistent with UNIX shell conventions. NULL is also returned if memory allocation fails, or in the case of bad expression syntax.
gmatch returns true if str matches wildcard expression term.
notwild returns true if term does not contain any wildcard characters.
wildcard2 returns either a pointer to list referencing one or more files, or NULL.
gmatch and notwild return nonzero if true, 0 if false.
The following errors are returned from wildcard2 via terrno:
EINVAL - Invalid wildcard expression.
ENOMEM - Unable to allocated enough memory
These errors are defined in /usr/include/errno.h. If wildcard2 returns NULL and terrno=0, there were no matching files for any terms involving wildcards.
/etc/passwd
malloc
wildcard2 was adapted from wildcard, written by Rong Chen for the University of Illinois Super Computer Center. wildcard makes extensive use of alloca, which is found under SUN UNIX, but not under other versions of UNIX, such as HP-UX. wildcard2 uses its own version of alloca for systems other than SUN.
Wildcards should not be used for output files, even in the case of ~.
Last Update: $Date: 1999/05/10 20:57:09 $