Patch-ID: bash41-003
[bash.git] / examples / loadables / push.c
blobb076073389a4b5180936ee906d034f6df2ab1258
1 /*
2 * push - anyone remember TOPS-20?
4 */
6 /*
7 Copyright (C) 1999-2009 Free Software Foundation, Inc.
9 This file is part of GNU Bash.
10 Bash is free software: you can redistribute it and/or modify
11 it under the terms of the GNU General Public License as published by
12 the Free Software Foundation, either version 3 of the License, or
13 (at your option) any later version.
15 Bash is distributed in the hope that it will be useful,
16 but WITHOUT ANY WARRANTY; without even the implied warranty of
17 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
18 GNU General Public License for more details.
20 You should have received a copy of the GNU General Public License
21 along with Bash. If not, see <http://www.gnu.org/licenses/>.
24 #include <config.h>
25 #include <stdio.h>
26 #include <errno.h>
28 #include "builtins.h"
29 #include "shell.h"
30 #include "jobs.h"
31 #include "bashgetopt.h"
32 #include "common.h"
34 #ifndef errno
35 extern int errno;
36 #endif
38 extern int dollar_dollar_pid;
39 extern int last_command_exit_value;
41 int
42 push_builtin (list)
43 WORD_LIST *list;
45 pid_t pid;
46 int xstatus, opt;
48 xstatus = EXECUTION_SUCCESS;
49 reset_internal_getopt ();
50 while ((opt = internal_getopt (list, "")) != -1)
52 switch (opt)
54 default:
55 builtin_usage ();
56 return (EX_USAGE);
59 list = loptend;
61 pid = make_child (savestring ("push"), 0);
62 if (pid == -1)
64 builtin_error ("cannot fork: %s", strerror (errno));
65 return (EXECUTION_FAILURE);
67 else if (pid == 0)
69 /* Shell variable adjustments: $SHLVL, $$, $PPID, $! */
70 adjust_shell_level (1);
71 dollar_dollar_pid = getpid ();
72 set_ppid ();
74 /* Clean up job control stuff. */
75 stop_making_children ();
76 cleanup_the_pipeline ();
77 delete_all_jobs (0);
79 last_asynchronous_pid = NO_PID;
81 /* Make sure the job control code has the right values for
82 the shell's process group and tty process group, and that
83 the signals are set correctly for job control. */
84 initialize_job_control (0);
85 initialize_job_signals ();
87 /* And read commands until exit. */
88 reader_loop ();
89 exit_shell (last_command_exit_value);
91 else
93 stop_pipeline (0, (COMMAND *)NULL);
94 xstatus = wait_for (pid);
95 return (xstatus);
99 char *push_doc[] = {
100 "Create child shell.",
102 "Create a child that is an exact duplicate of the running shell",
103 "and wait for it to exit. The $SHLVL, $!, $$, and $PPID variables",
104 "are adjusted in the child. The return value is the exit status",
105 "of the child.",
106 (char *)NULL
109 struct builtin push_struct = {
110 "push",
111 push_builtin,
112 BUILTIN_ENABLED,
113 push_doc,
114 "push",