Mercurial > hg > octave-kai > gnulib-hg
annotate lib/xgetcwd.c @ 5458:81e1a3092078
Import from coreutils.
author | Paul Eggert <eggert@cs.ucla.edu> |
---|---|
date | Wed, 17 Nov 2004 07:18:27 +0000 |
parents | 85cfcfd6d0be |
children | b3d5c90efc81 |
rev | line source |
---|---|
212 | 1 /* xgetcwd.c -- return current directory with unlimited length |
5129
85cfcfd6d0be
(errno): Don't declare; we assume C89 or better now.
Paul Eggert <eggert@cs.ucla.edu>
parents:
4675
diff
changeset
|
2 |
85cfcfd6d0be
(errno): Don't declare; we assume C89 or better now.
Paul Eggert <eggert@cs.ucla.edu>
parents:
4675
diff
changeset
|
3 Copyright (C) 1992, 1996, 2000, 2001, 2003, 2004 Free Software |
85cfcfd6d0be
(errno): Don't declare; we assume C89 or better now.
Paul Eggert <eggert@cs.ucla.edu>
parents:
4675
diff
changeset
|
4 Foundation, Inc. |
212 | 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 2, or (at your option) | |
9 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 | |
650
b4ef1c1a0171
update FSF address in copyright
Jim Meyering <jim@meyering.net>
parents:
212
diff
changeset
|
17 along with this program; if not, write to the Free Software Foundation, |
b4ef1c1a0171
update FSF address in copyright
Jim Meyering <jim@meyering.net>
parents:
212
diff
changeset
|
18 Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA. */ |
212 | 19 |
650
b4ef1c1a0171
update FSF address in copyright
Jim Meyering <jim@meyering.net>
parents:
212
diff
changeset
|
20 /* Written by David MacKenzie <djm@gnu.ai.mit.edu>. */ |
212 | 21 |
778 | 22 #if HAVE_CONFIG_H |
653 | 23 # include <config.h> |
212 | 24 #endif |
25 | |
5458 | 26 #include <limits.h> |
212 | 27 #include <stdio.h> |
28 #include <errno.h> | |
29 #include <sys/types.h> | |
4675 | 30 #include <stdlib.h> |
3431 | 31 |
32 #if HAVE_UNISTD_H | |
33 # include <unistd.h> | |
34 #endif | |
212 | 35 |
778 | 36 #if HAVE_GETCWD |
37 char *getcwd (); | |
38 #else | |
3452
baf8d5fe777a
Fix the !HAVE_GETCWD_NULL code to behave more
Jim Meyering <jim@meyering.net>
parents:
3448
diff
changeset
|
39 # include "pathmax.h" |
baf8d5fe777a
Fix the !HAVE_GETCWD_NULL code to behave more
Jim Meyering <jim@meyering.net>
parents:
3448
diff
changeset
|
40 # define INITIAL_BUFFER_SIZE (PATH_MAX + 1) |
212 | 41 char *getwd (); |
778 | 42 # define getcwd(Buf, Max) getwd (Buf) |
212 | 43 #endif |
44 | |
3466
45d73183ee54
Revert some of the previous change; intead,
Jim Meyering <jim@meyering.net>
parents:
3452
diff
changeset
|
45 #include "xalloc.h" |
4372 | 46 #include "xgetcwd.h" |
3466
45d73183ee54
Revert some of the previous change; intead,
Jim Meyering <jim@meyering.net>
parents:
3452
diff
changeset
|
47 |
4372 | 48 /* Return the current directory, newly allocated, assuming it fits |
49 within PATH_MAX bytes -- this is a common system-imposed limit | |
50 on how getcwd works. | |
51 Upon an out-of-memory error, call xalloc_die. | |
52 Upon any other type of error, return NULL. */ | |
212 | 53 |
54 char * | |
4372 | 55 xgetcwd (void) |
212 | 56 { |
3448
95da01de9977
(xgetcwd): Use HAVE_GETCWD_NULL, not (defined __GLIBC__ && __GLIBC__ >= 2),
Jim Meyering <jim@meyering.net>
parents:
3435
diff
changeset
|
57 #if HAVE_GETCWD_NULL |
3466
45d73183ee54
Revert some of the previous change; intead,
Jim Meyering <jim@meyering.net>
parents:
3452
diff
changeset
|
58 char *cwd = getcwd (NULL, 0); |
45d73183ee54
Revert some of the previous change; intead,
Jim Meyering <jim@meyering.net>
parents:
3452
diff
changeset
|
59 if (! cwd && errno == ENOMEM) |
45d73183ee54
Revert some of the previous change; intead,
Jim Meyering <jim@meyering.net>
parents:
3452
diff
changeset
|
60 xalloc_die (); |
45d73183ee54
Revert some of the previous change; intead,
Jim Meyering <jim@meyering.net>
parents:
3452
diff
changeset
|
61 return cwd; |
3343
1ff924ca3a9a
(xgetcwd) [defined __GLIBC__ && __GLIBC__ >= 2]:
Jim Meyering <jim@meyering.net>
parents:
2627
diff
changeset
|
62 #else |
3452
baf8d5fe777a
Fix the !HAVE_GETCWD_NULL code to behave more
Jim Meyering <jim@meyering.net>
parents:
3448
diff
changeset
|
63 |
5458 | 64 int saved_errno; |
65 | |
3452
baf8d5fe777a
Fix the !HAVE_GETCWD_NULL code to behave more
Jim Meyering <jim@meyering.net>
parents:
3448
diff
changeset
|
66 /* The initial buffer size for the working directory. A power of 2 |
baf8d5fe777a
Fix the !HAVE_GETCWD_NULL code to behave more
Jim Meyering <jim@meyering.net>
parents:
3448
diff
changeset
|
67 detects arithmetic overflow earlier, but is not required. */ |
baf8d5fe777a
Fix the !HAVE_GETCWD_NULL code to behave more
Jim Meyering <jim@meyering.net>
parents:
3448
diff
changeset
|
68 # ifndef INITIAL_BUFFER_SIZE |
baf8d5fe777a
Fix the !HAVE_GETCWD_NULL code to behave more
Jim Meyering <jim@meyering.net>
parents:
3448
diff
changeset
|
69 # define INITIAL_BUFFER_SIZE 128 |
baf8d5fe777a
Fix the !HAVE_GETCWD_NULL code to behave more
Jim Meyering <jim@meyering.net>
parents:
3448
diff
changeset
|
70 # endif |
baf8d5fe777a
Fix the !HAVE_GETCWD_NULL code to behave more
Jim Meyering <jim@meyering.net>
parents:
3448
diff
changeset
|
71 |
baf8d5fe777a
Fix the !HAVE_GETCWD_NULL code to behave more
Jim Meyering <jim@meyering.net>
parents:
3448
diff
changeset
|
72 size_t buf_size = INITIAL_BUFFER_SIZE; |
212 | 73 |
3433
656345934fec
(xgetcwd): Reorganize to avoid some duplication.
Jim Meyering <jim@meyering.net>
parents:
3431
diff
changeset
|
74 while (1) |
212 | 75 { |
3466
45d73183ee54
Revert some of the previous change; intead,
Jim Meyering <jim@meyering.net>
parents:
3452
diff
changeset
|
76 char *buf = xmalloc (buf_size); |
45d73183ee54
Revert some of the previous change; intead,
Jim Meyering <jim@meyering.net>
parents:
3452
diff
changeset
|
77 char *cwd = getcwd (buf, buf_size); |
3452
baf8d5fe777a
Fix the !HAVE_GETCWD_NULL code to behave more
Jim Meyering <jim@meyering.net>
parents:
3448
diff
changeset
|
78 if (cwd) |
3433
656345934fec
(xgetcwd): Reorganize to avoid some duplication.
Jim Meyering <jim@meyering.net>
parents:
3431
diff
changeset
|
79 return cwd; |
3452
baf8d5fe777a
Fix the !HAVE_GETCWD_NULL code to behave more
Jim Meyering <jim@meyering.net>
parents:
3448
diff
changeset
|
80 saved_errno = errno; |
baf8d5fe777a
Fix the !HAVE_GETCWD_NULL code to behave more
Jim Meyering <jim@meyering.net>
parents:
3448
diff
changeset
|
81 free (buf); |
baf8d5fe777a
Fix the !HAVE_GETCWD_NULL code to behave more
Jim Meyering <jim@meyering.net>
parents:
3448
diff
changeset
|
82 if (saved_errno != ERANGE) |
5458 | 83 break; |
84 | |
85 #ifdef PATH_MAX | |
86 if (PATH_MAX / 2 < buf_size) | |
87 { | |
88 if (PATH_MAX <= buf_size) | |
89 break; | |
90 buf_size = PATH_MAX; | |
91 continue; | |
92 } | |
93 #endif | |
94 | |
3452
baf8d5fe777a
Fix the !HAVE_GETCWD_NULL code to behave more
Jim Meyering <jim@meyering.net>
parents:
3448
diff
changeset
|
95 buf_size *= 2; |
baf8d5fe777a
Fix the !HAVE_GETCWD_NULL code to behave more
Jim Meyering <jim@meyering.net>
parents:
3448
diff
changeset
|
96 if (buf_size == 0) |
3466
45d73183ee54
Revert some of the previous change; intead,
Jim Meyering <jim@meyering.net>
parents:
3452
diff
changeset
|
97 xalloc_die (); |
212 | 98 } |
5458 | 99 |
100 errno = saved_errno; | |
101 return NULL; | |
3343
1ff924ca3a9a
(xgetcwd) [defined __GLIBC__ && __GLIBC__ >= 2]:
Jim Meyering <jim@meyering.net>
parents:
2627
diff
changeset
|
102 #endif |
212 | 103 } |