[REVERT] Bug 548612 – g_strstr_len() should use memmem when available
[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 #include "config.h"
32
33 #define _GNU_SOURCE             /* For stpcpy */
34
35 #include <stdarg.h>
36 #include <stdio.h>
37 #include <stdlib.h>
38 #include <string.h>
39 #include <locale.h>
40 #include <errno.h>
41 #include <ctype.h>              /* For tolower() */
42 #if !defined (HAVE_STRSIGNAL) || !defined(NO_SYS_SIGLIST_DECL)
43 #include <signal.h>
44 #endif
45
46 #include "glib.h"
47 #include "gprintf.h"
48 #include "gprintfint.h"
49 #include "glibintl.h"
50
51 #include "galias.h"
52
53 #ifdef G_OS_WIN32
54 #include <windows.h>
55 #endif
56
57 /* do not include <unistd.h> in this place since it
58  * interferes with g_strsignal() on some OSes
59  */
60
61 static const guint16 ascii_table_data[256] = {
62   0x004, 0x004, 0x004, 0x004, 0x004, 0x004, 0x004, 0x004,
63   0x004, 0x104, 0x104, 0x004, 0x104, 0x104, 0x004, 0x004,
64   0x004, 0x004, 0x004, 0x004, 0x004, 0x004, 0x004, 0x004,
65   0x004, 0x004, 0x004, 0x004, 0x004, 0x004, 0x004, 0x004,
66   0x140, 0x0d0, 0x0d0, 0x0d0, 0x0d0, 0x0d0, 0x0d0, 0x0d0,
67   0x0d0, 0x0d0, 0x0d0, 0x0d0, 0x0d0, 0x0d0, 0x0d0, 0x0d0,
68   0x459, 0x459, 0x459, 0x459, 0x459, 0x459, 0x459, 0x459,
69   0x459, 0x459, 0x0d0, 0x0d0, 0x0d0, 0x0d0, 0x0d0, 0x0d0,
70   0x0d0, 0x653, 0x653, 0x653, 0x653, 0x653, 0x653, 0x253,
71   0x253, 0x253, 0x253, 0x253, 0x253, 0x253, 0x253, 0x253,
72   0x253, 0x253, 0x253, 0x253, 0x253, 0x253, 0x253, 0x253,
73   0x253, 0x253, 0x253, 0x0d0, 0x0d0, 0x0d0, 0x0d0, 0x0d0,
74   0x0d0, 0x473, 0x473, 0x473, 0x473, 0x473, 0x473, 0x073,
75   0x073, 0x073, 0x073, 0x073, 0x073, 0x073, 0x073, 0x073,
76   0x073, 0x073, 0x073, 0x073, 0x073, 0x073, 0x073, 0x073,
77   0x073, 0x073, 0x073, 0x0d0, 0x0d0, 0x0d0, 0x0d0, 0x004
78   /* the upper 128 are all zeroes */
79 };
80
81 const guint16 * const g_ascii_table = ascii_table_data;
82
83 gchar*
84 g_strdup (const gchar *str)
85 {
86   gchar *new_str;
87   gsize length;
88
89   if (str)
90     {
91       length = strlen (str) + 1;
92       new_str = g_new (char, length);
93       memcpy (new_str, str, length);
94     }
95   else
96     new_str = NULL;
97
98   return new_str;
99 }
100
101 gpointer
102 g_memdup (gconstpointer mem,
103           guint         byte_size)
104 {
105   gpointer new_mem;
106
107   if (mem)
108     {
109       new_mem = g_malloc (byte_size);
110       memcpy (new_mem, mem, byte_size);
111     }
112   else
113     new_mem = NULL;
114
115   return new_mem;
116 }
117
118 /**
119  * g_strndup:
120  * @str: the string to duplicate
121  * @n: the maximum number of bytes to copy from @str
122  *
123  * Duplicates the first @n bytes of a string, returning a newly-allocated
124  * buffer @n + 1 bytes long which will always be nul-terminated.
125  * If @str is less than @n bytes long the buffer is padded with nuls.
126  * If @str is %NULL it returns %NULL.
127  * The returned value should be freed when no longer needed.
128  * 
129  * <note><para>
130  * To copy a number of characters from a UTF-8 encoded string, use
131  * g_utf8_strncpy() instead.
132  * </para></note>
133  * 
134  * Returns: a newly-allocated buffer containing the first @n bytes 
135  *          of @str, nul-terminated 
136  */
137 gchar*
138 g_strndup (const gchar *str,
139            gsize        n)    
140 {
141   gchar *new_str;
142
143   if (str)
144     {
145       new_str = g_new (gchar, n + 1);
146       strncpy (new_str, str, n);
147       new_str[n] = '\0';
148     }
149   else
150     new_str = NULL;
151
152   return new_str;
153 }
154
155 /**
156  * g_strnfill:
157  * @length: the length of the new string
158  * @fill_char: the byte to fill the string with
159  *
160  * Creates a new string @length bytes long filled with @fill_char.
161  * The returned string should be freed when no longer needed.
162  * 
163  * Returns: a newly-allocated string filled the @fill_char
164  */
165 gchar*
166 g_strnfill (gsize length,     
167             gchar fill_char)
168 {
169   gchar *str;
170
171   str = g_new (gchar, length + 1);
172   memset (str, (guchar)fill_char, length);
173   str[length] = '\0';
174
175   return str;
176 }
177
178 /**
179  * g_stpcpy:
180  * @dest: destination buffer.
181  * @src: source string.
182  * 
183  * Copies a nul-terminated string into the dest buffer, include the
184  * trailing nul, and return a pointer to the trailing nul byte.
185  * This is useful for concatenating multiple strings together
186  * without having to repeatedly scan for the end.
187  * 
188  * Return value: a pointer to trailing nul byte.
189  **/
190 gchar *
191 g_stpcpy (gchar       *dest,
192           const gchar *src)
193 {
194 #ifdef HAVE_STPCPY
195   g_return_val_if_fail (dest != NULL, NULL);
196   g_return_val_if_fail (src != NULL, NULL);
197   return stpcpy (dest, src);
198 #else
199   register gchar *d = dest;
200   register const gchar *s = src;
201
202   g_return_val_if_fail (dest != NULL, NULL);
203   g_return_val_if_fail (src != NULL, NULL);
204   do
205     *d++ = *s;
206   while (*s++ != '\0');
207
208   return d - 1;
209 #endif
210 }
211
212 gchar*
213 g_strdup_vprintf (const gchar *format,
214                   va_list      args)
215 {
216   gchar *string = NULL;
217
218   g_vasprintf (&string, format, args);
219
220   return string;
221 }
222
223 gchar*
224 g_strdup_printf (const gchar *format,
225                  ...)
226 {
227   gchar *buffer;
228   va_list args;
229
230   va_start (args, format);
231   buffer = g_strdup_vprintf (format, args);
232   va_end (args);
233
234   return buffer;
235 }
236
237 gchar*
238 g_strconcat (const gchar *string1, ...)
239 {
240   gsize   l;     
241   va_list args;
242   gchar   *s;
243   gchar   *concat;
244   gchar   *ptr;
245
246   if (!string1)
247     return NULL;
248
249   l = 1 + strlen (string1);
250   va_start (args, string1);
251   s = va_arg (args, gchar*);
252   while (s)
253     {
254       l += strlen (s);
255       s = va_arg (args, gchar*);
256     }
257   va_end (args);
258
259   concat = g_new (gchar, l);
260   ptr = concat;
261
262   ptr = g_stpcpy (ptr, string1);
263   va_start (args, string1);
264   s = va_arg (args, gchar*);
265   while (s)
266     {
267       ptr = g_stpcpy (ptr, s);
268       s = va_arg (args, gchar*);
269     }
270   va_end (args);
271
272   return concat;
273 }
274
275 /**
276  * g_strtod:
277  * @nptr:    the string to convert to a numeric value.
278  * @endptr:  if non-%NULL, it returns the character after
279  *           the last character used in the conversion.
280  * 
281  * Converts a string to a #gdouble value.
282  * It calls the standard strtod() function to handle the conversion, but
283  * if the string is not completely converted it attempts the conversion
284  * again with g_ascii_strtod(), and returns the best match.
285  *
286  * This function should seldomly be used. The normal situation when reading
287  * numbers not for human consumption is to use g_ascii_strtod(). Only when
288  * you know that you must expect both locale formatted and C formatted numbers
289  * should you use this. Make sure that you don't pass strings such as comma
290  * separated lists of values, since the commas may be interpreted as a decimal
291  * point in some locales, causing unexpected results.
292  * 
293  * Return value: the #gdouble value.
294  **/
295 gdouble
296 g_strtod (const gchar *nptr,
297           gchar      **endptr)
298 {
299   gchar *fail_pos_1;
300   gchar *fail_pos_2;
301   gdouble val_1;
302   gdouble val_2 = 0;
303
304   g_return_val_if_fail (nptr != NULL, 0);
305
306   fail_pos_1 = NULL;
307   fail_pos_2 = NULL;
308
309   val_1 = strtod (nptr, &fail_pos_1);
310
311   if (fail_pos_1 && fail_pos_1[0] != 0)
312     val_2 = g_ascii_strtod (nptr, &fail_pos_2);
313
314   if (!fail_pos_1 || fail_pos_1[0] == 0 || fail_pos_1 >= fail_pos_2)
315     {
316       if (endptr)
317         *endptr = fail_pos_1;
318       return val_1;
319     }
320   else
321     {
322       if (endptr)
323         *endptr = fail_pos_2;
324       return val_2;
325     }
326 }
327
328 /**
329  * g_ascii_strtod:
330  * @nptr:    the string to convert to a numeric value.
331  * @endptr:  if non-%NULL, it returns the character after
332  *           the last character used in the conversion.
333  * 
334  * Converts a string to a #gdouble value.
335  *
336  * This function behaves like the standard strtod() function
337  * does in the C locale. It does this without actually changing 
338  * the current locale, since that would not be thread-safe. 
339  * A limitation of the implementation is that this function
340  * will still accept localized versions of infinities and NANs. 
341  *
342  * This function is typically used when reading configuration
343  * files or other non-user input that should be locale independent.
344  * To handle input from the user you should normally use the
345  * locale-sensitive system strtod() function.
346  *
347  * To convert from a #gdouble to a string in a locale-insensitive
348  * way, use g_ascii_dtostr().
349  *
350  * If the correct value would cause overflow, plus or minus %HUGE_VAL
351  * is returned (according to the sign of the value), and %ERANGE is
352  * stored in %errno. If the correct value would cause underflow,
353  * zero is returned and %ERANGE is stored in %errno.
354  * 
355  * This function resets %errno before calling strtod() so that
356  * you can reliably detect overflow and underflow.
357  *
358  * Return value: the #gdouble value.
359  **/
360 gdouble
361 g_ascii_strtod (const gchar *nptr,
362                 gchar      **endptr)
363 {
364   gchar *fail_pos;
365   gdouble val;
366   struct lconv *locale_data;
367   const char *decimal_point;
368   int decimal_point_len;
369   const char *p, *decimal_point_pos;
370   const char *end = NULL; /* Silence gcc */
371   int strtod_errno;
372
373   g_return_val_if_fail (nptr != NULL, 0);
374
375   fail_pos = NULL;
376
377   locale_data = localeconv ();
378   decimal_point = locale_data->decimal_point;
379   decimal_point_len = strlen (decimal_point);
380
381   g_assert (decimal_point_len != 0);
382   
383   decimal_point_pos = NULL;
384   end = NULL;
385
386   if (decimal_point[0] != '.' || 
387       decimal_point[1] != 0)
388     {
389       p = nptr;
390       /* Skip leading space */
391       while (g_ascii_isspace (*p))
392         p++;
393       
394       /* Skip leading optional sign */
395       if (*p == '+' || *p == '-')
396         p++;
397       
398       if (p[0] == '0' && 
399           (p[1] == 'x' || p[1] == 'X'))
400         {
401           p += 2;
402           /* HEX - find the (optional) decimal point */
403           
404           while (g_ascii_isxdigit (*p))
405             p++;
406           
407           if (*p == '.')
408             decimal_point_pos = p++;
409               
410           while (g_ascii_isxdigit (*p))
411             p++;
412           
413           if (*p == 'p' || *p == 'P')
414             p++;
415           if (*p == '+' || *p == '-')
416             p++;
417           while (g_ascii_isdigit (*p))
418             p++;
419
420           end = p;
421         }
422       else if (g_ascii_isdigit (*p) || *p == '.')
423         {
424           while (g_ascii_isdigit (*p))
425             p++;
426           
427           if (*p == '.')
428             decimal_point_pos = p++;
429           
430           while (g_ascii_isdigit (*p))
431             p++;
432           
433           if (*p == 'e' || *p == 'E')
434             p++;
435           if (*p == '+' || *p == '-')
436             p++;
437           while (g_ascii_isdigit (*p))
438             p++;
439
440           end = p;
441         }
442       /* For the other cases, we need not convert the decimal point */
443     }
444
445   if (decimal_point_pos)
446     {
447       char *copy, *c;
448
449       /* We need to convert the '.' to the locale specific decimal point */
450       copy = g_malloc (end - nptr + 1 + decimal_point_len);
451       
452       c = copy;
453       memcpy (c, nptr, decimal_point_pos - nptr);
454       c += decimal_point_pos - nptr;
455       memcpy (c, decimal_point, decimal_point_len);
456       c += decimal_point_len;
457       memcpy (c, decimal_point_pos + 1, end - (decimal_point_pos + 1));
458       c += end - (decimal_point_pos + 1);
459       *c = 0;
460
461       errno = 0;
462       val = strtod (copy, &fail_pos);
463       strtod_errno = errno;
464
465       if (fail_pos)
466         {
467           if (fail_pos - copy > decimal_point_pos - nptr)
468             fail_pos = (char *)nptr + (fail_pos - copy) - (decimal_point_len - 1);
469           else
470             fail_pos = (char *)nptr + (fail_pos - copy);
471         }
472       
473       g_free (copy);
474           
475     }
476   else if (end)
477     {
478       char *copy;
479       
480       copy = g_malloc (end - (char *)nptr + 1);
481       memcpy (copy, nptr, end - nptr);
482       *(copy + (end - (char *)nptr)) = 0;
483       
484       errno = 0;
485       val = strtod (copy, &fail_pos);
486       strtod_errno = errno;
487
488       if (fail_pos)
489         {
490           fail_pos = (char *)nptr + (fail_pos - copy);
491         }
492       
493       g_free (copy);
494     }
495   else
496     {
497       errno = 0;
498       val = strtod (nptr, &fail_pos);
499       strtod_errno = errno;
500     }
501
502   if (endptr)
503     *endptr = fail_pos;
504
505   errno = strtod_errno;
506
507   return val;
508 }
509
510
511 /**
512  * g_ascii_dtostr:
513  * @buffer: A buffer to place the resulting string in
514  * @buf_len: The length of the buffer.
515  * @d: The #gdouble to convert
516  *
517  * Converts a #gdouble to a string, using the '.' as
518  * decimal point. 
519  * 
520  * This functions generates enough precision that converting
521  * the string back using g_ascii_strtod() gives the same machine-number
522  * (on machines with IEEE compatible 64bit doubles). It is
523  * guaranteed that the size of the resulting string will never
524  * be larger than @G_ASCII_DTOSTR_BUF_SIZE bytes.
525  *
526  * Return value: The pointer to the buffer with the converted string.
527  **/
528 gchar *
529 g_ascii_dtostr (gchar       *buffer,
530                 gint         buf_len,
531                 gdouble      d)
532 {
533   return g_ascii_formatd (buffer, buf_len, "%.17g", d);
534 }
535
536 /**
537  * g_ascii_formatd:
538  * @buffer: A buffer to place the resulting string in
539  * @buf_len: The length of the buffer.
540  * @format: The printf()-style format to use for the
541  *          code to use for converting. 
542  * @d: The #gdouble to convert
543  *
544  * Converts a #gdouble to a string, using the '.' as
545  * decimal point. To format the number you pass in
546  * a printf()-style format string. Allowed conversion
547  * specifiers are 'e', 'E', 'f', 'F', 'g' and 'G'. 
548  * 
549  * If you just want to want to serialize the value into a
550  * string, use g_ascii_dtostr().
551  *
552  * Return value: The pointer to the buffer with the converted string.
553  **/
554 gchar *
555 g_ascii_formatd (gchar       *buffer,
556                  gint         buf_len,
557                  const gchar *format,
558                  gdouble      d)
559 {
560   struct lconv *locale_data;
561   const char *decimal_point;
562   int decimal_point_len;
563   gchar *p;
564   int rest_len;
565   gchar format_char;
566
567   g_return_val_if_fail (buffer != NULL, NULL);
568   g_return_val_if_fail (format[0] == '%', NULL);
569   g_return_val_if_fail (strpbrk (format + 1, "'l%") == NULL, NULL);
570  
571   format_char = format[strlen (format) - 1];
572   
573   g_return_val_if_fail (format_char == 'e' || format_char == 'E' ||
574                         format_char == 'f' || format_char == 'F' ||
575                         format_char == 'g' || format_char == 'G',
576                         NULL);
577
578   if (format[0] != '%')
579     return NULL;
580
581   if (strpbrk (format + 1, "'l%"))
582     return NULL;
583
584   if (!(format_char == 'e' || format_char == 'E' ||
585         format_char == 'f' || format_char == 'F' ||
586         format_char == 'g' || format_char == 'G'))
587     return NULL;
588
589       
590   _g_snprintf (buffer, buf_len, format, d);
591
592   locale_data = localeconv ();
593   decimal_point = locale_data->decimal_point;
594   decimal_point_len = strlen (decimal_point);
595
596   g_assert (decimal_point_len != 0);
597
598   if (decimal_point[0] != '.' ||
599       decimal_point[1] != 0)
600     {
601       p = buffer;
602
603       while (g_ascii_isspace (*p))
604         p++;
605
606       if (*p == '+' || *p == '-')
607         p++;
608
609       while (isdigit ((guchar)*p))
610         p++;
611
612       if (strncmp (p, decimal_point, decimal_point_len) == 0)
613         {
614           *p = '.';
615           p++;
616           if (decimal_point_len > 1) 
617             {
618               rest_len = strlen (p + (decimal_point_len-1));
619               memmove (p, p + (decimal_point_len-1), rest_len);
620               p[rest_len] = 0;
621             }
622         }
623     }
624   
625   return buffer;
626 }
627
628 static guint64
629 g_parse_long_long (const gchar  *nptr,
630                    const gchar **endptr,
631                    guint         base,
632                    gboolean     *negative)
633 {
634   /* this code is based on on the strtol(3) code from GNU libc released under
635    * the GNU Lesser General Public License.
636    *
637    * Copyright (C) 1991,92,94,95,96,97,98,99,2000,01,02
638    *        Free Software Foundation, Inc.
639    */
640 #define ISSPACE(c)              ((c) == ' ' || (c) == '\f' || (c) == '\n' || \
641                                  (c) == '\r' || (c) == '\t' || (c) == '\v')
642 #define ISUPPER(c)              ((c) >= 'A' && (c) <= 'Z')
643 #define ISLOWER(c)              ((c) >= 'a' && (c) <= 'z')
644 #define ISALPHA(c)              (ISUPPER (c) || ISLOWER (c))
645 #define TOUPPER(c)              (ISLOWER (c) ? (c) - 'a' + 'A' : (c))
646 #define TOLOWER(c)              (ISUPPER (c) ? (c) - 'A' + 'a' : (c))
647   gboolean overflow;
648   guint64 cutoff;
649   guint64 cutlim;
650   guint64 ui64;
651   const gchar *s, *save;
652   guchar c;
653   
654   g_return_val_if_fail (nptr != NULL, 0);
655   
656   *negative = FALSE;
657   if (base == 1 || base > 36)
658     {
659       errno = EINVAL;
660       if (endptr)
661         *endptr = nptr;
662       return 0;
663     }
664   
665   save = s = nptr;
666   
667   /* Skip white space.  */
668   while (ISSPACE (*s))
669     ++s;
670
671   if (G_UNLIKELY (!*s))
672     goto noconv;
673   
674   /* Check for a sign.  */
675   if (*s == '-')
676     {
677       *negative = TRUE;
678       ++s;
679     }
680   else if (*s == '+')
681     ++s;
682   
683   /* Recognize number prefix and if BASE is zero, figure it out ourselves.  */
684   if (*s == '0')
685     {
686       if ((base == 0 || base == 16) && TOUPPER (s[1]) == 'X')
687         {
688           s += 2;
689           base = 16;
690         }
691       else if (base == 0)
692         base = 8;
693     }
694   else if (base == 0)
695     base = 10;
696   
697   /* Save the pointer so we can check later if anything happened.  */
698   save = s;
699   cutoff = G_MAXUINT64 / base;
700   cutlim = G_MAXUINT64 % base;
701   
702   overflow = FALSE;
703   ui64 = 0;
704   c = *s;
705   for (; c; c = *++s)
706     {
707       if (c >= '0' && c <= '9')
708         c -= '0';
709       else if (ISALPHA (c))
710         c = TOUPPER (c) - 'A' + 10;
711       else
712         break;
713       if (c >= base)
714         break;
715       /* Check for overflow.  */
716       if (ui64 > cutoff || (ui64 == cutoff && c > cutlim))
717         overflow = TRUE;
718       else
719         {
720           ui64 *= base;
721           ui64 += c;
722         }
723     }
724   
725   /* Check if anything actually happened.  */
726   if (s == save)
727     goto noconv;
728   
729   /* Store in ENDPTR the address of one character
730      past the last character we converted.  */
731   if (endptr)
732     *endptr = s;
733   
734   if (G_UNLIKELY (overflow))
735     {
736       errno = ERANGE;
737       return G_MAXUINT64;
738     }
739
740   return ui64;
741   
742  noconv:
743   /* We must handle a special case here: the base is 0 or 16 and the
744      first two characters are '0' and 'x', but the rest are no
745      hexadecimal digits.  This is no error case.  We return 0 and
746      ENDPTR points to the `x`.  */
747   if (endptr)
748     {
749       if (save - nptr >= 2 && TOUPPER (save[-1]) == 'X'
750           && save[-2] == '0')
751         *endptr = &save[-1];
752       else
753         /*  There was no number to convert.  */
754         *endptr = nptr;
755     }
756   return 0;
757 }
758
759 /**
760  * g_ascii_strtoull:
761  * @nptr:    the string to convert to a numeric value.
762  * @endptr:  if non-%NULL, it returns the character after
763  *           the last character used in the conversion.
764  * @base:    to be used for the conversion, 2..36 or 0
765  *
766  * Converts a string to a #guint64 value.
767  * This function behaves like the standard strtoull() function
768  * does in the C locale. It does this without actually
769  * changing the current locale, since that would not be
770  * thread-safe.
771  *
772  * This function is typically used when reading configuration
773  * files or other non-user input that should be locale independent.
774  * To handle input from the user you should normally use the
775  * locale-sensitive system strtoull() function.
776  *
777  * If the correct value would cause overflow, %G_MAXUINT64
778  * is returned, and %ERANGE is stored in %errno.  If the base is
779  * outside the valid range, zero is returned, and %EINVAL is stored
780  * in %errno.  If the string conversion fails, zero is returned, and
781  * @endptr returns @nptr (if @endptr is non-%NULL).
782  *
783  * Return value: the #guint64 value or zero on error.
784  *
785  * Since: 2.2
786  **/
787 guint64
788 g_ascii_strtoull (const gchar *nptr,
789                   gchar      **endptr,
790                   guint        base)
791 {
792   gboolean negative;
793   guint64 result;
794
795   result = g_parse_long_long (nptr, (const gchar **) endptr, base, &negative);
796
797   /* Return the result of the appropriate sign.  */
798   return negative ? -result : result;
799 }
800
801 /**
802  * g_ascii_strtoll:
803  * @nptr:    the string to convert to a numeric value.
804  * @endptr:  if non-%NULL, it returns the character after
805  *           the last character used in the conversion.
806  * @base:    to be used for the conversion, 2..36 or 0
807  *
808  * Converts a string to a #gint64 value.
809  * This function behaves like the standard strtoll() function
810  * does in the C locale. It does this without actually
811  * changing the current locale, since that would not be
812  * thread-safe.
813  *
814  * This function is typically used when reading configuration
815  * files or other non-user input that should be locale independent.
816  * To handle input from the user you should normally use the
817  * locale-sensitive system strtoll() function.
818  *
819  * If the correct value would cause overflow, %G_MAXINT64 or %G_MININT64
820  * is returned, and %ERANGE is stored in %errno.  If the base is
821  * outside the valid range, zero is returned, and %EINVAL is stored
822  * in %errno.  If the string conversion fails, zero is returned, and
823  * @endptr returns @nptr (if @endptr is non-%NULL).
824  *
825  * Return value: the #gint64 value or zero on error.
826  *
827  * Since: 2.12
828  **/
829 gint64 
830 g_ascii_strtoll (const gchar *nptr,
831                  gchar      **endptr,
832                  guint        base)
833 {
834   gboolean negative;
835   guint64 result;
836
837   result = g_parse_long_long (nptr, (const gchar **) endptr, base, &negative);
838
839   if (negative && result > (guint64) G_MININT64)
840     {
841       errno = ERANGE;
842       return G_MININT64;
843     }
844   else if (!negative && result > (guint64) G_MAXINT64)
845     {
846       errno = ERANGE;
847       return G_MAXINT64;
848     }
849   else if (negative)
850     return - (gint64) result;
851   else
852     return (gint64) result;
853 }
854
855 G_CONST_RETURN gchar*
856 g_strerror (gint errnum)
857 {
858   static GStaticPrivate msg_private = G_STATIC_PRIVATE_INIT;
859   char *msg;
860   int saved_errno = errno;
861
862 #ifdef HAVE_STRERROR
863   const char *msg_locale;
864
865   msg_locale = strerror (errnum);
866   if (g_get_charset (NULL))
867     {
868       errno = saved_errno;
869       return msg_locale;
870     }
871   else
872     {
873       gchar *msg_utf8 = g_locale_to_utf8 (msg_locale, -1, NULL, NULL, NULL);
874       if (msg_utf8)
875         {
876           /* Stick in the quark table so that we can return a static result
877            */
878           GQuark msg_quark = g_quark_from_string (msg_utf8);
879           g_free (msg_utf8);
880           
881           msg_utf8 = (gchar *) g_quark_to_string (msg_quark);
882           errno = saved_errno;
883           return msg_utf8;
884         }
885     }
886 #elif NO_SYS_ERRLIST
887   switch (errnum)
888     {
889 #ifdef E2BIG
890     case E2BIG: return "argument list too long";
891 #endif
892 #ifdef EACCES
893     case EACCES: return "permission denied";
894 #endif
895 #ifdef EADDRINUSE
896     case EADDRINUSE: return "address already in use";
897 #endif
898 #ifdef EADDRNOTAVAIL
899     case EADDRNOTAVAIL: return "can't assign requested address";
900 #endif
901 #ifdef EADV
902     case EADV: return "advertise error";
903 #endif
904 #ifdef EAFNOSUPPORT
905     case EAFNOSUPPORT: return "address family not supported by protocol family";
906 #endif
907 #ifdef EAGAIN
908     case EAGAIN: return "try again";
909 #endif
910 #ifdef EALIGN
911     case EALIGN: return "EALIGN";
912 #endif
913 #ifdef EALREADY
914     case EALREADY: return "operation already in progress";
915 #endif
916 #ifdef EBADE
917     case EBADE: return "bad exchange descriptor";
918 #endif
919 #ifdef EBADF
920     case EBADF: return "bad file number";
921 #endif
922 #ifdef EBADFD
923     case EBADFD: return "file descriptor in bad state";
924 #endif
925 #ifdef EBADMSG
926     case EBADMSG: return "not a data message";
927 #endif
928 #ifdef EBADR
929     case EBADR: return "bad request descriptor";
930 #endif
931 #ifdef EBADRPC
932     case EBADRPC: return "RPC structure is bad";
933 #endif
934 #ifdef EBADRQC
935     case EBADRQC: return "bad request code";
936 #endif
937 #ifdef EBADSLT
938     case EBADSLT: return "invalid slot";
939 #endif
940 #ifdef EBFONT
941     case EBFONT: return "bad font file format";
942 #endif
943 #ifdef EBUSY
944     case EBUSY: return "mount device busy";
945 #endif
946 #ifdef ECHILD
947     case ECHILD: return "no children";
948 #endif
949 #ifdef ECHRNG
950     case ECHRNG: return "channel number out of range";
951 #endif
952 #ifdef ECOMM
953     case ECOMM: return "communication error on send";
954 #endif
955 #ifdef ECONNABORTED
956     case ECONNABORTED: return "software caused connection abort";
957 #endif
958 #ifdef ECONNREFUSED
959     case ECONNREFUSED: return "connection refused";
960 #endif
961 #ifdef ECONNRESET
962     case ECONNRESET: return "connection reset by peer";
963 #endif
964 #if defined(EDEADLK) && (!defined(EWOULDBLOCK) || (EDEADLK != EWOULDBLOCK))
965     case EDEADLK: return "resource deadlock avoided";
966 #endif
967 #ifdef EDEADLOCK
968     case EDEADLOCK: return "resource deadlock avoided";
969 #endif
970 #ifdef EDESTADDRREQ
971     case EDESTADDRREQ: return "destination address required";
972 #endif
973 #ifdef EDIRTY
974     case EDIRTY: return "mounting a dirty fs w/o force";
975 #endif
976 #ifdef EDOM
977     case EDOM: return "math argument out of range";
978 #endif
979 #ifdef EDOTDOT
980     case EDOTDOT: return "cross mount point";
981 #endif
982 #ifdef EDQUOT
983     case EDQUOT: return "disk quota exceeded";
984 #endif
985 #ifdef EDUPPKG
986     case EDUPPKG: return "duplicate package name";
987 #endif
988 #ifdef EEXIST
989     case EEXIST: return "file already exists";
990 #endif
991 #ifdef EFAULT
992     case EFAULT: return "bad address in system call argument";
993 #endif
994 #ifdef EFBIG
995     case EFBIG: return "file too large";
996 #endif
997 #ifdef EHOSTDOWN
998     case EHOSTDOWN: return "host is down";
999 #endif
1000 #ifdef EHOSTUNREACH
1001     case EHOSTUNREACH: return "host is unreachable";
1002 #endif
1003 #ifdef EIDRM
1004     case EIDRM: return "identifier removed";
1005 #endif
1006 #ifdef EINIT
1007     case EINIT: return "initialization error";
1008 #endif
1009 #ifdef EINPROGRESS
1010     case EINPROGRESS: return "operation now in progress";
1011 #endif
1012 #ifdef EINTR
1013     case EINTR: return "interrupted system call";
1014 #endif
1015 #ifdef EINVAL
1016     case EINVAL: return "invalid argument";
1017 #endif
1018 #ifdef EIO
1019     case EIO: return "I/O error";
1020 #endif
1021 #ifdef EISCONN
1022     case EISCONN: return "socket is already connected";
1023 #endif
1024 #ifdef EISDIR
1025     case EISDIR: return "is a directory";
1026 #endif
1027 #ifdef EISNAME
1028     case EISNAM: return "is a name file";
1029 #endif
1030 #ifdef ELBIN
1031     case ELBIN: return "ELBIN";
1032 #endif
1033 #ifdef EL2HLT
1034     case EL2HLT: return "level 2 halted";
1035 #endif
1036 #ifdef EL2NSYNC
1037     case EL2NSYNC: return "level 2 not synchronized";
1038 #endif
1039 #ifdef EL3HLT
1040     case EL3HLT: return "level 3 halted";
1041 #endif
1042 #ifdef EL3RST
1043     case EL3RST: return "level 3 reset";
1044 #endif
1045 #ifdef ELIBACC
1046     case ELIBACC: return "can not access a needed shared library";
1047 #endif
1048 #ifdef ELIBBAD
1049     case ELIBBAD: return "accessing a corrupted shared library";
1050 #endif
1051 #ifdef ELIBEXEC
1052     case ELIBEXEC: return "can not exec a shared library directly";
1053 #endif
1054 #ifdef ELIBMAX
1055     case ELIBMAX: return "attempting to link in more shared libraries than system limit";
1056 #endif
1057 #ifdef ELIBSCN
1058     case ELIBSCN: return ".lib section in a.out corrupted";
1059 #endif
1060 #ifdef ELNRNG
1061     case ELNRNG: return "link number out of range";
1062 #endif
1063 #ifdef ELOOP
1064     case ELOOP: return "too many levels of symbolic links";
1065 #endif
1066 #ifdef EMFILE
1067     case EMFILE: return "too many open files";
1068 #endif
1069 #ifdef EMLINK
1070     case EMLINK: return "too many links";
1071 #endif
1072 #ifdef EMSGSIZE
1073     case EMSGSIZE: return "message too long";
1074 #endif
1075 #ifdef EMULTIHOP
1076     case EMULTIHOP: return "multihop attempted";
1077 #endif
1078 #ifdef ENAMETOOLONG
1079     case ENAMETOOLONG: return "file name too long";
1080 #endif
1081 #ifdef ENAVAIL
1082     case ENAVAIL: return "not available";
1083 #endif
1084 #ifdef ENET
1085     case ENET: return "ENET";
1086 #endif
1087 #ifdef ENETDOWN
1088     case ENETDOWN: return "network is down";
1089 #endif
1090 #ifdef ENETRESET
1091     case ENETRESET: return "network dropped connection on reset";
1092 #endif
1093 #ifdef ENETUNREACH
1094     case ENETUNREACH: return "network is unreachable";
1095 #endif
1096 #ifdef ENFILE
1097     case ENFILE: return "file table overflow";
1098 #endif
1099 #ifdef ENOANO
1100     case ENOANO: return "anode table overflow";
1101 #endif
1102 #if defined(ENOBUFS) && (!defined(ENOSR) || (ENOBUFS != ENOSR))
1103     case ENOBUFS: return "no buffer space available";
1104 #endif
1105 #ifdef ENOCSI
1106     case ENOCSI: return "no CSI structure available";
1107 #endif
1108 #ifdef ENODATA
1109     case ENODATA: return "no data available";
1110 #endif
1111 #ifdef ENODEV
1112     case ENODEV: return "no such device";
1113 #endif
1114 #ifdef ENOENT
1115     case ENOENT: return "no such file or directory";
1116 #endif
1117 #ifdef ENOEXEC
1118     case ENOEXEC: return "exec format error";
1119 #endif
1120 #ifdef ENOLCK
1121     case ENOLCK: return "no locks available";
1122 #endif
1123 #ifdef ENOLINK
1124     case ENOLINK: return "link has be severed";
1125 #endif
1126 #ifdef ENOMEM
1127     case ENOMEM: return "not enough memory";
1128 #endif
1129 #ifdef ENOMSG
1130     case ENOMSG: return "no message of desired type";
1131 #endif
1132 #ifdef ENONET
1133     case ENONET: return "machine is not on the network";
1134 #endif
1135 #ifdef ENOPKG
1136     case ENOPKG: return "package not installed";
1137 #endif
1138 #ifdef ENOPROTOOPT
1139     case ENOPROTOOPT: return "bad proocol option";
1140 #endif
1141 #ifdef ENOSPC
1142     case ENOSPC: return "no space left on device";
1143 #endif
1144 #ifdef ENOSR
1145     case ENOSR: return "out of stream resources";
1146 #endif
1147 #ifdef ENOSTR
1148     case ENOSTR: return "not a stream device";
1149 #endif
1150 #ifdef ENOSYM
1151     case ENOSYM: return "unresolved symbol name";
1152 #endif
1153 #ifdef ENOSYS
1154     case ENOSYS: return "function not implemented";
1155 #endif
1156 #ifdef ENOTBLK
1157     case ENOTBLK: return "block device required";
1158 #endif
1159 #ifdef ENOTCONN
1160     case ENOTCONN: return "socket is not connected";
1161 #endif
1162 #ifdef ENOTDIR
1163     case ENOTDIR: return "not a directory";
1164 #endif
1165 #ifdef ENOTEMPTY
1166     case ENOTEMPTY: return "directory not empty";
1167 #endif
1168 #ifdef ENOTNAM
1169     case ENOTNAM: return "not a name file";
1170 #endif
1171 #ifdef ENOTSOCK
1172     case ENOTSOCK: return "socket operation on non-socket";
1173 #endif
1174 #ifdef ENOTTY
1175     case ENOTTY: return "inappropriate device for ioctl";
1176 #endif
1177 #ifdef ENOTUNIQ
1178     case ENOTUNIQ: return "name not unique on network";
1179 #endif
1180 #ifdef ENXIO
1181     case ENXIO: return "no such device or address";
1182 #endif
1183 #ifdef EOPNOTSUPP
1184     case EOPNOTSUPP: return "operation not supported on socket";
1185 #endif
1186 #ifdef EPERM
1187     case EPERM: return "not owner";
1188 #endif
1189 #ifdef EPFNOSUPPORT
1190     case EPFNOSUPPORT: return "protocol family not supported";
1191 #endif
1192 #ifdef EPIPE
1193     case EPIPE: return "broken pipe";
1194 #endif
1195 #ifdef EPROCLIM
1196     case EPROCLIM: return "too many processes";
1197 #endif
1198 #ifdef EPROCUNAVAIL
1199     case EPROCUNAVAIL: return "bad procedure for program";
1200 #endif
1201 #ifdef EPROGMISMATCH
1202     case EPROGMISMATCH: return "program version wrong";
1203 #endif
1204 #ifdef EPROGUNAVAIL
1205     case EPROGUNAVAIL: return "RPC program not available";
1206 #endif
1207 #ifdef EPROTO
1208     case EPROTO: return "protocol error";
1209 #endif
1210 #ifdef EPROTONOSUPPORT
1211     case EPROTONOSUPPORT: return "protocol not suppored";
1212 #endif
1213 #ifdef EPROTOTYPE
1214     case EPROTOTYPE: return "protocol wrong type for socket";
1215 #endif
1216 #ifdef ERANGE
1217     case ERANGE: return "math result unrepresentable";
1218 #endif
1219 #if defined(EREFUSED) && (!defined(ECONNREFUSED) || (EREFUSED != ECONNREFUSED))
1220     case EREFUSED: return "EREFUSED";
1221 #endif
1222 #ifdef EREMCHG
1223     case EREMCHG: return "remote address changed";
1224 #endif
1225 #ifdef EREMDEV
1226     case EREMDEV: return "remote device";
1227 #endif
1228 #ifdef EREMOTE
1229     case EREMOTE: return "pathname hit remote file system";
1230 #endif
1231 #ifdef EREMOTEIO
1232     case EREMOTEIO: return "remote i/o error";
1233 #endif
1234 #ifdef EREMOTERELEASE
1235     case EREMOTERELEASE: return "EREMOTERELEASE";
1236 #endif
1237 #ifdef EROFS
1238     case EROFS: return "read-only file system";
1239 #endif
1240 #ifdef ERPCMISMATCH
1241     case ERPCMISMATCH: return "RPC version is wrong";
1242 #endif
1243 #ifdef ERREMOTE
1244     case ERREMOTE: return "object is remote";
1245 #endif
1246 #ifdef ESHUTDOWN
1247     case ESHUTDOWN: return "can't send afer socket shutdown";
1248 #endif
1249 #ifdef ESOCKTNOSUPPORT
1250     case ESOCKTNOSUPPORT: return "socket type not supported";
1251 #endif
1252 #ifdef ESPIPE
1253     case ESPIPE: return "invalid seek";
1254 #endif
1255 #ifdef ESRCH
1256     case ESRCH: return "no such process";
1257 #endif
1258 #ifdef ESRMNT
1259     case ESRMNT: return "srmount error";
1260 #endif
1261 #ifdef ESTALE
1262     case ESTALE: return "stale remote file handle";
1263 #endif
1264 #ifdef ESUCCESS
1265     case ESUCCESS: return "Error 0";
1266 #endif
1267 #ifdef ETIME
1268     case ETIME: return "timer expired";
1269 #endif
1270 #ifdef ETIMEDOUT
1271     case ETIMEDOUT: return "connection timed out";
1272 #endif
1273 #ifdef ETOOMANYREFS
1274     case ETOOMANYREFS: return "too many references: can't splice";
1275 #endif
1276 #ifdef ETXTBSY
1277     case ETXTBSY: return "text file or pseudo-device busy";
1278 #endif
1279 #ifdef EUCLEAN
1280     case EUCLEAN: return "structure needs cleaning";
1281 #endif
1282 #ifdef EUNATCH
1283     case EUNATCH: return "protocol driver not attached";
1284 #endif
1285 #ifdef EUSERS
1286     case EUSERS: return "too many users";
1287 #endif
1288 #ifdef EVERSION
1289     case EVERSION: return "version mismatch";
1290 #endif
1291 #if defined(EWOULDBLOCK) && (!defined(EAGAIN) || (EWOULDBLOCK != EAGAIN))
1292     case EWOULDBLOCK: return "operation would block";
1293 #endif
1294 #ifdef EXDEV
1295     case EXDEV: return "cross-domain link";
1296 #endif
1297 #ifdef EXFULL
1298     case EXFULL: return "message tables full";
1299 #endif
1300     }
1301 #else /* NO_SYS_ERRLIST */
1302   extern int sys_nerr;
1303   extern char *sys_errlist[];
1304
1305   if ((errnum > 0) && (errnum <= sys_nerr))
1306     return sys_errlist [errnum];
1307 #endif /* NO_SYS_ERRLIST */
1308
1309   msg = g_static_private_get (&msg_private);
1310   if (!msg)
1311     {
1312       msg = g_new (gchar, 64);
1313       g_static_private_set (&msg_private, msg, g_free);
1314     }
1315
1316   _g_sprintf (msg, "unknown error (%d)", errnum);
1317
1318   errno = saved_errno;
1319   return msg;
1320 }
1321
1322 G_CONST_RETURN gchar*
1323 g_strsignal (gint signum)
1324 {
1325   static GStaticPrivate msg_private = G_STATIC_PRIVATE_INIT;
1326   char *msg;
1327
1328 #ifdef HAVE_STRSIGNAL
1329   const char *msg_locale;
1330   
1331 #if defined(G_OS_BEOS) || defined(G_WITH_CYGWIN)
1332 extern const char *strsignal(int);
1333 #else
1334   /* this is declared differently (const) in string.h on BeOS */
1335   extern char *strsignal (int sig);
1336 #endif /* !G_OS_BEOS && !G_WITH_CYGWIN */
1337   msg_locale = strsignal (signum);
1338   if (g_get_charset (NULL))
1339     return msg_locale;
1340   else
1341     {
1342       gchar *msg_utf8 = g_locale_to_utf8 (msg_locale, -1, NULL, NULL, NULL);
1343       if (msg_utf8)
1344         {
1345           /* Stick in the quark table so that we can return a static result
1346            */
1347           GQuark msg_quark = g_quark_from_string (msg_utf8);
1348           g_free (msg_utf8);
1349           
1350           return g_quark_to_string (msg_quark);
1351         }
1352     }
1353 #elif NO_SYS_SIGLIST
1354   switch (signum)
1355     {
1356 #ifdef SIGHUP
1357     case SIGHUP: return "Hangup";
1358 #endif
1359 #ifdef SIGINT
1360     case SIGINT: return "Interrupt";
1361 #endif
1362 #ifdef SIGQUIT
1363     case SIGQUIT: return "Quit";
1364 #endif
1365 #ifdef SIGILL
1366     case SIGILL: return "Illegal instruction";
1367 #endif
1368 #ifdef SIGTRAP
1369     case SIGTRAP: return "Trace/breakpoint trap";
1370 #endif
1371 #ifdef SIGABRT
1372     case SIGABRT: return "IOT trap/Abort";
1373 #endif
1374 #ifdef SIGBUS
1375     case SIGBUS: return "Bus error";
1376 #endif
1377 #ifdef SIGFPE
1378     case SIGFPE: return "Floating point exception";
1379 #endif
1380 #ifdef SIGKILL
1381     case SIGKILL: return "Killed";
1382 #endif
1383 #ifdef SIGUSR1
1384     case SIGUSR1: return "User defined signal 1";
1385 #endif
1386 #ifdef SIGSEGV
1387     case SIGSEGV: return "Segmentation fault";
1388 #endif
1389 #ifdef SIGUSR2
1390     case SIGUSR2: return "User defined signal 2";
1391 #endif
1392 #ifdef SIGPIPE
1393     case SIGPIPE: return "Broken pipe";
1394 #endif
1395 #ifdef SIGALRM
1396     case SIGALRM: return "Alarm clock";
1397 #endif
1398 #ifdef SIGTERM
1399     case SIGTERM: return "Terminated";
1400 #endif
1401 #ifdef SIGSTKFLT
1402     case SIGSTKFLT: return "Stack fault";
1403 #endif
1404 #ifdef SIGCHLD
1405     case SIGCHLD: return "Child exited";
1406 #endif
1407 #ifdef SIGCONT
1408     case SIGCONT: return "Continued";
1409 #endif
1410 #ifdef SIGSTOP
1411     case SIGSTOP: return "Stopped (signal)";
1412 #endif
1413 #ifdef SIGTSTP
1414     case SIGTSTP: return "Stopped";
1415 #endif
1416 #ifdef SIGTTIN
1417     case SIGTTIN: return "Stopped (tty input)";
1418 #endif
1419 #ifdef SIGTTOU
1420     case SIGTTOU: return "Stopped (tty output)";
1421 #endif
1422 #ifdef SIGURG
1423     case SIGURG: return "Urgent condition";
1424 #endif
1425 #ifdef SIGXCPU
1426     case SIGXCPU: return "CPU time limit exceeded";
1427 #endif
1428 #ifdef SIGXFSZ
1429     case SIGXFSZ: return "File size limit exceeded";
1430 #endif
1431 #ifdef SIGVTALRM
1432     case SIGVTALRM: return "Virtual time alarm";
1433 #endif
1434 #ifdef SIGPROF
1435     case SIGPROF: return "Profile signal";
1436 #endif
1437 #ifdef SIGWINCH
1438     case SIGWINCH: return "Window size changed";
1439 #endif
1440 #ifdef SIGIO
1441     case SIGIO: return "Possible I/O";
1442 #endif
1443 #ifdef SIGPWR
1444     case SIGPWR: return "Power failure";
1445 #endif
1446 #ifdef SIGUNUSED
1447     case SIGUNUSED: return "Unused signal";
1448 #endif
1449     }
1450 #else /* NO_SYS_SIGLIST */
1451
1452 #ifdef NO_SYS_SIGLIST_DECL
1453   extern char *sys_siglist[];   /*(see Tue Jan 19 00:44:24 1999 in changelog)*/
1454 #endif
1455
1456   return (char*) /* this function should return const --josh */ sys_siglist [signum];
1457 #endif /* NO_SYS_SIGLIST */
1458
1459   msg = g_static_private_get (&msg_private);
1460   if (!msg)
1461     {
1462       msg = g_new (gchar, 64);
1463       g_static_private_set (&msg_private, msg, g_free);
1464     }
1465
1466   _g_sprintf (msg, "unknown signal (%d)", signum);
1467   
1468   return msg;
1469 }
1470
1471 /* Functions g_strlcpy and g_strlcat were originally developed by
1472  * Todd C. Miller <Todd.Miller@courtesan.com> to simplify writing secure code.
1473  * See ftp://ftp.openbsd.org/pub/OpenBSD/src/lib/libc/string/strlcpy.3
1474  * for more information.
1475  */
1476
1477 #ifdef HAVE_STRLCPY
1478 /* Use the native ones, if available; they might be implemented in assembly */
1479 gsize
1480 g_strlcpy (gchar       *dest,
1481            const gchar *src,
1482            gsize        dest_size)
1483 {
1484   g_return_val_if_fail (dest != NULL, 0);
1485   g_return_val_if_fail (src  != NULL, 0);
1486   
1487   return strlcpy (dest, src, dest_size);
1488 }
1489
1490 gsize
1491 g_strlcat (gchar       *dest,
1492            const gchar *src,
1493            gsize        dest_size)
1494 {
1495   g_return_val_if_fail (dest != NULL, 0);
1496   g_return_val_if_fail (src  != NULL, 0);
1497   
1498   return strlcat (dest, src, dest_size);
1499 }
1500
1501 #else /* ! HAVE_STRLCPY */
1502 /* g_strlcpy
1503  *
1504  * Copy string src to buffer dest (of buffer size dest_size).  At most
1505  * dest_size-1 characters will be copied.  Always NUL terminates
1506  * (unless dest_size == 0).  This function does NOT allocate memory.
1507  * Unlike strncpy, this function doesn't pad dest (so it's often faster).
1508  * Returns size of attempted result, strlen(src),
1509  * so if retval >= dest_size, truncation occurred.
1510  */
1511 gsize
1512 g_strlcpy (gchar       *dest,
1513            const gchar *src,
1514            gsize        dest_size)
1515 {
1516   register gchar *d = dest;
1517   register const gchar *s = src;
1518   register gsize n = dest_size;
1519   
1520   g_return_val_if_fail (dest != NULL, 0);
1521   g_return_val_if_fail (src  != NULL, 0);
1522   
1523   /* Copy as many bytes as will fit */
1524   if (n != 0 && --n != 0)
1525     do
1526       {
1527         register gchar c = *s++;
1528         
1529         *d++ = c;
1530         if (c == 0)
1531           break;
1532       }
1533     while (--n != 0);
1534   
1535   /* If not enough room in dest, add NUL and traverse rest of src */
1536   if (n == 0)
1537     {
1538       if (dest_size != 0)
1539         *d = 0;
1540       while (*s++)
1541         ;
1542     }
1543   
1544   return s - src - 1;  /* count does not include NUL */
1545 }
1546
1547 /* g_strlcat
1548  *
1549  * Appends string src to buffer dest (of buffer size dest_size).
1550  * At most dest_size-1 characters will be copied.
1551  * Unlike strncat, dest_size is the full size of dest, not the space left over.
1552  * This function does NOT allocate memory.
1553  * This always NUL terminates (unless siz == 0 or there were no NUL characters
1554  * in the dest_size characters of dest to start with).
1555  * Returns size of attempted result, which is
1556  * MIN (dest_size, strlen (original dest)) + strlen (src),
1557  * so if retval >= dest_size, truncation occurred.
1558  */
1559 gsize
1560 g_strlcat (gchar       *dest,
1561            const gchar *src,
1562            gsize        dest_size)
1563 {
1564   register gchar *d = dest;
1565   register const gchar *s = src;
1566   register gsize bytes_left = dest_size;
1567   gsize dlength;  /* Logically, MIN (strlen (d), dest_size) */
1568   
1569   g_return_val_if_fail (dest != NULL, 0);
1570   g_return_val_if_fail (src  != NULL, 0);
1571   
1572   /* Find the end of dst and adjust bytes left but don't go past end */
1573   while (*d != 0 && bytes_left-- != 0)
1574     d++;
1575   dlength = d - dest;
1576   bytes_left = dest_size - dlength;
1577   
1578   if (bytes_left == 0)
1579     return dlength + strlen (s);
1580   
1581   while (*s != 0)
1582     {
1583       if (bytes_left != 1)
1584         {
1585           *d++ = *s;
1586           bytes_left--;
1587         }
1588       s++;
1589     }
1590   *d = 0;
1591   
1592   return dlength + (s - src);  /* count does not include NUL */
1593 }
1594 #endif /* ! HAVE_STRLCPY */
1595
1596 /**
1597  * g_ascii_strdown:
1598  * @str: a string.
1599  * @len: length of @str in bytes, or -1 if @str is nul-terminated.
1600  * 
1601  * Converts all upper case ASCII letters to lower case ASCII letters.
1602  * 
1603  * Return value: a newly-allocated string, with all the upper case
1604  *               characters in @str converted to lower case, with
1605  *               semantics that exactly match g_ascii_tolower(). (Note
1606  *               that this is unlike the old g_strdown(), which modified
1607  *               the string in place.)
1608  **/
1609 gchar*
1610 g_ascii_strdown (const gchar *str,
1611                  gssize       len)
1612 {
1613   gchar *result, *s;
1614   
1615   g_return_val_if_fail (str != NULL, NULL);
1616
1617   if (len < 0)
1618     len = strlen (str);
1619
1620   result = g_strndup (str, len);
1621   for (s = result; *s; s++)
1622     *s = g_ascii_tolower (*s);
1623   
1624   return result;
1625 }
1626
1627 /**
1628  * g_ascii_strup:
1629  * @str: a string.
1630  * @len: length of @str in bytes, or -1 if @str is nul-terminated.
1631  * 
1632  * Converts all lower case ASCII letters to upper case ASCII letters.
1633  * 
1634  * Return value: a newly allocated string, with all the lower case
1635  *               characters in @str converted to upper case, with
1636  *               semantics that exactly match g_ascii_toupper(). (Note
1637  *               that this is unlike the old g_strup(), which modified
1638  *               the string in place.)
1639  **/
1640 gchar*
1641 g_ascii_strup (const gchar *str,
1642                gssize       len)
1643 {
1644   gchar *result, *s;
1645
1646   g_return_val_if_fail (str != NULL, NULL);
1647
1648   if (len < 0)
1649     len = strlen (str);
1650
1651   result = g_strndup (str, len);
1652   for (s = result; *s; s++)
1653     *s = g_ascii_toupper (*s);
1654
1655   return result;
1656 }
1657
1658 /**
1659  * g_strdown:
1660  * @string: the string to convert.
1661  * 
1662  * Converts a string to lower case.  
1663  * 
1664  * Return value: the string 
1665  *
1666  * Deprecated:2.2: This function is totally broken for the reasons discussed 
1667  * in the g_strncasecmp() docs - use g_ascii_strdown() or g_utf8_strdown() 
1668  * instead.
1669  **/
1670 gchar*
1671 g_strdown (gchar *string)
1672 {
1673   register guchar *s;
1674   
1675   g_return_val_if_fail (string != NULL, NULL);
1676   
1677   s = (guchar *) string;
1678   
1679   while (*s)
1680     {
1681       if (isupper (*s))
1682         *s = tolower (*s);
1683       s++;
1684     }
1685   
1686   return (gchar *) string;
1687 }
1688
1689 /**
1690  * g_strup:
1691  * @string: the string to convert.
1692  * 
1693  * Converts a string to upper case. 
1694  * 
1695  * Return value: the string
1696  *
1697  * Deprecated:2.2: This function is totally broken for the reasons discussed 
1698  * in the g_strncasecmp() docs - use g_ascii_strup() or g_utf8_strup() instead.
1699  **/
1700 gchar*
1701 g_strup (gchar *string)
1702 {
1703   register guchar *s;
1704
1705   g_return_val_if_fail (string != NULL, NULL);
1706
1707   s = (guchar *) string;
1708
1709   while (*s)
1710     {
1711       if (islower (*s))
1712         *s = toupper (*s);
1713       s++;
1714     }
1715
1716   return (gchar *) string;
1717 }
1718
1719 gchar*
1720 g_strreverse (gchar *string)
1721 {
1722   g_return_val_if_fail (string != NULL, NULL);
1723
1724   if (*string)
1725     {
1726       register gchar *h, *t;
1727
1728       h = string;
1729       t = string + strlen (string) - 1;
1730
1731       while (h < t)
1732         {
1733           register gchar c;
1734
1735           c = *h;
1736           *h = *t;
1737           h++;
1738           *t = c;
1739           t--;
1740         }
1741     }
1742
1743   return string;
1744 }
1745
1746 /**
1747  * g_ascii_tolower:
1748  * @c: any character.
1749  * 
1750  * Convert a character to ASCII lower case.
1751  *
1752  * Unlike the standard C library tolower() function, this only
1753  * recognizes standard ASCII letters and ignores the locale, returning
1754  * all non-ASCII characters unchanged, even if they are lower case
1755  * letters in a particular character set. Also unlike the standard
1756  * library function, this takes and returns a char, not an int, so
1757  * don't call it on %EOF but no need to worry about casting to #guchar
1758  * before passing a possibly non-ASCII character in.
1759  * 
1760  * Return value: the result of converting @c to lower case.
1761  *               If @c is not an ASCII upper case letter,
1762  *               @c is returned unchanged.
1763  **/
1764 gchar
1765 g_ascii_tolower (gchar c)
1766 {
1767   return g_ascii_isupper (c) ? c - 'A' + 'a' : c;
1768 }
1769
1770 /**
1771  * g_ascii_toupper:
1772  * @c: any character.
1773  * 
1774  * Convert a character to ASCII upper case.
1775  *
1776  * Unlike the standard C library toupper() function, this only
1777  * recognizes standard ASCII letters and ignores the locale, returning
1778  * all non-ASCII characters unchanged, even if they are upper case
1779  * letters in a particular character set. Also unlike the standard
1780  * library function, this takes and returns a char, not an int, so
1781  * don't call it on %EOF but no need to worry about casting to #guchar
1782  * before passing a possibly non-ASCII character in.
1783  * 
1784  * Return value: the result of converting @c to upper case.
1785  *               If @c is not an ASCII lower case letter,
1786  *               @c is returned unchanged.
1787  **/
1788 gchar
1789 g_ascii_toupper (gchar c)
1790 {
1791   return g_ascii_islower (c) ? c - 'a' + 'A' : c;
1792 }
1793
1794 /**
1795  * g_ascii_digit_value:
1796  * @c: an ASCII character.
1797  *
1798  * Determines the numeric value of a character as a decimal
1799  * digit. Differs from g_unichar_digit_value() because it takes
1800  * a char, so there's no worry about sign extension if characters
1801  * are signed.
1802  *
1803  * Return value: If @c is a decimal digit (according to
1804  * g_ascii_isdigit()), its numeric value. Otherwise, -1.
1805  **/
1806 int
1807 g_ascii_digit_value (gchar c)
1808 {
1809   if (g_ascii_isdigit (c))
1810     return c - '0';
1811   return -1;
1812 }
1813
1814 /**
1815  * g_ascii_xdigit_value:
1816  * @c: an ASCII character.
1817  *
1818  * Determines the numeric value of a character as a hexidecimal
1819  * digit. Differs from g_unichar_xdigit_value() because it takes
1820  * a char, so there's no worry about sign extension if characters
1821  * are signed.
1822  *
1823  * Return value: If @c is a hex digit (according to
1824  * g_ascii_isxdigit()), its numeric value. Otherwise, -1.
1825  **/
1826 int
1827 g_ascii_xdigit_value (gchar c)
1828 {
1829   if (c >= 'A' && c <= 'F')
1830     return c - 'A' + 10;
1831   if (c >= 'a' && c <= 'f')
1832     return c - 'a' + 10;
1833   return g_ascii_digit_value (c);
1834 }
1835
1836 /**
1837  * g_ascii_strcasecmp:
1838  * @s1: string to compare with @s2.
1839  * @s2: string to compare with @s1.
1840  * 
1841  * Compare two strings, ignoring the case of ASCII characters.
1842  *
1843  * Unlike the BSD strcasecmp() function, this only recognizes standard
1844  * ASCII letters and ignores the locale, treating all non-ASCII
1845  * bytes as if they are not letters.
1846  *
1847  * This function should be used only on strings that are known to be
1848  * in encodings where the bytes corresponding to ASCII letters always
1849  * represent themselves. This includes UTF-8 and the ISO-8859-*
1850  * charsets, but not for instance double-byte encodings like the
1851  * Windows Codepage 932, where the trailing bytes of double-byte
1852  * characters include all ASCII letters. If you compare two CP932
1853  * strings using this function, you will get false matches.
1854  *
1855  * Return value: 0 if the strings match, a negative value if @s1 &lt; @s2, 
1856  *   or a positive value if @s1 &gt; @s2.
1857  **/
1858 gint
1859 g_ascii_strcasecmp (const gchar *s1,
1860                     const gchar *s2)
1861 {
1862   gint c1, c2;
1863
1864   g_return_val_if_fail (s1 != NULL, 0);
1865   g_return_val_if_fail (s2 != NULL, 0);
1866
1867   while (*s1 && *s2)
1868     {
1869       c1 = (gint)(guchar) TOLOWER (*s1);
1870       c2 = (gint)(guchar) TOLOWER (*s2);
1871       if (c1 != c2)
1872         return (c1 - c2);
1873       s1++; s2++;
1874     }
1875
1876   return (((gint)(guchar) *s1) - ((gint)(guchar) *s2));
1877 }
1878
1879 /**
1880  * g_ascii_strncasecmp:
1881  * @s1: string to compare with @s2.
1882  * @s2: string to compare with @s1.
1883  * @n:  number of characters to compare.
1884  * 
1885  * Compare @s1 and @s2, ignoring the case of ASCII characters and any
1886  * characters after the first @n in each string.
1887  *
1888  * Unlike the BSD strcasecmp() function, this only recognizes standard
1889  * ASCII letters and ignores the locale, treating all non-ASCII
1890  * characters as if they are not letters.
1891  * 
1892  * The same warning as in g_ascii_strcasecmp() applies: Use this
1893  * function only on strings known to be in encodings where bytes
1894  * corresponding to ASCII letters always represent themselves.
1895  *
1896  * Return value: 0 if the strings match, a negative value if @s1 &lt; @s2, 
1897  *   or a positive value if @s1 &gt; @s2.
1898  **/
1899 gint
1900 g_ascii_strncasecmp (const gchar *s1,
1901                      const gchar *s2,
1902                      gsize n)
1903 {
1904   gint c1, c2;
1905
1906   g_return_val_if_fail (s1 != NULL, 0);
1907   g_return_val_if_fail (s2 != NULL, 0);
1908
1909   while (n && *s1 && *s2)
1910     {
1911       n -= 1;
1912       c1 = (gint)(guchar) TOLOWER (*s1);
1913       c2 = (gint)(guchar) TOLOWER (*s2);
1914       if (c1 != c2)
1915         return (c1 - c2);
1916       s1++; s2++;
1917     }
1918
1919   if (n)
1920     return (((gint) (guchar) *s1) - ((gint) (guchar) *s2));
1921   else
1922     return 0;
1923 }
1924
1925 /**
1926  * g_strcasecmp:
1927  * @s1: a string.
1928  * @s2: a string to compare with @s1.
1929  * 
1930  * A case-insensitive string comparison, corresponding to the standard
1931  * strcasecmp() function on platforms which support it.
1932  *
1933  * Return value: 0 if the strings match, a negative value if @s1 &lt; @s2, 
1934  *   or a positive value if @s1 &gt; @s2.
1935  *
1936  * Deprecated:2.2: See g_strncasecmp() for a discussion of why this function 
1937  *   is deprecated and how to replace it.
1938  **/
1939 gint
1940 g_strcasecmp (const gchar *s1,
1941               const gchar *s2)
1942 {
1943 #ifdef HAVE_STRCASECMP
1944   g_return_val_if_fail (s1 != NULL, 0);
1945   g_return_val_if_fail (s2 != NULL, 0);
1946
1947   return strcasecmp (s1, s2);
1948 #else
1949   gint c1, c2;
1950
1951   g_return_val_if_fail (s1 != NULL, 0);
1952   g_return_val_if_fail (s2 != NULL, 0);
1953
1954   while (*s1 && *s2)
1955     {
1956       /* According to A. Cox, some platforms have islower's that
1957        * don't work right on non-uppercase
1958        */
1959       c1 = isupper ((guchar)*s1) ? tolower ((guchar)*s1) : *s1;
1960       c2 = isupper ((guchar)*s2) ? tolower ((guchar)*s2) : *s2;
1961       if (c1 != c2)
1962         return (c1 - c2);
1963       s1++; s2++;
1964     }
1965
1966   return (((gint)(guchar) *s1) - ((gint)(guchar) *s2));
1967 #endif
1968 }
1969
1970 /**
1971  * g_strncasecmp:
1972  * @s1: a string.
1973  * @s2: a string to compare with @s1.
1974  * @n: the maximum number of characters to compare.
1975  * 
1976  * A case-insensitive string comparison, corresponding to the standard
1977  * strncasecmp() function on platforms which support it.
1978  * It is similar to g_strcasecmp() except it only compares the first @n 
1979  * characters of the strings.
1980  * 
1981  * Return value: 0 if the strings match, a negative value if @s1 &lt; @s2, 
1982  *   or a positive value if @s1 &gt; @s2.
1983  *
1984  * Deprecated:2.2: The problem with g_strncasecmp() is that it does the 
1985  * comparison by calling toupper()/tolower(). These functions are
1986  * locale-specific and operate on single bytes. However, it is impossible
1987  * to handle things correctly from an I18N standpoint by operating on
1988  * bytes, since characters may be multibyte. Thus g_strncasecmp() is
1989  * broken if your string is guaranteed to be ASCII, since it's
1990  * locale-sensitive, and it's broken if your string is localized, since
1991  * it doesn't work on many encodings at all, including UTF-8, EUC-JP,
1992  * etc.
1993  *
1994  * There are therefore two replacement functions: g_ascii_strncasecmp(),
1995  * which only works on ASCII and is not locale-sensitive, and
1996  * g_utf8_casefold(), which is good for case-insensitive sorting of UTF-8.
1997  **/
1998 gint
1999 g_strncasecmp (const gchar *s1,
2000                const gchar *s2,
2001                guint n)     
2002 {
2003 #ifdef HAVE_STRNCASECMP
2004   return strncasecmp (s1, s2, n);
2005 #else
2006   gint c1, c2;
2007
2008   g_return_val_if_fail (s1 != NULL, 0);
2009   g_return_val_if_fail (s2 != NULL, 0);
2010
2011   while (n && *s1 && *s2)
2012     {
2013       n -= 1;
2014       /* According to A. Cox, some platforms have islower's that
2015        * don't work right on non-uppercase
2016        */
2017       c1 = isupper ((guchar)*s1) ? tolower ((guchar)*s1) : *s1;
2018       c2 = isupper ((guchar)*s2) ? tolower ((guchar)*s2) : *s2;
2019       if (c1 != c2)
2020         return (c1 - c2);
2021       s1++; s2++;
2022     }
2023
2024   if (n)
2025     return (((gint) (guchar) *s1) - ((gint) (guchar) *s2));
2026   else
2027     return 0;
2028 #endif
2029 }
2030
2031 gchar*
2032 g_strdelimit (gchar       *string,
2033               const gchar *delimiters,
2034               gchar        new_delim)
2035 {
2036   register gchar *c;
2037
2038   g_return_val_if_fail (string != NULL, NULL);
2039
2040   if (!delimiters)
2041     delimiters = G_STR_DELIMITERS;
2042
2043   for (c = string; *c; c++)
2044     {
2045       if (strchr (delimiters, *c))
2046         *c = new_delim;
2047     }
2048
2049   return string;
2050 }
2051
2052 gchar*
2053 g_strcanon (gchar       *string,
2054             const gchar *valid_chars,
2055             gchar        substitutor)
2056 {
2057   register gchar *c;
2058
2059   g_return_val_if_fail (string != NULL, NULL);
2060   g_return_val_if_fail (valid_chars != NULL, NULL);
2061
2062   for (c = string; *c; c++)
2063     {
2064       if (!strchr (valid_chars, *c))
2065         *c = substitutor;
2066     }
2067
2068   return string;
2069 }
2070
2071 gchar*
2072 g_strcompress (const gchar *source)
2073 {
2074   const gchar *p = source, *octal;
2075   gchar *dest = g_malloc (strlen (source) + 1);
2076   gchar *q = dest;
2077   
2078   while (*p)
2079     {
2080       if (*p == '\\')
2081         {
2082           p++;
2083           switch (*p)
2084             {
2085             case '\0':
2086               g_warning ("g_strcompress: trailing \\");
2087               goto out;
2088             case '0':  case '1':  case '2':  case '3':  case '4':
2089             case '5':  case '6':  case '7':
2090               *q = 0;
2091               octal = p;
2092               while ((p < octal + 3) && (*p >= '0') && (*p <= '7'))
2093                 {
2094                   *q = (*q * 8) + (*p - '0');
2095                   p++;
2096                 }
2097               q++;
2098               p--;
2099               break;
2100             case 'b':
2101               *q++ = '\b';
2102               break;
2103             case 'f':
2104               *q++ = '\f';
2105               break;
2106             case 'n':
2107               *q++ = '\n';
2108               break;
2109             case 'r':
2110               *q++ = '\r';
2111               break;
2112             case 't':
2113               *q++ = '\t';
2114               break;
2115             default:            /* Also handles \" and \\ */
2116               *q++ = *p;
2117               break;
2118             }
2119         }
2120       else
2121         *q++ = *p;
2122       p++;
2123     }
2124 out:
2125   *q = 0;
2126   
2127   return dest;
2128 }
2129
2130 gchar *
2131 g_strescape (const gchar *source,
2132              const gchar *exceptions)
2133 {
2134   const guchar *p;
2135   gchar *dest;
2136   gchar *q;
2137   guchar excmap[256];
2138   
2139   g_return_val_if_fail (source != NULL, NULL);
2140
2141   p = (guchar *) source;
2142   /* Each source byte needs maximally four destination chars (\777) */
2143   q = dest = g_malloc (strlen (source) * 4 + 1);
2144
2145   memset (excmap, 0, 256);
2146   if (exceptions)
2147     {
2148       guchar *e = (guchar *) exceptions;
2149
2150       while (*e)
2151         {
2152           excmap[*e] = 1;
2153           e++;
2154         }
2155     }
2156
2157   while (*p)
2158     {
2159       if (excmap[*p])
2160         *q++ = *p;
2161       else
2162         {
2163           switch (*p)
2164             {
2165             case '\b':
2166               *q++ = '\\';
2167               *q++ = 'b';
2168               break;
2169             case '\f':
2170               *q++ = '\\';
2171               *q++ = 'f';
2172               break;
2173             case '\n':
2174               *q++ = '\\';
2175               *q++ = 'n';
2176               break;
2177             case '\r':
2178               *q++ = '\\';
2179               *q++ = 'r';
2180               break;
2181             case '\t':
2182               *q++ = '\\';
2183               *q++ = 't';
2184               break;
2185             case '\\':
2186               *q++ = '\\';
2187               *q++ = '\\';
2188               break;
2189             case '"':
2190               *q++ = '\\';
2191               *q++ = '"';
2192               break;
2193             default:
2194               if ((*p < ' ') || (*p >= 0177))
2195                 {
2196                   *q++ = '\\';
2197                   *q++ = '0' + (((*p) >> 6) & 07);
2198                   *q++ = '0' + (((*p) >> 3) & 07);
2199                   *q++ = '0' + ((*p) & 07);
2200                 }
2201               else
2202                 *q++ = *p;
2203               break;
2204             }
2205         }
2206       p++;
2207     }
2208   *q = 0;
2209   return dest;
2210 }
2211
2212 gchar*
2213 g_strchug (gchar *string)
2214 {
2215   guchar *start;
2216
2217   g_return_val_if_fail (string != NULL, NULL);
2218
2219   for (start = (guchar*) string; *start && g_ascii_isspace (*start); start++)
2220     ;
2221
2222   g_memmove (string, start, strlen ((gchar *) start) + 1);
2223
2224   return string;
2225 }
2226
2227 gchar*
2228 g_strchomp (gchar *string)
2229 {
2230   gsize len;
2231
2232   g_return_val_if_fail (string != NULL, NULL);
2233
2234   len = strlen (string);
2235   while (len--)
2236     {
2237       if (g_ascii_isspace ((guchar) string[len]))
2238         string[len] = '\0';
2239       else
2240         break;
2241     }
2242
2243   return string;
2244 }
2245
2246 /**
2247  * g_strsplit:
2248  * @string: a string to split.
2249  * @delimiter: a string which specifies the places at which to split the string.
2250  *     The delimiter is not included in any of the resulting strings, unless
2251  *     @max_tokens is reached.
2252  * @max_tokens: the maximum number of pieces to split @string into. If this is
2253  *              less than 1, the string is split completely.
2254  * 
2255  * Splits a string into a maximum of @max_tokens pieces, using the given
2256  * @delimiter. If @max_tokens is reached, the remainder of @string is appended
2257  * to the last token. 
2258  *
2259  * As a special case, the result of splitting the empty string "" is an empty
2260  * vector, not a vector containing a single string. The reason for this
2261  * special case is that being able to represent a empty vector is typically
2262  * more useful than consistent handling of empty elements. If you do need
2263  * to represent empty elements, you'll need to check for the empty string
2264  * before calling g_strsplit().
2265  * 
2266  * Return value: a newly-allocated %NULL-terminated array of strings. Use 
2267  *    g_strfreev() to free it.
2268  **/
2269 gchar**
2270 g_strsplit (const gchar *string,
2271             const gchar *delimiter,
2272             gint         max_tokens)
2273 {
2274   GSList *string_list = NULL, *slist;
2275   gchar **str_array, *s;
2276   guint n = 0;
2277   const gchar *remainder;
2278
2279   g_return_val_if_fail (string != NULL, NULL);
2280   g_return_val_if_fail (delimiter != NULL, NULL);
2281   g_return_val_if_fail (delimiter[0] != '\0', NULL);
2282
2283   if (max_tokens < 1)
2284     max_tokens = G_MAXINT;
2285
2286   remainder = string;
2287   s = strstr (remainder, delimiter);
2288   if (s)
2289     {
2290       gsize delimiter_len = strlen (delimiter);   
2291
2292       while (--max_tokens && s)
2293         {
2294           gsize len;     
2295
2296           len = s - remainder;
2297           string_list = g_slist_prepend (string_list,
2298                                          g_strndup (remainder, len));
2299           n++;
2300           remainder = s + delimiter_len;
2301           s = strstr (remainder, delimiter);
2302         }
2303     }
2304   if (*string)
2305     {
2306       n++;
2307       string_list = g_slist_prepend (string_list, g_strdup (remainder));
2308     }
2309
2310   str_array = g_new (gchar*, n + 1);
2311
2312   str_array[n--] = NULL;
2313   for (slist = string_list; slist; slist = slist->next)
2314     str_array[n--] = slist->data;
2315
2316   g_slist_free (string_list);
2317
2318   return str_array;
2319 }
2320
2321 /**
2322  * g_strsplit_set:
2323  * @string: The string to be tokenized
2324  * @delimiters: A nul-terminated string containing bytes that are used
2325  *              to split the string.
2326  * @max_tokens: The maximum number of tokens to split @string into. 
2327  *              If this is less than 1, the string is split completely
2328  * 
2329  * Splits @string into a number of tokens not containing any of the characters
2330  * in @delimiter. A token is the (possibly empty) longest string that does not
2331  * contain any of the characters in @delimiters. If @max_tokens is reached, the
2332  * remainder is appended to the last token.
2333  *
2334  * For example the result of g_strsplit_set ("abc:def/ghi", ":/", -1) is a
2335  * %NULL-terminated vector containing the three strings "abc", "def", 
2336  * and "ghi".
2337  *
2338  * The result if g_strsplit_set (":def/ghi:", ":/", -1) is a %NULL-terminated
2339  * vector containing the four strings "", "def", "ghi", and "".
2340  * 
2341  * As a special case, the result of splitting the empty string "" is an empty
2342  * vector, not a vector containing a single string. The reason for this
2343  * special case is that being able to represent a empty vector is typically
2344  * more useful than consistent handling of empty elements. If you do need
2345  * to represent empty elements, you'll need to check for the empty string
2346  * before calling g_strsplit_set().
2347  *
2348  * Note that this function works on bytes not characters, so it can't be used 
2349  * to delimit UTF-8 strings for anything but ASCII characters.
2350  * 
2351  * Return value: a newly-allocated %NULL-terminated array of strings. Use 
2352  *    g_strfreev() to free it.
2353  * 
2354  * Since: 2.4
2355  **/
2356 gchar **
2357 g_strsplit_set (const gchar *string,
2358                 const gchar *delimiters,
2359                 gint         max_tokens)
2360 {
2361   gboolean delim_table[256];
2362   GSList *tokens, *list;
2363   gint n_tokens;
2364   const gchar *s;
2365   const gchar *current;
2366   gchar *token;
2367   gchar **result;
2368   
2369   g_return_val_if_fail (string != NULL, NULL);
2370   g_return_val_if_fail (delimiters != NULL, NULL);
2371
2372   if (max_tokens < 1)
2373     max_tokens = G_MAXINT;
2374
2375   if (*string == '\0')
2376     {
2377       result = g_new (char *, 1);
2378       result[0] = NULL;
2379       return result;
2380     }
2381   
2382   memset (delim_table, FALSE, sizeof (delim_table));
2383   for (s = delimiters; *s != '\0'; ++s)
2384     delim_table[*(guchar *)s] = TRUE;
2385
2386   tokens = NULL;
2387   n_tokens = 0;
2388
2389   s = current = string;
2390   while (*s != '\0')
2391     {
2392       if (delim_table[*(guchar *)s] && n_tokens + 1 < max_tokens)
2393         {
2394           gchar *token;
2395
2396           token = g_strndup (current, s - current);
2397           tokens = g_slist_prepend (tokens, token);
2398           ++n_tokens;
2399
2400           current = s + 1;
2401         }
2402       
2403       ++s;
2404     }
2405
2406   token = g_strndup (current, s - current);
2407   tokens = g_slist_prepend (tokens, token);
2408   ++n_tokens;
2409
2410   result = g_new (gchar *, n_tokens + 1);
2411
2412   result[n_tokens] = NULL;
2413   for (list = tokens; list != NULL; list = list->next)
2414     result[--n_tokens] = list->data;
2415
2416   g_slist_free (tokens);
2417   
2418   return result;
2419 }
2420
2421 /**
2422  * g_strfreev:
2423  * @str_array: a %NULL-terminated array of strings to free.
2424
2425  * Frees a %NULL-terminated array of strings, and the array itself.
2426  * If called on a %NULL value, g_strfreev() simply returns. 
2427  **/
2428 void
2429 g_strfreev (gchar **str_array)
2430 {
2431   if (str_array)
2432     {
2433       int i;
2434
2435       for (i = 0; str_array[i] != NULL; i++)
2436         g_free (str_array[i]);
2437
2438       g_free (str_array);
2439     }
2440 }
2441
2442 /**
2443  * g_strdupv:
2444  * @str_array: %NULL-terminated array of strings.
2445  * 
2446  * Copies %NULL-terminated array of strings. The copy is a deep copy;
2447  * the new array should be freed by first freeing each string, then
2448  * the array itself. g_strfreev() does this for you. If called
2449  * on a %NULL value, g_strdupv() simply returns %NULL.
2450  * 
2451  * Return value: a new %NULL-terminated array of strings.
2452  **/
2453 gchar**
2454 g_strdupv (gchar **str_array)
2455 {
2456   if (str_array)
2457     {
2458       gint i;
2459       gchar **retval;
2460
2461       i = 0;
2462       while (str_array[i])
2463         ++i;
2464           
2465       retval = g_new (gchar*, i + 1);
2466
2467       i = 0;
2468       while (str_array[i])
2469         {
2470           retval[i] = g_strdup (str_array[i]);
2471           ++i;
2472         }
2473       retval[i] = NULL;
2474
2475       return retval;
2476     }
2477   else
2478     return NULL;
2479 }
2480
2481 gchar*
2482 g_strjoinv (const gchar  *separator,
2483             gchar       **str_array)
2484 {
2485   gchar *string;
2486   gchar *ptr;
2487
2488   g_return_val_if_fail (str_array != NULL, NULL);
2489
2490   if (separator == NULL)
2491     separator = "";
2492
2493   if (*str_array)
2494     {
2495       gint i;
2496       gsize len;
2497       gsize separator_len;     
2498
2499       separator_len = strlen (separator);
2500       /* First part, getting length */
2501       len = 1 + strlen (str_array[0]);
2502       for (i = 1; str_array[i] != NULL; i++)
2503         len += strlen (str_array[i]);
2504       len += separator_len * (i - 1);
2505
2506       /* Second part, building string */
2507       string = g_new (gchar, len);
2508       ptr = g_stpcpy (string, *str_array);
2509       for (i = 1; str_array[i] != NULL; i++)
2510         {
2511           ptr = g_stpcpy (ptr, separator);
2512           ptr = g_stpcpy (ptr, str_array[i]);
2513         }
2514       }
2515   else
2516     string = g_strdup ("");
2517
2518   return string;
2519 }
2520
2521 gchar*
2522 g_strjoin (const gchar  *separator,
2523            ...)
2524 {
2525   gchar *string, *s;
2526   va_list args;
2527   gsize len;               
2528   gsize separator_len;     
2529   gchar *ptr;
2530
2531   if (separator == NULL)
2532     separator = "";
2533
2534   separator_len = strlen (separator);
2535
2536   va_start (args, separator);
2537
2538   s = va_arg (args, gchar*);
2539
2540   if (s)
2541     {
2542       /* First part, getting length */
2543       len = 1 + strlen (s);
2544
2545       s = va_arg (args, gchar*);
2546       while (s)
2547         {
2548           len += separator_len + strlen (s);
2549           s = va_arg (args, gchar*);
2550         }
2551       va_end (args);
2552
2553       /* Second part, building string */
2554       string = g_new (gchar, len);
2555
2556       va_start (args, separator);
2557
2558       s = va_arg (args, gchar*);
2559       ptr = g_stpcpy (string, s);
2560
2561       s = va_arg (args, gchar*);
2562       while (s)
2563         {
2564           ptr = g_stpcpy (ptr, separator);
2565           ptr = g_stpcpy (ptr, s);
2566           s = va_arg (args, gchar*);
2567         }
2568     }
2569   else
2570     string = g_strdup ("");
2571
2572   va_end (args);
2573
2574   return string;
2575 }
2576
2577
2578 /**
2579  * g_strstr_len:
2580  * @haystack: a string.
2581  * @haystack_len: the maximum length of @haystack. Note that -1 is
2582  * a valid length, if @haystack is nul-terminated, meaning it will
2583  * search through the whole string.
2584  * @needle: the string to search for.
2585  *
2586  * Searches the string @haystack for the first occurrence
2587  * of the string @needle, limiting the length of the search
2588  * to @haystack_len. 
2589  *
2590  * Return value: a pointer to the found occurrence, or
2591  *    %NULL if not found.
2592  **/
2593 gchar *
2594 g_strstr_len (const gchar *haystack,
2595               gssize       haystack_len,
2596               const gchar *needle)
2597 {
2598   g_return_val_if_fail (haystack != NULL, NULL);
2599   g_return_val_if_fail (needle != NULL, NULL);
2600
2601   if (haystack_len < 0)
2602     return strstr (haystack, needle);
2603   else
2604     {
2605       const gchar *p = haystack;
2606       gsize needle_len = strlen (needle);
2607       const gchar *end;
2608       gsize i;
2609
2610       if (needle_len == 0)
2611         return (gchar *)haystack;
2612
2613       if (haystack_len < needle_len)
2614         return NULL;
2615       
2616       end = haystack + haystack_len - needle_len;
2617       
2618       while (p <= end && *p)
2619         {
2620           for (i = 0; i < needle_len; i++)
2621             if (p[i] != needle[i])
2622               goto next;
2623           
2624           return (gchar *)p;
2625           
2626         next:
2627           p++;
2628         }
2629       
2630       return NULL;
2631     }
2632 }
2633
2634 /**
2635  * g_strrstr:
2636  * @haystack: a nul-terminated string.
2637  * @needle: the nul-terminated string to search for.
2638  *
2639  * Searches the string @haystack for the last occurrence
2640  * of the string @needle.
2641  *
2642  * Return value: a pointer to the found occurrence, or
2643  *    %NULL if not found.
2644  **/
2645 gchar *
2646 g_strrstr (const gchar *haystack,
2647            const gchar *needle)
2648 {
2649   gsize i;
2650   gsize needle_len;
2651   gsize haystack_len;
2652   const gchar *p;
2653       
2654   g_return_val_if_fail (haystack != NULL, NULL);
2655   g_return_val_if_fail (needle != NULL, NULL);
2656
2657   needle_len = strlen (needle);
2658   haystack_len = strlen (haystack);
2659
2660   if (needle_len == 0)
2661     return (gchar *)haystack;
2662
2663   if (haystack_len < needle_len)
2664     return NULL;
2665   
2666   p = haystack + haystack_len - needle_len;
2667
2668   while (p >= haystack)
2669     {
2670       for (i = 0; i < needle_len; i++)
2671         if (p[i] != needle[i])
2672           goto next;
2673       
2674       return (gchar *)p;
2675       
2676     next:
2677       p--;
2678     }
2679   
2680   return NULL;
2681 }
2682
2683 /**
2684  * g_strrstr_len:
2685  * @haystack: a nul-terminated string.
2686  * @haystack_len: the maximum length of @haystack.
2687  * @needle: the nul-terminated string to search for.
2688  *
2689  * Searches the string @haystack for the last occurrence
2690  * of the string @needle, limiting the length of the search
2691  * to @haystack_len. 
2692  *
2693  * Return value: a pointer to the found occurrence, or
2694  *    %NULL if not found.
2695  **/
2696 gchar *
2697 g_strrstr_len (const gchar *haystack,
2698                gssize        haystack_len,
2699                const gchar *needle)
2700 {
2701   g_return_val_if_fail (haystack != NULL, NULL);
2702   g_return_val_if_fail (needle != NULL, NULL);
2703   
2704   if (haystack_len < 0)
2705     return g_strrstr (haystack, needle);
2706   else
2707     {
2708       gsize needle_len = strlen (needle);
2709       const gchar *haystack_max = haystack + haystack_len;
2710       const gchar *p = haystack;
2711       gsize i;
2712
2713       while (p < haystack_max && *p)
2714         p++;
2715
2716       if (p < haystack + needle_len)
2717         return NULL;
2718         
2719       p -= needle_len;
2720
2721       while (p >= haystack)
2722         {
2723           for (i = 0; i < needle_len; i++)
2724             if (p[i] != needle[i])
2725               goto next;
2726           
2727           return (gchar *)p;
2728           
2729         next:
2730           p--;
2731         }
2732
2733       return NULL;
2734     }
2735 }
2736
2737
2738 /**
2739  * g_str_has_suffix:
2740  * @str: a nul-terminated string.
2741  * @suffix: the nul-terminated suffix to look for.
2742  *
2743  * Looks whether the string @str ends with @suffix.
2744  *
2745  * Return value: %TRUE if @str end with @suffix, %FALSE otherwise.
2746  *
2747  * Since: 2.2
2748  **/
2749 gboolean
2750 g_str_has_suffix (const gchar  *str,
2751                   const gchar  *suffix)
2752 {
2753   int str_len;
2754   int suffix_len;
2755   
2756   g_return_val_if_fail (str != NULL, FALSE);
2757   g_return_val_if_fail (suffix != NULL, FALSE);
2758
2759   str_len = strlen (str);
2760   suffix_len = strlen (suffix);
2761
2762   if (str_len < suffix_len)
2763     return FALSE;
2764
2765   return strcmp (str + str_len - suffix_len, suffix) == 0;
2766 }
2767
2768 /**
2769  * g_str_has_prefix:
2770  * @str: a nul-terminated string.
2771  * @prefix: the nul-terminated prefix to look for.
2772  *
2773  * Looks whether the string @str begins with @prefix.
2774  *
2775  * Return value: %TRUE if @str begins with @prefix, %FALSE otherwise.
2776  *
2777  * Since: 2.2
2778  **/
2779 gboolean
2780 g_str_has_prefix (const gchar  *str,
2781                   const gchar  *prefix)
2782 {
2783   int str_len;
2784   int prefix_len;
2785   
2786   g_return_val_if_fail (str != NULL, FALSE);
2787   g_return_val_if_fail (prefix != NULL, FALSE);
2788
2789   str_len = strlen (str);
2790   prefix_len = strlen (prefix);
2791
2792   if (str_len < prefix_len)
2793     return FALSE;
2794   
2795   return strncmp (str, prefix, prefix_len) == 0;
2796 }
2797
2798
2799 /**
2800  * g_strip_context:
2801  * @msgid: a string
2802  * @msgval: another string
2803  * 
2804  * An auxiliary function for gettext() support (see Q_()).
2805  * 
2806  * Return value: @msgval, unless @msgval is identical to @msgid and contains
2807  *   a '|' character, in which case a pointer to the substring of msgid after
2808  *   the first '|' character is returned. 
2809  *
2810  * Since: 2.4
2811  **/
2812 G_CONST_RETURN gchar *
2813 g_strip_context  (const gchar *msgid, 
2814                   const gchar *msgval)
2815 {
2816   if (msgval == msgid)
2817     {
2818       const char *c = strchr (msgid, '|');
2819       if (c != NULL)
2820         return c + 1;
2821     }
2822   
2823   return msgval;
2824 }
2825
2826
2827 /**
2828  * g_strv_length:
2829  * @str_array: a %NULL-terminated array of strings.
2830  * 
2831  * Returns the length of the given %NULL-terminated 
2832  * string array @str_array.
2833  * 
2834  * Return value: length of @str_array.
2835  *
2836  * Since: 2.6
2837  **/
2838 guint
2839 g_strv_length (gchar **str_array)
2840 {
2841   guint i = 0;
2842
2843   g_return_val_if_fail (str_array != NULL, 0);
2844
2845   while (str_array[i])
2846     ++i;
2847
2848   return i;
2849 }
2850
2851
2852 /**
2853  * g_dpgettext:
2854  * @domain: the translation domain to use, or %NULL to use
2855  *   the domain set with textdomain()
2856  * @msgctxtid: a combined message context and message id, separated
2857  *   by a \004 character
2858  * @msgidoffset: the offset of the message id in @msgctxid
2859  *
2860  * This function is a variant of g_dgettext() which supports
2861  * a disambiguating message context. GNU gettext uses the
2862  * '\004' character to separate the message context and
2863  * message id in @msgctxtid.
2864  * If 0 is passed as @msgidoffset, this function will fall back to
2865  * trying to use the deprecated convention of using "|" as a separation
2866  * character.
2867  *
2868  * This uses g_dgettext() internally.  See that functions for differences
2869  * with dgettext() proper.
2870  *
2871  * Applications should normally not use this function directly,
2872  * but use the C_() macro for translations with context.
2873  *
2874  * Returns: The translated string
2875  *
2876  * Since: 2.16
2877  */
2878 G_CONST_RETURN gchar *
2879 g_dpgettext (const gchar *domain, 
2880              const gchar *msgctxtid, 
2881              gsize        msgidoffset)
2882 {
2883   const gchar *translation;
2884   gchar *sep;
2885
2886   translation = g_dgettext (domain, msgctxtid);
2887
2888   if (translation == msgctxtid)
2889     {
2890       if (msgidoffset > 0)
2891         return msgctxtid + msgidoffset;
2892
2893       sep = strchr (msgctxtid, '|');
2894  
2895       if (sep)
2896         {
2897           /* try with '\004' instead of '|', in case
2898            * xgettext -kQ_:1g was used
2899            */
2900           gchar *tmp = g_alloca (strlen (msgctxtid) + 1);
2901           strcpy (tmp, msgctxtid);
2902           tmp[sep - msgctxtid] = '\004';
2903
2904           translation = g_dgettext (domain, tmp);
2905    
2906           if (translation == tmp)
2907             return sep + 1; 
2908         }
2909     }
2910
2911   return translation;
2912 }
2913
2914 /* This function is taken from gettext.h 
2915  * GNU gettext uses '\004' to separate context and msgid in .mo files.
2916  */
2917 /**
2918  * g_dpgettext2:
2919  * @domain: the translation domain to use, or %NULL to use
2920  *   the domain set with textdomain()
2921  * @context: the message context
2922  * @msgid: the message
2923  *
2924  * This function is a variant of g_dgettext() which supports
2925  * a disambiguating message context. GNU gettext uses the
2926  * '\004' character to separate the message context and
2927  * message id in @msgctxtid.
2928  *
2929  * This uses g_dgettext() internally.  See that functions for differences
2930  * with dgettext() proper.
2931  *
2932  * This function differs from C_() in that it is not a macro and 
2933  * thus you may use non-string-literals as context and msgid arguments.
2934  *
2935  * Returns: The translated string
2936  *
2937  * Since: 2.18
2938  */
2939 G_CONST_RETURN char *
2940 g_dpgettext2 (const char *domain,
2941               const char *msgctxt,
2942               const char *msgid)
2943 {
2944   size_t msgctxt_len = strlen (msgctxt) + 1;
2945   size_t msgid_len = strlen (msgid) + 1;
2946   const char *translation;
2947   char* msg_ctxt_id;
2948
2949   msg_ctxt_id = g_alloca (msgctxt_len + msgid_len);
2950
2951   memcpy (msg_ctxt_id, msgctxt, msgctxt_len - 1);
2952   msg_ctxt_id[msgctxt_len - 1] = '\004';
2953   memcpy (msg_ctxt_id + msgctxt_len, msgid, msgid_len);
2954
2955   translation = g_dgettext (domain, msg_ctxt_id);
2956
2957   if (translation == msg_ctxt_id) 
2958     {
2959       /* try the old way of doing message contexts, too */
2960       msg_ctxt_id[msgctxt_len - 1] = '|';
2961       translation = g_dgettext (domain, msg_ctxt_id);
2962
2963       if (translation == msg_ctxt_id)
2964         return msgid;
2965     }
2966
2967   return translation;
2968 }
2969
2970 static gboolean
2971 _g_dgettext_should_translate (void)
2972 {
2973   static gsize translate = 0;
2974   enum {
2975     SHOULD_TRANSLATE = 1,
2976     SHOULD_NOT_TRANSLATE = 2
2977   };
2978
2979   if (G_UNLIKELY (g_once_init_enter (&translate)))
2980     {
2981       gboolean should_translate = TRUE;
2982
2983       const char *default_domain     = textdomain (NULL);
2984       const char *translator_comment = gettext ("");
2985 #ifndef G_OS_WIN32
2986       const char *translate_locale   = setlocale (LC_MESSAGES, NULL);
2987 #else
2988       const char *translate_locale   = g_win32_getlocale ();
2989 #endif
2990       /* We should NOT translate only if all the following hold:
2991        *   - user has called textdomain() and set textdomain to non-default
2992        *   - default domain has no translations
2993        *   - locale does not start with "en_" and is not "C"
2994        *
2995        * Rationale:
2996        *   - If text domain is still the default domain, maybe user calls
2997        *     it later. Continue with old behavior of translating.
2998        *   - If locale starts with "en_", we can continue using the
2999        *     translations even if the app doesn't have translations for
3000        *     this locale.  That is, en_UK and en_CA for example.
3001        *   - If locale is "C", maybe user calls setlocale(LC_ALL,"") later.
3002        *     Continue with old behavior of translating.
3003        */
3004       if (0 != strcmp (default_domain, "messages") &&
3005           '\0' == *translator_comment &&
3006           0 != strncmp (translate_locale, "en_", 3) &&
3007           0 != strcmp (translate_locale, "C"))
3008         should_translate = FALSE;
3009       
3010       g_once_init_leave (&translate,
3011                          should_translate ?
3012                          SHOULD_TRANSLATE :
3013                          SHOULD_NOT_TRANSLATE);
3014     }
3015
3016   return translate == SHOULD_TRANSLATE;
3017 }
3018
3019 /**
3020  * g_dgettext:
3021  * @domain: the translation domain to use, or %NULL to use
3022  *   the domain set with textdomain()
3023  * @msgid: message to translate
3024  *
3025  * This function is a wrapper of dgettext() which does not translate
3026  * the message if the default domain as set with textdomain() has no
3027  * translations for the current locale.
3028  *
3029  * The advantage of using this function over dgettext() proper is that
3030  * libraries using this function (like GTK+) will not use translations
3031  * if the application using the library does not have translations for
3032  * the current locale.  This results in a consistent English-only
3033  * interface instead of one having partial translations.  For this
3034  * feature to work, the call to textdomain() and setlocale() should
3035  * precede any g_dgettext() invocations.  For GTK+, it means calling
3036  * textdomain() before gtk_init or its variants.
3037  *
3038  * This function disables translations if and only if upon its first
3039  * call all the following conditions hold:
3040  * <itemizedlist>
3041  * <listitem>@domain is not %NULL</listitem>
3042  * <listitem>textdomain() has been called to set a default text domain</listitem>
3043  * <listitem>there is no translations available for the default text domain
3044  *           and the current locale</listitem>
3045  * <listitem>current locale is not "C" or any English locales (those
3046  *           starting with "en_")</listitem>
3047  * </itemizedlist>
3048  *
3049  * Note that this behavior may not be desired for example if an application
3050  * has its untranslated messages in a language other than English.  In those
3051  * cases the application should call textdomain() after initializing GTK+.
3052  *
3053  * Applications should normally not use this function directly,
3054  * but use the _() macro for translations.
3055  *
3056  * Returns: The translated string
3057  *
3058  * Since: 2.18
3059  */
3060 G_CONST_RETURN gchar *
3061 g_dgettext (const gchar *domain,
3062             const gchar *msgid)
3063 {
3064   if (domain && G_UNLIKELY (!_g_dgettext_should_translate ()))
3065     return msgid;
3066
3067   return dgettext (domain, msgid);
3068 }
3069
3070 /**
3071  * g_dngettext:
3072  * @domain: the translation domain to use, or %NULL to use
3073  *   the domain set with textdomain()
3074  * @msgid: message to translate
3075  * @msgid_plural: plural form of the message
3076  * @n: the quantity for which translation is needed
3077  *
3078  * This function is a wrapper of dngettext() which does not translate
3079  * the message if the default domain as set with textdomain() has no
3080  * translations for the current locale.
3081  *
3082  * See g_dgettext() for details of how this differs from dngettext()
3083  * proper.
3084  *
3085  * Returns: The translated string
3086  *
3087  * Since: 2.18
3088  */
3089 G_CONST_RETURN gchar *
3090 g_dngettext (const gchar *domain,
3091              const gchar *msgid,
3092              const gchar *msgid_plural,
3093              gulong       n)
3094 {
3095   if (domain && G_UNLIKELY (!_g_dgettext_should_translate ()))
3096     return n == 1 ? msgid : msgid_plural;
3097
3098   return dngettext (domain, msgid, msgid_plural, n);
3099 }
3100
3101
3102 #define __G_STRFUNCS_C__
3103 #include "galiasdef.c"