The "extern char* sys_siglist" declaration breaks systems with different
[platform/upstream/glib.git] / glib / gstrfuncs.c
1 /* GLIB - Library of useful routines for C programming
2  * Copyright (C) 1995-1997  Peter Mattis, Spencer Kimball and Josh MacDonald
3  *
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.
8  *
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.
13  *
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.
18  */
19
20 /*
21  * MT safe
22  */
23
24 #ifdef HAVE_CONFIG_H
25 #include <config.h>
26 #endif
27
28 #include <stdarg.h>
29 #include <stdio.h>
30 #include <stdlib.h>
31 #include <string.h>
32 #include <locale.h>
33 #include <ctype.h>              /* For tolower() */
34 #include <signal.h>
35 #include "glib.h"
36 /* do not include <unistd.h> in this place since it
37  * inteferes with g_strsignal() on some OSes
38  */
39
40 gchar*
41 g_strdup (const gchar *str)
42 {
43   gchar *new_str;
44
45   if (str)
46     {
47       new_str = g_new (char, strlen (str) + 1);
48       strcpy (new_str, str);
49     }
50   else
51     new_str = NULL;
52
53   return new_str;
54 }
55
56 gpointer
57 g_memdup (gconstpointer mem,
58           guint         byte_size)
59 {
60   gpointer new_mem;
61
62   if (mem)
63     {
64       new_mem = g_malloc (byte_size);
65       memcpy (new_mem, mem, byte_size);
66     }
67   else
68     new_mem = NULL;
69
70   return new_mem;
71 }
72
73 gchar*
74 g_strndup (const gchar *str,
75            guint        n)
76 {
77   gchar *new_str;
78
79   if (str)
80     {
81       new_str = g_new (gchar, n + 1);
82       strncpy (new_str, str, n);
83       new_str[n] = '\0';
84     }
85   else
86     new_str = NULL;
87
88   return new_str;
89 }
90
91 gchar*
92 g_strnfill (guint length,
93             gchar fill_char)
94 {
95   register gchar *str, *s, *end;
96
97   str = g_new (gchar, length + 1);
98   s = str;
99   end = str + length;
100   while (s < end)
101     *(s++) = fill_char;
102   *s = 0;
103
104   return str;
105 }
106
107 gchar*
108 g_strdup_vprintf (const gchar *format,
109                   va_list      args1)
110 {
111   gchar *buffer;
112   va_list args2;
113
114   G_VA_COPY (args2, args1);
115
116   buffer = g_new (gchar, g_printf_string_upper_bound (format, args1));
117
118   vsprintf (buffer, format, args2);
119   va_end (args2);
120
121   return buffer;
122 }
123
124 gchar*
125 g_strdup_printf (const gchar *format,
126                  ...)
127 {
128   gchar *buffer;
129   va_list args;
130
131   va_start (args, format);
132   buffer = g_strdup_vprintf (format, args);
133   va_end (args);
134
135   return buffer;
136 }
137
138 gchar*
139 g_strconcat (const gchar *string1, ...)
140 {
141   guint   l;
142   va_list args;
143   gchar   *s;
144   gchar   *concat;
145
146   g_return_val_if_fail (string1 != NULL, NULL);
147
148   l = 1 + strlen (string1);
149   va_start (args, string1);
150   s = va_arg (args, gchar*);
151   while (s)
152     {
153       l += strlen (s);
154       s = va_arg (args, gchar*);
155     }
156   va_end (args);
157
158   concat = g_new (gchar, l);
159   concat[0] = 0;
160
161   strcat (concat, string1);
162   va_start (args, string1);
163   s = va_arg (args, gchar*);
164   while (s)
165     {
166       strcat (concat, s);
167       s = va_arg (args, gchar*);
168     }
169   va_end (args);
170
171   return concat;
172 }
173
174 gdouble
175 g_strtod (const gchar *nptr,
176           gchar **endptr)
177 {
178   gchar *fail_pos_1;
179   gchar *fail_pos_2;
180   gdouble val_1;
181   gdouble val_2 = 0;
182
183   g_return_val_if_fail (nptr != NULL, 0);
184
185   fail_pos_1 = NULL;
186   fail_pos_2 = NULL;
187
188   val_1 = strtod (nptr, &fail_pos_1);
189
190   if (fail_pos_1 && fail_pos_1[0] != 0)
191     {
192       gchar *old_locale;
193
194       old_locale = setlocale (LC_NUMERIC, "C");
195       val_2 = strtod (nptr, &fail_pos_2);
196       setlocale (LC_NUMERIC, old_locale);
197     }
198
199   if (!fail_pos_1 || fail_pos_1[0] == 0 || fail_pos_1 >= fail_pos_2)
200     {
201       if (endptr)
202         *endptr = fail_pos_1;
203       return val_1;
204     }
205   else
206     {
207       if (endptr)
208         *endptr = fail_pos_2;
209       return val_2;
210     }
211 }
212
213 gchar*
214 g_strerror (gint errnum)
215 {
216   static GStaticPrivate msg_private = G_STATIC_PRIVATE_INIT;
217   char *msg;
218
219 #ifdef HAVE_STRERROR
220   return strerror (errnum);
221 #elif NO_SYS_ERRLIST
222   switch (errnum)
223     {
224 #ifdef E2BIG
225     case E2BIG: return "argument list too long";
226 #endif
227 #ifdef EACCES
228     case EACCES: return "permission denied";
229 #endif
230 #ifdef EADDRINUSE
231     case EADDRINUSE: return "address already in use";
232 #endif
233 #ifdef EADDRNOTAVAIL
234     case EADDRNOTAVAIL: return "can't assign requested address";
235 #endif
236 #ifdef EADV
237     case EADV: return "advertise error";
238 #endif
239 #ifdef EAFNOSUPPORT
240     case EAFNOSUPPORT: return "address family not supported by protocol family";
241 #endif
242 #ifdef EAGAIN
243     case EAGAIN: return "try again";
244 #endif
245 #ifdef EALIGN
246     case EALIGN: return "EALIGN";
247 #endif
248 #ifdef EALREADY
249     case EALREADY: return "operation already in progress";
250 #endif
251 #ifdef EBADE
252     case EBADE: return "bad exchange descriptor";
253 #endif
254 #ifdef EBADF
255     case EBADF: return "bad file number";
256 #endif
257 #ifdef EBADFD
258     case EBADFD: return "file descriptor in bad state";
259 #endif
260 #ifdef EBADMSG
261     case EBADMSG: return "not a data message";
262 #endif
263 #ifdef EBADR
264     case EBADR: return "bad request descriptor";
265 #endif
266 #ifdef EBADRPC
267     case EBADRPC: return "RPC structure is bad";
268 #endif
269 #ifdef EBADRQC
270     case EBADRQC: return "bad request code";
271 #endif
272 #ifdef EBADSLT
273     case EBADSLT: return "invalid slot";
274 #endif
275 #ifdef EBFONT
276     case EBFONT: return "bad font file format";
277 #endif
278 #ifdef EBUSY
279     case EBUSY: return "mount device busy";
280 #endif
281 #ifdef ECHILD
282     case ECHILD: return "no children";
283 #endif
284 #ifdef ECHRNG
285     case ECHRNG: return "channel number out of range";
286 #endif
287 #ifdef ECOMM
288     case ECOMM: return "communication error on send";
289 #endif
290 #ifdef ECONNABORTED
291     case ECONNABORTED: return "software caused connection abort";
292 #endif
293 #ifdef ECONNREFUSED
294     case ECONNREFUSED: return "connection refused";
295 #endif
296 #ifdef ECONNRESET
297     case ECONNRESET: return "connection reset by peer";
298 #endif
299 #if defined(EDEADLK) && (!defined(EWOULDBLOCK) || (EDEADLK != EWOULDBLOCK))
300     case EDEADLK: return "resource deadlock avoided";
301 #endif
302 #ifdef EDEADLOCK
303     case EDEADLOCK: return "resource deadlock avoided";
304 #endif
305 #ifdef EDESTADDRREQ
306     case EDESTADDRREQ: return "destination address required";
307 #endif
308 #ifdef EDIRTY
309     case EDIRTY: return "mounting a dirty fs w/o force";
310 #endif
311 #ifdef EDOM
312     case EDOM: return "math argument out of range";
313 #endif
314 #ifdef EDOTDOT
315     case EDOTDOT: return "cross mount point";
316 #endif
317 #ifdef EDQUOT
318     case EDQUOT: return "disk quota exceeded";
319 #endif
320 #ifdef EDUPPKG
321     case EDUPPKG: return "duplicate package name";
322 #endif
323 #ifdef EEXIST
324     case EEXIST: return "file already exists";
325 #endif
326 #ifdef EFAULT
327     case EFAULT: return "bad address in system call argument";
328 #endif
329 #ifdef EFBIG
330     case EFBIG: return "file too large";
331 #endif
332 #ifdef EHOSTDOWN
333     case EHOSTDOWN: return "host is down";
334 #endif
335 #ifdef EHOSTUNREACH
336     case EHOSTUNREACH: return "host is unreachable";
337 #endif
338 #ifdef EIDRM
339     case EIDRM: return "identifier removed";
340 #endif
341 #ifdef EINIT
342     case EINIT: return "initialization error";
343 #endif
344 #ifdef EINPROGRESS
345     case EINPROGRESS: return "operation now in progress";
346 #endif
347 #ifdef EINTR
348     case EINTR: return "interrupted system call";
349 #endif
350 #ifdef EINVAL
351     case EINVAL: return "invalid argument";
352 #endif
353 #ifdef EIO
354     case EIO: return "I/O error";
355 #endif
356 #ifdef EISCONN
357     case EISCONN: return "socket is already connected";
358 #endif
359 #ifdef EISDIR
360     case EISDIR: return "illegal operation on a directory";
361 #endif
362 #ifdef EISNAME
363     case EISNAM: return "is a name file";
364 #endif
365 #ifdef ELBIN
366     case ELBIN: return "ELBIN";
367 #endif
368 #ifdef EL2HLT
369     case EL2HLT: return "level 2 halted";
370 #endif
371 #ifdef EL2NSYNC
372     case EL2NSYNC: return "level 2 not synchronized";
373 #endif
374 #ifdef EL3HLT
375     case EL3HLT: return "level 3 halted";
376 #endif
377 #ifdef EL3RST
378     case EL3RST: return "level 3 reset";
379 #endif
380 #ifdef ELIBACC
381     case ELIBACC: return "can not access a needed shared library";
382 #endif
383 #ifdef ELIBBAD
384     case ELIBBAD: return "accessing a corrupted shared library";
385 #endif
386 #ifdef ELIBEXEC
387     case ELIBEXEC: return "can not exec a shared library directly";
388 #endif
389 #ifdef ELIBMAX
390     case ELIBMAX: return "attempting to link in more shared libraries than system limit";
391 #endif
392 #ifdef ELIBSCN
393     case ELIBSCN: return ".lib section in a.out corrupted";
394 #endif
395 #ifdef ELNRNG
396     case ELNRNG: return "link number out of range";
397 #endif
398 #ifdef ELOOP
399     case ELOOP: return "too many levels of symbolic links";
400 #endif
401 #ifdef EMFILE
402     case EMFILE: return "too many open files";
403 #endif
404 #ifdef EMLINK
405     case EMLINK: return "too many links";
406 #endif
407 #ifdef EMSGSIZE
408     case EMSGSIZE: return "message too long";
409 #endif
410 #ifdef EMULTIHOP
411     case EMULTIHOP: return "multihop attempted";
412 #endif
413 #ifdef ENAMETOOLONG
414     case ENAMETOOLONG: return "file name too long";
415 #endif
416 #ifdef ENAVAIL
417     case ENAVAIL: return "not available";
418 #endif
419 #ifdef ENET
420     case ENET: return "ENET";
421 #endif
422 #ifdef ENETDOWN
423     case ENETDOWN: return "network is down";
424 #endif
425 #ifdef ENETRESET
426     case ENETRESET: return "network dropped connection on reset";
427 #endif
428 #ifdef ENETUNREACH
429     case ENETUNREACH: return "network is unreachable";
430 #endif
431 #ifdef ENFILE
432     case ENFILE: return "file table overflow";
433 #endif
434 #ifdef ENOANO
435     case ENOANO: return "anode table overflow";
436 #endif
437 #if defined(ENOBUFS) && (!defined(ENOSR) || (ENOBUFS != ENOSR))
438     case ENOBUFS: return "no buffer space available";
439 #endif
440 #ifdef ENOCSI
441     case ENOCSI: return "no CSI structure available";
442 #endif
443 #ifdef ENODATA
444     case ENODATA: return "no data available";
445 #endif
446 #ifdef ENODEV
447     case ENODEV: return "no such device";
448 #endif
449 #ifdef ENOENT
450     case ENOENT: return "no such file or directory";
451 #endif
452 #ifdef ENOEXEC
453     case ENOEXEC: return "exec format error";
454 #endif
455 #ifdef ENOLCK
456     case ENOLCK: return "no locks available";
457 #endif
458 #ifdef ENOLINK
459     case ENOLINK: return "link has be severed";
460 #endif
461 #ifdef ENOMEM
462     case ENOMEM: return "not enough memory";
463 #endif
464 #ifdef ENOMSG
465     case ENOMSG: return "no message of desired type";
466 #endif
467 #ifdef ENONET
468     case ENONET: return "machine is not on the network";
469 #endif
470 #ifdef ENOPKG
471     case ENOPKG: return "package not installed";
472 #endif
473 #ifdef ENOPROTOOPT
474     case ENOPROTOOPT: return "bad proocol option";
475 #endif
476 #ifdef ENOSPC
477     case ENOSPC: return "no space left on device";
478 #endif
479 #ifdef ENOSR
480     case ENOSR: return "out of stream resources";
481 #endif
482 #ifdef ENOSTR
483     case ENOSTR: return "not a stream device";
484 #endif
485 #ifdef ENOSYM
486     case ENOSYM: return "unresolved symbol name";
487 #endif
488 #ifdef ENOSYS
489     case ENOSYS: return "function not implemented";
490 #endif
491 #ifdef ENOTBLK
492     case ENOTBLK: return "block device required";
493 #endif
494 #ifdef ENOTCONN
495     case ENOTCONN: return "socket is not connected";
496 #endif
497 #ifdef ENOTDIR
498     case ENOTDIR: return "not a directory";
499 #endif
500 #ifdef ENOTEMPTY
501     case ENOTEMPTY: return "directory not empty";
502 #endif
503 #ifdef ENOTNAM
504     case ENOTNAM: return "not a name file";
505 #endif
506 #ifdef ENOTSOCK
507     case ENOTSOCK: return "socket operation on non-socket";
508 #endif
509 #ifdef ENOTTY
510     case ENOTTY: return "inappropriate device for ioctl";
511 #endif
512 #ifdef ENOTUNIQ
513     case ENOTUNIQ: return "name not unique on network";
514 #endif
515 #ifdef ENXIO
516     case ENXIO: return "no such device or address";
517 #endif
518 #ifdef EOPNOTSUPP
519     case EOPNOTSUPP: return "operation not supported on socket";
520 #endif
521 #ifdef EPERM
522     case EPERM: return "not owner";
523 #endif
524 #ifdef EPFNOSUPPORT
525     case EPFNOSUPPORT: return "protocol family not supported";
526 #endif
527 #ifdef EPIPE
528     case EPIPE: return "broken pipe";
529 #endif
530 #ifdef EPROCLIM
531     case EPROCLIM: return "too many processes";
532 #endif
533 #ifdef EPROCUNAVAIL
534     case EPROCUNAVAIL: return "bad procedure for program";
535 #endif
536 #ifdef EPROGMISMATCH
537     case EPROGMISMATCH: return "program version wrong";
538 #endif
539 #ifdef EPROGUNAVAIL
540     case EPROGUNAVAIL: return "RPC program not available";
541 #endif
542 #ifdef EPROTO
543     case EPROTO: return "protocol error";
544 #endif
545 #ifdef EPROTONOSUPPORT
546     case EPROTONOSUPPORT: return "protocol not suppored";
547 #endif
548 #ifdef EPROTOTYPE
549     case EPROTOTYPE: return "protocol wrong type for socket";
550 #endif
551 #ifdef ERANGE
552     case ERANGE: return "math result unrepresentable";
553 #endif
554 #if defined(EREFUSED) && (!defined(ECONNREFUSED) || (EREFUSED != ECONNREFUSED))
555     case EREFUSED: return "EREFUSED";
556 #endif
557 #ifdef EREMCHG
558     case EREMCHG: return "remote address changed";
559 #endif
560 #ifdef EREMDEV
561     case EREMDEV: return "remote device";
562 #endif
563 #ifdef EREMOTE
564     case EREMOTE: return "pathname hit remote file system";
565 #endif
566 #ifdef EREMOTEIO
567     case EREMOTEIO: return "remote i/o error";
568 #endif
569 #ifdef EREMOTERELEASE
570     case EREMOTERELEASE: return "EREMOTERELEASE";
571 #endif
572 #ifdef EROFS
573     case EROFS: return "read-only file system";
574 #endif
575 #ifdef ERPCMISMATCH
576     case ERPCMISMATCH: return "RPC version is wrong";
577 #endif
578 #ifdef ERREMOTE
579     case ERREMOTE: return "object is remote";
580 #endif
581 #ifdef ESHUTDOWN
582     case ESHUTDOWN: return "can't send afer socket shutdown";
583 #endif
584 #ifdef ESOCKTNOSUPPORT
585     case ESOCKTNOSUPPORT: return "socket type not supported";
586 #endif
587 #ifdef ESPIPE
588     case ESPIPE: return "invalid seek";
589 #endif
590 #ifdef ESRCH
591     case ESRCH: return "no such process";
592 #endif
593 #ifdef ESRMNT
594     case ESRMNT: return "srmount error";
595 #endif
596 #ifdef ESTALE
597     case ESTALE: return "stale remote file handle";
598 #endif
599 #ifdef ESUCCESS
600     case ESUCCESS: return "Error 0";
601 #endif
602 #ifdef ETIME
603     case ETIME: return "timer expired";
604 #endif
605 #ifdef ETIMEDOUT
606     case ETIMEDOUT: return "connection timed out";
607 #endif
608 #ifdef ETOOMANYREFS
609     case ETOOMANYREFS: return "too many references: can't splice";
610 #endif
611 #ifdef ETXTBSY
612     case ETXTBSY: return "text file or pseudo-device busy";
613 #endif
614 #ifdef EUCLEAN
615     case EUCLEAN: return "structure needs cleaning";
616 #endif
617 #ifdef EUNATCH
618     case EUNATCH: return "protocol driver not attached";
619 #endif
620 #ifdef EUSERS
621     case EUSERS: return "too many users";
622 #endif
623 #ifdef EVERSION
624     case EVERSION: return "version mismatch";
625 #endif
626 #if defined(EWOULDBLOCK) && (!defined(EAGAIN) || (EWOULDBLOCK != EAGAIN))
627     case EWOULDBLOCK: return "operation would block";
628 #endif
629 #ifdef EXDEV
630     case EXDEV: return "cross-domain link";
631 #endif
632 #ifdef EXFULL
633     case EXFULL: return "message tables full";
634 #endif
635     }
636 #else /* NO_SYS_ERRLIST */
637   extern int sys_nerr;
638   extern char *sys_errlist[];
639
640   if ((errnum > 0) && (errnum <= sys_nerr))
641     return sys_errlist [errnum];
642 #endif /* NO_SYS_ERRLIST */
643
644   msg = g_static_private_get (&msg_private);
645   if( !msg )
646     {
647       msg = g_new( gchar, 64 );
648       g_static_private_set (&msg_private, msg, g_free);
649     }
650
651   sprintf (msg, "unknown error (%d)", errnum);
652   return msg;
653 }
654
655 gchar*
656 g_strsignal (gint signum)
657 {
658   static GStaticPrivate msg_private = G_STATIC_PRIVATE_INIT;
659   char *msg;
660
661 #ifdef HAVE_STRSIGNAL
662   extern char *strsignal (int sig);
663   return strsignal (signum);
664 #elif NO_SYS_SIGLIST
665   switch (signum)
666     {
667 #ifdef SIGHUP
668     case SIGHUP: return "Hangup";
669 #endif
670 #ifdef SIGINT
671     case SIGINT: return "Interrupt";
672 #endif
673 #ifdef SIGQUIT
674     case SIGQUIT: return "Quit";
675 #endif
676 #ifdef SIGILL
677     case SIGILL: return "Illegal instruction";
678 #endif
679 #ifdef SIGTRAP
680     case SIGTRAP: return "Trace/breakpoint trap";
681 #endif
682 #ifdef SIGABRT
683     case SIGABRT: return "IOT trap/Abort";
684 #endif
685 #ifdef SIGBUS
686     case SIGBUS: return "Bus error";
687 #endif
688 #ifdef SIGFPE
689     case SIGFPE: return "Floating point exception";
690 #endif
691 #ifdef SIGKILL
692     case SIGKILL: return "Killed";
693 #endif
694 #ifdef SIGUSR1
695     case SIGUSR1: return "User defined signal 1";
696 #endif
697 #ifdef SIGSEGV
698     case SIGSEGV: return "Segmentation fault";
699 #endif
700 #ifdef SIGUSR2
701     case SIGUSR2: return "User defined signal 2";
702 #endif
703 #ifdef SIGPIPE
704     case SIGPIPE: return "Broken pipe";
705 #endif
706 #ifdef SIGALRM
707     case SIGALRM: return "Alarm clock";
708 #endif
709 #ifdef SIGTERM
710     case SIGTERM: return "Terminated";
711 #endif
712 #ifdef SIGSTKFLT
713     case SIGSTKFLT: return "Stack fault";
714 #endif
715 #ifdef SIGCHLD
716     case SIGCHLD: return "Child exited";
717 #endif
718 #ifdef SIGCONT
719     case SIGCONT: return "Continued";
720 #endif
721 #ifdef SIGSTOP
722     case SIGSTOP: return "Stopped (signal)";
723 #endif
724 #ifdef SIGTSTP
725     case SIGTSTP: return "Stopped";
726 #endif
727 #ifdef SIGTTIN
728     case SIGTTIN: return "Stopped (tty input)";
729 #endif
730 #ifdef SIGTTOU
731     case SIGTTOU: return "Stopped (tty output)";
732 #endif
733 #ifdef SIGURG
734     case SIGURG: return "Urgent condition";
735 #endif
736 #ifdef SIGXCPU
737     case SIGXCPU: return "CPU time limit exceeded";
738 #endif
739 #ifdef SIGXFSZ
740     case SIGXFSZ: return "File size limit exceeded";
741 #endif
742 #ifdef SIGVTALRM
743     case SIGVTALRM: return "Virtual time alarm";
744 #endif
745 #ifdef SIGPROF
746     case SIGPROF: return "Profile signal";
747 #endif
748 #ifdef SIGWINCH
749     case SIGWINCH: return "Window size changed";
750 #endif
751 #ifdef SIGIO
752     case SIGIO: return "Possible I/O";
753 #endif
754 #ifdef SIGPWR
755     case SIGPWR: return "Power failure";
756 #endif
757 #ifdef SIGUNUSED
758     case SIGUNUSED: return "Unused signal";
759 #endif
760     }
761 #else /* NO_SYS_SIGLIST */
762
763 #ifndef NO_SYS_SIGLIST_DECL
764   /*(see Tue Jan 19 00:44:24 1999 in changelog)*/
765   extern char *sys_siglist[];
766 #endif
767
768   return (char*) /* this function should return const --josh */ sys_siglist [signum];
769 #endif /* NO_SYS_SIGLIST */
770
771   msg = g_static_private_get (&msg_private);
772   if( !msg )
773     {
774       msg = g_new( gchar, 64 );
775       g_static_private_set (&msg_private, msg, g_free);
776     }
777
778   sprintf (msg, "unknown signal (%d)", signum);
779   return msg;
780 }
781
782 guint
783 g_printf_string_upper_bound (const gchar* format,
784                              va_list      args)
785 {
786   guint len = 1;
787
788   while (*format)
789     {
790       gboolean long_int = FALSE;
791       gboolean extra_long = FALSE;
792       gchar c;
793
794       c = *format++;
795
796       if (c == '%')
797         {
798           gboolean done = FALSE;
799
800           while (*format && !done)
801             {
802               switch (*format++)
803                 {
804                   gchar *string_arg;
805
806                 case '*':
807                   len += va_arg (args, int);
808                   break;
809                 case '1':
810                 case '2':
811                 case '3':
812                 case '4':
813                 case '5':
814                 case '6':
815                 case '7':
816                 case '8':
817                 case '9':
818                   /* add specified format length, since it might exceed the
819                    * size we assume it to have.
820                    */
821                   format -= 1;
822                   len += strtol (format, (char**) &format, 10);
823                   break;
824                 case 'h':
825                   /* ignore short int flag, since all args have at least the
826                    * same size as an int
827                    */
828                   break;
829                 case 'l':
830                   if (long_int)
831                     extra_long = TRUE; /* linux specific */
832                   else
833                     long_int = TRUE;
834                   break;
835                 case 'q':
836                 case 'L':
837                   long_int = TRUE;
838                   extra_long = TRUE;
839                   break;
840                 case 's':
841                   string_arg = va_arg (args, char *);
842                   if (string_arg)
843                     len += strlen (string_arg);
844                   else
845                     {
846                       /* add enough padding to hold "(null)" identifier */
847                       len += 16;
848                     }
849                   done = TRUE;
850                   break;
851                 case 'd':
852                 case 'i':
853                 case 'o':
854                 case 'u':
855                 case 'x':
856                 case 'X':
857 #ifdef  G_HAVE_GINT64
858                   if (extra_long)
859                     (void) va_arg (args, gint64);
860                   else
861 #endif  /* G_HAVE_GINT64 */
862                     {
863                       if (long_int)
864                         (void) va_arg (args, long);
865                       else
866                         (void) va_arg (args, int);
867                     }
868                   len += extra_long ? 64 : 32;
869                   done = TRUE;
870                   break;
871                 case 'D':
872                 case 'O':
873                 case 'U':
874                   (void) va_arg (args, long);
875                   len += 32;
876                   done = TRUE;
877                   break;
878                 case 'e':
879                 case 'E':
880                 case 'f':
881                 case 'g':
882 #ifdef HAVE_LONG_DOUBLE
883                   if (extra_long)
884                     (void) va_arg (args, long double);
885                   else
886 #endif  /* HAVE_LONG_DOUBLE */
887                     (void) va_arg (args, double);
888                   len += extra_long ? 64 : 32;
889                   done = TRUE;
890                   break;
891                 case 'c':
892                   (void) va_arg (args, int);
893                   len += 1;
894                   done = TRUE;
895                   break;
896                 case 'p':
897                 case 'n':
898                   (void) va_arg (args, void*);
899                   len += 32;
900                   done = TRUE;
901                   break;
902                 case '%':
903                   len += 1;
904                   done = TRUE;
905                   break;
906                 default:
907                   /* ignore unknow/invalid flags */
908                   break;
909                 }
910             }
911         }
912       else
913         len += 1;
914     }
915
916   return len;
917 }
918
919 void
920 g_strdown (gchar  *string)
921 {
922   register gchar *s;
923
924   g_return_if_fail (string != NULL);
925
926   s = string;
927
928   while (*s)
929     {
930       *s = tolower (*s);
931       s++;
932     }
933 }
934
935 void
936 g_strup (gchar  *string)
937 {
938   register gchar *s;
939
940   g_return_if_fail (string != NULL);
941
942   s = string;
943
944   while (*s)
945     {
946       *s = toupper (*s);
947       s++;
948     }
949 }
950
951 void
952 g_strreverse (gchar       *string)
953 {
954   g_return_if_fail (string != NULL);
955
956   if (*string)
957     {
958       register gchar *h, *t;
959
960       h = string;
961       t = string + strlen (string) - 1;
962
963       while (h < t)
964         {
965           register gchar c;
966
967           c = *h;
968           *h = *t;
969           h++;
970           *t = c;
971           t--;
972         }
973     }
974 }
975
976 gint
977 g_strcasecmp (const gchar *s1,
978               const gchar *s2)
979 {
980 #ifdef HAVE_STRCASECMP
981   return strcasecmp (s1, s2);
982 #else
983   gint c1, c2;
984
985   g_return_val_if_fail (s1 != NULL, 0);
986   g_return_val_if_fail (s2 != NULL, 0);
987
988   while (*s1 && *s2)
989     {
990       /* According to A. Cox, some platforms have islower's that
991        * don't work right on non-uppercase
992        */
993       c1 = isupper ((guchar)*s1) ? tolower ((guchar)*s1) : *s1;
994       c2 = isupper ((guchar)*s2) ? tolower ((guchar)*s2) : *s2;
995       if (c1 != c2)
996         return (c1 - c2);
997       s1++; s2++;
998     }
999
1000   return (((gint)(guchar) *s1) - ((gint)(guchar) *s2));
1001 #endif
1002 }
1003
1004 gint
1005 g_strncasecmp (const gchar *s1,
1006                const gchar *s2,
1007                guint n)
1008 {
1009 #ifdef HAVE_STRNCASECMP
1010   return strncasecmp (s1, s2, n);
1011 #else
1012   gint c1, c2;
1013
1014   g_return_val_if_fail (s1 != NULL, 0);
1015   g_return_val_if_fail (s2 != NULL, 0);
1016
1017   while (n-- && *s1 && *s2)
1018     {
1019       /* According to A. Cox, some platforms have islower's that
1020        * don't work right on non-uppercase
1021        */
1022       c1 = isupper ((guchar)*s1) ? tolower ((guchar)*s1) : *s1;
1023       c2 = isupper ((guchar)*s2) ? tolower ((guchar)*s2) : *s2;
1024       if (c1 != c2)
1025         return (c1 - c2);
1026       s1++; s2++;
1027     }
1028
1029   if (n)
1030     return (((gint)(guchar) *s1) - ((gint)(guchar) *s2));
1031   else
1032     return 0;
1033 #endif
1034 }
1035
1036 gchar*
1037 g_strdelimit (gchar       *string,
1038               const gchar *delimiters,
1039               gchar        new_delim)
1040 {
1041   register gchar *c;
1042
1043   g_return_val_if_fail (string != NULL, NULL);
1044
1045   if (!delimiters)
1046     delimiters = G_STR_DELIMITERS;
1047
1048   for (c = string; *c; c++)
1049     {
1050       if (strchr (delimiters, *c))
1051         *c = new_delim;
1052     }
1053
1054   return string;
1055 }
1056
1057 gchar*
1058 g_strescape (gchar *string)
1059 {
1060   gchar *q;
1061   gchar *escaped;
1062   guint backslashes = 0;
1063   gchar *p = string;
1064
1065   g_return_val_if_fail (string != NULL, NULL);
1066
1067   while (*p != '\000')
1068     backslashes += (*p++ == '\\');
1069
1070   if (!backslashes)
1071     return g_strdup (string);
1072
1073   escaped = g_new (gchar, strlen (string) + backslashes + 1);
1074
1075   p = string;
1076   q = escaped;
1077
1078   while (*p != '\000')
1079     {
1080       if (*p == '\\')
1081         *q++ = '\\';
1082       *q++ = *p++;
1083     }
1084   *q = '\000';
1085
1086   return escaped;
1087 }
1088
1089 /* blame Elliot for these next five routines */
1090 gchar*
1091 g_strchug (gchar *string)
1092 {
1093   gchar *start;
1094
1095   g_return_val_if_fail (string != NULL, NULL);
1096
1097   for (start = string; *start && isspace (*start); start++)
1098     ;
1099
1100   strcpy (string, start);
1101
1102   return string;
1103 }
1104
1105 gchar*
1106 g_strchomp (gchar *string)
1107 {
1108   gchar *s;
1109
1110   g_return_val_if_fail (string != NULL, NULL);
1111
1112   if (!*string)
1113     return string;
1114
1115   for (s = string + strlen (string) - 1; s >= string && isspace (*s); s--)
1116     *s = '\0';
1117
1118   return string;
1119 }
1120
1121 gchar**
1122 g_strsplit (const gchar *string,
1123             const gchar *delimiter,
1124             gint         max_tokens)
1125 {
1126   GSList *string_list = NULL, *slist;
1127   gchar **str_array, *s;
1128   guint i, n = 1;
1129
1130   g_return_val_if_fail (string != NULL, NULL);
1131   g_return_val_if_fail (delimiter != NULL, NULL);
1132
1133   if (max_tokens < 1)
1134     max_tokens = G_MAXINT;
1135
1136   s = strstr (string, delimiter);
1137   if (s)
1138     {
1139       guint delimiter_len = strlen (delimiter);
1140
1141       do
1142         {
1143           guint len;
1144           gchar *new_string;
1145
1146           len = s - string;
1147           new_string = g_new (gchar, len + 1);
1148           strncpy (new_string, string, len);
1149           new_string[len] = 0;
1150           string_list = g_slist_prepend (string_list, new_string);
1151           n++;
1152           string = s + delimiter_len;
1153           s = strstr (string, delimiter);
1154         }
1155       while (--max_tokens && s);
1156     }
1157   if (*string)
1158     {
1159       n++;
1160       string_list = g_slist_prepend (string_list, g_strdup (string));
1161     }
1162
1163   str_array = g_new (gchar*, n);
1164
1165   i = n - 1;
1166
1167   str_array[i--] = NULL;
1168   for (slist = string_list; slist; slist = slist->next)
1169     str_array[i--] = slist->data;
1170
1171   g_slist_free (string_list);
1172
1173   return str_array;
1174 }
1175
1176 void
1177 g_strfreev (gchar **str_array)
1178 {
1179   if (str_array)
1180     {
1181       int i;
1182
1183       for(i = 0; str_array[i] != NULL; i++)
1184         g_free(str_array[i]);
1185
1186       g_free (str_array);
1187     }
1188 }
1189
1190 gchar*
1191 g_strjoinv (const gchar  *separator,
1192             gchar       **str_array)
1193 {
1194   gchar *string;
1195
1196   g_return_val_if_fail (str_array != NULL, NULL);
1197
1198   if(separator == NULL)
1199     separator = "";
1200
1201   if (*str_array)
1202     {
1203       guint i, len;
1204       guint separator_len;
1205
1206       separator_len = strlen (separator);
1207       len = 1 + strlen (str_array[0]);
1208       for(i = 1; str_array[i] != NULL; i++)
1209         len += separator_len + strlen(str_array[i]);
1210
1211       string = g_new (gchar, len);
1212       *string = 0;
1213       strcat (string, *str_array);
1214       for (i = 1; str_array[i] != NULL; i++)
1215         {
1216           strcat (string, separator);
1217           strcat (string, str_array[i]);
1218         }
1219       }
1220   else
1221     string = g_strdup ("");
1222
1223   return string;
1224 }
1225
1226 gchar*
1227 g_strjoin (const gchar  *separator,
1228            ...)
1229 {
1230   gchar *string, *s;
1231   va_list args;
1232   guint len;
1233   guint separator_len;
1234
1235   if(separator == NULL)
1236     separator = "";
1237
1238   separator_len = strlen (separator);
1239
1240   va_start(args, separator);
1241
1242   s = va_arg(args, gchar *);
1243
1244   if(s) {
1245     len = strlen(s) + 1;
1246
1247     while((s = va_arg(args, gchar*)))
1248       {
1249         len += separator_len + strlen(s);
1250       }
1251     va_end(args);
1252
1253     string = g_new (gchar, len);
1254
1255     va_start(args, separator);
1256
1257     *string = 0;
1258     s = va_arg(args, gchar*);
1259     strcat (string, s);
1260
1261     while((s = va_arg(args, gchar*)))
1262       {
1263         strcat(string, separator);
1264         strcat(string, s);
1265       }
1266
1267   } else
1268     string = g_strdup("");
1269
1270   va_end(args);
1271
1272   return string;
1273 }