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