Hi Jason,

Thank you for your quick feedback!  I've incorporated yours and
off-list feedback from ian@ into a new diff included below.  The diff
now also includes the removal of /* ARGSUSED */ lint comments.

On Wed, Dec 13, 2017 at 09:34:03PM +0000, Jason McIntyre wrote:
| > +To run a
| 
| i'd remove "a", but it's preference only - your choice.
| 
| > +.Ar command
| > +with its own arguments, enclose it in quotes.
| 
| and s/its own//

Rewrote this sentence a bit, it now reads:

-c command
        Run command instead of an interactive shell.  To run a command
        with arguments, enclose both in quotes.

Still not 100% happy with it, but the best I could come up with.
Better suggestions wanted :)

Paul

Index: script.1
===================================================================
RCS file: /cvs/src/usr.bin/script/script.1,v
retrieving revision 1.14
diff -u -p -r1.14 script.1
--- script.1    15 Jan 2012 20:06:40 -0000      1.14
+++ script.1    14 Dec 2017 07:36:11 -0000
@@ -39,6 +39,7 @@
 .Sh SYNOPSIS
 .Nm script
 .Op Fl a
+.Op Fl c Ar command
 .Op Ar file
 .Sh DESCRIPTION
 .Nm
@@ -65,9 +66,14 @@ Append the output to
 or
 .Pa typescript ,
 retaining the prior contents.
+.It Fl c Ar command
+Run
+.Ar command
+instead of an interactive shell.
+To run a command with arguments, enclose both in quotes.
 .El
 .Pp
-The script ends when the forked shell exits (a control-D
+The script ends when the forked program exits (a control-D
 .Pq Ql ^D
 to exit
 the Bourne shell
Index: script.c
===================================================================
RCS file: /cvs/src/usr.bin/script/script.c,v
retrieving revision 1.33
diff -u -p -r1.33 script.c
--- script.c    12 Apr 2017 14:49:05 -0000      1.33
+++ script.c    14 Dec 2017 07:34:10 -0000
@@ -89,7 +89,7 @@ int           istty;
 
 __dead void done(int);
 void dooutput(void);
-void doshell(void);
+void doshell(char *);
 void fail(void);
 void finish(int);
 void scriptflush(int);
@@ -102,17 +102,23 @@ main(int argc, char *argv[])
        struct sigaction sa;
        struct winsize win;
        char ibuf[BUFSIZ];
+       char *cmd;
        ssize_t cc, off;
        int aflg, ch;
 
+       cmd = NULL;
        aflg = 0;
-       while ((ch = getopt(argc, argv, "a")) != -1)
+       while ((ch = getopt(argc, argv, "ac:")) != -1)
                switch(ch) {
                case 'a':
                        aflg = 1;
                        break;
+               case 'c':
+                       cmd = optarg;
+                       break;
                default:
-                       fprintf(stderr, "usage: %s [-a] [file]\n", __progname);
+                       fprintf(stderr, "usage: %s [-a] [-c command] [file]\n",
+                           __progname);
                        exit(1);
                }
        argc -= optind;
@@ -163,7 +169,7 @@ main(int argc, char *argv[])
                if (child)
                        dooutput();
                else
-                       doshell();
+                       doshell(cmd);
        }
 
        bzero(&sa, sizeof sa);
@@ -196,7 +202,6 @@ main(int argc, char *argv[])
        done(sigdeadstatus);
 }
 
-/* ARGSUSED */
 void
 finish(int signo)
 {
@@ -215,7 +220,6 @@ finish(int signo)
        errno = save_errno;
 }
 
-/* ARGSUSED */
 void
 handlesigwinch(int signo)
 {
@@ -294,7 +298,6 @@ dooutput(void)
        done(0);
 }
 
-/* ARGSUSED */
 void
 scriptflush(int signo)
 {
@@ -302,9 +305,10 @@ scriptflush(int signo)
 }
 
 void
-doshell(void)
+doshell(char *cmd)
 {
        char *shell;
+       char *argp[] = {"sh", "-c", NULL, NULL};
 
        shell = getenv("SHELL");
        if (shell == NULL)
@@ -313,8 +317,15 @@ doshell(void)
        (void)close(master);
        (void)fclose(fscript);
        login_tty(slave);
-       execl(shell, shell, "-i", (char *)NULL);
-       warn("%s", shell);
+
+       if (cmd != NULL) {
+               argp[2] = cmd;
+               execv(_PATH_BSHELL, argp);
+               warn("unable to execute %s", _PATH_BSHELL);
+       } else {
+               execl(shell, shell, "-i", (char *)NULL);
+               warn("%s", shell);
+       }
        fail();
 }

 
-- 
>++++++++[<++++++++++>-]<+++++++.>+++[<------>-]<.>+++[<+
+++++++++++>-]<.>++[<------------>-]<+.--------------.[-]
                 http://www.weirdnet.nl/                 

Reply via email to