delorie.com/archives/browse.cgi   search  
Mail Archives: djgpp-workers/2001/04/04/10:58:23

From: Martin Stromberg <eplmst AT epl DOT ericsson DOT se>
Message-Id: <200104041458.QAA12665@lws256.lu.erisoft.se>
Subject: Re: Bug00323, final? (LONG)
To: djgpp-workers AT delorie DOT com
Date: Wed, 4 Apr 2001 16:58:09 +0200 (MET DST)
In-Reply-To: <Pine.SUN.3.91.1010401104226.15706P-100000@is> from "Eli Zaretskii" at Apr 01, 2001 10:42:50 AM
X-Mailer: ELM [version 2.5 PL3]
MIME-Version: 1.0
Reply-To: djgpp-workers AT delorie DOT com
Errors-To: nobody AT delorie DOT com
X-Mailing-List: djgpp-workers AT delorie DOT com
X-Unsubscribes-To: listserv AT delorie DOT com

Eli said:
> differently.  Can someone please check what happens on Unix and
> GNU/Linux systems?  A simple test program which opens an existing file
> with "a" and then calls ftell and prints the result is all you need.

----- c.c starts. -----
#include <errno.h>
#include <stdio.h>

int main(void)
{
  FILE *f;

  f = fopen(__FILE__, "a");
  if( f == NULL )
  {
    fprintf(stderr, "fopen failed, errno = %d.\n", errno);
    exit( 1 );
  }

  printf("1, ftell(f) = %ld.\n", ftell(f));

  fclose(f);

  f = fopen(__FILE__, "a+");
  if( f == NULL )
  {
    fprintf(stderr, "fopen failed, errno = %d.\n", errno);
    exit( 2 );
  }

  printf("2, ftell(f) = %ld.\n", ftell(f));

  fclose(f);

  return( 0 );
}
----- c.c ends. -----

; uname -a
SunOS lws256 5.6 Generic_105181-23 sun4u sparc
; l c.c
-rw-r-----   1 eplmst   erisoft       466 Apr  4 16:44 c.c
; ./c
1, ftell(f) = 466.
2, ftell(f) = 0.

> The Posix draft seem to say the same.  It would be interesting to know
> what happens with open and fdopen on Unix systems, btw.

----- d.c starts. -----
#include <errno.h>
#include <stdio.h>
#include <sys/types.h>
#include <sys/stat.h>
#include <fcntl.h>
#include <unistd.h>

int main(void)
{
  FILE *f;
  int fd;

  fd = open(__FILE__, O_RDONLY|O_APPEND);
  if( f < 0 )
  {
    fprintf(stderr, "open failed, errno = %d.\n", errno);
    exit( 1 );
  }

  printf("1, lseek(fd, 0, SEEK_CUR) = %ld.\n", lseek(fd, 0, SEEK_CUR));

  f = fdopen(fd, "a");
  if( f == NULL )
  {
    fprintf(stderr, "fdopen failed, errno = %d.\n", errno);
    exit( 3 );
  }

  printf("1, ftell(f) = %ld.\n", ftell(f));

  fclose(f);
  close(fd);

  fd = open(__FILE__, O_RDWR|O_APPEND);
  if( f == NULL )
  {
    fprintf(stderr, "open failed, errno = %d.\n", errno);
    exit( 2 );
  }

  printf("2, lseek(fd, 0, SEEK_CUR) = %ld.\n", lseek(fd, 0, SEEK_CUR));

  f = fdopen(fd, "a+");
  if( f == NULL )
  {
    fprintf(stderr, "fdopen failed, errno = %d.\n", errno);
    exit( 4 );
  }

  printf("2, ftell(f) = %ld.\n", ftell(f));

  fclose(f);
  close(fd);

  return( 0 );
}
----- d.c ends. -----

; ./d
1, lseek(fd, 0, SEEK_CUR) = 0.
1, ftell(f) = 1004.
2, lseek(fd, 0, SEEK_CUR) = 0.
2, ftell(f) = 1004.


Right,

						MartinS

- Raw text -


  webmaster     delorie software   privacy  
  Copyright © 2019   by DJ Delorie     Updated Jul 2019