mirror of
https://github.com/open-simh/simtools.git
synced 2026-04-30 13:31:33 +00:00
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:
86
util.c
86
util.c
@@ -55,6 +55,18 @@ DAMAGE.
|
|||||||
#include <unistd.h>
|
#include <unistd.h>
|
||||||
#endif
|
#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
|
/* Sure, the library typically provides some kind of
|
||||||
ultoa or _ultoa function. But since it's merely typical
|
ultoa or _ultoa function. But since it's merely typical
|
||||||
and not standard, and since the function is so simple,
|
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.
|
environment variable. I duplicate that function for portability.
|
||||||
Note also that mine avoids destination buffer overruns.
|
Note also that mine avoids destination buffer overruns.
|
||||||
|
|
||||||
Note: uses strtok. This means it'll screw you up if you
|
Added functionality to lowercase the file name and to remove the
|
||||||
expect your strtok context to remain intact when you use
|
device name and directory.
|
||||||
this function.
|
|
||||||
*/
|
*/
|
||||||
|
|
||||||
void my_searchenv(
|
void my_searchenv(
|
||||||
@@ -128,6 +139,64 @@ void my_searchenv(
|
|||||||
char *envname,
|
char *envname,
|
||||||
char *hitfile,
|
char *hitfile,
|
||||||
int hitlen)
|
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 *env;
|
||||||
char *envcopy;
|
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
|
/* padto adds blanks to the end of a string until it's the given
|
||||||
length. */
|
length. */
|
||||||
|
|
||||||
|
|||||||
Reference in New Issue
Block a user