]> diplodocus.org Git - nmh/blobdiff - uip/dropsbr.c
Alter HasSuffixC()'s char * to be const.
[nmh] / uip / dropsbr.c
index 76bc00dbe2852751637c3afc151d30bdcf9daaf6..3474e7bebc4aef62cef1f07b79f70f441910de2b 100644 (file)
 #include <h/mts.h>
 #include <h/tws.h>
 
 #include <h/mts.h>
 #include <h/tws.h>
 
-#ifdef HAVE_ERRNO_H
-# include <errno.h>
-#endif
-
 #ifdef NTOHLSWAP
 # include <netinet/in.h>
 #else
 #ifdef NTOHLSWAP
 # include <netinet/in.h>
 #else
@@ -44,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
 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--) {
     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 */
        }
 
        /* good file descriptor */
@@ -93,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
         */
         * 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;
 
     } else if (st.st_size > (off_t) 0) {
        int status;
 
@@ -143,13 +134,12 @@ mbx_chk_mbox (int fd)
 static int
 mbx_chk_mmdf (int fd)
 {
 static int
 mbx_chk_mmdf (int fd)
 {
-    size_t count;
+    ssize_t count;
     char ldelim[BUFSIZ];
 
     count = strlen (mmdlm2);
 
     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;
        return NOTOK;
     if (read (fd, ldelim, count) != count)
        return NOTOK;
@@ -166,20 +156,16 @@ mbx_chk_mmdf (int fd)
 
 
 int
 
 
 int
-mbx_read (FILE *fp, long pos, struct drop **drops, int noisy)
+mbx_read (FILE *fp, long pos, struct drop **drops)
 {
 {
-    register int len, size;
-    register long ld1, ld2;
-    register char *bp;
+    int len, size;
+    long ld1, ld2;
+    char *bp;
     char buffer[BUFSIZ];
     char buffer[BUFSIZ];
-    register struct drop *cp, *dp, *ep, *pp;
+    struct drop *cp, *dp, *ep, *pp;
 
 
-    pp = (struct drop *) calloc ((size_t) (len = MAXFOLDER), sizeof(*dp));
-    if (pp == NULL) {
-       if (noisy)
-           admonish (NULL, "unable to allocate drop storage");
-       return NOTOK;
-    }
+    len = MAXFOLDER;
+    pp = mh_xcalloc(len, sizeof *pp);
 
     ld1 = (long) strlen (mmdlm1);
     ld2 = (long) strlen (mmdlm2);
 
     ld1 = (long) strlen (mmdlm1);
     ld2 = (long) strlen (mmdlm2);
@@ -215,7 +201,7 @@ mbx_read (FILE *fp, long pos, struct drop **drops, int noisy)
        pos += ld2;
 
        if (dp >= ep) {
        pos += ld2;
 
        if (dp >= ep) {
-           register int    curlen = dp - pp;
+           int    curlen = dp - pp;
 
            cp = (struct drop *) mh_xrealloc ((char *) pp,
                                    (size_t) (len += MAXFOLDER) * sizeof(*pp));
 
            cp = (struct drop *) mh_xrealloc ((char *) pp,
                                    (size_t) (len += MAXFOLDER) * sizeof(*pp));
@@ -224,7 +210,7 @@ mbx_read (FILE *fp, long pos, struct drop **drops, int noisy)
     }
 
     if (dp == pp)
     }
 
     if (dp == pp)
-       free ((char *) pp);
+       free(pp);
     else
        *drops = pp;
     return (dp - pp);
     else
        *drops = pp;
     return (dp - pp);
@@ -235,10 +221,10 @@ int
 mbx_write(char *mailbox, int md, FILE *fp, int id, long last,
            long pos, off_t stop, int mapping, int noisy)
 {
 mbx_write(char *mailbox, int md, FILE *fp, int id, long last,
            long pos, off_t stop, int mapping, int noisy)
 {
-    register int i, j, size;
+    int i, j, size;
     off_t start;
     long off;
     off_t start;
     long off;
-    register char *cp;
+    char *cp;
     char buffer[BUFSIZ];
 
     off = (long) lseek (md, (off_t) 0, SEEK_CUR);
     char buffer[BUFSIZ];
 
     off = (long) lseek (md, (off_t) 0, SEEK_CUR);
@@ -251,9 +237,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);
     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;
            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;
            continue;
        if (write (md, buffer, i) != i)
            return NOTOK;
@@ -310,13 +296,13 @@ mbx_copy (char *mailbox, int mbx_style, int md, int fd,
            }
                    
            while ((i = read (fd, buffer, sizeof(buffer))) > 0) {
            }
                    
            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;
                    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;
                    continue;
                if (write (md, buffer, i) != i)
                    return NOTOK;
@@ -406,7 +392,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) {
                 * "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);
                    size++;
                }
                i = strlen (buffer);
@@ -440,9 +428,9 @@ mbx_copy (char *mailbox, int mbx_style, int md, int fd,
 int
 mbx_size (int md, off_t start, off_t stop)
 {
 int
 mbx_size (int md, off_t start, off_t stop)
 {
-    register int i, fd;
-    register long pos;
-    register FILE *fp;
+    int i, fd;
+    long pos;
+    FILE *fp;
 
     if ((fd = dup (md)) == NOTOK || (fp = fdopen (fd, "r")) == NULL) {
        if (fd != NOTOK)
 
     if ((fd = dup (md)) == NOTOK || (fp = fdopen (fd, "r")) == NULL) {
        if (fd != NOTOK)
@@ -467,7 +455,7 @@ mbx_size (int md, off_t start, off_t stop)
 int
 mbx_close (char *mailbox, int md)
 {
 int
 mbx_close (char *mailbox, int md)
 {
-    if (lkclose (md, mailbox) == 0)
+    if (lkclosespool (md, mailbox) == 0)
         return OK;
     return NOTOK;
 }
         return OK;
     return NOTOK;
 }
@@ -481,7 +469,7 @@ mbx_close (char *mailbox, int md)
 char *
 map_name (char *file)
 {
 char *
 map_name (char *file)
 {
-    register char *cp, *dp;
+    char *cp, *dp;
     static char buffer[BUFSIZ];
 
     if ((dp = strchr(cp = r1bindex (file, '/'), '.')) == NULL)
     static char buffer[BUFSIZ];
 
     if ((dp = strchr(cp = r1bindex (file, '/'), '.')) == NULL)
@@ -499,10 +487,10 @@ map_name (char *file)
 int
 map_read (char *file, long pos, struct drop **drops, int noisy)
 {
 int
 map_read (char *file, long pos, struct drop **drops, int noisy)
 {
-    register int i, md, msgp;
-    register char *cp;
+    int i, md, msgp;
+    char *cp;
     struct drop d;
     struct drop d;
-    register struct drop *mp, *dp;
+    struct drop *mp, *dp;
 
     if ((md = open (cp = map_name (file), O_RDONLY)) == NOTOK
            || map_chk (cp, md, mp = &d, pos, noisy)) {
 
     if ((md = open (cp = map_name (file), O_RDONLY)) == NOTOK
            || map_chk (cp, md, mp = &d, pos, noisy)) {
@@ -512,21 +500,17 @@ map_read (char *file, long pos, struct drop **drops, int noisy)
     }
 
     msgp = mp->d_id;
     }
 
     msgp = mp->d_id;
-    dp = (struct drop *) calloc ((size_t) (msgp + 1), sizeof(*dp));
-    if (dp == NULL) {
-       close (md);
-       return 0;
-    }
-
+    dp = mh_xcalloc(msgp + 1, sizeof *dp);
     memcpy((char *) dp, (char *) mp, sizeof(*dp));
 
     lseek (md, (off_t) sizeof(*mp), SEEK_SET);
     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;
        i = 0;
-       free ((char *) dp);
+       free(dp);
     } else {
 #ifdef NTOHLSWAP
     } else {
 #ifdef NTOHLSWAP
-       register struct drop *tdp;
+       struct drop *tdp;
        int j;
 
        for (j = 0, tdp = dp; j < i / sizeof(*dp); j++, tdp++) {
        int j;
 
        for (j = 0, tdp = dp; j < i / sizeof(*dp); j++, tdp++) {
@@ -549,12 +533,12 @@ int
 map_write (char *mailbox, int md, int id, long last, off_t start,
            off_t stop, long pos, int size, int noisy)
 {
 map_write (char *mailbox, int md, int id, long last, off_t start,
            off_t stop, long pos, int size, int noisy)
 {
-    register int i;
+    int i;
     int clear, fd, td;
     char *file;
     int clear, fd, td;
     char *file;
-    register struct drop *dp;
+    struct drop *dp;
     struct drop d1, d2, *rp;
     struct drop d1, d2, *rp;
-    register FILE *fp;
+    FILE *fp;
     struct stat st;
 
     if ((fd = map_open (file = map_name (mailbox), md)) == NOTOK)
     struct stat st;
 
     if ((fd = map_open (file = map_name (mailbox), md)) == NOTOK)
@@ -566,7 +550,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)) {
        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;
        mbx_close (file, fd);
        if ((fd = map_open (file, md)) == NOTOK)
            return NOTOK;
@@ -583,7 +567,7 @@ map_write (char *mailbox, int md, int id, long last, off_t start,
            return NOTOK;
        }
 
            return NOTOK;
        }
 
-       switch (i = mbx_read (fp, 0, &rp, noisy)) {
+       switch (i = mbx_read (fp, 0, &rp)) {
            case NOTOK:
                fclose (fp);
                mbx_close (file, fd);
            case NOTOK:
                fclose (fp);
                mbx_close (file, fd);
@@ -607,7 +591,7 @@ map_write (char *mailbox, int md, int id, long last, off_t start,
                        return NOTOK;
                    }
                }
                        return NOTOK;
                    }
                }
-               free ((char *) rp);
+               free(rp);
                fclose (fp);
                break;
        }
                fclose (fp);
                break;
        }
@@ -654,7 +638,7 @@ map_open (char *file, int md)
     mode_t mode;
     struct stat st;
 
     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);
 }
 
     return mbx_open (file, OTHER_FORMAT, st.st_uid, st.st_gid, mode);
 }
 
@@ -662,9 +646,9 @@ map_open (char *file, int md)
 int
 map_chk (char *file, int fd, struct drop *dp, long pos, int noisy)
 {
 int
 map_chk (char *file, int fd, struct drop *dp, long pos, int noisy)
 {
-    long count;
+    ssize_t count;
     struct drop d, tmpd;
     struct drop d, tmpd;
-    register struct drop *dl;
+    struct drop *dl;
 
     if (read (fd, (char *) &tmpd, sizeof(*dp)) != sizeof(*dp)) {
 #ifdef notdef
 
     if (read (fd, (char *) &tmpd, sizeof(*dp)) != sizeof(*dp)) {
 #ifdef notdef
@@ -703,7 +687,7 @@ map_chk (char *file, int fd, struct drop *dp, long pos, int noisy)
     }
 
     dl = &d;
     }
 
     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
     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