]> diplodocus.org Git - nmh/blobdiff - uip/rcvstore.c
fdcompare.c: Move interface to own file.
[nmh] / uip / rcvstore.c
index 2822093e57f851e1b207f82de24512f7effa0093..61980ea2e30dcfa3a925dd28ada7915d0ef35a98 100644 (file)
@@ -1,16 +1,30 @@
-
-/*
- * rcvstore.c -- asynchronously add mail to a folder
+/* rcvstore.c -- asynchronously add mail to a folder
  *
  * 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 "h/mh.h"
+#include "sbr/folder_read.h"
+#include "sbr/folder_free.h"
+#include "sbr/folder_addmsg.h"
+#include "sbr/context_save.h"
+#include "sbr/context_find.h"
+#include "sbr/ambigsw.h"
+#include "sbr/path.h"
+#include "sbr/print_version.h"
+#include "sbr/print_help.h"
+#include "sbr/seq_add.h"
+#include "sbr/error.h"
 #include <fcntl.h>
-#include <h/signals.h>
-#include <h/mts.h>
+#include "h/signals.h"
+#include "h/mts.h"
+#include "h/done.h"
+#include "h/utils.h"
+#include "sbr/m_maildir.h"
+#include "sbr/m_mktemp.h"
+#include "sbr/makedir.h"
 
 #define RCVSTORE_SWITCHES \
     X("create", 0, CRETSW) \
@@ -44,8 +58,10 @@ static void unlink_done(int) NORETURN;
 int
 main (int argc, char **argv)
 {
-    int publicsw = -1, zerosw = 0;
-    int create = 1, unseensw = 1;
+    int publicsw = -1;
+    bool zerosw = false;
+    bool create = true;
+    bool unseensw = true;
     int fd, msgnum;
     size_t seqp = 0;
     char *cp, *maildir, *folder = NULL, buf[BUFSIZ];
@@ -54,17 +70,11 @@ main (int argc, char **argv)
     struct msgs *mp;
     struct stat st;
 
-    done=unlink_done;
-
-#ifdef LOCALE
-    setlocale(LC_ALL, "");
-#endif
-    invo_name = r1bindex (argv[0], '/');
+    if (nmh_init(argv[0], true, false)) { return 1; }
 
-    /* read user profile/context */
-    context_read();
+    set_done(unlink_done);
 
-    mts_init (invo_name);
+    mts_init ();
     arguments = getarguments (invo_name, argc, argv, 1);
     argp = arguments;
 
@@ -76,7 +86,7 @@ main (int argc, char **argv)
                ambigsw (cp, switches);
                done (1);
            case UNKWNSW: 
-               adios (NULL, "-%s unknown", cp);
+               die("-%s unknown", cp);
 
            case HELPSW: 
                snprintf (buf, sizeof(buf), "%s [+folder] [switches]",
@@ -89,17 +99,17 @@ main (int argc, char **argv)
 
            case SEQSW: 
                if (!(cp = *argp++) || *cp == '-')
-                   adios (NULL, "missing argument name to %s", argp[-2]);
+                   die("missing argument name to %s", argp[-2]);
 
                svector_push_back (seqs, cp);
                seqp++;
                continue;
 
            case UNSEENSW:
-               unseensw = 1;
+               unseensw = true;
                continue;
            case NUNSEENSW:
-               unseensw = 0;
+               unseensw = false;
                continue;
 
            case PUBSW: 
@@ -110,27 +120,26 @@ main (int argc, char **argv)
                continue;
 
            case ZEROSW: 
-               zerosw++;
+               zerosw = true;
                continue;
            case NZEROSW: 
-               zerosw = 0;
+               zerosw = false;
                continue;
 
            case CRETSW: 
-               create++;
+               create = true;
                continue;
            case NCRETSW: 
-               create = 0;
+               create = false;
                continue;
            }
        }
        if (*cp == '+' || *cp == '@') {
            if (folder)
-               adios (NULL, "only one folder at a time!");
-           else
-               folder = pluspath (cp);
+               die("only one folder at a time!");
+            folder = pluspath (cp);
        } else {
-           adios (NULL, "usage: %s [+folder] [switches]", invo_name);
+           die("usage: %s [+folder] [switches]", invo_name);
        }
     }
 
@@ -147,9 +156,9 @@ main (int argc, char **argv)
        if (errno != ENOENT)
            adios (maildir, "error on folder");
        if (!create)
-           adios (NULL, "folder %s doesn't exist", maildir);
+           die("folder %s doesn't exist", maildir);
        if (!makedir (maildir))
-           adios (NULL, "unable to create folder %s", maildir);
+           die("unable to create folder %s", maildir);
     }
 
     if (chdir (maildir) == NOTOK)
@@ -164,7 +173,7 @@ main (int argc, char **argv)
     /* create a temporary file */
     tmpfilenam = m_mktemp (invo_name, &fd, NULL);
     if (tmpfilenam == NULL) {
-       adios ("rcvstore", "unable to create temporary file");
+       die("unable to create temporary file in %s", get_temp_dir());
     }
     chmod (tmpfilenam, m_gmprot());
 
@@ -172,7 +181,7 @@ main (int argc, char **argv)
     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)
@@ -180,8 +189,8 @@ main (int argc, char **argv)
 
     /* don't add file if it is empty */
     if (st.st_size == 0) {
-       unlink (tmpfilenam);
-       advise (NULL, "empty file");
+       (void) m_unlink (tmpfilenam);
+       inform("empty file");
        done (0);
     }
 
@@ -189,13 +198,13 @@ main (int argc, char **argv)
      * read folder and create message structure
      */
     if (!(mp = folder_read (folder, 1)))
-       adios (NULL, "unable to read folder %s", folder);
+       die("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, 0, (char *)0)) == -1)
+    if ((msgnum = folder_addmsg (&mp, tmpfilenam, 0, unseensw, 0, 0, NULL)) == -1)
        done (1);
 
     /*
@@ -218,7 +227,7 @@ main (int argc, char **argv)
     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);
@@ -228,10 +237,10 @@ main (int argc, char **argv)
 /*
  * Clean up and exit
  */
-static void
+static void NORETURN
 unlink_done(int status)
 {
     if (tmpfilenam && *tmpfilenam)
-       unlink (tmpfilenam);
+       (void) m_unlink (tmpfilenam);
     exit (status);
 }