]> git.notmuchmail.org Git - notmuch/blobdiff - xutil.c
test emacs: Add tests for hiding messages in notmuch-show view
[notmuch] / xutil.c
diff --git a/xutil.c b/xutil.c
index 7ee7a698f3c7142e1cab717eeaa930ced6fd18ad..5f98f3f7e9b83135679c801b01d898345f8c37f4 100644 (file)
--- a/xutil.c
+++ b/xutil.c
@@ -18,7 +18,6 @@
  * Author: Carl Worth <cworth@cworth.org>
  */
 
  * Author: Carl Worth <cworth@cworth.org>
  */
 
-#define _GNU_SOURCE /* For strndup */
 #include "notmuch-private.h"
 
 #include <stdio.h>
 #include "notmuch-private.h"
 
 #include <stdio.h>
@@ -84,11 +83,56 @@ xstrndup (const char *s, size_t n)
 {
     char *ret;
 
 {
     char *ret;
 
-    ret = strndup (s, n);
+    if (strlen (s) <= n)
+       n = strlen (s);
+
+    ret = malloc (n + 1);
     if (ret == NULL) {
        fprintf (stderr, "Out of memory.\n");
        exit (1);
     }
     if (ret == NULL) {
        fprintf (stderr, "Out of memory.\n");
        exit (1);
     }
+    memcpy (ret, s, n);
+    ret[n] = '\0';
 
     return ret;
 }
 
     return ret;
 }
+
+void
+xregcomp (regex_t *preg, const char *regex, int cflags)
+{
+    int rerr;
+
+    rerr = regcomp (preg, regex, cflags);
+    if (rerr) {
+       size_t error_size = regerror (rerr, preg, NULL, 0);
+       char *error = xmalloc (error_size);
+
+       regerror (rerr, preg, error, error_size);
+       fprintf (stderr, "Internal error: compiling regex %s: %s\n",
+                regex, error);
+       exit (1);
+    }
+}
+
+int
+xregexec (const regex_t *preg, const char *string,
+         size_t nmatch, regmatch_t pmatch[], int eflags)
+{
+    unsigned int i;
+    int rerr;
+
+    rerr = regexec (preg, string, nmatch, pmatch, eflags);
+    if (rerr)
+       return rerr;
+
+    for (i = 0; i < nmatch; i++) {
+       if (pmatch[i].rm_so == -1) {
+           fprintf (stderr, "Internal error: matching regex against %s:"
+                    "Sub-match %d not found\n",
+                    string, i);
+           exit (1);
+       }
+    }
+
+    return 0;
+}