[LTP] [PATCH 2/2] Use SAFE_RUNCMD()

Petr Vorel pvorel@suse.cz
Mon Mar 23 17:04:15 CET 2020


Hi Li,

> Or, simply to use access() if we gonna take care of embedded Linux, is this
> reliable?

> int tst_cmd_available(char *cmd)
> {
>     int ret = 0;
>     char path[PATH_MAX];

>     snprintf(path, PATH_MAX, "/usr/bin/%s", cmd);
>     if (!access(path, X_OK)) {
>         ret = 1;
>         goto out;
>     }

>     snprintf(path, PATH_MAX, "/usr/sbin/%s", cmd);
>     if (!access(path, X_OK)) {
>         ret = 1;
>         goto out;
>     }

>     snprintf(path, PATH_MAX, "/usr/local/bin/%s", cmd);
>     if (!access(path, X_OK)) {
>         ret = 1;
>         goto out;
>     }

>     snprintf(path, PATH_MAX, "/usr/local/sbin/%s", cmd);
>     if (!access(path, X_OK)) {
>         ret = 1;
>         goto out;
>     }

> out:
>     return ret;
> }

Something like this would work on whole PATH.
It's just a question if we want to use it.

int tst_cmd_available(char *cmd)
{
	char *dup = strdup(getenv("PATH"));
	char *s = dup;
	char *p = NULL;
	int ret = 0;
    char path[PATH_MAX];

	do {
		p = strchr(s, ':');
		if (p != NULL) {
			p[0] = 0;
		}
		snprintf(path, PATH_MAX, "%s/%s", s, cmd);

		if (!access(path, X_OK)) {
			ret = 1;
			break;
		}
		s = p + 1;
	} while (p != NULL);

	free(dup);
	return ret;
}

Kind regards,
Petr


More information about the ltp mailing list