about summary refs log tree commit diff
path: root/lib/libpm.c
diff options
context:
space:
mode:
authorgiraffedata <giraffedata@9d0c8265-081b-0410-96cb-a4ca84ce46f8>2012-02-11 22:28:21 +0000
committergiraffedata <giraffedata@9d0c8265-081b-0410-96cb-a4ca84ce46f8>2012-02-11 22:28:21 +0000
commita0f975d5f1d535d233373cb465a56e9612e99a1a (patch)
tree08d4d176aceed2dcc292eccf8a76c62b7d720a81 /lib/libpm.c
parentf578145b3bc3f37f0e09ca7a47a91a12212a821c (diff)
downloadnetpbm-mirror-a0f975d5f1d535d233373cb465a56e9612e99a1a.tar.gz
netpbm-mirror-a0f975d5f1d535d233373cb465a56e9612e99a1a.tar.xz
netpbm-mirror-a0f975d5f1d535d233373cb465a56e9612e99a1a.zip
Compute program name correctly on Windows
git-svn-id: http://svn.code.sf.net/p/netpbm/code/trunk@1631 9d0c8265-081b-0410-96cb-a4ca84ce46f8
Diffstat (limited to 'lib/libpm.c')
-rw-r--r--lib/libpm.c39
1 files changed, 31 insertions, 8 deletions
diff --git a/lib/libpm.c b/lib/libpm.c
index 69085099..54bd04cb 100644
--- a/lib/libpm.c
+++ b/lib/libpm.c
@@ -594,6 +594,32 @@ showNetpbmHelp(const char progname[]) {
 
 
 
+static const char *
+progNameFromArg0(const char * const arg0) {
+
+#ifdef _WIN32
+
+    char filename[256];
+
+    _splitpath(arg0, 0, 0, filename, 0);
+
+#else
+
+    /* Just take the last component of the file name, e.g. "foo" in
+       "/local/bin/foo"
+    */
+    const char * const slashPos = strrchr(arg0, '/');
+
+    const char * const filename = slashPos ? slashPos + 1 : arg0;
+
+#endif
+
+    return pm_strdup(filename);
+}
+
+    
+
+
 void
 pm_proginit(int * const argcP, const char * argv[]) {
 /*----------------------------------------------------------------------------
@@ -604,8 +630,12 @@ pm_proginit(int * const argcP, const char * argv[]) {
 
    This includes calling pm_init() to initialize the Netpbm libraries.
 -----------------------------------------------------------------------------*/
+    const char * const progname = progNameFromArg0(argv[0]);
+        /* Note: this is technically a memory leak; it is malloc'ed memory
+           that we have no way to give back because there is no pm_progterm().
+           So it goes down with the ship when the OS terminates the program.
+        */
     int argn, i;
-    const char * progname;
     bool showmessages;
     bool show_version;
         /* We're supposed to just show the version information, then exit the
@@ -615,13 +645,6 @@ pm_proginit(int * const argcP, const char * argv[]) {
         /* We're supposed to just tell user where to get help, then exit the
            program.
         */
-    
-    /* Extract program name. */
-    progname = strrchr( argv[0], '/');
-    if (progname == NULL)
-        progname = argv[0];
-    else
-        ++progname;
 
     pm_init(progname, 0);