| 1 | (* Sys.exec: this program replaced by sh, with an fd it keeps, at its |
| 2 | own number, and one it doesn't; sh's output and status are its. *) |
| 3 | module Exec; |
| 4 | import Sys; |
| 5 | |
| 6 | proc main*: INT; |
| 7 | begin |
| 8 | if (Sys.dupTo(Sys.stdout, 7) # 7) or (Sys.dupTo(Sys.stdout, 8) # 8) then return 1; end; |
| 9 | if (Sys.keepOnExec(7, false) # 0) or (Sys.keepOnExec(8, false) # 0) then return 1; end; |
| 10 | return Sys.exec({path: "sh", env: ["X=replaced"], more: [7], |
| 11 | args: ["sh", "-c", "echo $X >&7; { echo kept >&8; } 2>/dev/null || echo closed; exit 5"]}); |
| 12 | end; |
| 13 | |
| 14 | end Exec. |