8a2a3abeb490c3cc42f38210b4a394c866f9faf9
[platform/upstream/diffutils.git] / gnulib-tests / getcwd-lgpl.c
1 /* -*- buffer-read-only: t -*- vi: set ro: */
2 /* DO NOT EDIT! GENERATED AUTOMATICALLY! */
3 /* Copyright (C) 2011 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 <string.h>
26
27 #if GNULIB_GETCWD
28 /* Favor GPL getcwd.c if both getcwd and getcwd-lgpl modules are in use.  */
29 typedef int dummy;
30 #else
31
32 /* Get the name of the current working directory, and put it in SIZE
33    bytes of BUF.  Returns NULL if the directory couldn't be determined
34    (perhaps because the absolute name was longer than PATH_MAX, or
35    because of missing read/search permissions on parent directories)
36    or SIZE was too small.  If successful, returns BUF.  If BUF is
37    NULL, an array is allocated with `malloc'; the array is SIZE bytes
38    long, unless SIZE == 0, in which case it is as big as
39    necessary.  */
40
41 # undef getcwd
42 char *
43 rpl_getcwd (char *buf, size_t size)
44 {
45   char *ptr;
46   char *result;
47
48   /* Handle single size operations.  */
49   if (buf)
50     {
51       if (!size)
52         {
53           errno = EINVAL;
54           return NULL;
55         }
56       return getcwd (buf, size);
57     }
58
59   if (size)
60     {
61       buf = malloc (size);
62       if (!buf)
63         {
64           errno = ENOMEM;
65           return NULL;
66         }
67       result = getcwd (buf, size);
68       if (!result)
69         {
70           int saved_errno = errno;
71           free (buf);
72           errno = saved_errno;
73         }
74       return result;
75     }
76
77   /* Flexible sizing requested.  Avoid over-allocation for the common
78      case of a name that fits within a 4k page, minus some space for
79      local variables, to be sure we don't skip over a guard page.  */
80   {
81     char tmp[4032];
82     size = sizeof tmp;
83     ptr = getcwd (tmp, size);
84     if (ptr)
85       {
86         result = strdup (ptr);
87         if (!result)
88           errno = ENOMEM;
89         return result;
90       }
91     if (errno != ERANGE)
92       return NULL;
93   }
94
95   /* My what a large directory name we have.  */
96   do
97     {
98       size <<= 1;
99       ptr = realloc (buf, size);
100       if (ptr == NULL)
101         {
102           free (buf);
103           errno = ENOMEM;
104           return NULL;
105         }
106       buf = ptr;
107       result = getcwd (buf, size);
108     }
109   while (!result && errno == ERANGE);
110
111   if (!result)
112     {
113       int saved_errno = errno;
114       free (buf);
115       errno = saved_errno;
116     }
117   else
118     {
119       /* Trim to fit, if possible.  */
120       result = realloc (buf, strlen (buf) + 1);
121       if (!result)
122         result = buf;
123     }
124   return result;
125 }
126
127 #endif