Bump to version 1.22.1
[platform/upstream/busybox.git] / util-linux / script.c
index d16a291..8fb991d 100644 (file)
@@ -6,10 +6,22 @@
  *
  * Based on code from util-linux v 2.12r
  * Copyright (c) 1980
- *     The Regents of the University of California.  All rights reserved.
+ * The Regents of the University of California.  All rights reserved.
  *
- * Licensed under GPLv2 or later, see file License in this tarball for details.
+ * Licensed under GPLv2 or later, see file LICENSE in this source tree.
  */
+
+//usage:#define script_trivial_usage
+//usage:       "[-afq" IF_SCRIPTREPLAY("t") "] [-c PROG] [OUTFILE]"
+//usage:#define script_full_usage "\n\n"
+//usage:       "       -a      Append output"
+//usage:     "\n       -c PROG Run PROG, not shell"
+//usage:     "\n       -f      Flush output after each write"
+//usage:     "\n       -q      Quiet"
+//usage:       IF_SCRIPTREPLAY(
+//usage:     "\n       -t      Send timing to stderr"
+//usage:       )
+
 #include "libbb.h"
 
 int script_main(int argc, char **argv) MAIN_EXTERNALLY_VISIBLE;
@@ -33,24 +45,21 @@ int script_main(int argc UNUSED_PARAM, char **argv)
                OPT_c = (1 << 1),
                OPT_f = (1 << 2),
                OPT_q = (1 << 3),
-#if ENABLE_SCRIPTREPLAY
                OPT_t = (1 << 4),
-#endif
        };
 
-#if ENABLE_GETOPT_LONG
+#if ENABLE_LONG_OPTS
        static const char getopt_longopts[] ALIGN1 =
                "append\0"  No_argument       "a"
                "command\0" Required_argument "c"
                "flush\0"   No_argument       "f"
                "quiet\0"   No_argument       "q"
-# if ENABLE_SCRIPTREPLAY
-               "timing\0"  No_argument       "t"
-# endif
+               IF_SCRIPTREPLAY("timing\0" No_argument "t")
                ;
 
        applet_long_options = getopt_longopts;
 #endif
+
        opt_complementary = "?1"; /* max one arg */
        opt = getopt32(argv, "ac:fq" IF_SCRIPTREPLAY("t") , &shell_arg);
        //argc -= optind;
@@ -68,10 +77,7 @@ int script_main(int argc UNUSED_PARAM, char **argv)
        if (!(opt & OPT_q)) {
                printf("Script started, file is %s\n", fname);
        }
-       shell = getenv("SHELL");
-       if (shell == NULL) {
-               shell = DEFAULT_SHELL;
-       }
+       shell = get_shell_name();
 
        pty = xgetpty(pty_line);
 
@@ -91,19 +97,14 @@ int script_main(int argc UNUSED_PARAM, char **argv)
 
        /* TODO: SIGWINCH? pass window size changes down to slave? */
 
-       child_pid = vfork();
-       if (child_pid < 0) {
-               bb_perror_msg_and_die("vfork");
-       }
+       child_pid = xvfork();
 
        if (child_pid) {
                /* parent */
 #define buf bb_common_bufsiz1
                struct pollfd pfd[2];
                int outfd, count, loop;
-#if ENABLE_SCRIPTREPLAY
-               double oldtime = time(NULL);
-#endif
+               double oldtime = ENABLE_SCRIPTREPLAY ? time(NULL) : 0;
                smallint fd_count = 2;
 
                outfd = xopen(fname, mode);
@@ -132,8 +133,7 @@ int script_main(int argc UNUSED_PARAM, char **argv)
                                        goto restore;
                                }
                                if (count > 0) {
-#if ENABLE_SCRIPTREPLAY
-                                       if (opt & OPT_t) {
+                                       if (ENABLE_SCRIPTREPLAY && (opt & OPT_t)) {
                                                struct timeval tv;
                                                double newtime;
 
@@ -142,7 +142,6 @@ int script_main(int argc UNUSED_PARAM, char **argv)
                                                fprintf(stderr, "%f %u\n", newtime - oldtime, count);
                                                oldtime = newtime;
                                        }
-#endif
                                        full_write(STDOUT_FILENO, buf, count);
                                        full_write(outfd, buf, count);
                                        if (opt & OPT_f) {