1205821Sedwin/*- 2205821Sedwin * Copyright (c) 1989, 1993, 1994 3205821Sedwin * The Regents of the University of California. All rights reserved. 4205821Sedwin * 5205821Sedwin * Redistribution and use in source and binary forms, with or without 6205821Sedwin * modification, are permitted provided that the following conditions 7205821Sedwin * are met: 8205821Sedwin * 1. Redistributions of source code must retain the above copyright 9205821Sedwin * notice, this list of conditions and the following disclaimer. 10205821Sedwin * 2. Redistributions in binary form must reproduce the above copyright 11205821Sedwin * notice, this list of conditions and the following disclaimer in the 12205821Sedwin * documentation and/or other materials provided with the distribution. 13205821Sedwin * 4. Neither the name of the University nor the names of its contributors 14205821Sedwin * may be used to endorse or promote products derived from this software 15205821Sedwin * without specific prior written permission. 16205821Sedwin * 17205821Sedwin * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND 18205821Sedwin * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE 19205821Sedwin * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE 20205821Sedwin * ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE 21205821Sedwin * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL 22205821Sedwin * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS 23205821Sedwin * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) 24205821Sedwin * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT 25205821Sedwin * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY 26205821Sedwin * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF 27205821Sedwin * SUCH DAMAGE. 28205821Sedwin */ 29205821Sedwin 30205821Sedwin#include <sys/cdefs.h> 31205821Sedwin__FBSDID("$FreeBSD$"); 32205821Sedwin 33205821Sedwin#include <ctype.h> 34205821Sedwin#include <err.h> 35205821Sedwin#include <stdio.h> 36205821Sedwin#include <stdlib.h> 37205821Sedwin#include <string.h> 38205821Sedwin#include <time.h> 39205821Sedwin 40205821Sedwin#include "calendar.h" 41205821Sedwin 42205821Sedwinconst char *fdays[] = { 43205821Sedwin "Sunday", "Monday", "Tuesday", "Wednesday", "Thursday", "Friday", 44205821Sedwin "Saturday", NULL, 45205821Sedwin}; 46205821Sedwin 47205821Sedwinconst char *days[] = { 48205821Sedwin "Sun", "Mon", "Tue", "Wed", "Thu", "Fri", "Sat", NULL, 49205821Sedwin}; 50205821Sedwin 51205821Sedwinconst char *fmonths[] = { 52205821Sedwin "January", "February", "March", "April", "May", "June", "Juli", 53205821Sedwin "August", "September", "October", "November", "December", NULL, 54205821Sedwin}; 55205821Sedwin 56205821Sedwinconst char *months[] = { 57205821Sedwin "Jan", "Feb", "Mar", "Apr", "May", "Jun", 58205821Sedwin "Jul", "Aug", "Sep", "Oct", "Nov", "Dec", NULL, 59205821Sedwin}; 60205821Sedwin 61205821Sedwinconst char *sequences[] = { 62205821Sedwin "First", "Second", "Third", "Fourth", "Fifth", "Last" 63205821Sedwin}; 64205821Sedwin 65205821Sedwinstruct fixs fndays[8]; /* full national days names */ 66205821Sedwinstruct fixs ndays[8]; /* short national days names */ 67205821Sedwinstruct fixs fnmonths[13]; /* full national months names */ 68205821Sedwinstruct fixs nmonths[13]; /* short national month names */ 69205821Sedwinstruct fixs nsequences[10]; /* national sequence names */ 70205821Sedwin 71205821Sedwin 72205821Sedwinvoid 73205821Sedwinsetnnames(void) 74205821Sedwin{ 75205821Sedwin char buf[80]; 76205821Sedwin int i, l; 77205821Sedwin struct tm tm; 78205821Sedwin 79211517Sedwin memset(&tm, 0, sizeof(struct tm)); 80205821Sedwin for (i = 0; i < 7; i++) { 81205821Sedwin tm.tm_wday = i; 82205821Sedwin strftime(buf, sizeof(buf), "%a", &tm); 83205821Sedwin for (l = strlen(buf); 84205821Sedwin l > 0 && isspace((unsigned char)buf[l - 1]); 85205821Sedwin l--) 86205821Sedwin ; 87205821Sedwin buf[l] = '\0'; 88205821Sedwin if (ndays[i].name != NULL) 89205821Sedwin free(ndays[i].name); 90205821Sedwin if ((ndays[i].name = strdup(buf)) == NULL) 91205821Sedwin errx(1, "cannot allocate memory"); 92205821Sedwin ndays[i].len = strlen(buf); 93205821Sedwin 94205821Sedwin strftime(buf, sizeof(buf), "%A", &tm); 95205821Sedwin for (l = strlen(buf); 96205821Sedwin l > 0 && isspace((unsigned char)buf[l - 1]); 97205821Sedwin l--) 98205821Sedwin ; 99205821Sedwin buf[l] = '\0'; 100205821Sedwin if (fndays[i].name != NULL) 101205821Sedwin free(fndays[i].name); 102205821Sedwin if ((fndays[i].name = strdup(buf)) == NULL) 103205821Sedwin errx(1, "cannot allocate memory"); 104205821Sedwin fndays[i].len = strlen(buf); 105205821Sedwin } 106205821Sedwin 107211517Sedwin memset(&tm, 0, sizeof(struct tm)); 108205821Sedwin for (i = 0; i < 12; i++) { 109205821Sedwin tm.tm_mon = i; 110205821Sedwin strftime(buf, sizeof(buf), "%b", &tm); 111205821Sedwin for (l = strlen(buf); 112205821Sedwin l > 0 && isspace((unsigned char)buf[l - 1]); 113205821Sedwin l--) 114205821Sedwin ; 115205821Sedwin buf[l] = '\0'; 116205821Sedwin if (nmonths[i].name != NULL) 117205821Sedwin free(nmonths[i].name); 118205821Sedwin if ((nmonths[i].name = strdup(buf)) == NULL) 119205821Sedwin errx(1, "cannot allocate memory"); 120205821Sedwin nmonths[i].len = strlen(buf); 121205821Sedwin 122205821Sedwin strftime(buf, sizeof(buf), "%B", &tm); 123205821Sedwin for (l = strlen(buf); 124205821Sedwin l > 0 && isspace((unsigned char)buf[l - 1]); 125205821Sedwin l--) 126205821Sedwin ; 127205821Sedwin buf[l] = '\0'; 128205821Sedwin if (fnmonths[i].name != NULL) 129205821Sedwin free(fnmonths[i].name); 130205821Sedwin if ((fnmonths[i].name = strdup(buf)) == NULL) 131205821Sedwin errx(1, "cannot allocate memory"); 132205821Sedwin fnmonths[i].len = strlen(buf); 133205821Sedwin } 134205821Sedwin} 135205821Sedwin 136205821Sedwinvoid 137205821Sedwinsetnsequences(char *seq) 138205821Sedwin{ 139205821Sedwin int i; 140205821Sedwin char *p; 141205821Sedwin 142205821Sedwin p = seq; 143205821Sedwin for (i = 0; i < 5; i++) { 144205821Sedwin nsequences[i].name = p; 145205821Sedwin if ((p = strchr(p, ' ')) == NULL) { 146208827Sedwin /* Oh oh there is something wrong. Erase! Erase! */ 147205821Sedwin for (i = 0; i < 5; i++) { 148205821Sedwin nsequences[i].name = NULL; 149205821Sedwin nsequences[i].len = 0; 150205821Sedwin } 151208827Sedwin return; 152205821Sedwin } 153205821Sedwin *p = '\0'; 154205821Sedwin p++; 155205821Sedwin } 156205821Sedwin nsequences[i].name = p; 157205821Sedwin 158205821Sedwin for (i = 0; i < 5; i++) { 159205821Sedwin nsequences[i].name = strdup(nsequences[i].name); 160205821Sedwin nsequences[i].len = nsequences[i + 1].name - nsequences[i].name; 161205821Sedwin } 162205821Sedwin nsequences[i].name = strdup(nsequences[i].name); 163205821Sedwin nsequences[i].len = strlen(nsequences[i].name); 164205821Sedwin 165205821Sedwin return; 166205821Sedwin} 167