summaryrefslogtreecommitdiffstats
diff options
context:
space:
mode:
authorChristophe Grenier <grenier@cgsecurity.org>2020-11-27 19:19:33 +0100
committerChristophe Grenier <grenier@cgsecurity.org>2020-11-27 19:19:33 +0100
commit0a7811bc2bf24394d652a158ef0add02b8bea333 (patch)
tree1e1cd4de2c440386ad7ed4be9a7174ab14de7e8a
parent5167a2ddf8b171a4e6e823efee337b652f542dad (diff)
Do not return the file handle when calling log_open() and log_open_default()HEADmaster
-rw-r--r--src/log.c28
-rw-r--r--src/log.h4
-rw-r--r--src/phmain.c12
-rw-r--r--src/testdisk.c20
4 files changed, 32 insertions, 32 deletions
diff --git a/src/log.c b/src/log.c
index 65ac94c..25cb32f 100644
--- a/src/log.c
+++ b/src/log.c
@@ -69,7 +69,7 @@ unsigned int log_set_levels(const unsigned int levels)
return old_levels;
}
-FILE *log_open(const char*default_filename, const int mode, int *errsv)
+int log_open(const char*default_filename, const int mode, int *errsv)
{
log_handle=fopen(default_filename,(mode==TD_LOG_CREATE?"w":"a"));
*errsv=errno;
@@ -85,22 +85,22 @@ FILE *log_open(const char*default_filename, const int mode, int *errsv)
}
}
#endif
+ if(log_handle==NULL)
+ return 0;
#if defined(HAVE_DUP2)
- if(log_handle)
- {
- dup2(fileno(log_handle),2);
- }
+ dup2(fileno(log_handle),2);
#endif
- return log_handle;
+ return 1;
}
#if defined(__CYGWIN__) || defined(__MINGW32__)
-FILE *log_open_default(const char*default_filename, const int mode, int *errsv)
+int log_open_default(const char*default_filename, const int mode, int *errsv)
{
char*filename;
char *path;
+ int result;
if(log_handle != NULL)
- return log_handle;
+ return 1;
path = getenv("USERPROFILE");
if (path == NULL)
path = getenv("HOMEPATH");
@@ -117,16 +117,16 @@ FILE *log_open_default(const char*default_filename, const int mode, int *errsv)
#endif
strcat(filename, "/");
strcat(filename, default_filename);
- log_open(filename, mode, errsv);
+ result=log_open(filename, mode, errsv);
free(filename);
- return log_handle;
+ return result;
}
#else
-FILE *log_open_default(const char*default_filename, const int mode, int *errsv)
+int log_open_default(const char*default_filename, const int mode, int *errsv)
{
char*filename;
char *path;
- FILE *handle;
+ int result;
path = getenv("HOME");
if(path == NULL)
return log_open(default_filename, mode, errsv);
@@ -134,9 +134,9 @@ FILE *log_open_default(const char*default_filename, const int mode, int *errsv)
strcpy(filename, path);
strcat(filename, "/");
strcat(filename, default_filename);
- handle=log_open(filename, mode, errsv);
+ result=log_open(filename, mode, errsv);
free(filename);
- return handle;
+ return result;
}
#endif
diff --git a/src/log.h b/src/log.h
index cb91fed..638a304 100644
--- a/src/log.h
+++ b/src/log.h
@@ -32,14 +32,14 @@ unsigned int log_set_levels(const unsigned int levels);
@ requires \valid(errsv);
@ requires separation: \separated(default_filename, errsv);
@*/
-FILE *log_open(const char*default_filename, const int mode, int *errsv);
+int log_open(const char*default_filename, const int mode, int *errsv);
/*@
@ requires \valid_read(default_filename);
@ requires \valid(errsv);
@ requires separation: \separated(default_filename, errsv);
@*/
-FILE *log_open_default(const char*default_filename, const int mode, int *errsv);
+int log_open_default(const char*default_filename, const int mode, int *errsv);
int log_flush(void);
int log_close(void);
diff --git a/src/phmain.c b/src/phmain.c
index d97fda8..bc53fe4 100644
--- a/src/phmain.c
+++ b/src/phmain.c
@@ -153,7 +153,7 @@ int main( int argc, char **argv )
list_disk_t *list_disk=NULL;
list_disk_t *element_disk;
const char *logfile="photorec.log";
- FILE *log_handle=NULL;
+ int log_opened=0;
int log_errno=0;
struct ph_options options={
.paranoid=1,
@@ -301,7 +301,7 @@ int main( int argc, char **argv )
xml_set_command_line(argc, argv);
#endif
if(create_log!=TD_LOG_NONE)
- log_handle=log_open(logfile, create_log, &log_errno);
+ log_opened=log_open(logfile, create_log, &log_errno);
#ifdef HAVE_SETLOCALE
if(run_setlocale>0)
{
@@ -315,8 +315,8 @@ int main( int argc, char **argv )
}
}
#endif
- if(create_log!=TD_LOG_NONE && log_handle==NULL)
- log_handle=log_open_default(logfile, create_log, &log_errno);
+ if(create_log!=TD_LOG_NONE && log_opened==0)
+ log_opened=log_open_default(logfile, create_log, &log_errno);
#ifdef HAVE_NCURSES
/* ncurses need locale for correct unicode support */
if(start_ncurses("PhotoRec", argv[0]))
@@ -327,11 +327,11 @@ int main( int argc, char **argv )
}
{
const char*filename=logfile;
- while(create_log!=TD_LOG_NONE && log_handle==NULL)
+ while(create_log!=TD_LOG_NONE && log_opened==0)
{
filename=ask_log_location(filename, log_errno);
if(filename!=NULL)
- log_handle=log_open(filename, create_log, &log_errno);
+ log_opened=log_open(filename, create_log, &log_errno);
else
create_log=TD_LOG_NONE;
}
diff --git a/src/testdisk.c b/src/testdisk.c
index 499c116..6c4b8ac 100644
--- a/src/testdisk.c
+++ b/src/testdisk.c
@@ -231,7 +231,7 @@ int main( int argc, char **argv )
const char *cmd_device=NULL;
char *cmd_run=NULL;
const char *logfile="testdisk.log";
- FILE *log_handle=NULL;
+ int log_opened=0;
int log_errno=0;
/* srand needed for GPT creation (weak is ok) */
srand(time(NULL));
@@ -274,16 +274,16 @@ int main( int argc, char **argv )
{
if(create_log==TD_LOG_NONE)
create_log=TD_LOG_APPEND;
- if(log_handle==NULL)
- log_handle=log_open(logfile, create_log, &log_errno);
+ if(log_opened==0)
+ log_opened=log_open(logfile, create_log, &log_errno);
}
else if((strcmp(argv[i],"/debug")==0) || (strcmp(argv[i],"-debug")==0))
{
verbose++;
if(create_log==TD_LOG_NONE)
create_log=TD_LOG_APPEND;
- if(log_handle==NULL)
- log_handle=log_open(logfile, create_log, &log_errno);
+ if(log_opened==0)
+ log_opened=log_open(logfile, create_log, &log_errno);
}
else if((strcmp(argv[i],"/all")==0) || (strcmp(argv[i],"-all")==0))
testdisk_mode|=TESTDISK_O_ALL;
@@ -375,8 +375,8 @@ int main( int argc, char **argv )
}
}
#endif
- if(create_log!=TD_LOG_NONE && log_handle==NULL)
- log_handle=log_open_default(logfile, create_log, &log_errno);
+ if(create_log!=TD_LOG_NONE && log_opened==0)
+ log_opened=log_open_default(logfile, create_log, &log_errno);
#ifdef HAVE_NCURSES
/* ncurses need locale for correct unicode support */
if(start_ncurses("TestDisk",argv[0]))
@@ -389,15 +389,15 @@ int main( int argc, char **argv )
verbose=1;
create_log=ask_testdisk_log_creation();
if(create_log==TD_LOG_CREATE || create_log==TD_LOG_APPEND)
- log_handle=log_open(logfile, create_log, &log_errno);
+ log_opened=log_open(logfile, create_log, &log_errno);
}
{
const char*filename=logfile;
- while(create_log!=TD_LOG_NONE && log_handle==NULL)
+ while(create_log!=TD_LOG_NONE && log_opened==0)
{
filename=ask_log_location(filename, log_errno);
if(filename!=NULL)
- log_handle=log_open(filename, create_log, &log_errno);
+ log_opened=log_open(filename, create_log, &log_errno);
else
create_log=TD_LOG_NONE;
}