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() */
28 static GErrorFunc glib_error_func = NULL;
29 static GWarningFunc glib_warning_func = NULL;
30 static GPrintFunc glib_message_func = NULL;
31 static GPrintFunc glib_print_func = NULL;
33 extern char* g_vsprintf (const gchar *fmt, va_list *args, va_list *args2);
36 g_strdup (const gchar *str)
43 new_str = g_new (char, strlen (str) + 1);
44 strcpy (new_str, str);
51 g_strconcat (const gchar *string1, ...)
58 g_return_val_if_fail (string1 != NULL, NULL);
60 l = 1 + strlen (string1);
61 va_start (args, string1);
62 s = va_arg (args, gchar*);
66 s = va_arg (args, gchar*);
70 concat = g_new (gchar, l);
73 strcat (concat, string1);
74 va_start (args, string1);
75 s = va_arg (args, gchar*);
79 s = va_arg (args, gchar*);
87 g_strtod (const gchar *nptr,
95 g_return_val_if_fail (nptr != NULL, 0);
100 val_1 = strtod (nptr, &fail_pos_1);
102 if (fail_pos_1 && fail_pos_1[0] != 0)
106 old_locale = setlocale (LC_NUMERIC, "C");
107 val_2 = strtod (nptr, &fail_pos_2);
108 setlocale (LC_NUMERIC, old_locale);
111 if (!fail_pos_1 || fail_pos_1[0] == 0 || fail_pos_1 >= fail_pos_2)
114 *endptr = fail_pos_1;
120 *endptr = fail_pos_2;
126 g_strerror (gint errnum)
131 return strerror (errnum);
136 case E2BIG: return "argument list too long";
139 case EACCES: return "permission denied";
142 case EADDRINUSE: return "address already in use";
145 case EADDRNOTAVAIL: return "can't assign requested address";
148 case EADV: return "advertise error";
151 case EAFNOSUPPORT: return "address family not supported by protocol family";
154 case EAGAIN: return "try again";
157 case EALIGN: return "EALIGN";
160 case EALREADY: return "operation already in progress";
163 case EBADE: return "bad exchange descriptor";
166 case EBADF: return "bad file number";
169 case EBADFD: return "file descriptor in bad state";
172 case EBADMSG: return "not a data message";
175 case EBADR: return "bad request descriptor";
178 case EBADRPC: return "RPC structure is bad";
181 case EBADRQC: return "bad request code";
184 case EBADSLT: return "invalid slot";
187 case EBFONT: return "bad font file format";
190 case EBUSY: return "mount device busy";
193 case ECHILD: return "no children";
196 case ECHRNG: return "channel number out of range";
199 case ECOMM: return "communication error on send";
202 case ECONNABORTED: return "software caused connection abort";
205 case ECONNREFUSED: return "connection refused";
208 case ECONNRESET: return "connection reset by peer";
210 #if defined(EDEADLK) && (!defined(EWOULDBLOCK) || (EDEADLK != EWOULDBLOCK))
211 case EDEADLK: return "resource deadlock avoided";
214 case EDEADLOCK: return "resource deadlock avoided";
217 case EDESTADDRREQ: return "destination address required";
220 case EDIRTY: return "mounting a dirty fs w/o force";
223 case EDOM: return "math argument out of range";
226 case EDOTDOT: return "cross mount point";
229 case EDQUOT: return "disk quota exceeded";
232 case EDUPPKG: return "duplicate package name";
235 case EEXIST: return "file already exists";
238 case EFAULT: return "bad address in system call argument";
241 case EFBIG: return "file too large";
244 case EHOSTDOWN: return "host is down";
247 case EHOSTUNREACH: return "host is unreachable";
250 case EIDRM: return "identifier removed";
253 case EINIT: return "initialization error";
256 case EINPROGRESS: return "operation now in progress";
259 case EINTR: return "interrupted system call";
262 case EINVAL: return "invalid argument";
265 case EIO: return "I/O error";
268 case EISCONN: return "socket is already connected";
271 case EISDIR: return "illegal operation on a directory";
274 case EISNAM: return "is a name file";
277 case ELBIN: return "ELBIN";
280 case EL2HLT: return "level 2 halted";
283 case EL2NSYNC: return "level 2 not synchronized";
286 case EL3HLT: return "level 3 halted";
289 case EL3RST: return "level 3 reset";
292 case ELIBACC: return "can not access a needed shared library";
295 case ELIBBAD: return "accessing a corrupted shared library";
298 case ELIBEXEC: return "can not exec a shared library directly";
301 case ELIBMAX: return "attempting to link in more shared libraries than system limit";
304 case ELIBSCN: return ".lib section in a.out corrupted";
307 case ELNRNG: return "link number out of range";
310 case ELOOP: return "too many levels of symbolic links";
313 case EMFILE: return "too many open files";
316 case EMLINK: return "too many links";
319 case EMSGSIZE: return "message too long";
322 case EMULTIHOP: return "multihop attempted";
325 case ENAMETOOLONG: return "file name too long";
328 case ENAVAIL: return "not available";
331 case ENET: return "ENET";
334 case ENETDOWN: return "network is down";
337 case ENETRESET: return "network dropped connection on reset";
340 case ENETUNREACH: return "network is unreachable";
343 case ENFILE: return "file table overflow";
346 case ENOANO: return "anode table overflow";
348 #if defined(ENOBUFS) && (!defined(ENOSR) || (ENOBUFS != ENOSR))
349 case ENOBUFS: return "no buffer space available";
352 case ENOCSI: return "no CSI structure available";
355 case ENODATA: return "no data available";
358 case ENODEV: return "no such device";
361 case ENOENT: return "no such file or directory";
364 case ENOEXEC: return "exec format error";
367 case ENOLCK: return "no locks available";
370 case ENOLINK: return "link has be severed";
373 case ENOMEM: return "not enough memory";
376 case ENOMSG: return "no message of desired type";
379 case ENONET: return "machine is not on the network";
382 case ENOPKG: return "package not installed";
385 case ENOPROTOOPT: return "bad proocol option";
388 case ENOSPC: return "no space left on device";
391 case ENOSR: return "out of stream resources";
394 case ENOSTR: return "not a stream device";
397 case ENOSYM: return "unresolved symbol name";
400 case ENOSYS: return "function not implemented";
403 case ENOTBLK: return "block device required";
406 case ENOTCONN: return "socket is not connected";
409 case ENOTDIR: return "not a directory";
412 case ENOTEMPTY: return "directory not empty";
415 case ENOTNAM: return "not a name file";
418 case ENOTSOCK: return "socket operation on non-socket";
421 case ENOTTY: return "inappropriate device for ioctl";
424 case ENOTUNIQ: return "name not unique on network";
427 case ENXIO: return "no such device or address";
430 case EOPNOTSUPP: return "operation not supported on socket";
433 case EPERM: return "not owner";
436 case EPFNOSUPPORT: return "protocol family not supported";
439 case EPIPE: return "broken pipe";
442 case EPROCLIM: return "too many processes";
445 case EPROCUNAVAIL: return "bad procedure for program";
448 case EPROGMISMATCH: return "program version wrong";
451 case EPROGUNAVAIL: return "RPC program not available";
454 case EPROTO: return "protocol error";
456 #ifdef EPROTONOSUPPORT
457 case EPROTONOSUPPORT: return "protocol not suppored";
460 case EPROTOTYPE: return "protocol wrong type for socket";
463 case ERANGE: return "math result unrepresentable";
465 #if defined(EREFUSED) && (!defined(ECONNREFUSED) || (EREFUSED != ECONNREFUSED))
466 case EREFUSED: return "EREFUSED";
469 case EREMCHG: return "remote address changed";
472 case EREMDEV: return "remote device";
475 case EREMOTE: return "pathname hit remote file system";
478 case EREMOTEIO: return "remote i/o error";
480 #ifdef EREMOTERELEASE
481 case EREMOTERELEASE: return "EREMOTERELEASE";
484 case EROFS: return "read-only file system";
487 case ERPCMISMATCH: return "RPC version is wrong";
490 case ERREMOTE: return "object is remote";
493 case ESHUTDOWN: return "can't send afer socket shutdown";
495 #ifdef ESOCKTNOSUPPORT
496 case ESOCKTNOSUPPORT: return "socket type not supported";
499 case ESPIPE: return "invalid seek";
502 case ESRCH: return "no such process";
505 case ESRMNT: return "srmount error";
508 case ESTALE: return "stale remote file handle";
511 case ESUCCESS: return "Error 0";
514 case ETIME: return "timer expired";
517 case ETIMEDOUT: return "connection timed out";
520 case ETOOMANYREFS: return "too many references: can't splice";
523 case ETXTBSY: return "text file or pseudo-device busy";
526 case EUCLEAN: return "structure needs cleaning";
529 case EUNATCH: return "protocol driver not attached";
532 case EUSERS: return "too many users";
535 case EVERSION: return "version mismatch";
537 #if defined(EWOULDBLOCK) && (!defined(EAGAIN) || (EWOULDBLOCK != EAGAIN))
538 case EWOULDBLOCK: return "operation would block";
541 case EXDEV: return "cross-domain link";
544 case EXFULL: return "message tables full";
547 #else /* NO_SYS_ERRLIST */
549 extern char *sys_errlist[];
551 if ((errnum > 0) && (errnum <= sys_nerr))
552 return sys_errlist [errnum];
553 #endif /* NO_SYS_ERRLIST */
555 sprintf (msg, "unknown error (%d)", errnum);
560 g_strsignal (gint signum)
564 #ifdef HAVE_STRSIGNAL
565 extern char *strsignal (int sig);
566 return strsignal (signum);
571 case SIGHUP: return "Hangup";
574 case SIGINT: return "Interrupt";
577 case SIGQUIT: return "Quit";
580 case SIGILL: "Illegal instruction";
583 case SIGTRAP: "Trace/breakpoint trap";
586 case SIGABRT: "IOT trap/Abort";
589 case SIGBUS: "Bus error";
592 case SIGFPE: "Floating point exception";
595 case SIGKILL: "Killed";
598 case SIGUSR1: "User defined signal 1";
601 case SIGSEGV: "Segmentation fault";
604 case SIGUSR2: "User defined signal 2";
607 case SIGPIPE: "Broken pipe";
610 case SIGALRM: "Alarm clock";
613 case SIGTERM: "Terminated";
616 case SIGSTKFLT: "Stack fault";
619 case SIGCHLD: "Child exited";
622 case SIGCONT: "Continued";
625 case SIGSTOP: "Stopped (signal)";
628 case SIGTSTP: "Stopped";
631 case SIGTTIN: "Stopped (tty input)";
634 case SIGTTOU: "Stopped (tty output)";
637 case SIGURG: "Urgent condition";
640 case SIGXCPU: "CPU time limit exceeded";
643 case SIGXFSZ: "File size limit exceeded";
646 case SIGVTALRM: "Virtual time alarm";
649 case SIGPROF: "Profile signal";
652 case SIGWINCH: "Window size changed";
655 case SIGIO: "Possible I/O";
658 case SIGPWR: "Power failure";
661 case SIGUNUSED: return "Unused signal";
664 #else /* NO_SYS_SIGLIST */
665 extern char *sys_siglist[];
666 return sys_siglist [signum];
667 #endif /* NO_SYS_SIGLIST */
669 sprintf (msg, "unknown signal (%d)", signum);
674 g_error (gchar *format, ...)
679 va_start (args, format);
680 va_start (args2, format);
681 buf = g_vsprintf (format, &args, &args2);
687 (* glib_error_func) (buf);
691 fputs ("\n** ERROR **: ", stderr);
693 fputc ('\n', stderr);
700 g_warning (gchar *format, ...)
705 va_start (args, format);
706 va_start (args2, format);
707 buf = g_vsprintf (format, &args, &args2);
711 if (glib_warning_func)
713 (* glib_warning_func) (buf);
717 fputs ("\n** WARNING **: ", stderr);
719 fputc ('\n', stderr);
724 g_message (gchar *format, ...)
729 va_start (args, format);
730 va_start (args2, format);
731 buf = g_vsprintf (format, &args, &args2);
735 if (glib_message_func)
737 (* glib_message_func) (buf);
741 fputs ("message: ", stdout);
743 fputc ('\n', stdout);
748 g_print (gchar *format, ...)
753 va_start (args, format);
754 va_start (args2, format);
755 buf = g_vsprintf (format, &args, &args2);
761 (* glib_print_func) (buf);
770 g_set_error_handler (GErrorFunc func)
772 GErrorFunc old_error_func;
774 old_error_func = glib_error_func;
775 glib_error_func = func;
777 return old_error_func;
781 g_set_warning_handler (GWarningFunc func)
783 GWarningFunc old_warning_func;
785 old_warning_func = glib_warning_func;
786 glib_warning_func = func;
788 return old_warning_func;
792 g_set_message_handler (GPrintFunc func)
794 GPrintFunc old_message_func;
796 old_message_func = glib_message_func;
797 glib_message_func = func;
799 return old_message_func;
803 g_set_print_handler (GPrintFunc func)
805 GPrintFunc old_print_func;
807 old_print_func = glib_print_func;
808 glib_print_func = func;
810 return old_print_func;
814 g_snprintf (gchar *str,
819 #ifdef HAVE_VSNPRINTF
823 va_start (args, fmt);
824 retval = vsnprintf (str, n, fmt, args);
833 va_start (args, fmt);
834 va_start (args2, fmt);
836 printed = g_vsprintf (fmt, &args, &args2);
837 strncpy (str, printed, n);
849 g_strcasecmp (const gchar *s1, const gchar *s2)
851 #ifdef HAVE_STRCASECMP
852 return strcasecmp(s1, s2);
858 c1 = tolower((guchar)(*s1++)); c2 = tolower((guchar)(*s2++));
863 return (((gint)(guchar) *s1) - ((gint)(guchar) *s2));
868 g_parse_debug_string (const gchar *string,
875 g_return_val_if_fail (string != NULL, 0);
877 if (!g_strcasecmp (string, "all"))
879 for (i=0; i<nkeys; i++)
880 result |= keys[i].value;
884 gchar *str = g_strdup (string);
887 gboolean done = FALSE;
900 for (i=0; i<nkeys; i++)
901 if (!g_strcasecmp(keys[i].key, p))
902 result |= keys[i].value;
914 g_direct_hash(gpointer key)