"Fossies" - the Fresh Open Source Software Archive

Member "courier-1.2.2/libs/ldapaddressbook/abooksearch.c" (20 Jan 2022, 2391 Bytes) of package /linux/misc/courier-1.2.2.tar.bz2:


As a special service "Fossies" has tried to format the requested source page into HTML format using (guessed) C and C++ source code syntax highlighting (style: standard) with prefixed line numbers and code folding option. Alternatively you can here view or download the uninterpreted source code file. For more information about "abooksearch.c" see the Fossies "Dox" file reference documentation.

    1 /*
    2 **
    3 ** Copyright 2003-2006, Double Precision Inc.
    4 **
    5 ** See COPYING for distribution information.
    6 */
    7 
    8 #include    "config.h"
    9 #include    "ldapaddressbook.h"
   10 
   11 #include    <stdio.h>
   12 #include    <string.h>
   13 #include    <signal.h>
   14 #include    <stdlib.h>
   15 #include    <unistd.h>
   16 #include    <errno.h>
   17 #include    <sys/types.h>
   18 #if HAVE_SYS_WAIT_H
   19 #include    <sys/wait.h>
   20 #endif
   21 #ifndef WEXITSTATUS
   22 #define WEXITSTATUS(stat_val) ((unsigned)(stat_val) >> 8)
   23 #endif
   24 #ifndef WIFEXITED
   25 #define WIFEXITED(stat_val) (((stat_val) & 255) == 0)
   26 #endif
   27 
   28 #define exit(_a_) _exit(_a_)
   29 
   30 int ldapabook_search(const struct ldapabook *b, /* Search this address book */
   31              const char *script,
   32              const char *search,
   33              int (*callback_func)(const char *utf8_name,
   34                       const char *address,
   35                       void *callback_arg),
   36              void (*callback_err)(const char *errmsg,
   37                       void *callback_arg),
   38              void *callback_arg)
   39 {
   40     int pipefd[2];
   41     pid_t   p;
   42     const char *argv[40];
   43     char    buf1[BUFSIZ];
   44     char    buf2[BUFSIZ];
   45     FILE    *t, *fp;
   46     int rc_code=0;
   47     pid_t p2;
   48     int waitstat;
   49 
   50     signal(SIGCHLD, SIG_DFL);
   51 
   52     if (pipe(pipefd) < 0)   return (-1);
   53 
   54     if ((t=tmpfile()) == NULL)
   55     {
   56         close(pipefd[0]);
   57         close(pipefd[1]);
   58         return (-1);
   59     }
   60 
   61     if ((p=fork()) == -1)
   62     {
   63         fclose(t);
   64         close(pipefd[0]);
   65         close(pipefd[1]);
   66         return (-1);
   67     }
   68 
   69     if (p == 0)
   70     {
   71         dup2(pipefd[1], 1);
   72         close(pipefd[0]);
   73         close(pipefd[1]);
   74 
   75         dup2(fileno(t), 2);
   76         fclose(t);
   77 
   78         argv[0]=script;
   79         argv[1]=b->host;
   80         argv[2]=b->port;
   81         argv[3]=b->suffix;
   82         argv[4]=search;
   83         argv[5]=NULL;
   84 
   85         execvp(script, (char **)argv);
   86         perror(script);
   87         exit(1);
   88     }
   89 
   90     fp=fdopen(pipefd[0], "r");
   91     close(pipefd[1]);
   92 
   93     if (!fp)
   94     {
   95         sprintf(buf1, "%1.256s", strerror(errno));
   96 
   97         close(pipefd[0]);
   98 
   99         while ((p2=wait(NULL)) != p)
  100             ;
  101         fclose(t);
  102 
  103         (*callback_err)(buf1, callback_arg);
  104         return -1;
  105     }
  106 
  107     while (fgets(buf1, sizeof(buf1), fp) != NULL &&
  108            fgets(buf2, sizeof(buf2), fp) != NULL)
  109     {
  110         char *p=strchr(buf1, '\n');
  111 
  112         if (p) *p=0;
  113 
  114         p=strchr(buf2, '\n');
  115         if (p) *p=0;
  116 
  117         if (rc_code == 0)
  118             rc_code=(*callback_func)(buf1, buf2, callback_arg);
  119     }
  120 
  121     fclose(fp);
  122     close(pipefd[0]);
  123 
  124     while ((p2=wait(&waitstat)) != p)
  125         ;
  126 
  127     if (waitstat && rc_code == 0)
  128     {
  129         rc_code= -1;
  130         fseek(t, 0L, SEEK_SET);
  131 
  132         if (fgets(buf1, sizeof(buf1), t))
  133         {
  134             char *p=strchr(buf1, '\n');
  135             if (p) *p=0;
  136 
  137             (*callback_err)(buf1, callback_arg);
  138         }
  139     }
  140     fclose(t);
  141     return rc_code;
  142 }