This is an appendix to Understanding Unix/Linux Programming written for people interested in Ada programming on POSIX systems.

I/O Redirection and Pipes

Section 10.3.4, page 325-326

[...] listargs.adb, shows that the program does not even see the redirection notation on the command line.

% gnatmake listargs
[...]
% ./listargs testing one two
Number of arguments: 3
The arguments are:
 name) './listargs'
    1) 'testing'
    2) 'one'
    3) 'two'
This message is sent to standard error.
% ./listargs testing one two > xyz
This message is sent to standard error.
% cat xyz
Number of arguments: 3
The arguments are:
 name) './listargs'
    1) 'testing'
    2) 'one'
    3) 'two'
% ./listargs testing >xyz one two 2> oops
% cat xyz
Number of arguments: 3
The arguments are:
 name) './listargs'
    1) 'testing'
    2) 'one'
    3) 'two'
% cat oops
This message is sent to standard error.
%

Section 10.4.1, page 328-330

The program stdinredir1.adb uses the Close-then-Open method.

% make stdinredir1
[...]
% ./stdinredir1
line1
line1
testing line2
testing line2
line 3 here
line 3 here
root:x:0:0:root:/root:/bin/bash
sashroot:x:0:0:sashroot:/root:/bin/bash
daemon:x:1:1:daemon:/usr/sbin:/bin/sh
%

Section 10.4.2, page 330-332

The program stdinredir2.adb uses the Open-Close-Duplicate-Close method.

Section 10.4.4, page 332-333

The program stdinredir3.adb uses the Open-Duplicate_and_Close-Close method.

Section 10.5, page 335-336

The program whotofile.adb demonstrates the method.

Section 10.6.1, page 338-339

The program pipedemo.adb creates a pipe, and then uses it to send data to itself.

Section 10.6.2, page 340-341

The program pipedemo2.adb shows how to combine Create_Pipe and Fork to create a pair of processes that communicate through a pipe.

Section 10.6.3, page 342-343

The program pipe.adb combinex Create_Pipe, Fork and Exec to run two programs with a pipe from standard output of the first program to standard input of the second program.

Explorations, page 345-346

10.2

In pipe.adb, [...] By changing the test elsif Process_ID > 0 then to elsif Process_ID = 0 then, the roles will be reversed. [...]

10.4

In pipe.adb, [...]

10.8

In pipe.adb, [...]

10.9

How is POSIX.IO.Duplicate like POSIX.Files.Link?

Programming exercises, pages 346-347

10.11

Modify whotofile.adb, [...]

10.12

Write a program called sortfromfile.adb, [...]

10.13

Extend pipe.adb, [...]

10.16

POSIX.IO.Is_A_Terminal [...]. The library function POSIX.IO.Is_A_Terminal uses the system call POSIX.File_Status.Get_File_Status. [...]

-- 

Go to next chapter.

Collected examples:
posix-in-ada.zip
Based on:
Understanding Unix/Linux Programming, Bruce Molay, ISBN 0-13-008396-8.
Main page:
http://edb.jacob-sparre.dk/Posix_in_Ada/
Written by:
Jacob Sparre Andersen.
Latest update:
27th of September, 2007