]> git.cworth.org Git - ttt/blob - src/x.c
* src/ttt-curses-client.c Fixed backspace implementation
[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     if (s == NULL)
155         return NULL;
156
157     ret = strdup (s);
158
159     if (ret == NULL) {
160         fprintf (stderr, "Error: strdup failed: %s. Aborting.\n",
161                  strerror (errno));
162         exit (1);
163     }
164
165     return ret;
166 }
167
168 void
169 xfwrite (const void *ptr, size_t size, size_t nmemb, FILE *stream)
170 {
171     int ret;
172
173     ret = fwrite (ptr, size, nmemb, stream);
174     if (ret != nmemb) {
175         fprintf (stderr, "Error: error occured during fwrite. Aborting.\n");
176         exit (1);
177     }
178 }
179
180 int
181 xsocket (int domain, int type, int protocol)
182 {
183     int ret;
184
185     ret = socket (domain, type, protocol);
186     if (ret == -1) {
187         fprintf (stderr, "Error: socket failed: %s. Aborting.\n",
188                  strerror (errno));
189         exit (1);
190     }
191
192     return ret;
193 }
194
195 void
196 xbind (int sockfd, const struct sockaddr *my_addr, socklen_t addrlen)
197 {
198     int ret;
199
200     ret = bind (sockfd, my_addr, addrlen);
201     if (ret == -1) {
202         fprintf (stderr, "Error: bind failed: %s. Aborting.\n",
203                  strerror (errno));
204         exit (1);
205     }
206 }
207
208 ttt_status_t
209 xconnect (int sockfd, const struct sockaddr *serv_addr, socklen_t addrlen)
210 {
211     int ret;
212
213     ret = connect (sockfd, serv_addr, addrlen);
214     if (ret == -1) {
215         switch (errno) {
216         case ECONNREFUSED:
217             return TTT_STATUS_CONNECTION_REFUSED;
218         case EHOSTUNREACH:
219         case ENETUNREACH:
220             return TTT_STATUS_NETWORK_UNREACHABLE;
221         default:
222         fprintf (stderr, "Error: connect failed (errno = %d): %s. Aborting.\n",
223                  errno, strerror (errno));
224         exit (1);
225         }
226     }
227
228     return TTT_STATUS_SUCCESS;
229 }
230
231 void
232 xlisten (int s, int backlog)
233 {
234     int ret;
235
236     ret = listen (s, backlog);
237     if (ret == -1) {
238         fprintf (stderr, "Error: listen failed: %s. Aborting.\n",
239                  strerror (errno));
240         exit (1);
241     }
242 }
243
244 int
245 xfcntl (int fd, int cmd, long arg)
246 {
247     int ret;
248
249     ret = fcntl (fd, cmd, arg);
250     if (ret == -1) {
251         fprintf (stderr, "Error: fcntl failed: %s. Aborting.\n",
252                  strerror (errno));
253         exit (1);
254     }
255
256     return ret;
257 }
258
259 int
260 xselect (int             n,
261          fd_set         *readfds,
262          fd_set         *writefds,
263          fd_set         *exceptfds,
264          struct timeval *timeout)
265 {
266     int ret;
267
268     ret = select (n, readfds, writefds, exceptfds, timeout);
269     if (ret == -1) {
270         fprintf (stderr, "Error: select failed: %s. Aborting.\n",
271                  strerror (errno));
272         exit (1);
273     }
274
275     return ret;
276 }
277
278 ssize_t
279 xread (int fd, void *buf, size_t count)
280 {
281     int ret;
282
283     ret = read (fd, buf, count);
284     if (ret == -1) {
285         fprintf (stderr, "Error: read failed: %s. Aborting.\n",
286                  strerror (errno));
287         exit (1);
288     }
289
290     return ret;
291 }
292
293 ssize_t
294 xwrite (int fd, const void *buf, size_t count)
295 {
296     int ret;
297
298     ret = write (fd, buf, count);
299     if (ret == -1) {
300         fprintf (stderr, "Error: write failed: %s. Aborting.\n",
301                  strerror (errno));
302         exit (1);
303     }
304
305     return ret;
306 }