home *** CD-ROM | disk | FTP | other *** search
/ OS/2 Shareware BBS: 6 File / 06-File.zip / mc454src.zip / mc-4.5.4.src / os2emx / src / glib / gstrfuncs.c < prev    next >
C/C++ Source or Header  |  1999-01-04  |  26KB  |  1,276 lines

  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.  
  20. /* 
  21.  * MT safe
  22.  */
  23.  
  24. #ifdef HAVE_CONFIG_H
  25. #include <config.h>
  26. #endif
  27.  
  28. #include <stdarg.h>
  29. #include <stdio.h>
  30. #include <stdlib.h>
  31. #include <string.h>
  32. #include <locale.h>
  33. #include <ctype.h>        /* For tolower() */
  34. #include "glib.h"
  35. /* do not include <unistd.h> in this place since it
  36.  * inteferes with g_strsignal() on some OSes
  37.  */
  38.  
  39. gchar*
  40. g_strdup (const gchar *str)
  41. {
  42.   gchar *new_str;
  43.   
  44.   if (str)
  45.     {
  46.       new_str = g_new (char, strlen (str) + 1);
  47.       strcpy (new_str, str);
  48.     }
  49.   else
  50.     new_str = NULL;
  51.   
  52.   return new_str;
  53. }
  54.  
  55. gpointer
  56. g_memdup (gconstpointer mem,
  57.       guint         byte_size)
  58. {
  59.   gpointer new_mem;
  60.  
  61.   if (mem)
  62.     {
  63.       new_mem = g_malloc (byte_size);
  64.       memcpy (new_mem, mem, byte_size);
  65.     }
  66.   else
  67.     new_mem = NULL;
  68.  
  69.   return new_mem;
  70. }
  71.  
  72. gchar*
  73. g_strndup (const gchar *str,
  74.        guint        n)
  75. {
  76.   gchar *new_str;
  77.  
  78.   if (str)
  79.     {
  80.       new_str = g_new (gchar, n + 1);
  81.       strncpy (new_str, str, n);
  82.       new_str[n] = '\0';
  83.     }
  84.   else
  85.     new_str = NULL;
  86.  
  87.   return new_str;
  88. }
  89.  
  90. gchar*
  91. g_strnfill (guint length,
  92.         gchar fill_char)
  93. {
  94.   register gchar *str, *s, *end;
  95.  
  96.   str = g_new (gchar, length + 1);
  97.   s = str;
  98.   end = str + length;
  99.   while (s < end)
  100.     *(s++) = fill_char;
  101.   *s = 0;
  102.  
  103.   return str;
  104. }
  105.  
  106. gchar*
  107. g_strdup_vprintf (const gchar *format,
  108.           va_list      args1)
  109. {
  110.   gchar *buffer;
  111.   va_list args2;
  112.  
  113.   G_VA_COPY (args2, args1);
  114.  
  115.   buffer = g_new (gchar, g_printf_string_upper_bound (format, args1));
  116.  
  117.   vsprintf (buffer, format, args2);
  118.   va_end (args2);
  119.  
  120.   return buffer;
  121. }
  122.  
  123. gchar*
  124. g_strdup_printf (const gchar *format,
  125.          ...)
  126. {
  127.   gchar *buffer;
  128.   va_list args;
  129.  
  130.   va_start (args, format);
  131.   buffer = g_strdup_vprintf (format, args);
  132.   va_end (args);
  133.  
  134.   return buffer;
  135. }
  136.  
  137. gchar*
  138. g_strconcat (const gchar *string1, ...)
  139. {
  140.   guint      l;
  141.   va_list args;
  142.   gchar      *s;
  143.   gchar      *concat;
  144.   
  145.   g_return_val_if_fail (string1 != NULL, NULL);
  146.   
  147.   l = 1 + strlen (string1);
  148.   va_start (args, string1);
  149.   s = va_arg (args, gchar*);
  150.   while (s)
  151.     {
  152.       l += strlen (s);
  153.       s = va_arg (args, gchar*);
  154.     }
  155.   va_end (args);
  156.   
  157.   concat = g_new (gchar, l);
  158.   concat[0] = 0;
  159.   
  160.   strcat (concat, string1);
  161.   va_start (args, string1);
  162.   s = va_arg (args, gchar*);
  163.   while (s)
  164.     {
  165.       strcat (concat, s);
  166.       s = va_arg (args, gchar*);
  167.     }
  168.   va_end (args);
  169.   
  170.   return concat;
  171. }
  172.  
  173. gdouble
  174. g_strtod (const gchar *nptr,
  175.       gchar **endptr)
  176. {
  177.   gchar *fail_pos_1;
  178.   gchar *fail_pos_2;
  179.   gdouble val_1;
  180.   gdouble val_2 = 0;
  181.   
  182.   g_return_val_if_fail (nptr != NULL, 0);
  183.   
  184.   fail_pos_1 = NULL;
  185.   fail_pos_2 = NULL;
  186.   
  187.   val_1 = strtod (nptr, &fail_pos_1);
  188.   
  189.   if (fail_pos_1 && fail_pos_1[0] != 0)
  190.     {
  191.       gchar *old_locale;
  192.       
  193.       old_locale = setlocale (LC_NUMERIC, "C");
  194.       val_2 = strtod (nptr, &fail_pos_2);
  195.       setlocale (LC_NUMERIC, old_locale);
  196.     }
  197.   
  198.   if (!fail_pos_1 || fail_pos_1[0] == 0 || fail_pos_1 >= fail_pos_2)
  199.     {
  200.       if (endptr)
  201.     *endptr = fail_pos_1;
  202.       return val_1;
  203.     }
  204.   else
  205.     {
  206.       if (endptr)
  207.     *endptr = fail_pos_2;
  208.       return val_2;
  209.     }
  210. }
  211.  
  212. #ifndef OS2EMX
  213.  
  214. gchar*
  215. g_strerror (gint errnum)
  216. {
  217.   static GStaticPrivate msg_private = G_STATIC_PRIVATE_INIT; 
  218.   char *msg;
  219.   
  220. #ifdef HAVE_STRERROR
  221.   return strerror (errnum);
  222. #elif NO_SYS_ERRLIST
  223.   switch (errnum)
  224.     {
  225. #ifdef E2BIG
  226.     case E2BIG: return "argument list too long";
  227. #endif
  228. #ifdef EACCES
  229.     case EACCES: return "permission denied";
  230. #endif
  231. #ifdef EADDRINUSE
  232.     case EADDRINUSE: return "address already in use";
  233. #endif
  234. #ifdef EADDRNOTAVAIL
  235.     case EADDRNOTAVAIL: return "can't assign requested address";
  236. #endif
  237. #ifdef EADV
  238.     case EADV: return "advertise error";
  239. #endif
  240. #ifdef EAFNOSUPPORT
  241.     case EAFNOSUPPORT: return "address family not supported by protocol family";
  242. #endif
  243. #ifdef EAGAIN
  244.     case EAGAIN: return "try again";
  245. #endif
  246. #ifdef EALIGN
  247.     case EALIGN: return "EALIGN";
  248. #endif
  249. #ifdef EALREADY
  250.     case EALREADY: return "operation already in progress";
  251. #endif
  252. #ifdef EBADE
  253.     case EBADE: return "bad exchange descriptor";
  254. #endif
  255. #ifdef EBADF
  256.     case EBADF: return "bad file number";
  257. #endif
  258. #ifdef EBADFD
  259.     case EBADFD: return "file descriptor in bad state";
  260. #endif
  261. #ifdef EBADMSG
  262.     case EBADMSG: return "not a data message";
  263. #endif
  264. #ifdef EBADR
  265.     case EBADR: return "bad request descriptor";
  266. #endif
  267. #ifdef EBADRPC
  268.     case EBADRPC: return "RPC structure is bad";
  269. #endif
  270. #ifdef EBADRQC
  271.     case EBADRQC: return "bad request code";
  272. #endif
  273. #ifdef EBADSLT
  274.     case EBADSLT: return "invalid slot";
  275. #endif
  276. #ifdef EBFONT
  277.     case EBFONT: return "bad font file format";
  278. #endif
  279. #ifdef EBUSY
  280.     case EBUSY: return "mount device busy";
  281. #endif
  282. #ifdef ECHILD
  283.     case ECHILD: return "no children";
  284. #endif
  285. #ifdef ECHRNG
  286.     case ECHRNG: return "channel number out of range";
  287. #endif
  288. #ifdef ECOMM
  289.     case ECOMM: return "communication error on send";
  290. #endif
  291. #ifdef ECONNABORTED
  292.     case ECONNABORTED: return "software caused connection abort";
  293. #endif
  294. #ifdef ECONNREFUSED
  295.     case ECONNREFUSED: return "connection refused";
  296. #endif
  297. #ifdef ECONNRESET
  298.     case ECONNRESET: return "connection reset by peer";
  299. #endif
  300. #if defined(EDEADLK) && (!defined(EWOULDBLOCK) || (EDEADLK != EWOULDBLOCK))
  301.     case EDEADLK: return "resource deadlock avoided";
  302. #endif
  303. #ifdef EDEADLOCK
  304.     case EDEADLOCK: return "resource deadlock avoided";
  305. #endif
  306. #ifdef EDESTADDRREQ
  307.     case EDESTADDRREQ: return "destination address required";
  308. #endif
  309. #ifdef EDIRTY
  310.     case EDIRTY: return "mounting a dirty fs w/o force";
  311. #endif
  312. #ifdef EDOM
  313.     case EDOM: return "math argument out of range";
  314. #endif
  315. #ifdef EDOTDOT
  316.     case EDOTDOT: return "cross mount point";
  317. #endif
  318. #ifdef EDQUOT
  319.     case EDQUOT: return "disk quota exceeded";
  320. #endif
  321. #ifdef EDUPPKG
  322.     case EDUPPKG: return "duplicate package name";
  323. #endif
  324. #ifdef EEXIST
  325.     case EEXIST: return "file already exists";
  326. #endif
  327. #ifdef EFAULT
  328.     case EFAULT: return "bad address in system call argument";
  329. #endif
  330. #ifdef EFBIG
  331.     case EFBIG: return "file too large";
  332. #endif
  333. #ifdef EHOSTDOWN
  334.     case EHOSTDOWN: return "host is down";
  335. #endif
  336. #ifdef EHOSTUNREACH
  337.     case EHOSTUNREACH: return "host is unreachable";
  338. #endif
  339. #ifdef EIDRM
  340.     case EIDRM: return "identifier removed";
  341. #endif
  342. #ifdef EINIT
  343.     case EINIT: return "initialization error";
  344. #endif
  345. #ifdef EINPROGRESS
  346.     case EINPROGRESS: return "operation now in progress";
  347. #endif
  348. #ifdef EINTR
  349.     case EINTR: return "interrupted system call";
  350. #endif
  351. #ifdef EINVAL
  352.     case EINVAL: return "invalid argument";
  353. #endif
  354. #ifdef EIO
  355.     case EIO: return "I/O error";
  356. #endif
  357. #ifdef EISCONN
  358.     case EISCONN: return "socket is already connected";
  359. #endif
  360. #ifdef EISDIR
  361.     case EISDIR: return "illegal operation on a directory";
  362. #endif
  363. #ifdef EISNAME
  364.     case EISNAM: return "is a name file";
  365. #endif
  366. #ifdef ELBIN
  367.     case ELBIN: return "ELBIN";
  368. #endif
  369. #ifdef EL2HLT
  370.     case EL2HLT: return "level 2 halted";
  371. #endif
  372. #ifdef EL2NSYNC
  373.     case EL2NSYNC: return "level 2 not synchronized";
  374. #endif
  375. #ifdef EL3HLT
  376.     case EL3HLT: return "level 3 halted";
  377. #endif
  378. #ifdef EL3RST
  379.     case EL3RST: return "level 3 reset";
  380. #endif
  381. #ifdef ELIBACC
  382.     case ELIBACC: return "can not access a needed shared library";
  383. #endif
  384. #ifdef ELIBBAD
  385.     case ELIBBAD: return "accessing a corrupted shared library";
  386. #endif
  387. #ifdef ELIBEXEC
  388.     case ELIBEXEC: return "can not exec a shared library directly";
  389. #endif
  390. #ifdef ELIBMAX
  391.     case ELIBMAX: return "attempting to link in more shared libraries than system limit";
  392. #endif
  393. #ifdef ELIBSCN
  394.     case ELIBSCN: return ".lib section in a.out corrupted";
  395. #endif
  396. #ifdef ELNRNG
  397.     case ELNRNG: return "link number out of range";
  398. #endif
  399. #ifdef ELOOP
  400.     case ELOOP: return "too many levels of symbolic links";
  401. #endif
  402. #ifdef EMFILE
  403.     case EMFILE: return "too many open files";
  404. #endif
  405. #ifdef EMLINK
  406.     case EMLINK: return "too many links";
  407. #endif
  408. #ifdef EMSGSIZE
  409.     case EMSGSIZE: return "message too long";
  410. #endif
  411. #ifdef EMULTIHOP
  412.     case EMULTIHOP: return "multihop attempted";
  413. #endif
  414. #ifdef ENAMETOOLONG
  415.     case ENAMETOOLONG: return "file name too long";
  416. #endif
  417. #ifdef ENAVAIL
  418.     case ENAVAIL: return "not available";
  419. #endif
  420. #ifdef ENET
  421.     case ENET: return "ENET";
  422. #endif
  423. #ifdef ENETDOWN
  424.     case ENETDOWN: return "network is down";
  425. #endif
  426. #ifdef ENETRESET
  427.     case ENETRESET: return "network dropped connection on reset";
  428. #endif
  429. #ifdef ENETUNREACH
  430.     case ENETUNREACH: return "network is unreachable";
  431. #endif
  432. #ifdef ENFILE
  433.     case ENFILE: return "file table overflow";
  434. #endif
  435. #ifdef ENOANO
  436.     case ENOANO: return "anode table overflow";
  437. #endif
  438. #if defined(ENOBUFS) && (!defined(ENOSR) || (ENOBUFS != ENOSR))
  439.     case ENOBUFS: return "no buffer space available";
  440. #endif
  441. #ifdef ENOCSI
  442.     case ENOCSI: return "no CSI structure available";
  443. #endif
  444. #ifdef ENODATA
  445.     case ENODATA: return "no data available";
  446. #endif
  447. #ifdef ENODEV
  448.     case ENODEV: return "no such device";
  449. #endif
  450. #ifdef ENOENT
  451.     case ENOENT: return "no such file or directory";
  452. #endif
  453. #ifdef ENOEXEC
  454.     case ENOEXEC: return "exec format error";
  455. #endif
  456. #ifdef ENOLCK
  457.     case ENOLCK: return "no locks available";
  458. #endif
  459. #ifdef ENOLINK
  460.     case ENOLINK: return "link has be severed";
  461. #endif
  462. #ifdef ENOMEM
  463.     case ENOMEM: return "not enough memory";
  464. #endif
  465. #ifdef ENOMSG
  466.     case ENOMSG: return "no message of desired type";
  467. #endif
  468. #ifdef ENONET
  469.     case ENONET: return "machine is not on the network";
  470. #endif
  471. #ifdef ENOPKG
  472.     case ENOPKG: return "package not installed";
  473. #endif
  474. #ifdef ENOPROTOOPT
  475.     case ENOPROTOOPT: return "bad proocol option";
  476. #endif
  477. #ifdef ENOSPC
  478.     case ENOSPC: return "no space left on device";
  479. #endif
  480. #ifdef ENOSR
  481.     case ENOSR: return "out of stream resources";
  482. #endif
  483. #ifdef ENOSTR
  484.     case ENOSTR: return "not a stream device";
  485. #endif
  486. #ifdef ENOSYM
  487.     case ENOSYM: return "unresolved symbol name";
  488. #endif
  489. #ifdef ENOSYS
  490.     case ENOSYS: return "function not implemented";
  491. #endif
  492. #ifdef ENOTBLK
  493.     case ENOTBLK: return "block device required";
  494. #endif
  495. #ifdef ENOTCONN
  496.     case ENOTCONN: return "socket is not connected";
  497. #endif
  498. #ifdef ENOTDIR
  499.     case ENOTDIR: return "not a directory";
  500. #endif
  501. #ifdef ENOTEMPTY
  502.     case ENOTEMPTY: return "directory not empty";
  503. #endif
  504. #ifdef ENOTNAM
  505.     case ENOTNAM: return "not a name file";
  506. #endif
  507. #ifdef ENOTSOCK
  508.     case ENOTSOCK: return "socket operation on non-socket";
  509. #endif
  510. #ifdef ENOTTY
  511.     case ENOTTY: return "inappropriate device for ioctl";
  512. #endif
  513. #ifdef ENOTUNIQ
  514.     case ENOTUNIQ: return "name not unique on network";
  515. #endif
  516. #ifdef ENXIO
  517.     case ENXIO: return "no such device or address";
  518. #endif
  519. #ifdef EOPNOTSUPP
  520.     case EOPNOTSUPP: return "operation not supported on socket";
  521. #endif
  522. #ifdef EPERM
  523.     case EPERM: return "not owner";
  524. #endif
  525. #ifdef EPFNOSUPPORT
  526.     case EPFNOSUPPORT: return "protocol family not supported";
  527. #endif
  528. #ifdef EPIPE
  529.     case EPIPE: return "broken pipe";
  530. #endif
  531. #ifdef EPROCLIM
  532.     case EPROCLIM: return "too many processes";
  533. #endif
  534. #ifdef EPROCUNAVAIL
  535.     case EPROCUNAVAIL: return "bad procedure for program";
  536. #endif
  537. #ifdef EPROGMISMATCH
  538.     case EPROGMISMATCH: return "program version wrong";
  539. #endif
  540. #ifdef EPROGUNAVAIL
  541.     case EPROGUNAVAIL: return "RPC program not available";
  542. #endif
  543. #ifdef EPROTO
  544.     case EPROTO: return "protocol error";
  545. #endif
  546. #ifdef EPROTONOSUPPORT
  547.     case EPROTONOSUPPORT: return "protocol not suppored";
  548. #endif
  549. #ifdef EPROTOTYPE
  550.     case EPROTOTYPE: return "protocol wrong type for socket";
  551. #endif
  552. #ifdef ERANGE
  553.     case ERANGE: return "math result unrepresentable";
  554. #endif
  555. #if defined(EREFUSED) && (!defined(ECONNREFUSED) || (EREFUSED != ECONNREFUSED))
  556.     case EREFUSED: return "EREFUSED";
  557. #endif
  558. #ifdef EREMCHG
  559.     case EREMCHG: return "remote address changed";
  560. #endif
  561. #ifdef EREMDEV
  562.     case EREMDEV: return "remote device";
  563. #endif
  564. #ifdef EREMOTE
  565.     case EREMOTE: return "pathname hit remote file system";
  566. #endif
  567. #ifdef EREMOTEIO
  568.     case EREMOTEIO: return "remote i/o error";
  569. #endif
  570. #ifdef EREMOTERELEASE
  571.     case EREMOTERELEASE: return "EREMOTERELEASE";
  572. #endif
  573. #ifdef EROFS
  574.     case EROFS: return "read-only file system";
  575. #endif
  576. #ifdef ERPCMISMATCH
  577.     case ERPCMISMATCH: return "RPC version is wrong";
  578. #endif
  579. #ifdef ERREMOTE
  580.     case ERREMOTE: return "object is remote";
  581. #endif
  582. #ifdef ESHUTDOWN
  583.     case ESHUTDOWN: return "can't send afer socket shutdown";
  584. #endif
  585. #ifdef ESOCKTNOSUPPORT
  586.     case ESOCKTNOSUPPORT: return "socket type not supported";
  587. #endif
  588. #ifdef ESPIPE
  589.     case ESPIPE: return "invalid seek";
  590. #endif
  591. #ifdef ESRCH
  592.     case ESRCH: return "no such process";
  593. #endif
  594. #ifdef ESRMNT
  595.     case ESRMNT: return "srmount error";
  596. #endif
  597. #ifdef ESTALE
  598.     case ESTALE: return "stale remote file handle";
  599. #endif
  600. #ifdef ESUCCESS
  601.     case ESUCCESS: return "Error 0";
  602. #endif
  603. #ifdef ETIME
  604.     case ETIME: return "timer expired";
  605. #endif
  606. #ifdef ETIMEDOUT
  607.     case ETIMEDOUT: return "connection timed out";
  608. #endif
  609. #ifdef ETOOMANYREFS
  610.     case ETOOMANYREFS: return "too many references: can't splice";
  611. #endif
  612. #ifdef ETXTBSY
  613.     case ETXTBSY: return "text file or pseudo-device busy";
  614. #endif
  615. #ifdef EUCLEAN
  616.     case EUCLEAN: return "structure needs cleaning";
  617. #endif
  618. #ifdef EUNATCH
  619.     case EUNATCH: return "protocol driver not attached";
  620. #endif
  621. #ifdef EUSERS
  622.     case EUSERS: return "too many users";
  623. #endif
  624. #ifdef EVERSION
  625.     case EVERSION: return "version mismatch";
  626. #endif
  627. #if defined(EWOULDBLOCK) && (!defined(EAGAIN) || (EWOULDBLOCK != EAGAIN))
  628.     case EWOULDBLOCK: return "operation would block";
  629. #endif
  630. #ifdef EXDEV
  631.     case EXDEV: return "cross-domain link";
  632. #endif
  633. #ifdef EXFULL
  634.     case EXFULL: return "message tables full";
  635. #endif
  636.     }
  637. #else /* NO_SYS_ERRLIST */
  638.   extern int sys_nerr;
  639.   extern char *sys_errlist[];
  640.   
  641.   if ((errnum > 0) && (errnum <= sys_nerr))
  642.     return sys_errlist [errnum];
  643. #endif /* NO_SYS_ERRLIST */
  644.  
  645.   msg = g_static_private_get (&msg_private);
  646.   if( !msg )
  647.     {
  648.       msg = g_new( gchar, 64 );
  649.       g_static_private_set (&msg_private, msg, g_free);
  650.     }
  651.  
  652.   sprintf (msg, "unknown error (%d)", errnum);
  653.   return msg;
  654. }
  655.  
  656.  
  657. gchar*
  658. g_strsignal (gint signum)
  659. {
  660.   static GStaticPrivate msg_private = G_STATIC_PRIVATE_INIT; 
  661.   char *msg;
  662.   
  663. #ifdef HAVE_STRSIGNAL
  664.   extern char *strsignal (int sig);
  665.   return strsignal (signum);
  666. #elif NO_SYS_SIGLIST
  667.   switch (signum)
  668.     {
  669. #ifdef SIGHUP
  670.     case SIGHUP: return "Hangup";
  671. #endif
  672. #ifdef SIGINT
  673.     case SIGINT: return "Interrupt";
  674. #endif
  675. #ifdef SIGQUIT
  676.     case SIGQUIT: return "Quit";
  677. #endif
  678. #ifdef SIGILL
  679.     case SIGILL: return "Illegal instruction";
  680. #endif
  681. #ifdef SIGTRAP
  682.     case SIGTRAP: return "Trace/breakpoint trap";
  683. #endif
  684. #ifdef SIGABRT
  685.     case SIGABRT: return "IOT trap/Abort";
  686. #endif
  687. #ifdef SIGBUS
  688.     case SIGBUS: return "Bus error";
  689. #endif
  690. #ifdef SIGFPE
  691.     case SIGFPE: return "Floating point exception";
  692. #endif
  693. #ifdef SIGKILL
  694.     case SIGKILL: return "Killed";
  695. #endif
  696. #ifdef SIGUSR1
  697.     case SIGUSR1: return "User defined signal 1";
  698. #endif
  699. #ifdef SIGSEGV
  700.     case SIGSEGV: return "Segmentation fault";
  701. #endif
  702. #ifdef SIGUSR2
  703.     case SIGUSR2: return "User defined signal 2";
  704. #endif
  705. #ifdef SIGPIPE
  706.     case SIGPIPE: return "Broken pipe";
  707. #endif
  708. #ifdef SIGALRM
  709.     case SIGALRM: return "Alarm clock";
  710. #endif
  711. #ifdef SIGTERM
  712.     case SIGTERM: return "Terminated";
  713. #endif
  714. #ifdef SIGSTKFLT
  715.     case SIGSTKFLT: return "Stack fault";
  716. #endif
  717. #ifdef SIGCHLD
  718.     case SIGCHLD: return "Child exited";
  719. #endif
  720. #ifdef SIGCONT
  721.     case SIGCONT: return "Continued";
  722. #endif
  723. #ifdef SIGSTOP
  724.     case SIGSTOP: return "Stopped (signal)";
  725. #endif
  726. #ifdef SIGTSTP
  727.     case SIGTSTP: return "Stopped";
  728. #endif
  729. #ifdef SIGTTIN
  730.     case SIGTTIN: return "Stopped (tty input)";
  731. #endif
  732. #ifdef SIGTTOU
  733.     case SIGTTOU: return "Stopped (tty output)";
  734. #endif
  735. #ifdef SIGURG
  736.     case SIGURG: return "Urgent condition";
  737. #endif
  738. #ifdef SIGXCPU
  739.     case SIGXCPU: return "CPU time limit exceeded";
  740. #endif
  741. #ifdef SIGXFSZ
  742.     case SIGXFSZ: return "File size limit exceeded";
  743. #endif
  744. #ifdef SIGVTALRM
  745.     case SIGVTALRM: return "Virtual time alarm";
  746. #endif
  747. #ifdef SIGPROF
  748.     case SIGPROF: return "Profile signal";
  749. #endif
  750. #ifdef SIGWINCH
  751.     case SIGWINCH: return "Window size changed";
  752. #endif
  753. #ifdef SIGIO
  754.     case SIGIO: return "Possible I/O";
  755. #endif
  756. #ifdef SIGPWR
  757.     case SIGPWR: return "Power failure";
  758. #endif
  759. #ifdef SIGUNUSED
  760.     case SIGUNUSED: return "Unused signal";
  761. #endif
  762.     }
  763. #else /* NO_SYS_SIGLIST */
  764.   extern char *sys_siglist[];
  765.   return sys_siglist [signum];
  766. #endif /* NO_SYS_SIGLIST */
  767.  
  768.   msg = g_static_private_get (&msg_private);
  769.   if( !msg )
  770.     {
  771.       msg = g_new( gchar, 64 );
  772.       g_static_private_set (&msg_private, msg, g_free);
  773.     }
  774.   
  775.   sprintf (msg, "unknown signal (%d)", signum);
  776.   return msg;
  777. }
  778.  
  779. #endif
  780.  
  781. guint
  782. g_printf_string_upper_bound (const gchar* format,
  783.                  va_list      args)
  784. {
  785.   guint len = 1;
  786.   
  787.   while (*format)
  788.     {
  789.       gboolean long_int = FALSE;
  790.       gboolean extra_long = FALSE;
  791.       gchar c;
  792.       
  793.       c = *format++;
  794.       
  795.       if (c == '%')
  796.     {
  797.       gboolean done = FALSE;
  798.       
  799.       while (*format && !done)
  800.         {
  801.           switch (*format++)
  802.         {
  803.           gchar *string_arg;
  804.           
  805.         case '*':
  806.           len += va_arg (args, int);
  807.           break;
  808.         case '1':
  809.         case '2':
  810.         case '3':
  811.         case '4':
  812.         case '5':
  813.         case '6':
  814.         case '7':
  815.         case '8':
  816.         case '9':
  817.           /* add specified format length, since it might exceed the
  818.            * size we assume it to have.
  819.            */
  820.           format -= 1;
  821.           len += strtol (format, (char**) &format, 10);
  822.           break;
  823.         case 'h':
  824.           /* ignore short int flag, since all args have at least the
  825.            * same size as an int
  826.            */
  827.           break;
  828.         case 'l':
  829.           if (long_int)
  830.             extra_long = TRUE; /* linux specific */
  831.           else
  832.             long_int = TRUE;
  833.           break;
  834.         case 'q':
  835.         case 'L':
  836.           long_int = TRUE;
  837.           extra_long = TRUE;
  838.           break;
  839.         case 's':
  840.           string_arg = va_arg (args, char *);
  841.           if (string_arg)
  842.             len += strlen (string_arg);
  843.           else
  844.             {
  845.               /* add enough padding to hold "(null)" identifier */
  846.               len += 16;
  847.             }
  848.           done = TRUE;
  849.           break;
  850.         case 'd':
  851.         case 'i':
  852.         case 'o':
  853.         case 'u':
  854.         case 'x':
  855.         case 'X':
  856. #ifdef    G_HAVE_GINT64
  857.           if (extra_long)
  858.             (void) va_arg (args, gint64);
  859.           else
  860. #endif    /* G_HAVE_GINT64 */
  861.             {
  862.               if (long_int)
  863.             (void) va_arg (args, long);
  864.               else
  865.             (void) va_arg (args, int);
  866.             }
  867.           len += extra_long ? 64 : 32;
  868.           done = TRUE;
  869.           break;
  870.         case 'D':
  871.         case 'O':
  872.         case 'U':
  873.           (void) va_arg (args, long);
  874.           len += 32;
  875.           done = TRUE;
  876.           break;
  877.         case 'e':
  878.         case 'E':
  879.         case 'f':
  880.         case 'g':
  881. #ifdef HAVE_LONG_DOUBLE
  882.           if (extra_long)
  883.             (void) va_arg (args, long double);
  884.           else
  885. #endif    /* HAVE_LONG_DOUBLE */
  886.             (void) va_arg (args, double);
  887.           len += extra_long ? 64 : 32;
  888.           done = TRUE;
  889.           break;
  890.         case 'c':
  891.           (void) va_arg (args, int);
  892.           len += 1;
  893.           done = TRUE;
  894.           break;
  895.         case 'p':
  896.         case 'n':
  897.           (void) va_arg (args, void*);
  898.           len += 32;
  899.           done = TRUE;
  900.           break;
  901.         case '%':
  902.           len += 1;
  903.           done = TRUE;
  904.           break;
  905.         default:
  906.           /* ignore unknow/invalid flags */
  907.           break;
  908.         }
  909.         }
  910.     }
  911.       else
  912.     len += 1;
  913.     }
  914.   
  915.   return len;
  916. }
  917.  
  918. void
  919. g_strdown (gchar  *string)
  920. {
  921.   register gchar *s;
  922.   
  923.   g_return_if_fail (string != NULL);
  924.   
  925.   s = string;
  926.   
  927.   while (*s)
  928.     {
  929.       *s = tolower (*s);
  930.       s++;
  931.     }
  932. }
  933.  
  934. void
  935. g_strup (gchar    *string)
  936. {
  937.   register gchar *s;
  938.   
  939.   g_return_if_fail (string != NULL);
  940.   
  941.   s = string;
  942.   
  943.   while (*s)
  944.     {
  945.       *s = toupper (*s);
  946.       s++;
  947.     }
  948. }
  949.  
  950. void
  951. g_strreverse (gchar      *string)
  952. {
  953.   g_return_if_fail (string != NULL);
  954.   
  955.   if (*string)
  956.     {
  957.       register gchar *h, *t;
  958.       
  959.       h = string;
  960.       t = string + strlen (string) - 1;
  961.       
  962.       while (h < t)
  963.     {
  964.       register gchar c;
  965.       
  966.       c = *h;
  967.       *h = *t;
  968.       h++;
  969.       *t = c;
  970.       t--;
  971.     }
  972.     }
  973. }
  974.  
  975. gint
  976. g_strcasecmp (const gchar *s1,
  977.           const gchar *s2)
  978. {
  979. #ifdef HAVE_STRCASECMP
  980.   return strcasecmp (s1, s2);
  981. #else
  982.   gint c1, c2;
  983.   
  984.   g_return_val_if_fail (s1 != NULL, 0);
  985.   g_return_val_if_fail (s2 != NULL, 0);
  986.  
  987.   while (*s1 && *s2)
  988.     {
  989.       /* According to A. Cox, some platforms have islower's that
  990.        * don't work right on non-uppercase
  991.        */
  992.       c1 = isupper ((guchar)*s1) ? tolower ((guchar)*s1) : *s1;
  993.       c2 = isupper ((guchar)*s2) ? tolower ((guchar)*s2) : *s2;
  994.       if (c1 != c2)
  995.     return (c1 - c2);
  996.       s1++; s2++;
  997.     }
  998.   
  999.   return (((gint)(guchar) *s1) - ((gint)(guchar) *s2));
  1000. #endif
  1001. }
  1002.  
  1003. gint
  1004. g_strncasecmp (const gchar *s1,
  1005.            const gchar *s2,
  1006.            guint n)
  1007. {
  1008. #ifdef HAVE_STRNCASECMP
  1009.   return strncasecmp (s1, s2, n);
  1010. #else
  1011.   gint c1, c2;
  1012.   
  1013.   g_return_val_if_fail (s1 != NULL, 0);
  1014.   g_return_val_if_fail (s2 != NULL, 0);
  1015.  
  1016.   while (n-- && *s1 && *s2)
  1017.     {
  1018.       /* According to A. Cox, some platforms have islower's that
  1019.        * don't work right on non-uppercase
  1020.        */
  1021.       c1 = isupper ((guchar)*s1) ? tolower ((guchar)*s1) : *s1;
  1022.       c2 = isupper ((guchar)*s2) ? tolower ((guchar)*s2) : *s2;
  1023.       if (c1 != c2)
  1024.     return (c1 - c2);
  1025.       s1++; s2++;
  1026.     }
  1027.  
  1028.   if (n)
  1029.     return (((gint)(guchar) *s1) - ((gint)(guchar) *s2));
  1030.   else
  1031.     return 0;
  1032. #endif
  1033. }
  1034.  
  1035. gchar*
  1036. g_strdelimit (gchar      *string,
  1037.           const gchar *delimiters,
  1038.           gchar       new_delim)
  1039. {
  1040.   register gchar *c;
  1041.   
  1042.   g_return_val_if_fail (string != NULL, NULL);
  1043.   
  1044.   if (!delimiters)
  1045.     delimiters = G_STR_DELIMITERS;
  1046.   
  1047.   for (c = string; *c; c++)
  1048.     {
  1049.       if (strchr (delimiters, *c))
  1050.     *c = new_delim;
  1051.     }
  1052.  
  1053.   return string;
  1054. }
  1055.  
  1056. gchar*
  1057. g_strescape (gchar *string)
  1058. {
  1059.   gchar *q;
  1060.   gchar *escaped;
  1061.   guint backslashes = 0;
  1062.   gchar *p = string;
  1063.  
  1064.   g_return_val_if_fail (string != NULL, NULL);
  1065.  
  1066.   while (*p != '\000')
  1067.     backslashes += (*p++ == '\\');
  1068.  
  1069.   if (!backslashes)
  1070.     return g_strdup (string);
  1071.  
  1072.   escaped = g_new (gchar, strlen (string) + backslashes + 1);
  1073.  
  1074.   p = string;
  1075.   q = escaped;
  1076.  
  1077.   while (*p != '\000')
  1078.     {
  1079.       if (*p == '\\')
  1080.     *q++ = '\\';
  1081.       *q++ = *p++;
  1082.     }
  1083.   *q = '\000';
  1084.  
  1085.   return escaped;
  1086. }
  1087.  
  1088. /* blame Elliot for these next five routines */
  1089. gchar*
  1090. g_strchug (gchar *string)
  1091. {
  1092.   gchar *start;
  1093.  
  1094.   g_return_val_if_fail (string != NULL, NULL);
  1095.  
  1096.   for (start = string; *start && isspace (*start); start++)
  1097.     ;
  1098.  
  1099.   strcpy (string, start);
  1100.  
  1101.   return string;
  1102. }
  1103.  
  1104. gchar*
  1105. g_strchomp (gchar *string)
  1106. {
  1107.   gchar *s;
  1108.  
  1109.   g_return_val_if_fail (string != NULL, NULL);
  1110.  
  1111.   if (!*string)
  1112.     return string;
  1113.  
  1114.   for (s = string + strlen (string) - 1; s >= string && isspace (*s); s--)
  1115.     *s = '\0';
  1116.  
  1117.   return string;
  1118. }
  1119.  
  1120. #ifndef OS2EMX
  1121.  
  1122. gchar**
  1123. g_strsplit (const gchar *string,
  1124.         const gchar *delimiter,
  1125.         gint         max_tokens)
  1126. {
  1127.   GSList *string_list = NULL, *slist;
  1128.   gchar **str_array, *s;
  1129.   guint i, n = 1;
  1130.  
  1131.   g_return_val_if_fail (string != NULL, NULL);
  1132.   g_return_val_if_fail (delimiter != NULL, NULL);
  1133.  
  1134.   if (max_tokens < 1)
  1135.     max_tokens = G_MAXINT;
  1136.  
  1137.   s = strstr (string, delimiter);
  1138.   if (s)
  1139.     {
  1140.       guint delimiter_len = strlen (delimiter);
  1141.       
  1142.       do
  1143.     {
  1144.       guint len;
  1145.       gchar *new_string;
  1146.       
  1147.       len = s - string;
  1148.       new_string = g_new (gchar, len + 1);
  1149.       strncpy (new_string, string, len);
  1150.       new_string[len] = 0;
  1151.       string_list = g_slist_prepend (string_list, new_string);
  1152.       n++;
  1153.       string = s + delimiter_len;
  1154.       s = strstr (string, delimiter);
  1155.     }
  1156.       while (--max_tokens && s);
  1157.     }
  1158.   if (*string)
  1159.     {
  1160.       n++;
  1161.       string_list = g_slist_prepend (string_list, g_strdup (string));
  1162.     }
  1163.   
  1164.   str_array = g_new (gchar*, n);
  1165.  
  1166.   i = n - 1;
  1167.  
  1168.   str_array[i--] = NULL;
  1169.   for (slist = string_list; slist; slist = slist->next)
  1170.     str_array[i--] = slist->data;
  1171.  
  1172.   g_slist_free (string_list);
  1173.  
  1174.   return str_array;
  1175. }
  1176. #endif
  1177.  
  1178. void
  1179. g_strfreev (gchar **str_array)
  1180. {
  1181.   if (str_array)
  1182.     {
  1183.       int i;
  1184.  
  1185.       for(i = 0; str_array[i] != NULL; i++)
  1186.     g_free(str_array[i]);
  1187.  
  1188.       g_free (str_array);
  1189.     }
  1190. }
  1191.  
  1192. gchar*
  1193. g_strjoinv (const gchar  *separator,
  1194.         gchar       **str_array)
  1195. {
  1196.   gchar *string;
  1197.   
  1198.   g_return_val_if_fail (str_array != NULL, NULL);
  1199.  
  1200.   if(separator == NULL)
  1201.     separator = "";
  1202.  
  1203.   if (*str_array)
  1204.     {
  1205.       guint i, len;
  1206.       guint separator_len;
  1207.  
  1208.       separator_len = strlen (separator);
  1209.       len = 1 + strlen (str_array[0]);
  1210.       for(i = 1; str_array[i] != NULL; i++)
  1211.     len += separator_len + strlen(str_array[i]);
  1212.       
  1213.       string = g_new (gchar, len);
  1214.       *string = 0;
  1215.       strcat (string, *str_array);
  1216.       for (i = 1; str_array[i] != NULL; i++)
  1217.     {
  1218.       strcat (string, separator);
  1219.       strcat (string, str_array[i]);
  1220.     }
  1221.       }
  1222.   else
  1223.     string = g_strdup ("");
  1224.  
  1225.   return string;
  1226. }
  1227.  
  1228. gchar*
  1229. g_strjoin (const gchar  *separator,
  1230.        ...)
  1231. {
  1232.   gchar *string, *s;
  1233.   va_list args;
  1234.   guint len;
  1235.   guint separator_len;
  1236.  
  1237.   if(separator == NULL)
  1238.     separator = "";
  1239.  
  1240.   separator_len = strlen (separator);
  1241.  
  1242.   va_start(args, separator);
  1243.  
  1244.   s = va_arg(args, gchar *);
  1245.  
  1246.   if(s) {
  1247.     len = strlen(s) + 1;
  1248.  
  1249.     while((s = va_arg(args, gchar*)))
  1250.       {
  1251.     len += separator_len + strlen(s);
  1252.       }
  1253.     va_end(args);
  1254.  
  1255.     string = g_new (gchar, len);
  1256.  
  1257.     va_start(args, separator);
  1258.  
  1259.     *string = 0;
  1260.     s = va_arg(args, gchar*);
  1261.     strcat (string, s);
  1262.  
  1263.     while((s = va_arg(args, gchar*)))
  1264.       {
  1265.     strcat(string, separator);
  1266.     strcat(string, s);
  1267.       }
  1268.  
  1269.   } else
  1270.     string = g_strdup("");
  1271.  
  1272.   va_end(args);
  1273.  
  1274.   return string;
  1275. }
  1276.