- /* Fall back to searching $PATH (or _CS_PATH in absence of $PATH). */
- char *path = getenv("PATH");
- if (path == NULL) {
- /* _CS_PATH is typically something like "/bin:/usr/bin" */
- confstr(_CS_PATH, tmp, sizeof(tmp));
- sasprintf(&path, ":%s", tmp);
- } else {
- path = strdup(path);
- }
- const char *component;
- char *str = path;
- while (1) {
- if ((component = strtok(str, ":")) == NULL)
- break;
- str = NULL;
- snprintf(destpath, sizeof(destpath), "%s/%s", component, argv0);
- /* Of course this is not 100% equivalent to actually exec()ing the
+ /* Fall back to searching $PATH (or _CS_PATH in absence of $PATH). */
+ char *path = getenv("PATH");
+ if (path == NULL) {
+ /* _CS_PATH is typically something like "/bin:/usr/bin" */
+ while (confstr(_CS_PATH, tmp, tmp_size) > tmp_size) {
+ tmp_size = tmp_size * 2;
+ tmp = srealloc(tmp, tmp_size);
+ }
+ sasprintf(&path, ":%s", tmp);
+ } else {
+ path = sstrdup(path);
+ }
+ const char *component;
+ char *str = path;
+ while (1) {
+ if ((component = strtok(str, ":")) == NULL)
+ break;
+ str = NULL;
+ free(destpath);
+ sasprintf(&destpath, "%s/%s", component, argv0);
+ /* Of course this is not 100% equivalent to actually exec()ing the