Andrew Poelstra <apoelstra@wpsoftware.netwrites:
Quote:
On Wed, 2008-09-03 at 17:00 -0700, Keith Thompson wrote:
[...]
Quote:
Quote:
>One possible issue I can see is that argv[0], if it's non-null, points
>to a string that "represents the program name". The manner in which
>it does so may vary. For example, on a Unix-like system, it could be
>any of "./upper", "/some/long/path/upper", or just "upper", depending
>on how you invoked the program. I suggest printing the value of
>argv[0] to see how it's actually being set. <OT>Try both installing
>the program in some directory in your $PATH and invoking it by name,
>and typing the full path to the executable file, and see what
>happens.</OT>
>>
>For a simple test, you can just pick a way to invoke the program and
>check for whatever value of argv[0] that gives you. For more
>generality, you could figure out how to detect all the possible ways
>it could be invoked.
>
Probably he could use strstr() to check for "upper" or "lower" in
the program name that way, which would be reasonably portable.
I've seen that approach fail badly when the full path to the command
happens to contain a directory whose name contains the specified
pattern.
For example, suppose you install the command "upper" in the directory
"/home/username/slower_than_molasses/bin/upper".
You can check whether the argv[0] string *ends* in either "upper" or
"lower".
<OT>
For a Unix-like system, you probably want to check whether the argv[0]
string either ends in "/upper" or "/lower", or is exactly "upper" or
"lower" with no '/' characters at all. For other systems, there could
be other criteria; for example, you might want to accept "UPPER", or
"UPPER.EXE", or the directory delimiter might be something other than
'/'.
</OT>
--
Keith Thompson (The_Other_Keith)
kst-u@mib.org <http://www.ghoti.net/~kst>
Nokia
"We must do something. This is something. Therefore, we must do this."
-- Antony Jay and Jonathan Lynn, "Yes Minister"