@@ -117,9 +117,14 @@ AM_CONDITIONAL([WITH_GPIOSET_INTERACTIVE],
AS_IF([test "x$with_tools" = xtrue],
[# These are only needed to build tools
AC_CHECK_FUNC([daemon], [], [FUNC_NOT_FOUND_TOOLS([daemon])])
- AC_CHECK_FUNC([asprintf], [], [FUNC_NOT_FOUND_LIB([asprintf])])
- AC_CHECK_FUNC([scandir], [], [FUNC_NOT_FOUND_LIB([scandir])])
- AC_CHECK_FUNC([alphasort], [], [FUNC_NOT_FOUND_LIB([alphasort])])
+ AC_CHECK_FUNC([asprintf], [], [FUNC_NOT_FOUND_TOOLS([asprintf])])
+ AC_CHECK_FUNC([scandir], [], [FUNC_NOT_FOUND_TOOLS([scandir])])
+ AC_CHECK_FUNC([alphasort], [], [FUNC_NOT_FOUND_TOOLS([alphasort])])
+ 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>])])
AS_IF([test "x$with_gpioset_interactive" = xtrue],
[PKG_CHECK_MODULES([LIBEDIT], [libedit >= 3.1])])
])
@@ -12,7 +12,7 @@
static void print_help(void)
{
- printf("Usage: %s [OPTIONS] [chip]...\n", get_progname());
+ printf("Usage: %s [OPTIONS] [chip]...\n", getprogname());
printf("\n");
printf("List GPIO chips, print their labels and number of GPIO lines.\n");
printf("\n");
@@ -51,7 +51,7 @@ static int parse_config(int argc, char **argv)
print_version();
exit(EXIT_SUCCESS);
case '?':
- die("try %s --help", get_progname());
+ die("try %s --help", getprogname());
default:
abort();
}
@@ -26,7 +26,7 @@ struct config {
static void print_help(void)
{
- printf("Usage: %s [OPTIONS] <line>...\n", get_progname());
+ printf("Usage: %s [OPTIONS] <line>...\n", getprogname());
printf("\n");
printf("Read values of GPIO lines.\n");
printf("\n");
@@ -120,7 +120,7 @@ static int parse_config(int argc, char **argv, struct config *cfg)
print_version();
exit(EXIT_SUCCESS);
case '?':
- die("try %s --help", get_progname());
+ die("try %s --help", getprogname());
case 0:
break;
default:
@@ -20,7 +20,7 @@ struct config {
static void print_help(void)
{
- printf("Usage: %s [OPTIONS] [line]...\n", get_progname());
+ printf("Usage: %s [OPTIONS] [line]...\n", getprogname());
printf("\n");
printf("Print information about GPIO lines.\n");
printf("\n");
@@ -83,7 +83,7 @@ static int parse_config(int argc, char **argv, struct config *cfg)
print_version();
exit(EXIT_SUCCESS);
case '?':
- die("try %s --help", get_progname());
+ die("try %s --help", getprogname());
case 0:
break;
default:
@@ -34,7 +34,7 @@ struct config {
static void print_help(void)
{
- printf("Usage: %s [OPTIONS] <line>...\n", get_progname());
+ printf("Usage: %s [OPTIONS] <line>...\n", getprogname());
printf("\n");
printf("Wait for events on GPIO lines and print them to standard output.\n");
printf("\n");
@@ -195,7 +195,7 @@ static int parse_config(int argc, char **argv, struct config *cfg)
print_version();
exit(EXIT_SUCCESS);
case '?':
- die("try %s --help", get_progname());
+ die("try %s --help", getprogname());
case 0:
break;
default:
@@ -27,7 +27,7 @@ struct config {
static void print_help(void)
{
- printf("Usage: %s [OPTIONS] <line>...\n", get_progname());
+ printf("Usage: %s [OPTIONS] <line>...\n", getprogname());
printf("\n");
printf("Wait for changes to info on GPIO lines and print them to standard output.\n");
printf("\n");
@@ -144,7 +144,7 @@ static int parse_config(int argc, char **argv, struct config *cfg)
print_version();
exit(EXIT_SUCCESS);
case '?':
- die("try %s --help", get_progname());
+ die("try %s --help", getprogname());
case 0:
break;
default:
@@ -36,7 +36,7 @@ struct config {
static void print_help(void)
{
- printf("Usage: %s [OPTIONS] <line=value>...\n", get_progname());
+ printf("Usage: %s [OPTIONS] <line=value>...\n", getprogname());
printf("\n");
printf("Set values of GPIO lines.\n");
printf("\n");
@@ -212,7 +212,7 @@ static int parse_config(int argc, char **argv, struct config *cfg)
print_version();
exit(EXIT_SUCCESS);
case '?':
- die("try %s --help", get_progname());
+ die("try %s --help", getprogname());
case 0:
break;
default:
@@ -20,17 +20,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 print_error(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);
@@ -41,7 +43,7 @@ void print_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);
@@ -52,7 +54,7 @@ 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);
@@ -65,7 +67,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);
@@ -76,7 +78,7 @@ void die_perror(const char *fmt, ...)
void print_version(void)
{
printf("%s (libgpiod) v%s\n",
- program_invocation_short_name, gpiod_api_version());
+ getprogname(), gpiod_api_version());
printf("Copyright (C) 2017-2023 Bartosz Golaszewski\n");
printf("License: GPL-2.0-or-later\n");
printf("This is free software: you are free to change and redistribute it.\n");
@@ -78,7 +78,9 @@ struct line_resolver {
struct resolved_line lines[];
};
-const char *get_progname(void);
+#ifndef HAVE_GETPROGNAME
+const char *getprogname(void);
+#endif
void print_error(const char *fmt, ...) PRINTF(1, 2);
void print_perror(const char *fmt, ...) PRINTF(1, 2);
void die(const char *fmt, ...) NORETURN PRINTF(1, 2);
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 | 11 ++++++++--- tools/gpiodetect.c | 4 ++-- tools/gpioget.c | 4 ++-- tools/gpioinfo.c | 4 ++-- tools/gpiomon.c | 4 ++-- tools/gpionotify.c | 4 ++-- tools/gpioset.c | 4 ++-- tools/tools-common.c | 16 +++++++++------- tools/tools-common.h | 4 +++- 9 files changed, 32 insertions(+), 23 deletions(-)