summaryrefslogtreecommitdiffstats
path: root/bin
diff options
context:
space:
mode:
authormaxim <maxim@FreeBSD.org>2006-10-07 12:11:21 +0000
committermaxim <maxim@FreeBSD.org>2006-10-07 12:11:21 +0000
commita9cb1cf4c1c019322d972507dbca9d570755222f (patch)
treee0f349296877452b899ef820ccd32f94381e0a3f /bin
parenta906aad1ec63043c546ed2bb9562c084e15ac66a (diff)
downloadFreeBSD-src-a9cb1cf4c1c019322d972507dbca9d570755222f.zip
FreeBSD-src-a9cb1cf4c1c019322d972507dbca9d570755222f.tar.gz
o Avoid division by zero.
o Place error checking code near to the syscall. Submitted by: bde
Diffstat (limited to 'bin')
-rw-r--r--bin/cp/utils.c11
1 files changed, 8 insertions, 3 deletions
diff --git a/bin/cp/utils.c b/bin/cp/utils.c
index 64ebc52..5286c74 100644
--- a/bin/cp/utils.c
+++ b/bin/cp/utils.c
@@ -54,7 +54,8 @@ __FBSDID("$FreeBSD$");
#include <unistd.h>
#include "extern.h"
-#define cp_pct(x,y) (int)(100.0 * (double)(x) / (double)(y))
+
+#define cp_pct(x, y) ((y == 0) ? 0 : (int)(100.0 * (x) / (y)))
int
copy_file(const FTSENT *entp, int dne)
@@ -149,6 +150,8 @@ copy_file(const FTSENT *entp, int dne)
for (bufp = p, wresid = fs->st_size; ;
bufp += wcount, wresid -= (size_t)wcount) {
wcount = write(to_fd, bufp, wresid);
+ if (wcount <= 0)
+ break;
wtotal += wcount;
if (info) {
info = 0;
@@ -158,7 +161,7 @@ copy_file(const FTSENT *entp, int dne)
cp_pct(wtotal, fs->st_size));
}
- if (wcount >= (ssize_t)wresid || wcount <= 0)
+ if (wcount >= (ssize_t)wresid)
break;
}
if (wcount != (ssize_t)wresid) {
@@ -179,6 +182,8 @@ copy_file(const FTSENT *entp, int dne)
for (bufp = buf, wresid = rcount; ;
bufp += wcount, wresid -= wcount) {
wcount = write(to_fd, bufp, wresid);
+ if (wcount <= 0)
+ break;
wtotal += wcount;
if (info) {
info = 0;
@@ -188,7 +193,7 @@ copy_file(const FTSENT *entp, int dne)
cp_pct(wtotal, fs->st_size));
}
- if (wcount >= (ssize_t)wresid || wcount <= 0)
+ if (wcount >= (ssize_t)wresid)
break;
}
if (wcount != (ssize_t)wresid) {
OpenPOWER on IntegriCloud