xref: /freebsd/contrib/bsddialog/examples_library/datebox.c (revision b214fcceacad6b842545150664bd2695c1c2b34f)
1 /*-
2  * SPDX-License-Identifier: CC0-1.0
3  *
4  * Written in 2021 by Alfonso Sabato Siciliano.
5  * To the extent possible under law, the author has dedicated all copyright
6  * and related and neighboring rights to this software to the public domain
7  * worldwide. This software is distributed without any warranty, see:
8  *   <http://creativecommons.org/publicdomain/zero/1.0/>.
9  */
10 
11 #include <stdio.h>
12 #include <string.h>
13 #include <time.h>
14 
15 #include <bsddialog.h>
16 
17 int main()
18 {
19 	int output;
20 	unsigned int yy, mm, dd;
21 	struct bsddialog_conf conf;
22 	time_t cal;
23 	struct tm *localtm;
24 
25 	time(&cal);
26 	localtm = localtime(&cal);
27 	yy = localtm->tm_year + 1900;
28 	mm = localtm->tm_mon + 1;
29 	dd = localtm->tm_mday;
30 
31 	bsddialog_initconf(&conf);
32 	conf.title = "datebox";
33 	conf.bottomtitle = "Press TAB and arrows";
34 
35 	if (bsddialog_init() < 0)
36 		return -1;
37 
38 	output = bsddialog_datebox(&conf, "Example", 10, 50, &yy, &mm, &dd);
39 
40 	bsddialog_end();
41 
42 	switch (output) {
43 	case BSDDIALOG_OK:
44 		printf("Date: %u/%u/%u", yy, mm, dd);
45 		break;
46 	case BSDDIALOG_ESC:
47 		printf("ESC\n");
48 		break;
49 	case BSDDIALOG_CANCEL:
50 		printf("Cancel");
51 		break;
52 	case BSDDIALOG_ERROR:
53 		printf("Error: %s", bsddialog_geterror());
54 		break;
55 	}
56 	printf("\n");
57 
58 	return output;
59 }
60