]> diplodocus.org Git - nmh/blobdiff - uip/rcvstore.c
Use result, because (void) didn't prevent warning on FreeBSD 10.
[nmh] / uip / rcvstore.c
index 81a03e8f98f0feac456fd84db802924645037352..25bb8780dd6eb66ae96f7ee0fc39ad6f27966bdc 100644 (file)
@@ -2,69 +2,63 @@
 /*
  * rcvstore.c -- asynchronously add mail to a folder
  *
 /*
  * rcvstore.c -- asynchronously add mail to a folder
  *
- * $Id$
+ * This code is Copyright (c) 2002, by the authors of nmh.  See the
+ * COPYRIGHT file in the root directory of the nmh distribution for
+ * complete copyright information.
  */
 
 #include <h/mh.h>
 #include <fcntl.h>
 #include <h/signals.h>
  */
 
 #include <h/mh.h>
 #include <fcntl.h>
 #include <h/signals.h>
-#include <errno.h>
-#include <signal.h>
-
-static struct swit switches[] = {
-#define CRETSW         0
-    { "create",        0 },
-#define NCRETSW        1
-    { "nocreate", 0 },
-#define UNSEENSW       2
-    { "unseen", 0 },
-#define NUNSEENSW      3
-    { "nounseen", 0 },
-#define PUBSW          4
-    { "public",        0 },
-#define NPUBSW         5
-    { "nopublic",  0 },
-#define ZEROSW         6
-    { "zero",  0 },
-#define NZEROSW        7
-    { "nozero",        0 },
-#define SEQSW          8
-    { "sequence name", 0 },
-#define VERSIONSW      9
-    { "version", 0 },
-#define HELPSW        10
-    { "help", 4 },
-    { NULL, 0 }
-};
-
-extern int errno;
+#include <h/mts.h>
+
+#define RCVSTORE_SWITCHES \
+    X("create", 0, CRETSW) \
+    X("nocreate", 0, NCRETSW) \
+    X("unseen", 0, UNSEENSW) \
+    X("nounseen", 0, NUNSEENSW) \
+    X("public", 0, PUBSW) \
+    X("nopublic", 0, NPUBSW) \
+    X("zero", 0, ZEROSW) \
+    X("nozero", 0, NZEROSW) \
+    X("sequence name", 0, SEQSW) \
+    X("version", 0, VERSIONSW) \
+    X("help", 0, HELPSW) \
+
+#define X(sw, minchars, id) id,
+DEFINE_SWITCH_ENUM(RCVSTORE);
+#undef X
+
+#define X(sw, minchars, id) { sw, minchars, id },
+DEFINE_SWITCH_ARRAY(RCVSTORE, switches);
+#undef X
+
 
 /*
  * name of temporary file to store incoming message
  */
 static char *tmpfilenam = NULL;
 
 
 /*
  * name of temporary file to store incoming message
  */
 static char *tmpfilenam = NULL;
 
+static void unlink_done(int) NORETURN;
 
 int
 main (int argc, char **argv)
 {
     int publicsw = -1, zerosw = 0;
     int create = 1, unseensw = 1;
 
 int
 main (int argc, char **argv)
 {
     int publicsw = -1, zerosw = 0;
     int create = 1, unseensw = 1;
-    int fd, msgnum, seqp = 0;
+    int fd, msgnum;
+    size_t seqp = 0;
     char *cp, *maildir, *folder = NULL, buf[BUFSIZ];
     char *cp, *maildir, *folder = NULL, buf[BUFSIZ];
-    char **argp, **arguments, *seqs[NUMATTRS+1];
+    char **argp, **arguments;
+    svector_t seqs = svector_create (0);
     struct msgs *mp;
     struct stat st;
 
     struct msgs *mp;
     struct stat st;
 
-#ifdef LOCALE
-    setlocale(LC_ALL, "");
-#endif
-    invo_name = r1bindex (argv[0], '/');
+    if (nmh_init(argv[0], 2)) { return 1; }
 
 
-    /* read user profile/context */
-    context_read();
+    done=unlink_done;
 
 
-    mts_init (invo_name);
+    mts_init ();
     arguments = getarguments (invo_name, argc, argv, 1);
     argp = arguments;
 
     arguments = getarguments (invo_name, argc, argv, 1);
     argp = arguments;
 
@@ -82,19 +76,17 @@ main (int argc, char **argv)
                snprintf (buf, sizeof(buf), "%s [+folder] [switches]",
                          invo_name);
                print_help (buf, switches, 1);
                snprintf (buf, sizeof(buf), "%s [+folder] [switches]",
                          invo_name);
                print_help (buf, switches, 1);
-               done (1);
+               done (0);
            case VERSIONSW:
                print_version(invo_name);
            case VERSIONSW:
                print_version(invo_name);
-               done (1);
+               done (0);
 
            case SEQSW: 
                if (!(cp = *argp++) || *cp == '-')
                    adios (NULL, "missing argument name to %s", argp[-2]);
 
 
            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;
+               svector_push_back (seqs, cp);
+               seqp++;
                continue;
 
            case UNSEENSW:
                continue;
 
            case UNSEENSW:
@@ -130,14 +122,12 @@ main (int argc, char **argv)
            if (folder)
                adios (NULL, "only one folder at a time!");
            else
            if (folder)
                adios (NULL, "only one folder at a time!");
            else
-               folder = path (cp + 1, *cp == '+' ? TFOLDER : TSUBCWF);
+               folder = pluspath (cp);
        } else {
            adios (NULL, "usage: %s [+folder] [switches]", invo_name);
        }
     }
 
        } else {
            adios (NULL, "usage: %s [+folder] [switches]", invo_name);
        }
     }
 
-    seqs[seqp] = NULL; /* NULL terminate list of sequences */
-
     if (!context_find ("path"))
        free (path ("./", TFOLDER));
 
     if (!context_find ("path"))
        free (path ("./", TFOLDER));
 
@@ -166,16 +156,17 @@ main (int argc, char **argv)
     SIGNAL (SIGTERM, SIG_IGN);
 
     /* create a temporary file */
     SIGNAL (SIGTERM, SIG_IGN);
 
     /* create a temporary file */
-    tmpfilenam = m_scratch ("", invo_name);
-    if ((fd = creat (tmpfilenam, m_gmprot ())) == NOTOK)
-       adios (tmpfilenam, "unable to create");
+    tmpfilenam = m_mktemp (invo_name, &fd, NULL);
+    if (tmpfilenam == NULL) {
+       adios(NULL, "unable to create temporary file in %s", get_temp_dir());
+    }
     chmod (tmpfilenam, m_gmprot());
 
     /* copy the message from stdin into temp file */
     cpydata (fileno (stdin), fd, "standard input", tmpfilenam);
 
     if (fstat (fd, &st) == NOTOK) {
     chmod (tmpfilenam, m_gmprot());
 
     /* copy the message from stdin into temp file */
     cpydata (fileno (stdin), fd, "standard input", tmpfilenam);
 
     if (fstat (fd, &st) == NOTOK) {
-       unlink (tmpfilenam);
+       (void) m_unlink (tmpfilenam);
        adios (tmpfilenam, "unable to fstat");
     }
     if (close (fd) == NOTOK)
        adios (tmpfilenam, "unable to fstat");
     }
     if (close (fd) == NOTOK)
@@ -183,7 +174,7 @@ main (int argc, char **argv)
 
     /* don't add file if it is empty */
     if (st.st_size == 0) {
 
     /* don't add file if it is empty */
     if (st.st_size == 0) {
-       unlink (tmpfilenam);
+       (void) m_unlink (tmpfilenam);
        advise (NULL, "empty file");
        done (0);
     }
        advise (NULL, "empty file");
        done (0);
     }
@@ -191,43 +182,50 @@ main (int argc, char **argv)
     /*
      * read folder and create message structure
      */
     /*
      * read folder and create message structure
      */
-    if (!(mp = folder_read (folder)))
+    if (!(mp = folder_read (folder, 1)))
        adios (NULL, "unable to read folder %s", folder);
 
     /*
      * Link message into folder, and possibly add
      * to the Unseen-Sequence's.
      */
        adios (NULL, "unable to read folder %s", folder);
 
     /*
      * Link message into folder, and possibly add
      * to the Unseen-Sequence's.
      */
-    if ((msgnum = folder_addmsg (&mp, tmpfilenam, 0, unseensw, 0)) == -1)
+    if ((msgnum = folder_addmsg (&mp, tmpfilenam, 0, unseensw, 0, 0, NULL)) == -1)
        done (1);
 
     /*
      * Add the message to any extra sequences
      * that have been specified.
      */
        done (1);
 
     /*
      * Add the message to any extra sequences
      * that have been specified.
      */
-    for (seqp = 0; seqs[seqp]; seqp++) {
-       if (!seq_addmsg (mp, seqs[seqp], msgnum, publicsw, zerosw))
-           done (1);
+    if (seqp) {
+       /* The only reason that seqp was checked to be non-zero is in
+          case a -nosequence switch is added. */
+       for (seqp = 0; seqp < svector_size (seqs); seqp++) {
+           if (!seq_addmsg (mp, svector_at (seqs, seqp), msgnum, publicsw,
+                            zerosw))
+               done (1);
+       }
     }
 
     }
 
+    svector_free (seqs);
     seq_setunseen (mp, 0);     /* synchronize any Unseen-Sequence's      */
     seq_save (mp);             /* synchronize and save message sequences */
     folder_free (mp);          /* free folder/message structure          */
 
     context_save ();           /* save the global context file           */
     seq_setunseen (mp, 0);     /* synchronize any Unseen-Sequence's      */
     seq_save (mp);             /* synchronize and save message sequences */
     folder_free (mp);          /* free folder/message structure          */
 
     context_save ();           /* save the global context file           */
-    unlink (tmpfilenam);       /* remove temporary file                  */
+    (void) m_unlink (tmpfilenam); /* remove temporary file                  */
     tmpfilenam = NULL;
 
     done (0);
     tmpfilenam = NULL;
 
     done (0);
+    return 1;
 }
 
 /*
  * Clean up and exit
  */
 }
 
 /*
  * Clean up and exit
  */
-void
-done(int status)
+static void
+unlink_done(int status)
 {
     if (tmpfilenam && *tmpfilenam)
 {
     if (tmpfilenam && *tmpfilenam)
-       unlink (tmpfilenam);
+       (void) m_unlink (tmpfilenam);
     exit (status);
 }
     exit (status);
 }