diff mbox series

[libgpiod,v1.6.x,1/3] tools: use getprogname() when available to remove dependency on glibc

Message ID 20230225030813.3885661-2-benl@squareup.com
State New
Headers show
Series build: fix compile issues against non-glibc | expand

Commit Message

Benjamin Li Feb. 25, 2023, 3:08 a.m. UTC
Platforms like Bionic libc don't have program_invocation_[short_]name,
which is a GNU extension. Use getprogname() from stdlib.h, another
widely agreed extension, when it's available.

It seemed a little heavyweight to add gnulib to this project's autotools
set-up just for making one function portable, so I've just added the
portabilty shim to tools-common.c for the moment.

Signed-off-by: Benjamin Li <benl@squareup.com>
---
 configure.ac         |  5 +++++
 tools/gpiodetect.c   |  4 ++--
 tools/gpiofind.c     |  4 ++--
 tools/gpioget.c      |  4 ++--
 tools/gpioinfo.c     |  4 ++--
 tools/gpiomon.c      |  4 ++--
 tools/gpioset.c      |  4 ++--
 tools/tools-common.c | 12 +++++++-----
 tools/tools-common.h |  4 +++-
 9 files changed, 27 insertions(+), 18 deletions(-)
diff mbox series

Patch

diff --git a/configure.ac b/configure.ac
index 31681c2..53c26a0 100644
--- a/configure.ac
+++ b/configure.ac
@@ -132,6 +132,11 @@  then
 	AC_CHECK_FUNC([signalfd], [], [FUNC_NOT_FOUND_TOOLS([signalfd])])
 	AC_CHECK_FUNC([setlinebuf], [], [FUNC_NOT_FOUND_TOOLS([setlinebuf])])
 	AC_CHECK_HEADERS([sys/signalfd.h], [], [HEADER_NOT_FOUND_TOOLS([sys/signalfd.h])])
+	AC_CHECK_FUNC([getprogname],
+		[AC_DEFINE([HAVE_GETPROGNAME], 1, [Define to 1 if getprogname() is available.])],
+		[AC_CHECK_DECL([program_invocation_short_name], [],
+			[FUNC_NOT_FOUND_TOOLS([getprogname or program_invocation_short_name])],
+			[#include <errno.h>])])
 fi
 
 AC_ARG_ENABLE([tests],
diff --git a/tools/gpiodetect.c b/tools/gpiodetect.c
index 1c992a4..9e06b51 100644
--- a/tools/gpiodetect.c
+++ b/tools/gpiodetect.c
@@ -22,7 +22,7 @@  static const char *const shortopts = "+hv";
 
 static void print_help(void)
 {
-	printf("Usage: %s [OPTIONS]\n", get_progname());
+	printf("Usage: %s [OPTIONS]\n", getprogname());
 	printf("\n");
 	printf("List all GPIO chips, print their labels and number of GPIO lines.\n");
 	printf("\n");
@@ -50,7 +50,7 @@  int main(int argc, char **argv)
 			print_version();
 			return EXIT_SUCCESS;
 		case '?':
-			die("try %s --help", get_progname());
+			die("try %s --help", getprogname());
 		default:
 			abort();
 		}
diff --git a/tools/gpiofind.c b/tools/gpiofind.c
index 931ac40..6a81b63 100644
--- a/tools/gpiofind.c
+++ b/tools/gpiofind.c
@@ -22,7 +22,7 @@  static const char *const shortopts = "+hv";
 
 static void print_help(void)
 {
-	printf("Usage: %s [OPTIONS] <name>\n", get_progname());
+	printf("Usage: %s [OPTIONS] <name>\n", getprogname());
 	printf("\n");
 	printf("Find a GPIO line by name. The output of this command can be used as input for gpioget/set.\n");
 	printf("\n");
@@ -50,7 +50,7 @@  int main(int argc, char **argv)
 			print_version();
 			return EXIT_SUCCESS;
 		case '?':
-			die("try %s --help", get_progname());
+			die("try %s --help", getprogname());
 		default:
 			abort();
 		}
diff --git a/tools/gpioget.c b/tools/gpioget.c
index 815507d..066a8a8 100644
--- a/tools/gpioget.c
+++ b/tools/gpioget.c
@@ -26,7 +26,7 @@  static const char *const shortopts = "+hvlB:";
 static void print_help(void)
 {
 	printf("Usage: %s [OPTIONS] <chip name/number> <offset 1> <offset 2> ...\n",
-	       get_progname());
+	       getprogname());
 	printf("\n");
 	printf("Read line value(s) from a GPIO chip\n");
 	printf("\n");
@@ -67,7 +67,7 @@  int main(int argc, char **argv)
 			flags = bias_flags(optarg);
 			break;
 		case '?':
-			die("try %s --help", get_progname());
+			die("try %s --help", getprogname());
 		default:
 			abort();
 		}
diff --git a/tools/gpioinfo.c b/tools/gpioinfo.c
index 67be379..779475b 100644
--- a/tools/gpioinfo.c
+++ b/tools/gpioinfo.c
@@ -73,7 +73,7 @@  static const char *const shortopts = "+hv";
 
 static void print_help(void)
 {
-	printf("Usage: %s [OPTIONS] <gpiochip1> ...\n", get_progname());
+	printf("Usage: %s [OPTIONS] <gpiochip1> ...\n", getprogname());
 	printf("\n");
 	printf("Print information about all lines of the specified GPIO chip(s) (or all gpiochips if none are specified).\n");
 	printf("\n");
@@ -201,7 +201,7 @@  int main(int argc, char **argv)
 			print_version();
 			return EXIT_SUCCESS;
 		case '?':
-			die("try %s --help", get_progname());
+			die("try %s --help", getprogname());
 		default:
 			abort();
 		}
diff --git a/tools/gpiomon.c b/tools/gpiomon.c
index 213efaa..a2ef4ea 100644
--- a/tools/gpiomon.c
+++ b/tools/gpiomon.c
@@ -35,7 +35,7 @@  static const char *const shortopts = "+hvlB:n:srfbF:";
 static void print_help(void)
 {
 	printf("Usage: %s [OPTIONS] <chip name/number> <offset 1> <offset 2> ...\n",
-	       get_progname());
+	       getprogname());
 	printf("\n");
 	printf("Wait for events on GPIO lines and print them to standard output\n");
 	printf("\n");
@@ -275,7 +275,7 @@  int main(int argc, char **argv)
 			ctx.fmt = optarg;
 			break;
 		case '?':
-			die("try %s --help", get_progname());
+			die("try %s --help", getprogname());
 		default:
 			abort();
 		}
diff --git a/tools/gpioset.c b/tools/gpioset.c
index fb6d51e..0152179 100644
--- a/tools/gpioset.c
+++ b/tools/gpioset.c
@@ -35,7 +35,7 @@  static const char *const shortopts = "+hvlB:D:m:s:u:b";
 static void print_help(void)
 {
 	printf("Usage: %s [OPTIONS] <chip name/number> <offset1>=<value1> <offset2>=<value2> ...\n",
-	       get_progname());
+	       getprogname());
 	printf("\n");
 	printf("Set GPIO line values of a GPIO chip and maintain the state until the process exits\n");
 	printf("\n");
@@ -242,7 +242,7 @@  int main(int argc, char **argv)
 			cbdata.daemonize = true;
 			break;
 		case '?':
-			die("try %s --help", get_progname());
+			die("try %s --help", getprogname());
 		default:
 			abort();
 		}
diff --git a/tools/tools-common.c b/tools/tools-common.c
index 12bde20..cf829c3 100644
--- a/tools/tools-common.c
+++ b/tools/tools-common.c
@@ -19,17 +19,19 @@ 
 
 #include "tools-common.h"
 
-const char *get_progname(void)
+#ifndef HAVE_GETPROGNAME
+const char *getprogname(void)
 {
-	return program_invocation_name;
+	return program_invocation_short_name;
 }
+#endif
 
 void die(const char *fmt, ...)
 {
 	va_list va;
 
 	va_start(va, fmt);
-	fprintf(stderr, "%s: ", program_invocation_name);
+	fprintf(stderr, "%s: ", getprogname());
 	vfprintf(stderr, fmt, va);
 	fprintf(stderr, "\n");
 	va_end(va);
@@ -42,7 +44,7 @@  void die_perror(const char *fmt, ...)
 	va_list va;
 
 	va_start(va, fmt);
-	fprintf(stderr, "%s: ", program_invocation_name);
+	fprintf(stderr, "%s: ", getprogname());
 	vfprintf(stderr, fmt, va);
 	fprintf(stderr, ": %s\n", strerror(errno));
 	va_end(va);
@@ -53,7 +55,7 @@  void die_perror(const char *fmt, ...)
 void print_version(void)
 {
 	printf("%s (libgpiod) v%s\n",
-	       program_invocation_short_name, gpiod_version_string());
+	       getprogname(), gpiod_version_string());
 	printf("Copyright (C) 2017-2018 Bartosz Golaszewski\n");
 	printf("License: LGPLv2.1\n");
 	printf("This is free software: you are free to change and redistribute it.\n");
diff --git a/tools/tools-common.h b/tools/tools-common.h
index 9f1b801..7582c8b 100644
--- a/tools/tools-common.h
+++ b/tools/tools-common.h
@@ -21,7 +21,9 @@ 
 
 #define GETOPT_NULL_LONGOPT	NULL, 0, NULL, 0
 
-const char *get_progname(void);
+#ifndef HAVE_GETPROGNAME
+const char *getprogname(void);
+#endif
 void die(const char *fmt, ...) NORETURN PRINTF(1, 2);
 void die_perror(const char *fmt, ...) NORETURN PRINTF(1, 2);
 void print_version(void);