[comp.unix.questions] finding out if a directory is empty in C program

jik@athena.mit.edu (Jonathan I. Kamens) (04/26/91)

In article <1991Apr25.022029.5476@csc.canberra.edu.au>, rvp@softserver.canberra.edu.au (Rey Paulo) writes:
|> Is there any system call or library routine in UNIX which tests whether
|> a directory is empty?

No.  You open the directory with opendir() and read it, and if the only
entries in it are "." and "..", it's empty.  Just like in a shell script
(except the script uses ls, or shell wildcard expansion, instead of opendir()).

And if you have a system that doesn't have opendir() or something like it,
instead forcing you to open and read the directory file directly, then you
should find your vendor and shoot him.

-- 
Jonathan Kamens			              USnail:
MIT Project Athena				11 Ashford Terrace
jik@Athena.MIT.EDU				Allston, MA  02134
Office: 617-253-8085			      Home: 617-782-0710

Disclaimer: If you do find your vendor and shoot him, I'm not responsible. :-)

jtc@motcad.portal.com (J.T. Conklin) (04/26/91)

In article <1991Apr25.223540.19303@athena.mit.edu> jik@athena.mit.edu (Jonathan I. Kamens) writes:
>And if you have a system that doesn't have opendir() or something like it,
>instead forcing you to open and read the directory file directly, then you
>should find your vendor and shoot him.

And then obtain a copy of Doug Gwyn's dirent library and use it instead
of opening and reading the directory directly.  

In my opinion it is far easier to maintain a "portability library" of
routines that standardize non-standard machines than it is to maintain
baseline code with twisty mazes of #ifdefs.

-- 
J.T. Conklin    jtc@motcad.portal.com, ...!portal!motcad!jtc

chris@visionware.co.uk (Chris Davies) (05/03/91)

>In article <1991Apr25.022029.5476@csc.canberra.edu.au>, rvp@softserver.canberra.edu.au (Rey Paulo) writes:
>|> Is there any system call or library routine in UNIX which tests whether
>|> a directory is empty?

In article <1991Apr25.223540.19303@athena.mit.edu> jik@athena.mit.edu (Jonathan I. Kamens) writes:
>No.  You open the directory with opendir() and read it, and if the only
>entries in it are "." and "..", it's empty.  Just like in a shell script
>(except the script uses ls, or shell wildcard expansion, instead of opendir()).

Why couldn't you use stat() and check whether the number of links == 2 ?
Most of the time it's not possible (well, not recommended) for _users_ to
hard-link to a directory, so the link-count wouldn't be wrong.

Chris
-- 
         VISIONWARE LTD, 57 Cardigan Lane, LEEDS LS4 2LE, England
    Tel +44 532 788858.  Fax +44 532 304676.  Email chris@visionware.co.uk
-------------- "VisionWare:   The home of DOS/UNIX/X integration" -------------

jik@athena.mit.edu (Jonathan I. Kamens) (05/07/91)

In article <1991May3.155817.1807@visionware.co.uk>, chris@visionware.co.uk (Chris Davies) writes:
|> >In article <1991Apr25.022029.5476@csc.canberra.edu.au>, rvp@softserver.canberra.edu.au (Rey Paulo) writes:
|> >|> Is there any system call or library routine in UNIX which tests whether
|> >|> a directory is empty?
|> In article <1991Apr25.223540.19303@athena.mit.edu> jik@athena.mit.edu (Jonathan I. Kamens) writes:
|> >No.  You open the directory with opendir() and read it, and if the only
|> >entries in it are "." and "..", it's empty.  Just like in a shell script
|> >(except the script uses ls, or shell wildcard expansion, instead of opendir()).
|> Why couldn't you use stat() and check whether the number of links == 2 ?
|> Most of the time it's not possible (well, not recommended) for _users_ to
|> hard-link to a directory, so the link-count wouldn't be wrong.

  First of all, only subdirectoris change the link count of a directory.  So
stat()ing the directory and checking its link count will detect only whether
or not it has subdirectories, not whether or not it is empty.

  Second, stat()ing and checking the link count to find out the number of
subdirectories is a hack that is not guaranteed to work on every system and
that is not required to work according to POSIX (at least, I don't think it
is, although I am interested in being corrected).   Operating Systems that
don't really put entries for "." and ".." in a directory (e.g. HP-UX, I
believe) will not have the link counts you're expecting.

  If you want to hard-code the special case operating systems in order to use
the stat() trick to check for subdirectories, you can, but the more reliable
way to do it is opening and reading the directory to see what's in it, and
ignoring "." and ".." if they show up.

-- 
Jonathan Kamens			              USnail:
MIT Project Athena				11 Ashford Terrace
jik@Athena.MIT.EDU				Allston, MA  02134
Office: 617-253-8085			      Home: 617-782-0710

decot@hpcupt1.cup.hp.com (Dave Decot) (05/14/91)

>   Second, stat()ing and checking the link count to find out the number of
> subdirectories is a hack that is not guaranteed to work on every system and
> that is not required to work according to POSIX (at least, I don't think it
> is, although I am interested in being corrected).   Operating Systems that
> don't really put entries for "." and ".." in a directory (e.g. HP-UX, I
> believe) will not have the link counts you're expecting.

This is a true characteristic of POSIX, but HP-UX is not an example
of systems that do not have actual entries for "." and "..".  Only
the HP 9000 Series 500 used a directory format (called SDF) in which
this was not true, and it was obsoleted several years ago.  Even the
s500 would fake the existence of these directories if they appeared in a
path name, but readdir() and ls(1) would never say that they existed.

All modern HP-UX systems have actual entries for "." and ".." .

I agree with the poster that portable code should not assume that
"." and ".." exist everywhere.

Dave Decot