diff options
-rw-r--r-- | getprogname.c | 12 |
1 files changed, 8 insertions, 4 deletions
diff --git a/getprogname.c b/getprogname.c index 9294f44..2beac9e 100644 --- a/getprogname.c +++ b/getprogname.c @@ -1,6 +1,7 @@ #define _POSIX_C_SOURCE 2 #include <limits.h> #include <stdio.h> +#include <string.h> #include <unistd.h> char *get_program_name(size_t n, char buf[static n]) @@ -13,13 +14,16 @@ char *get_program_name(size_t n, char buf[static n]) return NULL; } - if (fscanf(pip, "%s", buf) != 1) { - pclose(pip); - return NULL; + char *ret = fgets(pip, buf, n); + if (ret) { + char *nl = strchr(ret, '\n'); + if (nl) { + *nl = '\0'; + } } pclose(pip); - return buf; + return ret; } char *getprogname(void) |