]>
Commit | Line | Data |
---|---|---|
e9ce8d39 A |
1 | /* |
2 | * Copyright (c) 1999 Apple Computer, Inc. All rights reserved. | |
3 | * | |
4 | * @APPLE_LICENSE_HEADER_START@ | |
5 | * | |
734aad71 | 6 | * Copyright (c) 1999-2003 Apple Computer, Inc. All Rights Reserved. |
e9ce8d39 | 7 | * |
734aad71 A |
8 | * This file contains Original Code and/or Modifications of Original Code |
9 | * as defined in and that are subject to the Apple Public Source License | |
10 | * Version 2.0 (the 'License'). You may not use this file except in | |
11 | * compliance with the License. Please obtain a copy of the License at | |
12 | * http://www.opensource.apple.com/apsl/ and read it before using this | |
13 | * file. | |
14 | * | |
15 | * The Original Code and all software distributed under the License are | |
16 | * distributed on an 'AS IS' basis, WITHOUT WARRANTY OF ANY KIND, EITHER | |
e9ce8d39 A |
17 | * EXPRESS OR IMPLIED, AND APPLE HEREBY DISCLAIMS ALL SUCH WARRANTIES, |
18 | * INCLUDING WITHOUT LIMITATION, ANY WARRANTIES OF MERCHANTABILITY, | |
734aad71 A |
19 | * FITNESS FOR A PARTICULAR PURPOSE, QUIET ENJOYMENT OR NON-INFRINGEMENT. |
20 | * Please see the License for the specific language governing rights and | |
21 | * limitations under the License. | |
e9ce8d39 A |
22 | * |
23 | * @APPLE_LICENSE_HEADER_END@ | |
24 | */ | |
25 | /* | |
26 | * Copyright (c) 1989, 1993 | |
27 | * The Regents of the University of California. All rights reserved. | |
28 | * | |
29 | * Redistribution and use in source and binary forms, with or without | |
30 | * modification, are permitted provided that the following conditions | |
31 | * are met: | |
32 | * 1. Redistributions of source code must retain the above copyright | |
33 | * notice, this list of conditions and the following disclaimer. | |
34 | * 2. Redistributions in binary form must reproduce the above copyright | |
35 | * notice, this list of conditions and the following disclaimer in the | |
36 | * documentation and/or other materials provided with the distribution. | |
37 | * 3. All advertising materials mentioning features or use of this software | |
38 | * must display the following acknowledgement: | |
39 | * This product includes software developed by the University of | |
40 | * California, Berkeley and its contributors. | |
41 | * 4. Neither the name of the University nor the names of its contributors | |
42 | * may be used to endorse or promote products derived from this software | |
43 | * without specific prior written permission. | |
44 | * | |
45 | * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND | |
46 | * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE | |
47 | * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE | |
48 | * ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE | |
49 | * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL | |
50 | * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS | |
51 | * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) | |
52 | * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT | |
53 | * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY | |
54 | * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF | |
55 | * SUCH DAMAGE. | |
56 | */ | |
57 | ||
58 | ||
59 | #include <sys/types.h> | |
60 | #include <sys/time.h> | |
61 | #include <tzfile.h> | |
62 | #include <string.h> | |
63 | ||
64 | static char *afmt[] = { | |
65 | "Sun", "Mon", "Tue", "Wed", "Thu", "Fri", "Sat", | |
66 | }; | |
67 | static char *Afmt[] = { | |
68 | "Sunday", "Monday", "Tuesday", "Wednesday", "Thursday", "Friday", | |
69 | "Saturday", | |
70 | }; | |
71 | static char *bfmt[] = { | |
72 | "Jan", "Feb", "Mar", "Apr", "May", "Jun", "Jul", "Aug", "Sep", | |
73 | "Oct", "Nov", "Dec", | |
74 | }; | |
75 | static char *Bfmt[] = { | |
76 | "January", "February", "March", "April", "May", "June", "July", | |
77 | "August", "September", "October", "November", "December", | |
78 | }; | |
79 | ||
80 | static size_t gsize; | |
81 | static char *pt; | |
82 | static int _add __P((char *)); | |
83 | static int _conv __P((int, int, int)); | |
84 | static int _secs __P((const struct tm *)); | |
85 | static size_t _fmt __P((const char *, const struct tm *)); | |
86 | ||
87 | size_t | |
88 | strftime(s, maxsize, format, t) | |
89 | char *s; | |
90 | size_t maxsize; | |
91 | const char *format; | |
92 | const struct tm *t; | |
93 | { | |
94 | ||
95 | pt = s; | |
96 | if ((gsize = maxsize) < 1) | |
97 | return(0); | |
98 | if (_fmt(format, t)) { | |
99 | *pt = '\0'; | |
100 | return(maxsize - gsize); | |
101 | } | |
102 | return(0); | |
103 | } | |
104 | ||
105 | static size_t | |
106 | _fmt(format, t) | |
107 | register const char *format; | |
108 | const struct tm *t; | |
109 | { | |
110 | for (; *format; ++format) { | |
111 | if (*format == '%') | |
112 | switch(*++format) { | |
113 | case '\0': | |
114 | --format; | |
115 | break; | |
116 | case 'A': | |
117 | if (t->tm_wday < 0 || t->tm_wday > 6) | |
118 | return(0); | |
119 | if (!_add(Afmt[t->tm_wday])) | |
120 | return(0); | |
121 | continue; | |
122 | case 'a': | |
123 | if (t->tm_wday < 0 || t->tm_wday > 6) | |
124 | return(0); | |
125 | if (!_add(afmt[t->tm_wday])) | |
126 | return(0); | |
127 | continue; | |
128 | case 'B': | |
129 | if (t->tm_mon < 0 || t->tm_mon > 11) | |
130 | return(0); | |
131 | if (!_add(Bfmt[t->tm_mon])) | |
132 | return(0); | |
133 | continue; | |
134 | case 'b': | |
135 | case 'h': | |
136 | if (t->tm_mon < 0 || t->tm_mon > 11) | |
137 | return(0); | |
138 | if (!_add(bfmt[t->tm_mon])) | |
139 | return(0); | |
140 | continue; | |
141 | case 'C': | |
142 | if (!_fmt("%a %b %e %H:%M:%S %Y", t)) | |
143 | return(0); | |
144 | continue; | |
145 | case 'c': | |
146 | if (!_fmt("%m/%d/%y %H:%M:%S", t)) | |
147 | return(0); | |
148 | continue; | |
149 | case 'D': | |
150 | if (!_fmt("%m/%d/%y", t)) | |
151 | return(0); | |
152 | continue; | |
153 | case 'd': | |
154 | if (!_conv(t->tm_mday, 2, '0')) | |
155 | return(0); | |
156 | continue; | |
157 | case 'e': | |
158 | if (!_conv(t->tm_mday, 2, ' ')) | |
159 | return(0); | |
160 | continue; | |
161 | case 'H': | |
162 | if (!_conv(t->tm_hour, 2, '0')) | |
163 | return(0); | |
164 | continue; | |
165 | case 'I': | |
166 | if (!_conv(t->tm_hour % 12 ? | |
167 | t->tm_hour % 12 : 12, 2, '0')) | |
168 | return(0); | |
169 | continue; | |
170 | case 'j': | |
171 | if (!_conv(t->tm_yday + 1, 3, '0')) | |
172 | return(0); | |
173 | continue; | |
174 | case 'k': | |
175 | if (!_conv(t->tm_hour, 2, ' ')) | |
176 | return(0); | |
177 | continue; | |
178 | case 'l': | |
179 | if (!_conv(t->tm_hour % 12 ? | |
180 | t->tm_hour % 12 : 12, 2, ' ')) | |
181 | return(0); | |
182 | continue; | |
183 | case 'M': | |
184 | if (!_conv(t->tm_min, 2, '0')) | |
185 | return(0); | |
186 | continue; | |
187 | case 'm': | |
188 | if (!_conv(t->tm_mon + 1, 2, '0')) | |
189 | return(0); | |
190 | continue; | |
191 | case 'n': | |
192 | if (!_add("\n")) | |
193 | return(0); | |
194 | continue; | |
195 | case 'p': | |
196 | if (!_add(t->tm_hour >= 12 ? "PM" : "AM")) | |
197 | return(0); | |
198 | continue; | |
199 | case 'R': | |
200 | if (!_fmt("%H:%M", t)) | |
201 | return(0); | |
202 | continue; | |
203 | case 'r': | |
204 | if (!_fmt("%I:%M:%S %p", t)) | |
205 | return(0); | |
206 | continue; | |
207 | case 'S': | |
208 | if (!_conv(t->tm_sec, 2, '0')) | |
209 | return(0); | |
210 | continue; | |
211 | case 's': | |
212 | if (!_secs(t)) | |
213 | return(0); | |
214 | continue; | |
215 | case 'T': | |
216 | case 'X': | |
217 | if (!_fmt("%H:%M:%S", t)) | |
218 | return(0); | |
219 | continue; | |
220 | case 't': | |
221 | if (!_add("\t")) | |
222 | return(0); | |
223 | continue; | |
224 | case 'U': | |
225 | if (!_conv((t->tm_yday + 7 - t->tm_wday) / 7, | |
226 | 2, '0')) | |
227 | return(0); | |
228 | continue; | |
229 | case 'W': | |
230 | if (!_conv((t->tm_yday + 7 - | |
231 | (t->tm_wday ? (t->tm_wday - 1) : 6)) | |
232 | / 7, 2, '0')) | |
233 | return(0); | |
234 | continue; | |
235 | case 'w': | |
236 | if (!_conv(t->tm_wday, 1, '0')) | |
237 | return(0); | |
238 | continue; | |
239 | case 'x': | |
240 | if (!_fmt("%m/%d/%y", t)) | |
241 | return(0); | |
242 | continue; | |
243 | case 'y': | |
244 | if (!_conv((t->tm_year + TM_YEAR_BASE) | |
245 | % 100, 2, '0')) | |
246 | return(0); | |
247 | continue; | |
248 | case 'Y': | |
249 | if (!_conv(t->tm_year + TM_YEAR_BASE, 4, '0')) | |
250 | return(0); | |
251 | continue; | |
252 | case 'Z': | |
253 | if (!t->tm_zone || !_add(t->tm_zone)) | |
254 | return(0); | |
255 | continue; | |
256 | case '%': | |
257 | /* | |
258 | * X311J/88-090 (4.12.3.5): if conversion char is | |
259 | * undefined, behavior is undefined. Print out the | |
260 | * character itself as printf(3) does. | |
261 | */ | |
262 | default: | |
263 | break; | |
264 | } | |
265 | if (!gsize--) | |
266 | return(0); | |
267 | *pt++ = *format; | |
268 | } | |
269 | return(gsize); | |
270 | } | |
271 | ||
272 | static int | |
273 | _secs(t) | |
274 | const struct tm *t; | |
275 | { | |
276 | static char buf[15]; | |
277 | register time_t s; | |
278 | register char *p; | |
279 | struct tm tmp; | |
280 | ||
281 | /* Make a copy, mktime(3) modifies the tm struct. */ | |
282 | tmp = *t; | |
283 | s = mktime(&tmp); | |
284 | for (p = buf + sizeof(buf) - 2; s > 0 && p > buf; s /= 10) | |
285 | *p-- = s % 10 + '0'; | |
286 | return(_add(++p)); | |
287 | } | |
288 | ||
289 | static int | |
290 | _conv(n, digits, pad) | |
291 | int n, digits, pad; | |
292 | { | |
293 | static char buf[10]; | |
294 | register char *p; | |
295 | ||
296 | for (p = buf + sizeof(buf) - 2; n > 0 && p > buf; n /= 10, --digits) | |
297 | *p-- = n % 10 + '0'; | |
298 | while (p > buf && digits-- > 0) | |
299 | *p-- = pad; | |
300 | return(_add(++p)); | |
301 | } | |
302 | ||
303 | static int | |
304 | _add(str) | |
305 | register char *str; | |
306 | { | |
307 | for (;; ++pt, --gsize) { | |
308 | if (!gsize) | |
309 | return(0); | |
310 | if (!(*pt = *str++)) | |
311 | return(1); | |
312 | } | |
313 | } |