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