Update. Don't mention pthreads.
[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 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 #define _GNU_SOURCE             /* For stpcpy */
36
37 #include <stdarg.h>
38 #include <stdio.h>
39 #include <stdlib.h>
40 #include <string.h>
41 #include <locale.h>
42 #include <ctype.h>              /* For tolower() */
43 #if !defined (HAVE_STRSIGNAL) || !defined(NO_SYS_SIGLIST_DECL)
44 #include <signal.h>
45 #endif
46 #include "glib.h"
47
48 #ifdef G_OS_WIN32
49 #include <windows.h>
50 #endif
51
52 /* do not include <unistd.h> in this place since it
53  * inteferes with g_strsignal() on some OSes
54  */
55
56 static const guint16 ascii_table_data[256] = {
57   0x004, 0x004, 0x004, 0x004, 0x004, 0x004, 0x004, 0x004,
58   0x004, 0x104, 0x104, 0x004, 0x104, 0x104, 0x004, 0x004,
59   0x004, 0x004, 0x004, 0x004, 0x004, 0x004, 0x004, 0x004,
60   0x004, 0x004, 0x004, 0x004, 0x004, 0x004, 0x004, 0x004,
61   0x140, 0x0d0, 0x0d0, 0x0d0, 0x0d0, 0x0d0, 0x0d0, 0x0d0,
62   0x0d0, 0x0d0, 0x0d0, 0x0d0, 0x0d0, 0x0d0, 0x0d0, 0x0d0,
63   0x459, 0x459, 0x459, 0x459, 0x459, 0x459, 0x459, 0x459,
64   0x459, 0x459, 0x0d0, 0x0d0, 0x0d0, 0x0d0, 0x0d0, 0x0d0,
65   0x0d0, 0x653, 0x653, 0x653, 0x653, 0x653, 0x653, 0x253,
66   0x253, 0x253, 0x253, 0x253, 0x253, 0x253, 0x253, 0x253,
67   0x253, 0x253, 0x253, 0x253, 0x253, 0x253, 0x253, 0x253,
68   0x253, 0x253, 0x253, 0x0d0, 0x0d0, 0x0d0, 0x0d0, 0x0d0,
69   0x0d0, 0x473, 0x473, 0x473, 0x473, 0x473, 0x473, 0x073,
70   0x073, 0x073, 0x073, 0x073, 0x073, 0x073, 0x073, 0x073,
71   0x073, 0x073, 0x073, 0x073, 0x073, 0x073, 0x073, 0x073,
72   0x073, 0x073, 0x073, 0x0d0, 0x0d0, 0x0d0, 0x0d0, 0x004
73   /* the upper 128 are all zeroes */
74 };
75
76 #if defined(G_PLATFORM_WIN32) && defined(__GNUC__)
77 __declspec(dllexport)
78 #endif
79 const guint16 * const g_ascii_table = ascii_table_data;
80
81 gchar*
82 g_strdup (const gchar *str)
83 {
84   gchar *new_str;
85
86   if (str)
87     {
88       new_str = g_new (char, strlen (str) + 1);
89       strcpy (new_str, str);
90     }
91   else
92     new_str = NULL;
93
94   return new_str;
95 }
96
97 gpointer
98 g_memdup (gconstpointer mem,
99           guint         byte_size)
100 {
101   gpointer new_mem;
102
103   if (mem)
104     {
105       new_mem = g_malloc (byte_size);
106       memcpy (new_mem, mem, byte_size);
107     }
108   else
109     new_mem = NULL;
110
111   return new_mem;
112 }
113
114 gchar*
115 g_strndup (const gchar *str,
116            gsize        n)    
117 {
118   gchar *new_str;
119
120   if (str)
121     {
122       new_str = g_new (gchar, n + 1);
123       strncpy (new_str, str, n);
124       new_str[n] = '\0';
125     }
126   else
127     new_str = NULL;
128
129   return new_str;
130 }
131
132 gchar*
133 g_strnfill (gsize length,     
134             gchar fill_char)
135 {
136   register gchar *str, *s, *end;
137
138   str = g_new (gchar, length + 1);
139   s = str;
140   end = str + length;
141   while (s < end)
142     *(s++) = fill_char;
143   *s = 0;
144
145   return str;
146 }
147
148 /**
149  * g_stpcpy:
150  * @dest: destination buffer
151  * @src: source string
152  * 
153  * Copies a nul-terminated string into the dest buffer, include the
154  * trailing nul, and return a pointer to the trailing nul byte.
155  * This is useful for concatenating multiple strings together
156  * without having to repeatedly scan for the end.
157  * 
158  * Return value: a pointer to trailing nul byte.
159  **/
160 gchar *
161 g_stpcpy (gchar       *dest,
162           const gchar *src)
163 {
164 #ifdef HAVE_STPCPY
165   g_return_val_if_fail (dest != NULL, NULL);
166   g_return_val_if_fail (src != NULL, NULL);
167   return stpcpy (dest, src);
168 #else
169   register gchar *d = dest;
170   register const gchar *s = src;
171
172   g_return_val_if_fail (dest != NULL, NULL);
173   g_return_val_if_fail (src != NULL, NULL);
174   do
175     *d++ = *s;
176   while (*s++ != '\0');
177
178   return d - 1;
179 #endif
180 }
181
182 gchar*
183 g_strdup_vprintf (const gchar *format,
184                   va_list      args1)
185 {
186   gchar *buffer;
187   va_list args2;
188
189   G_VA_COPY (args2, args1);
190
191   buffer = g_new (gchar, g_printf_string_upper_bound (format, args1));
192
193   vsprintf (buffer, format, args2);
194   va_end (args2);
195
196   return buffer;
197 }
198
199 gchar*
200 g_strdup_printf (const gchar *format,
201                  ...)
202 {
203   gchar *buffer;
204   va_list args;
205
206   va_start (args, format);
207   buffer = g_strdup_vprintf (format, args);
208   va_end (args);
209
210   return buffer;
211 }
212
213 gchar*
214 g_strconcat (const gchar *string1, ...)
215 {
216   gsize   l;     
217   va_list args;
218   gchar   *s;
219   gchar   *concat;
220   gchar   *ptr;
221
222   g_return_val_if_fail (string1 != NULL, NULL);
223
224   l = 1 + strlen (string1);
225   va_start (args, string1);
226   s = va_arg (args, gchar*);
227   while (s)
228     {
229       l += strlen (s);
230       s = va_arg (args, gchar*);
231     }
232   va_end (args);
233
234   concat = g_new (gchar, l);
235   ptr = concat;
236
237   ptr = g_stpcpy (ptr, string1);
238   va_start (args, string1);
239   s = va_arg (args, gchar*);
240   while (s)
241     {
242       ptr = g_stpcpy (ptr, s);
243       s = va_arg (args, gchar*);
244     }
245   va_end (args);
246
247   return concat;
248 }
249
250 gdouble
251 g_strtod (const gchar *nptr,
252           gchar **endptr)
253 {
254   gchar *fail_pos_1;
255   gchar *fail_pos_2;
256   gdouble val_1;
257   gdouble val_2 = 0;
258
259   g_return_val_if_fail (nptr != NULL, 0);
260
261   fail_pos_1 = NULL;
262   fail_pos_2 = NULL;
263
264   val_1 = strtod (nptr, &fail_pos_1);
265
266   if (fail_pos_1 && fail_pos_1[0] != 0)
267     {
268       gchar *old_locale;
269
270       old_locale = g_strdup (setlocale (LC_NUMERIC, NULL));
271       setlocale (LC_NUMERIC, "C");
272       val_2 = strtod (nptr, &fail_pos_2);
273       setlocale (LC_NUMERIC, old_locale);
274       g_free (old_locale);
275     }
276
277   if (!fail_pos_1 || fail_pos_1[0] == 0 || fail_pos_1 >= fail_pos_2)
278     {
279       if (endptr)
280         *endptr = fail_pos_1;
281       return val_1;
282     }
283   else
284     {
285       if (endptr)
286         *endptr = fail_pos_2;
287       return val_2;
288     }
289 }
290
291 G_CONST_RETURN gchar*
292 g_strerror (gint errnum)
293 {
294   static GStaticPrivate msg_private = G_STATIC_PRIVATE_INIT;
295   char *msg;
296
297 #ifdef HAVE_STRERROR
298   return strerror (errnum);
299 #elif NO_SYS_ERRLIST
300   switch (errnum)
301     {
302 #ifdef E2BIG
303     case E2BIG: return "argument list too long";
304 #endif
305 #ifdef EACCES
306     case EACCES: return "permission denied";
307 #endif
308 #ifdef EADDRINUSE
309     case EADDRINUSE: return "address already in use";
310 #endif
311 #ifdef EADDRNOTAVAIL
312     case EADDRNOTAVAIL: return "can't assign requested address";
313 #endif
314 #ifdef EADV
315     case EADV: return "advertise error";
316 #endif
317 #ifdef EAFNOSUPPORT
318     case EAFNOSUPPORT: return "address family not supported by protocol family";
319 #endif
320 #ifdef EAGAIN
321     case EAGAIN: return "try again";
322 #endif
323 #ifdef EALIGN
324     case EALIGN: return "EALIGN";
325 #endif
326 #ifdef EALREADY
327     case EALREADY: return "operation already in progress";
328 #endif
329 #ifdef EBADE
330     case EBADE: return "bad exchange descriptor";
331 #endif
332 #ifdef EBADF
333     case EBADF: return "bad file number";
334 #endif
335 #ifdef EBADFD
336     case EBADFD: return "file descriptor in bad state";
337 #endif
338 #ifdef EBADMSG
339     case EBADMSG: return "not a data message";
340 #endif
341 #ifdef EBADR
342     case EBADR: return "bad request descriptor";
343 #endif
344 #ifdef EBADRPC
345     case EBADRPC: return "RPC structure is bad";
346 #endif
347 #ifdef EBADRQC
348     case EBADRQC: return "bad request code";
349 #endif
350 #ifdef EBADSLT
351     case EBADSLT: return "invalid slot";
352 #endif
353 #ifdef EBFONT
354     case EBFONT: return "bad font file format";
355 #endif
356 #ifdef EBUSY
357     case EBUSY: return "mount device busy";
358 #endif
359 #ifdef ECHILD
360     case ECHILD: return "no children";
361 #endif
362 #ifdef ECHRNG
363     case ECHRNG: return "channel number out of range";
364 #endif
365 #ifdef ECOMM
366     case ECOMM: return "communication error on send";
367 #endif
368 #ifdef ECONNABORTED
369     case ECONNABORTED: return "software caused connection abort";
370 #endif
371 #ifdef ECONNREFUSED
372     case ECONNREFUSED: return "connection refused";
373 #endif
374 #ifdef ECONNRESET
375     case ECONNRESET: return "connection reset by peer";
376 #endif
377 #if defined(EDEADLK) && (!defined(EWOULDBLOCK) || (EDEADLK != EWOULDBLOCK))
378     case EDEADLK: return "resource deadlock avoided";
379 #endif
380 #ifdef EDEADLOCK
381     case EDEADLOCK: return "resource deadlock avoided";
382 #endif
383 #ifdef EDESTADDRREQ
384     case EDESTADDRREQ: return "destination address required";
385 #endif
386 #ifdef EDIRTY
387     case EDIRTY: return "mounting a dirty fs w/o force";
388 #endif
389 #ifdef EDOM
390     case EDOM: return "math argument out of range";
391 #endif
392 #ifdef EDOTDOT
393     case EDOTDOT: return "cross mount point";
394 #endif
395 #ifdef EDQUOT
396     case EDQUOT: return "disk quota exceeded";
397 #endif
398 #ifdef EDUPPKG
399     case EDUPPKG: return "duplicate package name";
400 #endif
401 #ifdef EEXIST
402     case EEXIST: return "file already exists";
403 #endif
404 #ifdef EFAULT
405     case EFAULT: return "bad address in system call argument";
406 #endif
407 #ifdef EFBIG
408     case EFBIG: return "file too large";
409 #endif
410 #ifdef EHOSTDOWN
411     case EHOSTDOWN: return "host is down";
412 #endif
413 #ifdef EHOSTUNREACH
414     case EHOSTUNREACH: return "host is unreachable";
415 #endif
416 #ifdef EIDRM
417     case EIDRM: return "identifier removed";
418 #endif
419 #ifdef EINIT
420     case EINIT: return "initialization error";
421 #endif
422 #ifdef EINPROGRESS
423     case EINPROGRESS: return "operation now in progress";
424 #endif
425 #ifdef EINTR
426     case EINTR: return "interrupted system call";
427 #endif
428 #ifdef EINVAL
429     case EINVAL: return "invalid argument";
430 #endif
431 #ifdef EIO
432     case EIO: return "I/O error";
433 #endif
434 #ifdef EISCONN
435     case EISCONN: return "socket is already connected";
436 #endif
437 #ifdef EISDIR
438     case EISDIR: return "illegal operation on a directory";
439 #endif
440 #ifdef EISNAME
441     case EISNAM: return "is a name file";
442 #endif
443 #ifdef ELBIN
444     case ELBIN: return "ELBIN";
445 #endif
446 #ifdef EL2HLT
447     case EL2HLT: return "level 2 halted";
448 #endif
449 #ifdef EL2NSYNC
450     case EL2NSYNC: return "level 2 not synchronized";
451 #endif
452 #ifdef EL3HLT
453     case EL3HLT: return "level 3 halted";
454 #endif
455 #ifdef EL3RST
456     case EL3RST: return "level 3 reset";
457 #endif
458 #ifdef ELIBACC
459     case ELIBACC: return "can not access a needed shared library";
460 #endif
461 #ifdef ELIBBAD
462     case ELIBBAD: return "accessing a corrupted shared library";
463 #endif
464 #ifdef ELIBEXEC
465     case ELIBEXEC: return "can not exec a shared library directly";
466 #endif
467 #ifdef ELIBMAX
468     case ELIBMAX: return "attempting to link in more shared libraries than system limit";
469 #endif
470 #ifdef ELIBSCN
471     case ELIBSCN: return ".lib section in a.out corrupted";
472 #endif
473 #ifdef ELNRNG
474     case ELNRNG: return "link number out of range";
475 #endif
476 #ifdef ELOOP
477     case ELOOP: return "too many levels of symbolic links";
478 #endif
479 #ifdef EMFILE
480     case EMFILE: return "too many open files";
481 #endif
482 #ifdef EMLINK
483     case EMLINK: return "too many links";
484 #endif
485 #ifdef EMSGSIZE
486     case EMSGSIZE: return "message too long";
487 #endif
488 #ifdef EMULTIHOP
489     case EMULTIHOP: return "multihop attempted";
490 #endif
491 #ifdef ENAMETOOLONG
492     case ENAMETOOLONG: return "file name too long";
493 #endif
494 #ifdef ENAVAIL
495     case ENAVAIL: return "not available";
496 #endif
497 #ifdef ENET
498     case ENET: return "ENET";
499 #endif
500 #ifdef ENETDOWN
501     case ENETDOWN: return "network is down";
502 #endif
503 #ifdef ENETRESET
504     case ENETRESET: return "network dropped connection on reset";
505 #endif
506 #ifdef ENETUNREACH
507     case ENETUNREACH: return "network is unreachable";
508 #endif
509 #ifdef ENFILE
510     case ENFILE: return "file table overflow";
511 #endif
512 #ifdef ENOANO
513     case ENOANO: return "anode table overflow";
514 #endif
515 #if defined(ENOBUFS) && (!defined(ENOSR) || (ENOBUFS != ENOSR))
516     case ENOBUFS: return "no buffer space available";
517 #endif
518 #ifdef ENOCSI
519     case ENOCSI: return "no CSI structure available";
520 #endif
521 #ifdef ENODATA
522     case ENODATA: return "no data available";
523 #endif
524 #ifdef ENODEV
525     case ENODEV: return "no such device";
526 #endif
527 #ifdef ENOENT
528     case ENOENT: return "no such file or directory";
529 #endif
530 #ifdef ENOEXEC
531     case ENOEXEC: return "exec format error";
532 #endif
533 #ifdef ENOLCK
534     case ENOLCK: return "no locks available";
535 #endif
536 #ifdef ENOLINK
537     case ENOLINK: return "link has be severed";
538 #endif
539 #ifdef ENOMEM
540     case ENOMEM: return "not enough memory";
541 #endif
542 #ifdef ENOMSG
543     case ENOMSG: return "no message of desired type";
544 #endif
545 #ifdef ENONET
546     case ENONET: return "machine is not on the network";
547 #endif
548 #ifdef ENOPKG
549     case ENOPKG: return "package not installed";
550 #endif
551 #ifdef ENOPROTOOPT
552     case ENOPROTOOPT: return "bad proocol option";
553 #endif
554 #ifdef ENOSPC
555     case ENOSPC: return "no space left on device";
556 #endif
557 #ifdef ENOSR
558     case ENOSR: return "out of stream resources";
559 #endif
560 #ifdef ENOSTR
561     case ENOSTR: return "not a stream device";
562 #endif
563 #ifdef ENOSYM
564     case ENOSYM: return "unresolved symbol name";
565 #endif
566 #ifdef ENOSYS
567     case ENOSYS: return "function not implemented";
568 #endif
569 #ifdef ENOTBLK
570     case ENOTBLK: return "block device required";
571 #endif
572 #ifdef ENOTCONN
573     case ENOTCONN: return "socket is not connected";
574 #endif
575 #ifdef ENOTDIR
576     case ENOTDIR: return "not a directory";
577 #endif
578 #ifdef ENOTEMPTY
579     case ENOTEMPTY: return "directory not empty";
580 #endif
581 #ifdef ENOTNAM
582     case ENOTNAM: return "not a name file";
583 #endif
584 #ifdef ENOTSOCK
585     case ENOTSOCK: return "socket operation on non-socket";
586 #endif
587 #ifdef ENOTTY
588     case ENOTTY: return "inappropriate device for ioctl";
589 #endif
590 #ifdef ENOTUNIQ
591     case ENOTUNIQ: return "name not unique on network";
592 #endif
593 #ifdef ENXIO
594     case ENXIO: return "no such device or address";
595 #endif
596 #ifdef EOPNOTSUPP
597     case EOPNOTSUPP: return "operation not supported on socket";
598 #endif
599 #ifdef EPERM
600     case EPERM: return "not owner";
601 #endif
602 #ifdef EPFNOSUPPORT
603     case EPFNOSUPPORT: return "protocol family not supported";
604 #endif
605 #ifdef EPIPE
606     case EPIPE: return "broken pipe";
607 #endif
608 #ifdef EPROCLIM
609     case EPROCLIM: return "too many processes";
610 #endif
611 #ifdef EPROCUNAVAIL
612     case EPROCUNAVAIL: return "bad procedure for program";
613 #endif
614 #ifdef EPROGMISMATCH
615     case EPROGMISMATCH: return "program version wrong";
616 #endif
617 #ifdef EPROGUNAVAIL
618     case EPROGUNAVAIL: return "RPC program not available";
619 #endif
620 #ifdef EPROTO
621     case EPROTO: return "protocol error";
622 #endif
623 #ifdef EPROTONOSUPPORT
624     case EPROTONOSUPPORT: return "protocol not suppored";
625 #endif
626 #ifdef EPROTOTYPE
627     case EPROTOTYPE: return "protocol wrong type for socket";
628 #endif
629 #ifdef ERANGE
630     case ERANGE: return "math result unrepresentable";
631 #endif
632 #if defined(EREFUSED) && (!defined(ECONNREFUSED) || (EREFUSED != ECONNREFUSED))
633     case EREFUSED: return "EREFUSED";
634 #endif
635 #ifdef EREMCHG
636     case EREMCHG: return "remote address changed";
637 #endif
638 #ifdef EREMDEV
639     case EREMDEV: return "remote device";
640 #endif
641 #ifdef EREMOTE
642     case EREMOTE: return "pathname hit remote file system";
643 #endif
644 #ifdef EREMOTEIO
645     case EREMOTEIO: return "remote i/o error";
646 #endif
647 #ifdef EREMOTERELEASE
648     case EREMOTERELEASE: return "EREMOTERELEASE";
649 #endif
650 #ifdef EROFS
651     case EROFS: return "read-only file system";
652 #endif
653 #ifdef ERPCMISMATCH
654     case ERPCMISMATCH: return "RPC version is wrong";
655 #endif
656 #ifdef ERREMOTE
657     case ERREMOTE: return "object is remote";
658 #endif
659 #ifdef ESHUTDOWN
660     case ESHUTDOWN: return "can't send afer socket shutdown";
661 #endif
662 #ifdef ESOCKTNOSUPPORT
663     case ESOCKTNOSUPPORT: return "socket type not supported";
664 #endif
665 #ifdef ESPIPE
666     case ESPIPE: return "invalid seek";
667 #endif
668 #ifdef ESRCH
669     case ESRCH: return "no such process";
670 #endif
671 #ifdef ESRMNT
672     case ESRMNT: return "srmount error";
673 #endif
674 #ifdef ESTALE
675     case ESTALE: return "stale remote file handle";
676 #endif
677 #ifdef ESUCCESS
678     case ESUCCESS: return "Error 0";
679 #endif
680 #ifdef ETIME
681     case ETIME: return "timer expired";
682 #endif
683 #ifdef ETIMEDOUT
684     case ETIMEDOUT: return "connection timed out";
685 #endif
686 #ifdef ETOOMANYREFS
687     case ETOOMANYREFS: return "too many references: can't splice";
688 #endif
689 #ifdef ETXTBSY
690     case ETXTBSY: return "text file or pseudo-device busy";
691 #endif
692 #ifdef EUCLEAN
693     case EUCLEAN: return "structure needs cleaning";
694 #endif
695 #ifdef EUNATCH
696     case EUNATCH: return "protocol driver not attached";
697 #endif
698 #ifdef EUSERS
699     case EUSERS: return "too many users";
700 #endif
701 #ifdef EVERSION
702     case EVERSION: return "version mismatch";
703 #endif
704 #if defined(EWOULDBLOCK) && (!defined(EAGAIN) || (EWOULDBLOCK != EAGAIN))
705     case EWOULDBLOCK: return "operation would block";
706 #endif
707 #ifdef EXDEV
708     case EXDEV: return "cross-domain link";
709 #endif
710 #ifdef EXFULL
711     case EXFULL: return "message tables full";
712 #endif
713     }
714 #else /* NO_SYS_ERRLIST */
715   extern int sys_nerr;
716   extern char *sys_errlist[];
717
718   if ((errnum > 0) && (errnum <= sys_nerr))
719     return sys_errlist [errnum];
720 #endif /* NO_SYS_ERRLIST */
721
722   msg = g_static_private_get (&msg_private);
723   if (!msg)
724     {
725       msg = g_new (gchar, 64);
726       g_static_private_set (&msg_private, msg, g_free);
727     }
728
729   sprintf (msg, "unknown error (%d)", errnum);
730
731   return msg;
732 }
733
734 G_CONST_RETURN gchar*
735 g_strsignal (gint signum)
736 {
737   static GStaticPrivate msg_private = G_STATIC_PRIVATE_INIT;
738   char *msg;
739
740 #ifdef HAVE_STRSIGNAL
741 #if defined(G_OS_BEOS) || defined(G_WITH_CYGWIN)
742 extern const char *strsignal(int);
743 #else
744   /* this is declared differently (const) in string.h on BeOS */
745   extern char *strsignal (int sig);
746 #endif /* !G_OS_BEOS && !G_WITH_CYGWIN */
747   return strsignal (signum);
748 #elif NO_SYS_SIGLIST
749   switch (signum)
750     {
751 #ifdef SIGHUP
752     case SIGHUP: return "Hangup";
753 #endif
754 #ifdef SIGINT
755     case SIGINT: return "Interrupt";
756 #endif
757 #ifdef SIGQUIT
758     case SIGQUIT: return "Quit";
759 #endif
760 #ifdef SIGILL
761     case SIGILL: return "Illegal instruction";
762 #endif
763 #ifdef SIGTRAP
764     case SIGTRAP: return "Trace/breakpoint trap";
765 #endif
766 #ifdef SIGABRT
767     case SIGABRT: return "IOT trap/Abort";
768 #endif
769 #ifdef SIGBUS
770     case SIGBUS: return "Bus error";
771 #endif
772 #ifdef SIGFPE
773     case SIGFPE: return "Floating point exception";
774 #endif
775 #ifdef SIGKILL
776     case SIGKILL: return "Killed";
777 #endif
778 #ifdef SIGUSR1
779     case SIGUSR1: return "User defined signal 1";
780 #endif
781 #ifdef SIGSEGV
782     case SIGSEGV: return "Segmentation fault";
783 #endif
784 #ifdef SIGUSR2
785     case SIGUSR2: return "User defined signal 2";
786 #endif
787 #ifdef SIGPIPE
788     case SIGPIPE: return "Broken pipe";
789 #endif
790 #ifdef SIGALRM
791     case SIGALRM: return "Alarm clock";
792 #endif
793 #ifdef SIGTERM
794     case SIGTERM: return "Terminated";
795 #endif
796 #ifdef SIGSTKFLT
797     case SIGSTKFLT: return "Stack fault";
798 #endif
799 #ifdef SIGCHLD
800     case SIGCHLD: return "Child exited";
801 #endif
802 #ifdef SIGCONT
803     case SIGCONT: return "Continued";
804 #endif
805 #ifdef SIGSTOP
806     case SIGSTOP: return "Stopped (signal)";
807 #endif
808 #ifdef SIGTSTP
809     case SIGTSTP: return "Stopped";
810 #endif
811 #ifdef SIGTTIN
812     case SIGTTIN: return "Stopped (tty input)";
813 #endif
814 #ifdef SIGTTOU
815     case SIGTTOU: return "Stopped (tty output)";
816 #endif
817 #ifdef SIGURG
818     case SIGURG: return "Urgent condition";
819 #endif
820 #ifdef SIGXCPU
821     case SIGXCPU: return "CPU time limit exceeded";
822 #endif
823 #ifdef SIGXFSZ
824     case SIGXFSZ: return "File size limit exceeded";
825 #endif
826 #ifdef SIGVTALRM
827     case SIGVTALRM: return "Virtual time alarm";
828 #endif
829 #ifdef SIGPROF
830     case SIGPROF: return "Profile signal";
831 #endif
832 #ifdef SIGWINCH
833     case SIGWINCH: return "Window size changed";
834 #endif
835 #ifdef SIGIO
836     case SIGIO: return "Possible I/O";
837 #endif
838 #ifdef SIGPWR
839     case SIGPWR: return "Power failure";
840 #endif
841 #ifdef SIGUNUSED
842     case SIGUNUSED: return "Unused signal";
843 #endif
844     }
845 #else /* NO_SYS_SIGLIST */
846
847 #ifdef NO_SYS_SIGLIST_DECL
848   extern char *sys_siglist[];   /*(see Tue Jan 19 00:44:24 1999 in changelog)*/
849 #endif
850
851   return (char*) /* this function should return const --josh */ sys_siglist [signum];
852 #endif /* NO_SYS_SIGLIST */
853
854   msg = g_static_private_get (&msg_private);
855   if (!msg)
856     {
857       msg = g_new (gchar, 64);
858       g_static_private_set (&msg_private, msg, g_free);
859     }
860
861   sprintf (msg, "unknown signal (%d)", signum);
862   
863   return msg;
864 }
865
866 /* Functions g_strlcpy and g_strlcat were originally developed by
867  * Todd C. Miller <Todd.Miller@courtesan.com> to simplify writing secure code.
868  * See ftp://ftp.openbsd.org/pub/OpenBSD/src/lib/libc/string/strlcpy.3
869  * for more information.
870  */
871
872 #ifdef HAVE_STRLCPY
873 /* Use the native ones, if available; they might be implemented in assembly */
874 gsize
875 g_strlcpy (gchar       *dest,
876            const gchar *src,
877            gsize        dest_size)
878 {
879   g_return_val_if_fail (dest != NULL, 0);
880   g_return_val_if_fail (src  != NULL, 0);
881   
882   return strlcpy (dest, src, dest_size);
883 }
884
885 gsize
886 g_strlcat (gchar       *dest,
887            const gchar *src,
888            gsize        dest_size)
889 {
890   g_return_val_if_fail (dest != NULL, 0);
891   g_return_val_if_fail (src  != NULL, 0);
892   
893   return strlcat (dest, src, dest_size);
894 }
895
896 #else /* ! HAVE_STRLCPY */
897 /* g_strlcpy
898  *
899  * Copy string src to buffer dest (of buffer size dest_size).  At most
900  * dest_size-1 characters will be copied.  Always NUL terminates
901  * (unless dest_size == 0).  This function does NOT allocate memory.
902  * Unlike strncpy, this function doesn't pad dest (so it's often faster).
903  * Returns size of attempted result, strlen(src),
904  * so if retval >= dest_size, truncation occurred.
905  */
906 gsize
907 g_strlcpy (gchar       *dest,
908            const gchar *src,
909            gsize        dest_size)
910 {
911   register gchar *d = dest;
912   register const gchar *s = src;
913   register gsize n = dest_size;
914   
915   g_return_val_if_fail (dest != NULL, 0);
916   g_return_val_if_fail (src  != NULL, 0);
917   
918   /* Copy as many bytes as will fit */
919   if (n != 0 && --n != 0)
920     do
921       {
922         register gchar c = *s++;
923         
924         *d++ = c;
925         if (c == 0)
926           break;
927       }
928     while (--n != 0);
929   
930   /* If not enough room in dest, add NUL and traverse rest of src */
931   if (n == 0)
932     {
933       if (dest_size != 0)
934         *d = 0;
935       while (*s++)
936         ;
937     }
938   
939   return s - src - 1;  /* count does not include NUL */
940 }
941
942 /* g_strlcat
943  *
944  * Appends string src to buffer dest (of buffer size dest_size).
945  * At most dest_size-1 characters will be copied.
946  * Unlike strncat, dest_size is the full size of dest, not the space left over.
947  * This function does NOT allocate memory.
948  * This always NUL terminates (unless siz == 0 or there were no NUL characters
949  * in the dest_size characters of dest to start with).
950  * Returns size of attempted result, which is
951  * MIN (dest_size, strlen (original dest)) + strlen (src),
952  * so if retval >= dest_size, truncation occurred.
953  */
954 gsize
955 g_strlcat (gchar       *dest,
956            const gchar *src,
957            gsize        dest_size)
958 {
959   register gchar *d = dest;
960   register const gchar *s = src;
961   register gsize bytes_left = dest_size;
962   gsize dlength;  /* Logically, MIN (strlen (d), dest_size) */
963   
964   g_return_val_if_fail (dest != NULL, 0);
965   g_return_val_if_fail (src  != NULL, 0);
966   
967   /* Find the end of dst and adjust bytes left but don't go past end */
968   while (*d != 0 && bytes_left-- != 0)
969     d++;
970   dlength = d - dest;
971   bytes_left = dest_size - dlength;
972   
973   if (bytes_left == 0)
974     return dlength + strlen (s);
975   
976   while (*s != 0)
977     {
978       if (bytes_left != 1)
979         {
980           *d++ = *s;
981           bytes_left--;
982         }
983       s++;
984     }
985   *d = 0;
986   
987   return dlength + (s - src);  /* count does not include NUL */
988 }
989 #endif /* ! HAVE_STRLCPY */
990
991 /**
992  * g_ascii_strdown:
993  * @string: a string
994  * 
995  * Converts all upper case ASCII letters to lower case ASCII letters.
996  * 
997  * Return value: a newly allocated string, with all the upper case
998  *               characters in @string converted to lower case, with
999  *               semantics that exactly match g_ascii_tolower. (Note
1000  *               that this is unlike the old g_strdown, which modified
1001  *               the string in place.)
1002  **/
1003 gchar*
1004 g_ascii_strdown (const gchar *string)
1005 {
1006   gchar *result, *s;
1007   
1008   g_return_val_if_fail (string != NULL, NULL);
1009
1010   result = g_strdup (string);
1011   for (s = result; *s; s++)
1012     *s = g_ascii_tolower (*s);
1013   
1014   return result;
1015 }
1016
1017 /**
1018  * g_ascii_strup:
1019  * @string: a string
1020  * 
1021  * Converts all lower case ASCII letters to upper case ASCII letters.
1022  * 
1023  * Return value: a newly allocated string, with all the lower case
1024  *               characters in @string converted to upper case, with
1025  *               semantics that exactly match g_ascii_toupper. (Note
1026  *               that this is unlike the old g_strup, which modified
1027  *               the string in place.)
1028  **/
1029 gchar*
1030 g_ascii_strup (const gchar *string)
1031 {
1032   gchar *result, *s;
1033
1034   g_return_val_if_fail (string != NULL, NULL);
1035
1036   result = g_strdup (string);
1037   for (s = result; *s; s++)
1038     *s = g_ascii_toupper (*s);
1039
1040   return result;
1041 }
1042
1043 gchar*
1044 g_strdown (gchar *string)
1045 {
1046   register guchar *s;
1047   
1048   g_return_val_if_fail (string != NULL, NULL);
1049   
1050   s = (guchar *) string;
1051   
1052   while (*s)
1053     {
1054       if (isupper (*s))
1055         *s = tolower (*s);
1056       s++;
1057     }
1058   
1059   return (gchar *) string;
1060 }
1061
1062 gchar*
1063 g_strup (gchar *string)
1064 {
1065   register guchar *s;
1066
1067   g_return_val_if_fail (string != NULL, NULL);
1068
1069   s = (guchar *) string;
1070
1071   while (*s)
1072     {
1073       if (islower (*s))
1074         *s = toupper (*s);
1075       s++;
1076     }
1077
1078   return (gchar *) string;
1079 }
1080
1081 gchar*
1082 g_strreverse (gchar *string)
1083 {
1084   g_return_val_if_fail (string != NULL, NULL);
1085
1086   if (*string)
1087     {
1088       register gchar *h, *t;
1089
1090       h = string;
1091       t = string + strlen (string) - 1;
1092
1093       while (h < t)
1094         {
1095           register gchar c;
1096
1097           c = *h;
1098           *h = *t;
1099           h++;
1100           *t = c;
1101           t--;
1102         }
1103     }
1104
1105   return string;
1106 }
1107
1108 /**
1109  * g_ascii_tolower:
1110  * @c: any character
1111  * 
1112  * Convert a character to ASCII lower case.
1113  *
1114  * Unlike the standard C library tolower function, this only
1115  * recognizes standard ASCII letters and ignores the locale, returning
1116  * all non-ASCII characters unchanged, even if they are lower case
1117  * letters in a particular character set. Also unlike the standard
1118  * library function, this takes and returns a char, not an int, so
1119  * don't call it on EOF but no need to worry about casting to guchar
1120  * before passing a possibly non-ASCII character in.
1121  * 
1122  * Return value: the result of converting @c to lower case.
1123  *               If @c is not an ASCII upper case letter,
1124  *               @c is returned unchanged.
1125  **/
1126 gchar
1127 g_ascii_tolower (gchar c)
1128 {
1129   return g_ascii_isupper (c) ? c - 'A' + 'a' : c;
1130 }
1131
1132 /**
1133  * g_ascii_toupper:
1134  * @c: any character
1135  * 
1136  * Convert a character to ASCII upper case.
1137  *
1138  * Unlike the standard C library toupper function, this only
1139  * recognizes standard ASCII letters and ignores the locale, returning
1140  * all non-ASCII characters unchanged, even if they are upper case
1141  * letters in a particular character set. Also unlike the standard
1142  * library function, this takes and returns a char, not an int, so
1143  * don't call it on EOF but no need to worry about casting to guchar
1144  * before passing a possibly non-ASCII character in.
1145  * 
1146  * Return value: the result of converting @c to upper case.
1147  *               If @c is not an ASCII lower case letter,
1148  *               @c is returned unchanged.
1149  **/
1150 gchar
1151 g_ascii_toupper (gchar c)
1152 {
1153   return g_ascii_islower (c) ? c - 'a' + 'A' : c;
1154 }
1155
1156 /**
1157  * g_ascii_digit_value:
1158  * @c: an ASCII character
1159  *
1160  * Determines the numeric value of a character as a decimal
1161  * digit. Differs from g_unichar_digit_value because it takes
1162  * a char, so there's no worry about sign extension if characters
1163  * are signed.
1164  *
1165  * Return value: If @c is a decimal digit (according to
1166  * `g_ascii_isdigit'), its numeric value. Otherwise, -1.
1167  **/
1168 int
1169 g_ascii_digit_value (gchar c)
1170 {
1171   if (g_ascii_isdigit (c))
1172     return c - '0';
1173   return -1;
1174 }
1175
1176 /**
1177  * g_ascii_xdigit_value:
1178  * @c: an ASCII character
1179  *
1180  * Determines the numeric value of a character as a hexidecimal
1181  * digit. Differs from g_unichar_xdigit_value because it takes
1182  * a char, so there's no worry about sign extension if characters
1183  * are signed.
1184  *
1185  * Return value: If @c is a hex digit (according to
1186  * `g_ascii_isxdigit'), its numeric value. Otherwise, -1.
1187  **/
1188 int
1189 g_ascii_xdigit_value (gchar c)
1190 {
1191   if (c >= 'A' && c <= 'F')
1192     return c - 'A' + 10;
1193   if (c >= 'a' && c <= 'f')
1194     return c - 'a' + 10;
1195   return g_ascii_digit_value (c);
1196 }
1197
1198 /**
1199  * g_ascii_strcasecmp:
1200  * @s1: string to compare with @s2
1201  * @s2: string to compare with @s1
1202  * 
1203  * Compare two strings, ignoring the case of ASCII characters.
1204  *
1205  * Unlike the BSD strcasecmp function, this only recognizes standard
1206  * ASCII letters and ignores the locale, treating all non-ASCII
1207  * characters as if they are not letters.
1208  * 
1209  * Return value: an integer less than, equal to, or greater than
1210  *               zero if @s1 is found, respectively, to be less than,
1211  *               to match, or to be greater than @s2.
1212  **/
1213 gint
1214 g_ascii_strcasecmp (const gchar *s1,
1215                     const gchar *s2)
1216 {
1217   gint c1, c2;
1218
1219   g_return_val_if_fail (s1 != NULL, 0);
1220   g_return_val_if_fail (s2 != NULL, 0);
1221
1222   while (*s1 && *s2)
1223     {
1224       c1 = (gint)(guchar) g_ascii_tolower (*s1);
1225       c2 = (gint)(guchar) g_ascii_tolower (*s2);
1226       if (c1 != c2)
1227         return (c1 - c2);
1228       s1++; s2++;
1229     }
1230
1231   return (((gint)(guchar) *s1) - ((gint)(guchar) *s2));
1232 }
1233
1234 /**
1235  * g_ascii_strncasecmp:
1236  * @s1: string to compare with @s2
1237  * @s2: string to compare with @s1
1238  * @n:  number of characters to compare
1239  * 
1240  * Compare @s1 and @s2, ignoring the case of ASCII characters and any
1241  * characters after the first @n in each string.
1242  *
1243  * Unlike the BSD strcasecmp function, this only recognizes standard
1244  * ASCII letters and ignores the locale, treating all non-ASCII
1245  * characters as if they are not letters.
1246  * 
1247  * Return value: an integer less than, equal to, or greater than zero
1248  *               if the first @n bytes of @s1 is found, respectively,
1249  *               to be less than, to match, or to be greater than the
1250  *               first @n bytes of @s2.
1251  **/
1252 gint
1253 g_ascii_strncasecmp (const gchar *s1,
1254                      const gchar *s2,
1255                      guint n)
1256 {
1257   gint c1, c2;
1258
1259   g_return_val_if_fail (s1 != NULL, 0);
1260   g_return_val_if_fail (s2 != NULL, 0);
1261
1262   while (n && *s1 && *s2)
1263     {
1264       n -= 1;
1265       c1 = (gint)(guchar) g_ascii_tolower (*s1);
1266       c2 = (gint)(guchar) g_ascii_tolower (*s2);
1267       if (c1 != c2)
1268         return (c1 - c2);
1269       s1++; s2++;
1270     }
1271
1272   if (n)
1273     return (((gint) (guchar) *s1) - ((gint) (guchar) *s2));
1274   else
1275     return 0;
1276 }
1277
1278 gint
1279 g_strcasecmp (const gchar *s1,
1280               const gchar *s2)
1281 {
1282 #ifdef HAVE_STRCASECMP
1283   g_return_val_if_fail (s1 != NULL, 0);
1284   g_return_val_if_fail (s2 != NULL, 0);
1285
1286   return strcasecmp (s1, s2);
1287 #else
1288   gint c1, c2;
1289
1290   g_return_val_if_fail (s1 != NULL, 0);
1291   g_return_val_if_fail (s2 != NULL, 0);
1292
1293   while (*s1 && *s2)
1294     {
1295       /* According to A. Cox, some platforms have islower's that
1296        * don't work right on non-uppercase
1297        */
1298       c1 = isupper ((guchar)*s1) ? tolower ((guchar)*s1) : *s1;
1299       c2 = isupper ((guchar)*s2) ? tolower ((guchar)*s2) : *s2;
1300       if (c1 != c2)
1301         return (c1 - c2);
1302       s1++; s2++;
1303     }
1304
1305   return (((gint)(guchar) *s1) - ((gint)(guchar) *s2));
1306 #endif
1307 }
1308
1309 gint
1310 g_strncasecmp (const gchar *s1,
1311                const gchar *s2,
1312                gsize n)     
1313 {
1314 #ifdef HAVE_STRNCASECMP
1315   return strncasecmp (s1, s2, n);
1316 #else
1317   gint c1, c2;
1318
1319   g_return_val_if_fail (s1 != NULL, 0);
1320   g_return_val_if_fail (s2 != NULL, 0);
1321
1322   while (n && *s1 && *s2)
1323     {
1324       n -= 1;
1325       /* According to A. Cox, some platforms have islower's that
1326        * don't work right on non-uppercase
1327        */
1328       c1 = isupper ((guchar)*s1) ? tolower ((guchar)*s1) : *s1;
1329       c2 = isupper ((guchar)*s2) ? tolower ((guchar)*s2) : *s2;
1330       if (c1 != c2)
1331         return (c1 - c2);
1332       s1++; s2++;
1333     }
1334
1335   if (n)
1336     return (((gint) (guchar) *s1) - ((gint) (guchar) *s2));
1337   else
1338     return 0;
1339 #endif
1340 }
1341
1342 gchar*
1343 g_strdelimit (gchar       *string,
1344               const gchar *delimiters,
1345               gchar        new_delim)
1346 {
1347   register gchar *c;
1348
1349   g_return_val_if_fail (string != NULL, NULL);
1350
1351   if (!delimiters)
1352     delimiters = G_STR_DELIMITERS;
1353
1354   for (c = string; *c; c++)
1355     {
1356       if (strchr (delimiters, *c))
1357         *c = new_delim;
1358     }
1359
1360   return string;
1361 }
1362
1363 gchar*
1364 g_strcanon (gchar       *string,
1365             const gchar *valid_chars,
1366             gchar        substitutor)
1367 {
1368   register gchar *c;
1369
1370   g_return_val_if_fail (string != NULL, NULL);
1371   g_return_val_if_fail (valid_chars != NULL, NULL);
1372
1373   for (c = string; *c; c++)
1374     {
1375       if (!strchr (valid_chars, *c))
1376         *c = substitutor;
1377     }
1378
1379   return string;
1380 }
1381
1382 gchar*
1383 g_strcompress (const gchar *source)
1384 {
1385   const gchar *p = source, *octal;
1386   gchar *dest = g_malloc (strlen (source) + 1);
1387   gchar *q = dest;
1388   
1389   while (*p)
1390     {
1391       if (*p == '\\')
1392         {
1393           p++;
1394           switch (*p)
1395             {
1396             case '0':  case '1':  case '2':  case '3':  case '4':
1397             case '5':  case '6':  case '7':
1398               *q = 0;
1399               octal = p;
1400               while ((p < octal + 3) && (*p >= '0') && (*p <= '7'))
1401                 {
1402                   *q = (*q * 8) + (*p - '0');
1403                   p++;
1404                 }
1405               q++;
1406               p--;
1407               break;
1408             case 'b':
1409               *q++ = '\b';
1410               break;
1411             case 'f':
1412               *q++ = '\f';
1413               break;
1414             case 'n':
1415               *q++ = '\n';
1416               break;
1417             case 'r':
1418               *q++ = '\r';
1419               break;
1420             case 't':
1421               *q++ = '\t';
1422               break;
1423             default:            /* Also handles \" and \\ */
1424               *q++ = *p;
1425               break;
1426             }
1427         }
1428       else
1429         *q++ = *p;
1430       p++;
1431     }
1432   *q = 0;
1433   
1434   return dest;
1435 }
1436
1437 gchar *
1438 g_strescape (const gchar *source,
1439              const gchar *exceptions)
1440 {
1441   const guchar *p;
1442   gchar *dest;
1443   gchar *q;
1444   guchar excmap[256];
1445   
1446   g_return_val_if_fail (source != NULL, NULL);
1447
1448   p = (guchar *) source;
1449   /* Each source byte needs maximally four destination chars (\777) */
1450   q = dest = g_malloc (strlen (source) * 4 + 1);
1451
1452   memset (excmap, 0, 256);
1453   if (exceptions)
1454     {
1455       guchar *e = (guchar *) exceptions;
1456
1457       while (*e)
1458         {
1459           excmap[*e] = 1;
1460           e++;
1461         }
1462     }
1463
1464   while (*p)
1465     {
1466       if (excmap[*p])
1467         *q++ = *p;
1468       else
1469         {
1470           switch (*p)
1471             {
1472             case '\b':
1473               *q++ = '\\';
1474               *q++ = 'b';
1475               break;
1476             case '\f':
1477               *q++ = '\\';
1478               *q++ = 'f';
1479               break;
1480             case '\n':
1481               *q++ = '\\';
1482               *q++ = 'n';
1483               break;
1484             case '\r':
1485               *q++ = '\\';
1486               *q++ = 'r';
1487               break;
1488             case '\t':
1489               *q++ = '\\';
1490               *q++ = 't';
1491               break;
1492             case '\\':
1493               *q++ = '\\';
1494               *q++ = '\\';
1495               break;
1496             case '"':
1497               *q++ = '\\';
1498               *q++ = '"';
1499               break;
1500             default:
1501               if ((*p < ' ') || (*p >= 0177))
1502                 {
1503                   *q++ = '\\';
1504                   *q++ = '0' + (((*p) >> 6) & 07);
1505                   *q++ = '0' + (((*p) >> 3) & 07);
1506                   *q++ = '0' + ((*p) & 07);
1507                 }
1508               else
1509                 *q++ = *p;
1510               break;
1511             }
1512         }
1513       p++;
1514     }
1515   *q = 0;
1516   return dest;
1517 }
1518
1519 gchar*
1520 g_strchug (gchar *string)
1521 {
1522   guchar *start;
1523
1524   g_return_val_if_fail (string != NULL, NULL);
1525
1526   for (start = (guchar*) string; *start && g_ascii_isspace (*start); start++)
1527     ;
1528
1529   g_memmove (string, start, strlen ((gchar *) start) + 1);
1530
1531   return string;
1532 }
1533
1534 gchar*
1535 g_strchomp (gchar *string)
1536 {
1537   gchar *s;
1538
1539   g_return_val_if_fail (string != NULL, NULL);
1540
1541   if (!*string)
1542     return string;
1543
1544   for (s = string + strlen (string) - 1; s >= string && g_ascii_isspace ((guchar)*s); 
1545        s--)
1546     *s = '\0';
1547
1548   return string;
1549 }
1550
1551 /**
1552  * g_strsplit:
1553  * @string: a string to split.
1554  * @delimiter: a string which specifies the places at which to split the string.
1555  *     The delimiter is not included in any of the resulting strings, unless
1556  *     max_tokens is reached.
1557  * @max_tokens: the maximum number of pieces to split @string into. If this is
1558  *              less than 1, the string is split completely.
1559  * 
1560  * Splits a string into a maximum of @max_tokens pieces, using the given
1561  * @delimiter. If @max_tokens is reached, the remainder of @string is appended
1562  * to the last token. 
1563  *
1564  * As a special case, the result of splitting the empty string "" is an empty
1565  * vector, not a vector containing a single string. The reason for this
1566  * special case is that being able to represent a empty vector is typically
1567  * more useful than consistent handling of empty elements. If you do need
1568  * to represent empty elements, you'll need to check for the empty string
1569  * before calling g_strsplit().
1570  * 
1571  * Return value: a newly-allocated %NULL-terminated array of strings. Use g_strfreev()
1572  *    to free it.
1573  **/
1574 gchar**
1575 g_strsplit (const gchar *string,
1576             const gchar *delimiter,
1577             gint         max_tokens)
1578 {
1579   GSList *string_list = NULL, *slist;
1580   gchar **str_array, *s;
1581   guint n = 0;
1582   const gchar *remainder;
1583
1584   g_return_val_if_fail (string != NULL, NULL);
1585   g_return_val_if_fail (delimiter != NULL, NULL);
1586   g_return_val_if_fail (delimiter[0] != '\0', NULL);
1587
1588   if (max_tokens < 1)
1589     max_tokens = G_MAXINT;
1590   else
1591     --max_tokens;
1592
1593   remainder = string;
1594   s = strstr (remainder, delimiter);
1595   if (s)
1596     {
1597       gsize delimiter_len = strlen (delimiter);   
1598
1599       do
1600         {
1601           gsize len;     
1602           gchar *new_string;
1603
1604           len = s - remainder;
1605           new_string = g_new (gchar, len + 1);
1606           strncpy (new_string, remainder, len);
1607           new_string[len] = 0;
1608           string_list = g_slist_prepend (string_list, new_string);
1609           n++;
1610           remainder = s + delimiter_len;
1611           s = strstr (remainder, delimiter);
1612         }
1613       while (--max_tokens && s);
1614     }
1615   if (*string)
1616     {
1617       n++;
1618       string_list = g_slist_prepend (string_list, g_strdup (remainder));
1619     }
1620
1621   str_array = g_new (gchar*, n + 1);
1622
1623   str_array[n--] = NULL;
1624   for (slist = string_list; slist; slist = slist->next)
1625     str_array[n--] = slist->data;
1626
1627   g_slist_free (string_list);
1628
1629   return str_array;
1630 }
1631
1632 void
1633 g_strfreev (gchar **str_array)
1634 {
1635   if (str_array)
1636     {
1637       int i;
1638
1639       for(i = 0; str_array[i] != NULL; i++)
1640         g_free(str_array[i]);
1641
1642       g_free (str_array);
1643     }
1644 }
1645
1646 /**
1647  * g_strdupv:
1648  * @str_array: %NULL-terminated array of strings
1649  * 
1650  * Copies %NULL-terminated array of strings. The copy is a deep copy;
1651  * the new array should be freed by first freeing each string, then
1652  * the array itself. g_strfreev() does this for you. If called
1653  * on a %NULL value, g_strdupv() simply returns %NULL.
1654  * 
1655  * Return value: a new %NULL-terminated array of strings
1656  **/
1657 gchar**
1658 g_strdupv (gchar **str_array)
1659 {
1660   if (str_array)
1661     {
1662       gint i;
1663       gchar **retval;
1664
1665       i = 0;
1666       while (str_array[i])
1667         ++i;
1668           
1669       retval = g_new (gchar*, i + 1);
1670
1671       i = 0;
1672       while (str_array[i])
1673         {
1674           retval[i] = g_strdup (str_array[i]);
1675           ++i;
1676         }
1677       retval[i] = NULL;
1678
1679       return retval;
1680     }
1681   else
1682     return NULL;
1683 }
1684
1685 gchar*
1686 g_strjoinv (const gchar  *separator,
1687             gchar       **str_array)
1688 {
1689   gchar *string;
1690   gchar *ptr;
1691
1692   g_return_val_if_fail (str_array != NULL, NULL);
1693
1694   if (separator == NULL)
1695     separator = "";
1696
1697   if (*str_array)
1698     {
1699       gint i;
1700       gsize len;
1701       gsize separator_len;     
1702
1703       separator_len = strlen (separator);
1704       /* First part, getting length */
1705       len = 1 + strlen (str_array[0]);
1706       for (i = 1; str_array[i] != NULL; i++)
1707         len += strlen (str_array[i]);
1708       len += separator_len * (i - 1);
1709
1710       /* Second part, building string */
1711       string = g_new (gchar, len);
1712       ptr = g_stpcpy (string, *str_array);
1713       for (i = 1; str_array[i] != NULL; i++)
1714         {
1715           ptr = g_stpcpy (ptr, separator);
1716           ptr = g_stpcpy (ptr, str_array[i]);
1717         }
1718       }
1719   else
1720     string = g_strdup ("");
1721
1722   return string;
1723 }
1724
1725 gchar*
1726 g_strjoin (const gchar  *separator,
1727            ...)
1728 {
1729   gchar *string, *s;
1730   va_list args;
1731   gsize len;               
1732   gsize separator_len;     
1733   gchar *ptr;
1734
1735   if (separator == NULL)
1736     separator = "";
1737
1738   separator_len = strlen (separator);
1739
1740   va_start (args, separator);
1741
1742   s = va_arg (args, gchar*);
1743
1744   if (s)
1745     {
1746       /* First part, getting length */
1747       len = 1 + strlen (s);
1748
1749       s = va_arg (args, gchar*);
1750       while (s)
1751         {
1752           len += separator_len + strlen (s);
1753           s = va_arg (args, gchar*);
1754         }
1755       va_end (args);
1756
1757       /* Second part, building string */
1758       string = g_new (gchar, len);
1759
1760       va_start (args, separator);
1761
1762       s = va_arg (args, gchar*);
1763       ptr = g_stpcpy (string, s);
1764
1765       s = va_arg (args, gchar*);
1766       while (s)
1767         {
1768           ptr = g_stpcpy (ptr, separator);
1769           ptr = g_stpcpy (ptr, s);
1770           s = va_arg (args, gchar*);
1771         }
1772     }
1773   else
1774     string = g_strdup ("");
1775
1776   va_end (args);
1777
1778   return string;
1779 }
1780
1781
1782 /**
1783  * g_strstr_len:
1784  * @haystack: a string
1785  * @haystack_len: The maximum length of haystack
1786  * @needle: The string to search for.
1787  *
1788  * Searches the string haystack for the first occurrence
1789  * of the string needle, limiting the length of the search
1790  * to haystack_len. 
1791  *
1792  * Return value: A pointer to the found occurrence, or
1793  * NULL if not found.
1794  **/
1795 gchar *
1796 g_strstr_len (const gchar *haystack,
1797               gssize       haystack_len,
1798               const gchar *needle)
1799 {
1800   g_return_val_if_fail (haystack != NULL, NULL);
1801   g_return_val_if_fail (needle != NULL, NULL);
1802   
1803   if (haystack_len < 0)
1804     return strstr (haystack, needle);
1805   else
1806     {
1807       const gchar *p = haystack;
1808       gsize needle_len = strlen (needle);
1809       const gchar *end;
1810       gsize i;
1811
1812       if (needle_len == 0)
1813         return (gchar *)haystack;
1814
1815       if (haystack_len < needle_len)
1816         return NULL;
1817       
1818       end = haystack + haystack_len - needle_len;
1819       
1820       while (*p && p <= end)
1821         {
1822           for (i = 0; i < needle_len; i++)
1823             if (p[i] != needle[i])
1824               goto next;
1825           
1826           return (gchar *)p;
1827           
1828         next:
1829           p++;
1830         }
1831       
1832       return NULL;
1833     }
1834 }
1835
1836 /**
1837  * g_strrstr_len:
1838  * @haystack: a nul-terminated string
1839  * @needle: The nul-terminated string to search for.
1840  *
1841  * Searches the string haystack for the last occurrence
1842  * of the string needle.
1843  *
1844  * Return value: A pointer to the found occurrence, or
1845  * NULL if not found.
1846  **/
1847 gchar *
1848 g_strrstr (const gchar *haystack,
1849            const gchar *needle)
1850 {
1851   gsize i;
1852   gsize needle_len;
1853   gsize haystack_len;
1854   const gchar *p;
1855       
1856   g_return_val_if_fail (haystack != NULL, NULL);
1857   g_return_val_if_fail (needle != NULL, NULL);
1858
1859   needle_len = strlen (needle);
1860   haystack_len = strlen (haystack);
1861
1862   if (needle_len == 0)
1863     return (gchar *)haystack;
1864
1865   if (haystack_len < needle_len)
1866     return NULL;
1867   
1868   p = haystack + haystack_len - needle_len;
1869
1870   while (p >= haystack)
1871     {
1872       for (i = 0; i < needle_len; i++)
1873         if (p[i] != needle[i])
1874           goto next;
1875       
1876       return (gchar *)p;
1877       
1878     next:
1879       p--;
1880     }
1881   
1882   return NULL;
1883 }
1884
1885 /**
1886  * g_strrstr_len:
1887  * @haystack: a nul-terminated string
1888  * @haystack_len: The maximum length of haystack
1889  * @needle: The nul-terminated string to search for.
1890  *
1891  * Searches the string haystack for the last occurrence
1892  * of the string needle, limiting the length of the search
1893  * to haystack_len. 
1894  *
1895  * Return value: A pointer to the found occurrence, or
1896  * NULL if not found.
1897  **/
1898 gchar *
1899 g_strrstr_len (const gchar *haystack,
1900                gssize        haystack_len,
1901                const gchar *needle)
1902 {
1903   g_return_val_if_fail (haystack != NULL, NULL);
1904   g_return_val_if_fail (needle != NULL, NULL);
1905   
1906   if (haystack_len < 0)
1907     return g_strrstr (haystack, needle);
1908   else
1909     {
1910       gsize needle_len = strlen (needle);
1911       const gchar *haystack_max = haystack + haystack_len;
1912       const gchar *p = haystack;
1913       gsize i;
1914
1915       while (p < haystack_max && *p)
1916         p++;
1917
1918       if (p < haystack + needle_len)
1919         return NULL;
1920         
1921       p -= needle_len;
1922
1923       while (p >= haystack)
1924         {
1925           for (i = 0; i < needle_len; i++)
1926             if (p[i] != needle[i])
1927               goto next;
1928           
1929           return (gchar *)p;
1930           
1931         next:
1932           p--;
1933         }
1934
1935       return NULL;
1936     }
1937 }
1938
1939