PageRenderTime 57ms CodeModel.GetById 19ms RepoModel.GetById 1ms app.codeStats 0ms

/xbmc/lib/libmms/glib-2.20.4/glib/gstrfuncs.c

http://github.com/elan/plex
C | 2612 lines | 1806 code | 208 blank | 598 comment | 298 complexity | f0391b9260d0a10bb6d628350854d556 MD5 | raw file
Possible License(s): LGPL-2.0, LGPL-3.0, CC-BY-SA-3.0, 0BSD, LGPL-2.1, GPL-3.0, BSD-3-Clause, CC0-1.0, Unlicense, GPL-2.0, AGPL-1.0

Large files files are truncated, but you can click here to view the full file

  1. /* GLIB - Library of useful routines for C programming
  2. * Copyright (C) 1995-1997 Peter Mattis, Spencer Kimball and Josh MacDonald
  3. *
  4. * This library is free software; you can redistribute it and/or
  5. * modify it under the terms of the GNU Lesser General Public
  6. * License as published by the Free Software Foundation; either
  7. * version 2 of the License, or (at your option) any later version.
  8. *
  9. * This library is distributed in the hope that it will be useful,
  10. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  11. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
  12. * Lesser General Public License for more details.
  13. *
  14. * You should have received a copy of the GNU Lesser General Public
  15. * License along with this library; if not, write to the
  16. * Free Software Foundation, Inc., 59 Temple Place - Suite 330,
  17. * Boston, MA 02111-1307, USA.
  18. */
  19. /*
  20. * Modified by the GLib Team and others 1997-2000. See the AUTHORS
  21. * file for a list of people on the GLib Team. See the ChangeLog
  22. * files for a list of changes. These files are distributed with
  23. * GLib at ftp://ftp.gtk.org/pub/gtk/.
  24. */
  25. /*
  26. * MT safe
  27. */
  28. #include "config.h"
  29. #define _GNU_SOURCE /* For stpcpy */
  30. #include <stdarg.h>
  31. #include <stdio.h>
  32. #include <stdlib.h>
  33. #include <string.h>
  34. #include <locale.h>
  35. #include <errno.h>
  36. #include <ctype.h> /* For tolower() */
  37. #if !defined (HAVE_STRSIGNAL) || !defined(NO_SYS_SIGLIST_DECL)
  38. #include <signal.h>
  39. #endif
  40. #include "glib.h"
  41. #include "gprintf.h"
  42. #include "gprintfint.h"
  43. #include "glibintl.h"
  44. #include "galias.h"
  45. #ifdef G_OS_WIN32
  46. #include <windows.h>
  47. #endif
  48. /* do not include <unistd.h> in this place since it
  49. * interferes with g_strsignal() on some OSes
  50. */
  51. static const guint16 ascii_table_data[256] = {
  52. 0x004, 0x004, 0x004, 0x004, 0x004, 0x004, 0x004, 0x004,
  53. 0x004, 0x104, 0x104, 0x004, 0x104, 0x104, 0x004, 0x004,
  54. 0x004, 0x004, 0x004, 0x004, 0x004, 0x004, 0x004, 0x004,
  55. 0x004, 0x004, 0x004, 0x004, 0x004, 0x004, 0x004, 0x004,
  56. 0x140, 0x0d0, 0x0d0, 0x0d0, 0x0d0, 0x0d0, 0x0d0, 0x0d0,
  57. 0x0d0, 0x0d0, 0x0d0, 0x0d0, 0x0d0, 0x0d0, 0x0d0, 0x0d0,
  58. 0x459, 0x459, 0x459, 0x459, 0x459, 0x459, 0x459, 0x459,
  59. 0x459, 0x459, 0x0d0, 0x0d0, 0x0d0, 0x0d0, 0x0d0, 0x0d0,
  60. 0x0d0, 0x653, 0x653, 0x653, 0x653, 0x653, 0x653, 0x253,
  61. 0x253, 0x253, 0x253, 0x253, 0x253, 0x253, 0x253, 0x253,
  62. 0x253, 0x253, 0x253, 0x253, 0x253, 0x253, 0x253, 0x253,
  63. 0x253, 0x253, 0x253, 0x0d0, 0x0d0, 0x0d0, 0x0d0, 0x0d0,
  64. 0x0d0, 0x473, 0x473, 0x473, 0x473, 0x473, 0x473, 0x073,
  65. 0x073, 0x073, 0x073, 0x073, 0x073, 0x073, 0x073, 0x073,
  66. 0x073, 0x073, 0x073, 0x073, 0x073, 0x073, 0x073, 0x073,
  67. 0x073, 0x073, 0x073, 0x0d0, 0x0d0, 0x0d0, 0x0d0, 0x004
  68. /* the upper 128 are all zeroes */
  69. };
  70. const guint16 * const g_ascii_table = ascii_table_data;
  71. /**
  72. * g_strdup:
  73. * @str: the string to duplicate
  74. *
  75. * Duplicates a string. If @str is %NULL it returns %NULL.
  76. * The returned string should be freed with g_free()
  77. * when no longer needed.
  78. *
  79. * Returns: a newly-allocated copy of @str
  80. */
  81. gchar*
  82. g_strdup (const gchar *str)
  83. {
  84. gchar *new_str;
  85. gsize length;
  86. if (str)
  87. {
  88. length = strlen (str) + 1;
  89. new_str = g_new (char, length);
  90. memcpy (new_str, str, length);
  91. }
  92. else
  93. new_str = NULL;
  94. return new_str;
  95. }
  96. gpointer
  97. g_memdup (gconstpointer mem,
  98. guint byte_size)
  99. {
  100. gpointer new_mem;
  101. if (mem)
  102. {
  103. new_mem = g_malloc (byte_size);
  104. memcpy (new_mem, mem, byte_size);
  105. }
  106. else
  107. new_mem = NULL;
  108. return new_mem;
  109. }
  110. /**
  111. * g_strndup:
  112. * @str: the string to duplicate
  113. * @n: the maximum number of bytes to copy from @str
  114. *
  115. * Duplicates the first @n bytes of a string, returning a newly-allocated
  116. * buffer @n + 1 bytes long which will always be nul-terminated.
  117. * If @str is less than @n bytes long the buffer is padded with nuls.
  118. * If @str is %NULL it returns %NULL.
  119. * The returned value should be freed when no longer needed.
  120. *
  121. * <note><para>
  122. * To copy a number of characters from a UTF-8 encoded string, use
  123. * g_utf8_strncpy() instead.
  124. * </para></note>
  125. *
  126. * Returns: a newly-allocated buffer containing the first @n bytes
  127. * of @str, nul-terminated
  128. */
  129. gchar*
  130. g_strndup (const gchar *str,
  131. gsize n)
  132. {
  133. gchar *new_str;
  134. if (str)
  135. {
  136. new_str = g_new (gchar, n + 1);
  137. strncpy (new_str, str, n);
  138. new_str[n] = '\0';
  139. }
  140. else
  141. new_str = NULL;
  142. return new_str;
  143. }
  144. /**
  145. * g_strnfill:
  146. * @length: the length of the new string
  147. * @fill_char: the byte to fill the string with
  148. *
  149. * Creates a new string @length bytes long filled with @fill_char.
  150. * The returned string should be freed when no longer needed.
  151. *
  152. * Returns: a newly-allocated string filled the @fill_char
  153. */
  154. gchar*
  155. g_strnfill (gsize length,
  156. gchar fill_char)
  157. {
  158. gchar *str;
  159. str = g_new (gchar, length + 1);
  160. memset (str, (guchar)fill_char, length);
  161. str[length] = '\0';
  162. return str;
  163. }
  164. /**
  165. * g_stpcpy:
  166. * @dest: destination buffer.
  167. * @src: source string.
  168. *
  169. * Copies a nul-terminated string into the dest buffer, include the
  170. * trailing nul, and return a pointer to the trailing nul byte.
  171. * This is useful for concatenating multiple strings together
  172. * without having to repeatedly scan for the end.
  173. *
  174. * Return value: a pointer to trailing nul byte.
  175. **/
  176. gchar *
  177. g_stpcpy (gchar *dest,
  178. const gchar *src)
  179. {
  180. #ifdef HAVE_STPCPY
  181. g_return_val_if_fail (dest != NULL, NULL);
  182. g_return_val_if_fail (src != NULL, NULL);
  183. return stpcpy (dest, src);
  184. #else
  185. register gchar *d = dest;
  186. register const gchar *s = src;
  187. g_return_val_if_fail (dest != NULL, NULL);
  188. g_return_val_if_fail (src != NULL, NULL);
  189. do
  190. *d++ = *s;
  191. while (*s++ != '\0');
  192. return d - 1;
  193. #endif
  194. }
  195. /**
  196. * g_strdup_vprintf:
  197. * @format: a standard printf() format string, but notice
  198. * <link linkend="string-precision">string precision pitfalls</link>
  199. * @args: the list of parameters to insert into the format string
  200. *
  201. * Similar to the standard C vsprintf() function but safer, since it
  202. * calculates the maximum space required and allocates memory to hold
  203. * the result. The returned string should be freed with g_free() when
  204. * no longer needed.
  205. *
  206. * See also g_vasprintf(), which offers the same functionality, but
  207. * additionally returns the length of the allocated string.
  208. *
  209. * Returns: a newly-allocated string holding the result
  210. */
  211. gchar*
  212. g_strdup_vprintf (const gchar *format,
  213. va_list args)
  214. {
  215. gchar *string = NULL;
  216. g_vasprintf (&string, format, args);
  217. return string;
  218. }
  219. /**
  220. * g_strdup_printf:
  221. * @format: a standard printf() format string, but notice
  222. * <link linkend="string-precision">string precision pitfalls</link>
  223. * @Varargs: the parameters to insert into the format string
  224. *
  225. * Similar to the standard C sprintf() function but safer, since it
  226. * calculates the maximum space required and allocates memory to hold
  227. * the result. The returned string should be freed with g_free() when no
  228. * longer needed.
  229. *
  230. * Returns: a newly-allocated string holding the result
  231. */
  232. gchar*
  233. g_strdup_printf (const gchar *format,
  234. ...)
  235. {
  236. gchar *buffer;
  237. va_list args;
  238. va_start (args, format);
  239. buffer = g_strdup_vprintf (format, args);
  240. va_end (args);
  241. return buffer;
  242. }
  243. /**
  244. * g_strconcat:
  245. * @string1: the first string to add, which must not be %NULL
  246. * @Varargs: a %NULL-terminated list of strings to append to the string
  247. *
  248. * Concatenates all of the given strings into one long string.
  249. * The returned string should be freed with g_free() when no longer needed.
  250. *
  251. *
  252. * <warning><para>The variable argument list <emphasis>must</emphasis> end
  253. * with %NULL. If you forget the %NULL, g_strconcat() will start appending
  254. * random memory junk to your string.</para></warning>
  255. *
  256. * Returns: a newly-allocated string containing all the string arguments
  257. */
  258. gchar*
  259. g_strconcat (const gchar *string1, ...)
  260. {
  261. gsize l;
  262. va_list args;
  263. gchar *s;
  264. gchar *concat;
  265. gchar *ptr;
  266. if (!string1)
  267. return NULL;
  268. l = 1 + strlen (string1);
  269. va_start (args, string1);
  270. s = va_arg (args, gchar*);
  271. while (s)
  272. {
  273. l += strlen (s);
  274. s = va_arg (args, gchar*);
  275. }
  276. va_end (args);
  277. concat = g_new (gchar, l);
  278. ptr = concat;
  279. ptr = g_stpcpy (ptr, string1);
  280. va_start (args, string1);
  281. s = va_arg (args, gchar*);
  282. while (s)
  283. {
  284. ptr = g_stpcpy (ptr, s);
  285. s = va_arg (args, gchar*);
  286. }
  287. va_end (args);
  288. return concat;
  289. }
  290. /**
  291. * g_strtod:
  292. * @nptr: the string to convert to a numeric value.
  293. * @endptr: if non-%NULL, it returns the character after
  294. * the last character used in the conversion.
  295. *
  296. * Converts a string to a #gdouble value.
  297. * It calls the standard strtod() function to handle the conversion, but
  298. * if the string is not completely converted it attempts the conversion
  299. * again with g_ascii_strtod(), and returns the best match.
  300. *
  301. * This function should seldomly be used. The normal situation when reading
  302. * numbers not for human consumption is to use g_ascii_strtod(). Only when
  303. * you know that you must expect both locale formatted and C formatted numbers
  304. * should you use this. Make sure that you don't pass strings such as comma
  305. * separated lists of values, since the commas may be interpreted as a decimal
  306. * point in some locales, causing unexpected results.
  307. *
  308. * Return value: the #gdouble value.
  309. **/
  310. gdouble
  311. g_strtod (const gchar *nptr,
  312. gchar **endptr)
  313. {
  314. gchar *fail_pos_1;
  315. gchar *fail_pos_2;
  316. gdouble val_1;
  317. gdouble val_2 = 0;
  318. g_return_val_if_fail (nptr != NULL, 0);
  319. fail_pos_1 = NULL;
  320. fail_pos_2 = NULL;
  321. val_1 = strtod (nptr, &fail_pos_1);
  322. if (fail_pos_1 && fail_pos_1[0] != 0)
  323. val_2 = g_ascii_strtod (nptr, &fail_pos_2);
  324. if (!fail_pos_1 || fail_pos_1[0] == 0 || fail_pos_1 >= fail_pos_2)
  325. {
  326. if (endptr)
  327. *endptr = fail_pos_1;
  328. return val_1;
  329. }
  330. else
  331. {
  332. if (endptr)
  333. *endptr = fail_pos_2;
  334. return val_2;
  335. }
  336. }
  337. /**
  338. * g_ascii_strtod:
  339. * @nptr: the string to convert to a numeric value.
  340. * @endptr: if non-%NULL, it returns the character after
  341. * the last character used in the conversion.
  342. *
  343. * Converts a string to a #gdouble value.
  344. *
  345. * This function behaves like the standard strtod() function
  346. * does in the C locale. It does this without actually changing
  347. * the current locale, since that would not be thread-safe.
  348. * A limitation of the implementation is that this function
  349. * will still accept localized versions of infinities and NANs.
  350. *
  351. * This function is typically used when reading configuration
  352. * files or other non-user input that should be locale independent.
  353. * To handle input from the user you should normally use the
  354. * locale-sensitive system strtod() function.
  355. *
  356. * To convert from a #gdouble to a string in a locale-insensitive
  357. * way, use g_ascii_dtostr().
  358. *
  359. * If the correct value would cause overflow, plus or minus %HUGE_VAL
  360. * is returned (according to the sign of the value), and %ERANGE is
  361. * stored in %errno. If the correct value would cause underflow,
  362. * zero is returned and %ERANGE is stored in %errno.
  363. *
  364. * This function resets %errno before calling strtod() so that
  365. * you can reliably detect overflow and underflow.
  366. *
  367. * Return value: the #gdouble value.
  368. **/
  369. gdouble
  370. g_ascii_strtod (const gchar *nptr,
  371. gchar **endptr)
  372. {
  373. gchar *fail_pos;
  374. gdouble val;
  375. struct lconv *locale_data;
  376. const char *decimal_point;
  377. int decimal_point_len;
  378. const char *p, *decimal_point_pos;
  379. const char *end = NULL; /* Silence gcc */
  380. int strtod_errno;
  381. g_return_val_if_fail (nptr != NULL, 0);
  382. fail_pos = NULL;
  383. locale_data = localeconv ();
  384. decimal_point = locale_data->decimal_point;
  385. decimal_point_len = strlen (decimal_point);
  386. g_assert (decimal_point_len != 0);
  387. decimal_point_pos = NULL;
  388. end = NULL;
  389. if (decimal_point[0] != '.' ||
  390. decimal_point[1] != 0)
  391. {
  392. p = nptr;
  393. /* Skip leading space */
  394. while (g_ascii_isspace (*p))
  395. p++;
  396. /* Skip leading optional sign */
  397. if (*p == '+' || *p == '-')
  398. p++;
  399. if (p[0] == '0' &&
  400. (p[1] == 'x' || p[1] == 'X'))
  401. {
  402. p += 2;
  403. /* HEX - find the (optional) decimal point */
  404. while (g_ascii_isxdigit (*p))
  405. p++;
  406. if (*p == '.')
  407. decimal_point_pos = p++;
  408. while (g_ascii_isxdigit (*p))
  409. p++;
  410. if (*p == 'p' || *p == 'P')
  411. p++;
  412. if (*p == '+' || *p == '-')
  413. p++;
  414. while (g_ascii_isdigit (*p))
  415. p++;
  416. end = p;
  417. }
  418. else if (g_ascii_isdigit (*p) || *p == '.')
  419. {
  420. while (g_ascii_isdigit (*p))
  421. p++;
  422. if (*p == '.')
  423. decimal_point_pos = p++;
  424. while (g_ascii_isdigit (*p))
  425. p++;
  426. if (*p == 'e' || *p == 'E')
  427. p++;
  428. if (*p == '+' || *p == '-')
  429. p++;
  430. while (g_ascii_isdigit (*p))
  431. p++;
  432. end = p;
  433. }
  434. /* For the other cases, we need not convert the decimal point */
  435. }
  436. if (decimal_point_pos)
  437. {
  438. char *copy, *c;
  439. /* We need to convert the '.' to the locale specific decimal point */
  440. copy = g_malloc (end - nptr + 1 + decimal_point_len);
  441. c = copy;
  442. memcpy (c, nptr, decimal_point_pos - nptr);
  443. c += decimal_point_pos - nptr;
  444. memcpy (c, decimal_point, decimal_point_len);
  445. c += decimal_point_len;
  446. memcpy (c, decimal_point_pos + 1, end - (decimal_point_pos + 1));
  447. c += end - (decimal_point_pos + 1);
  448. *c = 0;
  449. errno = 0;
  450. val = strtod (copy, &fail_pos);
  451. strtod_errno = errno;
  452. if (fail_pos)
  453. {
  454. if (fail_pos - copy > decimal_point_pos - nptr)
  455. fail_pos = (char *)nptr + (fail_pos - copy) - (decimal_point_len - 1);
  456. else
  457. fail_pos = (char *)nptr + (fail_pos - copy);
  458. }
  459. g_free (copy);
  460. }
  461. else if (end)
  462. {
  463. char *copy;
  464. copy = g_malloc (end - (char *)nptr + 1);
  465. memcpy (copy, nptr, end - nptr);
  466. *(copy + (end - (char *)nptr)) = 0;
  467. errno = 0;
  468. val = strtod (copy, &fail_pos);
  469. strtod_errno = errno;
  470. if (fail_pos)
  471. {
  472. fail_pos = (char *)nptr + (fail_pos - copy);
  473. }
  474. g_free (copy);
  475. }
  476. else
  477. {
  478. errno = 0;
  479. val = strtod (nptr, &fail_pos);
  480. strtod_errno = errno;
  481. }
  482. if (endptr)
  483. *endptr = fail_pos;
  484. errno = strtod_errno;
  485. return val;
  486. }
  487. /**
  488. * g_ascii_dtostr:
  489. * @buffer: A buffer to place the resulting string in
  490. * @buf_len: The length of the buffer.
  491. * @d: The #gdouble to convert
  492. *
  493. * Converts a #gdouble to a string, using the '.' as
  494. * decimal point.
  495. *
  496. * This functions generates enough precision that converting
  497. * the string back using g_ascii_strtod() gives the same machine-number
  498. * (on machines with IEEE compatible 64bit doubles). It is
  499. * guaranteed that the size of the resulting string will never
  500. * be larger than @G_ASCII_DTOSTR_BUF_SIZE bytes.
  501. *
  502. * Return value: The pointer to the buffer with the converted string.
  503. **/
  504. gchar *
  505. g_ascii_dtostr (gchar *buffer,
  506. gint buf_len,
  507. gdouble d)
  508. {
  509. return g_ascii_formatd (buffer, buf_len, "%.17g", d);
  510. }
  511. /**
  512. * g_ascii_formatd:
  513. * @buffer: A buffer to place the resulting string in
  514. * @buf_len: The length of the buffer.
  515. * @format: The printf()-style format to use for the
  516. * code to use for converting.
  517. * @d: The #gdouble to convert
  518. *
  519. * Converts a #gdouble to a string, using the '.' as
  520. * decimal point. To format the number you pass in
  521. * a printf()-style format string. Allowed conversion
  522. * specifiers are 'e', 'E', 'f', 'F', 'g' and 'G'.
  523. *
  524. * If you just want to want to serialize the value into a
  525. * string, use g_ascii_dtostr().
  526. *
  527. * Return value: The pointer to the buffer with the converted string.
  528. */
  529. gchar *
  530. g_ascii_formatd (gchar *buffer,
  531. gint buf_len,
  532. const gchar *format,
  533. gdouble d)
  534. {
  535. struct lconv *locale_data;
  536. const char *decimal_point;
  537. int decimal_point_len;
  538. gchar *p;
  539. int rest_len;
  540. gchar format_char;
  541. g_return_val_if_fail (buffer != NULL, NULL);
  542. g_return_val_if_fail (format[0] == '%', NULL);
  543. g_return_val_if_fail (strpbrk (format + 1, "'l%") == NULL, NULL);
  544. format_char = format[strlen (format) - 1];
  545. g_return_val_if_fail (format_char == 'e' || format_char == 'E' ||
  546. format_char == 'f' || format_char == 'F' ||
  547. format_char == 'g' || format_char == 'G',
  548. NULL);
  549. if (format[0] != '%')
  550. return NULL;
  551. if (strpbrk (format + 1, "'l%"))
  552. return NULL;
  553. if (!(format_char == 'e' || format_char == 'E' ||
  554. format_char == 'f' || format_char == 'F' ||
  555. format_char == 'g' || format_char == 'G'))
  556. return NULL;
  557. _g_snprintf (buffer, buf_len, format, d);
  558. locale_data = localeconv ();
  559. decimal_point = locale_data->decimal_point;
  560. decimal_point_len = strlen (decimal_point);
  561. g_assert (decimal_point_len != 0);
  562. if (decimal_point[0] != '.' ||
  563. decimal_point[1] != 0)
  564. {
  565. p = buffer;
  566. while (g_ascii_isspace (*p))
  567. p++;
  568. if (*p == '+' || *p == '-')
  569. p++;
  570. while (isdigit ((guchar)*p))
  571. p++;
  572. if (strncmp (p, decimal_point, decimal_point_len) == 0)
  573. {
  574. *p = '.';
  575. p++;
  576. if (decimal_point_len > 1)
  577. {
  578. rest_len = strlen (p + (decimal_point_len-1));
  579. memmove (p, p + (decimal_point_len-1), rest_len);
  580. p[rest_len] = 0;
  581. }
  582. }
  583. }
  584. return buffer;
  585. }
  586. static guint64
  587. g_parse_long_long (const gchar *nptr,
  588. const gchar **endptr,
  589. guint base,
  590. gboolean *negative)
  591. {
  592. /* this code is based on on the strtol(3) code from GNU libc released under
  593. * the GNU Lesser General Public License.
  594. *
  595. * Copyright (C) 1991,92,94,95,96,97,98,99,2000,01,02
  596. * Free Software Foundation, Inc.
  597. */
  598. #define ISSPACE(c) ((c) == ' ' || (c) == '\f' || (c) == '\n' || \
  599. (c) == '\r' || (c) == '\t' || (c) == '\v')
  600. #define ISUPPER(c) ((c) >= 'A' && (c) <= 'Z')
  601. #define ISLOWER(c) ((c) >= 'a' && (c) <= 'z')
  602. #define ISALPHA(c) (ISUPPER (c) || ISLOWER (c))
  603. #define TOUPPER(c) (ISLOWER (c) ? (c) - 'a' + 'A' : (c))
  604. #define TOLOWER(c) (ISUPPER (c) ? (c) - 'A' + 'a' : (c))
  605. gboolean overflow;
  606. guint64 cutoff;
  607. guint64 cutlim;
  608. guint64 ui64;
  609. const gchar *s, *save;
  610. guchar c;
  611. g_return_val_if_fail (nptr != NULL, 0);
  612. *negative = FALSE;
  613. if (base == 1 || base > 36)
  614. {
  615. errno = EINVAL;
  616. if (endptr)
  617. *endptr = nptr;
  618. return 0;
  619. }
  620. save = s = nptr;
  621. /* Skip white space. */
  622. while (ISSPACE (*s))
  623. ++s;
  624. if (G_UNLIKELY (!*s))
  625. goto noconv;
  626. /* Check for a sign. */
  627. if (*s == '-')
  628. {
  629. *negative = TRUE;
  630. ++s;
  631. }
  632. else if (*s == '+')
  633. ++s;
  634. /* Recognize number prefix and if BASE is zero, figure it out ourselves. */
  635. if (*s == '0')
  636. {
  637. if ((base == 0 || base == 16) && TOUPPER (s[1]) == 'X')
  638. {
  639. s += 2;
  640. base = 16;
  641. }
  642. else if (base == 0)
  643. base = 8;
  644. }
  645. else if (base == 0)
  646. base = 10;
  647. /* Save the pointer so we can check later if anything happened. */
  648. save = s;
  649. cutoff = G_MAXUINT64 / base;
  650. cutlim = G_MAXUINT64 % base;
  651. overflow = FALSE;
  652. ui64 = 0;
  653. c = *s;
  654. for (; c; c = *++s)
  655. {
  656. if (c >= '0' && c <= '9')
  657. c -= '0';
  658. else if (ISALPHA (c))
  659. c = TOUPPER (c) - 'A' + 10;
  660. else
  661. break;
  662. if (c >= base)
  663. break;
  664. /* Check for overflow. */
  665. if (ui64 > cutoff || (ui64 == cutoff && c > cutlim))
  666. overflow = TRUE;
  667. else
  668. {
  669. ui64 *= base;
  670. ui64 += c;
  671. }
  672. }
  673. /* Check if anything actually happened. */
  674. if (s == save)
  675. goto noconv;
  676. /* Store in ENDPTR the address of one character
  677. past the last character we converted. */
  678. if (endptr)
  679. *endptr = s;
  680. if (G_UNLIKELY (overflow))
  681. {
  682. errno = ERANGE;
  683. return G_MAXUINT64;
  684. }
  685. return ui64;
  686. noconv:
  687. /* We must handle a special case here: the base is 0 or 16 and the
  688. first two characters are '0' and 'x', but the rest are no
  689. hexadecimal digits. This is no error case. We return 0 and
  690. ENDPTR points to the `x`. */
  691. if (endptr)
  692. {
  693. if (save - nptr >= 2 && TOUPPER (save[-1]) == 'X'
  694. && save[-2] == '0')
  695. *endptr = &save[-1];
  696. else
  697. /* There was no number to convert. */
  698. *endptr = nptr;
  699. }
  700. return 0;
  701. }
  702. /**
  703. * g_ascii_strtoull:
  704. * @nptr: the string to convert to a numeric value.
  705. * @endptr: if non-%NULL, it returns the character after
  706. * the last character used in the conversion.
  707. * @base: to be used for the conversion, 2..36 or 0
  708. *
  709. * Converts a string to a #guint64 value.
  710. * This function behaves like the standard strtoull() function
  711. * does in the C locale. It does this without actually
  712. * changing the current locale, since that would not be
  713. * thread-safe.
  714. *
  715. * This function is typically used when reading configuration
  716. * files or other non-user input that should be locale independent.
  717. * To handle input from the user you should normally use the
  718. * locale-sensitive system strtoull() function.
  719. *
  720. * If the correct value would cause overflow, %G_MAXUINT64
  721. * is returned, and %ERANGE is stored in %errno. If the base is
  722. * outside the valid range, zero is returned, and %EINVAL is stored
  723. * in %errno. If the string conversion fails, zero is returned, and
  724. * @endptr returns @nptr (if @endptr is non-%NULL).
  725. *
  726. * Return value: the #guint64 value or zero on error.
  727. *
  728. * Since: 2.2
  729. */
  730. guint64
  731. g_ascii_strtoull (const gchar *nptr,
  732. gchar **endptr,
  733. guint base)
  734. {
  735. gboolean negative;
  736. guint64 result;
  737. result = g_parse_long_long (nptr, (const gchar **) endptr, base, &negative);
  738. /* Return the result of the appropriate sign. */
  739. return negative ? -result : result;
  740. }
  741. /**
  742. * g_ascii_strtoll:
  743. * @nptr: the string to convert to a numeric value.
  744. * @endptr: if non-%NULL, it returns the character after
  745. * the last character used in the conversion.
  746. * @base: to be used for the conversion, 2..36 or 0
  747. *
  748. * Converts a string to a #gint64 value.
  749. * This function behaves like the standard strtoll() function
  750. * does in the C locale. It does this without actually
  751. * changing the current locale, since that would not be
  752. * thread-safe.
  753. *
  754. * This function is typically used when reading configuration
  755. * files or other non-user input that should be locale independent.
  756. * To handle input from the user you should normally use the
  757. * locale-sensitive system strtoll() function.
  758. *
  759. * If the correct value would cause overflow, %G_MAXINT64 or %G_MININT64
  760. * is returned, and %ERANGE is stored in %errno. If the base is
  761. * outside the valid range, zero is returned, and %EINVAL is stored
  762. * in %errno. If the string conversion fails, zero is returned, and
  763. * @endptr returns @nptr (if @endptr is non-%NULL).
  764. *
  765. * Return value: the #gint64 value or zero on error.
  766. *
  767. * Since: 2.12
  768. */
  769. gint64
  770. g_ascii_strtoll (const gchar *nptr,
  771. gchar **endptr,
  772. guint base)
  773. {
  774. gboolean negative;
  775. guint64 result;
  776. result = g_parse_long_long (nptr, (const gchar **) endptr, base, &negative);
  777. if (negative && result > (guint64) G_MININT64)
  778. {
  779. errno = ERANGE;
  780. return G_MININT64;
  781. }
  782. else if (!negative && result > (guint64) G_MAXINT64)
  783. {
  784. errno = ERANGE;
  785. return G_MAXINT64;
  786. }
  787. else if (negative)
  788. return - (gint64) result;
  789. else
  790. return (gint64) result;
  791. }
  792. /**
  793. * g_strerror:
  794. * @errnum: the system error number. See the standard C %errno
  795. * documentation
  796. *
  797. * Returns a string corresponding to the given error code, e.g.
  798. * "no such process". You should use this function in preference to
  799. * strerror(), because it returns a string in UTF-8 encoding, and since
  800. * not all platforms support the strerror() function.
  801. *
  802. * Returns: a UTF-8 string describing the error code. If the error code
  803. * is unknown, it returns "unknown error (&lt;code&gt;)". The string
  804. * can only be used until the next call to g_strerror()
  805. */
  806. G_CONST_RETURN gchar*
  807. g_strerror (gint errnum)
  808. {
  809. static GStaticPrivate msg_private = G_STATIC_PRIVATE_INIT;
  810. char *msg;
  811. int saved_errno = errno;
  812. #ifdef HAVE_STRERROR
  813. const char *msg_locale;
  814. msg_locale = strerror (errnum);
  815. if (g_get_charset (NULL))
  816. {
  817. errno = saved_errno;
  818. return msg_locale;
  819. }
  820. else
  821. {
  822. gchar *msg_utf8 = g_locale_to_utf8 (msg_locale, -1, NULL, NULL, NULL);
  823. if (msg_utf8)
  824. {
  825. /* Stick in the quark table so that we can return a static result
  826. */
  827. GQuark msg_quark = g_quark_from_string (msg_utf8);
  828. g_free (msg_utf8);
  829. msg_utf8 = (gchar *) g_quark_to_string (msg_quark);
  830. errno = saved_errno;
  831. return msg_utf8;
  832. }
  833. }
  834. #elif NO_SYS_ERRLIST
  835. switch (errnum)
  836. {
  837. #ifdef E2BIG
  838. case E2BIG: return "argument list too long";
  839. #endif
  840. #ifdef EACCES
  841. case EACCES: return "permission denied";
  842. #endif
  843. #ifdef EADDRINUSE
  844. case EADDRINUSE: return "address already in use";
  845. #endif
  846. #ifdef EADDRNOTAVAIL
  847. case EADDRNOTAVAIL: return "can't assign requested address";
  848. #endif
  849. #ifdef EADV
  850. case EADV: return "advertise error";
  851. #endif
  852. #ifdef EAFNOSUPPORT
  853. case EAFNOSUPPORT: return "address family not supported by protocol family";
  854. #endif
  855. #ifdef EAGAIN
  856. case EAGAIN: return "try again";
  857. #endif
  858. #ifdef EALIGN
  859. case EALIGN: return "EALIGN";
  860. #endif
  861. #ifdef EALREADY
  862. case EALREADY: return "operation already in progress";
  863. #endif
  864. #ifdef EBADE
  865. case EBADE: return "bad exchange descriptor";
  866. #endif
  867. #ifdef EBADF
  868. case EBADF: return "bad file number";
  869. #endif
  870. #ifdef EBADFD
  871. case EBADFD: return "file descriptor in bad state";
  872. #endif
  873. #ifdef EBADMSG
  874. case EBADMSG: return "not a data message";
  875. #endif
  876. #ifdef EBADR
  877. case EBADR: return "bad request descriptor";
  878. #endif
  879. #ifdef EBADRPC
  880. case EBADRPC: return "RPC structure is bad";
  881. #endif
  882. #ifdef EBADRQC
  883. case EBADRQC: return "bad request code";
  884. #endif
  885. #ifdef EBADSLT
  886. case EBADSLT: return "invalid slot";
  887. #endif
  888. #ifdef EBFONT
  889. case EBFONT: return "bad font file format";
  890. #endif
  891. #ifdef EBUSY
  892. case EBUSY: return "mount device busy";
  893. #endif
  894. #ifdef ECHILD
  895. case ECHILD: return "no children";
  896. #endif
  897. #ifdef ECHRNG
  898. case ECHRNG: return "channel number out of range";
  899. #endif
  900. #ifdef ECOMM
  901. case ECOMM: return "communication error on send";
  902. #endif
  903. #ifdef ECONNABORTED
  904. case ECONNABORTED: return "software caused connection abort";
  905. #endif
  906. #ifdef ECONNREFUSED
  907. case ECONNREFUSED: return "connection refused";
  908. #endif
  909. #ifdef ECONNRESET
  910. case ECONNRESET: return "connection reset by peer";
  911. #endif
  912. #if defined(EDEADLK) && (!defined(EWOULDBLOCK) || (EDEADLK != EWOULDBLOCK))
  913. case EDEADLK: return "resource deadlock avoided";
  914. #endif
  915. #ifdef EDEADLOCK
  916. case EDEADLOCK: return "resource deadlock avoided";
  917. #endif
  918. #ifdef EDESTADDRREQ
  919. case EDESTADDRREQ: return "destination address required";
  920. #endif
  921. #ifdef EDIRTY
  922. case EDIRTY: return "mounting a dirty fs w/o force";
  923. #endif
  924. #ifdef EDOM
  925. case EDOM: return "math argument out of range";
  926. #endif
  927. #ifdef EDOTDOT
  928. case EDOTDOT: return "cross mount point";
  929. #endif
  930. #ifdef EDQUOT
  931. case EDQUOT: return "disk quota exceeded";
  932. #endif
  933. #ifdef EDUPPKG
  934. case EDUPPKG: return "duplicate package name";
  935. #endif
  936. #ifdef EEXIST
  937. case EEXIST: return "file already exists";
  938. #endif
  939. #ifdef EFAULT
  940. case EFAULT: return "bad address in system call argument";
  941. #endif
  942. #ifdef EFBIG
  943. case EFBIG: return "file too large";
  944. #endif
  945. #ifdef EHOSTDOWN
  946. case EHOSTDOWN: return "host is down";
  947. #endif
  948. #ifdef EHOSTUNREACH
  949. case EHOSTUNREACH: return "host is unreachable";
  950. #endif
  951. #ifdef EIDRM
  952. case EIDRM: return "identifier removed";
  953. #endif
  954. #ifdef EINIT
  955. case EINIT: return "initialization error";
  956. #endif
  957. #ifdef EINPROGRESS
  958. case EINPROGRESS: return "operation now in progress";
  959. #endif
  960. #ifdef EINTR
  961. case EINTR: return "interrupted system call";
  962. #endif
  963. #ifdef EINVAL
  964. case EINVAL: return "invalid argument";
  965. #endif
  966. #ifdef EIO
  967. case EIO: return "I/O error";
  968. #endif
  969. #ifdef EISCONN
  970. case EISCONN: return "socket is already connected";
  971. #endif
  972. #ifdef EISDIR
  973. case EISDIR: return "is a directory";
  974. #endif
  975. #ifdef EISNAME
  976. case EISNAM: return "is a name file";
  977. #endif
  978. #ifdef ELBIN
  979. case ELBIN: return "ELBIN";
  980. #endif
  981. #ifdef EL2HLT
  982. case EL2HLT: return "level 2 halted";
  983. #endif
  984. #ifdef EL2NSYNC
  985. case EL2NSYNC: return "level 2 not synchronized";
  986. #endif
  987. #ifdef EL3HLT
  988. case EL3HLT: return "level 3 halted";
  989. #endif
  990. #ifdef EL3RST
  991. case EL3RST: return "level 3 reset";
  992. #endif
  993. #ifdef ELIBACC
  994. case ELIBACC: return "can not access a needed shared library";
  995. #endif
  996. #ifdef ELIBBAD
  997. case ELIBBAD: return "accessing a corrupted shared library";
  998. #endif
  999. #ifdef ELIBEXEC
  1000. case ELIBEXEC: return "can not exec a shared library directly";
  1001. #endif
  1002. #ifdef ELIBMAX
  1003. case ELIBMAX: return "attempting to link in more shared libraries than system limit";
  1004. #endif
  1005. #ifdef ELIBSCN
  1006. case ELIBSCN: return ".lib section in a.out corrupted";
  1007. #endif
  1008. #ifdef ELNRNG
  1009. case ELNRNG: return "link number out of range";
  1010. #endif
  1011. #ifdef ELOOP
  1012. case ELOOP: return "too many levels of symbolic links";
  1013. #endif
  1014. #ifdef EMFILE
  1015. case EMFILE: return "too many open files";
  1016. #endif
  1017. #ifdef EMLINK
  1018. case EMLINK: return "too many links";
  1019. #endif
  1020. #ifdef EMSGSIZE
  1021. case EMSGSIZE: return "message too long";
  1022. #endif
  1023. #ifdef EMULTIHOP
  1024. case EMULTIHOP: return "multihop attempted";
  1025. #endif
  1026. #ifdef ENAMETOOLONG
  1027. case ENAMETOOLONG: return "file name too long";
  1028. #endif
  1029. #ifdef ENAVAIL
  1030. case ENAVAIL: return "not available";
  1031. #endif
  1032. #ifdef ENET
  1033. case ENET: return "ENET";
  1034. #endif
  1035. #ifdef ENETDOWN
  1036. case ENETDOWN: return "network is down";
  1037. #endif
  1038. #ifdef ENETRESET
  1039. case ENETRESET: return "network dropped connection on reset";
  1040. #endif
  1041. #ifdef ENETUNREACH
  1042. case ENETUNREACH: return "network is unreachable";
  1043. #endif
  1044. #ifdef ENFILE
  1045. case ENFILE: return "file table overflow";
  1046. #endif
  1047. #ifdef ENOANO
  1048. case ENOANO: return "anode table overflow";
  1049. #endif
  1050. #if defined(ENOBUFS) && (!defined(ENOSR) || (ENOBUFS != ENOSR))
  1051. case ENOBUFS: return "no buffer space available";
  1052. #endif
  1053. #ifdef ENOCSI
  1054. case ENOCSI: return "no CSI structure available";
  1055. #endif
  1056. #ifdef ENODATA
  1057. case ENODATA: return "no data available";
  1058. #endif
  1059. #ifdef ENODEV
  1060. case ENODEV: return "no such device";
  1061. #endif
  1062. #ifdef ENOENT
  1063. case ENOENT: return "no such file or directory";
  1064. #endif
  1065. #ifdef ENOEXEC
  1066. case ENOEXEC: return "exec format error";
  1067. #endif
  1068. #ifdef ENOLCK
  1069. case ENOLCK: return "no locks available";
  1070. #endif
  1071. #ifdef ENOLINK
  1072. case ENOLINK: return "link has be severed";
  1073. #endif
  1074. #ifdef ENOMEM
  1075. case ENOMEM: return "not enough memory";
  1076. #endif
  1077. #ifdef ENOMSG
  1078. case ENOMSG: return "no message of desired type";
  1079. #endif
  1080. #ifdef ENONET
  1081. case ENONET: return "machine is not on the network";
  1082. #endif
  1083. #ifdef ENOPKG
  1084. case ENOPKG: return "package not installed";
  1085. #endif
  1086. #ifdef ENOPROTOOPT
  1087. case ENOPROTOOPT: return "bad proocol option";
  1088. #endif
  1089. #ifdef ENOSPC
  1090. case ENOSPC: return "no space left on device";
  1091. #endif
  1092. #ifdef ENOSR
  1093. case ENOSR: return "out of stream resources";
  1094. #endif
  1095. #ifdef ENOSTR
  1096. case ENOSTR: return "not a stream device";
  1097. #endif
  1098. #ifdef ENOSYM
  1099. case ENOSYM: return "unresolved symbol name";
  1100. #endif
  1101. #ifdef ENOSYS
  1102. case ENOSYS: return "function not implemented";
  1103. #endif
  1104. #ifdef ENOTBLK
  1105. case ENOTBLK: return "block device required";
  1106. #endif
  1107. #ifdef ENOTCONN
  1108. case ENOTCONN: return "socket is not connected";
  1109. #endif
  1110. #ifdef ENOTDIR
  1111. case ENOTDIR: return "not a directory";
  1112. #endif
  1113. #ifdef ENOTEMPTY
  1114. case ENOTEMPTY: return "directory not empty";
  1115. #endif
  1116. #ifdef ENOTNAM
  1117. case ENOTNAM: return "not a name file";
  1118. #endif
  1119. #ifdef ENOTSOCK
  1120. case ENOTSOCK: return "socket operation on non-socket";
  1121. #endif
  1122. #ifdef ENOTTY
  1123. case ENOTTY: return "inappropriate device for ioctl";
  1124. #endif
  1125. #ifdef ENOTUNIQ
  1126. case ENOTUNIQ: return "name not unique on network";
  1127. #endif
  1128. #ifdef ENXIO
  1129. case ENXIO: return "no such device or address";
  1130. #endif
  1131. #ifdef EOPNOTSUPP
  1132. case EOPNOTSUPP: return "operation not supported on socket";
  1133. #endif
  1134. #ifdef EPERM
  1135. case EPERM: return "not owner";
  1136. #endif
  1137. #ifdef EPFNOSUPPORT
  1138. case EPFNOSUPPORT: return "protocol family not supported";
  1139. #endif
  1140. #ifdef EPIPE
  1141. case EPIPE: return "broken pipe";
  1142. #endif
  1143. #ifdef EPROCLIM
  1144. case EPROCLIM: return "too many processes";
  1145. #endif
  1146. #ifdef EPROCUNAVAIL
  1147. case EPROCUNAVAIL: return "bad procedure for program";
  1148. #endif
  1149. #ifdef EPROGMISMATCH
  1150. case EPROGMISMATCH: return "program version wrong";
  1151. #endif
  1152. #ifdef EPROGUNAVAIL
  1153. case EPROGUNAVAIL: return "RPC program not available";
  1154. #endif
  1155. #ifdef EPROTO
  1156. case EPROTO: return "protocol error";
  1157. #endif
  1158. #ifdef EPROTONOSUPPORT
  1159. case EPROTONOSUPPORT: return "protocol not suppored";
  1160. #endif
  1161. #ifdef EPROTOTYPE
  1162. case EPROTOTYPE: return "protocol wrong type for socket";
  1163. #endif
  1164. #ifdef ERANGE
  1165. case ERANGE: return "math result unrepresentable";
  1166. #endif
  1167. #if defined(EREFUSED) && (!defined(ECONNREFUSED) || (EREFUSED != ECONNREFUSED))
  1168. case EREFUSED: return "EREFUSED";
  1169. #endif
  1170. #ifdef EREMCHG
  1171. case EREMCHG: return "remote address changed";
  1172. #endif
  1173. #ifdef EREMDEV
  1174. case EREMDEV: return "remote device";
  1175. #endif
  1176. #ifdef EREMOTE
  1177. case EREMOTE: return "pathname hit remote file system";
  1178. #endif
  1179. #ifdef EREMOTEIO
  1180. case EREMOTEIO: return "remote i/o error";
  1181. #endif
  1182. #ifdef EREMOTERELEASE
  1183. case EREMOTERELEASE: return "EREMOTERELEASE";
  1184. #endif
  1185. #ifdef EROFS
  1186. case EROFS: return "read-only file system";
  1187. #endif
  1188. #ifdef ERPCMISMATCH
  1189. case ERPCMISMATCH: return "RPC version is wrong";
  1190. #endif
  1191. #ifdef ERREMOTE
  1192. case ERREMOTE: return "object is remote";
  1193. #endif
  1194. #ifdef ESHUTDOWN
  1195. case ESHUTDOWN: return "can't send afer socket shutdown";
  1196. #endif
  1197. #ifdef ESOCKTNOSUPPORT
  1198. case ESOCKTNOSUPPORT: return "socket type not supported";
  1199. #endif
  1200. #ifdef ESPIPE
  1201. case ESPIPE: return "invalid seek";
  1202. #endif
  1203. #ifdef ESRCH
  1204. case ESRCH: return "no such process";
  1205. #endif
  1206. #ifdef ESRMNT
  1207. case ESRMNT: return "srmount error";
  1208. #endif
  1209. #ifdef ESTALE
  1210. case ESTALE: return "stale remote file handle";
  1211. #endif
  1212. #ifdef ESUCCESS
  1213. case ESUCCESS: return "Error 0";
  1214. #endif
  1215. #ifdef ETIME
  1216. case ETIME: return "timer expired";
  1217. #endif
  1218. #ifdef ETIMEDOUT
  1219. case ETIMEDOUT: return "connection timed out";
  1220. #endif
  1221. #ifdef ETOOMANYREFS
  1222. case ETOOMANYREFS: return "too many references: can't splice";
  1223. #endif
  1224. #ifdef ETXTBSY
  1225. case ETXTBSY: return "text file or pseudo-device busy";
  1226. #endif
  1227. #ifdef EUCLEAN
  1228. case EUCLEAN: return "structure needs cleaning";
  1229. #endif
  1230. #ifdef EUNATCH
  1231. case EUNATCH: return "protocol driver not attached";
  1232. #endif
  1233. #ifdef EUSERS
  1234. case EUSERS: return "too many users";
  1235. #endif
  1236. #ifdef EVERSION
  1237. case EVERSION: return "version mismatch";
  1238. #endif
  1239. #if defined(EWOULDBLOCK) && (!defined(EAGAIN) || (EWOULDBLOCK != EAGAIN))
  1240. case EWOULDBLOCK: return "operation would block";
  1241. #endif
  1242. #ifdef EXDEV
  1243. case EXDEV: return "cross-domain link";
  1244. #endif
  1245. #ifdef EXFULL
  1246. case EXFULL: return "message tables full";
  1247. #endif
  1248. }
  1249. #else /* NO_SYS_ERRLIST */
  1250. extern int sys_nerr;
  1251. extern char *sys_errlist[];
  1252. if ((errnum > 0) && (errnum <= sys_nerr))
  1253. return sys_errlist [errnum];
  1254. #endif /* NO_SYS_ERRLIST */
  1255. msg = g_static_private_get (&msg_private);
  1256. if (!msg)
  1257. {
  1258. msg = g_new (gchar, 64);
  1259. g_static_private_set (&msg_private, msg, g_free);
  1260. }
  1261. _g_sprintf (msg, "unknown error (%d)", errnum);
  1262. errno = saved_errno;
  1263. return msg;
  1264. }
  1265. /**
  1266. * g_strsignal:
  1267. * @signum: the signal number. See the <literal>signal</literal>
  1268. * documentation
  1269. *
  1270. * Returns a string describing the given signal, e.g. "Segmentation fault".
  1271. * You should use this function in preference to strsignal(), because it
  1272. * returns a string in UTF-8 encoding, and since not all platforms support
  1273. * the strsignal() function.
  1274. *
  1275. * Returns: a UTF-8 string describing the signal. If the signal is unknown,
  1276. * it returns "unknown signal (&lt;signum&gt;)". The string can only be
  1277. * used until the next call to g_strsignal()
  1278. */
  1279. G_CONST_RETURN gchar*
  1280. g_strsignal (gint signum)
  1281. {
  1282. static GStaticPrivate msg_private = G_STATIC_PRIVATE_INIT;
  1283. char *msg;
  1284. #ifdef HAVE_STRSIGNAL
  1285. const char *msg_locale;
  1286. #if defined(G_OS_BEOS) || defined(G_WITH_CYGWIN)
  1287. extern const char *strsignal(int);
  1288. #else
  1289. /* this is declared differently (const) in string.h on BeOS */
  1290. extern char *strsignal (int sig);
  1291. #endif /* !G_OS_BEOS && !G_WITH_CYGWIN */
  1292. msg_locale = strsignal (signum);
  1293. if (g_get_charset (NULL))
  1294. return msg_locale;
  1295. else
  1296. {
  1297. gchar *msg_utf8 = g_locale_to_utf8 (msg_locale, -1, NULL, NULL, NULL);
  1298. if (msg_utf8)
  1299. {
  1300. /* Stick in the quark table so that we can return a static result
  1301. */
  1302. GQuark msg_quark = g_quark_from_string (msg_utf8);
  1303. g_free (msg_utf8);
  1304. return g_quark_to_string (msg_quark);
  1305. }
  1306. }
  1307. #elif NO_SYS_SIGLIST
  1308. switch (signum)
  1309. {
  1310. #ifdef SIGHUP
  1311. case SIGHUP: return "Hangup";
  1312. #endif
  1313. #ifdef SIGINT
  1314. case SIGINT: return "Interrupt";
  1315. #endif
  1316. #ifdef SIGQUIT
  1317. case SIGQUIT: return "Quit";
  1318. #endif
  1319. #ifdef SIGILL
  1320. case SIGILL: return "Illegal instruction";
  1321. #endif
  1322. #ifdef SIGTRAP
  1323. case SIGTRAP: return "Trace/breakpoint trap";
  1324. #endif
  1325. #ifdef SIGABRT
  1326. case SIGABRT: return "IOT trap/Abort";
  1327. #endif
  1328. #ifdef SIGBUS
  1329. case SIGBUS: return "Bus error";
  1330. #endif
  1331. #ifdef SIGFPE
  1332. case SIGFPE: return "Floating point exception";
  1333. #endif
  1334. #ifdef SIGKILL
  1335. case SIGKILL: return "Killed";
  1336. #endif
  1337. #ifdef SIGUSR1
  1338. case SIGUSR1: return "User defined signal 1";
  1339. #endif
  1340. #ifdef SIGSEGV
  1341. case SIGSEGV: return "Segmentation fault";
  1342. #endif
  1343. #ifdef SIGUSR2
  1344. case SIGUSR2: return "User defined signal 2";
  1345. #endif
  1346. #ifdef SIGPIPE
  1347. case SIGPIPE: return "Broken pipe";
  1348. #endif
  1349. #ifdef SIGALRM
  1350. case SIGALRM: return "Alarm clock";
  1351. #endif
  1352. #ifdef SIGTERM
  1353. case SIGTERM: return "Terminated";
  1354. #endif
  1355. #ifdef SIGSTKFLT
  1356. case SIGSTKFLT: return "Stack fault";
  1357. #endif
  1358. #ifdef SIGCHLD
  1359. case SIGCHLD: return "Child exited";
  1360. #endif
  1361. #ifdef SIGCONT
  1362. case SIGCONT: return "Continued";
  1363. #endif
  1364. #ifdef SIGSTOP
  1365. case SIGSTOP: return "Stopped (signal)";
  1366. #endif
  1367. #ifdef SIGTSTP
  1368. case SIGTSTP: return "Stopped";
  1369. #endif
  1370. #ifdef SIGTTIN
  1371. case SIGTTIN: return "Stopped (tty input)";
  1372. #endif
  1373. #ifdef SIGTTOU
  1374. case SIGTTOU: return "Stopped (tty output)";
  1375. #endif
  1376. #ifdef SIGURG
  1377. case SIGURG: return "Urgent condition";
  1378. #endif
  1379. #ifdef SIGXCPU
  1380. case SIGXCPU: return "CPU time limit exceeded";
  1381. #endif
  1382. #ifdef SIGXFSZ
  1383. case SIGXFSZ: return "File size limit exceeded";
  1384. #endif
  1385. #ifdef SIGVTALRM
  1386. case SIGVTALRM: return "Virtual time alarm";
  1387. #endif
  1388. #ifdef SIGPROF
  1389. case SIGPROF: return "Profile signal";
  1390. #endif
  1391. #ifdef SIGWINCH
  1392. case SIGWINCH: return "Window size changed";
  1393. #endif
  1394. #ifdef SIGIO
  1395. case SIGIO: return "Possible I/O";
  1396. #endif
  1397. #ifdef SIGPWR
  1398. case SIGPWR: return "Power failure";
  1399. #endif
  1400. #ifdef SIGUNUSED
  1401. case SIGUNUSED: return "Unused signal";
  1402. #endif
  1403. }
  1404. #else /* NO_SYS_SIGLIST */
  1405. #ifdef NO_SYS_SIGLIST_DECL
  1406. extern char *sys_siglist[]; /*(see Tue Jan 19 00:44:24 1999 in changelog)*/
  1407. #endif
  1408. return (char*) /* this function should return const --josh */ sys_siglist [signum];
  1409. #endif /* NO_SYS_SIGLIST */
  1410. msg = g_static_private_get (&msg_private);
  1411. if (!msg)
  1412. {
  1413. msg = g_new (gchar, 64);
  1414. g_static_private_set (&msg_private, msg, g_free);
  1415. }
  1416. _g_sprintf (msg, "unknown signal (%d)", signum);
  1417. return msg;
  1418. }
  1419. /* Functions g_strlcpy and g_strlcat were originally developed by
  1420. * Todd C. Miller <Todd.Miller@courtesan.com> to simplify writing secure code.
  1421. * See ftp://ftp.openbsd.org/pub/OpenBSD/src/lib/libc/string/strlcpy.3
  1422. * for more information.
  1423. */
  1424. #ifdef HAVE_STRLCPY
  1425. /* Use the native ones, if available; they might be implemented in assembly */
  1426. gsize
  1427. g_strlcpy (gchar *dest,
  1428. const gchar *src,
  1429. gsize dest_size)
  1430. {
  1431. g_return_val_if_fail (dest != NULL, 0);
  1432. g_return_val_if_fail (src != NULL, 0);
  1433. return strlcpy (dest, src, dest_size);
  1434. }
  1435. gsize
  1436. g_strlcat (gchar *dest,
  1437. const gchar *src,
  1438. gsize dest_size)
  1439. {
  1440. g_return_val_if_fail (dest != NULL, 0);
  1441. g_return_val_if_fail (src != NULL, 0);
  1442. return strlcat (dest, src, dest_size);
  1443. }
  1444. #else /* ! HAVE_STRLCPY */
  1445. /**
  1446. * g_strlcpy:
  1447. * @dest: destination buffer
  1448. * @src: source buffer
  1449. * @dest_size: length of @dest in bytes
  1450. *
  1451. * Portability wrapper that calls strlcpy() on systems which have it,
  1452. * and emulates strlcpy() otherwise. Copies @src to @dest; @dest is
  1453. * guaranteed to be nul-terminated; @src must be nul-terminated;
  1454. * @dest_size is the buffer size, not the number of chars to copy.
  1455. *
  1456. * At most dest_size - 1 characters will be copied. Always nul-terminates
  1457. * (unless dest_size == 0). This function does <emphasis>not</emphasis>
  1458. * allocate memory. Unlike strncpy(), this function doesn't pad dest (so
  1459. * it's often faster). It returns the size of the attempted result,
  1460. * strlen (src), so if @retval >= @dest_size, truncation occurred.
  1461. *
  1462. * <note><para>Caveat: strlcpy() is supposedly more secure than
  1463. * strcpy() or strncpy(), but if you really want to avoid screwups,
  1464. * g_strdup() is an even better idea.</para></note>
  1465. *
  1466. * Returns: length of @src
  1467. */
  1468. gsize
  1469. g_strlcpy (gchar *dest,
  1470. const gchar *src,
  1471. gsize dest_size)
  1472. {
  1473. register gchar *d = dest;
  1474. register const gchar *s = src;
  1475. register gsize n = dest_size;
  1476. g_return_val_if_fail (dest != NULL, 0);
  1477. g_return_val_if_fail (src != NULL, 0);
  1478. /* Copy as many bytes as will fit */
  1479. if (n != 0 && --n != 0)
  1480. do
  1481. {
  1482. register gchar c = *s++;
  1483. *d++ = c;
  1484. if (c == 0)
  1485. break;
  1486. }
  1487. while (--n != 0);
  1488. /* If not enough room in dest, add NUL and traverse rest of src */
  1489. if (n == 0)
  1490. {
  1491. if (dest_size != 0)
  1492. *d = 0;
  1493. while (*s++)
  1494. ;
  1495. }
  1496. return s - src - 1; /* count does not include NUL */
  1497. }
  1498. /**
  1499. * g_strlcat:
  1500. * @dest: destination buffer, already containing one nul-terminated string
  1501. * @src: source buffer
  1502. * @dest_size: length of @dest buffer in bytes (not length of existing string
  1503. * inside @dest)
  1504. *
  1505. * Portability wrapper that calls strlcat() on systems which have it,
  1506. * and emulates it otherwise. Appends nul-terminated @src string to @dest,
  1507. * guaranteeing nul-termination for @dest. The total size of @dest won't
  1508. * exceed @dest_size.
  1509. *
  1510. * At most dest_size - 1 characters will be copied.
  1511. * Unlike strncat, dest_size is the full size of dest, not the space left over.
  1512. * This function does NOT allocate memory.
  1513. * This always NUL terminates (unless siz == 0 or there were no NUL characters
  1514. * in the dest_size characters of dest to start with).
  1515. * Returns size of attempted result, which is
  1516. * MIN (dest_size, strlen (original dest)) + strlen (src),
  1517. * so if retval >= dest_size, truncation occurred.
  1518. *
  1519. * <note><para>Caveat: this is supposedly a more secure alternative to
  1520. * strcat() or strncat(), but for real security g_strconcat() is harder
  1521. * to mess up.</para></note>
  1522. *
  1523. */
  1524. gsize
  1525. g_strlcat (gchar *dest,
  1526. const gchar *src,
  1527. gsize dest_size)
  1528. {
  1529. register gchar *d = dest;
  1530. register const gchar *s = src;
  1531. register gsize bytes_left = dest_size;
  1532. gsize dlength; /* Logically, MIN (strlen (d), dest_size) */
  1533. g_return_val_if_fail (dest != NULL, 0);
  1534. g_return_val_if_fail (src != NULL, 0);
  1535. /* Find the end of dst and adjust bytes left but don't go past end */
  1536. while (*d != 0 && bytes_left-- != 0)
  1537. d++;
  1538. dlength = d - dest;
  1539. bytes_left = dest_size - dlength;
  1540. if (bytes_left == 0)
  1541. return dlength + strlen (s);
  1542. while (*s != 0)
  1543. {
  1544. if (bytes_left != 1)
  1545. {
  1546. *d++ = *s;
  1547. bytes_left--;
  1548. }
  1549. s++;
  1550. }
  1551. *d = 0;
  1552. return dlength + (s - src); /* count does not include NUL */
  1553. }
  1554. #endif /* ! HAVE_STRLCPY */
  1555. /**
  1556. * g_ascii_strdown:
  1557. * @str: a string.
  1558. * @len: length of @str in bytes, or -1 if @str is nul-terminated.
  1559. *
  1560. * Converts all upper case ASCII letters to lower case ASCII letters.
  1561. *
  1562. * Return value: a newly-allocated string, with all the upper case
  1563. * characters in @str converted to lower case, with
  1564. * semantics that exactly match g_ascii_tolower(). (Note
  1565. * that this is unlike the old g_strdown(), which modified
  1566. * the string in place.)
  1567. **/
  1568. gchar*
  1569. g_ascii_strdown (const gchar *str,
  1570. gssize len)
  1571. {
  1572. gchar *result, *s;
  1573. g_return_val_if_fail (str != NULL, NULL);
  1574. if (len < 0)
  1575. len = strlen (str);
  1576. result = g_strndup (str, len);
  1577. for (s = result; *s; s++)
  1578. *s = g_ascii_tolower (*s);
  1579. return result;
  1580. }
  1581. /**
  1582. * g_ascii_strup:
  1583. * @str: a string.
  1584. * @len: length of @str in bytes, or -1 if @str is nul-terminated.
  1585. *
  1586. * Converts all lower case ASCII letters to upper case ASCII letters.
  1587. *
  1588. * Return value: a newly allocated string, with all the lower case
  1589. * characters in @str converted to upper case, with
  1590. * semantics that exactly match g_ascii_toupper(). (Note
  1591. * that this is unlike the old g_strup(), which modified
  1592. * the string in place.)
  1593. **/
  1594. gchar*
  1595. g_ascii_strup (const gchar *str,
  1596. gssize len)
  1597. {
  1598. gchar *result, *s;
  1599. g_return_val_if_fail (str != NULL, NULL);
  1600. if (len < 0)
  1601. len = strlen (str);
  1602. result = g_strndup (str, len);
  1603. for (s = result; *s; s++)
  1604. *s = g_ascii_toupper (*s);
  1605. return result;
  1606. }
  1607. /**
  1608. * g_strdown:
  1609. * @string: the string to convert.
  1610. *
  1611. * Converts a string to lower case.
  1612. *
  1613. * Return value: the string
  1614. *
  1615. * Deprecated:2.2: This function is totally broken for the reasons discussed
  1616. * in the g_strncasecmp() docs - use g_ascii_strdown() or g_utf8_strdown()
  1617. * instead.
  1618. **/
  1619. gchar*
  1620. g_strdown (gchar *string)
  1621. {
  1622. register guchar *s;
  1623. g_return_val_if_fail (string != NULL, NULL);
  1624. s = (guchar *) string;
  1625. while (*s)
  1626. {
  1627. if (isupper (*s))
  1628. *s = tolower (*s);
  1629. s++;
  1630. }
  1631. return (gchar *) string;
  1632. }
  1633. /**
  1634. * g_strup:
  1635. * @string: the string to convert.
  1636. *
  1637. * Converts a string to upper case.
  1638. *
  1639. * Return value: the string
  1640. *
  1641. * Deprecated:2.2: This function is totally broken for the reasons discussed
  1642. * in the g_strncasecmp() docs - use g_ascii_strup() or g_utf8_strup() instead.
  1643. **/
  1644. gchar*
  1645. g_strup (gchar *string)
  1646. {
  1647. register guchar *s;
  1648. g_return_val_if_fail (string != NULL, NULL);
  1649. s = (guchar *) string;
  1650. while (*s)
  1651. {
  1652. if (islower (*s))
  1653. *s = toupper (*s);
  1654. s++;
  1655. }
  1656. return (gchar *) string;
  1657. }
  1658. /**
  1659. * g_strreverse:
  1660. * @string: the string to reverse
  1661. *
  1662. * Reverses all of the bytes in a string. For example,
  1663. * <literal>g_strreverse ("abcdef")</literal> will result
  1664. * in "fedcba".
  1665. *
  1666. * Note that g_strreverse() doesn't work on UTF-8 strings
  1667. * containing multibyte characters. For that purpose, use
  1668. * g_utf8_strreverse().
  1669. *
  1670. * Returns: the same pointer passed in as @string
  1671. */
  1672. gchar*
  1673. g_strreverse (gchar *string)
  1674. {
  1675. g_return_val_if_fail (string != NULL, NULL);
  1676. if (*string)
  1677. {
  1678. register gchar *h, *t;
  1679. h = string;
  1680. t = string + strlen (string) - 1;
  1681. while (h < t)
  1682. {
  1683. register gchar c;
  1684. c = *h;
  1685. *h = *t;
  1686. h++;
  1687. *t = c;
  1688. t--;
  1689. }
  1690. }
  1691. return string;
  1692. }
  1693. /**
  1694. * g_ascii_tolower:
  1695. * @c: any character.
  1696. *
  1697. * Convert a character to ASCII lower case.
  1698. *
  1699. * Unlike the standard C library tolower() function, this only
  1700. * recognizes standard ASCII letters and ignores the locale, returning
  1701. * all non-ASCII characters unchanged, even if they are lower case
  1702. * letters in a particular character set. Also unlike the standard
  1703. * library function, this takes and returns a char, not an int, so
  1704. * don't call it on %EOF but no need to worry about casting to #guchar
  1705. * before passing a possibly non-ASCII character in.
  1706. *
  1707. * Return value: the result of converting @c to lower case.
  1708. * If @c is not an ASCII upper case letter,
  1709. * @c is returned unchanged.
  1710. **/
  1711. gchar
  1712. g_ascii_tolower (gchar c)
  1713. {
  1714. return g_ascii_isupper (c) ? c - 'A' + 'a' : c;
  1715. }
  1716. /**
  1717. * g_ascii_toupper:
  1718. * @c: any character.
  1719. *
  1720. * Convert a character to ASCII upper case.
  1721. *
  1722. * Unlike the standard C library toupper() function, this only
  1723. * recognizes standard ASCII letters and ignores the locale, returning
  1724. * all non-ASCII characters unchanged, even if they are upper case
  1725. * letters in a particular character set. Also unlike the standard
  1726. * library function, this takes and returns a char, not an int, so
  1727. * don't call it on %EOF but no need to worry about casting to #guchar
  1728. * before passing a possibly non-ASCII character in.
  1729. *
  1730. * Return value: the result of converting @c to upper case.
  1731. * If @c is not an ASCII lower case letter,
  1732. * @c is returned unchanged.
  1733. **/
  1734. gchar
  1735. g_ascii_toupper (gchar c)
  1736. {
  1737. return g_ascii_islower (c) ? c - 'a' + 'A' : c;
  1738. }
  1739. /**
  1740. * g_ascii_digit_value:
  1741. * @c: an ASCII character.
  1742. *
  1743. * Determines the numeric value of a character as a decimal
  1744. * digit. Differs from g_unichar_digit_value() because it takes
  1745. * a char, so there's no worry about sign extension if characters
  1746. * are signed.
  1747. *
  1748. * Return value: If @c is a decimal digit (according to
  1749. * g_ascii_isdigit()), its numeric value. Otherwise, -1.
  1750. **/
  1751. int
  1752. g_ascii_digit_value (gchar c)
  1753. {
  1754. if (g_ascii_isdigit (c))
  1755. return c - '0';
  1756. return -1;
  1757. }
  1758. /**
  1759. * g_ascii_xdigit_value:
  1760. * @c: an ASCII character.
  1761. *
  1762. * Determines the numeric value of a character as a hexidecimal
  1763. * digit. Differs from g_unichar_xdigit_value() because it takes
  1764. * a char, so there's no worry about sign extension if characters
  1765. * are signed.
  1766. *
  1767. * Return value: If @c is a hex digit (according to
  1768. * g_ascii_isxdigit()), its numeric value. Otherwise, -1.
  1769. **/
  1770. int
  1771. g_ascii_xdigit_value (gchar c)
  1772. {
  1773. if (c >= 'A' && c <= 'F')
  1774. return c - 'A' + 10;
  1775. if (c >= 'a' && c <= 'f')
  1776. return c - 'a' + 10;
  1777. return g_ascii_digit_value (c);
  1778. }
  1779. /**
  1780. * g_ascii_strcasecmp:
  1781. * @s1: string to compare with @s2.
  1782. * @s2: string to compare with @s1.
  1783. *
  1784. * Compare two strings, ignoring the case of ASCII characters.
  1785. *
  1786. * Unlike the BSD strcasecmp() function, this only recognizes standard
  1787. * ASCII letters and ignores the locale, treating all non-ASCII
  1788. * bytes as if they ar…

Large files files are truncated, but you can click here to view the full file