Imported Upstream version 0.19.7
[platform/upstream/gettext.git] / gnulib-local / lib / xgetcwd.c
1 /* xgetcwd.c -- return current directory with unlimited length
2    Copyright (C) 1992, 1996, 2000, 2003, 2005-2006, 2011, 2015 Free
3    Software Foundation, Inc.
4
5    This program is free software: you can redistribute it and/or modify
6    it under the terms of the GNU General Public License as published by
7    the Free Software Foundation; either version 3 of the License, or
8    (at your option) any later version.
9
10    This program is distributed in the hope that it will be useful,
11    but WITHOUT ANY WARRANTY; without even the implied warranty of
12    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
13    GNU General Public License for more details.
14
15    You should have received a copy of the GNU General Public License
16    along with this program.  If not, see <http://www.gnu.org/licenses/>.  */
17
18 /* Written by David MacKenzie <djm@gnu.ai.mit.edu>.  */
19
20 #include <config.h>
21
22 #include <stdlib.h>
23 #include <stdio.h>
24 #include <errno.h>
25 #include <sys/types.h>
26 #include <unistd.h>
27
28 #include "pathmax.h"
29
30 /* In this file, PATH_MAX is the size of an initial memory allocation.  */
31 #ifndef PATH_MAX
32 # define PATH_MAX 8192
33 #endif
34
35 #if HAVE_GETCWD
36 # ifdef VMS
37    /* We want the directory in Unix syntax, not in VMS syntax.  */
38 #  define getcwd(Buf, Max) (getcwd) (Buf, Max, 0)
39 # else
40 char *getcwd ();
41 # endif
42 #else
43 char *getwd ();
44 # define getcwd(Buf, Max) getwd (Buf)
45 #endif
46
47 #include "xalloc.h"
48
49 /* Return the current directory, newly allocated, arbitrarily long.
50    Return NULL and set errno on error. */
51
52 char *
53 xgetcwd ()
54 {
55   char *ret;
56   unsigned path_max;
57   char buf[1024];
58
59   errno = 0;
60   ret = getcwd (buf, sizeof (buf));
61   if (ret != NULL)
62     return xstrdup (buf);
63   if (errno != ERANGE)
64     return NULL;
65
66   path_max = (unsigned) PATH_MAX;
67   path_max += 2;                /* The getcwd docs say to do this. */
68
69   for (;;)
70     {
71       char *cwd = XNMALLOC (path_max, char);
72
73       errno = 0;
74       ret = getcwd (cwd, path_max);
75       if (ret != NULL)
76         return ret;
77       if (errno != ERANGE)
78         {
79           int save_errno = errno;
80           free (cwd);
81           errno = save_errno;
82           return NULL;
83         }
84
85       free (cwd);
86
87       path_max += path_max / 16;
88       path_max += 32;
89     }
90 }