* Do not define _BSD_SOURCE but define _DEFAULT_SOURCE instead.
[alpine.git] / imap / src / osdep / unix / opendir.c
blob445fd148385a79c5c3d0511253788365ab0000df
1 /* ========================================================================
2 * Copyright 1988-2006 University of Washington
4 * Licensed under the Apache License, Version 2.0 (the "License");
5 * you may not use this file except in compliance with the License.
6 * You may obtain a copy of the License at
8 * http://www.apache.org/licenses/LICENSE-2.0
11 * ========================================================================
15 * Program: Read directories
17 * Author: Mark Crispin
18 * Networks and Distributed Computing
19 * Computing & Communications
20 * University of Washington
21 * Administration Building, AG-44
22 * Seattle, WA 98195
23 * Internet: MRC@CAC.Washington.EDU
25 * Date: 16 December 1993
26 * Last Edited: 30 August 2006
29 /* Emulator for BSD opendir() call
30 * Accepts: directory name
31 * Returns: directory structure pointer
34 DIR *opendir (char *name)
36 DIR *d = NIL;
37 struct stat sbuf;
38 int fd = open (name,O_RDONLY,NIL);
39 errno = ENOTDIR; /* default error is bogus directory */
40 if ((fd >= 0) && !(fstat (fd,&sbuf)) && ((sbuf.st_mode&S_IFMT) == S_IFDIR)) {
41 d = (DIR *) fs_get (sizeof (DIR));
42 /* initialize structure */
43 d->dd_loc = 0;
44 read (fd,d->dd_buf = (char *) fs_get (sbuf.st_size),
45 d->dd_size = sbuf.st_size);
47 else if (d) fs_give ((void **) &d);
48 if (fd >= 0) close (fd);
49 return d;
53 /* Emulator for BSD closedir() call
54 * Accepts: directory structure pointer
57 int closedir (DIR *d)
59 /* free storage */
60 fs_give ((void **) &(d->dd_buf));
61 fs_give ((void **) &d);
62 return NIL; /* return */
66 /* Emulator for BSD readdir() call
67 * Accepts: directory structure pointer
70 struct direct *readdir (DIR *d)
72 /* loop through directory */
73 while (d->dd_loc < d->dd_size) {
74 struct direct *dp = (struct direct *) (d->dd_buf + d->dd_loc);
75 d->dd_loc += sizeof (struct direct);
76 if (dp->d_ino) return dp; /* if have a good entry return it */
78 return NIL; /* all done */