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 00101 #include <sys/device.h> 00102 00103 #include <stdlib.h> 00104 #include <string.h> 00105 #include <errno.h> 00106 #include <memdebug.h> 00107 00108 #include <fs/fs.h> 00109 00110 #include <dirent.h> 00111 00116 00130 DIR *opendir(CONST char *name) 00131 { 00132 DIR *dir = 0; 00133 NUTDEVICE *dev; 00134 char dev_name[9]; 00135 uint8_t nidx; 00136 CONST char *nptr = name; 00137 00138 /* Extract the device name. */ 00139 for (nidx = 0; *nptr && *nptr != ':' && nidx < 8; nidx++) { 00140 dev_name[nidx] = *nptr++; 00141 } 00142 dev_name[nidx] = 0; 00143 00144 /* Get device structure of registered device. */ 00145 if ((dev = NutDeviceLookup(dev_name)) == 0) { 00146 errno = ENODEV; 00147 return 0; 00148 } 00149 00150 /* Allocate and initialize the stream. */ 00151 if ((dir = malloc(sizeof(DIR))) == 0) { 00152 errno = ENOMEM; 00153 return 0; 00154 } 00155 memset(dir, 0, sizeof(DIR)); 00156 00157 /* Allocate and initialize the data buffer. */ 00158 if ((dir->dd_len = strlen(name + 1)) < sizeof(struct dirent)) { 00159 dir->dd_len = sizeof(struct dirent); 00160 } 00161 if ((dir->dd_buf = malloc(dir->dd_len)) == 0) { 00162 free(dir); 00163 errno = ENOMEM; 00164 return 0; 00165 } 00166 if (*nptr == ':') { 00167 nptr++; 00168 } 00169 strcpy(dir->dd_buf, nptr); 00170 00171 /* Call the proper IOCTL. */ 00172 if ((*dev->dev_ioctl) (dev, FS_DIR_OPEN, dir)) { 00173 free(dir->dd_buf); 00174 free(dir); 00175 dir = 0; 00176 } 00177 return dir; 00178 } 00179 00190 int closedir(DIR * dir) 00191 { 00192 NUTDEVICE *dev; 00193 00194 if (dir) { 00195 dev = dir->dd_fd->nf_dev; 00196 (*dev->dev_ioctl) (dev, FS_DIR_CLOSE, dir); 00197 if (dir->dd_buf) { 00198 free(dir->dd_buf); 00199 } 00200 free(dir); 00201 } 00202 return 0; 00203 } 00204 00213 struct dirent *readdir(DIR * dir) 00214 { 00215 NUTDEVICE *dev = dir->dd_fd->nf_dev; 00216 00217 if ((*dev->dev_ioctl) (dev, FS_DIR_READ, dir)) { 00218 return 0; 00219 } 00220 return (struct dirent *) (dir->dd_buf); 00221 } 00222