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?
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.

after this small modification u can simple copy the argv[0] in ur variable of choice .. or can simple point at it..
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
misterMAuthor Commented:
Works like a charm. I was unaware of the strrchr function. Thanx!
welcome .. good luck with rest
All Courses

From novice to tech pro — start learning today.