]> git.notmuchmail.org Git - notmuch/blob - xutil.c
notmuch restore: Fix leak of FILE* object.
[notmuch] / 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 http://www.gnu.org/licenses/ .
17  *
18  * Author: Carl Worth <cworth@cworth.org>
19  */
20
21 #define _GNU_SOURCE /* For strndup */
22 #include "notmuch-private.h"
23
24 #include <stdio.h>
25
26 void *
27 xcalloc (size_t nmemb, size_t size)
28 {
29     void *ret;
30
31     ret = calloc (nmemb, size);
32     if (ret == NULL) {
33         fprintf (stderr, "Out of memory.\n");
34         exit (1);
35     }
36
37     return ret;
38 }
39
40 void *
41 xmalloc (size_t size)
42 {
43     void *ret;
44
45     ret = malloc (size);
46     if (ret == NULL) {
47         fprintf (stderr, "Out of memory.\n");
48         exit (1);
49     }
50
51     return ret;
52 }
53
54 void *
55 xrealloc (void *ptr, size_t size)
56 {
57     void *ret;
58
59     ret = realloc (ptr, size);
60     if (ret == NULL) {
61         fprintf (stderr, "Out of memory.\n");
62         exit (1);
63     }
64
65     return ret;
66 }
67
68 char *
69 xstrdup (const char *s)
70 {
71     char *ret;
72
73     ret = strdup (s);
74     if (ret == NULL) {
75         fprintf (stderr, "Out of memory.\n");
76         exit (1);
77     }
78
79     return ret;
80 }
81
82 char *
83 xstrndup (const char *s, size_t n)
84 {
85     char *ret;
86
87     ret = strndup (s, n);
88     if (ret == NULL) {
89         fprintf (stderr, "Out of memory.\n");
90         exit (1);
91     }
92
93     return ret;
94 }
95
96 void
97 xregcomp (regex_t *preg, const char *regex, int cflags)
98 {
99     int rerr;
100
101     rerr = regcomp (preg, regex, cflags);
102     if (rerr) {
103         size_t error_size = regerror (rerr, preg, NULL, 0);
104         char *error = xmalloc (error_size);
105
106         regerror (rerr, preg, error, error_size);
107         INTERNAL_ERROR ("compiling regex %s: %s\n",
108                         regex, error);
109     }
110 }
111
112 int
113 xregexec (const regex_t *preg, const char *string,
114           size_t nmatch, regmatch_t pmatch[], int eflags)
115 {
116     unsigned int i;
117     int rerr;
118
119     rerr = regexec (preg, string, nmatch, pmatch, eflags);
120     if (rerr)
121         return rerr;
122
123     for (i = 0; i < nmatch; i++) {
124         if (pmatch[i].rm_so == -1)
125             INTERNAL_ERROR ("matching regex against %s: Sub-match %d not found\n",
126                             string, i);
127     }
128
129     return 0;
130 }