GETPROGNAME(3)

πŸ“¦ version 15.1, in freebsd 14.5, freebsd 15.1

πŸ’– 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 you NULL if nothing's been set, and setprogname() 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