Преглед изворни кода

libdpkg: Add new progname module handling functions

Guillem Jover пре 15 година
родитељ
комит
ec206bf34b
6 измењених фајлова са 147 додато и 1 уклоњено
  1. 2 1
      configure.ac
  2. 2 0
      lib/dpkg/Makefile.am
  3. 4 0
      lib/dpkg/libdpkg.Versions
  4. 83 0
      lib/dpkg/progname.c
  5. 33 0
      lib/dpkg/progname.h
  6. 23 0
      m4/dpkg-funcs.m4

+ 2 - 1
configure.ac

@@ -80,11 +80,12 @@ DPKG_FUNC_C99_SNPRINTF
 DPKG_CHECK_DECL([offsetof], [stddef.h])
 DPKG_CHECK_DECL([WCOREDUMP], [sys/wait.h])
 DPKG_CHECK_DECL([TIOCNOTTY], [sys/ioctl.h])
+DPKG_CHECK_PROGNAME
 DPKG_CHECK_COMPAT_FUNCS([getopt getopt_long obstack_free \
                          strnlen strerror strsignal asprintf \
                          scandir alphasort unsetenv])
 AC_CHECK_FUNCS([strtoul strtoimax isascii bcopy memcpy setsid getdtablesize \
-                lutimes posix_fadvise])
+                getprogname getexecname lutimes posix_fadvise])
 
 DPKG_MMAP
 

+ 2 - 0
lib/dpkg/Makefile.am

@@ -56,6 +56,7 @@ libdpkg_a_SOURCES = \
 	pkg-list.c \
 	pkg-queue.c \
 	pkg-show.c \
+	progname.c \
 	progress.c \
 	string.c \
 	subproc.c \
@@ -92,6 +93,7 @@ pkginclude_HEADERS = \
 	pkg-list.h \
 	pkg-queue.h \
 	pkg-show.h \
+	progname.h \
 	progress.h \
 	string.h \
 	subproc.h \

+ 4 - 0
lib/dpkg/libdpkg.Versions

@@ -1,5 +1,9 @@
 LIBDPKG_0 {
 global:
+	# Program name
+	dpkg_set_progname;
+	dpkg_get_progname;
+
 	# Ar support
 	dpkg_ar_normalize_name;
 

+ 83 - 0
lib/dpkg/progname.c

@@ -0,0 +1,83 @@
+/*
+ * libdpkg - Debian packaging suite library routines
+ * progname.c - program name handling functions
+ *
+ * Copyright © 2011 Guillem Jover <guillem@debian.org>
+ *
+ * This is free software; you can redistribute it and/or modify
+ * it under the terms of the GNU General Public License as published by
+ * the Free Software Foundation; either version 2 of the License, or
+ * (at your option) any later version.
+ *
+ * This is distributed in the hope that it will be useful,
+ * but WITHOUT ANY WARRANTY; without even the implied warranty of
+ * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
+ * GNU General Public License for more details.
+ *
+ * You should have received a copy of the GNU General Public License
+ * along with this program.  If not, see <http://www.gnu.org/licenses/>.
+ */
+
+#include <config.h>
+#include <compat.h>
+
+#include <errno.h>
+#include <string.h>
+#include <stdlib.h>
+
+#include <dpkg/progname.h>
+
+static const char *progname;
+
+/**
+ * Set the program name.
+ *
+ * This function will set the program name which will be used by error
+ * reporting functions, among others.
+ *
+ * @param name The new program name.
+ */
+void
+dpkg_set_progname(const char *name)
+{
+	const char *last_slash;
+
+	last_slash = strrchr(name, '/');
+	if (last_slash == NULL)
+		progname = name;
+	else
+		progname = last_slash + 1;
+}
+
+#if defined(HAVE___PROGNAME)
+extern const char *__progname;
+#endif
+
+/**
+ * Get the program name.
+ *
+ * The program name might have been set previously by dpkg_set_progname(),
+ * but if not this function will try to initialize it by system dependant
+ * means, so it's half safe to not call dpkg_set_progname() at all. At worst
+ * the function might return NULL in that case.
+ *
+ * @return A pointer to a static buffer with the program name.
+ */
+const char *
+dpkg_get_progname(void)
+{
+	if (progname == NULL) {
+#if defined(HAVE_PROGRAM_INVOCATION_SHORT_NAME)
+		progname = program_invocation_short_name;
+#elif defined(HAVE___PROGNAME)
+		progname = __progname;
+#elif defined(HAVE_GETPROGNAME)
+		progname = getprogname();
+#elif defined(HAVE_GETEXECNAME)
+		/* getexecname(3) returns an absolute path, normalize it. */
+		dpkg_set_progname(getexecname());
+#endif
+	}
+
+	return progname;
+}

+ 33 - 0
lib/dpkg/progname.h

@@ -0,0 +1,33 @@
+/*
+ * libdpkg - Debian packaging suite library routines
+ * progname.h - program name handling functions
+ *
+ * Copyright © 2011 Guillem Jover <guillem@debian.org>
+ *
+ * This is free software; you can redistribute it and/or modify
+ * it under the terms of the GNU General Public License as published by
+ * the Free Software Foundation; either version 2 of the License, or
+ * (at your option) any later version.
+ *
+ * This is distributed in the hope that it will be useful,
+ * but WITHOUT ANY WARRANTY; without even the implied warranty of
+ * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
+ * GNU General Public License for more details.
+ *
+ * You should have received a copy of the GNU General Public License
+ * along with this program.  If not, see <http://www.gnu.org/licenses/>.
+ */
+
+#ifndef LIBDPKG_PROGNAME_H
+#define LIBDPKG_PROGNAME_H
+
+#include <dpkg/macros.h>
+
+DPKG_BEGIN_DECLS
+
+void dpkg_set_progname(const char *name);
+const char *dpkg_get_progname(void);
+
+DPKG_END_DECLS
+
+#endif

+ 23 - 0
m4/dpkg-funcs.m4

@@ -85,6 +85,29 @@ AC_DEFUN([DPKG_MMAP],
   ])
 ])
 
+# DPKG_CHECK_PROGNAME
+# -------------------
+# Check for system implementations of program name tracking.
+AC_DEFUN([DPKG_CHECK_PROGNAME],
+[
+  AC_MSG_CHECKING([for program_invocation_short_name])
+  AC_LINK_IFELSE([AC_LANG_PROGRAM([[#include <errno.h>]],
+                 [[const char *p = program_invocation_short_name;]])],
+                 [AC_DEFINE([HAVE_PROGRAM_INVOCATION_SHORT_NAME], [1],
+                            [Define to 1 if you have program_invocation_short_name])
+                  AC_MSG_RESULT([yes])],
+                 [AC_MSG_RESULT([no])])
+
+  AC_MSG_CHECKING([for __progname])
+  AC_LINK_IFELSE([AC_LANG_PROGRAM([[]],
+                 [[extern char *__progname;
+                   const char *p = __progname;]])],
+                 [AC_DEFINE([HAVE___PROGNAME], [1],
+                            [Define to 1 if you have __progname])
+                  AC_MSG_RESULT([yes])],
+                 [AC_MSG_RESULT([no])])
+]) # DPKG_CHECK_PROGNAME
+
 # DPKG_CHECK_COMPAT_FUNCS(LIST)
 # -----------------------
 # Check each function and define an automake conditional