Busybox: Upgrade to 1.21.1 (stable). lsof active.
[tomato.git] / release / src / router / glib / gutils.c
bloba93ed95081f4aa5b935613dc90ea6253f034715d
1 /* GLIB - Library of useful routines for C programming
2 * Copyright (C) 1995-1998 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/.
27 /*
28 * MT safe for the unix part, FIXME: make the win32 part MT safe as well.
31 #ifdef HAVE_CONFIG_H
32 #include <config.h>
33 #endif
35 #include "glibconfig.h"
37 #ifdef HAVE_UNISTD_H
38 #include <unistd.h>
39 #endif
40 #include <stdarg.h>
41 #include <stdlib.h>
42 #include <stdio.h>
43 #include <string.h>
44 #include <errno.h>
45 #ifdef HAVE_PWD_H
46 #include <pwd.h>
47 #endif
48 #include <sys/types.h>
49 #ifdef HAVE_SYS_PARAM_H
50 #include <sys/param.h>
51 #endif
53 #ifdef NATIVE_WIN32
54 # define STRICT /* Strict typing, please */
55 # include <windows.h>
56 # include <direct.h>
57 # include <errno.h>
58 # include <ctype.h>
59 # ifdef _MSC_VER
60 # include <io.h>
61 # endif /* _MSC_VER */
62 #endif /* NATIVE_WIN32 */
64 /* implement Glib's inline functions
66 #define G_INLINE_FUNC extern
67 #define G_CAN_INLINE 1
68 #include "glib.h"
70 #ifdef MAXPATHLEN
71 #define G_PATH_LENGTH (MAXPATHLEN + 1)
72 #elif defined (PATH_MAX)
73 #define G_PATH_LENGTH (PATH_MAX + 1)
74 #else /* !MAXPATHLEN */
75 #define G_PATH_LENGTH (2048 + 1)
76 #endif /* !MAXPATHLEN && !PATH_MAX */
78 const guint glib_major_version = GLIB_MAJOR_VERSION;
79 const guint glib_minor_version = GLIB_MINOR_VERSION;
80 const guint glib_micro_version = GLIB_MICRO_VERSION;
81 const guint glib_interface_age = GLIB_INTERFACE_AGE;
82 const guint glib_binary_age = GLIB_BINARY_AGE;
84 #if defined (NATIVE_WIN32) && defined (__LCC__)
85 int __stdcall
86 LibMain (void *hinstDll,
87 unsigned long dwReason,
88 void *reserved)
90 return 1;
92 #endif /* NATIVE_WIN32 && __LCC__ */
94 void
95 g_atexit (GVoidFunc func)
97 gint result;
98 gchar *error = NULL;
100 /* keep this in sync with glib.h */
102 #ifdef G_NATIVE_ATEXIT
103 result = ATEXIT (func);
104 if (result)
105 error = g_strerror (errno);
106 #elif defined (HAVE_ATEXIT)
107 # ifdef NeXT /* @#%@! NeXTStep */
108 result = !atexit ((void (*)(void)) func);
109 if (result)
110 error = g_strerror (errno);
111 # else
112 result = atexit ((void (*)(void)) func);
113 if (result)
114 error = g_strerror (errno);
115 # endif /* NeXT */
116 #elif defined (HAVE_ON_EXIT)
117 result = on_exit ((void (*)(int, void *)) func, NULL);
118 if (result)
119 error = g_strerror (errno);
120 #else
121 result = 0;
122 error = "no implementation";
123 #endif /* G_NATIVE_ATEXIT */
125 if (error)
126 g_error ("Could not register atexit() function: %s", error);
129 gint
130 g_snprintf (gchar *str,
131 gulong n,
132 gchar const *fmt,
133 ...)
135 #ifdef HAVE_VSNPRINTF
136 va_list args;
137 gint retval;
139 g_return_val_if_fail (str != NULL, 0);
140 g_return_val_if_fail (n > 0, 0);
141 g_return_val_if_fail (fmt != NULL, 0);
143 va_start (args, fmt);
144 retval = vsnprintf (str, n, fmt, args);
145 va_end (args);
147 if (retval < 0)
149 str[n-1] = '\0';
150 retval = strlen (str);
153 return retval;
154 #else /* !HAVE_VSNPRINTF */
155 gchar *printed;
156 va_list args;
158 g_return_val_if_fail (str != NULL, 0);
159 g_return_val_if_fail (n > 0, 0);
160 g_return_val_if_fail (fmt != NULL, 0);
162 va_start (args, fmt);
163 printed = g_strdup_vprintf (fmt, args);
164 va_end (args);
166 strncpy (str, printed, n);
167 str[n-1] = '\0';
169 g_free (printed);
171 return strlen (str);
172 #endif /* !HAVE_VSNPRINTF */
175 gint
176 g_vsnprintf (gchar *str,
177 gulong n,
178 gchar const *fmt,
179 va_list args)
181 #ifdef HAVE_VSNPRINTF
182 gint retval;
184 g_return_val_if_fail (str != NULL, 0);
185 g_return_val_if_fail (n > 0, 0);
186 g_return_val_if_fail (fmt != NULL, 0);
188 retval = vsnprintf (str, n, fmt, args);
190 if (retval < 0)
192 str[n-1] = '\0';
193 retval = strlen (str);
196 return retval;
197 #else /* !HAVE_VSNPRINTF */
198 gchar *printed;
200 g_return_val_if_fail (str != NULL, 0);
201 g_return_val_if_fail (n > 0, 0);
202 g_return_val_if_fail (fmt != NULL, 0);
204 printed = g_strdup_vprintf (fmt, args);
205 strncpy (str, printed, n);
206 str[n-1] = '\0';
208 g_free (printed);
210 return strlen (str);
211 #endif /* !HAVE_VSNPRINTF */
214 guint
215 g_parse_debug_string (const gchar *string,
216 GDebugKey *keys,
217 guint nkeys)
219 guint i;
220 guint result = 0;
222 g_return_val_if_fail (string != NULL, 0);
224 if (!g_strcasecmp (string, "all"))
226 for (i=0; i<nkeys; i++)
227 result |= keys[i].value;
229 else
231 gchar *str = g_strdup (string);
232 gchar *p = str;
233 gchar *q;
234 gboolean done = FALSE;
236 while (*p && !done)
238 q = strchr (p, ':');
239 if (!q)
241 q = p + strlen(p);
242 done = TRUE;
245 *q = 0;
247 for (i=0; i<nkeys; i++)
248 if (!g_strcasecmp(keys[i].key, p))
249 result |= keys[i].value;
251 p = q+1;
254 g_free (str);
257 return result;
260 gchar*
261 g_basename (const gchar *file_name)
263 register gchar *base;
265 g_return_val_if_fail (file_name != NULL, NULL);
267 base = strrchr (file_name, G_DIR_SEPARATOR);
268 if (base)
269 return base + 1;
271 #ifdef NATIVE_WIN32
272 if (isalpha (file_name[0]) && file_name[1] == ':')
273 return (gchar*) file_name + 2;
274 #endif /* NATIVE_WIN32 */
276 return (gchar*) file_name;
279 gboolean
280 g_path_is_absolute (const gchar *file_name)
282 g_return_val_if_fail (file_name != NULL, FALSE);
284 if (file_name[0] == G_DIR_SEPARATOR)
285 return TRUE;
287 #ifdef NATIVE_WIN32
288 if (isalpha (file_name[0]) && file_name[1] == ':' && file_name[2] == G_DIR_SEPARATOR)
289 return TRUE;
290 #endif
292 return FALSE;
295 gchar*
296 g_path_skip_root (gchar *file_name)
298 g_return_val_if_fail (file_name != NULL, NULL);
300 if (file_name[0] == G_DIR_SEPARATOR)
301 return file_name + 1;
303 #ifdef NATIVE_WIN32
304 if (isalpha (file_name[0]) && file_name[1] == ':' && file_name[2] == G_DIR_SEPARATOR)
305 return file_name + 3;
306 #endif
308 return NULL;
311 gchar*
312 g_dirname (const gchar *file_name)
314 register gchar *base;
315 register guint len;
317 g_return_val_if_fail (file_name != NULL, NULL);
319 base = strrchr (file_name, G_DIR_SEPARATOR);
320 if (!base)
321 return g_strdup (".");
322 while (base > file_name && *base == G_DIR_SEPARATOR)
323 base--;
324 len = (guint) 1 + base - file_name;
326 base = g_new (gchar, len + 1);
327 g_memmove (base, file_name, len);
328 base[len] = 0;
330 return base;
333 gchar*
334 g_get_current_dir (void)
336 gchar *buffer;
337 gchar *dir;
339 buffer = g_new (gchar, G_PATH_LENGTH);
340 *buffer = 0;
342 /* We don't use getcwd(3) on SUNOS, because, it does a popen("pwd")
343 * and, if that wasn't bad enough, hangs in doing so.
345 #if defined (sun) && !defined (__SVR4)
346 dir = getwd (buffer);
347 #else /* !sun */
348 dir = getcwd (buffer, G_PATH_LENGTH - 1);
349 #endif /* !sun */
351 if (!dir || !*buffer)
353 /* hm, should we g_error() out here?
354 * this can happen if e.g. "./" has mode \0000
356 buffer[0] = G_DIR_SEPARATOR;
357 buffer[1] = 0;
360 dir = g_strdup (buffer);
361 g_free (buffer);
363 return dir;
366 gchar*
367 g_getenv (const gchar *variable)
369 #ifndef NATIVE_WIN32
370 g_return_val_if_fail (variable != NULL, NULL);
372 return getenv (variable);
373 #else
374 gchar *v;
375 guint k;
376 static gchar *p = NULL;
377 static gint l;
378 gchar dummy[2];
380 g_return_val_if_fail (variable != NULL, NULL);
382 v = getenv (variable);
383 if (!v)
384 return NULL;
386 /* On Windows NT, it is relatively typical that environment variables
387 * contain references to other environment variables. Handle that by
388 * calling ExpandEnvironmentStrings.
391 /* First check how much space we need */
392 k = ExpandEnvironmentStrings (v, dummy, 2);
393 /* Then allocate that much, and actualy do the expansion */
394 if (p == NULL)
396 p = g_malloc (k);
397 l = k;
399 else if (k > l)
401 p = g_realloc (p, k);
402 l = k;
404 ExpandEnvironmentStrings (v, p, k);
405 return p;
406 #endif
410 G_LOCK_DEFINE_STATIC (g_utils_global);
412 static gchar *g_tmp_dir = NULL;
413 static gchar *g_user_name = NULL;
414 static gchar *g_real_name = NULL;
415 static gchar *g_home_dir = NULL;
417 /* HOLDS: g_utils_global_lock */
418 static void
419 g_get_any_init (void)
421 if (!g_tmp_dir)
423 g_tmp_dir = g_strdup (g_getenv ("TMPDIR"));
424 if (!g_tmp_dir)
425 g_tmp_dir = g_strdup (g_getenv ("TMP"));
426 if (!g_tmp_dir)
427 g_tmp_dir = g_strdup (g_getenv ("TEMP"));
429 #ifdef P_tmpdir
430 if (!g_tmp_dir)
432 int k;
433 g_tmp_dir = g_strdup (P_tmpdir);
434 k = strlen (g_tmp_dir);
435 if (g_tmp_dir[k-1] == G_DIR_SEPARATOR)
436 g_tmp_dir[k-1] = '\0';
438 #endif
440 if (!g_tmp_dir)
442 #ifndef NATIVE_WIN32
443 g_tmp_dir = g_strdup ("/tmp");
444 #else /* NATIVE_WIN32 */
445 g_tmp_dir = g_strdup ("C:\\");
446 #endif /* NATIVE_WIN32 */
449 if (!g_home_dir)
450 g_home_dir = g_strdup (g_getenv ("HOME"));
452 #ifdef NATIVE_WIN32
453 if (!g_home_dir)
455 /* The official way to specify a home directory on NT is
456 * the HOMEDRIVE and HOMEPATH environment variables.
458 * This is inside #ifdef NATIVE_WIN32 because with the cygwin dll,
459 * HOME should be a POSIX style pathname.
462 if (getenv ("HOMEDRIVE") != NULL && getenv ("HOMEPATH") != NULL)
464 gchar *homedrive, *homepath;
466 homedrive = g_strdup (g_getenv ("HOMEDRIVE"));
467 homepath = g_strdup (g_getenv ("HOMEPATH"));
469 g_home_dir = g_strconcat (homedrive, homepath, NULL);
470 g_free (homedrive);
471 g_free (homepath);
474 #endif /* !NATIVE_WIN32 */
476 #ifdef HAVE_PWD_H
478 struct passwd *pw = NULL;
479 gpointer buffer = NULL;
481 # ifdef HAVE_GETPWUID_R
482 struct passwd pwd;
483 # ifdef _SC_GETPW_R_SIZE_MAX
484 /* This reurns the maximum length */
485 guint bufsize = sysconf (_SC_GETPW_R_SIZE_MAX);
486 # else /* _SC_GETPW_R_SIZE_MAX */
487 guint bufsize = 64;
488 # endif /* _SC_GETPW_R_SIZE_MAX */
489 gint error;
493 g_free (buffer);
494 buffer = g_malloc (bufsize);
495 errno = 0;
497 # ifdef HAVE_GETPWUID_R_POSIX
498 error = getpwuid_r (getuid (), &pwd, buffer, bufsize, &pw);
499 error = error < 0 ? errno : error;
500 # else /* !HAVE_GETPWUID_R_POSIX */
501 # ifdef _AIX
502 error = getpwuid_r (getuid (), &pwd, buffer, bufsize);
503 pw = error == 0 ? &pwd : NULL;
504 # else /* !_AIX */
505 pw = getpwuid_r (getuid (), &pwd, buffer, bufsize);
506 error = pw ? 0 : errno;
507 # endif /* !_AIX */
508 # endif /* !HAVE_GETPWUID_R_POSIX */
510 if (!pw)
512 /* we bail out prematurely if the user id can't be found
513 * (should be pretty rare case actually), or if the buffer
514 * should be sufficiently big and lookups are still not
515 * successfull.
517 if (error == 0 || error == ENOENT)
519 g_warning ("getpwuid_r(): failed due to: No such user %d.",
520 getuid ());
521 break;
523 if (bufsize > 32 * 1024)
525 g_warning ("getpwuid_r(): failed due to: %s.",
526 g_strerror (error));
527 break;
530 bufsize *= 2;
533 while (!pw);
534 # endif /* !HAVE_GETPWUID_R */
536 if (!pw)
538 setpwent ();
539 pw = getpwuid (getuid ());
540 endpwent ();
542 if (pw)
544 g_user_name = g_strdup (pw->pw_name);
545 g_real_name = g_strdup (pw->pw_gecos);
546 if (!g_home_dir)
547 g_home_dir = g_strdup (pw->pw_dir);
549 g_free (buffer);
552 #else /* !HAVE_PWD_H */
554 # ifdef NATIVE_WIN32
556 guint len = 17;
557 gchar buffer[17];
559 if (GetUserName (buffer, &len))
561 g_user_name = g_strdup (buffer);
562 g_real_name = g_strdup (buffer);
565 # endif /* NATIVE_WIN32 */
567 #endif /* !HAVE_PWD_H */
569 if (!g_user_name)
570 g_user_name = g_strdup ("somebody");
571 if (!g_real_name)
572 g_real_name = g_strdup ("Unknown");
573 else
575 gchar *p;
577 for (p = g_real_name; *p; p++)
578 if (*p == ',')
580 *p = 0;
581 p = g_strdup (g_real_name);
582 g_free (g_real_name);
583 g_real_name = p;
584 break;
590 gchar*
591 g_get_user_name (void)
593 G_LOCK (g_utils_global);
594 if (!g_tmp_dir)
595 g_get_any_init ();
596 G_UNLOCK (g_utils_global);
598 return g_user_name;
601 gchar*
602 g_get_real_name (void)
604 G_LOCK (g_utils_global);
605 if (!g_tmp_dir)
606 g_get_any_init ();
607 G_UNLOCK (g_utils_global);
609 return g_real_name;
612 /* Return the home directory of the user. If there is a HOME
613 * environment variable, its value is returned, otherwise use some
614 * system-dependent way of finding it out. If no home directory can be
615 * deduced, return NULL.
618 gchar*
619 g_get_home_dir (void)
621 G_LOCK (g_utils_global);
622 if (!g_tmp_dir)
623 g_get_any_init ();
624 G_UNLOCK (g_utils_global);
626 return g_home_dir;
629 /* Return a directory to be used to store temporary files. This is the
630 * value of the TMPDIR, TMP or TEMP environment variables (they are
631 * checked in that order). If none of those exist, use P_tmpdir from
632 * stdio.h. If that isn't defined, return "/tmp" on POSIXly systems,
633 * and C:\ on Windows.
636 gchar*
637 g_get_tmp_dir (void)
639 G_LOCK (g_utils_global);
640 if (!g_tmp_dir)
641 g_get_any_init ();
642 G_UNLOCK (g_utils_global);
644 return g_tmp_dir;
647 static gchar *g_prgname = NULL;
649 gchar*
650 g_get_prgname (void)
652 gchar* retval;
654 G_LOCK (g_utils_global);
655 retval = g_prgname;
656 G_UNLOCK (g_utils_global);
658 return retval;
661 void
662 g_set_prgname (const gchar *prgname)
664 gchar *c;
666 G_LOCK (g_utils_global);
667 c = g_prgname;
668 g_prgname = g_strdup (prgname);
669 g_free (c);
670 G_UNLOCK (g_utils_global);
673 guint
674 g_direct_hash (gconstpointer v)
676 return GPOINTER_TO_UINT (v);
679 gint
680 g_direct_equal (gconstpointer v1,
681 gconstpointer v2)
683 return v1 == v2;
686 gint
687 g_int_equal (gconstpointer v1,
688 gconstpointer v2)
690 return *((const gint*) v1) == *((const gint*) v2);
693 guint
694 g_int_hash (gconstpointer v)
696 return *(const gint*) v;
699 #if 0 /* Old IO Channels */
701 GIOChannel*
702 g_iochannel_new (gint fd)
704 GIOChannel *channel = g_new (GIOChannel, 1);
706 channel->fd = fd;
708 #ifdef NATIVE_WIN32
709 channel->peer = 0;
710 channel->peer_fd = 0;
711 channel->offset = 0;
712 channel->need_wakeups = 0;
713 #endif /* NATIVE_WIN32 */
715 return channel;
718 void
719 g_iochannel_free (GIOChannel *channel)
721 g_return_if_fail (channel != NULL);
723 g_free (channel);
726 void
727 g_iochannel_close_and_free (GIOChannel *channel)
729 g_return_if_fail (channel != NULL);
731 close (channel->fd);
733 g_iochannel_free (channel);
736 #undef g_iochannel_wakeup_peer
738 void
739 g_iochannel_wakeup_peer (GIOChannel *channel)
741 #ifdef NATIVE_WIN32
742 static guint message = 0;
743 #endif
745 g_return_if_fail (channel != NULL);
747 #ifdef NATIVE_WIN32
748 if (message == 0)
749 message = RegisterWindowMessage ("gdk-pipe-readable");
751 # if 0
752 g_print ("g_iochannel_wakeup_peer: calling PostThreadMessage (%#x, %d, %d, %d)\n",
753 channel->peer, message, channel->peer_fd, channel->offset);
754 # endif
755 PostThreadMessage (channel->peer, message,
756 channel->peer_fd, channel->offset);
757 #endif /* NATIVE_WIN32 */
760 #endif /* Old IO Channels */
762 #ifdef NATIVE_WIN32
763 #ifdef _MSC_VER
766 gwin_ftruncate (gint fd,
767 guint size)
769 HANDLE hfile;
770 guint curpos;
772 g_return_val_if_fail (fd >= 0, -1);
774 hfile = (HANDLE) _get_osfhandle (fd);
775 curpos = SetFilePointer (hfile, 0, NULL, FILE_CURRENT);
776 if (curpos == 0xFFFFFFFF
777 || SetFilePointer (hfile, size, NULL, FILE_BEGIN) == 0xFFFFFFFF
778 || !SetEndOfFile (hfile))
780 gint error = GetLastError ();
782 switch (error)
784 case ERROR_INVALID_HANDLE:
785 errno = EBADF;
786 break;
787 default:
788 errno = EIO;
789 break;
792 return -1;
795 return 0;
798 DIR*
799 gwin_opendir (const char *dirname)
801 DIR *result;
802 gchar *mask;
803 guint k;
805 g_return_val_if_fail (dirname != NULL, NULL);
807 result = g_new0 (DIR, 1);
808 result->find_file_data = g_new0 (WIN32_FIND_DATA, 1);
809 result->dir_name = g_strdup (dirname);
811 k = strlen (result->dir_name);
812 if (k && result->dir_name[k - 1] == '\\')
814 result->dir_name[k - 1] = '\0';
815 k--;
817 mask = g_strdup_printf ("%s\\*", result->dir_name);
819 result->find_file_handle = (guint) FindFirstFile (mask,
820 (LPWIN32_FIND_DATA) result->find_file_data);
821 g_free (mask);
823 if (result->find_file_handle == (guint) INVALID_HANDLE_VALUE)
825 int error = GetLastError ();
827 g_free (result->dir_name);
828 g_free (result->find_file_data);
829 g_free (result);
830 switch (error)
832 default:
833 errno = EIO;
834 return NULL;
837 result->just_opened = TRUE;
839 return result;
842 struct dirent*
843 gwin_readdir (DIR *dir)
845 static struct dirent result;
847 g_return_val_if_fail (dir != NULL, NULL);
849 if (dir->just_opened)
850 dir->just_opened = FALSE;
851 else
853 if (!FindNextFile ((HANDLE) dir->find_file_handle,
854 (LPWIN32_FIND_DATA) dir->find_file_data))
856 int error = GetLastError ();
858 switch (error)
860 case ERROR_NO_MORE_FILES:
861 return NULL;
862 default:
863 errno = EIO;
864 return NULL;
868 strcpy (result.d_name, g_basename (((LPWIN32_FIND_DATA) dir->find_file_data)->cFileName));
870 return &result;
873 void
874 gwin_rewinddir (DIR *dir)
876 gchar *mask;
878 g_return_if_fail (dir != NULL);
880 if (!FindClose ((HANDLE) dir->find_file_handle))
881 g_warning ("gwin_rewinddir(): FindClose() failed\n");
883 mask = g_strdup_printf ("%s\\*", dir->dir_name);
884 dir->find_file_handle = (guint) FindFirstFile (mask,
885 (LPWIN32_FIND_DATA) dir->find_file_data);
886 g_free (mask);
888 if (dir->find_file_handle == (guint) INVALID_HANDLE_VALUE)
890 int error = GetLastError ();
892 switch (error)
894 default:
895 errno = EIO;
896 return;
899 dir->just_opened = TRUE;
902 gint
903 gwin_closedir (DIR *dir)
905 g_return_val_if_fail (dir != NULL, -1);
907 if (!FindClose ((HANDLE) dir->find_file_handle))
909 int error = GetLastError ();
911 switch (error)
913 default:
914 errno = EIO; return -1;
918 g_free (dir->dir_name);
919 g_free (dir->find_file_data);
920 g_free (dir);
922 return 0;
925 #endif /* _MSC_VER */
927 #endif /* NATIVE_WIN32 */