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() */
35 /* do not include <unistd.h> in this place since it
36 * inteferes with g_strsignal() on some OSes
40 g_strdup (const gchar *str)
46 new_str = g_new (char, strlen (str) + 1);
47 strcpy (new_str, str);
56 g_memdup (gconstpointer mem,
63 new_mem = g_malloc (byte_size);
64 memcpy (new_mem, mem, byte_size);
73 g_strndup (const gchar *str,
80 new_str = g_new (gchar, n + 1);
81 strncpy (new_str, str, n);
91 g_strnfill (guint length,
94 register gchar *str, *s, *end;
96 str = g_new (gchar, length + 1);
107 g_strdup_vprintf (const gchar *format,
113 G_VA_COPY (args2, args1);
115 buffer = g_new (gchar, g_printf_string_upper_bound (format, args1));
117 vsprintf (buffer, format, args2);
124 g_strdup_printf (const gchar *format,
130 va_start (args, format);
131 buffer = g_strdup_vprintf (format, args);
138 g_strconcat (const gchar *string1, ...)
145 g_return_val_if_fail (string1 != NULL, NULL);
147 l = 1 + strlen (string1);
148 va_start (args, string1);
149 s = va_arg (args, gchar*);
153 s = va_arg (args, gchar*);
157 concat = g_new (gchar, l);
160 strcat (concat, string1);
161 va_start (args, string1);
162 s = va_arg (args, gchar*);
166 s = va_arg (args, gchar*);
174 g_strtod (const gchar *nptr,
182 g_return_val_if_fail (nptr != NULL, 0);
187 val_1 = strtod (nptr, &fail_pos_1);
189 if (fail_pos_1 && fail_pos_1[0] != 0)
193 old_locale = setlocale (LC_NUMERIC, "C");
194 val_2 = strtod (nptr, &fail_pos_2);
195 setlocale (LC_NUMERIC, old_locale);
198 if (!fail_pos_1 || fail_pos_1[0] == 0 || fail_pos_1 >= fail_pos_2)
201 *endptr = fail_pos_1;
207 *endptr = fail_pos_2;
213 g_strerror (gint errnum)
215 static GStaticPrivate msg_private = G_STATIC_PRIVATE_INIT;
219 return strerror (errnum);
224 case E2BIG: return "argument list too long";
227 case EACCES: return "permission denied";
230 case EADDRINUSE: return "address already in use";
233 case EADDRNOTAVAIL: return "can't assign requested address";
236 case EADV: return "advertise error";
239 case EAFNOSUPPORT: return "address family not supported by protocol family";
242 case EAGAIN: return "try again";
245 case EALIGN: return "EALIGN";
248 case EALREADY: return "operation already in progress";
251 case EBADE: return "bad exchange descriptor";
254 case EBADF: return "bad file number";
257 case EBADFD: return "file descriptor in bad state";
260 case EBADMSG: return "not a data message";
263 case EBADR: return "bad request descriptor";
266 case EBADRPC: return "RPC structure is bad";
269 case EBADRQC: return "bad request code";
272 case EBADSLT: return "invalid slot";
275 case EBFONT: return "bad font file format";
278 case EBUSY: return "mount device busy";
281 case ECHILD: return "no children";
284 case ECHRNG: return "channel number out of range";
287 case ECOMM: return "communication error on send";
290 case ECONNABORTED: return "software caused connection abort";
293 case ECONNREFUSED: return "connection refused";
296 case ECONNRESET: return "connection reset by peer";
298 #if defined(EDEADLK) && (!defined(EWOULDBLOCK) || (EDEADLK != EWOULDBLOCK))
299 case EDEADLK: return "resource deadlock avoided";
302 case EDEADLOCK: return "resource deadlock avoided";
305 case EDESTADDRREQ: return "destination address required";
308 case EDIRTY: return "mounting a dirty fs w/o force";
311 case EDOM: return "math argument out of range";
314 case EDOTDOT: return "cross mount point";
317 case EDQUOT: return "disk quota exceeded";
320 case EDUPPKG: return "duplicate package name";
323 case EEXIST: return "file already exists";
326 case EFAULT: return "bad address in system call argument";
329 case EFBIG: return "file too large";
332 case EHOSTDOWN: return "host is down";
335 case EHOSTUNREACH: return "host is unreachable";
338 case EIDRM: return "identifier removed";
341 case EINIT: return "initialization error";
344 case EINPROGRESS: return "operation now in progress";
347 case EINTR: return "interrupted system call";
350 case EINVAL: return "invalid argument";
353 case EIO: return "I/O error";
356 case EISCONN: return "socket is already connected";
359 case EISDIR: return "illegal operation on a directory";
362 case EISNAM: return "is a name file";
365 case ELBIN: return "ELBIN";
368 case EL2HLT: return "level 2 halted";
371 case EL2NSYNC: return "level 2 not synchronized";
374 case EL3HLT: return "level 3 halted";
377 case EL3RST: return "level 3 reset";
380 case ELIBACC: return "can not access a needed shared library";
383 case ELIBBAD: return "accessing a corrupted shared library";
386 case ELIBEXEC: return "can not exec a shared library directly";
389 case ELIBMAX: return "attempting to link in more shared libraries than system limit";
392 case ELIBSCN: return ".lib section in a.out corrupted";
395 case ELNRNG: return "link number out of range";
398 case ELOOP: return "too many levels of symbolic links";
401 case EMFILE: return "too many open files";
404 case EMLINK: return "too many links";
407 case EMSGSIZE: return "message too long";
410 case EMULTIHOP: return "multihop attempted";
413 case ENAMETOOLONG: return "file name too long";
416 case ENAVAIL: return "not available";
419 case ENET: return "ENET";
422 case ENETDOWN: return "network is down";
425 case ENETRESET: return "network dropped connection on reset";
428 case ENETUNREACH: return "network is unreachable";
431 case ENFILE: return "file table overflow";
434 case ENOANO: return "anode table overflow";
436 #if defined(ENOBUFS) && (!defined(ENOSR) || (ENOBUFS != ENOSR))
437 case ENOBUFS: return "no buffer space available";
440 case ENOCSI: return "no CSI structure available";
443 case ENODATA: return "no data available";
446 case ENODEV: return "no such device";
449 case ENOENT: return "no such file or directory";
452 case ENOEXEC: return "exec format error";
455 case ENOLCK: return "no locks available";
458 case ENOLINK: return "link has be severed";
461 case ENOMEM: return "not enough memory";
464 case ENOMSG: return "no message of desired type";
467 case ENONET: return "machine is not on the network";
470 case ENOPKG: return "package not installed";
473 case ENOPROTOOPT: return "bad proocol option";
476 case ENOSPC: return "no space left on device";
479 case ENOSR: return "out of stream resources";
482 case ENOSTR: return "not a stream device";
485 case ENOSYM: return "unresolved symbol name";
488 case ENOSYS: return "function not implemented";
491 case ENOTBLK: return "block device required";
494 case ENOTCONN: return "socket is not connected";
497 case ENOTDIR: return "not a directory";
500 case ENOTEMPTY: return "directory not empty";
503 case ENOTNAM: return "not a name file";
506 case ENOTSOCK: return "socket operation on non-socket";
509 case ENOTTY: return "inappropriate device for ioctl";
512 case ENOTUNIQ: return "name not unique on network";
515 case ENXIO: return "no such device or address";
518 case EOPNOTSUPP: return "operation not supported on socket";
521 case EPERM: return "not owner";
524 case EPFNOSUPPORT: return "protocol family not supported";
527 case EPIPE: return "broken pipe";
530 case EPROCLIM: return "too many processes";
533 case EPROCUNAVAIL: return "bad procedure for program";
536 case EPROGMISMATCH: return "program version wrong";
539 case EPROGUNAVAIL: return "RPC program not available";
542 case EPROTO: return "protocol error";
544 #ifdef EPROTONOSUPPORT
545 case EPROTONOSUPPORT: return "protocol not suppored";
548 case EPROTOTYPE: return "protocol wrong type for socket";
551 case ERANGE: return "math result unrepresentable";
553 #if defined(EREFUSED) && (!defined(ECONNREFUSED) || (EREFUSED != ECONNREFUSED))
554 case EREFUSED: return "EREFUSED";
557 case EREMCHG: return "remote address changed";
560 case EREMDEV: return "remote device";
563 case EREMOTE: return "pathname hit remote file system";
566 case EREMOTEIO: return "remote i/o error";
568 #ifdef EREMOTERELEASE
569 case EREMOTERELEASE: return "EREMOTERELEASE";
572 case EROFS: return "read-only file system";
575 case ERPCMISMATCH: return "RPC version is wrong";
578 case ERREMOTE: return "object is remote";
581 case ESHUTDOWN: return "can't send afer socket shutdown";
583 #ifdef ESOCKTNOSUPPORT
584 case ESOCKTNOSUPPORT: return "socket type not supported";
587 case ESPIPE: return "invalid seek";
590 case ESRCH: return "no such process";
593 case ESRMNT: return "srmount error";
596 case ESTALE: return "stale remote file handle";
599 case ESUCCESS: return "Error 0";
602 case ETIME: return "timer expired";
605 case ETIMEDOUT: return "connection timed out";
608 case ETOOMANYREFS: return "too many references: can't splice";
611 case ETXTBSY: return "text file or pseudo-device busy";
614 case EUCLEAN: return "structure needs cleaning";
617 case EUNATCH: return "protocol driver not attached";
620 case EUSERS: return "too many users";
623 case EVERSION: return "version mismatch";
625 #if defined(EWOULDBLOCK) && (!defined(EAGAIN) || (EWOULDBLOCK != EAGAIN))
626 case EWOULDBLOCK: return "operation would block";
629 case EXDEV: return "cross-domain link";
632 case EXFULL: return "message tables full";
635 #else /* NO_SYS_ERRLIST */
637 extern char *sys_errlist[];
639 if ((errnum > 0) && (errnum <= sys_nerr))
640 return sys_errlist [errnum];
641 #endif /* NO_SYS_ERRLIST */
643 msg = g_static_private_get (&msg_private);
646 msg = g_new( gchar, 64 );
647 g_static_private_set (&msg_private, msg, g_free);
650 sprintf (msg, "unknown error (%d)", errnum);
655 g_strsignal (gint signum)
657 static GStaticPrivate msg_private = G_STATIC_PRIVATE_INIT;
660 #ifdef HAVE_STRSIGNAL
661 extern char *strsignal (int sig);
662 return strsignal (signum);
667 case SIGHUP: return "Hangup";
670 case SIGINT: return "Interrupt";
673 case SIGQUIT: return "Quit";
676 case SIGILL: return "Illegal instruction";
679 case SIGTRAP: return "Trace/breakpoint trap";
682 case SIGABRT: return "IOT trap/Abort";
685 case SIGBUS: return "Bus error";
688 case SIGFPE: return "Floating point exception";
691 case SIGKILL: return "Killed";
694 case SIGUSR1: return "User defined signal 1";
697 case SIGSEGV: return "Segmentation fault";
700 case SIGUSR2: return "User defined signal 2";
703 case SIGPIPE: return "Broken pipe";
706 case SIGALRM: return "Alarm clock";
709 case SIGTERM: return "Terminated";
712 case SIGSTKFLT: return "Stack fault";
715 case SIGCHLD: return "Child exited";
718 case SIGCONT: return "Continued";
721 case SIGSTOP: return "Stopped (signal)";
724 case SIGTSTP: return "Stopped";
727 case SIGTTIN: return "Stopped (tty input)";
730 case SIGTTOU: return "Stopped (tty output)";
733 case SIGURG: return "Urgent condition";
736 case SIGXCPU: return "CPU time limit exceeded";
739 case SIGXFSZ: return "File size limit exceeded";
742 case SIGVTALRM: return "Virtual time alarm";
745 case SIGPROF: return "Profile signal";
748 case SIGWINCH: return "Window size changed";
751 case SIGIO: return "Possible I/O";
754 case SIGPWR: return "Power failure";
757 case SIGUNUSED: return "Unused signal";
760 #else /* NO_SYS_SIGLIST */
761 extern char *sys_siglist[];
762 return sys_siglist [signum];
763 #endif /* NO_SYS_SIGLIST */
765 msg = g_static_private_get (&msg_private);
768 msg = g_new( gchar, 64 );
769 g_static_private_set (&msg_private, msg, g_free);
772 sprintf (msg, "unknown signal (%d)", signum);
777 g_printf_string_upper_bound (const gchar* format,
784 gboolean long_int = FALSE;
785 gboolean extra_long = FALSE;
792 gboolean done = FALSE;
794 while (*format && !done)
801 len += va_arg (args, int);
812 /* add specified format length, since it might exceed the
813 * size we assume it to have.
816 len += strtol (format, (char**) &format, 10);
819 /* ignore short int flag, since all args have at least the
820 * same size as an int
825 extra_long = TRUE; /* linux specific */
835 string_arg = va_arg (args, char *);
837 len += strlen (string_arg);
840 /* add enough padding to hold "(null)" identifier */
853 (void) va_arg (args, gint64);
855 #endif /* G_HAVE_GINT64 */
858 (void) va_arg (args, long);
860 (void) va_arg (args, int);
862 len += extra_long ? 64 : 32;
868 (void) va_arg (args, long);
876 #ifdef HAVE_LONG_DOUBLE
878 (void) va_arg (args, long double);
880 #endif /* HAVE_LONG_DOUBLE */
881 (void) va_arg (args, double);
882 len += extra_long ? 64 : 32;
886 (void) va_arg (args, int);
892 (void) va_arg (args, void*);
901 /* ignore unknow/invalid flags */
914 g_strdown (gchar *string)
918 g_return_if_fail (string != NULL);
930 g_strup (gchar *string)
934 g_return_if_fail (string != NULL);
946 g_strreverse (gchar *string)
948 g_return_if_fail (string != NULL);
952 register gchar *h, *t;
955 t = string + strlen (string) - 1;
971 g_strcasecmp (const gchar *s1,
974 #ifdef HAVE_STRCASECMP
975 return strcasecmp (s1, s2);
979 g_return_val_if_fail (s1 != NULL, 0);
980 g_return_val_if_fail (s2 != NULL, 0);
984 /* According to A. Cox, some platforms have islower's that
985 * don't work right on non-uppercase
987 c1 = isupper ((guchar)*s1) ? tolower ((guchar)*s1) : *s1;
988 c2 = isupper ((guchar)*s2) ? tolower ((guchar)*s2) : *s2;
994 return (((gint)(guchar) *s1) - ((gint)(guchar) *s2));
999 g_strncasecmp (const gchar *s1,
1003 #ifdef HAVE_STRNCASECMP
1004 return strncasecmp (s1, s2, n);
1008 g_return_val_if_fail (s1 != NULL, 0);
1009 g_return_val_if_fail (s2 != NULL, 0);
1011 while (n-- && *s1 && *s2)
1013 /* According to A. Cox, some platforms have islower's that
1014 * don't work right on non-uppercase
1016 c1 = isupper ((guchar)*s1) ? tolower ((guchar)*s1) : *s1;
1017 c2 = isupper ((guchar)*s2) ? tolower ((guchar)*s2) : *s2;
1024 return (((gint)(guchar) *s1) - ((gint)(guchar) *s2));
1031 g_strdelimit (gchar *string,
1032 const gchar *delimiters,
1037 g_return_val_if_fail (string != NULL, NULL);
1040 delimiters = G_STR_DELIMITERS;
1042 for (c = string; *c; c++)
1044 if (strchr (delimiters, *c))
1052 g_strescape (gchar *string)
1056 guint backslashes = 0;
1059 g_return_val_if_fail (string != NULL, NULL);
1061 while (*p != '\000')
1062 backslashes += (*p++ == '\\');
1065 return g_strdup (string);
1067 escaped = g_new (gchar, strlen (string) + backslashes + 1);
1072 while (*p != '\000')
1083 /* blame Elliot for these next five routines */
1085 g_strchug (gchar *string)
1089 g_return_val_if_fail (string != NULL, NULL);
1091 for (start = string; *start && isspace (*start); start++)
1094 strcpy (string, start);
1100 g_strchomp (gchar *string)
1104 g_return_val_if_fail (string != NULL, NULL);
1109 for (s = string + strlen (string) - 1; s >= string && isspace (*s); s--)
1116 g_strsplit (const gchar *string,
1117 const gchar *delimiter,
1120 GSList *string_list = NULL, *slist;
1121 gchar **str_array, *s;
1124 g_return_val_if_fail (string != NULL, NULL);
1125 g_return_val_if_fail (delimiter != NULL, NULL);
1128 max_tokens = G_MAXINT;
1130 s = strstr (string, delimiter);
1133 guint delimiter_len = strlen (delimiter);
1141 new_string = g_new (gchar, len + 1);
1142 strncpy (new_string, string, len);
1143 new_string[len] = 0;
1144 string_list = g_slist_prepend (string_list, new_string);
1146 string = s + delimiter_len;
1147 s = strstr (string, delimiter);
1149 while (--max_tokens && s);
1154 string_list = g_slist_prepend (string_list, g_strdup (string));
1157 str_array = g_new (gchar*, n);
1161 str_array[i--] = NULL;
1162 for (slist = string_list; slist; slist = slist->next)
1163 str_array[i--] = slist->data;
1165 g_slist_free (string_list);
1171 g_strfreev (gchar **str_array)
1177 for(i = 0; str_array[i] != NULL; i++)
1178 g_free(str_array[i]);
1185 g_strjoinv (const gchar *separator,
1190 g_return_val_if_fail (str_array != NULL, NULL);
1192 if(separator == NULL)
1198 guint separator_len;
1200 separator_len = strlen (separator);
1201 len = 1 + strlen (str_array[0]);
1202 for(i = 1; str_array[i] != NULL; i++)
1203 len += separator_len + strlen(str_array[i]);
1205 string = g_new (gchar, len);
1207 strcat (string, *str_array);
1208 for (i = 1; str_array[i] != NULL; i++)
1210 strcat (string, separator);
1211 strcat (string, str_array[i]);
1215 string = g_strdup ("");
1221 g_strjoin (const gchar *separator,
1227 guint separator_len;
1229 if(separator == NULL)
1232 separator_len = strlen (separator);
1234 va_start(args, separator);
1236 s = va_arg(args, gchar *);
1239 len = strlen(s) + 1;
1241 while((s = va_arg(args, gchar*)))
1243 len += separator_len + strlen(s);
1247 string = g_new (gchar, len);
1249 va_start(args, separator);
1252 s = va_arg(args, gchar*);
1255 while((s = va_arg(args, gchar*)))
1257 strcat(string, separator);
1262 string = g_strdup("");