summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorJakob Kaivo <jkk@ung.org>2020-02-04 17:10:33 -0500
committerJakob Kaivo <jkk@ung.org>2020-02-04 17:10:33 -0500
commit88ab03ef5d6530ccd9ec1d245a1836360e7e78f6 (patch)
tree53e748d46d77864a1dffe4fb69465f306e124981
parent655f41b76907269af71fd67a87cbe0391b90a99f (diff)
satisfy guarantee
-rw-r--r--getprogname.c12
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)