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