From mboxrd@z Thu Jan 1 00:00:00 1970 X-Msuck: nntp://news.gmane.org/gmane.linux.lib.musl.general/13863 Path: news.gmane.org!.POSTED.blaine.gmane.org!not-for-mail From: James Larrowe Newsgroups: gmane.linux.lib.musl.general Subject: Re: fgets() doesn't call fsync() before getting input Date: Mon, 25 Feb 2019 08:10:22 -0500 Message-ID: References: <20190221152243.GF23599@brightrain.aerifal.cx> <5c1de98e-9606-81d6-0e69-8f3dbb916065@adelielinux.org> <20190221170748.GI23599@brightrain.aerifal.cx> <20190222050442.GG19969@voyager> Reply-To: musl@lists.openwall.com Mime-Version: 1.0 Content-Type: multipart/alternative; boundary="0000000000006b32b60582b7aaa8" Injection-Info: blaine.gmane.org; posting-host="blaine.gmane.org:195.159.176.226"; logging-data="16610"; mail-complaints-to="usenet@blaine.gmane.org" To: musl@lists.openwall.com Original-X-From: musl-return-13879-gllmg-musl=m.gmane.org@lists.openwall.com Mon Feb 25 14:10:48 2019 Return-path: Envelope-to: gllmg-musl@m.gmane.org Original-Received: from mother.openwall.net ([195.42.179.200]) by blaine.gmane.org with smtp (Exim 4.89) (envelope-from ) id 1gyG1w-0004E1-M3 for gllmg-musl@m.gmane.org; Mon, 25 Feb 2019 14:10:48 +0100 Original-Received: (qmail 29844 invoked by uid 550); 25 Feb 2019 13:10:45 -0000 Mailing-List: contact musl-help@lists.openwall.com; run by ezmlm Precedence: bulk List-Post: List-Help: List-Unsubscribe: List-Subscribe: List-ID: Original-Received: (qmail 29825 invoked from network); 25 Feb 2019 13:10:45 -0000 DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=gmail.com; s=20161025; h=mime-version:references:in-reply-to:from:date:message-id:subject:to; bh=DpzToojO+genLysH2St8DYyvFTgjiBMHV4XDjhjktxM=; b=D1zTJidH6SPJwtNedh482uk0jy32fQrr+flCPJz3QgSOUU3zhdb43KTq+csBs4wvpZ nf6BY00hQXzE+d5Ul72fJVDlXPdfRZInPaKrOFWRoIyJWRN9Oz52rGbgCPz2cWJ7rDd3 s80yK6t8LK5W20NJscM7mvClkX0YwXZe5G62Kn7NPUkKDAzLvLU9e5kES229J2UeBavu ZqiE5dk9lFef9bacnr5L+xn5ZY05m0fKj0ZNj1QPmKCiJC1lXTlnNyNnPUbRaDfgrdos DTpdVOMF0ES+tZpmhXt4RrDvvB1JRPWbPcfhrHSbiRB071vGY8JSqgRAI1+EUh549rU/ SWow== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20161025; h=x-gm-message-state:mime-version:references:in-reply-to:from:date :message-id:subject:to; bh=DpzToojO+genLysH2St8DYyvFTgjiBMHV4XDjhjktxM=; b=bOBw8VvO9dGPjYvW2pdOwc/scgKT75d/1LyWUfbwg44O4YLAuegXy0F8N80e2ieaVt hdT0p4gvIjr+W5cL+6spfL3eGflqZfvQZjHcZOS3tOgTYVvDlxH+X8B/kwM1+bImVwlJ RpITIk+bQyt7iW9K9iL897LCfdvaKymoMJga2EJu1pgI7dLJzJD011mKfzw+BZq+OuOq +o66fxOAXDoJO7Xn+f8Fg92/woGoUuc2KvebpH+3Gj+XHLLIoX6N4pHTPWqvizNbgWy4 lPIx9+9iOLZz2EaXFTsed+F+aJE4+VNCFMqwQDYAOhlW0ei6GbIBB+YfzAi7SsDEGjaW 2Puw== X-Gm-Message-State: AHQUAuZQLguYJq3x/KKoxbTAobaBt7s8mYTAO4DAyWUlMeoO9ha/2Rh+ FtMR89ilotVuH7HOgmX1UZqCwWHdlNVVZpB3CvNPlw== X-Google-Smtp-Source: AHgI3IYu51cpXAP7FIecSgM8r+1gV01dBkE8bSCuTbC9y3VR572U4uHLqk2Xj9ALiSQhMM57mLsuRP1BNEB8vY4qIqg= X-Received: by 2002:a17:906:52d5:: with SMTP id w21mr12958012ejn.172.1551100233329; Mon, 25 Feb 2019 05:10:33 -0800 (PST) In-Reply-To: <20190222050442.GG19969@voyager> Xref: news.gmane.org gmane.linux.lib.musl.general:13863 Archived-At: --0000000000006b32b60582b7aaa8 Content-Type: text/plain; charset="UTF-8" Got it. Thank you. On Fri, Feb 22, 2019 at 12:05 AM Markus Wichmann 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 > --0000000000006b32b60582b7aaa8 Content-Type: text/html; charset="UTF-8" Content-Transfer-Encoding: quoted-printable
Got it. Thank you.

On Fri, Feb 22, 2019 at 12:05 AM Markus = Wichmann <nullplan@gmx.net> w= rote:
On Thu, Fe= b 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 pat= ch 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:

=C2=A0 =C2=A0 cnt =3D scanf("Some format", some variables);
=C2=A0 =C2=A0 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:

=C2=A0 =C2=A0 char buf[some appropriate line length];
=C2=A0 =C2=A0 if (fgets(buf, sizeof buf, stdin))
=C2=A0 =C2=A0 =C2=A0 =C2=A0 cnt =3D sscanf(buf, "Some format", so= me variables);

Ciao,
Markus
--0000000000006b32b60582b7aaa8--