You are not logged in.
Pages: 1
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 !
i want to execute the file by using the command
test
and Not
./test
Is this possible ?
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
Pages: 1