mailing list of musl libc
 help / color / mirror / code / Atom feed
From: Markus Wichmann <nullplan@gmx.net>
To: musl@lists.openwall.com
Subject: Re: fgets() doesn't call fsync() before getting input
Date: Fri, 22 Feb 2019 06:04:42 +0100	[thread overview]
Message-ID: <20190222050442.GG19969@voyager> (raw)
In-Reply-To: <CAEfvv4+E_6OO4NFd0-X=c-2xXsAWvKNyKkWQ-03+dKChf96QcA@mail.gmail.com>

On Thu, Feb 21, 2019 at 03:16:23PM -0500, James Larrowe wrote:
> Sorry, this was a typo. I meant fflush(). However, it's still not called.
> It's fixed in my program now, however I'm not sure what to do in this case.
> Do I just call ffush() on stdin, stdout, and stderr or do I send a patch to
> fgets()?
> 

You call fflush(stdout). stderr is already unbuffered, so flushing it
only makes sense if you used setvbuf() on it beforehand. Why would you,
though? Being unbuffered is sort of the defining feature of stderr.

Also fflush(stdin) is undefined behavior. fflush() is only defined for
output streams. Since you are asking beginner's questions, you are
likely to come accross the following idiom in C tutorials:

    cnt = scanf("Some format", some variables);
    fflush(stdin);

Or maybe the other way round. The reason for this is that scanf()
terminates at the newline character at the end of the line you inserted,
but doesn't consume it. So the next scanf() will read it as the first
thing. The format will likely not allow for whitespace at the start of
input, and so the scanf() will fail.

The solution is to either allow for the newline by starting the scanf
format with a space, or to use:

    char buf[some appropriate line length];
    if (fgets(buf, sizeof buf, stdin))
        cnt = sscanf(buf, "Some format", some variables);

Ciao,
Markus


  parent reply	other threads:[~2019-02-22  5:04 UTC|newest]

Thread overview: 9+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2019-02-21 15:09 James Larrowe
2019-02-21 15:22 ` Rich Felker
2019-02-21 16:31   ` A. Wilcox
2019-02-21 17:07     ` Rich Felker
2019-02-21 19:24       ` Laurent Bercot
2019-02-21 20:16       ` James Larrowe
2019-02-21 20:44         ` Rich Felker
2019-02-22  5:04         ` Markus Wichmann [this message]
2019-02-25 13:10           ` James Larrowe

Reply instructions:

You may reply publicly to this message via plain-text email
using any one of the following methods:

* Save the following mbox file, import it into your mail client,
  and reply-to-all from there: mbox

  Avoid top-posting and favor interleaved quoting:
  https://en.wikipedia.org/wiki/Posting_style#Interleaved_style

* Reply using the --to, --cc, and --in-reply-to
  switches of git-send-email(1):

  git send-email \
    --in-reply-to=20190222050442.GG19969@voyager \
    --to=nullplan@gmx.net \
    --cc=musl@lists.openwall.com \
    /path/to/YOUR_REPLY

  https://kernel.org/pub/software/scm/git/docs/git-send-email.html

* If your mail client supports setting the In-Reply-To header
  via mailto: links, try the mailto: link
Be sure your reply has a Subject: header at the top and a blank line before the message body.
Code repositories for project(s) associated with this public inbox

	https://git.vuxu.org/mirror/musl/

This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox;
as well as URLs for NNTP newsgroup(s).