[d4067a7] | 1 | /*
|
---|
| 2 | * Copyright (c) 2008 Jakub Jermar
|
---|
| 3 | * All rights reserved.
|
---|
| 4 | *
|
---|
| 5 | * Redistribution and use in source and binary forms, with or without
|
---|
| 6 | * modification, are permitted provided that the following conditions
|
---|
| 7 | * are met:
|
---|
| 8 | *
|
---|
| 9 | * - Redistributions of source code must retain the above copyright
|
---|
| 10 | * notice, this list of conditions and the following disclaimer.
|
---|
| 11 | * - Redistributions in binary form must reproduce the above copyright
|
---|
| 12 | * notice, this list of conditions and the following disclaimer in the
|
---|
| 13 | * documentation and/or other materials provided with the distribution.
|
---|
| 14 | * - The name of the author may not be used to endorse or promote products
|
---|
| 15 | * derived from this software without specific prior written permission.
|
---|
| 16 | *
|
---|
| 17 | * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR
|
---|
| 18 | * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
|
---|
| 19 | * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
|
---|
| 20 | * IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT,
|
---|
| 21 | * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
|
---|
| 22 | * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
|
---|
| 23 | * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
|
---|
| 24 | * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
|
---|
| 25 | * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF
|
---|
| 26 | * THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
|
---|
| 27 | */
|
---|
| 28 |
|
---|
| 29 | /** @addtogroup libc
|
---|
| 30 | * @{
|
---|
| 31 | */
|
---|
| 32 | /** @file
|
---|
| 33 | */
|
---|
| 34 |
|
---|
| 35 | #include <vfs/vfs.h>
|
---|
| 36 | #include <stdlib.h>
|
---|
| 37 | #include <dirent.h>
|
---|
[8d2dd7f2] | 38 | #include <stddef.h>
|
---|
[d4067a7] | 39 | #include <errno.h>
|
---|
| 40 | #include <assert.h>
|
---|
| 41 |
|
---|
| 42 | /** Open directory.
|
---|
| 43 | *
|
---|
| 44 | * @param dirname Directory pathname
|
---|
| 45 | *
|
---|
| 46 | * @return Non-NULL pointer on success. On error returns @c NULL and sets errno.
|
---|
| 47 | */
|
---|
| 48 | DIR *opendir(const char *dirname)
|
---|
| 49 | {
|
---|
| 50 | DIR *dirp = malloc(sizeof(DIR));
|
---|
| 51 | if (!dirp) {
|
---|
| 52 | errno = ENOMEM;
|
---|
| 53 | return NULL;
|
---|
| 54 | }
|
---|
[a35b458] | 55 |
|
---|
[f77c1c9] | 56 | int fd;
|
---|
[b7fd2a0] | 57 | errno_t rc = vfs_lookup(dirname, WALK_DIRECTORY, &fd);
|
---|
[f77c1c9] | 58 | if (rc != EOK) {
|
---|
[d4067a7] | 59 | free(dirp);
|
---|
[f77c1c9] | 60 | errno = rc;
|
---|
[d4067a7] | 61 | return NULL;
|
---|
| 62 | }
|
---|
[a35b458] | 63 |
|
---|
[f77c1c9] | 64 | rc = vfs_open(fd, MODE_READ);
|
---|
| 65 | if (rc != EOK) {
|
---|
[d4067a7] | 66 | free(dirp);
|
---|
| 67 | vfs_put(fd);
|
---|
| 68 | errno = rc;
|
---|
| 69 | return NULL;
|
---|
| 70 | }
|
---|
[a35b458] | 71 |
|
---|
[d4067a7] | 72 | dirp->fd = fd;
|
---|
| 73 | dirp->pos = 0;
|
---|
| 74 | return dirp;
|
---|
| 75 | }
|
---|
| 76 |
|
---|
| 77 | /** Read directory entry.
|
---|
| 78 | *
|
---|
| 79 | * @param dirp Open directory
|
---|
| 80 | * @return Non-NULL pointer to directory entry on success. On error returns
|
---|
| 81 | * @c NULL and sets errno.
|
---|
| 82 | */
|
---|
| 83 | struct dirent *readdir(DIR *dirp)
|
---|
| 84 | {
|
---|
[b7fd2a0] | 85 | errno_t rc;
|
---|
[d4067a7] | 86 | ssize_t len = 0;
|
---|
[a35b458] | 87 |
|
---|
[d4067a7] | 88 | rc = vfs_read_short(dirp->fd, dirp->pos, &dirp->res.d_name[0],
|
---|
| 89 | NAME_MAX + 1, &len);
|
---|
| 90 | if (rc != EOK) {
|
---|
| 91 | errno = rc;
|
---|
| 92 | return NULL;
|
---|
| 93 | }
|
---|
[a35b458] | 94 |
|
---|
[d4067a7] | 95 | dirp->pos += len;
|
---|
[a35b458] | 96 |
|
---|
[d4067a7] | 97 | return &dirp->res;
|
---|
| 98 | }
|
---|
| 99 |
|
---|
| 100 | /** Rewind directory position to the beginning.
|
---|
| 101 | *
|
---|
| 102 | * @param dirp Open directory
|
---|
| 103 | */
|
---|
| 104 | void rewinddir(DIR *dirp)
|
---|
| 105 | {
|
---|
| 106 | dirp->pos = 0;
|
---|
| 107 | }
|
---|
| 108 |
|
---|
| 109 | /** Close directory.
|
---|
| 110 | *
|
---|
| 111 | * @param dirp Open directory
|
---|
| 112 | * @return 0 on success. On error returns -1 and sets errno.
|
---|
| 113 | */
|
---|
| 114 | int closedir(DIR *dirp)
|
---|
| 115 | {
|
---|
[b7fd2a0] | 116 | errno_t rc = vfs_put(dirp->fd);
|
---|
[d4067a7] | 117 | free(dirp);
|
---|
| 118 |
|
---|
[f04b5b3] | 119 | if (rc == EOK) {
|
---|
| 120 | return 0;
|
---|
| 121 | } else {
|
---|
| 122 | errno = rc;
|
---|
| 123 | return -1;
|
---|
| 124 | }
|
---|
[d4067a7] | 125 | }
|
---|
| 126 |
|
---|
| 127 | /** @}
|
---|
| 128 | */
|
---|