From mboxrd@z Thu Jan 1 00:00:00 1970 X-Msuck: nntp://news.gmane.org/gmane.linux.lib.musl.general/13846 Path: news.gmane.org!.POSTED.blaine.gmane.org!not-for-mail From: Markus Wichmann Newsgroups: gmane.linux.lib.musl.general Subject: Re: fgets() doesn't call fsync() before getting input Date: Fri, 22 Feb 2019 06:04:42 +0100 Message-ID: <20190222050442.GG19969@voyager> References: <20190221152243.GF23599@brightrain.aerifal.cx> <5c1de98e-9606-81d6-0e69-8f3dbb916065@adelielinux.org> <20190221170748.GI23599@brightrain.aerifal.cx> Reply-To: musl@lists.openwall.com Mime-Version: 1.0 Content-Type: text/plain; charset=us-ascii Injection-Info: blaine.gmane.org; posting-host="blaine.gmane.org:195.159.176.226"; logging-data="238099"; mail-complaints-to="usenet@blaine.gmane.org" User-Agent: Mutt/1.10.1 (2018-07-13) To: musl@lists.openwall.com Original-X-From: musl-return-13862-gllmg-musl=m.gmane.org@lists.openwall.com Fri Feb 22 06:05:55 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 1gx322-000zpK-SX for gllmg-musl@m.gmane.org; Fri, 22 Feb 2019 06:05:54 +0100 Original-Received: (qmail 30719 invoked by uid 550); 22 Feb 2019 05:05:52 -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 30696 invoked from network); 22 Feb 2019 05:05:52 -0000 Content-Disposition: inline In-Reply-To: X-Provags-ID: V03:K1:btSO+MB+nrobnkIkyNivsVaGEkpInDlJCzVF8+nzCYdG1peKlsK jFG51ZCW7s24PGPFlKQuLitXC4Il54uC/iN4xRS0W7YytHJ5I/Bru3+vT72BWzv6QRRCIDo 83oNQryGzUUaNkfDaSAS50bqCem/NolEb9VjPTP67KtMxPng9sTRFjgGIjOyD2o5TbChoso aktVE3RZkrYP7CfgakKdA== X-UI-Out-Filterresults: notjunk:1;V03:K0:ocRfuIfHNQM=:eyTAowyV3ET+KgZJtlgqPK Uom1Cg3ZgLu4s50ZZgQ5uR85rfvrh4Ay8JBNGmxndAIriXfzRdVyXV6YPMfAf6cLo4iBVZXqI xp/4E1FkH6QtTTUWHfdIy/zJ21aqcgnefUAiP2e8lwDX/UtXYmXrkH3l9S9IzvgSRE1/CREKM 3B+ETc0jiSkL/2uzofUgrcQ2G6qCq+4Jb1TAzLeqILvHQ6uN/QKg/3ACJkfuu5SR6kHShNG// xCN04elxnV0w3Vp6UOo2IopqRF8ip6HrLnz31zuMouonbGhD1qiZoMSv+t6i3wX33jvX0GpuT QRhbotfwn8cKGoIMcG0ulMVwtPAHQbddnQlInRvrx/OlCBz7UFPLAguyZ723WFxDpySL12ZKI JWYmKJeE5Ese8rhoDGaoq2ddPNA8UjvdSyhglPPCqZa9MfSSIckIZhHAjysuc3lDix6899EDS DvLbAVQyGtonKvSPCxQX/XVxtmpFhDZX6zQwOu0iLHp97/pzgqBpaZ/UvkmJyRNRVe1IGASOl zfdaco+bOpAloF01asU2Uv21kp1sHsMFb6erX4ziEQA5b9tjTMUEbiAt+XTsYJczs5rAvx8Fj o+Ja5kSYm+UcsQYkAtTyfnyTp3aFSHGPp/ZVBA9Ic+F1h5y+op+/179//3sbU3q//zVVPiGND MF9q1QMgn4FT4ELNVXtuUhZDNEDqZRM3gxIhThXGYm53W9nKwbTeiJTeupj3D6Ngnpwf9f9Yc sGSXv4nbby7Km7oMKaoDNhadT1GEp5961fCI4tkg7dcvPVcDYwBl37SA87MMSgwHPXrBXi3Y Xref: news.gmane.org gmane.linux.lib.musl.general:13846 Archived-At: 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