summary refs log tree commit diff
path: root/doc/redirfd.html
blob: 9dbe409f774d20181653233d7628bd6ea8036b32 (plain) (blame)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
<html>
  <head>
    <meta name="viewport" content="width=device-width, initial-scale=1.0" />
    <meta http-equiv="Content-Type" content="text/html; charset=UTF-8" />
    <meta http-equiv="Content-Language" content="en" />
    <title>execline: the redirfd command</title>
    <meta name="Description" content="execline: the redirfd command" />
    <meta name="Keywords" content="execline command redirfd" />
    <!-- <link rel="stylesheet" type="text/css" href="//skarnet.org/default.css" /> -->
  </head>
<body>

<p>
<a href="index.html">execline</a><br />
<a href="//skarnet.org/software/">Software</a><br />
<a href="//skarnet.org/">skarnet.org</a>
</p>

<h1> The <tt>redirfd</tt> program </h1>

<p>
<tt>redirfd</tt> redirects a given file descriptor to a file, then
executes a program.
</p>

<h2> Interface </h2>

<pre>
     redirfd [ -r | -w | -u | -a | -x ] [ -n ] [ -b ] <em>fd</em> <em>file</em> <em>prog...</em>
</pre>

<p>
<tt>redirfd</tt> redirects the file descriptor number <em>fd</em>
to <em>file</em>, then execs into <em>prog...</em>.
</p>

<h2> Options </h2>

<p>
 One and only one of the -r, -w, -u, -a, or -x options must be given;
the -n and -b options may be added in any case.
</p>

<ul>
 <li> <tt>-r</tt>&nbsp;: open <em>file</em> for reading. </li>
 <li> <tt>-w</tt>&nbsp;: open <em>file</em> for writing, truncating it if it already exists. </li>
 <li> <tt>-u</tt>&nbsp;: open <em>file</em> for reading and writing. </li>
 <li> <tt>-a</tt>&nbsp;: open <em>file</em> for appending, creating it if it doesn't exist. </li>
 <li> <tt>-x</tt>&nbsp;: open <em>file</em> for writing, creating it, failing if it already exists. </li>
 <li> <tt>-n</tt>&nbsp;: open <em>file</em> in non-blocking mode. </li>
 <li> <tt>-b</tt>&nbsp;: change mode of <em>file</em> after opening it:
to non-blocking mode if the <tt>-n</tt> option was not given,
to blocking mode if it was. </li>
</ul>

<h2> Notes </h2>

<ul>
 <li> <tt>redirfd -r <em>n</em> <em>file</em> prog...</tt> is roughly equivalent to
<tt>sh -c 'exec prog... <em>n</em>&lt;<em>file</em>'</tt></li>
 <li> <tt>redirfd -w <em>n</em> <em>file</em> prog...</tt> is roughly equivalent to
<tt>sh -c 'exec prog... <em>n</em>&gt;<em>file</em>'</tt></li>
 <li> <tt>redirfd -u <em>n</em> <em>file</em> prog...</tt> is roughly equivalent to
<tt>sh -c 'exec prog... <em>n</em>&lt;&gt;<em>file</em>'</tt></li>
 <li> <tt>redirfd -a <em>n</em> <em>file</em> prog...</tt> is roughly equivalent to
<tt>sh -c 'exec prog... <em>n</em>&gt;&gt;<em>file</em>'</tt></li>
 <li> <tt>redirfd -x <em>n</em> <em>file</em> prog...</tt> has no portable
shell equivalent. </li>
</ul>

<h2> Special fifo handling </h2>

<p>
 The <tt>-n</tt> and <tt>-b</tt> options are especially useful with
named pipes. 
</p>

<ul>
 <li> Opening a fifo for reading, blocking if there is no writer:
<tt>redirfd -r <em>n</em> <em>fifo</em> prog...</tt></li>
 <li> Opening a fifo for reading, with instant success even if
there is no writer, and blocking at the first attempt to read from it:
<tt>redirfd -r -nb <em>n</em> <em>fifo</em> prog...</tt></li>
 <li> Opening a fifo for writing, blocking if there is no reader:
<tt>redirfd -w <em>n</em> <em>fifo</em> prog...</tt></li>
 <li> Opening a fifo for writing, with instant success even if
there is no reader:
<tt>redirfd -w -nb <em>n</em> <em>fifo</em> prog...</tt>. Warning:
the first attempt to write to the fifo will raise a SIGPIPE if there is
still no reader at that time. The named pipe semantics normally do not
allow a fifo to be open for writing without a reading end, and you
should know what you are doing if you're using <tt>redirfd</tt>
this way. </li>
</ul>

</body>
</html>