[go: up one dir, main page]

Menu

[r456]: / tags / 0.6.4 / src / common.c  Maximize  Restore  History

Download this file

265 lines (209 with data), 5.1 kB

  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
/* $Id$
* -------------------------------------------------------
* Copyright (C) 2002-2005 Tommi Saviranta <wnd@iki.fi>
* (C) 2002 Sebastian Kienzl <zap@riot.org>
* -------------------------------------------------------
* This program 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 of the License, or
* (at your option) any later version.
*
* This program 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.
*/
#ifdef HAVE_CONFIG_H
#include <config.h>
#endif /* ifdef HAVE_CONFIG_H */
#include "common.h"
#include "error.h"
#include "messages.h"
#include <stdlib.h>
#include <string.h>
#define REPORT_ERROR(func) error(ERR_UNEXPECTED, func, file, line)
#if HAVE_MALLOC != 1
/*
* Need to undef. Will cause warnings, but until someone can provide me a
* better way to do this, warnings shall we have. Same goes for rpl_realloc.
*/
#undef malloc
#include <stdlib.h>
void *
rpl_malloc(size_t size)
{
if (size == 0) {
size = 1;
}
return malloc(size);
} /* void *rpl_malloc(size_t size) */
#endif /* if HAVE_MALLOC != 1 */
#if HAVE_REALLOC != 1
#undef realloc
#include <stdlib.h>
void *
rpl_realloc(void *ptr, size_t size)
{
if (size == 0) {
size = 1;
if (ptr != NULL) {
free(ptr);
ptr = NULL;
}
}
if (ptr == NULL) {
return malloc(size);
} else {
return realloc(ptr, size);
}
} /* void *rpl_realloc(void *ptr, size_t size) */
#endif /* if HAVE_REALLOC != 1 */
int
_xstrcmp(const char *s1, const char *s2 DEBUG_ADDPARMS)
{
if (s1 != NULL && s2 != NULL) {
return strcmp(s1, s2);
} else {
#ifdef DEBUG_ADDPARMS
REPORT_ERROR("xstrcmp");
#endif
return 1;
}
} /* int _xstrcmp(const char *s1, const char *s2 DEBUG_ADDPARMS) */
int
_xstrncmp(const char *s1, const char *s2, size_t n DEBUG_ADDPARMS)
{
if (s1 != NULL && s2 != NULL) {
return strncmp(s1, s2, n);
} else {
#ifdef DEBUG_ADDPARMS
REPORT_ERROR("xstrncmp");
#endif
return 1;
}
} /* int _xstrncmp(const char *s1, const char *s2, size_t n DEBUG_ADDPARMS) */
int
_xstrcasecmp(const char *s1, const char *s2 DEBUG_ADDPARMS)
{
if (s1 != NULL && s2 != NULL) {
return strcasecmp(s1, s2);
} else {
#ifdef DEBUG_ADDPARMS
REPORT_ERROR("xstrcasecmp");
#endif
return 1;
}
} /* int _xstrcasecmp(const char *s1, const char *s2 DEBUG_ADDPARMS) */
int
_xstrncasecmp(const char *s1, const char *s2, size_t n DEBUG_ADDPARMS)
{
if (s1 != NULL && s2 != NULL) {
return strncasecmp(s1, s2, n);
} else {
#ifdef DEBUG_ADDPARMS
REPORT_ERROR("xstrncasecmp");
#endif
return 1;
}
} /* int _xstrncasecmp(const char *s1, const char *s2, size_t n
DEBUG_ADDPARMS) */
char *
_xstrcpy(char *dest, const char *src DEBUG_ADDPARMS)
{
if (dest != NULL && src != NULL) {
return strcpy(dest, src); /* dangerous, but can't help it */
} else {
#ifdef DEBUG_ADDPARMS
REPORT_ERROR("xstrcpy");
#endif
return 0;
}
} /* char *_xstrcpy(char *dest, const char *src DEBUG_ADDPARMS) */
char *
_xstrncpy(char *dest, const char *src, size_t n DEBUG_ADDPARMS)
{
if (dest != NULL && src != NULL) {
return strncpy(dest, src, n);
} else {
#ifdef DEBUG_ADDPARMS
REPORT_ERROR("xstrncpy");
#endif
return 0;
}
} /* char *_xstrncpy(char *dest, const char *src, size_t n DEBUG_ADDPARMS) */
char *
_xstrdup(const char *s DEBUG_ADDPARMS)
{
char *p;
if (s != NULL) {
p = strdup(s);
if (p == NULL) {
error(ERR_MEMORY);
exit(ERR_CODE_MEMORY);
}
return p;
} else {
#ifdef DEBUG_ADDPARMS
REPORT_ERROR("xstrdup");
#endif
return NULL;
}
} /* char *_xstrdup(const char *s DEBUG_ADDPARMS) */
void *
xmalloc(size_t size)
{
void *ret = malloc(size);
if (ret == NULL) {
error(ERR_MEMORY);
exit(ERR_CODE_MEMORY);
}
return ret;
} /* void *xmalloc(size_t size) */
void *
xcalloc(size_t nmemb, size_t size)
{
void *ret = calloc(nmemb, size);
if (ret == NULL) {
error(ERR_MEMORY);
exit(ERR_CODE_MEMORY);
}
return ret;
} /* void *xcalloc(size_t nmemb, size_t size) */
void
xfree(void *ptr)
{
if (ptr != NULL) {
free(ptr);
}
} /* void xfree(void *ptr) */
void *
xrealloc(void *ptr, size_t size)
{
void *ret = realloc(ptr, size);
if (ret == NULL && size > 0) {
error(ERR_MEMORY);
exit(ERR_CODE_MEMORY);
}
return ret;
} /* void *xrealloc(void *ptr, size_t size) */
/* these functions are "intentionally" unsafe! */
#ifndef HAVE_SNPRINTF
int
snprintf(char *str, size_t size, const char *format, ...)
{
int r;
va_list va;
va_start(va, format);
r = sprintf(str, format, va);
va_end(va);
str[size - 1] = '\0';
return r;
} /* int snprintf(char *str, size_t size, const char *format, ...) */
#endif /* ifndef HAVE_SNPRINTF */
#ifndef HAVE_VSNPRINTF
int
vsnprintf(char *str, size_t size, const char *format, va_list ap)
{
return vsprintf(str, format, ap);
} /* int vsnprintf(char *str, size_t size, const char *format, va_list ap) */
#endif /* ifndef HAVE_VSNPRINTF */