Imported Upstream version 1.6.9p6
[debian/sudo] / tgetpass.c
index 0cc287262535c1e9d81010602b80584ce9a7b2e8..6e356cd6d7a9dcfee85f85258c8116c38d13ff67 100644 (file)
@@ -1,5 +1,5 @@
 /*
- * Copyright (c) 1996, 1998-2004 Todd C. Miller <Todd.Miller@courtesan.com>
+ * Copyright (c) 1996, 1998-2005 Todd C. Miller <Todd.Miller@courtesan.com>
  *
  * Permission to use, copy, modify, and distribute this software for any
  * purpose with or without fee is hereby granted, provided that the above
@@ -22,7 +22,7 @@
 # include <floss.h>
 #endif
 
-#include "config.h"
+#include <config.h>
 
 #include <sys/types.h>
 #include <sys/param.h>
@@ -70,7 +70,7 @@
 #include "sudo.h"
 
 #ifndef lint
-static const char rcsid[] = "$Sudo: tgetpass.c,v 1.111 2004/06/06 23:58:11 millert Exp $";
+__unused static const char rcsid[] = "$Sudo: tgetpass.c,v 1.111.2.4 2007/10/08 16:01:10 millert Exp $";
 #endif /* lint */
 
 #ifndef TCSASOFT
@@ -97,7 +97,7 @@ static const char rcsid[] = "$Sudo: tgetpass.c,v 1.111 2004/06/06 23:58:11 mille
 # define TERM                  termios
 # define tflags                        c_lflag
 # define term_getattr(f, t)    tcgetattr(f, t)
-# define term_setattr(f, t)    tcsetattr(f, TCSAFLUSH|TCSASOFT, t)
+# define term_setattr(f, t)    tcsetattr(f, TCSADRAIN|TCSASOFT, t)
 #else
 # ifdef HAVE_TERMIO_H
 # define TERM                  termio
@@ -135,6 +135,9 @@ tgetpass(prompt, timeout, flags)
 
     (void) fflush(stdout);
 restart:
+    signo = 0;
+    pass = NULL;
+    save_errno = 0;
     /* Open /dev/tty for reading/writing if possible else use stdin/stderr. */
     if (ISSET(flags, TGP_STDIN) ||
        (input = output = open(_PATH_TTY, O_RDWR|O_NOCTTY)) == -1) {
@@ -172,17 +175,20 @@ restart:
        memset(&oterm, 0, sizeof(oterm));
     }
 
-    if (prompt)
-       (void) write(output, prompt, strlen(prompt));
+    /* No output if we are already backgrounded. */
+    if (signo != SIGTTOU && signo != SIGTTIN) {
+       if (prompt)
+           (void) write(output, prompt, strlen(prompt));
 
-    if (timeout > 0)
-       alarm(timeout);
-    pass = getln(input, buf, sizeof(buf));
-    alarm(0);
-    save_errno = errno;
+       if (timeout > 0)
+           alarm(timeout);
+       pass = getln(input, buf, sizeof(buf));
+       alarm(0);
+       save_errno = errno;
 
-    if (!ISSET(term.tflags, ECHO))
-       (void) write(output, "\n", 1);
+       if (!ISSET(term.tflags, ECHO))
+           (void) write(output, "\n", 1);
+    }
 
     /* Restore old tty settings and signals. */
     if (memcmp(&term, &oterm, sizeof(term)) != 0)
@@ -208,12 +214,12 @@ restart:
            case SIGTSTP:
            case SIGTTIN:
            case SIGTTOU:
-               signo = 0;
                goto restart;
        }
     }
 
-    errno = save_errno;
+    if (save_errno)
+       errno = save_errno;
     return(pass);
 }