diff options
author | dzwdz | 2024-05-05 15:08:05 +0200 |
---|---|---|
committer | dzwdz | 2024-05-05 15:08:05 +0200 |
commit | d7e389ae21685dda6ef6619cbd3c64ad2db149b4 (patch) | |
tree | 5e11e6ddb52ebfb0f4db1b92ec6adcf7c8ae6561 /src/libc | |
parent | cb518ecd55cd7a45d0368fb9d68a1981c6c91adf (diff) |
libc/progname: saner implementation
what the fuck was I thinking when I originally implemented that?
Diffstat (limited to 'src/libc')
-rw-r--r-- | src/libc/stdlib/progname.c | 32 |
1 files changed, 23 insertions, 9 deletions
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); + } } |