clang -cc1 -cc1 -triple amd64-unknown-openbsd7.4 -analyze -disable-free -clear-ast-before-backend -disable-llvm-verifier -discard-value-names -main-file-name xinstall.c -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 -ffp-contract=on -fno-rounding-math -mconstructor-aliases -funwind-tables=2 -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/llvm16/lib/clang/16 -internal-isystem /usr/local/llvm16/lib/clang/16/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 -fcf-protection=branch -fno-jump-tables -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/scan/2024-01-11-140451-98009-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; |
| 1 | 'fset' declared without an initial value | |
|
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) |
| 2 | | Assuming the condition is false | |
|
| 3 | | Loop condition is false. Execution continues on line 146 | |
|
94 | switch(ch) { |
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)) |
113 | errx(1, "%s: invalid flag", flags); |
114 | iflags |= SETFLAGS; |
115 | break; |
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 | default: |
144 | usage(); |
145 | } |
146 | argc -= optind; |
147 | argv += optind; |
148 | |
149 | |
150 | if ((docompare || dostrip) && dodir) |
| 4 | | Assuming 'docompare' is not equal to 0 | |
|
| |
151 | usage(); |
152 | |
153 | |
154 | if (argc == 0 || (argc == 1 && !dodir)) |
| 6 | | Assuming 'argc' is not equal to 0 | |
|
| 7 | | Assuming 'argc' is not equal to 1 | |
|
155 | usage(); |
156 | |
157 | |
158 | if (group != NULL && gid_from_group(group, &gid) == -1) { |
159 | gid = strtonum(group, 0, GID_MAX, &errstr); |
160 | if (errstr != NULL) |
161 | errx(1, "unknown group %s", group); |
162 | } |
163 | if (owner != NULL && uid_from_user(owner, &uid) == -1) { |
164 | uid = strtonum(owner, 0, UID_MAX, &errstr); |
165 | if (errstr != NULL) |
166 | errx(1, "unknown user %s", owner); |
167 | } |
168 | |
169 | if (dodir) { |
| |
170 | for (; *argv != NULL; ++argv) |
171 | install_dir(*argv, mode); |
172 | exit(0); |
173 | |
174 | } |
175 | |
176 | if (dodest) { |
| |
| |
177 | char *dest = dirname(argv[argc - 1]); |
178 | if (dest == NULL) |
179 | errx(1, "cannot determine dirname"); |
180 | |
181 | |
182 | |
183 | |
184 | |
185 | install_dir(dest, 0755); |
186 | } |
187 | |
188 | no_target = stat(to_name = argv[argc - 1], &to_sb); |
189 | if (!no_target && S_ISDIR(to_sb.st_mode)) { |
| 11 | | Assuming 'no_target' is 0 | |
|
| 12 | | Assuming the condition is true | |
|
| |
190 | for (; *argv != to_name; ++argv) |
| 14 | | Loop condition is true. Entering loop body | |
|
191 | install(*argv, to_name, fset, iflags | DIRECTORY); |
| 15 | | 3rd function call argument is an uninitialized value |
|
192 | exit(0); |
193 | |
194 | } |
195 | |
196 | |
197 | if (argc != 2) |
198 | errx(1, "Target: %s", argv[argc-1]); |
199 | |
200 | if (!no_target) { |
201 | if (stat(*argv, &from_sb)) |
202 | err(1, "%s", *argv); |
203 | if (!S_ISREG(to_sb.st_mode)) |
204 | errc(1, EFTYPE, "%s", to_name); |
205 | if (to_sb.st_dev == from_sb.st_dev && |
206 | to_sb.st_ino == from_sb.st_ino) |
207 | errx(1, "%s and %s are the same file", *argv, to_name); |
208 | } |
209 | install(*argv, to_name, fset, iflags); |
210 | exit(0); |
211 | |
212 | } |
213 | |
214 | |
215 | |
216 | |
217 | |
218 | void |
219 | install(char *from_name, char *to_name, u_long fset, u_int flags) |
220 | { |
221 | struct stat from_sb, to_sb; |
222 | struct timespec ts[2]; |
223 | int devnull, from_fd, to_fd, serrno, files_match = 0; |
224 | char *p; |
225 | char *target_name = tempfile; |
226 | |
227 | (void)memset((void *)&from_sb, 0, sizeof(from_sb)); |
228 | (void)memset((void *)&to_sb, 0, sizeof(to_sb)); |
229 | |
230 | |
231 | if (flags & DIRECTORY || strcmp(from_name, _PATH_DEVNULL)) { |
232 | if (stat(from_name, &from_sb)) |
233 | err(1, "%s", from_name); |
234 | if (!S_ISREG(from_sb.st_mode)) |
235 | errc(1, EFTYPE, "%s", from_name); |
236 | |
237 | if (flags & DIRECTORY) { |
238 | (void)snprintf(pathbuf, sizeof(pathbuf), "%s/%s", |
239 | to_name, |
240 | (p = strrchr(from_name, '/')) ? ++p : from_name); |
241 | to_name = pathbuf; |
242 | } |
243 | devnull = 0; |
244 | } else { |
245 | devnull = 1; |
246 | } |
247 | |
248 | if (stat(to_name, &to_sb) == 0) { |
249 | |
250 | if (docompare && !S_ISREG(to_sb.st_mode)) { |
251 | docompare = 0; |
252 | warnc(EFTYPE, "%s", to_name); |
253 | } |
254 | } else if (docompare) { |
255 | |
256 | docompare = 0; |
257 | } |
258 | |
259 | if (!devnull) { |
260 | if ((from_fd = open(from_name, O_RDONLY)) == -1) |
261 | err(1, "%s", from_name); |
262 | } |
263 | |
264 | to_fd = create_tempfile(to_name, tempfile, sizeof(tempfile)); |
265 | if (to_fd < 0) |
266 | err(1, "%s", tempfile); |
267 | |
268 | if (!devnull) |
269 | copy(from_fd, from_name, to_fd, tempfile, from_sb.st_size, |
270 | ((off_t)from_sb.st_blocks * S_BLKSIZE < from_sb.st_size)); |
271 | |
272 | if (dostrip) { |
273 | strip(tempfile); |
274 | |
275 | |
276 | |
277 | |
278 | |
279 | close(to_fd); |
280 | if ((to_fd = open(tempfile, O_RDONLY)) == -1) |
281 | err(1, "stripping %s", to_name); |
282 | } |
283 | |
284 | |
285 | |
286 | |
287 | if (docompare) { |
288 | int temp_fd = to_fd; |
289 | struct stat temp_sb; |
290 | |
291 | |
292 | if ((to_fd = open(to_name, O_RDONLY)) == -1) |
293 | err(1, "%s", to_name); |
294 | |
295 | if (fstat(temp_fd, &temp_sb)) { |
296 | serrno = errno; |
297 | (void)unlink(tempfile); |
298 | errc(1, serrno, "%s", tempfile); |
299 | } |
300 | |
301 | if (compare(temp_fd, tempfile, temp_sb.st_size, to_fd, |
302 | to_name, to_sb.st_size) == 0) { |
303 | |
304 | |
305 | |
306 | |
307 | |
308 | if (to_sb.st_nlink != 1) { |
309 | ts[0] = to_sb.st_atim; |
310 | ts[1] = to_sb.st_mtim; |
311 | futimens(temp_fd, ts); |
312 | } else { |
313 | files_match = 1; |
314 | (void)unlink(tempfile); |
315 | target_name = to_name; |
316 | (void)close(temp_fd); |
317 | } |
318 | } |
319 | if (!files_match) { |
320 | (void)close(to_fd); |
321 | to_fd = temp_fd; |
322 | } |
323 | } |
324 | |
325 | |
326 | |
327 | |
328 | if (dopreserve && !files_match) { |
329 | ts[0] = from_sb.st_atim; |
330 | ts[1] = from_sb.st_mtim; |
331 | futimens(to_fd, ts); |
332 | } |
333 | |
334 | |
335 | |
336 | |
337 | |
338 | if ((gid != (gid_t)-1 || uid != (uid_t)-1) && |
339 | fchown(to_fd, uid, gid)) { |
340 | serrno = errno; |
341 | if (target_name == tempfile) |
342 | (void)unlink(target_name); |
343 | errx(1, "%s: chown/chgrp: %s", target_name, strerror(serrno)); |
344 | } |
345 | if (fchmod(to_fd, mode)) { |
346 | serrno = errno; |
347 | if (target_name == tempfile) |
348 | (void)unlink(target_name); |
349 | errx(1, "%s: chmod: %s", target_name, strerror(serrno)); |
350 | } |
351 | |
352 | |
353 | |
354 | |
355 | |
356 | if (fchflags(to_fd, |
357 | flags & SETFLAGS ? fset : from_sb.st_flags & ~UF_NODUMP)) { |
358 | if (errno != EOPNOTSUPP || (from_sb.st_flags & ~UF_NODUMP) != 0) |
359 | warnx("%s: chflags: %s", target_name, strerror(errno)); |
360 | } |
361 | |
362 | if (flags & USEFSYNC) |
363 | fsync(to_fd); |
364 | (void)close(to_fd); |
365 | if (!devnull) |
366 | (void)close(from_fd); |
367 | |
368 | |
369 | |
370 | |
371 | |
372 | if (!files_match) { |
373 | |
374 | if (to_sb.st_flags & (NOCHANGEBITS)) |
375 | (void)chflags(to_name, to_sb.st_flags & ~(NOCHANGEBITS)); |
376 | if (dobackup) { |
377 | char backup[PATH_MAX]; |
378 | (void)snprintf(backup, PATH_MAX, "%s%s", to_name, |
379 | suffix); |
380 | |
381 | if (rename(to_name, backup) == -1 && errno != ENOENT) { |
382 | serrno = errno; |
383 | unlink(tempfile); |
384 | errx(1, "rename: %s to %s: %s", to_name, |
385 | backup, strerror(serrno)); |
386 | } |
387 | } |
388 | if (rename(tempfile, to_name) == -1 ) { |
389 | serrno = errno; |
390 | unlink(tempfile); |
391 | errx(1, "rename: %s to %s: %s", tempfile, |
392 | to_name, strerror(serrno)); |
393 | } |
394 | } |
395 | } |
396 | |
397 | |
398 | |
399 | |
400 | |
401 | void |
402 | copy(int from_fd, char *from_name, int to_fd, char *to_name, off_t size, |
403 | int sparse) |
404 | { |
405 | ssize_t nr, nw; |
406 | int serrno; |
407 | char *p, buf[_MAXBSIZE]; |
408 | |
409 | if (size == 0) |
410 | return; |
411 | |
412 | |
413 | if (lseek(from_fd, (off_t)0, SEEK_SET) == (off_t)-1) |
414 | err(1, "lseek: %s", from_name); |
415 | if (lseek(to_fd, (off_t)0, SEEK_SET) == (off_t)-1) |
416 | err(1, "lseek: %s", to_name); |
417 | |
418 | |
419 | |
420 | |
421 | |
422 | |
423 | if (!sparse && size <= 8 * 1048576) { |
424 | size_t siz; |
425 | |
426 | if ((p = mmap(NULL, (size_t)size, PROT_READ, MAP_PRIVATE, |
427 | from_fd, (off_t)0)) == MAP_FAILED) { |
428 | serrno = errno; |
429 | (void)unlink(to_name); |
430 | errc(1, serrno, "%s", from_name); |
431 | } |
432 | madvise(p, size, MADV_SEQUENTIAL); |
433 | siz = (size_t)size; |
434 | if ((nw = write(to_fd, p, siz)) != siz) { |
435 | serrno = errno; |
436 | (void)unlink(to_name); |
437 | errx(1, "%s: %s", |
438 | to_name, strerror(nw > 0 ? EIO : serrno)); |
439 | } |
440 | (void) munmap(p, (size_t)size); |
441 | } else { |
442 | int sz, rem, isem = 1; |
443 | struct stat sb; |
444 | |
445 | |
446 | |
447 | |
448 | |
449 | if (fstat(to_fd, &sb) != 0 || sb.st_blksize == 0) |
450 | sz = S_BLKSIZE; |
451 | else |
452 | sz = sb.st_blksize; |
453 | rem = sz; |
454 | |
455 | while ((nr = read(from_fd, buf, sizeof(buf))) > 0) { |
456 | if (sparse) |
457 | nw = file_write(to_fd, buf, nr, &rem, &isem, sz); |
458 | else |
459 | nw = write(to_fd, buf, nr); |
460 | if (nw != nr) { |
461 | serrno = errno; |
462 | (void)unlink(to_name); |
463 | errx(1, "%s: %s", |
464 | to_name, strerror(nw > 0 ? EIO : serrno)); |
465 | } |
466 | } |
467 | if (sparse) |
468 | file_flush(to_fd, isem); |
469 | if (nr != 0) { |
470 | serrno = errno; |
471 | (void)unlink(to_name); |
472 | errc(1, serrno, "%s", from_name); |
473 | } |
474 | } |
475 | } |
476 | |
477 | |
478 | |
479 | |
480 | |
481 | int |
482 | compare(int from_fd, const char *from_name, off_t from_len, int to_fd, |
483 | const char *to_name, off_t to_len) |
484 | { |
485 | caddr_t p1, p2; |
486 | size_t length; |
487 | off_t from_off, to_off, remainder; |
488 | int dfound; |
489 | |
490 | if (from_len == 0 && from_len == to_len) |
491 | return (0); |
492 | |
493 | if (from_len != to_len) |
494 | return (1); |
495 | |
496 | |
497 | |
498 | |
499 | |
500 | from_off = to_off = (off_t)0; |
501 | remainder = from_len; |
502 | do { |
503 | length = MINIMUM(remainder, 8 * 1048576); |
504 | remainder -= length; |
505 | |
506 | if ((p1 = mmap(NULL, length, PROT_READ, MAP_PRIVATE, |
507 | from_fd, from_off)) == MAP_FAILED) |
508 | err(1, "%s", from_name); |
509 | if ((p2 = mmap(NULL, length, PROT_READ, MAP_PRIVATE, |
510 | to_fd, to_off)) == MAP_FAILED) |
511 | err(1, "%s", to_name); |
512 | if (length) { |
513 | madvise(p1, length, MADV_SEQUENTIAL); |
514 | madvise(p2, length, MADV_SEQUENTIAL); |
515 | } |
516 | |
517 | dfound = memcmp(p1, p2, length); |
518 | |
519 | (void) munmap(p1, length); |
520 | (void) munmap(p2, length); |
521 | |
522 | from_off += length; |
523 | to_off += length; |
524 | |
525 | } while (!dfound && remainder > 0); |
526 | |
527 | return(dfound); |
528 | } |
529 | |
530 | |
531 | |
532 | |
533 | |
534 | void |
535 | strip(char *to_name) |
536 | { |
537 | int serrno, status; |
538 | char * volatile path_strip; |
539 | pid_t pid; |
540 | |
541 | if (issetugid() || (path_strip = getenv("STRIP")) == NULL) |
542 | path_strip = _PATH_STRIP; |
543 | |
544 | switch ((pid = vfork())) { |
545 | case -1: |
546 | serrno = errno; |
547 | (void)unlink(to_name); |
548 | errc(1, serrno, "forks"); |
549 | case 0: |
550 | execl(path_strip, "strip", "--", to_name, (char *)NULL); |
551 | warn("%s", path_strip); |
552 | _exit(1); |
553 | default: |
554 | while (waitpid(pid, &status, 0) == -1) { |
555 | if (errno != EINTR) |
556 | break; |
557 | } |
558 | if (!WIFEXITED(status)) |
559 | (void)unlink(to_name); |
560 | } |
561 | } |
562 | |
563 | |
564 | |
565 | |
566 | |
567 | void |
568 | install_dir(char *path, int mode) |
569 | { |
570 | char *p; |
571 | struct stat sb; |
572 | int ch; |
573 | |
574 | for (p = path;; ++p) |
575 | if (!*p || (p != path && *p == '/')) { |
576 | ch = *p; |
577 | *p = '\0'; |
578 | if (mkdir(path, 0777)) { |
579 | int mkdir_errno = errno; |
580 | if (stat(path, &sb)) { |
581 | |
582 | errc(1, mkdir_errno, "%s", |
583 | path); |
584 | |
585 | } |
586 | if (!S_ISDIR(sb.st_mode)) { |
587 | |
588 | errc(1, ENOTDIR, "%s", path); |
589 | |
590 | } |
591 | } |
592 | if (!(*p = ch)) |
593 | break; |
594 | } |
595 | |
596 | if (((gid != (gid_t)-1 || uid != (uid_t)-1) && chown(path, uid, gid)) || |
597 | chmod(path, mode)) { |
598 | warn("%s", path); |
599 | } |
600 | } |
601 | |
602 | |
603 | |
604 | |
605 | |
606 | void |
607 | usage(void) |
608 | { |
609 | (void)fprintf(stderr, "\ |
610 | usage: install [-bCcDdFpSs] [-B suffix] [-f flags] [-g group] [-m mode] [-o owner]\n source ... target ...\n"); |
611 | exit(1); |
612 | |
613 | } |
614 | |
615 | |
616 | |
617 | |
618 | |
619 | int |
620 | create_tempfile(char *path, char *temp, size_t tsize) |
621 | { |
622 | char *p; |
623 | |
624 | strlcpy(temp, path, tsize); |
625 | if ((p = strrchr(temp, '/')) != NULL) |
626 | p++; |
627 | else |
628 | p = temp; |
629 | *p = '\0'; |
630 | strlcat(p, "INS@XXXXXXXXXX", tsize); |
631 | |
632 | return(mkstemp(temp)); |
633 | } |
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 | int |
684 | file_write(int fd, char *str, size_t cnt, int *rem, int *isempt, int sz) |
685 | { |
686 | char *pt; |
687 | char *end; |
688 | size_t wcnt; |
689 | char *st = str; |
690 | |
691 | |
692 | |
693 | |
694 | while (cnt) { |
695 | if (!*rem) { |
696 | |
697 | |
698 | |
699 | |
700 | |
701 | *isempt = 1; |
702 | *rem = sz; |
703 | } |
704 | |
705 | |
706 | |
707 | |
708 | |
709 | wcnt = MINIMUM(cnt, *rem); |
710 | cnt -= wcnt; |
711 | *rem -= wcnt; |
712 | if (*isempt) { |
713 | |
714 | |
715 | |
716 | |
717 | pt = st; |
718 | end = st + wcnt; |
719 | |
720 | |
721 | |
722 | |
723 | while ((pt < end) && (*pt == '\0')) |
724 | ++pt; |
725 | |
726 | if (pt == end) { |
727 | |
728 | |
729 | |
730 | if (lseek(fd, (off_t)wcnt, SEEK_CUR) == -1) { |
731 | warn("lseek"); |
732 | return(-1); |
733 | } |
734 | st = pt; |
735 | continue; |
736 | } |
737 | |
738 | |
739 | |
740 | *isempt = 0; |
741 | } |
742 | |
743 | |
744 | |
745 | |
746 | if (write(fd, st, wcnt) != wcnt) { |
747 | warn("write"); |
748 | return(-1); |
749 | } |
750 | st += wcnt; |
751 | } |
752 | return(st - str); |
753 | } |
754 | |
755 | |
756 | |
757 | |
758 | |
759 | |
760 | |
761 | void |
762 | file_flush(int fd, int isempt) |
763 | { |
764 | static char blnk[] = "\0"; |
765 | |
766 | |
767 | |
768 | |
769 | |
770 | if (!isempt) |
771 | return; |
772 | |
773 | |
774 | |
775 | |
776 | if (lseek(fd, (off_t)-1, SEEK_CUR) == -1) { |
777 | warn("Failed seek on file"); |
778 | return; |
779 | } |
780 | |
781 | if (write(fd, blnk, 1) == -1) |
782 | warn("Failed write to file"); |
783 | return; |
784 | } |