Skip to content

Commit af3ffff

Browse files
committed
add is_regular_file and is_symlink
1 parent a5a340e commit af3ffff

File tree

2 files changed

+33
-0
lines changed

2 files changed

+33
-0
lines changed

src/stdlib_system.F90

Lines changed: 22 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -241,6 +241,9 @@ module stdlib_system
241241
!! Any encountered errors are handled using `state_type`.
242242
!!
243243
public :: exists
244+
245+
public :: is_symlink
246+
public :: is_regular_file
244247

245248
! CPU clock ticks storage
246249
integer, parameter, private :: TICKS = int64
@@ -1204,6 +1207,25 @@ end function stdlib_exists
12041207
end if
12051208
end function exists
12061209

1210+
logical function is_symlink(path)
1211+
character(len=*), intent(in) :: path
1212+
1213+
is_symlink = exists(path) == fs_type_symlink
1214+
end function is_symlink
1215+
1216+
logical function is_regular_file(path)
1217+
character(len=*), intent(in) :: path
1218+
1219+
interface
1220+
logical(c_bool) function stdlib_is_regular_file(path) bind(C, name='stdlib_is_regular_file')
1221+
import c_char, c_bool
1222+
character(kind=c_char) :: path(:)
1223+
end function stdlib_is_regular_file
1224+
end interface
1225+
1226+
is_regular_file = logical(stdlib_is_regular_file(to_c_char(path)))
1227+
end function is_regular_file
1228+
12071229
character function path_sep()
12081230
if (OS_TYPE() == OS_WINDOWS) then
12091231
path_sep = '\'

src/stdlib_system.c

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -119,3 +119,14 @@ int stdlib_exists(const char* path, int* stat){
119119
#endif /* ifdef _WIN32 */
120120
return type;
121121
}
122+
123+
// `stat` and `_stat` follow symlinks automatically.
124+
// so no need for winapi functions.
125+
bool stdlib_is_regular_file(const char* path) {
126+
struct stat buf = {0};
127+
#ifdef _WIN32
128+
return _stat(path, &buf) == 0 && S_ISREG(buf.st_mode);
129+
#else
130+
return stat(path, &buf) == 0 && S_ISREG(buf.st_mode);
131+
#endif /* ifdef _WIN32 */
132+
}

0 commit comments

Comments
 (0)