illegal seek

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

    illegal seek

    this code gives an "illegal seek error" on close() call
    what is this error and when does it come?

    main()
    {
    int fd,num;
    char buf[150];
    fd = open ("123.c",O_RDWR );
    if(fd!=-1)
    {

    printf("a file with fd=%d is opened\n",fd);
    num=read(fd,buf ,150);
    printf("num=%d\ nREAD:%s",num,b uf);
    perror("READ");
    close(fd);
    perror("CLOSE") ;
    }
    }
  • Richard Tobin

    #2
    Re: illegal seek

    In article <05d8e4bb-3aac-4714-bfb5-87441149da6c@p2 5g2000pri.googl egroups.com>,
    smarty <csmgsarma@gmai l.comwrote:
    >this code gives an "illegal seek error" on close() call
    >what is this error and when does it come?
    Your code contains several errors, notably printing a potentially
    unterminated string from buf.

    But the particular problem you are asking about seems to be that
    Linux's perror() often changes errno. I don't think this is legal,
    but I don't have the standard to hand.

    -- Richard
    --
    :wq

    Comment

    • Szabolcs Borsanyi

      #3
      Re: illegal seek

      >
      But the particular problem you are asking about seems to be that
      Linux's perror() often changes errno. I don't think this is legal,
      but I don't have the standard to hand.
      Well, a standard library function may set errno to a non-zero
      function, unless
      the use of errno is documented in the standard (even if there was no
      error).
      The use of errno in perror() is documented in the standard as using
      its
      value.

      By the way, the read/open/close functions are not standard library
      functions.
      So nothing is known about them. I don't quite remember the posix
      specifications,
      but I am not sure if errno can put to a positive value on success.
      The return value of these functions might better indicate the error
      condition.
      The standard fopen/fread/fclose functions might equally well do the
      task, I'd think. Then you have ferror, too.
      Putting errno=0 before a standard library, that has a documented use
      of
      errno, and reading errno then seems to me legal, too.

      Szabolcs

      Comment

      • Richard Tobin

        #4
        Re: illegal seek

        In article <7943d301-3c09-4c2b-99b4-d36c1a63d18a@a7 0g2000hsh.googl egroups.com>,
        Szabolcs Borsanyi <borsanyi@thphy s.uni-heidelberg.dewr ote:
        >By the way, the read/open/close functions are not standard library
        >functions.
        >So nothing is known about them. I don't quite remember the posix
        >specifications ,
        >but I am not sure if errno can put to a positive value on success.
        The use of those functions is irrelevant to the perror() problem.
        The following program exhibits the same behavious on Linux:

        #include <stdio.h>

        int main(void)
        {
        perror("one");
        perror("two");
        return 0;
        }

        When I run it here it prints

        one: Success
        two: Illegal seek

        -- Richard
        --
        :wq

        Comment

        • Ben Bacarisse

          #5
          Re: illegal seek

          richard@cogsci. ed.ac.uk (Richard Tobin) writes:
          In article <05d8e4bb-3aac-4714-bfb5-87441149da6c@p2 5g2000pri.googl egroups.com>,
          smarty <csmgsarma@gmai l.comwrote:
          >>this code gives an "illegal seek error" on close() call
          >>what is this error and when does it come?
          >
          Your code contains several errors, notably printing a potentially
          unterminated string from buf.
          >
          But the particular problem you are asking about seems to be that
          Linux's perror() often changes errno. I don't think this is legal,
          but I don't have the standard to hand.
          Hmmm... The standard says:

          "The value of errno may be set to nonzero by a library function call
          whether or not there is an error, provided the use of errno is not
          documented in the description of the function in this International
          Standard."

          perror "documents the use of errno" so it seems it is not permitted to
          change it. Of course, if the intent of that phrase it to permit
          functions to change errno provided they are not documented as
          *setting* it in some specific way, then perror *is* allowed to set it
          nonzero. But then, why use the obviously broad phrase "use of"?

          In practise, it hardly matters. One should only rely on the value
          immediately following a function call that has failed in a way that
          documents the setting of errno and this is the problem the OP is
          having. Even if perror stuck to the letter of the standard, both the
          printf and the close call can make it nonzero (even if there is no
          error).

          --
          Ben.

          Comment

          • CBFalconer

            #6
            Re: illegal seek

            smarty wrote:
            >
            this code gives an "illegal seek error" on close() call
            what is this error and when does it come?
            >
            main()
            {
            int fd,num;
            char buf[150];
            fd = open ("123.c",O_RDWR );
            if(fd!=-1)
            {
            >
            printf("a file with fd=%d is opened\n",fd);
            num=read(fd,buf ,150);
            printf("num=%d\ nREAD:%s",num,b uf);
            perror("READ");
            close(fd);
            perror("CLOSE") ;
            }
            }
            Who knows. There are no such functions as 'open', 'read', 'close'
            in standard C. Look up, and use, fopen and fclose, and check the
            various standard library calls for means of reading numbers.

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

            ** Posted from http://www.teranews.com **

            Comment

            • Richard Tobin

              #7
              Re: illegal seek

              In article <482AEEB2.4A51B C8A@yahoo.com>,
              CBFalconer <cbfalconer@mai neline.netwrote :
              >Who knows. There are no such functions as 'open', 'read', 'close'
              >in standard C.
              As it turns out, it's nothing to do with those functions.

              -- Richard
              --
              :wq

              Comment

              • Joachim Schmitz

                #8
                Re: illegal seek


                "smarty" <csmgsarma@gmai l.comschrieb im Newsbeitrag
                news:05d8e4bb-3aac-4714-bfb5-87441149da6c@p2 5g2000pri.googl egroups.com...
                this code gives an "illegal seek error" on close() call
                what is this error and when does it come?
                >
                main()
                {
                int fd,num;
                char buf[150];
                fd = open ("123.c",O_RDWR );
                if(fd!=-1)
                {
                >
                printf("a file with fd=%d is opened\n",fd);
                num=read(fd,buf ,150);
                printf("num=%d\ nREAD:%s",num,b uf);
                perror("READ");
                close(fd);
                perror("CLOSE") ;
                }
                }
                close() sets errno if (and only if) it fails, which it indicates by
                returning -1.
                printf() returns a negative value on failure.
                Only use perror() directly after a failed function call that is documented
                to set errno on failure or set errno to 0 prior to calling that function.

                Bye, Jojo


                Comment

                • Keith Thompson

                  #9
                  Re: illegal seek

                  CBFalconer <cbfalconer@yah oo.comwrites:
                  smarty wrote:
                  >>
                  >this code gives an "illegal seek error" on close() call
                  >what is this error and when does it come?
                  >>
                  >main()
                  >{
                  > int fd,num;
                  > char buf[150];
                  > fd = open ("123.c",O_RDWR );
                  > if(fd!=-1)
                  > {
                  >>
                  > printf("a file with fd=%d is opened\n",fd);
                  > num=read(fd,buf ,150);
                  > printf("num=%d\ nREAD:%s",num,b uf);
                  > perror("READ");
                  > close(fd);
                  > perror("CLOSE") ;
                  > }
                  >}
                  >
                  Who knows. There are no such functions as 'open', 'read', 'close'
                  in standard C. Look up, and use, fopen and fclose, and check the
                  various standard library calls for means of reading numbers.
                  Those are POSIX functions. The OP might well have a good reason to
                  use them instead of the C standard functions fopen, fread, and fclose
                  (the POSIX functions provide some features that the standard C
                  functions don't). The quoted program doesn't demonstrate any such
                  reason, but it's obviously just a sample.

                  But yes, it's generally better to use the standard C I/O functions
                  *unless* you have a specific reason to use the POSIX functions and pay
                  the price of losing some portability.

                  (Incidentally, the program has nothing to do with reading numbers; num
                  is the number of bytes read.)

                  Some incidental advice for the original poster:

                  Write "int main(void)" rather than "main()".

                  Adding a blank after each comma, after the "if" keyword, and before
                  and after each binary operator, aids readability.

                  Since main returns an int, it should do so: add "return 0;" before the
                  closing brace. There are circumstances in which this is not necessary
                  (if you're using a C99 implementation, or if you don't care about the
                  status returned to the host environment), but it never hurts, and it's
                  a good habit.

                  And, of course, you should examine the value of errno only after you
                  know a function has failed (but check the function's documentation to
                  find out whether it even sets errno).

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

                    #10
                    Re: illegal seek

                    Richard Tobin wrote:
                    CBFalconer <cbfalconer@mai neline.netwrote :
                    >
                    >Who knows. There are no such functions as 'open', 'read', 'close'
                    >in standard C.
                    >
                    As it turns out, it's nothing to do with those functions.
                    Since it is not written in C, who knows what is wrong with it. The
                    point is that it contains calls to unknown functions. That makes
                    it off-topic in c.l.c.

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


                    ** Posted from http://www.teranews.com **

                    Comment

                    • CBFalconer

                      #11
                      Re: illegal seek

                      Joachim Schmitz wrote:
                      >
                      .... snip ...
                      >
                      close() sets errno if (and only if) it fails, which it indicates
                      by returning -1.
                      The following is a legal definition of close:

                      int close(int parm) {
                      if (-1 == parm) parm++;
                      return parm;
                      }

                      which doesn't meet your description. The point is that close is
                      undefined in standard C. In this newsgroup the discussion stops
                      there.

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

                      ** Posted from http://www.teranews.com **

                      Comment

                      • Richard Heathfield

                        #12
                        Re: illegal seek

                        CBFalconer said:
                        Richard Tobin wrote:
                        >CBFalconer <cbfalconer@mai neline.netwrote :
                        >>
                        >>Who knows. There are no such functions as 'open', 'read', 'close'
                        >>in standard C.
                        >>
                        >As it turns out, it's nothing to do with those functions.
                        >
                        Since it is not written in C,
                        It sure looks like C to me.
                        who knows what is wrong with it.
                        comp.unix.progr ammer knows what's wrong with 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

                        • Jack Klein

                          #13
                          Re: illegal seek

                          On 14 May 2008 12:10:58 GMT, richard@cogsci. ed.ac.uk (Richard Tobin)
                          wrote in comp.lang.c:
                          In article <7943d301-3c09-4c2b-99b4-d36c1a63d18a@a7 0g2000hsh.googl egroups.com>,
                          Szabolcs Borsanyi <borsanyi@thphy s.uni-heidelberg.dewr ote:
                          >
                          By the way, the read/open/close functions are not standard library
                          functions.
                          So nothing is known about them. I don't quite remember the posix
                          specifications,
                          but I am not sure if errno can put to a positive value on success.
                          >
                          The use of those functions is irrelevant to the perror() problem.
                          The following program exhibits the same behavious on Linux:
                          >
                          #include <stdio.h>
                          >
                          int main(void)
                          {
                          perror("one");
                          perror("two");
                          return 0;
                          }
                          >
                          When I run it here it prints
                          >
                          one: Success
                          two: Illegal seek
                          In that case, the problem is system-specific, and he should take it up
                          with the Linux developers. It is a QOI issue, at best, since the
                          standard allows perror() to modify errno so long as it uses the
                          original value to produce output before modifying it.

                          --
                          Jack Klein
                          Home: http://JK-Technology.Com
                          FAQs for
                          comp.lang.c http://c-faq.com/
                          comp.lang.c++ http://www.parashift.com/c++-faq-lite/
                          alt.comp.lang.l earn.c-c++

                          Comment

                          • Antoninus Twink

                            #14
                            Re: illegal seek

                            On 14 May 2008 at 16:39, CBFalconer wrote:
                            The following is a legal definition of close:
                            >
                            int close(int parm) {
                            if (-1 == parm) parm++;
                            return parm;
                            }
                            Wrong. It fails to meet the specification for close():

                            "The close() function shall deallocate the file descriptor indicated
                            by fildes. To deallocate means to make the file descriptor available for
                            return by subsequent calls to open() or other functions that allocate
                            file descriptors. All outstanding record locks owned by the process on
                            the file associated with the file descriptor shall be removed (that is,
                            unlocked)."

                            Comment

                            • Joachim Schmitz

                              #15
                              Re: illegal seek

                              Antoninus Twink wrote:
                              On 14 May 2008 at 16:39, CBFalconer wrote:
                              >The following is a legal definition of close:
                              >>
                              > int close(int parm) {
                              > if (-1 == parm) parm++;
                              > return parm;
                              > }
                              >
                              Wrong. It fails to meet the specification for close():
                              >
                              "The close() function shall deallocate the file descriptor indicated
                              by fildes. To deallocate means to make the file descriptor available
                              for return by subsequent calls to open() or other functions that
                              allocate file descriptors. All outstanding record locks owned by the
                              process on the file associated with the file descriptor shall be
                              removed (that is, unlocked)."
                              Guess you missed his point: close() is not in the C-Standard, hence there is
                              no specification.
                              Chuck simply ignores that close() exists in POSIX and is available to the
                              vast majority of implementations . He also ignored the fact that the OP did
                              use close() and that the most likely reason is that this is because it is
                              provided by his implementation.

                              Bye, Jojo


                              Comment

                              Working...