From e8928c577c9448e5be22b300335441333ce632c3 Mon Sep 17 00:00:00 2001 From: sisungo Date: Mon, 6 Jul 2026 20:29:37 +0800 Subject: [PATCH] fix: getprogname() is not implemented Signed-off-by: sisungo --- compat.h | 19 ++++++++++++++++++- grep.c | 2 ++ 2 files changed, 20 insertions(+), 1 deletion(-) diff --git a/compat.h b/compat.h index ff2281e..44c6cc5 100644 --- a/compat.h +++ b/compat.h @@ -1,13 +1,30 @@ #include #include #include +#include #ifndef __DECONST #define __DECONST(type, var) ((type)(uintptr_t)(const void *)(var)) #endif +#ifdef MAINFILE +const char *PROGNAME = NULL; + +static inline void initprogname(const char *argv0) { + PROGNAME = "grep"; + if (argv0 && argv0[0]) { + char *copied_argv0 = strdup(argv0); + if (!copied_argv0) return; + PROGNAME = basename(copied_argv0); + } +} +#else +extern const char *PROGNAME; +#endif + static inline const char *getprogname() { - return "grep"; + if (!PROGNAME) abort(); + return PROGNAME; } static inline void warnc(int code, const char *fmt, ...) { diff --git a/grep.c b/grep.c index 31c2c32..6934eb8 100644 --- a/grep.c +++ b/grep.c @@ -47,6 +47,7 @@ #include #include +#define MAINFILE #include "compat.h" #include "grep.h" @@ -319,6 +320,7 @@ main(int argc, char *argv[]) int c, lastc, needpattern, newarg, prevoptind; bool matched; + initprogname(argv[0]); setlocale(LC_ALL, ""); /*