Whamcloud - gitweb
tune2fs: fix casefold+encrypt error message
[tools/e2fsprogs.git] / misc / logsave.c
index 81ac9f6..96b6d8e 100644 (file)
@@ -12,6 +12,7 @@
 
 #define _XOPEN_SOURCE 600 /* for inclusion of sa_handler in Solaris */
 
+#include "config.h"
 #include <stdio.h>
 #include <stdlib.h>
 #include <unistd.h>
@@ -31,17 +32,17 @@ extern char *optarg;
 extern int optind;
 #endif
 
-int    outfd = -1;
-int    outbufsize = 0;
-void   *outbuf = 0;
-int    verbose = 0;
-int    do_skip = 0;
-int    skip_mode = 0;
-pid_t  child_pid = -1;
+static int     outfd = -1;
+static int     outbufsize = 0;
+static void    *outbuf = 0;
+static int     verbose = 0;
+static int     do_skip = 0;
+static int     skip_mode = 0;
+static pid_t   child_pid = -1;
 
 static void usage(char *progname)
 {
-       printf("Usage: %s [-v] [-d dir] logfile program\n", progname);
+       printf("Usage: %s [-asv] logfile program\n", progname);
        exit(1);
 }
 
@@ -51,7 +52,7 @@ static void usage(char *progname)
 
 /*
  * Helper function that does the right thing if write returns a
- * partial write, or an EGAIN/EINTR error.
+ * partial write, or an EAGAIN/EINTR error.
  */
 static int write_all(int fd, const char *buf, size_t count)
 {
@@ -74,13 +75,32 @@ static int write_all(int fd, const char *buf, size_t count)
 
 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_all(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)
@@ -170,6 +190,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 */
+               close(fds[1]);
 
                execvp(argv[0], argv);
                perror(argv[0]);
@@ -189,7 +210,7 @@ static int run_program(char **argv)
                rc = WEXITSTATUS(status);
                if (rc) {
                        send_output(argv[0], 0, SEND_BOTH);
-                       sprintf(buffer, " died with exit status %d\n", rc);
+                       sprintf(buffer, " exited with status code %d\n", rc);
                        send_output(buffer, 0, SEND_BOTH);
                }
        } else {
@@ -198,7 +219,7 @@ static int run_program(char **argv)
                        sprintf(buffer, "died with signal %d\n",
                                WTERMSIG(status));
                        send_output(buffer, 0, SEND_BOTH);
-                       rc = 1;
+                       return 1;
                }
                rc = 0;
        }
@@ -306,7 +327,8 @@ int main(int argc, char **argv)
                write_all(outfd, outbuf, outbufsize);
                free(outbuf);
        }
-       close(outfd);
+       if (outfd >= 0)
+               close(outfd);
 
        exit(rc);
 }