Upload Tizen:Base source
[external/bash.git] / examples / loadables / push.c
1 /*
2  * push - anyone remember TOPS-20?
3  *
4  */
5
6 /*
7    Copyright (C) 1999-2009 Free Software Foundation, Inc.
8
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.
14
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.
19
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/>.
22 */
23
24 #include <config.h>
25 #include <stdio.h>
26 #include <errno.h>
27
28 #include "builtins.h"
29 #include "shell.h"
30 #include "jobs.h"
31 #include "bashgetopt.h"
32 #include "common.h"
33
34 #ifndef errno
35 extern int errno;
36 #endif
37
38 extern int dollar_dollar_pid;
39 extern int last_command_exit_value;
40
41 int
42 push_builtin (list)
43      WORD_LIST *list;
44 {
45   pid_t pid;
46   int xstatus, opt;
47
48   xstatus = EXECUTION_SUCCESS;
49   reset_internal_getopt ();
50   while ((opt = internal_getopt (list, "")) != -1)
51     {
52       switch (opt)
53         {
54         default:
55           builtin_usage ();
56           return (EX_USAGE);
57         }
58     }
59   list = loptend;  
60
61   pid = make_child (savestring ("push"), 0);
62   if (pid == -1)
63     {
64       builtin_error ("cannot fork: %s", strerror (errno));
65       return (EXECUTION_FAILURE);
66     }
67   else if (pid == 0)
68     {
69       /* Shell variable adjustments: $SHLVL, $$, $PPID, $! */
70       adjust_shell_level (1);
71       dollar_dollar_pid = getpid ();
72       set_ppid ();
73
74       /* Clean up job control stuff. */
75       stop_making_children ();
76       cleanup_the_pipeline ();
77       delete_all_jobs (0);
78
79       last_asynchronous_pid = NO_PID;
80
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 ();
86
87       /* And read commands until exit. */
88       reader_loop ();
89       exit_shell (last_command_exit_value);
90     }
91   else
92     {
93       stop_pipeline (0, (COMMAND *)NULL);
94       xstatus = wait_for (pid);
95       return (xstatus);
96     }   
97 }
98
99 char *push_doc[] = {
100         "Create child shell.",
101         "",
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
107 };
108
109 struct builtin push_struct = {
110         "push",
111         push_builtin,
112         BUILTIN_ENABLED,
113         push_doc,
114         "push",
115         0
116 };