From 957ee4a47e92f7e4929ca04a3c7f7bd355898d37 Mon Sep 17 00:00:00 2001 From: Geoff McLane Date: Wed, 14 Apr 2021 18:02:09 +0200 Subject: [PATCH] Is. #681 - read-only files, and dirs (#926) Tested in 3 majors OS'es... no problems... closes #681 --- src/tidylib.c | 20 +++++++++++++------- 1 file changed, 13 insertions(+), 7 deletions(-) diff --git a/src/tidylib.c b/src/tidylib.c index 75cb1d9..4d57510 100644 --- a/src/tidylib.c +++ b/src/tidylib.c @@ -1125,19 +1125,26 @@ int TIDY_CALL tidyParseSource( TidyDoc tdoc, TidyInputSource* source ) return tidyDocParseSource( doc, source ); } - +#ifdef WIN32 +#define M_IS_DIR _S_IFDIR +#else // !WIN32 +#define M_IS_DIR S_IFDIR +#endif int tidyDocParseFile( TidyDocImpl* doc, ctmbstr filnam ) { int status = -ENOENT; - FILE* fin = fopen( filnam, "r+" ); - - if ( !fin ) + FILE* fin = 0; + struct stat sbuf = { 0 }; /* Is. #681 - read-only files */ + if ( stat(filnam,&sbuf) != 0 ) { TY_(ReportFileError)( doc, filnam, FILE_NOT_FILE ); return status; } - - fclose( fin ); + if (sbuf.st_mode & M_IS_DIR) /* and /NOT/ if a DIRECTORY */ + { + TY_(ReportFileError)(doc, filnam, FILE_NOT_FILE); + return status; + } #ifdef _WIN32 return TY_(DocParseFileWithMappedFile)( doc, filnam ); @@ -1147,7 +1154,6 @@ int tidyDocParseFile( TidyDocImpl* doc, ctmbstr filnam ) #if PRESERVE_FILE_TIMES { - struct stat sbuf = { 0 }; /* get last modified time */ TidyClearMemory(&doc->filetimes, sizeof(doc->filetimes)); if (fin && cfgBool(doc, TidyKeepFileTimes) &&