GETPROGNAME(3)
π GETPROGNAME(3) β the libc girlies π
okay so bestie, we're in the Library Functions Manual today (that's the manual name, iconic), and this page is all about getprogname, setprogname β your little program's name tag π
β¨ NAME β¨
getprogname, setprogname β get or set the program name, literally that's it, that's the whole vibe π«Ά
π LIBRARY π
Standard C Library (libc, -lc) β the one you already linked, no cap π
π§ SYNOPSIS π§
#include <stdlib.h>
const char *
getprogname(void);
void
setprogname(const char *progname);
bestie the header is <stdlib.h> and yes you need it π
πΈ DESCRIPTION πΈ
So the getprogname() and setprogname() functions manipulate the name of the current program β they're the little duo that keeps your program's identity straight β¨ They're used by error-reporting routines to produce consistent output, which is giving brand consistency, and we love that for them π
The getprogname() function returns the name of the program. But heads up β if the name has not been set yet, it will return NULL. π« So fr fr, check before you printf("%s") it, iykyk.
The setprogname() function sets the name of the program to be the last component of the progname argument. And here's the tea π β since a pointer to the given string is kept as the program name, it should not be modified for the rest of the program's lifetime. Like, it's not a copy, it's a whole long-term relationship with your pointer, so don't be out here editing it later π
In FreeBSD, the name of the program is set by the start-up code that is run before main() β so running setprogname() is not necessary there, it's already handled for you, main character energy π Programs that desire maximum portability should still call it though; on another operating system, these functions may be implemented in a portability library. Calling setprogname() allows the aforementioned library to learn the program name without modifications to the start-up code. Basically: call it anyway, your future self on some other OS will thank you π
π₯ EXAMPLES π₯
The following example presents a simple program, which shows the difference between getprogname() and argv[0] β and the difference is so real, watch this:
#include <stdio.h>
#include <stdlib.h>
int
main(int argc, char** argv)
{
printf("getprogname(): %s\n", getprogname());
printf("argv[0]: %s\n", argv[0]);
return (0);
}
When compiled and executed (e.g., with ./a.out) the output of the program is going to look like this:
getprogname(): a.out
argv[0]: ./a.out
see?? getprogname() gives you just a.out, but argv[0] said ./a.out with the whole path situation π that's the whole point of the example bestie, the way that one has the prefix and one does not π
π¦ SEE ALSO π¦
err(3)β go read it, it's the friend that actually uses all this πsetproctitle(3)β its sibling, different job, also slay β¨
β HISTORY β
These functions first appeared in NetBSD 1.6, and made their way into FreeBSD 4.4 β glow up across operating systems, we love a journey πΈ
π tiny reminder:getprogname()can hand youNULLif nothing's been set, andsetprogname()keeps your pointer, not a copy. Be normal about it π
kisses bestie π that was the Debian build of this page, dated April 18, 2021 β mwah, go name your program ππ«Ά
π¦ where this page comes from
- freebsd 14.5: base 14.5
- freebsd 15.1: base 15.1
- Copyright (c) 2001 Christopher G. Demetriou
- π girlified by deepseek-ai/DeepSeek-V4.1-Flash-1017 on : a language model's rewrite of the original, under its terms