]> diplodocus.org Git - nmh/blob - docs/historical/mh-6.8.5/sbr/gans.c
Always check that mktemp()/mktemp2() succeeds before trying to
[nmh] / docs / historical / mh-6.8.5 / sbr / gans.c
1 /* gans.c - get an answer from the user */
2 #ifndef lint
3 static char ident[] = "@(#)$Id: gans.c,v 1.2 1992/10/26 22:50:52 jromine Exp $";
4 #endif /* lint */
5
6 #include "../h/mh.h"
7 #include <stdio.h>
8
9
10 gans (prompt, ansp)
11 register char *prompt;
12 register struct swit *ansp;
13 {
14 register int i;
15 register char *cp;
16 register struct swit *ap;
17 char ansbuf[BUFSIZ];
18
19 for (;;) {
20 printf ("%s", prompt);
21 (void) fflush (stdout);
22 cp = ansbuf;
23 while ((i = getchar ()) != '\n') {
24 if (i == EOF)
25 return 0;
26 if (cp < &ansbuf[sizeof ansbuf - 1]) {
27 #ifdef LOCALE
28 i = (isalpha(i) && isupper(i)) ? tolower(i) : i;
29 #else
30 if (i >= 'A' && i <= 'Z')
31 i += 'a' - 'A';
32 #endif
33 *cp++ = i;
34 }
35 }
36 *cp = 0;
37 if (ansbuf[0] == '?' || cp == ansbuf) {
38 printf ("Options are:\n");
39 for (ap = ansp; ap -> sw; ap++)
40 printf (" %s\n", ap -> sw);
41 continue;
42 }
43 if ((i = smatch (ansbuf, ansp)) < 0) {
44 printf ("%s: %s.\n", ansbuf, i == -1 ? "unknown" : "ambiguous");
45 continue;
46 }
47 return i;
48 }
49 }