UNIX Socket FAQ

A forum for questions and answers about network programming on Linux and all other Unix-like systems

You are not logged in.

  • Index
  • » C
  • » How to change the argv[0] value

#1 2010-11-12 01:32 AM

toni
Guest

How to change the argv[0] value

Hello,

i would like to know how i can change the argv[0] value in order to execute the  compiled c file with different name.

for instance,

if we have a test.c file and compile it            : gcc test.c -o test

so if we want to execute it, we write            : ./test

i would like  to change the execute name to   :  test

Without using the ./

i am trying this:

int main(int argc, char *argv[])
{
     argv[0] = "test" ;
     printf("%s",argv[0]);
     //code 
     //code
}

but it is not working, although the argv[0] is changed to test. if i try to execute the file i need to write again " ./test " and not " test "
=( 

Any help would be appreciated . Thanks !

#2 2010-11-12 03:00 PM

toni
Guest

Re: How to change the argv[0] value

i want to execute the file by using the command

test

and Not

./test

Is this possible ?

#3 2010-11-12 03:15 PM

i3839
Oddministrator
From: Amsterdam
Registered: 2003-06-07
Posts: 2,230

Re: How to change the argv[0] value

Just add the current working directory to your PATH environment variable. E.g. add

PATH="$PATH:./"

to your ~/.bashrc.

Offline

#4 2010-11-12 06:07 PM

RobSeace
Administrator
From: Boston, MA
Registered: 2002-06-12
Posts: 3,826
Website

Re: How to change the argv[0] value

Even that won't work, if the binary is truly called "test"...  Because, "test" is a shell built-in command, which will take precedence over a binary of that name, even if it's in your $PATH...  Name it something else...

I thought you were originally asking how to make the name of your app as seen in "ps" and such different from how you really called it...  That can be done a few ways: instead of replacing "argv[0]" with a new pointer, you need to actually overwrite the existing string it points at...  Alternatively, you may have a setproctitle() function that does this...  Or, the most portable method is to simply exec*() and supply your own arbitrary argv[0] which will be used (and which is separate from the actual command to execute)...  (Just throwing this out there for anyone else who stumbles across this question, looking for a way to do this...)

Offline

  • Index
  • » C
  • » How to change the argv[0] value

Board footer

Powered by FluxBB