D-Bus 1.13.18

dbus-sysdeps-util.c
1/* -*- mode: C; c-file-style: "gnu"; indent-tabs-mode: nil; -*- */
2/* dbus-sysdeps-util.c Tests for dbus-sysdeps.h API
3 *
4 * Copyright 2002-2008 Red Hat, Inc.
5 * Copyright 2003 CodeFactory AB
6 * Copyright 2006 Ralf Habacker
7 * Copyright 2006 Sjoerd Simons
8 * Copyright 2016-2018 Collabora Ltd.
9 *
10 * Licensed under the Academic Free License version 2.1
11 *
12 * This program is free software; you can redistribute it and/or modify
13 * it under the terms of the GNU General Public License as published by
14 * the Free Software Foundation; either version 2 of the License, or
15 * (at your option) any later version.
16 *
17 * This program is distributed in the hope that it will be useful,
18 * but WITHOUT ANY WARRANTY; without even the implied warranty of
19 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
20 * GNU General Public License for more details.
21 *
22 * You should have received a copy of the GNU General Public License
23 * along with this program; if not, write to the Free Software
24 * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
25 *
26 */
27
28#include <config.h>
29#include "dbus-sysdeps.h"
30#include "dbus-internals.h"
31#include "dbus-string.h"
32
33#include <stdlib.h>
34
35#ifdef DBUS_WIN
36 /* do nothing, it's in stdlib.h */
37#elif (defined __APPLE__)
38# include <crt_externs.h>
39# define environ (*_NSGetEnviron())
40#elif HAVE_DECL_ENVIRON && defined(HAVE_UNISTD_H)
41# include <unistd.h>
42#else
43 extern char **environ;
44#endif
45
52 char **
53_dbus_get_environment (void)
54{
55 int i, length;
56 char **environment;
57
58 _dbus_assert (environ != NULL);
59
60 for (length = 0; environ[length] != NULL; length++);
61
62 /* Add one for NULL */
63 length++;
64
65 environment = dbus_new0 (char *, length);
66
67 if (environment == NULL)
68 return NULL;
69
70 for (i = 0; environ[i] != NULL; i++)
71 {
72 environment[i] = _dbus_strdup (environ[i]);
73
74 if (environment[i] == NULL)
75 break;
76 }
77
78 if (environ[i] != NULL)
79 {
80 dbus_free_string_array (environment);
81 environment = NULL;
82 }
83
84 return environment;
85}
_dbus_assert
#define _dbus_assert(condition)
Aborts with an error message if the condition is false.
Definition: dbus-internals.h:153
_dbus_strdup
char * _dbus_strdup(const char *str)
Duplicates a string.
Definition: dbus-internals.c:589
NULL
#define NULL
A null pointer, defined appropriately for C or C++.
dbus_new0
#define dbus_new0(type, count)
Safe macro for using dbus_malloc0().
Definition: dbus-memory.h:58
dbus_free_string_array
void dbus_free_string_array(char **str_array)
Frees a NULL-terminated array of strings.
Definition: dbus-memory.c:752
_dbus_get_environment
char ** _dbus_get_environment(void)
Gets a NULL-terminated list of key=value pairs from the environment.
Definition: dbus-sysdeps-util.c:53