code question

Collapse
This topic is closed.
X
X
 
  • Time
  • Show
Clear All
new posts
  • Keith Thompson

    #61
    Re: code question

    Richard Heathfield <rjh@see.sig.in validwrites:
    [...]
    argv[0] is guaranteed *either* to represent the program name or to be NULL
    (and argc to be 0), and the scanning sequence works fine whichever of
    these is the case.
    [...]

    argv[0] is also allowed to point to an empty string if the program
    name is not available. The standard's exact wording (C99 5.1.2.2.1p2)
    is:

    If the value of argc is greater than zero, the string pointed to
    by argv[0] represents the _program name_; argv[0][0] shall be the
    null character if the program name is not available from the host
    environment.

    But the requirement that the string pointed to by argv[0][0]
    "represents the program name" is largely unenforced and unenforceable.
    Under POSIX, for example, the string is whatever the invoking program
    wants it to be. Though I suppose you could define "program name" in
    such a way as to make this consistent. In fact, since "program name"
    is in italics in the quoted paragraph, that supposedly *is* the
    definition of the phrase -- though I'm not convinced it's really a
    definition at all. It would make as much sense, and be more
    consistent, for the standard to say that the string *is* the program
    name.

    --
    Keith Thompson (The_Other_Keit h) 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"

    Comment

    • CBFalconer

      #62
      Re: code question

      Barry Schwarz wrote:
      CBFalconer <cbfalconer@yah oo.comwrote:
      >
      .... snip ...
      >
      >My point, which I haven't bothered to check, is that on some
      >systems argv[0] doesn't point to an identifier of the program. If
      >that is the case, is argv[0] value NULL, or does it point to an
      >empty string? If it is a NULL, the scanning sequence won't work.
      >
      Since 5.1.2.2.1 guarantees argc >= 0 and argv[argc] == NULL, the
      situation you are concerned about cannot occur.
      I wasn't talking about argv[argc]. I mentioned argv[0].
      Apparently there is no worry.

      --
      [mail]: Chuck F (cbfalconer at maineline dot net)
      [page]: <http://cbfalconer.home .att.net>
      Try the download section.

      Comment

      • CBFalconer

        #63
        Re: code question

        Richard Heathfield wrote:
        CBFalconer said:
        >
        .... snip ...
        >
        >My point, which I haven't bothered to check, is that on some
        >systems argv[0] doesn't point to an identifier of the program. If
        >that is the case, is argv[0] value NULL, or does it point to an
        >empty string? If it is a NULL, the scanning sequence won't work.
        >
        argv[0] is guaranteed *either* to represent the program name or to
        be NULL (and argc to be 0), and the scanning sequence works fine
        whichever of these is the case.
        >
        In future, please bother to check.
        Don't be so silly. I'm not worried about using it. I simply
        raised something for others, who might be worried, to check.

        --
        [mail]: Chuck F (cbfalconer at maineline dot net)
        [page]: <http://cbfalconer.home .att.net>
        Try the download section.

        Comment

        • CBFalconer

          #64
          Re: code question

          blargg wrote:
          Richard Heathfield <rjh@see.sig.in validwrote:
          >Default User said:
          [...]
          >>Replying to Bill is a waste of time one way or the other.
          >>
          >I agree that Bill Cunningham doesn't appear to gain any
          >significant benefit from the responses he gets. Whether this is
          >through malice or incompetence is really beside the point, and
          >Hanlon's Razor applies.
          >>
          >But his questions do sometimes provoke discussions that are
          >likely to be of moderate interest to /other/ learners. If you
          >think of a reply to one of Bill Cunningham's articles not as a
          >reply to /him/, but as a reply to the points he has made, for
          >general consumption, then it may perhaps seem slightly less of
          >a Sisyphean task.
          >
          Good point, but following it would mean answering his questions
          and not replying further when he ignores the answer or mentions
          his copy of "kandr2" from an alternate universe.
          Do whatever you wish. However my judgement is that there is
          neither malice nor incompetence involved, the man has a mental
          disability. In fact, I admire his persistence, and have noted
          slight progress over the years. One advantage of Usenet is that
          you are quite free to ignore him.

          --
          [mail]: Chuck F (cbfalconer at maineline dot net)
          [page]: <http://cbfalconer.home .att.net>
          Try the download section.

          Comment

          • Richard Heathfield

            #65
            Re: code question

            CBFalconer said:
            Richard Heathfield wrote:
            >CBFalconer said:
            >>
            ... snip ...
            >>
            >>My point, which I haven't bothered to check, is that on some
            >>systems argv[0] doesn't point to an identifier of the program. If
            >>that is the case, is argv[0] value NULL, or does it point to an
            >>empty string? If it is a NULL, the scanning sequence won't work.
            >>
            >argv[0] is guaranteed *either* to represent the program name or to
            >be NULL (and argc to be 0), and the scanning sequence works fine
            >whichever of these is the case.
            >>
            >In future, please bother to check.
            >
            Don't be so silly. I'm not worried about using it. I simply
            raised something for others, who might be worried, to check.
            Don't be so silly. The only person worried about it was you. Nobody else
            even suggested that the "scanning sequence won't work" if argv[0] is NULL,
            for the excellent reason that it works just fine.

            --
            Richard Heathfield <http://www.cpax.org.uk >
            Email: -http://www. +rjh@
            Google users: <http://www.cpax.org.uk/prg/writings/googly.php>
            "Usenet is a strange place" - dmr 29 July 1999

            Comment

            • Richard Heathfield

              #66
              Re: code question

              Keith Thompson said:
              Richard Heathfield <rjh@see.sig.in validwrites:
              [...]
              >argv[0] is guaranteed *either* to represent the program name or to be
              >NULL (and argc to be 0), and the scanning sequence works fine whichever
              >of these is the case.
              [...]
              >
              argv[0] is also allowed to point to an empty string if the program
              name is not available.
              In which case the "scanning sequence" still works just fine.
              But the requirement that the string pointed to by argv[0][0]
              ITYM argv[0].

              <snip>

              --
              Richard Heathfield <http://www.cpax.org.uk >
              Email: -http://www. +rjh@
              Google users: <http://www.cpax.org.uk/prg/writings/googly.php>
              "Usenet is a strange place" - dmr 29 July 1999

              Comment

              • Keith Thompson

                #67
                Re: code question

                Richard Heathfield <rjh@see.sig.in validwrites:
                Keith Thompson said:
                >Richard Heathfield <rjh@see.sig.in validwrites:
                >[...]
                >>argv[0] is guaranteed *either* to represent the program name or to be
                >>NULL (and argc to be 0), and the scanning sequence works fine whichever
                >>of these is the case.
                >[...]
                >>
                >argv[0] is also allowed to point to an empty string if the program
                >name is not available.
                >
                In which case the "scanning sequence" still works just fine.
                Yes.
                >But the requirement that the string pointed to by argv[0][0]
                >
                ITYM argv[0].
                D'oh!

                --
                Keith Thompson (The_Other_Keit h) 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"

                Comment

                • Richard

                  #68
                  Re: code question

                  "Default User" <defaultuserbr@ yahoo.comwrites :
                  Richard Heathfield wrote:
                  >
                  >Default User said:
                  >
                  >
                  Replying to Bill is a waste of time one way or the other.
                  >>
                  >I agree that Bill Cunningham doesn't appear to gain any significant
                  >benefit from the responses he gets. Whether this is through malice or
                  >incompetence is really beside the point, and Hanlon's Razor applies.
                  >>
                  >But his questions do sometimes provoke discussions that are likely to
                  >be of moderate interest to other learners. If you think of a reply to
                  >one of Bill Cunningham's articles not as a reply to him, but as a
                  >reply to the points he has made, for general consumption, then it may
                  >perhaps seem slightly less of a Sisyphean task.
                  >
                  I'll freely admit that I still read through the threads, even though I
                  have Bill killfiled. Indeed, there's sometimes a useful nugget. Each
                  will have to make a decision on how to approach it.
                  That's nice of you Brian. I'm sure we all appreciate the "go ahead" from
                  such a c.l.c luminary as yourself.
                  Brian
                  You need to try and get a decent news reader. If you killfile Bill
                  surely you have no desire to read his questions or the replies? Or are
                  you hunting for "off topicality"?

                  Comment

                  • lovecreatesbea...@gmail.com

                    #69
                    Re: code question

                    On Sep 20, 5:03 am, Richard Heathfield <r...@see.sig.i nvalidwrote:
                    Bill Cunningham said:
                    >
                        I have this code I would like to clean up.
                    >
                    #include <stdio.h>
                    #include <stdlib.h>
                    >
                    int main(int argc, char *argv[])
                    {
                        double x, y, a, b;
                        FILE *fp;
                        x = strtod(argv[1], NULL);
                        y = strtod(argv[2], NULL);
                        a = strtod(argv[3], NULL);
                        b = strtod(argv[4], NULL);
                        if ((fp = fopen(argv[4], "a")) == NULL) {
                            puts("fopen error");
                            exit(-1);
                        }
                        fprintf(fp, "%.2f\t%.2f\t%. 2f\t%.2f\n", x, y, a, b);
                        if (fclose(fp) == EOF) {
                            puts("fclose error");
                            exit(-1);
                        }
                        return 0;
                    }
                    >
                    If the program is run with no arguments I get a seg fault. If it is run
                    with 4, no problem. If it is run with less than four (that includes
                    argv[0]) then the program doesn't want to run right. How would I be able
                    to use this program with say one or two argvs ?
                    >
                    #include <stdio.h>
                    #include <stdlib.h>
                    >
                    #define DEFAULT_FILE_NA ME "foo.bar"
                    >
                    int main(int argc, char *argv[])
                    {
                        double x = 0.0, y = 0.0, a = 0.0, b = 0.0;
                        const char *filename = DEFAULT_FILE_NA ME;
                    >
                        FILE *fp = NULL;
                        if(argc 1)
                        {
                          x = strtod(argv[1], NULL);
                        }
                        if(argc 2)
                        {
                          y = strtod(argv[2], NULL);
                        }
                        if(argc 3)
                        {
                          a = strtod(argv[3], NULL);
                        }
                        if(argc 4)
                        {
                          b = strtod(argv[4], NULL);
                          filename = argv[4];
                        }
                        if ((fp = fopen(filename, "a")) == NULL) {
                            puts("fopen error");
                            exit(EXIT_FAILU RE);
                        }
                        fprintf(fp, "%.2f\t%.2f\t%. 2f\t%.2f\n", x, y, a, b);
                        if (fclose(fp) == EOF) {
                            puts("fclose error");
                            exit(EXIT_FAILU RE);
                        }
                        return 0;
                    >
                    }
                    The error check upon strtod is missed four times freely.

                    Comment

                    • Richard Heathfield

                      #70
                      Re: code question

                      lovecreatesbea. ..@gmail.com said:

                      <snip>
                      The error check upon strtod is missed four times freely.
                      Then insert it, dear lovecreatesbeau ty@gmail.com, dear
                      lovecreatesbeau ty@gmail.com, dear lovecreatesbeau ty@gmail.com
                      Then insert it, dear lovecreatesbeau ty@gmail.com, dear
                      lovecreatesbeau ty@gmail.com, insert it.

                      --
                      Richard Heathfield <http://www.cpax.org.uk >
                      Email: -http://www. +rjh@
                      Google users: <http://www.cpax.org.uk/prg/writings/googly.php>
                      "Usenet is a strange place" - dmr 29 July 1999

                      Comment

                      • lovecreatesbea...@gmail.com

                        #71
                        Re: code question

                        On Sep 22, 3:56 pm, Richard Heathfield <r...@see.sig.i nvalidwrote:
                        lovecreatesbea. ..@gmail.com said:
                        >
                        <snip>
                        >
                        The error check upon strtod is missed four times freely.
                        >
                        Then insert it
                        Yeah, how about this:

                        $ cat a.c
                        #include <stdlib.h>
                        #include <stdio.h>
                        #include <errno.h>

                        int main (int argc, char *argv[])
                        {
                        double d;
                        char *end;

                        for (; *++argv; ){
                        errno = 0;
                        d = strtod(*argv, &end);
                        if (errno){
                        perror(*argv);
                        continue;
                        }
                        if (d == 0 && *argv == end){
                        fprintf(stderr, "%s: Cant be converted\n",
                        *argv);
                        continue;
                        }
                        fprintf(stdout, "%f\n", d);
                        }
                        return EXIT_SUCCESS;
                        }
                        $
                        $ make && ./a.out 11
                        gcc -Wall -W -g -pedantic -ansi -c -o a.o a.c
                        a.c:5: warning: unused parameter ‘argc’
                        gcc a.o -o a.out
                        11.000000
                        $ make && ./a.out aa
                        make: `a.out' is up to date.
                        aa: Cant be converted
                        $ make && ./a.out
                        999999999999999 999999999999999 999999999999999 999999999999999 999999999999999 999999999999999 999999999999999 999999999999999 999999999999999 999999999999999 999999999999999 999999999999999 999999999999999 999999999999999 999999999999999 999999999999999 999999999999999 999999999999999 999999999999999 999999999999999 999999999999999 9999999999999.9 9
                        make: `a.out' is up to date.
                        999999999999999 999999999999999 999999999999999 999999999999999 999999999999999 999999999999999 999999999999999 999999999999999 999999999999999 999999999999999 999999999999999 999999999999999 999999999999999 999999999999999 999999999999999 999999999999999 999999999999999 999999999999999 999999999999999 999999999999999 999999999999999 9999999999999.9 9:
                        Numerical result out of range
                        $

                        Comment

                        • Richard Heathfield

                          #72
                          Re: code question

                          lovecreatesbea. ..@gmail.com said:
                          On Sep 22, 3:56 pm, Richard Heathfield <r...@see.sig.i nvalidwrote:
                          >lovecreatesbea ...@gmail.com said:
                          >>
                          ><snip>
                          >>
                          The error check upon strtod is missed four times freely.
                          >>
                          >Then insert it
                          >
                          Yeah, how about this:
                          >
                          $ cat a.c
                          #include <stdlib.h>
                          #include <stdio.h>
                          #include <errno.h>
                          >
                          int main (int argc, char *argv[])
                          {
                          double d;
                          char *end;
                          >
                          for (; *++argv; ){
                          errno = 0;
                          d = strtod(*argv, &end);
                          if (errno){
                          perror(*argv);
                          continue;
                          }
                          if (d == 0 && *argv == end){
                          fprintf(stderr, "%s: Cant be converted\n",
                          *argv);
                          continue;
                          }
                          fprintf(stdout, "%f\n", d);
                          }
                          return EXIT_SUCCESS;
                          }
                          Unfortunately, this destroys the value of each argument (except the last)
                          without storing it safely for further usage. This is known as "throwing
                          the baby out with the bathwater".

                          --
                          Richard Heathfield <http://www.cpax.org.uk >
                          Email: -http://www. +rjh@
                          Google users: <http://www.cpax.org.uk/prg/writings/googly.php>
                          "Usenet is a strange place" - dmr 29 July 1999

                          Comment

                          • Barry Schwarz

                            #73
                            Re: code question

                            On Mon, 22 Sep 2008 03:20:09 -0700 (PDT),
                            "lovecreatesbea ...@gmail.com" <lovecreatesbea uty@gmail.comwr ote:
                            >On Sep 22, 3:56 pm, Richard Heathfield <r...@see.sig.i nvalidwrote:
                            >lovecreatesbea ...@gmail.com said:
                            >>
                            ><snip>
                            >>
                            The error check upon strtod is missed four times freely.
                            >>
                            >Then insert it
                            >
                            >Yeah, how about this:
                            >
                            >$ cat a.c
                            >#include <stdlib.h>
                            >#include <stdio.h>
                            >#include <errno.h>
                            >
                            >int main (int argc, char *argv[])
                            >{
                            double d;
                            char *end;
                            >
                            for (; *++argv; ){
                            errno = 0;
                            d = strtod(*argv, &end);
                            if (errno){
                            perror(*argv);
                            continue;
                            }
                            if (d == 0 && *argv == end){
                            If *argv == end, does it matter what d is?

                            This doesn't handle the situation where the input is "12xyz". You
                            really want to check the *end == '\0'.
                            fprintf(stderr, "%s: Cant be converted\n",
                            >*argv);
                            continue;
                            }
                            fprintf(stdout, "%f\n", d);
                            }
                            return EXIT_SUCCESS;
                            >}
                            --
                            Remove del for email

                            Comment

                            • lovecreatesbea...@gmail.com

                              #74
                              Re: code question

                              On Sep 23, 10:18 am, Barry Schwarz <schwa...@dqel. comwrote:
                              On Mon, 22 Sep 2008 03:20:09 -0700 (PDT),
                              >
                              >
                              >
                              >
                              >
                              "lovecreatesbea ...@gmail.com" <lovecreatesbea ...@gmail.comwr ote:
                              On Sep 22, 3:56 pm, Richard Heathfield <r...@see.sig.i nvalidwrote:
                              lovecreatesbea. ..@gmail.com said:
                              >
                              <snip>
                              >
                              The error check upon strtod is missed four times freely.
                              >
                              Then insert it
                              >
                              Yeah, how about this:
                              >
                              $ cat a.c
                              #include <stdlib.h>
                              #include <stdio.h>
                              #include <errno.h>
                              >
                              int main (int argc, char *argv[])
                              {
                                     double d;
                                     char *end;
                              >
                                     for (; *++argv; ){
                                             errno = 0;
                                             d = strtod(*argv, &end);
                                             if (errno){
                                                     perror(*argv);
                                                     continue;
                                             }
                                             if (d == 0 && *argv == end){
                              >
                              If *argv == end, does it matter what d is?
                              >
                              This doesn't handle the situation where the input is "12xyz".  You
                              really want to check the *end == '\0'.
                              Thank you for also pointed out this in my other post before. I add an
                              additional check same as before to let input like "12 " pass
                              through.
                              >
                                                     fprintf(stderr, "%s: Cant be converted\n",
                              *argv);
                                                     continue;
                                             }
                                             fprintf(stdout, "%f\n", d);
                                     }
                                     return EXIT_SUCCESS;
                              }
                              #include <stdlib.h>
                              #include <stdio.h>
                              #include <errno.h>
                              #include <string.h>

                              int main (int argc, char *argv[])
                              {
                              double d;
                              char **a = argv, *e;

                              while (*++a){
                              errno = 0;
                              d = strtod(*a, &e);
                              if (errno){
                              perror(*a);
                              continue;
                              }
                              if (d == 0 && *a == e){
                              fprintf(stderr, "%s: Cant be converted\n",
                              *a);
                              continue;
                              }
                              while (*e){
                              if (!isspace(*e))
                              break;
                              e++;
                              }
                              if (!*e)
                              fprintf(stdout, "%f, %s\n\n", d, *a);
                              }
                              return EXIT_SUCCESS;
                              }

                              Comment

                              • lovecreatesbea...@gmail.com

                                #75
                                Re: code question

                                On Sep 23, 11:57 am, "lovecreatesbea ...@gmail.com"
                                <lovecreatesbea ...@gmail.comwr ote:
                                On Sep 23, 10:18 am, Barry Schwarz <schwa...@dqel. comwrote:
                                >
                                >
                                >
                                >
                                >
                                On Mon, 22 Sep 2008 03:20:09 -0700 (PDT),
                                >
                                "lovecreatesbea ...@gmail.com" <lovecreatesbea ...@gmail.comwr ote:
                                >On Sep 22, 3:56 pm, Richard Heathfield <r...@see.sig.i nvalidwrote:
                                >lovecreatesbea ...@gmail.com said:
                                >
                                ><snip>
                                >
                                The error check upon strtod is missed four times freely.
                                >
                                >Then insert it
                                >
                                >Yeah, how about this:
                                >
                                >$ cat a.c
                                >#include <stdlib.h>
                                >#include <stdio.h>
                                >#include <errno.h>
                                >
                                >int main (int argc, char *argv[])
                                >{
                                       double d;
                                       char *end;
                                >
                                       for (; *++argv; ){
                                               errno = 0;
                                               d = strtod(*argv, &end);
                                               if (errno){
                                                       perror(*argv);
                                                       continue;
                                               }
                                               if (d == 0 && *argv == end){
                                >
                                If *argv == end, does it matter what d is?
                                >
                                This doesn't handle the situation where the input is "12xyz".  You
                                really want to check the *end == '\0'.
                                >
                                Thank you for also pointed out this in my other post before. I add an
                                additional check same as before to let input like "12   " pass
                                through.
                                >
                                >
                                >
                                                       fprintf(stderr, "%s: Cant be converted\n",
                                >*argv);
                                                       continue;
                                               }
                                               fprintf(stdout, "%f\n", d);
                                       }
                                       return EXIT_SUCCESS;
                                >}
                                >
                                #include <stdlib.h>
                                #include <stdio.h>
                                #include <errno.h>
                                #include <string.h>
                                >
                                int main (int argc, char *argv[])
                                {
                                        double d;
                                        char **a = argv, *e;
                                >
                                        while (*++a){
                                                errno = 0;
                                                d = strtod(*a, &e);
                                                if (errno){
                                                        perror(*a);
                                                        continue;
                                                }
                                                if (d == 0 && *a == e){
                                                        fprintf(stderr, "%s: Cantbe converted\n",
                                *a);
                                                        continue;
                                                }
                                                while (*e){
                                                        if (!isspace(*e))
                                                                break;
                                                        e++;
                                                }
                                                if (!*e)
                                                        fprintf(stdout, "%f, %s\n\n", d, *a);
                                        }
                                        return EXIT_SUCCESS;
                                >
                                >
                                >
                                }
                                >
                                #include <stdlib.h>
                                #include <stdio.h>
                                #include <errno.h>
                                #include <string.h>

                                int main (int argc, char *argv[])
                                {
                                double d;
                                char **a = argv, *e;

                                while (*++a){
                                errno = 0;
                                d = strtod(*a, &e);
                                if (errno){
                                perror(*a);
                                continue;
                                }
                                if (d == 0 && *a == e){
                                fprintf(stderr, "%s: Cant be converted\n",
                                *a);
                                continue;
                                }
                                while (*e)
                                if (!isspace(*e++) )
                                break;
                                if (*e){
                                fprintf(stderr, "%s: Invalid format\n", *a);
                                continue;
                                }
                                fprintf(stdout, "%f, %s\n\n", d, *a);
                                }
                                return EXIT_SUCCESS;
                                }

                                Comment

                                Working...