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