Back to index

courier  0.68.2
sqconfig.c
Go to the documentation of this file.
00001 /*
00002 ** Copyright 1998 - 1999 Double Precision, Inc.  See COPYING for
00003 ** distribution information.
00004 */
00005 
00006 
00007 /*
00008 */
00009 #include      "sqwebmail.h"
00010 #include      "sqconfig.h"
00011 #include      <stdio.h>
00012 #include      <stdlib.h>
00013 #include      <string.h>
00014 #if    HAVE_UNISTD_H
00015 #include      <unistd.h>
00016 #endif
00017 #include      <sys/types.h>
00018 #include      <sys/stat.h>
00019 #include      "maildir/maildircreate.h"
00020 
00021 /* Assume all configuration data fits in 256 char buffer. */
00022 
00023 static char linebuf[256];
00024 
00025 const char *read_sqconfig(const char *dir, const char *configfile, time_t *mtime)
00026 {
00027 char *p=malloc(strlen(dir) + strlen(configfile) + 2);
00028 struct stat stat_buf;
00029 FILE   *f;
00030 
00031        if (!p)       enomem();
00032        strcat(strcat(strcpy(p, dir), "/"), configfile);
00033        f=fopen(p, "r");
00034        free(p);
00035        if (!f)       return (0);
00036        if (fstat(fileno(f), &stat_buf) != 0 ||
00037               !fgets(linebuf, sizeof(linebuf), f))
00038        {
00039               fclose(f);
00040               return (0);
00041        }
00042        fclose(f);
00043        if (mtime)    *mtime=stat_buf.st_mtime;
00044 
00045        linebuf[sizeof(linebuf)-1]=0;
00046        if ((p=strchr(linebuf, '\n')) != 0)       *p=0;
00047        return (linebuf);
00048 }
00049 
00050 void write_sqconfig(const char *dir, const char *configfile, const char *val)
00051 {
00052        char *p=malloc(strlen(dir) + strlen(configfile) + 2);
00053 
00054        struct maildir_tmpcreate_info createInfo;
00055        FILE *fp;
00056 
00057        if (!p)       enomem();
00058 
00059        strcat(strcat(strcpy(p, dir), "/"), configfile);
00060        if (!val)
00061        {
00062               unlink(p);
00063               free(p);
00064               return;
00065        }
00066 
00067        maildir_tmpcreate_init(&createInfo);
00068 
00069        createInfo.maildir=dir;
00070        createInfo.uniq="config";
00071        createInfo.doordie=1;
00072 
00073        fp=maildir_tmpcreate_fp(&createInfo);
00074 
00075        if (!fp)
00076               enomem();
00077 
00078 
00079        free(createInfo.newname);
00080        createInfo.newname=p;
00081 
00082        fprintf(fp, "%s\n", val);
00083        fflush(fp);
00084        if (ferror(fp))      eio("Error after write:",p);
00085        fclose(fp);
00086 
00087        /* Note - umask should already turn off the 077 bits, but
00088        ** just in case someone screwed up previously, I'll fix it
00089        ** myself */
00090 
00091        chmod(createInfo.tmpname, 0600);
00092        rename(createInfo.tmpname, createInfo.newname);
00093        maildir_tmpcreate_free(&createInfo);
00094 }