Skip site navigation (1)Skip section navigation (2)
Date:      Fri, 7 Sep 2001 22:52:53 +0400
From:      "Andrey A. Chernov" <ache@nagual.pp.ru>
To:        Brian Somers <brian@freebsd-services.com>
Cc:        cvs-committers@FreeBSD.org, cvs-all@FreeBSD.org
Subject:   Re: cvs commit: src/lib/libc/stdio stdio.c
Message-ID:  <20010907225253.A6238@nagual.pp.ru>
In-Reply-To: <200109071826.f87IQtJ20155@hak.lan.Awfulhak.org>
References:  <ache@FreeBSD.org> <200109071716.f87HG3a05025@freefall.freebsd.org> <200109071826.f87IQtJ20155@hak.lan.Awfulhak.org>

next in thread | previous in thread | raw e-mail | index | archive | help
On Fri, Sep 07, 2001 at 19:26:55 +0100, Brian Somers wrote:
> Will the following code work ?
> 
>   fd = socket(blah);
>   connect(fd, blah);
>   dup2(fd, STDIN_FILENO);
>   fp = fopen(fd, "a+");
>   fputc('x', fp);
>   close(STDIN_FILENO);
>   open("myfile", O_RDWR|O_APPEND);	/* file contains data */
>   fseek(fp, 0, SEEK_SET);
>   fputs("hello world\n", fp);
> 
> or will it end up not attempting to seek to the end of the file and 
> overwriting from offset 0 ?

Short answer: it ends up to seek to the end and overwriting from the
offset 0.

Long answer: yes, in this particular case old code sense file handle
changing on the fly because it not cache result, but this is not true in
too many other places in stdio.

In general stdio NOT works with file handle changing on the fly because it
cache too many data it sense once (file offset, file type, etc.), so your
example code is incorrect, you need separate fdopen() for each new file
handle.

-- 
Andrey A. Chernov
http://ache.pp.ru/

To Unsubscribe: send mail to majordomo@FreeBSD.org
with "unsubscribe cvs-all" in the body of the message




Want to link to this message? Use this URL: <https://mail-archive.FreeBSD.org/cgi/mid.cgi?20010907225253.A6238>