chiark / gitweb /
New spec for iterator functions.
[adns.git] / src / general.c
index 45aec6894d319cd0e3d0d86f632a3b6c2698d418..71009f7d674c36d22221e1708fba911c10bcccd7 100644 (file)
@@ -4,7 +4,7 @@
  * - vbuf handling
  */
 /*
- *  This file is part of adns, which is Copyright (C) 1997, 1998 Ian Jackson
+ *  This file is part of adns, which is Copyright (C) 1997-1999 Ian Jackson
  *  
  *  This program is free software; you can redistribute it and/or modify
  *  it under the terms of the GNU General Public License as published by
@@ -24,6 +24,9 @@
 #include <stdlib.h>
 #include <string.h>
 
+#include <sys/types.h>
+#include <sys/socket.h>
+#include <netinet/in.h>
 #include <arpa/inet.h>
 
 #include "internal.h"
@@ -197,10 +200,11 @@ adns_status adns_rr_info(adns_rrtype type,
   return st;
 }
 
-#define SINFO(n,s) { adns_s_##n, s }
+#define SINFO(n,s) { adns_s_##n, #n, s }
 
 static const struct sinfo {
   adns_status st;
+  const char *abbrev;
   const char *string;
 } sinfos[]= {
   SINFO(  ok,                  "OK"                                            ),
@@ -241,26 +245,34 @@ static int si_compar(const void *key, const void *elem) {
   return *st < si->st ? -1 : *st > si->st ? 1 : 0;
 }
 
+static const struct sinfo *findsinfo(adns_status st) {
+  return bsearch(&st,sinfos,sizeof(sinfos)/sizeof(*sinfos),sizeof(*sinfos),si_compar);
+}
+
 const char *adns_strerror(adns_status st) {
-  static char buf[100];
+  const struct sinfo *si;
+
+  si= findsinfo(st);
+  return si->string;
+}
 
+const char *adns_errabbrev(adns_status st) {
   const struct sinfo *si;
 
-  si= bsearch(&st,sinfos,sizeof(sinfos)/sizeof(*si),sizeof(*si),si_compar);
-  if (si) return si->string;
-  
-  snprintf(buf,sizeof(buf),"code %d",st);
-  return buf;
+  si= findsinfo(st);
+  return si->abbrev;
 }
 
 void adns__isort(void *array, int nobjs, int sz, void *tempbuf,
-                int (*needswap)(const void *a, const void *b)) {
+                int (*needswap)(void *context, const void *a, const void *b),
+                void *context) {
   byte *data= array;
   int i, place;
 
   for (i=0; i<nobjs; i++) {
-    for (place= i; place>0 && needswap(data + (place-1)*sz, data + i*sz); place--);
-
+    for (place= i;
+        place>0 && needswap(context, data + (place-1)*sz, data + i*sz);
+        place--);
     if (place != i) {
       memcpy(tempbuf, data + i*sz, sz);
       memmove(data + (place+1)*sz, data + place*sz, (i-place)*sz);
@@ -268,3 +280,32 @@ void adns__isort(void *array, int nobjs, int sz, void *tempbuf,
     }
   }
 }
+
+/* SIGPIPE protection. */
+
+void adns__sigpipe_protect(adns_state ads) {
+  sigset_t toblock;
+  struct sigaction sa;
+  int r;
+
+  if (ads->iflags & adns_if_nosigpipe) return;
+
+  sigfillset(&toblock);
+  sigdelset(&toblock,SIGPIPE);
+
+  sa.sa_handler= SIG_IGN;
+  sigfillset(&sa.sa_mask);
+  sa.sa_flags= 0;
+  
+  r= sigprocmask(SIG_SETMASK,&toblock,&ads->stdsigmask); assert(!r);
+  r= sigaction(SIGPIPE,&sa,&ads->stdsigpipe); assert(!r);
+}
+
+void adns__sigpipe_unprotect(adns_state ads) {
+  int r;
+
+  if (ads->iflags & adns_if_nosigpipe) return;
+
+  r= sigaction(SIGPIPE,&ads->stdsigpipe,0); assert(!r);
+  r= sigprocmask(SIG_SETMASK,&ads->stdsigmask,0); assert(!r);
+}