Path: csiph.com!optima2.xanadu-bbs.net!xanadu-bbs.net!news.glorb.com!usenet.stanford.edu!not-for-mail From: Eric Blake Newsgroups: gnu.bash.bug Subject: Re: Why does a Bash shell script write prompts followed by reads and do it right? Date: Wed, 2 Sep 2015 14:55:25 -0600 Organization: Red Hat, Inc. Lines: 66 Approved: bug-bash@gnu.org Message-ID: References: <1441218426.1517.2.camel@16bits.net> <20150902141935720946809@bob.proulx.com> NNTP-Posting-Host: lists.gnu.org Mime-Version: 1.0 Content-Type: multipart/signed; micalg=pgp-sha256; protocol="application/pgp-signature"; boundary="uF7Dblg6f6GJBo9DPF7LD5QvH7BveVX6E" X-Trace: usenet.stanford.edu 1441227338 11326 208.118.235.17 (2 Sep 2015 20:55:38 GMT) X-Complaints-To: action@cs.stanford.edu To: Robert Parker , bug-bash@gnu.org Envelope-to: bug-bash@gnu.org Openpgp: url=http://people.redhat.com/eblake/eblake.gpg X-Enigmail-Draft-Status: N1110 User-Agent: Mozilla/5.0 (X11; Linux x86_64; rv:38.0) Gecko/20100101 Thunderbird/38.1.0 In-Reply-To: <20150902141935720946809@bob.proulx.com> X-Scanned-By: MIMEDefang 2.68 on 10.5.11.26 X-detected-operating-system: by eggs.gnu.org: GNU/Linux 3.x X-Received-From: 209.132.183.28 X-BeenThere: bug-bash@gnu.org X-Mailman-Version: 2.1.14 Precedence: list List-Id: Bug reports for the GNU Bourne Again SHell List-Unsubscribe: , List-Archive: List-Post: List-Help: List-Subscribe: , Xref: csiph.com gnu.bash.bug:11471 This is an OpenPGP/MIME signed message (RFC 4880 and 3156) --uF7Dblg6f6GJBo9DPF7LD5QvH7BveVX6E Content-Type: text/plain; charset=utf-8 Content-Transfer-Encoding: quoted-printable On 09/02/2015 02:22 PM, Bob Proulx wrote: > Robert Parker wrote: >> fputs(shortprompt, stdout); >> fflush(stdin); >=20 > Uhm... fflush'ing stdin? That doesn't make sense. There is one case where fflush(stdin) is useful on a terminal: it lets you discard any pending input. For example, if you have just prompted for a password and altered the tty to not echo what is typed, then flushing stdin prior to reading the password is a nice step to ensure that any accidental type-ahead data entered before the prompt doesn't get mixed up with the password attempt, particularly since the lack of echoing won't show the user that the password got munged. Additionally, fflush(stdin) on a regular file is useful for applications that might be used in a sequence of processes, and which have scenarios where they only partially consuming input; POSIX requires that such applications should reset the file pointer to the last byte actually processed so that the next process starts processing at the next byte rather than skipping ahead by however many bytes were cached early but left unprocessed. Example: { sed -n 1q; cat; } < file | ... - for the 'cat' to pass the remaining lines to the rest of the pipeline, the 'sed' must rewind back to the end of the first line even if it read more than that. But in most coding scenarios, flushing stdin is rather rare; it wasn't even well-specified by POSIX until 2012. > Obviously you > meant to flush stdout. And in the code snippet shown here, this observation is certainly correct= =2E --=20 Eric Blake eblake redhat com +1-919-301-3266 Libvirt virtualization library http://libvirt.org --uF7Dblg6f6GJBo9DPF7LD5QvH7BveVX6E Content-Type: application/pgp-signature; name="signature.asc" Content-Description: OpenPGP digital signature Content-Disposition: attachment; filename="signature.asc" -----BEGIN PGP SIGNATURE----- Version: GnuPG v2 Comment: Public key at http://people.redhat.com/eblake/eblake.gpg Comment: Using GnuPG with Thunderbird - http://www.enigmail.net/ iQEcBAEBCAAGBQJV52I9AAoJEKeha0olJ0Nq0j8H/1EK1tYI9uqRShPj72O8iMjX cRTv47rDeFtcz9QRF6WgilrjVunbDB0qQAm3amClQrhVanuAEEUuPiLTXELDsIvw JTmL89bXmoV8onObSUe/ShNlYRFo5N+UnlJdr+1NlwI52aJ1r0MOCyJDOmvYbpVn gLuCMGRs295ip14krJKMWWIF6EWbKi8cmRTXt8HQs+J0QJGmXKAZlypEsk7hgLgi PUrXKtDcKJLjDAIPfs9HgvsL1QoSBV2Tqq/CU3Gk2P9MNe+V1axxfniGIiAlBB4h ZRTouL6lHr2nQs1u8uTNke8VlU0KFA9iXEJUsavPQzeqd4HtBr3jOvxOYnvc2dA= =YiW9 -----END PGP SIGNATURE----- --uF7Dblg6f6GJBo9DPF7LD5QvH7BveVX6E--