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