getcwd.c 3.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135
  1. /* Determine current working directory. Linux version.
  2. Copyright (C) 1997-2019 Free Software Foundation, Inc.
  3. This file is part of the GNU C Library.
  4. Contributed by Ulrich Drepper <drepper@cygnus.com>, 1997.
  5. The GNU C Library is free software; you can redistribute it and/or
  6. modify it under the terms of the GNU Lesser General Public
  7. License as published by the Free Software Foundation; either
  8. version 2.1 of the License, or (at your option) any later version.
  9. The GNU C Library is distributed in the hope that it will be useful,
  10. but WITHOUT ANY WARRANTY; without even the implied warranty of
  11. MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
  12. Lesser General Public License for more details.
  13. You should have received a copy of the GNU Lesser General Public
  14. License along with the GNU C Library; if not, see
  15. <http://www.gnu.org/licenses/>. */
  16. #include <assert.h>
  17. #include <errno.h>
  18. #include <limits.h>
  19. #include <stdlib.h>
  20. #include <unistd.h>
  21. #include <sys/param.h>
  22. #include <sysdep.h>
  23. #include <sys/syscall.h>
  24. /* If we compile the file for use in ld.so we don't need the feature
  25. that getcwd() allocates the buffers itself. */
  26. #if IS_IN (rtld)
  27. # define NO_ALLOCATION 1
  28. #endif
  29. /* The "proc" filesystem provides an easy method to retrieve the value.
  30. For each process, the corresponding directory contains a symbolic link
  31. named `cwd'. Reading the content of this link immediate gives us the
  32. information. But we have to take care for systems which do not have
  33. the proc filesystem mounted. Use the POSIX implementation in this case. */
  34. static char *generic_getcwd (char *buf, size_t size);
  35. char *
  36. __getcwd (char *buf, size_t size)
  37. {
  38. char *path;
  39. char *result;
  40. #ifndef NO_ALLOCATION
  41. size_t alloc_size = size;
  42. if (size == 0)
  43. {
  44. if (buf != NULL)
  45. {
  46. __set_errno (EINVAL);
  47. return NULL;
  48. }
  49. alloc_size = MAX (PATH_MAX, __getpagesize ());
  50. }
  51. if (buf == NULL)
  52. {
  53. path = malloc (alloc_size);
  54. if (path == NULL)
  55. return NULL;
  56. }
  57. else
  58. #else
  59. # define alloc_size size
  60. #endif
  61. path = buf;
  62. int retval;
  63. retval = INLINE_SYSCALL (getcwd, 2, path, alloc_size);
  64. if (retval > 0 && path[0] == '/')
  65. {
  66. #ifndef NO_ALLOCATION
  67. if (buf == NULL && size == 0)
  68. /* Ensure that the buffer is only as large as necessary. */
  69. buf = realloc (path, (size_t) retval);
  70. if (buf == NULL)
  71. /* Either buf was NULL all along, or `realloc' failed but
  72. we still have the original string. */
  73. buf = path;
  74. #endif
  75. return buf;
  76. }
  77. /* The system call either cannot handle paths longer than a page
  78. or can succeed without returning an absolute path. Just use the
  79. generic implementation right away. */
  80. if (retval >= 0 || errno == ENAMETOOLONG)
  81. {
  82. #ifndef NO_ALLOCATION
  83. if (buf == NULL && size == 0)
  84. {
  85. free (path);
  86. path = NULL;
  87. }
  88. #endif
  89. result = generic_getcwd (path, size);
  90. #ifndef NO_ALLOCATION
  91. if (result == NULL && buf == NULL && size != 0)
  92. free (path);
  93. #endif
  94. return result;
  95. }
  96. /* It should never happen that the `getcwd' syscall failed because
  97. the buffer is too small if we allocated the buffer ourselves
  98. large enough. */
  99. assert (errno != ERANGE || buf != NULL || size != 0);
  100. #ifndef NO_ALLOCATION
  101. if (buf == NULL)
  102. free (path);
  103. #endif
  104. return NULL;
  105. }
  106. weak_alias (__getcwd, getcwd)
  107. /* Get the code for the generic version. */
  108. #define GETCWD_RETURN_TYPE static char *
  109. #define __getcwd generic_getcwd
  110. #include <sysdeps/posix/getcwd.c>