Sync usage with man page.
[netbsd-mini2440.git] / gnu / dist / gettext / gettext-tools / lib / xgetcwd.c
blobdffefc7ec47bfbaf4e35604ff0a4fe74a91a1316
1 /* xgetcwd.c -- return current directory with unlimited length
2 Copyright (C) 1992, 1996, 2000, 2003 Free Software Foundation, Inc.
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 2, or (at your option)
7 any later version.
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.
14 You should have received a copy of the GNU General Public License
15 along with this program; if not, write to the Free Software Foundation,
16 Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA. */
18 /* Written by David MacKenzie <djm@gnu.ai.mit.edu>. */
20 #if HAVE_CONFIG_H
21 # include <config.h>
22 #endif
24 #include <stdlib.h>
25 #include <stdio.h>
26 #include <errno.h>
27 #ifndef errno
28 extern int errno;
29 #endif
31 #include <sys/types.h>
32 #if HAVE_UNISTD_H
33 # include <unistd.h>
34 #endif
36 #include "pathmax.h"
38 #if HAVE_GETCWD
39 # ifdef VMS
40 /* We want the directory in Unix syntax, not in VMS syntax. */
41 # define getcwd(Buf, Max) (getcwd) (Buf, Max, 0)
42 # else
43 # ifndef _FORTIFY_SOURCE
44 char *getcwd ();
45 # endif
46 # endif
47 #else
48 char *getwd ();
49 # define getcwd(Buf, Max) getwd (Buf)
50 #endif
52 #include "xalloc.h"
54 /* Return the current directory, newly allocated, arbitrarily long.
55 Return NULL and set errno on error. */
57 char *
58 xgetcwd ()
60 char *ret;
61 unsigned path_max;
62 char buf[1024];
64 errno = 0;
65 ret = getcwd (buf, sizeof (buf));
66 if (ret != NULL)
67 return xstrdup (buf);
68 if (errno != ERANGE)
69 return NULL;
71 path_max = (unsigned) PATH_MAX;
72 path_max += 2; /* The getcwd docs say to do this. */
74 for (;;)
76 char *cwd = (char *) xmalloc (path_max);
78 errno = 0;
79 ret = getcwd (cwd, path_max);
80 if (ret != NULL)
81 return ret;
82 if (errno != ERANGE)
84 int save_errno = errno;
85 free (cwd);
86 errno = save_errno;
87 return NULL;
90 free (cwd);
92 path_max += path_max / 16;
93 path_max += 32;