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_strconcat (const gchar *string1, ...)
53 g_return_val_if_fail (string1 != NULL, NULL);
55 l = 1 + strlen (string1);
56 va_start (args, string1);
57 s = va_arg (args, gchar*);
61 s = va_arg (args, gchar*);
65 concat = g_new (gchar, l);
68 strcat (concat, string1);
69 va_start (args, string1);
70 s = va_arg (args, gchar*);
74 s = va_arg (args, gchar*);
82 g_strtod (const gchar *nptr,
90 g_return_val_if_fail (nptr != NULL, 0);
95 val_1 = strtod (nptr, &fail_pos_1);
97 if (fail_pos_1 && fail_pos_1[0] != 0)
101 old_locale = setlocale (LC_NUMERIC, "C");
102 val_2 = strtod (nptr, &fail_pos_2);
103 setlocale (LC_NUMERIC, old_locale);
106 if (!fail_pos_1 || fail_pos_1[0] == 0 || fail_pos_1 >= fail_pos_2)
109 *endptr = fail_pos_1;
115 *endptr = fail_pos_2;
121 g_strerror (gint errnum)
126 return strerror (errnum);
131 case E2BIG: return "argument list too long";
134 case EACCES: return "permission denied";
137 case EADDRINUSE: return "address already in use";
140 case EADDRNOTAVAIL: return "can't assign requested address";
143 case EADV: return "advertise error";
146 case EAFNOSUPPORT: return "address family not supported by protocol family";
149 case EAGAIN: return "try again";
152 case EALIGN: return "EALIGN";
155 case EALREADY: return "operation already in progress";
158 case EBADE: return "bad exchange descriptor";
161 case EBADF: return "bad file number";
164 case EBADFD: return "file descriptor in bad state";
167 case EBADMSG: return "not a data message";
170 case EBADR: return "bad request descriptor";
173 case EBADRPC: return "RPC structure is bad";
176 case EBADRQC: return "bad request code";
179 case EBADSLT: return "invalid slot";
182 case EBFONT: return "bad font file format";
185 case EBUSY: return "mount device busy";
188 case ECHILD: return "no children";
191 case ECHRNG: return "channel number out of range";
194 case ECOMM: return "communication error on send";
197 case ECONNABORTED: return "software caused connection abort";
200 case ECONNREFUSED: return "connection refused";
203 case ECONNRESET: return "connection reset by peer";
205 #if defined(EDEADLK) && (!defined(EWOULDBLOCK) || (EDEADLK != EWOULDBLOCK))
206 case EDEADLK: return "resource deadlock avoided";
209 case EDEADLOCK: return "resource deadlock avoided";
212 case EDESTADDRREQ: return "destination address required";
215 case EDIRTY: return "mounting a dirty fs w/o force";
218 case EDOM: return "math argument out of range";
221 case EDOTDOT: return "cross mount point";
224 case EDQUOT: return "disk quota exceeded";
227 case EDUPPKG: return "duplicate package name";
230 case EEXIST: return "file already exists";
233 case EFAULT: return "bad address in system call argument";
236 case EFBIG: return "file too large";
239 case EHOSTDOWN: return "host is down";
242 case EHOSTUNREACH: return "host is unreachable";
245 case EIDRM: return "identifier removed";
248 case EINIT: return "initialization error";
251 case EINPROGRESS: return "operation now in progress";
254 case EINTR: return "interrupted system call";
257 case EINVAL: return "invalid argument";
260 case EIO: return "I/O error";
263 case EISCONN: return "socket is already connected";
266 case EISDIR: return "illegal operation on a directory";
269 case EISNAM: return "is a name file";
272 case ELBIN: return "ELBIN";
275 case EL2HLT: return "level 2 halted";
278 case EL2NSYNC: return "level 2 not synchronized";
281 case EL3HLT: return "level 3 halted";
284 case EL3RST: return "level 3 reset";
287 case ELIBACC: return "can not access a needed shared library";
290 case ELIBBAD: return "accessing a corrupted shared library";
293 case ELIBEXEC: return "can not exec a shared library directly";
296 case ELIBMAX: return "attempting to link in more shared libraries than system limit";
299 case ELIBSCN: return ".lib section in a.out corrupted";
302 case ELNRNG: return "link number out of range";
305 case ELOOP: return "too many levels of symbolic links";
308 case EMFILE: return "too many open files";
311 case EMLINK: return "too many links";
314 case EMSGSIZE: return "message too long";
317 case EMULTIHOP: return "multihop attempted";
320 case ENAMETOOLONG: return "file name too long";
323 case ENAVAIL: return "not available";
326 case ENET: return "ENET";
329 case ENETDOWN: return "network is down";
332 case ENETRESET: return "network dropped connection on reset";
335 case ENETUNREACH: return "network is unreachable";
338 case ENFILE: return "file table overflow";
341 case ENOANO: return "anode table overflow";
343 #if defined(ENOBUFS) && (!defined(ENOSR) || (ENOBUFS != ENOSR))
344 case ENOBUFS: return "no buffer space available";
347 case ENOCSI: return "no CSI structure available";
350 case ENODATA: return "no data available";
353 case ENODEV: return "no such device";
356 case ENOENT: return "no such file or directory";
359 case ENOEXEC: return "exec format error";
362 case ENOLCK: return "no locks available";
365 case ENOLINK: return "link has be severed";
368 case ENOMEM: return "not enough memory";
371 case ENOMSG: return "no message of desired type";
374 case ENONET: return "machine is not on the network";
377 case ENOPKG: return "package not installed";
380 case ENOPROTOOPT: return "bad proocol option";
383 case ENOSPC: return "no space left on device";
386 case ENOSR: return "out of stream resources";
389 case ENOSTR: return "not a stream device";
392 case ENOSYM: return "unresolved symbol name";
395 case ENOSYS: return "function not implemented";
398 case ENOTBLK: return "block device required";
401 case ENOTCONN: return "socket is not connected";
404 case ENOTDIR: return "not a directory";
407 case ENOTEMPTY: return "directory not empty";
410 case ENOTNAM: return "not a name file";
413 case ENOTSOCK: return "socket operation on non-socket";
416 case ENOTTY: return "inappropriate device for ioctl";
419 case ENOTUNIQ: return "name not unique on network";
422 case ENXIO: return "no such device or address";
425 case EOPNOTSUPP: return "operation not supported on socket";
428 case EPERM: return "not owner";
431 case EPFNOSUPPORT: return "protocol family not supported";
434 case EPIPE: return "broken pipe";
437 case EPROCLIM: return "too many processes";
440 case EPROCUNAVAIL: return "bad procedure for program";
443 case EPROGMISMATCH: return "program version wrong";
446 case EPROGUNAVAIL: return "RPC program not available";
449 case EPROTO: return "protocol error";
451 #ifdef EPROTONOSUPPORT
452 case EPROTONOSUPPORT: return "protocol not suppored";
455 case EPROTOTYPE: return "protocol wrong type for socket";
458 case ERANGE: return "math result unrepresentable";
460 #if defined(EREFUSED) && (!defined(ECONNREFUSED) || (EREFUSED != ECONNREFUSED))
461 case EREFUSED: return "EREFUSED";
464 case EREMCHG: return "remote address changed";
467 case EREMDEV: return "remote device";
470 case EREMOTE: return "pathname hit remote file system";
473 case EREMOTEIO: return "remote i/o error";
475 #ifdef EREMOTERELEASE
476 case EREMOTERELEASE: return "EREMOTERELEASE";
479 case EROFS: return "read-only file system";
482 case ERPCMISMATCH: return "RPC version is wrong";
485 case ERREMOTE: return "object is remote";
488 case ESHUTDOWN: return "can't send afer socket shutdown";
490 #ifdef ESOCKTNOSUPPORT
491 case ESOCKTNOSUPPORT: return "socket type not supported";
494 case ESPIPE: return "invalid seek";
497 case ESRCH: return "no such process";
500 case ESRMNT: return "srmount error";
503 case ESTALE: return "stale remote file handle";
506 case ESUCCESS: return "Error 0";
509 case ETIME: return "timer expired";
512 case ETIMEDOUT: return "connection timed out";
515 case ETOOMANYREFS: return "too many references: can't splice";
518 case ETXTBSY: return "text file or pseudo-device busy";
521 case EUCLEAN: return "structure needs cleaning";
524 case EUNATCH: return "protocol driver not attached";
527 case EUSERS: return "too many users";
530 case EVERSION: return "version mismatch";
532 #if defined(EWOULDBLOCK) && (!defined(EAGAIN) || (EWOULDBLOCK != EAGAIN))
533 case EWOULDBLOCK: return "operation would block";
536 case EXDEV: return "cross-domain link";
539 case EXFULL: return "message tables full";
542 #else /* NO_SYS_ERRLIST */
544 extern char *sys_errlist[];
546 if ((errnum > 0) && (errnum <= sys_nerr))
547 return sys_errlist [errnum];
548 #endif /* NO_SYS_ERRLIST */
550 sprintf (msg, "unknown error (%d)", errnum);
555 g_strsignal (gint signum)
559 #ifdef HAVE_STRSIGNAL
560 extern char *strsignal (int sig);
561 return strsignal (signum);
566 case SIGHUP: return "Hangup";
569 case SIGINT: return "Interrupt";
572 case SIGQUIT: return "Quit";
575 case SIGILL: return "Illegal instruction";
578 case SIGTRAP: return "Trace/breakpoint trap";
581 case SIGABRT: return "IOT trap/Abort";
584 case SIGBUS: return "Bus error";
587 case SIGFPE: return "Floating point exception";
590 case SIGKILL: return "Killed";
593 case SIGUSR1: return "User defined signal 1";
596 case SIGSEGV: return "Segmentation fault";
599 case SIGUSR2: return "User defined signal 2";
602 case SIGPIPE: return "Broken pipe";
605 case SIGALRM: return "Alarm clock";
608 case SIGTERM: return "Terminated";
611 case SIGSTKFLT: return "Stack fault";
614 case SIGCHLD: return "Child exited";
617 case SIGCONT: return "Continued";
620 case SIGSTOP: return "Stopped (signal)";
623 case SIGTSTP: return "Stopped";
626 case SIGTTIN: return "Stopped (tty input)";
629 case SIGTTOU: return "Stopped (tty output)";
632 case SIGURG: return "Urgent condition";
635 case SIGXCPU: return "CPU time limit exceeded";
638 case SIGXFSZ: return "File size limit exceeded";
641 case SIGVTALRM: return "Virtual time alarm";
644 case SIGPROF: return "Profile signal";
647 case SIGWINCH: return "Window size changed";
650 case SIGIO: return "Possible I/O";
653 case SIGPWR: return "Power failure";
656 case SIGUNUSED: return "Unused signal";
659 #else /* NO_SYS_SIGLIST */
660 extern char *sys_siglist[];
661 return sys_siglist [signum];
662 #endif /* NO_SYS_SIGLIST */
664 sprintf (msg, "unknown signal (%d)", signum);
669 g_strdown (gchar *string)
673 g_return_if_fail (string != NULL);
685 g_strup (gchar *string)
689 g_return_if_fail (string != NULL);
701 g_strreverse (gchar *string)
703 g_return_if_fail (string != NULL);
707 register gchar *h, *t;
710 t = string + strlen (string) - 1;
726 g_strcasecmp (const gchar *s1,
729 #ifdef HAVE_STRCASECMP
730 return strcasecmp (s1, s2);
736 /* According to A. Cox, some platforms have islower's that
737 * don't work right on non-uppercase
739 c1 = isupper ((guchar)*s1) ? tolower ((guchar)*s1) : *s1;
740 c2 = isupper ((guchar)*s2) ? tolower ((guchar)*s2) : *s2;
746 return (((gint)(guchar) *s1) - ((gint)(guchar) *s2));
751 g_strdelimit (gchar *string,
752 const gchar *delimiters,
757 g_return_if_fail (string != NULL);
760 delimiters = G_STR_DELIMITERS;
762 for (c = string; *c; c++)
764 if (strchr (delimiters, *c))