]> git.ipfire.org Git - thirdparty/dracut.git/blob - logtee.c
iscsi: always popd, even if there is no iscsi device
[thirdparty/dracut.git] / logtee.c
1 #define _GNU_SOURCE
2 #include <fcntl.h>
3 #include <stdio.h>
4 #include <stdlib.h>
5 #include <unistd.h>
6 #include <errno.h>
7 #include <limits.h>
8
9 #define BUFLEN 4096
10
11 int
12 main(int argc, char *argv[])
13 {
14 int fd;
15 int len, slen;
16
17 if (argc != 2) {
18 fprintf(stderr, "Usage: %s <file>\n", argv[0]);
19 exit(EXIT_FAILURE);
20 }
21
22 fd = open(argv[1], O_WRONLY | O_CREAT | O_TRUNC, 0644);
23 if (fd == -1) {
24 perror("open");
25 exit(EXIT_FAILURE);
26 }
27
28 fprintf(stderr, "Logging to %s: ", argv[1]);
29
30 slen = 0;
31
32 do {
33 len = splice(STDIN_FILENO, NULL, fd, NULL,
34 BUFLEN, SPLICE_F_MOVE);
35
36 if (len < 0) {
37 if (errno == EAGAIN)
38 continue;
39 perror("tee");
40 exit(EXIT_FAILURE);
41 } else
42 if (len == 0)
43 break;
44 slen += len;
45 if ((slen/BUFLEN) > 0) {
46 fprintf(stderr, ".");
47 }
48 slen = slen % BUFLEN;
49
50 } while (1);
51 close(fd);
52 fprintf(stderr, "\n");
53 exit(EXIT_SUCCESS);
54 }