]> git.cworth.org Git - ttt/blob - src/x.c
18c69a47457bf786aa9899999ed419ea96cdb4dd
[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 xfdopen (int filedes, const char *mode)
122 {
123     FILE *ret;
124
125     ret = fdopen (filedes, mode);
126
127     if (ret == NULL) {
128         fprintf (stderr, "Error: fdopen failed: %s. Aborting.\n",
129                  strerror (errno));
130         exit (1);
131     }
132
133     return ret;
134 }
135
136 void
137 xfreopen (const char *path, const char *mode, FILE *stream)
138 {
139     FILE *ret;
140
141     ret = freopen (path, mode, stream);
142     if (ret == NULL) {
143         fprintf (stderr, "Error: freopen of %s failed: %s. Aborting.\n",
144                  path, strerror (errno));
145         exit (1);
146     }
147 }
148
149 char *
150 xstrdup (const char *s)
151 {
152     char *ret;
153
154     ret = strdup (s);
155
156     if (ret == NULL) {
157         fprintf (stderr, "Error: strdup failed: %s. Aborting.\n",
158                  strerror (errno));
159         exit (1);
160     }
161
162     return ret;
163 }
164
165 void
166 xfwrite (const void *ptr, size_t size, size_t nmemb, FILE *stream)
167 {
168     int ret;
169
170     ret = fwrite (ptr, size, nmemb, stream);
171     if (ret != nmemb) {
172         fprintf (stderr, "Error: error occured during fwrite. Aborting.\n");
173         exit (1);
174     }
175 }
176
177 int
178 xsocket (int domain, int type, int protocol)
179 {
180     int ret;
181
182     ret = socket (domain, type, protocol);
183     if (ret == -1) {
184         fprintf (stderr, "Error: socket failed: %s. Aborting.\n",
185                  strerror (errno));
186         exit (1);
187     }
188
189     return ret;
190 }
191
192 void
193 xbind (int sockfd, const struct sockaddr *my_addr, socklen_t addrlen)
194 {
195     int ret;
196
197     ret = bind (sockfd, my_addr, addrlen);
198     if (ret == -1) {
199         fprintf (stderr, "Error: bind failed: %s. Aborting.\n",
200                  strerror (errno));
201         exit (1);
202     }
203 }
204
205 void
206 xlisten (int s, int backlog)
207 {
208     int ret;
209
210     ret = listen (s, backlog);
211     if (ret == -1) {
212         fprintf (stderr, "Error: listen failed: %s. Aborting.\n",
213                  strerror (errno));
214         exit (1);
215     }
216 }
217
218 int
219 xfcntl (int fd, int cmd, long arg)
220 {
221     int ret;
222
223     ret = fcntl (fd, cmd, arg);
224     if (ret == -1) {
225         fprintf (stderr, "Error: fcntl failed: %s. Aborting.\n",
226                  strerror (errno));
227         exit (1);
228     }
229
230     return ret;
231 }
232
233 int
234 xselect (int             n,
235          fd_set         *readfds,
236          fd_set         *writefds,
237          fd_set         *exceptfds,
238          struct timeval *timeout)
239 {
240     int ret;
241
242     ret = select (n, readfds, writefds, exceptfds, timeout);
243     if (ret == -1) {
244         fprintf (stderr, "Error: select failed: %s. Aborting.\n",
245                  strerror (errno));
246         exit (1);
247     }
248
249     return ret;
250 }