]> git.ipfire.org Git - thirdparty/libbsd.git/commitdiff
progname: Port to Windows
authorGuillem Jover <guillem@hadrons.org>
Sun, 4 Mar 2018 23:37:47 +0000 (00:37 +0100)
committerGuillem Jover <guillem@hadrons.org>
Tue, 6 Mar 2018 00:10:14 +0000 (01:10 +0100)
Define the directory separator depending on the system targetted.

Reported-by: Progyan Bhattacharya <progyanb@acm.org>
src/progname.c

index 10c3701b5533a51f40b6447df356a95f6c8fbd58..3edbf24b31787f22c66024e97663769cb8ebf8bd 100644 (file)
 #include <string.h>
 #include <stdlib.h>
 
+#if defined(_WIN32) || defined(__WIN32__) || defined(__WINDOWS__)
+#define LIBBSD_IS_PATHNAME_SEPARATOR(c) ((c) == '/' || (c) == '\\')
+#else
+#define LIBBSD_IS_PATHNAME_SEPARATOR(c) ((c) == '/')
+#endif
+
 #ifdef HAVE___PROGNAME
 extern const char *__progname;
 #else
@@ -58,11 +64,13 @@ getprogname(void)
 void
 setprogname(const char *progname)
 {
-       const char *last_slash;
+       size_t i;
 
-       last_slash = strrchr(progname, '/');
-       if (last_slash == NULL)
-               __progname = progname;
-       else
-               __progname = last_slash + 1;
+       for (i = strlen(progname); i > 0; i--) {
+               if (LIBBSD_IS_PATHNAME_SEPARATOR(progname[i - 1])) {
+                       __progname = progname + i;
+                       return;
+               }
+       }
+       __progname = progname;
 }