static void print_help(void)
{
- printf("Usage: %s [OPTIONS] [chip]...\n", get_progname());
+ printf("Usage: %s [OPTIONS] [chip]...\n", get_prog_name());
printf("\n");
printf("List GPIO chips, print their labels and number of GPIO lines.\n");
printf("\n");
print_version();
exit(EXIT_SUCCESS);
case '?':
- die("try %s --help", get_progname());
+ die("try %s --help", get_prog_name());
default:
abort();
}
int num_chips, i, ret = EXIT_SUCCESS;
char **paths, *path;
+ set_prog_name(argv[0]);
i = parse_config(argc, argv);
argc -= i;
argv += i;
static void print_help(void)
{
- printf("Usage: %s [OPTIONS] <line>...\n", get_progname());
+ printf("Usage: %s [OPTIONS] <line>...\n", get_prog_name());
printf("\n");
printf("Read values of GPIO lines.\n");
printf("\n");
print_version();
exit(EXIT_SUCCESS);
case '?':
- die("try %s --help", get_progname());
+ die("try %s --help", get_prog_name());
case 0:
break;
default:
struct config cfg;
const char *fmt;
+ set_prog_name(argv[0]);
i = parse_config(argc, argv, &cfg);
argc -= i;
argv += i;
static void print_help(void)
{
- printf("Usage: %s [OPTIONS] [line]...\n", get_progname());
+ printf("Usage: %s [OPTIONS] [line]...\n", get_prog_name());
printf("\n");
printf("Print information about GPIO lines.\n");
printf("\n");
print_version();
exit(EXIT_SUCCESS);
case '?':
- die("try %s --help", get_progname());
+ die("try %s --help", get_prog_name());
case 0:
break;
default:
struct config cfg;
char **paths;
+ set_prog_name(argv[0]);
i = parse_config(argc, argv, &cfg);
argc -= i;
argv += i;
static void print_help(void)
{
- printf("Usage: %s [OPTIONS] <line>...\n", get_progname());
+ printf("Usage: %s [OPTIONS] <line>...\n", get_prog_name());
printf("\n");
printf("Wait for events on GPIO lines and print them to standard output.\n");
printf("\n");
print_version();
exit(EXIT_SUCCESS);
case '?':
- die("try %s --help", get_progname());
+ die("try %s --help", get_prog_name());
case 0:
break;
default:
struct config cfg;
int ret, i, j;
+ set_prog_name(argv[0]);
i = parse_config(argc, argv, &cfg);
argc -= i;
argv += i;
static void print_help(void)
{
- printf("Usage: %s [OPTIONS] <line>...\n", get_progname());
+ printf("Usage: %s [OPTIONS] <line>...\n", get_prog_name());
printf("\n");
printf("Wait for changes to info on GPIO lines and print them to standard output.\n");
printf("\n");
print_version();
exit(EXIT_SUCCESS);
case '?':
- die("try %s --help", get_progname());
+ die("try %s --help", get_prog_name());
case 0:
break;
default:
struct pollfd *pollfds;
struct config cfg;
+ set_prog_name(argv[0]);
i = parse_config(argc, argv, &cfg);
argc -= optind;
argv += optind;
static void print_help(void)
{
- printf("Usage: %s [OPTIONS] <line=value>...\n", get_progname());
+ printf("Usage: %s [OPTIONS] <line=value>...\n", get_prog_name());
printf("\n");
printf("Set values of GPIO lines.\n");
printf("\n");
print_version();
exit(EXIT_SUCCESS);
case '?':
- die("try %s --help", get_progname());
+ die("try %s --help", get_prog_name());
case 0:
break;
default:
struct config cfg;
char **lines;
+ set_prog_name(argv[0]);
i = parse_config(argc, argv, &cfg);
argc -= i;
argv += i;
#include "tools-common.h"
-const char *get_progname(void)
+static const char *prog_name = NULL;
+static const char *prog_short_name = NULL;
+
+void set_prog_name(const char *name)
+{
+ prog_name = name;
+ prog_short_name = name;
+ while (*name) {
+ if (*name++ == '/') {
+ prog_short_name = name;
+ }
+ }
+}
+
+const char *get_prog_name(void)
+{
+ return prog_name;
+}
+
+const char *get_prog_short_name(void)
{
- return program_invocation_name;
+ return prog_short_name;
}
void print_error(const char *fmt, ...)
va_list va;
va_start(va, fmt);
- fprintf(stderr, "%s: ", program_invocation_name);
+ fprintf(stderr, "%s: ", get_prog_name());
vfprintf(stderr, fmt, va);
fprintf(stderr, "\n");
va_end(va);
va_list va;
va_start(va, fmt);
- fprintf(stderr, "%s: ", program_invocation_name);
+ fprintf(stderr, "%s: ", get_prog_name());
vfprintf(stderr, fmt, va);
fprintf(stderr, ": %s\n", strerror(errno));
va_end(va);
va_list va;
va_start(va, fmt);
- fprintf(stderr, "%s: ", program_invocation_name);
+ fprintf(stderr, "%s: ", get_prog_name());
vfprintf(stderr, fmt, va);
fprintf(stderr, "\n");
va_end(va);
va_list va;
va_start(va, fmt);
- fprintf(stderr, "%s: ", program_invocation_name);
+ fprintf(stderr, "%s: ", get_prog_name());
vfprintf(stderr, fmt, va);
fprintf(stderr, ": %s\n", strerror(errno));
va_end(va);
void print_version(void)
{
- printf("%s (libgpiod) v%s\n",
- program_invocation_short_name, gpiod_api_version());
+ printf("%s (libgpiod) v%s\n", get_prog_short_name(), 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");
struct resolved_line lines[];
};
-const char *get_progname(void);
+void set_prog_name(const char *name);
+const char *get_prog_name(void);
+const char *get_prog_short_name(void);
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);