dirent.c

Go to the documentation of this file.
00001 /*
00002  * Copyright (C) 2004-2005 by egnite Software GmbH. All rights reserved.
00003  *
00004  * Redistribution and use in source and binary forms, with or without
00005  * modification, are permitted provided that the following conditions
00006  * are met:
00007  *
00008  * 1. Redistributions of source code must retain the above copyright
00009  *    notice, this list of conditions and the following disclaimer.
00010  * 2. Redistributions in binary form must reproduce the above copyright
00011  *    notice, this list of conditions and the following disclaimer in the
00012  *    documentation and/or other materials provided with the distribution.
00013  * 3. Neither the name of the copyright holders nor the names of
00014  *    contributors may be used to endorse or promote products derived
00015  *    from this software without specific prior written permission.
00016  *
00017  * THIS SOFTWARE IS PROVIDED BY EGNITE SOFTWARE GMBH AND CONTRIBUTORS
00018  * ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
00019  * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS
00020  * FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL EGNITE
00021  * SOFTWARE GMBH OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT,
00022  * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING,
00023  * BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS
00024  * OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED
00025  * AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY,
00026  * OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF
00027  * THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
00028  * SUCH DAMAGE.
00029  *
00030  * For additional information see http://www.ethernut.de/
00031  *
00032  *-
00033  * Parts are
00034  *
00035  * Copyright (c) 1989, 1993
00036  *      The Regents of the University of California.  All rights reserved.
00037  *
00038  * Redistribution and use in source and binary forms, with or without
00039  * modification, are permitted provided that the following conditions
00040  * are met:
00041  * 1. Redistributions of source code must retain the above copyright
00042  *    notice, this list of conditions and the following disclaimer.
00043  * 2. Redistributions in binary form must reproduce the above copyright
00044  *    notice, this list of conditions and the following disclaimer in the
00045  *    documentation and/or other materials provided with the distribution.
00046  * 3. Neither the name of the University nor the names of its contributors
00047  *    may be used to endorse or promote products derived from this software
00048  *    without specific prior written permission.
00049  *
00050  * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
00051  * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
00052  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
00053  * ARE DISCLAIMED.  IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
00054  * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
00055  * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
00056  * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
00057  * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
00058  * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
00059  * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
00060  * SUCH DAMAGE.
00061  *
00062  */
00063 
00098 #include <sys/device.h>
00099 
00100 #include <stdlib.h>
00101 #include <string.h>
00102 #include <errno.h>
00103 
00104 #include <fs/fs.h>
00105 
00106 #include <dirent.h>
00107 
00112 
00126 DIR *opendir(CONST char *name)
00127 {
00128     DIR *dir = 0;
00129     NUTDEVICE *dev;
00130     char dev_name[9];
00131     uint8_t nidx;
00132     CONST char *nptr = name;
00133 
00134     /* Extract the device name. */
00135     for (nidx = 0; *nptr && *nptr != ':' && nidx < 8; nidx++) {
00136         dev_name[nidx] = *nptr++;
00137     }
00138     dev_name[nidx] = 0;
00139 
00140     /* Get device structure of registered device. */
00141     if ((dev = NutDeviceLookup(dev_name)) == 0) {
00142         errno = ENODEV;
00143         return 0;
00144     }
00145 
00146     /* Allocate and initialize the stream. */
00147     if ((dir = malloc(sizeof(DIR))) == 0) {
00148         errno = ENOMEM;
00149         return 0;
00150     }
00151     memset(dir, 0, sizeof(DIR));
00152 
00153     /* Allocate and initialize the data buffer. */
00154     if ((dir->dd_len = strlen(name + 1)) < sizeof(struct dirent)) {
00155         dir->dd_len = sizeof(struct dirent);
00156     }
00157     if ((dir->dd_buf = malloc(dir->dd_len)) == 0) {
00158         free(dir);
00159         errno = ENOMEM;
00160         return 0;
00161     }
00162     if (*nptr == ':') {
00163         nptr++;
00164     }
00165     strcpy(dir->dd_buf, nptr);
00166 
00167     /* Call the proper IOCTL. */
00168     if ((*dev->dev_ioctl) (dev, FS_DIR_OPEN, dir)) {
00169         free(dir->dd_buf);
00170         free(dir);
00171         dir = 0;
00172     }
00173     return dir;
00174 }
00175 
00186 int closedir(DIR * dir)
00187 {
00188     NUTDEVICE *dev;
00189 
00190     if (dir) {
00191         dev = dir->dd_fd->nf_dev;
00192         (*dev->dev_ioctl) (dev, FS_DIR_CLOSE, dir);
00193         if (dir->dd_buf) {
00194             free(dir->dd_buf);
00195         }
00196         free(dir);
00197     }
00198     return 0;
00199 }
00200 
00209 struct dirent *readdir(DIR * dir)
00210 {
00211     NUTDEVICE *dev = dir->dd_fd->nf_dev;
00212 
00213     if ((*dev->dev_ioctl) (dev, FS_DIR_READ, dir)) {
00214         return 0;
00215     }
00216     return (struct dirent *) (dir->dd_buf);
00217 }
00218 

© 2000-2007 by egnite Software GmbH - visit http://www.ethernut.de/