summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorStef Walter <stefw@gnome.org>2012-05-01 10:38:58 +0200
committerStef Walter <stefw@gnome.org>2012-05-01 10:38:58 +0200
commitfed549ee2049a318081cfce3fde01ae625263d98 (patch)
treecc23dd39240091aeee0b45b17064abfd31e0a1ae
parenta3bcb9037ddf6657f79f0aae42aa83dd2b8f6b14 (diff)
downloadp11-kit-fed549ee2049a318081cfce3fde01ae625263d98.tar.gz
Provide compat getprogname() implementations on other OS's
* And use them in our replacement err() and p11_kit_set_progname()
-rw-r--r--common/compat.c87
-rw-r--r--common/compat.h10
-rw-r--r--p11-kit/Makefile.am7
-rw-r--r--p11-kit/util.c38
-rw-r--r--tools/Makefile.am1
-rw-r--r--tools/p11-kit.c2
6 files changed, 81 insertions, 64 deletions
diff --git a/common/compat.c b/common/compat.c
index 93ba77c..4c8016b 100644
--- a/common/compat.c
+++ b/common/compat.c
@@ -36,40 +36,81 @@
#include "compat.h"
-#ifndef HAVE_ERR_H
+#ifndef HAVE_GETPROGNAME
-#include <stdlib.h>
-#include <stdio.h>
-#include <string.h>
-#include <stdarg.h>
+#ifdef OS_UNIX
+
+#if defined (HAVE_PROGRAM_INVOCATION_SHORT_NAME) && !HAVE_DECL_PROGRAM_INVOCATION_SHORT_NAME
+extern char *program_invocation_short_name;
+#endif
-static const char *
-calc_prog_name (void)
+#if defined (HAVE___PROGNAME) && !HAVE_DECL___PROGNAME
+extern char *__progname;
+#endif
+
+const char *
+getprogname (void)
{
- static char prognamebuf[256];
- static int prepared = 0;
+ const char *name;
- if(!prepared)
- {
- const char* beg = strrchr(__argv[0], '\\');
- const char* temp = strrchr(__argv[0], '/');
- beg = (beg > temp) ? beg : temp;
- beg = (beg) ? beg + 1 : __argv[0];
+#if defined (HAVE_GETEXECNAME)
+ const char *p;
+ name = getexecname();
+ p = strrchr (name ? name : "", '/');
+ if (p != NULL)
+ name = p + 1;
+#elif defined (HAVE_PROGRAM_INVOCATION_SHORT_NAME)
+ name = program_invocation_short_name;
+#elif defined (HAVE___PROGNAME)
+ name = __progname;
+#else
+ #error No way to retrieve short program name
+#endif
+
+ return name;
+}
- temp = strrchr(__argv[0], '.');
- temp = (temp > beg) ? temp : __argv[0] + strlen(__argv[0]);
+#else /* OS_WIN32 */
- if((temp - beg) > 255)
- temp = beg + 255;
+extern char **__argv;
+static char prognamebuf[256];
- strncpy(prognamebuf, beg, temp - beg);
- prognamebuf[temp - beg] = 0;
- prepared = 1;
- }
+const char *
+getprogname (void)
+{
+ const char *name;
+ const char *p;
+ size_t length;
+
+ name = __argv[0];
+ if (name == NULL)
+ return NULL;
+
+ p = strrchr (name, '\\');
+ if (p != NULL)
+ name = p + 1;
+
+ length = sizeof (prognamebuf) - 1;
+ strncpy (prognamebuf, name, length);
+ prognamebuf[length] = 0;
+ length = strlen (prognamebuf);
+ if (length > 4 && _stricmp (prognamebuf + (length - 4), ".exe"))
+ prognamebuf[length - 4] = '\0';
return prognamebuf;
}
+#endif /* OS_WIN32 */
+
+#endif /* HAVE_GETPROGNAME */
+
+#ifndef HAVE_ERR_H
+
+#include <stdlib.h>
+#include <stdio.h>
+#include <string.h>
+#include <stdarg.h>
+
static FILE *err_file; /* file to use for error output */
/*
diff --git a/common/compat.h b/common/compat.h
index 1562964..b2774f1 100644
--- a/common/compat.h
+++ b/common/compat.h
@@ -32,11 +32,15 @@
* Author: Stef Walter <stefw@collabora.co.uk>
*/
-#ifndef __ERR_H__
-#define __ERR_H__
+#ifndef __COMPAT_H__
+#define __COMPAT_H__
#include "config.h"
+#ifndef HAVE_GETPROGNAME
+const char * getprogname (void);
+#endif
+
#ifdef HAVE_ERR_H
#include <err.h>
@@ -60,4 +64,4 @@ void vwarnx (const char *fmt, va_list ap);
#endif /* !HAVE_ERR_H */
-#endif /* __ERR_H__ */
+#endif /* __COMPAT_H__ */
diff --git a/p11-kit/Makefile.am b/p11-kit/Makefile.am
index b855fb7..b1243c1 100644
--- a/p11-kit/Makefile.am
+++ b/p11-kit/Makefile.am
@@ -2,6 +2,7 @@ NULL =
INCLUDES = \
-I$(top_srcdir) \
+ -I$(top_srcdir)/common \
-DP11_KIT_FUTURE_UNSTABLE_API \
$(NULL)
@@ -25,6 +26,8 @@ MODULE_SRCS = \
ptr-array.c ptr-array.h \
messages.c \
uri.c \
+ $(top_srcdir)/common/compat.c \
+ $(top_srcdir)/common/compat.h \
$(inc_HEADERS)
lib_LTLIBRARIES = \
@@ -65,7 +68,9 @@ libp11_kit_testable_la_CFLAGS = \
$(NULL)
libp11_kit_compat_la_SOURCES = \
- util.c util.h
+ util.c util.h \
+ $(top_srcdir)/common/compat.c \
+ $(top_srcdir)/common/compat.h
pkgconfigdir = $(libdir)/pkgconfig
pkgconfig_DATA = p11-kit-1.pc
diff --git a/p11-kit/util.c b/p11-kit/util.c
index 63183b1..033c775 100644
--- a/p11-kit/util.c
+++ b/p11-kit/util.c
@@ -37,6 +37,7 @@
#include "config.h"
+#include "compat.h"
#define DEBUG_FLAG DEBUG_LIB
#include "debug.h"
#include "p11-kit.h"
@@ -316,41 +317,6 @@ _p11_mutex_init (mutex_t *mutex)
pthread_mutexattr_destroy (&attr);
}
-#if defined (HAVE_PROGRAM_INVOCATION_SHORT_NAME) && !HAVE_DECL_PROGRAM_INVOCATION_SHORT_NAME
-extern char *program_invocation_short_name;
-#endif
-
-#if defined (HAVE___PROGNAME) && !HAVE_DECL___PROGNAME
-extern char *__progname;
-#endif
-
-static char *
-get_default_progname (void)
-{
- const char *name;
-
-#if defined (HAVE_GETPROGNAME)
- name = getprogname();
-#elif defined (HAVE_GETEXECNAME)
- const char *p;
- name = getexecname();
- p = strrchr (name ? name : "", '/');
- if (p != NULL)
- name = p + 1;
-#elif defined (HAVE_PROGRAM_INVOCATION_SHORT_NAME)
- name = program_invocation_short_name;
-#elif defined (HAVE___PROGNAME)
- name = __progname;
-#else
- #error No way to retrieve program name from p11-kit library
-#endif
-
- if (name == NULL)
- return NULL;
-
- return strdup (name);
-}
-
#endif /* OS_UNIX */
#ifdef OS_WIN32
@@ -562,6 +528,6 @@ const char *
_p11_get_progname_unlocked (void)
{
if (_p11_my_progname == NULL)
- _p11_my_progname = get_default_progname ();
+ _p11_set_progname_unlocked (getprogname ());
return _p11_my_progname;
}
diff --git a/tools/Makefile.am b/tools/Makefile.am
index cec6bfc..bfa1953 100644
--- a/tools/Makefile.am
+++ b/tools/Makefile.am
@@ -1,6 +1,7 @@
INCLUDES = \
-I$(top_srcdir) \
+ -I$(top_srcdir)/common \
-I$(top_srcdir)/p11-kit \
-DSRCDIR=\"$(srcdir)\"
diff --git a/tools/p11-kit.c b/tools/p11-kit.c
index d4b0759..d154fe6 100644
--- a/tools/p11-kit.c
+++ b/tools/p11-kit.c
@@ -34,7 +34,7 @@
#include "config.h"
-#include "common/compat.h"
+#include "compat.h"
#include <assert.h>
#include <ctype.h>