Annotation of /trunk/mkinitrd-magellan/klibc/usr/klibc/readdir.c
Parent Directory | Revision Log
Revision 532 -
(hide annotations)
(download)
Sat Sep 1 22:45:15 2007 UTC (16 years, 9 months ago) by niro
File MIME type: text/plain
File size: 856 byte(s)
Sat Sep 1 22:45:15 2007 UTC (16 years, 9 months ago) by niro
File MIME type: text/plain
File size: 856 byte(s)
-import if magellan mkinitrd; it is a fork of redhats mkinitrd-5.0.8 with all magellan patches and features; deprecates magellan-src/mkinitrd
1 | niro | 532 | /* |
2 | * readdir.c: opendir/readdir/closedir | ||
3 | */ | ||
4 | |||
5 | #include <unistd.h> | ||
6 | #include <fcntl.h> | ||
7 | #include <stdlib.h> | ||
8 | |||
9 | #define __KLIBC_DIRENT_INTERNALS | ||
10 | #include <dirent.h> | ||
11 | |||
12 | DIR *opendir(const char *name) | ||
13 | { | ||
14 | DIR *dp = malloc(sizeof(DIR)); | ||
15 | |||
16 | if (!dp) | ||
17 | return NULL; | ||
18 | |||
19 | dp->__fd = open(name, O_DIRECTORY | O_RDONLY); | ||
20 | |||
21 | if (dp->__fd < 0) { | ||
22 | free(dp); | ||
23 | return NULL; | ||
24 | } | ||
25 | |||
26 | dp->bytes_left = 0; | ||
27 | |||
28 | return dp; | ||
29 | } | ||
30 | |||
31 | struct dirent *readdir(DIR *dir) | ||
32 | { | ||
33 | struct dirent *dent; | ||
34 | int rv; | ||
35 | |||
36 | if (!dir->bytes_left) { | ||
37 | rv = getdents(dir->__fd, dir->buffer, sizeof(dir->buffer)); | ||
38 | if (rv <= 0) | ||
39 | return NULL; | ||
40 | dir->bytes_left = rv; | ||
41 | dir->next = dir->buffer; | ||
42 | } | ||
43 | |||
44 | dent = dir->next; | ||
45 | dir->next = (struct dirent *)((char *)dir->next + dent->d_reclen); | ||
46 | dir->bytes_left -= dent->d_reclen; | ||
47 | |||
48 | return dent; | ||
49 | } | ||
50 | |||
51 | int closedir(DIR *dir) | ||
52 | { | ||
53 | int rv; | ||
54 | rv = close(dir->__fd); | ||
55 | free(dir); | ||
56 | return rv; | ||
57 | } |