?
Solved

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

Posted on 2003-03-12
4
Medium Priority
?
182 Views
Last Modified: 2010-08-05
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!
0
Comment
Question by:misterM
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 3
4 Comments
 
LVL 8

Accepted Solution

by:
akshayxx earned 80 total points
ID: 8124917
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
 
LVL 8

Expert Comment

by:akshayxx
ID: 8124926
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
 

Author Comment

by:misterM
ID: 8124949
Works like a charm. I was unaware of the strrchr function. Thanx!
0
 
LVL 8

Expert Comment

by:akshayxx
ID: 8125554
welcome .. good luck with rest
0

Featured Post

Industry Leaders: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Preface I don't like visual development tools that are supposed to write a program for me. Even if it is Xcode and I can use Interface Builder. Yes, it is a perfect tool and has helped me a lot, mainly, in the beginning, when my programs were small…
Windows programmers of the C/C++ variety, how many of you realise that since Window 9x Microsoft has been lying to you about what constitutes Unicode (http://en.wikipedia.org/wiki/Unicode)? They will have you believe that Unicode requires you to use…
The goal of this video is to provide viewers with basic examples to understand how to use strings and some functions related to them in the C programming language.
The goal of this video is to provide viewers with basic examples to understand and use switch statements in the C programming language.
Suggested Courses
Course of the Month10 days, 2 hours left to enroll

762 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question