xref: /freebsd/usr.bin/calendar/locale.c (revision cc68614da8232d8baaca0ae0d0dd8f890f06623e)
1 /*-
2  * SPDX-License-Identifier: BSD-3-Clause
3  *
4  * Copyright (c) 1989, 1993, 1994
5  *	The Regents of the University of California.  All rights reserved.
6  *
7  * Redistribution and use in source and binary forms, with or without
8  * modification, are permitted provided that the following conditions
9  * are met:
10  * 1. Redistributions of source code must retain the above copyright
11  *    notice, this list of conditions and the following disclaimer.
12  * 2. Redistributions in binary form must reproduce the above copyright
13  *    notice, this list of conditions and the following disclaimer in the
14  *    documentation and/or other materials provided with the distribution.
15  * 3. Neither the name of the University nor the names of its contributors
16  *    may be used to endorse or promote products derived from this software
17  *    without specific prior written permission.
18  *
19  * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
20  * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
21  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
22  * ARE DISCLAIMED.  IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
23  * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
24  * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
25  * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
26  * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
27  * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
28  * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
29  * SUCH DAMAGE.
30  */
31 
32 #include <sys/cdefs.h>
33 __FBSDID("$FreeBSD$");
34 
35 #include <ctype.h>
36 #include <err.h>
37 #include <stdio.h>
38 #include <stdlib.h>
39 #include <string.h>
40 #include <time.h>
41 
42 #include "calendar.h"
43 
44 const char *fdays[] = {
45 	"Sunday", "Monday", "Tuesday", "Wednesday", "Thursday", "Friday",
46 	"Saturday", NULL,
47 };
48 
49 const char *days[] = {
50 	"Sun", "Mon", "Tue", "Wed", "Thu", "Fri", "Sat", NULL,
51 };
52 
53 const char *fmonths[] = {
54 	"January", "February", "March", "April", "May", "June", "Juli",
55 	"August", "September", "October", "November", "December", NULL,
56 };
57 
58 const char *months[] = {
59 	"Jan", "Feb", "Mar", "Apr", "May", "Jun",
60 	"Jul", "Aug", "Sep", "Oct", "Nov", "Dec", NULL,
61 };
62 
63 const char *sequences[] = {
64 	"First", "Second", "Third", "Fourth", "Fifth", "Last"
65 };
66 
67 struct fixs fndays[8];		/* full national days names */
68 struct fixs ndays[8];		/* short national days names */
69 struct fixs fnmonths[13];	/* full national months names */
70 struct fixs nmonths[13];	/* short national month names */
71 struct fixs nsequences[10];	/* national sequence names */
72 
73 
74 void
75 setnnames(void)
76 {
77 	char buf[80];
78 	int i, l;
79 	struct tm tm;
80 
81 	memset(&tm, 0, sizeof(struct tm));
82 	for (i = 0; i < 7; i++) {
83 		tm.tm_wday = i;
84 		strftime(buf, sizeof(buf), "%a", &tm);
85 		for (l = strlen(buf);
86 		     l > 0 && isspace((unsigned char)buf[l - 1]);
87 		     l--)
88 			;
89 		buf[l] = '\0';
90 		if (ndays[i].name != NULL)
91 			free(ndays[i].name);
92 		if ((ndays[i].name = strdup(buf)) == NULL)
93 			errx(1, "cannot allocate memory");
94 		ndays[i].len = strlen(buf);
95 
96 		strftime(buf, sizeof(buf), "%A", &tm);
97 		for (l = strlen(buf);
98 		     l > 0 && isspace((unsigned char)buf[l - 1]);
99 		     l--)
100 			;
101 		buf[l] = '\0';
102 		if (fndays[i].name != NULL)
103 			free(fndays[i].name);
104 		if ((fndays[i].name = strdup(buf)) == NULL)
105 			errx(1, "cannot allocate memory");
106 		fndays[i].len = strlen(buf);
107 	}
108 
109 	memset(&tm, 0, sizeof(struct tm));
110 	for (i = 0; i < 12; i++) {
111 		tm.tm_mon = i;
112 		strftime(buf, sizeof(buf), "%b", &tm);
113 		for (l = strlen(buf);
114 		     l > 0 && isspace((unsigned char)buf[l - 1]);
115 		     l--)
116 			;
117 		buf[l] = '\0';
118 		if (nmonths[i].name != NULL)
119 			free(nmonths[i].name);
120 		if ((nmonths[i].name = strdup(buf)) == NULL)
121 			errx(1, "cannot allocate memory");
122 		nmonths[i].len = strlen(buf);
123 
124 		strftime(buf, sizeof(buf), "%B", &tm);
125 		for (l = strlen(buf);
126 		     l > 0 && isspace((unsigned char)buf[l - 1]);
127 		     l--)
128 			;
129 		buf[l] = '\0';
130 		if (fnmonths[i].name != NULL)
131 			free(fnmonths[i].name);
132 		if ((fnmonths[i].name = strdup(buf)) == NULL)
133 			errx(1, "cannot allocate memory");
134 		fnmonths[i].len = strlen(buf);
135 	}
136 }
137 
138 void
139 setnsequences(char *seq)
140 {
141 	int i;
142 	char *p;
143 
144 	p = seq;
145 	for (i = 0; i < 5; i++) {
146 		nsequences[i].name = p;
147 		if ((p = strchr(p, ' ')) == NULL) {
148 			/* Oh oh there is something wrong. Erase! Erase! */
149 			for (i = 0; i < 5; i++) {
150 				nsequences[i].name = NULL;
151 				nsequences[i].len = 0;
152 			}
153 			return;
154 		}
155 		*p = '\0';
156 		p++;
157 	}
158 	nsequences[i].name = p;
159 
160 	for (i = 0; i < 5; i++) {
161 		nsequences[i].name = strdup(nsequences[i].name);
162 		nsequences[i].len = nsequences[i + 1].name - nsequences[i].name;
163 	}
164 	nsequences[i].name = strdup(nsequences[i].name);
165 	nsequences[i].len = strlen(nsequences[i].name);
166 
167 	return;
168 }
169