# %popen() function

The %popen() function creates a pipe
between the calling process and the command to be executed.

## Syntax

```
pointer = (char*)%popen(command, type)
```

## Parameter(s)

| pointer | Stream pointer that directs a write to the standard input of the command if the I/O mode is w, and reads from the standard output of the command if the type is r. |
| --- | --- |
| command | Shell command line. |
| type | I/O mode. |
| r | Reading |
| w | Writing |

## Description

A stream opened by %popen() must be closed by %pclose(). The stream is closed automatically at FlashBASIC program termination.

## Example(s)

```
include dm,bp,includes sysid.inc
cfunction unix.builtin
char buffer[128]
if system(38)<sys$imp> = sys$win then
 cmd = "dir"
end else
 cmd = "ls -l"
end
ptr=(char*)%popen(cmd,"r")
if ptr=0 then crt "error" ; stop
loop
 n=(char*)%fgets(buffer,128,(char*)ptr)
while n # 0 do
 print field(buffer,char(0),1)
repeat
%pclose((char*)ptr)
```
This example executes a UNIX command, capturing the result.

## See also

- [%fgets() function](https://d3codex.com/pickbasic-flashbasic/percent-fgets-function/)
- [%fputs() function](https://d3codex.com/pickbasic-flashbasic/percent-fputs-function/)
- [%pclose() function](https://d3codex.com/pickbasic-flashbasic/percent-pclose-function/)
- [FlashBASIC C functions overview](https://d3codex.com/pickbasic-flashbasic/flashbasic-c-functions-overview/)
- [cfunction statement](https://d3codex.com/pickbasic-flashbasic/cfunction-statement/)
- [execute statement (UNIX)](https://d3codex.com/pickbasic-flashbasic/execute-statement-unix/)

---
Source: https://d3codex.com/pickbasic-flashbasic/percent-popen-function/ - part of the D3Codex reference.
