2

Exercise 7-1. Write a program that converts upper case to lower or lower case to upper, depending on the name it is invoked with, as found in argv[0].

For those of you interested in writing the program, you can find sample solutions here: http://users.powernet.co.uk/eton/kandr2/krx701.html

My question, however, is exactly how do you invoke the program? argv[0] refers to the name of the program, but along with the program's filepath in addition. When I do a printf() on argv[0], it outputs the full path from /home if I run the program via my code::blocks IDE. If I try to execute the program via the command line with './upper' (without quotes), argv[0] includes the './' before the program name.

Is there anyway to invoke this program WITHOUT adding the program path to the PATH environment variable?

AK-33
  • 167
  • 2
  • 10
  • Are you doing this as a homework assignment? What is your target platform (windows, linux, etc)? – M.Babcock Dec 18 '11 at 05:40
  • Not a homework assignment - I only code as a hobby now. My platform is an Arch Linux virtual machine. Coding in Windows (without a *nix emulator like Cygwin) causes too many problems. – AK-33 Dec 18 '11 at 05:51
  • Understood, it's just that the Microsoft APIs allow you to determine the working directory which simplifies your question. There is probably something similar in the *nix world that I haven't had to encounter yet. – M.Babcock Dec 18 '11 at 05:54

5 Answers5

3

Can you just scan the string for the last / character and use all the characters from that point on?

Gravity
  • 2,696
  • 1
  • 20
  • 29
2

Normally, argv[0] reflects exactly what you type into the command prompt to execute the program. If your program appears in a directory included in the PATH environment variable, then you won't have to specify the directory name with ./ or a full path.

However, even if your program does reside in a PATH directory, the user may still type a full path to the executable. Your program should be prepared to deal with this.

See your shell documentation for more information about the PATH environment variable and how to modify it.

Greg Hewgill
  • 951,095
  • 183
  • 1,149
  • 1,285
2

You can compile it once, whatever the output filename (e.g.: my_c_program) and with bash, use:

$ exec -a upper ./my_c_program
foo
FOO

$ exec -a lower ./my_c_program
BAR
bar

If you want it to run in a separate shell:

$ bash -c "exec -a upper ./my_c_program"

Basically, the syntax is exec -a <newname> <command>, where <newname> will be argv[0].

netcoder
  • 66,435
  • 19
  • 125
  • 142
2

Take a look at this answer. It discusses the use of a standard Posix method for determining the current working directory for your application which is what you need in order to resolve the location of your executable. Basically determine if the path is an absolute path and if it is then execute as it is, if it isn't then (after checking to see that the path terminator is on the arg[0]) append arg[0] to the current working directory and the OS should figure out the rest (I believe; it has been a few years since I've done any serious *nix dev).

Community
  • 1
  • 1
M.Babcock
  • 18,753
  • 6
  • 54
  • 84
1

You could write a little wrapper around execv(3) that does the exec for you, with arguments set just right:

char *args = {"Uppercase", NULL};

execve("/path/to/your/Uppercase", args);

Or, you could make your program more flexible in how it parses argv[0]; see strrchr(3) for an easy way to get to your program's name if a directory portion is given.

sarnold
  • 102,305
  • 22
  • 181
  • 238