clang -cc1 -cc1 -triple amd64-unknown-openbsd7.0 -analyze -disable-free -disable-llvm-verifier -discard-value-names -main-file-name xinstall.c -analyzer-store=region -analyzer-opt-analyze-nested-blocks -analyzer-checker=core -analyzer-checker=apiModeling -analyzer-checker=unix -analyzer-checker=deadcode -analyzer-checker=security.insecureAPI.UncheckedReturn -analyzer-checker=security.insecureAPI.getpw -analyzer-checker=security.insecureAPI.gets -analyzer-checker=security.insecureAPI.mktemp -analyzer-checker=security.insecureAPI.mkstemp -analyzer-checker=security.insecureAPI.vfork -analyzer-checker=nullability.NullPassedToNonnull -analyzer-checker=nullability.NullReturnedFromNonnull -analyzer-output plist -w -setup-static-analyzer -mrelocation-model pic -pic-level 1 -pic-is-pie -mframe-pointer=all -relaxed-aliasing -fno-rounding-math -mconstructor-aliases -munwind-tables -target-cpu x86-64 -target-feature +retpoline-indirect-calls -target-feature +retpoline-indirect-branches -tune-cpu generic -debugger-tuning=gdb -fcoverage-compilation-dir=/usr/src/usr.bin/xinstall/obj -resource-dir /usr/local/lib/clang/13.0.0 -internal-isystem /usr/local/lib/clang/13.0.0/include -internal-externc-isystem /usr/include -O2 -fdebug-compilation-dir=/usr/src/usr.bin/xinstall/obj -ferror-limit 19 -fwrapv -D_RET_PROTECTOR -ret-protector -fgnuc-version=4.2.1 -vectorize-loops -vectorize-slp -fno-builtin-malloc -fno-builtin-calloc -fno-builtin-realloc -fno-builtin-valloc -fno-builtin-free -fno-builtin-strdup -fno-builtin-strndup -analyzer-output=html -faddrsig -D__GCC_HAVE_DWARF2_CFI_ASM=1 -o /home/ben/Projects/vmm/scan-build/2022-01-12-194120-40624-1 -x c /usr/src/usr.bin/xinstall/xinstall.c
1 | |
2 | |
3 | |
4 | |
5 | |
6 | |
7 | |
8 | |
9 | |
10 | |
11 | |
12 | |
13 | |
14 | |
15 | |
16 | |
17 | |
18 | |
19 | |
20 | |
21 | |
22 | |
23 | |
24 | |
25 | |
26 | |
27 | |
28 | |
29 | |
30 | |
31 | |
32 | |
33 | #include <sys/types.h> |
34 | #include <sys/wait.h> |
35 | #include <sys/mman.h> |
36 | #include <sys/stat.h> |
37 | |
38 | #include <ctype.h> |
39 | #include <err.h> |
40 | #include <errno.h> |
41 | #include <fcntl.h> |
42 | #include <grp.h> |
43 | #include <paths.h> |
44 | #include <pwd.h> |
45 | #include <stdio.h> |
46 | #include <stdlib.h> |
47 | #include <string.h> |
48 | #include <unistd.h> |
49 | #include <limits.h> |
50 | #include <libgen.h> |
51 | |
52 | #include "pathnames.h" |
53 | |
54 | #define _MAXBSIZE (64 * 1024) |
55 | |
56 | #define MINIMUM(a, b) (((a) < (b)) ? (a) : (b)) |
57 | |
58 | #define DIRECTORY 0x01 /* Tell install it's a directory. */ |
59 | #define SETFLAGS 0x02 /* Tell install to set flags. */ |
60 | #define USEFSYNC 0x04 /* Tell install to use fsync(2). */ |
61 | #define NOCHANGEBITS (UF_IMMUTABLE | UF_APPEND | SF_IMMUTABLE | SF_APPEND) |
62 | #define BACKUP_SUFFIX ".old" |
63 | |
64 | int dobackup, docompare, dodest, dodir, dopreserve, dostrip; |
65 | int mode = S_IRWXU|S_IRGRP|S_IXGRP|S_IROTH|S_IXOTH; |
66 | char pathbuf[PATH_MAX], tempfile[PATH_MAX]; |
67 | char *suffix = BACKUP_SUFFIX; |
68 | uid_t uid = (uid_t)-1; |
69 | gid_t gid = (gid_t)-1; |
70 | |
71 | void copy(int, char *, int, char *, off_t, int); |
72 | int compare(int, const char *, off_t, int, const char *, off_t); |
73 | void install(char *, char *, u_long, u_int); |
74 | void install_dir(char *, int); |
75 | void strip(char *); |
76 | void usage(void); |
77 | int create_tempfile(char *, char *, size_t); |
78 | int file_write(int, char *, size_t, int *, int *, int); |
79 | void file_flush(int, int); |
80 | |
81 | int |
82 | main(int argc, char *argv[]) |
83 | { |
84 | struct stat from_sb, to_sb; |
85 | void *set; |
86 | u_int32_t fset; |
87 | u_int iflags; |
88 | int ch, no_target; |
89 | char *flags, *to_name, *group = NULL, *owner = NULL; |
90 | const char *errstr; |
91 | |
92 | iflags = 0; |
93 | while ((ch = getopt(argc, argv, "B:bCcDdFf:g:m:o:pSs")) != -1) |
| 1 | Assuming the condition is true | |
|
| 2 | | Loop condition is true. Entering loop body | |
|
| 7 | | Assuming the condition is false | |
|
| 8 | | Loop condition is false. Execution continues on line 147 | |
|
94 | switch(ch) { |
| 3 | | Control jumps to 'case 102:' at line 110 | |
|
95 | case 'C': |
96 | docompare = 1; |
97 | break; |
98 | case 'B': |
99 | suffix = optarg; |
100 | |
101 | case 'b': |
102 | dobackup = 1; |
103 | break; |
104 | case 'c': |
105 | |
106 | break; |
107 | case 'F': |
108 | iflags |= USEFSYNC; |
109 | break; |
110 | case 'f': |
111 | flags = optarg; |
112 | if (strtofflags(&flags, &fset, NULL)) |
| 4 | | Assuming the condition is false | |
|
| |
113 | errx(1, "%s: invalid flag", flags); |
114 | iflags |= SETFLAGS; |
115 | break; |
| 6 | | Execution continues on line 93 | |
|
116 | case 'g': |
117 | group = optarg; |
118 | break; |
119 | case 'm': |
120 | if (!(set = setmode(optarg))) |
121 | errx(1, "%s: invalid file mode", optarg); |
122 | mode = getmode(set, 0); |
123 | free(set); |
124 | break; |
125 | case 'o': |
126 | owner = optarg; |
127 | break; |
128 | case 'p': |
129 | docompare = dopreserve = 1; |
130 | break; |
131 | case 'S': |
132 | |
133 | break; |
134 | case 's': |
135 | dostrip = 1; |
136 | break; |
137 | case 'D': |
138 | dodest = 1; |
139 | break; |
140 | case 'd': |
141 | dodir = 1; |
142 | break; |
143 | case '?': |
144 | default: |
145 | usage(); |
146 | } |
147 | argc -= optind; |
148 | argv += optind; |
149 | |
150 | |
151 | if ((docompare || dostrip) && dodir) |
| 9 | | Assuming 'docompare' is 0 | |
|
| 10 | | Assuming 'dostrip' is 0 | |
|
152 | usage(); |
153 | |
154 | |
155 | if (argc == 0 || (argc == 1 && !dodir)) |
| 11 | | Assuming 'argc' is not equal to 0 | |
|
| 12 | | Assuming 'argc' is not equal to 1 | |
|
156 | usage(); |
157 | |
158 | |
159 | if (group != NULL && gid_from_group(group, &gid) == -1) { |
160 | gid = strtonum(group, 0, GID_MAX, &errstr); |
161 | if (errstr != NULL) |
162 | errx(1, "unknown group %s", group); |
163 | } |
164 | if (owner != NULL && uid_from_user(owner, &uid) == -1) { |
165 | uid = strtonum(owner, 0, UID_MAX, &errstr); |
166 | if (errstr != NULL) |
167 | errx(1, "unknown user %s", owner); |
168 | } |
169 | |
170 | if (dodir) { |
| |
| |
171 | for (; *argv != NULL; ++argv) |
172 | install_dir(*argv, mode); |
173 | exit(0); |
174 | |
175 | } |
176 | |
177 | if (dodest) { |
| |
| |
178 | char *dest = dirname(argv[argc - 1]); |
179 | if (dest == NULL) |
180 | errx(1, "cannot determine dirname"); |
181 | |
182 | |
183 | |
184 | |
185 | |
186 | install_dir(dest, 0755); |
187 | } |
188 | |
189 | no_target = stat(to_name = argv[argc - 1], &to_sb); |
190 | if (!no_target && S_ISDIR(to_sb.st_mode)) { |
| 17 | | Assuming 'no_target' is 0 | |
|
| 18 | | Assuming the condition is true | |
|
| |
191 | for (; *argv != to_name; ++argv) |
| 20 | | Loop condition is true. Entering loop body | |
|
192 | install(*argv, to_name, fset, iflags | DIRECTORY); |
| |
193 | exit(0); |
194 | |
195 | } |
196 | |
197 | |
198 | if (argc != 2) |
199 | errx(1, "Target: %s", argv[argc-1]); |
200 | |
201 | if (!no_target) { |
202 | if (stat(*argv, &from_sb)) |
203 | err(1, "%s", *argv); |
204 | if (!S_ISREG(to_sb.st_mode)) |
205 | errc(1, EFTYPE, "%s", to_name); |
206 | if (to_sb.st_dev == from_sb.st_dev && |
207 | to_sb.st_ino == from_sb.st_ino) |
208 | errx(1, "%s and %s are the same file", *argv, to_name); |
209 | } |
210 | install(*argv, to_name, fset, iflags); |
211 | exit(0); |
212 | |
213 | } |
214 | |
215 | |
216 | |
217 | |
218 | |
219 | void |
220 | install(char *from_name, char *to_name, u_long fset, u_int flags) |
221 | { |
222 | struct stat from_sb, to_sb; |
223 | struct timespec ts[2]; |
224 | int devnull, from_fd, to_fd, serrno, files_match = 0; |
225 | char *p; |
226 | char *target_name = tempfile; |
227 | |
228 | (void)memset((void *)&from_sb, 0, sizeof(from_sb)); |
229 | (void)memset((void *)&to_sb, 0, sizeof(to_sb)); |
230 | |
231 | |
232 | if (flags & DIRECTORY || strcmp(from_name, _PATH_DEVNULL)) { |
233 | if (stat(from_name, &from_sb)) |
| 22 | | Assuming the condition is false | |
|
| |
234 | err(1, "%s", from_name); |
235 | if (!S_ISREG(from_sb.st_mode)) |
| 24 | | Assuming the condition is true | |
|
| |
236 | errc(1, EFTYPE, "%s", from_name); |
237 | |
238 | if (flags & DIRECTORY) { |
| |
239 | (void)snprintf(pathbuf, sizeof(pathbuf), "%s/%s", |
240 | to_name, |
241 | (p = strrchr(from_name, '/')) ? ++p : from_name); |
| |
| |
242 | to_name = pathbuf; |
243 | } |
244 | devnull = 0; |
245 | } else { |
246 | devnull = 1; |
247 | } |
248 | |
249 | if (stat(to_name, &to_sb) == 0) { |
| 29 | | Assuming the condition is false | |
|
| |
250 | |
251 | if (docompare && !S_ISREG(to_sb.st_mode)) { |
252 | docompare = 0; |
253 | warnc(EFTYPE, "%s", to_name); |
254 | } |
255 | } else if (docompare) { |
| |
256 | |
257 | docompare = 0; |
258 | } |
259 | |
260 | if (!devnull) { |
| |
261 | if ((from_fd = open(from_name, O_RDONLY)) == -1) |
| 33 | | Assuming the condition is false | |
|
| |
262 | err(1, "%s", from_name); |
263 | } |
264 | |
265 | to_fd = create_tempfile(to_name, tempfile, sizeof(tempfile)); |
266 | if (to_fd < 0) |
| 35 | | Assuming 'to_fd' is >= 0 | |
|
| |
267 | err(1, "%s", tempfile); |
268 | |
269 | if (!devnull) |
| |
270 | copy(from_fd, from_name, to_fd, tempfile, from_sb.st_size, |
271 | ((off_t)from_sb.st_blocks * S_BLKSIZE < from_sb.st_size)); |
| 38 | | Assuming the condition is true | |
|
272 | |
273 | if (dostrip) { |
| 39 | | Assuming 'dostrip' is not equal to 0 | |
|
| |
274 | strip(tempfile); |
| |
275 | |
276 | |
277 | |
278 | |
279 | |
280 | close(to_fd); |
281 | if ((to_fd = open(tempfile, O_RDONLY)) == -1) |
282 | err(1, "stripping %s", to_name); |
283 | } |
284 | |
285 | |
286 | |
287 | |
288 | if (docompare) { |
289 | int temp_fd = to_fd; |
290 | struct stat temp_sb; |
291 | |
292 | |
293 | if ((to_fd = open(to_name, O_RDONLY)) == -1) |
294 | err(1, "%s", to_name); |
295 | |
296 | if (fstat(temp_fd, &temp_sb)) { |
297 | serrno = errno; |
298 | (void)unlink(tempfile); |
299 | errc(1, serrno, "%s", tempfile); |
300 | } |
301 | |
302 | if (compare(temp_fd, tempfile, temp_sb.st_size, to_fd, |
303 | to_name, to_sb.st_size) == 0) { |
304 | |
305 | |
306 | |
307 | |
308 | |
309 | if (to_sb.st_nlink != 1) { |
310 | ts[0] = to_sb.st_atim; |
311 | ts[1] = to_sb.st_mtim; |
312 | futimens(temp_fd, ts); |
313 | } else { |
314 | files_match = 1; |
315 | (void)unlink(tempfile); |
316 | target_name = to_name; |
317 | (void)close(temp_fd); |
318 | } |
319 | } |
320 | if (!files_match) { |
321 | (void)close(to_fd); |
322 | to_fd = temp_fd; |
323 | } |
324 | } |
325 | |
326 | |
327 | |
328 | |
329 | if (dopreserve && !files_match) { |
330 | ts[0] = from_sb.st_atim; |
331 | ts[1] = from_sb.st_mtim; |
332 | futimens(to_fd, ts); |
333 | } |
334 | |
335 | |
336 | |
337 | |
338 | |
339 | if ((gid != (gid_t)-1 || uid != (uid_t)-1) && |
340 | fchown(to_fd, uid, gid)) { |
341 | serrno = errno; |
342 | if (target_name == tempfile) |
343 | (void)unlink(target_name); |
344 | errx(1, "%s: chown/chgrp: %s", target_name, strerror(serrno)); |
345 | } |
346 | if (fchmod(to_fd, mode)) { |
347 | serrno = errno; |
348 | if (target_name == tempfile) |
349 | (void)unlink(target_name); |
350 | errx(1, "%s: chmod: %s", target_name, strerror(serrno)); |
351 | } |
352 | |
353 | |
354 | |
355 | |
356 | |
357 | if (fchflags(to_fd, |
358 | flags & SETFLAGS ? fset : from_sb.st_flags & ~UF_NODUMP)) { |
359 | if (errno != EOPNOTSUPP || (from_sb.st_flags & ~UF_NODUMP) != 0) |
360 | warnx("%s: chflags: %s", target_name, strerror(errno)); |
361 | } |
362 | |
363 | if (flags & USEFSYNC) |
364 | fsync(to_fd); |
365 | (void)close(to_fd); |
366 | if (!devnull) |
367 | (void)close(from_fd); |
368 | |
369 | |
370 | |
371 | |
372 | |
373 | if (!files_match) { |
374 | |
375 | if (to_sb.st_flags & (NOCHANGEBITS)) |
376 | (void)chflags(to_name, to_sb.st_flags & ~(NOCHANGEBITS)); |
377 | if (dobackup) { |
378 | char backup[PATH_MAX]; |
379 | (void)snprintf(backup, PATH_MAX, "%s%s", to_name, |
380 | suffix); |
381 | |
382 | if (rename(to_name, backup) == -1 && errno != ENOENT) { |
383 | serrno = errno; |
384 | unlink(tempfile); |
385 | errx(1, "rename: %s to %s: %s", to_name, |
386 | backup, strerror(serrno)); |
387 | } |
388 | } |
389 | if (rename(tempfile, to_name) == -1 ) { |
390 | serrno = errno; |
391 | unlink(tempfile); |
392 | errx(1, "rename: %s to %s: %s", tempfile, |
393 | to_name, strerror(serrno)); |
394 | } |
395 | } |
396 | } |
397 | |
398 | |
399 | |
400 | |
401 | |
402 | void |
403 | copy(int from_fd, char *from_name, int to_fd, char *to_name, off_t size, |
404 | int sparse) |
405 | { |
406 | ssize_t nr, nw; |
407 | int serrno; |
408 | char *p, buf[_MAXBSIZE]; |
409 | |
410 | if (size == 0) |
411 | return; |
412 | |
413 | |
414 | if (lseek(from_fd, (off_t)0, SEEK_SET) == (off_t)-1) |
415 | err(1, "lseek: %s", from_name); |
416 | if (lseek(to_fd, (off_t)0, SEEK_SET) == (off_t)-1) |
417 | err(1, "lseek: %s", to_name); |
418 | |
419 | |
420 | |
421 | |
422 | |
423 | |
424 | if (!sparse && size <= 8 * 1048576) { |
425 | size_t siz; |
426 | |
427 | if ((p = mmap(NULL, (size_t)size, PROT_READ, MAP_PRIVATE, |
428 | from_fd, (off_t)0)) == MAP_FAILED) { |
429 | serrno = errno; |
430 | (void)unlink(to_name); |
431 | errc(1, serrno, "%s", from_name); |
432 | } |
433 | madvise(p, size, MADV_SEQUENTIAL); |
434 | siz = (size_t)size; |
435 | if ((nw = write(to_fd, p, siz)) != siz) { |
436 | serrno = errno; |
437 | (void)unlink(to_name); |
438 | errx(1, "%s: %s", |
439 | to_name, strerror(nw > 0 ? EIO : serrno)); |
440 | } |
441 | (void) munmap(p, (size_t)size); |
442 | } else { |
443 | int sz, rem, isem = 1; |
444 | struct stat sb; |
445 | |
446 | |
447 | |
448 | |
449 | |
450 | if (fstat(to_fd, &sb) != 0 || sb.st_blksize == 0) |
451 | sz = S_BLKSIZE; |
452 | else |
453 | sz = sb.st_blksize; |
454 | rem = sz; |
455 | |
456 | while ((nr = read(from_fd, buf, sizeof(buf))) > 0) { |
457 | if (sparse) |
458 | nw = file_write(to_fd, buf, nr, &rem, &isem, sz); |
459 | else |
460 | nw = write(to_fd, buf, nr); |
461 | if (nw != nr) { |
462 | serrno = errno; |
463 | (void)unlink(to_name); |
464 | errx(1, "%s: %s", |
465 | to_name, strerror(nw > 0 ? EIO : serrno)); |
466 | } |
467 | } |
468 | if (sparse) |
469 | file_flush(to_fd, isem); |
470 | if (nr != 0) { |
471 | serrno = errno; |
472 | (void)unlink(to_name); |
473 | errc(1, serrno, "%s", from_name); |
474 | } |
475 | } |
476 | } |
477 | |
478 | |
479 | |
480 | |
481 | |
482 | int |
483 | compare(int from_fd, const char *from_name, off_t from_len, int to_fd, |
484 | const char *to_name, off_t to_len) |
485 | { |
486 | caddr_t p1, p2; |
487 | size_t length; |
488 | off_t from_off, to_off, remainder; |
489 | int dfound; |
490 | |
491 | if (from_len == 0 && from_len == to_len) |
492 | return (0); |
493 | |
494 | if (from_len != to_len) |
495 | return (1); |
496 | |
497 | |
498 | |
499 | |
500 | |
501 | from_off = to_off = (off_t)0; |
502 | remainder = from_len; |
503 | do { |
504 | length = MINIMUM(remainder, 8 * 1048576); |
505 | remainder -= length; |
506 | |
507 | if ((p1 = mmap(NULL, length, PROT_READ, MAP_PRIVATE, |
508 | from_fd, from_off)) == MAP_FAILED) |
509 | err(1, "%s", from_name); |
510 | if ((p2 = mmap(NULL, length, PROT_READ, MAP_PRIVATE, |
511 | to_fd, to_off)) == MAP_FAILED) |
512 | err(1, "%s", to_name); |
513 | if (length) { |
514 | madvise(p1, length, MADV_SEQUENTIAL); |
515 | madvise(p2, length, MADV_SEQUENTIAL); |
516 | } |
517 | |
518 | dfound = memcmp(p1, p2, length); |
519 | |
520 | (void) munmap(p1, length); |
521 | (void) munmap(p2, length); |
522 | |
523 | from_off += length; |
524 | to_off += length; |
525 | |
526 | } while (!dfound && remainder > 0); |
527 | |
528 | return(dfound); |
529 | } |
530 | |
531 | |
532 | |
533 | |
534 | |
535 | void |
536 | strip(char *to_name) |
537 | { |
538 | int serrno, status; |
539 | char * volatile path_strip; |
540 | pid_t pid; |
541 | |
542 | if (issetugid() || (path_strip = getenv("STRIP")) == NULL) |
| 42 | | Assuming the condition is true | |
|
543 | path_strip = _PATH_STRIP; |
544 | |
545 | switch ((pid = vfork())) { |
| 43 | | Control jumps to 'case 0:' at line 550 | |
|
546 | case -1: |
547 | serrno = errno; |
548 | (void)unlink(to_name); |
549 | errc(1, serrno, "forks"); |
550 | case 0: |
551 | execl(path_strip, "strip", "--", to_name, (char *)NULL); |
552 | warn("%s", path_strip); |
| 44 | | This function call is prohibited after a successful vfork |
|
553 | _exit(1); |
554 | default: |
555 | while (waitpid(pid, &status, 0) == -1) { |
556 | if (errno != EINTR) |
557 | break; |
558 | } |
559 | if (!WIFEXITED(status)) |
560 | (void)unlink(to_name); |
561 | } |
562 | } |
563 | |
564 | |
565 | |
566 | |
567 | |
568 | void |
569 | install_dir(char *path, int mode) |
570 | { |
571 | char *p; |
572 | struct stat sb; |
573 | int ch; |
574 | |
575 | for (p = path;; ++p) |
576 | if (!*p || (p != path && *p == '/')) { |
577 | ch = *p; |
578 | *p = '\0'; |
579 | if (mkdir(path, 0777)) { |
580 | int mkdir_errno = errno; |
581 | if (stat(path, &sb)) { |
582 | |
583 | errc(1, mkdir_errno, "%s", |
584 | path); |
585 | |
586 | } |
587 | if (!S_ISDIR(sb.st_mode)) { |
588 | |
589 | errc(1, ENOTDIR, "%s", path); |
590 | |
591 | } |
592 | } |
593 | if (!(*p = ch)) |
594 | break; |
595 | } |
596 | |
597 | if (((gid != (gid_t)-1 || uid != (uid_t)-1) && chown(path, uid, gid)) || |
598 | chmod(path, mode)) { |
599 | warn("%s", path); |
600 | } |
601 | } |
602 | |
603 | |
604 | |
605 | |
606 | |
607 | void |
608 | usage(void) |
609 | { |
610 | (void)fprintf(stderr, "\ |
611 | usage: install [-bCcDdFpSs] [-B suffix] [-f flags] [-g group] [-m mode] [-o owner]\n source ... target ...\n"); |
612 | exit(1); |
613 | |
614 | } |
615 | |
616 | |
617 | |
618 | |
619 | |
620 | int |
621 | create_tempfile(char *path, char *temp, size_t tsize) |
622 | { |
623 | char *p; |
624 | |
625 | strlcpy(temp, path, tsize); |
626 | if ((p = strrchr(temp, '/')) != NULL) |
627 | p++; |
628 | else |
629 | p = temp; |
630 | *p = '\0'; |
631 | strlcat(p, "INS@XXXXXXXXXX", tsize); |
632 | |
633 | return(mkstemp(temp)); |
634 | } |
635 | |
636 | |
637 | |
638 | |
639 | |
640 | |
641 | |
642 | |
643 | |
644 | |
645 | |
646 | |
647 | |
648 | |
649 | |
650 | |
651 | |
652 | |
653 | |
654 | |
655 | |
656 | |
657 | |
658 | |
659 | |
660 | |
661 | |
662 | |
663 | |
664 | |
665 | |
666 | |
667 | |
668 | |
669 | |
670 | |
671 | |
672 | |
673 | |
674 | |
675 | |
676 | |
677 | |
678 | |
679 | |
680 | |
681 | |
682 | |
683 | |
684 | int |
685 | file_write(int fd, char *str, size_t cnt, int *rem, int *isempt, int sz) |
686 | { |
687 | char *pt; |
688 | char *end; |
689 | size_t wcnt; |
690 | char *st = str; |
691 | |
692 | |
693 | |
694 | |
695 | while (cnt) { |
696 | if (!*rem) { |
697 | |
698 | |
699 | |
700 | |
701 | |
702 | *isempt = 1; |
703 | *rem = sz; |
704 | } |
705 | |
706 | |
707 | |
708 | |
709 | |
710 | wcnt = MINIMUM(cnt, *rem); |
711 | cnt -= wcnt; |
712 | *rem -= wcnt; |
713 | if (*isempt) { |
714 | |
715 | |
716 | |
717 | |
718 | pt = st; |
719 | end = st + wcnt; |
720 | |
721 | |
722 | |
723 | |
724 | while ((pt < end) && (*pt == '\0')) |
725 | ++pt; |
726 | |
727 | if (pt == end) { |
728 | |
729 | |
730 | |
731 | if (lseek(fd, (off_t)wcnt, SEEK_CUR) == -1) { |
732 | warn("lseek"); |
733 | return(-1); |
734 | } |
735 | st = pt; |
736 | continue; |
737 | } |
738 | |
739 | |
740 | |
741 | *isempt = 0; |
742 | } |
743 | |
744 | |
745 | |
746 | |
747 | if (write(fd, st, wcnt) != wcnt) { |
748 | warn("write"); |
749 | return(-1); |
750 | } |
751 | st += wcnt; |
752 | } |
753 | return(st - str); |
754 | } |
755 | |
756 | |
757 | |
758 | |
759 | |
760 | |
761 | |
762 | void |
763 | file_flush(int fd, int isempt) |
764 | { |
765 | static char blnk[] = "\0"; |
766 | |
767 | |
768 | |
769 | |
770 | |
771 | if (!isempt) |
772 | return; |
773 | |
774 | |
775 | |
776 | |
777 | if (lseek(fd, (off_t)-1, SEEK_CUR) == -1) { |
778 | warn("Failed seek on file"); |
779 | return; |
780 | } |
781 | |
782 | if (write(fd, blnk, 1) == -1) |
783 | warn("Failed write to file"); |
784 | return; |
785 | } |