xref: /openbsd/gnu/usr.bin/cvs/vms/getpass.c (revision 78b63d65)
1 /* This program is free software; you can redistribute it and/or modify
2    it under the terms of the GNU General Public License as published by
3    the Free Software Foundation; either version 2, or (at your option)
4    any later version.
5 
6    This program is distributed in the hope that it will be useful,
7    but WITHOUT ANY WARRANTY; without even the implied warranty of
8    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
9    GNU General Public License for more details.  */
10 
11 #undef TEST
12 
13 #include <stdio.h>
14 #include <iodef.h>
15 #include <descrip.h>
16 #include <starlet.h>
17 #include <string.h>
18 
19 #ifdef TEST
20 #include <stdlib.h>
21 static void error (int, int, char *);
22 #else
23 #  include "cvs.h"
24 #endif
25 
26 char *
27 getpass (char *prompt)
28 {
29     int status;
30     unsigned short chan;
31     static $DESCRIPTOR (sys_command, "SYS$COMMAND");
32     unsigned short iosb[4];
33     /* Arbitrary limit.  It doesn't seem worth going through multiple
34        SYS$QIOW calls and who knows what to get rid of it, I don't
35        think.  */
36     static char buf[2048];
37 
38     /* Try to ensure that we avoid stepping on whatever output has
39        been sent to stdout.  */
40     printf ("\n");
41     fflush (stdout);
42 
43     status = sys$assign (&sys_command, &chan, 0, 0);
44     if (!(status & 1))
45 	error (1, 0, "sys$assign failed in getpass");
46     status = sys$qiow (0, chan, IO$_READPROMPT | IO$M_NOECHO, &iosb, 0, 0,
47 		       buf, sizeof (buf) - 1, 0, 0, prompt, strlen (prompt));
48     if (!(status & 1))
49 	error (1, 0, "sys$qiow failed in getpass");
50     if (!(iosb[0] & 1))
51 	error (1, 0, "sys$qiow (iosb) failed in getpass");
52     buf[iosb[1]] = '\0';
53     status = sys$dassgn (chan);
54     if (!(status & 1))
55 	error (0, 0, "sys$dassgn failed in getpass");
56     /* Since there is no echo, we better go to the next line ourselves.  */
57     printf ("\n");
58     return buf;
59 }
60 
61 #ifdef TEST
62 int
63 main ()
64 {
65     printf ("thank you for saying \"%s\"\n", getpass ("What'll it be? "));
66     return 0;
67 }
68 
69 static void error (int x, int y, char *msg)
70 {
71     printf ("error: %s\n", msg);
72     if (x)
73         exit (EXIT_FAILURE);
74 }
75 #endif
76