+2008-10-10 Miguel de Icaza <miguel@novell.com>
+
+ * Split functionality that is operating system specific into
+ -win32.c, -unix.c and -posix.c
+
+ * Use g_malloc everywhere, and g_free, so that these can be easily
+ overwritten consistently.
+
2008-09-16 Bill Holmes <billholmes54@gmail.com>
* src/gunicode.c : Fix a warning.
AC_CANONICAL_SYSTEM
AM_CONFIG_HEADER(config.h)
-AM_INIT_AUTOMAKE(eglib,0.1)
+AM_INIT_AUTOMAKE(eglib,0.2)
AM_MAINTAINER_MODE
AC_PROG_CC
noinst_LTLIBRARIES = libeglib.la
+win_files = gdate-win32.c gdir-win32.c gfile-win32.c
+unix_files = gdate-unix.c gdir-unix.c gfile-unix.c
+
+if PLATFORM_WIN32
+os_files = $(win_files)
+else
+os_files = $(unix_files)
+endif
+
libeglib_la_SOURCES = \
eglib-config.h \
sort.frag.h \
gshell.c \
gspawn.c \
gtimer.c \
- gdate.c \
gfile.c \
+ gfile-posix.c \
gpattern.c \
gdir.c \
gmarkup.c \
gutf8.c \
- gunicode.c
+ gunicode.c \
+ $(os_files)
libeglib_la_CFLAGS = -Wall -D_FORTIFY_SOURCE=2
MAINTAINERCLEANFILES = Makefile.in
-EXTRA_DIST = eglib-config.h.in
+EXTRA_DIST = eglib-config.h.in $(win_files) $(unix_files)
--- /dev/null
+/*
+ * gdate-unix.c: Date and time utility functions.
+ *
+ * Author:
+ * Gonzalo Paniagua Javier (gonzalo@novell.com
+ *
+ * (C) 2006 Novell, Inc.
+ *
+ * Permission is hereby granted, free of charge, to any person obtaining
+ * a copy of this software and associated documentation files (the
+ * "Software"), to deal in the Software without restriction, including
+ * without limitation the rights to use, copy, modify, merge, publish,
+ * distribute, sublicense, and/or sell copies of the Software, and to
+ * permit persons to whom the Software is furnished to do so, subject to
+ * the following conditions:
+ *
+ * The above copyright notice and this permission notice shall be
+ * included in all copies or substantial portions of the Software.
+ *
+ * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
+ * EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
+ * MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
+ * NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE
+ * LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION
+ * OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION
+ * WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
+ */
+#include <stdio.h>
+#include <glib.h>
+
+#include <sys/time.h>
+
+void
+g_get_current_time (GTimeVal *result)
+{
+ g_return_if_fail (result != NULL);
+ struct timeval tv;
+
+ g_return_if_fail (result != NULL);
+ gettimeofday (&tv, NULL);
+ result->tv_sec = tv.tv_sec;
+ result->tv_usec = tv.tv_usec;
+}
--- /dev/null
+/*
+ * gdate-win32.c: Date and time utility functions.
+ *
+ * Author:
+ * Gonzalo Paniagua Javier (gonzalo@novell.com
+ *
+ * (C) 2006 Novell, Inc.
+ *
+ * Permission is hereby granted, free of charge, to any person obtaining
+ * a copy of this software and associated documentation files (the
+ * "Software"), to deal in the Software without restriction, including
+ * without limitation the rights to use, copy, modify, merge, publish,
+ * distribute, sublicense, and/or sell copies of the Software, and to
+ * permit persons to whom the Software is furnished to do so, subject to
+ * the following conditions:
+ *
+ * The above copyright notice and this permission notice shall be
+ * included in all copies or substantial portions of the Software.
+ *
+ * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
+ * EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
+ * MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
+ * NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE
+ * LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION
+ * OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION
+ * WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
+ */
+#include <stdio.h>
+#include <glib.h>
+
+#include <winsock2.h>
+
+void
+g_get_current_time (GTimeVal *result)
+{
+ g_return_if_fail (result != NULL);
+ long int l = GetTickCount();
+
+ g_return_if_fail (result != NULL);
+
+ result->tv_sec = l / 1000;
+ result->tv_usec = (l % 1000) * 1000;
+}
+++ /dev/null
-/*
- * Date and time utility functions.
- *
- * Author:
- * Gonzalo Paniagua Javier (gonzalo@novell.com
- *
- * (C) 2006 Novell, Inc.
- *
- * Permission is hereby granted, free of charge, to any person obtaining
- * a copy of this software and associated documentation files (the
- * "Software"), to deal in the Software without restriction, including
- * without limitation the rights to use, copy, modify, merge, publish,
- * distribute, sublicense, and/or sell copies of the Software, and to
- * permit persons to whom the Software is furnished to do so, subject to
- * the following conditions:
- *
- * The above copyright notice and this permission notice shall be
- * included in all copies or substantial portions of the Software.
- *
- * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
- * EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
- * MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
- * NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE
- * LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION
- * OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION
- * WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
- */
-#include <stdio.h>
-#include <glib.h>
-
-#ifdef G_OS_WIN32
-#include <winsock2.h>
-#else
-#include <sys/time.h>
-#endif
-
-void
-g_get_current_time (GTimeVal *result)
-{
-#ifdef G_OS_WIN32
- long int l = GetTickCount();
-
- g_return_if_fail (result != NULL);
-
- result->tv_sec = l / 1000;
- result->tv_usec = (l % 1000) * 1000;
-#else
- struct timeval tv;
-
- g_return_if_fail (result != NULL);
- gettimeofday (&tv, NULL);
- result->tv_sec = tv.tv_sec;
- result->tv_usec = tv.tv_usec;
-#endif
-}
--- /dev/null
+/*
+ * Directory utility functions.
+ *
+ * Author:
+ * Gonzalo Paniagua Javier (gonzalo@novell.com)
+ *
+ * (C) 2006 Novell, Inc.
+ *
+ * Permission is hereby granted, free of charge, to any person obtaining
+ * a copy of this software and associated documentation files (the
+ * "Software"), to deal in the Software without restriction, including
+ * without limitation the rights to use, copy, modify, merge, publish,
+ * distribute, sublicense, and/or sell copies of the Software, and to
+ * permit persons to whom the Software is furnished to do so, subject to
+ * the following conditions:
+ *
+ * The above copyright notice and this permission notice shall be
+ * included in all copies or substantial portions of the Software.
+ *
+ * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
+ * EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
+ * MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
+ * NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE
+ * LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION
+ * OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION
+ * WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
+ */
+#include <glib.h>
+#include <stdio.h>
+#include <stdlib.h>
+#include <errno.h>
+#include <sys/types.h>
+#include <sys/stat.h>
+#include <unistd.h>
+#include <dirent.h>
+
+struct _GDir {
+ DIR *dir;
+};
+
+GDir *
+g_dir_open (const gchar *path, guint flags, GError **error)
+{
+ GDir *dir;
+
+ g_return_val_if_fail (path != NULL, NULL);
+ g_return_val_if_fail (error == NULL || *error == NULL, NULL);
+
+ (void) flags; /* this is not used */
+ dir = g_new (GDir, 1);
+ dir->dir = opendir (path);
+ if (dir->dir == NULL) {
+ if (error) {
+ gint err = errno;
+ *error = g_error_new (G_LOG_DOMAIN, g_file_error_from_errno (err), strerror (err));
+ }
+ g_free (dir);
+ return NULL;
+ }
+ return dir;
+}
+
+const gchar *
+g_dir_read_name (GDir *dir)
+{
+ struct dirent *entry;
+
+ g_return_val_if_fail (dir != NULL && dir->dir != NULL, NULL);
+ do {
+ entry = readdir (dir->dir);
+ if (entry == NULL)
+ return NULL;
+ } while ((strcmp (entry->d_name, ".") == 0) || (strcmp (entry->d_name, "..") == 0));
+
+ return entry->d_name;
+}
+
+void
+g_dir_rewind (GDir *dir)
+{
+ g_return_if_fail (dir != NULL && dir->dir != NULL);
+ rewinddir (dir->dir);
+}
+
+void
+g_dir_close (GDir *dir)
+{
+ g_return_if_fail (dir != NULL && dir->dir != 0);
+ closedir (dir->dir);
+ dir->dir = NULL;
+ g_free (dir);
+}
+
+
--- /dev/null
+/*
+ * Directory utility functions.
+ *
+ * Author:
+ * Gonzalo Paniagua Javier (gonzalo@novell.com)
+ *
+ * (C) 2006 Novell, Inc.
+ *
+ * Permission is hereby granted, free of charge, to any person obtaining
+ * a copy of this software and associated documentation files (the
+ * "Software"), to deal in the Software without restriction, including
+ * without limitation the rights to use, copy, modify, merge, publish,
+ * distribute, sublicense, and/or sell copies of the Software, and to
+ * permit persons to whom the Software is furnished to do so, subject to
+ * the following conditions:
+ *
+ * The above copyright notice and this permission notice shall be
+ * included in all copies or substantial portions of the Software.
+ *
+ * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
+ * EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
+ * MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
+ * NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE
+ * LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION
+ * OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION
+ * WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
+ */
+#include <glib.h>
+#include <stdio.h>
+#include <stdlib.h>
+#include <errno.h>
+#include <sys/types.h>
+#include <sys/stat.h>
+#include <io.h>
+
+#include <winsock2.h>
+
+struct _GDir {
+ HANDLE handle;
+ gchar* current;
+ gchar* next;
+};
+
+GDir *
+g_dir_open (const gchar *path, guint flags, GError **error)
+{
+ GDir *dir;
+ gunichar2* path_utf16;
+ gunichar2* path_utf16_search;
+ WIN32_FIND_DATA find_data;
+
+ g_return_val_if_fail (path != NULL, NULL);
+ g_return_val_if_fail (error == NULL || *error == NULL, NULL);
+ dir = g_new0 (GDir, 1);
+
+ path_utf16 = u8to16 (path);
+
+ dir->handle = FindFirstFile (path_utf16, &find_data);
+ if (dir->handle == INVALID_HANDLE_VALUE) {
+ if (error) {
+ gint err = errno;
+ *error = g_error_new (G_LOG_DOMAIN, g_file_error_from_errno (err), strerror (err));
+ }
+ g_free (dir);
+ g_free (path_utf16);
+ return NULL;
+ }
+
+ /* now get files */
+ FindClose (dir->handle);
+ path_utf16_search = g_malloc ((wcslen(path_utf16) + 3)*sizeof(gunichar2));
+ wcscpy (path_utf16_search, path_utf16);
+ wcscat (path_utf16_search, L"\\*");
+
+ dir->handle = FindFirstFile (path_utf16_search, &find_data);
+ g_free (path_utf16_search);
+
+ while ((wcscmp (find_data.cFileName, L".") == 0) || (wcscmp (find_data.cFileName, L"..") == 0)) {
+ if (!FindNextFile (dir->handle, &find_data)) {
+ if (error) {
+ gint err = errno;
+ *error = g_error_new (G_LOG_DOMAIN, g_file_error_from_errno (err), strerror (err));
+ }
+ g_free (dir);
+ g_free (path_utf16);
+ return NULL;
+ }
+ }
+
+ dir->current = NULL;
+ dir->next = u16to8 (find_data.cFileName);
+
+ g_free (path_utf16);
+ return dir;
+}
+
+const gchar *
+g_dir_read_name (GDir *dir)
+{
+ WIN32_FIND_DATA find_data;
+
+ g_return_val_if_fail (dir != NULL && dir->handle != 0, NULL);
+
+ if (dir->current)
+ g_free (dir->current);
+ dir->current = NULL;
+
+ dir->current = dir->next;
+
+ if (!dir->current)
+ return NULL;
+
+ dir->next = NULL;
+
+ do {
+ if (!FindNextFile (dir->handle, &find_data)) {
+ dir->next = NULL;
+ return dir->current;
+ }
+ } while ((wcscmp (find_data.cFileName, L".") == 0) || (wcscmp (find_data.cFileName, L"..") == 0));
+
+ dir->next = u16to8 (find_data.cFileName);
+ return dir->current;
+}
+
+void
+g_dir_rewind (GDir *dir)
+{
+}
+
+void
+g_dir_close (GDir *dir)
+{
+ g_return_if_fail (dir != NULL && dir->handle != 0);
+
+ if (dir->current)
+ g_free (dir->current);
+ dir->current = NULL;
+ if (dir->next)
+ g_free (dir->next);
+ dir->next = NULL;
+ FindClose (dir->handle);
+ dir->handle = 0;
+ g_free (dir);
+}
+
+
--- /dev/null
+/*
+ * File utility functions.
+ *
+ * Author:
+ * Gonzalo Paniagua Javier (gonzalo@novell.com)
+ *
+ * (C) 2006 Novell, Inc.
+ *
+ * Permission is hereby granted, free of charge, to any person obtaining
+ * a copy of this software and associated documentation files (the
+ * "Software"), to deal in the Software without restriction, including
+ * without limitation the rights to use, copy, modify, merge, publish,
+ * distribute, sublicense, and/or sell copies of the Software, and to
+ * permit persons to whom the Software is furnished to do so, subject to
+ * the following conditions:
+ *
+ * The above copyright notice and this permission notice shall be
+ * included in all copies or substantial portions of the Software.
+ *
+ * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
+ * EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
+ * MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
+ * NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE
+ * LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION
+ * OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION
+ * WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
+ */
+#include <config.h>
+#include <glib.h>
+#include <stdio.h>
+#include <stdlib.h>
+#include <errno.h>
+#include <fcntl.h>
+#include <sys/stat.h>
+#include <sys/types.h>
+
+#ifndef O_LARGEFILE
+#define OPEN_FLAGS (O_RDONLY)
+#else
+#define OPEN_FLAGS (O_RDONLY | O_LARGEFILE)
+#endif
+gboolean
+g_file_get_contents (const gchar *filename, gchar **contents, gsize *length, GError **error)
+{
+ gchar *str;
+ int fd;
+ struct stat st;
+ long offset;
+ int nread;
+
+ g_return_val_if_fail (filename != NULL, FALSE);
+ g_return_val_if_fail (contents != NULL, FALSE);
+ g_return_val_if_fail (error == NULL || *error == NULL, FALSE);
+
+ *contents = NULL;
+ if (length)
+ *length = 0;
+
+ fd = open (filename, OPEN_FLAGS);
+ if (fd == -1) {
+ if (error != NULL) {
+ int err = errno;
+ *error = g_error_new (G_LOG_DOMAIN, g_file_error_from_errno (err), "Error opening file");
+ }
+ return FALSE;
+ }
+
+ if (fstat (fd, &st) != 0) {
+ if (error != NULL) {
+ int err = errno;
+ *error = g_error_new (G_LOG_DOMAIN, g_file_error_from_errno (err), "Error in fstat()");
+ }
+ close (fd);
+ return FALSE;
+ }
+
+ str = g_malloc (st.st_size + 1);
+ offset = 0;
+ do {
+ nread = read (fd, str + offset, st.st_size - offset);
+ if (nread > 0) {
+ offset += nread;
+ }
+ } while ((nread > 0 && offset < st.st_size) || (nread == -1 && errno == EINTR));
+
+ close (fd);
+ str [st.st_size] = '\0';
+ if (length) {
+ *length = st.st_size;
+ }
+ *contents = str;
+ return TRUE;
+}
+
+gint
+g_file_open_tmp (const gchar *tmpl, gchar **name_used, GError **error)
+{
+ const static gchar *default_tmpl = ".XXXXXX";
+ gchar *t;
+ gint fd;
+ size_t len;
+
+ g_return_val_if_fail (error == NULL || *error == NULL, -1);
+
+ if (tmpl == NULL)
+ tmpl = default_tmpl;
+
+ if (strchr (tmpl, G_DIR_SEPARATOR) != NULL) {
+ if (error) {
+ *error = g_error_new (G_LOG_DOMAIN, 24, "Template should not have any " G_DIR_SEPARATOR_S);
+ }
+ return -1;
+ }
+
+ len = strlen (tmpl);
+ if (len < 6 || strcmp (tmpl + len - 6, "XXXXXX")) {
+ if (error) {
+ *error = g_error_new (G_LOG_DOMAIN, 24, "Template should end with XXXXXX");
+ }
+ return -1;
+ }
+
+ t = g_build_filename (g_get_tmp_dir (), tmpl, NULL);
+
+ fd = mkstemp (t);
+
+ if (fd == -1) {
+ if (error) {
+ int err = errno;
+ *error = g_error_new (G_LOG_DOMAIN, g_file_error_from_errno (err), "Error in mkstemp()");
+ }
+ g_free (t);
+ return -1;
+ }
+
+ if (name_used) {
+ *name_used = t;
+ } else {
+ g_free (t);
+ }
+ return fd;
+}
--- /dev/null
+/*
+ * File utility functions.
+ *
+ * Author:
+ * Gonzalo Paniagua Javier (gonzalo@novell.com)
+ *
+ * (C) 2006 Novell, Inc.
+ *
+ * Permission is hereby granted, free of charge, to any person obtaining
+ * a copy of this software and associated documentation files (the
+ * "Software"), to deal in the Software without restriction, including
+ * without limitation the rights to use, copy, modify, merge, publish,
+ * distribute, sublicense, and/or sell copies of the Software, and to
+ * permit persons to whom the Software is furnished to do so, subject to
+ * the following conditions:
+ *
+ * The above copyright notice and this permission notice shall be
+ * included in all copies or substantial portions of the Software.
+ *
+ * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
+ * EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
+ * MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
+ * NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE
+ * LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION
+ * OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION
+ * WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
+ */
+#include <config.h>
+#include <glib.h>
+#include <stdio.h>
+#include <stdlib.h>
+#include <errno.h>
+#include <sys/types.h>
+#include <sys/stat.h>
+#include <fcntl.h>
+#include <sys/types.h>
+#include <sys/stat.h>
+
+#ifdef HAVE_UNISTD_H
+#include <unistd.h>
+#endif
+
+gboolean
+g_file_test (const gchar *filename, GFileTest test)
+{
+ struct stat st;
+ gboolean have_stat;
+
+ if (filename == NULL || test == 0)
+ return FALSE;
+
+ have_stat = FALSE;
+
+ if ((test & G_FILE_TEST_EXISTS) != 0) {
+ if (access (filename, F_OK) == 0)
+ return TRUE;
+ }
+
+ if ((test & G_FILE_TEST_IS_EXECUTABLE) != 0) {
+ if (access (filename, X_OK) == 0)
+ return TRUE;
+ }
+ if ((test & G_FILE_TEST_IS_SYMLINK) != 0) {
+ have_stat = (lstat (filename, &st) == 0);
+ if (have_stat && S_ISLNK (st.st_mode))
+ return TRUE;
+ }
+
+ if ((test & G_FILE_TEST_IS_REGULAR) != 0) {
+ if (!have_stat)
+ have_stat = (stat (filename, &st) == 0);
+ if (have_stat && S_ISREG (st.st_mode))
+ return TRUE;
+ }
+ if ((test & G_FILE_TEST_IS_DIR) != 0) {
+ if (!have_stat)
+ have_stat = (stat (filename, &st) == 0);
+ if (have_stat && S_ISDIR (st.st_mode))
+ return TRUE;
+ }
+ return FALSE;
+}
+
+
--- /dev/null
+/*
+ * File utility functions.
+ *
+ * Author:
+ * Gonzalo Paniagua Javier (gonzalo@novell.com)
+ *
+ * (C) 2006 Novell, Inc.
+ *
+ * Permission is hereby granted, free of charge, to any person obtaining
+ * a copy of this software and associated documentation files (the
+ * "Software"), to deal in the Software without restriction, including
+ * without limitation the rights to use, copy, modify, merge, publish,
+ * distribute, sublicense, and/or sell copies of the Software, and to
+ * permit persons to whom the Software is furnished to do so, subject to
+ * the following conditions:
+ *
+ * The above copyright notice and this permission notice shall be
+ * included in all copies or substantial portions of the Software.
+ *
+ * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
+ * EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
+ * MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
+ * NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE
+ * LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION
+ * OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION
+ * WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
+ */
+#include <config.h>
+#include <glib.h>
+#include <stdio.h>
+#include <stdlib.h>
+#include <errno.h>
+#include <sys/types.h>
+#include <sys/stat.h>
+#include <fcntl.h>
+#include <sys/types.h>
+#include <sys/stat.h>
+
+#ifdef G_OS_WIN32
+#include <io.h>
+#define open _open
+#define S_ISREG(x) ((x & _S_IFMT) == _S_IFREG)
+#define S_ISDIR(x) ((x & _S_IFMT) == _S_IFDIR)
+#endif
+
+int mkstemp (char *tmp_template)
+{
+ int fd;
+ gunichar2* utf16_template;
+
+ utf16_template = u8to16 (tmp_template);
+
+ fd = -1;
+ utf16_template = _wmktemp( utf16_template);
+ if (utf16_template && *utf16_template) {
+ /* FIXME: _O_TEMPORARY causes file to disappear on close causing a test to fail */
+ fd = _wopen( utf16_template, _O_BINARY | _O_CREAT /*| _O_TEMPORARY*/ | _O_EXCL, _S_IREAD | _S_IWRITE);
+ }
+
+ sprintf (tmp_template + strlen (tmp_template) - 6, "%S", utf16_template + wcslen (utf16_template) - 6);
+
+ g_free (utf16_template);
+ return fd;
+}
+
+#ifdef _MSC_VER
+#pragma warning(disable:4701)
+#endif
+
+gboolean
+g_file_test (const gchar *filename, GFileTest test)
+{
+ struct _stat64 stat;
+ int ret = 0;
+ gunichar2* utf16_filename = NULL;
+
+ if (filename == NULL || test == 0)
+ return FALSE;
+
+ utf16_filename = u8to16 (filename);
+ ret = _wstati64 (utf16_filename, &stat);
+ g_free (utf16_filename);
+
+ if ((test & G_FILE_TEST_EXISTS) != 0) {
+ if (ret == 0)
+ return TRUE;
+ }
+
+ if (ret != 0)
+ return FALSE;
+
+ if ((test & G_FILE_TEST_IS_EXECUTABLE) != 0) {
+ if (stat.st_mode & _S_IEXEC)
+ return TRUE;
+ }
+
+ if ((test & G_FILE_TEST_IS_REGULAR) != 0) {
+ if (stat.st_mode & _S_IFREG)
+ return TRUE;
+ }
+
+ if ((test & G_FILE_TEST_IS_DIR) != 0) {
+ if (stat.st_mode & _S_IFDIR)
+ return TRUE;
+ }
+
+ /* make this last in case it is OR'd with something else */
+ if ((test & G_FILE_TEST_IS_SYMLINK) != 0) {
+ return FALSE;
+ }
+
+ return FALSE;
+}
#include <config.h>
#include <glib.h>
#include <stdio.h>
-#include <stdlib.h>
#include <errno.h>
-#include <sys/types.h>
-#include <sys/stat.h>
-#include <fcntl.h>
-
-#ifdef G_OS_WIN32
-#include <io.h>
-#define open _open
-#define S_ISREG(x) ((x & _S_IFMT) == _S_IFREG)
-#define S_ISDIR(x) ((x & _S_IFMT) == _S_IFDIR)
-#endif
-
-#ifdef HAVE_UNISTD_H
-#include <unistd.h>
-#endif
GFileError
g_file_error_from_errno (gint err_no)
}
}
-#ifndef O_LARGEFILE
-#define OPEN_FLAGS (O_RDONLY)
-#else
-#define OPEN_FLAGS (O_RDONLY | O_LARGEFILE)
-#endif
-gboolean
-g_file_get_contents (const gchar *filename, gchar **contents, gsize *length, GError **error)
-{
- gchar *str;
- int fd;
- struct stat st;
- long offset;
- int nread;
-
- g_return_val_if_fail (filename != NULL, FALSE);
- g_return_val_if_fail (contents != NULL, FALSE);
- g_return_val_if_fail (error == NULL || *error == NULL, FALSE);
-
- *contents = NULL;
- if (length)
- *length = 0;
-
- fd = open (filename, OPEN_FLAGS);
- if (fd == -1) {
- if (error != NULL) {
- int err = errno;
- *error = g_error_new (G_LOG_DOMAIN, g_file_error_from_errno (err), "Error opening file");
- }
- return FALSE;
- }
-
- if (fstat (fd, &st) != 0) {
- if (error != NULL) {
- int err = errno;
- *error = g_error_new (G_LOG_DOMAIN, g_file_error_from_errno (err), "Error in fstat()");
- }
- close (fd);
- return FALSE;
- }
-
- str = g_malloc (st.st_size + 1);
- offset = 0;
- do {
- nread = read (fd, str + offset, st.st_size - offset);
- if (nread > 0) {
- offset += nread;
- }
- } while ((nread > 0 && offset < st.st_size) || (nread == -1 && errno == EINTR));
-
- close (fd);
- str [st.st_size] = '\0';
- if (length) {
- *length = st.st_size;
- }
- *contents = str;
- return TRUE;
-}
-
-#ifdef _MSC_VER
-int mkstemp (char *tmp_template)
-{
- int fd;
- gunichar2* utf16_template;
-
- utf16_template = u8to16 (tmp_template);
-
- fd = -1;
- utf16_template = _wmktemp( utf16_template);
- if (utf16_template && *utf16_template) {
- /* FIXME: _O_TEMPORARY causes file to disappear on close causing a test to fail */
- fd = _wopen( utf16_template, _O_BINARY | _O_CREAT /*| _O_TEMPORARY*/ | _O_EXCL, _S_IREAD | _S_IWRITE);
- }
-
- sprintf (tmp_template + strlen (tmp_template) - 6, "%S", utf16_template + wcslen (utf16_template) - 6);
-
- g_free (utf16_template);
- return fd;
-}
-#endif
-
-gint
-g_file_open_tmp (const gchar *tmpl, gchar **name_used, GError **error)
-{
- const static gchar *default_tmpl = ".XXXXXX";
- gchar *t;
- gint fd;
- size_t len;
-
- g_return_val_if_fail (error == NULL || *error == NULL, -1);
-
- if (tmpl == NULL)
- tmpl = default_tmpl;
-
- if (strchr (tmpl, G_DIR_SEPARATOR) != NULL) {
- if (error) {
- *error = g_error_new (G_LOG_DOMAIN, 24, "Template should not have any " G_DIR_SEPARATOR_S);
- }
- return -1;
- }
-
- len = strlen (tmpl);
- if (len < 6 || strcmp (tmpl + len - 6, "XXXXXX")) {
- if (error) {
- *error = g_error_new (G_LOG_DOMAIN, 24, "Template should end with XXXXXX");
- }
- return -1;
- }
-
- t = g_build_filename (g_get_tmp_dir (), tmpl, NULL);
-
- fd = mkstemp (t);
-
- if (fd == -1) {
- if (error) {
- int err = errno;
- *error = g_error_new (G_LOG_DOMAIN, g_file_error_from_errno (err), "Error in mkstemp()");
- }
- g_free (t);
- return -1;
- }
-
- if (name_used) {
- *name_used = t;
- } else {
- g_free (t);
- }
- return fd;
-}
-
-#ifdef _MSC_VER
-#pragma warning(disable:4701)
-#endif
-
-gboolean
-g_file_test (const gchar *filename, GFileTest test)
-{
-#ifdef G_OS_WIN32
- struct _stat64 stat;
- int ret = 0;
- gunichar2* utf16_filename = NULL;
-
- if (filename == NULL || test == 0)
- return FALSE;
-
- utf16_filename = u8to16 (filename);
- ret = _wstati64 (utf16_filename, &stat);
- g_free (utf16_filename);
-
- if ((test & G_FILE_TEST_EXISTS) != 0) {
- if (ret == 0)
- return TRUE;
- }
-
- if (ret != 0)
- return FALSE;
-
- if ((test & G_FILE_TEST_IS_EXECUTABLE) != 0) {
- if (stat.st_mode & _S_IEXEC)
- return TRUE;
- }
-
- if ((test & G_FILE_TEST_IS_REGULAR) != 0) {
- if (stat.st_mode & _S_IFREG)
- return TRUE;
- }
-
- if ((test & G_FILE_TEST_IS_DIR) != 0) {
- if (stat.st_mode & _S_IFDIR)
- return TRUE;
- }
-
- /* make this last in case it is OR'd with something else */
- if ((test & G_FILE_TEST_IS_SYMLINK) != 0) {
- return FALSE;
- }
-
- return FALSE;
-#else
- struct stat st;
- gboolean have_stat;
-
- if (filename == NULL || test == 0)
- return FALSE;
-
- have_stat = FALSE;
-
- if ((test & G_FILE_TEST_EXISTS) != 0) {
- if (access (filename, F_OK) == 0)
- return TRUE;
- }
-
- if ((test & G_FILE_TEST_IS_EXECUTABLE) != 0) {
- if (access (filename, X_OK) == 0)
- return TRUE;
- }
- if ((test & G_FILE_TEST_IS_SYMLINK) != 0) {
- have_stat = (lstat (filename, &st) == 0);
- if (have_stat && S_ISLNK (st.st_mode))
- return TRUE;
- }
-
- if ((test & G_FILE_TEST_IS_REGULAR) != 0) {
- if (!have_stat)
- have_stat = (stat (filename, &st) == 0);
- if (have_stat && S_ISREG (st.st_mode))
- return TRUE;
- }
- if ((test & G_FILE_TEST_IS_DIR) != 0) {
- if (!have_stat)
- have_stat = (stat (filename, &st) == 0);
- if (have_stat && S_ISDIR (st.st_mode))
- return TRUE;
- }
- return FALSE;
-#endif
-}
-
return end;
l = (int)(p - start);
p++;
- *value = malloc (l + 1);
+ *value = g_malloc (l + 1);
if (*value == NULL)
return end;
strncpy (*value, start, l);
return end;
l = (int)(p - start);
- *value = malloc (l + 1);
+ *value = g_malloc (l + 1);
if (*value == NULL)
return end;
strncpy (*value, start, l);
p = skip_space (p, end);
if (p == end){
- free (name);
+ g_free (name);
return p;
}
if (*p != '='){
set_error ("Expected an = after the attribute name `%s'", name);
- free (name);
+ g_free (name);
return end;
}
p++;
p = skip_space (p, end);
if (p == end){
- free (name);
+ g_free (name);
return end;
}
p = parse_value (p, end, &value, error);
if (p == end){
- free (name);
+ g_free (name);
return p;
}
goto fail;
}
l = (int)(element_end - element_start);
- ename = malloc (l + 1);
+ ename = g_malloc (l + 1);
if (ename == NULL)
goto fail;
strncpy (ename, element_start, l);
}
if (error != NULL && *error != NULL){
- free (ename);
+ g_free (ename);
goto fail;
}
goto fail;
}
}
- free (ename);
+ g_free (ename);
} else {
context->level = g_slist_prepend (context->level, ename);
}
if (context->parser.end_element != NULL){
context->parser.end_element (context, text, context->user_data, error);
if (error != NULL && *error != NULL){
- free (text);
+ g_free (text);
goto fail;
}
}
- free (text);
+ g_free (text);
while (p < end && *p != '>')
p++;
gchar *home_dir = NULL;
if (drive && path) {
- home_dir = malloc(strlen(drive) + strlen(path) +1);
+ home_dir = g_malloc(strlen(drive) + strlen(path) +1);
if (home_dir) {
sprintf(home_dir, "%s%s", drive, path);
}
return strndup (str, n);
#else
if (str) {
- char *retval = malloc(n+1);
+ char *retval = g_malloc(n+1);
if (retval) {
strncpy(retval, str, n)[n] = 0;
}
va_end (args);
g_string_append (string, ret);
- free (ret);
+ g_free (ret);
}
void
int vasprintf(char **ret, const char *fmt, va_list ap)
{
-char *buf;
-int len;
-size_t buflen;
-va_list ap2;
-
+ char *buf;
+ int len;
+ size_t buflen;
+ va_list ap2;
+
#ifdef _MSC_VER
-ap2 = ap;
-len = _vscprintf(fmt, ap2); // NOTE MS specific extension ( :-( )
+ ap2 = ap;
+ len = _vscprintf(fmt, ap2); // NOTE MS specific extension ( :-( )
#else
-va_copy(ap2, ap);
-len = vsnprintf(NULL, 0, fmt, ap2);
+ va_copy(ap2, ap);
+ len = vsnprintf(NULL, 0, fmt, ap2);
#endif
-
-if (len > 0 && (buf = malloc((buflen = (size_t) (len + 1)))) != NULL) {
-len = vsnprintf(buf, buflen, fmt, ap);
-*ret = buf;
-} else {
-*ret = NULL;
-len = -1;
-}
-
-va_end(ap2);
-return len;
+
+ if (len > 0 && (buf = malloc((buflen = (size_t) (len + 1)))) != NULL) {
+ len = vsnprintf(buf, buflen, fmt, ap);
+ *ret = buf;
+ } else {
+ *ret = NULL;
+ len = -1;
+ }
+
+ va_end(ap2);
+ return len;
}