File: dup2.lua.html

package info (click to toggle)
lua-posix 36.3-1
  • links: PTS, VCS
  • area: main
  • in suites: forky, sid, trixie
  • size: 1,720 kB
  • sloc: ansic: 5,462; makefile: 21; sh: 6
file content (159 lines) | stat: -rw-r--r-- 7,252 bytes parent folder | download
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
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Strict//EN"
   "http://www.w3.org/TR/xhtml1/DTD/xhtml1-strict.dtd">
<html>
<meta http-equiv="Content-Type" content="text/html; charset=UTF-8"/>
<head>
    <title>luaposix 36.3 Reference</title>
    <link rel="stylesheet" href="../ldoc.css" type="text/css" />
</head>
<body>

<div id="container">

<div id="product">
	<div id="product_logo"></div>
	<div id="product_name"><big><b></b></big></div>
	<div id="product_description"></div>
</div> <!-- id="product" -->


<div id="main">


<!-- Menu -->

<div id="navigation">
<br/>
<h1>luaposix 36.3</h1>


<ul>
  <li><a href="../index.html">Index</a></li>
</ul>



<h2>Examples</h2>
<ul class="nowrap">
  <li><a href="../examples/abstract-ns-client.lua.html">abstract-ns-client.lua</a></li>
  <li><a href="../examples/abstract-ns-server.lua.html">abstract-ns-server.lua</a></li>
  <li><a href="../examples/dir.lua.html">dir.lua</a></li>
  <li><strong>dup2.lua</strong></li>
  <li><a href="../examples/fork.lua.html">fork.lua</a></li>
  <li><a href="../examples/fork2.lua.html">fork2.lua</a></li>
  <li><a href="../examples/getopt.lua.html">getopt.lua</a></li>
  <li><a href="../examples/glob.lua.html">glob.lua</a></li>
  <li><a href="../examples/limit.lua.html">limit.lua</a></li>
  <li><a href="../examples/lock.lua.html">lock.lua</a></li>
  <li><a href="../examples/netlink-uevent.lua.html">netlink-uevent.lua</a></li>
  <li><a href="../examples/packet-socket.lua.html">packet-socket.lua</a></li>
  <li><a href="../examples/ping.lua.html">ping.lua</a></li>
  <li><a href="../examples/poll.lua.html">poll.lua</a></li>
  <li><a href="../examples/rt_sched.lua.html">rt_sched.lua</a></li>
  <li><a href="../examples/signal.lua.html">signal.lua</a></li>
  <li><a href="../examples/socket.lua.html">socket.lua</a></li>
  <li><a href="../examples/termios.lua.html">termios.lua</a></li>
  <li><a href="../examples/tree.lua.html">tree.lua</a></li>
</ul>
<h2>Modules</h2>
<ul class="nowrap">
  <li><a href="../modules/posix.html">posix</a></li>
  <li><a href="../modules/posix.ctype.html">posix.ctype</a></li>
  <li><a href="../modules/posix.dirent.html">posix.dirent</a></li>
  <li><a href="../modules/posix.errno.html">posix.errno</a></li>
  <li><a href="../modules/posix.fcntl.html">posix.fcntl</a></li>
  <li><a href="../modules/posix.fnmatch.html">posix.fnmatch</a></li>
  <li><a href="../modules/posix.glob.html">posix.glob</a></li>
  <li><a href="../modules/posix.grp.html">posix.grp</a></li>
  <li><a href="../modules/posix.libgen.html">posix.libgen</a></li>
  <li><a href="../modules/posix.poll.html">posix.poll</a></li>
  <li><a href="../modules/posix.pwd.html">posix.pwd</a></li>
  <li><a href="../modules/posix.sched.html">posix.sched</a></li>
  <li><a href="../modules/posix.signal.html">posix.signal</a></li>
  <li><a href="../modules/posix.stdio.html">posix.stdio</a></li>
  <li><a href="../modules/posix.stdlib.html">posix.stdlib</a></li>
  <li><a href="../modules/posix.sys.msg.html">posix.sys.msg</a></li>
  <li><a href="../modules/posix.sys.resource.html">posix.sys.resource</a></li>
  <li><a href="../modules/posix.sys.socket.html">posix.sys.socket</a></li>
  <li><a href="../modules/posix.sys.stat.html">posix.sys.stat</a></li>
  <li><a href="../modules/posix.sys.statvfs.html">posix.sys.statvfs</a></li>
  <li><a href="../modules/posix.sys.time.html">posix.sys.time</a></li>
  <li><a href="../modules/posix.sys.times.html">posix.sys.times</a></li>
  <li><a href="../modules/posix.sys.utsname.html">posix.sys.utsname</a></li>
  <li><a href="../modules/posix.sys.wait.html">posix.sys.wait</a></li>
  <li><a href="../modules/posix.syslog.html">posix.syslog</a></li>
  <li><a href="../modules/posix.termio.html">posix.termio</a></li>
  <li><a href="../modules/posix.time.html">posix.time</a></li>
  <li><a href="../modules/posix.unistd.html">posix.unistd</a></li>
  <li><a href="../modules/posix.utime.html">posix.utime</a></li>
</ul>

</div>

<div id="content">

    <h2>dup2.lua</h2>
<pre>
#! /usr/bin/env lua

<span class="keyword">local</span> M = <span class="global">require</span> <span class="string">'posix.unistd'</span>


<span class="keyword">local</span> <span class="keyword">function</span> <span class="function-name">pipe</span>()
   <span class="keyword">local</span> r, w = M.<span class="function-name">pipe</span>()
   <span class="global">assert</span>(r ~= <span class="keyword">nil</span>, w)
   <span class="keyword">return</span> r, w
<span class="keyword">end</span>

<span class="keyword">local</span> stdout_r, stdout_w = <span class="function-name">pipe</span>()
<span class="keyword">local</span> stderr_r, stderr_w = <span class="function-name">pipe</span>()

<span class="keyword">local</span> pid, errmsg = M.<span class="function-name">fork</span>()
<span class="global">assert</span>(pid ~= <span class="keyword">nil</span>, errmsg)

<span class="keyword">if</span> pid == <span class="number">0</span> <span class="keyword">then</span>
   <span class="comment">-- Child Process:
</span>
   M.<span class="function-name">close</span>(stdout_r)
   M.<span class="function-name">close</span>(stderr_r)

   M.<span class="function-name">dup2</span>(stdout_w, M.STDOUT_FILENO)
   M.<span class="function-name">dup2</span>(stderr_w, M.STDERR_FILENO)

   <span class="comment">-- Exec() a subprocess here instead if you like --
</span>
   <span class="global">io</span>.stdout:<span class="function-name">write</span> <span class="string">'output string'</span>
   <span class="global">io</span>.stderr:<span class="function-name">write</span> <span class="string">'oh noes!'</span>
   <span class="global">os</span>.<span class="function-name">exit</span>(<span class="number">42</span>)

<span class="keyword">else</span>
   <span class="comment">-- Parent Process:
</span>
   M.<span class="function-name">close</span>(stdout_w)
   M.<span class="function-name">close</span>(stderr_w)

   <span class="keyword">local</span> <span class="keyword">function</span> <span class="function-name">read</span>(msg, fd)
      <span class="keyword">local</span> outs, errmsg = M.<span class="function-name">read</span>(fd, <span class="number">1024</span>)
      <span class="global">assert</span>(outs ~= <span class="keyword">nil</span>, errmsg)
      <span class="global">print</span>(msg, outs)
   <span class="keyword">end</span>

   <span class="function-name">read</span>(<span class="string">'STDOUT:'</span>, stdout_r)
   <span class="function-name">read</span>(<span class="string">'STDERR:'</span>, stderr_r)


   <span class="keyword">local</span> childpid, reason, status = <span class="global">require</span> <span class="string">'posix.sys.wait'</span>.<span class="function-name">wait</span>(pid)
   <span class="global">assert</span>(childpid ~= <span class="keyword">nil</span>, reason)
   <span class="global">print</span>(<span class="string">'child '</span> .. reason, status)
<span class="keyword">end</span></pre>


</div> <!-- id="content" -->
</div> <!-- id="main" -->
<div id="about">
<i>generated by <a href="http://github.com/lunarmodules/LDoc">LDoc 1.5.0</a></i>
<i style="float:right;">Last updated 2025-02-16 13:16:41 </i>
</div> <!-- id="about" -->
</div> <!-- id="container" -->
</body>
</html>