mailing list of musl libc
 help / color / mirror / code / Atom feed
From: James Larrowe <larrowe.semaj11@gmail.com>
To: musl@lists.openwall.com
Subject: Re: fgets() doesn't call fsync() before getting input
Date: Mon, 25 Feb 2019 08:10:22 -0500	[thread overview]
Message-ID: <CAEfvv4K3nHydmuz5HJmifGPHb=3-qZ1WtxzkAMmin4M8xQZ-NQ@mail.gmail.com> (raw)
In-Reply-To: <20190222050442.GG19969@voyager>

[-- Attachment #1: Type: text/plain, Size: 1523 bytes --]

Got it. Thank you.

On Fri, Feb 22, 2019 at 12:05 AM Markus Wichmann <nullplan@gmx.net> wrote:

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

[-- Attachment #2: Type: text/html, Size: 1967 bytes --]

      reply	other threads:[~2019-02-25 13:10 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
2019-02-25 13:10           ` James Larrowe [this message]

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='CAEfvv4K3nHydmuz5HJmifGPHb=3-qZ1WtxzkAMmin4M8xQZ-NQ@mail.gmail.com' \
    --to=larrowe.semaj11@gmail.com \
    --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).