summaryrefslogtreecommitdiff
path: root/libspectre/spectre-utils.c
blob: 0d8cf02da2dde10ea927f01e1392ff727d2ded76 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
/* This file is part of Libspectre.
 * 
 * Copyright (C) 2007 Albert Astals Cid <aacid@kde.org>
 * Copyright (C) 2007 Carlos Garcia Campos <carlosgc@gnome.org>
 *
 * Libspectre is free software; you can redistribute it and/or modify
 * it under the terms of the GNU General Public License as published by
 * the Free Software Foundation; either version 2, or (at your option)
 * any later version.
 *
 * Libspectre 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 General Public License for more details.
 *
 * 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., 51 Franklin Street, Fifth Floor, Boston, MA  02110-1301, USA.
 */

#define _GNU_SOURCE
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <errno.h>
#include <locale.h>

#include "spectre-utils.h"

#ifdef HAVE_SYS_TYPES_H
#include <sys/types.h>
#endif
#ifdef HAVE_UNISTD_H
#include <unistd.h>
#endif

#ifdef WIN32
#include <windows.h>
#endif

static unsigned long
_spectre_get_pid (void)
{
#if defined(HAVE_SYS_TYPES_H) && defined(HAVE_UNISTD_H)
	return getpid ();
#elif defined(WIN32)
	return GetCurrentProcessId ();
#endif
}

static int warn_initted = FALSE;
static int fatal_warnings = FALSE;
static int fatal_warnings_on_check_failed = FALSE;

static void
init_warnings (void)
{
	const char *s;
	
	if (warn_initted)
		return;

	warn_initted = TRUE;
	
	s = getenv ("SPECTRE_FATAL_WARNINGS");
	if (!s || !(*s))
		return;

	if (*s == '0') {
		fatal_warnings = FALSE;
		fatal_warnings_on_check_failed = FALSE;
	} else if (*s == '1') {
		fatal_warnings = TRUE;
		fatal_warnings_on_check_failed = TRUE;
	} else {
		fprintf (stderr,
			 "SPECTRE_FATAL_WARNINGS should be set to 0 or 1 if set, not '%s'",
			 s);
	}
}

/**
 * Prints a warning message to stderr. Can optionally be made to exit
 * fatally by setting SPECTRE_FATAL_WARNINGS, but this is rarely
 * used. This function should be considered pretty much equivalent to
 * fprintf(stderr). _spectre_warn_check_failed() on the other hand is
 * suitable for use when a programming mistake has been made.
 */
void
_spectre_warn (const char *format,
            ...)
{
	va_list args;

	if (!warn_initted)
		init_warnings ();
  
	va_start (args, format);
	vfprintf (stderr, format, args);
	va_end (args);

	if (fatal_warnings) {
		fflush (stderr);
		abort ();
	}
}

/**
 * Prints a "critical" warning to stderr when an assertion fails;
 * differs from _spectre_warn primarily in that it prefixes the pid and
 * defaults to fatal. This should be used only when a programming
 * error has been detected. (NOT for unavoidable errors that an app
 * might handle. Calling this means "there is a bug"
 */
void
_spectre_warn_check_failed (const char *format,
			    ...)
{
	va_list args;

	if (!warn_initted)
		init_warnings ();

	fprintf (stderr, "process %lu: ", _spectre_get_pid ());

	va_start (args, format);
	vfprintf (stderr, format, args);
	va_end (args);

	if (fatal_warnings_on_check_failed) {
		fflush (stderr);
		abort ();
	}
}

#ifndef SPECTRE_DISABLE_ASSERT
void
_spectre_real_assert (int          condition,
		      const char  *condition_text,
		      const char  *file,
		      int          line,
		      const char  *func)
{
	if (_SPECTRE_UNLIKELY (!condition)) {
		_spectre_warn ("%lu: assertion failed \"%s\" file \"%s\" line %d function %s\n",
			       _spectre_get_pid (), condition_text, file, line, func);
		abort ();
	}
}
#endif /* SPECTRE_DISABLE_ASSERT */

static char *
spectre_strdup_vprintf (const char *format,
			va_list     args)
{
	char *string = NULL;
	int len;
#if defined(HAVE_VASPRINTF)
	len = vasprintf (&string, format, args);
	
	if (len < 0)
		string = NULL;
#else /* !HAVE_VASPRINTF */
	va_list args_copy;
	char c;

	SPECTRE_VA_COPY (args_copy, args);

	string = malloc ((vsnprintf (&c, 1, format, args) + 1) * sizeof (char));
	if (string) {
		len = vsprintf (string, format, args_copy);
		if (len < 0) {
			free (string);
			string = NULL;
		}
	}

	va_end (args_copy);
#endif	
	
	return string;
}

char *
_spectre_strdup_printf (const char *format, ...)
{
	char *buffer;
	va_list args;
	
	va_start (args, format);
	buffer = spectre_strdup_vprintf (format, args);
	va_end (args);
	
	return buffer;
}

char *
_spectre_strdup (const char *str)
{
	size_t len;
	char *copy;

	if (!str)
		return NULL;

	len = strlen (str) + 1;

	copy = malloc (len);
	if (!copy)
		return NULL;

	memcpy (copy, str, len);

	return copy; 
}

#define TOLOWER(c) (((c) >= 'A' && (c) <= 'Z') ? (c) - 'A' + 'a' : (c))
int
_spectre_strncasecmp (const char *s1,
		      const char *s2,
		      size_t      n)
{
	int c1, c2;

	while (n && *s1 && *s2)	{
		n -= 1;
		c1 = (int)(unsigned char) TOLOWER (*s1);
		c2 = (int)(unsigned char) TOLOWER (*s2);
		if (c1 != c2)
			return (c1 - c2);
		s1++;
		s2++;
	}

	return (n) ? (((int) (unsigned char) *s1) - ((int) (unsigned char) *s2)) : 0;
}

int
_spectre_strcasecmp (const char *s1,
		     const char *s2)
{
	int c1, c2;

	while (*s1 && *s2) {
		c1 = (int)(unsigned char) TOLOWER (*s1);
		c2 = (int)(unsigned char) TOLOWER (*s2);
		if (c1 != c2)
			return (c1 - c2);
		s1++;
		s2++;
	}

	return (((int)(unsigned char) *s1) - ((int)(unsigned char) *s2));
}

#define ascii_isspace(c) \
	(c == ' ' || c == '\f' || c == '\n' || c == '\r' || c == '\t' || c == '\v')
#define ascii_isdigit(c) \
	(c >= '0' && c <= '9')

/* This function behaves like the standard strtod() function
 * does in the C locale. It does this without actually changing
 * the current locale, since that would not be thread-safe.
 * A limitation of the implementation is that this function
 * will still accept localized versions of infinities and NANs.
 */
double
_spectre_strtod (const char *nptr,
		 char      **endptr)
{
	char *fail_pos;
	double val;
	struct lconv *locale_data;
	const char *decimal_point;
	int decimal_point_len;
	const char *p, *decimal_point_pos;
	const char *end = NULL; /* Silence gcc */
	int strtod_errno;

	fail_pos = NULL;
	
	locale_data = localeconv ();
	decimal_point = locale_data->decimal_point;
	decimal_point_len = strlen (decimal_point);

	decimal_point_pos = NULL;
	end = NULL;

	if (decimal_point[0] != '.' || decimal_point[1] != 0) {
		p = nptr;
		/* Skip leading space */
		while (ascii_isspace (*p))
			p++;

		/* Skip leading optional sign */
		if (*p == '+' || *p == '-')
			p++;

		if (ascii_isdigit (*p) || *p == '.') {
			while (ascii_isdigit (*p))
				p++;

			if (*p == '.')
				decimal_point_pos = p++;

			while (ascii_isdigit (*p))
				p++;

			if (*p == 'e' || *p == 'E')
				p++;
			if (*p == '+' || *p == '-')
				p++;
			while (ascii_isdigit (*p))
				p++;

			end = p;
		}
		/* For the other cases, we need not convert the decimal point */
	}

	if (decimal_point_pos) {
		char *copy, *c;

		/* We need to convert the '.' to the locale specific decimal point */
		copy = (char *) malloc (end - nptr + 1 + decimal_point_len);

		c = copy;
		memcpy (c, nptr, decimal_point_pos - nptr);
		c += decimal_point_pos - nptr;
		memcpy (c, decimal_point, decimal_point_len);
		c += decimal_point_len;
		memcpy (c, decimal_point_pos + 1, end - (decimal_point_pos + 1));
		c += end - (decimal_point_pos + 1);
		*c = 0;

		errno = 0;
		val = strtod (copy, &fail_pos);
		strtod_errno = errno;

		if (fail_pos) {
			if (fail_pos - copy > decimal_point_pos - nptr)
				fail_pos = (char *)nptr + (fail_pos - copy) - (decimal_point_len - 1);
			else
				fail_pos = (char *)nptr + (fail_pos - copy);
		}

		free (copy);
	} else if (end)	{
		char *copy;
		
		copy = (char *) malloc (end - (char *)nptr + 1);
		memcpy (copy, nptr, end - nptr);
		*(copy + (end - (char *)nptr)) = 0;
		
		errno = 0;
		val = strtod (copy, &fail_pos);
		strtod_errno = errno;
		
		if (fail_pos) {
			fail_pos = (char *)nptr + (fail_pos - copy);
		}

		free (copy);
	} else {
		errno = 0;
		val = strtod (nptr, &fail_pos);
		strtod_errno = errno;
	}

	if (endptr)
		*endptr = fail_pos;

	errno = strtod_errno;

	return val;
}