bug-gnulib
[Top][All Lists]
Advanced

[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]

[Bug-gnulib] proposed patch for getpass and unlocked-io


From: Paul Eggert
Subject: [Bug-gnulib] proposed patch for getpass and unlocked-io
Date: 08 Oct 2003 01:18:12 -0700
User-agent: Gnus/5.09 (Gnus v5.9.0) Emacs/21.3

I merged getpass.c with glibc and with our discussion and came up with
the following proposed patch to gnulib.  It also ended up requiring
some rewriting of unlocked-io.h.  I've briefly tested this with
coreutils.  Comments welcome.

2003-10-08  Paul Eggert  <address@hidden>

        Merge getpass from libc, plus a few fixes.

        * lib/getpass.c (_LIBC): Define to 0 if not defined.
        (HAVE_STDIO_EXT, HAVE_WCHAR_H) [_LIBC]: Define to 1.
        Include <stdbool.h>.
        Include <stdio_ext.h> if HAVE_STDIO_H, otherwise define
        __fsetlocking to empty.
        [_LIBC]: Do not include "getline.h" or "unlocked-io.h", but
        do include <bits/libc-lock.h>.
        Do not include <fcntl.h>; not needed.
        [HAVE_WCHAR_H]: Include <wchar.h>.
        (NOTCANCEL_MODE): New macro.
        (flockfile, funlockfile) [_LIBC]: New macros.
        (__libc_cleanup_push, __libc_cleanup_pop, __getline, __tcgetattr)
        [!_LIBC]: New macros.
        (USE_IN_LIBIO, __IO_fwide, __fwprintf) [!_LIBC && HAVE_DECL_FWIDE &&
        HAVE_DECL_FWPRINTF]: New macros.
        (call_fclose): New function.
        (getpass): Use it.  Save tty stream separately; this simplifies the
        code and makes it more reliable if stdin happens to equal stdout.
        Invoke __fsetlocking on tty.
        Handle thread cancellation if needed.
        Namespace cleanup (use __tcgetattr, __getline).
        Use bool for Booleans.
        Handle wide streams.
        [!_LIBC]: Unconditionally do the fseek, since we don't know what
        stream might go where.

        * lib/unlocked-io.h: Include <stdio.h>, so that the caller
        doesn't have to include <stdio.h> before us.
        (clearerr_unlocked, feof_unlocked, ferror_unlocked,
        fflush_unlocked, fgets_unlocked, fputc_unlocked, fputs_unlocked,
        fread_unlocked, fwrite_unlocked, getc_unlocked, getchar_unlocked,
        putc_unlocked, putchar_unlocked): Define to the unlocked counterpart
        if not declared, so that we can use getpass.c code from libc without
        rewriting it.
        (flockfile, ftrylockfile, funlockfile): New macros.
        [HAVE_WCHAR_H]: Include <wchar.h>.
        (fputws, fputws_unlocked, putwc, putwc_unlocked) [HAVE_WCHAR_H]:
        New macros, defined similarly to the above.

        * m4/getpass.m4 (gl_PREREQ_GETPASS): Check for stdio_ext.h,
        wchar.h, fwide, fwprintf.

        * m4/unlocked-io.m4 (jm_FUNC_GLIBC_UNLOCKED_IO): Check for
        wchar.h, fputws_unlocked, putwc_unlocked.

        * modules/getpass: Depend on stdbool.

Index: lib/getpass.c
===================================================================
RCS file: /cvsroot/gnulib/gnulib/lib/getpass.c,v
retrieving revision 1.5
diff -p -u -r1.5 getpass.c
--- lib/getpass.c       1 Oct 2003 11:11:02 -0000       1.5
+++ lib/getpass.c       8 Oct 2003 07:51:32 -0000
@@ -19,12 +19,66 @@
 # include <config.h>
 #endif
 
+#ifndef _LIBC
+# define _LIBC 0
+#endif
+
+#if _LIBC
+# define HAVE_STDIO_EXT_H 1
+# define HAVE_WCHAR_H 1
+#endif
+
+#include <stdbool.h>
+
 #include <stdio.h>
+#if HAVE_STDIO_EXT_H
+# include <stdio_ext.h>
+#else
+# define __fsetlocking(stream, type) /* empty */
+#endif
+#if !_LIBC
+# include "getline.h"
+#endif
+
 #include <termios.h>
 #include <unistd.h>
-#include <fcntl.h>
-#include "getline.h"
-#include "unlocked-io.h"
+
+#if HAVE_WCHAR_H
+# include <wchar.h>
+#endif
+
+#if _LIBC
+# define NOTCANCEL_MODE "c"
+#else
+# define NOTCANCEL_MODE
+#endif
+
+#if _LIBC
+# define flockfile(s) _IO_flockfile (s)
+# define funlockfile(s) _IO_funlockfile (s)
+#else
+# include "unlocked-io.h"
+#endif
+
+#if _LIBC
+# include <bits/libc-lock.h>
+#else
+# define __libc_cleanup_push(function, arg) /* empty */
+# define __libc_cleanup_pop(execute) /* empty */
+#endif
+
+#if !_LIBC
+# define __getline getline
+# define __tcgetattr tcgetattr
+
+# if HAVE_DECL_FWIDE && HAVE_DECL_FWPRINTF
+#  define USE_IN_LIBIO 1
+#  ifndef _IO_fwide
+#   define _IO_fwide fwide
+#  endif
+#  define __fwprintf fwprintf
+# endif
+#endif
 
 /* It is desirable to use this bit on systems that have it.
    The only bit of terminal state we want to twiddle is echoing, which is
@@ -35,12 +89,20 @@
 # define TCSASOFT 0
 #endif
 
+static void
+call_fclose (void *arg)
+{
+  if (arg != NULL)
+    fclose (arg);
+}
+
 char *
 getpass (const char *prompt)
 {
+  FILE *tty;
   FILE *in, *out;
   struct termios s, t;
-  int tty_changed;
+  bool tty_changed;
   static char *buf;
   static size_t bufsize;
   ssize_t nread;
@@ -48,18 +110,29 @@ getpass (const char *prompt)
   /* Try to write to and read from the terminal if we can.
      If we can't open the terminal, use stderr and stdin.  */
 
-  in = fopen ("/dev/tty", "w+");
-  if (in == NULL)
+  tty = fopen ("/dev/tty", "w+" NOTCANCEL_MODE);
+  if (tty == NULL)
     {
       in = stdin;
       out = stderr;
     }
   else
-    out = in;
+    {
+      /* We do the locking ourselves.  */
+      __fsetlocking (tty, FSETLOCKING_BYCALLER);
+
+      out = in = tty;
+    }
+
+  /* Make sure the stream we opened is closed even if the thread is
+     canceled.  */
+  __libc_cleanup_push (call_fclose, tty);
+
+  flockfile (out);
 
   /* Turn echoing off if it is on now.  */
 
-  if (tcgetattr (fileno (in), &t) == 0)
+  if (__tcgetattr (fileno (in), &t) == 0)
     {
       /* Save the old one. */
       s = t;
@@ -68,14 +141,19 @@ getpass (const char *prompt)
       tty_changed = (tcsetattr (fileno (in), TCSAFLUSH|TCSASOFT, &t) == 0);
     }
   else
-    tty_changed = 0;
+    tty_changed = false;
 
   /* Write the prompt.  */
-  fputs (prompt, out);
-  fflush (out);
+#ifdef USE_IN_LIBIO
+  if (_IO_fwide (out, 0) > 0)
+    __fwprintf (out, L"%s", prompt);
+  else
+#endif
+    fputs_unlocked (prompt, out);
+  fflush_unlocked (out);
 
   /* Read the password.  */
-  nread = getline (&buf, &bufsize, in);
+  nread = __getline (&buf, &bufsize, in);
   if (buf != NULL)
     {
       if (nread < 0)
@@ -86,16 +164,25 @@ getpass (const char *prompt)
          buf[nread - 1] = '\0';
          if (tty_changed)
            {
-             /* Write the newline that was not echoed.
-                But before doing that, do a no-op fseek.  According to the C
-                standard, input may not be followed by output on the same
-                stream without an intervening call to a file positioning
-                function.  Without this fseek() call, on Solaris, HP-UX,
-                AIX, OSF/1, the previous input gets echoed, whereas on IRIX,
-                the following newline is not output as it should.  */
-             if (out == in)
-               fseek (out, 0, SEEK_CUR);
-             putc ('\n', out);
+             if (!_LIBC)
+               {
+                 /* Do a no-op fseek.  According to the C standard,
+                    input may not be followed by output on the same
+                    stream without an intervening call to a file
+                    positioning function.  Without this fseek() call,
+                    on Solaris, HP-UX, AIX, OSF/1, the previous input
+                    gets echoed, whereas on IRIX, the following
+                    newline is not output as it should.  */
+                 fseek (out, 0, SEEK_CUR);
+               }
+
+             /* Write the newline that was not echoed.  */
+#ifdef USE_IN_LIBIO
+             if (_IO_fwide (out, 0) > 0)
+               putwc_unlocked (L'\n', out);
+             else
+#endif
+               putc_unlocked ('\n', out);
            }
        }
     }
@@ -104,9 +191,11 @@ getpass (const char *prompt)
   if (tty_changed)
     (void) tcsetattr (fileno (in), TCSAFLUSH|TCSASOFT, &s);
 
-  if (in != stdin)
-    /* We opened the terminal; now close it.  */
-    fclose (in);
+  funlockfile (out);
+
+  __libc_cleanup_pop (0);
+
+  call_fclose (tty);
 
   return buf;
 }
Index: lib/unlocked-io.h
===================================================================
RCS file: /cvsroot/gnulib/gnulib/lib/unlocked-io.h,v
retrieving revision 1.2
diff -p -u -r1.2 unlocked-io.h
--- lib/unlocked-io.h   14 Jul 2003 16:36:07 -0000      1.2
+++ lib/unlocked-io.h   8 Oct 2003 07:51:32 -0000
@@ -27,63 +27,126 @@
 
 # if USE_UNLOCKED_IO
 
-/* These are wrappers for functions/macros from GNU libc.
+/* These are wrappers for functions/macros from the GNU C library, and
+   from other C libraries supporting POSIX's optional thread-safe functions.
+
    The standard I/O functions are thread-safe.  These *_unlocked ones are
    more efficient but not thread-safe.  That they're not thread-safe is
-   fine since all of the applications in this package are single threaded.  */
+   fine since all of the applications in this package are single threaded.
+
+   Also, some code that is shared with the GNU C library may invoke
+   the *_unlocked functions directly.  On hosts that lack those
+   functions, invoke the non-thread-safe versions instead.  */
+
+#  include <stdio.h>
 
 #  if HAVE_DECL_CLEARERR_UNLOCKED
 #   undef clearerr
 #   define clearerr(x) clearerr_unlocked (x)
+#  else
+#   define clearerr_unlocked(x) clearerr (x)
 #  endif
 #  if HAVE_DECL_FEOF_UNLOCKED
 #   undef feof
 #   define feof(x) feof_unlocked (x)
+#  else
+#   define feof_unlocked(x) feof (x)
 #  endif
 #  if HAVE_DECL_FERROR_UNLOCKED
 #   undef ferror
 #   define ferror(x) ferror_unlocked (x)
+#  else
+#   define ferror_unlocked(x) ferror (x)
 #  endif
 #  if HAVE_DECL_FFLUSH_UNLOCKED
 #   undef fflush
 #   define fflush(x) fflush_unlocked (x)
+#  else
+#   define fflush_unlocked(x) fflush (x)
 #  endif
 #  if HAVE_DECL_FGETS_UNLOCKED
 #   undef fgets
 #   define fgets(x,y,z) fgets_unlocked (x,y,z)
+#  else
+#   define fgets_unlocked(x,y,z) fgets (x,y,z)
 #  endif
 #  if HAVE_DECL_FPUTC_UNLOCKED
 #   undef fputc
 #   define fputc(x,y) fputc_unlocked (x,y)
+#  else
+#   define fputc_unlocked(x,y) fputc (x,y)
 #  endif
 #  if HAVE_DECL_FPUTS_UNLOCKED
 #   undef fputs
 #   define fputs(x,y) fputs_unlocked (x,y)
+#  else
+#   define fputs_unlocked(x, y) fputs (x, y)
 #  endif
 #  if HAVE_DECL_FREAD_UNLOCKED
 #   undef fread
 #   define fread(w,x,y,z) fread_unlocked (w,x,y,z)
+#  else
+#   define fread_unlocked(w,x,y,z) fread (w,x,y,z)
 #  endif
 #  if HAVE_DECL_FWRITE_UNLOCKED
 #   undef fwrite
 #   define fwrite(w,x,y,z) fwrite_unlocked (w,x,y,z)
+#  else
+#   define fwrite_unlocked(w,x,y,z) fwrite (w,x,y,z)
 #  endif
 #  if HAVE_DECL_GETC_UNLOCKED
 #   undef getc
 #   define getc(x) getc_unlocked (x)
+#  else
+#   define getc_unlocked(x) getc (x)
 #  endif
 #  if HAVE_DECL_GETCHAR_UNLOCKED
 #   undef getchar
 #   define getchar() getchar_unlocked ()
+#  else
+#   define getchar_unlocked() getchar ()
 #  endif
 #  if HAVE_DECL_PUTC_UNLOCKED
 #   undef putc
 #   define putc(x,y) putc_unlocked (x,y)
+#  else
+#   define putc_unlocked(x, y) putc (x, y)
 #  endif
 #  if HAVE_DECL_PUTCHAR_UNLOCKED
 #   undef putchar
 #   define putchar(x) putchar_unlocked (x)
+#  else
+#   define putchar_unlocked(x) putchar (x)
 #  endif
 
+#  undef flockfile
+#  define flockfile(x) ((void) 0)
+
+#  undef ftrylockfile
+#  define ftrylockfile(x) 0
+
+#  undef funlockfile
+#  define funlockfile(x) ((void) 0)
+
+
+#  if HAVE_WCHAR_H
+
+#   include <wchar.h>
+
+#   if HAVE_DECL_FPUTWS_UNLOCKED
+#    undef fputws
+#    define fputws(x) fputws_unlocked (x)
+#   else
+#    define fputws_unlocked(x) fputws (x)
+#   endif
+
+#   if HAVE_DECL_PUTWC_UNLOCKED
+#    undef putwc
+#    define putwc(x) putwc_unlocked (x)
+#   else
+#    define putwc_unlocked(x) putwc (x)
+#   endif
+
+#  endif /* HAVE_WCHAR_H */
 # endif /* USE_UNLOCKED_IO */
 #endif /* UNLOCKED_IO_H */
Index: m4/getpass.m4
===================================================================
RCS file: /cvsroot/gnulib/gnulib/m4/getpass.m4,v
retrieving revision 1.3
diff -p -u -r1.3 getpass.m4
--- m4/getpass.m4       31 Jul 2003 14:51:31 -0000      1.3
+++ m4/getpass.m4       8 Oct 2003 07:51:33 -0000
@@ -31,6 +31,8 @@ AC_DEFUN([gl_FUNC_GETPASS_GNU],
 
 # Prerequisites of lib/getpass.c.
 AC_DEFUN([gl_PREREQ_GETPASS], [
+  AC_CHECK_HEADERS_ONCE(stdio_ext.h wchar.h)
+  AC_CHECK_DECLS([fwide, fwprintf],,, [#include <wchar.h>])
   :
 ])
 
Index: m4/unlocked-io.m4
===================================================================
RCS file: /cvsroot/gnulib/gnulib/m4/unlocked-io.m4,v
retrieving revision 1.2
diff -p -u -r1.2 unlocked-io.m4
--- m4/unlocked-io.m4   8 Aug 2003 20:15:52 -0000       1.2
+++ m4/unlocked-io.m4   8 Oct 2003 07:51:33 -0000
@@ -19,4 +19,7 @@ AC_DEFUN([jm_FUNC_GLIBC_UNLOCKED_IO],
       fflush_unlocked fgets_unlocked fputc_unlocked fputs_unlocked
       fread_unlocked fwrite_unlocked getc_unlocked
       getchar_unlocked putc_unlocked putchar_unlocked])
+
+  AC_CHECK_HEADERS_ONCE(wchar.h)
+  AC_CHECK_DECLS([fputws_unlocked, putwc_unlocked],,, [#include <wchar.h>])
 ])
Index: modules/getpass
===================================================================
RCS file: /cvsroot/gnulib/gnulib/modules/getpass,v
retrieving revision 1.3
diff -p -u -r1.3 getpass
--- modules/getpass     20 Jan 2003 10:02:37 -0000      1.3
+++ modules/getpass     8 Oct 2003 07:51:33 -0000
@@ -8,6 +8,7 @@ m4/getpass.m4
 Depends-on:
 unlocked-io
 getline
+stdbool
 
 configure.ac:
 gl_FUNC_GETPASS




reply via email to

[Prev in Thread] Current Thread [Next in Thread]