Get process name w/o using argv[0] in C function?

Jon Brawn jonb at specialix.co.uk
Fri Aug 3 00:35:51 AEST 1990


Someone writes:

>Also, I do not think I want to use a global and initialize it to
>argv[0] in body of main(), because I do not use globals!

Try this sort of thing:

#include <stdio.h>	/* for NULL and printf */

char *ProgName();

main( argc, argv )
char **argv;
{
	ProgName( argv[0] );

	/*
	** Code of your choice entered here
	*/

	Funky1();
}

Funky1()
{
	printf("My name is '%s'\n",ProgName(NULL));
}

/*
** ProgName - if passed a name will save a (private) handle to the name passed.
** Regardless, it will return a pointer to the current name stored.
*/
char *ProgName( pname )
char *pname;
{
	static char *myname = NULL;

	if ( pname )
		myname = pname;
	return myname;
}

No guarantees - no liability accepted. Have fun!
---
Jon Brawn, Specialix, 3 Wintersells Road, Byfleet, Surrey, KT14 7LF, UK.
Tel: +44(0)9323-54254,	Fax:+44(0)9323-52781,	jonb at specialix.co.uk
or: {backbone}!mcsun!ukc!slxsys!jonb
``Once upon a time, not so very long ago, in a land, not so very far away''
-- 
Jon Brawn, Specialix, 3 Wintersells Road, Byfleet, Surrey, KT14 7LF, UK.
Tel: +44(0)9323-54254,	Fax:+44(0)9323-52781,	jonb at specialix.co.uk
or: {backbone}!mcsun!ukc!slxsys!jonb



More information about the Comp.unix.questions mailing list