maildirmake.c
上传用户:s81996212
上传日期:2007-01-04
资源大小:722k
文件大小:5k
- /*
- ** Copyright 1998 - 2000 Double Precision, Inc.
- ** See COPYING for distribution information.
- */
- /* $Id: maildirmake.c,v 1.5 2000/05/07 17:14:24 mrsam Exp $ */
- #if HAVE_CONFIG_H
- #include "config.h"
- #endif
- #include <stdio.h>
- #include <string.h>
- #include <stdlib.h>
- #if HAVE_FCNTL_H
- #include <fcntl.h>
- #endif
- #if HAVE_UNISTD_H
- #include <unistd.h>
- #endif
- #include <sys/types.h>
- #if HAVE_SYS_STAT_H
- #include <sys/stat.h>
- #endif
- #include <ctype.h>
- #include "maildircreate.h"
- #include "maildirmisc.h"
- #include "maildirsharedrc.h"
- static void usage()
- {
- printf("Usage: maildirmake [ options ] maildirn");
- exit(1);
- }
- extern FILE *maildir_shared_fopen(const char *, const char *);
- extern void maildir_shared_fparse(char *, char **, char **);
- static void add(const char *dir, const char *name)
- {
- char *c=malloc(strlen(name)+1);
- char *s;
- FILE *fp;
- char buf[BUFSIZ];
- char *ptr, *dirp;
- if (!c)
- {
- perror("malloc");
- exit(1);
- }
- strcpy(c, name);
- if ((s=strchr(c, '=')) == 0)
- usage();
- *s++=0;
- if (*s != '/')
- usage();
- if (access(s, R_OK))
- {
- perror(s);
- exit(1);
- }
- if (strchr(c, '.') || strchr(c, '/'))
- {
- fprintf(stderr, "%s: invalid namen", c);
- exit(1);
- }
- if (chdir(dir))
- {
- perror(dir);
- exit(1);
- }
- if ((fp=fopen(MAILDIRSHAREDRC, "r")) != 0)
- {
- while ((ptr=fgets(buf, sizeof(buf), fp)) != 0)
- {
- char *namep;
- maildir_shared_fparse(buf, &namep, &dirp);
- if (!namep) continue;
- if (strcmp(namep, c) == 0)
- {
- fclose(fp);
- fprintf(stderr,
- "%s: already defined as a sharable maildir in %s.n",
- namep, MAILDIRSHAREDRC);
- exit(2);
- }
- }
- fclose(fp);
- }
- if ((fp=maildir_shared_fopen(".", "a+")) == 0)
- {
- perror(dir);
- exit(1);
- }
- while ((ptr=fgets(buf, sizeof(buf), fp)) != 0)
- {
- char *namep;
- maildir_shared_fparse(buf, &namep, &dirp);
- if (!namep) continue;
- if (strcmp(namep, c) == 0)
- {
- fclose(fp);
- fprintf(stderr, "%s: already defined as a sharable maildir.n",
- namep);
- exit(2);
- }
- }
- fprintf(fp, "%st%sn", c, s);
- if (fflush(fp) || ferror(fp) || fclose(fp))
- {
- perror("dir");
- exit(1);
- }
- exit(0);
- }
- static void del(const char *dir, const char *n)
- {
- FILE *fp;
- char buf[BUFSIZ];
- char buf2[BUFSIZ];
- char *tmpname, *newname;
- int rc;
- FILE *fp2;
- int found;
- if (chdir(dir))
- {
- perror(dir);
- exit(1);
- }
- if ((fp=maildir_shared_fopen(".", "r")) == 0)
- {
- perror(dir);
- exit(1);
- }
- for (;;)
- {
- rc=maildir_try_create(".", "", 0, &tmpname, &newname);
- if (rc < 0)
- {
- perror(dir);
- exit(1);
- }
- if (rc == 0) break;
- sleep(3);
- }
- free(newname);
- rc=maildir_safeopen(tmpname, O_CREAT|O_WRONLY|O_EXCL, 0600);
- if (rc < 0)
- {
- perror(tmpname);
- exit(1);
- }
- fp2=fdopen(rc, "w");
- if (!fp2)
- {
- perror(tmpname);
- close(rc);
- unlink(tmpname);
- free(tmpname);
- exit(1);
- }
- found=0;
- while (fgets(buf, sizeof(buf), fp))
- {
- char *name, *dirp;
- strcpy(buf2, buf);
- maildir_shared_fparse(buf2, &name, &dirp);
- if (name && strcmp(name, n) == 0)
- {
- found=1;
- continue;
- }
- fprintf(fp2, "%s", buf);
- }
- fclose(fp);
- if (fflush(fp2) || ferror(fp2) || fclose(fp2) || rename(tmpname,
- "shared-maildirs"))
- {
- perror(tmpname);
- unlink(tmpname);
- exit(1);
- }
- free(tmpname);
- if (!found)
- {
- fprintf(stderr, "%s: not found.n", n);
- exit(1);
- }
- exit(0);
- }
- int main(int argc, char *argv[])
- {
- const char *maildir, *folder=0;
- char *p;
- int argn;
- int perm=0700;
- int musthavefolder=0;
- int subdirperm;
- char *addshared=0, *delshared=0;
- for (argn=1; argn<argc; argn++)
- {
- if (argv[argn][0] != '-') break;
- if (strcmp(argv[argn], "-") == 0) break;
- if (strncmp(argv[argn], "-f", 2) == 0)
- {
- folder=argv[argn]+2;
- if (*folder == 0 && argn+1 < argc)
- folder=argv[++argn];
- continue;
- }
- if (strcmp(argv[argn], "-S") == 0)
- {
- perm=0755;
- continue;
- }
- if (strncmp(argv[argn], "-s", 2) == 0)
- {
- const char *p=argv[argn]+2;
- if (*p == 0 && argn+1 < argc)
- p=argv[++argn];
- perm=0755;
- for (; *p; ++p)
- {
- if (isspace((int)(unsigned char)*p) ||
- *p == ',')
- continue;
- if (*p == 'r')
- perm=0755;
- else if (*p == 'w')
- perm=0777 | S_ISVTX;
- else if (*p == 'g')
- perm &= ~0007;
- while (*p && !isspace((int)(unsigned char)*p)
- && *p != ',')
- ++p;
- if (!*p) break;
- }
- musthavefolder=1;
- continue;
- }
- if (strcmp(argv[argn], "--add") == 0 && argc-argn > 1)
- {
- addshared=argv[++argn];
- continue;
- }
- if (strcmp(argv[argn], "--del") == 0 && argc-argn > 1)
- {
- delshared=argv[++argn];
- continue;
- }
- usage();
- }
- if (argn == argc) usage();
- maildir=argv[argn];
- if (addshared)
- {
- add(maildir, addshared);
- exit (0);
- }
- if (delshared)
- {
- del(maildir, delshared);
- exit (0);
- }
- if (folder && *folder == '.')
- {
- printf("Invalid folder name: %sn", folder);
- exit(1);
- }
- if (folder)
- {
- if ((p=(char *)malloc(strlen(folder)+2)) == 0)
- {
- perror("maildirmake");
- exit(1);
- }
- p[0]='.';
- strcpy(p+1, folder);
- folder=p;
- }
- else if (musthavefolder)
- usage();
- subdirperm=perm;
- if (!folder) subdirperm=0700;
- umask(0);
- if ((folder && chdir(maildir)) ||
- mkdir(folder ? folder:maildir, perm & ~0022) < 0 ||
- chdir(folder ? folder:maildir) < 0 ||
- mkdir("tmp", subdirperm) < 0 ||
- chmod("tmp", subdirperm) < 0 ||
- mkdir("new", subdirperm) < 0 ||
- chmod("new", subdirperm) < 0 ||
- mkdir("cur", subdirperm) < 0 ||
- chmod("cur", subdirperm) < 0 ||
- (folder && open("maildirfolder", O_CREAT|O_WRONLY, 0600) < 0))
- {
- perror("maildirmake");
- exit(1);
- }
- exit(0);
- return (0);
- }