D-Bus: dbus-sysdeps-util.c Source File

D-Bus 1.12.20

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 (C) 2002, 2003, 2004, 2005 Red Hat, Inc.
5 * Copyright (C) 2003 CodeFactory AB
6 *
7 * Licensed under the Academic Free License version 2.1
8 *
9 * This program is free software; you can redistribute it and/or modify
10 * it under the terms of the GNU General Public License as published by
11 * the Free Software Foundation; either version 2 of the License, or
12 * (at your option) any later version.
13 *
14 * This program is distributed in the hope that it will be useful,
15 * but WITHOUT ANY WARRANTY; without even the implied warranty of
16 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
17 * GNU General Public License for more details.
18 *
19 * You should have received a copy of the GNU General Public License
20 * along with this program; if not, write to the Free Software
21 * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
22 *
23 */
24
25 #include <config.h>
26 #include "dbus-sysdeps.h"
27 #include "dbus-internals.h"
28 #include "dbus-string.h"
29 #include "dbus-test.h"
30
31 #include <stdlib.h>
32
33 #ifdef DBUS_WIN
34 /* do nothing, it's in stdlib.h */
35 #elif (defined __APPLE__)
36 # include <crt_externs.h>
37 # define environ (*_NSGetEnviron())
38 #elif HAVE_DECL_ENVIRON && defined(HAVE_UNISTD_H)
39 # include <unistd.h>
40 #else
41 extern char **environ;
42 #endif
43
50 char **
51 _dbus_get_environment (void)
52 {
53 int i, length;
54 char **environment;
55
56 _dbus_assert (environ != NULL);
57
58 for (length = 0; environ[length] != NULL; length++);
59
60 /* Add one for NULL */
61 length++;
62
63 environment = dbus_new0 (char *, length);
64
65 if (environment == NULL)
66 return NULL;
67
68 for (i = 0; environ[i] != NULL; i++)
69 {
70 environment[i] = _dbus_strdup (environ[i]);
71
72 if (environment[i] == NULL)
73 break;
74 }
75
76 if (environ[i] != NULL)
77 {
78 dbus_free_string_array (environment);
79 environment = NULL;
80 }
81
82 return environment;
83 }
84
85 #ifdef DBUS_ENABLE_EMBEDDED_TESTS
86 static void
87 check_dirname (const char *filename,
88 const char *dirname)
89 {
90 DBusString f, d;
91
92 _dbus_string_init_const (&f, filename);
93
94 if (!_dbus_string_init (&d))
95 _dbus_assert_not_reached ("no memory");
96
97 if (!_dbus_string_get_dirname (&f, &d))
98 _dbus_assert_not_reached ("no memory");
99
100 if (!_dbus_string_equal_c_str (&d, dirname))
101 {
102 _dbus_warn ("For filename \"%s\" got dirname \"%s\" and expected \"%s\"",
103 filename,
104 _dbus_string_get_const_data (&d),
105 dirname);
106 exit (1);
107 }
108
109 _dbus_string_free (&d);
110 }
111
112 static void
113 check_path_absolute (const char *path,
114 dbus_bool_t expected)
115 {
116 DBusString p;
117
118 _dbus_string_init_const (&p, path);
119
120 if (_dbus_path_is_absolute (&p) != expected)
121 {
122 _dbus_warn ("For path \"%s\" expected absolute = %d got %d",
123 path, expected, _dbus_path_is_absolute (&p));
124 exit (1);
125 }
126 }
127
133 dbus_bool_t
134 _dbus_sysdeps_test (void)
135 {
136 #ifdef DBUS_WIN
137 check_dirname ("foo\\bar", "foo");
138 check_dirname ("foo\\\\bar", "foo");
139 check_dirname ("foo/\\/bar", "foo");
140 check_dirname ("foo\\bar/", "foo");
141 check_dirname ("foo//bar\\", "foo");
142 check_dirname ("foo\\bar/", "foo");
143 check_dirname ("foo/bar\\\\", "foo");
144 check_dirname ("\\foo", "\\");
145 check_dirname ("\\\\foo", "\\");
146 check_dirname ("\\", "\\");
147 check_dirname ("\\\\", "\\");
148 check_dirname ("\\/", "\\");
149 check_dirname ("/\\/", "/");
150 check_dirname ("c:\\foo\\bar", "c:\\foo");
151 check_dirname ("c:\\foo", "c:\\");
152 check_dirname ("c:/foo", "c:/");
153 check_dirname ("c:\\", "c:\\");
154 check_dirname ("c:/", "c:/");
155 check_dirname ("", ".");
156 #else
157 check_dirname ("foo", ".");
158 check_dirname ("foo/bar", "foo");
159 check_dirname ("foo//bar", "foo");
160 check_dirname ("foo///bar", "foo");
161 check_dirname ("foo/bar/", "foo");
162 check_dirname ("foo//bar/", "foo");
163 check_dirname ("foo///bar/", "foo");
164 check_dirname ("foo/bar//", "foo");
165 check_dirname ("foo//bar////", "foo");
166 check_dirname ("foo///bar///////", "foo");
167 check_dirname ("/foo", "/");
168 check_dirname ("////foo", "/");
169 check_dirname ("/foo/bar", "/foo");
170 check_dirname ("/foo//bar", "/foo");
171 check_dirname ("/foo///bar", "/foo");
172 check_dirname ("/", "/");
173 check_dirname ("///", "/");
174 check_dirname ("", ".");
175 #endif
176
177 #ifdef DBUS_WIN
178 check_path_absolute ("c:/", TRUE);
179 check_path_absolute ("c:/foo", TRUE);
180 check_path_absolute ("", FALSE);
181 check_path_absolute ("foo", FALSE);
182 check_path_absolute ("foo/bar", FALSE);
183 check_path_absolute ("", FALSE);
184 check_path_absolute ("foo\\bar", FALSE);
185 check_path_absolute ("c:\\", TRUE);
186 check_path_absolute ("c:\\foo", TRUE);
187 check_path_absolute ("c:", TRUE);
188 check_path_absolute ("c:\\foo\\bar", TRUE);
189 check_path_absolute ("\\", TRUE);
190 check_path_absolute ("/", TRUE);
191 #else
192 check_path_absolute ("/", TRUE);
193 check_path_absolute ("/foo", TRUE);
194 check_path_absolute ("", FALSE);
195 check_path_absolute ("foo", FALSE);
196 check_path_absolute ("foo/bar", FALSE);
197 #endif
198
199 return TRUE;
200 }
201 #endif /* DBUS_ENABLE_EMBEDDED_TESTS */
_dbus_string_free
void _dbus_string_free(DBusString *str)
Frees a string created by _dbus_string_init().
Definition: dbus-string.c:259
_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:51
_dbus_path_is_absolute
dbus_bool_t _dbus_path_is_absolute(const DBusString *filename)
Checks whether the filename is an absolute path.
Definition: dbus-sysdeps-util-unix.c:607
_dbus_string_init
dbus_bool_t _dbus_string_init(DBusString *str)
Initializes a string.
Definition: dbus-string.c:175
_dbus_string_get_dirname
dbus_bool_t _dbus_string_get_dirname(const DBusString *filename, DBusString *dirname)
Get the directory name from a complete filename.
Definition: dbus-sysdeps-util-unix.c:1050
TRUE
#define TRUE
DBusString
Definition: dbus-string.h:43
FALSE
#define FALSE
_dbus_assert_not_reached
#define _dbus_assert_not_reached(explanation)
Definition: dbus-internals.h:163
_dbus_assert
#define _dbus_assert(condition)
Definition: dbus-internals.h:152
_dbus_warn
void _dbus_warn(const char *format,...)
Prints a warning message to stderr.
Definition: dbus-internals.c:230
_dbus_strdup
char * _dbus_strdup(const char *str)
Duplicates a string.
Definition: dbus-internals.c:536
dbus_free_string_array
void dbus_free_string_array(char **str_array)
Frees a NULL-terminated array of strings.
Definition: dbus-memory.c:750
_dbus_string_equal_c_str
dbus_bool_t _dbus_string_equal_c_str(const DBusString *a, const char *c_str)
Checks whether a string is equal to a C string.
Definition: dbus-string.c:2152
_dbus_string_init_const
void _dbus_string_init_const(DBusString *str, const char *value)
Initializes a constant string.
Definition: dbus-string.c:190
dbus_new0
#define dbus_new0(type, count)
Definition: dbus-memory.h:58
dbus_bool_t
dbus_uint32_t dbus_bool_t
Definition: dbus-types.h:35
NULL
#define NULL