Use correct variable types when copying files.
authorSimon Schubert <corecode@dragonflybsd.org>
Tue, 12 Jun 2007 20:56:16 +0000 (20:56 +0000)
committerSimon Schubert <corecode@dragonflybsd.org>
Tue, 12 Jun 2007 20:56:16 +0000 (20:56 +0000)
Reported/Reviewed-by:  Trevor Kendall

bin/cp/utils.c
bin/mv/mv.c

index af96f1e..0416def 100644 (file)
@@ -32,7 +32,7 @@
  *
  * @(#)utils.c 8.3 (Berkeley) 4/1/94
  * $FreeBSD: src/bin/cp/utils.c,v 1.45 2005/02/09 17:37:37 ru Exp $
- * $DragonFly: src/bin/cp/utils.c,v 1.9 2006/08/03 16:40:45 swildner Exp $
+ * $DragonFly: src/bin/cp/utils.c,v 1.10 2007/06/12 20:56:16 corecode Exp $
  */
 
 #include <sys/param.h>
@@ -62,7 +62,9 @@ copy_file(const FTSENT *entp, int dne)
 {
        static char buf[MAXBSIZE];
        struct stat *fs;
-       int ch, checkch, from_fd, rcount, rval, to_fd, wcount, wresid, wtotal;
+       int ch, checkch, from_fd, rval, to_fd;
+       size_t rcount, wcount, wresid;
+       off_t wtotal;
        char *bufp;
 #ifdef VM_AND_BUFFER_CACHE_SYNCHRONIZED
        char *p;
@@ -140,6 +142,8 @@ copy_file(const FTSENT *entp, int dne)
                        for (bufp = p, wresid = fs->st_size; ;
                            bufp += wcount, wresid -= wcount) {
                                wcount = write(to_fd, bufp, wresid);
+                               if ((ssize_t)wcount == -1)
+                                       break;
                                wtotal += wcount;
                                if (info) {
                                        info = 0;
@@ -148,7 +152,7 @@ copy_file(const FTSENT *entp, int dne)
                                            entp->fts_path, to.p_path,
                                            cp_pct(wtotal, fs->st_size));
                                }
-                               if (wcount >= wresid || wcount <= 0)
+                               if (wcount >= wresid || wcount == 0)
                                        break;
                        }
                        if (wcount != wresid) {
@@ -165,10 +169,12 @@ copy_file(const FTSENT *entp, int dne)
 #endif
        {
                wtotal = 0;
-               while ((rcount = read(from_fd, buf, MAXBSIZE)) > 0) {
+               while ((ssize_t)(rcount = read(from_fd, buf, MAXBSIZE)) != 0) {
                        for (bufp = buf, wresid = rcount; ;
                            bufp += wcount, wresid -= wcount) {
                                wcount = write(to_fd, bufp, wresid);
+                               if ((ssize_t)wcount == -1)
+                                       break;
                                wtotal += wcount;
                                if (info) {
                                        info = 0;
@@ -177,7 +183,7 @@ copy_file(const FTSENT *entp, int dne)
                                            entp->fts_path, to.p_path,
                                            cp_pct(wtotal, fs->st_size));
                                }
-                               if (wcount >= wresid || wcount <= 0)
+                               if (wcount >= wresid || wcount == 0)
                                        break;
                        }
                        if (wcount != wresid) {
@@ -186,7 +192,7 @@ copy_file(const FTSENT *entp, int dne)
                                break;
                        }
                }
-               if (rcount < 0) {
+               if ((ssize_t)rcount == -1) {
                        warn("%s", entp->fts_path);
                        rval = 1;
                }
index a615e24..86b0c60 100644 (file)
@@ -32,7 +32,7 @@
  * @(#) Copyright (c) 1989, 1993, 1994 The Regents of the University of California.  All rights reserved.
  * @(#)mv.c    8.2 (Berkeley) 4/2/94
  * $FreeBSD: /usr/local/www/cvsroot/FreeBSD/src/bin/mv/mv.c,v 1.24.2.6 2004/03/24 08:34:36 pjd Exp $
- * $DragonFly: src/bin/mv/mv.c,v 1.12 2006/11/11 12:05:36 victor Exp $
+ * $DragonFly: src/bin/mv/mv.c,v 1.13 2007/06/12 20:56:16 corecode Exp $
  */
 
 #include <sys/param.h>
@@ -248,8 +248,9 @@ fastcopy(const char *from, const char *to, struct stat *sbp)
        static u_int blen;
        static char *bp;
        mode_t oldmode;
-       int nread, from_fd, to_fd;
-       ssize_t wtotal = 0, wcount;
+       int from_fd, to_fd;
+       size_t nread, wcount;
+       off_t wtotal = 0;
 
        if ((from_fd = open(from, O_RDONLY, 0)) < 0) {
                warn("%s", from);
@@ -273,8 +274,8 @@ fastcopy(const char *from, const char *to, struct stat *sbp)
                close(from_fd);
                return (1);
        }
-       while ((nread = read(from_fd, bp, (size_t)blen)) > 0) {
-               wcount = write(to_fd, bp, (size_t)nread);
+       while ((ssize_t)(nread = read(from_fd, bp, (size_t)blen)) != -1) {
+               wcount = write(to_fd, bp, nread);
                wtotal += wcount;
 
                if (wcount != nread) {
@@ -289,7 +290,7 @@ fastcopy(const char *from, const char *to, struct stat *sbp)
                                        mv_pct(wtotal, sbp->st_size));
                }
        }
-       if (nread < 0) {
+       if ((ssize_t)nread == -1) {
                warn("%s", from);
 err:           if (unlink(to))
                        warn("%s: remove", to);