#include <_proc.h> #include #include #include static const char *progname; const char *getprogname(void) { return progname; } void setprogname(const char *pg) { progname = pg; setproctitle(NULL); } #define DESCLEN (sizeof(_psdata_loc->desc)) void setproctitle(const char *fmt, ...) { 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; } } 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); } }