Skip to content

Commit c7305c5

Browse files
committed
Add generic program name handlers
This is useful for platforms that don't have a native way to get/set the program name, such as AIX. In a way, this reverts commit fa06b68 but makes use of the newly added wrapper functions. Related: #4053 (cherry picked from commit b5de0f8)
1 parent 56f00c3 commit c7305c5

File tree

1 file changed

+13
-6
lines changed

1 file changed

+13
-6
lines changed

rpmio/progname.cc

Lines changed: 13 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -6,8 +6,9 @@
66
#include <stdlib.h>
77
#elif defined(HAVE___PROGNAME) /* glibc and others */
88
extern const char *__progname;
9-
#else
10-
# error "Did not find any sutable implementation of getprogname/setprogname"
9+
#else /* generic implementation */
10+
#include <string.h> /* strrchr */
11+
static const char *rprogname = NULL;
1112
#endif
1213

1314
const char *rgetprogname(void)
@@ -16,8 +17,8 @@ const char *rgetprogname(void)
1617
return getprogname();
1718
#elif defined(HAVE___PROGNAME) /* glibc and others */
1819
return __progname;
19-
#else
20-
# error "Did not find any sutable implementation of getprogname"
20+
#else /* generic implementation */
21+
return (rprogname != NULL) ? rprogname : "";
2122
#endif
2223
}
2324

@@ -26,7 +27,13 @@ void rsetprogname(const char *pn)
2627
#if defined(HAVE_SETPROGNAME) /* BSD'ish systems */
2728
setprogname(pn);
2829
#elif defined(HAVE___PROGNAME) /* glibc and others */
29-
#else
30-
# error "Did not find any sutable implementation of setprogname"
30+
#else /* generic implementation */
31+
if (pn != NULL && rprogname == NULL /* set the value only once */) {
32+
const char *p = strrchr(pn, '/'); /* locate the last occurrence of '/' */
33+
if (p != NULL)
34+
rprogname = p + 1 /* strip beginning '/' */;
35+
else
36+
rprogname = pn;
37+
}
3138
#endif
3239
}

0 commit comments

Comments
 (0)