util.cpp 13 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509
  1. /** BEGIN COPYRIGHT BLOCK
  2. * This Program is free software; you can redistribute it and/or modify it under
  3. * the terms of the GNU General Public License as published by the Free Software
  4. * Foundation; version 2 of the License.
  5. *
  6. * This Program is distributed in the hope that it will be useful, but WITHOUT
  7. * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS
  8. * FOR A PARTICULAR PURPOSE. See the GNU General Public License for more details.
  9. *
  10. * You should have received a copy of the GNU General Public License along with
  11. * this Program; if not, write to the Free Software Foundation, Inc., 59 Temple
  12. * Place, Suite 330, Boston, MA 02111-1307 USA.
  13. *
  14. * In addition, as a special exception, Red Hat, Inc. gives You the additional
  15. * right to link the code of this Program with code not covered under the GNU
  16. * General Public License ("Non-GPL Code") and to distribute linked combinations
  17. * including the two, subject to the limitations in this paragraph. Non-GPL Code
  18. * permitted under this exception must only link to the code of this Program
  19. * through those well defined interfaces identified in the file named EXCEPTION
  20. * found in the source code files (the "Approved Interfaces"). The files of
  21. * Non-GPL Code may instantiate templates or use macros or inline functions from
  22. * the Approved Interfaces without causing the resulting work to be covered by
  23. * the GNU General Public License. Only Red Hat, Inc. may make changes or
  24. * additions to the list of Approved Interfaces. You must obey the GNU General
  25. * Public License in all respects for all of the Program code and other code used
  26. * in conjunction with the Program except the Non-GPL Code covered by this
  27. * exception. If you modify this file, you may extend this exception to your
  28. * version of the file, but you are not obligated to do so. If you do not wish to
  29. * provide this exception without modification, you must delete this exception
  30. * statement from your version and license this file solely under the GPL without
  31. * exception.
  32. *
  33. *
  34. * Copyright (C) 2001 Sun Microsystems, Inc. Used by permission.
  35. * Copyright (C) 2005 Red Hat, Inc.
  36. * All rights reserved.
  37. * END COPYRIGHT BLOCK **/
  38. #ifdef HAVE_CONFIG_H
  39. # include <config.h>
  40. #endif
  41. /*
  42. * util.c: A hodge podge of utility functions and standard functions which
  43. * are unavailable on certain systems
  44. *
  45. * Rob McCool
  46. */
  47. #ifdef XP_UNIX
  48. #include <sys/types.h>
  49. #include <sys/wait.h>
  50. #include <stdlib.h>
  51. #include "prthread.h"
  52. #endif /* XP_UNIX */
  53. #include "base/util.h"
  54. #include "base/dbtbase.h"
  55. #ifdef XP_UNIX
  56. #include <sys/types.h>
  57. #endif /* WIN32 */
  58. /* ------------------------------ util_itoa ------------------------------- */
  59. /*
  60. * Assumption: Reversing the digits will be faster in the general case
  61. * than doing a log10 or some nasty trick to find the # of digits.
  62. */
  63. NSAPI_PUBLIC int util_itoa(int i, char *a)
  64. {
  65. register int x, y, p;
  66. register char c;
  67. int negative;
  68. negative = 0;
  69. if(i < 0) {
  70. *a++ = '-';
  71. negative = 1;
  72. i = -i;
  73. }
  74. p = 0;
  75. while(i > 9) {
  76. a[p++] = (i%10) + '0';
  77. i /= 10;
  78. }
  79. a[p++] = i + '0';
  80. if(p > 1) {
  81. for(x = 0, y = p - 1; x < y; ++x, --y) {
  82. c = a[x];
  83. a[x] = a[y];
  84. a[y] = c;
  85. }
  86. }
  87. a[p] = '\0';
  88. return p + negative;
  89. }
  90. /* ----------------------------- util_sprintf ----------------------------- */
  91. #include "prprf.h"
  92. /*
  93. XXXrobm the NSPR interfaces don't allow me to just pass in a buffer
  94. without a size
  95. */
  96. #define UTIL_PRF_MAXSIZE 1048576
  97. NSAPI_PUBLIC int util_vsnprintf(char *s, int n, register const char *fmt,
  98. va_list args)
  99. {
  100. return PR_vsnprintf(s, n, fmt, args);
  101. }
  102. NSAPI_PUBLIC int util_snprintf(char *s, int n, const char *fmt, ...)
  103. {
  104. va_list args;
  105. va_start(args, fmt);
  106. return PR_vsnprintf(s, n, fmt, args);
  107. }
  108. NSAPI_PUBLIC int util_vsprintf(char *s, register const char *fmt, va_list args)
  109. {
  110. return PR_vsnprintf(s, UTIL_PRF_MAXSIZE, fmt, args);
  111. }
  112. NSAPI_PUBLIC int util_sprintf(char *s, const char *fmt, ...)
  113. {
  114. va_list args;
  115. va_start(args, fmt);
  116. return PR_vsnprintf(s, UTIL_PRF_MAXSIZE, fmt, args);
  117. }
  118. /* --------------------------- util_strcasecmp ---------------------------- */
  119. #ifdef NEED_STRCASECMP
  120. /* These are stolen from mcom/lib/xp */
  121. NSAPI_PUBLIC
  122. int util_strcasecmp(CASECMPARG_T char *one, CASECMPARG_T char *two)
  123. {
  124. CASECMPARG_T char *pA;
  125. CASECMPARG_T char *pB;
  126. for(pA=one, pB=two; *pA && *pB; pA++, pB++)
  127. {
  128. int tmp = tolower(*pA) - tolower(*pB);
  129. if (tmp)
  130. return tmp;
  131. }
  132. if (*pA)
  133. return 1;
  134. if (*pB)
  135. return -1;
  136. return 0;
  137. }
  138. #endif /* NEED_STRCASECMP */
  139. #ifdef NEED_STRNCASECMP
  140. NSAPI_PUBLIC
  141. int util_strncasecmp(CASECMPARG_T char *one, CASECMPARG_T char *two, int n)
  142. {
  143. CASECMPARG_T char *pA;
  144. CASECMPARG_T char *pB;
  145. for(pA=one, pB=two;; pA++, pB++)
  146. {
  147. int tmp;
  148. if (pA == one+n)
  149. return 0;
  150. if (!(*pA && *pB))
  151. return *pA - *pB;
  152. tmp = tolower(*pA) - tolower(*pB);
  153. if (tmp)
  154. return tmp;
  155. }
  156. }
  157. #endif /* NEED_STRNCASECMP */
  158. /* ------------------------------ util_strftime --------------------------- */
  159. /*
  160. * Copyright (c) 1989 The Regents of the University of California.
  161. * All rights reserved.
  162. *
  163. * Redistribution and use in source and binary forms, with or without
  164. * modification, are permitted provided that the following conditions
  165. * are met:
  166. * 1. Redistributions of source code must retain the above copyright
  167. * notice, this list of conditions and the following disclaimer.
  168. * 2. Redistributions in binary form must reproduce the above copyright
  169. * notice, this list of conditions and the following disclaimer in the
  170. * documentation and/or other materials provided with the distribution.
  171. * 3. All advertising materials mentioning features or use of this software
  172. * must display the following acknowledgement:
  173. * This product includes software developed by the University of
  174. * California, Berkeley and its contributors.
  175. * 4. Neither the name of the University nor the names of its contributors
  176. * may be used to endorse or promote products derived from this software
  177. * without specific prior written permission.
  178. *
  179. * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
  180. * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
  181. * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
  182. * ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
  183. * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
  184. * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
  185. * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
  186. * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
  187. * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
  188. * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
  189. * SUCH DAMAGE.
  190. */
  191. #if defined(LIBC_SCCS) && !defined(lint)
  192. static char sccsid[] = "@(#)strftime.c 5.11 (Berkeley) 2/24/91";
  193. #endif /* LIBC_SCCS and not lint */
  194. #ifdef XP_UNIX
  195. #include <sys/types.h>
  196. #include <sys/time.h>
  197. #include <string.h>
  198. #include <stdio.h>
  199. #endif
  200. static char *afmt[] = {
  201. "Sun", "Mon", "Tue", "Wed", "Thu", "Fri", "Sat",
  202. };
  203. static char *Afmt[] = {
  204. "Sunday", "Monday", "Tuesday", "Wednesday", "Thursday", "Friday",
  205. "Saturday",
  206. };
  207. static char *bfmt[] = {
  208. "Jan", "Feb", "Mar", "Apr", "May", "Jun", "Jul", "Aug", "Sep",
  209. "Oct", "Nov", "Dec",
  210. };
  211. static char *Bfmt[] = {
  212. "January", "February", "March", "April", "May", "June", "July",
  213. "August", "September", "October", "November", "December",
  214. };
  215. #define TM_YEAR_BASE 1900
  216. static void _util_strftime_conv(char *, int, int, char);
  217. #define _util_strftime_add(str) for (;(*pt = *str++); pt++);
  218. #define _util_strftime_copy(str, len) memcpy(pt, str, len); pt += len;
  219. #define _util_strftime_fmt util_strftime
  220. /* util_strftime()
  221. * This is an optimized version of strftime for speed. Avoids the thread
  222. * unsafeness of BSD strftime calls.
  223. */
  224. int
  225. util_strftime(char *pt, const char *format, const struct tm *t)
  226. {
  227. char *start = pt;
  228. char *scrap;
  229. for (; *format; ++format) {
  230. if (*format == '%')
  231. switch(*++format) {
  232. case 'a': /* abbreviated weekday name */
  233. *pt++ = afmt[t->tm_wday][0];
  234. *pt++ = afmt[t->tm_wday][1];
  235. *pt++ = afmt[t->tm_wday][2];
  236. continue;
  237. case 'd': /* day of month */
  238. _util_strftime_conv(pt, t->tm_mday, 2, '0');
  239. pt += 2;
  240. continue;
  241. case 'S':
  242. _util_strftime_conv(pt, t->tm_sec, 2, '0');
  243. pt += 2;
  244. continue;
  245. case 'M':
  246. _util_strftime_conv(pt, t->tm_min, 2, '0');
  247. pt += 2;
  248. continue;
  249. case 'H':
  250. _util_strftime_conv(pt, t->tm_hour, 2, '0');
  251. pt += 2;
  252. continue;
  253. case 'Y':
  254. if (t->tm_year < 100) {
  255. *pt++ = '1';
  256. *pt++ = '9';
  257. _util_strftime_conv(pt, t->tm_year, 2, '0');
  258. } else {
  259. /* will fail after 2100; but who cares? */
  260. *pt++ = '2';
  261. *pt++ = '0';
  262. _util_strftime_conv(pt, t->tm_year-100, 2, '0');
  263. }
  264. pt += 2;
  265. continue;
  266. case 'b': /* abbreviated month name */
  267. case 'h':
  268. *pt++ = bfmt[t->tm_mon][0];
  269. *pt++ = bfmt[t->tm_mon][1];
  270. *pt++ = bfmt[t->tm_mon][2];
  271. continue;
  272. case 'T':
  273. case 'X':
  274. pt += _util_strftime_fmt(pt, "%H:%M:%S", t);
  275. continue;
  276. case '\0':
  277. --format;
  278. break;
  279. case 'A':
  280. if (t->tm_wday < 0 || t->tm_wday > 6)
  281. return(0);
  282. scrap = Afmt[t->tm_wday];
  283. _util_strftime_add(scrap);
  284. continue;
  285. case 'B':
  286. if (t->tm_mon < 0 || t->tm_mon > 11)
  287. return(0);
  288. scrap = Bfmt[t->tm_mon];
  289. _util_strftime_add(scrap);
  290. continue;
  291. case 'C':
  292. pt += _util_strftime_fmt(pt, "%a %b %e %H:%M:%S %Y", t);
  293. continue;
  294. case 'c':
  295. pt += _util_strftime_fmt(pt, "%m/%d/%y %H:%M:%S", t);
  296. continue;
  297. case 'D':
  298. pt += _util_strftime_fmt(pt, "%m/%d/%y", t);
  299. continue;
  300. case 'e':
  301. _util_strftime_conv(pt, t->tm_mday, 2, ' ');
  302. pt += 2;
  303. continue;
  304. case 'I':
  305. _util_strftime_conv(pt, t->tm_hour % 12 ?
  306. t->tm_hour % 12 : 12, 2, '0');
  307. pt += 2;
  308. continue;
  309. case 'j':
  310. _util_strftime_conv(pt, t->tm_yday + 1, 3, '0');
  311. pt += 3;
  312. continue;
  313. case 'k':
  314. _util_strftime_conv(pt, t->tm_hour, 2, ' ');
  315. pt += 2;
  316. continue;
  317. case 'l':
  318. _util_strftime_conv(pt, t->tm_hour % 12 ?
  319. t->tm_hour % 12 : 12, 2, ' ');
  320. pt += 2;
  321. continue;
  322. case 'm':
  323. _util_strftime_conv(pt, t->tm_mon + 1, 2, '0');
  324. pt += 2;
  325. continue;
  326. case 'n':
  327. *pt = '\n';
  328. pt++;
  329. continue;
  330. case 'p':
  331. if (t->tm_hour >= 12) {
  332. *pt = 'P';
  333. pt++;
  334. } else {
  335. *pt = 'A';
  336. pt++;
  337. }
  338. *pt = 'M';
  339. pt++;
  340. continue;
  341. case 'R':
  342. pt += _util_strftime_fmt(pt, "%H:%M", t);
  343. continue;
  344. case 'r':
  345. pt += _util_strftime_fmt(pt, "%I:%M:%S %p", t);
  346. continue;
  347. case 't':
  348. *pt = '\t';
  349. pt++;
  350. continue;
  351. case 'U':
  352. _util_strftime_conv(pt, (t->tm_yday + 7 - t->tm_wday) / 7,
  353. 2, '0');
  354. pt += 2;
  355. continue;
  356. case 'W':
  357. _util_strftime_conv(pt, (t->tm_yday + 7 -
  358. (t->tm_wday ? (t->tm_wday - 1) : 6))
  359. / 7, 2, '0');
  360. pt += 2;
  361. continue;
  362. case 'w':
  363. _util_strftime_conv(pt, t->tm_wday, 1, '0');
  364. pt += 1;
  365. continue;
  366. case 'x':
  367. pt += _util_strftime_fmt(pt, "%m/%d/%y", t);
  368. continue;
  369. case 'y':
  370. _util_strftime_conv(pt, (t->tm_year + TM_YEAR_BASE)
  371. % 100, 2, '0');
  372. pt += 2;
  373. continue;
  374. case '%':
  375. /*
  376. * X311J/88-090 (4.12.3.5): if conversion char is
  377. * undefined, behavior is undefined. Print out the
  378. * character itself as printf(3) does.
  379. */
  380. default:
  381. break;
  382. }
  383. *pt = *format;
  384. pt++;
  385. }
  386. start[pt-start] = '\0';
  387. return pt - start;
  388. }
  389. static void
  390. _util_strftime_conv(char *pt, int n, int digits, char pad)
  391. {
  392. static char buf[10];
  393. register char *p;
  394. if (n >= 100) {
  395. p = buf + sizeof(buf)-2;
  396. for (; n > 0 && p > buf; n /= 10, --digits)
  397. *p-- = n % 10 + '0';
  398. while (p > buf && digits-- > 0)
  399. *p-- = pad;
  400. p++;
  401. _util_strftime_add(p);
  402. } else {
  403. int tens;
  404. int ones = n;
  405. tens = 0;
  406. if ( ones >= 10 ) {
  407. while ( ones >= 10 ) {
  408. tens++;
  409. ones = ones - 10;
  410. }
  411. *pt++ = '0'+tens;
  412. digits--;
  413. }
  414. else
  415. *pt++ = '0';
  416. *pt++ = '0'+ones;
  417. digits--;
  418. while(digits--)
  419. *pt++ = pad;
  420. }
  421. return;
  422. }
  423. /*
  424. * Various reentrant routines by mikep. See util.h and systems.h
  425. */
  426. /*
  427. * These are only necessary if we turn on interrupts in NSPR
  428. */
  429. #ifdef NEED_RELOCKS
  430. #include "crit.h"
  431. #define RE_LOCK(name) \
  432. static CRITICAL name##_crit = 0; \
  433. if (name##_crit == 0) name##_crit = crit_init(); \
  434. crit_enter(name##_crit)
  435. #define RE_UNLOCK(name) crit_exit(name##_crit)
  436. #else
  437. #define RE_LOCK(name) /* nada */
  438. #define RE_UNLOCK(name) /* nil */
  439. #endif
  440. NSAPI_PUBLIC struct tm *
  441. util_localtime(const time_t *clock, struct tm *res)
  442. {
  443. #ifdef HAVE_TIME_R
  444. return localtime_r(clock, res);
  445. #else
  446. struct tm *rv;
  447. time_t zero = 0x7fffffff;
  448. RE_LOCK(localtime);
  449. RE_UNLOCK(localtime);
  450. rv = localtime(clock);
  451. if (!rv)
  452. rv = localtime(&zero);
  453. if (rv)
  454. *res = *rv;
  455. else
  456. return NULL;
  457. return res;
  458. #endif
  459. }