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 
00095 #include <sys/device.h>
00096 
00097 #include <stdlib.h>
00098 #include <string.h>
00099 #include <errno.h>
00100 
00101 #include <fs/fs.h>
00102 
00103 #include <dirent.h>
00104 
00109 
00123 DIR *opendir(CONST char *name)
00124 {
00125     DIR *dir = 0;
00126     NUTDEVICE *dev;
00127     char dev_name[9];
00128     u_char nidx;
00129     CONST char *nptr = name;
00130 
00131     /* Extract the device name. */
00132     for (nidx = 0; *nptr && *nptr != ':' && nidx < 8; nidx++) {
00133         dev_name[nidx] = *nptr++;
00134     }
00135     dev_name[nidx] = 0;
00136 
00137     /* Get device structure of registered device. */
00138     if ((dev = NutDeviceLookup(dev_name)) == 0) {
00139         errno = ENODEV;
00140         return 0;
00141     }
00142 
00143     /* Allocate and initialize the stream. */
00144     if ((dir = malloc(sizeof(DIR))) == 0) {
00145         errno = ENOMEM;
00146         return 0;
00147     }
00148     memset(dir, 0, sizeof(DIR));
00149 
00150     /* Allocate and initialize the data buffer. */
00151     if ((dir->dd_len = strlen(name + 1)) < sizeof(struct dirent)) {
00152         dir->dd_len = sizeof(struct dirent);
00153     }
00154     if ((dir->dd_buf = malloc(dir->dd_len)) == 0) {
00155         free(dir);
00156         errno = ENOMEM;
00157         return 0;
00158     }
00159     if (*nptr == ':') {
00160         nptr++;
00161     }
00162     strcpy(dir->dd_buf, nptr);
00163 
00164     /* Call the proper IOCTL. */
00165     if ((*dev->dev_ioctl) (dev, FS_DIR_OPEN, dir)) {
00166         free(dir->dd_buf);
00167         free(dir);
00168         dir = 0;
00169     }
00170     return dir;
00171 }
00172 
00183 int closedir(DIR * dir)
00184 {
00185     NUTDEVICE *dev;
00186 
00187     if (dir) {
00188         dev = dir->dd_fd->nf_dev;
00189         (*dev->dev_ioctl) (dev, FS_DIR_CLOSE, dir);
00190         if (dir->dd_buf) {
00191             free(dir->dd_buf);
00192         }
00193         free(dir);
00194     }
00195     return 0;
00196 }
00197 
00206 struct dirent *readdir(DIR * dir)
00207 {
00208     NUTDEVICE *dev = dir->dd_fd->nf_dev;
00209 
00210     if ((*dev->dev_ioctl) (dev, FS_DIR_READ, dir)) {
00211         return 0;
00212     }
00213     return (struct dirent *) (dir->dd_buf);
00214 }
00215 

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