File: dumbask.c

package info (click to toggle)
libdumb 1%3A0.9.3-6
  • links: PTS, VCS
  • area: main
  • in suites: bookworm, bullseye, buster, jessie, jessie-kfreebsd, sid, stretch, trixie
  • size: 1,056 kB
  • ctags: 1,270
  • sloc: ansic: 9,403; makefile: 290; sh: 23
file content (32 lines) | stat: -rw-r--r-- 716 bytes parent folder | download | duplicates (5)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
#include <stdio.h>
#include <ctype.h>


int main(int argc, const char *const argv[])
{
	const char *message = argv[1];
	const char *options;

	if (!message) {
		fprintf(stderr,
			"dumbask: asks the user a question.\n"
			"Specify a message as the first argument (quoted!).\n"
			"You may optionally specify the choices as the second argument.\n"
			"Default choices are YN. Exit code is 0 for first, 1 for second, etc.\n");
		return 0;
	}

	options = argv[2] ? : "YN"; /* I _had_ to use a GNU Extension _somewhere_! */

	printf("%s", argv[1]);

	for (;;) {
		char c = getchar();
		if (c == EOF) return 0;
		c = toupper(c);
		int i;
		for (i = 0; options[i]; i++)
			if (c == toupper(options[i]))
				return i;
	}
}