Updates to Tomato RAF including NGINX && PHP
[tomato.git] / release / src / router / glib / gstrfuncs.c
blobf89e8679b70ecc98eba6f3e30318a25756c0a9f4
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.
21 * Modified by the GLib Team and others 1997-1999. See the AUTHORS
22 * file for a list of people on the GLib Team. See the ChangeLog
23 * files for a list of changes. These files are distributed with
24 * GLib at ftp://ftp.gtk.org/pub/gtk/.
28 * MT safe
31 #ifdef HAVE_CONFIG_H
32 #include <config.h>
33 #endif
35 #include <stdarg.h>
36 #include <stdio.h>
37 #include <stdlib.h>
38 #include <string.h>
39 #include <locale.h>
40 #include <errno.h>
41 #include <ctype.h> /* For tolower() */
42 #if !defined (HAVE_STRSIGNAL) || !defined(NO_SYS_SIGLIST_DECL)
43 #include <signal.h>
44 #endif
45 #include "glib.h"
46 /* do not include <unistd.h> in this place since it
47 * inteferes with g_strsignal() on some OSes
50 #define G_GNUC_PRETTY_FUNCTION
52 typedef union _GDoubleIEEE754 GDoubleIEEE754;
53 #define G_IEEE754_DOUBLE_BIAS (1023)
54 /* multiply with base2 exponent to get base10 exponent (nomal numbers) */
55 #define G_LOG_2_BASE_10 (0.30102999566398119521)
56 #if G_BYTE_ORDER == G_LITTLE_ENDIAN
57 union _GDoubleIEEE754
59 gdouble v_double;
60 struct {
61 guint mantissa_low : 32;
62 guint mantissa_high : 20;
63 guint biased_exponent : 11;
64 guint sign : 1;
65 } mpn;
67 #elif G_BYTE_ORDER == G_BIG_ENDIAN
68 union _GDoubleIEEE754
70 gdouble v_double;
71 struct {
72 guint sign : 1;
73 guint biased_exponent : 11;
74 guint mantissa_high : 20;
75 guint mantissa_low : 32;
76 } mpn;
78 #else /* !G_LITTLE_ENDIAN && !G_BIG_ENDIAN */
79 #error unknown ENDIAN type
80 #endif /* !G_LITTLE_ENDIAN && !G_BIG_ENDIAN */
82 gchar*
83 g_strdup (const gchar *str)
85 gchar *new_str;
87 if (str)
89 new_str = g_new (char, strlen (str) + 1);
90 strcpy (new_str, str);
92 else
93 new_str = NULL;
95 return new_str;
98 gpointer
99 g_memdup (gconstpointer mem,
100 guint byte_size)
102 gpointer new_mem;
104 if (mem)
106 new_mem = g_malloc (byte_size);
107 memcpy (new_mem, mem, byte_size);
109 else
110 new_mem = NULL;
112 return new_mem;
115 gchar*
116 g_strndup (const gchar *str,
117 guint n)
119 gchar *new_str;
121 if (str)
123 new_str = g_new (gchar, n + 1);
124 strncpy (new_str, str, n);
125 new_str[n] = '\0';
127 else
128 new_str = NULL;
130 return new_str;
133 gchar*
134 g_strnfill (guint length,
135 gchar fill_char)
137 register gchar *str, *s, *end;
139 str = g_new (gchar, length + 1);
140 s = str;
141 end = str + length;
142 while (s < end)
143 *(s++) = fill_char;
144 *s = 0;
146 return str;
149 gchar*
150 g_strdup_vprintf (const gchar *format,
151 va_list args1)
153 gchar *buffer;
154 va_list args2;
156 G_VA_COPY (args2, args1);
158 buffer = g_new (gchar, g_printf_string_upper_bound (format, args1));
160 vsprintf (buffer, format, args2);
161 va_end (args2);
163 return buffer;
166 gchar*
167 g_strdup_printf (const gchar *format,
168 ...)
170 gchar *buffer;
171 va_list args;
173 va_start (args, format);
174 buffer = g_strdup_vprintf (format, args);
175 va_end (args);
177 return buffer;
180 gchar*
181 g_strconcat (const gchar *string1, ...)
183 guint l;
184 va_list args;
185 gchar *s;
186 gchar *concat;
188 g_return_val_if_fail (string1 != NULL, NULL);
190 l = 1 + strlen (string1);
191 va_start (args, string1);
192 s = va_arg (args, gchar*);
193 while (s)
195 l += strlen (s);
196 s = va_arg (args, gchar*);
198 va_end (args);
200 concat = g_new (gchar, l);
201 concat[0] = 0;
203 strcat (concat, string1);
204 va_start (args, string1);
205 s = va_arg (args, gchar*);
206 while (s)
208 strcat (concat, s);
209 s = va_arg (args, gchar*);
211 va_end (args);
213 return concat;
216 gdouble
217 g_strtod (const gchar *nptr,
218 gchar **endptr)
220 gchar *fail_pos_1;
221 gchar *fail_pos_2;
222 gdouble val_1;
223 gdouble val_2 = 0;
225 g_return_val_if_fail (nptr != NULL, 0);
227 fail_pos_1 = NULL;
228 fail_pos_2 = NULL;
230 val_1 = strtod (nptr, &fail_pos_1);
232 if (fail_pos_1 && fail_pos_1[0] != 0)
234 gchar *old_locale;
236 old_locale = g_strdup (setlocale (LC_NUMERIC, NULL));
237 setlocale (LC_NUMERIC, "C");
238 val_2 = strtod (nptr, &fail_pos_2);
239 setlocale (LC_NUMERIC, old_locale);
240 g_free (old_locale);
243 if (!fail_pos_1 || fail_pos_1[0] == 0 || fail_pos_1 >= fail_pos_2)
245 if (endptr)
246 *endptr = fail_pos_1;
247 return val_1;
249 else
251 if (endptr)
252 *endptr = fail_pos_2;
253 return val_2;
257 gchar*
258 g_strerror (gint errnum)
260 static GStaticPrivate msg_private = G_STATIC_PRIVATE_INIT;
261 char *msg;
263 #ifdef HAVE_STRERROR
264 return strerror (errnum);
265 #elif NO_SYS_ERRLIST
266 switch (errnum)
268 #ifdef E2BIG
269 case E2BIG: return "argument list too long";
270 #endif
271 #ifdef EACCES
272 case EACCES: return "permission denied";
273 #endif
274 #ifdef EADDRINUSE
275 case EADDRINUSE: return "address already in use";
276 #endif
277 #ifdef EADDRNOTAVAIL
278 case EADDRNOTAVAIL: return "can't assign requested address";
279 #endif
280 #ifdef EADV
281 case EADV: return "advertise error";
282 #endif
283 #ifdef EAFNOSUPPORT
284 case EAFNOSUPPORT: return "address family not supported by protocol family";
285 #endif
286 #ifdef EAGAIN
287 case EAGAIN: return "try again";
288 #endif
289 #ifdef EALIGN
290 case EALIGN: return "EALIGN";
291 #endif
292 #ifdef EALREADY
293 case EALREADY: return "operation already in progress";
294 #endif
295 #ifdef EBADE
296 case EBADE: return "bad exchange descriptor";
297 #endif
298 #ifdef EBADF
299 case EBADF: return "bad file number";
300 #endif
301 #ifdef EBADFD
302 case EBADFD: return "file descriptor in bad state";
303 #endif
304 #ifdef EBADMSG
305 case EBADMSG: return "not a data message";
306 #endif
307 #ifdef EBADR
308 case EBADR: return "bad request descriptor";
309 #endif
310 #ifdef EBADRPC
311 case EBADRPC: return "RPC structure is bad";
312 #endif
313 #ifdef EBADRQC
314 case EBADRQC: return "bad request code";
315 #endif
316 #ifdef EBADSLT
317 case EBADSLT: return "invalid slot";
318 #endif
319 #ifdef EBFONT
320 case EBFONT: return "bad font file format";
321 #endif
322 #ifdef EBUSY
323 case EBUSY: return "mount device busy";
324 #endif
325 #ifdef ECHILD
326 case ECHILD: return "no children";
327 #endif
328 #ifdef ECHRNG
329 case ECHRNG: return "channel number out of range";
330 #endif
331 #ifdef ECOMM
332 case ECOMM: return "communication error on send";
333 #endif
334 #ifdef ECONNABORTED
335 case ECONNABORTED: return "software caused connection abort";
336 #endif
337 #ifdef ECONNREFUSED
338 case ECONNREFUSED: return "connection refused";
339 #endif
340 #ifdef ECONNRESET
341 case ECONNRESET: return "connection reset by peer";
342 #endif
343 #if defined(EDEADLK) && (!defined(EWOULDBLOCK) || (EDEADLK != EWOULDBLOCK))
344 case EDEADLK: return "resource deadlock avoided";
345 #endif
346 #ifdef EDEADLOCK
347 case EDEADLOCK: return "resource deadlock avoided";
348 #endif
349 #ifdef EDESTADDRREQ
350 case EDESTADDRREQ: return "destination address required";
351 #endif
352 #ifdef EDIRTY
353 case EDIRTY: return "mounting a dirty fs w/o force";
354 #endif
355 #ifdef EDOM
356 case EDOM: return "math argument out of range";
357 #endif
358 #ifdef EDOTDOT
359 case EDOTDOT: return "cross mount point";
360 #endif
361 #ifdef EDQUOT
362 case EDQUOT: return "disk quota exceeded";
363 #endif
364 #ifdef EDUPPKG
365 case EDUPPKG: return "duplicate package name";
366 #endif
367 #ifdef EEXIST
368 case EEXIST: return "file already exists";
369 #endif
370 #ifdef EFAULT
371 case EFAULT: return "bad address in system call argument";
372 #endif
373 #ifdef EFBIG
374 case EFBIG: return "file too large";
375 #endif
376 #ifdef EHOSTDOWN
377 case EHOSTDOWN: return "host is down";
378 #endif
379 #ifdef EHOSTUNREACH
380 case EHOSTUNREACH: return "host is unreachable";
381 #endif
382 #ifdef EIDRM
383 case EIDRM: return "identifier removed";
384 #endif
385 #ifdef EINIT
386 case EINIT: return "initialization error";
387 #endif
388 #ifdef EINPROGRESS
389 case EINPROGRESS: return "operation now in progress";
390 #endif
391 #ifdef EINTR
392 case EINTR: return "interrupted system call";
393 #endif
394 #ifdef EINVAL
395 case EINVAL: return "invalid argument";
396 #endif
397 #ifdef EIO
398 case EIO: return "I/O error";
399 #endif
400 #ifdef EISCONN
401 case EISCONN: return "socket is already connected";
402 #endif
403 #ifdef EISDIR
404 case EISDIR: return "illegal operation on a directory";
405 #endif
406 #ifdef EISNAME
407 case EISNAM: return "is a name file";
408 #endif
409 #ifdef ELBIN
410 case ELBIN: return "ELBIN";
411 #endif
412 #ifdef EL2HLT
413 case EL2HLT: return "level 2 halted";
414 #endif
415 #ifdef EL2NSYNC
416 case EL2NSYNC: return "level 2 not synchronized";
417 #endif
418 #ifdef EL3HLT
419 case EL3HLT: return "level 3 halted";
420 #endif
421 #ifdef EL3RST
422 case EL3RST: return "level 3 reset";
423 #endif
424 #ifdef ELIBACC
425 case ELIBACC: return "can not access a needed shared library";
426 #endif
427 #ifdef ELIBBAD
428 case ELIBBAD: return "accessing a corrupted shared library";
429 #endif
430 #ifdef ELIBEXEC
431 case ELIBEXEC: return "can not exec a shared library directly";
432 #endif
433 #ifdef ELIBMAX
434 case ELIBMAX: return "attempting to link in more shared libraries than system limit";
435 #endif
436 #ifdef ELIBSCN
437 case ELIBSCN: return ".lib section in a.out corrupted";
438 #endif
439 #ifdef ELNRNG
440 case ELNRNG: return "link number out of range";
441 #endif
442 #ifdef ELOOP
443 case ELOOP: return "too many levels of symbolic links";
444 #endif
445 #ifdef EMFILE
446 case EMFILE: return "too many open files";
447 #endif
448 #ifdef EMLINK
449 case EMLINK: return "too many links";
450 #endif
451 #ifdef EMSGSIZE
452 case EMSGSIZE: return "message too long";
453 #endif
454 #ifdef EMULTIHOP
455 case EMULTIHOP: return "multihop attempted";
456 #endif
457 #ifdef ENAMETOOLONG
458 case ENAMETOOLONG: return "file name too long";
459 #endif
460 #ifdef ENAVAIL
461 case ENAVAIL: return "not available";
462 #endif
463 #ifdef ENET
464 case ENET: return "ENET";
465 #endif
466 #ifdef ENETDOWN
467 case ENETDOWN: return "network is down";
468 #endif
469 #ifdef ENETRESET
470 case ENETRESET: return "network dropped connection on reset";
471 #endif
472 #ifdef ENETUNREACH
473 case ENETUNREACH: return "network is unreachable";
474 #endif
475 #ifdef ENFILE
476 case ENFILE: return "file table overflow";
477 #endif
478 #ifdef ENOANO
479 case ENOANO: return "anode table overflow";
480 #endif
481 #if defined(ENOBUFS) && (!defined(ENOSR) || (ENOBUFS != ENOSR))
482 case ENOBUFS: return "no buffer space available";
483 #endif
484 #ifdef ENOCSI
485 case ENOCSI: return "no CSI structure available";
486 #endif
487 #ifdef ENODATA
488 case ENODATA: return "no data available";
489 #endif
490 #ifdef ENODEV
491 case ENODEV: return "no such device";
492 #endif
493 #ifdef ENOENT
494 case ENOENT: return "no such file or directory";
495 #endif
496 #ifdef ENOEXEC
497 case ENOEXEC: return "exec format error";
498 #endif
499 #ifdef ENOLCK
500 case ENOLCK: return "no locks available";
501 #endif
502 #ifdef ENOLINK
503 case ENOLINK: return "link has be severed";
504 #endif
505 #ifdef ENOMEM
506 case ENOMEM: return "not enough memory";
507 #endif
508 #ifdef ENOMSG
509 case ENOMSG: return "no message of desired type";
510 #endif
511 #ifdef ENONET
512 case ENONET: return "machine is not on the network";
513 #endif
514 #ifdef ENOPKG
515 case ENOPKG: return "package not installed";
516 #endif
517 #ifdef ENOPROTOOPT
518 case ENOPROTOOPT: return "bad proocol option";
519 #endif
520 #ifdef ENOSPC
521 case ENOSPC: return "no space left on device";
522 #endif
523 #ifdef ENOSR
524 case ENOSR: return "out of stream resources";
525 #endif
526 #ifdef ENOSTR
527 case ENOSTR: return "not a stream device";
528 #endif
529 #ifdef ENOSYM
530 case ENOSYM: return "unresolved symbol name";
531 #endif
532 #ifdef ENOSYS
533 case ENOSYS: return "function not implemented";
534 #endif
535 #ifdef ENOTBLK
536 case ENOTBLK: return "block device required";
537 #endif
538 #ifdef ENOTCONN
539 case ENOTCONN: return "socket is not connected";
540 #endif
541 #ifdef ENOTDIR
542 case ENOTDIR: return "not a directory";
543 #endif
544 #ifdef ENOTEMPTY
545 case ENOTEMPTY: return "directory not empty";
546 #endif
547 #ifdef ENOTNAM
548 case ENOTNAM: return "not a name file";
549 #endif
550 #ifdef ENOTSOCK
551 case ENOTSOCK: return "socket operation on non-socket";
552 #endif
553 #ifdef ENOTTY
554 case ENOTTY: return "inappropriate device for ioctl";
555 #endif
556 #ifdef ENOTUNIQ
557 case ENOTUNIQ: return "name not unique on network";
558 #endif
559 #ifdef ENXIO
560 case ENXIO: return "no such device or address";
561 #endif
562 #ifdef EOPNOTSUPP
563 case EOPNOTSUPP: return "operation not supported on socket";
564 #endif
565 #ifdef EPERM
566 case EPERM: return "not owner";
567 #endif
568 #ifdef EPFNOSUPPORT
569 case EPFNOSUPPORT: return "protocol family not supported";
570 #endif
571 #ifdef EPIPE
572 case EPIPE: return "broken pipe";
573 #endif
574 #ifdef EPROCLIM
575 case EPROCLIM: return "too many processes";
576 #endif
577 #ifdef EPROCUNAVAIL
578 case EPROCUNAVAIL: return "bad procedure for program";
579 #endif
580 #ifdef EPROGMISMATCH
581 case EPROGMISMATCH: return "program version wrong";
582 #endif
583 #ifdef EPROGUNAVAIL
584 case EPROGUNAVAIL: return "RPC program not available";
585 #endif
586 #ifdef EPROTO
587 case EPROTO: return "protocol error";
588 #endif
589 #ifdef EPROTONOSUPPORT
590 case EPROTONOSUPPORT: return "protocol not suppored";
591 #endif
592 #ifdef EPROTOTYPE
593 case EPROTOTYPE: return "protocol wrong type for socket";
594 #endif
595 #ifdef ERANGE
596 case ERANGE: return "math result unrepresentable";
597 #endif
598 #if defined(EREFUSED) && (!defined(ECONNREFUSED) || (EREFUSED != ECONNREFUSED))
599 case EREFUSED: return "EREFUSED";
600 #endif
601 #ifdef EREMCHG
602 case EREMCHG: return "remote address changed";
603 #endif
604 #ifdef EREMDEV
605 case EREMDEV: return "remote device";
606 #endif
607 #ifdef EREMOTE
608 case EREMOTE: return "pathname hit remote file system";
609 #endif
610 #ifdef EREMOTEIO
611 case EREMOTEIO: return "remote i/o error";
612 #endif
613 #ifdef EREMOTERELEASE
614 case EREMOTERELEASE: return "EREMOTERELEASE";
615 #endif
616 #ifdef EROFS
617 case EROFS: return "read-only file system";
618 #endif
619 #ifdef ERPCMISMATCH
620 case ERPCMISMATCH: return "RPC version is wrong";
621 #endif
622 #ifdef ERREMOTE
623 case ERREMOTE: return "object is remote";
624 #endif
625 #ifdef ESHUTDOWN
626 case ESHUTDOWN: return "can't send afer socket shutdown";
627 #endif
628 #ifdef ESOCKTNOSUPPORT
629 case ESOCKTNOSUPPORT: return "socket type not supported";
630 #endif
631 #ifdef ESPIPE
632 case ESPIPE: return "invalid seek";
633 #endif
634 #ifdef ESRCH
635 case ESRCH: return "no such process";
636 #endif
637 #ifdef ESRMNT
638 case ESRMNT: return "srmount error";
639 #endif
640 #ifdef ESTALE
641 case ESTALE: return "stale remote file handle";
642 #endif
643 #ifdef ESUCCESS
644 case ESUCCESS: return "Error 0";
645 #endif
646 #ifdef ETIME
647 case ETIME: return "timer expired";
648 #endif
649 #ifdef ETIMEDOUT
650 case ETIMEDOUT: return "connection timed out";
651 #endif
652 #ifdef ETOOMANYREFS
653 case ETOOMANYREFS: return "too many references: can't splice";
654 #endif
655 #ifdef ETXTBSY
656 case ETXTBSY: return "text file or pseudo-device busy";
657 #endif
658 #ifdef EUCLEAN
659 case EUCLEAN: return "structure needs cleaning";
660 #endif
661 #ifdef EUNATCH
662 case EUNATCH: return "protocol driver not attached";
663 #endif
664 #ifdef EUSERS
665 case EUSERS: return "too many users";
666 #endif
667 #ifdef EVERSION
668 case EVERSION: return "version mismatch";
669 #endif
670 #if defined(EWOULDBLOCK) && (!defined(EAGAIN) || (EWOULDBLOCK != EAGAIN))
671 case EWOULDBLOCK: return "operation would block";
672 #endif
673 #ifdef EXDEV
674 case EXDEV: return "cross-domain link";
675 #endif
676 #ifdef EXFULL
677 case EXFULL: return "message tables full";
678 #endif
680 #else /* NO_SYS_ERRLIST */
681 extern int sys_nerr;
682 extern char *sys_errlist[];
684 if ((errnum > 0) && (errnum <= sys_nerr))
685 return sys_errlist [errnum];
686 #endif /* NO_SYS_ERRLIST */
688 msg = g_static_private_get (&msg_private);
689 if (!msg)
691 msg = g_new (gchar, 64);
692 g_static_private_set (&msg_private, msg, g_free);
695 sprintf (msg, "unknown error (%d)", errnum);
697 return msg;
700 gchar*
701 g_strsignal (gint signum)
703 static GStaticPrivate msg_private = G_STATIC_PRIVATE_INIT;
704 char *msg;
706 #ifdef HAVE_STRSIGNAL
707 extern char *strsignal (int sig);
708 return strsignal (signum);
709 #elif NO_SYS_SIGLIST
710 switch (signum)
712 #ifdef SIGHUP
713 case SIGHUP: return "Hangup";
714 #endif
715 #ifdef SIGINT
716 case SIGINT: return "Interrupt";
717 #endif
718 #ifdef SIGQUIT
719 case SIGQUIT: return "Quit";
720 #endif
721 #ifdef SIGILL
722 case SIGILL: return "Illegal instruction";
723 #endif
724 #ifdef SIGTRAP
725 case SIGTRAP: return "Trace/breakpoint trap";
726 #endif
727 #ifdef SIGABRT
728 case SIGABRT: return "IOT trap/Abort";
729 #endif
730 #ifdef SIGBUS
731 case SIGBUS: return "Bus error";
732 #endif
733 #ifdef SIGFPE
734 case SIGFPE: return "Floating point exception";
735 #endif
736 #ifdef SIGKILL
737 case SIGKILL: return "Killed";
738 #endif
739 #ifdef SIGUSR1
740 case SIGUSR1: return "User defined signal 1";
741 #endif
742 #ifdef SIGSEGV
743 case SIGSEGV: return "Segmentation fault";
744 #endif
745 #ifdef SIGUSR2
746 case SIGUSR2: return "User defined signal 2";
747 #endif
748 #ifdef SIGPIPE
749 case SIGPIPE: return "Broken pipe";
750 #endif
751 #ifdef SIGALRM
752 case SIGALRM: return "Alarm clock";
753 #endif
754 #ifdef SIGTERM
755 case SIGTERM: return "Terminated";
756 #endif
757 #ifdef SIGSTKFLT
758 case SIGSTKFLT: return "Stack fault";
759 #endif
760 #ifdef SIGCHLD
761 case SIGCHLD: return "Child exited";
762 #endif
763 #ifdef SIGCONT
764 case SIGCONT: return "Continued";
765 #endif
766 #ifdef SIGSTOP
767 case SIGSTOP: return "Stopped (signal)";
768 #endif
769 #ifdef SIGTSTP
770 case SIGTSTP: return "Stopped";
771 #endif
772 #ifdef SIGTTIN
773 case SIGTTIN: return "Stopped (tty input)";
774 #endif
775 #ifdef SIGTTOU
776 case SIGTTOU: return "Stopped (tty output)";
777 #endif
778 #ifdef SIGURG
779 case SIGURG: return "Urgent condition";
780 #endif
781 #ifdef SIGXCPU
782 case SIGXCPU: return "CPU time limit exceeded";
783 #endif
784 #ifdef SIGXFSZ
785 case SIGXFSZ: return "File size limit exceeded";
786 #endif
787 #ifdef SIGVTALRM
788 case SIGVTALRM: return "Virtual time alarm";
789 #endif
790 #ifdef SIGPROF
791 case SIGPROF: return "Profile signal";
792 #endif
793 #ifdef SIGWINCH
794 case SIGWINCH: return "Window size changed";
795 #endif
796 #ifdef SIGIO
797 case SIGIO: return "Possible I/O";
798 #endif
799 #ifdef SIGPWR
800 case SIGPWR: return "Power failure";
801 #endif
802 #ifdef SIGUNUSED
803 case SIGUNUSED: return "Unused signal";
804 #endif
806 #else /* NO_SYS_SIGLIST */
808 #ifdef NO_SYS_SIGLIST_DECL
809 extern char *sys_siglist[]; /*(see Tue Jan 19 00:44:24 1999 in changelog)*/
810 #endif
812 return (char*) /* this function should return const --josh */ sys_siglist [signum];
813 #endif /* NO_SYS_SIGLIST */
815 msg = g_static_private_get (&msg_private);
816 if (!msg)
818 msg = g_new (gchar, 64);
819 g_static_private_set (&msg_private, msg, g_free);
822 sprintf (msg, "unknown signal (%d)", signum);
824 return msg;
827 typedef struct
829 guint min_width;
830 guint precision;
831 gboolean alternate_format, zero_padding, adjust_left, locale_grouping;
832 gboolean add_space, add_sign, possible_sign, seen_precision;
833 gboolean mod_half, mod_long, mod_extra_long;
834 } PrintfArgSpec;
836 guint
837 g_printf_string_upper_bound (const gchar* format,
838 va_list args)
840 static const gboolean honour_longs = SIZEOF_LONG > 4 || SIZEOF_VOID_P > 4;
841 guint len = 1;
843 if (!format)
844 return len;
846 while (*format)
848 register gchar c = *format++;
850 if (c != '%')
851 len += 1;
852 else /* (c == '%') */
854 PrintfArgSpec spec = { 0, };
855 gboolean seen_l = FALSE, conv_done = FALSE;
856 guint conv_len = 0;
857 const gchar *spec_start = format;
861 c = *format++;
862 switch (c)
864 GDoubleIEEE754 u_double;
865 guint v_uint;
866 gint v_int;
867 const gchar *v_string;
869 /* beware of positional parameters
871 case '$':
872 g_warning (G_GNUC_PRETTY_FUNCTION
873 "(): unable to handle positional parameters (%%n$)");
874 len += 1024; /* try adding some safety padding */
875 break;
877 /* parse flags
879 case '#':
880 spec.alternate_format = TRUE;
881 break;
882 case '0':
883 spec.zero_padding = TRUE;
884 break;
885 case '-':
886 spec.adjust_left = TRUE;
887 break;
888 case ' ':
889 spec.add_space = TRUE;
890 break;
891 case '+':
892 spec.add_sign = TRUE;
893 break;
894 case '\'':
895 spec.locale_grouping = TRUE;
896 break;
898 /* parse output size specifications
900 case '.':
901 spec.seen_precision = TRUE;
902 break;
903 case '1':
904 case '2':
905 case '3':
906 case '4':
907 case '5':
908 case '6':
909 case '7':
910 case '8':
911 case '9':
912 v_uint = c - '0';
913 c = *format;
914 while (c >= '0' && c <= '9')
916 format++;
917 v_uint = v_uint * 10 + c - '0';
918 c = *format;
920 if (spec.seen_precision)
921 spec.precision = MAX (spec.precision, v_uint);
922 else
923 spec.min_width = MAX (spec.min_width, v_uint);
924 break;
925 case '*':
926 v_int = va_arg (args, int);
927 if (spec.seen_precision)
929 /* forget about negative precision */
930 if (v_int >= 0)
931 spec.precision = MAX (spec.precision, v_int);
933 else
935 if (v_int < 0)
937 v_int = - v_int;
938 spec.adjust_left = TRUE;
940 spec.min_width = MAX (spec.min_width, v_int);
942 break;
944 /* parse type modifiers
946 case 'h':
947 spec.mod_half = TRUE;
948 break;
949 case 'l':
950 if (!seen_l)
952 spec.mod_long = TRUE;
953 seen_l = TRUE;
954 break;
956 /* else, fall through */
957 case 'L':
958 case 'q':
959 spec.mod_long = TRUE;
960 spec.mod_extra_long = TRUE;
961 break;
962 case 'z':
963 case 'Z':
964 #if GLIB_SIZEOF_SIZE_T > 4
965 spec.mod_long = TRUE;
966 spec.mod_extra_long = TRUE;
967 #endif /* GLIB_SIZEOF_SIZE_T > 4 */
968 break;
969 case 't':
970 #if GLIB_SIZEOF_PTRDIFF_T > 4
971 spec.mod_long = TRUE;
972 spec.mod_extra_long = TRUE;
973 #endif /* GLIB_SIZEOF_PTRDIFF_T > 4 */
974 break;
975 case 'j':
976 #if GLIB_SIZEOF_INTMAX_T > 4
977 spec.mod_long = TRUE;
978 spec.mod_extra_long = TRUE;
979 #endif /* GLIB_SIZEOF_INTMAX_T > 4 */
980 break;
982 /* parse output conversions
984 case '%':
985 conv_len += 1;
986 break;
987 case 'O':
988 case 'D':
989 case 'I':
990 case 'U':
991 /* some C libraries feature long variants for these as well? */
992 spec.mod_long = TRUE;
993 /* fall through */
994 case 'o':
995 conv_len += 2;
996 /* fall through */
997 case 'd':
998 case 'i':
999 conv_len += 1; /* sign */
1000 /* fall through */
1001 case 'u':
1002 conv_len += 4;
1003 /* fall through */
1004 case 'x':
1005 case 'X':
1006 spec.possible_sign = TRUE;
1007 conv_len += 10;
1008 if (spec.mod_long && honour_longs)
1009 conv_len *= 2;
1010 if (spec.mod_extra_long)
1011 conv_len *= 2;
1012 if (spec.mod_extra_long)
1014 #ifdef G_HAVE_GINT64
1015 (void) va_arg (args, gint64);
1016 #else /* !G_HAVE_GINT64 */
1017 (void) va_arg (args, long);
1018 #endif /* !G_HAVE_GINT64 */
1020 else if (spec.mod_long)
1021 (void) va_arg (args, long);
1022 else
1023 (void) va_arg (args, int);
1024 break;
1025 case 'A':
1026 case 'a':
1027 /* 0x */
1028 conv_len += 2;
1029 /* fall through */
1030 case 'g':
1031 case 'G':
1032 case 'e':
1033 case 'E':
1034 case 'f':
1035 spec.possible_sign = TRUE;
1036 /* n . dddddddddddddddddddddddd E +- eeee */
1037 conv_len += 1 + 1 + MAX (24, spec.precision) + 1 + 1 + 4;
1038 if (spec.mod_extra_long)
1039 g_warning (G_GNUC_PRETTY_FUNCTION
1040 "(): unable to handle long double, collecting double only");
1041 #ifdef HAVE_LONG_DOUBLE
1042 #error need to implement special handling for long double
1043 #endif
1044 u_double.v_double = va_arg (args, double);
1045 /* %f can expand up to all significant digits before '.' (308) */
1046 if (c == 'f' &&
1047 u_double.mpn.biased_exponent > 0 && u_double.mpn.biased_exponent < 2047)
1049 gint exp = u_double.mpn.biased_exponent;
1051 exp -= G_IEEE754_DOUBLE_BIAS;
1052 exp = exp * G_LOG_2_BASE_10 + 1;
1053 conv_len += exp;
1055 /* some printf() implementations require extra padding for rounding */
1056 conv_len += 2;
1057 /* we can't really handle locale specific grouping here */
1058 if (spec.locale_grouping)
1059 conv_len *= 2;
1060 break;
1061 case 'C':
1062 spec.mod_long = TRUE;
1063 /* fall through */
1064 case 'c':
1065 conv_len += spec.mod_long ? MB_LEN_MAX : 1;
1066 (void) va_arg (args, int);
1067 break;
1068 case 'S':
1069 spec.mod_long = TRUE;
1070 /* fall through */
1071 case 's':
1072 v_string = va_arg (args, char*);
1073 if (!v_string)
1074 conv_len += 8; /* hold "(null)" */
1075 else if (spec.seen_precision)
1076 conv_len += spec.precision;
1077 else
1078 conv_len += strlen (v_string);
1079 conv_done = TRUE;
1080 if (spec.mod_long)
1082 g_warning (G_GNUC_PRETTY_FUNCTION
1083 "(): unable to handle wide char strings");
1084 len += 1024; /* try adding some safety padding */
1086 break;
1087 case 'P': /* do we actually need this? */
1088 /* fall through */
1089 case 'p':
1090 spec.alternate_format = TRUE;
1091 conv_len += 10;
1092 if (honour_longs)
1093 conv_len *= 2;
1094 /* fall through */
1095 case 'n':
1096 conv_done = TRUE;
1097 (void) va_arg (args, void*);
1098 break;
1099 case 'm':
1100 /* there's not much we can do to be clever */
1101 v_string = g_strerror (errno);
1102 v_uint = v_string ? strlen (v_string) : 0;
1103 conv_len += MAX (256, v_uint);
1104 break;
1106 /* handle invalid cases
1108 case '\000':
1109 /* no conversion specification, bad bad */
1110 conv_len += format - spec_start;
1111 break;
1112 default:
1113 g_warning (G_GNUC_PRETTY_FUNCTION
1114 "(): unable to handle `%c' while parsing format",
1116 break;
1118 conv_done |= conv_len > 0;
1120 while (!conv_done);
1121 /* handle width specifications */
1122 conv_len = MAX (conv_len, MAX (spec.precision, spec.min_width));
1123 /* handle flags */
1124 conv_len += spec.alternate_format ? 2 : 0;
1125 conv_len += (spec.add_space || spec.add_sign || spec.possible_sign);
1126 /* finally done */
1127 len += conv_len;
1128 } /* else (c == '%') */
1129 } /* while (*format) */
1131 return len;
1134 void
1135 g_strdown (gchar *string)
1137 register guchar *s;
1139 g_return_if_fail (string != NULL);
1141 s = string;
1143 while (*s)
1145 *s = tolower (*s);
1146 s++;
1150 void
1151 g_strup (gchar *string)
1153 register guchar *s;
1155 g_return_if_fail (string != NULL);
1157 s = string;
1159 while (*s)
1161 *s = toupper (*s);
1162 s++;
1166 void
1167 g_strreverse (gchar *string)
1169 g_return_if_fail (string != NULL);
1171 if (*string)
1173 register gchar *h, *t;
1175 h = string;
1176 t = string + strlen (string) - 1;
1178 while (h < t)
1180 register gchar c;
1182 c = *h;
1183 *h = *t;
1184 h++;
1185 *t = c;
1186 t--;
1191 gint
1192 g_strcasecmp (const gchar *s1,
1193 const gchar *s2)
1195 #ifdef HAVE_STRCASECMP
1196 g_return_val_if_fail (s1 != NULL, 0);
1197 g_return_val_if_fail (s2 != NULL, 0);
1199 return strcasecmp (s1, s2);
1200 #else
1201 gint c1, c2;
1203 g_return_val_if_fail (s1 != NULL, 0);
1204 g_return_val_if_fail (s2 != NULL, 0);
1206 while (*s1 && *s2)
1208 /* According to A. Cox, some platforms have islower's that
1209 * don't work right on non-uppercase
1211 c1 = isupper ((guchar)*s1) ? tolower ((guchar)*s1) : *s1;
1212 c2 = isupper ((guchar)*s2) ? tolower ((guchar)*s2) : *s2;
1213 if (c1 != c2)
1214 return (c1 - c2);
1215 s1++; s2++;
1218 return (((gint)(guchar) *s1) - ((gint)(guchar) *s2));
1219 #endif
1222 gint
1223 g_strncasecmp (const gchar *s1,
1224 const gchar *s2,
1225 guint n)
1227 #ifdef HAVE_STRNCASECMP
1228 return strncasecmp (s1, s2, n);
1229 #else
1230 gint c1, c2;
1232 g_return_val_if_fail (s1 != NULL, 0);
1233 g_return_val_if_fail (s2 != NULL, 0);
1235 while (n && *s1 && *s2)
1237 n -= 1;
1238 /* According to A. Cox, some platforms have islower's that
1239 * don't work right on non-uppercase
1241 c1 = isupper ((guchar)*s1) ? tolower ((guchar)*s1) : *s1;
1242 c2 = isupper ((guchar)*s2) ? tolower ((guchar)*s2) : *s2;
1243 if (c1 != c2)
1244 return (c1 - c2);
1245 s1++; s2++;
1248 if (n)
1249 return (((gint) (guchar) *s1) - ((gint) (guchar) *s2));
1250 else
1251 return 0;
1252 #endif
1255 gchar*
1256 g_strdelimit (gchar *string,
1257 const gchar *delimiters,
1258 gchar new_delim)
1260 register gchar *c;
1262 g_return_val_if_fail (string != NULL, NULL);
1264 if (!delimiters)
1265 delimiters = G_STR_DELIMITERS;
1267 for (c = string; *c; c++)
1269 if (strchr (delimiters, *c))
1270 *c = new_delim;
1273 return string;
1276 gchar*
1277 g_strescape (gchar *string)
1279 gchar *q;
1280 gchar *escaped;
1281 guint backslashes = 0;
1282 gchar *p = string;
1284 g_return_val_if_fail (string != NULL, NULL);
1286 while (*p != '\000')
1287 backslashes += (*p++ == '\\');
1289 if (!backslashes)
1290 return g_strdup (string);
1292 escaped = g_new (gchar, strlen (string) + backslashes + 1);
1294 p = string;
1295 q = escaped;
1297 while (*p != '\000')
1299 if (*p == '\\')
1300 *q++ = '\\';
1301 *q++ = *p++;
1303 *q = '\000';
1305 return escaped;
1308 /* blame Elliot for these next five routines */
1309 gchar*
1310 g_strchug (gchar *string)
1312 guchar *start;
1314 g_return_val_if_fail (string != NULL, NULL);
1316 for (start = string; *start && isspace (*start); start++)
1319 g_memmove(string, start, strlen(start) + 1);
1321 return string;
1324 gchar*
1325 g_strchomp (gchar *string)
1327 gchar *s;
1329 g_return_val_if_fail (string != NULL, NULL);
1331 if (!*string)
1332 return string;
1334 for (s = string + strlen (string) - 1; s >= string && isspace ((guchar)*s);
1335 s--)
1336 *s = '\0';
1338 return string;
1341 gchar**
1342 g_strsplit (const gchar *string,
1343 const gchar *delimiter,
1344 gint max_tokens)
1346 GSList *string_list = NULL, *slist;
1347 gchar **str_array, *s;
1348 guint i, n = 1;
1350 g_return_val_if_fail (string != NULL, NULL);
1351 g_return_val_if_fail (delimiter != NULL, NULL);
1353 if (max_tokens < 1)
1354 max_tokens = G_MAXINT;
1356 s = strstr (string, delimiter);
1357 if (s)
1359 guint delimiter_len = strlen (delimiter);
1363 guint len;
1364 gchar *new_string;
1366 len = s - string;
1367 new_string = g_new (gchar, len + 1);
1368 strncpy (new_string, string, len);
1369 new_string[len] = 0;
1370 string_list = g_slist_prepend (string_list, new_string);
1371 n++;
1372 string = s + delimiter_len;
1373 s = strstr (string, delimiter);
1375 while (--max_tokens && s);
1377 if (*string)
1379 n++;
1380 string_list = g_slist_prepend (string_list, g_strdup (string));
1383 str_array = g_new (gchar*, n);
1385 i = n - 1;
1387 str_array[i--] = NULL;
1388 for (slist = string_list; slist; slist = slist->next)
1389 str_array[i--] = slist->data;
1391 g_slist_free (string_list);
1393 return str_array;
1396 void
1397 g_strfreev (gchar **str_array)
1399 if (str_array)
1401 int i;
1403 for(i = 0; str_array[i] != NULL; i++)
1404 g_free(str_array[i]);
1406 g_free (str_array);
1410 gchar*
1411 g_strjoinv (const gchar *separator,
1412 gchar **str_array)
1414 gchar *string;
1416 g_return_val_if_fail (str_array != NULL, NULL);
1418 if (separator == NULL)
1419 separator = "";
1421 if (*str_array)
1423 guint i, len;
1424 guint separator_len;
1426 separator_len = strlen (separator);
1427 len = 1 + strlen (str_array[0]);
1428 for(i = 1; str_array[i] != NULL; i++)
1429 len += separator_len + strlen(str_array[i]);
1431 string = g_new (gchar, len);
1432 *string = 0;
1433 strcat (string, *str_array);
1434 for (i = 1; str_array[i] != NULL; i++)
1436 strcat (string, separator);
1437 strcat (string, str_array[i]);
1440 else
1441 string = g_strdup ("");
1443 return string;
1446 gchar*
1447 g_strjoin (const gchar *separator,
1448 ...)
1450 gchar *string, *s;
1451 va_list args;
1452 guint len;
1453 guint separator_len;
1455 if (separator == NULL)
1456 separator = "";
1458 separator_len = strlen (separator);
1460 va_start (args, separator);
1462 s = va_arg (args, gchar*);
1464 if (s)
1466 len = strlen (s);
1468 s = va_arg (args, gchar*);
1469 while (s)
1471 len += separator_len + strlen (s);
1472 s = va_arg (args, gchar*);
1474 va_end (args);
1476 string = g_new (gchar, len + 1);
1477 *string = 0;
1479 va_start (args, separator);
1481 s = va_arg (args, gchar*);
1482 strcat (string, s);
1484 s = va_arg (args, gchar*);
1485 while (s)
1487 strcat (string, separator);
1488 strcat (string, s);
1489 s = va_arg (args, gchar*);
1492 else
1493 string = g_strdup ("");
1495 va_end (args);
1497 return string;