Skip to content

Commit fc55153

Browse files
committed
Fixed trivial issues for Linux
1 parent fb90149 commit fc55153

File tree

2 files changed

+21
-20
lines changed

2 files changed

+21
-20
lines changed

source/matplot/backend/gnuplot.cpp

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -4,12 +4,13 @@
44

55
#include "gnuplot.h"
66

7-
#include <cstdlib>
8-
#include <iostream>
97
#include <matplot/util/common.h>
108
#include <matplot/util/popen.h>
9+
#include <iostream>
1110
#include <regex>
1211
#include <thread>
12+
#include <cstring>
13+
#include <cstdlib>
1314

1415
#ifdef __has_include
1516
#if __has_include(<filesystem>)

source/matplot/util/popen.cpp

Lines changed: 18 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,7 @@
66

77
#include <io.h>
88

9-
int common_pipe::open(const std::string& cmd, char mode)
9+
int common_pipe::open(const std::string& cmd, char mode)
1010
{
1111
if (opened())
1212
close(); // prevent resource leak
@@ -124,41 +124,41 @@ int common_pipe::close(int *exit_code)
124124
#include <cerrno>
125125
#include <sys/wait.h> // waitpid
126126

127-
int common_pipe::open(const std::string &cmd, char mode)
127+
int common_pipe::open(const std::string &cmd, char mode)
128128
{
129129
constexpr auto READ = 0u;
130130
constexpr auto WRITE = 1u;
131131
int fd[2];
132-
if (pipe(fd) == -1)
132+
if (::pipe(fd) == -1)
133133
return error(errno, "pipe");
134-
if ((pid = fork()) == -1)
134+
if ((pid = ::fork()) == -1)
135135
return error(errno, "fork");
136136

137137
if (pid == 0) { // child process
138138
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
141141
} 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
143143
dup2(fd[READ], 0); // Redirect stdin to pipe
144144
}
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);
147147
// execl returns only upon error
148148
std::exit(EXIT_FAILURE);
149149
} else {
150150
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
152152
// fd is read-only
153153
} 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
155155
// is write-only
156156
}
157157
}
158158
if (mode == 'r')
159-
file_ = fdopen(fd[READ], "r");
159+
file_ = ::fdopen(fd[READ], "r");
160160
else
161-
file_ = fdopen(fd[WRITE], "w");
161+
file_ = ::fdopen(fd[WRITE], "w");
162162
if (file_ == nullptr)
163163
return error(errno, "fdopen");
164164
return 0;
@@ -167,7 +167,7 @@ int common_pipe::open(const std::string &cmd, char mode)
167167
/// Closes the pipe opened by popen2 and waits for termination
168168
int common_pipe::close(int *exit_code)
169169
{
170-
if (!valid())
170+
if (!opened())
171171
return 0;
172172
fclose(file_);
173173
file_ = nullptr;
@@ -190,7 +190,7 @@ inline int common_pipe::error(int code, const std::string& what) const
190190
return code;
191191
}
192192

193-
int opipe::write(std::string_view data)
193+
int opipe::write(std::string_view data)
194194
{
195195
constexpr auto CSIZE = sizeof(std::string_view::value_type);
196196
if (!opened())
@@ -205,7 +205,7 @@ int opipe::write(std::string_view data)
205205
return 0;
206206
}
207207

208-
int opipe::flush(std::string_view data)
208+
int opipe::flush(std::string_view data)
209209
{
210210
if (!opened())
211211
return error(EINVAL, "opipe::flush");
@@ -217,7 +217,7 @@ int opipe::flush(std::string_view data)
217217
return 0;
218218
}
219219

220-
int ipipe::read(std::string &data)
220+
int ipipe::read(std::string &data)
221221
{
222222
if (!opened())
223223
return error(EINVAL, "ipipe::read");
@@ -234,7 +234,7 @@ int ipipe::read(std::string &data)
234234
return 0;
235235
}
236236

237-
int shell_write(const std::string &cmd, std::string &data)
237+
int shell_write(const std::string &cmd, std::string &data)
238238
{
239239
auto pipe = opipe{};
240240
if (auto err = pipe.open(cmd); err != 0)

0 commit comments

Comments
 (0)