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