mirror of
https://git.dev.opencascade.org/repos/occt.git
synced 2025-08-09 13:22:24 +03:00
0026179: Coding rules - eliminate -Wdeprecated-declarations CLang warnings on tmpnam() usage
Make a temporary file using BuildTemporary() in "/tmp" folder on Linux or using "TEMP" environment variable on Windows. Use the new OSD_File::Capture() method for standard output redirection.
This commit is contained in:
@@ -26,6 +26,7 @@
|
||||
#include <OSD_Process.hxx>
|
||||
#include <OSD_Path.hxx>
|
||||
#include <OSD.hxx>
|
||||
#include <OSD_File.hxx>
|
||||
|
||||
#include <string.h>
|
||||
#include <tcl.h>
|
||||
@@ -66,52 +67,33 @@ namespace {
|
||||
cout << flush;
|
||||
}
|
||||
|
||||
FILE* capture_start (int std_fd, int *save_fd, char*& tmp_name)
|
||||
int capture_start (OSD_File& theTmpFile, int std_fd)
|
||||
{
|
||||
*save_fd = 0;
|
||||
|
||||
// open temporary files
|
||||
#if defined(_WIN32)
|
||||
// use _tempnam() to decrease chances of failure (tmpfile() creates
|
||||
// file in root folder and will fail if it is write protected), see #24132
|
||||
static const char* tmpdir = getenv("TEMP");
|
||||
static char prefix[256] = ""; // prefix for temporary files, initialize once per process using pid
|
||||
if (prefix[0] == '\0')
|
||||
sprintf (prefix, "drawtmp%d_", (int)OSD_Process().ProcessId());
|
||||
tmp_name = _tempnam (tmpdir, prefix);
|
||||
FILE* aTmpFile = (tmp_name != NULL ? fopen (tmp_name, "w+b") : tmpfile());
|
||||
#else
|
||||
tmp_name = NULL;
|
||||
FILE* aTmpFile = tmpfile();
|
||||
#endif
|
||||
int fd_tmp = (aTmpFile != NULL ? fileno (aTmpFile) : -1);
|
||||
if (fd_tmp < 0)
|
||||
theTmpFile.BuildTemporary();
|
||||
if (theTmpFile.Failed())
|
||||
{
|
||||
cerr << "Error: cannot create temporary file for capturing console output" << endl;
|
||||
fclose (aTmpFile);
|
||||
return NULL;
|
||||
return -1;
|
||||
}
|
||||
|
||||
// remember current file descriptors of standard stream, and replace it by temporary
|
||||
(*save_fd) = dup(std_fd);
|
||||
dup2(fd_tmp, std_fd);
|
||||
return aTmpFile;
|
||||
return theTmpFile.Capture(std_fd);
|
||||
}
|
||||
|
||||
void capture_end (FILE* tmp_file, int std_fd, int save_fd, char* tmp_name, Standard_OStream &log, Standard_Boolean doEcho)
|
||||
void capture_end (OSD_File* tmp_file, int std_fd, int save_fd, Standard_OStream &log, Standard_Boolean doEcho)
|
||||
{
|
||||
if (! tmp_file)
|
||||
if (!tmp_file)
|
||||
return;
|
||||
|
||||
// restore normal descriptors of console stream
|
||||
dup2 (save_fd, std_fd);
|
||||
dup2(save_fd, std_fd);
|
||||
close(save_fd);
|
||||
|
||||
// extract all output and copy it to log and optionally to cout
|
||||
const int BUFSIZE = 2048;
|
||||
char buf[BUFSIZE];
|
||||
rewind(tmp_file);
|
||||
while (fgets (buf, BUFSIZE, tmp_file) != NULL)
|
||||
TCollection_AsciiString buf;
|
||||
tmp_file->Rewind();
|
||||
while (tmp_file->ReadLine (buf, BUFSIZE) > 0)
|
||||
{
|
||||
log << buf;
|
||||
if (doEcho)
|
||||
@@ -119,12 +101,13 @@ namespace {
|
||||
}
|
||||
|
||||
// close temporary file
|
||||
fclose (tmp_file);
|
||||
tmp_file->Close();
|
||||
|
||||
// remove temporary file if this is not done by the system
|
||||
if (tmp_name)
|
||||
remove (tmp_name);
|
||||
if (tmp_file->Exists())
|
||||
tmp_file->Remove();
|
||||
}
|
||||
|
||||
};
|
||||
|
||||
// MKV 29.03.05
|
||||
@@ -157,15 +140,13 @@ static Standard_Integer CommandCmd
|
||||
flush_standard_streams();
|
||||
|
||||
// capture cout and cerr to log
|
||||
char *err_name = NULL, *out_name = NULL;
|
||||
FILE * aFile_err = NULL;
|
||||
FILE * aFile_out = NULL;
|
||||
int fd_err_save = 0;
|
||||
int fd_out_save = 0;
|
||||
OSD_File aFile_out, aFile_err;
|
||||
int fd_err_save = -1;
|
||||
int fd_out_save = -1;
|
||||
if (doLog)
|
||||
{
|
||||
aFile_out = capture_start (STDOUT_FILENO, &fd_out_save, out_name);
|
||||
aFile_err = capture_start (STDERR_FILENO, &fd_err_save, err_name);
|
||||
fd_out_save = capture_start (aFile_out, STDOUT_FILENO);
|
||||
fd_err_save = capture_start (aFile_err, STDERR_FILENO);
|
||||
}
|
||||
|
||||
// run command
|
||||
@@ -218,8 +199,8 @@ static Standard_Integer CommandCmd
|
||||
// end capturing cout and cerr
|
||||
if (doLog)
|
||||
{
|
||||
capture_end (aFile_err, STDERR_FILENO, fd_err_save, err_name, di.Log(), doEcho);
|
||||
capture_end (aFile_out, STDOUT_FILENO, fd_out_save, out_name, di.Log(), doEcho);
|
||||
capture_end (&aFile_err, STDERR_FILENO, fd_err_save, di.Log(), doEcho);
|
||||
capture_end (&aFile_out, STDOUT_FILENO, fd_out_save, di.Log(), doEcho);
|
||||
}
|
||||
|
||||
// log command result
|
||||
|
Reference in New Issue
Block a user