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)
38 new_str = g_new (char, strlen (str) + 1);
39 strcpy (new_str, str);
46 g_strndup (const gchar *str, gulong n)
53 new_str = g_new (char, n + 1);
54 strncpy (new_str, str, n);
62 g_strdup_vprintf (const gchar *format,
68 G_VA_COPY (args2, args1);
70 buffer = g_new (gchar, g_printf_string_upper_bound (format, args1));
72 vsprintf (buffer, format, args2);
79 g_strdup_printf (const gchar *format,
85 va_start (args, format);
86 buffer = g_strdup_vprintf (format, args);
93 g_strconcat (const gchar *string1, ...)
100 g_return_val_if_fail (string1 != NULL, NULL);
102 l = 1 + strlen (string1);
103 va_start (args, string1);
104 s = va_arg (args, gchar*);
108 s = va_arg (args, gchar*);
112 concat = g_new (gchar, l);
115 strcat (concat, string1);
116 va_start (args, string1);
117 s = va_arg (args, gchar*);
121 s = va_arg (args, gchar*);
129 g_strtod (const gchar *nptr,
137 g_return_val_if_fail (nptr != NULL, 0);
142 val_1 = strtod (nptr, &fail_pos_1);
144 if (fail_pos_1 && fail_pos_1[0] != 0)
148 old_locale = setlocale (LC_NUMERIC, "C");
149 val_2 = strtod (nptr, &fail_pos_2);
150 setlocale (LC_NUMERIC, old_locale);
153 if (!fail_pos_1 || fail_pos_1[0] == 0 || fail_pos_1 >= fail_pos_2)
156 *endptr = fail_pos_1;
162 *endptr = fail_pos_2;
168 g_strerror (gint errnum)
173 return strerror (errnum);
178 case E2BIG: return "argument list too long";
181 case EACCES: return "permission denied";
184 case EADDRINUSE: return "address already in use";
187 case EADDRNOTAVAIL: return "can't assign requested address";
190 case EADV: return "advertise error";
193 case EAFNOSUPPORT: return "address family not supported by protocol family";
196 case EAGAIN: return "try again";
199 case EALIGN: return "EALIGN";
202 case EALREADY: return "operation already in progress";
205 case EBADE: return "bad exchange descriptor";
208 case EBADF: return "bad file number";
211 case EBADFD: return "file descriptor in bad state";
214 case EBADMSG: return "not a data message";
217 case EBADR: return "bad request descriptor";
220 case EBADRPC: return "RPC structure is bad";
223 case EBADRQC: return "bad request code";
226 case EBADSLT: return "invalid slot";
229 case EBFONT: return "bad font file format";
232 case EBUSY: return "mount device busy";
235 case ECHILD: return "no children";
238 case ECHRNG: return "channel number out of range";
241 case ECOMM: return "communication error on send";
244 case ECONNABORTED: return "software caused connection abort";
247 case ECONNREFUSED: return "connection refused";
250 case ECONNRESET: return "connection reset by peer";
252 #if defined(EDEADLK) && (!defined(EWOULDBLOCK) || (EDEADLK != EWOULDBLOCK))
253 case EDEADLK: return "resource deadlock avoided";
256 case EDEADLOCK: return "resource deadlock avoided";
259 case EDESTADDRREQ: return "destination address required";
262 case EDIRTY: return "mounting a dirty fs w/o force";
265 case EDOM: return "math argument out of range";
268 case EDOTDOT: return "cross mount point";
271 case EDQUOT: return "disk quota exceeded";
274 case EDUPPKG: return "duplicate package name";
277 case EEXIST: return "file already exists";
280 case EFAULT: return "bad address in system call argument";
283 case EFBIG: return "file too large";
286 case EHOSTDOWN: return "host is down";
289 case EHOSTUNREACH: return "host is unreachable";
292 case EIDRM: return "identifier removed";
295 case EINIT: return "initialization error";
298 case EINPROGRESS: return "operation now in progress";
301 case EINTR: return "interrupted system call";
304 case EINVAL: return "invalid argument";
307 case EIO: return "I/O error";
310 case EISCONN: return "socket is already connected";
313 case EISDIR: return "illegal operation on a directory";
316 case EISNAM: return "is a name file";
319 case ELBIN: return "ELBIN";
322 case EL2HLT: return "level 2 halted";
325 case EL2NSYNC: return "level 2 not synchronized";
328 case EL3HLT: return "level 3 halted";
331 case EL3RST: return "level 3 reset";
334 case ELIBACC: return "can not access a needed shared library";
337 case ELIBBAD: return "accessing a corrupted shared library";
340 case ELIBEXEC: return "can not exec a shared library directly";
343 case ELIBMAX: return "attempting to link in more shared libraries than system limit";
346 case ELIBSCN: return ".lib section in a.out corrupted";
349 case ELNRNG: return "link number out of range";
352 case ELOOP: return "too many levels of symbolic links";
355 case EMFILE: return "too many open files";
358 case EMLINK: return "too many links";
361 case EMSGSIZE: return "message too long";
364 case EMULTIHOP: return "multihop attempted";
367 case ENAMETOOLONG: return "file name too long";
370 case ENAVAIL: return "not available";
373 case ENET: return "ENET";
376 case ENETDOWN: return "network is down";
379 case ENETRESET: return "network dropped connection on reset";
382 case ENETUNREACH: return "network is unreachable";
385 case ENFILE: return "file table overflow";
388 case ENOANO: return "anode table overflow";
390 #if defined(ENOBUFS) && (!defined(ENOSR) || (ENOBUFS != ENOSR))
391 case ENOBUFS: return "no buffer space available";
394 case ENOCSI: return "no CSI structure available";
397 case ENODATA: return "no data available";
400 case ENODEV: return "no such device";
403 case ENOENT: return "no such file or directory";
406 case ENOEXEC: return "exec format error";
409 case ENOLCK: return "no locks available";
412 case ENOLINK: return "link has be severed";
415 case ENOMEM: return "not enough memory";
418 case ENOMSG: return "no message of desired type";
421 case ENONET: return "machine is not on the network";
424 case ENOPKG: return "package not installed";
427 case ENOPROTOOPT: return "bad proocol option";
430 case ENOSPC: return "no space left on device";
433 case ENOSR: return "out of stream resources";
436 case ENOSTR: return "not a stream device";
439 case ENOSYM: return "unresolved symbol name";
442 case ENOSYS: return "function not implemented";
445 case ENOTBLK: return "block device required";
448 case ENOTCONN: return "socket is not connected";
451 case ENOTDIR: return "not a directory";
454 case ENOTEMPTY: return "directory not empty";
457 case ENOTNAM: return "not a name file";
460 case ENOTSOCK: return "socket operation on non-socket";
463 case ENOTTY: return "inappropriate device for ioctl";
466 case ENOTUNIQ: return "name not unique on network";
469 case ENXIO: return "no such device or address";
472 case EOPNOTSUPP: return "operation not supported on socket";
475 case EPERM: return "not owner";
478 case EPFNOSUPPORT: return "protocol family not supported";
481 case EPIPE: return "broken pipe";
484 case EPROCLIM: return "too many processes";
487 case EPROCUNAVAIL: return "bad procedure for program";
490 case EPROGMISMATCH: return "program version wrong";
493 case EPROGUNAVAIL: return "RPC program not available";
496 case EPROTO: return "protocol error";
498 #ifdef EPROTONOSUPPORT
499 case EPROTONOSUPPORT: return "protocol not suppored";
502 case EPROTOTYPE: return "protocol wrong type for socket";
505 case ERANGE: return "math result unrepresentable";
507 #if defined(EREFUSED) && (!defined(ECONNREFUSED) || (EREFUSED != ECONNREFUSED))
508 case EREFUSED: return "EREFUSED";
511 case EREMCHG: return "remote address changed";
514 case EREMDEV: return "remote device";
517 case EREMOTE: return "pathname hit remote file system";
520 case EREMOTEIO: return "remote i/o error";
522 #ifdef EREMOTERELEASE
523 case EREMOTERELEASE: return "EREMOTERELEASE";
526 case EROFS: return "read-only file system";
529 case ERPCMISMATCH: return "RPC version is wrong";
532 case ERREMOTE: return "object is remote";
535 case ESHUTDOWN: return "can't send afer socket shutdown";
537 #ifdef ESOCKTNOSUPPORT
538 case ESOCKTNOSUPPORT: return "socket type not supported";
541 case ESPIPE: return "invalid seek";
544 case ESRCH: return "no such process";
547 case ESRMNT: return "srmount error";
550 case ESTALE: return "stale remote file handle";
553 case ESUCCESS: return "Error 0";
556 case ETIME: return "timer expired";
559 case ETIMEDOUT: return "connection timed out";
562 case ETOOMANYREFS: return "too many references: can't splice";
565 case ETXTBSY: return "text file or pseudo-device busy";
568 case EUCLEAN: return "structure needs cleaning";
571 case EUNATCH: return "protocol driver not attached";
574 case EUSERS: return "too many users";
577 case EVERSION: return "version mismatch";
579 #if defined(EWOULDBLOCK) && (!defined(EAGAIN) || (EWOULDBLOCK != EAGAIN))
580 case EWOULDBLOCK: return "operation would block";
583 case EXDEV: return "cross-domain link";
586 case EXFULL: return "message tables full";
589 #else /* NO_SYS_ERRLIST */
591 extern char *sys_errlist[];
593 if ((errnum > 0) && (errnum <= sys_nerr))
594 return sys_errlist [errnum];
595 #endif /* NO_SYS_ERRLIST */
597 sprintf (msg, "unknown error (%d)", errnum);
602 g_strsignal (gint signum)
606 #ifdef HAVE_STRSIGNAL
607 extern char *strsignal (int sig);
608 return strsignal (signum);
613 case SIGHUP: return "Hangup";
616 case SIGINT: return "Interrupt";
619 case SIGQUIT: return "Quit";
622 case SIGILL: return "Illegal instruction";
625 case SIGTRAP: return "Trace/breakpoint trap";
628 case SIGABRT: return "IOT trap/Abort";
631 case SIGBUS: return "Bus error";
634 case SIGFPE: return "Floating point exception";
637 case SIGKILL: return "Killed";
640 case SIGUSR1: return "User defined signal 1";
643 case SIGSEGV: return "Segmentation fault";
646 case SIGUSR2: return "User defined signal 2";
649 case SIGPIPE: return "Broken pipe";
652 case SIGALRM: return "Alarm clock";
655 case SIGTERM: return "Terminated";
658 case SIGSTKFLT: return "Stack fault";
661 case SIGCHLD: return "Child exited";
664 case SIGCONT: return "Continued";
667 case SIGSTOP: return "Stopped (signal)";
670 case SIGTSTP: return "Stopped";
673 case SIGTTIN: return "Stopped (tty input)";
676 case SIGTTOU: return "Stopped (tty output)";
679 case SIGURG: return "Urgent condition";
682 case SIGXCPU: return "CPU time limit exceeded";
685 case SIGXFSZ: return "File size limit exceeded";
688 case SIGVTALRM: return "Virtual time alarm";
691 case SIGPROF: return "Profile signal";
694 case SIGWINCH: return "Window size changed";
697 case SIGIO: return "Possible I/O";
700 case SIGPWR: return "Power failure";
703 case SIGUNUSED: return "Unused signal";
706 #else /* NO_SYS_SIGLIST */
707 extern char *sys_siglist[];
708 return sys_siglist [signum];
709 #endif /* NO_SYS_SIGLIST */
711 sprintf (msg, "unknown signal (%d)", signum);
716 g_printf_string_upper_bound (const gchar* format,
723 gboolean long_int = FALSE;
724 gboolean extra_long = FALSE;
731 gboolean done = FALSE;
733 while (*format && !done)
740 len += va_arg (args, int);
751 /* add specified format length, since it might exceed the
752 * size we assume it to have.
755 len += strtol (format, (char**) &format, 10);
758 /* ignore short int flag, since all args have at least the
759 * same size as an int
764 extra_long = TRUE; /* linux specific */
774 string_arg = va_arg (args, char *);
776 len += strlen (string_arg);
779 /* add enough padding to hold "(null)" identifier */
792 (void) va_arg (args, gint64);
794 #endif /* HAVE_GINT64 */
797 (void) va_arg (args, long);
799 (void) va_arg (args, int);
801 len += extra_long ? 64 : 32;
807 (void) va_arg (args, long);
815 #ifdef HAVE_LONG_DOUBLE
817 (void) va_arg (args, long double);
819 #endif /* HAVE_LONG_DOUBLE */
820 (void) va_arg (args, double);
821 len += extra_long ? 64 : 32;
825 (void) va_arg (args, int);
831 (void) va_arg (args, void*);
840 /* ignore unknow/invalid flags */
853 g_strdown (gchar *string)
857 g_return_if_fail (string != NULL);
869 g_strup (gchar *string)
873 g_return_if_fail (string != NULL);
885 g_strreverse (gchar *string)
887 g_return_if_fail (string != NULL);
891 register gchar *h, *t;
894 t = string + strlen (string) - 1;
910 g_strcasecmp (const gchar *s1,
913 #ifdef HAVE_STRCASECMP
914 return strcasecmp (s1, s2);
920 /* According to A. Cox, some platforms have islower's that
921 * don't work right on non-uppercase
923 c1 = isupper ((guchar)*s1) ? tolower ((guchar)*s1) : *s1;
924 c2 = isupper ((guchar)*s2) ? tolower ((guchar)*s2) : *s2;
930 return (((gint)(guchar) *s1) - ((gint)(guchar) *s2));
935 g_strdelimit (gchar *string,
936 const gchar *delimiters,
941 g_return_if_fail (string != NULL);
944 delimiters = G_STR_DELIMITERS;
946 for (c = string; *c; c++)
948 if (strchr (delimiters, *c))