Starting with a path to some directory, determine whether the directory is empty.

An empty directory contains no files nor subdirectories.
With Unix or Windows systems, every directory contains an entry for “.” and almost every directory contains “..” (except for a root directory); an empty directory contains no other entries.

procedure main()
   every dir := "." | "./empty" do 
      write(dir, if isdirempty(dir) then " is empty" else " is not empty")
end
 
procedure isdirempty(s)  #: succeeds if directory s is empty (and a directory)
local d,f
   if ( stat(s).mode ? ="d" ) & ( d := open(s) ) then {
         while f := read(d) do 
            if f == ("."|"..") then next else fail 
         close(d)
         return s
         }
   else stop(s," is not a directory or will not open")
end