Skip to content

Commit

Permalink
make: strip leading whitespace in shell assignment
Browse files Browse the repository at this point in the history
Assignment of shell output to a macro ('!=') was originally a
non-POSIX extension.  It later became a POSIX 202X feature.

However, the implementation failed to include the additional
POSIX requirement that leading whitespace is removed from the
shell output.

Neither GNU make nor bmake strip leading whitespace.  Implement
this behaviour as a non-POSIX extension.
  • Loading branch information
rmyorston committed Feb 1, 2023
1 parent 2d848eb commit 6e9d680
Showing 1 changed file with 28 additions and 11 deletions.
39 changes: 28 additions & 11 deletions miscutils/make.c
Expand Up @@ -1636,22 +1636,39 @@ run_command(const char *cmd)
}
pclose(fd);

if (val) {
#if ENABLE_PLATFORM_MINGW32
len = remove_cr(val, len + 1) - 1;
if (val == NULL)
return NULL;

// Strip leading whitespace in POSIX 202X mode
if (posix) {
s = val;
while (isspace(*s)) {
++s;
--len;
}

if (len == 0) {
free(val);
return NULL;
}
memmove(val, s, len + 1);
}

#if ENABLE_PLATFORM_MINGW32
len = remove_cr(val, len + 1) - 1;
if (len == 0) {
free(val);
return NULL;
}
#endif
// Remove one newline from the end (BSD compatibility)
if (val[len - 1] == '\n')
val[len - 1] = '\0';
// Other newlines are changed to spaces
for (s = val; *s; ++s) {
if (*s == '\n')
*s = ' ';
}

// Remove one newline from the end (BSD compatibility)
if (val[len - 1] == '\n')
val[len - 1] = '\0';
// Other newlines are changed to spaces
for (s = val; *s; ++s) {
if (*s == '\n')
*s = ' ';
}
return val;
}
Expand Down

0 comments on commit 6e9d680

Please sign in to comment.