Connecting Tech Pros Worldwide Forums | Help | Site Map

Re: Exercise 7-1

Andrew Poelstra
Guest
 
Posts: n/a
#1: Sep 4 '08
On Wed, 2008-09-03 at 17:00 -0700, Keith Thompson wrote:
Quote:
mdh <mdeh@comcast.netwrites:
[big snip]
>
Quote:
Your point is taken. Let me then accept your code, but having done
this, does it answer the query that I am puzzled about?
And for completeness, I include, the **hopefully** full code from T&G
as well. ( I have added braces as per your suggestion )
Quote:
>>>>>>>>>
#include <stdio.h>
#include <ctype.h>
#include <string.h>

int main (int argc, char * argv[]) {
int c;
if (strcmp(argv[0], "upper")==0){
while ( (c = getchar() ) != EOF)
putchar(toupper(c));
}
else {
while (( c = getchar() ) != EOF)
putchar(tolower(c));
}
return 0;
}

<<<<<<<<
>
Ok, that looks reasonable, mostly. Your indentation is still
inconsistent, and I'd add braces to the while statements as well as the
if statements:
>
#include <stdio.h>
#include <ctype.h>
#include <string.h>
>
int main(int argc, char *argv[])
{
int c;
if (strcmp(argv[0], "upper") == 0) {
while ((c = getchar()) != EOF) {
putchar(toupper(c));
}
}
else {
while ((c = getchar()) != EOF) {
putchar(tolower(c));
}
}
return 0;
}
>
But now I've lost track of what you were asking about.
>
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.



Keith Thompson
Guest
 
Posts: n/a
#2: Sep 4 '08

re: Re: Exercise 7-1


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"
Closed Thread