[cs631apue] getprogname and setprogname

Jan Schaumann jschauma at stevens.edu
Mon Sep 16 12:10:55 EDT 2013


Nick Smith <nsmith1 at stevens.edu> wrote:
> In the code style guide, there is a note to use getprogname(3) and  
> setprogname(3) to get/set the name of the executable for compatibility  
> on some systems. I've tried including this in my code and haven't been  
> able to include the definitions on my GNU/Linux system or on the 
> linux-lab.

Correct, Linux does not provide getprogname(3)/setprogname(3) as part of
the standard library:

$ cat moo.c
#include <bsd/stdlib.h>
#include <stdio.h>

int
main(int argc, char **argv) {
	setprogname("cowsays");
	if (argc != 2) {
		fprintf(stderr, "Usage: %s 'moo'\n", getprogname());
		exit(EXIT_FAILURE);
		/* NOTREACHED */
	}
	printf("%s %s\n", getprogname(), argv[1]);
	return EXIT_SUCCESS;
}
$ cc -Wall moo.c
/tmp/ccbIIvi9.o: In function `main':
moo.c:(.text+0x16): undefined reference to `setprogname'
moo.c:(.text+0x21): undefined reference to `getprogname'
moo.c:(.text+0x5a): undefined reference to `getprogname'
collect2: ld returned 1 exit status
$ 

Note that the error is not a syntax error at compile time, but a linking
error.  (We will go into the differences in more detail in a future
lecture.)


On Linux, these functions are provided by a different library, called
'libbsd':

$ cc -Wall moo.c -lbsd
$ ./a.out
Usage: cowsays 'moo'
$ ./a.out moo
cowsays moo
$ 


Note that the package providing the library and header files does not
install a manual page for getprogname(3)/setprogname(3):

$ dpkg -S /usr/include/bsd/stdlib.h           
libbsd-dev: /usr/include/bsd/stdlib.h
$ dpkg -L libbsd-dev 
[...]

This is rather unfortunate, since normally a manual page would include
the information of which library to link against.  See, for example,
ceil(3):

===

NAME
       ceil, ceilf, ceill - ceiling function: smallest integral value not less
       than argument

SYNOPSIS
       #include <math.h>

       double ceil(double x);
       float ceilf(float x);
       long double ceill(long double x);

       Link with -lm.


===


The quality of manual pages, documentation, and packages varies
significnatly across different versions of Unix (and even different
variations or distributions of Linux).

-Jan


More information about the cs631apue mailing list