util.cpp 13 KB

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