187e5d32f5ab1f79522e9c1a2889a435145ce4c7
[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 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.
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  * Library General Public License for more details.
13  *
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.
18  */
19 #include <stdarg.h>
20 #include <stdio.h>
21 #include <stdlib.h>
22 #include <string.h>
23 #include <locale.h>
24 #include <ctype.h>              /* For tolower() */
25 #include "glib.h"
26 /* do not include <unistd.h> in this place since it
27  * inteferes with g_strsignal() on some OSes
28  */
29
30 gchar*
31 g_strdup (const gchar *str)
32 {
33   gchar *new_str;
34   
35   new_str = NULL;
36   if (str)
37     {
38       new_str = g_new (char, strlen (str) + 1);
39       strcpy (new_str, str);
40     }
41   
42   return new_str;
43 }
44
45 gchar*
46 g_strndup (const gchar *str, gulong n)
47 {
48   char *new_str;
49
50   new_str = NULL;
51   if (str)
52     {
53       new_str = g_new (char, n + 1);
54       strncpy (new_str, str, n);
55       new_str[n] = '\0';
56     }
57
58   return new_str;
59 }
60
61 gchar*
62 g_strconcat (const gchar *string1, ...)
63 {
64   guint   l;
65   va_list args;
66   gchar   *s;
67   gchar   *concat;
68   
69   g_return_val_if_fail (string1 != NULL, NULL);
70   
71   l = 1 + strlen (string1);
72   va_start (args, string1);
73   s = va_arg (args, gchar*);
74   while (s)
75     {
76       l += strlen (s);
77       s = va_arg (args, gchar*);
78     }
79   va_end (args);
80   
81   concat = g_new (gchar, l);
82   concat[0] = 0;
83   
84   strcat (concat, string1);
85   va_start (args, string1);
86   s = va_arg (args, gchar*);
87   while (s)
88     {
89       strcat (concat, s);
90       s = va_arg (args, gchar*);
91     }
92   va_end (args);
93   
94   return concat;
95 }
96
97 gdouble
98 g_strtod (const gchar *nptr,
99           gchar **endptr)
100 {
101   gchar *fail_pos_1;
102   gchar *fail_pos_2;
103   gdouble val_1;
104   gdouble val_2 = 0;
105   
106   g_return_val_if_fail (nptr != NULL, 0);
107   
108   fail_pos_1 = NULL;
109   fail_pos_2 = NULL;
110   
111   val_1 = strtod (nptr, &fail_pos_1);
112   
113   if (fail_pos_1 && fail_pos_1[0] != 0)
114     {
115       gchar *old_locale;
116       
117       old_locale = setlocale (LC_NUMERIC, "C");
118       val_2 = strtod (nptr, &fail_pos_2);
119       setlocale (LC_NUMERIC, old_locale);
120     }
121   
122   if (!fail_pos_1 || fail_pos_1[0] == 0 || fail_pos_1 >= fail_pos_2)
123     {
124       if (endptr)
125         *endptr = fail_pos_1;
126       return val_1;
127     }
128   else
129     {
130       if (endptr)
131         *endptr = fail_pos_2;
132       return val_2;
133     }
134 }
135
136 gchar*
137 g_strerror (gint errnum)
138 {
139   static char msg[64];
140   
141 #ifdef HAVE_STRERROR
142   return strerror (errnum);
143 #elif NO_SYS_ERRLIST
144   switch (errnum)
145     {
146 #ifdef E2BIG
147     case E2BIG: return "argument list too long";
148 #endif
149 #ifdef EACCES
150     case EACCES: return "permission denied";
151 #endif
152 #ifdef EADDRINUSE
153     case EADDRINUSE: return "address already in use";
154 #endif
155 #ifdef EADDRNOTAVAIL
156     case EADDRNOTAVAIL: return "can't assign requested address";
157 #endif
158 #ifdef EADV
159     case EADV: return "advertise error";
160 #endif
161 #ifdef EAFNOSUPPORT
162     case EAFNOSUPPORT: return "address family not supported by protocol family";
163 #endif
164 #ifdef EAGAIN
165     case EAGAIN: return "try again";
166 #endif
167 #ifdef EALIGN
168     case EALIGN: return "EALIGN";
169 #endif
170 #ifdef EALREADY
171     case EALREADY: return "operation already in progress";
172 #endif
173 #ifdef EBADE
174     case EBADE: return "bad exchange descriptor";
175 #endif
176 #ifdef EBADF
177     case EBADF: return "bad file number";
178 #endif
179 #ifdef EBADFD
180     case EBADFD: return "file descriptor in bad state";
181 #endif
182 #ifdef EBADMSG
183     case EBADMSG: return "not a data message";
184 #endif
185 #ifdef EBADR
186     case EBADR: return "bad request descriptor";
187 #endif
188 #ifdef EBADRPC
189     case EBADRPC: return "RPC structure is bad";
190 #endif
191 #ifdef EBADRQC
192     case EBADRQC: return "bad request code";
193 #endif
194 #ifdef EBADSLT
195     case EBADSLT: return "invalid slot";
196 #endif
197 #ifdef EBFONT
198     case EBFONT: return "bad font file format";
199 #endif
200 #ifdef EBUSY
201     case EBUSY: return "mount device busy";
202 #endif
203 #ifdef ECHILD
204     case ECHILD: return "no children";
205 #endif
206 #ifdef ECHRNG
207     case ECHRNG: return "channel number out of range";
208 #endif
209 #ifdef ECOMM
210     case ECOMM: return "communication error on send";
211 #endif
212 #ifdef ECONNABORTED
213     case ECONNABORTED: return "software caused connection abort";
214 #endif
215 #ifdef ECONNREFUSED
216     case ECONNREFUSED: return "connection refused";
217 #endif
218 #ifdef ECONNRESET
219     case ECONNRESET: return "connection reset by peer";
220 #endif
221 #if defined(EDEADLK) && (!defined(EWOULDBLOCK) || (EDEADLK != EWOULDBLOCK))
222     case EDEADLK: return "resource deadlock avoided";
223 #endif
224 #ifdef EDEADLOCK
225     case EDEADLOCK: return "resource deadlock avoided";
226 #endif
227 #ifdef EDESTADDRREQ
228     case EDESTADDRREQ: return "destination address required";
229 #endif
230 #ifdef EDIRTY
231     case EDIRTY: return "mounting a dirty fs w/o force";
232 #endif
233 #ifdef EDOM
234     case EDOM: return "math argument out of range";
235 #endif
236 #ifdef EDOTDOT
237     case EDOTDOT: return "cross mount point";
238 #endif
239 #ifdef EDQUOT
240     case EDQUOT: return "disk quota exceeded";
241 #endif
242 #ifdef EDUPPKG
243     case EDUPPKG: return "duplicate package name";
244 #endif
245 #ifdef EEXIST
246     case EEXIST: return "file already exists";
247 #endif
248 #ifdef EFAULT
249     case EFAULT: return "bad address in system call argument";
250 #endif
251 #ifdef EFBIG
252     case EFBIG: return "file too large";
253 #endif
254 #ifdef EHOSTDOWN
255     case EHOSTDOWN: return "host is down";
256 #endif
257 #ifdef EHOSTUNREACH
258     case EHOSTUNREACH: return "host is unreachable";
259 #endif
260 #ifdef EIDRM
261     case EIDRM: return "identifier removed";
262 #endif
263 #ifdef EINIT
264     case EINIT: return "initialization error";
265 #endif
266 #ifdef EINPROGRESS
267     case EINPROGRESS: return "operation now in progress";
268 #endif
269 #ifdef EINTR
270     case EINTR: return "interrupted system call";
271 #endif
272 #ifdef EINVAL
273     case EINVAL: return "invalid argument";
274 #endif
275 #ifdef EIO
276     case EIO: return "I/O error";
277 #endif
278 #ifdef EISCONN
279     case EISCONN: return "socket is already connected";
280 #endif
281 #ifdef EISDIR
282     case EISDIR: return "illegal operation on a directory";
283 #endif
284 #ifdef EISNAME
285     case EISNAM: return "is a name file";
286 #endif
287 #ifdef ELBIN
288     case ELBIN: return "ELBIN";
289 #endif
290 #ifdef EL2HLT
291     case EL2HLT: return "level 2 halted";
292 #endif
293 #ifdef EL2NSYNC
294     case EL2NSYNC: return "level 2 not synchronized";
295 #endif
296 #ifdef EL3HLT
297     case EL3HLT: return "level 3 halted";
298 #endif
299 #ifdef EL3RST
300     case EL3RST: return "level 3 reset";
301 #endif
302 #ifdef ELIBACC
303     case ELIBACC: return "can not access a needed shared library";
304 #endif
305 #ifdef ELIBBAD
306     case ELIBBAD: return "accessing a corrupted shared library";
307 #endif
308 #ifdef ELIBEXEC
309     case ELIBEXEC: return "can not exec a shared library directly";
310 #endif
311 #ifdef ELIBMAX
312     case ELIBMAX: return "attempting to link in more shared libraries than system limit";
313 #endif
314 #ifdef ELIBSCN
315     case ELIBSCN: return ".lib section in a.out corrupted";
316 #endif
317 #ifdef ELNRNG
318     case ELNRNG: return "link number out of range";
319 #endif
320 #ifdef ELOOP
321     case ELOOP: return "too many levels of symbolic links";
322 #endif
323 #ifdef EMFILE
324     case EMFILE: return "too many open files";
325 #endif
326 #ifdef EMLINK
327     case EMLINK: return "too many links";
328 #endif
329 #ifdef EMSGSIZE
330     case EMSGSIZE: return "message too long";
331 #endif
332 #ifdef EMULTIHOP
333     case EMULTIHOP: return "multihop attempted";
334 #endif
335 #ifdef ENAMETOOLONG
336     case ENAMETOOLONG: return "file name too long";
337 #endif
338 #ifdef ENAVAIL
339     case ENAVAIL: return "not available";
340 #endif
341 #ifdef ENET
342     case ENET: return "ENET";
343 #endif
344 #ifdef ENETDOWN
345     case ENETDOWN: return "network is down";
346 #endif
347 #ifdef ENETRESET
348     case ENETRESET: return "network dropped connection on reset";
349 #endif
350 #ifdef ENETUNREACH
351     case ENETUNREACH: return "network is unreachable";
352 #endif
353 #ifdef ENFILE
354     case ENFILE: return "file table overflow";
355 #endif
356 #ifdef ENOANO
357     case ENOANO: return "anode table overflow";
358 #endif
359 #if defined(ENOBUFS) && (!defined(ENOSR) || (ENOBUFS != ENOSR))
360     case ENOBUFS: return "no buffer space available";
361 #endif
362 #ifdef ENOCSI
363     case ENOCSI: return "no CSI structure available";
364 #endif
365 #ifdef ENODATA
366     case ENODATA: return "no data available";
367 #endif
368 #ifdef ENODEV
369     case ENODEV: return "no such device";
370 #endif
371 #ifdef ENOENT
372     case ENOENT: return "no such file or directory";
373 #endif
374 #ifdef ENOEXEC
375     case ENOEXEC: return "exec format error";
376 #endif
377 #ifdef ENOLCK
378     case ENOLCK: return "no locks available";
379 #endif
380 #ifdef ENOLINK
381     case ENOLINK: return "link has be severed";
382 #endif
383 #ifdef ENOMEM
384     case ENOMEM: return "not enough memory";
385 #endif
386 #ifdef ENOMSG
387     case ENOMSG: return "no message of desired type";
388 #endif
389 #ifdef ENONET
390     case ENONET: return "machine is not on the network";
391 #endif
392 #ifdef ENOPKG
393     case ENOPKG: return "package not installed";
394 #endif
395 #ifdef ENOPROTOOPT
396     case ENOPROTOOPT: return "bad proocol option";
397 #endif
398 #ifdef ENOSPC
399     case ENOSPC: return "no space left on device";
400 #endif
401 #ifdef ENOSR
402     case ENOSR: return "out of stream resources";
403 #endif
404 #ifdef ENOSTR
405     case ENOSTR: return "not a stream device";
406 #endif
407 #ifdef ENOSYM
408     case ENOSYM: return "unresolved symbol name";
409 #endif
410 #ifdef ENOSYS
411     case ENOSYS: return "function not implemented";
412 #endif
413 #ifdef ENOTBLK
414     case ENOTBLK: return "block device required";
415 #endif
416 #ifdef ENOTCONN
417     case ENOTCONN: return "socket is not connected";
418 #endif
419 #ifdef ENOTDIR
420     case ENOTDIR: return "not a directory";
421 #endif
422 #ifdef ENOTEMPTY
423     case ENOTEMPTY: return "directory not empty";
424 #endif
425 #ifdef ENOTNAM
426     case ENOTNAM: return "not a name file";
427 #endif
428 #ifdef ENOTSOCK
429     case ENOTSOCK: return "socket operation on non-socket";
430 #endif
431 #ifdef ENOTTY
432     case ENOTTY: return "inappropriate device for ioctl";
433 #endif
434 #ifdef ENOTUNIQ
435     case ENOTUNIQ: return "name not unique on network";
436 #endif
437 #ifdef ENXIO
438     case ENXIO: return "no such device or address";
439 #endif
440 #ifdef EOPNOTSUPP
441     case EOPNOTSUPP: return "operation not supported on socket";
442 #endif
443 #ifdef EPERM
444     case EPERM: return "not owner";
445 #endif
446 #ifdef EPFNOSUPPORT
447     case EPFNOSUPPORT: return "protocol family not supported";
448 #endif
449 #ifdef EPIPE
450     case EPIPE: return "broken pipe";
451 #endif
452 #ifdef EPROCLIM
453     case EPROCLIM: return "too many processes";
454 #endif
455 #ifdef EPROCUNAVAIL
456     case EPROCUNAVAIL: return "bad procedure for program";
457 #endif
458 #ifdef EPROGMISMATCH
459     case EPROGMISMATCH: return "program version wrong";
460 #endif
461 #ifdef EPROGUNAVAIL
462     case EPROGUNAVAIL: return "RPC program not available";
463 #endif
464 #ifdef EPROTO
465     case EPROTO: return "protocol error";
466 #endif
467 #ifdef EPROTONOSUPPORT
468     case EPROTONOSUPPORT: return "protocol not suppored";
469 #endif
470 #ifdef EPROTOTYPE
471     case EPROTOTYPE: return "protocol wrong type for socket";
472 #endif
473 #ifdef ERANGE
474     case ERANGE: return "math result unrepresentable";
475 #endif
476 #if defined(EREFUSED) && (!defined(ECONNREFUSED) || (EREFUSED != ECONNREFUSED))
477     case EREFUSED: return "EREFUSED";
478 #endif
479 #ifdef EREMCHG
480     case EREMCHG: return "remote address changed";
481 #endif
482 #ifdef EREMDEV
483     case EREMDEV: return "remote device";
484 #endif
485 #ifdef EREMOTE
486     case EREMOTE: return "pathname hit remote file system";
487 #endif
488 #ifdef EREMOTEIO
489     case EREMOTEIO: return "remote i/o error";
490 #endif
491 #ifdef EREMOTERELEASE
492     case EREMOTERELEASE: return "EREMOTERELEASE";
493 #endif
494 #ifdef EROFS
495     case EROFS: return "read-only file system";
496 #endif
497 #ifdef ERPCMISMATCH
498     case ERPCMISMATCH: return "RPC version is wrong";
499 #endif
500 #ifdef ERREMOTE
501     case ERREMOTE: return "object is remote";
502 #endif
503 #ifdef ESHUTDOWN
504     case ESHUTDOWN: return "can't send afer socket shutdown";
505 #endif
506 #ifdef ESOCKTNOSUPPORT
507     case ESOCKTNOSUPPORT: return "socket type not supported";
508 #endif
509 #ifdef ESPIPE
510     case ESPIPE: return "invalid seek";
511 #endif
512 #ifdef ESRCH
513     case ESRCH: return "no such process";
514 #endif
515 #ifdef ESRMNT
516     case ESRMNT: return "srmount error";
517 #endif
518 #ifdef ESTALE
519     case ESTALE: return "stale remote file handle";
520 #endif
521 #ifdef ESUCCESS
522     case ESUCCESS: return "Error 0";
523 #endif
524 #ifdef ETIME
525     case ETIME: return "timer expired";
526 #endif
527 #ifdef ETIMEDOUT
528     case ETIMEDOUT: return "connection timed out";
529 #endif
530 #ifdef ETOOMANYREFS
531     case ETOOMANYREFS: return "too many references: can't splice";
532 #endif
533 #ifdef ETXTBSY
534     case ETXTBSY: return "text file or pseudo-device busy";
535 #endif
536 #ifdef EUCLEAN
537     case EUCLEAN: return "structure needs cleaning";
538 #endif
539 #ifdef EUNATCH
540     case EUNATCH: return "protocol driver not attached";
541 #endif
542 #ifdef EUSERS
543     case EUSERS: return "too many users";
544 #endif
545 #ifdef EVERSION
546     case EVERSION: return "version mismatch";
547 #endif
548 #if defined(EWOULDBLOCK) && (!defined(EAGAIN) || (EWOULDBLOCK != EAGAIN))
549     case EWOULDBLOCK: return "operation would block";
550 #endif
551 #ifdef EXDEV
552     case EXDEV: return "cross-domain link";
553 #endif
554 #ifdef EXFULL
555     case EXFULL: return "message tables full";
556 #endif
557     }
558 #else /* NO_SYS_ERRLIST */
559   extern int sys_nerr;
560   extern char *sys_errlist[];
561   
562   if ((errnum > 0) && (errnum <= sys_nerr))
563     return sys_errlist [errnum];
564 #endif /* NO_SYS_ERRLIST */
565   
566   sprintf (msg, "unknown error (%d)", errnum);
567   return msg;
568 }
569
570 gchar*
571 g_strsignal (gint signum)
572 {
573   static char msg[64];
574   
575 #ifdef HAVE_STRSIGNAL
576   extern char *strsignal (int sig);
577   return strsignal (signum);
578 #elif NO_SYS_SIGLIST
579   switch (signum)
580     {
581 #ifdef SIGHUP
582     case SIGHUP: return "Hangup";
583 #endif
584 #ifdef SIGINT
585     case SIGINT: return "Interrupt";
586 #endif
587 #ifdef SIGQUIT
588     case SIGQUIT: return "Quit";
589 #endif
590 #ifdef SIGILL
591     case SIGILL: return "Illegal instruction";
592 #endif
593 #ifdef SIGTRAP
594     case SIGTRAP: return "Trace/breakpoint trap";
595 #endif
596 #ifdef SIGABRT
597     case SIGABRT: return "IOT trap/Abort";
598 #endif
599 #ifdef SIGBUS
600     case SIGBUS: return "Bus error";
601 #endif
602 #ifdef SIGFPE
603     case SIGFPE: return "Floating point exception";
604 #endif
605 #ifdef SIGKILL
606     case SIGKILL: return "Killed";
607 #endif
608 #ifdef SIGUSR1
609     case SIGUSR1: return "User defined signal 1";
610 #endif
611 #ifdef SIGSEGV
612     case SIGSEGV: return "Segmentation fault";
613 #endif
614 #ifdef SIGUSR2
615     case SIGUSR2: return "User defined signal 2";
616 #endif
617 #ifdef SIGPIPE
618     case SIGPIPE: return "Broken pipe";
619 #endif
620 #ifdef SIGALRM
621     case SIGALRM: return "Alarm clock";
622 #endif
623 #ifdef SIGTERM
624     case SIGTERM: return "Terminated";
625 #endif
626 #ifdef SIGSTKFLT
627     case SIGSTKFLT: return "Stack fault";
628 #endif
629 #ifdef SIGCHLD
630     case SIGCHLD: return "Child exited";
631 #endif
632 #ifdef SIGCONT
633     case SIGCONT: return "Continued";
634 #endif
635 #ifdef SIGSTOP
636     case SIGSTOP: return "Stopped (signal)";
637 #endif
638 #ifdef SIGTSTP
639     case SIGTSTP: return "Stopped";
640 #endif
641 #ifdef SIGTTIN
642     case SIGTTIN: return "Stopped (tty input)";
643 #endif
644 #ifdef SIGTTOU
645     case SIGTTOU: return "Stopped (tty output)";
646 #endif
647 #ifdef SIGURG
648     case SIGURG: return "Urgent condition";
649 #endif
650 #ifdef SIGXCPU
651     case SIGXCPU: return "CPU time limit exceeded";
652 #endif
653 #ifdef SIGXFSZ
654     case SIGXFSZ: return "File size limit exceeded";
655 #endif
656 #ifdef SIGVTALRM
657     case SIGVTALRM: return "Virtual time alarm";
658 #endif
659 #ifdef SIGPROF
660     case SIGPROF: return "Profile signal";
661 #endif
662 #ifdef SIGWINCH
663     case SIGWINCH: return "Window size changed";
664 #endif
665 #ifdef SIGIO
666     case SIGIO: return "Possible I/O";
667 #endif
668 #ifdef SIGPWR
669     case SIGPWR: return "Power failure";
670 #endif
671 #ifdef SIGUNUSED
672     case SIGUNUSED: return "Unused signal";
673 #endif
674     }
675 #else /* NO_SYS_SIGLIST */
676   extern char *sys_siglist[];
677   return sys_siglist [signum];
678 #endif /* NO_SYS_SIGLIST */
679   
680   sprintf (msg, "unknown signal (%d)", signum);
681   return msg;
682 }
683
684 void
685 g_strdown (gchar  *string)
686 {
687   register gchar *s;
688   
689   g_return_if_fail (string != NULL);
690   
691   s = string;
692   
693   while (*s)
694     {
695       *s = tolower (*s);
696       s++;
697     }
698 }
699
700 void
701 g_strup (gchar  *string)
702 {
703   register gchar *s;
704   
705   g_return_if_fail (string != NULL);
706   
707   s = string;
708   
709   while (*s)
710     {
711       *s = toupper (*s);
712       s++;
713     }
714 }
715
716 void
717 g_strreverse (gchar       *string)
718 {
719   g_return_if_fail (string != NULL);
720   
721   if (*string)
722     {
723       register gchar *h, *t;
724       
725       h = string;
726       t = string + strlen (string) - 1;
727       
728       while (h < t)
729         {
730           register gchar c;
731           
732           c = *h;
733           *h = *t;
734           h++;
735           *t = c;
736           t--;
737         }
738     }
739 }
740
741 gint
742 g_strcasecmp (const gchar *s1,
743               const gchar *s2)
744 {
745 #ifdef HAVE_STRCASECMP
746   return strcasecmp (s1, s2);
747 #else
748   gint c1, c2;
749   
750   while (*s1 && *s2)
751     {
752       /* According to A. Cox, some platforms have islower's that
753        * don't work right on non-uppercase
754        */
755       c1 = isupper ((guchar)*s1) ? tolower ((guchar)*s1) : *s1;
756       c2 = isupper ((guchar)*s2) ? tolower ((guchar)*s2) : *s2;
757       if (c1 != c2)
758         return (c1 - c2);
759       s1++; s2++;
760     }
761   
762   return (((gint)(guchar) *s1) - ((gint)(guchar) *s2));
763 #endif
764 }
765
766 void
767 g_strdelimit (gchar       *string,
768               const gchar *delimiters,
769               gchar        new_delim)
770 {
771   register gchar *c;
772   
773   g_return_if_fail (string != NULL);
774   
775   if (!delimiters)
776     delimiters = G_STR_DELIMITERS;
777   
778   for (c = string; *c; c++)
779     {
780       if (strchr (delimiters, *c))
781         *c = new_delim;
782     }
783 }