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 Library 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 * Library General Public License for more details.
14 * You should have received a copy of the GNU Library 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-1999. 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/.
40 #include <ctype.h> /* For tolower() */
41 #if !defined (HAVE_STRSIGNAL) || !defined(NO_SYS_SIGLIST_DECL)
45 /* do not include <unistd.h> in this place since it
46 * inteferes with g_strsignal() on some OSes
50 g_strdup (const gchar *str)
56 new_str = g_new (char, strlen (str) + 1);
57 strcpy (new_str, str);
66 g_memdup (gconstpointer mem,
73 new_mem = g_malloc (byte_size);
74 memcpy (new_mem, mem, byte_size);
83 g_strndup (const gchar *str,
90 new_str = g_new (gchar, n + 1);
91 strncpy (new_str, str, n);
101 g_strnfill (guint length,
104 register gchar *str, *s, *end;
106 str = g_new (gchar, length + 1);
117 g_strdup_vprintf (const gchar *format,
123 G_VA_COPY (args2, args1);
125 buffer = g_new (gchar, g_printf_string_upper_bound (format, args1));
127 vsprintf (buffer, format, args2);
134 g_strdup_printf (const gchar *format,
140 va_start (args, format);
141 buffer = g_strdup_vprintf (format, args);
148 g_strconcat (const gchar *string1, ...)
155 g_return_val_if_fail (string1 != NULL, NULL);
157 l = 1 + strlen (string1);
158 va_start (args, string1);
159 s = va_arg (args, gchar*);
163 s = va_arg (args, gchar*);
167 concat = g_new (gchar, l);
170 strcat (concat, string1);
171 va_start (args, string1);
172 s = va_arg (args, gchar*);
176 s = va_arg (args, gchar*);
184 g_strtod (const gchar *nptr,
192 g_return_val_if_fail (nptr != NULL, 0);
197 val_1 = strtod (nptr, &fail_pos_1);
199 if (fail_pos_1 && fail_pos_1[0] != 0)
203 old_locale = g_strdup (setlocale (LC_NUMERIC, NULL));
204 setlocale (LC_NUMERIC, "C");
205 val_2 = strtod (nptr, &fail_pos_2);
206 setlocale (LC_NUMERIC, old_locale);
210 if (!fail_pos_1 || fail_pos_1[0] == 0 || fail_pos_1 >= fail_pos_2)
213 *endptr = fail_pos_1;
219 *endptr = fail_pos_2;
225 g_strerror (gint errnum)
227 static GStaticPrivate msg_private = G_STATIC_PRIVATE_INIT;
231 return strerror (errnum);
236 case E2BIG: return "argument list too long";
239 case EACCES: return "permission denied";
242 case EADDRINUSE: return "address already in use";
245 case EADDRNOTAVAIL: return "can't assign requested address";
248 case EADV: return "advertise error";
251 case EAFNOSUPPORT: return "address family not supported by protocol family";
254 case EAGAIN: return "try again";
257 case EALIGN: return "EALIGN";
260 case EALREADY: return "operation already in progress";
263 case EBADE: return "bad exchange descriptor";
266 case EBADF: return "bad file number";
269 case EBADFD: return "file descriptor in bad state";
272 case EBADMSG: return "not a data message";
275 case EBADR: return "bad request descriptor";
278 case EBADRPC: return "RPC structure is bad";
281 case EBADRQC: return "bad request code";
284 case EBADSLT: return "invalid slot";
287 case EBFONT: return "bad font file format";
290 case EBUSY: return "mount device busy";
293 case ECHILD: return "no children";
296 case ECHRNG: return "channel number out of range";
299 case ECOMM: return "communication error on send";
302 case ECONNABORTED: return "software caused connection abort";
305 case ECONNREFUSED: return "connection refused";
308 case ECONNRESET: return "connection reset by peer";
310 #if defined(EDEADLK) && (!defined(EWOULDBLOCK) || (EDEADLK != EWOULDBLOCK))
311 case EDEADLK: return "resource deadlock avoided";
314 case EDEADLOCK: return "resource deadlock avoided";
317 case EDESTADDRREQ: return "destination address required";
320 case EDIRTY: return "mounting a dirty fs w/o force";
323 case EDOM: return "math argument out of range";
326 case EDOTDOT: return "cross mount point";
329 case EDQUOT: return "disk quota exceeded";
332 case EDUPPKG: return "duplicate package name";
335 case EEXIST: return "file already exists";
338 case EFAULT: return "bad address in system call argument";
341 case EFBIG: return "file too large";
344 case EHOSTDOWN: return "host is down";
347 case EHOSTUNREACH: return "host is unreachable";
350 case EIDRM: return "identifier removed";
353 case EINIT: return "initialization error";
356 case EINPROGRESS: return "operation now in progress";
359 case EINTR: return "interrupted system call";
362 case EINVAL: return "invalid argument";
365 case EIO: return "I/O error";
368 case EISCONN: return "socket is already connected";
371 case EISDIR: return "illegal operation on a directory";
374 case EISNAM: return "is a name file";
377 case ELBIN: return "ELBIN";
380 case EL2HLT: return "level 2 halted";
383 case EL2NSYNC: return "level 2 not synchronized";
386 case EL3HLT: return "level 3 halted";
389 case EL3RST: return "level 3 reset";
392 case ELIBACC: return "can not access a needed shared library";
395 case ELIBBAD: return "accessing a corrupted shared library";
398 case ELIBEXEC: return "can not exec a shared library directly";
401 case ELIBMAX: return "attempting to link in more shared libraries than system limit";
404 case ELIBSCN: return ".lib section in a.out corrupted";
407 case ELNRNG: return "link number out of range";
410 case ELOOP: return "too many levels of symbolic links";
413 case EMFILE: return "too many open files";
416 case EMLINK: return "too many links";
419 case EMSGSIZE: return "message too long";
422 case EMULTIHOP: return "multihop attempted";
425 case ENAMETOOLONG: return "file name too long";
428 case ENAVAIL: return "not available";
431 case ENET: return "ENET";
434 case ENETDOWN: return "network is down";
437 case ENETRESET: return "network dropped connection on reset";
440 case ENETUNREACH: return "network is unreachable";
443 case ENFILE: return "file table overflow";
446 case ENOANO: return "anode table overflow";
448 #if defined(ENOBUFS) && (!defined(ENOSR) || (ENOBUFS != ENOSR))
449 case ENOBUFS: return "no buffer space available";
452 case ENOCSI: return "no CSI structure available";
455 case ENODATA: return "no data available";
458 case ENODEV: return "no such device";
461 case ENOENT: return "no such file or directory";
464 case ENOEXEC: return "exec format error";
467 case ENOLCK: return "no locks available";
470 case ENOLINK: return "link has be severed";
473 case ENOMEM: return "not enough memory";
476 case ENOMSG: return "no message of desired type";
479 case ENONET: return "machine is not on the network";
482 case ENOPKG: return "package not installed";
485 case ENOPROTOOPT: return "bad proocol option";
488 case ENOSPC: return "no space left on device";
491 case ENOSR: return "out of stream resources";
494 case ENOSTR: return "not a stream device";
497 case ENOSYM: return "unresolved symbol name";
500 case ENOSYS: return "function not implemented";
503 case ENOTBLK: return "block device required";
506 case ENOTCONN: return "socket is not connected";
509 case ENOTDIR: return "not a directory";
512 case ENOTEMPTY: return "directory not empty";
515 case ENOTNAM: return "not a name file";
518 case ENOTSOCK: return "socket operation on non-socket";
521 case ENOTTY: return "inappropriate device for ioctl";
524 case ENOTUNIQ: return "name not unique on network";
527 case ENXIO: return "no such device or address";
530 case EOPNOTSUPP: return "operation not supported on socket";
533 case EPERM: return "not owner";
536 case EPFNOSUPPORT: return "protocol family not supported";
539 case EPIPE: return "broken pipe";
542 case EPROCLIM: return "too many processes";
545 case EPROCUNAVAIL: return "bad procedure for program";
548 case EPROGMISMATCH: return "program version wrong";
551 case EPROGUNAVAIL: return "RPC program not available";
554 case EPROTO: return "protocol error";
556 #ifdef EPROTONOSUPPORT
557 case EPROTONOSUPPORT: return "protocol not suppored";
560 case EPROTOTYPE: return "protocol wrong type for socket";
563 case ERANGE: return "math result unrepresentable";
565 #if defined(EREFUSED) && (!defined(ECONNREFUSED) || (EREFUSED != ECONNREFUSED))
566 case EREFUSED: return "EREFUSED";
569 case EREMCHG: return "remote address changed";
572 case EREMDEV: return "remote device";
575 case EREMOTE: return "pathname hit remote file system";
578 case EREMOTEIO: return "remote i/o error";
580 #ifdef EREMOTERELEASE
581 case EREMOTERELEASE: return "EREMOTERELEASE";
584 case EROFS: return "read-only file system";
587 case ERPCMISMATCH: return "RPC version is wrong";
590 case ERREMOTE: return "object is remote";
593 case ESHUTDOWN: return "can't send afer socket shutdown";
595 #ifdef ESOCKTNOSUPPORT
596 case ESOCKTNOSUPPORT: return "socket type not supported";
599 case ESPIPE: return "invalid seek";
602 case ESRCH: return "no such process";
605 case ESRMNT: return "srmount error";
608 case ESTALE: return "stale remote file handle";
611 case ESUCCESS: return "Error 0";
614 case ETIME: return "timer expired";
617 case ETIMEDOUT: return "connection timed out";
620 case ETOOMANYREFS: return "too many references: can't splice";
623 case ETXTBSY: return "text file or pseudo-device busy";
626 case EUCLEAN: return "structure needs cleaning";
629 case EUNATCH: return "protocol driver not attached";
632 case EUSERS: return "too many users";
635 case EVERSION: return "version mismatch";
637 #if defined(EWOULDBLOCK) && (!defined(EAGAIN) || (EWOULDBLOCK != EAGAIN))
638 case EWOULDBLOCK: return "operation would block";
641 case EXDEV: return "cross-domain link";
644 case EXFULL: return "message tables full";
647 #else /* NO_SYS_ERRLIST */
649 extern char *sys_errlist[];
651 if ((errnum > 0) && (errnum <= sys_nerr))
652 return sys_errlist [errnum];
653 #endif /* NO_SYS_ERRLIST */
655 msg = g_static_private_get (&msg_private);
658 msg = g_new (gchar, 64);
659 g_static_private_set (&msg_private, msg, g_free);
662 sprintf (msg, "unknown error (%d)", errnum);
668 g_strsignal (gint signum)
670 static GStaticPrivate msg_private = G_STATIC_PRIVATE_INIT;
673 #ifdef HAVE_STRSIGNAL
675 extern const char * strsignal(int);
676 #else /* !G_OS_BEOS */
677 /* this is declared differently (const) in string.h on BeOS */
678 extern char *strsignal (int sig);
679 #endif /* !G_OS_BEOS */
680 return strsignal (signum);
685 case SIGHUP: return "Hangup";
688 case SIGINT: return "Interrupt";
691 case SIGQUIT: return "Quit";
694 case SIGILL: return "Illegal instruction";
697 case SIGTRAP: return "Trace/breakpoint trap";
700 case SIGABRT: return "IOT trap/Abort";
703 case SIGBUS: return "Bus error";
706 case SIGFPE: return "Floating point exception";
709 case SIGKILL: return "Killed";
712 case SIGUSR1: return "User defined signal 1";
715 case SIGSEGV: return "Segmentation fault";
718 case SIGUSR2: return "User defined signal 2";
721 case SIGPIPE: return "Broken pipe";
724 case SIGALRM: return "Alarm clock";
727 case SIGTERM: return "Terminated";
730 case SIGSTKFLT: return "Stack fault";
733 case SIGCHLD: return "Child exited";
736 case SIGCONT: return "Continued";
739 case SIGSTOP: return "Stopped (signal)";
742 case SIGTSTP: return "Stopped";
745 case SIGTTIN: return "Stopped (tty input)";
748 case SIGTTOU: return "Stopped (tty output)";
751 case SIGURG: return "Urgent condition";
754 case SIGXCPU: return "CPU time limit exceeded";
757 case SIGXFSZ: return "File size limit exceeded";
760 case SIGVTALRM: return "Virtual time alarm";
763 case SIGPROF: return "Profile signal";
766 case SIGWINCH: return "Window size changed";
769 case SIGIO: return "Possible I/O";
772 case SIGPWR: return "Power failure";
775 case SIGUNUSED: return "Unused signal";
778 #else /* NO_SYS_SIGLIST */
780 #ifdef NO_SYS_SIGLIST_DECL
781 extern char *sys_siglist[]; /*(see Tue Jan 19 00:44:24 1999 in changelog)*/
784 return (char*) /* this function should return const --josh */ sys_siglist [signum];
785 #endif /* NO_SYS_SIGLIST */
787 msg = g_static_private_get (&msg_private);
790 msg = g_new (gchar, 64);
791 g_static_private_set (&msg_private, msg, g_free);
794 sprintf (msg, "unknown signal (%d)", signum);
800 g_strdown (gchar *string)
804 g_return_if_fail (string != NULL);
816 g_strup (gchar *string)
820 g_return_if_fail (string != NULL);
832 g_strreverse (gchar *string)
834 g_return_if_fail (string != NULL);
838 register gchar *h, *t;
841 t = string + strlen (string) - 1;
857 g_strcasecmp (const gchar *s1,
860 #ifdef HAVE_STRCASECMP
861 g_return_val_if_fail (s1 != NULL, 0);
862 g_return_val_if_fail (s2 != NULL, 0);
864 return strcasecmp (s1, s2);
868 g_return_val_if_fail (s1 != NULL, 0);
869 g_return_val_if_fail (s2 != NULL, 0);
873 /* According to A. Cox, some platforms have islower's that
874 * don't work right on non-uppercase
876 c1 = isupper ((guchar)*s1) ? tolower ((guchar)*s1) : *s1;
877 c2 = isupper ((guchar)*s2) ? tolower ((guchar)*s2) : *s2;
883 return (((gint)(guchar) *s1) - ((gint)(guchar) *s2));
888 g_strncasecmp (const gchar *s1,
892 #ifdef HAVE_STRNCASECMP
893 return strncasecmp (s1, s2, n);
897 g_return_val_if_fail (s1 != NULL, 0);
898 g_return_val_if_fail (s2 != NULL, 0);
900 while (n-- && *s1 && *s2)
902 /* According to A. Cox, some platforms have islower's that
903 * don't work right on non-uppercase
905 c1 = isupper ((guchar)*s1) ? tolower ((guchar)*s1) : *s1;
906 c2 = isupper ((guchar)*s2) ? tolower ((guchar)*s2) : *s2;
913 return (((gint)(guchar) *s1) - ((gint)(guchar) *s2));
920 g_strdelimit (gchar *string,
921 const gchar *delimiters,
926 g_return_val_if_fail (string != NULL, NULL);
929 delimiters = G_STR_DELIMITERS;
931 for (c = string; *c; c++)
933 if (strchr (delimiters, *c))
941 g_strcompress (const gchar *source)
943 const gchar *p = source, *octal;
944 gchar *dest = g_malloc (strlen (source) + 1);
954 case '0': case '1': case '2': case '3': case '4':
955 case '5': case '6': case '7':
958 while ((p < octal + 3) && (*p >= '0') && (*p <= '7'))
960 *q = (*q * 8) + (*p - '0');
981 default: /* Also handles \" and \\ */
995 g_strescape (const gchar *source,
996 const gchar *exceptions)
998 const guchar *p = (guchar *) source;
999 /* Each source byte needs maximally four destination chars (\777) */
1000 gchar *dest = g_malloc (strlen (source) * 4 + 1);
1004 memset (excmap, 0, 256);
1007 guchar *e = (guchar *) exceptions;
1053 if ((*p < ' ') || (*p >= 0177))
1056 *q++ = '0' + (((*p) >> 6) & 07);
1057 *q++ = '0' + (((*p) >> 3) & 07);
1058 *q++ = '0' + ((*p) & 07);
1071 /* blame Elliot for these next five routines */
1073 g_strchug (gchar *string)
1077 g_return_val_if_fail (string != NULL, NULL);
1079 for (start = string; *start && isspace (*start); start++)
1082 g_memmove(string, start, strlen(start) + 1);
1088 g_strchomp (gchar *string)
1092 g_return_val_if_fail (string != NULL, NULL);
1097 for (s = string + strlen (string) - 1; s >= string && isspace ((guchar)*s);
1105 g_strsplit (const gchar *string,
1106 const gchar *delimiter,
1109 GSList *string_list = NULL, *slist;
1110 gchar **str_array, *s;
1113 g_return_val_if_fail (string != NULL, NULL);
1114 g_return_val_if_fail (delimiter != NULL, NULL);
1117 max_tokens = G_MAXINT;
1119 s = strstr (string, delimiter);
1122 guint delimiter_len = strlen (delimiter);
1130 new_string = g_new (gchar, len + 1);
1131 strncpy (new_string, string, len);
1132 new_string[len] = 0;
1133 string_list = g_slist_prepend (string_list, new_string);
1135 string = s + delimiter_len;
1136 s = strstr (string, delimiter);
1138 while (--max_tokens && s);
1143 string_list = g_slist_prepend (string_list, g_strdup (string));
1146 str_array = g_new (gchar*, n);
1150 str_array[i--] = NULL;
1151 for (slist = string_list; slist; slist = slist->next)
1152 str_array[i--] = slist->data;
1154 g_slist_free (string_list);
1160 g_strfreev (gchar **str_array)
1166 for(i = 0; str_array[i] != NULL; i++)
1167 g_free(str_array[i]);
1174 g_strjoinv (const gchar *separator,
1179 g_return_val_if_fail (str_array != NULL, NULL);
1181 if (separator == NULL)
1187 guint separator_len;
1189 separator_len = strlen (separator);
1190 len = 1 + strlen (str_array[0]);
1191 for(i = 1; str_array[i] != NULL; i++)
1192 len += separator_len + strlen(str_array[i]);
1194 string = g_new (gchar, len);
1196 strcat (string, *str_array);
1197 for (i = 1; str_array[i] != NULL; i++)
1199 strcat (string, separator);
1200 strcat (string, str_array[i]);
1204 string = g_strdup ("");
1210 g_strjoin (const gchar *separator,
1216 guint separator_len;
1218 if (separator == NULL)
1221 separator_len = strlen (separator);
1223 va_start (args, separator);
1225 s = va_arg (args, gchar*);
1231 s = va_arg (args, gchar*);
1234 len += separator_len + strlen (s);
1235 s = va_arg (args, gchar*);
1239 string = g_new (gchar, len + 1);
1242 va_start (args, separator);
1244 s = va_arg (args, gchar*);
1247 s = va_arg (args, gchar*);
1250 strcat (string, separator);
1252 s = va_arg (args, gchar*);
1256 string = g_strdup ("");