Empty directory: Difference between revisions

(R language)
(insert →‎Pascal)
Line 924:
 
dir_is_empty("/tmp"): 0</pre>
 
=={{header|Pascal}}==
{{works with|GNU Pascal}}
Pascal does not know the notion of directories.
Therefore it is ''not possible'' to determine whether a directory is empty.
 
However, ''GNU Pascal'' provides certain extensions to [[Extended Pascal]] (ISO 10206) so it is possible nonetheless.
Note, the following solution works only on ''strictly hierarchical'' file systems.
<lang pascal>program emptyDirectory(input, output);
 
type
path = string(1024);
 
{
\brief determines whether a (hierarchial FS) directory is empty
\param accessVia a possible route to access a directory
\return whether \param accessVia is an empty directory
}
function isEmptyDirectory(protected accessVia: path): Boolean;
var
{ NB: `file` data types without a domain type are non-standard }
directory: bindable file;
FD: bindingType;
begin
{ initialize variables }
unbind(directory);
FD := binding(directory);
FD.name := accessVia;
{ binding to directories is usually not possible }
FD.force := true;
{ the actual test }
bind(directory, FD);
FD := binding(directory);
unbind(directory);
isEmptyDirectory := FD.bound and FD.directory and (FD.links <= 2)
end;
 
{ === MAIN ============================================================= }
var
s: path;
begin
readLn(s);
writeLn(isEmptyDirectory(s))
end.</lang>
Note, symbolic links’ targets are resolved, so <tt>accessVia</tt> could in fact be a symbolic link ''to'' an empty directory.
 
=={{header|Perl}}==
149

edits