Rocksolid Light

Welcome to RetroBBS

mail  files  register  newsreader  groups  login

Message-ID:  

Dijkstra probably hates me. -- Linus Torvalds, in kernel/sched.c


devel / comp.unix.programmer / Configure stdout to immediately output single characters (unbuffered)

SubjectAuthor
* Configure stdout to immediately output single characters (unbuffered)Janis Papanagnou
+- Re: Configure stdout to immediately output single characters (unbuffered)Rainer Weikusat
+- Re: Configure stdout to immediately output single characters (unbuffered)Kenny McCormack
+- Re: Configure stdout to immediately output single characters (unbuffered)Lawrence D'Oliveiro
+- Re: Configure stdout to immediately output single characters (unbuffered)James Kuyper
`* Re: Configure stdout to immediately output single characters (unbuffered)Janis Papanagnou
 +* Re: Configure stdout to immediately output single characters (unbuffered)Kaz Kylheku
 |`- Re: Configure stdout to immediately output single characters (unbuffered)Kaz Kylheku
 `* Re: Configure stdout to immediately output single characters (unbuffered)Rainer Weikusat
  `* Re: Configure stdout to immediately output single characters (unbuffered)Janis Papanagnou
   `* Re: Configure stdout to immediately output single characters (unbuffered)Rainer Weikusat
    `- Re: Configure stdout to immediately output single characters (unbuffered)Kaz Kylheku

1
Configure stdout to immediately output single characters (unbuffered)

<uojh53$8bse$1@dont-email.me>

  copy mid

https://www.rocksolidbbs.com/devel/article-flat.php?id=17905&group=comp.unix.programmer#17905

  copy link   Newsgroups: comp.unix.programmer
Path: i2pn2.org!i2pn.org!newsfeed.endofthelinebbs.com!news.hispagatos.org!eternal-september.org!feeder3.eternal-september.org!news.eternal-september.org!.POSTED!not-for-mail
From: janis_papanagnou+ng@hotmail.com (Janis Papanagnou)
Newsgroups: comp.unix.programmer
Subject: Configure stdout to immediately output single characters (unbuffered)
Date: Sun, 21 Jan 2024 17:35:46 +0100
Organization: A noiseless patient Spider
Lines: 13
Message-ID: <uojh53$8bse$1@dont-email.me>
MIME-Version: 1.0
Content-Type: text/plain; charset=utf-8
Content-Transfer-Encoding: 7bit
Injection-Date: Sun, 21 Jan 2024 16:35:47 -0000 (UTC)
Injection-Info: dont-email.me; posting-host="0260ad705eb16fcff7e917c4075b9281";
logging-data="274318"; mail-complaints-to="abuse@eternal-september.org"; posting-account="U2FsdGVkX1+bklJjKpaJhdWXsbW1bqY+"
User-Agent: Mozilla/5.0 (X11; Linux x86_64; rv:45.0) Gecko/20100101
Thunderbird/45.8.0
Cancel-Lock: sha1:DI2Ap9I4PpR2zg2QAujypXrl+6M=
X-Enigmail-Draft-Status: N1110
X-Mozilla-News-Host: news://news.eternal-september.org:119
 by: Janis Papanagnou - Sun, 21 Jan 2024 16:35 UTC

In an application to be run on Linux I want every output character
to be flushed immediately through stdout onto the screen.

Currently I have a couple explicit fflush() calls in my C program.
I wanted to avoid these calls and configure the output channel on
OS level to immediately output any arriving character automatically.

It seems I need something like the tcsetattr(3) 'FLUSHO', but the
man page says "FLUSHO (not in POSIX; not supported under Linux)".

What would then be the way to achieve that on Linux (if possible)?

Janis

Re: Configure stdout to immediately output single characters (unbuffered)

<87zfwyha5q.fsf@doppelsaurus.mobileactivedefense.com>

  copy mid

https://www.rocksolidbbs.com/devel/article-flat.php?id=17906&group=comp.unix.programmer#17906

  copy link   Newsgroups: comp.unix.programmer
Path: i2pn2.org!i2pn.org!weretis.net!feeder8.news.weretis.net!fu-berlin.de!uni-berlin.de!individual.net!not-for-mail
From: rweikusat@talktalk.net (Rainer Weikusat)
Newsgroups: comp.unix.programmer
Subject: Re: Configure stdout to immediately output single characters (unbuffered)
Date: Sun, 21 Jan 2024 17:24:33 +0000
Lines: 15
Message-ID: <87zfwyha5q.fsf@doppelsaurus.mobileactivedefense.com>
References: <uojh53$8bse$1@dont-email.me>
Mime-Version: 1.0
Content-Type: text/plain
X-Trace: individual.net QEuosrRp7OqD4Dc82F6CuQ0X9etpHjGhhDOlQkVoYU2lFKyrM=
Cancel-Lock: sha1:b9Z4kBrEc/nH24N4nU6Lx1gTwC0= sha1:Zly0SuocYdiSfTy84kK1lduPwNM= sha256:vN95fq3x1Iq9988eW8tLAszcUXdutA62Uglb9TvA+G4=
User-Agent: Gnus/5.13 (Gnus v5.13) Emacs/27.1 (gnu/linux)
 by: Rainer Weikusat - Sun, 21 Jan 2024 17:24 UTC

Janis Papanagnou <janis_papanagnou+ng@hotmail.com> writes:
> In an application to be run on Linux I want every output character
> to be flushed immediately through stdout onto the screen.
>
> Currently I have a couple explicit fflush() calls in my C program.
> I wanted to avoid these calls and configure the output channel on
> OS level to immediately output any arriving character automatically.
>
> It seems I need something like the tcsetattr(3) 'FLUSHO', but the
> man page says "FLUSHO (not in POSIX; not supported under Linux)".
>
> What would then be the way to achieve that on Linux (if possible)?

setvbuf(3) if you want to use stdio. But if you don't want any
buffering, you can also just use write(2) directly.

Re: Configure stdout to immediately output single characters (unbuffered)

<uojkco$ajrb$2@news.xmission.com>

  copy mid

https://www.rocksolidbbs.com/devel/article-flat.php?id=17907&group=comp.unix.programmer#17907

  copy link   Newsgroups: comp.unix.programmer
Path: i2pn2.org!i2pn.org!weretis.net!feeder6.news.weretis.net!xmission!nnrp.xmission!.POSTED.shell.xmission.com!not-for-mail
From: gazelle@shell.xmission.com (Kenny McCormack)
Newsgroups: comp.unix.programmer
Subject: Re: Configure stdout to immediately output single characters (unbuffered)
Date: Sun, 21 Jan 2024 17:31:04 -0000 (UTC)
Organization: The official candy of the new Millennium
Message-ID: <uojkco$ajrb$2@news.xmission.com>
References: <uojh53$8bse$1@dont-email.me>
Injection-Date: Sun, 21 Jan 2024 17:31:04 -0000 (UTC)
Injection-Info: news.xmission.com; posting-host="shell.xmission.com:166.70.8.4";
logging-data="348011"; mail-complaints-to="abuse@xmission.com"
X-Newsreader: trn 4.0-test77 (Sep 1, 2010)
Originator: gazelle@shell.xmission.com (Kenny McCormack)
 by: Kenny McCormack - Sun, 21 Jan 2024 17:31 UTC

In article <uojh53$8bse$1@dont-email.me>,
Janis Papanagnou <janis_papanagnou+ng@hotmail.com> wrote:
>In an application to be run on Linux I want every output character
>to be flushed immediately through stdout onto the screen.
>
>Currently I have a couple explicit fflush() calls in my C program.
>I wanted to avoid these calls and configure the output channel on
>OS level to immediately output any arriving character automatically.
>
>It seems I need something like the tcsetattr(3) 'FLUSHO', but the
>man page says "FLUSHO (not in POSIX; not supported under Linux)".
>
>What would then be the way to achieve that on Linux (if possible)?
>
>Janis

#include <stdio.h>
#include <unistd.h>

int main(int argc,char **argv)
{ setvbuf(stdout,NULL,_IONBF,0);
printf("This is a test...\t");
sleep(10);
puts("Done!");
}

--
Christianity is not a religion.

- Rick C Hodgin -

Re: Configure stdout to immediately output single characters (unbuffered)

<uok2u2$cgf7$7@dont-email.me>

  copy mid

https://www.rocksolidbbs.com/devel/article-flat.php?id=17908&group=comp.unix.programmer#17908

  copy link   Newsgroups: comp.unix.programmer
Path: i2pn2.org!i2pn.org!eternal-september.org!feeder3.eternal-september.org!news.eternal-september.org!.POSTED!not-for-mail
From: ldo@nz.invalid (Lawrence D'Oliveiro)
Newsgroups: comp.unix.programmer
Subject: Re: Configure stdout to immediately output single characters
(unbuffered)
Date: Sun, 21 Jan 2024 21:39:14 -0000 (UTC)
Organization: A noiseless patient Spider
Lines: 8
Message-ID: <uok2u2$cgf7$7@dont-email.me>
References: <uojh53$8bse$1@dont-email.me>
MIME-Version: 1.0
Content-Type: text/plain; charset=UTF-8
Content-Transfer-Encoding: 8bit
Injection-Date: Sun, 21 Jan 2024 21:39:14 -0000 (UTC)
Injection-Info: dont-email.me; posting-host="942a1f970482f2e4dbf1b31185d2ff4b";
logging-data="410087"; mail-complaints-to="abuse@eternal-september.org"; posting-account="U2FsdGVkX18tf8VCZwF9vHGa8K4oS59f"
User-Agent: Pan/0.155 (Kherson; fc5a80b8)
Cancel-Lock: sha1:iwwuc4eVxB4Tfgfy2eMhzRLwuRY=
 by: Lawrence D'Oliv - Sun, 21 Jan 2024 21:39 UTC

On Sun, 21 Jan 2024 17:35:46 +0100, Janis Papanagnou wrote:

> In an application to be run on Linux I want every output character to be
> flushed immediately through stdout onto the screen.

Don’t want buffering, don’t use the buffered interface, then. Use write(2)
calls directly to FD 1, or STDOUT_FILENO, if you want to be symbolic about
it.

Re: Configure stdout to immediately output single characters (unbuffered)

<uoke60$e3u9$1@dont-email.me>

  copy mid

https://www.rocksolidbbs.com/devel/article-flat.php?id=17909&group=comp.unix.programmer#17909

  copy link   Newsgroups: comp.unix.programmer
Path: i2pn2.org!i2pn.org!eternal-september.org!feeder3.eternal-september.org!news.eternal-september.org!.POSTED!not-for-mail
From: jameskuyper@alumni.caltech.edu (James Kuyper)
Newsgroups: comp.unix.programmer
Subject: Re: Configure stdout to immediately output single characters
(unbuffered)
Date: Sun, 21 Jan 2024 19:51:12 -0500
Organization: A noiseless patient Spider
Lines: 14
Message-ID: <uoke60$e3u9$1@dont-email.me>
References: <uojh53$8bse$1@dont-email.me>
MIME-Version: 1.0
Content-Type: text/plain; charset=UTF-8
Content-Transfer-Encoding: 7bit
Injection-Date: Mon, 22 Jan 2024 00:51:12 -0000 (UTC)
Injection-Info: dont-email.me; posting-host="74d1c30c7548c2c7b2cee884ce9ac0bf";
logging-data="462793"; mail-complaints-to="abuse@eternal-september.org"; posting-account="U2FsdGVkX1/3LEwgm7CY8q6bIMK4unirrKFJCiHx0RY="
User-Agent: Mozilla Thunderbird
Cancel-Lock: sha1:88j50dujoXSNglwztjPSnplMIes=
In-Reply-To: <uojh53$8bse$1@dont-email.me>
Content-Language: en-US
 by: James Kuyper - Mon, 22 Jan 2024 00:51 UTC

On 1/21/24 11:35, Janis Papanagnou wrote:
> In an application to be run on Linux I want every output character
> to be flushed immediately through stdout onto the screen.
>
> Currently I have a couple explicit fflush() calls in my C program.
> I wanted to avoid these calls and configure the output channel on
> OS level to immediately output any arriving character automatically.
>
> It seems I need something like the tcsetattr(3) 'FLUSHO', but the
> man page says "FLUSHO (not in POSIX; not supported under Linux)".
>
> What would then be the way to achieve that on Linux (if possible)?

Would setvbuf(stdout, _IONBF, null) do what you want?

Re: Configure stdout to immediately output single characters (unbuffered)

<uq5lqs$2nemk$1@dont-email.me>

  copy mid

https://www.rocksolidbbs.com/devel/article-flat.php?id=18326&group=comp.unix.programmer#18326

  copy link   Newsgroups: comp.unix.programmer
Path: i2pn2.org!i2pn.org!paganini.bofh.team!eternal-september.org!feeder3.eternal-september.org!news.eternal-september.org!.POSTED!not-for-mail
From: janis_papanagnou+ng@hotmail.com (Janis Papanagnou)
Newsgroups: comp.unix.programmer
Subject: Re: Configure stdout to immediately output single characters
(unbuffered)
Date: Fri, 9 Feb 2024 18:02:19 +0100
Organization: A noiseless patient Spider
Lines: 46
Message-ID: <uq5lqs$2nemk$1@dont-email.me>
References: <uojh53$8bse$1@dont-email.me>
MIME-Version: 1.0
Content-Type: text/plain; charset=utf-8
Content-Transfer-Encoding: 7bit
Injection-Date: Fri, 9 Feb 2024 17:02:20 -0000 (UTC)
Injection-Info: dont-email.me; posting-host="b4bd5c7591744e47e5aa1baa70120b2c";
logging-data="2865876"; mail-complaints-to="abuse@eternal-september.org"; posting-account="U2FsdGVkX1+UZm4Gyc/qVYDHK+VzegbD"
User-Agent: Mozilla/5.0 (X11; Linux x86_64; rv:45.0) Gecko/20100101
Thunderbird/45.8.0
Cancel-Lock: sha1:Y6E9ItbudKNuBiXMCybotwpU2xM=
X-Enigmail-Draft-Status: N1110
In-Reply-To: <uojh53$8bse$1@dont-email.me>
 by: Janis Papanagnou - Fri, 9 Feb 2024 17:02 UTC

On 21.01.2024 17:35, Janis Papanagnou wrote:
> In an application to be run on Linux I want every output character
> to be flushed immediately through stdout onto the screen.
>
> Currently I have a couple explicit fflush() calls in my C program.
> I wanted to avoid these calls and configure the output channel on
> OS level to immediately output any arriving character automatically.
>
> [...]

The task was delayed, so I'm coming back to this a bit late...

I used a slightly modified test program based on Kenny's code
that outputs single characters with a sleep delay, both with
write(2) and setvbuf(3). I also switched buffering from mode
to mode, to see the effects.
Both suggestions in this thread work well.[*] - Thank you!

There's one point, though, that unsettles me a bit. The setvbuf()
man page says:

"The setvbuf() function may only be used after opening a stream
and before any other operations have been performed on it."

If taken literally, the consequence would be that we can use that
function just once at the beginning of a program (and never again).
Especially switching modes would not be allowed or be [reliably]
possible. That would greatly restrict it's usefulness beyond the
initial (and then persistent) definition of the channel buffering
(unless closing and then reopening the channel, in my case stdout,
which is not exactly what I'd strive for).

In my code I've experimented; I made a couple initial outputs on
stdout, made a fflush() to serve my paranoia, and then switched to
unbuffered output. I also switched forth and back from unbuffered
to line-buffered output in a loop. - It *seems* to work at least.

Any experiences with the restriction documented in the man page,
or opinions and suggestions on the mode-switching I intend to do?

Janis

[*] With write I hadn't been sure whether there's another hidden
stream buffer involved that would have to be configured. But no,
a single character is output, that's fine.

Re: Configure stdout to immediately output single characters (unbuffered)

<20240209091356.619@kylheku.com>

  copy mid

https://www.rocksolidbbs.com/devel/article-flat.php?id=18328&group=comp.unix.programmer#18328

  copy link   Newsgroups: comp.unix.programmer
Path: i2pn2.org!i2pn.org!usenet.blueworldhosting.com!diablo1.usenet.blueworldhosting.com!eternal-september.org!feeder3.eternal-september.org!news.eternal-september.org!.POSTED!not-for-mail
From: 433-929-6894@kylheku.com (Kaz Kylheku)
Newsgroups: comp.unix.programmer
Subject: Re: Configure stdout to immediately output single characters
(unbuffered)
Date: Fri, 9 Feb 2024 17:34:23 -0000 (UTC)
Organization: A noiseless patient Spider
Lines: 68
Message-ID: <20240209091356.619@kylheku.com>
References: <uojh53$8bse$1@dont-email.me> <uq5lqs$2nemk$1@dont-email.me>
Injection-Date: Fri, 9 Feb 2024 17:34:23 -0000 (UTC)
Injection-Info: dont-email.me; posting-host="f0d9c4d56e1bf3d087a60d362bc7520c";
logging-data="2877021"; mail-complaints-to="abuse@eternal-september.org"; posting-account="U2FsdGVkX1+a+hrVmxWS9N+psd05z8sP69JD1N5s9Q4="
User-Agent: slrn/pre1.0.4-9 (Linux)
Cancel-Lock: sha1:w3Xt7hEUDp6i5RrnTlk37HfHcPM=
 by: Kaz Kylheku - Fri, 9 Feb 2024 17:34 UTC

On 2024-02-09, Janis Papanagnou <janis_papanagnou+ng@hotmail.com> wrote:
> There's one point, though, that unsettles me a bit. The setvbuf()
> man page says:
>
> "The setvbuf() function may only be used after opening a stream
> and before any other operations have been performed on it."

As of C99, the freopen function allows the path argument to be null.

You should be able to do this:

FILE *new_stream = freopen(NULL, "r", old_stream);

After this the old_stream object is toast. The new_stream object
is open to the same file/device.

Since no operations have been done on new_stream at this point,
we should be able to change the buffering mode to whatever
we want.

The downside is that we have to replace all copies of the old_stream
pointer with new_stream. (Easly done if we have some context structure
that holds the stream, where only that context structure is shared).

> In my code I've experimented; I made a couple initial outputs on
> stdout, made a fflush() to serve my paranoia, and then switched to
> unbuffered output. I also switched forth and back from unbuffered
> to line-buffered output in a loop. - It *seems* to work at least.

I would say all bets are off. Even if it works, there could be a memory
leak. The requirements allow a FILE stream to lazily allocate the
buffer. E.g. there can be a null pointer there, and when the first
operation notices this, it mallocs a buffer. Thus setvbuf can just
blindly store a pointer, without trying to free anything, such that it
results in a memory leak if it is used after a buffer is allocated.

To test that we can write a loop which repeatedly opens a stream,
performs some I/O, and then changes the buffer and closes,
monitoring the program for leaks.

> Any experiences with the restriction documented in the man page,
> or opinions and suggestions on the mode-switching I intend to do?

If it's a Linux man page, do not trust that. Linux man pages
are second-hand information cribbed from multiple sources.

Note that Linux systems have more than one C library, yet
those pages from the Linux man page project are the same!

Sometimes it's just copy and paste or paraphrasing from POSIX
or ISO C.

If you're on glibc, the documentation for setvbuf is this:

https://sourceware.org/glibc/manual/html_mono/libc.html#Controlling-Buffering

It doesn't mention anything about this issue, which means it remains
undefined behavior (i.e. glibc isn't providing a documented extension
whereby buffering can be changed after other operations have been done).

glibc is actually implementing POSIX, but regarding setvbuf, POSIX
defers all the requirements to ISO C. It carries forward the
restriction.

--
TXR Programming Language: http://nongnu.org/txr
Cygnal: Cygwin Native Application Library: http://kylheku.com/cygnal
Mastodon: @Kazinator@mstdn.ca

Re: Configure stdout to immediately output single characters (unbuffered)

<87le7t796d.fsf@doppelsaurus.mobileactivedefense.com>

  copy mid

https://www.rocksolidbbs.com/devel/article-flat.php?id=18333&group=comp.unix.programmer#18333

  copy link   Newsgroups: comp.unix.programmer
Path: i2pn2.org!i2pn.org!news.swapon.de!fu-berlin.de!uni-berlin.de!individual.net!not-for-mail
From: rweikusat@talktalk.net (Rainer Weikusat)
Newsgroups: comp.unix.programmer
Subject: Re: Configure stdout to immediately output single characters (unbuffered)
Date: Fri, 09 Feb 2024 19:02:34 +0000
Lines: 38
Message-ID: <87le7t796d.fsf@doppelsaurus.mobileactivedefense.com>
References: <uojh53$8bse$1@dont-email.me> <uq5lqs$2nemk$1@dont-email.me>
Mime-Version: 1.0
Content-Type: text/plain
X-Trace: individual.net UN0p5ERkGd+nB8nVmPF7wQVrWv3L0U0BbPNc7k0VqH9W7xgwQ=
Cancel-Lock: sha1:sVV9Sg3orCO4IMPUmBcTCeN+E9M= sha1:hZqkFuND7jIi8yLf/HZy6LAWmnY= sha256:90qKhSvBi4LTCAlFMpnII8l/C1NAobcijXVVNohw8iM=
User-Agent: Gnus/5.13 (Gnus v5.13) Emacs/27.1 (gnu/linux)
 by: Rainer Weikusat - Fri, 9 Feb 2024 19:02 UTC

Janis Papanagnou <janis_papanagnou+ng@hotmail.com> writes:
> On 21.01.2024 17:35, Janis Papanagnou wrote:
>> In an application to be run on Linux I want every output character
>> to be flushed immediately through stdout onto the screen.
>>
>> Currently I have a couple explicit fflush() calls in my C program.
>> I wanted to avoid these calls and configure the output channel on
>> OS level to immediately output any arriving character automatically.
>>
>> [...]
>

[...]

> I used a slightly modified test program based on Kenny's code
> that outputs single characters with a sleep delay, both with
> write(2) and setvbuf(3). I also switched buffering from mode
> to mode, to see the effects.
> Both suggestions in this thread work well.[*] - Thank you!
>
> There's one point, though, that unsettles me a bit. The setvbuf()
> man page says:
>
> "The setvbuf() function may only be used after opening a stream
> and before any other operations have been performed on it."
>
> If taken literally, the consequence would be that we can use that
> function just once at the beginning of a program (and never again).
> Especially switching modes would not be allowed or be [reliably]
> possible.

IMHO, using stdio for unbuffered output is even more insane than using
stdio at all (the locking model forced into it is downright
horrible). Even if you want buffered single character/ small string
output, this can be implemented on stop of write in a sane way with very
little code. And the easiest to do unbuffered output is to make the
corresponding system calls instead of using stdio as
proxy-system-caller.

Re: Configure stdout to immediately output single characters (unbuffered)

<20240209184724.826@kylheku.com>

  copy mid

https://www.rocksolidbbs.com/devel/article-flat.php?id=18353&group=comp.unix.programmer#18353

  copy link   Newsgroups: comp.unix.programmer
Path: i2pn2.org!i2pn.org!usenet.goja.nl.eu.org!weretis.net!feeder8.news.weretis.net!eternal-september.org!feeder3.eternal-september.org!news.eternal-september.org!.POSTED!not-for-mail
From: 433-929-6894@kylheku.com (Kaz Kylheku)
Newsgroups: comp.unix.programmer
Subject: Re: Configure stdout to immediately output single characters
(unbuffered)
Date: Sat, 10 Feb 2024 03:01:12 -0000 (UTC)
Organization: A noiseless patient Spider
Lines: 57
Message-ID: <20240209184724.826@kylheku.com>
References: <uojh53$8bse$1@dont-email.me> <uq5lqs$2nemk$1@dont-email.me>
<20240209091356.619@kylheku.com>
Injection-Date: Sat, 10 Feb 2024 03:01:12 -0000 (UTC)
Injection-Info: dont-email.me; posting-host="3de03a7474c831a6f9d55487e9415a62";
logging-data="3176849"; mail-complaints-to="abuse@eternal-september.org"; posting-account="U2FsdGVkX19wMsvZOVwoU57FbhJg0c5QOT5G7CONzrI="
User-Agent: slrn/pre1.0.4-9 (Linux)
Cancel-Lock: sha1:L1JrW+v1vWis4S3aHMcjMhutfco=
 by: Kaz Kylheku - Sat, 10 Feb 2024 03:01 UTC

On 2024-02-09, Kaz Kylheku <433-929-6894@kylheku.com> wrote:
> On 2024-02-09, Janis Papanagnou <janis_papanagnou+ng@hotmail.com> wrote:
>> There's one point, though, that unsettles me a bit. The setvbuf()
>> man page says:
>>
>> "The setvbuf() function may only be used after opening a stream
>> and before any other operations have been performed on it."
>
> As of C99, the freopen function allows the path argument to be null.
>
> You should be able to do this:
>
> FILE *new_stream = freopen(NULL, "r", old_stream);

Unfortunately, freopen(NULL, ...) is kind of buggered.

If old_stream is connected to a file, it will reopen the file, which
means that the stream is repositioned at the beginning. Whereas if the
stream is a pipe or socket, then repositioning won't take place.

I'm playing with the function on glibc, and seeing the following.

If you use the "w" mode on a file stream, the file is reopened, and the
truncation implied by "w" takes place!

In other words, this sequence of operations:

FILE *f = fopen("foo", "w");
fputs("abcdefg\n", f);
fflush(f);

f = freopen(NULL, "w", f);
fputs("xyz\n", f);

fclose(f);

will end with "foo" containing just the line "xyz\n".

The "abcdefg\n" line is lost because because the freopen
truncates the file!

freopen is not a good solution if all you want is to fiddle with
buffering settings without upsetting anything else, or even moving the
read/write position.

Maybe it's better behaved for pipes and such, which cannot be truncated
or change position. But it's not clear whether it's supported at all for
something that was the result of fdopen on a pipe or a popen.

Probably about all freopen is good for is changing stdin or stdout from
binary to text mode at close to program startup. (Since there is no API
for doing that like for doing buffering)

--
TXR Programming Language: http://nongnu.org/txr
Cygnal: Cygwin Native Application Library: http://kylheku.com/cygnal
Mastodon: @Kazinator@mstdn.ca

Re: Configure stdout to immediately output single characters (unbuffered)

<uqae6g$vc49$1@dont-email.me>

  copy mid

https://www.rocksolidbbs.com/devel/article-flat.php?id=18359&group=comp.unix.programmer#18359

  copy link   Newsgroups: comp.unix.programmer
Path: i2pn2.org!i2pn.org!eternal-september.org!feeder3.eternal-september.org!news.eternal-september.org!.POSTED!not-for-mail
From: janis_papanagnou+ng@hotmail.com (Janis Papanagnou)
Newsgroups: comp.unix.programmer
Subject: Re: Configure stdout to immediately output single characters
(unbuffered)
Date: Sun, 11 Feb 2024 13:22:39 +0100
Organization: A noiseless patient Spider
Lines: 30
Message-ID: <uqae6g$vc49$1@dont-email.me>
References: <uojh53$8bse$1@dont-email.me> <uq5lqs$2nemk$1@dont-email.me>
<87le7t796d.fsf@doppelsaurus.mobileactivedefense.com>
MIME-Version: 1.0
Content-Type: text/plain; charset=windows-1252
Content-Transfer-Encoding: 7bit
Injection-Date: Sun, 11 Feb 2024 12:22:40 -0000 (UTC)
Injection-Info: dont-email.me; posting-host="a016585c957c3c34a97f03dfc059e855";
logging-data="1028233"; mail-complaints-to="abuse@eternal-september.org"; posting-account="U2FsdGVkX19U8cbcv9zctnvVMw5fz/d5"
User-Agent: Mozilla/5.0 (X11; Linux x86_64; rv:45.0) Gecko/20100101
Thunderbird/45.8.0
Cancel-Lock: sha1:0MQJCPsQ1IGcaGLsNN826iyro74=
X-Enigmail-Draft-Status: N1110
In-Reply-To: <87le7t796d.fsf@doppelsaurus.mobileactivedefense.com>
 by: Janis Papanagnou - Sun, 11 Feb 2024 12:22 UTC

On 09.02.2024 20:02, Rainer Weikusat wrote:
>
> IMHO, using stdio for unbuffered output is even more insane than using
> stdio at all (the locking model forced into it is downright
> horrible). Even if you want buffered single character/ small string
> output, this can be implemented on stop of write in a sane way with very
> little code. And the easiest to do unbuffered output is to make the
> corresponding system calls instead of using stdio as
> proxy-system-caller.

I have to admit that I'm a bit confused about your statement since
in your initial reply you proposed both, setvbuf(3) and write(2).
This is somewhat in contrast to the strong wording "insane" you used
here. (I mean, the stdio designers must have had some idea in mind
when they invented 'setvbuf()' with '_IONBF'.)

But okay, here you are suggesting to avoid stdio, which effectively
means that I replace, for example, all printf(...) formatted output
by a sequence of sprintf(buf,...); write(...,buf,...) and similarly
also replace all fputs() in favour of 'write' (and certainly also not
to mix them), i.e. to stay with stdio for formatting only, and for
the plain output process to use only system calls. Right? - This way
issues like the one I quoted from the man page[*], which can be
considered being a "proxy"-effect, could be prevented. Makes sense.

Janis

[*] "The setvbuf() function may only be used after opening a stream
and before any other operations have been performed on it."

Re: Configure stdout to immediately output single characters (unbuffered)

<87il2uheng.fsf@doppelsaurus.mobileactivedefense.com>

  copy mid

https://www.rocksolidbbs.com/devel/article-flat.php?id=18360&group=comp.unix.programmer#18360

  copy link   Newsgroups: comp.unix.programmer
Path: i2pn2.org!i2pn.org!news.swapon.de!fu-berlin.de!uni-berlin.de!individual.net!not-for-mail
From: rweikusat@talktalk.net (Rainer Weikusat)
Newsgroups: comp.unix.programmer
Subject: Re: Configure stdout to immediately output single characters (unbuffered)
Date: Sun, 11 Feb 2024 21:30:59 +0000
Lines: 31
Message-ID: <87il2uheng.fsf@doppelsaurus.mobileactivedefense.com>
References: <uojh53$8bse$1@dont-email.me> <uq5lqs$2nemk$1@dont-email.me>
<87le7t796d.fsf@doppelsaurus.mobileactivedefense.com>
<uqae6g$vc49$1@dont-email.me>
Mime-Version: 1.0
Content-Type: text/plain
X-Trace: individual.net +a59KJN2muP2wC5ZzYPjcgpjzYx0IJQnhLmp41MpzfhZJXX9Y=
Cancel-Lock: sha1:1WJCBNwP1w0rPlVrSU3PNNcBLO8= sha1:+imkatZao4RkurJYOQOLEL2Sy4s= sha256:b1FLFstHoVHyJKq2ZOmBeh1NsA3UmM5r35W7G3LhFq0=
User-Agent: Gnus/5.13 (Gnus v5.13) Emacs/27.1 (gnu/linux)
 by: Rainer Weikusat - Sun, 11 Feb 2024 21:30 UTC

Janis Papanagnou <janis_papanagnou+ng@hotmail.com> writes:
> On 09.02.2024 20:02, Rainer Weikusat wrote:
>>
>> IMHO, using stdio for unbuffered output is even more insane than using
>> stdio at all (the locking model forced into it is downright
>> horrible). Even if you want buffered single character/ small string
>> output, this can be implemented on stop of write in a sane way with very
>> little code. And the easiest to do unbuffered output is to make the
>> corresponding system calls instead of using stdio as
>> proxy-system-caller.
>
> I have to admit that I'm a bit confused about your statement since
> in your initial reply you proposed both, setvbuf(3) and write(2).
> This is somewhat in contrast to the strong wording "insane" you used
> here. (I mean, the stdio designers must have had some idea in mind
> when they invented 'setvbuf()' with '_IONBF'.)

stdio is part of C, write and read are UNIX interfaces. Hence, code
using stdio is portable to systems which don't support UNIX interfaces.

The 'insane' is my personal opinion on the thread-safe locking model
stdio is nowadays required to follow on UNIX. That's based on the
assumption that all stdio-streams will be used uncoordinately from
different threads and that because of this, every stdio-operation has to
acquire a lock on the stream before doing anyhing. People who don't use
stdio in this way may prefix _unlocked to each stdio-call their code is
making.

IMHO, it would have been much more sensible to declare that streams are
not thread-safe and that application need to apply whatever locking
suits their needs when sharing them between threads.

Re: Configure stdout to immediately output single characters (unbuffered)

<20240211134043.256@kylheku.com>

  copy mid

https://www.rocksolidbbs.com/devel/article-flat.php?id=18361&group=comp.unix.programmer#18361

  copy link   Newsgroups: comp.unix.programmer
Path: i2pn2.org!i2pn.org!eternal-september.org!feeder3.eternal-september.org!news.eternal-september.org!.POSTED!not-for-mail
From: 433-929-6894@kylheku.com (Kaz Kylheku)
Newsgroups: comp.unix.programmer
Subject: Re: Configure stdout to immediately output single characters
(unbuffered)
Date: Sun, 11 Feb 2024 21:42:34 -0000 (UTC)
Organization: A noiseless patient Spider
Lines: 7
Message-ID: <20240211134043.256@kylheku.com>
References: <uojh53$8bse$1@dont-email.me> <uq5lqs$2nemk$1@dont-email.me>
<87le7t796d.fsf@doppelsaurus.mobileactivedefense.com>
<uqae6g$vc49$1@dont-email.me>
<87il2uheng.fsf@doppelsaurus.mobileactivedefense.com>
Injection-Date: Sun, 11 Feb 2024 21:42:34 -0000 (UTC)
Injection-Info: dont-email.me; posting-host="00a9f2e9b63e24c156cd63276b33d69c";
logging-data="1207004"; mail-complaints-to="abuse@eternal-september.org"; posting-account="U2FsdGVkX1+bNhjlAcEqL9Rh+Z8FKH+fM82SKcGNBpU="
User-Agent: slrn/pre1.0.4-9 (Linux)
Cancel-Lock: sha1:CzX6NUdhrAHuvToWdiPP+vIAhL0=
 by: Kaz Kylheku - Sun, 11 Feb 2024 21:42 UTC

On 2024-02-11, Rainer Weikusat <rweikusat@talktalk.net> wrote:
> The 'insane' is my personal opinion on the thread-safe locking model
> stdio is nowadays required to follow on UNIX.

On Glibc/Linux, a single-threaded process that isn't linked with
-lpthread doesn't suffer from the locking. It is magically switched
on when you link in the threading library.

1
server_pubkey.txt

rocksolid light 0.9.81
clearnet tor