1 /* -*- mode: C; c-file-style: "gnu" -*- */
2 /* dbus-sysdeps.c Wrappers around system/libc features (internal to D-BUS implementation)
4 * Copyright (C) 2002 Red Hat, Inc.
6 * Licensed under the Academic Free License version 1.2
8 * This program is free software; you can redistribute it and/or modify
9 * it under the terms of the GNU General Public License as published by
10 * the Free Software Foundation; either version 2 of the License, or
11 * (at your option) any later version.
13 * This program is distributed in the hope that it will be useful,
14 * but WITHOUT ANY WARRANTY; without even the implied warranty of
15 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
16 * GNU General Public License for more details.
18 * You should have received a copy of the GNU General Public License
19 * along with this program; if not, write to the Free Software
20 * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
24 #include "dbus-sysdeps.h"
32 * @addtogroup DBusInternalsUtils
36 * Aborts the program with SIGABRT (dumping core).
42 _exit (1); /* in case someone manages to ignore SIGABRT */
46 * Wrapper for getenv().
48 * @param varname name of environment variable
49 * @returns value of environment variable or #NULL if unset
52 _dbus_getenv (const char *varname)
54 return getenv (varname);
60 * @addtogroup DBusString
65 * Appends an integer to a DBusString.
67 * @param str the string
68 * @param value the integer value
69 * @returns #FALSE if not enough memory or other failure.
72 _dbus_string_append_int (DBusString *str,
75 /* this calculation is from comp.lang.c faq */
76 #define MAX_LONG_LEN ((sizeof (long) * 8 + 2) / 3 + 1) /* +1 for '-' */
81 orig_len = _dbus_string_get_length (str);
83 if (!_dbus_string_lengthen (str, MAX_LONG_LEN))
86 _dbus_string_get_data_len (str, &buf, orig_len, MAX_LONG_LEN);
88 snprintf (buf, MAX_LONG_LEN, "%ld", value);
97 _dbus_string_shorten (str, MAX_LONG_LEN - i);
103 * Appends a double to a DBusString.
105 * @param str the string
106 * @param value the floating point value
107 * @returns #FALSE if not enough memory or other failure.
110 _dbus_string_append_double (DBusString *str,
113 #define MAX_DOUBLE_LEN 64 /* this is completely made up :-/ */
118 orig_len = _dbus_string_get_length (str);
120 if (!_dbus_string_lengthen (str, MAX_DOUBLE_LEN))
123 _dbus_string_get_data_len (str, &buf, orig_len, MAX_DOUBLE_LEN);
125 snprintf (buf, MAX_LONG_LEN, "%g", value);
134 _dbus_string_shorten (str, MAX_DOUBLE_LEN - i);
140 * Parses an integer contained in a DBusString. Either return parameter
141 * may be #NULL if you aren't interested in it. The integer is parsed
142 * and stored in value_return. Return parameters are not initialized
143 * if the function returns #FALSE.
145 * @param str the string
146 * @param start the byte index of the start of the integer
147 * @param value_return return location of the integer value or #NULL
148 * @param end_return return location of the end of the integer, or #NULL
149 * @returns #TRUE on success
152 _dbus_string_parse_int (const DBusString *str,
161 _dbus_string_get_const_data_len (str, &p, start,
162 _dbus_string_get_length (str) - start);
166 v = strtol (p, &end, 0);
167 if (end == NULL || end == p || errno != 0)
173 *end_return = (end - p);
179 * Parses a floating point number contained in a DBusString. Either
180 * return parameter may be #NULL if you aren't interested in it. The
181 * integer is parsed and stored in value_return. Return parameters are
182 * not initialized if the function returns #FALSE.
184 * @todo this function is currently locale-dependent. Should
185 * ask alexl to relicense g_ascii_strtod() code and put that in
186 * here instead, so it's locale-independent.
188 * @param str the string
189 * @param start the byte index of the start of the float
190 * @param value_return return location of the float value or #NULL
191 * @param end_return return location of the end of the float, or #NULL
192 * @returns #TRUE on success
195 _dbus_string_parse_double (const DBusString *str,
197 double *value_return,
204 _dbus_warn ("_dbus_string_parse_double() needs to be made locale-independent\n");
206 _dbus_string_get_const_data_len (str, &p, start,
207 _dbus_string_get_length (str) - start);
211 v = strtod (p, &end);
212 if (end == NULL || end == p || errno != 0)
218 *end_return = (end - p);
223 /** @} end of DBusString */