don't do a strlen() if length is provided
[unix-history] / usr / src / games / monop / getinp.c
CommitLineData
ec5c7841 1/*
d99e6414 2 * Copyright (c) 1980 Regents of the University of California.
ec5c7841
KB
3 * All rights reserved.
4 *
102fca3d 5 * %sccs.include.redist.c%
ec5c7841
KB
6 */
7
8#ifndef lint
786b3585 9static char sccsid[] = "@(#)getinp.c 5.4 (Berkeley) %G%";
ec5c7841
KB
10#endif /* not lint */
11
12# include <stdio.h>
13# include <ctype.h>
14
15# define reg register
16
17# define LINE 70
18
19static char buf[257];
20
21getinp(prompt, list)
22char *prompt, *list[]; {
23
24 reg int i, n_match, match;
25 char *sp;
26 int plen;
786b3585 27 static int comp();
ec5c7841
KB
28
29 for (;;) {
30inter:
31 printf(prompt);
32 for (sp = buf; (*sp=getchar()) != '\n'; )
33 if (*sp == -1) /* check for interupted system call */
34 goto inter;
35 else if (sp != buf || *sp != ' ')
36 sp++;
37 if (buf[0] == '?' && buf[1] == '\n') {
38 printf("Valid inputs are: ");
39 for (i = 0, match = 18; list[i]; i++) {
40 if ((match+=(n_match=strlen(list[i]))) > LINE) {
41 printf("\n\t");
42 match = n_match + 8;
43 }
44 if (*list[i] == '\0') {
45 match += 8;
46 printf("<RETURN>");
47 }
48 else
49 printf(list[i]);
50 if (list[i+1])
51 printf(", ");
52 else
53 putchar('\n');
54 match += 2;
55 }
56 continue;
57 }
58 *sp = '\0';
59 for (sp = buf; *sp; sp++)
60 if (isupper(*sp))
61 *sp = tolower(*sp);
62 for (i = n_match = 0; list[i]; i++)
63 if (comp(list[i])) {
64 n_match++;
65 match = i;
66 }
67 if (n_match == 1)
68 return match;
69 else if (buf[0] != '\0')
70 printf("Illegal response: \"%s\". Use '?' to get list of valid answers\n", buf);
71 }
72}
73
74static
75comp(s1)
76char *s1; {
77
78 reg char *sp, *tsp, c;
79
80 if (buf[0] != '\0')
81 for (sp = buf, tsp = s1; *sp; ) {
82 c = isupper(*tsp) ? tolower(*tsp) : *tsp;
83 tsp++;
84 if (c != *sp++)
85 return 0;
86 }
87 else if (*s1 != '\0')
88 return 0;
89 return 1;
90}