|
|
1.1 ! root 1: /* dirent.c ! 2: Replacements for opendir, readdir and closedir for the original ! 3: Unix filesystem only. ! 4: ! 5: Copyright (C) 1992 Ian Lance Taylor ! 6: ! 7: This file is part of the Taylor UUCP package. ! 8: ! 9: This program is free software; you can redistribute it and/or ! 10: modify it under the terms of the GNU General Public License as ! 11: published by the Free Software Foundation; either version 2 of the ! 12: License, or (at your option) any later version. ! 13: ! 14: This program is distributed in the hope that it will be useful, but ! 15: WITHOUT ANY WARRANTY; without even the implied warranty of ! 16: MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU ! 17: General Public License for more details. ! 18: ! 19: You should have received a copy of the GNU General Public License ! 20: along with this program; if not, write to the Free Software ! 21: Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA. ! 22: ! 23: The author of the program may be contacted at [email protected] or ! 24: c/o Infinity Development Systems, P.O. Box 520, Waltham, MA 02254. ! 25: */ ! 26: ! 27: #include "uucp.h" ! 28: ! 29: #include "sysdep.h" ! 30: ! 31: #include <errno.h> ! 32: ! 33: #if HAVE_FCNTL_H ! 34: #include <fcntl.h> ! 35: #else ! 36: #if HAVE_SYS_FILE_H ! 37: #include <sys/file.h> ! 38: #endif ! 39: #endif ! 40: ! 41: #ifndef O_RDONLY ! 42: #define O_RDONLY 0 ! 43: #endif ! 44: ! 45: #ifndef O_NOCTTY ! 46: #define O_NOCTTY 0 ! 47: #endif ! 48: ! 49: #ifndef FD_CLOEXEC ! 50: #define FD_CLOEXEC 1 ! 51: #endif ! 52: ! 53: /* Simple emulations of opendir/readdir/closedir for systems which ! 54: have the original format of Unix directories. It's probably better ! 55: to get Doug Gwyn's public domain set of emulation functions. */ ! 56: ! 57: DIR * ! 58: opendir (zdir) ! 59: const char *zdir; ! 60: { ! 61: int o; ! 62: struct stat s; ! 63: DIR *qret; ! 64: ! 65: o = open ((char *) zdir, O_RDONLY | O_NOCTTY, 0); ! 66: if (o < 0) ! 67: return NULL; ! 68: if (fcntl (o, F_SETFD, fcntl (o, F_GETFD, 0) | FD_CLOEXEC) < 0 ! 69: || fstat (o, &s) < 0) ! 70: { ! 71: int isave; ! 72: ! 73: isave = errno; ! 74: (void) close (o); ! 75: errno = isave; ! 76: return NULL; ! 77: } ! 78: if (! S_ISDIR (s.st_mode)) ! 79: { ! 80: (void) close (o); ! 81: errno = ENOTDIR; ! 82: return NULL; ! 83: } ! 84: qret = (DIR *) xmalloc (sizeof (DIR)); ! 85: qret->o = o; ! 86: return qret; ! 87: } ! 88: ! 89: struct dirent * ! 90: readdir (q) ! 91: DIR *q; ! 92: { ! 93: struct direct sdir; ! 94: int cgot; ! 95: ! 96: do ! 97: { ! 98: cgot = read (q->o, &sdir, sizeof (struct direct)); ! 99: if (cgot <= 0) ! 100: return NULL; ! 101: if (cgot != sizeof (struct direct)) ! 102: { ! 103: errno = ENOENT; ! 104: return NULL; ! 105: } ! 106: } ! 107: while (sdir.d_ino == 0); ! 108: ! 109: strncpy (q->s.d_name, sdir.d_name, DIRSIZ); ! 110: q->s.d_name[DIRSIZ] = '\0'; ! 111: return &q->s; ! 112: } ! 113: ! 114: int ! 115: closedir (q) ! 116: DIR *q; ! 117: { ! 118: int o; ! 119: ! 120: o = q->o; ! 121: xfree (q); ! 122: return close (o); ! 123: }
This archive runs on limited infrastructure. Preserving old code on modern bandwidth. Automated agents are requested to crawl responsibly.