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.
29 #include <ctype.h> /* For tolower() */
31 /* do not include <unistd.h> in this place since it
32 * inteferes with g_strsignal() on some OSes
36 g_strdup (const gchar *str)
42 new_str = g_new (char, strlen (str) + 1);
43 strcpy (new_str, str);
52 g_memdup (gconstpointer mem,
59 new_mem = g_malloc (byte_size);
60 memcpy (new_mem, mem, byte_size);
69 g_strndup (const gchar *str,
76 new_str = g_new (gchar, n + 1);
77 strncpy (new_str, str, n);
87 g_strnfill (guint length,
90 register gchar *str, *s, *end;
92 str = g_new (gchar, length + 1);
103 g_strdup_vprintf (const gchar *format,
109 G_VA_COPY (args2, args1);
111 buffer = g_new (gchar, g_printf_string_upper_bound (format, args1));
113 vsprintf (buffer, format, args2);
120 g_strdup_printf (const gchar *format,
126 va_start (args, format);
127 buffer = g_strdup_vprintf (format, args);
134 g_strconcat (const gchar *string1, ...)
141 g_return_val_if_fail (string1 != NULL, NULL);
143 l = 1 + strlen (string1);
144 va_start (args, string1);
145 s = va_arg (args, gchar*);
149 s = va_arg (args, gchar*);
153 concat = g_new (gchar, l);
156 strcat (concat, string1);
157 va_start (args, string1);
158 s = va_arg (args, gchar*);
162 s = va_arg (args, gchar*);
170 g_strtod (const gchar *nptr,
178 g_return_val_if_fail (nptr != NULL, 0);
183 val_1 = strtod (nptr, &fail_pos_1);
185 if (fail_pos_1 && fail_pos_1[0] != 0)
189 old_locale = setlocale (LC_NUMERIC, "C");
190 val_2 = strtod (nptr, &fail_pos_2);
191 setlocale (LC_NUMERIC, old_locale);
194 if (!fail_pos_1 || fail_pos_1[0] == 0 || fail_pos_1 >= fail_pos_2)
197 *endptr = fail_pos_1;
203 *endptr = fail_pos_2;
209 g_strerror (gint errnum)
214 return strerror (errnum);
219 case E2BIG: return "argument list too long";
222 case EACCES: return "permission denied";
225 case EADDRINUSE: return "address already in use";
228 case EADDRNOTAVAIL: return "can't assign requested address";
231 case EADV: return "advertise error";
234 case EAFNOSUPPORT: return "address family not supported by protocol family";
237 case EAGAIN: return "try again";
240 case EALIGN: return "EALIGN";
243 case EALREADY: return "operation already in progress";
246 case EBADE: return "bad exchange descriptor";
249 case EBADF: return "bad file number";
252 case EBADFD: return "file descriptor in bad state";
255 case EBADMSG: return "not a data message";
258 case EBADR: return "bad request descriptor";
261 case EBADRPC: return "RPC structure is bad";
264 case EBADRQC: return "bad request code";
267 case EBADSLT: return "invalid slot";
270 case EBFONT: return "bad font file format";
273 case EBUSY: return "mount device busy";
276 case ECHILD: return "no children";
279 case ECHRNG: return "channel number out of range";
282 case ECOMM: return "communication error on send";
285 case ECONNABORTED: return "software caused connection abort";
288 case ECONNREFUSED: return "connection refused";
291 case ECONNRESET: return "connection reset by peer";
293 #if defined(EDEADLK) && (!defined(EWOULDBLOCK) || (EDEADLK != EWOULDBLOCK))
294 case EDEADLK: return "resource deadlock avoided";
297 case EDEADLOCK: return "resource deadlock avoided";
300 case EDESTADDRREQ: return "destination address required";
303 case EDIRTY: return "mounting a dirty fs w/o force";
306 case EDOM: return "math argument out of range";
309 case EDOTDOT: return "cross mount point";
312 case EDQUOT: return "disk quota exceeded";
315 case EDUPPKG: return "duplicate package name";
318 case EEXIST: return "file already exists";
321 case EFAULT: return "bad address in system call argument";
324 case EFBIG: return "file too large";
327 case EHOSTDOWN: return "host is down";
330 case EHOSTUNREACH: return "host is unreachable";
333 case EIDRM: return "identifier removed";
336 case EINIT: return "initialization error";
339 case EINPROGRESS: return "operation now in progress";
342 case EINTR: return "interrupted system call";
345 case EINVAL: return "invalid argument";
348 case EIO: return "I/O error";
351 case EISCONN: return "socket is already connected";
354 case EISDIR: return "illegal operation on a directory";
357 case EISNAM: return "is a name file";
360 case ELBIN: return "ELBIN";
363 case EL2HLT: return "level 2 halted";
366 case EL2NSYNC: return "level 2 not synchronized";
369 case EL3HLT: return "level 3 halted";
372 case EL3RST: return "level 3 reset";
375 case ELIBACC: return "can not access a needed shared library";
378 case ELIBBAD: return "accessing a corrupted shared library";
381 case ELIBEXEC: return "can not exec a shared library directly";
384 case ELIBMAX: return "attempting to link in more shared libraries than system limit";
387 case ELIBSCN: return ".lib section in a.out corrupted";
390 case ELNRNG: return "link number out of range";
393 case ELOOP: return "too many levels of symbolic links";
396 case EMFILE: return "too many open files";
399 case EMLINK: return "too many links";
402 case EMSGSIZE: return "message too long";
405 case EMULTIHOP: return "multihop attempted";
408 case ENAMETOOLONG: return "file name too long";
411 case ENAVAIL: return "not available";
414 case ENET: return "ENET";
417 case ENETDOWN: return "network is down";
420 case ENETRESET: return "network dropped connection on reset";
423 case ENETUNREACH: return "network is unreachable";
426 case ENFILE: return "file table overflow";
429 case ENOANO: return "anode table overflow";
431 #if defined(ENOBUFS) && (!defined(ENOSR) || (ENOBUFS != ENOSR))
432 case ENOBUFS: return "no buffer space available";
435 case ENOCSI: return "no CSI structure available";
438 case ENODATA: return "no data available";
441 case ENODEV: return "no such device";
444 case ENOENT: return "no such file or directory";
447 case ENOEXEC: return "exec format error";
450 case ENOLCK: return "no locks available";
453 case ENOLINK: return "link has be severed";
456 case ENOMEM: return "not enough memory";
459 case ENOMSG: return "no message of desired type";
462 case ENONET: return "machine is not on the network";
465 case ENOPKG: return "package not installed";
468 case ENOPROTOOPT: return "bad proocol option";
471 case ENOSPC: return "no space left on device";
474 case ENOSR: return "out of stream resources";
477 case ENOSTR: return "not a stream device";
480 case ENOSYM: return "unresolved symbol name";
483 case ENOSYS: return "function not implemented";
486 case ENOTBLK: return "block device required";
489 case ENOTCONN: return "socket is not connected";
492 case ENOTDIR: return "not a directory";
495 case ENOTEMPTY: return "directory not empty";
498 case ENOTNAM: return "not a name file";
501 case ENOTSOCK: return "socket operation on non-socket";
504 case ENOTTY: return "inappropriate device for ioctl";
507 case ENOTUNIQ: return "name not unique on network";
510 case ENXIO: return "no such device or address";
513 case EOPNOTSUPP: return "operation not supported on socket";
516 case EPERM: return "not owner";
519 case EPFNOSUPPORT: return "protocol family not supported";
522 case EPIPE: return "broken pipe";
525 case EPROCLIM: return "too many processes";
528 case EPROCUNAVAIL: return "bad procedure for program";
531 case EPROGMISMATCH: return "program version wrong";
534 case EPROGUNAVAIL: return "RPC program not available";
537 case EPROTO: return "protocol error";
539 #ifdef EPROTONOSUPPORT
540 case EPROTONOSUPPORT: return "protocol not suppored";
543 case EPROTOTYPE: return "protocol wrong type for socket";
546 case ERANGE: return "math result unrepresentable";
548 #if defined(EREFUSED) && (!defined(ECONNREFUSED) || (EREFUSED != ECONNREFUSED))
549 case EREFUSED: return "EREFUSED";
552 case EREMCHG: return "remote address changed";
555 case EREMDEV: return "remote device";
558 case EREMOTE: return "pathname hit remote file system";
561 case EREMOTEIO: return "remote i/o error";
563 #ifdef EREMOTERELEASE
564 case EREMOTERELEASE: return "EREMOTERELEASE";
567 case EROFS: return "read-only file system";
570 case ERPCMISMATCH: return "RPC version is wrong";
573 case ERREMOTE: return "object is remote";
576 case ESHUTDOWN: return "can't send afer socket shutdown";
578 #ifdef ESOCKTNOSUPPORT
579 case ESOCKTNOSUPPORT: return "socket type not supported";
582 case ESPIPE: return "invalid seek";
585 case ESRCH: return "no such process";
588 case ESRMNT: return "srmount error";
591 case ESTALE: return "stale remote file handle";
594 case ESUCCESS: return "Error 0";
597 case ETIME: return "timer expired";
600 case ETIMEDOUT: return "connection timed out";
603 case ETOOMANYREFS: return "too many references: can't splice";
606 case ETXTBSY: return "text file or pseudo-device busy";
609 case EUCLEAN: return "structure needs cleaning";
612 case EUNATCH: return "protocol driver not attached";
615 case EUSERS: return "too many users";
618 case EVERSION: return "version mismatch";
620 #if defined(EWOULDBLOCK) && (!defined(EAGAIN) || (EWOULDBLOCK != EAGAIN))
621 case EWOULDBLOCK: return "operation would block";
624 case EXDEV: return "cross-domain link";
627 case EXFULL: return "message tables full";
630 #else /* NO_SYS_ERRLIST */
632 extern char *sys_errlist[];
634 if ((errnum > 0) && (errnum <= sys_nerr))
635 return sys_errlist [errnum];
636 #endif /* NO_SYS_ERRLIST */
638 sprintf (msg, "unknown error (%d)", errnum);
643 g_strsignal (gint signum)
647 #ifdef HAVE_STRSIGNAL
648 extern char *strsignal (int sig);
649 return strsignal (signum);
654 case SIGHUP: return "Hangup";
657 case SIGINT: return "Interrupt";
660 case SIGQUIT: return "Quit";
663 case SIGILL: return "Illegal instruction";
666 case SIGTRAP: return "Trace/breakpoint trap";
669 case SIGABRT: return "IOT trap/Abort";
672 case SIGBUS: return "Bus error";
675 case SIGFPE: return "Floating point exception";
678 case SIGKILL: return "Killed";
681 case SIGUSR1: return "User defined signal 1";
684 case SIGSEGV: return "Segmentation fault";
687 case SIGUSR2: return "User defined signal 2";
690 case SIGPIPE: return "Broken pipe";
693 case SIGALRM: return "Alarm clock";
696 case SIGTERM: return "Terminated";
699 case SIGSTKFLT: return "Stack fault";
702 case SIGCHLD: return "Child exited";
705 case SIGCONT: return "Continued";
708 case SIGSTOP: return "Stopped (signal)";
711 case SIGTSTP: return "Stopped";
714 case SIGTTIN: return "Stopped (tty input)";
717 case SIGTTOU: return "Stopped (tty output)";
720 case SIGURG: return "Urgent condition";
723 case SIGXCPU: return "CPU time limit exceeded";
726 case SIGXFSZ: return "File size limit exceeded";
729 case SIGVTALRM: return "Virtual time alarm";
732 case SIGPROF: return "Profile signal";
735 case SIGWINCH: return "Window size changed";
738 case SIGIO: return "Possible I/O";
741 case SIGPWR: return "Power failure";
744 case SIGUNUSED: return "Unused signal";
747 #else /* NO_SYS_SIGLIST */
748 extern char *sys_siglist[];
749 return sys_siglist [signum];
750 #endif /* NO_SYS_SIGLIST */
752 sprintf (msg, "unknown signal (%d)", signum);
757 g_printf_string_upper_bound (const gchar* format,
764 gboolean long_int = FALSE;
765 gboolean extra_long = FALSE;
772 gboolean done = FALSE;
774 while (*format && !done)
781 len += va_arg (args, int);
792 /* add specified format length, since it might exceed the
793 * size we assume it to have.
796 len += strtol (format, (char**) &format, 10);
799 /* ignore short int flag, since all args have at least the
800 * same size as an int
805 extra_long = TRUE; /* linux specific */
815 string_arg = va_arg (args, char *);
817 len += strlen (string_arg);
820 /* add enough padding to hold "(null)" identifier */
833 (void) va_arg (args, gint64);
835 #endif /* G_HAVE_GINT64 */
838 (void) va_arg (args, long);
840 (void) va_arg (args, int);
842 len += extra_long ? 64 : 32;
848 (void) va_arg (args, long);
856 #ifdef HAVE_LONG_DOUBLE
858 (void) va_arg (args, long double);
860 #endif /* HAVE_LONG_DOUBLE */
861 (void) va_arg (args, double);
862 len += extra_long ? 64 : 32;
866 (void) va_arg (args, int);
872 (void) va_arg (args, void*);
881 /* ignore unknow/invalid flags */
894 g_strdown (gchar *string)
898 g_return_if_fail (string != NULL);
910 g_strup (gchar *string)
914 g_return_if_fail (string != NULL);
926 g_strreverse (gchar *string)
928 g_return_if_fail (string != NULL);
932 register gchar *h, *t;
935 t = string + strlen (string) - 1;
951 g_strcasecmp (const gchar *s1,
954 #ifdef HAVE_STRCASECMP
955 return strcasecmp (s1, s2);
959 g_return_val_if_fail (s1 != NULL, 0);
960 g_return_val_if_fail (s2 != NULL, 0);
964 /* According to A. Cox, some platforms have islower's that
965 * don't work right on non-uppercase
967 c1 = isupper ((guchar)*s1) ? tolower ((guchar)*s1) : *s1;
968 c2 = isupper ((guchar)*s2) ? tolower ((guchar)*s2) : *s2;
974 return (((gint)(guchar) *s1) - ((gint)(guchar) *s2));
979 g_strncasecmp (const gchar *s1,
983 #ifdef HAVE_STRNCASECMP
984 return strncasecmp (s1, s2, n);
988 g_return_val_if_fail (s1 != NULL, 0);
989 g_return_val_if_fail (s2 != NULL, 0);
991 while (n-- && *s1 && *s2)
993 /* According to A. Cox, some platforms have islower's that
994 * don't work right on non-uppercase
996 c1 = isupper ((guchar)*s1) ? tolower ((guchar)*s1) : *s1;
997 c2 = isupper ((guchar)*s2) ? tolower ((guchar)*s2) : *s2;
1004 return (((gint)(guchar) *s1) - ((gint)(guchar) *s2));
1011 g_strdelimit (gchar *string,
1012 const gchar *delimiters,
1017 g_return_val_if_fail (string != NULL, NULL);
1020 delimiters = G_STR_DELIMITERS;
1022 for (c = string; *c; c++)
1024 if (strchr (delimiters, *c))
1032 g_strescape (gchar *string)
1036 guint backslashes = 0;
1039 g_return_val_if_fail (string != NULL, NULL);
1041 while (*p != '\000')
1042 backslashes += (*p++ == '\\');
1045 return g_strdup (string);
1047 escaped = g_new (gchar, strlen (string) + backslashes + 1);
1052 while (*p != '\000')
1063 /* blame Elliot for these next five routines */
1065 g_strchug (gchar *string)
1069 g_return_val_if_fail (string != NULL, NULL);
1071 for (start = string; *start && isspace (*start); start++)
1074 strcpy (string, start);
1080 g_strchomp (gchar *string)
1084 g_return_val_if_fail (string != NULL, NULL);
1089 for (s = string + strlen (string) - 1; s >= string && isspace (*s); s--)
1096 g_strsplit (const gchar *string,
1097 const gchar *delimiter,
1100 GSList *string_list = NULL, *slist;
1101 gchar **str_array, *s;
1104 g_return_val_if_fail (string != NULL, NULL);
1105 g_return_val_if_fail (delimiter != NULL, NULL);
1108 max_tokens = G_MAXINT;
1110 s = strstr (string, delimiter);
1113 guint delimiter_len = strlen (delimiter);
1121 new_string = g_new (gchar, len + 1);
1122 strncpy (new_string, string, len);
1123 new_string[len] = 0;
1124 string_list = g_slist_prepend (string_list, new_string);
1126 string = s + delimiter_len;
1127 s = strstr (string, delimiter);
1129 while (--max_tokens && s);
1134 string_list = g_slist_prepend (string_list, g_strdup (string));
1137 str_array = g_new (gchar*, n);
1141 str_array[i--] = NULL;
1142 for (slist = string_list; slist; slist = slist->next)
1143 str_array[i--] = slist->data;
1145 g_slist_free (string_list);
1151 g_strfreev (gchar **str_array)
1157 for(i = 0; str_array[i] != NULL; i++)
1158 g_free(str_array[i]);
1165 g_strjoinv (const gchar *separator,
1170 g_return_val_if_fail (str_array != NULL, NULL);
1172 if(separator == NULL)
1178 guint separator_len;
1180 separator_len = strlen (separator);
1181 len = 1 + strlen (str_array[0]);
1182 for(i = 1; str_array[i] != NULL; i++)
1183 len += separator_len + strlen(str_array[i]);
1185 string = g_new (gchar, len);
1187 strcat (string, *str_array);
1188 for (i = 1; str_array[i] != NULL; i++)
1190 strcat (string, separator);
1191 strcat (string, str_array[i]);
1195 string = g_strdup ("");
1201 g_strjoin (const gchar *separator,
1207 guint separator_len;
1209 if(separator == NULL)
1212 separator_len = strlen (separator);
1214 va_start(args, separator);
1216 s = va_arg(args, gchar *);
1219 len = strlen(s) + 1;
1221 while((s = va_arg(args, gchar*)))
1223 len += separator_len + strlen(s);
1227 string = g_new (gchar, len);
1229 va_start(args, separator);
1232 s = va_arg(args, gchar*);
1235 while((s = va_arg(args, gchar*)))
1237 strcat(string, separator);
1242 string = g_strdup("");