util.cpp 11 KB

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