Skip to content

Commit 957ee4a

Browse files
authored
Is. #681 - read-only files, and dirs (#926)
Tested in 3 majors OS'es... no problems... closes #681
1 parent 8e70d4d commit 957ee4a

File tree

1 file changed

+13
-7
lines changed

1 file changed

+13
-7
lines changed

src/tidylib.c

+13-7
Original file line numberDiff line numberDiff line change
@@ -1125,19 +1125,26 @@ int TIDY_CALL tidyParseSource( TidyDoc tdoc, TidyInputSource* source )
11251125
return tidyDocParseSource( doc, source );
11261126
}
11271127

1128-
1128+
#ifdef WIN32
1129+
#define M_IS_DIR _S_IFDIR
1130+
#else // !WIN32
1131+
#define M_IS_DIR S_IFDIR
1132+
#endif
11291133
int tidyDocParseFile( TidyDocImpl* doc, ctmbstr filnam )
11301134
{
11311135
int status = -ENOENT;
1132-
FILE* fin = fopen( filnam, "r+" );
1133-
1134-
if ( !fin )
1136+
FILE* fin = 0;
1137+
struct stat sbuf = { 0 }; /* Is. #681 - read-only files */
1138+
if ( stat(filnam,&sbuf) != 0 )
11351139
{
11361140
TY_(ReportFileError)( doc, filnam, FILE_NOT_FILE );
11371141
return status;
11381142
}
1139-
1140-
fclose( fin );
1143+
if (sbuf.st_mode & M_IS_DIR) /* and /NOT/ if a DIRECTORY */
1144+
{
1145+
TY_(ReportFileError)(doc, filnam, FILE_NOT_FILE);
1146+
return status;
1147+
}
11411148

11421149
#ifdef _WIN32
11431150
return TY_(DocParseFileWithMappedFile)( doc, filnam );
@@ -1147,7 +1154,6 @@ int tidyDocParseFile( TidyDocImpl* doc, ctmbstr filnam )
11471154

11481155
#if PRESERVE_FILE_TIMES
11491156
{
1150-
struct stat sbuf = { 0 };
11511157
/* get last modified time */
11521158
TidyClearMemory(&doc->filetimes, sizeof(doc->filetimes));
11531159
if (fin && cfgBool(doc, TidyKeepFileTimes) &&

0 commit comments

Comments
 (0)