f197a1122c391cf4e6507a982d9d919d3d2b7f72
[debian/tar] / gnu / getcwd-lgpl.c
1 /* -*- buffer-read-only: t -*- vi: set ro: */
2 /* DO NOT EDIT! GENERATED AUTOMATICALLY! */
3 /* Copyright (C) 2011-2013 Free Software Foundation, Inc.
4    This file is part of gnulib.
5
6    This program is free software: you can redistribute it and/or modify
7    it under the terms of the GNU General Public License as published by
8    the Free Software Foundation; either version 3 of the License, or
9    (at your option) any later version.
10
11    This program is distributed in the hope that it will be useful,
12    but WITHOUT ANY WARRANTY; without even the implied warranty of
13    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
14    GNU General Public License for more details.
15
16    You should have received a copy of the GNU General Public License
17    along with this program.  If not, see <http://www.gnu.org/licenses/>.  */
18
19 #include <config.h>
20
21 /* Specification */
22 #include <unistd.h>
23
24 #include <errno.h>
25 #include <stdlib.h>
26 #include <string.h>
27
28 #if GNULIB_GETCWD
29 /* Favor GPL getcwd.c if both getcwd and getcwd-lgpl modules are in use.  */
30 typedef int dummy;
31 #else
32
33 /* Get the name of the current working directory, and put it in SIZE
34    bytes of BUF.  Returns NULL if the directory couldn't be determined
35    (perhaps because the absolute name was longer than PATH_MAX, or
36    because of missing read/search permissions on parent directories)
37    or SIZE was too small.  If successful, returns BUF.  If BUF is
38    NULL, an array is allocated with 'malloc'; the array is SIZE bytes
39    long, unless SIZE == 0, in which case it is as big as
40    necessary.  */
41
42 # undef getcwd
43 char *
44 rpl_getcwd (char *buf, size_t size)
45 {
46   char *ptr;
47   char *result;
48
49   /* Handle single size operations.  */
50   if (buf)
51     {
52       if (!size)
53         {
54           errno = EINVAL;
55           return NULL;
56         }
57       return getcwd (buf, size);
58     }
59
60   if (size)
61     {
62       buf = malloc (size);
63       if (!buf)
64         {
65           errno = ENOMEM;
66           return NULL;
67         }
68       result = getcwd (buf, size);
69       if (!result)
70         {
71           int saved_errno = errno;
72           free (buf);
73           errno = saved_errno;
74         }
75       return result;
76     }
77
78   /* Flexible sizing requested.  Avoid over-allocation for the common
79      case of a name that fits within a 4k page, minus some space for
80      local variables, to be sure we don't skip over a guard page.  */
81   {
82     char tmp[4032];
83     size = sizeof tmp;
84     ptr = getcwd (tmp, size);
85     if (ptr)
86       {
87         result = strdup (ptr);
88         if (!result)
89           errno = ENOMEM;
90         return result;
91       }
92     if (errno != ERANGE)
93       return NULL;
94   }
95
96   /* My what a large directory name we have.  */
97   do
98     {
99       size <<= 1;
100       ptr = realloc (buf, size);
101       if (ptr == NULL)
102         {
103           free (buf);
104           errno = ENOMEM;
105           return NULL;
106         }
107       buf = ptr;
108       result = getcwd (buf, size);
109     }
110   while (!result && errno == ERANGE);
111
112   if (!result)
113     {
114       int saved_errno = errno;
115       free (buf);
116       errno = saved_errno;
117     }
118   else
119     {
120       /* Trim to fit, if possible.  */
121       result = realloc (buf, strlen (buf) + 1);
122       if (!result)
123         result = buf;
124     }
125   return result;
126 }
127
128 #endif