Patch-ID: bash32-032
[bash.git] / examples / loadables / push.c
blob497ecd0e7e0da8a9641630e7241e62394f1c7d6d
1 /*
2 * push - anyone remember TOPS-20?
4 */
6 #include <config.h>
7 #include <stdio.h>
8 #include <errno.h>
10 #include "builtins.h"
11 #include "shell.h"
12 #include "jobs.h"
13 #include "bashgetopt.h"
15 #ifndef errno
16 extern int errno;
17 #endif
19 extern int dollar_dollar_pid;
20 extern int last_command_exit_value;
22 int
23 push_builtin (list)
24 WORD_LIST *list;
26 pid_t pid;
27 int xstatus, opt;
29 xstatus = EXECUTION_SUCCESS;
30 reset_internal_getopt ();
31 while ((opt = internal_getopt (list, "")) != -1)
33 switch (opt)
35 default:
36 builtin_usage ();
37 return (EX_USAGE);
40 list = loptend;
42 pid = make_child (savestring ("push"), 0);
43 if (pid == -1)
45 builtin_error ("cannot fork: %s", strerror (errno));
46 return (EXECUTION_FAILURE);
48 else if (pid == 0)
50 /* Shell variable adjustments: $SHLVL, $$, $PPID, $! */
51 adjust_shell_level (1);
52 dollar_dollar_pid = getpid ();
53 set_ppid ();
55 /* Clean up job control stuff. */
56 stop_making_children ();
57 cleanup_the_pipeline ();
58 delete_all_jobs (0);
60 last_asynchronous_pid = NO_PID;
62 /* Make sure the job control code has the right values for
63 the shell's process group and tty process group, and that
64 the signals are set correctly for job control. */
65 initialize_job_control (0);
66 initialize_job_signals ();
68 /* And read commands until exit. */
69 reader_loop ();
70 exit_shell (last_command_exit_value);
72 else
74 stop_pipeline (0, (COMMAND *)NULL);
75 xstatus = wait_for (pid);
76 return (xstatus);
80 char *push_doc[] = {
81 "Create a child that is an exact duplicate of the running shell",
82 "and wait for it to exit. The $SHLVL, $!, $$, and $PPID variables",
83 "are adjusted in the child. The return value is the exit status",
84 "of the child.",
85 (char *)NULL
88 struct builtin push_struct = {
89 "push",
90 push_builtin,
91 BUILTIN_ENABLED,
92 push_doc,
93 "push",