Mercurial > hg > octave-nkf > gnulib-hg
annotate lib/amemxfrm.c @ 17334:bb52d9cece01
unsetenv etc.: port to Solaris 11 + GNU Emacs
* lib/canonicalize-lgpl.c, lib/getaddrinfo.c, lib/getdelim.c:
* lib/glob.c, lib/random_r.c, lib/setenv.c, lib/tsearch.c:
* lib/unsetenv.c (_GL_ARG_NONNULL): Define before including <config.h>.
GNU Emacs's <config.h> includes <stdlib.h> (which is not a great
idea but is too painful to fix right now), and without this gnulib
change <stdlib.h> was defining _GL_ARG_NONNULL incorrectly when
compiling unsetenv.c on Solaris 11. Fix the problem for
unsetenv.c, and fix other similar occurrences.
author | Paul Eggert <eggert@cs.ucla.edu> |
---|---|
date | Mon, 11 Feb 2013 14:58:56 -0800 |
parents | e542fd46ad6f |
children | 344018b6e5d7 |
rev | line source |
---|---|
13557
8db1bc2a38a3
Rename module 'memxfrm' to 'amemxfrm'.
Bruno Haible <bruno@clisp.org>
parents:
diff
changeset
|
1 /* Locale dependent memory area transformation for comparison. |
17249
e542fd46ad6f
maint: update all copyright year number ranges
Eric Blake <eblake@redhat.com>
parents:
16201
diff
changeset
|
2 Copyright (C) 2009-2013 Free Software Foundation, Inc. |
13557
8db1bc2a38a3
Rename module 'memxfrm' to 'amemxfrm'.
Bruno Haible <bruno@clisp.org>
parents:
diff
changeset
|
3 Written by Bruno Haible <bruno@clisp.org>, 2009. |
8db1bc2a38a3
Rename module 'memxfrm' to 'amemxfrm'.
Bruno Haible <bruno@clisp.org>
parents:
diff
changeset
|
4 |
8db1bc2a38a3
Rename module 'memxfrm' to 'amemxfrm'.
Bruno Haible <bruno@clisp.org>
parents:
diff
changeset
|
5 This program is free software: you can redistribute it and/or modify it |
8db1bc2a38a3
Rename module 'memxfrm' to 'amemxfrm'.
Bruno Haible <bruno@clisp.org>
parents:
diff
changeset
|
6 under the terms of the GNU Lesser General Public License as published |
8db1bc2a38a3
Rename module 'memxfrm' to 'amemxfrm'.
Bruno Haible <bruno@clisp.org>
parents:
diff
changeset
|
7 by the Free Software Foundation; either version 3 of the License, or |
8db1bc2a38a3
Rename module 'memxfrm' to 'amemxfrm'.
Bruno Haible <bruno@clisp.org>
parents:
diff
changeset
|
8 (at your option) any later version. |
8db1bc2a38a3
Rename module 'memxfrm' to 'amemxfrm'.
Bruno Haible <bruno@clisp.org>
parents:
diff
changeset
|
9 |
8db1bc2a38a3
Rename module 'memxfrm' to 'amemxfrm'.
Bruno Haible <bruno@clisp.org>
parents:
diff
changeset
|
10 This program is distributed in the hope that it will be useful, |
8db1bc2a38a3
Rename module 'memxfrm' to 'amemxfrm'.
Bruno Haible <bruno@clisp.org>
parents:
diff
changeset
|
11 but WITHOUT ANY WARRANTY; without even the implied warranty of |
8db1bc2a38a3
Rename module 'memxfrm' to 'amemxfrm'.
Bruno Haible <bruno@clisp.org>
parents:
diff
changeset
|
12 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU |
8db1bc2a38a3
Rename module 'memxfrm' to 'amemxfrm'.
Bruno Haible <bruno@clisp.org>
parents:
diff
changeset
|
13 Lesser General Public License for more details. |
8db1bc2a38a3
Rename module 'memxfrm' to 'amemxfrm'.
Bruno Haible <bruno@clisp.org>
parents:
diff
changeset
|
14 |
8db1bc2a38a3
Rename module 'memxfrm' to 'amemxfrm'.
Bruno Haible <bruno@clisp.org>
parents:
diff
changeset
|
15 You should have received a copy of the GNU Lesser General Public License |
8db1bc2a38a3
Rename module 'memxfrm' to 'amemxfrm'.
Bruno Haible <bruno@clisp.org>
parents:
diff
changeset
|
16 along with this program. If not, see <http://www.gnu.org/licenses/>. */ |
8db1bc2a38a3
Rename module 'memxfrm' to 'amemxfrm'.
Bruno Haible <bruno@clisp.org>
parents:
diff
changeset
|
17 |
8db1bc2a38a3
Rename module 'memxfrm' to 'amemxfrm'.
Bruno Haible <bruno@clisp.org>
parents:
diff
changeset
|
18 #include <config.h> |
8db1bc2a38a3
Rename module 'memxfrm' to 'amemxfrm'.
Bruno Haible <bruno@clisp.org>
parents:
diff
changeset
|
19 |
8db1bc2a38a3
Rename module 'memxfrm' to 'amemxfrm'.
Bruno Haible <bruno@clisp.org>
parents:
diff
changeset
|
20 /* Specification. */ |
8db1bc2a38a3
Rename module 'memxfrm' to 'amemxfrm'.
Bruno Haible <bruno@clisp.org>
parents:
diff
changeset
|
21 #include "amemxfrm.h" |
8db1bc2a38a3
Rename module 'memxfrm' to 'amemxfrm'.
Bruno Haible <bruno@clisp.org>
parents:
diff
changeset
|
22 |
8db1bc2a38a3
Rename module 'memxfrm' to 'amemxfrm'.
Bruno Haible <bruno@clisp.org>
parents:
diff
changeset
|
23 #include <errno.h> |
8db1bc2a38a3
Rename module 'memxfrm' to 'amemxfrm'.
Bruno Haible <bruno@clisp.org>
parents:
diff
changeset
|
24 #include <stdlib.h> |
8db1bc2a38a3
Rename module 'memxfrm' to 'amemxfrm'.
Bruno Haible <bruno@clisp.org>
parents:
diff
changeset
|
25 #include <string.h> |
8db1bc2a38a3
Rename module 'memxfrm' to 'amemxfrm'.
Bruno Haible <bruno@clisp.org>
parents:
diff
changeset
|
26 |
8db1bc2a38a3
Rename module 'memxfrm' to 'amemxfrm'.
Bruno Haible <bruno@clisp.org>
parents:
diff
changeset
|
27 char * |
8db1bc2a38a3
Rename module 'memxfrm' to 'amemxfrm'.
Bruno Haible <bruno@clisp.org>
parents:
diff
changeset
|
28 amemxfrm (char *s, size_t n, char *resultbuf, size_t *lengthp) |
8db1bc2a38a3
Rename module 'memxfrm' to 'amemxfrm'.
Bruno Haible <bruno@clisp.org>
parents:
diff
changeset
|
29 { |
8db1bc2a38a3
Rename module 'memxfrm' to 'amemxfrm'.
Bruno Haible <bruno@clisp.org>
parents:
diff
changeset
|
30 /* Result accumulator. */ |
8db1bc2a38a3
Rename module 'memxfrm' to 'amemxfrm'.
Bruno Haible <bruno@clisp.org>
parents:
diff
changeset
|
31 char *result; |
8db1bc2a38a3
Rename module 'memxfrm' to 'amemxfrm'.
Bruno Haible <bruno@clisp.org>
parents:
diff
changeset
|
32 size_t length; |
8db1bc2a38a3
Rename module 'memxfrm' to 'amemxfrm'.
Bruno Haible <bruno@clisp.org>
parents:
diff
changeset
|
33 size_t allocated; |
8db1bc2a38a3
Rename module 'memxfrm' to 'amemxfrm'.
Bruno Haible <bruno@clisp.org>
parents:
diff
changeset
|
34 |
8db1bc2a38a3
Rename module 'memxfrm' to 'amemxfrm'.
Bruno Haible <bruno@clisp.org>
parents:
diff
changeset
|
35 char orig_sentinel; |
8db1bc2a38a3
Rename module 'memxfrm' to 'amemxfrm'.
Bruno Haible <bruno@clisp.org>
parents:
diff
changeset
|
36 |
8db1bc2a38a3
Rename module 'memxfrm' to 'amemxfrm'.
Bruno Haible <bruno@clisp.org>
parents:
diff
changeset
|
37 /* Initial memory allocation. */ |
8db1bc2a38a3
Rename module 'memxfrm' to 'amemxfrm'.
Bruno Haible <bruno@clisp.org>
parents:
diff
changeset
|
38 if (resultbuf != NULL && *lengthp > 0) |
8db1bc2a38a3
Rename module 'memxfrm' to 'amemxfrm'.
Bruno Haible <bruno@clisp.org>
parents:
diff
changeset
|
39 { |
8db1bc2a38a3
Rename module 'memxfrm' to 'amemxfrm'.
Bruno Haible <bruno@clisp.org>
parents:
diff
changeset
|
40 result = resultbuf; |
8db1bc2a38a3
Rename module 'memxfrm' to 'amemxfrm'.
Bruno Haible <bruno@clisp.org>
parents:
diff
changeset
|
41 allocated = *lengthp; |
8db1bc2a38a3
Rename module 'memxfrm' to 'amemxfrm'.
Bruno Haible <bruno@clisp.org>
parents:
diff
changeset
|
42 } |
8db1bc2a38a3
Rename module 'memxfrm' to 'amemxfrm'.
Bruno Haible <bruno@clisp.org>
parents:
diff
changeset
|
43 else |
8db1bc2a38a3
Rename module 'memxfrm' to 'amemxfrm'.
Bruno Haible <bruno@clisp.org>
parents:
diff
changeset
|
44 { |
8db1bc2a38a3
Rename module 'memxfrm' to 'amemxfrm'.
Bruno Haible <bruno@clisp.org>
parents:
diff
changeset
|
45 allocated = (n > 0 ? n : 1); |
8db1bc2a38a3
Rename module 'memxfrm' to 'amemxfrm'.
Bruno Haible <bruno@clisp.org>
parents:
diff
changeset
|
46 result = (char *) malloc (allocated); |
8db1bc2a38a3
Rename module 'memxfrm' to 'amemxfrm'.
Bruno Haible <bruno@clisp.org>
parents:
diff
changeset
|
47 if (result == NULL) |
8db1bc2a38a3
Rename module 'memxfrm' to 'amemxfrm'.
Bruno Haible <bruno@clisp.org>
parents:
diff
changeset
|
48 goto out_of_memory_2; |
8db1bc2a38a3
Rename module 'memxfrm' to 'amemxfrm'.
Bruno Haible <bruno@clisp.org>
parents:
diff
changeset
|
49 } |
8db1bc2a38a3
Rename module 'memxfrm' to 'amemxfrm'.
Bruno Haible <bruno@clisp.org>
parents:
diff
changeset
|
50 length = 0; |
8db1bc2a38a3
Rename module 'memxfrm' to 'amemxfrm'.
Bruno Haible <bruno@clisp.org>
parents:
diff
changeset
|
51 |
8db1bc2a38a3
Rename module 'memxfrm' to 'amemxfrm'.
Bruno Haible <bruno@clisp.org>
parents:
diff
changeset
|
52 /* Add sentinel.byte. */ |
8db1bc2a38a3
Rename module 'memxfrm' to 'amemxfrm'.
Bruno Haible <bruno@clisp.org>
parents:
diff
changeset
|
53 orig_sentinel = s[n]; |
8db1bc2a38a3
Rename module 'memxfrm' to 'amemxfrm'.
Bruno Haible <bruno@clisp.org>
parents:
diff
changeset
|
54 s[n] = '\0'; |
8db1bc2a38a3
Rename module 'memxfrm' to 'amemxfrm'.
Bruno Haible <bruno@clisp.org>
parents:
diff
changeset
|
55 |
8db1bc2a38a3
Rename module 'memxfrm' to 'amemxfrm'.
Bruno Haible <bruno@clisp.org>
parents:
diff
changeset
|
56 /* Iterate through S, transforming each NUL terminated segment. |
8db1bc2a38a3
Rename module 'memxfrm' to 'amemxfrm'.
Bruno Haible <bruno@clisp.org>
parents:
diff
changeset
|
57 Accumulate the resulting transformed segments in result, separated by |
8db1bc2a38a3
Rename module 'memxfrm' to 'amemxfrm'.
Bruno Haible <bruno@clisp.org>
parents:
diff
changeset
|
58 NULs. */ |
8db1bc2a38a3
Rename module 'memxfrm' to 'amemxfrm'.
Bruno Haible <bruno@clisp.org>
parents:
diff
changeset
|
59 { |
8db1bc2a38a3
Rename module 'memxfrm' to 'amemxfrm'.
Bruno Haible <bruno@clisp.org>
parents:
diff
changeset
|
60 const char *p_end = s + n + 1; |
8db1bc2a38a3
Rename module 'memxfrm' to 'amemxfrm'.
Bruno Haible <bruno@clisp.org>
parents:
diff
changeset
|
61 const char *p; |
8db1bc2a38a3
Rename module 'memxfrm' to 'amemxfrm'.
Bruno Haible <bruno@clisp.org>
parents:
diff
changeset
|
62 |
8db1bc2a38a3
Rename module 'memxfrm' to 'amemxfrm'.
Bruno Haible <bruno@clisp.org>
parents:
diff
changeset
|
63 p = s; |
8db1bc2a38a3
Rename module 'memxfrm' to 'amemxfrm'.
Bruno Haible <bruno@clisp.org>
parents:
diff
changeset
|
64 for (;;) |
8db1bc2a38a3
Rename module 'memxfrm' to 'amemxfrm'.
Bruno Haible <bruno@clisp.org>
parents:
diff
changeset
|
65 { |
8db1bc2a38a3
Rename module 'memxfrm' to 'amemxfrm'.
Bruno Haible <bruno@clisp.org>
parents:
diff
changeset
|
66 /* Search next NUL byte. */ |
8db1bc2a38a3
Rename module 'memxfrm' to 'amemxfrm'.
Bruno Haible <bruno@clisp.org>
parents:
diff
changeset
|
67 size_t l = strlen (p); |
8db1bc2a38a3
Rename module 'memxfrm' to 'amemxfrm'.
Bruno Haible <bruno@clisp.org>
parents:
diff
changeset
|
68 |
8db1bc2a38a3
Rename module 'memxfrm' to 'amemxfrm'.
Bruno Haible <bruno@clisp.org>
parents:
diff
changeset
|
69 for (;;) |
8db1bc2a38a3
Rename module 'memxfrm' to 'amemxfrm'.
Bruno Haible <bruno@clisp.org>
parents:
diff
changeset
|
70 { |
8db1bc2a38a3
Rename module 'memxfrm' to 'amemxfrm'.
Bruno Haible <bruno@clisp.org>
parents:
diff
changeset
|
71 size_t k; |
8db1bc2a38a3
Rename module 'memxfrm' to 'amemxfrm'.
Bruno Haible <bruno@clisp.org>
parents:
diff
changeset
|
72 |
8db1bc2a38a3
Rename module 'memxfrm' to 'amemxfrm'.
Bruno Haible <bruno@clisp.org>
parents:
diff
changeset
|
73 /* A call to strxfrm costs about 20 times more than a call to |
8db1bc2a38a3
Rename module 'memxfrm' to 'amemxfrm'.
Bruno Haible <bruno@clisp.org>
parents:
diff
changeset
|
74 strdup of the result. Therefore it is worth to try to avoid |
8db1bc2a38a3
Rename module 'memxfrm' to 'amemxfrm'.
Bruno Haible <bruno@clisp.org>
parents:
diff
changeset
|
75 calling strxfrm more than once on a given string, by making |
8db1bc2a38a3
Rename module 'memxfrm' to 'amemxfrm'.
Bruno Haible <bruno@clisp.org>
parents:
diff
changeset
|
76 enough room before calling strxfrm. |
8db1bc2a38a3
Rename module 'memxfrm' to 'amemxfrm'.
Bruno Haible <bruno@clisp.org>
parents:
diff
changeset
|
77 The size of the strxfrm result, k, is likely to be between |
8db1bc2a38a3
Rename module 'memxfrm' to 'amemxfrm'.
Bruno Haible <bruno@clisp.org>
parents:
diff
changeset
|
78 l and 3 * l. */ |
8db1bc2a38a3
Rename module 'memxfrm' to 'amemxfrm'.
Bruno Haible <bruno@clisp.org>
parents:
diff
changeset
|
79 if (3 * l >= allocated - length) |
8db1bc2a38a3
Rename module 'memxfrm' to 'amemxfrm'.
Bruno Haible <bruno@clisp.org>
parents:
diff
changeset
|
80 { |
8db1bc2a38a3
Rename module 'memxfrm' to 'amemxfrm'.
Bruno Haible <bruno@clisp.org>
parents:
diff
changeset
|
81 /* Grow the result buffer. */ |
8db1bc2a38a3
Rename module 'memxfrm' to 'amemxfrm'.
Bruno Haible <bruno@clisp.org>
parents:
diff
changeset
|
82 size_t new_allocated; |
8db1bc2a38a3
Rename module 'memxfrm' to 'amemxfrm'.
Bruno Haible <bruno@clisp.org>
parents:
diff
changeset
|
83 char *new_result; |
8db1bc2a38a3
Rename module 'memxfrm' to 'amemxfrm'.
Bruno Haible <bruno@clisp.org>
parents:
diff
changeset
|
84 |
8db1bc2a38a3
Rename module 'memxfrm' to 'amemxfrm'.
Bruno Haible <bruno@clisp.org>
parents:
diff
changeset
|
85 new_allocated = length + 3 * l + 1; |
8db1bc2a38a3
Rename module 'memxfrm' to 'amemxfrm'.
Bruno Haible <bruno@clisp.org>
parents:
diff
changeset
|
86 if (new_allocated < 2 * allocated) |
8db1bc2a38a3
Rename module 'memxfrm' to 'amemxfrm'.
Bruno Haible <bruno@clisp.org>
parents:
diff
changeset
|
87 new_allocated = 2 * allocated; |
8db1bc2a38a3
Rename module 'memxfrm' to 'amemxfrm'.
Bruno Haible <bruno@clisp.org>
parents:
diff
changeset
|
88 if (new_allocated < 64) |
8db1bc2a38a3
Rename module 'memxfrm' to 'amemxfrm'.
Bruno Haible <bruno@clisp.org>
parents:
diff
changeset
|
89 new_allocated = 64; |
8db1bc2a38a3
Rename module 'memxfrm' to 'amemxfrm'.
Bruno Haible <bruno@clisp.org>
parents:
diff
changeset
|
90 if (result == resultbuf) |
8db1bc2a38a3
Rename module 'memxfrm' to 'amemxfrm'.
Bruno Haible <bruno@clisp.org>
parents:
diff
changeset
|
91 new_result = (char *) malloc (new_allocated); |
8db1bc2a38a3
Rename module 'memxfrm' to 'amemxfrm'.
Bruno Haible <bruno@clisp.org>
parents:
diff
changeset
|
92 else |
8db1bc2a38a3
Rename module 'memxfrm' to 'amemxfrm'.
Bruno Haible <bruno@clisp.org>
parents:
diff
changeset
|
93 new_result = (char *) realloc (result, new_allocated); |
8db1bc2a38a3
Rename module 'memxfrm' to 'amemxfrm'.
Bruno Haible <bruno@clisp.org>
parents:
diff
changeset
|
94 if (new_result != NULL) |
8db1bc2a38a3
Rename module 'memxfrm' to 'amemxfrm'.
Bruno Haible <bruno@clisp.org>
parents:
diff
changeset
|
95 { |
8db1bc2a38a3
Rename module 'memxfrm' to 'amemxfrm'.
Bruno Haible <bruno@clisp.org>
parents:
diff
changeset
|
96 allocated = new_allocated; |
8db1bc2a38a3
Rename module 'memxfrm' to 'amemxfrm'.
Bruno Haible <bruno@clisp.org>
parents:
diff
changeset
|
97 result = new_result; |
8db1bc2a38a3
Rename module 'memxfrm' to 'amemxfrm'.
Bruno Haible <bruno@clisp.org>
parents:
diff
changeset
|
98 } |
8db1bc2a38a3
Rename module 'memxfrm' to 'amemxfrm'.
Bruno Haible <bruno@clisp.org>
parents:
diff
changeset
|
99 } |
8db1bc2a38a3
Rename module 'memxfrm' to 'amemxfrm'.
Bruno Haible <bruno@clisp.org>
parents:
diff
changeset
|
100 |
8db1bc2a38a3
Rename module 'memxfrm' to 'amemxfrm'.
Bruno Haible <bruno@clisp.org>
parents:
diff
changeset
|
101 errno = 0; |
8db1bc2a38a3
Rename module 'memxfrm' to 'amemxfrm'.
Bruno Haible <bruno@clisp.org>
parents:
diff
changeset
|
102 k = strxfrm (result + length, p, allocated - length); |
8db1bc2a38a3
Rename module 'memxfrm' to 'amemxfrm'.
Bruno Haible <bruno@clisp.org>
parents:
diff
changeset
|
103 if (errno != 0) |
8db1bc2a38a3
Rename module 'memxfrm' to 'amemxfrm'.
Bruno Haible <bruno@clisp.org>
parents:
diff
changeset
|
104 goto fail; |
8db1bc2a38a3
Rename module 'memxfrm' to 'amemxfrm'.
Bruno Haible <bruno@clisp.org>
parents:
diff
changeset
|
105 if (k >= allocated - length) |
8db1bc2a38a3
Rename module 'memxfrm' to 'amemxfrm'.
Bruno Haible <bruno@clisp.org>
parents:
diff
changeset
|
106 { |
8db1bc2a38a3
Rename module 'memxfrm' to 'amemxfrm'.
Bruno Haible <bruno@clisp.org>
parents:
diff
changeset
|
107 /* Grow the result buffer. */ |
8db1bc2a38a3
Rename module 'memxfrm' to 'amemxfrm'.
Bruno Haible <bruno@clisp.org>
parents:
diff
changeset
|
108 size_t new_allocated; |
8db1bc2a38a3
Rename module 'memxfrm' to 'amemxfrm'.
Bruno Haible <bruno@clisp.org>
parents:
diff
changeset
|
109 char *new_result; |
8db1bc2a38a3
Rename module 'memxfrm' to 'amemxfrm'.
Bruno Haible <bruno@clisp.org>
parents:
diff
changeset
|
110 |
8db1bc2a38a3
Rename module 'memxfrm' to 'amemxfrm'.
Bruno Haible <bruno@clisp.org>
parents:
diff
changeset
|
111 new_allocated = length + k + 1; |
8db1bc2a38a3
Rename module 'memxfrm' to 'amemxfrm'.
Bruno Haible <bruno@clisp.org>
parents:
diff
changeset
|
112 if (new_allocated < 2 * allocated) |
8db1bc2a38a3
Rename module 'memxfrm' to 'amemxfrm'.
Bruno Haible <bruno@clisp.org>
parents:
diff
changeset
|
113 new_allocated = 2 * allocated; |
8db1bc2a38a3
Rename module 'memxfrm' to 'amemxfrm'.
Bruno Haible <bruno@clisp.org>
parents:
diff
changeset
|
114 if (new_allocated < 64) |
8db1bc2a38a3
Rename module 'memxfrm' to 'amemxfrm'.
Bruno Haible <bruno@clisp.org>
parents:
diff
changeset
|
115 new_allocated = 64; |
8db1bc2a38a3
Rename module 'memxfrm' to 'amemxfrm'.
Bruno Haible <bruno@clisp.org>
parents:
diff
changeset
|
116 if (result == resultbuf) |
8db1bc2a38a3
Rename module 'memxfrm' to 'amemxfrm'.
Bruno Haible <bruno@clisp.org>
parents:
diff
changeset
|
117 new_result = (char *) malloc (new_allocated); |
8db1bc2a38a3
Rename module 'memxfrm' to 'amemxfrm'.
Bruno Haible <bruno@clisp.org>
parents:
diff
changeset
|
118 else |
8db1bc2a38a3
Rename module 'memxfrm' to 'amemxfrm'.
Bruno Haible <bruno@clisp.org>
parents:
diff
changeset
|
119 new_result = (char *) realloc (result, new_allocated); |
8db1bc2a38a3
Rename module 'memxfrm' to 'amemxfrm'.
Bruno Haible <bruno@clisp.org>
parents:
diff
changeset
|
120 if (new_result == NULL) |
8db1bc2a38a3
Rename module 'memxfrm' to 'amemxfrm'.
Bruno Haible <bruno@clisp.org>
parents:
diff
changeset
|
121 goto out_of_memory_1; |
8db1bc2a38a3
Rename module 'memxfrm' to 'amemxfrm'.
Bruno Haible <bruno@clisp.org>
parents:
diff
changeset
|
122 allocated = new_allocated; |
8db1bc2a38a3
Rename module 'memxfrm' to 'amemxfrm'.
Bruno Haible <bruno@clisp.org>
parents:
diff
changeset
|
123 result = new_result; |
8db1bc2a38a3
Rename module 'memxfrm' to 'amemxfrm'.
Bruno Haible <bruno@clisp.org>
parents:
diff
changeset
|
124 } |
8db1bc2a38a3
Rename module 'memxfrm' to 'amemxfrm'.
Bruno Haible <bruno@clisp.org>
parents:
diff
changeset
|
125 else |
8db1bc2a38a3
Rename module 'memxfrm' to 'amemxfrm'.
Bruno Haible <bruno@clisp.org>
parents:
diff
changeset
|
126 { |
8db1bc2a38a3
Rename module 'memxfrm' to 'amemxfrm'.
Bruno Haible <bruno@clisp.org>
parents:
diff
changeset
|
127 length += k; |
8db1bc2a38a3
Rename module 'memxfrm' to 'amemxfrm'.
Bruno Haible <bruno@clisp.org>
parents:
diff
changeset
|
128 break; |
8db1bc2a38a3
Rename module 'memxfrm' to 'amemxfrm'.
Bruno Haible <bruno@clisp.org>
parents:
diff
changeset
|
129 } |
8db1bc2a38a3
Rename module 'memxfrm' to 'amemxfrm'.
Bruno Haible <bruno@clisp.org>
parents:
diff
changeset
|
130 } |
8db1bc2a38a3
Rename module 'memxfrm' to 'amemxfrm'.
Bruno Haible <bruno@clisp.org>
parents:
diff
changeset
|
131 |
8db1bc2a38a3
Rename module 'memxfrm' to 'amemxfrm'.
Bruno Haible <bruno@clisp.org>
parents:
diff
changeset
|
132 p = p + l + 1; |
8db1bc2a38a3
Rename module 'memxfrm' to 'amemxfrm'.
Bruno Haible <bruno@clisp.org>
parents:
diff
changeset
|
133 if (p == p_end) |
8db1bc2a38a3
Rename module 'memxfrm' to 'amemxfrm'.
Bruno Haible <bruno@clisp.org>
parents:
diff
changeset
|
134 break; |
8db1bc2a38a3
Rename module 'memxfrm' to 'amemxfrm'.
Bruno Haible <bruno@clisp.org>
parents:
diff
changeset
|
135 result[length] = '\0'; |
8db1bc2a38a3
Rename module 'memxfrm' to 'amemxfrm'.
Bruno Haible <bruno@clisp.org>
parents:
diff
changeset
|
136 length++; |
8db1bc2a38a3
Rename module 'memxfrm' to 'amemxfrm'.
Bruno Haible <bruno@clisp.org>
parents:
diff
changeset
|
137 } |
8db1bc2a38a3
Rename module 'memxfrm' to 'amemxfrm'.
Bruno Haible <bruno@clisp.org>
parents:
diff
changeset
|
138 } |
8db1bc2a38a3
Rename module 'memxfrm' to 'amemxfrm'.
Bruno Haible <bruno@clisp.org>
parents:
diff
changeset
|
139 |
8db1bc2a38a3
Rename module 'memxfrm' to 'amemxfrm'.
Bruno Haible <bruno@clisp.org>
parents:
diff
changeset
|
140 /* Shrink the allocated memory if possible. |
8db1bc2a38a3
Rename module 'memxfrm' to 'amemxfrm'.
Bruno Haible <bruno@clisp.org>
parents:
diff
changeset
|
141 It is not worth calling realloc when length + 1 == allocated; it would |
8db1bc2a38a3
Rename module 'memxfrm' to 'amemxfrm'.
Bruno Haible <bruno@clisp.org>
parents:
diff
changeset
|
142 save just one byte. */ |
8db1bc2a38a3
Rename module 'memxfrm' to 'amemxfrm'.
Bruno Haible <bruno@clisp.org>
parents:
diff
changeset
|
143 if (result != resultbuf && length + 1 < allocated) |
8db1bc2a38a3
Rename module 'memxfrm' to 'amemxfrm'.
Bruno Haible <bruno@clisp.org>
parents:
diff
changeset
|
144 { |
8db1bc2a38a3
Rename module 'memxfrm' to 'amemxfrm'.
Bruno Haible <bruno@clisp.org>
parents:
diff
changeset
|
145 if ((length > 0 ? length : 1) <= *lengthp) |
8db1bc2a38a3
Rename module 'memxfrm' to 'amemxfrm'.
Bruno Haible <bruno@clisp.org>
parents:
diff
changeset
|
146 { |
8db1bc2a38a3
Rename module 'memxfrm' to 'amemxfrm'.
Bruno Haible <bruno@clisp.org>
parents:
diff
changeset
|
147 memcpy (resultbuf, result, length); |
8db1bc2a38a3
Rename module 'memxfrm' to 'amemxfrm'.
Bruno Haible <bruno@clisp.org>
parents:
diff
changeset
|
148 free (result); |
8db1bc2a38a3
Rename module 'memxfrm' to 'amemxfrm'.
Bruno Haible <bruno@clisp.org>
parents:
diff
changeset
|
149 result = resultbuf; |
8db1bc2a38a3
Rename module 'memxfrm' to 'amemxfrm'.
Bruno Haible <bruno@clisp.org>
parents:
diff
changeset
|
150 } |
8db1bc2a38a3
Rename module 'memxfrm' to 'amemxfrm'.
Bruno Haible <bruno@clisp.org>
parents:
diff
changeset
|
151 else |
8db1bc2a38a3
Rename module 'memxfrm' to 'amemxfrm'.
Bruno Haible <bruno@clisp.org>
parents:
diff
changeset
|
152 { |
8db1bc2a38a3
Rename module 'memxfrm' to 'amemxfrm'.
Bruno Haible <bruno@clisp.org>
parents:
diff
changeset
|
153 char *memory = (char *) realloc (result, length > 0 ? length : 1); |
8db1bc2a38a3
Rename module 'memxfrm' to 'amemxfrm'.
Bruno Haible <bruno@clisp.org>
parents:
diff
changeset
|
154 if (memory != NULL) |
8db1bc2a38a3
Rename module 'memxfrm' to 'amemxfrm'.
Bruno Haible <bruno@clisp.org>
parents:
diff
changeset
|
155 result = memory; |
8db1bc2a38a3
Rename module 'memxfrm' to 'amemxfrm'.
Bruno Haible <bruno@clisp.org>
parents:
diff
changeset
|
156 } |
8db1bc2a38a3
Rename module 'memxfrm' to 'amemxfrm'.
Bruno Haible <bruno@clisp.org>
parents:
diff
changeset
|
157 } |
8db1bc2a38a3
Rename module 'memxfrm' to 'amemxfrm'.
Bruno Haible <bruno@clisp.org>
parents:
diff
changeset
|
158 |
8db1bc2a38a3
Rename module 'memxfrm' to 'amemxfrm'.
Bruno Haible <bruno@clisp.org>
parents:
diff
changeset
|
159 s[n] = orig_sentinel; |
8db1bc2a38a3
Rename module 'memxfrm' to 'amemxfrm'.
Bruno Haible <bruno@clisp.org>
parents:
diff
changeset
|
160 *lengthp = length; |
8db1bc2a38a3
Rename module 'memxfrm' to 'amemxfrm'.
Bruno Haible <bruno@clisp.org>
parents:
diff
changeset
|
161 return result; |
8db1bc2a38a3
Rename module 'memxfrm' to 'amemxfrm'.
Bruno Haible <bruno@clisp.org>
parents:
diff
changeset
|
162 |
8db1bc2a38a3
Rename module 'memxfrm' to 'amemxfrm'.
Bruno Haible <bruno@clisp.org>
parents:
diff
changeset
|
163 fail: |
8db1bc2a38a3
Rename module 'memxfrm' to 'amemxfrm'.
Bruno Haible <bruno@clisp.org>
parents:
diff
changeset
|
164 { |
8db1bc2a38a3
Rename module 'memxfrm' to 'amemxfrm'.
Bruno Haible <bruno@clisp.org>
parents:
diff
changeset
|
165 int saved_errno = errno; |
8db1bc2a38a3
Rename module 'memxfrm' to 'amemxfrm'.
Bruno Haible <bruno@clisp.org>
parents:
diff
changeset
|
166 if (result != resultbuf) |
8db1bc2a38a3
Rename module 'memxfrm' to 'amemxfrm'.
Bruno Haible <bruno@clisp.org>
parents:
diff
changeset
|
167 free (result); |
8db1bc2a38a3
Rename module 'memxfrm' to 'amemxfrm'.
Bruno Haible <bruno@clisp.org>
parents:
diff
changeset
|
168 s[n] = orig_sentinel; |
8db1bc2a38a3
Rename module 'memxfrm' to 'amemxfrm'.
Bruno Haible <bruno@clisp.org>
parents:
diff
changeset
|
169 errno = saved_errno; |
8db1bc2a38a3
Rename module 'memxfrm' to 'amemxfrm'.
Bruno Haible <bruno@clisp.org>
parents:
diff
changeset
|
170 return NULL; |
8db1bc2a38a3
Rename module 'memxfrm' to 'amemxfrm'.
Bruno Haible <bruno@clisp.org>
parents:
diff
changeset
|
171 } |
8db1bc2a38a3
Rename module 'memxfrm' to 'amemxfrm'.
Bruno Haible <bruno@clisp.org>
parents:
diff
changeset
|
172 |
8db1bc2a38a3
Rename module 'memxfrm' to 'amemxfrm'.
Bruno Haible <bruno@clisp.org>
parents:
diff
changeset
|
173 out_of_memory_1: |
8db1bc2a38a3
Rename module 'memxfrm' to 'amemxfrm'.
Bruno Haible <bruno@clisp.org>
parents:
diff
changeset
|
174 if (result != resultbuf) |
8db1bc2a38a3
Rename module 'memxfrm' to 'amemxfrm'.
Bruno Haible <bruno@clisp.org>
parents:
diff
changeset
|
175 free (result); |
8db1bc2a38a3
Rename module 'memxfrm' to 'amemxfrm'.
Bruno Haible <bruno@clisp.org>
parents:
diff
changeset
|
176 s[n] = orig_sentinel; |
8db1bc2a38a3
Rename module 'memxfrm' to 'amemxfrm'.
Bruno Haible <bruno@clisp.org>
parents:
diff
changeset
|
177 out_of_memory_2: |
8db1bc2a38a3
Rename module 'memxfrm' to 'amemxfrm'.
Bruno Haible <bruno@clisp.org>
parents:
diff
changeset
|
178 errno = ENOMEM; |
8db1bc2a38a3
Rename module 'memxfrm' to 'amemxfrm'.
Bruno Haible <bruno@clisp.org>
parents:
diff
changeset
|
179 return NULL; |
8db1bc2a38a3
Rename module 'memxfrm' to 'amemxfrm'.
Bruno Haible <bruno@clisp.org>
parents:
diff
changeset
|
180 } |