386BSD 0.1 development
[unix-history] / usr / othersrc / contrib / isode / compat / getpassword.c
CommitLineData
48435ab0
WJ
1/* getpassword.c - generic read-the-password-from-the-tty */
2
3#ifndef lint
4static char *rcsid = "$Header: /f/osi/compat/RCS/getpassword.c,v 7.2 91/02/22 09:15:11 mrose Interim $";
5#endif
6
7/*
8 * $Header: /f/osi/compat/RCS/getpassword.c,v 7.2 91/02/22 09:15:11 mrose Interim $
9 *
10 *
11 * $Log: getpassword.c,v $
12 * Revision 7.2 91/02/22 09:15:11 mrose
13 * Interim 6.8
14 *
15 * Revision 7.1 90/11/21 11:29:39 mrose
16 * sun
17 *
18 * Revision 7.0 89/11/23 21:23:01 mrose
19 * Release 6.0
20 *
21 */
22
23/*
24 * NOTICE
25 *
26 * Acquisition, use, and distribution of this module and related
27 * materials are subject to the restrictions of a license agreement.
28 * Consult the Preface in the User's Manual for the full terms of
29 * this agreement.
30 *
31 */
32
33
34/* LINTLIBRARY */
35
36#include <signal.h>
37#include <stdio.h>
38#include "general.h"
39#include "manifest.h"
40#ifdef BSD42
41#include <sys/file.h>
42#endif
43#ifdef SYS5
44#include <fcntl.h>
45#include <termio.h>
46#endif
47#include <sys/ioctl.h>
48
49
50#ifdef BSD44
51char *getpass ();
52#endif
53
54/* \f */
55
56/* roll our own since want to get past UNIX's limit of 8 octets... */
57
58char *getpassword (prompt)
59char *prompt;
60{
61#ifndef BSD44
62 register int c;
63 int flags,
64 isopen;
65 register char *bp,
66 *ep;
67#if !defined(SYS5) && !defined(XOS_2)
68 struct sgttyb sg;
69#else
70 struct termio sg;
71#endif
72 SFP istat;
73 FILE *fp;
74 static char buffer[BUFSIZ];
75
76#ifdef SUNLINK_7_0
77 fp = stdin, isopen = 0; /* will help greatly to work off a script */
78#else
79 if ((c = open ("/dev/tty", O_RDWR)) != NOTOK && (fp = fdopen (c, "r")))
80 setbuf (fp, NULLCP), isopen = 1;
81 else {
82 if (c != NOTOK)
83 (void) close (c);
84
85 fp = stdin, isopen = 0;
86 }
87#endif
88
89 istat = signal (SIGINT, SIG_IGN);
90
91#if !defined(SYS5) && !defined(XOS_2)
92 (void) gtty (fileno (fp), &sg);
93 flags = sg.sg_flags;
94 sg.sg_flags &= ~ECHO;
95 (void) stty (fileno (fp), &sg);
96#else
97 (void) ioctl (fileno (fp), TCGETA, (char *) &sg);
98 flags = sg.c_lflag;
99 sg.c_lflag &= ~ECHO;
100 (void) ioctl (fileno (fp), TCSETAW, (char *) &sg);
101#endif
102
103#ifdef SUNLINK_7_0
104 fprintf (stdout, "%s", prompt);
105 (void) fflush (stdout);
106#else
107 fprintf (stderr, "%s", prompt);
108 (void) fflush (stderr);
109#endif
110
111 for (ep = (bp = buffer) + sizeof buffer - 1; (c = getc (fp)) != EOF;)
112#ifndef apollo
113 if (c == '\n')
114#else
115 if (c == '\n' || c == '\r')
116#endif
117 break;
118 else
119 if (bp < ep)
120 *bp++ = c;
121 *bp = NULL;
122
123#ifdef SUNLINK_7_0
124 fprintf (stdout, "\n");
125 (void) fflush (stdout);
126#else
127 fprintf (stderr, "\n");
128 (void) fflush (stderr);
129#endif
130
131#if !defined(SYS5) && !defined(XOS_2)
132 sg.sg_flags = flags;
133 (void) stty (fileno (fp), &sg);
134#else
135 sg.c_lflag = flags;
136 (void) ioctl (fileno (fp), TCSETAW, (char *) &sg);
137#endif
138
139 (void) signal (SIGINT, istat);
140
141 if (isopen)
142 (void) fclose (fp);
143
144 return buffer;
145#else
146 return getpass (prompt);
147#endif
148}