Re: Exercise 7-1

Discussion in 'C Programming' started by Andrew Poelstra, Sep 4, 2008.

  1. On Wed, 2008-09-03 at 17:00 -0700, Keith Thompson wrote:
    > mdh <> 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.
    Andrew Poelstra, Sep 4, 2008
    #1
    1. Advertising

  2. Andrew Poelstra <> 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.ghoti.net/~kst>
    Nokia
    "We must do something. This is something. Therefore, we must do this."
    -- Antony Jay and Jonathan Lynn, "Yes Minister"
    Keith Thompson, Sep 4, 2008
    #2
    1. Advertising

Want to reply to this thread or ask your own question?

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. Kevin Spencer
    Replies:
    2
    Views:
    437
    John Saunders
    Aug 6, 2003
  2. lonelyplanet999

    Exercise needed for java 2 programmer test

    lonelyplanet999, Sep 30, 2003, in forum: Java
    Replies:
    1
    Views:
    4,181
    VisionSet
    Sep 30, 2003
  3. Xah Lee
    Replies:
    12
    Views:
    607
    Duncan Booth
    Jun 22, 2005
  4. Bruce .J Sam
    Replies:
    0
    Views:
    1,945
    Bruce .J Sam
    Jun 16, 2005
  5. Aries
    Replies:
    7
    Views:
    403
    Aries
    May 3, 2006
Loading...

Share This Page