Execute a system command: Difference between revisions

PDP-11
(Update the Io example with one that works.)
(PDP-11)
Line 834:
my $cmd = 'ls';
my @ls = qqx/$ls/; # same thing with interpolation</lang>
 
=={{header|PDP-11}}==
PDP-11 running Unix
<lang pdp-11>; Execute a file - the equivalent of system() in stdio
;
; On entry, r1=>nul-terminated command string
; On exit, VS=Couldn't fork
; VC=Forked successfully, r0=return value
;
.CLIsystem
trap 2 ; fork()
br CLIchild ; Child process returns here
bcc CLIparent ; Parent process returns here
mov (sp)+,r1
tst (sp)+
sev ; Couldn't fork, set V
rts pc
.CLIparent
mov r0,-(sp) ; Save child's PID
.CLIwait
trap 7 ; wait()
cmp r0,(sp)
beq CLIfinished
cmp r0,#&FFFF
bne CLIwait ; Loop until child finished
.CLIfinished
tst (sp)+ ; Drop child's PID
mov r1,r0 ; R0=return value
mov (sp)+,r1 ; Restore R1
tst (sp)+ ; Drop original R0
swab r0 ; Move return value to bottom byte
rts pc
 
; CLI child process
; -----------------
.CLIchild
clr -(sp) ; end of string array
mov r1,-(sp) ; => command string
mov #UXsh3,-(sp) ; => "-c"
mov #UXsh2,-(sp) ; => "sh"
mov #&890B,TRAP_BUF ; exec
mov #UXsh1,TRAP_BUF+2 ; => "/bin/sh"
mov sp,TRAP_BUF+4 ; => pointers to command strings
;mov SV_ENVPTR,TRAP_BUF+6 ; => "PATH=etc"
trap 0 ; indir()
EQUW TRAP_BUF ; exec(shell, parameters)
add #8,sp ; If we get back, we didn't fork, we spawned
mov (sp)+,r1 ; So, restore registers
clr (sp)+ ; and return exit value in R0
rts pc
 
.UXsh1 EQUS "/bin/sh",0
.UXsh2 EQUS "sh",0
.UXsh3 EQUS "-c",0
ALIGN
 
.TRAP_BUF
EQUW 0
EQUW 0
EQUW 0
EQUW 0</lang>
So, call with, for example:
<lang pdp-11>mov #cmd_ls,r1 ; => "-c"
jsr pc,CLIsystem
...
.cmd_ls EQUS "ls",0</lang>
 
=={{header|PHP}}==
Anonymous user