]> git.cworth.org Git - ttt/blob - src/x.c
2005-11-09 Carl Worth <cworth@cworth.org>
[ttt] / src / x.c
1 /* x.c - Some wrappers for various functions to check and exit on out-of-memory
2  *
3  * Copyright © 2005 Carl Worth
4  *
5  * This program is free software; you can redistribute it and/or modify
6  * it under the terms of the GNU General Public License as published by
7  * the Free Software Foundation; either version 2, or (at your option)
8  * any later version.
9  *
10  * This program is distributed in the hope that it will be useful,
11  * but WITHOUT ANY WARRANTY; without even the implied warranty of
12  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
13  * GNU General Public License for more details.
14  *
15  * You should have received a copy of the GNU General Public License
16  * along with this program; if not, write to the Free Software Foundation,
17  * Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA.
18  *
19  * Author: Carl Worth <carl@theworths.org>
20  */
21
22 #include "ttt.h"
23
24 #include <stdarg.h>
25
26 void
27 xasprintf (char **strp, const char *fmt, ...)
28 {
29     va_list ap;
30
31     va_start (ap, fmt);
32     xvasprintf (strp, fmt, ap);
33     va_end (ap);
34 }
35
36 void
37 xvasprintf (char **strp, const char *fmt, va_list ap)
38 {
39     int ret;
40
41     ret = vasprintf (strp, fmt, ap);
42
43     if (ret == -1) {
44         fprintf (stderr, "Error: vasprintf failed: Out of memory? Aborting.\n");
45         exit (1);
46     }
47 }
48
49 void
50 xpipe (int filedes[2])
51 {
52     int ret;
53
54     ret = pipe (filedes);
55
56     if (ret == -1) {
57         fprintf (stderr, "Error: pipe failed: %s. Aborting.\n", strerror (errno));
58         exit (1);
59     }
60 }
61
62 pid_t
63 xfork (void)
64 {
65     pid_t pid;
66
67     pid = fork ();
68
69     if (pid == -1) {
70         fprintf (stderr, "Error: fork failed: %s. Aborting.\n", strerror (errno));
71         exit (1);
72     }
73
74     return pid;
75 }
76
77 void *
78 xmalloc (size_t size)
79 {
80     void *ret;
81
82     ret = malloc (size);
83
84     if (ret == NULL) {
85         fprintf (stderr, "Error: malloc failed. Out of memory.  Aborting.\n");
86         exit (1);
87     }
88
89     return ret;
90 }
91
92 void *
93 xcalloc (size_t nmemb, size_t size)
94 {
95     void *ret;
96
97     ret = calloc (nmemb, size);
98
99     if (ret == NULL) {
100         fprintf (stderr, "Error: calloc failed. Out of memory. Aborting.\n");
101         exit (1);
102     }
103
104     return ret;
105 }
106
107 void *
108 xrealloc (void *ptr, size_t size)
109 {
110     void *ret;
111
112     ret = realloc (ptr, size);
113
114     if (ret == NULL) {
115         fprintf (stderr, "Error: realloc failed: Out of memory. Aborting.\n");
116         exit (1);
117     }
118
119     return ret;
120 }
121
122 FILE *
123 xfdopen (int filedes, const char *mode)
124 {
125     FILE *ret;
126
127     ret = fdopen (filedes, mode);
128
129     if (ret == NULL) {
130         fprintf (stderr, "Error: fdopen failed: %s. Aborting.\n",
131                  strerror (errno));
132         exit (1);
133     }
134
135     return ret;
136 }
137
138 FILE *
139 xfreopen (const char *path, const char *mode, FILE *stream)
140 {
141     FILE *ret;
142
143     ret = freopen (path, mode, stream);
144     if (ret == NULL) {
145         fprintf (stderr, "Error: freopen of %s failed: %s. Aborting.\n",
146                  path, strerror (errno));
147         exit (1);
148     }
149
150     return ret;
151 }
152
153 char *
154 xstrdup (const char *s)
155 {
156     char *ret;
157
158     ret = strdup (s);
159
160     if (ret == NULL) {
161         fprintf (stderr, "Error: strdup failed: %s. Aborting.\n",
162                  strerror (errno));
163         exit (1);
164     }
165
166     return ret;
167 }
168
169 void
170 xfwrite (const void *ptr, size_t size, size_t nmemb, FILE *stream)
171 {
172     int ret;
173
174     ret = fwrite (ptr, size, nmemb, stream);
175     if (ret != nmemb) {
176         fprintf (stderr, "Error: error occured during fwrite. Aborting.\n");
177         exit (1);
178     }
179 }
180
181 int
182 xsocket (int domain, int type, int protocol)
183 {
184     int ret;
185
186     ret = socket (domain, type, protocol);
187     if (ret == -1) {
188         fprintf (stderr, "Error: socket failed: %s. Aborting.\n",
189                  strerror (errno));
190         exit (1);
191     }
192
193     return ret;
194 }
195
196 void
197 xbind (int sockfd, const struct sockaddr *my_addr, socklen_t addrlen)
198 {
199     int ret;
200
201     ret = bind (sockfd, my_addr, addrlen);
202     if (ret == -1) {
203         fprintf (stderr, "Error: bind failed: %s. Aborting.\n",
204                  strerror (errno));
205         exit (1);
206     }
207 }
208
209 void
210 xlisten (int s, int backlog)
211 {
212     int ret;
213
214     ret = listen (s, backlog);
215     if (ret == -1) {
216         fprintf (stderr, "Error: listen failed: %s. Aborting.\n",
217                  strerror (errno));
218         exit (1);
219     }
220 }
221
222 int
223 xfcntl (int fd, int cmd, long arg)
224 {
225     int ret;
226
227     ret = fcntl (fd, cmd, arg);
228     if (ret == -1) {
229         fprintf (stderr, "Error: fcntl failed: %s. Aborting.\n",
230                  strerror (errno));
231         exit (1);
232     }
233
234     return ret;
235 }
236
237 int
238 xselect (int             n,
239          fd_set         *readfds,
240          fd_set         *writefds,
241          fd_set         *exceptfds,
242          struct timeval *timeout)
243 {
244     int ret;
245
246     ret = select (n, readfds, writefds, exceptfds, timeout);
247     if (ret == -1) {
248         fprintf (stderr, "Error: select failed: %s. Aborting.\n",
249                  strerror (errno));
250         exit (1);
251     }
252
253     return ret;
254 }