1 /* GLIB - Library of useful routines for C programming
2 * Copyright (C) 1995-1997 Peter Mattis, Spencer Kimball and Josh MacDonald
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.
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.
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.
21 * Modified by the GLib Team and others 1997-2000. See the AUTHORS
22 * file for a list of people on the GLib Team. See the ChangeLog
23 * files for a list of changes. These files are distributed with
24 * GLib at ftp://ftp.gtk.org/pub/gtk/.
35 #define _GNU_SOURCE /* For stpcpy */
43 #include <ctype.h> /* For tolower() */
44 #if !defined (HAVE_STRSIGNAL) || !defined(NO_SYS_SIGLIST_DECL)
53 /* do not include <unistd.h> in this place since it
54 * inteferes with g_strsignal() on some OSes
57 static const guint16 ascii_table_data[256] = {
58 0x004, 0x004, 0x004, 0x004, 0x004, 0x004, 0x004, 0x004,
59 0x004, 0x104, 0x104, 0x004, 0x104, 0x104, 0x004, 0x004,
60 0x004, 0x004, 0x004, 0x004, 0x004, 0x004, 0x004, 0x004,
61 0x004, 0x004, 0x004, 0x004, 0x004, 0x004, 0x004, 0x004,
62 0x140, 0x0d0, 0x0d0, 0x0d0, 0x0d0, 0x0d0, 0x0d0, 0x0d0,
63 0x0d0, 0x0d0, 0x0d0, 0x0d0, 0x0d0, 0x0d0, 0x0d0, 0x0d0,
64 0x459, 0x459, 0x459, 0x459, 0x459, 0x459, 0x459, 0x459,
65 0x459, 0x459, 0x0d0, 0x0d0, 0x0d0, 0x0d0, 0x0d0, 0x0d0,
66 0x0d0, 0x653, 0x653, 0x653, 0x653, 0x653, 0x653, 0x253,
67 0x253, 0x253, 0x253, 0x253, 0x253, 0x253, 0x253, 0x253,
68 0x253, 0x253, 0x253, 0x253, 0x253, 0x253, 0x253, 0x253,
69 0x253, 0x253, 0x253, 0x0d0, 0x0d0, 0x0d0, 0x0d0, 0x0d0,
70 0x0d0, 0x473, 0x473, 0x473, 0x473, 0x473, 0x473, 0x073,
71 0x073, 0x073, 0x073, 0x073, 0x073, 0x073, 0x073, 0x073,
72 0x073, 0x073, 0x073, 0x073, 0x073, 0x073, 0x073, 0x073,
73 0x073, 0x073, 0x073, 0x0d0, 0x0d0, 0x0d0, 0x0d0, 0x004
74 /* the upper 128 are all zeroes */
77 #if defined(G_PLATFORM_WIN32) && defined(__GNUC__)
80 const guint16 * const g_ascii_table = ascii_table_data;
83 g_strdup (const gchar *str)
89 new_str = g_new (char, strlen (str) + 1);
90 strcpy (new_str, str);
99 g_memdup (gconstpointer mem,
106 new_mem = g_malloc (byte_size);
107 memcpy (new_mem, mem, byte_size);
116 g_strndup (const gchar *str,
123 new_str = g_new (gchar, n + 1);
124 strncpy (new_str, str, n);
134 g_strnfill (gsize length,
139 str = g_new (gchar, length + 1);
140 memset (str, (guchar)fill_char, length);
148 * @dest: destination buffer.
149 * @src: source string.
151 * Copies a nul-terminated string into the dest buffer, include the
152 * trailing nul, and return a pointer to the trailing nul byte.
153 * This is useful for concatenating multiple strings together
154 * without having to repeatedly scan for the end.
156 * Return value: a pointer to trailing nul byte.
159 g_stpcpy (gchar *dest,
163 g_return_val_if_fail (dest != NULL, NULL);
164 g_return_val_if_fail (src != NULL, NULL);
165 return stpcpy (dest, src);
167 register gchar *d = dest;
168 register const gchar *s = src;
170 g_return_val_if_fail (dest != NULL, NULL);
171 g_return_val_if_fail (src != NULL, NULL);
174 while (*s++ != '\0');
181 g_strdup_vprintf (const gchar *format,
185 #ifdef HAVE_VASPRINTF
186 if (vasprintf (&buffer, format, args1) < 0)
188 else if (!g_mem_is_system_malloc ())
190 gchar *buffer1 = g_strdup (buffer);
197 G_VA_COPY (args2, args1);
199 buffer = g_new (gchar, g_printf_string_upper_bound (format, args1));
201 vsprintf (buffer, format, args2);
208 g_strdup_printf (const gchar *format,
214 va_start (args, format);
215 buffer = g_strdup_vprintf (format, args);
222 g_strconcat (const gchar *string1, ...)
230 g_return_val_if_fail (string1 != NULL, NULL);
232 l = 1 + strlen (string1);
233 va_start (args, string1);
234 s = va_arg (args, gchar*);
238 s = va_arg (args, gchar*);
242 concat = g_new (gchar, l);
245 ptr = g_stpcpy (ptr, string1);
246 va_start (args, string1);
247 s = va_arg (args, gchar*);
250 ptr = g_stpcpy (ptr, s);
251 s = va_arg (args, gchar*);
260 * @nptr: the string to convert to a numeric value.
261 * @endptr: if non-%NULL, it returns the character after
262 * the last character used in the conversion.
264 * Converts a string to a #gdouble value.
265 * It calls the standard strtod() function to handle the conversion, but
266 * if the string is not completely converted it attempts the conversion
267 * again with g_ascii_strtod(), and returns the best match.
269 * This function should seldomly be used. The normal situation when reading
270 * numbers not for human consumption is to use g_ascii_strtod(). Only when
271 * you know that you must expect both locale formatted and C formatted numbers
272 * should you use this. Make sure that you don't pass strings such as comma
273 * separated lists of values, since the commas may be interpreted as a decimal
274 * point in some locales, causing unexpected results.
276 * Return value: the #gdouble value.
279 g_strtod (const gchar *nptr,
287 g_return_val_if_fail (nptr != NULL, 0);
292 val_1 = strtod (nptr, &fail_pos_1);
294 if (fail_pos_1 && fail_pos_1[0] != 0)
295 val_2 = g_ascii_strtod (nptr, &fail_pos_2);
297 if (!fail_pos_1 || fail_pos_1[0] == 0 || fail_pos_1 >= fail_pos_2)
300 *endptr = fail_pos_1;
306 *endptr = fail_pos_2;
313 * @nptr: the string to convert to a numeric value.
314 * @endptr: if non-%NULL, it returns the character after
315 * the last character used in the conversion.
317 * Converts a string to a #gdouble value.
318 * This function behaves like the standard strtod() function
319 * does in the C locale. It does this without actually
320 * changing the current locale, since that would not be
323 * This function is typically used when reading configuration
324 * files or other non-user input that should be locale independent.
325 * To handle input from the user you should normally use the
326 * locale-sensitive system strtod() function.
328 * To convert from a string to #gdouble in a locale-insensitive
329 * way, use g_ascii_dtostr().
331 * If the correct value would cause overflow, plus or minus %HUGE_VAL
332 * is returned (according to the sign of the value), and %ERANGE is
333 * stored in %errno. If the correct value would cause underflow,
334 * zero is returned and %ERANGE is stored in %errno.
336 * This function resets %errno before calling strtod() so that
337 * you can reliably detect overflow and underflow.
339 * Return value: the #gdouble value.
342 g_ascii_strtod (const gchar *nptr,
347 struct lconv *locale_data;
348 const char *decimal_point;
349 int decimal_point_len;
350 const char *p, *decimal_point_pos;
351 const char *end = NULL; /* Silence gcc */
353 g_return_val_if_fail (nptr != NULL, 0);
357 locale_data = localeconv ();
358 decimal_point = locale_data->decimal_point;
359 decimal_point_len = strlen (decimal_point);
361 g_assert (decimal_point_len != 0);
363 decimal_point_pos = NULL;
364 if (decimal_point[0] != '.' ||
365 decimal_point[1] != 0)
368 /* Skip leading space */
369 while (g_ascii_isspace (*p))
372 /* Skip leading optional sign */
373 if (*p == '+' || *p == '-')
377 (p[1] == 'x' || p[1] == 'X'))
380 /* HEX - find the (optional) decimal point */
382 while (g_ascii_isxdigit (*p))
387 decimal_point_pos = p++;
389 while (g_ascii_isxdigit (*p))
392 if (*p == 'p' || *p == 'P')
394 if (*p == '+' || *p == '-')
396 while (g_ascii_isdigit (*p))
403 while (g_ascii_isdigit (*p))
408 decimal_point_pos = p++;
410 while (g_ascii_isdigit (*p))
413 if (*p == 'e' || *p == 'E')
415 if (*p == '+' || *p == '-')
417 while (g_ascii_isdigit (*p))
422 /* For the other cases, we need not convert the decimal point */
425 /* Set errno to zero, so that we can distinguish zero results
429 if (decimal_point_pos)
433 /* We need to convert the '.' to the locale specific decimal point */
434 copy = g_malloc (end - nptr + 1 + decimal_point_len);
437 memcpy (c, nptr, decimal_point_pos - nptr);
438 c += decimal_point_pos - nptr;
439 memcpy (c, decimal_point, decimal_point_len);
440 c += decimal_point_len;
441 memcpy (c, decimal_point_pos + 1, end - (decimal_point_pos + 1));
442 c += end - (decimal_point_pos + 1);
445 val = strtod (copy, &fail_pos);
449 if (fail_pos > decimal_point_pos)
450 fail_pos = (char *)nptr + (fail_pos - copy) - (decimal_point_len - 1);
452 fail_pos = (char *)nptr + (fail_pos - copy);
459 val = strtod (nptr, &fail_pos);
470 * @buffer: A buffer to place the resulting string in
471 * @buf_len: The length of the buffer.
472 * @d: The #gdouble to convert
474 * Converts a #gdouble to a string, using the '.' as
477 * This functions generates enough precision that converting
478 * the string back using g_strtod() gives the same machine-number
479 * (on machines with IEEE compatible 64bit doubles). It is
480 * guaranteed that the size of the resulting string will never
481 * be larger than @G_ASCII_DTOSTR_BUF_SIZE bytes.
483 * Return value: The pointer to the buffer with the converted string.
486 g_ascii_dtostr (gchar *buffer,
490 return g_ascii_formatd (buffer, buf_len, "%.17g", d);
495 * @buffer: A buffer to place the resulting string in
496 * @buf_len: The length of the buffer.
497 * @format: The printf-style format to use for the
498 * code to use for converting.
499 * @d: The #gdouble to convert
501 * Converts a #gdouble to a string, using the '.' as
502 * decimal point. To format the number you pass in
503 * a printf-style formating string. Allowed conversion
504 * specifiers are 'e', 'E', 'f', 'F', 'g' and 'G'.
506 * If you just want to want to serialize the value into a
507 * string, use g_ascii_dtostr().
509 * Return value: The pointer to the buffer with the converted string.
512 g_ascii_formatd (gchar *buffer,
517 struct lconv *locale_data;
518 const char *decimal_point;
519 int decimal_point_len;
524 g_return_val_if_fail (buffer != NULL, NULL);
525 g_return_val_if_fail (format[0] == '%', NULL);
526 g_return_val_if_fail (strpbrk (format + 1, "'l%") == NULL, NULL);
528 format_char = format[strlen (format) - 1];
530 g_return_val_if_fail (format_char == 'e' || format_char == 'E' ||
531 format_char == 'f' || format_char == 'F' ||
532 format_char == 'g' || format_char == 'G',
535 if (format[0] != '%')
538 if (strpbrk (format + 1, "'l%"))
541 if (!(format_char == 'e' || format_char == 'E' ||
542 format_char == 'f' || format_char == 'F' ||
543 format_char == 'g' || format_char == 'G'))
547 g_snprintf (buffer, buf_len, format, d);
549 locale_data = localeconv ();
550 decimal_point = locale_data->decimal_point;
551 decimal_point_len = strlen (decimal_point);
553 g_assert (decimal_point_len != 0);
555 if (decimal_point[0] != '.' ||
556 decimal_point[1] != 0)
560 if (*p == '+' || *p == '-')
563 while (isdigit ((guchar)*p))
566 if (strncmp (p, decimal_point, decimal_point_len) == 0)
570 if (decimal_point_len > 1) {
571 rest_len = strlen (p + (decimal_point_len-1));
572 memmove (p, p + (decimal_point_len-1),
585 * @nptr: the string to convert to a numeric value.
586 * @endptr: if non-%NULL, it returns the character after
587 * the last character used in the conversion.
588 * @base: to be used for the conversion, 2..36 or 0
590 * Converts a string to a #guint64 value.
591 * This function behaves like the standard strtull() function
592 * does in the C locale. It does this without actually
593 * changing the current locale, since that would not be
596 * This function is typically used when reading configuration
597 * files or other non-user input that should be locale independent.
598 * To handle input from the user you should normally use the
599 * locale-sensitive system strtoull() function.
601 * If the correct value would cause overflow, %G_MAXUINT64
602 * is returned, and %ERANGE is stored in %errno.
604 * Return value: the #guint64 value.
607 g_ascii_strtoull (const gchar *nptr,
611 /* this code is based on on the strtol(3) code from GLibC released under
612 * the GNU Lesser General Public License.
614 #define ISSPACE(c) ((c) == ' ' || (c) == '\f' || (c) == '\n' || \
615 (c) == '\r' || (c) == '\t' || (c) == '\v')
616 #define ISUPPER(c) ((c) >= 'A' && (c) <= 'Z')
617 #define ISLOWER(c) ((c) >= 'a' && (c) <= 'z')
618 #define ISALPHA(c) (ISUPPER (c) || ISLOWER (c))
619 #define TOUPPER(c) (ISLOWER (c) ? (c) - 'a' + 'A' : (c))
620 #define TOLOWER(c) (ISUPPER (c) ? (c) - 'A' + 'a' : (c))
621 gboolean negative, overflow;
625 const gchar *s, *save;
628 g_return_val_if_fail (nptr != NULL, 0);
630 if (base == 1 || base > 36)
638 /* Skip white space. */
644 /* Check for a sign. */
654 /* Recognize number prefix and if BASE is zero, figure it out ourselves. */
657 if ((base == 0 || base == 16) && TOUPPER (s[1]) == 'X')
668 /* Save the pointer so we can check later if anything happened. */
670 cutoff = G_MAXUINT64 / base;
671 cutlim = G_MAXUINT64 % base;
678 if (c >= '0' && c <= '9')
680 else if (ISALPHA (c))
681 c = TOUPPER (c) - 'A' + 10;
686 /* Check for overflow. */
687 if (ui64 > cutoff || (ui64 == cutoff && c > cutlim))
696 /* Check if anything actually happened. */
700 /* Store in ENDPTR the address of one character
701 past the last character we converted. */
703 *endptr = (gchar*) s;
711 /* Return the result of the appropriate sign. */
712 return negative ? -ui64 : ui64;
715 /* We must handle a special case here: the base is 0 or 16 and the
716 first two characters are '0' and 'x', but the rest are no
717 hexadecimal digits. This is no error case. We return 0 and
718 ENDPTR points to the `x`. */
721 if (save - nptr >= 2 && TOUPPER (save[-1]) == 'X'
723 *endptr = (gchar*) &save[-1];
725 /* There was no number to convert. */
726 *endptr = (gchar*) nptr;
732 G_CONST_RETURN gchar*
733 g_strerror (gint errnum)
735 static GStaticPrivate msg_private = G_STATIC_PRIVATE_INIT;
739 const char *msg_locale;
741 msg_locale = strerror (errnum);
742 if (g_get_charset (NULL))
746 gchar *msg_utf8 = g_locale_to_utf8 (msg_locale, -1, NULL, NULL, NULL);
749 /* Stick in the quark table so that we can return a static result
751 GQuark msg_quark = g_quark_from_string (msg_utf8);
754 return g_quark_to_string (msg_quark);
761 case E2BIG: return "argument list too long";
764 case EACCES: return "permission denied";
767 case EADDRINUSE: return "address already in use";
770 case EADDRNOTAVAIL: return "can't assign requested address";
773 case EADV: return "advertise error";
776 case EAFNOSUPPORT: return "address family not supported by protocol family";
779 case EAGAIN: return "try again";
782 case EALIGN: return "EALIGN";
785 case EALREADY: return "operation already in progress";
788 case EBADE: return "bad exchange descriptor";
791 case EBADF: return "bad file number";
794 case EBADFD: return "file descriptor in bad state";
797 case EBADMSG: return "not a data message";
800 case EBADR: return "bad request descriptor";
803 case EBADRPC: return "RPC structure is bad";
806 case EBADRQC: return "bad request code";
809 case EBADSLT: return "invalid slot";
812 case EBFONT: return "bad font file format";
815 case EBUSY: return "mount device busy";
818 case ECHILD: return "no children";
821 case ECHRNG: return "channel number out of range";
824 case ECOMM: return "communication error on send";
827 case ECONNABORTED: return "software caused connection abort";
830 case ECONNREFUSED: return "connection refused";
833 case ECONNRESET: return "connection reset by peer";
835 #if defined(EDEADLK) && (!defined(EWOULDBLOCK) || (EDEADLK != EWOULDBLOCK))
836 case EDEADLK: return "resource deadlock avoided";
839 case EDEADLOCK: return "resource deadlock avoided";
842 case EDESTADDRREQ: return "destination address required";
845 case EDIRTY: return "mounting a dirty fs w/o force";
848 case EDOM: return "math argument out of range";
851 case EDOTDOT: return "cross mount point";
854 case EDQUOT: return "disk quota exceeded";
857 case EDUPPKG: return "duplicate package name";
860 case EEXIST: return "file already exists";
863 case EFAULT: return "bad address in system call argument";
866 case EFBIG: return "file too large";
869 case EHOSTDOWN: return "host is down";
872 case EHOSTUNREACH: return "host is unreachable";
875 case EIDRM: return "identifier removed";
878 case EINIT: return "initialization error";
881 case EINPROGRESS: return "operation now in progress";
884 case EINTR: return "interrupted system call";
887 case EINVAL: return "invalid argument";
890 case EIO: return "I/O error";
893 case EISCONN: return "socket is already connected";
896 case EISDIR: return "is a directory";
899 case EISNAM: return "is a name file";
902 case ELBIN: return "ELBIN";
905 case EL2HLT: return "level 2 halted";
908 case EL2NSYNC: return "level 2 not synchronized";
911 case EL3HLT: return "level 3 halted";
914 case EL3RST: return "level 3 reset";
917 case ELIBACC: return "can not access a needed shared library";
920 case ELIBBAD: return "accessing a corrupted shared library";
923 case ELIBEXEC: return "can not exec a shared library directly";
926 case ELIBMAX: return "attempting to link in more shared libraries than system limit";
929 case ELIBSCN: return ".lib section in a.out corrupted";
932 case ELNRNG: return "link number out of range";
935 case ELOOP: return "too many levels of symbolic links";
938 case EMFILE: return "too many open files";
941 case EMLINK: return "too many links";
944 case EMSGSIZE: return "message too long";
947 case EMULTIHOP: return "multihop attempted";
950 case ENAMETOOLONG: return "file name too long";
953 case ENAVAIL: return "not available";
956 case ENET: return "ENET";
959 case ENETDOWN: return "network is down";
962 case ENETRESET: return "network dropped connection on reset";
965 case ENETUNREACH: return "network is unreachable";
968 case ENFILE: return "file table overflow";
971 case ENOANO: return "anode table overflow";
973 #if defined(ENOBUFS) && (!defined(ENOSR) || (ENOBUFS != ENOSR))
974 case ENOBUFS: return "no buffer space available";
977 case ENOCSI: return "no CSI structure available";
980 case ENODATA: return "no data available";
983 case ENODEV: return "no such device";
986 case ENOENT: return "no such file or directory";
989 case ENOEXEC: return "exec format error";
992 case ENOLCK: return "no locks available";
995 case ENOLINK: return "link has be severed";
998 case ENOMEM: return "not enough memory";
1001 case ENOMSG: return "no message of desired type";
1004 case ENONET: return "machine is not on the network";
1007 case ENOPKG: return "package not installed";
1010 case ENOPROTOOPT: return "bad proocol option";
1013 case ENOSPC: return "no space left on device";
1016 case ENOSR: return "out of stream resources";
1019 case ENOSTR: return "not a stream device";
1022 case ENOSYM: return "unresolved symbol name";
1025 case ENOSYS: return "function not implemented";
1028 case ENOTBLK: return "block device required";
1031 case ENOTCONN: return "socket is not connected";
1034 case ENOTDIR: return "not a directory";
1037 case ENOTEMPTY: return "directory not empty";
1040 case ENOTNAM: return "not a name file";
1043 case ENOTSOCK: return "socket operation on non-socket";
1046 case ENOTTY: return "inappropriate device for ioctl";
1049 case ENOTUNIQ: return "name not unique on network";
1052 case ENXIO: return "no such device or address";
1055 case EOPNOTSUPP: return "operation not supported on socket";
1058 case EPERM: return "not owner";
1061 case EPFNOSUPPORT: return "protocol family not supported";
1064 case EPIPE: return "broken pipe";
1067 case EPROCLIM: return "too many processes";
1070 case EPROCUNAVAIL: return "bad procedure for program";
1072 #ifdef EPROGMISMATCH
1073 case EPROGMISMATCH: return "program version wrong";
1076 case EPROGUNAVAIL: return "RPC program not available";
1079 case EPROTO: return "protocol error";
1081 #ifdef EPROTONOSUPPORT
1082 case EPROTONOSUPPORT: return "protocol not suppored";
1085 case EPROTOTYPE: return "protocol wrong type for socket";
1088 case ERANGE: return "math result unrepresentable";
1090 #if defined(EREFUSED) && (!defined(ECONNREFUSED) || (EREFUSED != ECONNREFUSED))
1091 case EREFUSED: return "EREFUSED";
1094 case EREMCHG: return "remote address changed";
1097 case EREMDEV: return "remote device";
1100 case EREMOTE: return "pathname hit remote file system";
1103 case EREMOTEIO: return "remote i/o error";
1105 #ifdef EREMOTERELEASE
1106 case EREMOTERELEASE: return "EREMOTERELEASE";
1109 case EROFS: return "read-only file system";
1112 case ERPCMISMATCH: return "RPC version is wrong";
1115 case ERREMOTE: return "object is remote";
1118 case ESHUTDOWN: return "can't send afer socket shutdown";
1120 #ifdef ESOCKTNOSUPPORT
1121 case ESOCKTNOSUPPORT: return "socket type not supported";
1124 case ESPIPE: return "invalid seek";
1127 case ESRCH: return "no such process";
1130 case ESRMNT: return "srmount error";
1133 case ESTALE: return "stale remote file handle";
1136 case ESUCCESS: return "Error 0";
1139 case ETIME: return "timer expired";
1142 case ETIMEDOUT: return "connection timed out";
1145 case ETOOMANYREFS: return "too many references: can't splice";
1148 case ETXTBSY: return "text file or pseudo-device busy";
1151 case EUCLEAN: return "structure needs cleaning";
1154 case EUNATCH: return "protocol driver not attached";
1157 case EUSERS: return "too many users";
1160 case EVERSION: return "version mismatch";
1162 #if defined(EWOULDBLOCK) && (!defined(EAGAIN) || (EWOULDBLOCK != EAGAIN))
1163 case EWOULDBLOCK: return "operation would block";
1166 case EXDEV: return "cross-domain link";
1169 case EXFULL: return "message tables full";
1172 #else /* NO_SYS_ERRLIST */
1173 extern int sys_nerr;
1174 extern char *sys_errlist[];
1176 if ((errnum > 0) && (errnum <= sys_nerr))
1177 return sys_errlist [errnum];
1178 #endif /* NO_SYS_ERRLIST */
1180 msg = g_static_private_get (&msg_private);
1183 msg = g_new (gchar, 64);
1184 g_static_private_set (&msg_private, msg, g_free);
1187 sprintf (msg, "unknown error (%d)", errnum);
1192 G_CONST_RETURN gchar*
1193 g_strsignal (gint signum)
1195 static GStaticPrivate msg_private = G_STATIC_PRIVATE_INIT;
1198 #ifdef HAVE_STRSIGNAL
1199 const char *msg_locale;
1201 #if defined(G_OS_BEOS) || defined(G_WITH_CYGWIN)
1202 extern const char *strsignal(int);
1204 /* this is declared differently (const) in string.h on BeOS */
1205 extern char *strsignal (int sig);
1206 #endif /* !G_OS_BEOS && !G_WITH_CYGWIN */
1207 msg_locale = strsignal (signum);
1208 if (g_get_charset (NULL))
1212 gchar *msg_utf8 = g_locale_to_utf8 (msg_locale, -1, NULL, NULL, NULL);
1215 /* Stick in the quark table so that we can return a static result
1217 GQuark msg_quark = g_quark_from_string (msg_utf8);
1220 return g_quark_to_string (msg_quark);
1223 #elif NO_SYS_SIGLIST
1227 case SIGHUP: return "Hangup";
1230 case SIGINT: return "Interrupt";
1233 case SIGQUIT: return "Quit";
1236 case SIGILL: return "Illegal instruction";
1239 case SIGTRAP: return "Trace/breakpoint trap";
1242 case SIGABRT: return "IOT trap/Abort";
1245 case SIGBUS: return "Bus error";
1248 case SIGFPE: return "Floating point exception";
1251 case SIGKILL: return "Killed";
1254 case SIGUSR1: return "User defined signal 1";
1257 case SIGSEGV: return "Segmentation fault";
1260 case SIGUSR2: return "User defined signal 2";
1263 case SIGPIPE: return "Broken pipe";
1266 case SIGALRM: return "Alarm clock";
1269 case SIGTERM: return "Terminated";
1272 case SIGSTKFLT: return "Stack fault";
1275 case SIGCHLD: return "Child exited";
1278 case SIGCONT: return "Continued";
1281 case SIGSTOP: return "Stopped (signal)";
1284 case SIGTSTP: return "Stopped";
1287 case SIGTTIN: return "Stopped (tty input)";
1290 case SIGTTOU: return "Stopped (tty output)";
1293 case SIGURG: return "Urgent condition";
1296 case SIGXCPU: return "CPU time limit exceeded";
1299 case SIGXFSZ: return "File size limit exceeded";
1302 case SIGVTALRM: return "Virtual time alarm";
1305 case SIGPROF: return "Profile signal";
1308 case SIGWINCH: return "Window size changed";
1311 case SIGIO: return "Possible I/O";
1314 case SIGPWR: return "Power failure";
1317 case SIGUNUSED: return "Unused signal";
1320 #else /* NO_SYS_SIGLIST */
1322 #ifdef NO_SYS_SIGLIST_DECL
1323 extern char *sys_siglist[]; /*(see Tue Jan 19 00:44:24 1999 in changelog)*/
1326 return (char*) /* this function should return const --josh */ sys_siglist [signum];
1327 #endif /* NO_SYS_SIGLIST */
1329 msg = g_static_private_get (&msg_private);
1332 msg = g_new (gchar, 64);
1333 g_static_private_set (&msg_private, msg, g_free);
1336 sprintf (msg, "unknown signal (%d)", signum);
1341 /* Functions g_strlcpy and g_strlcat were originally developed by
1342 * Todd C. Miller <Todd.Miller@courtesan.com> to simplify writing secure code.
1343 * See ftp://ftp.openbsd.org/pub/OpenBSD/src/lib/libc/string/strlcpy.3
1344 * for more information.
1348 /* Use the native ones, if available; they might be implemented in assembly */
1350 g_strlcpy (gchar *dest,
1354 g_return_val_if_fail (dest != NULL, 0);
1355 g_return_val_if_fail (src != NULL, 0);
1357 return strlcpy (dest, src, dest_size);
1361 g_strlcat (gchar *dest,
1365 g_return_val_if_fail (dest != NULL, 0);
1366 g_return_val_if_fail (src != NULL, 0);
1368 return strlcat (dest, src, dest_size);
1371 #else /* ! HAVE_STRLCPY */
1374 * Copy string src to buffer dest (of buffer size dest_size). At most
1375 * dest_size-1 characters will be copied. Always NUL terminates
1376 * (unless dest_size == 0). This function does NOT allocate memory.
1377 * Unlike strncpy, this function doesn't pad dest (so it's often faster).
1378 * Returns size of attempted result, strlen(src),
1379 * so if retval >= dest_size, truncation occurred.
1382 g_strlcpy (gchar *dest,
1386 register gchar *d = dest;
1387 register const gchar *s = src;
1388 register gsize n = dest_size;
1390 g_return_val_if_fail (dest != NULL, 0);
1391 g_return_val_if_fail (src != NULL, 0);
1393 /* Copy as many bytes as will fit */
1394 if (n != 0 && --n != 0)
1397 register gchar c = *s++;
1405 /* If not enough room in dest, add NUL and traverse rest of src */
1414 return s - src - 1; /* count does not include NUL */
1419 * Appends string src to buffer dest (of buffer size dest_size).
1420 * At most dest_size-1 characters will be copied.
1421 * Unlike strncat, dest_size is the full size of dest, not the space left over.
1422 * This function does NOT allocate memory.
1423 * This always NUL terminates (unless siz == 0 or there were no NUL characters
1424 * in the dest_size characters of dest to start with).
1425 * Returns size of attempted result, which is
1426 * MIN (dest_size, strlen (original dest)) + strlen (src),
1427 * so if retval >= dest_size, truncation occurred.
1430 g_strlcat (gchar *dest,
1434 register gchar *d = dest;
1435 register const gchar *s = src;
1436 register gsize bytes_left = dest_size;
1437 gsize dlength; /* Logically, MIN (strlen (d), dest_size) */
1439 g_return_val_if_fail (dest != NULL, 0);
1440 g_return_val_if_fail (src != NULL, 0);
1442 /* Find the end of dst and adjust bytes left but don't go past end */
1443 while (*d != 0 && bytes_left-- != 0)
1446 bytes_left = dest_size - dlength;
1448 if (bytes_left == 0)
1449 return dlength + strlen (s);
1453 if (bytes_left != 1)
1462 return dlength + (s - src); /* count does not include NUL */
1464 #endif /* ! HAVE_STRLCPY */
1469 * @len: length of @str in bytes, or -1 if @str is nul-terminated.
1471 * Converts all upper case ASCII letters to lower case ASCII letters.
1473 * Return value: a newly-allocated string, with all the upper case
1474 * characters in @str converted to lower case, with
1475 * semantics that exactly match g_ascii_tolower(). (Note
1476 * that this is unlike the old g_strdown(), which modified
1477 * the string in place.)
1480 g_ascii_strdown (const gchar *str,
1485 g_return_val_if_fail (str != NULL, NULL);
1490 result = g_strndup (str, len);
1491 for (s = result; *s; s++)
1492 *s = g_ascii_tolower (*s);
1500 * @len: length of @str in bytes, or -1 if @str is nul-terminated.
1502 * Converts all lower case ASCII letters to upper case ASCII letters.
1504 * Return value: a newly allocated string, with all the lower case
1505 * characters in @str converted to upper case, with
1506 * semantics that exactly match g_ascii_toupper(). (Note
1507 * that this is unlike the old g_strup(), which modified
1508 * the string in place.)
1511 g_ascii_strup (const gchar *str,
1516 g_return_val_if_fail (str != NULL, NULL);
1521 result = g_strndup (str, len);
1522 for (s = result; *s; s++)
1523 *s = g_ascii_toupper (*s);
1529 g_strdown (gchar *string)
1533 g_return_val_if_fail (string != NULL, NULL);
1535 s = (guchar *) string;
1544 return (gchar *) string;
1548 g_strup (gchar *string)
1552 g_return_val_if_fail (string != NULL, NULL);
1554 s = (guchar *) string;
1563 return (gchar *) string;
1567 g_strreverse (gchar *string)
1569 g_return_val_if_fail (string != NULL, NULL);
1573 register gchar *h, *t;
1576 t = string + strlen (string) - 1;
1595 * @c: any character.
1597 * Convert a character to ASCII lower case.
1599 * Unlike the standard C library tolower() function, this only
1600 * recognizes standard ASCII letters and ignores the locale, returning
1601 * all non-ASCII characters unchanged, even if they are lower case
1602 * letters in a particular character set. Also unlike the standard
1603 * library function, this takes and returns a char, not an int, so
1604 * don't call it on %EOF but no need to worry about casting to #guchar
1605 * before passing a possibly non-ASCII character in.
1607 * Return value: the result of converting @c to lower case.
1608 * If @c is not an ASCII upper case letter,
1609 * @c is returned unchanged.
1612 g_ascii_tolower (gchar c)
1614 return g_ascii_isupper (c) ? c - 'A' + 'a' : c;
1619 * @c: any character.
1621 * Convert a character to ASCII upper case.
1623 * Unlike the standard C library toupper() function, this only
1624 * recognizes standard ASCII letters and ignores the locale, returning
1625 * all non-ASCII characters unchanged, even if they are upper case
1626 * letters in a particular character set. Also unlike the standard
1627 * library function, this takes and returns a char, not an int, so
1628 * don't call it on %EOF but no need to worry about casting to #guchar
1629 * before passing a possibly non-ASCII character in.
1631 * Return value: the result of converting @c to upper case.
1632 * If @c is not an ASCII lower case letter,
1633 * @c is returned unchanged.
1636 g_ascii_toupper (gchar c)
1638 return g_ascii_islower (c) ? c - 'a' + 'A' : c;
1642 * g_ascii_digit_value:
1643 * @c: an ASCII character.
1645 * Determines the numeric value of a character as a decimal
1646 * digit. Differs from g_unichar_digit_value() because it takes
1647 * a char, so there's no worry about sign extension if characters
1650 * Return value: If @c is a decimal digit (according to
1651 * g_ascii_isdigit()), its numeric value. Otherwise, -1.
1654 g_ascii_digit_value (gchar c)
1656 if (g_ascii_isdigit (c))
1662 * g_ascii_xdigit_value:
1663 * @c: an ASCII character.
1665 * Determines the numeric value of a character as a hexidecimal
1666 * digit. Differs from g_unichar_xdigit_value() because it takes
1667 * a char, so there's no worry about sign extension if characters
1670 * Return value: If @c is a hex digit (according to
1671 * g_ascii_isxdigit()), its numeric value. Otherwise, -1.
1674 g_ascii_xdigit_value (gchar c)
1676 if (c >= 'A' && c <= 'F')
1677 return c - 'A' + 10;
1678 if (c >= 'a' && c <= 'f')
1679 return c - 'a' + 10;
1680 return g_ascii_digit_value (c);
1684 * g_ascii_strcasecmp:
1685 * @s1: string to compare with @s2.
1686 * @s2: string to compare with @s1.
1688 * Compare two strings, ignoring the case of ASCII characters.
1690 * Unlike the BSD strcasecmp() function, this only recognizes standard
1691 * ASCII letters and ignores the locale, treating all non-ASCII
1692 * characters as if they are not letters.
1694 * Return value: an integer less than, equal to, or greater than
1695 * zero if @s1 is found, respectively, to be less than,
1696 * to match, or to be greater than @s2.
1699 g_ascii_strcasecmp (const gchar *s1,
1704 g_return_val_if_fail (s1 != NULL, 0);
1705 g_return_val_if_fail (s2 != NULL, 0);
1709 c1 = (gint)(guchar) g_ascii_tolower (*s1);
1710 c2 = (gint)(guchar) g_ascii_tolower (*s2);
1716 return (((gint)(guchar) *s1) - ((gint)(guchar) *s2));
1720 * g_ascii_strncasecmp:
1721 * @s1: string to compare with @s2.
1722 * @s2: string to compare with @s1.
1723 * @n: number of characters to compare.
1725 * Compare @s1 and @s2, ignoring the case of ASCII characters and any
1726 * characters after the first @n in each string.
1728 * Unlike the BSD strcasecmp() function, this only recognizes standard
1729 * ASCII letters and ignores the locale, treating all non-ASCII
1730 * characters as if they are not letters.
1732 * Return value: an integer less than, equal to, or greater than zero
1733 * if the first @n bytes of @s1 is found, respectively,
1734 * to be less than, to match, or to be greater than the
1735 * first @n bytes of @s2.
1738 g_ascii_strncasecmp (const gchar *s1,
1744 g_return_val_if_fail (s1 != NULL, 0);
1745 g_return_val_if_fail (s2 != NULL, 0);
1747 while (n && *s1 && *s2)
1750 c1 = (gint)(guchar) g_ascii_tolower (*s1);
1751 c2 = (gint)(guchar) g_ascii_tolower (*s2);
1758 return (((gint) (guchar) *s1) - ((gint) (guchar) *s2));
1764 g_strcasecmp (const gchar *s1,
1767 #ifdef HAVE_STRCASECMP
1768 g_return_val_if_fail (s1 != NULL, 0);
1769 g_return_val_if_fail (s2 != NULL, 0);
1771 return strcasecmp (s1, s2);
1775 g_return_val_if_fail (s1 != NULL, 0);
1776 g_return_val_if_fail (s2 != NULL, 0);
1780 /* According to A. Cox, some platforms have islower's that
1781 * don't work right on non-uppercase
1783 c1 = isupper ((guchar)*s1) ? tolower ((guchar)*s1) : *s1;
1784 c2 = isupper ((guchar)*s2) ? tolower ((guchar)*s2) : *s2;
1790 return (((gint)(guchar) *s1) - ((gint)(guchar) *s2));
1795 g_strncasecmp (const gchar *s1,
1799 #ifdef HAVE_STRNCASECMP
1800 return strncasecmp (s1, s2, n);
1804 g_return_val_if_fail (s1 != NULL, 0);
1805 g_return_val_if_fail (s2 != NULL, 0);
1807 while (n && *s1 && *s2)
1810 /* According to A. Cox, some platforms have islower's that
1811 * don't work right on non-uppercase
1813 c1 = isupper ((guchar)*s1) ? tolower ((guchar)*s1) : *s1;
1814 c2 = isupper ((guchar)*s2) ? tolower ((guchar)*s2) : *s2;
1821 return (((gint) (guchar) *s1) - ((gint) (guchar) *s2));
1828 g_strdelimit (gchar *string,
1829 const gchar *delimiters,
1834 g_return_val_if_fail (string != NULL, NULL);
1837 delimiters = G_STR_DELIMITERS;
1839 for (c = string; *c; c++)
1841 if (strchr (delimiters, *c))
1849 g_strcanon (gchar *string,
1850 const gchar *valid_chars,
1855 g_return_val_if_fail (string != NULL, NULL);
1856 g_return_val_if_fail (valid_chars != NULL, NULL);
1858 for (c = string; *c; c++)
1860 if (!strchr (valid_chars, *c))
1868 g_strcompress (const gchar *source)
1870 const gchar *p = source, *octal;
1871 gchar *dest = g_malloc (strlen (source) + 1);
1881 case '0': case '1': case '2': case '3': case '4':
1882 case '5': case '6': case '7':
1885 while ((p < octal + 3) && (*p >= '0') && (*p <= '7'))
1887 *q = (*q * 8) + (*p - '0');
1908 default: /* Also handles \" and \\ */
1923 g_strescape (const gchar *source,
1924 const gchar *exceptions)
1931 g_return_val_if_fail (source != NULL, NULL);
1933 p = (guchar *) source;
1934 /* Each source byte needs maximally four destination chars (\777) */
1935 q = dest = g_malloc (strlen (source) * 4 + 1);
1937 memset (excmap, 0, 256);
1940 guchar *e = (guchar *) exceptions;
1986 if ((*p < ' ') || (*p >= 0177))
1989 *q++ = '0' + (((*p) >> 6) & 07);
1990 *q++ = '0' + (((*p) >> 3) & 07);
1991 *q++ = '0' + ((*p) & 07);
2005 g_strchug (gchar *string)
2009 g_return_val_if_fail (string != NULL, NULL);
2011 for (start = (guchar*) string; *start && g_ascii_isspace (*start); start++)
2014 g_memmove (string, start, strlen ((gchar *) start) + 1);
2020 g_strchomp (gchar *string)
2024 g_return_val_if_fail (string != NULL, NULL);
2029 for (s = string + strlen (string) - 1; s >= string && g_ascii_isspace ((guchar)*s);
2038 * @string: a string to split.
2039 * @delimiter: a string which specifies the places at which to split the string.
2040 * The delimiter is not included in any of the resulting strings, unless
2041 * @max_tokens is reached.
2042 * @max_tokens: the maximum number of pieces to split @string into. If this is
2043 * less than 1, the string is split completely.
2045 * Splits a string into a maximum of @max_tokens pieces, using the given
2046 * @delimiter. If @max_tokens is reached, the remainder of @string is appended
2047 * to the last token.
2049 * As a special case, the result of splitting the empty string "" is an empty
2050 * vector, not a vector containing a single string. The reason for this
2051 * special case is that being able to represent a empty vector is typically
2052 * more useful than consistent handling of empty elements. If you do need
2053 * to represent empty elements, you'll need to check for the empty string
2054 * before calling g_strsplit().
2056 * Return value: a newly-allocated %NULL-terminated array of strings. Use
2057 * g_strfreev() to free it.
2060 g_strsplit (const gchar *string,
2061 const gchar *delimiter,
2064 GSList *string_list = NULL, *slist;
2065 gchar **str_array, *s;
2067 const gchar *remainder;
2069 g_return_val_if_fail (string != NULL, NULL);
2070 g_return_val_if_fail (delimiter != NULL, NULL);
2071 g_return_val_if_fail (delimiter[0] != '\0', NULL);
2074 max_tokens = G_MAXINT;
2077 s = strstr (remainder, delimiter);
2080 gsize delimiter_len = strlen (delimiter);
2082 while (--max_tokens && s)
2087 len = s - remainder;
2088 new_string = g_new (gchar, len + 1);
2089 strncpy (new_string, remainder, len);
2090 new_string[len] = 0;
2091 string_list = g_slist_prepend (string_list, new_string);
2093 remainder = s + delimiter_len;
2094 s = strstr (remainder, delimiter);
2100 string_list = g_slist_prepend (string_list, g_strdup (remainder));
2103 str_array = g_new (gchar*, n + 1);
2105 str_array[n--] = NULL;
2106 for (slist = string_list; slist; slist = slist->next)
2107 str_array[n--] = slist->data;
2109 g_slist_free (string_list);
2115 g_strfreev (gchar **str_array)
2121 for(i = 0; str_array[i] != NULL; i++)
2122 g_free(str_array[i]);
2130 * @str_array: %NULL-terminated array of strings.
2132 * Copies %NULL-terminated array of strings. The copy is a deep copy;
2133 * the new array should be freed by first freeing each string, then
2134 * the array itself. g_strfreev() does this for you. If called
2135 * on a %NULL value, g_strdupv() simply returns %NULL.
2137 * Return value: a new %NULL-terminated array of strings.
2140 g_strdupv (gchar **str_array)
2148 while (str_array[i])
2151 retval = g_new (gchar*, i + 1);
2154 while (str_array[i])
2156 retval[i] = g_strdup (str_array[i]);
2168 g_strjoinv (const gchar *separator,
2174 g_return_val_if_fail (str_array != NULL, NULL);
2176 if (separator == NULL)
2183 gsize separator_len;
2185 separator_len = strlen (separator);
2186 /* First part, getting length */
2187 len = 1 + strlen (str_array[0]);
2188 for (i = 1; str_array[i] != NULL; i++)
2189 len += strlen (str_array[i]);
2190 len += separator_len * (i - 1);
2192 /* Second part, building string */
2193 string = g_new (gchar, len);
2194 ptr = g_stpcpy (string, *str_array);
2195 for (i = 1; str_array[i] != NULL; i++)
2197 ptr = g_stpcpy (ptr, separator);
2198 ptr = g_stpcpy (ptr, str_array[i]);
2202 string = g_strdup ("");
2208 g_strjoin (const gchar *separator,
2214 gsize separator_len;
2217 if (separator == NULL)
2220 separator_len = strlen (separator);
2222 va_start (args, separator);
2224 s = va_arg (args, gchar*);
2228 /* First part, getting length */
2229 len = 1 + strlen (s);
2231 s = va_arg (args, gchar*);
2234 len += separator_len + strlen (s);
2235 s = va_arg (args, gchar*);
2239 /* Second part, building string */
2240 string = g_new (gchar, len);
2242 va_start (args, separator);
2244 s = va_arg (args, gchar*);
2245 ptr = g_stpcpy (string, s);
2247 s = va_arg (args, gchar*);
2250 ptr = g_stpcpy (ptr, separator);
2251 ptr = g_stpcpy (ptr, s);
2252 s = va_arg (args, gchar*);
2256 string = g_strdup ("");
2266 * @haystack: a string.
2267 * @haystack_len: the maximum length of @haystack.
2268 * @needle: the string to search for.
2270 * Searches the string @haystack for the first occurrence
2271 * of the string @needle, limiting the length of the search
2274 * Return value: a pointer to the found occurrence, or
2275 * %NULL if not found.
2278 g_strstr_len (const gchar *haystack,
2279 gssize haystack_len,
2280 const gchar *needle)
2282 g_return_val_if_fail (haystack != NULL, NULL);
2283 g_return_val_if_fail (needle != NULL, NULL);
2285 if (haystack_len < 0)
2286 return strstr (haystack, needle);
2289 const gchar *p = haystack;
2290 gsize needle_len = strlen (needle);
2294 if (needle_len == 0)
2295 return (gchar *)haystack;
2297 if (haystack_len < needle_len)
2300 end = haystack + haystack_len - needle_len;
2302 while (*p && p <= end)
2304 for (i = 0; i < needle_len; i++)
2305 if (p[i] != needle[i])
2320 * @haystack: a nul-terminated string.
2321 * @needle: the nul-terminated string to search for.
2323 * Searches the string @haystack for the last occurrence
2324 * of the string @needle.
2326 * Return value: a pointer to the found occurrence, or
2327 * %NULL if not found.
2330 g_strrstr (const gchar *haystack,
2331 const gchar *needle)
2338 g_return_val_if_fail (haystack != NULL, NULL);
2339 g_return_val_if_fail (needle != NULL, NULL);
2341 needle_len = strlen (needle);
2342 haystack_len = strlen (haystack);
2344 if (needle_len == 0)
2345 return (gchar *)haystack;
2347 if (haystack_len < needle_len)
2350 p = haystack + haystack_len - needle_len;
2352 while (p >= haystack)
2354 for (i = 0; i < needle_len; i++)
2355 if (p[i] != needle[i])
2369 * @haystack: a nul-terminated string.
2370 * @haystack_len: the maximum length of @haystack.
2371 * @needle: the nul-terminated string to search for.
2373 * Searches the string @haystack for the last occurrence
2374 * of the string @needle, limiting the length of the search
2377 * Return value: a pointer to the found occurrence, or
2378 * %NULL if not found.
2381 g_strrstr_len (const gchar *haystack,
2382 gssize haystack_len,
2383 const gchar *needle)
2385 g_return_val_if_fail (haystack != NULL, NULL);
2386 g_return_val_if_fail (needle != NULL, NULL);
2388 if (haystack_len < 0)
2389 return g_strrstr (haystack, needle);
2392 gsize needle_len = strlen (needle);
2393 const gchar *haystack_max = haystack + haystack_len;
2394 const gchar *p = haystack;
2397 while (p < haystack_max && *p)
2400 if (p < haystack + needle_len)
2405 while (p >= haystack)
2407 for (i = 0; i < needle_len; i++)
2408 if (p[i] != needle[i])
2424 * @str: a nul-terminated string.
2425 * @suffix: the nul-terminated suffix to look for.
2427 * Looks whether the string @str ends with @suffix.
2429 * Return value: %TRUE if @str end with @suffix, %FALSE otherwise.
2432 g_str_has_suffix (const gchar *str,
2433 const gchar *suffix)
2438 g_return_val_if_fail (str != NULL, FALSE);
2439 g_return_val_if_fail (suffix != NULL, FALSE);
2441 str_len = strlen (str);
2442 suffix_len = strlen (suffix);
2444 if (str_len < suffix_len)
2447 return strcmp (str + str_len - suffix_len, suffix) == 0;
2452 * @str: a nul-terminated string.
2453 * @prefix: the nul-terminated prefix to look for.
2455 * Looks whether the string @str begins with @prefix.
2457 * Return value: %TRUE if @str begins with @prefix, %FALSE otherwise.
2460 g_str_has_prefix (const gchar *str,
2461 const gchar *prefix)
2466 g_return_val_if_fail (str != NULL, FALSE);
2467 g_return_val_if_fail (prefix != NULL, FALSE);
2469 str_len = strlen (str);
2470 prefix_len = strlen (prefix);
2472 if (str_len < prefix_len)
2475 return strncmp (str, prefix, prefix_len) == 0;