1 /* GLIB - Library of useful routines for C programming
2 * Copyright (C) 1995-1997 Peter Mattis, Spencer Kimball and Josh MacDonald
4 * This library is free software; you can redistribute it and/or
5 * modify it under the terms of the GNU Library General Public
6 * License as published by the Free Software Foundation; either
7 * version 2 of the License, or (at your option) any later version.
9 * This library is distributed in the hope that it will be useful,
10 * but WITHOUT ANY WARRANTY; without even the implied warranty of
11 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
12 * Library General Public License for more details.
14 * You should have received a copy of the GNU Library General Public
15 * License along with this library; if not, write to the
16 * Free Software Foundation, Inc., 59 Temple Place - Suite 330,
17 * Boston, MA 02111-1307, USA.
21 * Modified by the GLib Team and others 1997-1999. See the AUTHORS
22 * file for a list of people on the GLib Team. See the ChangeLog
23 * files for a list of changes. These files are distributed with
24 * GLib at ftp://ftp.gtk.org/pub/gtk/.
40 #include <ctype.h> /* For tolower() */
41 #if !defined (HAVE_STRSIGNAL) || !defined(NO_SYS_SIGLIST_DECL)
45 /* do not include <unistd.h> in this place since it
46 * inteferes with g_strsignal() on some OSes
50 g_strdup (const gchar
*str
)
56 new_str
= g_new (char, strlen (str
) + 1);
57 strcpy (new_str
, str
);
66 g_memdup (gconstpointer mem
,
73 new_mem
= g_malloc (byte_size
);
74 memcpy (new_mem
, mem
, byte_size
);
83 g_strndup (const gchar
*str
,
90 new_str
= g_new (gchar
, n
+ 1);
91 strncpy (new_str
, str
, n
);
101 g_strnfill (guint length
,
104 register gchar
*str
, *s
, *end
;
106 str
= g_new (gchar
, length
+ 1);
117 g_strdup_vprintf (const gchar
*format
,
123 G_VA_COPY (args2
, args1
);
125 buffer
= g_new (gchar
, g_printf_string_upper_bound (format
, args1
));
127 vsprintf (buffer
, format
, args2
);
134 g_strdup_printf (const gchar
*format
,
140 va_start (args
, format
);
141 buffer
= g_strdup_vprintf (format
, args
);
148 g_strconcat (const gchar
*string1
, ...)
155 g_return_val_if_fail (string1
!= NULL
, NULL
);
157 l
= 1 + strlen (string1
);
158 va_start (args
, string1
);
159 s
= va_arg (args
, gchar
*);
163 s
= va_arg (args
, gchar
*);
167 concat
= g_new (gchar
, l
);
170 strcat (concat
, string1
);
171 va_start (args
, string1
);
172 s
= va_arg (args
, gchar
*);
176 s
= va_arg (args
, gchar
*);
184 g_strtod (const gchar
*nptr
,
192 g_return_val_if_fail (nptr
!= NULL
, 0);
197 val_1
= strtod (nptr
, &fail_pos_1
);
199 if (fail_pos_1
&& fail_pos_1
[0] != 0)
203 old_locale
= setlocale (LC_NUMERIC
, "C");
204 val_2
= strtod (nptr
, &fail_pos_2
);
205 setlocale (LC_NUMERIC
, old_locale
);
208 if (!fail_pos_1
|| fail_pos_1
[0] == 0 || fail_pos_1
>= fail_pos_2
)
211 *endptr
= fail_pos_1
;
217 *endptr
= fail_pos_2
;
223 g_strerror (gint errnum
)
225 static GStaticPrivate msg_private
= G_STATIC_PRIVATE_INIT
;
229 return strerror (errnum
);
234 case E2BIG
: return "argument list too long";
237 case EACCES
: return "permission denied";
240 case EADDRINUSE
: return "address already in use";
243 case EADDRNOTAVAIL
: return "can't assign requested address";
246 case EADV
: return "advertise error";
249 case EAFNOSUPPORT
: return "address family not supported by protocol family";
252 case EAGAIN
: return "try again";
255 case EALIGN
: return "EALIGN";
258 case EALREADY
: return "operation already in progress";
261 case EBADE
: return "bad exchange descriptor";
264 case EBADF
: return "bad file number";
267 case EBADFD
: return "file descriptor in bad state";
270 case EBADMSG
: return "not a data message";
273 case EBADR
: return "bad request descriptor";
276 case EBADRPC
: return "RPC structure is bad";
279 case EBADRQC
: return "bad request code";
282 case EBADSLT
: return "invalid slot";
285 case EBFONT
: return "bad font file format";
288 case EBUSY
: return "mount device busy";
291 case ECHILD
: return "no children";
294 case ECHRNG
: return "channel number out of range";
297 case ECOMM
: return "communication error on send";
300 case ECONNABORTED
: return "software caused connection abort";
303 case ECONNREFUSED
: return "connection refused";
306 case ECONNRESET
: return "connection reset by peer";
308 #if defined(EDEADLK) && (!defined(EWOULDBLOCK) || (EDEADLK != EWOULDBLOCK))
309 case EDEADLK
: return "resource deadlock avoided";
312 case EDEADLOCK
: return "resource deadlock avoided";
315 case EDESTADDRREQ
: return "destination address required";
318 case EDIRTY
: return "mounting a dirty fs w/o force";
321 case EDOM
: return "math argument out of range";
324 case EDOTDOT
: return "cross mount point";
327 case EDQUOT
: return "disk quota exceeded";
330 case EDUPPKG
: return "duplicate package name";
333 case EEXIST
: return "file already exists";
336 case EFAULT
: return "bad address in system call argument";
339 case EFBIG
: return "file too large";
342 case EHOSTDOWN
: return "host is down";
345 case EHOSTUNREACH
: return "host is unreachable";
348 case EIDRM
: return "identifier removed";
351 case EINIT
: return "initialization error";
354 case EINPROGRESS
: return "operation now in progress";
357 case EINTR
: return "interrupted system call";
360 case EINVAL
: return "invalid argument";
363 case EIO
: return "I/O error";
366 case EISCONN
: return "socket is already connected";
369 case EISDIR
: return "illegal operation on a directory";
372 case EISNAM
: return "is a name file";
375 case ELBIN
: return "ELBIN";
378 case EL2HLT
: return "level 2 halted";
381 case EL2NSYNC
: return "level 2 not synchronized";
384 case EL3HLT
: return "level 3 halted";
387 case EL3RST
: return "level 3 reset";
390 case ELIBACC
: return "can not access a needed shared library";
393 case ELIBBAD
: return "accessing a corrupted shared library";
396 case ELIBEXEC
: return "can not exec a shared library directly";
399 case ELIBMAX
: return "attempting to link in more shared libraries than system limit";
402 case ELIBSCN
: return ".lib section in a.out corrupted";
405 case ELNRNG
: return "link number out of range";
408 case ELOOP
: return "too many levels of symbolic links";
411 case EMFILE
: return "too many open files";
414 case EMLINK
: return "too many links";
417 case EMSGSIZE
: return "message too long";
420 case EMULTIHOP
: return "multihop attempted";
423 case ENAMETOOLONG
: return "file name too long";
426 case ENAVAIL
: return "not available";
429 case ENET
: return "ENET";
432 case ENETDOWN
: return "network is down";
435 case ENETRESET
: return "network dropped connection on reset";
438 case ENETUNREACH
: return "network is unreachable";
441 case ENFILE
: return "file table overflow";
444 case ENOANO
: return "anode table overflow";
446 #if defined(ENOBUFS) && (!defined(ENOSR) || (ENOBUFS != ENOSR))
447 case ENOBUFS
: return "no buffer space available";
450 case ENOCSI
: return "no CSI structure available";
453 case ENODATA
: return "no data available";
456 case ENODEV
: return "no such device";
459 case ENOENT
: return "no such file or directory";
462 case ENOEXEC
: return "exec format error";
465 case ENOLCK
: return "no locks available";
468 case ENOLINK
: return "link has be severed";
471 case ENOMEM
: return "not enough memory";
474 case ENOMSG
: return "no message of desired type";
477 case ENONET
: return "machine is not on the network";
480 case ENOPKG
: return "package not installed";
483 case ENOPROTOOPT
: return "bad proocol option";
486 case ENOSPC
: return "no space left on device";
489 case ENOSR
: return "out of stream resources";
492 case ENOSTR
: return "not a stream device";
495 case ENOSYM
: return "unresolved symbol name";
498 case ENOSYS
: return "function not implemented";
501 case ENOTBLK
: return "block device required";
504 case ENOTCONN
: return "socket is not connected";
507 case ENOTDIR
: return "not a directory";
510 case ENOTEMPTY
: return "directory not empty";
513 case ENOTNAM
: return "not a name file";
516 case ENOTSOCK
: return "socket operation on non-socket";
519 case ENOTTY
: return "inappropriate device for ioctl";
522 case ENOTUNIQ
: return "name not unique on network";
525 case ENXIO
: return "no such device or address";
528 case EOPNOTSUPP
: return "operation not supported on socket";
531 case EPERM
: return "not owner";
534 case EPFNOSUPPORT
: return "protocol family not supported";
537 case EPIPE
: return "broken pipe";
540 case EPROCLIM
: return "too many processes";
543 case EPROCUNAVAIL
: return "bad procedure for program";
546 case EPROGMISMATCH
: return "program version wrong";
549 case EPROGUNAVAIL
: return "RPC program not available";
552 case EPROTO
: return "protocol error";
554 #ifdef EPROTONOSUPPORT
555 case EPROTONOSUPPORT
: return "protocol not suppored";
558 case EPROTOTYPE
: return "protocol wrong type for socket";
561 case ERANGE
: return "math result unrepresentable";
563 #if defined(EREFUSED) && (!defined(ECONNREFUSED) || (EREFUSED != ECONNREFUSED))
564 case EREFUSED
: return "EREFUSED";
567 case EREMCHG
: return "remote address changed";
570 case EREMDEV
: return "remote device";
573 case EREMOTE
: return "pathname hit remote file system";
576 case EREMOTEIO
: return "remote i/o error";
578 #ifdef EREMOTERELEASE
579 case EREMOTERELEASE
: return "EREMOTERELEASE";
582 case EROFS
: return "read-only file system";
585 case ERPCMISMATCH
: return "RPC version is wrong";
588 case ERREMOTE
: return "object is remote";
591 case ESHUTDOWN
: return "can't send afer socket shutdown";
593 #ifdef ESOCKTNOSUPPORT
594 case ESOCKTNOSUPPORT
: return "socket type not supported";
597 case ESPIPE
: return "invalid seek";
600 case ESRCH
: return "no such process";
603 case ESRMNT
: return "srmount error";
606 case ESTALE
: return "stale remote file handle";
609 case ESUCCESS
: return "Error 0";
612 case ETIME
: return "timer expired";
615 case ETIMEDOUT
: return "connection timed out";
618 case ETOOMANYREFS
: return "too many references: can't splice";
621 case ETXTBSY
: return "text file or pseudo-device busy";
624 case EUCLEAN
: return "structure needs cleaning";
627 case EUNATCH
: return "protocol driver not attached";
630 case EUSERS
: return "too many users";
633 case EVERSION
: return "version mismatch";
635 #if defined(EWOULDBLOCK) && (!defined(EAGAIN) || (EWOULDBLOCK != EAGAIN))
636 case EWOULDBLOCK
: return "operation would block";
639 case EXDEV
: return "cross-domain link";
642 case EXFULL
: return "message tables full";
645 #else /* NO_SYS_ERRLIST */
647 extern char *sys_errlist
[];
649 if ((errnum
> 0) && (errnum
<= sys_nerr
))
650 return sys_errlist
[errnum
];
651 #endif /* NO_SYS_ERRLIST */
653 msg
= g_static_private_get (&msg_private
);
656 msg
= g_new (gchar
, 64);
657 g_static_private_set (&msg_private
, msg
, g_free
);
660 sprintf (msg
, "unknown error (%d)", errnum
);
666 g_strsignal (gint signum
)
668 static GStaticPrivate msg_private
= G_STATIC_PRIVATE_INIT
;
671 #ifdef HAVE_STRSIGNAL
672 extern char *strsignal (int sig
);
673 return strsignal (signum
);
678 case SIGHUP
: return "Hangup";
681 case SIGINT
: return "Interrupt";
684 case SIGQUIT
: return "Quit";
687 case SIGILL
: return "Illegal instruction";
690 case SIGTRAP
: return "Trace/breakpoint trap";
693 case SIGABRT
: return "IOT trap/Abort";
696 case SIGBUS
: return "Bus error";
699 case SIGFPE
: return "Floating point exception";
702 case SIGKILL
: return "Killed";
705 case SIGUSR1
: return "User defined signal 1";
708 case SIGSEGV
: return "Segmentation fault";
711 case SIGUSR2
: return "User defined signal 2";
714 case SIGPIPE
: return "Broken pipe";
717 case SIGALRM
: return "Alarm clock";
720 case SIGTERM
: return "Terminated";
723 case SIGSTKFLT
: return "Stack fault";
726 case SIGCHLD
: return "Child exited";
729 case SIGCONT
: return "Continued";
732 case SIGSTOP
: return "Stopped (signal)";
735 case SIGTSTP
: return "Stopped";
738 case SIGTTIN
: return "Stopped (tty input)";
741 case SIGTTOU
: return "Stopped (tty output)";
744 case SIGURG
: return "Urgent condition";
747 case SIGXCPU
: return "CPU time limit exceeded";
750 case SIGXFSZ
: return "File size limit exceeded";
753 case SIGVTALRM
: return "Virtual time alarm";
756 case SIGPROF
: return "Profile signal";
759 case SIGWINCH
: return "Window size changed";
762 case SIGIO
: return "Possible I/O";
765 case SIGPWR
: return "Power failure";
768 case SIGUNUSED
: return "Unused signal";
771 #else /* NO_SYS_SIGLIST */
773 #ifdef NO_SYS_SIGLIST_DECL
774 extern char *sys_siglist
[]; /*(see Tue Jan 19 00:44:24 1999 in changelog)*/
777 return (char*) /* this function should return const --josh */ sys_siglist
[signum
];
778 #endif /* NO_SYS_SIGLIST */
780 msg
= g_static_private_get (&msg_private
);
783 msg
= g_new (gchar
, 64);
784 g_static_private_set (&msg_private
, msg
, g_free
);
787 sprintf (msg
, "unknown signal (%d)", signum
);
793 g_printf_string_upper_bound (const gchar
* format
,
800 gboolean long_int
= FALSE
;
801 gboolean extra_long
= FALSE
;
808 gboolean done
= FALSE
;
810 while (*format
&& !done
)
817 len
+= va_arg (args
, int);
828 /* add specified format length, since it might exceed the
829 * size we assume it to have.
832 len
+= strtol (format
, (char**) &format
, 10);
835 /* ignore short int flag, since all args have at least the
836 * same size as an int
841 extra_long
= TRUE
; /* linux specific */
851 string_arg
= va_arg (args
, char *);
853 len
+= strlen (string_arg
);
856 /* add enough padding to hold "(null)" identifier */
869 (void) va_arg (args
, gint64
);
871 #endif /* G_HAVE_GINT64 */
874 (void) va_arg (args
, long);
876 (void) va_arg (args
, int);
878 len
+= extra_long
? 64 : 32;
884 (void) va_arg (args
, long);
892 #ifdef HAVE_LONG_DOUBLE
894 (void) va_arg (args
, long double);
896 #endif /* HAVE_LONG_DOUBLE */
897 (void) va_arg (args
, double);
898 len
+= extra_long
? 64 : 32;
902 (void) va_arg (args
, int);
908 (void) va_arg (args
, void*);
917 /* ignore unknow/invalid flags */
930 g_strdown (gchar
*string
)
934 g_return_if_fail (string
!= NULL
);
946 g_strup (gchar
*string
)
950 g_return_if_fail (string
!= NULL
);
962 g_strreverse (gchar
*string
)
964 g_return_if_fail (string
!= NULL
);
968 register gchar
*h
, *t
;
971 t
= string
+ strlen (string
) - 1;
987 g_strcasecmp (const gchar
*s1
,
990 #ifdef HAVE_STRCASECMP
991 return strcasecmp (s1
, s2
);
995 g_return_val_if_fail (s1
!= NULL
, 0);
996 g_return_val_if_fail (s2
!= NULL
, 0);
1000 /* According to A. Cox, some platforms have islower's that
1001 * don't work right on non-uppercase
1003 c1
= isupper ((guchar
)*s1
) ? tolower ((guchar
)*s1
) : *s1
;
1004 c2
= isupper ((guchar
)*s2
) ? tolower ((guchar
)*s2
) : *s2
;
1010 return (((gint
)(guchar
) *s1
) - ((gint
)(guchar
) *s2
));
1015 g_strncasecmp (const gchar
*s1
,
1019 #ifdef HAVE_STRNCASECMP
1020 return strncasecmp (s1
, s2
, n
);
1024 g_return_val_if_fail (s1
!= NULL
, 0);
1025 g_return_val_if_fail (s2
!= NULL
, 0);
1027 while (n
-- && *s1
&& *s2
)
1029 /* According to A. Cox, some platforms have islower's that
1030 * don't work right on non-uppercase
1032 c1
= isupper ((guchar
)*s1
) ? tolower ((guchar
)*s1
) : *s1
;
1033 c2
= isupper ((guchar
)*s2
) ? tolower ((guchar
)*s2
) : *s2
;
1040 return (((gint
)(guchar
) *s1
) - ((gint
)(guchar
) *s2
));
1047 g_strdelimit (gchar
*string
,
1048 const gchar
*delimiters
,
1053 g_return_val_if_fail (string
!= NULL
, NULL
);
1056 delimiters
= G_STR_DELIMITERS
;
1058 for (c
= string
; *c
; c
++)
1060 if (strchr (delimiters
, *c
))
1068 g_strescape (gchar
*string
)
1072 guint backslashes
= 0;
1075 g_return_val_if_fail (string
!= NULL
, NULL
);
1077 while (*p
!= '\000')
1078 backslashes
+= (*p
++ == '\\');
1081 return g_strdup (string
);
1083 escaped
= g_new (gchar
, strlen (string
) + backslashes
+ 1);
1088 while (*p
!= '\000')
1099 /* blame Elliot for these next five routines */
1101 g_strchug (gchar
*string
)
1105 g_return_val_if_fail (string
!= NULL
, NULL
);
1107 for (start
= string
; *start
&& isspace (*start
); start
++)
1110 strcpy (string
, start
);
1116 g_strchomp (gchar
*string
)
1120 g_return_val_if_fail (string
!= NULL
, NULL
);
1125 for (s
= string
+ strlen (string
) - 1; s
>= string
&& isspace (*s
); s
--)
1132 g_strsplit (const gchar
*string
,
1133 const gchar
*delimiter
,
1136 GSList
*string_list
= NULL
, *slist
;
1137 gchar
**str_array
, *s
;
1140 g_return_val_if_fail (string
!= NULL
, NULL
);
1141 g_return_val_if_fail (delimiter
!= NULL
, NULL
);
1144 max_tokens
= G_MAXINT
;
1146 s
= strstr (string
, delimiter
);
1149 guint delimiter_len
= strlen (delimiter
);
1157 new_string
= g_new (gchar
, len
+ 1);
1158 strncpy (new_string
, string
, len
);
1159 new_string
[len
] = 0;
1160 string_list
= g_slist_prepend (string_list
, new_string
);
1162 string
= s
+ delimiter_len
;
1163 s
= strstr (string
, delimiter
);
1165 while (--max_tokens
&& s
);
1170 string_list
= g_slist_prepend (string_list
, g_strdup (string
));
1173 str_array
= g_new (gchar
*, n
);
1177 str_array
[i
--] = NULL
;
1178 for (slist
= string_list
; slist
; slist
= slist
->next
)
1179 str_array
[i
--] = slist
->data
;
1181 g_slist_free (string_list
);
1187 g_strfreev (gchar
**str_array
)
1193 for(i
= 0; str_array
[i
] != NULL
; i
++)
1194 g_free(str_array
[i
]);
1201 g_strjoinv (const gchar
*separator
,
1206 g_return_val_if_fail (str_array
!= NULL
, NULL
);
1208 if(separator
== NULL
)
1214 guint separator_len
;
1216 separator_len
= strlen (separator
);
1217 len
= 1 + strlen (str_array
[0]);
1218 for(i
= 1; str_array
[i
] != NULL
; i
++)
1219 len
+= separator_len
+ strlen(str_array
[i
]);
1221 string
= g_new (gchar
, len
);
1223 strcat (string
, *str_array
);
1224 for (i
= 1; str_array
[i
] != NULL
; i
++)
1226 strcat (string
, separator
);
1227 strcat (string
, str_array
[i
]);
1231 string
= g_strdup ("");
1237 g_strjoin (const gchar
*separator
,
1243 guint separator_len
;
1245 if(separator
== NULL
)
1248 separator_len
= strlen (separator
);
1250 va_start(args
, separator
);
1252 s
= va_arg(args
, gchar
*);
1255 len
= strlen(s
) + 1;
1257 while((s
= va_arg(args
, gchar
*)))
1259 len
+= separator_len
+ strlen(s
);
1263 string
= g_new (gchar
, len
);
1265 va_start(args
, separator
);
1268 s
= va_arg(args
, gchar
*);
1271 while((s
= va_arg(args
, gchar
*)))
1273 strcat(string
, separator
);
1278 string
= g_strdup("");