]> git.cworth.org Git - ttt/blob - src/x.c
Add a dependency of ttt-client.c on ttt-lex.h to fix the build.
[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 "x.h"
23
24 void
25 xasprintf (char **strp, const char *fmt, ...)
26 {
27     va_list ap;
28
29     va_start (ap, fmt);
30     xvasprintf (strp, fmt, ap);
31     va_end (ap);
32 }
33
34 void
35 xvasprintf (char **strp, const char *fmt, va_list ap)
36 {
37     int ret;
38
39     ret = vasprintf (strp, fmt, ap);
40
41     if (ret == -1) {
42         fprintf (stderr, "Error: vasprintf failed: Out of memory? Aborting.\n");
43         exit (1);
44     }
45 }
46
47 void
48 xpipe (int filedes[2])
49 {
50     int ret;
51
52     ret = pipe (filedes);
53
54     if (ret == -1) {
55         fprintf (stderr, "Error: pipe failed: %s. Aborting.\n", strerror (errno));
56         exit (1);
57     }
58 }
59
60 pid_t
61 xfork (void)
62 {
63     pid_t pid;
64
65     pid = fork ();
66
67     if (pid == -1) {
68         fprintf (stderr, "Error: fork failed: %s. Aborting.\n", strerror (errno));
69         exit (1);
70     }
71
72     return pid;
73 }
74
75 void *
76 xmalloc (size_t size)
77 {
78     void *ret;
79
80     ret = malloc (size);
81
82     if (ret == NULL) {
83         fprintf (stderr, "Error: malloc failed. Out of memory.  Aborting.\n");
84         exit (1);
85     }
86
87     return ret;
88 }
89
90 void *
91 xcalloc (size_t nmemb, size_t size)
92 {
93     void *ret;
94
95     ret = calloc (nmemb, size);
96
97     if (ret == NULL) {
98         fprintf (stderr, "Error: calloc failed. Out of memory. Aborting.\n");
99         exit (1);
100     }
101
102     return ret;
103 }
104
105 void *
106 xrealloc (void *ptr, size_t size)
107 {
108     void *ret;
109
110     ret = realloc (ptr, size);
111
112     if (ret == NULL) {
113         fprintf (stderr, "Error: realloc failed: Out of memory. Aborting.\n");
114         exit (1);
115     }
116
117     return ret;
118 }
119
120 FILE *
121 xfopen (const char *path, const char *mode)
122 {
123     FILE *ret;
124
125     ret = fopen (path, mode);
126
127     if (ret == NULL) {
128         fprintf (stderr, "Error: fopen of %s failed: %s. Aborting.\n",
129                  path, strerror (errno));
130         exit (1);
131     }
132
133     return ret;
134 }
135
136 FILE *
137 xfdopen (int filedes, const char *mode)
138 {
139     FILE *ret;
140
141     ret = fdopen (filedes, mode);
142
143     if (ret == NULL) {
144         fprintf (stderr, "Error: fdopen failed: %s. Aborting.\n",
145                  strerror (errno));
146         exit (1);
147     }
148
149     return ret;
150 }
151
152 void
153 xdup2 (int oldfd, int newfd)
154 {
155     int ret;
156
157     ret = dup2 (oldfd, newfd);
158     if (ret == -1) {
159         printf ("Error: dup2 failed: %s. Aborting.\n",
160                 strerror (errno));
161         exit (1);
162     }
163 }
164
165 char *
166 xstrdup (const char *s)
167 {
168     char *ret;
169
170     if (s == NULL)
171         return NULL;
172
173     ret = strdup (s);
174
175     if (ret == NULL) {
176         fprintf (stderr, "Error: strdup failed: %s. Aborting.\n",
177                  strerror (errno));
178         exit (1);
179     }
180
181     return ret;
182 }
183
184 void
185 xfwrite (const void *ptr, size_t size, size_t nmemb, FILE *stream)
186 {
187     int ret;
188
189     ret = fwrite (ptr, size, nmemb, stream);
190     if (ret != nmemb) {
191         fprintf (stderr, "Error: error occured during fwrite. Aborting.\n");
192         exit (1);
193     }
194 }
195
196 int
197 xsocket (int domain, int type, int protocol)
198 {
199     int ret;
200
201     ret = socket (domain, type, protocol);
202     if (ret == -1) {
203         fprintf (stderr, "Error: socket failed: %s. Aborting.\n",
204                  strerror (errno));
205         exit (1);
206     }
207
208     return ret;
209 }
210
211 void
212 xbind (int sockfd, const struct sockaddr *my_addr, socklen_t addrlen)
213 {
214     int ret;
215
216     ret = bind (sockfd, my_addr, addrlen);
217     if (ret == -1) {
218         fprintf (stderr, "Error: bind failed: %s. Aborting.\n",
219                  strerror (errno));
220         exit (1);
221     }
222 }
223
224 ttt_status_t
225 xconnect (int sockfd, const struct sockaddr *serv_addr, socklen_t addrlen)
226 {
227     int ret;
228
229     ret = connect (sockfd, serv_addr, addrlen);
230     if (ret == -1) {
231         switch (errno) {
232         case ECONNREFUSED:
233             return TTT_STATUS_CONNECTION_REFUSED;
234         case EHOSTUNREACH:
235         case ENETUNREACH:
236             return TTT_STATUS_NETWORK_UNREACHABLE;
237         default:
238         fprintf (stderr, "Error: connect failed (errno = %d): %s. Aborting.\n",
239                  errno, strerror (errno));
240         exit (1);
241         }
242     }
243
244     return TTT_STATUS_SUCCESS;
245 }
246
247 void
248 xlisten (int s, int backlog)
249 {
250     int ret;
251
252     ret = listen (s, backlog);
253     if (ret == -1) {
254         fprintf (stderr, "Error: listen failed: %s. Aborting.\n",
255                  strerror (errno));
256         exit (1);
257     }
258 }
259
260 int
261 xfcntl (int fd, int cmd, long arg)
262 {
263     int ret;
264
265     ret = fcntl (fd, cmd, arg);
266     if (ret == -1) {
267         fprintf (stderr, "Error: fcntl failed: %s. Aborting.\n",
268                  strerror (errno));
269         exit (1);
270     }
271
272     return ret;
273 }
274
275 int
276 xselect (int             n,
277          fd_set         *readfds,
278          fd_set         *writefds,
279          fd_set         *exceptfds,
280          struct timeval *timeout)
281 {
282     int ret;
283
284     ret = select (n, readfds, writefds, exceptfds, timeout);
285     if (ret == -1) {
286         fprintf (stderr, "Error: select failed: %s. Aborting.\n",
287                  strerror (errno));
288         exit (1);
289     }
290
291     return ret;
292 }
293
294 ssize_t
295 xread (int fd, void *buf, size_t count)
296 {
297     int ret;
298
299     ret = read (fd, buf, count);
300     if (ret == -1) {
301         fprintf (stderr, "Error: read failed: %s. Aborting.\n",
302                  strerror (errno));
303         exit (1);
304     }
305
306     return ret;
307 }
308
309 ssize_t
310 xwrite (int fd, const void *buf, size_t count)
311 {
312     int ret;
313
314     ret = write (fd, buf, count);
315     if (ret == -1) {
316         fprintf (stderr, "Error: write failed: %s. Aborting.\n",
317                  strerror (errno));
318         exit (1);
319     }
320
321     return ret;
322 }