__ goes away
[unix-history] / usr / src / lib / libc / gen / getpass.c
CommitLineData
843a841b
KB
1/*
2 * Copyright (c) 1988 The Regents of the University of California.
3 * All rights reserved.
4 *
269a7923 5 * %sccs.include.redist.c%
843a841b
KB
6 */
7
2ce81398 8#if defined(LIBC_SCCS) && !defined(lint)
3149d508 9static char sccsid[] = "@(#)getpass.c 5.10 (Berkeley) %G%";
843a841b 10#endif /* LIBC_SCCS and not lint */
b8f253e8 11
c40ae03a 12#include <sys/termios.h>
843a841b 13#include <sys/signal.h>
3149d508
KB
14
15#include <paths.h>
16#include <pwd.h>
6a4d2ab3 17#include <stdio.h>
c5980113 18#include <unistd.h>
6a4d2ab3
BJ
19
20char *
21getpass(prompt)
c5980113 22 const char *prompt;
6a4d2ab3 23{
c40ae03a 24 struct termios term;
843a841b 25 register int ch;
6a4d2ab3 26 register char *p;
843a841b
KB
27 FILE *fp, *outfp;
28 long omask;
c40ae03a
MT
29 int echo;
30 static char buf[_PASSWORD_LEN + 1];
6a4d2ab3 31
843a841b
KB
32 /*
33 * read and write to /dev/tty if possible; else read from
34 * stdin and write to stderr.
35 */
3149d508 36 if ((outfp = fp = fopen(_PATH_TTY, "w+")) == NULL) {
843a841b
KB
37 outfp = stderr;
38 fp = stdin;
6a4d2ab3 39 }
c40ae03a
MT
40 /*
41 * note - blocking signals isn't necessarily the
42 * right thing, but we leave it for now.
43 */
44 omask = sigblock(sigmask(SIGINT)|sigmask(SIGTSTP));
45 (void)tcgetattr(fileno(fp), &term);
46 if (echo = (term.c_lflag & ECHO)) {
47 term.c_lflag &= ~ECHO;
942b1a34 48 (void)tcsetattr(fileno(fp), TCSAFLUSH|TCSASOFT, &term);
c40ae03a 49 }
5f797d54 50 (void)fputs(prompt, outfp);
843a841b
KB
51 rewind(outfp); /* implied flush */
52 for (p = buf; (ch = getc(fp)) != EOF && ch != '\n';)
c40ae03a 53 if (p < buf + _PASSWORD_LEN)
843a841b 54 *p++ = ch;
6a4d2ab3 55 *p = '\0';
843a841b 56 (void)write(fileno(outfp), "\n", 1);
c40ae03a
MT
57 if (echo) {
58 term.c_lflag |= ECHO;
3149d508 59 (void)tcsetattr(fileno(fp), TCSAFLUSH|TCSASOFT, &term);
c40ae03a 60 }
843a841b
KB
61 (void)sigsetmask(omask);
62 if (fp != stdin)
63 (void)fclose(fp);
64 return(buf);
6a4d2ab3 65}