backport
[AROS.git] / compiler / clib / closedir.c
blobaca26c73b38b5b45ffb3506fc69e225dbeb5812b
1 /*
2 Copyright © 1995-2009, The AROS Development Team. All rights reserved.
3 $Id$
5 POSIX function closedir().
6 */
8 #include <proto/dos.h>
9 #include <proto/exec.h>
10 #include <stdlib.h>
11 #include <unistd.h>
12 #include <errno.h>
14 #include "__fdesc.h"
15 #include "__dirdesc.h"
17 /*****************************************************************************
19 NAME */
20 #include <dirent.h>
22 int closedir(
24 /* SYNOPSIS */
25 DIR *dir)
27 /* FUNCTION
28 Closes a directory
30 INPUTS
31 dir - the directory stream pointing to the directory being closed
33 RESULT
34 The closedir() function returns 0 on success or -1 on
35 failure.
37 NOTES
39 EXAMPLE
41 BUGS
43 SEE ALSO
44 close(), opendir(), readdir(), rewinddir(), seekdir(),
45 telldir(), scandir()
47 INTERNALS
49 ******************************************************************************/
51 fdesc *desc;
53 if (!dir)
55 errno = EFAULT;
56 return -1;
59 desc = __getfdesc(dir->fd);
60 if (!desc)
62 errno = EBADF;
63 return -1;
66 if (--desc->fcb->opencount == 0)
68 UnLock(desc->fcb->fh);
69 FreeVec(desc->fcb);
71 __free_fdesc(desc);
72 __setfdesc(dir->fd, NULL);
74 FreeDosObject(DOS_FIB, dir->priv);
75 free(dir);
77 return 0;