Skip to content

Commit fbed7f1

Browse files
tautschnigDaniel Kroening
authored and
Daniel Kroening
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 bafd7e7 commit fbed7f1

File tree

1 file changed

+29
-49
lines changed

1 file changed

+29
-49
lines changed

src/goto-cc/compile.cpp

Lines changed: 29 additions & 49 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,21 +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-
#pragma warning(disable : 5039)
56-
// pointer or reference to potentially throwing function passed to extern C
57-
#endif
58-
#include <direct.h>
59-
#include <windows.h>
60-
#define popen _popen
61-
#define pclose _pclose
62-
#include <util/pragma_pop.def>
63-
#endif
64-
6552
/// reads and source and object files, compiles and links them into goto program
6653
/// objects.
6754
/// \return true on error, false otherwise
@@ -221,9 +208,6 @@ bool compilet::add_files_from_archive(
221208
const std::string &file_name,
222209
bool thin_archive)
223210
{
224-
std::stringstream cmd;
225-
FILE *stream;
226-
227211
std::string tstr = working_directory;
228212

229213
if(!thin_archive)
@@ -234,44 +218,40 @@ bool compilet::add_files_from_archive(
234218
set_current_path(tmp_dirs.back());
235219

236220
// unpack now
237-
cmd << "ar x " << concat_dir_file(working_directory, file_name);
238-
239-
stream=popen(cmd.str().c_str(), "r");
240-
pclose(stream);
241-
242-
cmd.clear();
243-
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+
}
244228
}
245229

246230
// add the files from "ar t"
247-
cmd << "ar t " << concat_dir_file(working_directory, file_name);
248-
249-
stream = popen(cmd.str().c_str(), "r");
250-
251-
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)
252239
{
253-
std::string line;
254-
int ch; // fgetc returns an int, not char
255-
while((ch = fgetc(stream)) != EOF)
256-
{
257-
if(ch != '\n')
258-
{
259-
line += static_cast<char>(ch);
260-
}
261-
else
262-
{
263-
std::string t = concat_dir_file(tstr, line);
240+
error() << "Failed to list archive " << file_name << eom;
241+
return true;
242+
}
264243

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

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

274-
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;
275255
}
276256

277257
if(!thin_archive)

0 commit comments

Comments
 (0)