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.
33 #include <ctype.h> /* For tolower() */
34 #if !defined (HAVE_STRSIGNAL) && NO_SYS_SIGLIST
38 /* do not include <unistd.h> in this place since it
39 * inteferes with g_strsignal() on some OSes
43 g_strdup (const gchar *str)
49 new_str = g_new (char, strlen (str) + 1);
50 strcpy (new_str, str);
59 g_memdup (gconstpointer mem,
66 new_mem = g_malloc (byte_size);
67 memcpy (new_mem, mem, byte_size);
76 g_strndup (const gchar *str,
83 new_str = g_new (gchar, n + 1);
84 strncpy (new_str, str, n);
94 g_strnfill (guint length,
97 register gchar *str, *s, *end;
99 str = g_new (gchar, length + 1);
110 g_strdup_vprintf (const gchar *format,
116 G_VA_COPY (args2, args1);
118 buffer = g_new (gchar, g_printf_string_upper_bound (format, args1));
120 vsprintf (buffer, format, args2);
127 g_strdup_printf (const gchar *format,
133 va_start (args, format);
134 buffer = g_strdup_vprintf (format, args);
141 g_strconcat (const gchar *string1, ...)
148 g_return_val_if_fail (string1 != NULL, NULL);
150 l = 1 + strlen (string1);
151 va_start (args, string1);
152 s = va_arg (args, gchar*);
156 s = va_arg (args, gchar*);
160 concat = g_new (gchar, l);
163 strcat (concat, string1);
164 va_start (args, string1);
165 s = va_arg (args, gchar*);
169 s = va_arg (args, gchar*);
177 g_strtod (const gchar *nptr,
185 g_return_val_if_fail (nptr != NULL, 0);
190 val_1 = strtod (nptr, &fail_pos_1);
192 if (fail_pos_1 && fail_pos_1[0] != 0)
196 old_locale = setlocale (LC_NUMERIC, "C");
197 val_2 = strtod (nptr, &fail_pos_2);
198 setlocale (LC_NUMERIC, old_locale);
201 if (!fail_pos_1 || fail_pos_1[0] == 0 || fail_pos_1 >= fail_pos_2)
204 *endptr = fail_pos_1;
210 *endptr = fail_pos_2;
216 g_strerror (gint errnum)
218 static GStaticPrivate msg_private = G_STATIC_PRIVATE_INIT;
222 return strerror (errnum);
227 case E2BIG: return "argument list too long";
230 case EACCES: return "permission denied";
233 case EADDRINUSE: return "address already in use";
236 case EADDRNOTAVAIL: return "can't assign requested address";
239 case EADV: return "advertise error";
242 case EAFNOSUPPORT: return "address family not supported by protocol family";
245 case EAGAIN: return "try again";
248 case EALIGN: return "EALIGN";
251 case EALREADY: return "operation already in progress";
254 case EBADE: return "bad exchange descriptor";
257 case EBADF: return "bad file number";
260 case EBADFD: return "file descriptor in bad state";
263 case EBADMSG: return "not a data message";
266 case EBADR: return "bad request descriptor";
269 case EBADRPC: return "RPC structure is bad";
272 case EBADRQC: return "bad request code";
275 case EBADSLT: return "invalid slot";
278 case EBFONT: return "bad font file format";
281 case EBUSY: return "mount device busy";
284 case ECHILD: return "no children";
287 case ECHRNG: return "channel number out of range";
290 case ECOMM: return "communication error on send";
293 case ECONNABORTED: return "software caused connection abort";
296 case ECONNREFUSED: return "connection refused";
299 case ECONNRESET: return "connection reset by peer";
301 #if defined(EDEADLK) && (!defined(EWOULDBLOCK) || (EDEADLK != EWOULDBLOCK))
302 case EDEADLK: return "resource deadlock avoided";
305 case EDEADLOCK: return "resource deadlock avoided";
308 case EDESTADDRREQ: return "destination address required";
311 case EDIRTY: return "mounting a dirty fs w/o force";
314 case EDOM: return "math argument out of range";
317 case EDOTDOT: return "cross mount point";
320 case EDQUOT: return "disk quota exceeded";
323 case EDUPPKG: return "duplicate package name";
326 case EEXIST: return "file already exists";
329 case EFAULT: return "bad address in system call argument";
332 case EFBIG: return "file too large";
335 case EHOSTDOWN: return "host is down";
338 case EHOSTUNREACH: return "host is unreachable";
341 case EIDRM: return "identifier removed";
344 case EINIT: return "initialization error";
347 case EINPROGRESS: return "operation now in progress";
350 case EINTR: return "interrupted system call";
353 case EINVAL: return "invalid argument";
356 case EIO: return "I/O error";
359 case EISCONN: return "socket is already connected";
362 case EISDIR: return "illegal operation on a directory";
365 case EISNAM: return "is a name file";
368 case ELBIN: return "ELBIN";
371 case EL2HLT: return "level 2 halted";
374 case EL2NSYNC: return "level 2 not synchronized";
377 case EL3HLT: return "level 3 halted";
380 case EL3RST: return "level 3 reset";
383 case ELIBACC: return "can not access a needed shared library";
386 case ELIBBAD: return "accessing a corrupted shared library";
389 case ELIBEXEC: return "can not exec a shared library directly";
392 case ELIBMAX: return "attempting to link in more shared libraries than system limit";
395 case ELIBSCN: return ".lib section in a.out corrupted";
398 case ELNRNG: return "link number out of range";
401 case ELOOP: return "too many levels of symbolic links";
404 case EMFILE: return "too many open files";
407 case EMLINK: return "too many links";
410 case EMSGSIZE: return "message too long";
413 case EMULTIHOP: return "multihop attempted";
416 case ENAMETOOLONG: return "file name too long";
419 case ENAVAIL: return "not available";
422 case ENET: return "ENET";
425 case ENETDOWN: return "network is down";
428 case ENETRESET: return "network dropped connection on reset";
431 case ENETUNREACH: return "network is unreachable";
434 case ENFILE: return "file table overflow";
437 case ENOANO: return "anode table overflow";
439 #if defined(ENOBUFS) && (!defined(ENOSR) || (ENOBUFS != ENOSR))
440 case ENOBUFS: return "no buffer space available";
443 case ENOCSI: return "no CSI structure available";
446 case ENODATA: return "no data available";
449 case ENODEV: return "no such device";
452 case ENOENT: return "no such file or directory";
455 case ENOEXEC: return "exec format error";
458 case ENOLCK: return "no locks available";
461 case ENOLINK: return "link has be severed";
464 case ENOMEM: return "not enough memory";
467 case ENOMSG: return "no message of desired type";
470 case ENONET: return "machine is not on the network";
473 case ENOPKG: return "package not installed";
476 case ENOPROTOOPT: return "bad proocol option";
479 case ENOSPC: return "no space left on device";
482 case ENOSR: return "out of stream resources";
485 case ENOSTR: return "not a stream device";
488 case ENOSYM: return "unresolved symbol name";
491 case ENOSYS: return "function not implemented";
494 case ENOTBLK: return "block device required";
497 case ENOTCONN: return "socket is not connected";
500 case ENOTDIR: return "not a directory";
503 case ENOTEMPTY: return "directory not empty";
506 case ENOTNAM: return "not a name file";
509 case ENOTSOCK: return "socket operation on non-socket";
512 case ENOTTY: return "inappropriate device for ioctl";
515 case ENOTUNIQ: return "name not unique on network";
518 case ENXIO: return "no such device or address";
521 case EOPNOTSUPP: return "operation not supported on socket";
524 case EPERM: return "not owner";
527 case EPFNOSUPPORT: return "protocol family not supported";
530 case EPIPE: return "broken pipe";
533 case EPROCLIM: return "too many processes";
536 case EPROCUNAVAIL: return "bad procedure for program";
539 case EPROGMISMATCH: return "program version wrong";
542 case EPROGUNAVAIL: return "RPC program not available";
545 case EPROTO: return "protocol error";
547 #ifdef EPROTONOSUPPORT
548 case EPROTONOSUPPORT: return "protocol not suppored";
551 case EPROTOTYPE: return "protocol wrong type for socket";
554 case ERANGE: return "math result unrepresentable";
556 #if defined(EREFUSED) && (!defined(ECONNREFUSED) || (EREFUSED != ECONNREFUSED))
557 case EREFUSED: return "EREFUSED";
560 case EREMCHG: return "remote address changed";
563 case EREMDEV: return "remote device";
566 case EREMOTE: return "pathname hit remote file system";
569 case EREMOTEIO: return "remote i/o error";
571 #ifdef EREMOTERELEASE
572 case EREMOTERELEASE: return "EREMOTERELEASE";
575 case EROFS: return "read-only file system";
578 case ERPCMISMATCH: return "RPC version is wrong";
581 case ERREMOTE: return "object is remote";
584 case ESHUTDOWN: return "can't send afer socket shutdown";
586 #ifdef ESOCKTNOSUPPORT
587 case ESOCKTNOSUPPORT: return "socket type not supported";
590 case ESPIPE: return "invalid seek";
593 case ESRCH: return "no such process";
596 case ESRMNT: return "srmount error";
599 case ESTALE: return "stale remote file handle";
602 case ESUCCESS: return "Error 0";
605 case ETIME: return "timer expired";
608 case ETIMEDOUT: return "connection timed out";
611 case ETOOMANYREFS: return "too many references: can't splice";
614 case ETXTBSY: return "text file or pseudo-device busy";
617 case EUCLEAN: return "structure needs cleaning";
620 case EUNATCH: return "protocol driver not attached";
623 case EUSERS: return "too many users";
626 case EVERSION: return "version mismatch";
628 #if defined(EWOULDBLOCK) && (!defined(EAGAIN) || (EWOULDBLOCK != EAGAIN))
629 case EWOULDBLOCK: return "operation would block";
632 case EXDEV: return "cross-domain link";
635 case EXFULL: return "message tables full";
638 #else /* NO_SYS_ERRLIST */
640 extern char *sys_errlist[];
642 if ((errnum > 0) && (errnum <= sys_nerr))
643 return sys_errlist [errnum];
644 #endif /* NO_SYS_ERRLIST */
646 msg = g_static_private_get (&msg_private);
649 msg = g_new( gchar, 64 );
650 g_static_private_set (&msg_private, msg, g_free);
653 sprintf (msg, "unknown error (%d)", errnum);
658 g_strsignal (gint signum)
660 static GStaticPrivate msg_private = G_STATIC_PRIVATE_INIT;
663 #ifdef HAVE_STRSIGNAL
664 extern char *strsignal (int sig);
665 return strsignal (signum);
670 case SIGHUP: return "Hangup";
673 case SIGINT: return "Interrupt";
676 case SIGQUIT: return "Quit";
679 case SIGILL: return "Illegal instruction";
682 case SIGTRAP: return "Trace/breakpoint trap";
685 case SIGABRT: return "IOT trap/Abort";
688 case SIGBUS: return "Bus error";
691 case SIGFPE: return "Floating point exception";
694 case SIGKILL: return "Killed";
697 case SIGUSR1: return "User defined signal 1";
700 case SIGSEGV: return "Segmentation fault";
703 case SIGUSR2: return "User defined signal 2";
706 case SIGPIPE: return "Broken pipe";
709 case SIGALRM: return "Alarm clock";
712 case SIGTERM: return "Terminated";
715 case SIGSTKFLT: return "Stack fault";
718 case SIGCHLD: return "Child exited";
721 case SIGCONT: return "Continued";
724 case SIGSTOP: return "Stopped (signal)";
727 case SIGTSTP: return "Stopped";
730 case SIGTTIN: return "Stopped (tty input)";
733 case SIGTTOU: return "Stopped (tty output)";
736 case SIGURG: return "Urgent condition";
739 case SIGXCPU: return "CPU time limit exceeded";
742 case SIGXFSZ: return "File size limit exceeded";
745 case SIGVTALRM: return "Virtual time alarm";
748 case SIGPROF: return "Profile signal";
751 case SIGWINCH: return "Window size changed";
754 case SIGIO: return "Possible I/O";
757 case SIGPWR: return "Power failure";
760 case SIGUNUSED: return "Unused signal";
763 #else /* NO_SYS_SIGLIST */
765 #ifndef NO_SYS_SIGLIST_DECL
766 /*(see Tue Jan 19 00:44:24 1999 in changelog)*/
767 extern char *sys_siglist[];
770 return (char*) /* this function should return const --josh */ sys_siglist [signum];
771 #endif /* NO_SYS_SIGLIST */
773 msg = g_static_private_get (&msg_private);
776 msg = g_new( gchar, 64 );
777 g_static_private_set (&msg_private, msg, g_free);
780 sprintf (msg, "unknown signal (%d)", signum);
785 g_printf_string_upper_bound (const gchar* format,
792 gboolean long_int = FALSE;
793 gboolean extra_long = FALSE;
800 gboolean done = FALSE;
802 while (*format && !done)
809 len += va_arg (args, int);
820 /* add specified format length, since it might exceed the
821 * size we assume it to have.
824 len += strtol (format, (char**) &format, 10);
827 /* ignore short int flag, since all args have at least the
828 * same size as an int
833 extra_long = TRUE; /* linux specific */
843 string_arg = va_arg (args, char *);
845 len += strlen (string_arg);
848 /* add enough padding to hold "(null)" identifier */
861 (void) va_arg (args, gint64);
863 #endif /* G_HAVE_GINT64 */
866 (void) va_arg (args, long);
868 (void) va_arg (args, int);
870 len += extra_long ? 64 : 32;
876 (void) va_arg (args, long);
884 #ifdef HAVE_LONG_DOUBLE
886 (void) va_arg (args, long double);
888 #endif /* HAVE_LONG_DOUBLE */
889 (void) va_arg (args, double);
890 len += extra_long ? 64 : 32;
894 (void) va_arg (args, int);
900 (void) va_arg (args, void*);
909 /* ignore unknow/invalid flags */
922 g_strdown (gchar *string)
926 g_return_if_fail (string != NULL);
938 g_strup (gchar *string)
942 g_return_if_fail (string != NULL);
954 g_strreverse (gchar *string)
956 g_return_if_fail (string != NULL);
960 register gchar *h, *t;
963 t = string + strlen (string) - 1;
979 g_strcasecmp (const gchar *s1,
982 #ifdef HAVE_STRCASECMP
983 return strcasecmp (s1, s2);
987 g_return_val_if_fail (s1 != NULL, 0);
988 g_return_val_if_fail (s2 != NULL, 0);
992 /* According to A. Cox, some platforms have islower's that
993 * don't work right on non-uppercase
995 c1 = isupper ((guchar)*s1) ? tolower ((guchar)*s1) : *s1;
996 c2 = isupper ((guchar)*s2) ? tolower ((guchar)*s2) : *s2;
1002 return (((gint)(guchar) *s1) - ((gint)(guchar) *s2));
1007 g_strncasecmp (const gchar *s1,
1011 #ifdef HAVE_STRNCASECMP
1012 return strncasecmp (s1, s2, n);
1016 g_return_val_if_fail (s1 != NULL, 0);
1017 g_return_val_if_fail (s2 != NULL, 0);
1019 while (n-- && *s1 && *s2)
1021 /* According to A. Cox, some platforms have islower's that
1022 * don't work right on non-uppercase
1024 c1 = isupper ((guchar)*s1) ? tolower ((guchar)*s1) : *s1;
1025 c2 = isupper ((guchar)*s2) ? tolower ((guchar)*s2) : *s2;
1032 return (((gint)(guchar) *s1) - ((gint)(guchar) *s2));
1039 g_strdelimit (gchar *string,
1040 const gchar *delimiters,
1045 g_return_val_if_fail (string != NULL, NULL);
1048 delimiters = G_STR_DELIMITERS;
1050 for (c = string; *c; c++)
1052 if (strchr (delimiters, *c))
1060 g_strescape (gchar *string)
1064 guint backslashes = 0;
1067 g_return_val_if_fail (string != NULL, NULL);
1069 while (*p != '\000')
1070 backslashes += (*p++ == '\\');
1073 return g_strdup (string);
1075 escaped = g_new (gchar, strlen (string) + backslashes + 1);
1080 while (*p != '\000')
1091 /* blame Elliot for these next five routines */
1093 g_strchug (gchar *string)
1097 g_return_val_if_fail (string != NULL, NULL);
1099 for (start = string; *start && isspace (*start); start++)
1102 strcpy (string, start);
1108 g_strchomp (gchar *string)
1112 g_return_val_if_fail (string != NULL, NULL);
1117 for (s = string + strlen (string) - 1; s >= string && isspace (*s); s--)
1124 g_strsplit (const gchar *string,
1125 const gchar *delimiter,
1128 GSList *string_list = NULL, *slist;
1129 gchar **str_array, *s;
1132 g_return_val_if_fail (string != NULL, NULL);
1133 g_return_val_if_fail (delimiter != NULL, NULL);
1136 max_tokens = G_MAXINT;
1138 s = strstr (string, delimiter);
1141 guint delimiter_len = strlen (delimiter);
1149 new_string = g_new (gchar, len + 1);
1150 strncpy (new_string, string, len);
1151 new_string[len] = 0;
1152 string_list = g_slist_prepend (string_list, new_string);
1154 string = s + delimiter_len;
1155 s = strstr (string, delimiter);
1157 while (--max_tokens && s);
1162 string_list = g_slist_prepend (string_list, g_strdup (string));
1165 str_array = g_new (gchar*, n);
1169 str_array[i--] = NULL;
1170 for (slist = string_list; slist; slist = slist->next)
1171 str_array[i--] = slist->data;
1173 g_slist_free (string_list);
1179 g_strfreev (gchar **str_array)
1185 for(i = 0; str_array[i] != NULL; i++)
1186 g_free(str_array[i]);
1193 g_strjoinv (const gchar *separator,
1198 g_return_val_if_fail (str_array != NULL, NULL);
1200 if(separator == NULL)
1206 guint separator_len;
1208 separator_len = strlen (separator);
1209 len = 1 + strlen (str_array[0]);
1210 for(i = 1; str_array[i] != NULL; i++)
1211 len += separator_len + strlen(str_array[i]);
1213 string = g_new (gchar, len);
1215 strcat (string, *str_array);
1216 for (i = 1; str_array[i] != NULL; i++)
1218 strcat (string, separator);
1219 strcat (string, str_array[i]);
1223 string = g_strdup ("");
1229 g_strjoin (const gchar *separator,
1235 guint separator_len;
1237 if(separator == NULL)
1240 separator_len = strlen (separator);
1242 va_start(args, separator);
1244 s = va_arg(args, gchar *);
1247 len = strlen(s) + 1;
1249 while((s = va_arg(args, gchar*)))
1251 len += separator_len + strlen(s);
1255 string = g_new (gchar, len);
1257 va_start(args, separator);
1260 s = va_arg(args, gchar*);
1263 while((s = va_arg(args, gchar*)))
1265 strcat(string, separator);
1270 string = g_strdup("");