]> git.notmuchmail.org Git - notmuch/blob - util/xutil.c
gmime-cleanup: pass NULL arguments explicitly where GMime 3.0 expects it
[notmuch] / util / xutil.c
1 /* xutil.c - Various wrapper functions to abort on error.
2  *
3  * Copyright © 2009 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 3 of the License, or
8  * (at your option) 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, see https://www.gnu.org/licenses/ .
17  *
18  * Author: Carl Worth <cworth@cworth.org>
19  */
20
21 #include <stdio.h>
22 #include <string.h>
23
24 #include "xutil.h"
25 #include "error_util.h"
26
27 void *
28 xcalloc (size_t nmemb, size_t size)
29 {
30     void *ret;
31
32     ret = calloc (nmemb, size);
33     if (ret == NULL) {
34         fprintf (stderr, "Out of memory.\n");
35         exit (1);
36     }
37
38     return ret;
39 }
40
41 void *
42 xmalloc (size_t size)
43 {
44     void *ret;
45
46     ret = malloc (size);
47     if (ret == NULL) {
48         fprintf (stderr, "Out of memory.\n");
49         exit (1);
50     }
51
52     return ret;
53 }
54
55 void *
56 xrealloc (void *ptr, size_t size)
57 {
58     void *ret;
59
60     ret = realloc (ptr, size);
61     if (ret == NULL) {
62         fprintf (stderr, "Out of memory.\n");
63         exit (1);
64     }
65
66     return ret;
67 }
68
69 char *
70 xstrdup (const char *s)
71 {
72     char *ret;
73
74     ret = strdup (s);
75     if (ret == NULL) {
76         fprintf (stderr, "Out of memory.\n");
77         exit (1);
78     }
79
80     return ret;
81 }
82
83 char *
84 xstrndup (const char *s, size_t n)
85 {
86     char *ret;
87
88     if (strlen (s) <= n)
89         n = strlen (s);
90
91     ret = malloc (n + 1);
92     if (ret == NULL) {
93         fprintf (stderr, "Out of memory.\n");
94         exit (1);
95     }
96     memcpy (ret, s, n);
97     ret[n] = '\0';
98
99     return ret;
100 }
101
102 int
103 xregcomp (regex_t *preg, const char *regex, int cflags)
104 {
105     int rerr;
106
107     rerr = regcomp (preg, regex, cflags);
108     if (rerr) {
109         size_t error_size = regerror (rerr, preg, NULL, 0);
110         char *error = xmalloc (error_size);
111
112         regerror (rerr, preg, error, error_size);
113         fprintf (stderr, "compiling regex %s: %s\n",
114                         regex, error);
115         free (error);
116         return 1;
117     }
118     return 0;
119 }
120
121 int
122 xregexec (const regex_t *preg, const char *string,
123           size_t nmatch, regmatch_t pmatch[], int eflags)
124 {
125     unsigned int i;
126     int rerr;
127
128     rerr = regexec (preg, string, nmatch, pmatch, eflags);
129     if (rerr)
130         return rerr;
131
132     for (i = 0; i < nmatch; i++) {
133         if (pmatch[i].rm_so == -1)
134             INTERNAL_ERROR ("matching regex against %s: Sub-match %d not found\n",
135                             string, i);
136     }
137
138     return 0;
139 }