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,
112 #if (HAVE_VASPRINTF) && !(ENABLE_MEM_CHECK)
113 /* if memory checking is disabled, that means we can call g_free() on
114 * memory obtained via malloc(). This allows us to use the
115 * glibc vasprintf() call where available.
117 vasprintf (&buffer, format, args1);
122 G_VA_COPY (args2, args1);
124 buffer = g_new (gchar, g_printf_string_upper_bound (format, args1));
126 vsprintf (buffer, format, args2);
129 #endif /* (HAVE_VASPRINTF) && !(ENABLE_MEM_CHECK) */
135 g_strdup_printf (const gchar *format,
141 va_start (args, format);
142 buffer = g_strdup_vprintf (format, args);
149 g_strconcat (const gchar *string1, ...)
156 g_return_val_if_fail (string1 != NULL, NULL);
158 l = 1 + strlen (string1);
159 va_start (args, string1);
160 s = va_arg (args, gchar*);
164 s = va_arg (args, gchar*);
168 concat = g_new (gchar, l);
171 strcat (concat, string1);
172 va_start (args, string1);
173 s = va_arg (args, gchar*);
177 s = va_arg (args, gchar*);
185 g_strtod (const gchar *nptr,
193 g_return_val_if_fail (nptr != NULL, 0);
198 val_1 = strtod (nptr, &fail_pos_1);
200 if (fail_pos_1 && fail_pos_1[0] != 0)
204 old_locale = setlocale (LC_NUMERIC, "C");
205 val_2 = strtod (nptr, &fail_pos_2);
206 setlocale (LC_NUMERIC, old_locale);
209 if (!fail_pos_1 || fail_pos_1[0] == 0 || fail_pos_1 >= fail_pos_2)
212 *endptr = fail_pos_1;
218 *endptr = fail_pos_2;
224 g_strerror (gint errnum)
226 static GStaticPrivate msg_private = G_STATIC_PRIVATE_INIT;
230 return strerror (errnum);
235 case E2BIG: return "argument list too long";
238 case EACCES: return "permission denied";
241 case EADDRINUSE: return "address already in use";
244 case EADDRNOTAVAIL: return "can't assign requested address";
247 case EADV: return "advertise error";
250 case EAFNOSUPPORT: return "address family not supported by protocol family";
253 case EAGAIN: return "try again";
256 case EALIGN: return "EALIGN";
259 case EALREADY: return "operation already in progress";
262 case EBADE: return "bad exchange descriptor";
265 case EBADF: return "bad file number";
268 case EBADFD: return "file descriptor in bad state";
271 case EBADMSG: return "not a data message";
274 case EBADR: return "bad request descriptor";
277 case EBADRPC: return "RPC structure is bad";
280 case EBADRQC: return "bad request code";
283 case EBADSLT: return "invalid slot";
286 case EBFONT: return "bad font file format";
289 case EBUSY: return "mount device busy";
292 case ECHILD: return "no children";
295 case ECHRNG: return "channel number out of range";
298 case ECOMM: return "communication error on send";
301 case ECONNABORTED: return "software caused connection abort";
304 case ECONNREFUSED: return "connection refused";
307 case ECONNRESET: return "connection reset by peer";
309 #if defined(EDEADLK) && (!defined(EWOULDBLOCK) || (EDEADLK != EWOULDBLOCK))
310 case EDEADLK: return "resource deadlock avoided";
313 case EDEADLOCK: return "resource deadlock avoided";
316 case EDESTADDRREQ: return "destination address required";
319 case EDIRTY: return "mounting a dirty fs w/o force";
322 case EDOM: return "math argument out of range";
325 case EDOTDOT: return "cross mount point";
328 case EDQUOT: return "disk quota exceeded";
331 case EDUPPKG: return "duplicate package name";
334 case EEXIST: return "file already exists";
337 case EFAULT: return "bad address in system call argument";
340 case EFBIG: return "file too large";
343 case EHOSTDOWN: return "host is down";
346 case EHOSTUNREACH: return "host is unreachable";
349 case EIDRM: return "identifier removed";
352 case EINIT: return "initialization error";
355 case EINPROGRESS: return "operation now in progress";
358 case EINTR: return "interrupted system call";
361 case EINVAL: return "invalid argument";
364 case EIO: return "I/O error";
367 case EISCONN: return "socket is already connected";
370 case EISDIR: return "illegal operation on a directory";
373 case EISNAM: return "is a name file";
376 case ELBIN: return "ELBIN";
379 case EL2HLT: return "level 2 halted";
382 case EL2NSYNC: return "level 2 not synchronized";
385 case EL3HLT: return "level 3 halted";
388 case EL3RST: return "level 3 reset";
391 case ELIBACC: return "can not access a needed shared library";
394 case ELIBBAD: return "accessing a corrupted shared library";
397 case ELIBEXEC: return "can not exec a shared library directly";
400 case ELIBMAX: return "attempting to link in more shared libraries than system limit";
403 case ELIBSCN: return ".lib section in a.out corrupted";
406 case ELNRNG: return "link number out of range";
409 case ELOOP: return "too many levels of symbolic links";
412 case EMFILE: return "too many open files";
415 case EMLINK: return "too many links";
418 case EMSGSIZE: return "message too long";
421 case EMULTIHOP: return "multihop attempted";
424 case ENAMETOOLONG: return "file name too long";
427 case ENAVAIL: return "not available";
430 case ENET: return "ENET";
433 case ENETDOWN: return "network is down";
436 case ENETRESET: return "network dropped connection on reset";
439 case ENETUNREACH: return "network is unreachable";
442 case ENFILE: return "file table overflow";
445 case ENOANO: return "anode table overflow";
447 #if defined(ENOBUFS) && (!defined(ENOSR) || (ENOBUFS != ENOSR))
448 case ENOBUFS: return "no buffer space available";
451 case ENOCSI: return "no CSI structure available";
454 case ENODATA: return "no data available";
457 case ENODEV: return "no such device";
460 case ENOENT: return "no such file or directory";
463 case ENOEXEC: return "exec format error";
466 case ENOLCK: return "no locks available";
469 case ENOLINK: return "link has be severed";
472 case ENOMEM: return "not enough memory";
475 case ENOMSG: return "no message of desired type";
478 case ENONET: return "machine is not on the network";
481 case ENOPKG: return "package not installed";
484 case ENOPROTOOPT: return "bad proocol option";
487 case ENOSPC: return "no space left on device";
490 case ENOSR: return "out of stream resources";
493 case ENOSTR: return "not a stream device";
496 case ENOSYM: return "unresolved symbol name";
499 case ENOSYS: return "function not implemented";
502 case ENOTBLK: return "block device required";
505 case ENOTCONN: return "socket is not connected";
508 case ENOTDIR: return "not a directory";
511 case ENOTEMPTY: return "directory not empty";
514 case ENOTNAM: return "not a name file";
517 case ENOTSOCK: return "socket operation on non-socket";
520 case ENOTTY: return "inappropriate device for ioctl";
523 case ENOTUNIQ: return "name not unique on network";
526 case ENXIO: return "no such device or address";
529 case EOPNOTSUPP: return "operation not supported on socket";
532 case EPERM: return "not owner";
535 case EPFNOSUPPORT: return "protocol family not supported";
538 case EPIPE: return "broken pipe";
541 case EPROCLIM: return "too many processes";
544 case EPROCUNAVAIL: return "bad procedure for program";
547 case EPROGMISMATCH: return "program version wrong";
550 case EPROGUNAVAIL: return "RPC program not available";
553 case EPROTO: return "protocol error";
555 #ifdef EPROTONOSUPPORT
556 case EPROTONOSUPPORT: return "protocol not suppored";
559 case EPROTOTYPE: return "protocol wrong type for socket";
562 case ERANGE: return "math result unrepresentable";
564 #if defined(EREFUSED) && (!defined(ECONNREFUSED) || (EREFUSED != ECONNREFUSED))
565 case EREFUSED: return "EREFUSED";
568 case EREMCHG: return "remote address changed";
571 case EREMDEV: return "remote device";
574 case EREMOTE: return "pathname hit remote file system";
577 case EREMOTEIO: return "remote i/o error";
579 #ifdef EREMOTERELEASE
580 case EREMOTERELEASE: return "EREMOTERELEASE";
583 case EROFS: return "read-only file system";
586 case ERPCMISMATCH: return "RPC version is wrong";
589 case ERREMOTE: return "object is remote";
592 case ESHUTDOWN: return "can't send afer socket shutdown";
594 #ifdef ESOCKTNOSUPPORT
595 case ESOCKTNOSUPPORT: return "socket type not supported";
598 case ESPIPE: return "invalid seek";
601 case ESRCH: return "no such process";
604 case ESRMNT: return "srmount error";
607 case ESTALE: return "stale remote file handle";
610 case ESUCCESS: return "Error 0";
613 case ETIME: return "timer expired";
616 case ETIMEDOUT: return "connection timed out";
619 case ETOOMANYREFS: return "too many references: can't splice";
622 case ETXTBSY: return "text file or pseudo-device busy";
625 case EUCLEAN: return "structure needs cleaning";
628 case EUNATCH: return "protocol driver not attached";
631 case EUSERS: return "too many users";
634 case EVERSION: return "version mismatch";
636 #if defined(EWOULDBLOCK) && (!defined(EAGAIN) || (EWOULDBLOCK != EAGAIN))
637 case EWOULDBLOCK: return "operation would block";
640 case EXDEV: return "cross-domain link";
643 case EXFULL: return "message tables full";
646 #else /* NO_SYS_ERRLIST */
648 extern char *sys_errlist[];
650 if ((errnum > 0) && (errnum <= sys_nerr))
651 return sys_errlist [errnum];
652 #endif /* NO_SYS_ERRLIST */
654 msg = g_static_private_get (&msg_private);
657 msg = g_new( gchar, 64 );
658 g_static_private_set (&msg_private, msg, g_free);
661 sprintf (msg, "unknown error (%d)", errnum);
666 g_strsignal (gint signum)
668 static GStaticPrivate msg_private = G_STATIC_PRIVATE_INIT;
671 #ifdef HAVE_STRSIGNAL
672 extern char *strsignal (int sig);
673 return strsignal (signum);
678 case SIGHUP: return "Hangup";
681 case SIGINT: return "Interrupt";
684 case SIGQUIT: return "Quit";
687 case SIGILL: return "Illegal instruction";
690 case SIGTRAP: return "Trace/breakpoint trap";
693 case SIGABRT: return "IOT trap/Abort";
696 case SIGBUS: return "Bus error";
699 case SIGFPE: return "Floating point exception";
702 case SIGKILL: return "Killed";
705 case SIGUSR1: return "User defined signal 1";
708 case SIGSEGV: return "Segmentation fault";
711 case SIGUSR2: return "User defined signal 2";
714 case SIGPIPE: return "Broken pipe";
717 case SIGALRM: return "Alarm clock";
720 case SIGTERM: return "Terminated";
723 case SIGSTKFLT: return "Stack fault";
726 case SIGCHLD: return "Child exited";
729 case SIGCONT: return "Continued";
732 case SIGSTOP: return "Stopped (signal)";
735 case SIGTSTP: return "Stopped";
738 case SIGTTIN: return "Stopped (tty input)";
741 case SIGTTOU: return "Stopped (tty output)";
744 case SIGURG: return "Urgent condition";
747 case SIGXCPU: return "CPU time limit exceeded";
750 case SIGXFSZ: return "File size limit exceeded";
753 case SIGVTALRM: return "Virtual time alarm";
756 case SIGPROF: return "Profile signal";
759 case SIGWINCH: return "Window size changed";
762 case SIGIO: return "Possible I/O";
765 case SIGPWR: return "Power failure";
768 case SIGUNUSED: return "Unused signal";
771 #else /* NO_SYS_SIGLIST */
772 extern char *sys_siglist[];
773 return sys_siglist [signum];
774 #endif /* NO_SYS_SIGLIST */
776 msg = g_static_private_get (&msg_private);
779 msg = g_new( gchar, 64 );
780 g_static_private_set (&msg_private, msg, g_free);
783 sprintf (msg, "unknown signal (%d)", signum);
788 g_printf_string_upper_bound (const gchar* format,
795 gboolean long_int = FALSE;
796 gboolean extra_long = FALSE;
803 gboolean done = FALSE;
805 while (*format && !done)
812 len += va_arg (args, int);
823 /* add specified format length, since it might exceed the
824 * size we assume it to have.
827 len += strtol (format, (char**) &format, 10);
830 /* ignore short int flag, since all args have at least the
831 * same size as an int
836 extra_long = TRUE; /* linux specific */
846 string_arg = va_arg (args, char *);
848 len += strlen (string_arg);
851 /* add enough padding to hold "(null)" identifier */
864 (void) va_arg (args, gint64);
866 #endif /* G_HAVE_GINT64 */
869 (void) va_arg (args, long);
871 (void) va_arg (args, int);
873 len += extra_long ? 64 : 32;
879 (void) va_arg (args, long);
887 #ifdef HAVE_LONG_DOUBLE
889 (void) va_arg (args, long double);
891 #endif /* HAVE_LONG_DOUBLE */
892 (void) va_arg (args, double);
893 len += extra_long ? 64 : 32;
897 (void) va_arg (args, int);
903 (void) va_arg (args, void*);
912 /* ignore unknow/invalid flags */
925 g_strdown (gchar *string)
929 g_return_if_fail (string != NULL);
941 g_strup (gchar *string)
945 g_return_if_fail (string != NULL);
957 g_strreverse (gchar *string)
959 g_return_if_fail (string != NULL);
963 register gchar *h, *t;
966 t = string + strlen (string) - 1;
982 g_strcasecmp (const gchar *s1,
985 #ifdef HAVE_STRCASECMP
986 return strcasecmp (s1, s2);
990 g_return_val_if_fail (s1 != NULL, 0);
991 g_return_val_if_fail (s2 != NULL, 0);
995 /* According to A. Cox, some platforms have islower's that
996 * don't work right on non-uppercase
998 c1 = isupper ((guchar)*s1) ? tolower ((guchar)*s1) : *s1;
999 c2 = isupper ((guchar)*s2) ? tolower ((guchar)*s2) : *s2;
1005 return (((gint)(guchar) *s1) - ((gint)(guchar) *s2));
1010 g_strncasecmp (const gchar *s1,
1014 #ifdef HAVE_STRNCASECMP
1015 return strncasecmp (s1, s2, n);
1019 g_return_val_if_fail (s1 != NULL, 0);
1020 g_return_val_if_fail (s2 != NULL, 0);
1022 while (n-- && *s1 && *s2)
1024 /* According to A. Cox, some platforms have islower's that
1025 * don't work right on non-uppercase
1027 c1 = isupper ((guchar)*s1) ? tolower ((guchar)*s1) : *s1;
1028 c2 = isupper ((guchar)*s2) ? tolower ((guchar)*s2) : *s2;
1035 return (((gint)(guchar) *s1) - ((gint)(guchar) *s2));
1042 g_strdelimit (gchar *string,
1043 const gchar *delimiters,
1048 g_return_val_if_fail (string != NULL, NULL);
1051 delimiters = G_STR_DELIMITERS;
1053 for (c = string; *c; c++)
1055 if (strchr (delimiters, *c))
1063 g_strescape (gchar *string)
1067 guint backslashes = 0;
1070 g_return_val_if_fail (string != NULL, NULL);
1072 while (*p != '\000')
1073 backslashes += (*p++ == '\\');
1076 return g_strdup (string);
1078 escaped = g_new (gchar, strlen (string) + backslashes + 1);
1083 while (*p != '\000')
1094 /* blame Elliot for these next five routines */
1096 g_strchug (gchar *string)
1100 g_return_val_if_fail (string != NULL, NULL);
1102 for (start = string; *start && isspace (*start); start++)
1105 strcpy (string, start);
1111 g_strchomp (gchar *string)
1115 g_return_val_if_fail (string != NULL, NULL);
1120 for (s = string + strlen (string) - 1; s >= string && isspace (*s); s--)
1127 g_strsplit (const gchar *string,
1128 const gchar *delimiter,
1131 GSList *string_list = NULL, *slist;
1132 gchar **str_array, *s;
1135 g_return_val_if_fail (string != NULL, NULL);
1136 g_return_val_if_fail (delimiter != NULL, NULL);
1139 max_tokens = G_MAXINT;
1141 s = strstr (string, delimiter);
1144 guint delimiter_len = strlen (delimiter);
1152 new_string = g_new (gchar, len + 1);
1153 strncpy (new_string, string, len);
1154 new_string[len] = 0;
1155 string_list = g_slist_prepend (string_list, new_string);
1157 string = s + delimiter_len;
1158 s = strstr (string, delimiter);
1160 while (--max_tokens && s);
1165 string_list = g_slist_prepend (string_list, g_strdup (string));
1168 str_array = g_new (gchar*, n);
1172 str_array[i--] = NULL;
1173 for (slist = string_list; slist; slist = slist->next)
1174 str_array[i--] = slist->data;
1176 g_slist_free (string_list);
1182 g_strfreev (gchar **str_array)
1188 for(i = 0; str_array[i] != NULL; i++)
1189 g_free(str_array[i]);
1196 g_strjoinv (const gchar *separator,
1201 g_return_val_if_fail (str_array != NULL, NULL);
1203 if(separator == NULL)
1209 guint separator_len;
1211 separator_len = strlen (separator);
1212 len = 1 + strlen (str_array[0]);
1213 for(i = 1; str_array[i] != NULL; i++)
1214 len += separator_len + strlen(str_array[i]);
1216 string = g_new (gchar, len);
1218 strcat (string, *str_array);
1219 for (i = 1; str_array[i] != NULL; i++)
1221 strcat (string, separator);
1222 strcat (string, str_array[i]);
1226 string = g_strdup ("");
1232 g_strjoin (const gchar *separator,
1238 guint separator_len;
1240 if(separator == NULL)
1243 separator_len = strlen (separator);
1245 va_start(args, separator);
1247 s = va_arg(args, gchar *);
1250 len = strlen(s) + 1;
1252 while((s = va_arg(args, gchar*)))
1254 len += separator_len + strlen(s);
1258 string = g_new (gchar, len);
1260 va_start(args, separator);
1263 s = va_arg(args, gchar*);
1266 while((s = va_arg(args, gchar*)))
1268 strcat(string, separator);
1273 string = g_strdup("");