Changeset View
Changeset View
Standalone View
Standalone View
usr.bin/xinstall/xinstall.c
Show First 20 Lines • Show All 1,300 Lines • ▼ Show 20 Lines | copy(int from_fd, const char *from_name, int to_fd, const char *to_name, | ||||
} | } | ||||
return (digest_end(&ctx, NULL)); | return (digest_end(&ctx, NULL)); | ||||
} | } | ||||
/* | /* | ||||
* strip -- | * strip -- | ||||
* Use strip(1) to strip the target file. | * Use strip(1) to strip the target file. | ||||
* Just invoke strip(1) on to_name if from_name is NULL, else try | * Just invoke strip(1) on to_name if from_name is NULL, else try | ||||
* to run "strip -o to_name -- from_name" and return 0 on failure. | * to run "strip -o to_name from_name" and return 0 on failure. | ||||
* Return 1 on success and assign result of digest_file(to_name) | * Return 1 on success and assign result of digest_file(to_name) | ||||
* to *dresp. | * to *dresp. | ||||
*/ | */ | ||||
static int | static int | ||||
strip(const char *to_name, int to_fd, const char *from_name, char **dresp) | strip(const char *to_name, int to_fd, const char *from_name, char **dresp) | ||||
{ | { | ||||
const char *stripbin; | const char *stripbin; | ||||
const char *args[6]; | const char *args[5]; | ||||
char *prefixed_from_name; | |||||
jilles: Perhaps name this variable something like `prefixed_from_name`. | |||||
pid_t pid; | pid_t pid; | ||||
int error, serrno, status; | int error, serrno, status; | ||||
prefixed_from_name = NULL; | |||||
stripbin = getenv("STRIPBIN"); | stripbin = getenv("STRIPBIN"); | ||||
if (stripbin == NULL) | if (stripbin == NULL) | ||||
stripbin = "strip"; | stripbin = "strip"; | ||||
args[0] = stripbin; | args[0] = stripbin; | ||||
if (from_name == NULL) { | if (from_name == NULL) { | ||||
args[1] = to_name; | args[1] = to_name; | ||||
args[2] = NULL; | args[2] = NULL; | ||||
} else { | } else { | ||||
args[1] = "-o"; | args[1] = "-o"; | ||||
args[2] = to_name; | args[2] = to_name; | ||||
args[3] = "--"; | |||||
args[4] = from_name; | /* Prepend './' if from_name begins with '-' */ | ||||
args[5] = NULL; | if (from_name[0] == '-') { | ||||
if (asprintf(&prefixed_from_name, "./%s", from_name) == -1) | |||||
mhorneAuthorUnsubmitted Done Inline ActionsI'll note that this now goes slightly over 80 columns, but I found a linebreak here to be less legible. mhorne: I'll note that this now goes slightly over 80 columns, but I found a linebreak here to be less… | |||||
return (0); | |||||
args[3] = prefixed_from_name; | |||||
Done Inline ActionsThe extra semicolon means it will return here always, and never run strip with the modified path. Also, snprintf() will not fail if the buffer is too small. Instead, it will write a truncated string and return the full length. An alternative to a fixed-size buffer could be heap allocation using asprintf(); this is not limited to a fixed length, but the resulting string needs to be freed afterward. jilles: The extra semicolon means it will return here always, and never run strip with the modified… | |||||
} else { | |||||
args[3] = from_name; | |||||
} | } | ||||
args[4] = NULL; | |||||
} | |||||
error = posix_spawnp(&pid, stripbin, NULL, NULL, | error = posix_spawnp(&pid, stripbin, NULL, NULL, | ||||
__DECONST(char **, args), environ); | __DECONST(char **, args), environ); | ||||
if (error != 0) { | if (error != 0) { | ||||
(void)unlink(to_name); | (void)unlink(to_name); | ||||
errc(error == EAGAIN || error == EPROCLIM || error == ENOMEM ? | errc(error == EAGAIN || error == EPROCLIM || error == ENOMEM ? | ||||
EX_TEMPFAIL : EX_OSERR, error, "spawn %s", stripbin); | EX_TEMPFAIL : EX_OSERR, error, "spawn %s", stripbin); | ||||
} | } | ||||
free(prefixed_from_name); | |||||
if (waitpid(pid, &status, 0) == -1) { | if (waitpid(pid, &status, 0) == -1) { | ||||
Not Done Inline ActionsCalling free(NULL) need not be prevented. jilles: Calling `free(NULL)` need not be prevented. | |||||
error = errno; | error = errno; | ||||
(void)unlink(to_name); | (void)unlink(to_name); | ||||
errc(EX_SOFTWARE, error, "wait"); | errc(EX_SOFTWARE, error, "wait"); | ||||
/* NOTREACHED */ | /* NOTREACHED */ | ||||
} | } | ||||
if (status != 0) { | if (status != 0) { | ||||
if (from_name != NULL) | if (from_name != NULL) | ||||
return (0); | return (0); | ||||
▲ Show 20 Lines • Show All 190 Lines • Show Last 20 Lines |
Perhaps name this variable something like prefixed_from_name.