From d7e389ae21685dda6ef6619cbd3c64ad2db149b4 Mon Sep 17 00:00:00 2001 From: dzwdz Date: Sun, 5 May 2024 15:08:05 +0200 Subject: libc/progname: saner implementation what the fuck was I thinking when I originally implemented that? --- src/libc/stdlib/progname.c | 32 +++++++++++++++++++++++--------- 1 file changed, 23 insertions(+), 9 deletions(-) (limited to 'src') diff --git a/src/libc/stdlib/progname.c b/src/libc/stdlib/progname.c index 4cc0fb2..bd90063 100644 --- a/src/libc/stdlib/progname.c +++ b/src/libc/stdlib/progname.c @@ -12,17 +12,31 @@ void setprogname(const char *pg) { setproctitle(NULL); } +#define DESCLEN (sizeof(_psdata_loc->desc)) + void setproctitle(const char *fmt, ...) { - // TODO bounds checking - if (!fmt) { - strcpy(_psdata_loc->desc, progname); - return; + char *title = NULL; + + if (fmt) { + va_list ap; + va_start(ap, fmt); + int ret = vasprintf(&title, fmt, ap); + va_end(ap); + if (ret < 0) { + /* strictly speaking this is unnecessary (my vasprintf already + * behaves like this), but I want it to be clear that if title + * isn't NULL, it's a valid string */ + title = NULL; + } } - sprintf(_psdata_loc->desc, "%s: ", progname); - va_list argp; - va_start(argp, fmt); - vsnprintf(_psdata_loc->desc + strlen(_psdata_loc->desc), 128, fmt, argp); - va_end(argp); + if (title) { + snprintf(_psdata_loc->desc, DESCLEN, "%s: %s", progname, title); + free(title); + } else { + /* poor man's strlcpy. i do like the symmetry between the two branches + * though */ + snprintf(_psdata_loc->desc, DESCLEN, "%s", progname); + } } -- cgit v1.2.3