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