Add new files.

2000-10-09  Havoc Pennington  <hp@redhat.com>

        * Makefile.am, tests/Makefile.am: Add new files.

	* tests/spawn-test.c, tests/shell-test.c: new tests for
	the shell/spawn stuff

	* gutils.c (g_find_program_in_path): convert a relative
	program name into an absolute pathname to an existing
	executable

	* gspawn.h, gspawn.c: New fork/exec API

	* gshell.h, gshell.c: Shell-related utilities, at the moment
	simply routines to parse argv and quote/unquote strings

	* guniprop.c (g_unichar_isspace): Return TRUE for the
	ASCII space characters isspace() returns TRUE for.

	* gfileutils.c (g_file_get_contents): Convenience function
	to slurp entire file into a string and return it. Partially
	written by Joel Becker.
	(g_file_test): file test function
This commit is contained in:
Havoc Pennington 2000-10-09 16:24:57 +00:00 committed by Havoc Pennington
parent 18e7dc0215
commit 8e3a3eef63
31 changed files with 6319 additions and 7 deletions

View File

@ -1,3 +1,27 @@
2000-10-09 Havoc Pennington <hp@redhat.com>
* Makefile.am, tests/Makefile.am: Add new files.
* tests/spawn-test.c, tests/shell-test.c: new tests for
the shell/spawn stuff
* gutils.c (g_find_program_in_path): convert a relative
program name into an absolute pathname to an existing
executable
* gspawn.h, gspawn.c: New fork/exec API
* gshell.h, gshell.c: Shell-related utilities, at the moment
simply routines to parse argv and quote/unquote strings
* guniprop.c (g_unichar_isspace): Return TRUE for the
ASCII space characters isspace() returns TRUE for.
* gfileutils.c (g_file_get_contents): Convenience function
to slurp entire file into a string and return it. Partially
written by Joel Becker.
(g_file_test): file test function
2000-10-06 Tor Lillqvist <tml@iki.fi>
* makefile.msc.in: Revamp to be like makefile.mingw.in, make

View File

@ -1,3 +1,27 @@
2000-10-09 Havoc Pennington <hp@redhat.com>
* Makefile.am, tests/Makefile.am: Add new files.
* tests/spawn-test.c, tests/shell-test.c: new tests for
the shell/spawn stuff
* gutils.c (g_find_program_in_path): convert a relative
program name into an absolute pathname to an existing
executable
* gspawn.h, gspawn.c: New fork/exec API
* gshell.h, gshell.c: Shell-related utilities, at the moment
simply routines to parse argv and quote/unquote strings
* guniprop.c (g_unichar_isspace): Return TRUE for the
ASCII space characters isspace() returns TRUE for.
* gfileutils.c (g_file_get_contents): Convenience function
to slurp entire file into a string and return it. Partially
written by Joel Becker.
(g_file_test): file test function
2000-10-06 Tor Lillqvist <tml@iki.fi>
* makefile.msc.in: Revamp to be like makefile.mingw.in, make

View File

@ -1,3 +1,27 @@
2000-10-09 Havoc Pennington <hp@redhat.com>
* Makefile.am, tests/Makefile.am: Add new files.
* tests/spawn-test.c, tests/shell-test.c: new tests for
the shell/spawn stuff
* gutils.c (g_find_program_in_path): convert a relative
program name into an absolute pathname to an existing
executable
* gspawn.h, gspawn.c: New fork/exec API
* gshell.h, gshell.c: Shell-related utilities, at the moment
simply routines to parse argv and quote/unquote strings
* guniprop.c (g_unichar_isspace): Return TRUE for the
ASCII space characters isspace() returns TRUE for.
* gfileutils.c (g_file_get_contents): Convenience function
to slurp entire file into a string and return it. Partially
written by Joel Becker.
(g_file_test): file test function
2000-10-06 Tor Lillqvist <tml@iki.fi>
* makefile.msc.in: Revamp to be like makefile.mingw.in, make

View File

@ -1,3 +1,27 @@
2000-10-09 Havoc Pennington <hp@redhat.com>
* Makefile.am, tests/Makefile.am: Add new files.
* tests/spawn-test.c, tests/shell-test.c: new tests for
the shell/spawn stuff
* gutils.c (g_find_program_in_path): convert a relative
program name into an absolute pathname to an existing
executable
* gspawn.h, gspawn.c: New fork/exec API
* gshell.h, gshell.c: Shell-related utilities, at the moment
simply routines to parse argv and quote/unquote strings
* guniprop.c (g_unichar_isspace): Return TRUE for the
ASCII space characters isspace() returns TRUE for.
* gfileutils.c (g_file_get_contents): Convenience function
to slurp entire file into a string and return it. Partially
written by Joel Becker.
(g_file_test): file test function
2000-10-06 Tor Lillqvist <tml@iki.fi>
* makefile.msc.in: Revamp to be like makefile.mingw.in, make

View File

@ -1,3 +1,27 @@
2000-10-09 Havoc Pennington <hp@redhat.com>
* Makefile.am, tests/Makefile.am: Add new files.
* tests/spawn-test.c, tests/shell-test.c: new tests for
the shell/spawn stuff
* gutils.c (g_find_program_in_path): convert a relative
program name into an absolute pathname to an existing
executable
* gspawn.h, gspawn.c: New fork/exec API
* gshell.h, gshell.c: Shell-related utilities, at the moment
simply routines to parse argv and quote/unquote strings
* guniprop.c (g_unichar_isspace): Return TRUE for the
ASCII space characters isspace() returns TRUE for.
* gfileutils.c (g_file_get_contents): Convenience function
to slurp entire file into a string and return it. Partially
written by Joel Becker.
(g_file_test): file test function
2000-10-06 Tor Lillqvist <tml@iki.fi>
* makefile.msc.in: Revamp to be like makefile.mingw.in, make

View File

@ -1,3 +1,27 @@
2000-10-09 Havoc Pennington <hp@redhat.com>
* Makefile.am, tests/Makefile.am: Add new files.
* tests/spawn-test.c, tests/shell-test.c: new tests for
the shell/spawn stuff
* gutils.c (g_find_program_in_path): convert a relative
program name into an absolute pathname to an existing
executable
* gspawn.h, gspawn.c: New fork/exec API
* gshell.h, gshell.c: Shell-related utilities, at the moment
simply routines to parse argv and quote/unquote strings
* guniprop.c (g_unichar_isspace): Return TRUE for the
ASCII space characters isspace() returns TRUE for.
* gfileutils.c (g_file_get_contents): Convenience function
to slurp entire file into a string and return it. Partially
written by Joel Becker.
(g_file_test): file test function
2000-10-06 Tor Lillqvist <tml@iki.fi>
* makefile.msc.in: Revamp to be like makefile.mingw.in, make

View File

@ -1,3 +1,27 @@
2000-10-09 Havoc Pennington <hp@redhat.com>
* Makefile.am, tests/Makefile.am: Add new files.
* tests/spawn-test.c, tests/shell-test.c: new tests for
the shell/spawn stuff
* gutils.c (g_find_program_in_path): convert a relative
program name into an absolute pathname to an existing
executable
* gspawn.h, gspawn.c: New fork/exec API
* gshell.h, gshell.c: Shell-related utilities, at the moment
simply routines to parse argv and quote/unquote strings
* guniprop.c (g_unichar_isspace): Return TRUE for the
ASCII space characters isspace() returns TRUE for.
* gfileutils.c (g_file_get_contents): Convenience function
to slurp entire file into a string and return it. Partially
written by Joel Becker.
(g_file_test): file test function
2000-10-06 Tor Lillqvist <tml@iki.fi>
* makefile.msc.in: Revamp to be like makefile.mingw.in, make

View File

@ -1,3 +1,27 @@
2000-10-09 Havoc Pennington <hp@redhat.com>
* Makefile.am, tests/Makefile.am: Add new files.
* tests/spawn-test.c, tests/shell-test.c: new tests for
the shell/spawn stuff
* gutils.c (g_find_program_in_path): convert a relative
program name into an absolute pathname to an existing
executable
* gspawn.h, gspawn.c: New fork/exec API
* gshell.h, gshell.c: Shell-related utilities, at the moment
simply routines to parse argv and quote/unquote strings
* guniprop.c (g_unichar_isspace): Return TRUE for the
ASCII space characters isspace() returns TRUE for.
* gfileutils.c (g_file_get_contents): Convenience function
to slurp entire file into a string and return it. Partially
written by Joel Becker.
(g_file_test): file test function
2000-10-06 Tor Lillqvist <tml@iki.fi>
* makefile.msc.in: Revamp to be like makefile.mingw.in, make

View File

@ -49,6 +49,7 @@ libglib_1_3_la_SOURCES = \
gdataset.c \
gdate.c \
gerror.c \
gfileutils.c \
ghash.c \
ghook.c \
giochannel.c \
@ -63,7 +64,9 @@ libglib_1_3_la_SOURCES = \
grel.c \
grand.c \
gscanner.c \
gshell.c \
gslist.c \
gspawn.c \
gstrfuncs.c \
gstring.c \
gthread.c \
@ -81,8 +84,11 @@ libglib_1_3_la_SOURCES = \
glibincludedir=$(includedir)/glib-2.0
glibinclude_HEADERS = \
gerror.h \
gfileutils.h \
glib.h \
glib-object.h \
gshell.h \
gspawn.h \
gunicode.h
configexecincludedir = $(libdir)/glib-2.0/include

470
gfileutils.c Normal file
View File

@ -0,0 +1,470 @@
/* 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.
*/
#include "glib.h"
#include <sys/stat.h>
#include <unistd.h>
#include <stdio.h>
#include <string.h>
#include <errno.h>
#include <sys/types.h>
#include <sys/stat.h>
#include <fcntl.h>
#define _(x) x
/**
* g_file_test:
* @filename: a filename to test
* @test: bitfield of #GFileTest flags
*
* Returns TRUE if any of the tests in the bitfield @test are
* TRUE. For example, (G_FILE_TEST_EXISTS | G_FILE_TEST_IS_DIR)
* 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
* passing in more than one test at a time.
*
* Return value: whether a test was TRUE
**/
gboolean
g_file_test (const gchar *filename,
GFileTest test)
{
if (test & G_FILE_TEST_EXISTS)
return (access (filename, F_OK) == 0);
else if (test & G_FILE_TEST_IS_EXECUTABLE)
return (access (filename, X_OK) == 0);
else
{
struct stat s;
if (stat (filename, &s) < 0)
return FALSE;
if ((test & G_FILE_TEST_IS_REGULAR) &&
S_ISREG (s.st_mode))
return TRUE;
else if ((test & G_FILE_TEST_IS_DIR) &&
S_ISDIR (s.st_mode))
return TRUE;
else if ((test & G_FILE_TEST_IS_SYMLINK) &&
S_ISLNK (s.st_mode))
return TRUE;
else
return FALSE;
}
}
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;
}
GFileError
g_file_error_from_errno (gint en)
{
switch (en)
{
#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,
guint *length,
GError **error)
{
gchar buf[2048];
size_t bytes;
GString *str;
g_assert (f != NULL);
str = g_string_new ("");
while (!feof (f))
{
bytes = fread (buf, 1, 2048, f);
if (ferror (f))
{
g_set_error (error,
G_FILE_ERROR,
g_file_error_from_errno (errno),
_("Error reading file '%s': %s"),
filename, strerror (errno));
g_string_free (str, TRUE);
return FALSE;
}
g_string_append_len (str, buf, bytes);
}
fclose (f);
if (length)
*length = str->len;
*contents = g_string_free (str, FALSE);
return TRUE;
}
static gboolean
get_contents_regfile (const gchar *filename,
struct stat *stat_buf,
gint fd,
gchar **contents,
guint *length,
GError **error)
{
gchar *buf;
size_t bytes_read;
size_t size;
size = stat_buf->st_size;
buf = g_new (gchar, size + 1);
bytes_read = 0;
while (bytes_read < size)
{
gint rc;
rc = read (fd, buf + bytes_read, size - bytes_read);
if (rc < 0)
{
if (errno != EINTR)
{
close (fd);
g_free (buf);
g_set_error (error,
G_FILE_ERROR,
g_file_error_from_errno (errno),
_("Failed to read from file '%s': %s"),
filename, strerror (errno));
return FALSE;
}
}
else if (rc == 0)
break;
else
bytes_read += rc;
}
buf[bytes_read] = '\0';
if (length)
*length = bytes_read;
*contents = buf;
return TRUE;
}
static gboolean
get_contents_posix (const gchar *filename,
gchar **contents,
guint *length,
GError **error)
{
struct stat stat_buf;
gint fd;
fd = open (filename, O_RDONLY);
if (fd < 0)
{
g_set_error (error,
G_FILE_ERROR,
g_file_error_from_errno (errno),
_("Failed to open file '%s': %s"),
filename, strerror (errno));
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"),
filename, strerror (errno));
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"),
filename, strerror (errno));
return FALSE;
}
return get_contents_stdio (filename, f, contents, length, error);
}
}
#ifdef G_OS_WIN32
static gboolean
get_contents_win32 (const gchar *filename,
gchar **contents,
guint *length,
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"),
filename, strerror (errno));
return FALSE;
}
return get_contents_stdio (filename, f, contents, length, error);
}
#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
* 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
* will be set to the file contents. The string stored in @contents
* will be nul-terminated, so for text files you can pass NULL for the
* @length argument. The error domain is #G_FILE_ERROR. Possible
* error codes are those in the #GFileError enumeration.
*
* FIXME currently crashes if the file is too big to fit in memory;
* should probably use g_try_malloc() when we have that function.
*
* Return value: TRUE on success, FALSE if error is set
**/
gboolean
g_file_get_contents (const gchar *filename,
gchar **contents,
guint *length,
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
}

91
gfileutils.h Normal file
View File

@ -0,0 +1,91 @@
/* gfileutils.h - 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.
*/
#ifndef __GFILEUTILS_H__
#define __GFILEUTILS_H__
#ifdef __cplusplus
extern "C"
{
#endif
#define G_FILE_ERROR g_file_error_quark ()
typedef enum
{
G_FILE_ERROR_EXIST,
G_FILE_ERROR_ISDIR,
G_FILE_ERROR_ACCES,
G_FILE_ERROR_NAMETOOLONG,
G_FILE_ERROR_NOENT,
G_FILE_ERROR_NOTDIR,
G_FILE_ERROR_NXIO,
G_FILE_ERROR_NODEV,
G_FILE_ERROR_ROFS,
G_FILE_ERROR_TXTBSY,
G_FILE_ERROR_FAULT,
G_FILE_ERROR_LOOP,
G_FILE_ERROR_NOSPC,
G_FILE_ERROR_NOMEM,
G_FILE_ERROR_MFILE,
G_FILE_ERROR_NFILE,
G_FILE_ERROR_BADF,
G_FILE_ERROR_INVAL,
G_FILE_ERROR_PIPE,
G_FILE_ERROR_AGAIN,
G_FILE_ERROR_INTR,
G_FILE_ERROR_IO,
G_FILE_ERROR_PERM,
G_FILE_ERROR_FAILED
} GFileError;
/* For backward-compat reasons, these are synced to an old
* anonymous enum in libgnome. But don't use that enum
* in new code.
*/
typedef enum
{
G_FILE_TEST_IS_REGULAR = 1 << 0,
G_FILE_TEST_IS_SYMLINK = 1 << 1,
G_FILE_TEST_IS_DIR = 1 << 2,
G_FILE_TEST_IS_EXECUTABLE = 1 << 3,
G_FILE_TEST_EXISTS = 1 << 4,
} GFileTest;
GQuark g_file_error_quark (void);
/* So other code can generate a GFileError */
GFileError g_file_error_from_errno (gint err_no);
gboolean g_file_test (const gchar *filename,
GFileTest test);
gboolean g_file_get_contents (const gchar *filename,
gchar **contents,
guint *length,
GError **error);
#ifdef __cplusplus
}
#endif
#endif /* __GFILEUTILS_H__ */

6
glib.h
View File

@ -1831,6 +1831,8 @@ gchar* g_getenv (const gchar *variable);
*/
void g_atexit (GVoidFunc func);
/* Look for an executable in PATH, following execvp() rules */
gchar* g_find_program_in_path (const gchar *program);
/* Bit tests
*/
@ -3506,5 +3508,9 @@ gchar* g_convert_with_fallback (const gchar *str,
#endif /* __cplusplus */
#include <gunicode.h>
#include <gerror.h>
#include <gshell.h>
#include <gspawn.h>
#include <gfileutils.h>
#endif /* __G_LIB_H__ */

View File

@ -49,6 +49,7 @@ libglib_1_3_la_SOURCES = \
gdataset.c \
gdate.c \
gerror.c \
gfileutils.c \
ghash.c \
ghook.c \
giochannel.c \
@ -63,7 +64,9 @@ libglib_1_3_la_SOURCES = \
grel.c \
grand.c \
gscanner.c \
gshell.c \
gslist.c \
gspawn.c \
gstrfuncs.c \
gstring.c \
gthread.c \
@ -81,8 +84,11 @@ libglib_1_3_la_SOURCES = \
glibincludedir=$(includedir)/glib-2.0
glibinclude_HEADERS = \
gerror.h \
gfileutils.h \
glib.h \
glib-object.h \
gshell.h \
gspawn.h \
gunicode.h
configexecincludedir = $(libdir)/glib-2.0/include

470
glib/gfileutils.c Normal file
View File

@ -0,0 +1,470 @@
/* 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.
*/
#include "glib.h"
#include <sys/stat.h>
#include <unistd.h>
#include <stdio.h>
#include <string.h>
#include <errno.h>
#include <sys/types.h>
#include <sys/stat.h>
#include <fcntl.h>
#define _(x) x
/**
* g_file_test:
* @filename: a filename to test
* @test: bitfield of #GFileTest flags
*
* Returns TRUE if any of the tests in the bitfield @test are
* TRUE. For example, (G_FILE_TEST_EXISTS | G_FILE_TEST_IS_DIR)
* 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
* passing in more than one test at a time.
*
* Return value: whether a test was TRUE
**/
gboolean
g_file_test (const gchar *filename,
GFileTest test)
{
if (test & G_FILE_TEST_EXISTS)
return (access (filename, F_OK) == 0);
else if (test & G_FILE_TEST_IS_EXECUTABLE)
return (access (filename, X_OK) == 0);
else
{
struct stat s;
if (stat (filename, &s) < 0)
return FALSE;
if ((test & G_FILE_TEST_IS_REGULAR) &&
S_ISREG (s.st_mode))
return TRUE;
else if ((test & G_FILE_TEST_IS_DIR) &&
S_ISDIR (s.st_mode))
return TRUE;
else if ((test & G_FILE_TEST_IS_SYMLINK) &&
S_ISLNK (s.st_mode))
return TRUE;
else
return FALSE;
}
}
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;
}
GFileError
g_file_error_from_errno (gint en)
{
switch (en)
{
#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,
guint *length,
GError **error)
{
gchar buf[2048];
size_t bytes;
GString *str;
g_assert (f != NULL);
str = g_string_new ("");
while (!feof (f))
{
bytes = fread (buf, 1, 2048, f);
if (ferror (f))
{
g_set_error (error,
G_FILE_ERROR,
g_file_error_from_errno (errno),
_("Error reading file '%s': %s"),
filename, strerror (errno));
g_string_free (str, TRUE);
return FALSE;
}
g_string_append_len (str, buf, bytes);
}
fclose (f);
if (length)
*length = str->len;
*contents = g_string_free (str, FALSE);
return TRUE;
}
static gboolean
get_contents_regfile (const gchar *filename,
struct stat *stat_buf,
gint fd,
gchar **contents,
guint *length,
GError **error)
{
gchar *buf;
size_t bytes_read;
size_t size;
size = stat_buf->st_size;
buf = g_new (gchar, size + 1);
bytes_read = 0;
while (bytes_read < size)
{
gint rc;
rc = read (fd, buf + bytes_read, size - bytes_read);
if (rc < 0)
{
if (errno != EINTR)
{
close (fd);
g_free (buf);
g_set_error (error,
G_FILE_ERROR,
g_file_error_from_errno (errno),
_("Failed to read from file '%s': %s"),
filename, strerror (errno));
return FALSE;
}
}
else if (rc == 0)
break;
else
bytes_read += rc;
}
buf[bytes_read] = '\0';
if (length)
*length = bytes_read;
*contents = buf;
return TRUE;
}
static gboolean
get_contents_posix (const gchar *filename,
gchar **contents,
guint *length,
GError **error)
{
struct stat stat_buf;
gint fd;
fd = open (filename, O_RDONLY);
if (fd < 0)
{
g_set_error (error,
G_FILE_ERROR,
g_file_error_from_errno (errno),
_("Failed to open file '%s': %s"),
filename, strerror (errno));
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"),
filename, strerror (errno));
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"),
filename, strerror (errno));
return FALSE;
}
return get_contents_stdio (filename, f, contents, length, error);
}
}
#ifdef G_OS_WIN32
static gboolean
get_contents_win32 (const gchar *filename,
gchar **contents,
guint *length,
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"),
filename, strerror (errno));
return FALSE;
}
return get_contents_stdio (filename, f, contents, length, error);
}
#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
* 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
* will be set to the file contents. The string stored in @contents
* will be nul-terminated, so for text files you can pass NULL for the
* @length argument. The error domain is #G_FILE_ERROR. Possible
* error codes are those in the #GFileError enumeration.
*
* FIXME currently crashes if the file is too big to fit in memory;
* should probably use g_try_malloc() when we have that function.
*
* Return value: TRUE on success, FALSE if error is set
**/
gboolean
g_file_get_contents (const gchar *filename,
gchar **contents,
guint *length,
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
}

91
glib/gfileutils.h Normal file
View File

@ -0,0 +1,91 @@
/* gfileutils.h - 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.
*/
#ifndef __GFILEUTILS_H__
#define __GFILEUTILS_H__
#ifdef __cplusplus
extern "C"
{
#endif
#define G_FILE_ERROR g_file_error_quark ()
typedef enum
{
G_FILE_ERROR_EXIST,
G_FILE_ERROR_ISDIR,
G_FILE_ERROR_ACCES,
G_FILE_ERROR_NAMETOOLONG,
G_FILE_ERROR_NOENT,
G_FILE_ERROR_NOTDIR,
G_FILE_ERROR_NXIO,
G_FILE_ERROR_NODEV,
G_FILE_ERROR_ROFS,
G_FILE_ERROR_TXTBSY,
G_FILE_ERROR_FAULT,
G_FILE_ERROR_LOOP,
G_FILE_ERROR_NOSPC,
G_FILE_ERROR_NOMEM,
G_FILE_ERROR_MFILE,
G_FILE_ERROR_NFILE,
G_FILE_ERROR_BADF,
G_FILE_ERROR_INVAL,
G_FILE_ERROR_PIPE,
G_FILE_ERROR_AGAIN,
G_FILE_ERROR_INTR,
G_FILE_ERROR_IO,
G_FILE_ERROR_PERM,
G_FILE_ERROR_FAILED
} GFileError;
/* For backward-compat reasons, these are synced to an old
* anonymous enum in libgnome. But don't use that enum
* in new code.
*/
typedef enum
{
G_FILE_TEST_IS_REGULAR = 1 << 0,
G_FILE_TEST_IS_SYMLINK = 1 << 1,
G_FILE_TEST_IS_DIR = 1 << 2,
G_FILE_TEST_IS_EXECUTABLE = 1 << 3,
G_FILE_TEST_EXISTS = 1 << 4,
} GFileTest;
GQuark g_file_error_quark (void);
/* So other code can generate a GFileError */
GFileError g_file_error_from_errno (gint err_no);
gboolean g_file_test (const gchar *filename,
GFileTest test);
gboolean g_file_get_contents (const gchar *filename,
gchar **contents,
guint *length,
GError **error);
#ifdef __cplusplus
}
#endif
#endif /* __GFILEUTILS_H__ */

View File

@ -1831,6 +1831,8 @@ gchar* g_getenv (const gchar *variable);
*/
void g_atexit (GVoidFunc func);
/* Look for an executable in PATH, following execvp() rules */
gchar* g_find_program_in_path (const gchar *program);
/* Bit tests
*/
@ -3506,5 +3508,9 @@ gchar* g_convert_with_fallback (const gchar *str,
#endif /* __cplusplus */
#include <gunicode.h>
#include <gerror.h>
#include <gshell.h>
#include <gspawn.h>
#include <gfileutils.h>
#endif /* __G_LIB_H__ */

651
glib/gshell.c Normal file
View File

@ -0,0 +1,651 @@
/* gshell.c - Shell-related utilities
*
* Copyright 2000 Red Hat, Inc.
* g_execvpe implementation based on GNU libc execvp:
* Copyright 1991, 92, 95, 96, 97, 98, 99 Free Software Foundation, 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.
*/
#include "glib.h"
#include <string.h>
#ifdef _
#warning "FIXME remove gettext hack"
#endif
#define _(x) x
GQuark
g_shell_error_quark (void)
{
static GQuark quark = 0;
if (quark == 0)
quark = g_quark_from_static_string ("g-shell-error-quark");
return quark;
}
/* Single quotes preserve the literal string exactly. escape
* sequences are not allowed; not even \' - if you want a '
* in the quoted text, you have to do something like 'foo'\''bar'
*
* Double quotes allow $ ` " \ and newline to be escaped with backslash.
* Otherwise double quotes preserve things literally.
*/
gboolean
unquote_string_inplace (gchar* str, gchar** end, GError** err)
{
gchar* dest;
gchar* s;
gchar quote_char;
g_return_val_if_fail(end != NULL, FALSE);
g_return_val_if_fail(err == NULL || *err == NULL, FALSE);
g_return_val_if_fail(str != NULL, FALSE);
dest = s = str;
quote_char = *s;
if (!(*s == '"' || *s == '\''))
{
if (err)
*err = g_error_new(G_SHELL_ERROR,
G_SHELL_ERROR_BAD_QUOTING,
_("Quoted text doesn't begin with a quotation mark"));
*end = str;
return FALSE;
}
/* Skip the initial quote mark */
++s;
if (quote_char == '"')
{
while (*s)
{
g_assert(s > dest); /* loop invariant */
switch (*s)
{
case '"':
/* End of the string, return now */
*dest = '\0';
++s;
*end = s;
return TRUE;
break;
case '\\':
/* Possible escaped quote or \ */
++s;
switch (*s)
{
case '"':
case '\\':
case '`':
case '$':
case '\n':
*dest = *s;
++s;
++dest;
break;
default:
/* not an escaped char */
*dest = '\\';
++dest;
/* ++s already done. */
break;
}
break;
default:
*dest = *s;
++dest;
++s;
break;
}
g_assert(s > dest); /* loop invariant */
}
}
else
{
while (*s)
{
g_assert(s > dest); /* loop invariant */
if (*s == '\'')
{
/* End of the string, return now */
*dest = '\0';
++s;
*end = s;
return TRUE;
}
else
{
*dest = *s;
++dest;
++s;
}
g_assert(s > dest); /* loop invariant */
}
}
/* If we reach here this means the close quote was never encountered */
*dest = '\0';
if (err)
*err = g_error_new(G_SHELL_ERROR,
G_SHELL_ERROR_BAD_QUOTING,
_("Unmatched quotation mark in command line or other shell-quoted text"));
*end = s;
return FALSE;
}
/**
* g_shell_quote:
* @unquoted_string: a literal string
*
* Quotes a string so that the shell (/bin/sh) will interpret the
* quoted string to mean @unquoted_string. If you pass a filename to
* the shell, for example, you should first quote it with this
* function. The return value must be freed with g_free(). The
* quoting style used is undefined (single or double quotes may be
* used).
*
* Return value: quoted string
**/
gchar*
g_shell_quote (const gchar *unquoted_string)
{
/* We always use single quotes, because the algorithm is cheesier.
* We could use double if we felt like it, that might be more
* human-readable.
*/
const gchar *p;
GString *dest;
g_return_val_if_fail (unquoted_string != NULL, NULL);
dest = g_string_new ("'");
p = unquoted_string;
/* could speed this up a lot by appending chunks of text at a
* time.
*/
while (*p)
{
/* Replace literal ' with a close ', a \', and a open ' */
if (*p == '\'')
g_string_append (dest, "'\''");
else
g_string_append_c (dest, *p);
++p;
}
/* close the quote */
g_string_append_c (dest, '\'');
return g_string_free (dest, FALSE);
}
/**
* g_shell_unquote:
* @quoted_string: shell-quoted string
* @error: error return location or NULL
*
* Unquotes a string as the shell (/bin/sh) would. Only handles
* quotes; if a string contains file globs, arithmetic operators,
* variables, backticks, redirections, or other special-to-the-shell
* features, the result will be different from the result a real shell
* would produce (the variables, backticks, etc. will be passed
* through literally instead of being expanded). This function is
* guaranteed to succeed if applied to the result of
* g_shell_quote(). If it fails, it returns NULL and sets the
* error. The @quoted_string need not actually contain quoted or
* escaped text; g_shell_unquote() simply goes through the string and
* unquotes/unescapes anything that the shell would. Both single and
* double quotes are handled, as are escapes including escaped
* newlines. The return value must be freed with g_free(). Possible
* errors are in the #G_SHELL_ERROR domain.
*
* Return value: an unquoted string
**/
gchar*
g_shell_unquote (const gchar *quoted_string,
GError **error)
{
gchar *unquoted;
gchar *end;
gchar *start;
GString *retval;
g_return_val_if_fail (quoted_string != NULL, NULL);
unquoted = g_strdup (quoted_string);
start = unquoted;
end = unquoted;
retval = g_string_new ("");
/* The loop allows cases such as
* "foo"blah blah'bar'woo foo"baz"la la la\'\''foo'
*/
while (*start)
{
/* Append all non-quoted chars, honoring backslash escape
*/
while (*start && !(*start == '"' || *start == '\''))
{
if (*start == '\\')
{
/* all characters can get escaped by backslash,
* except newline, which is removed if it follows
* a backslash outside of quotes
*/
++start;
if (*start)
{
if (*start != '\n')
g_string_append_c (retval, *start);
++start;
}
}
else
{
g_string_append_c (retval, *start);
++start;
}
}
if (*start)
{
if (!unquote_string_inplace (start, &end, error))
{
goto error;
}
else
{
g_string_append (retval, start);
start = end;
}
}
}
return g_string_free (retval, FALSE);
error:
g_assert (error == NULL || *error != NULL);
g_free (unquoted);
g_string_free (retval, TRUE);
return NULL;
}
/* g_parse_argv() does a semi-arbitrary weird subset of the way
* the shell parses a command line. We don't do variable expansion,
* don't understand that operators are tokens, don't do tilde expansion,
* don't do command substitution, no arithmetic expansion, IFS gets ignored,
* don't do filename globs, don't remove redirection stuff, etc.
*
* READ THE UNIX98 SPEC on "Shell Command Language" before changing
* the behavior of this code.
*
* Steps to parsing the argv string:
*
* - tokenize the string (but since we ignore operators,
* our tokenization may diverge from what the shell would do)
* note that tokenization ignores the internals of a quoted
* word and it always splits on spaces, not on IFS even
* if we used IFS. We also ignore "end of input indicator"
* (I guess this is control-D?)
*
* Tokenization steps, from UNIX98 with operator stuff removed,
* are:
*
* 1) "If the current character is backslash, single-quote or
* double-quote (\, ' or ") and it is not quoted, it will affect
* quoting for subsequent characters up to the end of the quoted
* text. The rules for quoting are as described in Quoting
* . During token recognition no substitutions will be actually
* performed, and the result token will contain exactly the
* characters that appear in the input (except for newline
* character joining), unmodified, including any embedded or
* enclosing quotes or substitution operators, between the quote
* mark and the end of the quoted text. The token will not be
* delimited by the end of the quoted field."
*
* 2) "If the current character is an unquoted newline character,
* the current token will be delimited."
*
* 3) "If the current character is an unquoted blank character, any
* token containing the previous character is delimited and the
* current character will be discarded."
*
* 4) "If the previous character was part of a word, the current
* character will be appended to that word."
*
* 5) "If the current character is a "#", it and all subsequent
* characters up to, but excluding, the next newline character
* will be discarded as a comment. The newline character that
* ends the line is not considered part of the comment. The
* "#" starts a comment only when it is at the beginning of a
* token. Since the search for the end-of-comment does not
* consider an escaped newline character specially, a comment
* cannot be continued to the next line."
*
* 6) "The current character will be used as the start of a new word."
*
*
* - for each token (word), perform portions of word expansion, namely
* field splitting (using default whitespace IFS) and quote
* removal. Field splitting may increase the number of words.
* Quote removal does not increase the number of words.
*
* "If the complete expansion appropriate for a word results in an
* empty field, that empty field will be deleted from the list of
* fields that form the completely expanded command, unless the
* original word contained single-quote or double-quote characters."
* - UNIX98 spec
*
*
*/
static inline void
ensure_token (GString **token)
{
if (*token == NULL)
*token = g_string_new ("");
}
static void
delimit_token (GString **token,
GSList **retval)
{
if (*token == NULL)
return;
*retval = g_slist_prepend (*retval, g_string_free (*token, FALSE));
*token = NULL;
}
static GSList*
tokenize_command_line (const gchar *command_line,
GError **error)
{
gchar current_quote;
const gchar *p;
GString *current_token = NULL;
GSList *retval = NULL;
current_quote = '\0';
p = command_line;
while (*p)
{
if (current_quote == '\\')
{
if (*p == '\n')
{
/* we append nothing; backslash-newline become nothing */
}
else
{
/* we append the backslash and the current char,
* to be interpreted later after tokenization
*/
ensure_token (&current_token);
g_string_append_c (current_token, '\\');
g_string_append_c (current_token, *p);
}
current_quote = '\0';
}
else if (current_quote == '#')
{
/* Discard up to and including next newline */
while (*p && *p != '\n')
++p;
current_quote = '\0';
if (*p == '\0')
break;
}
else if (current_quote)
{
if (*p == current_quote &&
/* check that it isn't an escaped double quote */
!(current_quote == '"' && p != command_line && *(p - 1) == '\\'))
{
/* close the quote */
current_quote = '\0';
}
/* Everything inside quotes, and the close quote,
* gets appended literally.
*/
ensure_token (&current_token);
g_string_append_c (current_token, *p);
}
else
{
switch (*p)
{
case '\n':
delimit_token (&current_token, &retval);
break;
case ' ':
case '\t':
/* If the current token contains the previous char, delimit
* the current token. A nonzero length
* token should always contain the previous char.
*/
if (current_token &&
current_token->len > 0)
{
delimit_token (&current_token, &retval);
}
/* discard all unquoted blanks (don't add them to a token) */
break;
/* single/double quotes are appended to the token,
* escapes are maybe appended next time through the loop,
* comment chars are never appended.
*/
case '\'':
case '"':
ensure_token (&current_token);
g_string_append_c (current_token, *p);
/* FALL THRU */
case '#':
case '\\':
current_quote = *p;
break;
default:
/* Combines rules 4) and 6) - if we have a token, append to it,
* otherwise create a new token.
*/
ensure_token (&current_token);
g_string_append_c (current_token, *p);
break;
}
}
++p;
}
delimit_token (&current_token, &retval);
if (current_quote)
{
if (current_quote == '\\')
g_set_error (error,
G_SHELL_ERROR,
G_SHELL_ERROR_BAD_QUOTING,
_("Text ended just after a '\' character."
" (The text was '%s')"),
command_line);
else
g_set_error (error,
G_SHELL_ERROR,
G_SHELL_ERROR_BAD_QUOTING,
_("Text ended before matching quote was found for %c."
" (The text was '%s')"),
current_quote, command_line);
goto error;
}
if (retval == NULL)
{
g_set_error (error,
G_SHELL_ERROR,
G_SHELL_ERROR_EMPTY_STRING,
_("Text was empty (or contained only whitespace)"));
goto error;
}
/* we appended backward */
retval = g_slist_reverse (retval);
return retval;
error:
g_assert (error == NULL || *error != NULL);
if (retval)
{
g_slist_foreach (retval, (GFunc)g_free, NULL);
g_slist_free (retval);
}
return NULL;
}
/**
* g_shell_parse_argv:
* @command_line: command line to parse
* @argcp: return location for number of args
* @argvp: return location for array of args
* @error: return location for error
*
* Parses a command line into an argument vector, in much the same way
* the shell would, but without many of the expansions the shell would
* perform (variable expansion, globs, operators, filename expansion,
* etc. are not supported). The results are defined to be the same as
* those you would get from a UNIX98 /bin/sh, as long as the input
* contains none of the unsupported shell expansions. If the input
* does contain such expansions, they are passed through
* literally. Possible errors are those from the #G_SHELL_ERROR
* domain.
*
* Return value: TRUE on success, FALSE if error set
**/
gboolean
g_shell_parse_argv (const gchar *command_line,
gint *argcp,
gchar ***argvp,
GError **error)
{
/* Code based on poptParseArgvString() from libpopt */
gint argc = 0;
gchar **argv = NULL;
GSList *tokens = NULL;
gint i;
GSList *tmp_list;
g_return_val_if_fail (command_line != NULL, FALSE);
tokens = tokenize_command_line (command_line, error);
if (tokens == NULL)
return FALSE;
/* Because we can't have introduced any new blank space into the
* tokens (we didn't do any new expansions), we don't need to
* perform field splitting. If we were going to honor IFS or do any
* expansions, we would have to do field splitting on each word
* here. Also, if we were going to do any expansion we would need to
* remove any zero-length words that didn't contain quotes
* originally; but since there's no expansion we know all words have
* nonzero length, unless they contain quotes.
*
* So, we simply remove quotes, and don't do any field splitting or
* empty word removal, since we know there was no way to introduce
* such things.
*/
argc = g_slist_length (tokens);
argv = g_new0 (gchar*, argc + 1);
i = 0;
tmp_list = tokens;
while (tmp_list)
{
argv[i] = g_shell_unquote (tmp_list->data, error);
/* Since we already checked that quotes matched up in the
* tokenizer, this shouldn't be possible to reach I guess.
*/
if (argv[i] == NULL)
goto failed;
tmp_list = g_slist_next (tmp_list);
++i;
}
g_slist_foreach (tokens, (GFunc)g_free, NULL);
g_slist_free (tokens);
if (argcp)
*argcp = argc;
if (argvp)
*argvp = argv;
else
g_strfreev (argv);
return TRUE;
failed:
g_assert (error == NULL || *error != NULL);
g_strfreev (argv);
g_slist_foreach (tokens, (GFunc) g_free, NULL);
g_slist_free (tokens);
return FALSE;
}

59
glib/gshell.h Normal file
View File

@ -0,0 +1,59 @@
/* gshell.h - Shell-related utilities
*
* 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.
*/
#ifndef __GSHELL_H__
#define __GSHELL_H__
#include <gerror.h>
#ifdef __cplusplus
extern "C"
{
#endif
#define G_SHELL_ERROR g_shell_error_quark ()
typedef enum
{
/* mismatched or otherwise mangled quoting */
G_SHELL_ERROR_BAD_QUOTING,
/* string to be parsed was empty */
G_SHELL_ERROR_EMPTY_STRING,
G_SHELL_ERROR_FAILED
} GShellError;
GQuark g_shell_error_quark (void);
gchar* g_shell_quote (const gchar *unquoted_string);
gchar* g_shell_unquote (const gchar *quoted_string,
GError **error);
gboolean g_shell_parse_argv (const gchar *command_line,
gint *argc,
gchar ***argv,
GError **error);
#ifdef __cplusplus
}
#endif
#endif /* __GSHELL_H__ */

1392
glib/gspawn.c Normal file

File diff suppressed because it is too large Load Diff

132
glib/gspawn.h Normal file
View File

@ -0,0 +1,132 @@
/* gspawn.h - Process launching
*
* 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.
*/
#ifndef __GSPAWN_H__
#define __GSPAWN_H__
#include <gerror.h>
#ifdef __cplusplus
extern "C"
{
#endif
/* I'm not sure I remember our proposed naming convention here. */
#define G_SPAWN_ERROR g_spawn_error_quark ()
typedef enum
{
G_SPAWN_ERROR_FORK, /* fork failed due to lack of memory */
G_SPAWN_ERROR_READ, /* read or select on pipes failed */
G_SPAWN_ERROR_CHDIR, /* changing to working dir failed */
G_SPAWN_ERROR_ACCES, /* execv() returned EACCES */
G_SPAWN_ERROR_PERM, /* execv() returned EPERM */
G_SPAWN_ERROR_2BIG, /* execv() returned E2BIG */
G_SPAWN_ERROR_NOEXEC, /* execv() returned ENOEXEC */
G_SPAWN_ERROR_NAMETOOLONG, /* "" "" ENAMETOOLONG */
G_SPAWN_ERROR_NOENT, /* "" "" ENOENT */
G_SPAWN_ERROR_NOMEM, /* "" "" ENOMEM */
G_SPAWN_ERROR_NOTDIR, /* "" "" ENOTDIR */
G_SPAWN_ERROR_LOOP, /* "" "" ELOOP */
G_SPAWN_ERROR_TXTBUSY, /* "" "" ETXTBUSY */
G_SPAWN_ERROR_IO, /* "" "" EIO */
G_SPAWN_ERROR_NFILE, /* "" "" ENFILE */
G_SPAWN_ERROR_MFILE, /* "" "" EMFLE */
G_SPAWN_ERROR_INVAL, /* "" "" EINVAL */
G_SPAWN_ERROR_ISDIR, /* "" "" EISDIR */
G_SPAWN_ERROR_LIBBAD, /* "" "" ELIBBAD */
G_SPAWN_ERROR_FAILED /* other fatal failure, error->message
* should explain
*/
} GSpawnError;
typedef void (* GSpawnChildSetupFunc) (gpointer user_data);
typedef enum
{
G_SPAWN_LEAVE_DESCRIPTORS_OPEN = 1 << 0,
G_SPAWN_DO_NOT_REAP_CHILD = 1 << 1,
/* look for argv[0] in the path i.e. use execvp() */
G_SPAWN_SEARCH_PATH = 1 << 2,
/* Dump output to /dev/null */
G_SPAWN_STDOUT_TO_DEV_NULL = 1 << 3,
G_SPAWN_STDERR_TO_DEV_NULL = 1 << 4,
G_SPAWN_CHILD_INHERITS_STDIN = 1 << 5
} GSpawnFlags;
GQuark g_spawn_error_quark (void);
gboolean g_spawn_async (const gchar *working_directory,
gchar **argv,
gchar **envp,
GSpawnFlags flags,
GSpawnChildSetupFunc child_setup,
gpointer user_data,
gint *child_pid,
GError **error);
/* Opens pipes for non-NULL standard_output, standard_input, standard_error,
* and returns the parent's end of the pipes.
*/
gboolean g_spawn_async_with_pipes (const gchar *working_directory,
gchar **argv,
gchar **envp,
GSpawnFlags flags,
GSpawnChildSetupFunc child_setup,
gpointer user_data,
gint *child_pid,
gint *standard_input,
gint *standard_output,
gint *standard_error,
GError **error);
/* If standard_output or standard_error are non-NULL, the full
* standard output or error of the command will be placed there.
*/
gboolean g_spawn_sync (const gchar *working_directory,
gchar **argv,
gchar **envp,
GSpawnFlags flags,
GSpawnChildSetupFunc child_setup,
gpointer user_data,
gchar **standard_output,
gchar **standard_error,
gint *exit_status,
GError **error);
gboolean g_spawn_command_line_sync (const gchar *command_line,
gchar **standard_output,
gchar **standard_error,
gint *exit_status,
GError **error);
gboolean g_spawn_command_line_async (const gchar *command_line,
GError **error);
#ifdef __cplusplus
}
#endif
#endif /* __GSPAWN_H__ */

View File

@ -117,11 +117,18 @@ g_unichar_ispunct (gunichar c)
gboolean
g_unichar_isspace (gunichar c)
{
/* special-case these since Unicode thinks they are not spaces */
if (c == ' ' || c == '\t' || c == '\n' || c == '\r' ||
c == '\f' || c == '\v') /* "the mythical vertical tab" */
return TRUE;
else
{
int t = TYPE (c);
return (t == G_UNICODE_SPACE_SEPARATOR || t == G_UNICODE_LINE_SEPARATOR
|| t == G_UNICODE_PARAGRAPH_SEPARATOR);
}
}
/**
* g_unichar_isupper:

View File

@ -143,6 +143,106 @@ g_atexit (GVoidFunc func)
g_error ("Could not register atexit() function: %s", error);
}
/* Based on execvp() from GNU Libc.
* Some of this code is cut-and-pasted into gspawn.c
*/
static gchar*
my_strchrnul (const gchar *str, gchar c)
{
gchar *p = (gchar*)str;
while (*p && (*p != c))
++p;
return p;
}
/**
* g_find_program_in_path:
* @file: a program name
*
* Locates the first executable named @file in the user's path, in the
* same way that execvp() would locate it. Returns an allocated string
* with the absolute path name, or NULL if the program is not found in
* the path. If @file is already an absolute path, returns a copy of
* @file if @file exists and is executable, and NULL otherwise.
*
* Return value: absolute path, or NULL
**/
gchar*
g_find_program_in_path (const gchar *file)
{
gchar *path, *p, *name, *freeme;
size_t len;
size_t pathlen;
g_return_val_if_fail (file != NULL, NULL);
if (*file == '/')
{
if (g_file_test (file, G_FILE_TEST_IS_EXECUTABLE))
return g_strdup (file);
else
return NULL;
}
path = g_getenv ("PATH");
if (path == NULL)
{
/* There is no `PATH' in the environment. The default
* search path in libc is the current directory followed by
* the path `confstr' returns for `_CS_PATH'.
*/
/* In GLib we put . last, for security, and don't use the
* unportable confstr(); UNIX98 does not actually specify
* what to search if PATH is unset. POSIX may, dunno.
*/
path = "/bin:/usr/bin:.";
}
len = strlen (file) + 1;
pathlen = strlen (path);
freeme = name = g_malloc (pathlen + len + 1);
/* Copy the file name at the top, including '\0' */
memcpy (name + pathlen + 1, file, len);
name = name + pathlen;
/* And add the slash before the filename */
*name = '/';
p = path;
do
{
char *startp;
path = p;
p = my_strchrnul (path, ':');
if (p == path)
/* Two adjacent colons, or a colon at the beginning or the end
* of `PATH' means to search the current directory.
*/
startp = name + 1;
else
startp = memcpy (name - (p - path), path, p - path);
if (g_file_test (startp, G_FILE_TEST_IS_EXECUTABLE))
{
gchar *ret;
ret = g_strdup (startp);
g_free (freeme);
return ret;
}
}
while (*p++ != '\0');
g_free (freeme);
return NULL;
}
gint
g_snprintf (gchar *str,
gulong n,

651
gshell.c Normal file
View File

@ -0,0 +1,651 @@
/* gshell.c - Shell-related utilities
*
* Copyright 2000 Red Hat, Inc.
* g_execvpe implementation based on GNU libc execvp:
* Copyright 1991, 92, 95, 96, 97, 98, 99 Free Software Foundation, 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.
*/
#include "glib.h"
#include <string.h>
#ifdef _
#warning "FIXME remove gettext hack"
#endif
#define _(x) x
GQuark
g_shell_error_quark (void)
{
static GQuark quark = 0;
if (quark == 0)
quark = g_quark_from_static_string ("g-shell-error-quark");
return quark;
}
/* Single quotes preserve the literal string exactly. escape
* sequences are not allowed; not even \' - if you want a '
* in the quoted text, you have to do something like 'foo'\''bar'
*
* Double quotes allow $ ` " \ and newline to be escaped with backslash.
* Otherwise double quotes preserve things literally.
*/
gboolean
unquote_string_inplace (gchar* str, gchar** end, GError** err)
{
gchar* dest;
gchar* s;
gchar quote_char;
g_return_val_if_fail(end != NULL, FALSE);
g_return_val_if_fail(err == NULL || *err == NULL, FALSE);
g_return_val_if_fail(str != NULL, FALSE);
dest = s = str;
quote_char = *s;
if (!(*s == '"' || *s == '\''))
{
if (err)
*err = g_error_new(G_SHELL_ERROR,
G_SHELL_ERROR_BAD_QUOTING,
_("Quoted text doesn't begin with a quotation mark"));
*end = str;
return FALSE;
}
/* Skip the initial quote mark */
++s;
if (quote_char == '"')
{
while (*s)
{
g_assert(s > dest); /* loop invariant */
switch (*s)
{
case '"':
/* End of the string, return now */
*dest = '\0';
++s;
*end = s;
return TRUE;
break;
case '\\':
/* Possible escaped quote or \ */
++s;
switch (*s)
{
case '"':
case '\\':
case '`':
case '$':
case '\n':
*dest = *s;
++s;
++dest;
break;
default:
/* not an escaped char */
*dest = '\\';
++dest;
/* ++s already done. */
break;
}
break;
default:
*dest = *s;
++dest;
++s;
break;
}
g_assert(s > dest); /* loop invariant */
}
}
else
{
while (*s)
{
g_assert(s > dest); /* loop invariant */
if (*s == '\'')
{
/* End of the string, return now */
*dest = '\0';
++s;
*end = s;
return TRUE;
}
else
{
*dest = *s;
++dest;
++s;
}
g_assert(s > dest); /* loop invariant */
}
}
/* If we reach here this means the close quote was never encountered */
*dest = '\0';
if (err)
*err = g_error_new(G_SHELL_ERROR,
G_SHELL_ERROR_BAD_QUOTING,
_("Unmatched quotation mark in command line or other shell-quoted text"));
*end = s;
return FALSE;
}
/**
* g_shell_quote:
* @unquoted_string: a literal string
*
* Quotes a string so that the shell (/bin/sh) will interpret the
* quoted string to mean @unquoted_string. If you pass a filename to
* the shell, for example, you should first quote it with this
* function. The return value must be freed with g_free(). The
* quoting style used is undefined (single or double quotes may be
* used).
*
* Return value: quoted string
**/
gchar*
g_shell_quote (const gchar *unquoted_string)
{
/* We always use single quotes, because the algorithm is cheesier.
* We could use double if we felt like it, that might be more
* human-readable.
*/
const gchar *p;
GString *dest;
g_return_val_if_fail (unquoted_string != NULL, NULL);
dest = g_string_new ("'");
p = unquoted_string;
/* could speed this up a lot by appending chunks of text at a
* time.
*/
while (*p)
{
/* Replace literal ' with a close ', a \', and a open ' */
if (*p == '\'')
g_string_append (dest, "'\''");
else
g_string_append_c (dest, *p);
++p;
}
/* close the quote */
g_string_append_c (dest, '\'');
return g_string_free (dest, FALSE);
}
/**
* g_shell_unquote:
* @quoted_string: shell-quoted string
* @error: error return location or NULL
*
* Unquotes a string as the shell (/bin/sh) would. Only handles
* quotes; if a string contains file globs, arithmetic operators,
* variables, backticks, redirections, or other special-to-the-shell
* features, the result will be different from the result a real shell
* would produce (the variables, backticks, etc. will be passed
* through literally instead of being expanded). This function is
* guaranteed to succeed if applied to the result of
* g_shell_quote(). If it fails, it returns NULL and sets the
* error. The @quoted_string need not actually contain quoted or
* escaped text; g_shell_unquote() simply goes through the string and
* unquotes/unescapes anything that the shell would. Both single and
* double quotes are handled, as are escapes including escaped
* newlines. The return value must be freed with g_free(). Possible
* errors are in the #G_SHELL_ERROR domain.
*
* Return value: an unquoted string
**/
gchar*
g_shell_unquote (const gchar *quoted_string,
GError **error)
{
gchar *unquoted;
gchar *end;
gchar *start;
GString *retval;
g_return_val_if_fail (quoted_string != NULL, NULL);
unquoted = g_strdup (quoted_string);
start = unquoted;
end = unquoted;
retval = g_string_new ("");
/* The loop allows cases such as
* "foo"blah blah'bar'woo foo"baz"la la la\'\''foo'
*/
while (*start)
{
/* Append all non-quoted chars, honoring backslash escape
*/
while (*start && !(*start == '"' || *start == '\''))
{
if (*start == '\\')
{
/* all characters can get escaped by backslash,
* except newline, which is removed if it follows
* a backslash outside of quotes
*/
++start;
if (*start)
{
if (*start != '\n')
g_string_append_c (retval, *start);
++start;
}
}
else
{
g_string_append_c (retval, *start);
++start;
}
}
if (*start)
{
if (!unquote_string_inplace (start, &end, error))
{
goto error;
}
else
{
g_string_append (retval, start);
start = end;
}
}
}
return g_string_free (retval, FALSE);
error:
g_assert (error == NULL || *error != NULL);
g_free (unquoted);
g_string_free (retval, TRUE);
return NULL;
}
/* g_parse_argv() does a semi-arbitrary weird subset of the way
* the shell parses a command line. We don't do variable expansion,
* don't understand that operators are tokens, don't do tilde expansion,
* don't do command substitution, no arithmetic expansion, IFS gets ignored,
* don't do filename globs, don't remove redirection stuff, etc.
*
* READ THE UNIX98 SPEC on "Shell Command Language" before changing
* the behavior of this code.
*
* Steps to parsing the argv string:
*
* - tokenize the string (but since we ignore operators,
* our tokenization may diverge from what the shell would do)
* note that tokenization ignores the internals of a quoted
* word and it always splits on spaces, not on IFS even
* if we used IFS. We also ignore "end of input indicator"
* (I guess this is control-D?)
*
* Tokenization steps, from UNIX98 with operator stuff removed,
* are:
*
* 1) "If the current character is backslash, single-quote or
* double-quote (\, ' or ") and it is not quoted, it will affect
* quoting for subsequent characters up to the end of the quoted
* text. The rules for quoting are as described in Quoting
* . During token recognition no substitutions will be actually
* performed, and the result token will contain exactly the
* characters that appear in the input (except for newline
* character joining), unmodified, including any embedded or
* enclosing quotes or substitution operators, between the quote
* mark and the end of the quoted text. The token will not be
* delimited by the end of the quoted field."
*
* 2) "If the current character is an unquoted newline character,
* the current token will be delimited."
*
* 3) "If the current character is an unquoted blank character, any
* token containing the previous character is delimited and the
* current character will be discarded."
*
* 4) "If the previous character was part of a word, the current
* character will be appended to that word."
*
* 5) "If the current character is a "#", it and all subsequent
* characters up to, but excluding, the next newline character
* will be discarded as a comment. The newline character that
* ends the line is not considered part of the comment. The
* "#" starts a comment only when it is at the beginning of a
* token. Since the search for the end-of-comment does not
* consider an escaped newline character specially, a comment
* cannot be continued to the next line."
*
* 6) "The current character will be used as the start of a new word."
*
*
* - for each token (word), perform portions of word expansion, namely
* field splitting (using default whitespace IFS) and quote
* removal. Field splitting may increase the number of words.
* Quote removal does not increase the number of words.
*
* "If the complete expansion appropriate for a word results in an
* empty field, that empty field will be deleted from the list of
* fields that form the completely expanded command, unless the
* original word contained single-quote or double-quote characters."
* - UNIX98 spec
*
*
*/
static inline void
ensure_token (GString **token)
{
if (*token == NULL)
*token = g_string_new ("");
}
static void
delimit_token (GString **token,
GSList **retval)
{
if (*token == NULL)
return;
*retval = g_slist_prepend (*retval, g_string_free (*token, FALSE));
*token = NULL;
}
static GSList*
tokenize_command_line (const gchar *command_line,
GError **error)
{
gchar current_quote;
const gchar *p;
GString *current_token = NULL;
GSList *retval = NULL;
current_quote = '\0';
p = command_line;
while (*p)
{
if (current_quote == '\\')
{
if (*p == '\n')
{
/* we append nothing; backslash-newline become nothing */
}
else
{
/* we append the backslash and the current char,
* to be interpreted later after tokenization
*/
ensure_token (&current_token);
g_string_append_c (current_token, '\\');
g_string_append_c (current_token, *p);
}
current_quote = '\0';
}
else if (current_quote == '#')
{
/* Discard up to and including next newline */
while (*p && *p != '\n')
++p;
current_quote = '\0';
if (*p == '\0')
break;
}
else if (current_quote)
{
if (*p == current_quote &&
/* check that it isn't an escaped double quote */
!(current_quote == '"' && p != command_line && *(p - 1) == '\\'))
{
/* close the quote */
current_quote = '\0';
}
/* Everything inside quotes, and the close quote,
* gets appended literally.
*/
ensure_token (&current_token);
g_string_append_c (current_token, *p);
}
else
{
switch (*p)
{
case '\n':
delimit_token (&current_token, &retval);
break;
case ' ':
case '\t':
/* If the current token contains the previous char, delimit
* the current token. A nonzero length
* token should always contain the previous char.
*/
if (current_token &&
current_token->len > 0)
{
delimit_token (&current_token, &retval);
}
/* discard all unquoted blanks (don't add them to a token) */
break;
/* single/double quotes are appended to the token,
* escapes are maybe appended next time through the loop,
* comment chars are never appended.
*/
case '\'':
case '"':
ensure_token (&current_token);
g_string_append_c (current_token, *p);
/* FALL THRU */
case '#':
case '\\':
current_quote = *p;
break;
default:
/* Combines rules 4) and 6) - if we have a token, append to it,
* otherwise create a new token.
*/
ensure_token (&current_token);
g_string_append_c (current_token, *p);
break;
}
}
++p;
}
delimit_token (&current_token, &retval);
if (current_quote)
{
if (current_quote == '\\')
g_set_error (error,
G_SHELL_ERROR,
G_SHELL_ERROR_BAD_QUOTING,
_("Text ended just after a '\' character."
" (The text was '%s')"),
command_line);
else
g_set_error (error,
G_SHELL_ERROR,
G_SHELL_ERROR_BAD_QUOTING,
_("Text ended before matching quote was found for %c."
" (The text was '%s')"),
current_quote, command_line);
goto error;
}
if (retval == NULL)
{
g_set_error (error,
G_SHELL_ERROR,
G_SHELL_ERROR_EMPTY_STRING,
_("Text was empty (or contained only whitespace)"));
goto error;
}
/* we appended backward */
retval = g_slist_reverse (retval);
return retval;
error:
g_assert (error == NULL || *error != NULL);
if (retval)
{
g_slist_foreach (retval, (GFunc)g_free, NULL);
g_slist_free (retval);
}
return NULL;
}
/**
* g_shell_parse_argv:
* @command_line: command line to parse
* @argcp: return location for number of args
* @argvp: return location for array of args
* @error: return location for error
*
* Parses a command line into an argument vector, in much the same way
* the shell would, but without many of the expansions the shell would
* perform (variable expansion, globs, operators, filename expansion,
* etc. are not supported). The results are defined to be the same as
* those you would get from a UNIX98 /bin/sh, as long as the input
* contains none of the unsupported shell expansions. If the input
* does contain such expansions, they are passed through
* literally. Possible errors are those from the #G_SHELL_ERROR
* domain.
*
* Return value: TRUE on success, FALSE if error set
**/
gboolean
g_shell_parse_argv (const gchar *command_line,
gint *argcp,
gchar ***argvp,
GError **error)
{
/* Code based on poptParseArgvString() from libpopt */
gint argc = 0;
gchar **argv = NULL;
GSList *tokens = NULL;
gint i;
GSList *tmp_list;
g_return_val_if_fail (command_line != NULL, FALSE);
tokens = tokenize_command_line (command_line, error);
if (tokens == NULL)
return FALSE;
/* Because we can't have introduced any new blank space into the
* tokens (we didn't do any new expansions), we don't need to
* perform field splitting. If we were going to honor IFS or do any
* expansions, we would have to do field splitting on each word
* here. Also, if we were going to do any expansion we would need to
* remove any zero-length words that didn't contain quotes
* originally; but since there's no expansion we know all words have
* nonzero length, unless they contain quotes.
*
* So, we simply remove quotes, and don't do any field splitting or
* empty word removal, since we know there was no way to introduce
* such things.
*/
argc = g_slist_length (tokens);
argv = g_new0 (gchar*, argc + 1);
i = 0;
tmp_list = tokens;
while (tmp_list)
{
argv[i] = g_shell_unquote (tmp_list->data, error);
/* Since we already checked that quotes matched up in the
* tokenizer, this shouldn't be possible to reach I guess.
*/
if (argv[i] == NULL)
goto failed;
tmp_list = g_slist_next (tmp_list);
++i;
}
g_slist_foreach (tokens, (GFunc)g_free, NULL);
g_slist_free (tokens);
if (argcp)
*argcp = argc;
if (argvp)
*argvp = argv;
else
g_strfreev (argv);
return TRUE;
failed:
g_assert (error == NULL || *error != NULL);
g_strfreev (argv);
g_slist_foreach (tokens, (GFunc) g_free, NULL);
g_slist_free (tokens);
return FALSE;
}

59
gshell.h Normal file
View File

@ -0,0 +1,59 @@
/* gshell.h - Shell-related utilities
*
* 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.
*/
#ifndef __GSHELL_H__
#define __GSHELL_H__
#include <gerror.h>
#ifdef __cplusplus
extern "C"
{
#endif
#define G_SHELL_ERROR g_shell_error_quark ()
typedef enum
{
/* mismatched or otherwise mangled quoting */
G_SHELL_ERROR_BAD_QUOTING,
/* string to be parsed was empty */
G_SHELL_ERROR_EMPTY_STRING,
G_SHELL_ERROR_FAILED
} GShellError;
GQuark g_shell_error_quark (void);
gchar* g_shell_quote (const gchar *unquoted_string);
gchar* g_shell_unquote (const gchar *quoted_string,
GError **error);
gboolean g_shell_parse_argv (const gchar *command_line,
gint *argc,
gchar ***argv,
GError **error);
#ifdef __cplusplus
}
#endif
#endif /* __GSHELL_H__ */

1392
gspawn.c Normal file

File diff suppressed because it is too large Load Diff

132
gspawn.h Normal file
View File

@ -0,0 +1,132 @@
/* gspawn.h - Process launching
*
* 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.
*/
#ifndef __GSPAWN_H__
#define __GSPAWN_H__
#include <gerror.h>
#ifdef __cplusplus
extern "C"
{
#endif
/* I'm not sure I remember our proposed naming convention here. */
#define G_SPAWN_ERROR g_spawn_error_quark ()
typedef enum
{
G_SPAWN_ERROR_FORK, /* fork failed due to lack of memory */
G_SPAWN_ERROR_READ, /* read or select on pipes failed */
G_SPAWN_ERROR_CHDIR, /* changing to working dir failed */
G_SPAWN_ERROR_ACCES, /* execv() returned EACCES */
G_SPAWN_ERROR_PERM, /* execv() returned EPERM */
G_SPAWN_ERROR_2BIG, /* execv() returned E2BIG */
G_SPAWN_ERROR_NOEXEC, /* execv() returned ENOEXEC */
G_SPAWN_ERROR_NAMETOOLONG, /* "" "" ENAMETOOLONG */
G_SPAWN_ERROR_NOENT, /* "" "" ENOENT */
G_SPAWN_ERROR_NOMEM, /* "" "" ENOMEM */
G_SPAWN_ERROR_NOTDIR, /* "" "" ENOTDIR */
G_SPAWN_ERROR_LOOP, /* "" "" ELOOP */
G_SPAWN_ERROR_TXTBUSY, /* "" "" ETXTBUSY */
G_SPAWN_ERROR_IO, /* "" "" EIO */
G_SPAWN_ERROR_NFILE, /* "" "" ENFILE */
G_SPAWN_ERROR_MFILE, /* "" "" EMFLE */
G_SPAWN_ERROR_INVAL, /* "" "" EINVAL */
G_SPAWN_ERROR_ISDIR, /* "" "" EISDIR */
G_SPAWN_ERROR_LIBBAD, /* "" "" ELIBBAD */
G_SPAWN_ERROR_FAILED /* other fatal failure, error->message
* should explain
*/
} GSpawnError;
typedef void (* GSpawnChildSetupFunc) (gpointer user_data);
typedef enum
{
G_SPAWN_LEAVE_DESCRIPTORS_OPEN = 1 << 0,
G_SPAWN_DO_NOT_REAP_CHILD = 1 << 1,
/* look for argv[0] in the path i.e. use execvp() */
G_SPAWN_SEARCH_PATH = 1 << 2,
/* Dump output to /dev/null */
G_SPAWN_STDOUT_TO_DEV_NULL = 1 << 3,
G_SPAWN_STDERR_TO_DEV_NULL = 1 << 4,
G_SPAWN_CHILD_INHERITS_STDIN = 1 << 5
} GSpawnFlags;
GQuark g_spawn_error_quark (void);
gboolean g_spawn_async (const gchar *working_directory,
gchar **argv,
gchar **envp,
GSpawnFlags flags,
GSpawnChildSetupFunc child_setup,
gpointer user_data,
gint *child_pid,
GError **error);
/* Opens pipes for non-NULL standard_output, standard_input, standard_error,
* and returns the parent's end of the pipes.
*/
gboolean g_spawn_async_with_pipes (const gchar *working_directory,
gchar **argv,
gchar **envp,
GSpawnFlags flags,
GSpawnChildSetupFunc child_setup,
gpointer user_data,
gint *child_pid,
gint *standard_input,
gint *standard_output,
gint *standard_error,
GError **error);
/* If standard_output or standard_error are non-NULL, the full
* standard output or error of the command will be placed there.
*/
gboolean g_spawn_sync (const gchar *working_directory,
gchar **argv,
gchar **envp,
GSpawnFlags flags,
GSpawnChildSetupFunc child_setup,
gpointer user_data,
gchar **standard_output,
gchar **standard_error,
gint *exit_status,
GError **error);
gboolean g_spawn_command_line_sync (const gchar *command_line,
gchar **standard_output,
gchar **standard_error,
gint *exit_status,
GError **error);
gboolean g_spawn_command_line_async (const gchar *command_line,
GError **error);
#ifdef __cplusplus
}
#endif
#endif /* __GSPAWN_H__ */

View File

@ -117,11 +117,18 @@ g_unichar_ispunct (gunichar c)
gboolean
g_unichar_isspace (gunichar c)
{
/* special-case these since Unicode thinks they are not spaces */
if (c == ' ' || c == '\t' || c == '\n' || c == '\r' ||
c == '\f' || c == '\v') /* "the mythical vertical tab" */
return TRUE;
else
{
int t = TYPE (c);
return (t == G_UNICODE_SPACE_SEPARATOR || t == G_UNICODE_LINE_SEPARATOR
|| t == G_UNICODE_PARAGRAPH_SEPARATOR);
}
}
/**
* g_unichar_isupper:

100
gutils.c
View File

@ -143,6 +143,106 @@ g_atexit (GVoidFunc func)
g_error ("Could not register atexit() function: %s", error);
}
/* Based on execvp() from GNU Libc.
* Some of this code is cut-and-pasted into gspawn.c
*/
static gchar*
my_strchrnul (const gchar *str, gchar c)
{
gchar *p = (gchar*)str;
while (*p && (*p != c))
++p;
return p;
}
/**
* g_find_program_in_path:
* @file: a program name
*
* Locates the first executable named @file in the user's path, in the
* same way that execvp() would locate it. Returns an allocated string
* with the absolute path name, or NULL if the program is not found in
* the path. If @file is already an absolute path, returns a copy of
* @file if @file exists and is executable, and NULL otherwise.
*
* Return value: absolute path, or NULL
**/
gchar*
g_find_program_in_path (const gchar *file)
{
gchar *path, *p, *name, *freeme;
size_t len;
size_t pathlen;
g_return_val_if_fail (file != NULL, NULL);
if (*file == '/')
{
if (g_file_test (file, G_FILE_TEST_IS_EXECUTABLE))
return g_strdup (file);
else
return NULL;
}
path = g_getenv ("PATH");
if (path == NULL)
{
/* There is no `PATH' in the environment. The default
* search path in libc is the current directory followed by
* the path `confstr' returns for `_CS_PATH'.
*/
/* In GLib we put . last, for security, and don't use the
* unportable confstr(); UNIX98 does not actually specify
* what to search if PATH is unset. POSIX may, dunno.
*/
path = "/bin:/usr/bin:.";
}
len = strlen (file) + 1;
pathlen = strlen (path);
freeme = name = g_malloc (pathlen + len + 1);
/* Copy the file name at the top, including '\0' */
memcpy (name + pathlen + 1, file, len);
name = name + pathlen;
/* And add the slash before the filename */
*name = '/';
p = path;
do
{
char *startp;
path = p;
p = my_strchrnul (path, ':');
if (p == path)
/* Two adjacent colons, or a colon at the beginning or the end
* of `PATH' means to search the current directory.
*/
startp = name + 1;
else
startp = memcpy (name - (p - path), path, p - path);
if (g_file_test (startp, G_FILE_TEST_IS_EXECUTABLE))
{
gchar *ret;
ret = g_strdup (startp);
g_free (freeme);
return ret;
}
}
while (*p++ != '\0');
g_free (freeme);
return NULL;
}
gint
g_snprintf (gchar *str,
gulong n,

View File

@ -1,6 +1,8 @@
INCLUDES = -I$(top_srcdir) @GLIB_DEBUG_FLAGS@
EFENCE=
EXTRA_DIST = \
makefile.msc \
makefile.msc.in \
@ -18,7 +20,9 @@ TESTS = \
queue-test \
rand-test \
relation-test \
shell-test \
slist-test \
spawn-test \
strfunc-test \
string-test \
thread-test \
@ -28,7 +32,7 @@ TESTS = \
noinst_PROGRAMS = $(TESTS)
progs_LDADD = $(top_builddir)/libglib-1.3.la
progs_LDADD = $(EFENCE) $(top_builddir)/libglib-1.3.la $(EFENCE)
thread_LDADD = $(progs_LDADD) $(top_builddir)/gthread/libgthread-1.3.la @G_THREAD_LIBS@
array_test_LDADD = $(progs_LDADD)
@ -41,7 +45,9 @@ node_test_LDADD = $(progs_LDADD)
queue_test_LDADD = $(progs_LDADD)
rand_test_LDADD = $(progs_LDADD)
relation_test_LDADD = $(progs_LDADD)
shell_test_LDADD = $(progs_LDADD)
slist_test_LDADD = $(progs_LDADD)
spawn_test_LDADD = $(progs_LDADD)
strfunc_test_LDADD = $(progs_LDADD)
string_test_LDADD = $(progs_LDADD)
thread_test_LDADD = $(thread_LDADD)

190
tests/shell-test.c Normal file
View File

@ -0,0 +1,190 @@
/* GLIB - Library of useful routines for C programming
* Copyright (C) 1995-1997 Peter Mattis, Spencer Kimball and Josh MacDonald
*
* This library 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.
*
* This library 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 this library; if not, write to the
* Free Software Foundation, Inc., 59 Temple Place - Suite 330,
* Boston, MA 02111-1307, USA.
*/
/*
* Modified by the GLib Team and others 1997-2000. See the AUTHORS
* file for a list of people on the GLib Team. See the ChangeLog
* files for a list of changes. These files are distributed with
* GLib at ftp://ftp.gtk.org/pub/gtk/.
*/
#undef G_LOG_DOMAIN
#include <glib.h>
#include <stdio.h>
#include <string.h>
#include <stdlib.h>
typedef struct _TestResult TestResult;
struct _TestResult
{
gint argc;
const gchar **argv;
};
static const gchar *
test_command_lines[] =
{
/* 0 */ "foo bar",
/* 1 */ "foo 'bar'",
/* 2 */ "foo \"bar\"",
/* 3 */ "foo '' 'bar'",
/* 4 */ "foo \"bar\"'baz'blah'foo'\\''blah'\"boo\"",
/* 5 */ "foo \t \tblah\tfoo\t\tbar baz",
/* 6 */ "foo ' spaces more spaces lots of spaces in this ' \t",
/* 7 */ "foo \\\nbar",
/* 8 */ "foo '' ''",
/* 9 */ "foo \\\" la la la",
/* 10 */ "foo \\ foo woo woo\\ ",
/* 11 */ "foo \"yada yada \\$\\\"\"",
NULL
};
static const gchar *result0[] = { "foo", "bar", NULL };
static const gchar *result1[] = { "foo", "bar", NULL };
static const gchar *result2[] = { "foo", "bar", NULL };
static const gchar *result3[] = { "foo", "", "bar", NULL };
static const gchar *result4[] = { "foo", "barbazblahfoo'blahboo", NULL };
static const gchar *result5[] = { "foo", "blah", "foo", "bar", "baz", NULL };
static const gchar *result6[] = { "foo", " spaces more spaces lots of spaces in this ", NULL };
static const gchar *result7[] = { "foo", "bar", NULL };
static const gchar *result8[] = { "foo", "", "", NULL };
static const gchar *result9[] = { "foo", "\"", "la", "la", "la", NULL };
static const gchar *result10[] = { "foo", " foo", "woo", "woo ", NULL };
static const gchar *result11[] = { "foo", "yada yada $\"", NULL };
static const TestResult
correct_results[] =
{
{ G_N_ELEMENTS (result0) - 1, result0 },
{ G_N_ELEMENTS (result1) - 1, result1 },
{ G_N_ELEMENTS (result2) - 1, result2 },
{ G_N_ELEMENTS (result3) - 1, result3 },
{ G_N_ELEMENTS (result4) - 1, result4 },
{ G_N_ELEMENTS (result5) - 1, result5 },
{ G_N_ELEMENTS (result6) - 1, result6 },
{ G_N_ELEMENTS (result7) - 1, result7 },
{ G_N_ELEMENTS (result8) - 1, result8 },
{ G_N_ELEMENTS (result9) - 1, result9 },
{ G_N_ELEMENTS (result10) - 1, result10 },
{ G_N_ELEMENTS (result11) - 1, result11 }
};
static void
print_test (const gchar *cmdline, gint argc, gchar **argv,
const TestResult *result)
{
gint i;
printf ("\nCommand line was: '%s'\n", cmdline);
printf ("Expected result (%d args):\n", result->argc);
i = 0;
while (result->argv[i])
{
printf (" %3d '%s'\n", i, result->argv[i]);
++i;
}
printf ("Actual result (%d args):\n", argc);
i = 0;
while (argv[i])
{
printf (" %3d '%s'\n", i, argv[i]);
++i;
}
}
static void
do_argv_test (const gchar *cmdline, const TestResult *result)
{
gint argc;
gchar **argv;
GError *err;
gint i;
err = NULL;
if (!g_shell_parse_argv (cmdline, &argc, &argv, &err))
{
fprintf (stderr, "Error parsing command line that should work fine: %s\n",
err->message);
exit (1);
}
if (argc != result->argc)
{
fprintf (stderr, "Expected and actual argc don't match\n");
print_test (cmdline, argc, argv, result);
exit (1);
}
i = 0;
while (argv[i])
{
if (strcmp (argv[i], result->argv[i]) != 0)
{
fprintf (stderr, "Expected and actual arg %d do not match\n", i);
print_test (cmdline, argc, argv, result);
exit (1);
}
++i;
}
if (argv[i] != NULL)
{
fprintf (stderr, "argv didn't get NULL-terminated\n");
exit (1);
}
}
static void
run_tests (void)
{
GError *err;
gint i;
i = 0;
while (test_command_lines[i])
{
printf ("g_shell_parse_argv() test %d - ", i);
do_argv_test (test_command_lines[i], &correct_results[i]);
printf ("ok (%s)\n", test_command_lines[i]);
++i;
}
}
int
main (int argc,
char *argv[])
{
run_tests ();
return 0;
}

96
tests/spawn-test.c Normal file
View File

@ -0,0 +1,96 @@
/* GLIB - Library of useful routines for C programming
* Copyright (C) 1995-1997 Peter Mattis, Spencer Kimball and Josh MacDonald
*
* This library 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.
*
* This library 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 this library; if not, write to the
* Free Software Foundation, Inc., 59 Temple Place - Suite 330,
* Boston, MA 02111-1307, USA.
*/
/*
* Modified by the GLib Team and others 1997-2000. See the AUTHORS
* file for a list of people on the GLib Team. See the ChangeLog
* files for a list of changes. These files are distributed with
* GLib at ftp://ftp.gtk.org/pub/gtk/.
*/
#undef G_LOG_DOMAIN
#include <glib.h>
#include <stdio.h>
#include <string.h>
#include <stdlib.h>
static void
run_tests (void)
{
GError *err;
gchar *output = NULL;
printf ("The following errors are supposed to occur:\n");
err = NULL;
if (!g_spawn_command_line_sync ("nonexistent_application foo 'bar baz' blah blah",
NULL, NULL, NULL,
&err))
{
fprintf (stderr, "Error (normal, supposed to happen): %s\n", err->message);
g_error_free (err);
}
err = NULL;
if (!g_spawn_command_line_async ("nonexistent_application foo bar baz \"blah blah\"",
&err))
{
fprintf (stderr, "Error (normal, supposed to happen): %s\n", err->message);
g_error_free (err);
}
printf ("Errors after this are not supposed to happen:\n");
err = NULL;
if (!g_spawn_command_line_sync ("/bin/sh -c 'echo hello'",
&output, NULL, NULL,
&err))
{
fprintf (stderr, "Error: %s\n", err->message);
g_error_free (err);
exit (1);
}
else
{
g_assert (output != NULL);
if (strcmp (output, "hello\n") != 0)
{
printf ("output was '%s', should have been 'hello'\n",
output);
exit (1);
}
g_free (output);
}
}
int
main (int argc,
char *argv[])
{
run_tests ();
return 0;
}