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_strconcat (const gchar *string1, ...)
69 g_return_val_if_fail (string1 != NULL, NULL);
71 l = 1 + strlen (string1);
72 va_start (args, string1);
73 s = va_arg (args, gchar*);
77 s = va_arg (args, gchar*);
81 concat = g_new (gchar, l);
84 strcat (concat, string1);
85 va_start (args, string1);
86 s = va_arg (args, gchar*);
90 s = va_arg (args, gchar*);
98 g_strtod (const gchar *nptr,
106 g_return_val_if_fail (nptr != NULL, 0);
111 val_1 = strtod (nptr, &fail_pos_1);
113 if (fail_pos_1 && fail_pos_1[0] != 0)
117 old_locale = setlocale (LC_NUMERIC, "C");
118 val_2 = strtod (nptr, &fail_pos_2);
119 setlocale (LC_NUMERIC, old_locale);
122 if (!fail_pos_1 || fail_pos_1[0] == 0 || fail_pos_1 >= fail_pos_2)
125 *endptr = fail_pos_1;
131 *endptr = fail_pos_2;
137 g_strerror (gint errnum)
142 return strerror (errnum);
147 case E2BIG: return "argument list too long";
150 case EACCES: return "permission denied";
153 case EADDRINUSE: return "address already in use";
156 case EADDRNOTAVAIL: return "can't assign requested address";
159 case EADV: return "advertise error";
162 case EAFNOSUPPORT: return "address family not supported by protocol family";
165 case EAGAIN: return "try again";
168 case EALIGN: return "EALIGN";
171 case EALREADY: return "operation already in progress";
174 case EBADE: return "bad exchange descriptor";
177 case EBADF: return "bad file number";
180 case EBADFD: return "file descriptor in bad state";
183 case EBADMSG: return "not a data message";
186 case EBADR: return "bad request descriptor";
189 case EBADRPC: return "RPC structure is bad";
192 case EBADRQC: return "bad request code";
195 case EBADSLT: return "invalid slot";
198 case EBFONT: return "bad font file format";
201 case EBUSY: return "mount device busy";
204 case ECHILD: return "no children";
207 case ECHRNG: return "channel number out of range";
210 case ECOMM: return "communication error on send";
213 case ECONNABORTED: return "software caused connection abort";
216 case ECONNREFUSED: return "connection refused";
219 case ECONNRESET: return "connection reset by peer";
221 #if defined(EDEADLK) && (!defined(EWOULDBLOCK) || (EDEADLK != EWOULDBLOCK))
222 case EDEADLK: return "resource deadlock avoided";
225 case EDEADLOCK: return "resource deadlock avoided";
228 case EDESTADDRREQ: return "destination address required";
231 case EDIRTY: return "mounting a dirty fs w/o force";
234 case EDOM: return "math argument out of range";
237 case EDOTDOT: return "cross mount point";
240 case EDQUOT: return "disk quota exceeded";
243 case EDUPPKG: return "duplicate package name";
246 case EEXIST: return "file already exists";
249 case EFAULT: return "bad address in system call argument";
252 case EFBIG: return "file too large";
255 case EHOSTDOWN: return "host is down";
258 case EHOSTUNREACH: return "host is unreachable";
261 case EIDRM: return "identifier removed";
264 case EINIT: return "initialization error";
267 case EINPROGRESS: return "operation now in progress";
270 case EINTR: return "interrupted system call";
273 case EINVAL: return "invalid argument";
276 case EIO: return "I/O error";
279 case EISCONN: return "socket is already connected";
282 case EISDIR: return "illegal operation on a directory";
285 case EISNAM: return "is a name file";
288 case ELBIN: return "ELBIN";
291 case EL2HLT: return "level 2 halted";
294 case EL2NSYNC: return "level 2 not synchronized";
297 case EL3HLT: return "level 3 halted";
300 case EL3RST: return "level 3 reset";
303 case ELIBACC: return "can not access a needed shared library";
306 case ELIBBAD: return "accessing a corrupted shared library";
309 case ELIBEXEC: return "can not exec a shared library directly";
312 case ELIBMAX: return "attempting to link in more shared libraries than system limit";
315 case ELIBSCN: return ".lib section in a.out corrupted";
318 case ELNRNG: return "link number out of range";
321 case ELOOP: return "too many levels of symbolic links";
324 case EMFILE: return "too many open files";
327 case EMLINK: return "too many links";
330 case EMSGSIZE: return "message too long";
333 case EMULTIHOP: return "multihop attempted";
336 case ENAMETOOLONG: return "file name too long";
339 case ENAVAIL: return "not available";
342 case ENET: return "ENET";
345 case ENETDOWN: return "network is down";
348 case ENETRESET: return "network dropped connection on reset";
351 case ENETUNREACH: return "network is unreachable";
354 case ENFILE: return "file table overflow";
357 case ENOANO: return "anode table overflow";
359 #if defined(ENOBUFS) && (!defined(ENOSR) || (ENOBUFS != ENOSR))
360 case ENOBUFS: return "no buffer space available";
363 case ENOCSI: return "no CSI structure available";
366 case ENODATA: return "no data available";
369 case ENODEV: return "no such device";
372 case ENOENT: return "no such file or directory";
375 case ENOEXEC: return "exec format error";
378 case ENOLCK: return "no locks available";
381 case ENOLINK: return "link has be severed";
384 case ENOMEM: return "not enough memory";
387 case ENOMSG: return "no message of desired type";
390 case ENONET: return "machine is not on the network";
393 case ENOPKG: return "package not installed";
396 case ENOPROTOOPT: return "bad proocol option";
399 case ENOSPC: return "no space left on device";
402 case ENOSR: return "out of stream resources";
405 case ENOSTR: return "not a stream device";
408 case ENOSYM: return "unresolved symbol name";
411 case ENOSYS: return "function not implemented";
414 case ENOTBLK: return "block device required";
417 case ENOTCONN: return "socket is not connected";
420 case ENOTDIR: return "not a directory";
423 case ENOTEMPTY: return "directory not empty";
426 case ENOTNAM: return "not a name file";
429 case ENOTSOCK: return "socket operation on non-socket";
432 case ENOTTY: return "inappropriate device for ioctl";
435 case ENOTUNIQ: return "name not unique on network";
438 case ENXIO: return "no such device or address";
441 case EOPNOTSUPP: return "operation not supported on socket";
444 case EPERM: return "not owner";
447 case EPFNOSUPPORT: return "protocol family not supported";
450 case EPIPE: return "broken pipe";
453 case EPROCLIM: return "too many processes";
456 case EPROCUNAVAIL: return "bad procedure for program";
459 case EPROGMISMATCH: return "program version wrong";
462 case EPROGUNAVAIL: return "RPC program not available";
465 case EPROTO: return "protocol error";
467 #ifdef EPROTONOSUPPORT
468 case EPROTONOSUPPORT: return "protocol not suppored";
471 case EPROTOTYPE: return "protocol wrong type for socket";
474 case ERANGE: return "math result unrepresentable";
476 #if defined(EREFUSED) && (!defined(ECONNREFUSED) || (EREFUSED != ECONNREFUSED))
477 case EREFUSED: return "EREFUSED";
480 case EREMCHG: return "remote address changed";
483 case EREMDEV: return "remote device";
486 case EREMOTE: return "pathname hit remote file system";
489 case EREMOTEIO: return "remote i/o error";
491 #ifdef EREMOTERELEASE
492 case EREMOTERELEASE: return "EREMOTERELEASE";
495 case EROFS: return "read-only file system";
498 case ERPCMISMATCH: return "RPC version is wrong";
501 case ERREMOTE: return "object is remote";
504 case ESHUTDOWN: return "can't send afer socket shutdown";
506 #ifdef ESOCKTNOSUPPORT
507 case ESOCKTNOSUPPORT: return "socket type not supported";
510 case ESPIPE: return "invalid seek";
513 case ESRCH: return "no such process";
516 case ESRMNT: return "srmount error";
519 case ESTALE: return "stale remote file handle";
522 case ESUCCESS: return "Error 0";
525 case ETIME: return "timer expired";
528 case ETIMEDOUT: return "connection timed out";
531 case ETOOMANYREFS: return "too many references: can't splice";
534 case ETXTBSY: return "text file or pseudo-device busy";
537 case EUCLEAN: return "structure needs cleaning";
540 case EUNATCH: return "protocol driver not attached";
543 case EUSERS: return "too many users";
546 case EVERSION: return "version mismatch";
548 #if defined(EWOULDBLOCK) && (!defined(EAGAIN) || (EWOULDBLOCK != EAGAIN))
549 case EWOULDBLOCK: return "operation would block";
552 case EXDEV: return "cross-domain link";
555 case EXFULL: return "message tables full";
558 #else /* NO_SYS_ERRLIST */
560 extern char *sys_errlist[];
562 if ((errnum > 0) && (errnum <= sys_nerr))
563 return sys_errlist [errnum];
564 #endif /* NO_SYS_ERRLIST */
566 sprintf (msg, "unknown error (%d)", errnum);
571 g_strsignal (gint signum)
575 #ifdef HAVE_STRSIGNAL
576 extern char *strsignal (int sig);
577 return strsignal (signum);
582 case SIGHUP: return "Hangup";
585 case SIGINT: return "Interrupt";
588 case SIGQUIT: return "Quit";
591 case SIGILL: return "Illegal instruction";
594 case SIGTRAP: return "Trace/breakpoint trap";
597 case SIGABRT: return "IOT trap/Abort";
600 case SIGBUS: return "Bus error";
603 case SIGFPE: return "Floating point exception";
606 case SIGKILL: return "Killed";
609 case SIGUSR1: return "User defined signal 1";
612 case SIGSEGV: return "Segmentation fault";
615 case SIGUSR2: return "User defined signal 2";
618 case SIGPIPE: return "Broken pipe";
621 case SIGALRM: return "Alarm clock";
624 case SIGTERM: return "Terminated";
627 case SIGSTKFLT: return "Stack fault";
630 case SIGCHLD: return "Child exited";
633 case SIGCONT: return "Continued";
636 case SIGSTOP: return "Stopped (signal)";
639 case SIGTSTP: return "Stopped";
642 case SIGTTIN: return "Stopped (tty input)";
645 case SIGTTOU: return "Stopped (tty output)";
648 case SIGURG: return "Urgent condition";
651 case SIGXCPU: return "CPU time limit exceeded";
654 case SIGXFSZ: return "File size limit exceeded";
657 case SIGVTALRM: return "Virtual time alarm";
660 case SIGPROF: return "Profile signal";
663 case SIGWINCH: return "Window size changed";
666 case SIGIO: return "Possible I/O";
669 case SIGPWR: return "Power failure";
672 case SIGUNUSED: return "Unused signal";
675 #else /* NO_SYS_SIGLIST */
676 extern char *sys_siglist[];
677 return sys_siglist [signum];
678 #endif /* NO_SYS_SIGLIST */
680 sprintf (msg, "unknown signal (%d)", signum);
685 g_strdown (gchar *string)
689 g_return_if_fail (string != NULL);
701 g_strup (gchar *string)
705 g_return_if_fail (string != NULL);
717 g_strreverse (gchar *string)
719 g_return_if_fail (string != NULL);
723 register gchar *h, *t;
726 t = string + strlen (string) - 1;
742 g_strcasecmp (const gchar *s1,
745 #ifdef HAVE_STRCASECMP
746 return strcasecmp (s1, s2);
752 /* According to A. Cox, some platforms have islower's that
753 * don't work right on non-uppercase
755 c1 = isupper ((guchar)*s1) ? tolower ((guchar)*s1) : *s1;
756 c2 = isupper ((guchar)*s2) ? tolower ((guchar)*s2) : *s2;
762 return (((gint)(guchar) *s1) - ((gint)(guchar) *s2));
767 g_strdelimit (gchar *string,
768 const gchar *delimiters,
773 g_return_if_fail (string != NULL);
776 delimiters = G_STR_DELIMITERS;
778 for (c = string; *c; c++)
780 if (strchr (delimiters, *c))