libbb: fix empty PATH components handling

function                                             old     new   delta
find_execable                                         81      86      +5
exists_execable                                       71      66      -5

Signed-off-by: Denys Vlasenko <vda.linux@googlemail.com>
This commit is contained in:
Denys Vlasenko 2014-05-02 17:08:29 +02:00
parent a4476eb654
commit 15a357e596

View File

@ -30,6 +30,14 @@ int FAST_FUNC execable_file(const char *name)
*/ */
char* FAST_FUNC find_execable(const char *filename, char **PATHp) char* FAST_FUNC find_execable(const char *filename, char **PATHp)
{ {
/* About empty components in $PATH:
* http://pubs.opengroup.org/onlinepubs/009695399/basedefs/xbd_chap08.html
* 8.3 Other Environment Variables - PATH
* A zero-length prefix is a legacy feature that indicates the current
* working directory. It appears as two adjacent colons ( "::" ), as an
* initial colon preceding the rest of the list, or as a trailing colon
* following the rest of the list.
*/
char *p, *n; char *p, *n;
p = *PATHp; p = *PATHp;
@ -37,14 +45,15 @@ char* FAST_FUNC find_execable(const char *filename, char **PATHp)
n = strchr(p, ':'); n = strchr(p, ':');
if (n) if (n)
*n++ = '\0'; *n++ = '\0';
if (*p != '\0') { /* it's not a PATH="foo::bar" situation */ p = concat_path_file(
p = concat_path_file(p, filename); p[0] ? p : ".", /* handle "::" case */
if (execable_file(p)) { filename
*PATHp = n; );
return p; if (execable_file(p)) {
} *PATHp = n;
free(p); return p;
} }
free(p);
p = n; p = n;
} /* on loop exit p == NULL */ } /* on loop exit p == NULL */
return p; return p;
@ -60,11 +69,8 @@ int FAST_FUNC exists_execable(const char *filename)
char *tmp = path; char *tmp = path;
char *ret = find_execable(filename, &tmp); char *ret = find_execable(filename, &tmp);
free(path); free(path);
if (ret) { free(ret);
free(ret); return ret != NULL;
return 1;
}
return 0;
} }
#if ENABLE_FEATURE_PREFER_APPLETS #if ENABLE_FEATURE_PREFER_APPLETS