Skip to content

Commit 8176363

Browse files
committed
Add file tree walk interface.
1 parent 70b5163 commit 8176363

File tree

4 files changed

+94
-8
lines changed

4 files changed

+94
-8
lines changed

Makefile

Lines changed: 9 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -42,13 +42,14 @@ DOCDIR = ./doc
4242
TARGET = libfortran-unix.a
4343

4444
SRC = src/unix.f90 src/unix_dirent.F90 src/unix_errno.F90 src/unix_fcntl.F90 \
45-
src/unix_inet.F90 src/unix_ioctl.F90 src/unix_ipc.F90 src/unix_mqueue.F90 \
46-
src/unix_msg.F90 src/unix_netdb.F90 src/unix_pthread.F90 \
47-
src/unix_regex.F90 src/unix_semaphore.F90 src/unix_signal.F90 \
48-
src/unix_socket.F90 src/unix_stat.F90 src/unix_stdio.F90 \
49-
src/unix_stdlib.F90 src/unix_string.F90 src/unix_syslog.F90 \
50-
src/unix_termios.F90 src/unix_time.F90 src/unix_types.F90 \
51-
src/unix_unistd.F90 src/unix_utsname.F90 src/unix_wait.F90
45+
src/unix_ftw.F90 src/unix_inet.F90 src/unix_ioctl.F90 src/unix_ipc.F90 \
46+
src/unix_mqueue.F90 src/unix_msg.F90 src/unix_netdb.F90 \
47+
src/unix_pthread.F90 src/unix_regex.F90 src/unix_semaphore.F90 \
48+
src/unix_signal.F90 src/unix_socket.F90 src/unix_stat.F90 \
49+
src/unix_stdio.F90 src/unix_stdlib.F90 src/unix_string.F90 \
50+
src/unix_syslog.F90 src/unix_termios.F90 src/unix_time.F90 \
51+
src/unix_types.F90 src/unix_unistd.F90 src/unix_utsname.F90 \
52+
src/unix_wait.F90
5253

5354
OBJ = unix.o unix_dirent.o unix_errno.o unix_fcntl.o \
5455
unix_inet.o unix_ioctl.o unix_ipc.o unix_mqueue.o unix_msg.o \
@@ -71,6 +72,7 @@ $(TARGET): $(SRC)
7172
$(FC) $(FFLAGS) $(PPFLAGS) -c src/unix_dirent.F90
7273
$(FC) $(FFLAGS) $(PPFLAGS) -c src/unix_errno.F90
7374
$(FC) $(FFLAGS) $(PPFLAGS) -c src/unix_fcntl.F90
75+
$(FC) $(FFLAGS) $(PPFLAGS) -c src/unix_ftw.F90
7476
$(FC) $(FFLAGS) $(PPFLAGS) -c src/unix_inet.F90
7577
$(FC) $(FFLAGS) $(PPFLAGS) -c src/unix_ioctl.F90
7678
$(FC) $(FFLAGS) $(PPFLAGS) -c src/unix_ipc.F90

examples/stat/stat.f90

Lines changed: 21 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -8,9 +8,10 @@ program main
88
implicit none
99

1010
character(len=*), parameter :: FILE_NAME = 'README.md'
11+
character(len=*), parameter :: DIR_PATH = '/bin'
1112

1213
character(len=:), allocatable :: atime, mtime, ctime
13-
integer :: file_type, stat
14+
integer :: file_type, stat, total
1415
integer(kind=c_int64_t) :: file_mode
1516
type(c_stat_type) :: file_stat
1617

@@ -54,4 +55,23 @@ program main
5455
print '("File access time.: ", a24)', atime
5556
print '("File modify time.: ", a24)', mtime
5657
print '("File changed time: ", a24)', ctime
58+
59+
total = 0
60+
stat = c_nftw(DIR_PATH // c_null_char, c_funloc(nftw_callback), 1, 0)
61+
62+
print '("Directory........: ", a)', DIR_PATH
63+
print '("Directory size...: ", i0, " KiB")', total / 1024
64+
contains
65+
integer(kind=c_int) function nftw_callback(path, sb, flag, ftw) bind(c)
66+
type(c_ptr), intent(in), value :: path
67+
type(c_ptr), intent(in), value :: sb
68+
integer(kind=c_int), intent(in), value :: flag
69+
type(c_ptr), intent(in), value :: ftw
70+
71+
type(c_stat_type), pointer :: stat
72+
73+
call c_f_pointer(sb, stat)
74+
total = total + stat%st_size
75+
nftw_callback = 0
76+
end function nftw_callback
5777
end program main

src/unix.f90

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -10,6 +10,7 @@ module unix
1010
use :: unix_dirent
1111
use :: unix_errno
1212
use :: unix_fcntl
13+
use :: unix_ftw
1314
use :: unix_inet
1415
use :: unix_ipc
1516
use :: unix_ioctl

src/unix_ftw.F90

Lines changed: 63 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,63 @@
1+
! unix_ftw.F90
2+
!
3+
! Author: Philipp Engel
4+
! Licence: ISC
5+
module unix_ftw
6+
use, intrinsic :: iso_c_binding
7+
implicit none
8+
private
9+
10+
#if defined (__linux__)
11+
12+
integer(kind=c_int), parameter, public :: FTW_F = 0
13+
integer(kind=c_int), parameter, public :: FTW_D = 1
14+
integer(kind=c_int), parameter, public :: FTW_DNR = 2
15+
integer(kind=c_int), parameter, public :: FTW_NS = 3
16+
integer(kind=c_int), parameter, public :: FTW_SL = 4
17+
integer(kind=c_int), parameter, public :: FTW_DP = 5
18+
integer(kind=c_int), parameter, public :: FTW_SLN = 6
19+
20+
integer(kind=c_int), parameter, public :: FTW_PHYS = 1
21+
integer(kind=c_int), parameter, public :: FTW_MOUNT = 2
22+
integer(kind=c_int), parameter, public :: FTW_CHDIR = 4
23+
integer(kind=c_int), parameter, public :: FTW_DEPTH = 8
24+
integer(kind=c_int), parameter, public :: FTW_ACTIONRETVAL = 16
25+
26+
#elif defined (__FreeBSD__)
27+
28+
integer(kind=c_int), parameter, public :: FTW_F = 0 ! File.
29+
integer(kind=c_int), parameter, public :: FTW_D = 1 ! Directory.
30+
integer(kind=c_int), parameter, public :: FTW_DNR = 2 ! Directory without read permission.
31+
integer(kind=c_int), parameter, public :: FTW_DP = 3 ! Directory with subdirectories visited.
32+
integer(kind=c_int), parameter, public :: FTW_NS = 4 ! Unknown type; stat() failed.
33+
integer(kind=c_int), parameter, public :: FTW_SL = 5 ! Symbolic link.
34+
integer(kind=c_int), parameter, public :: FTW_SLN = 6 ! Sym link that names a nonexistent file.
35+
36+
integer(kind=c_int), parameter, public :: FTW_PHYS = int(z'01') ! Physical walk, don't follow sym links.
37+
integer(kind=c_int), parameter, public :: FTW_MOUNT = int(z'02') ! The walk does not cross a mount point.
38+
integer(kind=c_int), parameter, public :: FTW_DEPTH = int(z'04') ! Subdirs visited before the dir itself.
39+
integer(kind=c_int), parameter, public :: FTW_CHDIR = int(z'08') ! Change to a directory before reading it.
40+
41+
#endif
42+
43+
! struct FTW
44+
type, bind(c), public :: c_ftw_type
45+
integer(kind=c_int) :: base = 0
46+
integer(kind=c_int) :: level = 0
47+
end type c_ftw_type
48+
49+
public :: c_nftw
50+
51+
interface
52+
! int nftw(const char *path, int (*fn)(const char *, const struct stat *, int, struct FTW *), int maxfds, int flags)
53+
function c_nftw(path, fn, maxfds, flags) bind(c, name='ftw')
54+
import :: c_char, c_funptr, c_int
55+
implicit none
56+
character(kind=c_char), intent(in) :: path
57+
type(c_funptr), intent(in), value :: fn
58+
integer(kind=c_int), intent(in), value :: maxfds
59+
integer(kind=c_int), intent(in), value :: flags
60+
integer(kind=c_int) :: c_nftw
61+
end function c_nftw
62+
end interface
63+
end module unix_ftw

0 commit comments

Comments
 (0)