Skip to content

Commit 8393283

Browse files
committed
Use run instead of popen to run ar
Loading files from archives requires the use of ar; invoke it via run instead of popen to eliminate OS-specific code from the goto-cc folder.
1 parent c11b81c commit 8393283

File tree

1 file changed

+29
-47
lines changed

1 file changed

+29
-47
lines changed

src/goto-cc/compile.cpp

Lines changed: 29 additions & 47 deletions
Original file line numberDiff line numberDiff line change
@@ -21,9 +21,11 @@ Date: June 2006
2121
#include <util/config.h>
2222
#include <util/file_util.h>
2323
#include <util/get_base_name.h>
24+
#include <util/run.h>
2425
#include <util/suffix.h>
2526
#include <util/symbol_table_builder.h>
2627
#include <util/tempdir.h>
28+
#include <util/tempfile.h>
2729
#include <util/unicode.h>
2830
#include <util/version.h>
2931

@@ -47,19 +49,6 @@ Date: June 2006
4749
"size=\"30,40\";"\
4850
"ratio=compress;"
4951

50-
#ifdef _WIN32
51-
#include <util/pragma_push.def>
52-
#ifdef _MSC_VER
53-
#pragma warning(disable:4668)
54-
// using #if/#elif on undefined macro
55-
#endif
56-
#include <direct.h>
57-
#include <windows.h>
58-
#define popen _popen
59-
#define pclose _pclose
60-
#include <util/pragma_pop.def>
61-
#endif
62-
6352
/// reads and source and object files, compiles and links them into goto program
6453
/// objects.
6554
/// \return true on error, false otherwise
@@ -219,9 +208,6 @@ bool compilet::add_files_from_archive(
219208
const std::string &file_name,
220209
bool thin_archive)
221210
{
222-
std::stringstream cmd;
223-
FILE *stream;
224-
225211
std::string tstr = working_directory;
226212

227213
if(!thin_archive)
@@ -232,44 +218,40 @@ bool compilet::add_files_from_archive(
232218
set_current_path(tmp_dirs.back());
233219

234220
// unpack now
235-
cmd << "ar x " << concat_dir_file(working_directory, file_name);
236-
237-
stream=popen(cmd.str().c_str(), "r");
238-
pclose(stream);
239-
240-
cmd.clear();
241-
cmd.str("");
221+
int ret =
222+
run("ar", {"ar", "x", concat_dir_file(working_directory, file_name)});
223+
if(ret != 0)
224+
{
225+
error() << "Failed to extract archive " << file_name << eom;
226+
return true;
227+
}
242228
}
243229

244230
// add the files from "ar t"
245-
cmd << "ar t " << concat_dir_file(working_directory, file_name);
246-
247-
stream = popen(cmd.str().c_str(), "r");
248-
249-
if(stream != nullptr)
231+
temporary_filet tmp_file_out("", "");
232+
int ret = run(
233+
"ar",
234+
{"ar", "t", concat_dir_file(working_directory, file_name)},
235+
"",
236+
tmp_file_out(),
237+
"");
238+
if(ret != 0)
250239
{
251-
std::string line;
252-
int ch; // fgetc returns an int, not char
253-
while((ch = fgetc(stream)) != EOF)
254-
{
255-
if(ch != '\n')
256-
{
257-
line += static_cast<char>(ch);
258-
}
259-
else
260-
{
261-
std::string t = concat_dir_file(tstr, line);
240+
error() << "Failed to list archive " << file_name << eom;
241+
return true;
242+
}
262243

263-
if(is_goto_binary(t))
264-
object_files.push_back(t);
265-
else
266-
debug() << "Object file is not a goto binary: " << line << eom;
244+
std::ifstream in(tmp_file_out());
245+
std::string line;
267246

268-
line = "";
269-
}
270-
}
247+
while(!in.fail() && std::getline(in, line))
248+
{
249+
std::string t = concat_dir_file(tstr, line);
271250

272-
pclose(stream);
251+
if(is_goto_binary(t))
252+
object_files.push_back(t);
253+
else
254+
debug() << "Object file is not a goto binary: " << line << eom;
273255
}
274256

275257
if(!thin_archive)

0 commit comments

Comments
 (0)