Whamcloud - gitweb
tune2fs: Fix up to be 64-bit block number safe
[tools/e2fsprogs.git] / misc / logsave.c
index aa3235e..43eb6f7 100644 (file)
@@ -49,19 +49,61 @@ static void usage(char *progname)
 #define SEND_CONSOLE   0x02
 #define SEND_BOTH      0x03
 
+/*
+ * Helper function that does the right thing if write returns a
+ * partial write, or an EGAIN/EINTR error.
+ */
+static int write_all(int fd, const char *buf, size_t count)
+{
+       ssize_t ret;
+       int c = 0;
+
+       while (count > 0) {
+               ret = write(fd, buf, count);
+               if (ret < 0) {
+                       if ((errno == EAGAIN) || (errno == EINTR))
+                               continue;
+                       return -1;
+               }
+               count -= ret;
+               buf += ret;
+               c += ret;
+       }
+       return c;
+}
+
 static void send_output(const char *buffer, int c, int flag)
 {
-       char    *n;
-       
+       const char      *cp;
+       char            *n;
+       int             cnt, d, del;
+
        if (c == 0)
                c = strlen(buffer);
-       
-       if (flag & SEND_CONSOLE)
-               write(1, buffer, c);
+
+       if (flag & SEND_CONSOLE) {
+               cnt = c;
+               cp = buffer;
+               while (cnt) {
+                       del = 0;
+                       for (d=0; d < cnt; d++) {
+                               if (skip_mode &&
+                                   (cp[d] == '\001' || cp[d] == '\002')) {
+                                       del = 1;
+                                       break;
+                               }
+                       }
+                       write_all(1, cp, d);
+                       if (del)
+                               d++;
+                       cnt -= d;
+                       cp += d;
+               }
+       }
        if (!(flag & SEND_LOG))
                return;
        if (outfd > 0)
-               write(outfd, buffer, c);
+               write_all(outfd, buffer, c);
        else {
                n = realloc(outbuf, outbufsize + c);
                if (n) {
@@ -147,7 +189,7 @@ static int run_program(char **argv)
                dup2(fds[1],1);         /* fds[1] replaces stdout */
                dup2(fds[1],2);         /* fds[1] replaces stderr */
                close(fds[0]);  /* don't need this here */
-               
+
                execvp(argv[0], argv);
                perror(argv[0]);
                exit(1);
@@ -201,8 +243,8 @@ static int copy_from_stdin(void)
                bad_read = 0;
        }
        return 0;
-}      
-       
+}
+
 
 
 int main(int argc, char **argv)
@@ -213,7 +255,7 @@ int main(int argc, char **argv)
        int     send_flag = SEND_LOG;
        int     do_stdin;
        time_t  t;
-       
+
        while ((c = getopt(argc, argv, "+asv")) != EOF) {
                switch (c) {
                case 'a':
@@ -238,7 +280,7 @@ int main(int argc, char **argv)
 
        outfd = open(outfn, openflags, 0644);
        do_stdin = !strcmp(argv[0], "-");
-       
+
        send_output("Log of ", 0, send_flag);
        if (do_stdin)
                send_output("stdin", 0, send_flag);
@@ -257,7 +299,7 @@ int main(int argc, char **argv)
                rc = copy_from_stdin();
        else
                rc = run_program(argv);
-       
+
        send_output("\n", 0, send_flag);
        t = time(0);
        send_output(ctime(&t), 0, send_flag);
@@ -279,11 +321,11 @@ int main(int argc, char **argv)
                while (outfd < 0) {
                        outfd = open(outfn, openflags, 0644);
                        sleep(1);
-               } 
-               write(outfd, outbuf, outbufsize);
+               }
+               write_all(outfd, outbuf, outbufsize);
                free(outbuf);
        }
        close(outfd);
-       
+
        exit(rc);
 }