-/* -*- mode: C; c-file-style: "gnu" -*- */
+/* -*- mode: C; c-file-style: "gnu"; indent-tabs-mode: nil; -*- */
/* dbus-sysdeps.c Wrappers around system/libc features shared between UNIX and Windows (internal to D-Bus implementation)
*
* Copyright (C) 2002, 2003, 2006 Red Hat, Inc.
*
* You should have received a copy of the GNU General Public License
* along with this program; if not, write to the Free Software
- * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
+ * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
*
*/
+#include <config.h>
#include "dbus-internals.h"
#include "dbus-sysdeps.h"
#include "dbus-threads.h"
*
* These are the standard ANSI C headers...
*/
+#if HAVE_LOCALE_H
#include <locale.h>
+#endif
#include <stdlib.h>
#include <string.h>
#include <stdio.h>
-/* This is UNIX-specific (on windows it's just in stdlib.h I believe)
- * but OK since the same stuff does exist on Windows in stdlib.h
- * and covered by a configure check.
- */
#ifdef HAVE_ERRNO_H
#include <errno.h>
#endif
_DBUS_DEFINE_GLOBAL_LOCK (sid_atom_cache);
_DBUS_DEFINE_GLOBAL_LOCK (system_users);
+#ifdef DBUS_WIN
+ #include <stdlib.h>
+#elif (defined __APPLE__)
+# include <crt_externs.h>
+# define environ (*_NSGetEnviron())
+#else
+extern char **environ;
+#endif
+
/**
* @defgroup DBusSysdeps Internal system-dependent API
* @ingroup DBusInternals
return getenv (varname);
}
-/*
- * init a pipe instance.
- *
- * @param pipe the pipe
- * @param fd the file descriptor to init from
- */
-void
-_dbus_pipe_init (DBusPipe *pipe,
- int fd)
-{
- pipe->fd_or_handle = fd;
-}
-
/**
- * init a pipe with stdout
+ * Wrapper for clearenv().
*
- * @param pipe the pipe
- */
-void
-_dbus_pipe_init_stdout (DBusPipe *pipe)
-{
- _dbus_pipe_init (pipe, 1);
-}
-
-/**
- * check if a pipe is valid; pipes can be set invalid, similar to
- * a -1 file descriptor.
- *
- * @param pipe the pipe instance
- * @returns #FALSE if pipe is not valid
+ * @returns #TRUE on success.
*/
dbus_bool_t
-_dbus_pipe_is_valid(DBusPipe *pipe)
+_dbus_clearenv (void)
{
- return pipe->fd_or_handle >= 0;
+ dbus_bool_t rc = TRUE;
+
+#ifdef HAVE_CLEARENV
+ if (clearenv () != 0)
+ rc = FALSE;
+#else
+
+ if (environ != NULL)
+ environ[0] = NULL;
+#endif
+
+ return rc;
}
/**
- * Check if a pipe is stdout or stderr.
+ * Gets a #NULL-terminated list of key=value pairs from the
+ * environment. Use dbus_free_string_array to free it.
*
- * @param pipe the pipe instance
- * @returns #TRUE if pipe is one of the standard out/err channels
+ * @returns the environment or #NULL on OOM
*/
-dbus_bool_t
-_dbus_pipe_is_stdout_or_stderr (DBusPipe *pipe)
+char **
+_dbus_get_environment (void)
{
- return pipe->fd_or_handle == 1 || pipe->fd_or_handle == 2;
-}
+ int i, length;
+ char **environment;
-/**
- * Initializes a pipe to an invalid value.
- * @param pipe the pipe
- */
-void
-_dbus_pipe_invalidate (DBusPipe *pipe)
-{
- pipe->fd_or_handle = -1;
+ _dbus_assert (environ != NULL);
+
+ for (length = 0; environ[length] != NULL; length++);
+
+ /* Add one for NULL */
+ length++;
+
+ environment = dbus_new0 (char *, length);
+
+ if (environment == NULL)
+ return NULL;
+
+ for (i = 0; environ[i] != NULL; i++)
+ {
+ environment[i] = _dbus_strdup (environ[i]);
+
+ if (environment[i] == NULL)
+ break;
+ }
+
+ if (environ[i] != NULL)
+ {
+ dbus_free_string_array (environment);
+ environment = NULL;
+ }
+
+ return environment;
}
/**
_dbus_string_get_length (str) - start);
end = NULL;
- errno = 0;
+ _dbus_set_errno_to_zero ();
v = strtol (p, &end, 0);
if (end == NULL || end == p || errno != 0)
return FALSE;
_dbus_string_get_length (str) - start);
end = NULL;
- errno = 0;
+ _dbus_set_errno_to_zero ();
v = strtoul (p, &end, 0);
if (end == NULL || end == p || errno != 0)
return FALSE;
fail_pos = NULL;
+#if HAVE_LOCALECONV
locale_data = localeconv ();
decimal_point = locale_data->decimal_point;
- decimal_point_len = strlen (decimal_point);
+#else
+ decimal_point = ".";
+#endif
+ decimal_point_len = strlen (decimal_point);
_dbus_assert (decimal_point_len != 0);
decimal_point_pos = NULL;
/* Set errno to zero, so that we can distinguish zero results
and underflows */
- errno = 0;
+ _dbus_set_errno_to_zero ();
if (decimal_point_pos)
{
p = _dbus_string_get_const_data_len (str, start,
_dbus_string_get_length (str) - start);
+ /* parsing hex works on linux but isn't portable, so intercept it
+ * here to get uniform behavior.
+ */
+ if (p[0] == '0' && (p[1] == 'x' || p[1] == 'X'))
+ return FALSE;
+
end = NULL;
- errno = 0;
+ _dbus_set_errno_to_zero ();
v = ascii_strtod (p, &end);
if (end == NULL || end == p || errno != 0)
return FALSE;
}
/**
- * Converts a UNIX or Windows errno
- * into a #DBusError name.
+ * Converts a UNIX errno, or Windows errno or WinSock error value into
+ * a #DBusError name.
*
* @todo should cover more errnos, specifically those
* from open().
case EPROTONOSUPPORT:
return DBUS_ERROR_NOT_SUPPORTED;
#endif
+#ifdef WSAEPROTONOSUPPORT
+ case WSAEPROTONOSUPPORT:
+ return DBUS_ERROR_NOT_SUPPORTED;
+#endif
#ifdef EAFNOSUPPORT
case EAFNOSUPPORT:
return DBUS_ERROR_NOT_SUPPORTED;
#endif
+#ifdef WSAEAFNOSUPPORT
+ case WSAEAFNOSUPPORT:
+ return DBUS_ERROR_NOT_SUPPORTED;
+#endif
#ifdef ENFILE
case ENFILE:
return DBUS_ERROR_LIMITS_EXCEEDED; /* kernel out of memory */
case ENOMEM:
return DBUS_ERROR_NO_MEMORY;
#endif
-#ifdef EINVAL
- case EINVAL:
- return DBUS_ERROR_FAILED;
-#endif
-#ifdef EBADF
- case EBADF:
- return DBUS_ERROR_FAILED;
-#endif
-#ifdef EFAULT
- case EFAULT:
- return DBUS_ERROR_FAILED;
-#endif
-#ifdef ENOTSOCK
- case ENOTSOCK:
- return DBUS_ERROR_FAILED;
-#endif
-#ifdef EISCONN
- case EISCONN:
- return DBUS_ERROR_FAILED;
-#endif
#ifdef ECONNREFUSED
case ECONNREFUSED:
return DBUS_ERROR_NO_SERVER;
#endif
+#ifdef WSAECONNREFUSED
+ case WSAECONNREFUSED:
+ return DBUS_ERROR_NO_SERVER;
+#endif
#ifdef ETIMEDOUT
case ETIMEDOUT:
return DBUS_ERROR_TIMEOUT;
#endif
+#ifdef WSAETIMEDOUT
+ case WSAETIMEDOUT:
+ return DBUS_ERROR_TIMEOUT;
+#endif
#ifdef ENETUNREACH
case ENETUNREACH:
return DBUS_ERROR_NO_NETWORK;
#endif
+#ifdef WSAENETUNREACH
+ case WSAENETUNREACH:
+ return DBUS_ERROR_NO_NETWORK;
+#endif
#ifdef EADDRINUSE
case EADDRINUSE:
return DBUS_ERROR_ADDRESS_IN_USE;
#endif
+#ifdef WSAEADDRINUSE
+ case WSAEADDRINUSE:
+ return DBUS_ERROR_ADDRESS_IN_USE;
+#endif
#ifdef EEXIST
case EEXIST:
return DBUS_ERROR_FILE_EXISTS;
return DBUS_ERROR_FAILED;
}
+/**
+ * Converts the current system errno value into a #DBusError name.
+ *
+ * @returns an error name
+ */
+const char*
+_dbus_error_from_system_errno (void)
+{
+ return _dbus_error_from_errno (errno);
+}
+
+/**
+ * Assign 0 to the global errno variable
+ */
+void
+_dbus_set_errno_to_zero (void)
+{
+#ifdef DBUS_WINCE
+ SetLastError (0);
+#else
+ errno = 0;
+#endif
+}
+
+/**
+ * See if errno is set
+ * @returns #TRUE if errno is not 0
+ */
+dbus_bool_t
+_dbus_get_is_errno_nonzero (void)
+{
+ return errno != 0;
+}
+
+/**
+ * See if errno is ENOMEM
+ * @returns #TRUE if errno == ENOMEM
+ */
+dbus_bool_t
+_dbus_get_is_errno_enomem (void)
+{
+ return errno == ENOMEM;
+}
+
+/**
+ * See if errno is EINTR
+ * @returns #TRUE if errno == EINTR
+ */
+dbus_bool_t
+_dbus_get_is_errno_eintr (void)
+{
+ return errno == EINTR;
+}
+
+/**
+ * See if errno is EPIPE
+ * @returns #TRUE if errno == EPIPE
+ */
+dbus_bool_t
+_dbus_get_is_errno_epipe (void)
+{
+ return errno == EPIPE;
+}
+
+/**
+ * Get error message from errno
+ * @returns _dbus_strerror(errno)
+ */
+const char*
+_dbus_strerror_from_errno (void)
+{
+ return _dbus_strerror (errno);
+}
+
/** @} end of sysdeps */
/* tests in dbus-sysdeps-util.c */