1    let fileno (stream : CStream) : int
2    let kd_pipe (fds : MutPtr<int>) : int
3    let fdopen (fd : int) (mode : CString) : CStream
4    let popen (command : CString) (modes : CString) : CStream
5    
6    module process
7    
8    let set_nonblocking (stream : CStream) =
9        let fd = fileno stream
10       sys/set_nonblocking fd
11   
12   def run_c (path : String) =
13       let mut pfd : [int; 2] = memory@zero
14   
15       if kd_pipe pfd.as_mut_ptr < 0 then
16           fail
17   
18       let stderr = fdopen pfd[0] "r"
19   
20       let mut command_buffer : [CChar; 512] = memory@zero
21       let mut i : u32 = 0
22       while path[i] <> '\0' do
23           command_buffer[i] = path[i] as CChar
24           i += 1
25   
26       os@ not windows
27   
28       let s = " 2>&${pfd[1]}"
29       let mut index = 0
30       while index < s.length do
31           command_buffer[i] = s[index] as CChar
32           i += 1
33           index += 1
34   
35       endos@
36   
37       command_buffer[i] = '\0' as CChar
38       let command = CString command_buffer.as_ptr
39       let stdout = popen command "r"
40       close pfd[1]
41   
42       set_nonblocking stdout
43       set_nonblocking stderr
44   
45       CProcess =stdout =stderr
46                fd = pfd[0]
47   
48   os@ windows
49   
50   def run_w (path : String) =
51       let process = WProcess path
52       process
53   
54   def run (path : String) = run_w path
55   
56   os@ other
57   
58   def run (path : String) = run_c path
59   
60   endos@
61