summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
-rw-r--r--.gitignore3
-rw-r--r--getprogname.c29
-rw-r--r--getprogname.h9
3 files changed, 41 insertions, 0 deletions
diff --git a/.gitignore b/.gitignore
new file mode 100644
index 0000000..33ec174
--- /dev/null
+++ b/.gitignore
@@ -0,0 +1,3 @@
+*.o
+*.so
+*.a
diff --git a/getprogname.c b/getprogname.c
new file mode 100644
index 0000000..9294f44
--- /dev/null
+++ b/getprogname.c
@@ -0,0 +1,29 @@
+#define _POSIX_C_SOURCE 2
+#include <limits.h>
+#include <stdio.h>
+#include <unistd.h>
+
+char *get_program_name(size_t n, char buf[static n])
+{
+ char cmd[_POSIX_ARG_MAX];
+ snprintf(cmd, sizeof(cmd), "ps -p %ld -o comm=", (long)getpid());
+
+ FILE *pip = popen(cmd, "r");
+ if (pip == NULL) {
+ return NULL;
+ }
+
+ if (fscanf(pip, "%s", buf) != 1) {
+ pclose(pip);
+ return NULL;
+ }
+
+ pclose(pip);
+ return buf;
+}
+
+char *getprogname(void)
+{
+ static char progname[_POSIX_NAME_MAX];
+ return get_program_name(sizeof(progname), progname);
+}
diff --git a/getprogname.h b/getprogname.h
new file mode 100644
index 0000000..f8f55bb
--- /dev/null
+++ b/getprogname.h
@@ -0,0 +1,9 @@
+#ifndef GETPROGNAME_H
+#define GETPROGNAME_H
+
+#include <stddef.h>
+
+char *get_program_name(size_t n, char buf[static n]);
+char *getprogname(void);
+
+#endif