6
6
7
7
#include < io.h>
8
8
9
- int common_pipe::open (const std::string& cmd, char mode)
9
+ int common_pipe::open (const std::string& cmd, char mode)
10
10
{
11
11
if (opened ())
12
12
close (); // prevent resource leak
@@ -124,41 +124,41 @@ int common_pipe::close(int *exit_code)
124
124
#include < cerrno>
125
125
#include < sys/wait.h> // waitpid
126
126
127
- int common_pipe::open (const std::string &cmd, char mode)
127
+ int common_pipe::open (const std::string &cmd, char mode)
128
128
{
129
129
constexpr auto READ = 0u ;
130
130
constexpr auto WRITE = 1u ;
131
131
int fd[2 ];
132
- if (pipe (fd) == -1 )
132
+ if (:: pipe (fd) == -1 )
133
133
return error (errno, " pipe" );
134
- if ((pid = fork ()) == -1 )
134
+ if ((pid = :: fork ()) == -1 )
135
135
return error (errno, " fork" );
136
136
137
137
if (pid == 0 ) { // child process
138
138
if (mode == ' r' ) {
139
- close (fd[READ]); // Close the READ end of the pipe
140
- dup2 (fd[WRITE], 1 ); // Redirect stdout to pipe
139
+ :: close (fd[READ]); // Close the READ end of the pipe
140
+ :: dup2 (fd[WRITE], 1 ); // Redirect stdout to pipe
141
141
} else { // (mode == 'w')
142
- close (fd[WRITE]); // Close the WRITE end of the pipe
142
+ :: close (fd[WRITE]); // Close the WRITE end of the pipe
143
143
dup2 (fd[READ], 0 ); // Redirect stdin to pipe
144
144
}
145
- setpgid (pid, pid); // Needed so negative PIDs can kill children of /bin/sh
146
- execl (" /bin/sh" , " /bin/sh" , " -c" , cmd.c_str (), nullptr );
145
+ :: setpgid (pid, pid); // Needed so negative PIDs can kill children of /bin/sh
146
+ :: execl (" /bin/sh" , " /bin/sh" , " -c" , cmd.c_str(), nullptr);
147
147
// execl returns only upon error
148
148
std::exit (EXIT_FAILURE);
149
149
} else {
150
150
if (mode == ' r' ) {
151
- close (fd[WRITE]); // Close the WRITE end of the pipe since parent's
151
+ :: close (fd[WRITE]); // Close the WRITE end of the pipe since parent's
152
152
// fd is read-only
153
153
} else { // (mode == 'w')
154
- close (fd[READ]); // Close the READ end of the pipe since parent's fd
154
+ :: close (fd[READ]); // Close the READ end of the pipe since parent's fd
155
155
// is write-only
156
156
}
157
157
}
158
158
if (mode == ' r' )
159
- file_ = fdopen (fd[READ], " r" );
159
+ file_ = :: fdopen (fd[READ], " r" );
160
160
else
161
- file_ = fdopen (fd[WRITE], " w" );
161
+ file_ = :: fdopen (fd[WRITE], " w" );
162
162
if (file_ == nullptr )
163
163
return error (errno, " fdopen" );
164
164
return 0 ;
@@ -167,7 +167,7 @@ int common_pipe::open(const std::string &cmd, char mode)
167
167
// / Closes the pipe opened by popen2 and waits for termination
168
168
int common_pipe::close (int *exit_code)
169
169
{
170
- if (!valid ())
170
+ if (!opened ())
171
171
return 0 ;
172
172
fclose (file_);
173
173
file_ = nullptr ;
@@ -190,7 +190,7 @@ inline int common_pipe::error(int code, const std::string& what) const
190
190
return code;
191
191
}
192
192
193
- int opipe::write (std::string_view data)
193
+ int opipe::write (std::string_view data)
194
194
{
195
195
constexpr auto CSIZE = sizeof (std::string_view::value_type);
196
196
if (!opened ())
@@ -205,7 +205,7 @@ int opipe::write(std::string_view data)
205
205
return 0 ;
206
206
}
207
207
208
- int opipe::flush (std::string_view data)
208
+ int opipe::flush (std::string_view data)
209
209
{
210
210
if (!opened ())
211
211
return error (EINVAL, " opipe::flush" );
@@ -217,7 +217,7 @@ int opipe::flush(std::string_view data)
217
217
return 0 ;
218
218
}
219
219
220
- int ipipe::read (std::string &data)
220
+ int ipipe::read (std::string &data)
221
221
{
222
222
if (!opened ())
223
223
return error (EINVAL, " ipipe::read" );
@@ -234,7 +234,7 @@ int ipipe::read(std::string &data)
234
234
return 0 ;
235
235
}
236
236
237
- int shell_write (const std::string &cmd, std::string &data)
237
+ int shell_write (const std::string &cmd, std::string &data)
238
238
{
239
239
auto pipe = opipe{};
240
240
if (auto err = pipe.open (cmd); err != 0 )
0 commit comments