Which is the easiest way to get the root program name from argv?

With the following defined which is the easiest way to get the root of the program name?

char *progname;
progname = argv[0];

If executed as './foo' or '/home/moo/foo', this would result in progname being set to "./foo" or "/home/moo/foo". However, I would like progname being set to only "foo".

Would strtok be the easiest or is there something else which can be used?
Thanx!
misterMAsked:
Who is Participating?
 
akshayxxConnect With a Mentor Commented:
here is what i do in my program..
//modifying argv[0] to have the name of the executable only.. removing any path characters.
if(strrchr(argv[0],'/')!=NULL)argv[0]=strrchr(argv[0],'/')+1;

after this small modification u can simple copy the argv[0] in ur variable of choice .. or can simple point at it..
0
 
akshayxxCommented:
hope u got what i wrote there..
simple find the last '/' character and make argv[0] to point to the very next character ..
no tokenisation stuff needed
0
 
misterMAuthor Commented:
Works like a charm. I was unaware of the strrchr function. Thanx!
0
 
akshayxxCommented:
welcome .. good luck with rest
0
All Courses

From novice to tech pro — start learning today.