timelocal.c (50476) | timelocal.c (51186) |
---|---|
1/*- 2 * Copyright (c) 1997 FreeBSD Inc. 3 * 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 unchanged lines hidden (view full) --- 18 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL 19 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS 20 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) 21 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT 22 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY 23 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF 24 * SUCH DAMAGE. 25 * | 1/*- 2 * Copyright (c) 1997 FreeBSD Inc. 3 * 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 unchanged lines hidden (view full) --- 18 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL 19 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS 20 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) 21 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT 22 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY 23 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF 24 * SUCH DAMAGE. 25 * |
26 * $FreeBSD: head/lib/libc/stdtime/timelocal.c 50476 1999-08-28 00:22:10Z peter $ | 26 * $FreeBSD: head/lib/libc/stdtime/timelocal.c 51186 1999-09-11 21:35:21Z dt $ |
27 */ 28 29#include <sys/types.h> 30#include <sys/stat.h> 31#include <sys/syslimits.h> 32#include <fcntl.h> 33#include <locale.h> | 27 */ 28 29#include <sys/types.h> 30#include <sys/stat.h> 31#include <sys/syslimits.h> 32#include <fcntl.h> 33#include <locale.h> |
34#include <stddef.h> |
|
34#include <stdlib.h> 35#include <string.h> 36#include "setlocale.h" 37#include "timelocal.h" 38 | 35#include <stdlib.h> 36#include <string.h> 37#include "setlocale.h" 38#include "timelocal.h" 39 |
40static int split_lines(char *, const char *); 41static void set_from_buf(const char *, int); 42 |
|
39struct lc_time_T _time_localebuf; 40int _time_using_locale; 41 | 43struct lc_time_T _time_localebuf; 44int _time_using_locale; 45 |
46#define LCTIME_SIZE_FULL (sizeof(struct lc_time_T) / sizeof(char *)) 47#define LCTIME_SIZE_1 \ 48 (offsetof(struct lc_time_T, alt_month[0]) / sizeof(char *)) 49 |
|
42const struct lc_time_T _C_time_locale = { 43 { 44 "Jan", "Feb", "Mar", "Apr", "May", "Jun", 45 "Jul", "Aug", "Sep", "Oct", "Nov", "Dec" 46 }, { 47 "January", "February", "March", "April", "May", "June", 48 "July", "August", "September", "October", "November", "December" 49 }, { --- 26 unchanged lines hidden (view full) --- 76 77 /* am */ 78 "AM", 79 80 /* pm */ 81 "PM", 82 83 /* date_fmt */ | 50const struct lc_time_T _C_time_locale = { 51 { 52 "Jan", "Feb", "Mar", "Apr", "May", "Jun", 53 "Jul", "Aug", "Sep", "Oct", "Nov", "Dec" 54 }, { 55 "January", "February", "March", "April", "May", "June", 56 "July", "August", "September", "October", "November", "December" 57 }, { --- 26 unchanged lines hidden (view full) --- 84 85 /* am */ 86 "AM", 87 88 /* pm */ 89 "PM", 90 91 /* date_fmt */ |
84 "%a %b %e %X %Z %Y" | 92 "%a %b %e %X %Z %Y", 93 94 { 95 "January", "February", "March", "April", "May", "June", 96 "July", "August", "September", "October", "November", "December" 97 } |
85}; 86 87 88int 89__time_load_locale(const char *name) 90{ 91 static char * locale_buf; 92 static char locale_buf_C[] = "C"; | 98}; 99 100 101int 102__time_load_locale(const char *name) 103{ 104 static char * locale_buf; 105 static char locale_buf_C[] = "C"; |
106 static int num_lines; |
|
93 94 int fd; 95 char * lbuf; 96 char * p; | 107 108 int fd; 109 char * lbuf; 110 char * p; |
97 const char ** ap; | |
98 const char * plim; 99 char filename[PATH_MAX]; 100 struct stat st; 101 size_t namesize; 102 size_t bufsize; 103 int save_using_locale; 104 105 save_using_locale = _time_using_locale; --- 5 unchanged lines hidden (view full) --- 111 if (!strcmp(name, "C") || !strcmp(name, "POSIX")) 112 return 0; 113 114 /* 115 ** If the locale name is the same as our cache, use the cache. 116 */ 117 lbuf = locale_buf; 118 if (lbuf != NULL && strcmp(name, lbuf) == 0) { | 111 const char * plim; 112 char filename[PATH_MAX]; 113 struct stat st; 114 size_t namesize; 115 size_t bufsize; 116 int save_using_locale; 117 118 save_using_locale = _time_using_locale; --- 5 unchanged lines hidden (view full) --- 124 if (!strcmp(name, "C") || !strcmp(name, "POSIX")) 125 return 0; 126 127 /* 128 ** If the locale name is the same as our cache, use the cache. 129 */ 130 lbuf = locale_buf; 131 if (lbuf != NULL && strcmp(name, lbuf) == 0) { |
119 p = lbuf; 120 for (ap = (const char **) &_time_localebuf; 121 ap < (const char **) (&_time_localebuf + 1); 122 ++ap) 123 *ap = p += strlen(p) + 1; | 132 set_from_buf(lbuf, num_lines); |
124 _time_using_locale = 1; 125 return 0; 126 } 127 /* 128 ** Slurp the locale file into the cache. 129 */ 130 namesize = strlen(name) + 1; 131 --- 24 unchanged lines hidden (view full) --- 156 goto bad_lbuf; 157 if (close(fd) != 0) 158 goto bad_lbuf; 159 /* 160 ** Parse the locale file into localebuf. 161 */ 162 if (plim[-1] != '\n') 163 goto bad_lbuf; | 133 _time_using_locale = 1; 134 return 0; 135 } 136 /* 137 ** Slurp the locale file into the cache. 138 */ 139 namesize = strlen(name) + 1; 140 --- 24 unchanged lines hidden (view full) --- 165 goto bad_lbuf; 166 if (close(fd) != 0) 167 goto bad_lbuf; 168 /* 169 ** Parse the locale file into localebuf. 170 */ 171 if (plim[-1] != '\n') 172 goto bad_lbuf; |
164 for (ap = (const char **) &_time_localebuf; 165 ap < (const char **) (&_time_localebuf + 1); 166 ++ap) { 167 if (p == plim) 168 goto reset_locale; 169 *ap = p; 170 while (*p != '\n') 171 ++p; 172 *p++ = '\0'; 173 } | 173 num_lines = split_lines(p, plim); 174 if (num_lines >= LCTIME_SIZE_FULL) 175 num_lines = LCTIME_SIZE_FULL; 176 else if (num_lines >= LCTIME_SIZE_1) 177 num_lines = LCTIME_SIZE_1; 178 else 179 goto reset_locale; 180 set_from_buf(lbuf, num_lines); |
174 /* 175 ** Record the successful parse in the cache. 176 */ 177 locale_buf = lbuf; 178 179 _time_using_locale = 1; 180 return 0; 181 --- 8 unchanged lines hidden (view full) --- 190bad_lbuf: 191 free(lbuf); 192bad_locale: 193 (void) close(fd); 194no_locale: 195 _time_using_locale = save_using_locale; 196 return -1; 197} | 181 /* 182 ** Record the successful parse in the cache. 183 */ 184 locale_buf = lbuf; 185 186 _time_using_locale = 1; 187 return 0; 188 --- 8 unchanged lines hidden (view full) --- 197bad_lbuf: 198 free(lbuf); 199bad_locale: 200 (void) close(fd); 201no_locale: 202 _time_using_locale = save_using_locale; 203 return -1; 204} |
205 206static int 207split_lines(char *p, const char *plim) 208{ 209 int i; 210 211 for (i = 0; p < plim; i++) { 212 p = strchr(p, '\n'); 213 *p++ = '\0'; 214 } 215 return i; 216} 217 218static void 219set_from_buf(const char *p, int num_lines) 220{ 221 const char **ap; 222 int i; 223 224 for (ap = (const char **) &_time_localebuf, i = 0; 225 i < num_lines; ++ap, ++i) 226 *ap = p += strlen(p) + 1; 227 if (num_lines == LCTIME_SIZE_FULL) 228 return; 229 for (i = 0; i < 12; i++) 230 _time_localebuf.alt_month[i] = _time_localebuf.month[i]; 231} |
|