2000-10-09 16:24:57 +00:00
|
|
|
/* gfileutils.c - File utility functions
|
|
|
|
*
|
|
|
|
* Copyright 2000 Red Hat, Inc.
|
|
|
|
*
|
|
|
|
* GLib is free software; you can redistribute it and/or modify it
|
|
|
|
* under the terms of the GNU Lesser General Public License as
|
|
|
|
* published by the Free Software Foundation; either version 2 of the
|
|
|
|
* License, or (at your option) any later version.
|
|
|
|
*
|
|
|
|
* GLib is distributed in the hope that it will be useful,
|
|
|
|
* but WITHOUT ANY WARRANTY; without even the implied warranty of
|
|
|
|
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
|
|
|
|
* Lesser General Public License for more details.
|
|
|
|
*
|
|
|
|
* You should have received a copy of the GNU Lesser General Public
|
|
|
|
* License along with GLib; see the file COPYING.LIB. If not,
|
|
|
|
* write to the Free Software Foundation, Inc., 59 Temple Place - Suite 330,
|
|
|
|
* Boston, MA 02111-1307, USA.
|
|
|
|
*/
|
|
|
|
|
Some more debugging output. (g_io_channel_win32_poll): Remove unused vars.
2000-10-16 Tor Lillqvist <tml@iki.fi>
* giowin32.c (reader_thread): Some more debugging output.
(g_io_channel_win32_poll): Remove unused vars.
* gfileutils.c: Changes for Win32, with no unistd.h and no
S_ISLNK().
* gspawn-win32.c: Implementation of the g_spwan_* functions for
Win32. Due to the general non-Unixness of Win32, much of the
functionality that is relatively clean to implement on Unix, is
hard to do on Win32. We must use a separate helper program to
change directory, close extra file descriptors, redirect the std
ones, as needed, and only then start the child process. No child
process pid can be returned, unfortunately. Or if we used
CreateProcess directly, it probably could. (Now we use the spawnv*
functions from msvcrt.)
* glib.def: Add new entry points.
* glib.def
* giowin32.c: Remove g_io_channel_win32_wait_for_condition(),
g_io_channel_win32_poll() subsumes it.
* gbacktrace.h: G_BREAKPOINT for MSVC (on the ix86).
* gwin32.c (g_win32_getlocale): Use "sp" for
LANG_CROATIAN+SUBLANG_SERBIAN_LATIN.
* makefile.{mingw,msc}.in (glib_OBJECTS): Add new files.
Add gspawn-win32-helper.exe rule.
* tests/makefile.{mingw,msc}.in (TESTS): Add shell-test and
spawn-test.
* tests/spawn-test.c: (run_tests): On Win32, don't try to run
/bin/sh, but ipconfig (no special significance in choosing that,
just a program that outputs something to stdout).
2000-10-16 18:54:29 +00:00
|
|
|
#include "config.h"
|
|
|
|
|
2000-10-09 16:24:57 +00:00
|
|
|
#include "glib.h"
|
|
|
|
|
|
|
|
#include <sys/stat.h>
|
Some more debugging output. (g_io_channel_win32_poll): Remove unused vars.
2000-10-16 Tor Lillqvist <tml@iki.fi>
* giowin32.c (reader_thread): Some more debugging output.
(g_io_channel_win32_poll): Remove unused vars.
* gfileutils.c: Changes for Win32, with no unistd.h and no
S_ISLNK().
* gspawn-win32.c: Implementation of the g_spwan_* functions for
Win32. Due to the general non-Unixness of Win32, much of the
functionality that is relatively clean to implement on Unix, is
hard to do on Win32. We must use a separate helper program to
change directory, close extra file descriptors, redirect the std
ones, as needed, and only then start the child process. No child
process pid can be returned, unfortunately. Or if we used
CreateProcess directly, it probably could. (Now we use the spawnv*
functions from msvcrt.)
* glib.def: Add new entry points.
* glib.def
* giowin32.c: Remove g_io_channel_win32_wait_for_condition(),
g_io_channel_win32_poll() subsumes it.
* gbacktrace.h: G_BREAKPOINT for MSVC (on the ix86).
* gwin32.c (g_win32_getlocale): Use "sp" for
LANG_CROATIAN+SUBLANG_SERBIAN_LATIN.
* makefile.{mingw,msc}.in (glib_OBJECTS): Add new files.
Add gspawn-win32-helper.exe rule.
* tests/makefile.{mingw,msc}.in (TESTS): Add shell-test and
spawn-test.
* tests/spawn-test.c: (run_tests): On Win32, don't try to run
/bin/sh, but ipconfig (no special significance in choosing that,
just a program that outputs something to stdout).
2000-10-16 18:54:29 +00:00
|
|
|
#ifdef HAVE_UNISTD_H
|
2000-10-09 16:24:57 +00:00
|
|
|
#include <unistd.h>
|
Some more debugging output. (g_io_channel_win32_poll): Remove unused vars.
2000-10-16 Tor Lillqvist <tml@iki.fi>
* giowin32.c (reader_thread): Some more debugging output.
(g_io_channel_win32_poll): Remove unused vars.
* gfileutils.c: Changes for Win32, with no unistd.h and no
S_ISLNK().
* gspawn-win32.c: Implementation of the g_spwan_* functions for
Win32. Due to the general non-Unixness of Win32, much of the
functionality that is relatively clean to implement on Unix, is
hard to do on Win32. We must use a separate helper program to
change directory, close extra file descriptors, redirect the std
ones, as needed, and only then start the child process. No child
process pid can be returned, unfortunately. Or if we used
CreateProcess directly, it probably could. (Now we use the spawnv*
functions from msvcrt.)
* glib.def: Add new entry points.
* glib.def
* giowin32.c: Remove g_io_channel_win32_wait_for_condition(),
g_io_channel_win32_poll() subsumes it.
* gbacktrace.h: G_BREAKPOINT for MSVC (on the ix86).
* gwin32.c (g_win32_getlocale): Use "sp" for
LANG_CROATIAN+SUBLANG_SERBIAN_LATIN.
* makefile.{mingw,msc}.in (glib_OBJECTS): Add new files.
Add gspawn-win32-helper.exe rule.
* tests/makefile.{mingw,msc}.in (TESTS): Add shell-test and
spawn-test.
* tests/spawn-test.c: (run_tests): On Win32, don't try to run
/bin/sh, but ipconfig (no special significance in choosing that,
just a program that outputs something to stdout).
2000-10-16 18:54:29 +00:00
|
|
|
#endif
|
2000-10-09 16:24:57 +00:00
|
|
|
#include <stdio.h>
|
2000-12-05 20:45:33 +00:00
|
|
|
#include <stdlib.h>
|
2001-09-10 13:30:41 +00:00
|
|
|
#include <stdarg.h>
|
2000-10-09 16:24:57 +00:00
|
|
|
#include <string.h>
|
|
|
|
#include <errno.h>
|
|
|
|
#include <sys/types.h>
|
|
|
|
#include <sys/stat.h>
|
|
|
|
#include <fcntl.h>
|
2000-11-21 15:27:44 +00:00
|
|
|
#include <stdlib.h>
|
2000-10-09 16:24:57 +00:00
|
|
|
|
2000-10-19 17:52:12 +00:00
|
|
|
#ifdef G_OS_WIN32
|
|
|
|
#include <io.h>
|
|
|
|
#ifndef F_OK
|
|
|
|
#define F_OK 0
|
|
|
|
#define X_OK 1
|
|
|
|
#define W_OK 2
|
|
|
|
#define R_OK 4
|
|
|
|
#endif /* !F_OK */
|
|
|
|
|
|
|
|
#ifndef S_ISREG
|
|
|
|
#define S_ISREG(mode) ((mode)&_S_IFREG)
|
|
|
|
#endif
|
|
|
|
|
|
|
|
#ifndef S_ISDIR
|
|
|
|
#define S_ISDIR(mode) ((mode)&_S_IFDIR)
|
|
|
|
#endif
|
|
|
|
|
|
|
|
#endif /* G_OS_WIN32 */
|
|
|
|
|
Some more debugging output. (g_io_channel_win32_poll): Remove unused vars.
2000-10-16 Tor Lillqvist <tml@iki.fi>
* giowin32.c (reader_thread): Some more debugging output.
(g_io_channel_win32_poll): Remove unused vars.
* gfileutils.c: Changes for Win32, with no unistd.h and no
S_ISLNK().
* gspawn-win32.c: Implementation of the g_spwan_* functions for
Win32. Due to the general non-Unixness of Win32, much of the
functionality that is relatively clean to implement on Unix, is
hard to do on Win32. We must use a separate helper program to
change directory, close extra file descriptors, redirect the std
ones, as needed, and only then start the child process. No child
process pid can be returned, unfortunately. Or if we used
CreateProcess directly, it probably could. (Now we use the spawnv*
functions from msvcrt.)
* glib.def: Add new entry points.
* glib.def
* giowin32.c: Remove g_io_channel_win32_wait_for_condition(),
g_io_channel_win32_poll() subsumes it.
* gbacktrace.h: G_BREAKPOINT for MSVC (on the ix86).
* gwin32.c (g_win32_getlocale): Use "sp" for
LANG_CROATIAN+SUBLANG_SERBIAN_LATIN.
* makefile.{mingw,msc}.in (glib_OBJECTS): Add new files.
Add gspawn-win32-helper.exe rule.
* tests/makefile.{mingw,msc}.in (TESTS): Add shell-test and
spawn-test.
* tests/spawn-test.c: (run_tests): On Win32, don't try to run
/bin/sh, but ipconfig (no special significance in choosing that,
just a program that outputs something to stdout).
2000-10-16 18:54:29 +00:00
|
|
|
#ifndef S_ISLNK
|
2000-10-19 17:52:12 +00:00
|
|
|
#define S_ISLNK(x) 0
|
Some more debugging output. (g_io_channel_win32_poll): Remove unused vars.
2000-10-16 Tor Lillqvist <tml@iki.fi>
* giowin32.c (reader_thread): Some more debugging output.
(g_io_channel_win32_poll): Remove unused vars.
* gfileutils.c: Changes for Win32, with no unistd.h and no
S_ISLNK().
* gspawn-win32.c: Implementation of the g_spwan_* functions for
Win32. Due to the general non-Unixness of Win32, much of the
functionality that is relatively clean to implement on Unix, is
hard to do on Win32. We must use a separate helper program to
change directory, close extra file descriptors, redirect the std
ones, as needed, and only then start the child process. No child
process pid can be returned, unfortunately. Or if we used
CreateProcess directly, it probably could. (Now we use the spawnv*
functions from msvcrt.)
* glib.def: Add new entry points.
* glib.def
* giowin32.c: Remove g_io_channel_win32_wait_for_condition(),
g_io_channel_win32_poll() subsumes it.
* gbacktrace.h: G_BREAKPOINT for MSVC (on the ix86).
* gwin32.c (g_win32_getlocale): Use "sp" for
LANG_CROATIAN+SUBLANG_SERBIAN_LATIN.
* makefile.{mingw,msc}.in (glib_OBJECTS): Add new files.
Add gspawn-win32-helper.exe rule.
* tests/makefile.{mingw,msc}.in (TESTS): Add shell-test and
spawn-test.
* tests/spawn-test.c: (run_tests): On Win32, don't try to run
/bin/sh, but ipconfig (no special significance in choosing that,
just a program that outputs something to stdout).
2000-10-16 18:54:29 +00:00
|
|
|
#endif
|
2000-10-09 16:24:57 +00:00
|
|
|
|
2000-10-31 05:51:24 +00:00
|
|
|
#ifndef O_BINARY
|
|
|
|
#define O_BINARY 0
|
|
|
|
#endif
|
|
|
|
|
2001-01-16 02:24:24 +00:00
|
|
|
#include "glibintl.h"
|
2000-10-09 16:24:57 +00:00
|
|
|
|
|
|
|
/**
|
|
|
|
* g_file_test:
|
|
|
|
* @filename: a filename to test
|
|
|
|
* @test: bitfield of #GFileTest flags
|
|
|
|
*
|
2001-12-16 19:31:36 +00:00
|
|
|
* Returns %TRUE if any of the tests in the bitfield @test are
|
|
|
|
* %TRUE. For example, <literal>(G_FILE_TEST_EXISTS |
|
|
|
|
* G_FILE_TEST_IS_DIR)</literal> will return %TRUE if the file exists;
|
|
|
|
* the check whether it's a directory doesn't matter since the existence
|
|
|
|
* test is %TRUE. With the current set of available tests, there's no point
|
2000-10-09 16:24:57 +00:00
|
|
|
* passing in more than one test at a time.
|
2002-02-07 20:14:00 +00:00
|
|
|
*
|
2002-12-09 04:52:52 +00:00
|
|
|
* Apart from %G_FILE_TEST_IS_SYMLINK all tests follow symbolic links,
|
2002-02-07 20:14:00 +00:00
|
|
|
* so for a symbolic link to a regular file g_file_test() will return
|
2002-12-09 04:52:52 +00:00
|
|
|
* %TRUE for both %G_FILE_TEST_IS_SYMLINK and %G_FILE_TEST_IS_REGULAR.
|
2000-10-09 16:24:57 +00:00
|
|
|
*
|
2002-02-08 08:25:49 +00:00
|
|
|
* Note, that for a dangling symbolic link g_file_test() will return
|
2002-12-09 04:52:52 +00:00
|
|
|
* %TRUE for %G_FILE_TEST_IS_SYMLINK and %FALSE for all other flags.
|
|
|
|
*
|
|
|
|
* You should never use g_file_test() to test whether it is safe
|
|
|
|
* to perform an operaton, because there is always the possibility
|
|
|
|
* of the condition changing before you actually perform the operation.
|
|
|
|
* For example, you might think you could use %G_FILE_TEST_IS_SYMLINK
|
|
|
|
* to know whether it is is safe to write to a file without being
|
|
|
|
* tricked into writing into a different location. It doesn't work!
|
|
|
|
*
|
|
|
|
* <informalexample><programlisting>
|
|
|
|
* /* DON'T DO THIS */
|
|
|
|
* if (!g_file_test (filename, G_FILE_TEST_IS_SYMLINK)) {
|
|
|
|
* fd = open (filename, O_WRONLY);
|
|
|
|
* /* write to fd */
|
|
|
|
* }
|
|
|
|
* </programlisting></informalexample>
|
|
|
|
*
|
|
|
|
* Another thing to note is that %G_FILE_TEST_EXISTS and
|
|
|
|
* %G_FILE_TEST_IS_EXECUTABLE are implemented using the access()
|
|
|
|
* system call. This usually doesn't matter, but if your program
|
|
|
|
* is setuid or setgid it means that these tests will give you
|
|
|
|
* the answer for the real user ID and group ID , rather than the
|
|
|
|
* effective user ID and group ID.
|
2002-02-08 08:25:49 +00:00
|
|
|
*
|
2001-12-16 19:31:36 +00:00
|
|
|
* Return value: whether a test was %TRUE
|
2000-10-09 16:24:57 +00:00
|
|
|
**/
|
|
|
|
gboolean
|
|
|
|
g_file_test (const gchar *filename,
|
|
|
|
GFileTest test)
|
|
|
|
{
|
2002-02-07 20:14:00 +00:00
|
|
|
if ((test & G_FILE_TEST_EXISTS) && (access (filename, F_OK) == 0))
|
|
|
|
return TRUE;
|
|
|
|
|
|
|
|
if ((test & G_FILE_TEST_IS_EXECUTABLE) && (access (filename, X_OK) == 0))
|
2002-12-09 04:52:52 +00:00
|
|
|
{
|
|
|
|
#ifndef G_OS_WIN32
|
|
|
|
if (getuid () != 0)
|
|
|
|
#endif
|
|
|
|
return TRUE;
|
|
|
|
|
|
|
|
/* For root, on some POSIX systems, access (filename, X_OK)
|
|
|
|
* will succeed even if no executable bits are set on the
|
|
|
|
* file. We fall through to a stat test to avoid that.
|
|
|
|
*/
|
|
|
|
}
|
|
|
|
else
|
|
|
|
test &= ~G_FILE_TEST_IS_EXECUTABLE;
|
2002-02-07 20:14:00 +00:00
|
|
|
|
|
|
|
if (test & G_FILE_TEST_IS_SYMLINK)
|
2000-10-09 16:24:57 +00:00
|
|
|
{
|
2002-02-10 16:42:36 +00:00
|
|
|
#ifdef G_OS_WIN32
|
|
|
|
/* no sym links on win32, no lstat in msvcrt */
|
|
|
|
#else
|
2000-10-09 16:24:57 +00:00
|
|
|
struct stat s;
|
|
|
|
|
2002-02-07 20:14:00 +00:00
|
|
|
if ((lstat (filename, &s) == 0) && S_ISLNK (s.st_mode))
|
2000-10-09 16:24:57 +00:00
|
|
|
return TRUE;
|
2002-02-10 16:42:36 +00:00
|
|
|
#endif
|
2000-10-09 16:24:57 +00:00
|
|
|
}
|
2002-02-07 20:14:00 +00:00
|
|
|
|
2002-12-09 04:52:52 +00:00
|
|
|
if (test & (G_FILE_TEST_IS_REGULAR |
|
|
|
|
G_FILE_TEST_IS_DIR |
|
|
|
|
G_FILE_TEST_IS_EXECUTABLE))
|
2002-02-07 20:14:00 +00:00
|
|
|
{
|
|
|
|
struct stat s;
|
|
|
|
|
|
|
|
if (stat (filename, &s) == 0)
|
|
|
|
{
|
|
|
|
if ((test & G_FILE_TEST_IS_REGULAR) && S_ISREG (s.st_mode))
|
|
|
|
return TRUE;
|
|
|
|
|
|
|
|
if ((test & G_FILE_TEST_IS_DIR) && S_ISDIR (s.st_mode))
|
|
|
|
return TRUE;
|
2002-12-09 04:52:52 +00:00
|
|
|
|
2002-12-11 23:40:10 +00:00
|
|
|
#ifndef G_OS_WIN32
|
2002-12-09 04:52:52 +00:00
|
|
|
/* The extra test for root when access (file, X_OK) succeeds.
|
2002-12-11 23:40:10 +00:00
|
|
|
* Probably only makes sense on Unix.
|
2002-12-09 04:52:52 +00:00
|
|
|
*/
|
|
|
|
if ((test & G_FILE_TEST_IS_EXECUTABLE) &&
|
|
|
|
((s.st_mode & S_IXOTH) ||
|
|
|
|
(s.st_mode & S_IXUSR) ||
|
|
|
|
(s.st_mode & S_IXGRP)))
|
|
|
|
return TRUE;
|
2002-12-11 23:40:10 +00:00
|
|
|
#endif
|
2002-02-07 20:14:00 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
return FALSE;
|
2000-10-09 16:24:57 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
GQuark
|
|
|
|
g_file_error_quark (void)
|
|
|
|
{
|
|
|
|
static GQuark q = 0;
|
|
|
|
if (q == 0)
|
|
|
|
q = g_quark_from_static_string ("g-file-error-quark");
|
|
|
|
|
|
|
|
return q;
|
|
|
|
}
|
|
|
|
|
2001-04-16 20:05:25 +00:00
|
|
|
/**
|
|
|
|
* g_file_error_from_errno:
|
|
|
|
* @err_no: an "errno" value
|
|
|
|
*
|
2001-12-16 19:31:36 +00:00
|
|
|
* Gets a #GFileError constant based on the passed-in @errno.
|
|
|
|
* For example, if you pass in %EEXIST this function returns
|
|
|
|
* #G_FILE_ERROR_EXIST. Unlike @errno values, you can portably
|
2001-04-16 20:05:25 +00:00
|
|
|
* assume that all #GFileError values will exist.
|
|
|
|
*
|
|
|
|
* Normally a #GFileError value goes into a #GError returned
|
|
|
|
* from a function that manipulates files. So you would use
|
|
|
|
* g_file_error_from_errno() when constructing a #GError.
|
|
|
|
*
|
2001-12-16 19:31:36 +00:00
|
|
|
* Return value: #GFileError corresponding to the given @errno
|
2001-04-16 20:05:25 +00:00
|
|
|
**/
|
2000-10-09 16:24:57 +00:00
|
|
|
GFileError
|
2001-04-16 20:05:25 +00:00
|
|
|
g_file_error_from_errno (gint err_no)
|
2000-10-09 16:24:57 +00:00
|
|
|
{
|
2001-04-16 20:05:25 +00:00
|
|
|
switch (err_no)
|
2000-10-09 16:24:57 +00:00
|
|
|
{
|
|
|
|
#ifdef EEXIST
|
|
|
|
case EEXIST:
|
|
|
|
return G_FILE_ERROR_EXIST;
|
|
|
|
break;
|
|
|
|
#endif
|
|
|
|
|
|
|
|
#ifdef EISDIR
|
|
|
|
case EISDIR:
|
|
|
|
return G_FILE_ERROR_ISDIR;
|
|
|
|
break;
|
|
|
|
#endif
|
|
|
|
|
|
|
|
#ifdef EACCES
|
|
|
|
case EACCES:
|
|
|
|
return G_FILE_ERROR_ACCES;
|
|
|
|
break;
|
|
|
|
#endif
|
|
|
|
|
|
|
|
#ifdef ENAMETOOLONG
|
|
|
|
case ENAMETOOLONG:
|
|
|
|
return G_FILE_ERROR_NAMETOOLONG;
|
|
|
|
break;
|
|
|
|
#endif
|
|
|
|
|
|
|
|
#ifdef ENOENT
|
|
|
|
case ENOENT:
|
|
|
|
return G_FILE_ERROR_NOENT;
|
|
|
|
break;
|
|
|
|
#endif
|
|
|
|
|
|
|
|
#ifdef ENOTDIR
|
|
|
|
case ENOTDIR:
|
|
|
|
return G_FILE_ERROR_NOTDIR;
|
|
|
|
break;
|
|
|
|
#endif
|
|
|
|
|
|
|
|
#ifdef ENXIO
|
|
|
|
case ENXIO:
|
|
|
|
return G_FILE_ERROR_NXIO;
|
|
|
|
break;
|
|
|
|
#endif
|
|
|
|
|
|
|
|
#ifdef ENODEV
|
|
|
|
case ENODEV:
|
|
|
|
return G_FILE_ERROR_NODEV;
|
|
|
|
break;
|
|
|
|
#endif
|
|
|
|
|
|
|
|
#ifdef EROFS
|
|
|
|
case EROFS:
|
|
|
|
return G_FILE_ERROR_ROFS;
|
|
|
|
break;
|
|
|
|
#endif
|
|
|
|
|
|
|
|
#ifdef ETXTBSY
|
|
|
|
case ETXTBSY:
|
|
|
|
return G_FILE_ERROR_TXTBSY;
|
|
|
|
break;
|
|
|
|
#endif
|
|
|
|
|
|
|
|
#ifdef EFAULT
|
|
|
|
case EFAULT:
|
|
|
|
return G_FILE_ERROR_FAULT;
|
|
|
|
break;
|
|
|
|
#endif
|
|
|
|
|
|
|
|
#ifdef ELOOP
|
|
|
|
case ELOOP:
|
|
|
|
return G_FILE_ERROR_LOOP;
|
|
|
|
break;
|
|
|
|
#endif
|
|
|
|
|
|
|
|
#ifdef ENOSPC
|
|
|
|
case ENOSPC:
|
|
|
|
return G_FILE_ERROR_NOSPC;
|
|
|
|
break;
|
|
|
|
#endif
|
|
|
|
|
|
|
|
#ifdef ENOMEM
|
|
|
|
case ENOMEM:
|
|
|
|
return G_FILE_ERROR_NOMEM;
|
|
|
|
break;
|
|
|
|
#endif
|
|
|
|
|
|
|
|
#ifdef EMFILE
|
|
|
|
case EMFILE:
|
|
|
|
return G_FILE_ERROR_MFILE;
|
|
|
|
break;
|
|
|
|
#endif
|
|
|
|
|
|
|
|
#ifdef ENFILE
|
|
|
|
case ENFILE:
|
|
|
|
return G_FILE_ERROR_NFILE;
|
|
|
|
break;
|
|
|
|
#endif
|
|
|
|
|
|
|
|
#ifdef EBADF
|
|
|
|
case EBADF:
|
|
|
|
return G_FILE_ERROR_BADF;
|
|
|
|
break;
|
|
|
|
#endif
|
|
|
|
|
|
|
|
#ifdef EINVAL
|
|
|
|
case EINVAL:
|
|
|
|
return G_FILE_ERROR_INVAL;
|
|
|
|
break;
|
|
|
|
#endif
|
|
|
|
|
|
|
|
#ifdef EPIPE
|
|
|
|
case EPIPE:
|
|
|
|
return G_FILE_ERROR_PIPE;
|
|
|
|
break;
|
|
|
|
#endif
|
|
|
|
|
|
|
|
#ifdef EAGAIN
|
|
|
|
case EAGAIN:
|
|
|
|
return G_FILE_ERROR_AGAIN;
|
|
|
|
break;
|
|
|
|
#endif
|
|
|
|
|
|
|
|
#ifdef EINTR
|
|
|
|
case EINTR:
|
|
|
|
return G_FILE_ERROR_INTR;
|
|
|
|
break;
|
|
|
|
#endif
|
|
|
|
|
|
|
|
#ifdef EIO
|
|
|
|
case EIO:
|
|
|
|
return G_FILE_ERROR_IO;
|
|
|
|
break;
|
|
|
|
#endif
|
|
|
|
|
|
|
|
#ifdef EPERM
|
|
|
|
case EPERM:
|
|
|
|
return G_FILE_ERROR_PERM;
|
|
|
|
break;
|
|
|
|
#endif
|
|
|
|
|
|
|
|
default:
|
|
|
|
return G_FILE_ERROR_FAILED;
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
static gboolean
|
|
|
|
get_contents_stdio (const gchar *filename,
|
|
|
|
FILE *f,
|
|
|
|
gchar **contents,
|
Changes for 64-bit cleanliness, loosely based on patch from Mark Murnane.
Wed Jun 20 12:00:54 2001 Owen Taylor <otaylor@redhat.com>
Changes for 64-bit cleanliness, loosely based on patch
from Mark Murnane.
* gconvert.c (g_convert/g_convert_with_fallback): Remove
workarounds for since-fixed GNU libc bugs. Minor
doc fix.
* gconvert.[ch]: Change gint to gsize/gssize as
appropriate.
* gconvert.c (g_locale/filename_to/from_utf8): Fix incorrect
computation of bytes_read / bytes_written.
* gfileutils.[ch] (g_file_get_contents): Make length
out parameter 'gsize *len'.
* ghook.c (g_hook_compare_ids): Don't compare a
and b as 'a - b'.
* gmacros.h (GSIZE_TO_POINTER): Add GPOINTER_TO_SIZE,
GSIZE_TO_POINTER.
* gmain.c (g_timeout_prepare): Rewrite to avoid
overflows. (Fixes bug when system clock skews
backwards more than 24 days.)
* gmarkup.[ch]: Make lengths passed to callbacks
gsize, length for g_markup_parse-context_parse(),
g_markup_escape_text() gssize.
* gmessages.[ch] (g_printf_string_upper_bound): Change
return value to gsize.
* gmessages.c (printf_string_upper_bound): Remove
a ridiculous use of 'inline' on a 300 line function.
* gstring.[ch]: Represent size of string as a gsize,
not gint. Make parameters to functions take gsize,
or gssize where -1 is allowed.
* gstring.c (g_string_erase): Make
g_string_erase (string, pos, -1) a synonym for
g_string_truncate for consistency with other G*
APIs.
* gstrfuncs.[ch]: Make all functions taking a string
length, take a gsize, or gssize if -1 is allowed.
(g_strstr_len, g_strrstr_len). Also fix some boundary
conditions in g_str[r]str[_len].
* gutf8.c tests/unicode-encoding.c: Make parameters that
are byte lengths gsize, gssize as appropriate. Make
character offsets, other counts, glong.
* gasyncqueue.c gcompletion.c
timeloop.c timeloop-basic.c gutils.c gspawn.c.
Small 64 bit cleanliness fixups.
* glist.c (g_list_sort2, g_list_sort_real): Fix functions
that should have been static.
* gdate.c (g_date_fill_parse_tokens): Fix extra
declaration that was shadowing another.
* tests/module-test.c: Include string.h
Mon Jun 18 15:43:29 2001 Owen Taylor <otaylor@redhat.com>
* gutf8.c (g_get_charset): Make argument
G_CONST_RETURN char **.
2001-06-23 13:55:09 +00:00
|
|
|
gsize *length,
|
2000-10-09 16:24:57 +00:00
|
|
|
GError **error)
|
|
|
|
{
|
|
|
|
gchar buf[2048];
|
|
|
|
size_t bytes;
|
2001-12-18 18:46:41 +00:00
|
|
|
char *str;
|
|
|
|
size_t total_bytes;
|
|
|
|
size_t total_allocated;
|
|
|
|
|
2000-10-09 16:24:57 +00:00
|
|
|
g_assert (f != NULL);
|
2001-12-18 18:46:41 +00:00
|
|
|
|
|
|
|
#define STARTING_ALLOC 64
|
2000-10-09 16:24:57 +00:00
|
|
|
|
2001-12-18 18:46:41 +00:00
|
|
|
total_bytes = 0;
|
|
|
|
total_allocated = STARTING_ALLOC;
|
|
|
|
str = g_malloc (STARTING_ALLOC);
|
2000-10-09 16:24:57 +00:00
|
|
|
|
|
|
|
while (!feof (f))
|
|
|
|
{
|
|
|
|
bytes = fread (buf, 1, 2048, f);
|
2001-12-18 18:46:41 +00:00
|
|
|
|
|
|
|
while ((total_bytes + bytes + 1) > total_allocated)
|
|
|
|
{
|
|
|
|
total_allocated *= 2;
|
|
|
|
str = g_try_realloc (str, total_allocated);
|
|
|
|
|
|
|
|
if (str == NULL)
|
|
|
|
{
|
|
|
|
g_set_error (error,
|
|
|
|
G_FILE_ERROR,
|
|
|
|
G_FILE_ERROR_NOMEM,
|
|
|
|
_("Could not allocate %lu bytes to read file \"%s\""),
|
|
|
|
(gulong) total_allocated, filename);
|
|
|
|
goto error;
|
|
|
|
}
|
|
|
|
}
|
2000-10-09 16:24:57 +00:00
|
|
|
|
|
|
|
if (ferror (f))
|
|
|
|
{
|
|
|
|
g_set_error (error,
|
|
|
|
G_FILE_ERROR,
|
|
|
|
g_file_error_from_errno (errno),
|
|
|
|
_("Error reading file '%s': %s"),
|
2002-03-08 15:50:54 +00:00
|
|
|
filename, g_strerror (errno));
|
2000-10-09 16:24:57 +00:00
|
|
|
|
2001-12-18 18:46:41 +00:00
|
|
|
goto error;
|
2000-10-09 16:24:57 +00:00
|
|
|
}
|
|
|
|
|
2001-12-18 18:46:41 +00:00
|
|
|
memcpy (str + total_bytes, buf, bytes);
|
|
|
|
total_bytes += bytes;
|
2000-10-09 16:24:57 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
fclose (f);
|
|
|
|
|
2001-12-18 18:46:41 +00:00
|
|
|
str[total_bytes] = '\0';
|
|
|
|
|
2000-10-09 16:24:57 +00:00
|
|
|
if (length)
|
2001-12-18 18:46:41 +00:00
|
|
|
*length = total_bytes;
|
2000-10-09 16:24:57 +00:00
|
|
|
|
2001-12-18 18:46:41 +00:00
|
|
|
*contents = str;
|
|
|
|
|
|
|
|
return TRUE;
|
|
|
|
|
|
|
|
error:
|
2000-10-09 16:24:57 +00:00
|
|
|
|
2001-12-18 18:46:41 +00:00
|
|
|
g_free (str);
|
|
|
|
fclose (f);
|
|
|
|
|
|
|
|
return FALSE;
|
2000-10-09 16:24:57 +00:00
|
|
|
}
|
|
|
|
|
Some more debugging output. (g_io_channel_win32_poll): Remove unused vars.
2000-10-16 Tor Lillqvist <tml@iki.fi>
* giowin32.c (reader_thread): Some more debugging output.
(g_io_channel_win32_poll): Remove unused vars.
* gfileutils.c: Changes for Win32, with no unistd.h and no
S_ISLNK().
* gspawn-win32.c: Implementation of the g_spwan_* functions for
Win32. Due to the general non-Unixness of Win32, much of the
functionality that is relatively clean to implement on Unix, is
hard to do on Win32. We must use a separate helper program to
change directory, close extra file descriptors, redirect the std
ones, as needed, and only then start the child process. No child
process pid can be returned, unfortunately. Or if we used
CreateProcess directly, it probably could. (Now we use the spawnv*
functions from msvcrt.)
* glib.def: Add new entry points.
* glib.def
* giowin32.c: Remove g_io_channel_win32_wait_for_condition(),
g_io_channel_win32_poll() subsumes it.
* gbacktrace.h: G_BREAKPOINT for MSVC (on the ix86).
* gwin32.c (g_win32_getlocale): Use "sp" for
LANG_CROATIAN+SUBLANG_SERBIAN_LATIN.
* makefile.{mingw,msc}.in (glib_OBJECTS): Add new files.
Add gspawn-win32-helper.exe rule.
* tests/makefile.{mingw,msc}.in (TESTS): Add shell-test and
spawn-test.
* tests/spawn-test.c: (run_tests): On Win32, don't try to run
/bin/sh, but ipconfig (no special significance in choosing that,
just a program that outputs something to stdout).
2000-10-16 18:54:29 +00:00
|
|
|
#ifndef G_OS_WIN32
|
|
|
|
|
2000-10-09 16:24:57 +00:00
|
|
|
static gboolean
|
|
|
|
get_contents_regfile (const gchar *filename,
|
|
|
|
struct stat *stat_buf,
|
|
|
|
gint fd,
|
|
|
|
gchar **contents,
|
Changes for 64-bit cleanliness, loosely based on patch from Mark Murnane.
Wed Jun 20 12:00:54 2001 Owen Taylor <otaylor@redhat.com>
Changes for 64-bit cleanliness, loosely based on patch
from Mark Murnane.
* gconvert.c (g_convert/g_convert_with_fallback): Remove
workarounds for since-fixed GNU libc bugs. Minor
doc fix.
* gconvert.[ch]: Change gint to gsize/gssize as
appropriate.
* gconvert.c (g_locale/filename_to/from_utf8): Fix incorrect
computation of bytes_read / bytes_written.
* gfileutils.[ch] (g_file_get_contents): Make length
out parameter 'gsize *len'.
* ghook.c (g_hook_compare_ids): Don't compare a
and b as 'a - b'.
* gmacros.h (GSIZE_TO_POINTER): Add GPOINTER_TO_SIZE,
GSIZE_TO_POINTER.
* gmain.c (g_timeout_prepare): Rewrite to avoid
overflows. (Fixes bug when system clock skews
backwards more than 24 days.)
* gmarkup.[ch]: Make lengths passed to callbacks
gsize, length for g_markup_parse-context_parse(),
g_markup_escape_text() gssize.
* gmessages.[ch] (g_printf_string_upper_bound): Change
return value to gsize.
* gmessages.c (printf_string_upper_bound): Remove
a ridiculous use of 'inline' on a 300 line function.
* gstring.[ch]: Represent size of string as a gsize,
not gint. Make parameters to functions take gsize,
or gssize where -1 is allowed.
* gstring.c (g_string_erase): Make
g_string_erase (string, pos, -1) a synonym for
g_string_truncate for consistency with other G*
APIs.
* gstrfuncs.[ch]: Make all functions taking a string
length, take a gsize, or gssize if -1 is allowed.
(g_strstr_len, g_strrstr_len). Also fix some boundary
conditions in g_str[r]str[_len].
* gutf8.c tests/unicode-encoding.c: Make parameters that
are byte lengths gsize, gssize as appropriate. Make
character offsets, other counts, glong.
* gasyncqueue.c gcompletion.c
timeloop.c timeloop-basic.c gutils.c gspawn.c.
Small 64 bit cleanliness fixups.
* glist.c (g_list_sort2, g_list_sort_real): Fix functions
that should have been static.
* gdate.c (g_date_fill_parse_tokens): Fix extra
declaration that was shadowing another.
* tests/module-test.c: Include string.h
Mon Jun 18 15:43:29 2001 Owen Taylor <otaylor@redhat.com>
* gutf8.c (g_get_charset): Make argument
G_CONST_RETURN char **.
2001-06-23 13:55:09 +00:00
|
|
|
gsize *length,
|
2000-10-09 16:24:57 +00:00
|
|
|
GError **error)
|
|
|
|
{
|
|
|
|
gchar *buf;
|
|
|
|
size_t bytes_read;
|
|
|
|
size_t size;
|
2001-12-18 18:46:41 +00:00
|
|
|
size_t alloc_size;
|
|
|
|
|
2000-10-09 16:24:57 +00:00
|
|
|
size = stat_buf->st_size;
|
|
|
|
|
2001-12-18 18:46:41 +00:00
|
|
|
alloc_size = size + 1;
|
|
|
|
buf = g_try_malloc (alloc_size);
|
|
|
|
|
|
|
|
if (buf == NULL)
|
|
|
|
{
|
|
|
|
g_set_error (error,
|
|
|
|
G_FILE_ERROR,
|
|
|
|
G_FILE_ERROR_NOMEM,
|
|
|
|
_("Could not allocate %lu bytes to read file \"%s\""),
|
|
|
|
(gulong) alloc_size, filename);
|
|
|
|
|
2002-03-20 16:21:58 +00:00
|
|
|
goto error;
|
2001-12-18 18:46:41 +00:00
|
|
|
}
|
|
|
|
|
2000-10-09 16:24:57 +00:00
|
|
|
bytes_read = 0;
|
|
|
|
while (bytes_read < size)
|
|
|
|
{
|
Changes for 64-bit cleanliness, loosely based on patch from Mark Murnane.
Wed Jun 20 12:00:54 2001 Owen Taylor <otaylor@redhat.com>
Changes for 64-bit cleanliness, loosely based on patch
from Mark Murnane.
* gconvert.c (g_convert/g_convert_with_fallback): Remove
workarounds for since-fixed GNU libc bugs. Minor
doc fix.
* gconvert.[ch]: Change gint to gsize/gssize as
appropriate.
* gconvert.c (g_locale/filename_to/from_utf8): Fix incorrect
computation of bytes_read / bytes_written.
* gfileutils.[ch] (g_file_get_contents): Make length
out parameter 'gsize *len'.
* ghook.c (g_hook_compare_ids): Don't compare a
and b as 'a - b'.
* gmacros.h (GSIZE_TO_POINTER): Add GPOINTER_TO_SIZE,
GSIZE_TO_POINTER.
* gmain.c (g_timeout_prepare): Rewrite to avoid
overflows. (Fixes bug when system clock skews
backwards more than 24 days.)
* gmarkup.[ch]: Make lengths passed to callbacks
gsize, length for g_markup_parse-context_parse(),
g_markup_escape_text() gssize.
* gmessages.[ch] (g_printf_string_upper_bound): Change
return value to gsize.
* gmessages.c (printf_string_upper_bound): Remove
a ridiculous use of 'inline' on a 300 line function.
* gstring.[ch]: Represent size of string as a gsize,
not gint. Make parameters to functions take gsize,
or gssize where -1 is allowed.
* gstring.c (g_string_erase): Make
g_string_erase (string, pos, -1) a synonym for
g_string_truncate for consistency with other G*
APIs.
* gstrfuncs.[ch]: Make all functions taking a string
length, take a gsize, or gssize if -1 is allowed.
(g_strstr_len, g_strrstr_len). Also fix some boundary
conditions in g_str[r]str[_len].
* gutf8.c tests/unicode-encoding.c: Make parameters that
are byte lengths gsize, gssize as appropriate. Make
character offsets, other counts, glong.
* gasyncqueue.c gcompletion.c
timeloop.c timeloop-basic.c gutils.c gspawn.c.
Small 64 bit cleanliness fixups.
* glist.c (g_list_sort2, g_list_sort_real): Fix functions
that should have been static.
* gdate.c (g_date_fill_parse_tokens): Fix extra
declaration that was shadowing another.
* tests/module-test.c: Include string.h
Mon Jun 18 15:43:29 2001 Owen Taylor <otaylor@redhat.com>
* gutf8.c (g_get_charset): Make argument
G_CONST_RETURN char **.
2001-06-23 13:55:09 +00:00
|
|
|
gssize rc;
|
2000-10-09 16:24:57 +00:00
|
|
|
|
|
|
|
rc = read (fd, buf + bytes_read, size - bytes_read);
|
|
|
|
|
|
|
|
if (rc < 0)
|
|
|
|
{
|
|
|
|
if (errno != EINTR)
|
|
|
|
{
|
|
|
|
g_free (buf);
|
|
|
|
|
|
|
|
g_set_error (error,
|
|
|
|
G_FILE_ERROR,
|
|
|
|
g_file_error_from_errno (errno),
|
|
|
|
_("Failed to read from file '%s': %s"),
|
2002-03-08 15:50:54 +00:00
|
|
|
filename, g_strerror (errno));
|
2000-10-09 16:24:57 +00:00
|
|
|
|
2002-03-20 16:21:58 +00:00
|
|
|
goto error;
|
2000-10-09 16:24:57 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
else if (rc == 0)
|
|
|
|
break;
|
|
|
|
else
|
|
|
|
bytes_read += rc;
|
|
|
|
}
|
|
|
|
|
|
|
|
buf[bytes_read] = '\0';
|
|
|
|
|
|
|
|
if (length)
|
|
|
|
*length = bytes_read;
|
|
|
|
|
|
|
|
*contents = buf;
|
|
|
|
|
2002-03-20 16:21:58 +00:00
|
|
|
close (fd);
|
|
|
|
|
2000-10-09 16:24:57 +00:00
|
|
|
return TRUE;
|
2002-03-20 16:21:58 +00:00
|
|
|
|
|
|
|
error:
|
|
|
|
|
|
|
|
close (fd);
|
|
|
|
|
|
|
|
return FALSE;
|
2000-10-09 16:24:57 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
static gboolean
|
|
|
|
get_contents_posix (const gchar *filename,
|
|
|
|
gchar **contents,
|
Changes for 64-bit cleanliness, loosely based on patch from Mark Murnane.
Wed Jun 20 12:00:54 2001 Owen Taylor <otaylor@redhat.com>
Changes for 64-bit cleanliness, loosely based on patch
from Mark Murnane.
* gconvert.c (g_convert/g_convert_with_fallback): Remove
workarounds for since-fixed GNU libc bugs. Minor
doc fix.
* gconvert.[ch]: Change gint to gsize/gssize as
appropriate.
* gconvert.c (g_locale/filename_to/from_utf8): Fix incorrect
computation of bytes_read / bytes_written.
* gfileutils.[ch] (g_file_get_contents): Make length
out parameter 'gsize *len'.
* ghook.c (g_hook_compare_ids): Don't compare a
and b as 'a - b'.
* gmacros.h (GSIZE_TO_POINTER): Add GPOINTER_TO_SIZE,
GSIZE_TO_POINTER.
* gmain.c (g_timeout_prepare): Rewrite to avoid
overflows. (Fixes bug when system clock skews
backwards more than 24 days.)
* gmarkup.[ch]: Make lengths passed to callbacks
gsize, length for g_markup_parse-context_parse(),
g_markup_escape_text() gssize.
* gmessages.[ch] (g_printf_string_upper_bound): Change
return value to gsize.
* gmessages.c (printf_string_upper_bound): Remove
a ridiculous use of 'inline' on a 300 line function.
* gstring.[ch]: Represent size of string as a gsize,
not gint. Make parameters to functions take gsize,
or gssize where -1 is allowed.
* gstring.c (g_string_erase): Make
g_string_erase (string, pos, -1) a synonym for
g_string_truncate for consistency with other G*
APIs.
* gstrfuncs.[ch]: Make all functions taking a string
length, take a gsize, or gssize if -1 is allowed.
(g_strstr_len, g_strrstr_len). Also fix some boundary
conditions in g_str[r]str[_len].
* gutf8.c tests/unicode-encoding.c: Make parameters that
are byte lengths gsize, gssize as appropriate. Make
character offsets, other counts, glong.
* gasyncqueue.c gcompletion.c
timeloop.c timeloop-basic.c gutils.c gspawn.c.
Small 64 bit cleanliness fixups.
* glist.c (g_list_sort2, g_list_sort_real): Fix functions
that should have been static.
* gdate.c (g_date_fill_parse_tokens): Fix extra
declaration that was shadowing another.
* tests/module-test.c: Include string.h
Mon Jun 18 15:43:29 2001 Owen Taylor <otaylor@redhat.com>
* gutf8.c (g_get_charset): Make argument
G_CONST_RETURN char **.
2001-06-23 13:55:09 +00:00
|
|
|
gsize *length,
|
2000-10-09 16:24:57 +00:00
|
|
|
GError **error)
|
|
|
|
{
|
|
|
|
struct stat stat_buf;
|
|
|
|
gint fd;
|
|
|
|
|
2001-03-09 21:18:33 +00:00
|
|
|
/* O_BINARY useful on Cygwin */
|
|
|
|
fd = open (filename, O_RDONLY|O_BINARY);
|
2000-10-09 16:24:57 +00:00
|
|
|
|
|
|
|
if (fd < 0)
|
|
|
|
{
|
|
|
|
g_set_error (error,
|
|
|
|
G_FILE_ERROR,
|
|
|
|
g_file_error_from_errno (errno),
|
|
|
|
_("Failed to open file '%s': %s"),
|
2002-03-08 15:50:54 +00:00
|
|
|
filename, g_strerror (errno));
|
2000-10-09 16:24:57 +00:00
|
|
|
|
|
|
|
return FALSE;
|
|
|
|
}
|
|
|
|
|
|
|
|
/* I don't think this will ever fail, aside from ENOMEM, but. */
|
|
|
|
if (fstat (fd, &stat_buf) < 0)
|
|
|
|
{
|
|
|
|
close (fd);
|
|
|
|
|
|
|
|
g_set_error (error,
|
|
|
|
G_FILE_ERROR,
|
|
|
|
g_file_error_from_errno (errno),
|
|
|
|
_("Failed to get attributes of file '%s': fstat() failed: %s"),
|
2002-03-08 15:50:54 +00:00
|
|
|
filename, g_strerror (errno));
|
2000-10-09 16:24:57 +00:00
|
|
|
|
|
|
|
return FALSE;
|
|
|
|
}
|
|
|
|
|
|
|
|
if (stat_buf.st_size > 0 && S_ISREG (stat_buf.st_mode))
|
|
|
|
{
|
|
|
|
return get_contents_regfile (filename,
|
|
|
|
&stat_buf,
|
|
|
|
fd,
|
|
|
|
contents,
|
|
|
|
length,
|
|
|
|
error);
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
FILE *f;
|
|
|
|
|
|
|
|
f = fdopen (fd, "r");
|
|
|
|
|
|
|
|
if (f == NULL)
|
|
|
|
{
|
|
|
|
g_set_error (error,
|
|
|
|
G_FILE_ERROR,
|
|
|
|
g_file_error_from_errno (errno),
|
|
|
|
_("Failed to open file '%s': fdopen() failed: %s"),
|
2002-03-08 15:50:54 +00:00
|
|
|
filename, g_strerror (errno));
|
2000-10-09 16:24:57 +00:00
|
|
|
|
|
|
|
return FALSE;
|
|
|
|
}
|
|
|
|
|
|
|
|
return get_contents_stdio (filename, f, contents, length, error);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
Some more debugging output. (g_io_channel_win32_poll): Remove unused vars.
2000-10-16 Tor Lillqvist <tml@iki.fi>
* giowin32.c (reader_thread): Some more debugging output.
(g_io_channel_win32_poll): Remove unused vars.
* gfileutils.c: Changes for Win32, with no unistd.h and no
S_ISLNK().
* gspawn-win32.c: Implementation of the g_spwan_* functions for
Win32. Due to the general non-Unixness of Win32, much of the
functionality that is relatively clean to implement on Unix, is
hard to do on Win32. We must use a separate helper program to
change directory, close extra file descriptors, redirect the std
ones, as needed, and only then start the child process. No child
process pid can be returned, unfortunately. Or if we used
CreateProcess directly, it probably could. (Now we use the spawnv*
functions from msvcrt.)
* glib.def: Add new entry points.
* glib.def
* giowin32.c: Remove g_io_channel_win32_wait_for_condition(),
g_io_channel_win32_poll() subsumes it.
* gbacktrace.h: G_BREAKPOINT for MSVC (on the ix86).
* gwin32.c (g_win32_getlocale): Use "sp" for
LANG_CROATIAN+SUBLANG_SERBIAN_LATIN.
* makefile.{mingw,msc}.in (glib_OBJECTS): Add new files.
Add gspawn-win32-helper.exe rule.
* tests/makefile.{mingw,msc}.in (TESTS): Add shell-test and
spawn-test.
* tests/spawn-test.c: (run_tests): On Win32, don't try to run
/bin/sh, but ipconfig (no special significance in choosing that,
just a program that outputs something to stdout).
2000-10-16 18:54:29 +00:00
|
|
|
#else /* G_OS_WIN32 */
|
|
|
|
|
2000-10-09 16:24:57 +00:00
|
|
|
static gboolean
|
|
|
|
get_contents_win32 (const gchar *filename,
|
|
|
|
gchar **contents,
|
Changes for 64-bit cleanliness, loosely based on patch from Mark Murnane.
Wed Jun 20 12:00:54 2001 Owen Taylor <otaylor@redhat.com>
Changes for 64-bit cleanliness, loosely based on patch
from Mark Murnane.
* gconvert.c (g_convert/g_convert_with_fallback): Remove
workarounds for since-fixed GNU libc bugs. Minor
doc fix.
* gconvert.[ch]: Change gint to gsize/gssize as
appropriate.
* gconvert.c (g_locale/filename_to/from_utf8): Fix incorrect
computation of bytes_read / bytes_written.
* gfileutils.[ch] (g_file_get_contents): Make length
out parameter 'gsize *len'.
* ghook.c (g_hook_compare_ids): Don't compare a
and b as 'a - b'.
* gmacros.h (GSIZE_TO_POINTER): Add GPOINTER_TO_SIZE,
GSIZE_TO_POINTER.
* gmain.c (g_timeout_prepare): Rewrite to avoid
overflows. (Fixes bug when system clock skews
backwards more than 24 days.)
* gmarkup.[ch]: Make lengths passed to callbacks
gsize, length for g_markup_parse-context_parse(),
g_markup_escape_text() gssize.
* gmessages.[ch] (g_printf_string_upper_bound): Change
return value to gsize.
* gmessages.c (printf_string_upper_bound): Remove
a ridiculous use of 'inline' on a 300 line function.
* gstring.[ch]: Represent size of string as a gsize,
not gint. Make parameters to functions take gsize,
or gssize where -1 is allowed.
* gstring.c (g_string_erase): Make
g_string_erase (string, pos, -1) a synonym for
g_string_truncate for consistency with other G*
APIs.
* gstrfuncs.[ch]: Make all functions taking a string
length, take a gsize, or gssize if -1 is allowed.
(g_strstr_len, g_strrstr_len). Also fix some boundary
conditions in g_str[r]str[_len].
* gutf8.c tests/unicode-encoding.c: Make parameters that
are byte lengths gsize, gssize as appropriate. Make
character offsets, other counts, glong.
* gasyncqueue.c gcompletion.c
timeloop.c timeloop-basic.c gutils.c gspawn.c.
Small 64 bit cleanliness fixups.
* glist.c (g_list_sort2, g_list_sort_real): Fix functions
that should have been static.
* gdate.c (g_date_fill_parse_tokens): Fix extra
declaration that was shadowing another.
* tests/module-test.c: Include string.h
Mon Jun 18 15:43:29 2001 Owen Taylor <otaylor@redhat.com>
* gutf8.c (g_get_charset): Make argument
G_CONST_RETURN char **.
2001-06-23 13:55:09 +00:00
|
|
|
gsize *length,
|
2000-10-09 16:24:57 +00:00
|
|
|
GError **error)
|
|
|
|
{
|
|
|
|
FILE *f;
|
|
|
|
|
|
|
|
/* I guess you want binary mode; maybe you want text sometimes? */
|
|
|
|
f = fopen (filename, "rb");
|
|
|
|
|
|
|
|
if (f == NULL)
|
|
|
|
{
|
|
|
|
g_set_error (error,
|
|
|
|
G_FILE_ERROR,
|
|
|
|
g_file_error_from_errno (errno),
|
|
|
|
_("Failed to open file '%s': %s"),
|
2002-03-08 15:50:54 +00:00
|
|
|
filename, g_strerror (errno));
|
2000-10-09 16:24:57 +00:00
|
|
|
|
|
|
|
return FALSE;
|
|
|
|
}
|
|
|
|
|
|
|
|
return get_contents_stdio (filename, f, contents, length, error);
|
|
|
|
}
|
Some more debugging output. (g_io_channel_win32_poll): Remove unused vars.
2000-10-16 Tor Lillqvist <tml@iki.fi>
* giowin32.c (reader_thread): Some more debugging output.
(g_io_channel_win32_poll): Remove unused vars.
* gfileutils.c: Changes for Win32, with no unistd.h and no
S_ISLNK().
* gspawn-win32.c: Implementation of the g_spwan_* functions for
Win32. Due to the general non-Unixness of Win32, much of the
functionality that is relatively clean to implement on Unix, is
hard to do on Win32. We must use a separate helper program to
change directory, close extra file descriptors, redirect the std
ones, as needed, and only then start the child process. No child
process pid can be returned, unfortunately. Or if we used
CreateProcess directly, it probably could. (Now we use the spawnv*
functions from msvcrt.)
* glib.def: Add new entry points.
* glib.def
* giowin32.c: Remove g_io_channel_win32_wait_for_condition(),
g_io_channel_win32_poll() subsumes it.
* gbacktrace.h: G_BREAKPOINT for MSVC (on the ix86).
* gwin32.c (g_win32_getlocale): Use "sp" for
LANG_CROATIAN+SUBLANG_SERBIAN_LATIN.
* makefile.{mingw,msc}.in (glib_OBJECTS): Add new files.
Add gspawn-win32-helper.exe rule.
* tests/makefile.{mingw,msc}.in (TESTS): Add shell-test and
spawn-test.
* tests/spawn-test.c: (run_tests): On Win32, don't try to run
/bin/sh, but ipconfig (no special significance in choosing that,
just a program that outputs something to stdout).
2000-10-16 18:54:29 +00:00
|
|
|
|
2000-10-09 16:24:57 +00:00
|
|
|
#endif
|
|
|
|
|
|
|
|
/**
|
|
|
|
* g_file_get_contents:
|
|
|
|
* @filename: a file to read contents from
|
|
|
|
* @contents: location to store an allocated string
|
|
|
|
* @length: location to store length in bytes of the contents
|
|
|
|
* @error: return location for a #GError
|
|
|
|
*
|
|
|
|
* Reads an entire file into allocated memory, with good error
|
2001-12-16 19:31:36 +00:00
|
|
|
* checking. If @error is set, %FALSE is returned, and @contents is set
|
|
|
|
* to %NULL. If %TRUE is returned, @error will not be set, and @contents
|
2000-10-09 16:24:57 +00:00
|
|
|
* will be set to the file contents. The string stored in @contents
|
2001-12-16 19:31:36 +00:00
|
|
|
* will be nul-terminated, so for text files you can pass %NULL for the
|
2000-10-09 16:24:57 +00:00
|
|
|
* @length argument. The error domain is #G_FILE_ERROR. Possible
|
|
|
|
* error codes are those in the #GFileError enumeration.
|
|
|
|
*
|
2001-12-16 19:31:36 +00:00
|
|
|
* Return value: %TRUE on success, %FALSE if error is set
|
2000-10-09 16:24:57 +00:00
|
|
|
**/
|
|
|
|
gboolean
|
|
|
|
g_file_get_contents (const gchar *filename,
|
|
|
|
gchar **contents,
|
Changes for 64-bit cleanliness, loosely based on patch from Mark Murnane.
Wed Jun 20 12:00:54 2001 Owen Taylor <otaylor@redhat.com>
Changes for 64-bit cleanliness, loosely based on patch
from Mark Murnane.
* gconvert.c (g_convert/g_convert_with_fallback): Remove
workarounds for since-fixed GNU libc bugs. Minor
doc fix.
* gconvert.[ch]: Change gint to gsize/gssize as
appropriate.
* gconvert.c (g_locale/filename_to/from_utf8): Fix incorrect
computation of bytes_read / bytes_written.
* gfileutils.[ch] (g_file_get_contents): Make length
out parameter 'gsize *len'.
* ghook.c (g_hook_compare_ids): Don't compare a
and b as 'a - b'.
* gmacros.h (GSIZE_TO_POINTER): Add GPOINTER_TO_SIZE,
GSIZE_TO_POINTER.
* gmain.c (g_timeout_prepare): Rewrite to avoid
overflows. (Fixes bug when system clock skews
backwards more than 24 days.)
* gmarkup.[ch]: Make lengths passed to callbacks
gsize, length for g_markup_parse-context_parse(),
g_markup_escape_text() gssize.
* gmessages.[ch] (g_printf_string_upper_bound): Change
return value to gsize.
* gmessages.c (printf_string_upper_bound): Remove
a ridiculous use of 'inline' on a 300 line function.
* gstring.[ch]: Represent size of string as a gsize,
not gint. Make parameters to functions take gsize,
or gssize where -1 is allowed.
* gstring.c (g_string_erase): Make
g_string_erase (string, pos, -1) a synonym for
g_string_truncate for consistency with other G*
APIs.
* gstrfuncs.[ch]: Make all functions taking a string
length, take a gsize, or gssize if -1 is allowed.
(g_strstr_len, g_strrstr_len). Also fix some boundary
conditions in g_str[r]str[_len].
* gutf8.c tests/unicode-encoding.c: Make parameters that
are byte lengths gsize, gssize as appropriate. Make
character offsets, other counts, glong.
* gasyncqueue.c gcompletion.c
timeloop.c timeloop-basic.c gutils.c gspawn.c.
Small 64 bit cleanliness fixups.
* glist.c (g_list_sort2, g_list_sort_real): Fix functions
that should have been static.
* gdate.c (g_date_fill_parse_tokens): Fix extra
declaration that was shadowing another.
* tests/module-test.c: Include string.h
Mon Jun 18 15:43:29 2001 Owen Taylor <otaylor@redhat.com>
* gutf8.c (g_get_charset): Make argument
G_CONST_RETURN char **.
2001-06-23 13:55:09 +00:00
|
|
|
gsize *length,
|
2000-10-09 16:24:57 +00:00
|
|
|
GError **error)
|
|
|
|
{
|
|
|
|
g_return_val_if_fail (filename != NULL, FALSE);
|
|
|
|
g_return_val_if_fail (contents != NULL, FALSE);
|
|
|
|
|
|
|
|
*contents = NULL;
|
|
|
|
if (length)
|
|
|
|
*length = 0;
|
|
|
|
|
|
|
|
#ifdef G_OS_WIN32
|
|
|
|
return get_contents_win32 (filename, contents, length, error);
|
|
|
|
#else
|
|
|
|
return get_contents_posix (filename, contents, length, error);
|
|
|
|
#endif
|
|
|
|
}
|
2000-10-30 22:06:22 +00:00
|
|
|
|
2000-11-05 16:38:16 +00:00
|
|
|
/*
|
|
|
|
* mkstemp() implementation is from the GNU C library.
|
|
|
|
* Copyright (C) 1991,92,93,94,95,96,97,98,99 Free Software Foundation, Inc.
|
|
|
|
*/
|
2000-10-30 22:06:22 +00:00
|
|
|
/**
|
|
|
|
* g_mkstemp:
|
2000-11-05 16:38:16 +00:00
|
|
|
* @tmpl: template filename
|
2000-10-30 22:06:22 +00:00
|
|
|
*
|
2003-07-25 21:32:47 +00:00
|
|
|
* Opens a temporary file. See the mkstemp() documentation
|
2001-12-16 19:31:36 +00:00
|
|
|
* on most UNIX-like systems. This is a portability wrapper, which simply calls
|
2003-07-25 21:32:47 +00:00
|
|
|
* mkstemp() on systems that have it, and implements
|
2001-12-16 19:31:36 +00:00
|
|
|
* it in GLib otherwise.
|
2000-10-30 22:06:22 +00:00
|
|
|
*
|
2001-12-16 19:31:36 +00:00
|
|
|
* The parameter is a string that should match the rules for
|
2003-07-25 21:32:47 +00:00
|
|
|
* mkstemp(), i.e. end in "XXXXXX". The X string will
|
2001-12-16 19:31:36 +00:00
|
|
|
* be modified to form the name of a file that didn't exist.
|
2000-10-30 22:06:22 +00:00
|
|
|
*
|
2003-07-25 21:32:47 +00:00
|
|
|
* Return value: A file handle (as from open()) to the file
|
2000-10-30 22:06:22 +00:00
|
|
|
* opened for reading and writing. The file is opened in binary mode
|
|
|
|
* on platforms where there is a difference. The file handle should be
|
2003-07-25 21:32:47 +00:00
|
|
|
* closed with close(). In case of errors, -1 is returned.
|
2000-10-30 22:06:22 +00:00
|
|
|
*/
|
|
|
|
int
|
|
|
|
g_mkstemp (char *tmpl)
|
|
|
|
{
|
|
|
|
#ifdef HAVE_MKSTEMP
|
|
|
|
return mkstemp (tmpl);
|
|
|
|
#else
|
|
|
|
int len;
|
|
|
|
char *XXXXXX;
|
|
|
|
int count, fd;
|
|
|
|
static const char letters[] =
|
2000-11-10 23:43:33 +00:00
|
|
|
"ABCDEFGHIJKLMNOPQRSTUVWXYZ0123456789";
|
|
|
|
static const int NLETTERS = sizeof (letters) - 1;
|
2000-10-30 22:06:22 +00:00
|
|
|
glong value;
|
|
|
|
GTimeVal tv;
|
2000-11-11 13:46:15 +00:00
|
|
|
static int counter = 0;
|
2000-10-30 22:06:22 +00:00
|
|
|
|
|
|
|
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);
|
2000-11-11 13:46:15 +00:00
|
|
|
value = (tv.tv_usec ^ tv.tv_sec) + counter++;
|
2000-10-30 22:06:22 +00:00
|
|
|
|
|
|
|
for (count = 0; count < 100; value += 7777, ++count)
|
|
|
|
{
|
|
|
|
glong v = value;
|
|
|
|
|
|
|
|
/* Fill in the random bits. */
|
2000-11-10 23:43:33 +00:00
|
|
|
XXXXXX[0] = letters[v % NLETTERS];
|
|
|
|
v /= NLETTERS;
|
|
|
|
XXXXXX[1] = letters[v % NLETTERS];
|
|
|
|
v /= NLETTERS;
|
|
|
|
XXXXXX[2] = letters[v % NLETTERS];
|
|
|
|
v /= NLETTERS;
|
|
|
|
XXXXXX[3] = letters[v % NLETTERS];
|
|
|
|
v /= NLETTERS;
|
|
|
|
XXXXXX[4] = letters[v % NLETTERS];
|
|
|
|
v /= NLETTERS;
|
|
|
|
XXXXXX[5] = letters[v % NLETTERS];
|
2000-10-30 22:06:22 +00:00
|
|
|
|
|
|
|
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
|
|
|
|
}
|
2000-11-10 23:43:33 +00:00
|
|
|
|
|
|
|
/**
|
|
|
|
* g_file_open_tmp:
|
2001-12-16 19:31:36 +00:00
|
|
|
* @tmpl: Template for file name, as in g_mkstemp(), basename only
|
2000-11-10 23:43:33 +00:00
|
|
|
* @name_used: location to store actual name used
|
|
|
|
* @error: return location for a #GError
|
|
|
|
*
|
|
|
|
* Opens a file for writing in the preferred directory for temporary
|
|
|
|
* files (as returned by g_get_tmp_dir()).
|
|
|
|
*
|
2000-11-12 20:38:19 +00:00
|
|
|
* @tmpl should be a string ending with six 'X' characters, as the
|
2003-07-25 21:32:47 +00:00
|
|
|
* parameter to g_mkstemp() (or mkstemp()).
|
2001-12-16 19:31:36 +00:00
|
|
|
* However, unlike these functions, the template should only be a
|
|
|
|
* basename, no directory components are allowed. If template is %NULL,
|
|
|
|
* a default template is used.
|
2000-11-10 23:43:33 +00:00
|
|
|
*
|
2003-07-25 21:32:47 +00:00
|
|
|
* Note that in contrast to g_mkstemp() (and mkstemp())
|
2001-12-16 19:31:36 +00:00
|
|
|
* @tmpl is not modified, and might thus be a read-only literal string.
|
2000-11-10 23:49:37 +00:00
|
|
|
*
|
2001-12-16 19:31:36 +00:00
|
|
|
* The actual name used is returned in @name_used if non-%NULL. This
|
|
|
|
* string should be freed with g_free() when not needed any longer.
|
2000-11-10 23:43:33 +00:00
|
|
|
*
|
2003-07-25 21:32:47 +00:00
|
|
|
* Return value: A file handle (as from open()) to
|
2001-12-16 19:31:36 +00:00
|
|
|
* the 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
|
2003-07-25 21:32:47 +00:00
|
|
|
* closed with close(). In case of errors, -1 is returned
|
2001-12-16 19:31:36 +00:00
|
|
|
* and @error will be set.
|
2000-11-10 23:43:33 +00:00
|
|
|
**/
|
|
|
|
int
|
2000-11-12 20:38:19 +00:00
|
|
|
g_file_open_tmp (const char *tmpl,
|
2000-11-10 23:43:33 +00:00
|
|
|
char **name_used,
|
|
|
|
GError **error)
|
|
|
|
{
|
|
|
|
int retval;
|
2001-02-17 23:30:48 +00:00
|
|
|
const char *tmpdir;
|
2000-11-10 23:43:33 +00:00
|
|
|
char *sep;
|
|
|
|
char *fulltemplate;
|
|
|
|
|
2000-11-12 20:38:19 +00:00
|
|
|
if (tmpl == NULL)
|
|
|
|
tmpl = ".XXXXXX";
|
2000-11-10 23:43:33 +00:00
|
|
|
|
2001-01-05 19:30:13 +00:00
|
|
|
if (strchr (tmpl, G_DIR_SEPARATOR)
|
|
|
|
#ifdef G_OS_WIN32
|
|
|
|
|| strchr (tmpl, '/')
|
|
|
|
#endif
|
|
|
|
)
|
2000-11-10 23:43:33 +00:00
|
|
|
{
|
|
|
|
g_set_error (error,
|
|
|
|
G_FILE_ERROR,
|
|
|
|
G_FILE_ERROR_FAILED,
|
2001-09-18 16:33:20 +00:00
|
|
|
_("Template '%s' invalid, should not contain a '%s'"),
|
2000-11-12 20:38:19 +00:00
|
|
|
tmpl, G_DIR_SEPARATOR_S);
|
2000-11-10 23:43:33 +00:00
|
|
|
|
|
|
|
return -1;
|
|
|
|
}
|
|
|
|
|
2000-11-12 20:38:19 +00:00
|
|
|
if (strlen (tmpl) < 6 ||
|
|
|
|
strcmp (tmpl + strlen (tmpl) - 6, "XXXXXX") != 0)
|
2000-11-10 23:43:33 +00:00
|
|
|
{
|
|
|
|
g_set_error (error,
|
|
|
|
G_FILE_ERROR,
|
|
|
|
G_FILE_ERROR_FAILED,
|
2001-01-19 08:32:46 +00:00
|
|
|
_("Template '%s' doesn't end with XXXXXX"),
|
2000-11-12 20:38:19 +00:00
|
|
|
tmpl);
|
2000-11-10 23:43:33 +00:00
|
|
|
return -1;
|
|
|
|
}
|
|
|
|
|
|
|
|
tmpdir = g_get_tmp_dir ();
|
|
|
|
|
|
|
|
if (tmpdir [strlen (tmpdir) - 1] == G_DIR_SEPARATOR)
|
|
|
|
sep = "";
|
|
|
|
else
|
|
|
|
sep = G_DIR_SEPARATOR_S;
|
|
|
|
|
2000-11-12 20:38:19 +00:00
|
|
|
fulltemplate = g_strconcat (tmpdir, sep, tmpl, NULL);
|
2000-11-10 23:43:33 +00:00
|
|
|
|
|
|
|
retval = g_mkstemp (fulltemplate);
|
|
|
|
|
|
|
|
if (retval == -1)
|
|
|
|
{
|
|
|
|
g_set_error (error,
|
|
|
|
G_FILE_ERROR,
|
|
|
|
g_file_error_from_errno (errno),
|
|
|
|
_("Failed to create file '%s': %s"),
|
2002-03-08 15:50:54 +00:00
|
|
|
fulltemplate, g_strerror (errno));
|
2000-11-10 23:43:33 +00:00
|
|
|
g_free (fulltemplate);
|
|
|
|
return -1;
|
|
|
|
}
|
|
|
|
|
|
|
|
if (name_used)
|
|
|
|
*name_used = fulltemplate;
|
|
|
|
else
|
|
|
|
g_free (fulltemplate);
|
|
|
|
|
|
|
|
return retval;
|
|
|
|
}
|
2001-09-10 13:30:41 +00:00
|
|
|
|
|
|
|
static gchar *
|
|
|
|
g_build_pathv (const gchar *separator,
|
|
|
|
const gchar *first_element,
|
|
|
|
va_list args)
|
|
|
|
{
|
|
|
|
GString *result;
|
|
|
|
gint separator_len = strlen (separator);
|
|
|
|
gboolean is_first = TRUE;
|
2002-07-26 19:59:07 +00:00
|
|
|
gboolean have_leading = FALSE;
|
|
|
|
const gchar *single_element = NULL;
|
2001-09-10 13:30:41 +00:00
|
|
|
const gchar *next_element;
|
2002-07-26 19:59:07 +00:00
|
|
|
const gchar *last_trailing = NULL;
|
2001-09-10 13:30:41 +00:00
|
|
|
|
|
|
|
result = g_string_new (NULL);
|
|
|
|
|
|
|
|
next_element = first_element;
|
|
|
|
|
|
|
|
while (TRUE)
|
|
|
|
{
|
|
|
|
const gchar *element;
|
|
|
|
const gchar *start;
|
|
|
|
const gchar *end;
|
|
|
|
|
|
|
|
if (next_element)
|
|
|
|
{
|
|
|
|
element = next_element;
|
|
|
|
next_element = va_arg (args, gchar *);
|
|
|
|
}
|
|
|
|
else
|
|
|
|
break;
|
|
|
|
|
2002-07-26 19:59:07 +00:00
|
|
|
/* Ignore empty elements */
|
|
|
|
if (!*element)
|
|
|
|
continue;
|
2001-09-10 13:30:41 +00:00
|
|
|
|
2002-07-26 19:59:07 +00:00
|
|
|
start = element;
|
|
|
|
|
|
|
|
if (separator_len)
|
2001-09-10 13:30:41 +00:00
|
|
|
{
|
|
|
|
while (start &&
|
|
|
|
strncmp (start, separator, separator_len) == 0)
|
|
|
|
start += separator_len;
|
2002-07-26 19:59:07 +00:00
|
|
|
}
|
2001-09-10 13:30:41 +00:00
|
|
|
|
|
|
|
end = start + strlen (start);
|
|
|
|
|
2002-07-26 19:59:07 +00:00
|
|
|
if (separator_len)
|
2001-09-10 13:30:41 +00:00
|
|
|
{
|
2002-07-26 19:59:07 +00:00
|
|
|
while (end >= start + separator_len &&
|
2001-09-10 13:30:41 +00:00
|
|
|
strncmp (end - separator_len, separator, separator_len) == 0)
|
|
|
|
end -= separator_len;
|
2002-07-26 19:59:07 +00:00
|
|
|
|
|
|
|
last_trailing = end;
|
|
|
|
while (last_trailing >= element + separator_len &&
|
|
|
|
strncmp (last_trailing - separator_len, separator, separator_len) == 0)
|
|
|
|
last_trailing -= separator_len;
|
|
|
|
|
|
|
|
if (!have_leading)
|
|
|
|
{
|
|
|
|
/* If the leading and trailing separator strings are in the
|
|
|
|
* same element and overlap, the result is exactly that element
|
|
|
|
*/
|
|
|
|
if (last_trailing <= start)
|
|
|
|
single_element = element;
|
|
|
|
|
|
|
|
g_string_append_len (result, element, start - element);
|
|
|
|
have_leading = TRUE;
|
|
|
|
}
|
|
|
|
else
|
|
|
|
single_element = NULL;
|
2001-09-10 13:30:41 +00:00
|
|
|
}
|
|
|
|
|
2002-07-26 19:59:07 +00:00
|
|
|
if (end == start)
|
|
|
|
continue;
|
2001-09-10 13:30:41 +00:00
|
|
|
|
2002-07-26 19:59:07 +00:00
|
|
|
if (!is_first)
|
|
|
|
g_string_append (result, separator);
|
|
|
|
|
|
|
|
g_string_append_len (result, start, end - start);
|
|
|
|
is_first = FALSE;
|
2001-09-10 13:30:41 +00:00
|
|
|
}
|
2002-07-26 19:59:07 +00:00
|
|
|
|
|
|
|
if (single_element)
|
|
|
|
{
|
|
|
|
g_string_free (result, TRUE);
|
|
|
|
return g_strdup (single_element);
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
if (last_trailing)
|
|
|
|
g_string_append (result, last_trailing);
|
2001-09-10 13:30:41 +00:00
|
|
|
|
2002-07-26 19:59:07 +00:00
|
|
|
return g_string_free (result, FALSE);
|
|
|
|
}
|
2001-09-10 13:30:41 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* g_build_path:
|
|
|
|
* @separator: a string used to separator the elements of the path.
|
|
|
|
* @first_element: the first element in the path
|
2002-11-25 21:43:35 +00:00
|
|
|
* @Varargs: remaining elements in path, terminated by %NULL
|
2001-09-10 13:30:41 +00:00
|
|
|
*
|
2001-12-16 19:31:36 +00:00
|
|
|
* Creates a path from a series of elements using @separator as the
|
2001-09-10 13:30:41 +00:00
|
|
|
* separator between elements. At the boundary between two elements,
|
|
|
|
* any trailing occurrences of separator in the first element, or
|
|
|
|
* leading occurrences of separator in the second element are removed
|
|
|
|
* and exactly one copy of the separator is inserted.
|
2002-07-26 19:59:07 +00:00
|
|
|
*
|
|
|
|
* Empty elements are ignored.
|
|
|
|
*
|
|
|
|
* The number of leading copies of the separator on the result is
|
|
|
|
* the same as the number of leading copies of the separator on
|
|
|
|
* the first non-empty element.
|
|
|
|
*
|
|
|
|
* The number of trailing copies of the separator on the result is
|
|
|
|
* the same as the number of trailing copies of the separator on
|
|
|
|
* the last non-empty element. (Determination of the number of
|
|
|
|
* trailing copies is done without stripping leading copies, so
|
|
|
|
* if the separator is <literal>ABA</literal>, <literal>ABABA</literal>
|
|
|
|
* has 1 trailing copy.)
|
|
|
|
*
|
|
|
|
* However, if there is only a single non-empty element, and there
|
|
|
|
* are no characters in that element not part of the leading or
|
|
|
|
* trailing separators, then the result is exactly the original value
|
|
|
|
* of that element.
|
|
|
|
*
|
|
|
|
* Other than for determination of the number of leading and trailing
|
|
|
|
* copies of the separator, elements consisting only of copies
|
|
|
|
* of the separator are ignored.
|
2001-09-10 13:30:41 +00:00
|
|
|
*
|
2001-12-16 19:31:36 +00:00
|
|
|
* Return value: a newly-allocated string that must be freed with g_free().
|
2001-09-10 13:30:41 +00:00
|
|
|
**/
|
|
|
|
gchar *
|
|
|
|
g_build_path (const gchar *separator,
|
|
|
|
const gchar *first_element,
|
|
|
|
...)
|
|
|
|
{
|
|
|
|
gchar *str;
|
|
|
|
va_list args;
|
|
|
|
|
|
|
|
g_return_val_if_fail (separator != NULL, NULL);
|
|
|
|
|
|
|
|
va_start (args, first_element);
|
|
|
|
str = g_build_pathv (separator, first_element, args);
|
|
|
|
va_end (args);
|
|
|
|
|
|
|
|
return str;
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* g_build_filename:
|
|
|
|
* @first_element: the first element in the path
|
2002-11-25 21:43:35 +00:00
|
|
|
* @Varargs: remaining elements in path, terminated by %NULL
|
2001-09-10 13:30:41 +00:00
|
|
|
*
|
2001-12-16 19:31:36 +00:00
|
|
|
* Creates a filename from a series of elements using the correct
|
2001-09-10 13:30:41 +00:00
|
|
|
* separator for filenames. This function behaves identically
|
2001-12-16 19:31:36 +00:00
|
|
|
* to <literal>g_build_path (G_DIR_SEPARATOR_S, first_element, ....)</literal>.
|
2001-09-10 13:30:41 +00:00
|
|
|
*
|
|
|
|
* No attempt is made to force the resulting filename to be an absolute
|
|
|
|
* path. If the first element is a relative path, the result will
|
|
|
|
* be a relative path.
|
|
|
|
*
|
2001-12-16 19:31:36 +00:00
|
|
|
* Return value: a newly-allocated string that must be freed with g_free().
|
2001-09-10 13:30:41 +00:00
|
|
|
**/
|
|
|
|
gchar *
|
|
|
|
g_build_filename (const gchar *first_element,
|
|
|
|
...)
|
|
|
|
{
|
|
|
|
gchar *str;
|
|
|
|
va_list args;
|
|
|
|
|
|
|
|
va_start (args, first_element);
|
|
|
|
str = g_build_pathv (G_DIR_SEPARATOR_S, first_element, args);
|
|
|
|
va_end (args);
|
|
|
|
|
|
|
|
return str;
|
|
|
|
}
|