Reputation: 1451
In Unix I know that there are functions setprogname
and getprogname
. Both are for setting and getting the program name respectively. They are found in the library stdilib.c
. I was wondering if Linux has these functions built in as well because I cannot get them to run on the Linux machine (Ubuntu 10.04). Are these functions available? The code is below in case the man pages I found online didn't tell the whole story and I didn't add something that I needed.
Thanks!
#define _XOPEN_SOURCE 500
#include<sys/stat.h>
#include<sys/types.h>
#include<errno.h>
#include<fcntl.h>
#include<stdio.h>
#include<stdlib.h>
#include<string.h>
#include<unistd.h>
int main (int argc, char *argv[])
{
setprogname(argv[0]);
Upvotes: 3
Views: 5178
Reputation: 584
Tried this on ubuntu 12.04 and it works:
Install the libbsd-dev
package. Then try compiling the following code:
//filename=somec.c
#include <stdio.h>
#include <bsd/stdlib.h>
int main(int argc, char *argv[]){
if(argc>1)
setprogname((char*)argv[1]);
printf("Program name is: %s\n",getprogname());
return 0;
}
Using this set of arguments:
gcc somec.c -o somec -lbsd
This works for me.
Upvotes: 3
Reputation: 2585
You can use libbsd on GNU/Linux to get access to these and other useful utility functions from *BSD operating systems.
Upvotes: 1
Reputation: 1316
From the *BSD man page about {set,get}progname
:
The getprogname() and setprogname() functions manipulate the name of the current program. They are used by error-reporting routines to produce consistent output.
If you want to customize the program name for error reporting with error
and error_at_line
you can declare and set error_print_progname
variable as described in the glibc manual :
As mentioned above the error and error_at_line functions can be customized by defining a variable named error_print_progname.
— Variable: void (*) error_print_progname (void) If the error_print_progname variable is defined to a non-zero value the function pointed to is called by error or error_at_line. It is expected to print the program name or do something similarly useful.
The function is expected to be print to the stderr stream and must be able to handle whatever orientation the stream has.
The variable is global and shared by all threads.
Edit: I just check the gnulib manual about {set,get}progname
and error_print_progname
:
This variable is missing on all non-glibc platforms: MacOS X 10.5, FreeBSD 6.0, NetBSD 5.0, OpenBSD 3.8, Minix 3.1.8, AIX 5.1, HP-UX 11, IRIX 6.5, OSF/1 5.1, Solaris 11 2011-11, Cygwin, mingw, MSVC 9, Interix 3.5, BeOS.
Upvotes: 4