util.cpp 13 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515
  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. int rc;
  105. va_list args;
  106. va_start(args, fmt);
  107. rc = PR_vsnprintf(s, n, fmt, args);
  108. va_end(args);
  109. return rc;
  110. }
  111. NSAPI_PUBLIC int util_vsprintf(char *s, register const char *fmt, va_list args)
  112. {
  113. return PR_vsnprintf(s, UTIL_PRF_MAXSIZE, fmt, args);
  114. }
  115. NSAPI_PUBLIC int util_sprintf(char *s, const char *fmt, ...)
  116. {
  117. int rc;
  118. va_list args;
  119. va_start(args, fmt);
  120. rc = PR_vsnprintf(s, UTIL_PRF_MAXSIZE, fmt, args);
  121. va_end(args);
  122. return rc;
  123. }
  124. /* --------------------------- util_strcasecmp ---------------------------- */
  125. #ifdef NEED_STRCASECMP
  126. /* These are stolen from mcom/lib/xp */
  127. NSAPI_PUBLIC
  128. int util_strcasecmp(CASECMPARG_T char *one, CASECMPARG_T char *two)
  129. {
  130. CASECMPARG_T char *pA;
  131. CASECMPARG_T char *pB;
  132. for(pA=one, pB=two; *pA && *pB; pA++, pB++)
  133. {
  134. int tmp = tolower(*pA) - tolower(*pB);
  135. if (tmp)
  136. return tmp;
  137. }
  138. if (*pA)
  139. return 1;
  140. if (*pB)
  141. return -1;
  142. return 0;
  143. }
  144. #endif /* NEED_STRCASECMP */
  145. #ifdef NEED_STRNCASECMP
  146. NSAPI_PUBLIC
  147. int util_strncasecmp(CASECMPARG_T char *one, CASECMPARG_T char *two, int n)
  148. {
  149. CASECMPARG_T char *pA;
  150. CASECMPARG_T char *pB;
  151. for(pA=one, pB=two;; pA++, pB++)
  152. {
  153. int tmp;
  154. if (pA == one+n)
  155. return 0;
  156. if (!(*pA && *pB))
  157. return *pA - *pB;
  158. tmp = tolower(*pA) - tolower(*pB);
  159. if (tmp)
  160. return tmp;
  161. }
  162. }
  163. #endif /* NEED_STRNCASECMP */
  164. /* ------------------------------ util_strftime --------------------------- */
  165. /*
  166. * Copyright (c) 1989 The Regents of the University of California.
  167. * All rights reserved.
  168. *
  169. * Redistribution and use in source and binary forms, with or without
  170. * modification, are permitted provided that the following conditions
  171. * are met:
  172. * 1. Redistributions of source code must retain the above copyright
  173. * notice, this list of conditions and the following disclaimer.
  174. * 2. Redistributions in binary form must reproduce the above copyright
  175. * notice, this list of conditions and the following disclaimer in the
  176. * documentation and/or other materials provided with the distribution.
  177. * 3. All advertising materials mentioning features or use of this software
  178. * must display the following acknowledgement:
  179. * This product includes software developed by the University of
  180. * California, Berkeley and its contributors.
  181. * 4. Neither the name of the University nor the names of its contributors
  182. * may be used to endorse or promote products derived from this software
  183. * without specific prior written permission.
  184. *
  185. * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
  186. * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
  187. * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
  188. * ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
  189. * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
  190. * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
  191. * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
  192. * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
  193. * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
  194. * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
  195. * SUCH DAMAGE.
  196. */
  197. #if defined(LIBC_SCCS) && !defined(lint)
  198. static char sccsid[] = "@(#)strftime.c 5.11 (Berkeley) 2/24/91";
  199. #endif /* LIBC_SCCS and not lint */
  200. #ifdef XP_UNIX
  201. #include <sys/types.h>
  202. #include <sys/time.h>
  203. #include <string.h>
  204. #include <stdio.h>
  205. #endif
  206. static char *afmt[] = {
  207. "Sun", "Mon", "Tue", "Wed", "Thu", "Fri", "Sat",
  208. };
  209. static char *Afmt[] = {
  210. "Sunday", "Monday", "Tuesday", "Wednesday", "Thursday", "Friday",
  211. "Saturday",
  212. };
  213. static char *bfmt[] = {
  214. "Jan", "Feb", "Mar", "Apr", "May", "Jun", "Jul", "Aug", "Sep",
  215. "Oct", "Nov", "Dec",
  216. };
  217. static char *Bfmt[] = {
  218. "January", "February", "March", "April", "May", "June", "July",
  219. "August", "September", "October", "November", "December",
  220. };
  221. #define TM_YEAR_BASE 1900
  222. static void _util_strftime_conv(char *, int, int, char);
  223. #define _util_strftime_add(str) for (;(*pt = *str++); pt++);
  224. #define _util_strftime_copy(str, len) memcpy(pt, str, len); pt += len;
  225. #define _util_strftime_fmt util_strftime
  226. /* util_strftime()
  227. * This is an optimized version of strftime for speed. Avoids the thread
  228. * unsafeness of BSD strftime calls.
  229. */
  230. int
  231. util_strftime(char *pt, const char *format, const struct tm *t)
  232. {
  233. char *start = pt;
  234. char *scrap;
  235. for (; *format; ++format) {
  236. if (*format == '%')
  237. switch(*++format) {
  238. case 'a': /* abbreviated weekday name */
  239. *pt++ = afmt[t->tm_wday][0];
  240. *pt++ = afmt[t->tm_wday][1];
  241. *pt++ = afmt[t->tm_wday][2];
  242. continue;
  243. case 'd': /* day of month */
  244. _util_strftime_conv(pt, t->tm_mday, 2, '0');
  245. pt += 2;
  246. continue;
  247. case 'S':
  248. _util_strftime_conv(pt, t->tm_sec, 2, '0');
  249. pt += 2;
  250. continue;
  251. case 'M':
  252. _util_strftime_conv(pt, t->tm_min, 2, '0');
  253. pt += 2;
  254. continue;
  255. case 'H':
  256. _util_strftime_conv(pt, t->tm_hour, 2, '0');
  257. pt += 2;
  258. continue;
  259. case 'Y':
  260. if (t->tm_year < 100) {
  261. *pt++ = '1';
  262. *pt++ = '9';
  263. _util_strftime_conv(pt, t->tm_year, 2, '0');
  264. } else {
  265. /* will fail after 2100; but who cares? */
  266. *pt++ = '2';
  267. *pt++ = '0';
  268. _util_strftime_conv(pt, t->tm_year-100, 2, '0');
  269. }
  270. pt += 2;
  271. continue;
  272. case 'b': /* abbreviated month name */
  273. case 'h':
  274. *pt++ = bfmt[t->tm_mon][0];
  275. *pt++ = bfmt[t->tm_mon][1];
  276. *pt++ = bfmt[t->tm_mon][2];
  277. continue;
  278. case 'T':
  279. case 'X':
  280. pt += _util_strftime_fmt(pt, "%H:%M:%S", t);
  281. continue;
  282. case '\0':
  283. --format;
  284. break;
  285. case 'A':
  286. if (t->tm_wday < 0 || t->tm_wday > 6)
  287. return(0);
  288. scrap = Afmt[t->tm_wday];
  289. _util_strftime_add(scrap);
  290. continue;
  291. case 'B':
  292. if (t->tm_mon < 0 || t->tm_mon > 11)
  293. return(0);
  294. scrap = Bfmt[t->tm_mon];
  295. _util_strftime_add(scrap);
  296. continue;
  297. case 'C':
  298. pt += _util_strftime_fmt(pt, "%a %b %e %H:%M:%S %Y", t);
  299. continue;
  300. case 'c':
  301. pt += _util_strftime_fmt(pt, "%m/%d/%y %H:%M:%S", t);
  302. continue;
  303. case 'D':
  304. pt += _util_strftime_fmt(pt, "%m/%d/%y", t);
  305. continue;
  306. case 'e':
  307. _util_strftime_conv(pt, t->tm_mday, 2, ' ');
  308. pt += 2;
  309. continue;
  310. case 'I':
  311. _util_strftime_conv(pt, t->tm_hour % 12 ?
  312. t->tm_hour % 12 : 12, 2, '0');
  313. pt += 2;
  314. continue;
  315. case 'j':
  316. _util_strftime_conv(pt, t->tm_yday + 1, 3, '0');
  317. pt += 3;
  318. continue;
  319. case 'k':
  320. _util_strftime_conv(pt, t->tm_hour, 2, ' ');
  321. pt += 2;
  322. continue;
  323. case 'l':
  324. _util_strftime_conv(pt, t->tm_hour % 12 ?
  325. t->tm_hour % 12 : 12, 2, ' ');
  326. pt += 2;
  327. continue;
  328. case 'm':
  329. _util_strftime_conv(pt, t->tm_mon + 1, 2, '0');
  330. pt += 2;
  331. continue;
  332. case 'n':
  333. *pt = '\n';
  334. pt++;
  335. continue;
  336. case 'p':
  337. if (t->tm_hour >= 12) {
  338. *pt = 'P';
  339. pt++;
  340. } else {
  341. *pt = 'A';
  342. pt++;
  343. }
  344. *pt = 'M';
  345. pt++;
  346. continue;
  347. case 'R':
  348. pt += _util_strftime_fmt(pt, "%H:%M", t);
  349. continue;
  350. case 'r':
  351. pt += _util_strftime_fmt(pt, "%I:%M:%S %p", t);
  352. continue;
  353. case 't':
  354. *pt = '\t';
  355. pt++;
  356. continue;
  357. case 'U':
  358. _util_strftime_conv(pt, (t->tm_yday + 7 - t->tm_wday) / 7,
  359. 2, '0');
  360. pt += 2;
  361. continue;
  362. case 'W':
  363. _util_strftime_conv(pt, (t->tm_yday + 7 -
  364. (t->tm_wday ? (t->tm_wday - 1) : 6))
  365. / 7, 2, '0');
  366. pt += 2;
  367. continue;
  368. case 'w':
  369. _util_strftime_conv(pt, t->tm_wday, 1, '0');
  370. pt += 1;
  371. continue;
  372. case 'x':
  373. pt += _util_strftime_fmt(pt, "%m/%d/%y", t);
  374. continue;
  375. case 'y':
  376. _util_strftime_conv(pt, (t->tm_year + TM_YEAR_BASE)
  377. % 100, 2, '0');
  378. pt += 2;
  379. continue;
  380. case '%':
  381. /*
  382. * X311J/88-090 (4.12.3.5): if conversion char is
  383. * undefined, behavior is undefined. Print out the
  384. * character itself as printf(3) does.
  385. */
  386. default:
  387. break;
  388. }
  389. *pt = *format;
  390. pt++;
  391. }
  392. start[pt-start] = '\0';
  393. return pt - start;
  394. }
  395. static void
  396. _util_strftime_conv(char *pt, int n, int digits, char pad)
  397. {
  398. static char buf[10];
  399. register char *p;
  400. if (n >= 100) {
  401. p = buf + sizeof(buf)-2;
  402. for (; n > 0 && p > buf; n /= 10, --digits)
  403. *p-- = n % 10 + '0';
  404. while (p > buf && digits-- > 0)
  405. *p-- = pad;
  406. p++;
  407. _util_strftime_add(p);
  408. } else {
  409. int tens;
  410. int ones = n;
  411. tens = 0;
  412. if ( ones >= 10 ) {
  413. while ( ones >= 10 ) {
  414. tens++;
  415. ones = ones - 10;
  416. }
  417. *pt++ = '0'+tens;
  418. digits--;
  419. }
  420. else
  421. *pt++ = '0';
  422. *pt++ = '0'+ones;
  423. digits--;
  424. while(digits--)
  425. *pt++ = pad;
  426. }
  427. return;
  428. }
  429. /*
  430. * Various reentrant routines by mikep. See util.h and systems.h
  431. */
  432. /*
  433. * These are only necessary if we turn on interrupts in NSPR
  434. */
  435. #ifdef NEED_RELOCKS
  436. #include "crit.h"
  437. #define RE_LOCK(name) \
  438. static CRITICAL name##_crit = 0; \
  439. if (name##_crit == 0) name##_crit = crit_init(); \
  440. crit_enter(name##_crit)
  441. #define RE_UNLOCK(name) crit_exit(name##_crit)
  442. #else
  443. #define RE_LOCK(name) /* nada */
  444. #define RE_UNLOCK(name) /* nil */
  445. #endif
  446. NSAPI_PUBLIC struct tm *
  447. util_localtime(const time_t *clock, struct tm *res)
  448. {
  449. #ifdef HAVE_TIME_R
  450. return localtime_r(clock, res);
  451. #else
  452. struct tm *rv;
  453. time_t zero = 0x7fffffff;
  454. RE_LOCK(localtime);
  455. RE_UNLOCK(localtime);
  456. rv = localtime(clock);
  457. if (!rv)
  458. rv = localtime(&zero);
  459. if (rv)
  460. *res = *rv;
  461. else
  462. return NULL;
  463. return res;
  464. #endif
  465. }