Ticket #66: frebsd-subshell.patch

File frebsd-subshell.patch, 4.5 KB (added by slavazanko, 16 years ago)
Line 
1--- subshell.c  2006/11/27 21:33:46     1.93
2+++ subshell.c  2007/03/01 11:07:04     1.94
3@@ -91,8 +91,9 @@
4 TRUE;
5 #endif
6 
7-/* File descriptor of the pseudoterminal used by the subshell */
8+/* File descriptors of the pseudoterminal used by the subshell */
9 int subshell_pty = 0;
10+static int subshell_pty_slave = -1;
11 
12 /* The key for switching back to MC from the subshell */
13 static const char subshell_switch_key = XCTRL('o') & 255;
14@@ -184,7 +185,6 @@
15 static void
16 init_subshell_child (const char *pty_name)
17 {
18-    int pty_slave;
19     const char *init_file = NULL;
20 #ifdef HAVE_GETSID
21     pid_t mc_sid;
22@@ -192,26 +192,12 @@
23 
24     setsid ();                 /* Get a fresh terminal session */
25 
26-    /* Open the slave side of the pty: again */
27-    pty_slave = pty_open_slave (pty_name);
28-
29-    /* This must be done before closing the master side of the pty, */
30-    /* or it will fail on certain idiotic systems, such as Solaris.     */
31-
32-    /* Close master side of pty.  This is important; apart from */
33-    /* freeing up the descriptor for use in the subshell, it also       */
34-    /* means that when MC exits, the subshell will get a SIGHUP and     */
35-    /* exit too, because there will be no more descriptors pointing     */
36-    /* at the master side of the pty and so it will disappear.  */
37-
38-    close (subshell_pty);
39-
40     /* Make sure that it has become our controlling terminal */
41 
42     /* Redundant on Linux and probably most systems, but just in case: */
43 
44 #ifdef TIOCSCTTY
45-    ioctl (pty_slave, TIOCSCTTY, 0);
46+    ioctl (subshell_pty_slave, TIOCSCTTY, 0);
47 #endif
48 
49     /* Configure its terminal modes and window size */
50@@ -220,7 +206,7 @@
51     /* TOSTOP, which keeps background processes from writing to the pty */
52 
53     shell_mode.c_lflag |= TOSTOP;      /* So background writers get SIGTTOU */
54-    if (tcsetattr (pty_slave, TCSANOW, &shell_mode)) {
55+    if (tcsetattr (subshell_pty_slave, TCSANOW, &shell_mode)) {
56    fprintf (stderr, "Cannot set pty terminal modes: %s\r\n",
57         unix_error_string (errno));
58    _exit (FORK_FAILURE);
59@@ -228,7 +214,7 @@
60 
61     /* Set the pty's size (80x25 by default on Linux) according to the */
62     /* size of the real terminal as calculated by ncurses, if possible */
63-    resize_tty (pty_slave);
64+    resize_tty (subshell_pty_slave);
65 
66     /* Set up the subshell's environment and init file name */
67 
68@@ -279,14 +265,20 @@
69     /* be connected to the real tty during the above error messages; */
70     /* otherwise the user will never see them.                   */
71 
72-    dup2 (pty_slave, STDIN_FILENO);
73-    dup2 (pty_slave, STDOUT_FILENO);
74-    dup2 (pty_slave, STDERR_FILENO);
75-
76-    /* Execute the subshell at last */
77+    dup2 (subshell_pty_slave, STDIN_FILENO);
78+    dup2 (subshell_pty_slave, STDOUT_FILENO);
79+    dup2 (subshell_pty_slave, STDERR_FILENO);
80 
81     close (subshell_pipe[READ]);
82-    close (pty_slave);         /* These may be FD_CLOEXEC, but just in case... */
83+    close (subshell_pty_slave);                /* These may be FD_CLOEXEC, but just in case... */
84+    /* Close master side of pty.  This is important; apart from */
85+    /* freeing up the descriptor for use in the subshell, it also       */
86+    /* means that when MC exits, the subshell will get a SIGHUP and     */
87+    /* exit too, because there will be no more descriptors pointing     */
88+    /* at the master side of the pty and so it will disappear.  */
89+    close (subshell_pty);
90+
91+    /* Execute the subshell at last */
92 
93     switch (subshell_type) {
94     case BASH:
95@@ -370,7 +362,6 @@
96     /* This must be remembered across calls to init_subshell() */
97     static char pty_name[BUF_SMALL];
98     char precmd[BUF_SMALL];
99-    int pty_slave = -1;
100 
101 #ifdef HAVE_GETSID
102     switch (check_sid ()) {
103@@ -413,8 +404,8 @@
104        use_subshell = FALSE;
105        return;
106    }
107-       pty_slave = pty_open_slave (pty_name);
108-       if (pty_slave == -1) {
109+       subshell_pty_slave = pty_open_slave (pty_name);
110+       if (subshell_pty_slave == -1) {
111        fprintf (stderr, "Cannot open slave side of pty %s: %s\r\n",
112             pty_name, unix_error_string (errno));
113        use_subshell = FALSE;
114@@ -473,11 +464,6 @@
115    init_subshell_child (pty_name);
116     }
117 
118-    /* pty_slave is only opened when called the first time */
119-    if (pty_slave != -1) {
120-       close (pty_slave);
121-    }
122-
123     /* Set up `precmd' or equivalent for reading the subshell's CWD */
124 
125     switch (subshell_type) {
126@@ -1063,7 +1049,7 @@
127 
128     if (subshell_state != ACTIVE) {
129    /* Discard all remaining data from stdin to the subshell */
130-       tcflush (subshell_pty, TCOFLUSH);
131+       tcflush (subshell_pty_slave, TCIFLUSH);
132     }
133 
134     subshell_stopped = FALSE;