/usr.bin/calendar/locale.c

https://bitbucket.org/freebsd/freebsd-head/ · C · 166 lines · 118 code · 19 blank · 29 comment · 31 complexity · d6b86b0005f1be2dfe842769448571f9 MD5 · raw file

  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. #include <sys/cdefs.h>
  30. __FBSDID("$FreeBSD$");
  31. #include <ctype.h>
  32. #include <err.h>
  33. #include <stdio.h>
  34. #include <stdlib.h>
  35. #include <string.h>
  36. #include <time.h>
  37. #include "calendar.h"
  38. const char *fdays[] = {
  39. "Sunday", "Monday", "Tuesday", "Wednesday", "Thursday", "Friday",
  40. "Saturday", NULL,
  41. };
  42. const char *days[] = {
  43. "Sun", "Mon", "Tue", "Wed", "Thu", "Fri", "Sat", NULL,
  44. };
  45. const char *fmonths[] = {
  46. "January", "February", "March", "April", "May", "June", "Juli",
  47. "August", "September", "October", "November", "December", NULL,
  48. };
  49. const char *months[] = {
  50. "Jan", "Feb", "Mar", "Apr", "May", "Jun",
  51. "Jul", "Aug", "Sep", "Oct", "Nov", "Dec", NULL,
  52. };
  53. const char *sequences[] = {
  54. "First", "Second", "Third", "Fourth", "Fifth", "Last"
  55. };
  56. struct fixs fndays[8]; /* full national days names */
  57. struct fixs ndays[8]; /* short national days names */
  58. struct fixs fnmonths[13]; /* full national months names */
  59. struct fixs nmonths[13]; /* short national month names */
  60. struct fixs nsequences[10]; /* national sequence names */
  61. void
  62. setnnames(void)
  63. {
  64. char buf[80];
  65. int i, l;
  66. struct tm tm;
  67. memset(&tm, 0, sizeof(struct tm));
  68. for (i = 0; i < 7; i++) {
  69. tm.tm_wday = i;
  70. strftime(buf, sizeof(buf), "%a", &tm);
  71. for (l = strlen(buf);
  72. l > 0 && isspace((unsigned char)buf[l - 1]);
  73. l--)
  74. ;
  75. buf[l] = '\0';
  76. if (ndays[i].name != NULL)
  77. free(ndays[i].name);
  78. if ((ndays[i].name = strdup(buf)) == NULL)
  79. errx(1, "cannot allocate memory");
  80. ndays[i].len = strlen(buf);
  81. strftime(buf, sizeof(buf), "%A", &tm);
  82. for (l = strlen(buf);
  83. l > 0 && isspace((unsigned char)buf[l - 1]);
  84. l--)
  85. ;
  86. buf[l] = '\0';
  87. if (fndays[i].name != NULL)
  88. free(fndays[i].name);
  89. if ((fndays[i].name = strdup(buf)) == NULL)
  90. errx(1, "cannot allocate memory");
  91. fndays[i].len = strlen(buf);
  92. }
  93. memset(&tm, 0, sizeof(struct tm));
  94. for (i = 0; i < 12; i++) {
  95. tm.tm_mon = i;
  96. strftime(buf, sizeof(buf), "%b", &tm);
  97. for (l = strlen(buf);
  98. l > 0 && isspace((unsigned char)buf[l - 1]);
  99. l--)
  100. ;
  101. buf[l] = '\0';
  102. if (nmonths[i].name != NULL)
  103. free(nmonths[i].name);
  104. if ((nmonths[i].name = strdup(buf)) == NULL)
  105. errx(1, "cannot allocate memory");
  106. nmonths[i].len = strlen(buf);
  107. strftime(buf, sizeof(buf), "%B", &tm);
  108. for (l = strlen(buf);
  109. l > 0 && isspace((unsigned char)buf[l - 1]);
  110. l--)
  111. ;
  112. buf[l] = '\0';
  113. if (fnmonths[i].name != NULL)
  114. free(fnmonths[i].name);
  115. if ((fnmonths[i].name = strdup(buf)) == NULL)
  116. errx(1, "cannot allocate memory");
  117. fnmonths[i].len = strlen(buf);
  118. }
  119. }
  120. void
  121. setnsequences(char *seq)
  122. {
  123. int i;
  124. char *p;
  125. p = seq;
  126. for (i = 0; i < 5; i++) {
  127. nsequences[i].name = p;
  128. if ((p = strchr(p, ' ')) == NULL) {
  129. /* Oh oh there is something wrong. Erase! Erase! */
  130. for (i = 0; i < 5; i++) {
  131. nsequences[i].name = NULL;
  132. nsequences[i].len = 0;
  133. }
  134. return;
  135. }
  136. *p = '\0';
  137. p++;
  138. }
  139. nsequences[i].name = p;
  140. for (i = 0; i < 5; i++) {
  141. nsequences[i].name = strdup(nsequences[i].name);
  142. nsequences[i].len = nsequences[i + 1].name - nsequences[i].name;
  143. }
  144. nsequences[i].name = strdup(nsequences[i].name);
  145. nsequences[i].len = strlen(nsequences[i].name);
  146. return;
  147. }