1 /*
2 * Copyright (c) 2013 Holger Weiss <holger@weiss.in-berlin.de>
3 * All rights reserved.
4 *
5 * Redistribution and use in source and binary forms, with or without
6 * modification, are permitted provided that the following conditions are met:
7 *
8 * 1. Redistributions of source code must retain the above copyright notice,
9 * this list of conditions and the following disclaimer.
10 *
11 * 2. Redistributions in binary form must reproduce the above copyright notice,
12 * this list of conditions and the following disclaimer in the documentation
13 * and/or other materials provided with the distribution.
14 *
15 * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
16 * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
17 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
18 * ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE
19 * LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
20 * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
21 * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
22 * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
23 * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
24 * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
25 * POSSIBILITY OF SUCH DAMAGE.
26 */
27
28 #if HAVE_CONFIG_H
29 # include <config.h>
30 #endif
31
32 #include <stdio.h>
33 #include <stdlib.h>
34 #include <string.h>
35
36 #include "log.h"
37 #include "system.h"
38 #include "wrappers.h"
39
40 void *
xmalloc(size_t size)41 xmalloc(size_t size)
42 {
43 void *new;
44
45 if ((new = malloc(size)) == NULL)
46 die("Error: Cannot allocate %zu bytes", size);
47
48 return new;
49 }
50
51 void *
xrealloc(void * p,size_t size)52 xrealloc(void *p, size_t size)
53 {
54 void *new;
55
56 if ((new = realloc(p, size)) == NULL) {
57 free(p);
58 die("Cannot reallocate %zu bytes: %m", size);
59 }
60 return new;
61 }
62
63 char *
xstrdup(const char * string)64 xstrdup(const char *string)
65 {
66 char *new;
67
68 if ((new = strdup(string)) == NULL)
69 die("Cannot duplicate string: %m");
70
71 return new;
72 }
73
74 void
xasprintf(char ** restrict result,const char * restrict format,...)75 xasprintf(char ** restrict result, const char * restrict format, ...)
76 {
77 va_list ap;
78
79 va_start(ap, format);
80 xvasprintf(result, format, ap);
81 va_end(ap);
82 }
83
84 void
xvasprintf(char ** restrict result,const char * restrict format,va_list ap)85 xvasprintf(char ** restrict result, const char * restrict format, va_list ap)
86 {
87 if (vasprintf(result, format, ap) < 0)
88 die("Cannot create buffer with formatted output");
89 }
90
91 size_t
xfgets(char * restrict buf,int size,FILE * restrict fp)92 xfgets(char * restrict buf, int size, FILE * restrict fp)
93 {
94 size_t len;
95
96 if (fgets(buf, size, fp) != NULL)
97 len = strlen(buf);
98 else {
99 if (ferror(fp))
100 die("Cannot read input stream: %m");
101 len = 0;
102 }
103 return len;
104 }
105
106 /* vim:set joinspaces noexpandtab textwidth=80 cinoptions=(4,u0: */
107