- int publicsw = -1, zerosw = 0;
- int create = 1, unseensw = 1;
- int fd, msgnum, seqp = 0;
- char *cp, *maildir, *folder = NULL, buf[BUFSIZ];
- char **argp, **arguments, *seqs[NUMATTRS+1];
- struct msgs *mp;
- struct stat st;
-
-#ifdef LOCALE
- setlocale(LC_ALL, "");
-#endif
- invo_name = r1bindex (argv[0], '/');
-
- /* read user profile/context */
- context_read();
-
- mts_init (invo_name);
- arguments = getarguments (invo_name, argc, argv, 1);
- argp = arguments;
-
- /* parse arguments */
- while ((cp = *argp++)) {
- if (*cp == '-') {
- switch (smatch (++cp, switches)) {
- case AMBIGSW:
- ambigsw (cp, switches);
- done (1);
- case UNKWNSW:
- adios (NULL, "-%s unknown", cp);
-
- case HELPSW:
- snprintf (buf, sizeof(buf), "%s [+folder] [switches]",
- invo_name);
- print_help (buf, switches, 1);
- done (1);
- case VERSIONSW:
- print_version(invo_name);
- done (1);
-
- case SEQSW:
- if (!(cp = *argp++) || *cp == '-')
- adios (NULL, "missing argument name to %s", argp[-2]);
-
- /* check if too many sequences specified */
- if (seqp >= NUMATTRS)
- adios (NULL, "too many sequences (more than %d) specified", NUMATTRS);
- seqs[seqp++] = cp;
- continue;
-
- case UNSEENSW:
- unseensw = 1;
- continue;
- case NUNSEENSW:
- unseensw = 0;
- continue;
-
- case PUBSW:
- publicsw = 1;
- continue;
- case NPUBSW:
- publicsw = 0;
- continue;
-
- case ZEROSW:
- zerosw++;
- continue;
- case NZEROSW:
- zerosw = 0;
- continue;
-
- case CRETSW:
- create++;
- continue;
- case NCRETSW:
- create = 0;
- continue;
- }
+ int publicsw = -1, zerosw = 0;
+ int create = 1, unseensw = 1;
+ int fd, msgnum;
+ size_t seqp = 0;
+ char *cp, *maildir, *folder = NULL, buf[BUFSIZ];
+ char **argp, **arguments, *seqs[NUMATTRS+1];
+ struct msgs *mp;
+ struct stat st;
+
+ if (atexit(unlink_done) != 0) {
+ adios(EX_OSERR, NULL, "atexit failed");
+ }
+
+ setlocale(LC_ALL, "");
+ invo_name = mhbasename(argv[0]);
+
+ /* read user profile/context */
+ context_read();
+
+ arguments = getarguments(invo_name, argc, argv, 1);
+ argp = arguments;
+
+ /* parse arguments */
+ while ((cp = *argp++)) {
+ if (*cp == '-') {
+ switch (smatch(++cp, switches)) {
+ case AMBIGSW:
+ ambigsw(cp, switches);
+ exit(EX_USAGE);
+ case UNKWNSW:
+ adios(EX_USAGE, NULL, "-%s unknown", cp);
+
+ case HELPSW:
+ snprintf(buf, sizeof(buf),
+ "%s [+folder] [switches]",
+ invo_name);
+ print_help(buf, switches, 1);
+ exit(argc == 2 ? EX_OK : EX_USAGE);
+ case VERSIONSW:
+ print_version(invo_name);
+ exit(argc == 2 ? EX_OK : EX_USAGE);
+
+ case SEQSW:
+ if (!(cp = *argp++) || *cp == '-')
+ adios(EX_USAGE, NULL, "missing argument name to %s", argp[-2]);
+
+ /* check if too many sequences specified */
+ if (seqp >= NUMATTRS)
+ adios(EX_USAGE, NULL, "too many sequences (more than %d) specified", NUMATTRS);
+ seqs[seqp++] = cp;
+ continue;
+
+ case UNSEENSW:
+ unseensw = 1;
+ continue;
+ case NUNSEENSW:
+ unseensw = 0;
+ continue;
+
+ case PUBSW:
+ publicsw = 1;
+ continue;
+ case NPUBSW:
+ publicsw = 0;
+ continue;
+
+ case ZEROSW:
+ zerosw++;
+ continue;
+ case NZEROSW:
+ zerosw = 0;
+ continue;
+
+ case CRETSW:
+ create++;
+ continue;
+ case NCRETSW:
+ create = 0;
+ continue;
+ }
+ }
+ if (*cp == '+' || *cp == '@') {
+ if (folder)
+ adios(EX_USAGE, NULL, "only one folder at a time!");
+ else
+ folder = mh_xstrdup(expandfol(cp));
+ } else {
+ adios(EX_USAGE, NULL, "usage: %s [+folder] [switches]",
+ invo_name);
+ }
+ }
+
+ seqs[seqp] = NULL; /* NULL terminate list of sequences */
+
+ /* if no folder is given, use default folder */
+ if (!folder)
+ folder = getdeffol();
+ maildir = toabsdir(folder);
+
+ /* check if folder exists */
+ if (stat(maildir, &st) == NOTOK) {
+ if (errno != ENOENT)
+ adios(EX_IOERR, maildir, "error on folder");
+ if (!create)
+ adios(EX_USAGE, NULL, "folder %s doesn't exist", maildir);
+ if (!makedir(maildir))
+ adios(EX_CANTCREAT, NULL, "unable to create folder %s", maildir);
+ }
+
+ if (chdir(maildir) == NOTOK)
+ adios(EX_OSERR, maildir, "unable to change directory to");
+
+ /* ignore a few signals */
+ SIGNAL(SIGHUP, SIG_IGN);
+ SIGNAL(SIGINT, SIG_IGN);
+ SIGNAL(SIGQUIT, SIG_IGN);
+ SIGNAL(SIGTERM, SIG_IGN);
+
+ /* create a temporary file */
+ tmpfilenam = m_mktemp(invo_name, &fd, NULL);
+ if (tmpfilenam == NULL) {
+ adios(EX_CANTCREAT, "rcvstore", "unable to create temporary file");
+ }
+ chmod(tmpfilenam, m_gmprot());
+
+ /* check if incoming mail is in mbox-format */
+ fix_mbox(fd, tmpfilenam);
+
+ /* copy the message from stdin into temp file */
+ cpydata(fileno(stdin), fd, "standard input", tmpfilenam);
+
+ if (fstat(fd, &st) == NOTOK) {
+ unlink(tmpfilenam);
+ adios(EX_IOERR, tmpfilenam, "unable to fstat");