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.
24 #include <ctype.h> /* For tolower() */
26 /* do not include <unistd.h> in this place since it
27 * inteferes with g_strsignal() on some OSes
31 g_strdup (const gchar *str)
37 new_str = g_new (char, strlen (str) + 1);
38 strcpy (new_str, str);
47 g_strndup (const gchar *str,
54 new_str = g_new (gchar, n + 1);
55 strncpy (new_str, str, n);
65 g_strnfill (guint length,
68 register gchar *str, *s, *end;
70 str = g_new (gchar, length + 1);
81 g_strdup_vprintf (const gchar *format,
87 G_VA_COPY (args2, args1);
89 buffer = g_new (gchar, g_printf_string_upper_bound (format, args1));
91 vsprintf (buffer, format, args2);
98 g_strdup_printf (const gchar *format,
104 va_start (args, format);
105 buffer = g_strdup_vprintf (format, args);
112 g_strconcat (const gchar *string1, ...)
119 g_return_val_if_fail (string1 != NULL, NULL);
121 l = 1 + strlen (string1);
122 va_start (args, string1);
123 s = va_arg (args, gchar*);
127 s = va_arg (args, gchar*);
131 concat = g_new (gchar, l);
134 strcat (concat, string1);
135 va_start (args, string1);
136 s = va_arg (args, gchar*);
140 s = va_arg (args, gchar*);
148 g_strtod (const gchar *nptr,
156 g_return_val_if_fail (nptr != NULL, 0);
161 val_1 = strtod (nptr, &fail_pos_1);
163 if (fail_pos_1 && fail_pos_1[0] != 0)
167 old_locale = setlocale (LC_NUMERIC, "C");
168 val_2 = strtod (nptr, &fail_pos_2);
169 setlocale (LC_NUMERIC, old_locale);
172 if (!fail_pos_1 || fail_pos_1[0] == 0 || fail_pos_1 >= fail_pos_2)
175 *endptr = fail_pos_1;
181 *endptr = fail_pos_2;
187 g_strerror (gint errnum)
192 return strerror (errnum);
197 case E2BIG: return "argument list too long";
200 case EACCES: return "permission denied";
203 case EADDRINUSE: return "address already in use";
206 case EADDRNOTAVAIL: return "can't assign requested address";
209 case EADV: return "advertise error";
212 case EAFNOSUPPORT: return "address family not supported by protocol family";
215 case EAGAIN: return "try again";
218 case EALIGN: return "EALIGN";
221 case EALREADY: return "operation already in progress";
224 case EBADE: return "bad exchange descriptor";
227 case EBADF: return "bad file number";
230 case EBADFD: return "file descriptor in bad state";
233 case EBADMSG: return "not a data message";
236 case EBADR: return "bad request descriptor";
239 case EBADRPC: return "RPC structure is bad";
242 case EBADRQC: return "bad request code";
245 case EBADSLT: return "invalid slot";
248 case EBFONT: return "bad font file format";
251 case EBUSY: return "mount device busy";
254 case ECHILD: return "no children";
257 case ECHRNG: return "channel number out of range";
260 case ECOMM: return "communication error on send";
263 case ECONNABORTED: return "software caused connection abort";
266 case ECONNREFUSED: return "connection refused";
269 case ECONNRESET: return "connection reset by peer";
271 #if defined(EDEADLK) && (!defined(EWOULDBLOCK) || (EDEADLK != EWOULDBLOCK))
272 case EDEADLK: return "resource deadlock avoided";
275 case EDEADLOCK: return "resource deadlock avoided";
278 case EDESTADDRREQ: return "destination address required";
281 case EDIRTY: return "mounting a dirty fs w/o force";
284 case EDOM: return "math argument out of range";
287 case EDOTDOT: return "cross mount point";
290 case EDQUOT: return "disk quota exceeded";
293 case EDUPPKG: return "duplicate package name";
296 case EEXIST: return "file already exists";
299 case EFAULT: return "bad address in system call argument";
302 case EFBIG: return "file too large";
305 case EHOSTDOWN: return "host is down";
308 case EHOSTUNREACH: return "host is unreachable";
311 case EIDRM: return "identifier removed";
314 case EINIT: return "initialization error";
317 case EINPROGRESS: return "operation now in progress";
320 case EINTR: return "interrupted system call";
323 case EINVAL: return "invalid argument";
326 case EIO: return "I/O error";
329 case EISCONN: return "socket is already connected";
332 case EISDIR: return "illegal operation on a directory";
335 case EISNAM: return "is a name file";
338 case ELBIN: return "ELBIN";
341 case EL2HLT: return "level 2 halted";
344 case EL2NSYNC: return "level 2 not synchronized";
347 case EL3HLT: return "level 3 halted";
350 case EL3RST: return "level 3 reset";
353 case ELIBACC: return "can not access a needed shared library";
356 case ELIBBAD: return "accessing a corrupted shared library";
359 case ELIBEXEC: return "can not exec a shared library directly";
362 case ELIBMAX: return "attempting to link in more shared libraries than system limit";
365 case ELIBSCN: return ".lib section in a.out corrupted";
368 case ELNRNG: return "link number out of range";
371 case ELOOP: return "too many levels of symbolic links";
374 case EMFILE: return "too many open files";
377 case EMLINK: return "too many links";
380 case EMSGSIZE: return "message too long";
383 case EMULTIHOP: return "multihop attempted";
386 case ENAMETOOLONG: return "file name too long";
389 case ENAVAIL: return "not available";
392 case ENET: return "ENET";
395 case ENETDOWN: return "network is down";
398 case ENETRESET: return "network dropped connection on reset";
401 case ENETUNREACH: return "network is unreachable";
404 case ENFILE: return "file table overflow";
407 case ENOANO: return "anode table overflow";
409 #if defined(ENOBUFS) && (!defined(ENOSR) || (ENOBUFS != ENOSR))
410 case ENOBUFS: return "no buffer space available";
413 case ENOCSI: return "no CSI structure available";
416 case ENODATA: return "no data available";
419 case ENODEV: return "no such device";
422 case ENOENT: return "no such file or directory";
425 case ENOEXEC: return "exec format error";
428 case ENOLCK: return "no locks available";
431 case ENOLINK: return "link has be severed";
434 case ENOMEM: return "not enough memory";
437 case ENOMSG: return "no message of desired type";
440 case ENONET: return "machine is not on the network";
443 case ENOPKG: return "package not installed";
446 case ENOPROTOOPT: return "bad proocol option";
449 case ENOSPC: return "no space left on device";
452 case ENOSR: return "out of stream resources";
455 case ENOSTR: return "not a stream device";
458 case ENOSYM: return "unresolved symbol name";
461 case ENOSYS: return "function not implemented";
464 case ENOTBLK: return "block device required";
467 case ENOTCONN: return "socket is not connected";
470 case ENOTDIR: return "not a directory";
473 case ENOTEMPTY: return "directory not empty";
476 case ENOTNAM: return "not a name file";
479 case ENOTSOCK: return "socket operation on non-socket";
482 case ENOTTY: return "inappropriate device for ioctl";
485 case ENOTUNIQ: return "name not unique on network";
488 case ENXIO: return "no such device or address";
491 case EOPNOTSUPP: return "operation not supported on socket";
494 case EPERM: return "not owner";
497 case EPFNOSUPPORT: return "protocol family not supported";
500 case EPIPE: return "broken pipe";
503 case EPROCLIM: return "too many processes";
506 case EPROCUNAVAIL: return "bad procedure for program";
509 case EPROGMISMATCH: return "program version wrong";
512 case EPROGUNAVAIL: return "RPC program not available";
515 case EPROTO: return "protocol error";
517 #ifdef EPROTONOSUPPORT
518 case EPROTONOSUPPORT: return "protocol not suppored";
521 case EPROTOTYPE: return "protocol wrong type for socket";
524 case ERANGE: return "math result unrepresentable";
526 #if defined(EREFUSED) && (!defined(ECONNREFUSED) || (EREFUSED != ECONNREFUSED))
527 case EREFUSED: return "EREFUSED";
530 case EREMCHG: return "remote address changed";
533 case EREMDEV: return "remote device";
536 case EREMOTE: return "pathname hit remote file system";
539 case EREMOTEIO: return "remote i/o error";
541 #ifdef EREMOTERELEASE
542 case EREMOTERELEASE: return "EREMOTERELEASE";
545 case EROFS: return "read-only file system";
548 case ERPCMISMATCH: return "RPC version is wrong";
551 case ERREMOTE: return "object is remote";
554 case ESHUTDOWN: return "can't send afer socket shutdown";
556 #ifdef ESOCKTNOSUPPORT
557 case ESOCKTNOSUPPORT: return "socket type not supported";
560 case ESPIPE: return "invalid seek";
563 case ESRCH: return "no such process";
566 case ESRMNT: return "srmount error";
569 case ESTALE: return "stale remote file handle";
572 case ESUCCESS: return "Error 0";
575 case ETIME: return "timer expired";
578 case ETIMEDOUT: return "connection timed out";
581 case ETOOMANYREFS: return "too many references: can't splice";
584 case ETXTBSY: return "text file or pseudo-device busy";
587 case EUCLEAN: return "structure needs cleaning";
590 case EUNATCH: return "protocol driver not attached";
593 case EUSERS: return "too many users";
596 case EVERSION: return "version mismatch";
598 #if defined(EWOULDBLOCK) && (!defined(EAGAIN) || (EWOULDBLOCK != EAGAIN))
599 case EWOULDBLOCK: return "operation would block";
602 case EXDEV: return "cross-domain link";
605 case EXFULL: return "message tables full";
608 #else /* NO_SYS_ERRLIST */
610 extern char *sys_errlist[];
612 if ((errnum > 0) && (errnum <= sys_nerr))
613 return sys_errlist [errnum];
614 #endif /* NO_SYS_ERRLIST */
616 sprintf (msg, "unknown error (%d)", errnum);
621 g_strsignal (gint signum)
625 #ifdef HAVE_STRSIGNAL
626 extern char *strsignal (int sig);
627 return strsignal (signum);
632 case SIGHUP: return "Hangup";
635 case SIGINT: return "Interrupt";
638 case SIGQUIT: return "Quit";
641 case SIGILL: return "Illegal instruction";
644 case SIGTRAP: return "Trace/breakpoint trap";
647 case SIGABRT: return "IOT trap/Abort";
650 case SIGBUS: return "Bus error";
653 case SIGFPE: return "Floating point exception";
656 case SIGKILL: return "Killed";
659 case SIGUSR1: return "User defined signal 1";
662 case SIGSEGV: return "Segmentation fault";
665 case SIGUSR2: return "User defined signal 2";
668 case SIGPIPE: return "Broken pipe";
671 case SIGALRM: return "Alarm clock";
674 case SIGTERM: return "Terminated";
677 case SIGSTKFLT: return "Stack fault";
680 case SIGCHLD: return "Child exited";
683 case SIGCONT: return "Continued";
686 case SIGSTOP: return "Stopped (signal)";
689 case SIGTSTP: return "Stopped";
692 case SIGTTIN: return "Stopped (tty input)";
695 case SIGTTOU: return "Stopped (tty output)";
698 case SIGURG: return "Urgent condition";
701 case SIGXCPU: return "CPU time limit exceeded";
704 case SIGXFSZ: return "File size limit exceeded";
707 case SIGVTALRM: return "Virtual time alarm";
710 case SIGPROF: return "Profile signal";
713 case SIGWINCH: return "Window size changed";
716 case SIGIO: return "Possible I/O";
719 case SIGPWR: return "Power failure";
722 case SIGUNUSED: return "Unused signal";
725 #else /* NO_SYS_SIGLIST */
726 extern char *sys_siglist[];
727 return sys_siglist [signum];
728 #endif /* NO_SYS_SIGLIST */
730 sprintf (msg, "unknown signal (%d)", signum);
735 g_printf_string_upper_bound (const gchar* format,
742 gboolean long_int = FALSE;
743 gboolean extra_long = FALSE;
750 gboolean done = FALSE;
752 while (*format && !done)
759 len += va_arg (args, int);
770 /* add specified format length, since it might exceed the
771 * size we assume it to have.
774 len += strtol (format, (char**) &format, 10);
777 /* ignore short int flag, since all args have at least the
778 * same size as an int
783 extra_long = TRUE; /* linux specific */
793 string_arg = va_arg (args, char *);
795 len += strlen (string_arg);
798 /* add enough padding to hold "(null)" identifier */
811 (void) va_arg (args, gint64);
813 #endif /* HAVE_GINT64 */
816 (void) va_arg (args, long);
818 (void) va_arg (args, int);
820 len += extra_long ? 64 : 32;
826 (void) va_arg (args, long);
834 #ifdef HAVE_LONG_DOUBLE
836 (void) va_arg (args, long double);
838 #endif /* HAVE_LONG_DOUBLE */
839 (void) va_arg (args, double);
840 len += extra_long ? 64 : 32;
844 (void) va_arg (args, int);
850 (void) va_arg (args, void*);
859 /* ignore unknow/invalid flags */
872 g_strdown (gchar *string)
876 g_return_if_fail (string != NULL);
888 g_strup (gchar *string)
892 g_return_if_fail (string != NULL);
904 g_strreverse (gchar *string)
906 g_return_if_fail (string != NULL);
910 register gchar *h, *t;
913 t = string + strlen (string) - 1;
929 g_strcasecmp (const gchar *s1,
932 #ifdef HAVE_STRCASECMP
933 return strcasecmp (s1, s2);
939 /* According to A. Cox, some platforms have islower's that
940 * don't work right on non-uppercase
942 c1 = isupper ((guchar)*s1) ? tolower ((guchar)*s1) : *s1;
943 c2 = isupper ((guchar)*s2) ? tolower ((guchar)*s2) : *s2;
949 return (((gint)(guchar) *s1) - ((gint)(guchar) *s2));
954 g_strdelimit (gchar *string,
955 const gchar *delimiters,
960 g_return_if_fail (string != NULL);
963 delimiters = G_STR_DELIMITERS;
965 for (c = string; *c; c++)
967 if (strchr (delimiters, *c))