changeset 1228:2366f9d73745

Several cpio bugfixes spotted by Isaac Dunham.
author Rob Landley <rob@landley.net>
date Tue, 25 Mar 2014 07:24:50 -0500
parents 903acf85bd1d
children 34d4ee60e78f
files toys/pending/cpio.c
diffstat 1 files changed, 6 insertions(+), 3 deletions(-) [+]
line wrap: on
line diff
--- a/toys/pending/cpio.c	Mon Mar 24 08:19:21 2014 -0500
+++ b/toys/pending/cpio.c	Tue Mar 25 07:24:50 2014 -0500
@@ -157,13 +157,15 @@
     for (;;) {
       struct stat st;
       unsigned nlen = strlen(name)+1, error = 0, zero = 0;
-      int len, fd;
+      int len, fd = -1;
       ssize_t llen;
 
       len = getline(&name, &size, stdin);
       if (len<1) break;
       if (name[len-1] == '\n') name[--len] = 0;
-      if (lstat(name, &st) || (fd = open(name, O_RDONLY))<0) {
+      if (lstat(name, &st)
+          || (S_ISREG(st.st_mode) && (fd = open(name, O_RDONLY))<0))
+      {
         perror_msg("%s", name);
         continue;
       }
@@ -191,13 +193,14 @@
           else perror_msg("readlink '%s'", name);
         } else while (llen) {
           nlen = llen > sizeof(toybuf) ? sizeof(toybuf) : llen;
+          llen -= nlen;
           // If read fails, write anyway (already wrote size in header)
           if (nlen != readall(fd, toybuf, nlen))
             if (!error++) perror_msg("bad read from file '%s'", name);
           xwrite(afd, toybuf, nlen);
         }
         llen = st.st_size & 3;
-        if (nlen) write(afd, &zero, 4-llen);
+        if (llen) write(afd, &zero, 4-llen);
       }
       close(fd);
     }