Mercurial > hg > octave-kai > gnulib-hg
annotate lib/xgetcwd.c @ 4372:61fb4061ff91
From coreutils.
(xgetcwd): Include "xgetcwd.h".
Improve comment.
author | Jim Meyering <jim@meyering.net> |
---|---|
date | Sat, 07 Jun 2003 10:07:29 +0000 |
parents | 45d73183ee54 |
children | 7135d3d3d962 |
rev | line source |
---|---|
212 | 1 /* xgetcwd.c -- return current directory with unlimited length |
4372 | 2 Copyright (C) 1992, 1996, 2000, 2001, 2003 Free Software Foundation, Inc. |
212 | 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 2, or (at your option) | |
7 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 | |
650
b4ef1c1a0171
update FSF address in copyright
Jim Meyering <jim@meyering.net>
parents:
212
diff
changeset
|
15 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
|
16 Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA. */ |
212 | 17 |
650
b4ef1c1a0171
update FSF address in copyright
Jim Meyering <jim@meyering.net>
parents:
212
diff
changeset
|
18 /* Written by David MacKenzie <djm@gnu.ai.mit.edu>. */ |
212 | 19 |
778 | 20 #if HAVE_CONFIG_H |
653 | 21 # include <config.h> |
212 | 22 #endif |
23 | |
24 #include <stdio.h> | |
25 #include <errno.h> | |
26 #ifndef errno | |
27 extern int errno; | |
28 #endif | |
778 | 29 |
212 | 30 #include <sys/types.h> |
3431 | 31 |
32 #if HAVE_STDLIB_H | |
33 # include <stdlib.h> | |
34 #endif | |
35 #if HAVE_UNISTD_H | |
36 # include <unistd.h> | |
37 #endif | |
212 | 38 |
778 | 39 #if HAVE_GETCWD |
40 char *getcwd (); | |
41 #else | |
3452
baf8d5fe777a
Fix the !HAVE_GETCWD_NULL code to behave more
Jim Meyering <jim@meyering.net>
parents:
3448
diff
changeset
|
42 # include "pathmax.h" |
baf8d5fe777a
Fix the !HAVE_GETCWD_NULL code to behave more
Jim Meyering <jim@meyering.net>
parents:
3448
diff
changeset
|
43 # define INITIAL_BUFFER_SIZE (PATH_MAX + 1) |
212 | 44 char *getwd (); |
778 | 45 # define getcwd(Buf, Max) getwd (Buf) |
212 | 46 #endif |
47 | |
3466
45d73183ee54
Revert some of the previous change; intead,
Jim Meyering <jim@meyering.net>
parents:
3452
diff
changeset
|
48 #include "xalloc.h" |
4372 | 49 #include "xgetcwd.h" |
3466
45d73183ee54
Revert some of the previous change; intead,
Jim Meyering <jim@meyering.net>
parents:
3452
diff
changeset
|
50 |
4372 | 51 /* Return the current directory, newly allocated, assuming it fits |
52 within PATH_MAX bytes -- this is a common system-imposed limit | |
53 on how getcwd works. | |
54 Upon an out-of-memory error, call xalloc_die. | |
55 Upon any other type of error, return NULL. */ | |
212 | 56 |
57 char * | |
4372 | 58 xgetcwd (void) |
212 | 59 { |
3448
95da01de9977
(xgetcwd): Use HAVE_GETCWD_NULL, not (defined __GLIBC__ && __GLIBC__ >= 2),
Jim Meyering <jim@meyering.net>
parents:
3435
diff
changeset
|
60 #if HAVE_GETCWD_NULL |
3466
45d73183ee54
Revert some of the previous change; intead,
Jim Meyering <jim@meyering.net>
parents:
3452
diff
changeset
|
61 char *cwd = getcwd (NULL, 0); |
45d73183ee54
Revert some of the previous change; intead,
Jim Meyering <jim@meyering.net>
parents:
3452
diff
changeset
|
62 if (! cwd && errno == ENOMEM) |
45d73183ee54
Revert some of the previous change; intead,
Jim Meyering <jim@meyering.net>
parents:
3452
diff
changeset
|
63 xalloc_die (); |
45d73183ee54
Revert some of the previous change; intead,
Jim Meyering <jim@meyering.net>
parents:
3452
diff
changeset
|
64 return cwd; |
3343
1ff924ca3a9a
(xgetcwd) [defined __GLIBC__ && __GLIBC__ >= 2]:
Jim Meyering <jim@meyering.net>
parents:
2627
diff
changeset
|
65 #else |
3452
baf8d5fe777a
Fix the !HAVE_GETCWD_NULL code to behave more
Jim Meyering <jim@meyering.net>
parents:
3448
diff
changeset
|
66 |
baf8d5fe777a
Fix the !HAVE_GETCWD_NULL code to behave more
Jim Meyering <jim@meyering.net>
parents:
3448
diff
changeset
|
67 /* 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
|
68 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
|
69 # ifndef INITIAL_BUFFER_SIZE |
baf8d5fe777a
Fix the !HAVE_GETCWD_NULL code to behave more
Jim Meyering <jim@meyering.net>
parents:
3448
diff
changeset
|
70 # define INITIAL_BUFFER_SIZE 128 |
baf8d5fe777a
Fix the !HAVE_GETCWD_NULL code to behave more
Jim Meyering <jim@meyering.net>
parents:
3448
diff
changeset
|
71 # endif |
baf8d5fe777a
Fix the !HAVE_GETCWD_NULL code to behave more
Jim Meyering <jim@meyering.net>
parents:
3448
diff
changeset
|
72 |
baf8d5fe777a
Fix the !HAVE_GETCWD_NULL code to behave more
Jim Meyering <jim@meyering.net>
parents:
3448
diff
changeset
|
73 size_t buf_size = INITIAL_BUFFER_SIZE; |
212 | 74 |
3433
656345934fec
(xgetcwd): Reorganize to avoid some duplication.
Jim Meyering <jim@meyering.net>
parents:
3431
diff
changeset
|
75 while (1) |
212 | 76 { |
3466
45d73183ee54
Revert some of the previous change; intead,
Jim Meyering <jim@meyering.net>
parents:
3452
diff
changeset
|
77 char *buf = xmalloc (buf_size); |
45d73183ee54
Revert some of the previous change; intead,
Jim Meyering <jim@meyering.net>
parents:
3452
diff
changeset
|
78 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
|
79 int saved_errno; |
baf8d5fe777a
Fix the !HAVE_GETCWD_NULL code to behave more
Jim Meyering <jim@meyering.net>
parents:
3448
diff
changeset
|
80 if (cwd) |
3433
656345934fec
(xgetcwd): Reorganize to avoid some duplication.
Jim Meyering <jim@meyering.net>
parents:
3431
diff
changeset
|
81 return cwd; |
3452
baf8d5fe777a
Fix the !HAVE_GETCWD_NULL code to behave more
Jim Meyering <jim@meyering.net>
parents:
3448
diff
changeset
|
82 saved_errno = errno; |
baf8d5fe777a
Fix the !HAVE_GETCWD_NULL code to behave more
Jim Meyering <jim@meyering.net>
parents:
3448
diff
changeset
|
83 free (buf); |
baf8d5fe777a
Fix the !HAVE_GETCWD_NULL code to behave more
Jim Meyering <jim@meyering.net>
parents:
3448
diff
changeset
|
84 if (saved_errno != ERANGE) |
baf8d5fe777a
Fix the !HAVE_GETCWD_NULL code to behave more
Jim Meyering <jim@meyering.net>
parents:
3448
diff
changeset
|
85 return NULL; |
baf8d5fe777a
Fix the !HAVE_GETCWD_NULL code to behave more
Jim Meyering <jim@meyering.net>
parents:
3448
diff
changeset
|
86 buf_size *= 2; |
baf8d5fe777a
Fix the !HAVE_GETCWD_NULL code to behave more
Jim Meyering <jim@meyering.net>
parents:
3448
diff
changeset
|
87 if (buf_size == 0) |
3466
45d73183ee54
Revert some of the previous change; intead,
Jim Meyering <jim@meyering.net>
parents:
3452
diff
changeset
|
88 xalloc_die (); |
212 | 89 } |
3343
1ff924ca3a9a
(xgetcwd) [defined __GLIBC__ && __GLIBC__ >= 2]:
Jim Meyering <jim@meyering.net>
parents:
2627
diff
changeset
|
90 #endif |
212 | 91 } |