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