opendir.c 2.0 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283
  1. /* -*-comment-start: "//";comment-end:""-*-
  2. * GNU Mes --- Maxwell Equations of Software
  3. * Copyright (C) 1991-1996,98,2000,2001 Free Software Foundation, Inc.
  4. * Copyright © 2018 Jan (janneke) Nieuwenhuizen <janneke@gnu.org>
  5. *
  6. * This file is part of GNU Mes.
  7. *
  8. * GNU Mes is free software; you can redistribute it and/or modify it
  9. * under the terms of the GNU General Public License as published by
  10. * the Free Software Foundation; either version 3 of the License, or (at
  11. * your option) any later version.
  12. *
  13. * GNU Mes is distributed in the hope that it will be useful, but
  14. * WITHOUT ANY WARRANTY; without even the implied warranty of
  15. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  16. * GNU General Public License for more details.
  17. *
  18. * You should have received a copy of the GNU General Public License
  19. * along with GNU Mes. If not, see <http://www.gnu.org/licenses/>.
  20. */
  21. // Taken from GNU C Library 2.2.5
  22. #include <mes/lib.h>
  23. #include <errno.h>
  24. #include <limits.h>
  25. #include <stddef.h>
  26. #include <stdlib.h>
  27. #include <dirent.h>
  28. #include <fcntl.h>
  29. #include <sys/types.h>
  30. #include <sys/stat.h>
  31. #include <unistd.h>
  32. #include <stdio.h>
  33. #include <dirstream.h>
  34. /* Open a directory stream on NAME. */
  35. DIR *
  36. opendir (char const *name)
  37. {
  38. DIR *dirp;
  39. struct stat statbuf;
  40. int fd;
  41. size_t allocation;
  42. int save_errno;
  43. if (name[0] == '\0')
  44. {
  45. /* POSIX.1-1990 says an empty name gets ENOENT;
  46. but `open' might like it fine. */
  47. errno = ENOENT;
  48. return 0;
  49. }
  50. fd = open (name, O_RDONLY | O_DIRECTORY);
  51. if (fd < 0)
  52. return 0;
  53. if (fstat (fd, &statbuf) < 0)
  54. goto lose;
  55. if (fcntl (fd, F_SETFD, FD_CLOEXEC) < 0)
  56. goto lose;
  57. allocation = statbuf.st_blksize;
  58. dirp = (DIR *) calloc (1, sizeof (DIR) + allocation);
  59. if (!dirp)
  60. lose:
  61. {
  62. save_errno = errno;
  63. close (fd);
  64. errno = save_errno;
  65. return 0;
  66. }
  67. dirp->data = (char *) (dirp + 1);
  68. dirp->allocation = allocation;
  69. dirp->fd = fd;
  70. return dirp;
  71. }