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