Back to index

glibc  2.9
opendir.c
Go to the documentation of this file.
00001 /* Copyright (C) 1993,1994,1995,1996,1997,1998,2001,2003,2005,2006
00002        Free Software Foundation, Inc.
00003    This file is part of the GNU C Library.
00004 
00005    The GNU C Library is free software; you can redistribute it and/or
00006    modify it under the terms of the GNU Lesser General Public
00007    License as published by the Free Software Foundation; either
00008    version 2.1 of the License, or (at your option) any later version.
00009 
00010    The GNU C Library is distributed in the hope that it will be useful,
00011    but WITHOUT ANY WARRANTY; without even the implied warranty of
00012    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
00013    Lesser General Public License for more details.
00014 
00015    You should have received a copy of the GNU Lesser General Public
00016    License along with the GNU C Library; if not, write to the Free
00017    Software Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA
00018    02111-1307 USA.  */
00019 
00020 #include <errno.h>
00021 #include <limits.h>
00022 #include <stddef.h>
00023 #include <stdlib.h>
00024 #include <string.h>
00025 #include <dirent.h>
00026 #include <fcntl.h>
00027 #include <sys/types.h>
00028 #include <sys/stat.h>
00029 #include <unistd.h>
00030 #include <stdio.h>
00031 #include <hurd.h>
00032 #include <hurd/fd.h>
00033 #include "dirstream.h"
00034 
00035 
00036 /* Open a directory stream on a file descriptor in Hurd internal form.
00037    We do no checking here on the descriptor.  */
00038 DIR *
00039 _hurd_fd_opendir (struct hurd_fd *d)
00040 {
00041   DIR *dirp;
00042 
00043   if (d == NULL)
00044     {
00045       errno = EBADF;
00046       return NULL;
00047     }
00048 
00049   dirp = (DIR *) malloc (sizeof (DIR));
00050   if (dirp == NULL)
00051     return NULL;
00052 
00053   /* Set the descriptor to close on exec. */
00054   __spin_lock (&d->port.lock);
00055   d->flags |= FD_CLOEXEC;
00056   __spin_unlock (&d->port.lock);
00057 
00058   dirp->__fd = d;
00059   dirp->__data = dirp->__ptr = NULL;
00060   dirp->__entry_data = dirp->__entry_ptr = 0;
00061   dirp->__allocation = 0;
00062   dirp->__size = 0;
00063 
00064   __libc_lock_init (dirp->__lock);
00065 
00066   return dirp;
00067 }
00068 
00069 
00070 /* Open a directory stream on NAME.  */
00071 DIR *
00072 __opendir (const char *name)
00073 {
00074   if (name[0] == '\0')
00075     {
00076       /* POSIX.1-1990 says an empty name gets ENOENT;
00077         but `open' might like it fine.  */
00078       __set_errno (ENOENT);
00079       return NULL;
00080     }
00081 
00082   int fd = __open (name, O_RDONLY | O_NONBLOCK | O_DIRECTORY);
00083   if (fd < 0)
00084     return NULL;
00085 
00086   /* Extract the pointer to the descriptor structure.  */
00087   DIR *dirp = _hurd_fd_opendir (_hurd_fd_get (fd));
00088   if (dirp == NULL)
00089     __close (fd);
00090 
00091   return dirp;
00092 }
00093 weak_alias (__opendir, opendir)