git/list[1] front-page[2] threads[3] people[4] search[5] about
 

Re: [PATCH] Avoid crippled getpass function on Solaris

From
Jeff King <peff@peff.net>
Date
Aug 7, 2012, 00:23 UTC
Message-ID
<20120807002318.GA17498@sigill.intra.peff.net>
In-Reply-To
<m2pq73zkmu.fsf@igel.home>
On Tue, Aug 07, 2012 at 01:05:45AM +0200, Andreas Schwab wrote:
Show 27 quoted lines
> > The stdio behavior on Solaris is weird. If I run this sample program:
> >
> >   #include <stdio.h>
> >   int main(void)
> >   {
> >     FILE *fh = fopen("/dev/tty", "w+");
> >     char buf[32] = {0};
> >     fgets(buf, sizeof(buf), fh);
> >     fprintf(fh, "got %s\n", buf);
> >     return 0;
> >   }
> >
> > on Linux, I get:
> >
> >   $ ./a.out
> >   foo        <-- me typing
> >   got foo    <-- program output
> >
> > On Solaris, I get:
> >
> >   $ ./a.out
> >   foo        <-- me typing
> >   foo        <-- ???
> >   got foo    <-- program output
> 
> That's not a bug, you need to flush or seek when you want to switch
> between read to write.

Thanks. Inserting an fflush() before the fprintf does fix it, but I think that a flush is disallowed by the standard in this case. From C99, 7.19.5.2 (fflush):

  If stream points to an output stream or an update stream in which the
  most recent operation was not input, the fflush function causes any
  unwritten data for that stream to be delivered to the host environment
  to be written to the file; otherwise, the behavior is undefined.
And later, from 7.19.5.3 (fopen):
  When a file is opened with update mode ('+' as the second or third
  character in the above list of mode argument values), both input and
  output may be performed on the associated stream. However, output
  shall not be directly followed by input without an intervening call to
  the fflush function or to a file positioning function (fseek, fsetpos,
  or rewind), and input shall not be directly followed by output without
  an intervening call to a file positioning function, unless the input
  operation encounters end-of-file.

I don't know if any implementation actually cares in practice, of course, but probably the sane thing would be to call "fseek(fh, SEEK_CUR, 0)" before the fprintf.

This is all moot if we end up ripping stdio out of this code for other reasons, but it does give us another option for a fix.

Thanks for the pointer.
-Peff
Previous: Andreas SchwabNext: Jeff King
Message 17 of 27 in “Avoid crippled getpass function on Solaris”
  1. Avoid crippled getpass function on SolarisBen Walton, Aug 5, 2012
  2. Tay Ray ChuanAug 6, 2012
  3. Ben WaltonAug 6, 2012
  4. Junio C HamanoAug 6, 2012
  5. Ben WaltonAug 6, 2012
  6. Jeff KingAug 6, 2012
  7. Junio C HamanoAug 6, 2012
  8. Ben WaltonAug 6, 2012
  9. Jeff KingAug 6, 2012
  10. Ben WaltonAug 6, 2012
  11. Jeff KingAug 6, 2012
  12. Ben WaltonAug 6, 2012
  13. Jeff KingAug 6, 2012
  14. Ben WaltonAug 6, 2012
  15. Jeff KingAug 7, 2012
  16. Andreas SchwabAug 6, 2012
  17. Jeff KingAug 7, 2012
  18. Jeff KingAug 7, 2012
  19. Tay Ray ChuanAug 7, 2012
  20. Ben WaltonAug 7, 2012
  21. Enable HAVE_DEV_TTY for SolarisBen Walton, Aug 7, 2012
  22. Junio C HamanoAug 7, 2012
  23. Jeff KingAug 7, 2012
  24. Jeff KingAug 7, 2012
  25. Ben WaltonAug 7, 2012
  26. Erik Faye-LundAug 8, 2012
  27. Jeff KingAug 8, 2012

Read the whole thread, see it on lore, or plain text.

$ cat FOOTERMessages come from the public archive at lore.kernel.org/git, fetched every hour. The front page is chosen and written each morning by an AI editor and can be wrong; the threads themselves are the record. About and API. For agents: an MCP server at https://gitlist.dev/mcp, and any thread, story or person page as Markdown by adding .md to its URL (or sending Accept: text/markdown). Details in /llms.txt.