0

I am looking for a way to find the name of the executable file which runs it. meaning that if I have a file called program which runs something I would like to get its name.

Using __FILE__ does not suite me since I need the executable name not the C files name which contains the code.

I am wondering if there is a gcc macro which can help me or a built in function which I couldn't find.

EDIT: Also using argv[0] is not appropriate since I need to call the function not only inside main.

And passing the argv[0] parameter to all the functions that might need this parameter also isnt acceptable since it will be used in the entire system (I need to hash by it).

9
  • @zenith thnx. I was just adding it as a comment specifying it does not meet my needs. Commented Feb 8, 2015 at 13:53
  • Do you need this to work on Windows, or are POSIX systems sufficient? Commented Feb 8, 2015 at 13:55
  • I am running it on linux Commented Feb 8, 2015 at 13:55
  • 4
    But can't you pass argv[0] to whatever part of your code that needs it? Or just set argv[0] to a global variable or something if it's too much passing around? Commented Feb 8, 2015 at 13:57
  • 2
    Macros are expanded at compile time, so if someone goes and changes the name of the executable after compilation, macros are out of question. Commented Feb 8, 2015 at 14:08

5 Answers 5

3
int main(int argc,char* argv[])
{
    printf("%s",argv[0]);
    return 0;
}

The first argument(argv[0]) contains the name of the program which is being run.

Sign up to request clarification or add additional context in comments.

5 Comments

Hi and thanks, I am sorry but using argv[0] doesn't suite my needs, I was just adding it with edit when you answered.
@Anton.P copy argv[0]'s content and use it as you wish?
@Anton.P Please mark it as an edit with the argv[0], otherwise people which comes here think that Cool Guy just completely misunderstood the question
@iharob thats a possibility but I dont want to pass the name as a parameter to every function that might use this feature. unfortunately that's too much functions.
@Anton.P Then use a global variable, but I would pass the variable.
2

The standard definition for main() in C is:

int main(int argc, char **argv)

argv[0] contains the name of the program as executed.

4 Comments

Hi and thanks, I am sorry but using argv[0] doesn't suite my needs, I was just adding it with edit when you answered.
OP has mentioned against argv[0].
That's a really weird requirement. Is this code golf?
Anton, you can define global pointer, like char * exeName; and in the main function assigne it like exeName = argv [0];. So, this global string will be available for any function in your code.
2

From main, pass argv[0] to wherever you might need the program's name.

Or if a lot of functions need it and you want to avoid passing it around too much, assign it to a global variable.

You asked for macros as a solution, but they are expanded at compile time. And since the name of the executable file can be changed after compilation, what you want cannot be determined at compile time, so macros are out of question.

3 Comments

There program is quite large and contains dozens of such modules, the feature I am adding is a small one and I dont want to make changes in all the modules just to add a tiny feature.
@Anton.P The only solution I can see is to add two lines to define a global variable and in main assign argv[0] to that. Would two small lines be too much modification?
That doesn't seem to me like you have to make changes to all modules.
1

Often remembering argv[0] from main will be quite sufficient.

If this is not the case -- for example if you're a shared library, or if you worry that your caller started you with a faked argv[0] (this is possible but unusual), you can, on POSIX-compliant systems with a mounted /proc, use readlink to resolve /proc/self/exe and get a path to the main binary of the running process:

#include <limits.h>
#include <unistd.h>

// Note that readlink does not null-terminate the result itself. This
// is important if the link target contains null bytes itself, I suppose,
// but it means that you have to take care that the null-terminator is
// placed yourself if you're going to use the file name as a string.
char buf[PATH_MAX] = { 0 };
readlink("/proc/self/exe", buf, PATH_MAX);

And on Windows, there is the GetModuleFileName function:

#include <windows.h>

TCHAR buf[MAX_PATH];
GetModuleFileName(NULL, buf, MAX_PATH);

Comments

0

The startup name is given via main()'s argv[0] and can be considered constant.

So why not make it available through out the program using a global reference to it.

progname.h

#ifndef PROGNAME_H
#define PROGNAME_H

extern char * progname;

#endif

main.c

#include "progname.h"

char * progname = NULL;

int main(int argc, char ** argv)
{
  progname = argv[0];

  ...

}

Access it from any where like so

module1.c

#include <stdio.h>
#include "progname.h"

void f(void)
{
  printf("progname='%s'\n", progname ?progname :"<not set>");
}

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.