summaryrefslogtreecommitdiff
path: root/tests/getcwd-lgpl.c
blob: 67ed2878e6e0d574910d5004d3e21f1feb37bd08 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
/* Copyright (C) 2011-2024 Free Software Foundation, Inc.
   This file is part of gnulib.

   This file is free software: you can redistribute it and/or modify
   it under the terms of the GNU Lesser General Public License as
   published by the Free Software Foundation; either version 2.1 of the
   License, or (at your option) any later version.

   This file is distributed in the hope that it will be useful,
   but WITHOUT ANY WARRANTY; without even the implied warranty of
   MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
   GNU Lesser General Public License for more details.

   You should have received a copy of the GNU Lesser General Public License
   along with this program.  If not, see <https://www.gnu.org/licenses/>.  */

#include <config.h>

/* Specification */
#include <unistd.h>

#include <errno.h>
#include <stdlib.h>
#include <string.h>

#if GNULIB_GETCWD
/* Favor GPL getcwd.c if both getcwd and getcwd-lgpl modules are in use.  */
typedef int dummy;
#else

/* Get the name of the current working directory, and put it in SIZE
   bytes of BUF.  Returns NULL if the directory couldn't be determined
   (perhaps because the absolute name was longer than PATH_MAX, or
   because of missing read/search permissions on parent directories)
   or SIZE was too small.  If successful, returns BUF.  If BUF is
   NULL, an array is allocated with 'malloc'; the array is SIZE bytes
   long, unless SIZE == 0, in which case it is as big as
   necessary.  */

# undef getcwd
# if defined _WIN32 && !defined __CYGWIN__
#  define getcwd _getcwd
# endif

char *
rpl_getcwd (char *buf, size_t size)
{
  char *result;

  /* Handle single size operations.  */
  if (buf)
    {
      /* Check SIZE argument.  */
      if (!size)
        {
          errno = EINVAL;
          return NULL;
        }
      return getcwd (buf, size);
    }

  if (size)
    {
      buf = malloc (size);
      if (!buf)
        {
          errno = ENOMEM;
          return NULL;
        }
      result = getcwd (buf, size);
      if (!result)
        free (buf);
      return result;
    }

  /* Flexible sizing requested.  Avoid over-allocation for the common
     case of a name that fits within a 4k page, minus some space for
     local variables, to be sure we don't skip over a guard page.  */
  {
    char tmp[4032];
    size = sizeof tmp;
    char *ptr = getcwd (tmp, size);
    if (ptr)
      {
        result = strdup (ptr);
        if (!result)
          errno = ENOMEM;
        return result;
      }
    if (errno != ERANGE)
      return NULL;
  }

  /* My what a large directory name we have.  */
  do
    {
      size <<= 1;
      char *ptr = realloc (buf, size);
      if (ptr == NULL)
        {
          free (buf);
          errno = ENOMEM;
          return NULL;
        }
      buf = ptr;
      result = getcwd (buf, size);
    }
  while (!result && errno == ERANGE);

  if (!result)
    free (buf);
  else
    {
      /* Here result == buf.  */
      /* Shrink result before returning it.  */
      size_t actual_size = strlen (result) + 1;
      if (actual_size < size)
        {
          char *shrinked_result = realloc (result, actual_size);
          if (shrinked_result != NULL)
            result = shrinked_result;
        }
    }
  return result;
}

#endif