Mercurial > hg > toybox
comparison toys/pending/cpio.c @ 1228:2366f9d73745 draft
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 |
comparison
equal
deleted
inserted
replaced
1227:903acf85bd1d | 1228:2366f9d73745 |
---|---|
155 size_t size = 0; | 155 size_t size = 0; |
156 | 156 |
157 for (;;) { | 157 for (;;) { |
158 struct stat st; | 158 struct stat st; |
159 unsigned nlen = strlen(name)+1, error = 0, zero = 0; | 159 unsigned nlen = strlen(name)+1, error = 0, zero = 0; |
160 int len, fd; | 160 int len, fd = -1; |
161 ssize_t llen; | 161 ssize_t llen; |
162 | 162 |
163 len = getline(&name, &size, stdin); | 163 len = getline(&name, &size, stdin); |
164 if (len<1) break; | 164 if (len<1) break; |
165 if (name[len-1] == '\n') name[--len] = 0; | 165 if (name[len-1] == '\n') name[--len] = 0; |
166 if (lstat(name, &st) || (fd = open(name, O_RDONLY))<0) { | 166 if (lstat(name, &st) |
167 || (S_ISREG(st.st_mode) && (fd = open(name, O_RDONLY))<0)) | |
168 { | |
167 perror_msg("%s", name); | 169 perror_msg("%s", name); |
168 continue; | 170 continue; |
169 } | 171 } |
170 | 172 |
171 if (!S_ISREG(st.st_mode) && !S_ISLNK(st.st_mode)) st.st_size = 0; | 173 if (!S_ISREG(st.st_mode) && !S_ISLNK(st.st_mode)) st.st_size = 0; |
189 if (readlink(name, toybuf, sizeof(toybuf)-1) == llen) | 191 if (readlink(name, toybuf, sizeof(toybuf)-1) == llen) |
190 xwrite(afd, toybuf, llen); | 192 xwrite(afd, toybuf, llen); |
191 else perror_msg("readlink '%s'", name); | 193 else perror_msg("readlink '%s'", name); |
192 } else while (llen) { | 194 } else while (llen) { |
193 nlen = llen > sizeof(toybuf) ? sizeof(toybuf) : llen; | 195 nlen = llen > sizeof(toybuf) ? sizeof(toybuf) : llen; |
196 llen -= nlen; | |
194 // If read fails, write anyway (already wrote size in header) | 197 // If read fails, write anyway (already wrote size in header) |
195 if (nlen != readall(fd, toybuf, nlen)) | 198 if (nlen != readall(fd, toybuf, nlen)) |
196 if (!error++) perror_msg("bad read from file '%s'", name); | 199 if (!error++) perror_msg("bad read from file '%s'", name); |
197 xwrite(afd, toybuf, nlen); | 200 xwrite(afd, toybuf, nlen); |
198 } | 201 } |
199 llen = st.st_size & 3; | 202 llen = st.st_size & 3; |
200 if (nlen) write(afd, &zero, 4-llen); | 203 if (llen) write(afd, &zero, 4-llen); |
201 } | 204 } |
202 close(fd); | 205 close(fd); |
203 } | 206 } |
204 free(name); | 207 free(name); |
205 | 208 |