Velocity Reviews - Computer Hardware Reviews

Velocity Reviews > Newsgroups > Programming > C Programming > Re: Exercise 7-1

Reply
Thread Tools

Re: Exercise 7-1

 
 
Andrew Poelstra
Guest
Posts: n/a
 
      09-04-2008
On Wed, 2008-09-03 at 17:00 -0700, Keith Thompson wrote:
> mdh <(E-Mail Removed)> writes:
> [big snip]
>
> > 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 )
> >
> >>>>>>>>>>

> >
> > #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.


 
Reply With Quote
 
 
 
 
Keith Thompson
Guest
Posts: n/a
 
      09-04-2008
Andrew Poelstra <(E-Mail Removed)> writes:
> On Wed, 2008-09-03 at 17:00 -0700, Keith Thompson wrote:

[...]
>> 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) http://www.velocityreviews.com/forums/(E-Mail Removed) <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"
 
Reply With Quote
 
 
 
Reply

Thread Tools

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are Off


Similar Threads
Thread Thread Starter Forum Replies Last Post
tree functions daily exercise: Range Xah Lee Java 12 06-22-2005 08:51 AM
Cisco Student VPN exercise problem : gen_unrfrag: fail to generate unreachable, unexpected args robert Cisco 0 06-02-2004 07:33 PM
2154 module 4 Exercise 2 Drew Brown MCSE 0 10-22-2003 02:47 AM
Exercise needed for java 2 programmer test lonelyplanet999 Java 1 09-30-2003 10:37 AM
Re: Development best practices and knowing when to exercise control over development Kevin Spencer ASP .Net 2 08-06-2003 09:33 PM



Advertisments