[PATCH] Cross-platform: progname: Made Path Seperator for Win

bprogyan at gmail.com bprogyan at gmail.com
Tue Jan 16 12:27:18 UTC 2018


From: Progyan Bhattacharya <progyanb at acm.org>

setprogname() can extract program name from path in windows systems as well.

Signed-off-by: Progyan Bhattacharya <progyanb at acm.org>
---
 include/bsd/stdlib.h | 10 ++++++++++
 src/progname.c       |  2 +-
 2 files changed, 11 insertions(+), 1 deletion(-)

diff --git a/include/bsd/stdlib.h b/include/bsd/stdlib.h
index ebc9638..37aa52d 100644
--- a/include/bsd/stdlib.h
+++ b/include/bsd/stdlib.h
@@ -46,6 +46,16 @@
 #include <sys/stat.h>
 #include <stdint.h>
 
+#if defined(_WIN32) || defined(__WIN32__)
+    # ifndef ENV_PATH_SEPERATOR
+        # define ENV_PATH_SEPERATOR '\\'
+    # endif
+#else
+    # ifndef ENV_PATH_SEPERATOR
+        # define ENV_PATH_SEPERATOR '/'
+    # endif
+#endif
+
 __BEGIN_DECLS
 uint32_t arc4random(void);
 void arc4random_stir(void);
diff --git a/src/progname.c b/src/progname.c
index 10c3701..64be30e 100644
--- a/src/progname.c
+++ b/src/progname.c
@@ -60,7 +60,7 @@ setprogname(const char *progname)
 {
 	const char *last_slash;
 
-	last_slash = strrchr(progname, '/');
+	last_slash = strrchr(progname, ENV_PATH_SEPERATOR);
 	if (last_slash == NULL)
 		__progname = progname;
 	else
-- 
2.15.1



More information about the libbsd mailing list