1
/*
2
 * Copyright © 2004 Red Hat, Inc.
3
 *
4
 * Permission to use, copy, modify, distribute, and sell this software
5
 * and its documentation for any purpose is hereby granted without
6
 * fee, provided that the above copyright notice appear in all copies
7
 * and that both that copyright notice and this permission notice
8
 * appear in supporting documentation, and that the name of
9
 * Red Hat, Inc. not be used in advertising or publicity pertaining to
10
 * distribution of the software without specific, written prior
11
 * permission. Red Hat, Inc. makes no representations about the
12
 * suitability of this software for any purpose.  It is provided "as
13
 * is" without express or implied warranty.
14
 *
15
 * RED HAT, INC. DISCLAIMS ALL WARRANTIES WITH REGARD TO THIS
16
 * SOFTWARE, INCLUDING ALL IMPLIED WARRANTIES OF MERCHANTABILITY AND
17
 * FITNESS, IN NO EVENT SHALL RED HAT, INC. BE LIABLE FOR ANY SPECIAL,
18
 * INDIRECT OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES WHATSOEVER
19
 * RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN ACTION
20
 * OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF OR
21
 * IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE.
22
 *
23
 * Author: Carl D. Worth <cworth@cworth.org>
24
 */
25

            
26
#include "cairo-boilerplate.h"
27
#include "cairo-boilerplate-system.h"
28

            
29
#include <stdio.h>
30
#include <stdlib.h>
31
#include <stdarg.h>
32
#ifdef HAVE_UNISTD_H
33
#include <unistd.h>
34
#endif
35
#include <errno.h>
36

            
37
void *
38
1020
xmalloc (size_t size)
39
{
40
    void *buf;
41

            
42
1020
    if (size == 0)
43
8
	return NULL;
44

            
45
1012
    buf = malloc (size);
46
1012
    if (buf == NULL) {
47
	fprintf (stderr, "Error: Out of memory. Exiting.\n");
48
	exit (1);
49
    }
50

            
51
1012
    return buf;
52
}
53

            
54
void *
55
21
xcalloc (size_t nmemb,
56
	 size_t size)
57
{
58
    void *buf;
59

            
60
21
    if (nmemb == 0 || size == 0)
61
	return NULL;
62

            
63
21
    buf = calloc (nmemb, size);
64
21
    if (buf == NULL) {
65
	fprintf (stderr, "Error: Out of memory. Exiting\n");
66
	exit (1);
67
    }
68

            
69
21
    return buf;
70
}
71

            
72
void *
73
3
xrealloc (void	 *buf,
74
	  size_t  size)
75
{
76
3
    buf = realloc (buf, size);
77
3
    if (buf == NULL && size != 0) {
78
	fprintf (stderr, "Error: Out of memory. Exiting\n");
79
	exit (1);
80
    }
81

            
82
3
    return buf;
83
}
84

            
85
void
86
58404
xasprintf (char       **strp,
87
	   const char  *fmt,
88
			...)
89
{
90
#ifdef HAVE_VASPRINTF
91
    va_list va;
92
    int ret;
93

            
94
    va_start (va, fmt);
95
    ret = vasprintf (strp, fmt, va);
96
    va_end (va);
97

            
98
    if (ret < 0) {
99
	fprintf (stderr, "Error: Out of memory. Exiting.\n");
100
	exit (1);
101
    }
102
#else /* !HAVE_VASNPRINTF */
103
#define BUF_SIZE 1024
104
    va_list va;
105
    char buffer[BUF_SIZE];
106
    int ret, len;
107

            
108
58404
    va_start (va, fmt);
109
58404
    ret = vsnprintf (buffer, sizeof (buffer), fmt, va);
110
58404
    va_end (va);
111

            
112
58404
    if (ret < 0) {
113
	fprintf (stderr, "Failure in vsnprintf\n");
114
	exit (1);
115
    }
116

            
117
58404
    len = (ret + sizeof (int)) & -sizeof (int);
118
58404
    *strp = malloc (len);
119
58404
    if (*strp == NULL) {
120
	fprintf (stderr, "Out of memory\n");
121
	exit (1);
122
    }
123

            
124
58404
    if ((unsigned) ret < sizeof (buffer)) {
125
58404
	memcpy (*strp, buffer, ret);
126
    } else {
127
	va_start (va, fmt);
128
	ret = vsnprintf (*strp, len, fmt, va);
129
	va_end (va);
130

            
131
	if (ret >= len) {
132
	    free (*strp);
133
	    fprintf (stderr, "Overflowed dynamic buffer\n");
134
	    exit (1);
135
	}
136
    }
137
58404
    memset (*strp + ret, 0, len-ret);
138
#endif /* !HAVE_VASNPRINTF */
139
58404
}
140

            
141
void
142
xunlink (const char *pathname)
143
{
144
    if (unlink (pathname) < 0 && errno != ENOENT) {
145
	fprintf (stderr, "Error: Cannot remove %s: %s\n",
146
			       pathname, strerror (errno));
147
	exit (1);
148
    }
149
}
150

            
151
char *
152
3411
xstrdup (const char *str)
153
{
154
3411
    if (str == NULL)
155
1
	return NULL;
156

            
157
3410
    str = strdup (str);
158
3410
    if (str == NULL) {
159
	fprintf (stderr, "Error: Out of memory. Exiting.\n");
160
	exit (1);
161
    }
162

            
163
3410
    return (char *) str;
164
}