1 /* $OpenBSD: readdir.c,v 1.4 1997/07/21 15:43:54 mickey Exp $ */ 2 3 /* 4 * Copyright (c) 1996 Michael Shalayeff 5 * All rights reserved. 6 * 7 * Redistribution and use in source and binary forms, with or without 8 * modification, are permitted provided that the following conditions 9 * are met: 10 * 1. Redistributions of source code must retain the above copyright 11 * notice, this list of conditions and the following disclaimer. 12 * 2. Redistributions in binary form must reproduce the above copyright 13 * notice, this list of conditions and the following disclaimer in the 14 * documentation and/or other materials provided with the distribution. 15 * 3. All advertising materials mentioning features or use of this software 16 * must display the following acknowledgement: 17 * This product includes software developed by Michael Shalayeff. 18 * 4. The name of the author may not be used to endorse or promote products 19 * derived from this software without specific prior written permission. 20 * 21 * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR 22 * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED 23 * WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE 24 * ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE 25 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL 26 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS 27 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) 28 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT 29 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY 30 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF 31 * SUCH DAMAGE. 32 * 33 */ 34 35 #ifndef NO_READDIR 36 37 #include <sys/types.h> 38 #include <sys/stat.h> 39 #define _KERNEL 40 #include <sys/fcntl.h> 41 #undef _KERNEL 42 #include "stand.h" 43 44 45 int 46 opendir(name) 47 char *name; 48 { 49 int fd; 50 struct stat sb; 51 52 if (stat(name, &sb) < 0) 53 return -1; 54 55 if (!S_ISDIR(sb.st_mode)) { 56 errno = ENOTDIR; 57 return -1; 58 } 59 60 /* XXX rewind needed for some dirs */ 61 #ifdef __INTERNAL_LIBSA_CREAD 62 if ((fd = oopen(name, O_RDONLY)) >= 0) 63 olseek(fd, 0, 0); 64 #else 65 if ((fd = open(name, O_RDONLY)) >= 0) 66 lseek(fd, 0, 0); 67 #endif 68 69 return fd; 70 } 71 72 int 73 readdir(fd, dest) 74 int fd; 75 char *dest; 76 { 77 register struct open_file *f = &files[fd]; 78 79 if (fd < 0 || fd >= SOPEN_MAX || 80 !((f = &files[fd])->f_flags & F_READ)) { 81 errno = EBADF; 82 return (-1); 83 } 84 85 if (f->f_flags & F_RAW) { 86 errno = EOPNOTSUPP; 87 return (-1); 88 } 89 if ((errno = (f->f_ops->readdir)(f, dest))) 90 return (-1); 91 92 return 0; 93 } 94 95 void 96 closedir(fd) 97 int fd; 98 { 99 #ifdef __INTERNAL_LIBSA_CREAD 100 oclose(fd); 101 #else 102 close(fd); 103 #endif 104 } 105 106 #endif /* NO_READDIR */ 107