Solved

Getting Program Name

Posted on 2001-06-19
24
267 Views
Last Modified: 2010-05-18
Hi !

How can I get the programs name from within a program.

If my program is called hello.cgi how can I from within the same program get the program name of 'hello.cgi' into a variable ?


Marvin.
0
Comment
Question by:checkin
  • 8
  • 6
  • 3
  • +4
24 Comments
 
LVL 10

Expert Comment

by:makerp
ID: 6205519
int main(int argc, char *argv[])

declare main like this, then use argv[0], this has the program name, argv[1] to argv[n] has the programs command line arguments. to copy it into your own variable simply

#include <string.h>

char filename[255];
strcpy(filename,argv[0]);

the argv variables are valid throughout the life of the program so you dont really need to copy them.

the only probablem with argv[0] is it includes teh full path aswell so you may have to do a bit if string manipulation to get just the name
0
 
LVL 10

Expert Comment

by:makerp
ID: 6205554
here you go, enjoy

#include <stdio.h>
#include <string.h>

char *get_P_name(char *full_path)
{
     for(int i = strlen(full_path) ; i >= 0 ; i--)
     {
          if(full_path[i] == '\\')
          {
             return full_path + (i + 1);
          }
     }
     return NULL;
}


void main(int agvc, char *argv[])
{
     char filename[255];
     printf("argv[0] : %s\n",argv[0]);
     strcpy(filename,get_P_name(argv[0]));
     printf("filename : %s\n",filename);
}
0
 

Author Comment

by:checkin
ID: 6205570
ok

but if I am not in main and do not have access to argv[0], how can I get it then ?

What I am doing is, deep down in a logging library I need the program name, I do not have access to main() from this library.
0
 
LVL 10

Expert Comment

by:makerp
ID: 6205583
what platform
0
 

Author Comment

by:checkin
ID: 6205708
Sun Sparc running solaris
0
 
LVL 10

Expert Comment

by:makerp
ID: 6205723
i am lost then, sorry.
0
 

Author Comment

by:checkin
ID: 6205738
is that because there is no way or you do not develop on solaris ?
0
 
LVL 10

Expert Comment

by:makerp
ID: 6205761
i dont have a solaris box at home, i am on windoze at home and i am working from home at the moment.

is there no way you can recompile main. i would create a global variable in main then extern to it in your file. you can initalize it when main starts an then you know it will be safe when you ref it in your file
0
 

Author Comment

by:checkin
ID: 6205810
if I did that it would mean going through a lot of source files as we have over 100 main() programs each linking to the log function that I want to write.
0
 
LVL 10

Expert Comment

by:makerp
ID: 6205927
you need to find an API (thats what we call them on windows) on solaris the gets the name of the program. i dont even know of one on windows, but then ive never really looked
0
 

Author Comment

by:checkin
ID: 6205937
ok thanks for your help
0
 
LVL 10

Expert Comment

by:makerp
ID: 6206458
sorry i could not solve your problem, let me know what the solution is
0
Better Security Awareness With Threat Intelligence

See how one of the leading financial services organizations uses Recorded Future as part of a holistic threat intelligence program to promote security awareness and proactively and efficiently identify threats.

 
LVL 4

Accepted Solution

by:
newmang earned 100 total points
ID: 6208866
checkin

The way I do this on Solaris 2.7 (or Solaris 7 if you want to use the marketing guys terminology) running on SPARC is as follows...

1) Get the PID of the process pid_t pidnumber=getpid();
2) Open the file /proc/pidnumber as readonly
   fd=open("/proc/pidnumber",O_RDONLY)
3) Get the information normally seen in a ps display
   prpsinfo_t prpsinfo;
   ioctl(fd,PIOCPSINFO,&prpsinfo);
4) Extract the command line
   prpsinfo->pr_psargs
5) Extract from that the application name.
6) Close the file descriptor fd.

Cheers - Gavin

0
 

Expert Comment

by:MacTruck
ID: 6213000
I read all of the comments as to what you want to do, and I have an answer that doesn't involve anything terribly complicated:

Create two global variables called, say, GlobalArgc and GlobalArgv:

int GlobalArgc;
char **GlobalArgv;
char **GlobalEnvp;

int main(int argc, char **argv, char **envp)
{
  GlobalArgc = argc;
  GlobalArgv = argv;
  GlobalEnvp = envp;
}

Just thought I'd throw in that little bonus (it gets the environment that the program received at runtime).

Then, from anywhere in your program you can access the globals as long as you have nothing local conflicting with the global name!

MacTruck

0
 
LVL 4

Expert Comment

by:newmang
ID: 6213015
MacTruck

You really should put your suggestion up as a comment, not an answer, unless you are sure that it is the one true answer to the question.

The problem I see with your answer is that it imples that the poster needs to modify each of the myriad programs that may call his function, a task which he has said previously that he does not feel inclined to do. It also limits the portability of his code as would then require any new code that called his funtion to do likewise.

A better solution is one where the establishment of the program name is self-contained as per my comment earlier.

Cheers
0
 

Author Comment

by:checkin
ID: 6213996
Thanks Gavin !

That works fine.
0
 
LVL 10

Expert Comment

by:makerp
ID: 6214010
i already sugessted that !
0
 

Expert Comment

by:MacTruck
ID: 6216294
Oops...missed the comment on the extern'd globals.  Never mind about the "answer" I posted.  If you wanted to quickly make the change (which is the most portable solution I know of), you can do something like grep to find all your cpp files that use argv.  Then, for each of those files, have a program find ' main(' and replace all references within main to argv with GlobalArgv, argc with GlobalArgc, envp with GlobalEnvp.  Then, just re-build all the applications (shouldn't be that hard...search for all makefiles, switch directories, run make, back out...).  Sure it is a bit of work, but I could probably process all 100 cpp files in a single day using that method.

MacTruck

0
 
LVL 30

Expert Comment

by:Axter
ID: 6234057
Hi (MacTruck), welcome to EE.
All of the experts here, for the most part have learn from other experts as to the proper etiquette for posting answer.

An answer should not be posted as an answer, if other experts have previously posted possible answers as comments, and/or have already made contributions to the question.

There are many experts who never post answers as answer.  Instead, they post their answers as comments.
If you read the following link, you'll see why this is the preferred method for many of our valued experts, including myself.

http://www.experts-exchange.com/jsp/cmtyQuestAnswer.jsp


Hi (checkin):
Feel free to click the [Reject Answer] button near (Answer-poster's) response, even if it seems like a good answer.
Doing so will increase your chance of obtaining additional input from other experts.  Later, you can click the [Select Comment as Answer] button on any response.
0
 
LVL 30

Expert Comment

by:Axter
ID: 6234066
Hi (MacTruck), welcome to EE.
All of the experts here, for the most part have learn from other experts as to the proper etiquette for posting answer.

An answer should not be posted as an answer, if other experts have previously posted possible answers as comments, and/or have already made contributions to the question.

There are many experts who never post answers as answer.  Instead, they post their answers as comments.
If you read the following link, you'll see why this is the preferred method for many of our valued experts, including myself.

http://www.experts-exchange.com/jsp/cmtyQuestAnswer.jsp


Hi (checkin):
Feel free to click the [Reject Answer] button near (Answer-poster's) response, even if it seems like a good answer.
Doing so will increase your chance of obtaining additional input from other experts.  Later, you can click the [Select Comment as Answer] button on any response.
0
 
LVL 1

Expert Comment

by:Moondancer
ID: 6402285
This question was LOCKED with a PROPOSED ANSWER and awaits your decision today.  Once a question is LOCKED with a Proposed Answer, few new experts will step in to help on that question, since the assumption is, you've been helped.  If the Proposed Answer helped you, please accept it and award that expert.  If it did not help you, please reject it and add comments as to status and what else is needed.
 
If you wish to award multiple experts, just comment here with detail, I'll respond as soon as possible.  As it stands today, you asked the question, got help and not one expert was awarded for the contribution(s) made.  Your response is needed.  I'll monitor through month end, and if you've not returned to complete this, we'll need to decide.  Expert input is welcome (as always) to determine the outcome here if the Asker does not respond.
 
Your response in finalizing this (and ALL) your question(s) is appreciated.
 
Moondancer
Community Support Moderator @ Experts Exchange

0
 

Expert Comment

by:wenderson
ID: 6853161
Some compilers have __argc and __argv, that are global variables which are the same as main's parameters argc and argv.
So, if your compiler has those, you use __argv[0]

Wenderson
0
 
LVL 1

Expert Comment

by:Moondancer
ID: 6935020
ADMINISTRATION WILL BE CONTACTING YOU SHORTLY.  Moderators Computer101 or Netminder will return to finalize these if still open in seven days.  Please post closing recommendations before that time.

Question(s) below appears to have been abandoned. Your options are:
 
1. Accept a Comment As Answer (use the button next to the Expert's name).
2. Close the question if the information was not useful to you, but may help others. You must tell the participants why you wish to do this, and allow for Expert response.  This choice will include a refund to you, and will move this question to our PAQ (Previously Asked Question) database.  If you found information outside this question thread, please add it.
3. Ask Community Support to help split points between participating experts, or just comment here with details and we'll respond with the process.
4. Delete the question (if it has no potential value for others).
   --> Post comments for expert of your intention to delete and why
   --> YOU CANNOT DELETE A QUESTION with comments; special handling by a Moderator is required.

For special handling needs, please post a zero point question in the link below and include the URL (question QID/link) that it regards with details.
http://www.experts-exchange.com/jsp/qList.jsp?ta=commspt
 
Please click the Help Desk link on the left for Member Guidelines, Member Agreement and the Question/Answer process for further information, if needed.  http://www.experts-exchange.com/jsp/cmtyHelpDesk.jsp

Click you Member Profile to view your question history and keep them all current with updates as the collaboration effort continues, to track all your open and locked questions at this site.  If you are an EE KnowledgePro user, use the Power Search option to find them.  Anytime you have questions which are LOCKED with a Proposed Answer but does not serve your needs, please reject it and add comments as to why.  In addition, when you do grade the question, if the grade is less than an A, please add a comment as to why.  This helps all involved, as well as future persons who may access this item in the future to seek help.

To view your open questions, please click the following link(s) and keep them all current with updates.
http://www.experts-exchange.com/questions/Q.20107074.html
http://www.experts-exchange.com/questions/Q.20122982.html
http://www.experts-exchange.com/questions/Q.20133895.html
http://www.experts-exchange.com/questions/Q.20135988.html
http://www.experts-exchange.com/questions/Q.20138777.html
http://www.experts-exchange.com/questions/Q.20144361.html
http://www.experts-exchange.com/questions/Q.20193285.html
http://www.experts-exchange.com/questions/Q.20144706.html


To view your locked questions, please click the following link(s) and evaluate the proposed answer.
http://www.experts-exchange.com/questions/Q.20137752.html

**** PLEASE DO NOT AWARD THE POINTS TO ME. *****
 
------------>  EXPERTS:  Please leave your closing recommendations if this item remains inactive another seven (7) days.  If you are interested in the cleanup effort, please click this link http://www.experts-exchange.com/jsp/qManageQuestion.jsp?ta=commspt&qid=20274643
POINTS FOR EXPERTS awaiting comments are listed here -> http://www.experts-exchange.com/commspt/Q.20277028.html
 

Moderators will finalize this question if still open in 7 days, by either moving this to the PAQ (Previously Asked Questions) at zero points, deleting it or awarding expert(s) when recommendations are made, or an independent determination can be made.  Expert input is always appreciated to determine the fair outcome.
 
Thank you everyone.
 
Moondancer
Moderator @ Experts Exchange
0
 
LVL 1

Expert Comment

by:Moondancer
ID: 7116466
Zero response, finalized.
Moondancer - EE Moderator
0

Featured Post

Do You Know the 4 Main Threat Actor Types?

Do you know the main threat actor types? Most attackers fall into one of four categories, each with their own favored tactics, techniques, and procedures.

Join & Write a Comment

Have you thought about creating an iPhone application (app), but didn't even know where to get started? Here's how: ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ Important pre-programming comments: I’ve never tri…
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…
The goal of this video is to provide viewers with basic examples to understand and use structures in the C programming language.
The goal of this video is to provide viewers with basic examples to understand opening and writing to files in the C programming language.

746 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

Need Help in Real-Time?

Connect with top rated Experts

12 Experts available now in Live!

Get 1:1 Help Now