summaryrefslogtreecommitdiff
path: root/examples/strtoi/prog.c
blob: 04175774ca9253b5f5fde054438b0a24af9bbfb0 (plain)
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
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
#include <stdio.h>
#include <string.h>
#include <inttypes.h>
#include <errno.h>

#include <mkc_getline.h>
#include <mkc_strtoi.h>
#include <mkc_strtou.h>

int main (int argc, char ** argv)
{
	char *buf = NULL;
	size_t size = 0;
	ssize_t len = 0;
	intmax_t sval;
	uintmax_t uval;
	int rstatus;

	while (len = getline(&buf, &size, stdin), len != -1){
		if (len > 0 && buf[len-1] == '\n')
			buf[len - 1] = 0;

		/* strtoi */
		printf("strtoi: ");
		sval = strtoi(buf, NULL, 10, -99, 99, &rstatus);
		switch (rstatus){
			case ECANCELED:
				puts("ECANCELED");
				break;
			case EINVAL:
				puts("EINVAL");
				break;
			case ENOTSUP:
				puts("ENOTSUP");
				break;
			case ERANGE:
				puts("ERANGE");
				break;
			case 0:
				printf("%" PRIdMAX "\n", sval);
				break;
			default:
				abort();
		}

		/* strtoi */
		printf("strtou: ");
		uval = strtou(buf, NULL, 10, 0, 99, &rstatus);
		switch (rstatus){
			case ECANCELED:
				puts("ECANCELED");
				break;
			case EINVAL:
				puts("EINVAL");
				break;
			case ENOTSUP:
				puts("ENOTSUP");
				break;
			case ERANGE:
				puts("ERANGE");
				break;
			case 0:
				printf("%" PRIuMAX "\n", uval);
				break;
			default:
				abort();
		}
	}

	return 0;
}