Skip to content

Navigation Menu

Sign in
Appearance settings

Search code, repositories, users, issues, pull requests...

Provide feedback

We read every piece of feedback, and take your input very seriously.

Saved searches

Use saved searches to filter your results more quickly

Sign up
Appearance settings

Commit 2c738a8

Browse files
committed
add is_regular_file and is_symlink
1 parent a5a340e commit 2c738a8

File tree

2 files changed

+34
-0
lines changed

2 files changed

+34
-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: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -119,3 +119,15 @@ 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+
#ifdef _WIN32
127+
struct _stat buf = {0};
128+
return _stat(path, &buf) == 0 && S_ISREG(buf.st_mode);
129+
#else
130+
struct stat buf = {0};
131+
return stat(path, &buf) == 0 && S_ISREG(buf.st_mode);
132+
#endif /* ifdef _WIN32 */
133+
}

0 commit comments

Comments
(0)

AltStyle によって変換されたページ (->オリジナル) /