Improve company productivity with a Business Account.Sign Up

x
  • Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 312
  • Last Modified:

Getting Program Name

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
checkin
Asked:
checkin
  • 8
  • 6
  • 3
  • +4
1 Solution
 
makerpCommented:
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
 
makerpCommented:
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
 
checkinAuthor Commented:
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
What Kind of Coding Program is Right for You?

There are many ways to learn to code these days. From coding bootcamps like Flatiron School to online courses to totally free beginner resources. The best way to learn to code depends on many factors, but the most important one is you. See what course is best for you.

 
makerpCommented:
what platform
0
 
checkinAuthor Commented:
Sun Sparc running solaris
0
 
makerpCommented:
i am lost then, sorry.
0
 
checkinAuthor Commented:
is that because there is no way or you do not develop on solaris ?
0
 
makerpCommented:
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
 
checkinAuthor Commented:
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
 
makerpCommented:
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
 
checkinAuthor Commented:
ok thanks for your help
0
 
makerpCommented:
sorry i could not solve your problem, let me know what the solution is
0
 
newmangCommented:
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
 
MacTruckCommented:
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
 
newmangCommented:
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
 
checkinAuthor Commented:
Thanks Gavin !

That works fine.
0
 
makerpCommented:
i already sugessted that !
0
 
MacTruckCommented:
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
 
AxterCommented:
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
 
AxterCommented:
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
 
MoondancerCommented:
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
 
wendersonCommented:
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
 
MoondancerCommented:
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
 
MoondancerCommented:
Zero response, finalized.
Moondancer - EE Moderator
0
Question has a verified solution.

Are you are experiencing a similar issue? Get a personalized answer when you ask a related question.

Have a better answer? Share it in a comment.

Join & Write a Comment

Featured Post

What Kind of Coding Program is Right for You?

There are many ways to learn to code these days. From coding bootcamps like Flatiron School to online courses to totally free beginner resources. The best way to learn to code depends on many factors, but the most important one is you. See what course is best for you.

  • 8
  • 6
  • 3
  • +4
Tackle projects and never again get stuck behind a technical roadblock.
Join Now