getprogname(3)

getprogname3

Get or set the program name

Usage

#include <stdlib.h>

const char *

getprogname(void);

void

setprogname(const char *progname);

Library

Standard C Library (libc, -lc)

Overview

The getprogname() and setprogname() functions manipulate the name of the current program. They are used by error-reporting routines to produce consistent output.

The getprogname() function returns the name of the program. If the name has not been set yet, it will return NULL.

The setprogname() function sets the name of the program to be the last component of the progname argument. 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.

In FreeBSD, the name of the program is set by the start-up code that is run before main(); thus, running setprogname() is not necessary. Programs that desire maximum portability should still call it; 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.

Examples

The following example presents a simple program, which shows the difference between getprogname() and argv[0].

#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

Releases

65 releases since FreeBSD 4.4-RELEASE; the text changed 35 times.

  1. 4.4
  2. 4.5–4.11
  3. 5.0–6.3
  4. 6.4
  5. 7.0
  6. 7.1
  7. 7.2
  8. 7.3
  9. 7.4
  10. 8.0
  11. 8.1
  12. 8.2
  13. 8.3
  14. 8.4
  15. 9.0
  16. 9.1
  17. 9.2
  18. 9.3
  19. 10.0
  20. 10.1
  21. 10.2
  22. 10.3
  23. 10.4
  24. 11.0
  25. 11.1
  26. 11.2
  27. 11.3
  28. 11.4
  29. 12.0
  30. 12.1
  31. 12.2
  32. 12.3
  33. 12.4
  34. 13.0
  35. 13.1–13.2
  36. 13.3–16.0

History

These functions first appeared in NetBSD 1.6, and made their way into FreeBSD 4.4.

From base 16.0-CURRENT-20261027-61c70dc16a79 in FreeBSD 16.0-CURRENT (2026-10-27). Parsed by carta from the page's mdoc source.