Mercurial > hg > octave-lojdl > gnulib-hg
annotate lib/getcwd-lgpl.c @ 17249:e542fd46ad6f
maint: update all copyright year number ranges
Run "make update-copyright". Compare to commit 1602f0a from last year.
Signed-off-by: Eric Blake <eblake@redhat.com>
author | Eric Blake <eblake@redhat.com> |
---|---|
date | Tue, 01 Jan 2013 00:50:58 +0000 |
parents | 18a38c9615f0 |
children | 744044c581c4 |
rev | line source |
---|---|
17249
e542fd46ad6f
maint: update all copyright year number ranges
Eric Blake <eblake@redhat.com>
parents:
16235
diff
changeset
|
1 /* Copyright (C) 2011-2013 Free Software Foundation, Inc. |
14629 | 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 <string.h> | |
24 | |
25 #if GNULIB_GETCWD | |
26 /* Favor GPL getcwd.c if both getcwd and getcwd-lgpl modules are in use. */ | |
27 typedef int dummy; | |
28 #else | |
29 | |
30 /* Get the name of the current working directory, and put it in SIZE | |
31 bytes of BUF. Returns NULL if the directory couldn't be determined | |
32 (perhaps because the absolute name was longer than PATH_MAX, or | |
33 because of missing read/search permissions on parent directories) | |
34 or SIZE was too small. If successful, returns BUF. If BUF is | |
16235
18a38c9615f0
In commentary, do not use ` to quote.
Paul Eggert <eggert@cs.ucla.edu>
parents:
16201
diff
changeset
|
35 NULL, an array is allocated with 'malloc'; the array is SIZE bytes |
14629 | 36 long, unless SIZE == 0, in which case it is as big as |
37 necessary. */ | |
38 | |
39 # undef getcwd | |
40 char * | |
41 rpl_getcwd (char *buf, size_t size) | |
42 { | |
43 char *ptr; | |
44 char *result; | |
45 | |
46 /* Handle single size operations. */ | |
47 if (buf) | |
14826
a57aca916cfd
getcwd: work around mingw bug
Eric Blake <eblake@redhat.com>
parents:
14641
diff
changeset
|
48 { |
a57aca916cfd
getcwd: work around mingw bug
Eric Blake <eblake@redhat.com>
parents:
14641
diff
changeset
|
49 if (!size) |
a57aca916cfd
getcwd: work around mingw bug
Eric Blake <eblake@redhat.com>
parents:
14641
diff
changeset
|
50 { |
a57aca916cfd
getcwd: work around mingw bug
Eric Blake <eblake@redhat.com>
parents:
14641
diff
changeset
|
51 errno = EINVAL; |
a57aca916cfd
getcwd: work around mingw bug
Eric Blake <eblake@redhat.com>
parents:
14641
diff
changeset
|
52 return NULL; |
a57aca916cfd
getcwd: work around mingw bug
Eric Blake <eblake@redhat.com>
parents:
14641
diff
changeset
|
53 } |
a57aca916cfd
getcwd: work around mingw bug
Eric Blake <eblake@redhat.com>
parents:
14641
diff
changeset
|
54 return getcwd (buf, size); |
a57aca916cfd
getcwd: work around mingw bug
Eric Blake <eblake@redhat.com>
parents:
14641
diff
changeset
|
55 } |
14629 | 56 |
57 if (size) | |
58 { | |
59 buf = malloc (size); | |
60 if (!buf) | |
61 { | |
62 errno = ENOMEM; | |
14641 | 63 return NULL; |
14629 | 64 } |
65 result = getcwd (buf, size); | |
66 if (!result) | |
67 { | |
68 int saved_errno = errno; | |
69 free (buf); | |
70 errno = saved_errno; | |
71 } | |
72 return result; | |
73 } | |
74 | |
75 /* Flexible sizing requested. Avoid over-allocation for the common | |
76 case of a name that fits within a 4k page, minus some space for | |
77 local variables, to be sure we don't skip over a guard page. */ | |
78 { | |
79 char tmp[4032]; | |
80 size = sizeof tmp; | |
81 ptr = getcwd (tmp, size); | |
82 if (ptr) | |
83 { | |
84 result = strdup (ptr); | |
85 if (!result) | |
86 errno = ENOMEM; | |
87 return result; | |
88 } | |
89 if (errno != ERANGE) | |
90 return NULL; | |
91 } | |
92 | |
93 /* My what a large directory name we have. */ | |
94 do | |
95 { | |
96 size <<= 1; | |
97 ptr = realloc (buf, size); | |
98 if (ptr == NULL) | |
99 { | |
100 free (buf); | |
101 errno = ENOMEM; | |
102 return NULL; | |
103 } | |
104 buf = ptr; | |
105 result = getcwd (buf, size); | |
106 } | |
107 while (!result && errno == ERANGE); | |
108 | |
109 if (!result) | |
110 { | |
111 int saved_errno = errno; | |
112 free (buf); | |
113 errno = saved_errno; | |
114 } | |
115 else | |
116 { | |
117 /* Trim to fit, if possible. */ | |
118 result = realloc (buf, strlen (buf) + 1); | |
119 if (!result) | |
120 result = buf; | |
121 } | |
122 return result; | |
123 } | |
124 | |
125 #endif |