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