xref: /freebsd/contrib/bsddialog/examples_library/datebox.c (revision 13ec1e3155c7e9bf037b12af186351b7fa9b9450)
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 	if (bsddialog_init() == BSDDIALOG_ERROR) {
32 		printf("Error: %s\n", bsddialog_geterror());
33 		return (1);
34 	}
35 
36 	bsddialog_initconf(&conf);
37 	conf.title = "datebox";
38 	conf.bottomtitle = "Press TAB and arrows";
39 	output = bsddialog_datebox(&conf, "Example", 10, 50, &yy, &mm, &dd);
40 
41 	bsddialog_end();
42 
43 	switch (output) {
44 	case BSDDIALOG_OK:
45 		printf("Date: %u/%u/%u", yy, mm, dd);
46 		break;
47 	case BSDDIALOG_CANCEL:
48 		printf("Cancel");
49 		break;
50 	case BSDDIALOG_ERROR:
51 		printf("Error: %s", bsddialog_geterror());
52 		break;
53 	}
54 	printf("\n");
55 
56 	return (output);
57 }