Back to index

courier  0.68.2
abookdel.c
Go to the documentation of this file.
00001 /*
00002 ** Copyright 2006, Double Precision Inc.
00003 **
00004 ** See COPYING for distribution information.
00005 */
00006 
00007 #include      "config.h"
00008 #include      "ldapaddressbook.h"
00009 
00010 #include      <stdio.h>
00011 #include      <string.h>
00012 #include      <stdlib.h>
00013 #include      <unistd.h>
00014 
00015 int ldapabook_del(const char *filename, const char *tempname,
00016               const char *delname)
00017 {
00018 /* This is cheating, but we won't really have many abooks, come on... */
00019 struct ldapabook *a=ldapabook_read(filename), *b;
00020 
00021 FILE   *fp;
00022 
00023        if (!a)       return (0);
00024 
00025        if ((fp=fopen(tempname, "w")) == 0)
00026        {
00027               ldapabook_free(a);
00028               return (-1);
00029        }
00030 
00031        for (b=a; b; b=b->next)
00032        {
00033               if (strcmp(b->name, delname) == 0) continue;
00034 
00035               ldapabook_writerec(b, fp);
00036        }
00037        ldapabook_free(a);
00038 
00039        if (fflush(fp) || fclose(fp))
00040        {
00041               fclose(fp);
00042               unlink(tempname);
00043               return (-1);
00044        }
00045 
00046        if (rename(tempname, filename))
00047        {
00048               unlink(tempname);
00049               return (-1);
00050        }
00051 
00052        return (0);
00053 }