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