From: Tor Lillqvist Date: Mon, 30 Oct 2000 21:55:21 +0000 (+0000) Subject: Check for mkstemp. X-Git-Url: http://git.openbox.org/?a=commitdiff_plain;h=6acee58bdfc7e90ff8be4130b3eb3aaa319cfd3b;p=dana%2Fcg-glib.git Check for mkstemp. 2000-10-30 Tor Lillqvist * configure.in: Check for mkstemp. * gutils.c (g_mkstemp): New function. If HAVE_MKSTEMP, just call it, otherwise use code lifted from glibc. * gutils.h: Declare it. * glib.def: Here, too. --- diff --git a/ChangeLog b/ChangeLog index 7fedca6d..23d4930d 100644 --- a/ChangeLog +++ b/ChangeLog @@ -1,3 +1,14 @@ +2000-10-30 Tor Lillqvist + + * configure.in: Check for mkstemp. + + * gutils.c (g_mkstemp): New function. If HAVE_MKSTEMP, just call + it, otherwise use code lifted from glibc. + + * gutils.h: Declare it. + + * glib.def: Here, too. + 2000-10-30 Sebastian Wilhelmi * gcache.h, gcache.c, ghash.h, ghash.c, grel.c, grel.h, gtypes.h: diff --git a/ChangeLog.pre-2-0 b/ChangeLog.pre-2-0 index 7fedca6d..23d4930d 100644 --- a/ChangeLog.pre-2-0 +++ b/ChangeLog.pre-2-0 @@ -1,3 +1,14 @@ +2000-10-30 Tor Lillqvist + + * configure.in: Check for mkstemp. + + * gutils.c (g_mkstemp): New function. If HAVE_MKSTEMP, just call + it, otherwise use code lifted from glibc. + + * gutils.h: Declare it. + + * glib.def: Here, too. + 2000-10-30 Sebastian Wilhelmi * gcache.h, gcache.c, ghash.h, ghash.c, grel.c, grel.h, gtypes.h: diff --git a/ChangeLog.pre-2-10 b/ChangeLog.pre-2-10 index 7fedca6d..23d4930d 100644 --- a/ChangeLog.pre-2-10 +++ b/ChangeLog.pre-2-10 @@ -1,3 +1,14 @@ +2000-10-30 Tor Lillqvist + + * configure.in: Check for mkstemp. + + * gutils.c (g_mkstemp): New function. If HAVE_MKSTEMP, just call + it, otherwise use code lifted from glibc. + + * gutils.h: Declare it. + + * glib.def: Here, too. + 2000-10-30 Sebastian Wilhelmi * gcache.h, gcache.c, ghash.h, ghash.c, grel.c, grel.h, gtypes.h: diff --git a/ChangeLog.pre-2-12 b/ChangeLog.pre-2-12 index 7fedca6d..23d4930d 100644 --- a/ChangeLog.pre-2-12 +++ b/ChangeLog.pre-2-12 @@ -1,3 +1,14 @@ +2000-10-30 Tor Lillqvist + + * configure.in: Check for mkstemp. + + * gutils.c (g_mkstemp): New function. If HAVE_MKSTEMP, just call + it, otherwise use code lifted from glibc. + + * gutils.h: Declare it. + + * glib.def: Here, too. + 2000-10-30 Sebastian Wilhelmi * gcache.h, gcache.c, ghash.h, ghash.c, grel.c, grel.h, gtypes.h: diff --git a/ChangeLog.pre-2-2 b/ChangeLog.pre-2-2 index 7fedca6d..23d4930d 100644 --- a/ChangeLog.pre-2-2 +++ b/ChangeLog.pre-2-2 @@ -1,3 +1,14 @@ +2000-10-30 Tor Lillqvist + + * configure.in: Check for mkstemp. + + * gutils.c (g_mkstemp): New function. If HAVE_MKSTEMP, just call + it, otherwise use code lifted from glibc. + + * gutils.h: Declare it. + + * glib.def: Here, too. + 2000-10-30 Sebastian Wilhelmi * gcache.h, gcache.c, ghash.h, ghash.c, grel.c, grel.h, gtypes.h: diff --git a/ChangeLog.pre-2-4 b/ChangeLog.pre-2-4 index 7fedca6d..23d4930d 100644 --- a/ChangeLog.pre-2-4 +++ b/ChangeLog.pre-2-4 @@ -1,3 +1,14 @@ +2000-10-30 Tor Lillqvist + + * configure.in: Check for mkstemp. + + * gutils.c (g_mkstemp): New function. If HAVE_MKSTEMP, just call + it, otherwise use code lifted from glibc. + + * gutils.h: Declare it. + + * glib.def: Here, too. + 2000-10-30 Sebastian Wilhelmi * gcache.h, gcache.c, ghash.h, ghash.c, grel.c, grel.h, gtypes.h: diff --git a/ChangeLog.pre-2-6 b/ChangeLog.pre-2-6 index 7fedca6d..23d4930d 100644 --- a/ChangeLog.pre-2-6 +++ b/ChangeLog.pre-2-6 @@ -1,3 +1,14 @@ +2000-10-30 Tor Lillqvist + + * configure.in: Check for mkstemp. + + * gutils.c (g_mkstemp): New function. If HAVE_MKSTEMP, just call + it, otherwise use code lifted from glibc. + + * gutils.h: Declare it. + + * glib.def: Here, too. + 2000-10-30 Sebastian Wilhelmi * gcache.h, gcache.c, ghash.h, ghash.c, grel.c, grel.h, gtypes.h: diff --git a/ChangeLog.pre-2-8 b/ChangeLog.pre-2-8 index 7fedca6d..23d4930d 100644 --- a/ChangeLog.pre-2-8 +++ b/ChangeLog.pre-2-8 @@ -1,3 +1,14 @@ +2000-10-30 Tor Lillqvist + + * configure.in: Check for mkstemp. + + * gutils.c (g_mkstemp): New function. If HAVE_MKSTEMP, just call + it, otherwise use code lifted from glibc. + + * gutils.h: Declare it. + + * glib.def: Here, too. + 2000-10-30 Sebastian Wilhelmi * gcache.h, gcache.c, ghash.h, ghash.c, grel.c, grel.h, gtypes.h: diff --git a/configure.in b/configure.in index 695c7157..4cfafaa0 100644 --- a/configure.in +++ b/configure.in @@ -351,7 +351,7 @@ GLIB_SIZEOF([$size_includes], ptrdiff_t, ptrdiff_t) GLIB_SIZEOF([$size_includes], intmax_t, intmax_t) # Check for some functions -AC_CHECK_FUNCS(lstat strerror strsignal memmove vsnprintf strcasecmp strncasecmp poll getcwd) +AC_CHECK_FUNCS(lstat strerror strsignal memmove mkstemp vsnprintf strcasecmp strncasecmp poll getcwd) # Check if bcopy can be used for overlapping copies, if memmove isn't found. # The check is borrowed from the PERL Configure script. diff --git a/glib.def b/glib.def index 543288ad..37f9f9a7 100644 --- a/glib.def +++ b/glib.def @@ -239,6 +239,7 @@ EXPORTS g_mem_profile g_memdup g_messages_init + g_mkstemp g_mutex_init g_node_child_index g_node_child_position diff --git a/glib/glib.def b/glib/glib.def index 543288ad..37f9f9a7 100644 --- a/glib/glib.def +++ b/glib/glib.def @@ -239,6 +239,7 @@ EXPORTS g_mem_profile g_memdup g_messages_init + g_mkstemp g_mutex_init g_node_child_index g_node_child_position diff --git a/glib/gutils.c b/glib/gutils.c index fac67375..b7b786da 100644 --- a/glib/gutils.c +++ b/glib/gutils.c @@ -40,6 +40,7 @@ #include #include #include +#include #ifdef HAVE_PWD_H #include #endif @@ -76,6 +77,10 @@ #include #endif +#ifndef O_BINARY +#define O_BINARY 0 +#endif + const guint glib_major_version = GLIB_MAJOR_VERSION; const guint glib_minor_version = GLIB_MINOR_VERSION; const guint glib_micro_version = GLIB_MICRO_VERSION; @@ -964,3 +969,78 @@ g_get_codeset (void) #endif #endif } + +/** + * g_mkstemp: + * + * Open a temporary file + * + * The parameter is a string that should match the rules for mktemp, i.e. + * end in "XXXXXX". The X string will be modified to form the name + * of a file that didn't exist. + * + * Return value: A file handle (as from open()) to the file file + * opened for reading and writing. The file is opened in binary mode + * on platforms where there is a difference. The file handle should be + * closed with close(). In case of errors, -1 is returned. + * + * From the GNU C library. + * Copyright (C) 1991,92,93,94,95,96,97,98,99 Free Software Foundation, Inc. + */ +int +g_mkstemp (char *tmpl) +{ +#ifdef HAVE_MKSTEMP + return mkstemp (tmpl); +#else + int len; + char *XXXXXX; + int count, fd; + static const char letters[] = + "abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ0123456789"; + glong value; + GTimeVal tv; + + len = strlen (tmpl); + if (len < 6 || strcmp (&tmpl[len - 6], "XXXXXX")) + return -1; + + /* This is where the Xs start. */ + XXXXXX = &tmpl[len - 6]; + + /* Get some more or less random data. */ + g_get_current_time (&tv); + value = tv.tv_usec ^ tv.tv_sec; + + for (count = 0; count < 100; value += 7777, ++count) + { + glong v = value; + + /* Fill in the random bits. */ + XXXXXX[0] = letters[v % 62]; + v /= 62; + XXXXXX[1] = letters[v % 62]; + v /= 62; + XXXXXX[2] = letters[v % 62]; + v /= 62; + XXXXXX[3] = letters[v % 62]; + v /= 62; + XXXXXX[4] = letters[v % 62]; + v /= 62; + XXXXXX[5] = letters[v % 62]; + + fd = open (tmpl, O_RDWR | O_CREAT | O_EXCL | O_BINARY, 0600); + + if (fd >= 0) + return fd; + else if (errno != EEXIST) + /* Any other error will apply also to other names we might + * try, and there are 2^32 or so of them, so give up now. + */ + return -1; + } + + /* We got out of the loop because we ran out of combinations to try. */ + return -1; +#endif +} diff --git a/glib/gutils.h b/glib/gutils.h index c4dcb1b8..e70ec48d 100644 --- a/glib/gutils.h +++ b/glib/gutils.h @@ -161,6 +161,9 @@ gchar* g_path_get_dirname (const gchar *file_name); /* Get the codeset for the current locale */ /* gchar * g_get_codeset (void); */ +/* Wrapper / workalike for mkstemp() */ +int g_mkstemp (char *tmpl); + /* return the environment string for the variable. The returned memory * must not be freed. */ gchar* g_getenv (const gchar *variable); diff --git a/glib/makefile.msc.in b/glib/makefile.msc.in index 53002c38..e78672c3 100644 --- a/glib/makefile.msc.in +++ b/glib/makefile.msc.in @@ -96,6 +96,7 @@ glib-$(GLIB_VER).dll : $(glib_OBJECTS) glib.def gspawn-win32-helper.exe : gspawn-win32.c glib-$(GLIB_VER).dll $(CC) $(CFLAGS) -Fe$@ -DGSPAWN_HELPER -DG_LOG_DOMAIN=\"gspawn-win32-helper\" gspawn-win32.c glib-$(GLIB_VER).lib $(LDFLAGS) /subsystem:windows user32.lib + @erase gspawn-win32.obj ################ subdirectories diff --git a/gutils.c b/gutils.c index fac67375..b7b786da 100644 --- a/gutils.c +++ b/gutils.c @@ -40,6 +40,7 @@ #include #include #include +#include #ifdef HAVE_PWD_H #include #endif @@ -76,6 +77,10 @@ #include #endif +#ifndef O_BINARY +#define O_BINARY 0 +#endif + const guint glib_major_version = GLIB_MAJOR_VERSION; const guint glib_minor_version = GLIB_MINOR_VERSION; const guint glib_micro_version = GLIB_MICRO_VERSION; @@ -964,3 +969,78 @@ g_get_codeset (void) #endif #endif } + +/** + * g_mkstemp: + * + * Open a temporary file + * + * The parameter is a string that should match the rules for mktemp, i.e. + * end in "XXXXXX". The X string will be modified to form the name + * of a file that didn't exist. + * + * Return value: A file handle (as from open()) to the file file + * opened for reading and writing. The file is opened in binary mode + * on platforms where there is a difference. The file handle should be + * closed with close(). In case of errors, -1 is returned. + * + * From the GNU C library. + * Copyright (C) 1991,92,93,94,95,96,97,98,99 Free Software Foundation, Inc. + */ +int +g_mkstemp (char *tmpl) +{ +#ifdef HAVE_MKSTEMP + return mkstemp (tmpl); +#else + int len; + char *XXXXXX; + int count, fd; + static const char letters[] = + "abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ0123456789"; + glong value; + GTimeVal tv; + + len = strlen (tmpl); + if (len < 6 || strcmp (&tmpl[len - 6], "XXXXXX")) + return -1; + + /* This is where the Xs start. */ + XXXXXX = &tmpl[len - 6]; + + /* Get some more or less random data. */ + g_get_current_time (&tv); + value = tv.tv_usec ^ tv.tv_sec; + + for (count = 0; count < 100; value += 7777, ++count) + { + glong v = value; + + /* Fill in the random bits. */ + XXXXXX[0] = letters[v % 62]; + v /= 62; + XXXXXX[1] = letters[v % 62]; + v /= 62; + XXXXXX[2] = letters[v % 62]; + v /= 62; + XXXXXX[3] = letters[v % 62]; + v /= 62; + XXXXXX[4] = letters[v % 62]; + v /= 62; + XXXXXX[5] = letters[v % 62]; + + fd = open (tmpl, O_RDWR | O_CREAT | O_EXCL | O_BINARY, 0600); + + if (fd >= 0) + return fd; + else if (errno != EEXIST) + /* Any other error will apply also to other names we might + * try, and there are 2^32 or so of them, so give up now. + */ + return -1; + } + + /* We got out of the loop because we ran out of combinations to try. */ + return -1; +#endif +} diff --git a/gutils.h b/gutils.h index c4dcb1b8..e70ec48d 100644 --- a/gutils.h +++ b/gutils.h @@ -161,6 +161,9 @@ gchar* g_path_get_dirname (const gchar *file_name); /* Get the codeset for the current locale */ /* gchar * g_get_codeset (void); */ +/* Wrapper / workalike for mkstemp() */ +int g_mkstemp (char *tmpl); + /* return the environment string for the variable. The returned memory * must not be freed. */ gchar* g_getenv (const gchar *variable); diff --git a/makefile.msc.in b/makefile.msc.in index 53002c38..e78672c3 100644 --- a/makefile.msc.in +++ b/makefile.msc.in @@ -96,6 +96,7 @@ glib-$(GLIB_VER).dll : $(glib_OBJECTS) glib.def gspawn-win32-helper.exe : gspawn-win32.c glib-$(GLIB_VER).dll $(CC) $(CFLAGS) -Fe$@ -DGSPAWN_HELPER -DG_LOG_DOMAIN=\"gspawn-win32-helper\" gspawn-win32.c glib-$(GLIB_VER).lib $(LDFLAGS) /subsystem:windows user32.lib + @erase gspawn-win32.obj ################ subdirectories