util.cpp 13 KB

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