Add a simple file name parser to my_searchenv(),

to try to find the requested file without device, directory and/or in
lower case.
This commit is contained in:
Olaf Seibert 2015-05-31 22:00:04 +02:00
parent 87814f7bb9
commit e46f8ee2c7
2 changed files with 86 additions and 3 deletions

86
util.c
View File

@ -55,6 +55,18 @@ DAMAGE.
#include <unistd.h>
#endif
static void my_searchenv1(
char *name,
char *envname,
char *hitfile,
int hitlen);
static void my_searchenv2(
char *name,
char *envname,
char *hitfile,
int hitlen);
/* Sure, the library typically provides some kind of
ultoa or _ultoa function. But since it's merely typical
and not standard, and since the function is so simple,
@ -118,9 +130,8 @@ char *my_ltoa(
environment variable. I duplicate that function for portability.
Note also that mine avoids destination buffer overruns.
Note: uses strtok. This means it'll screw you up if you
expect your strtok context to remain intact when you use
this function.
Added functionality to lowercase the file name and to remove the
device name and directory.
*/
void my_searchenv(
@ -128,6 +139,64 @@ void my_searchenv(
char *envname,
char *hitfile,
int hitlen)
{
my_searchenv1(name, envname, hitfile, hitlen);
if (*hitfile) {
return;
}
char *copy = memcheck(strdup(name));
downcase(copy);
my_searchenv1(copy, envname, hitfile, hitlen);
free(copy);
}
static void my_searchenv1(
char *name,
char *envname,
char *hitfile,
int hitlen)
{
my_searchenv2(name, envname, hitfile, hitlen);
if (*hitfile) {
return;
}
/*
* Parse DEV:[DIR]NAME
* while re-trying the search after DEV: and after [DIR].
*
* Let's not be too critical about the characters we allow in those.
*/
char *p = name;
char c;
while ((c = *p++)) {
if (c == ':') {
my_searchenv2(p, envname, hitfile, hitlen);
if (*hitfile) {
return;
}
}
if (c == '[') {
char *enddir = strchr(p, ']');
if (enddir == NULL) {
return; /* weird syntax */
}
p = enddir + 1;
my_searchenv2(p, envname, hitfile, hitlen);
return;
}
}
return;
}
static void my_searchenv2(
char *name,
char *envname,
char *hitfile,
int hitlen)
{
char *env;
char *envcopy;
@ -215,6 +284,17 @@ void upcase(
}
}
/* downcase turns a string to lower case */
void downcase(
char *str)
{
while (*str) {
*str = tolower((unsigned char)*str);
str++;
}
}
/* padto adds blanks to the end of a string until it's the given
length. */

3
util.h
View File

@ -79,6 +79,9 @@ typedef unsigned long long ulong64;
void upcase(
char *str);
void downcase(
char *str);
void padto(
char *str,
int to);