Browse Source

Shell and its pipe now fully working.

themultiplexer 7 years ago
parent
commit
04ea38eb1f
1 changed files with 28 additions and 33 deletions
  1. 28
    33
      hw7/task2/src/lib.rs

+ 28
- 33
hw7/task2/src/lib.rs View File

1
 extern crate nix;
1
 extern crate nix;
2
 
2
 
3
 use std::io::{BufRead, Write};
3
 use std::io::{BufRead, Write};
4
-use std::str::FromStr;
4
+use std::str::{FromStr, Split};
5
 use std::env;
5
 use std::env;
6
-use std::str::Split;
7
 use std::os::unix::io::RawFd;
6
 use std::os::unix::io::RawFd;
8
 use std::ffi::{OsString, CString};
7
 use std::ffi::{OsString, CString};
9
 use nix::unistd::ForkResult::{Child, Parent};
8
 use nix::unistd::ForkResult::{Child, Parent};
10
-use nix::sys::wait::{wait, WaitStatus};
11
-use nix::unistd::dup2;
12
-use nix::unistd::close;
13
-use nix::unistd::{execvp, fork, pipe, read, write};
9
+use nix::sys::wait::{wait, waitpid, WaitStatus};
10
+use nix::unistd::{dup2, close, execvp, fork, pipe, read, write};
14
 
11
 
15
 mod shellerror;
12
 mod shellerror;
16
 
13
 
144
                 
141
                 
145
                 let needs_pipe = commands_vec.len() == 2;
142
                 let needs_pipe = commands_vec.len() == 2;
146
 
143
 
147
-                if let Ok((reader, writer)) = pipe() {
144
+
148
 
145
 
149
                     let fi = fork();
146
                     let fi = fork();
150
                     match fi {
147
                     match fi {
151
-                        Ok(Parent { .. }) => {
148
+                        Ok(Parent { child }) => {
152
                             wait();
149
                             wait();
153
-                        }
154
-                        Ok(Child) => {
155
-                            if let Some(first) = commands_vec.get(0) {
156
-                                if needs_pipe {
157
-                                    close(reader).unwrap();
158
-                                    dup2(writer, 1).unwrap();
159
-                                }
160
-                                self.execute(first);
161
-                            }
162
-                        }
163
-                        Err(_) => println!("Fatal error: Fork failed"),
164
-                    }
165
-
166
 
150
 
167
-                    let sec = fork();
168
-                    match sec {
169
-                        Ok(Parent { .. }) => {
170
-                            wait();
171
                         }
151
                         }
172
                         Ok(Child) => {
152
                         Ok(Child) => {
173
-                            if let Some(second) = commands_vec.get(1) {
174
-                                close(1).unwrap();
175
-                                close(writer).unwrap();
176
-                                dup2(reader, 0).unwrap();
177
-                                self.execute(second);
153
+                            if let Ok((reader, writer)) = pipe() {
154
+                                let sec = fork();
155
+                                match sec {
156
+                                    Ok(Parent { child }) => {
157
+                                        wait();
158
+                                        if let Some(second) = commands_vec.get(1) {
159
+                                            close(writer).unwrap();
160
+                                            dup2(reader, 0).unwrap();
161
+                                            self.execute(second);
162
+                                        }
163
+                                    }
164
+                                    Ok(Child) => {
165
+                                        if let Some(first) = commands_vec.get(0) {
166
+                                            if needs_pipe {
167
+                                                close(reader).unwrap();
168
+                                                dup2(writer, 1).unwrap();
169
+                                            }
170
+                                            self.execute(first);
171
+                                        }
172
+                                    }
173
+                                    Err(_) => println!("Fatal error: Fork failed"),
174
+                                }
178
                             }
175
                             }
179
                         }
176
                         }
180
                         Err(_) => println!("Fatal error: Fork failed"),
177
                         Err(_) => println!("Fatal error: Fork failed"),
181
                     }
178
                     }
182
 
179
 
183
-                }
184
-
185
             },
180
             },
186
             Command::Exit => self.should_exit = true,
181
             Command::Exit => self.should_exit = true,
187
             Command::Cd(_) => {
182
             Command::Cd(_) => {
196
         let args:Vec<CString> = parts.iter().map(|f| CString::new(*f).unwrap()).collect();
191
         let args:Vec<CString> = parts.iter().map(|f| CString::new(*f).unwrap()).collect();
197
         let t = args.into_boxed_slice();
192
         let t = args.into_boxed_slice();
198
 
193
 
199
-        execvp(&t[0], &t);
194
+        execvp(&t[0], &t).unwrap();
200
     }
195
     }
201
 }
196
 }
202
 
197
 

Loading…
Cancel
Save