3 * Functions copied from glib 2.6 and 2.8
5 * Copyright 2005 David Schleef <ds@schleef.org>
8 /* gfileutils.c - File utility functions
10 * Copyright 2000 Red Hat, Inc.
12 * GLib is free software; you can redistribute it and/or modify it
13 * under the terms of the GNU Lesser General Public License as
14 * published by the Free Software Foundation; either version 2 of the
15 * License, or (at your option) any later version.
17 * GLib is distributed in the hope that it will be useful,
18 * but WITHOUT ANY WARRANTY; without even the implied warranty of
19 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
20 * Lesser General Public License for more details.
22 * You should have received a copy of the GNU Lesser General Public
23 * License along with GLib; see the file COPYING.LIB. If not,
24 * write to the Free Software Foundation, Inc., 59 Temple Place - Suite 330,
25 * Boston, MA 02111-1307, USA.
31 #if GLIB_CHECK_VERSION (2, 6, 0)
32 #include <glib/gstdio.h>
38 #include "glib-compat.h"
44 #include <sys/types.h>
59 #endif /* G_OS_WIN32 */
63 #define G_DIR_SEPARATOR '\\'
64 #define G_DIR_SEPARATOR_S "\\"
65 #define G_IS_DIR_SEPARATOR(c) ((c) == G_DIR_SEPARATOR || (c) == '/')
66 #define G_SEARCHPATH_SEPARATOR ';'
67 #define G_SEARCHPATH_SEPARATOR_S ";"
69 #define G_DIR_SEPARATOR '/'
70 #define G_DIR_SEPARATOR_S "/"
71 #define G_IS_DIR_SEPARATOR(c) ((c) == G_DIR_SEPARATOR)
72 #define G_SEARCHPATH_SEPARATOR ':'
73 #define G_SEARCHPATH_SEPARATOR_S ":"
77 #if !GLIB_CHECK_VERSION (2, 8, 0)
79 * g_mkdir_with_parents:
80 * @pathname: a pathname in the GLib file name encoding
81 * @mode: permissions to use for newly created directories
83 * Create a directory if it doesn't already exist. Create intermediate
84 * parent directories as needed, too.
86 * Returns: 0 if the directory already exists, or was successfully
87 * created. Returns -1 if an error occurred, with errno set.
92 g_mkdir_with_parents (const gchar * pathname, int mode)
96 if (pathname == NULL || *pathname == '\0') {
101 fn = g_strdup (pathname);
103 if (g_path_is_absolute (fn))
104 p = (gchar *) g_path_skip_root (fn);
109 while (*p && !G_IS_DIR_SEPARATOR (*p))
117 if (!g_file_test (fn, G_FILE_TEST_EXISTS)) {
118 if (g_mkdir (fn, mode) == -1) {
119 int errno_save = errno;
125 } else if (!g_file_test (fn, G_FILE_TEST_IS_DIR)) {
131 *p++ = G_DIR_SEPARATOR;
132 while (*p && G_IS_DIR_SEPARATOR (*p))
145 #if !GLIB_CHECK_VERSION (2, 6, 0)
148 * @filename: a pathname in the GLib file name encoding (UTF-8 on Windows)
149 * @mode: permissions to use for the newly created directory
151 * A wrapper for the POSIX mkdir() function. The mkdir() function
152 * attempts to create a directory with the given name and permissions.
154 * See the C library manual for more details about mkdir().
156 * Returns: 0 if the directory was successfully created, -1 if an error
162 g_mkdir (const gchar * filename, int mode)
165 if (G_WIN32_HAVE_WIDECHAR_API ()) {
166 wchar_t *wfilename = g_utf8_to_utf16 (filename, -1, NULL, NULL, NULL);
170 if (wfilename == NULL) {
175 retval = _wmkdir (wfilename);
183 gchar *cp_filename = g_locale_from_utf8 (filename, -1, NULL, NULL, NULL);
187 if (cp_filename == NULL) {
192 retval = mkdir (cp_filename);
195 g_free (cp_filename);
201 return mkdir (filename, mode);
206 #if !GLIB_CHECK_VERSION (2, 6, 0)
209 * @filename: a pathname in the GLib file name encoding (UTF-8 on Windows)
210 * @buf: a pointer to a <structname>stat</structname> struct, which
211 * will be filled with the file information
213 * A wrapper for the POSIX stat() function. The stat() function
214 * returns information about a file.
216 * See the C library manual for more details about stat().
218 * Returns: 0 if the information was successfully retrieved, -1 if an error
224 g_stat (const gchar * filename, struct stat *buf)
227 if (G_WIN32_HAVE_WIDECHAR_API ()) {
228 wchar_t *wfilename = g_utf8_to_utf16 (filename, -1, NULL, NULL, NULL);
232 if (wfilename == NULL) {
237 retval = _wstat (wfilename, (struct _stat *) buf);
245 gchar *cp_filename = g_locale_from_utf8 (filename, -1, NULL, NULL, NULL);
249 if (cp_filename == NULL) {
254 retval = stat (cp_filename, buf);
257 g_free (cp_filename);
263 return stat (filename, buf);