]> diplodocus.org Git - nmh/blobdiff - uip/dropsbr.c
Another pass at cleaning up (some of) the manpages.
[nmh] / uip / dropsbr.c
index 2b3d0208740bb0fe88692ff8290d6d82c96ace65..f6d67a088c86d66743fb2a5c4b93b00bb72e9220 100644 (file)
@@ -2,8 +2,6 @@
 /*
  * dropsbr.c -- create/read/manipulate mail drops
  *
- * $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/mts.h>
 #include <h/tws.h>
 
-#ifdef HAVE_ERRNO_H
-# include <errno.h>
-#endif
-
 #ifdef NTOHLSWAP
 # include <netinet/in.h>
 #else
@@ -46,32 +40,23 @@ static int map_open (char *, int);
 int
 mbx_open (char *file, int mbx_style, uid_t uid, gid_t gid, mode_t mode)
 {
-    int j, count, fd;
+    int j, count, fd = NOTOK;
     struct stat st;
 
     j = 0;
 
     /* attempt to open and lock file */
     for (count = 4; count > 0; count--) {
-       if ((fd = lkopen (file, O_RDWR | O_CREAT | O_NONBLOCK, mode)) == NOTOK) {
-           switch (errno) {
-#if defined(FCNTL_LOCKING) || defined(LOCKF_LOCKING)
-               case EACCES:
-               case EAGAIN:
-#endif
-
-#ifdef FLOCK_LOCKING
-               case EWOULDBLOCK:
-#endif
-               case ETXTBSY: 
-                   j = errno;
-                   sleep (5);
-                   break;
-
-               default: 
-                   /* just return error */
-                   return NOTOK;
-           }
+        int failed_to_lock = 0;
+       if ((fd = lkopenspool (file, O_RDWR | O_CREAT |
+                              O_NONBLOCK, mode, &failed_to_lock)) == NOTOK) {
+            if (failed_to_lock) {
+                j = errno;
+                sleep (5);
+                continue;
+            } else {
+                return NOTOK;
+            }
        }
 
        /* good file descriptor */
@@ -95,8 +80,12 @@ mbx_open (char *file, int mbx_style, uid_t uid, gid_t gid, mode_t mode)
         * The stat failed.  So we make sure file
         * has right ownership/modes
         */
-       chown (file, uid, gid);
-       chmod (file, mode);
+       if (chown (file, uid, gid) < 0) {
+           advise (file, "chown");
+       }
+       if (chmod (file, mode) < 0) {
+           advise (file, "chmod");
+       }
     } else if (st.st_size > (off_t) 0) {
        int status;
 
@@ -145,13 +134,12 @@ mbx_chk_mbox (int fd)
 static int
 mbx_chk_mmdf (int fd)
 {
-    size_t count;
+    ssize_t count;
     char ldelim[BUFSIZ];
 
     count = strlen (mmdlm2);
 
-    /* casting -count to off_t, seem to break FreeBSD 2.2.6 */
-    if (lseek (fd, (long) (-count), SEEK_END) == (off_t) NOTOK)
+    if (lseek (fd, -count, SEEK_END) == (off_t) NOTOK)
        return NOTOK;
     if (read (fd, ldelim, count) != count)
        return NOTOK;
@@ -176,7 +164,7 @@ mbx_read (FILE *fp, long pos, struct drop **drops, int noisy)
     char buffer[BUFSIZ];
     register struct drop *cp, *dp, *ep, *pp;
 
-    pp = (struct drop *) calloc ((size_t) (len = MAXFOLDER), sizeof(*dp));
+    pp = (struct drop *) mh_xcalloc ((size_t) (len = MAXFOLDER), sizeof(*dp));
     if (pp == NULL) {
        if (noisy)
            admonish (NULL, "unable to allocate drop storage");
@@ -253,9 +241,9 @@ mbx_write(char *mailbox, int md, FILE *fp, int id, long last,
     fseek (fp, pos, SEEK_SET);
     while (fgets (buffer, sizeof(buffer), fp) && (pos < stop)) {
        i = strlen (buffer);
-       for (j = 0; (j = stringdex (mmdlm1, buffer)) >= 0; buffer[j]++)
+       for ( ; (j = stringdex (mmdlm1, buffer)) >= 0; buffer[j]++)
            continue;
-       for (j = 0; (j = stringdex (mmdlm2, buffer)) >= 0; buffer[j]++)
+       for ( ; (j = stringdex (mmdlm2, buffer)) >= 0; buffer[j]++)
            continue;
        if (write (md, buffer, i) != i)
            return NOTOK;
@@ -312,13 +300,13 @@ mbx_copy (char *mailbox, int mbx_style, int md, int fd,
            }
                    
            while ((i = read (fd, buffer, sizeof(buffer))) > 0) {
-               for (j = 0;
-                       (j = stringdex (mmdlm1, buffer)) >= 0;
-                       buffer[j]++)
+                /* valgrind noticed that stringdex depends on null
+                   termination. */
+                buffer[i] = '\0';
+
+               for ( ; (j = stringdex (mmdlm1, buffer)) >= 0; buffer[j]++)
                    continue;
-               for (j = 0;
-                       (j = stringdex (mmdlm2, buffer)) >= 0;
-                       buffer[j]++)
+               for ( ; (j = stringdex (mmdlm2, buffer)) >= 0; buffer[j]++)
                    continue;
                if (write (md, buffer, i) != i)
                    return NOTOK;
@@ -376,7 +364,7 @@ mbx_copy (char *mailbox, int mbx_style, int md, int fd,
                            fp = strchr(ep + 1, '\n');
                        tp = dctime(dlocaltimenow());
                        snprintf (buffer, sizeof(buffer), "From %.*s  %s",
-                               fp - ep, ep, tp);
+                               (int)(fp - ep), ep, tp);
                    } else if (!strncmp (buffer, "X-Envelope-From:", 16)) {
                        /*
                         * Change the "X-Envelope-From:" field
@@ -408,7 +396,9 @@ mbx_copy (char *mailbox, int mbx_style, int md, int fd,
                 * "From ", then prepend line with ">".
                 */
                if (j != 0 && strncmp (buffer, "From ", 5) == 0) {
-                   write (md, ">", 1);
+                   if (write (md, ">", 1) < 0) {
+                       advise (mailbox, "write");
+                   }
                    size++;
                }
                i = strlen (buffer);
@@ -469,8 +459,9 @@ mbx_size (int md, off_t start, off_t stop)
 int
 mbx_close (char *mailbox, int md)
 {
-    lkclose (md, mailbox);
-    return OK;
+    if (lkclosespool (md, mailbox) == 0)
+        return OK;
+    return NOTOK;
 }
 
 
@@ -488,10 +479,10 @@ map_name (char *file)
     if ((dp = strchr(cp = r1bindex (file, '/'), '.')) == NULL)
        dp = cp + strlen (cp);
     if (cp == file)
-       snprintf (buffer, sizeof(buffer), ".%.*s%s", dp - cp, cp, ".map");
+       snprintf (buffer, sizeof(buffer), ".%.*s%s", (int)(dp - cp), cp, ".map");
     else
        snprintf (buffer, sizeof(buffer), "%.*s.%.*s%s",
-               cp - file, file, dp - cp, cp, ".map");
+               (int)(cp - file), file, (int)(dp - cp), cp, ".map");
 
     return buffer;
 }
@@ -513,7 +504,7 @@ map_read (char *file, long pos, struct drop **drops, int noisy)
     }
 
     msgp = mp->d_id;
-    dp = (struct drop *) calloc ((size_t) (msgp + 1), sizeof(*dp));
+    dp = (struct drop *) mh_xcalloc ((size_t) (msgp + 1), sizeof(*dp));
     if (dp == NULL) {
        close (md);
        return 0;
@@ -522,7 +513,8 @@ map_read (char *file, long pos, struct drop **drops, int noisy)
     memcpy((char *) dp, (char *) mp, sizeof(*dp));
 
     lseek (md, (off_t) sizeof(*mp), SEEK_SET);
-    if ((i = read (md, (char *) (dp + 1), msgp * sizeof(*dp))) < sizeof(*dp)) {
+    if ((i = read (md, (char *) (dp + 1), msgp * sizeof(*dp))) <
+        (int) sizeof(*dp)) {
        i = 0;
        free ((char *) dp);
     } else {
@@ -567,7 +559,7 @@ map_write (char *mailbox, int md, int id, long last, off_t start,
        clear = 1;
 
     if (!clear && map_chk (file, fd, &d1, pos, noisy)) {
-       unlink (file);
+       (void) m_unlink (file);
        mbx_close (file, fd);
        if ((fd = map_open (file, md)) == NOTOK)
            return NOTOK;
@@ -655,7 +647,7 @@ map_open (char *file, int md)
     mode_t mode;
     struct stat st;
 
-    mode = fstat (md, &st) != NOTOK ? (mode_t) (st.st_mode & 0777) : m_gmprot ();
+    mode = fstat (md, &st) != NOTOK ? (int) (st.st_mode & 0777) : m_gmprot ();
     return mbx_open (file, OTHER_FORMAT, st.st_uid, st.st_gid, mode);
 }
 
@@ -663,7 +655,7 @@ map_open (char *file, int md)
 int
 map_chk (char *file, int fd, struct drop *dp, long pos, int noisy)
 {
-    long count;
+    ssize_t count;
     struct drop d, tmpd;
     register struct drop *dl;
 
@@ -704,7 +696,7 @@ map_chk (char *file, int fd, struct drop *dp, long pos, int noisy)
     }
 
     dl = &d;
-    count = (long) strlen (mmdlm2);
+    count = strlen (mmdlm2);
     lseek (fd, (off_t) (dp->d_id * sizeof(*dp)), SEEK_SET);
     if (read (fd, (char *) dl, sizeof(*dl)) != sizeof(*dl)
            || (ntohl(dl->d_stop) != dp->d_stop