annotate lib/astrxfrm.c @ 14379:2330aac2ae54

maint: adjust cpp indentation to reflect nesting depth I.e., in a block of code that begins with an unnested "#if", put one space between the "#" in column 1 and following token. For example, -#include <sys/vfs.h> +# include <sys/vfs.h> Do this only in .c files that are part of a module I maintain. * lib/linkat.c: Filter through cppi. * lib/nanosleep.c: Likewise. * lib/openat.c: Likewise. * lib/openat-die.c: Likewise. * lib/dup3.c: Likewise. * lib/fchownat.c: Likewise. * lib/flock.c: Likewise. * lib/fsync.c: Likewise. * lib/fts.c: Likewise. * lib/getpass.c: Likewise. * lib/gettimeofday.c: Likewise. * lib/userspec.c: Likewise. * Makefile (sc_cpp_indent_check): New rule, to check this.
author Jim Meyering <meyering@redhat.com>
date Sun, 20 Feb 2011 23:02:43 +0100
parents 97fc9a21a8fb
children 8250f2777afc
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
rev   line source
13555
8ad4ee9c2fd3 New module 'astrxfrm'.
Bruno Haible <bruno@clisp.org>
parents:
diff changeset
1 /* Locale dependent string transformation for comparison.
14079
97fc9a21a8fb maint: update almost all copyright ranges to include 2011
Jim Meyering <meyering@redhat.com>
parents: 13555
diff changeset
2 Copyright (C) 2010-2011 Free Software Foundation, Inc.
13555
8ad4ee9c2fd3 New module 'astrxfrm'.
Bruno Haible <bruno@clisp.org>
parents:
diff changeset
3 Written by Bruno Haible <bruno@clisp.org>, 2010.
8ad4ee9c2fd3 New module 'astrxfrm'.
Bruno Haible <bruno@clisp.org>
parents:
diff changeset
4
8ad4ee9c2fd3 New module 'astrxfrm'.
Bruno Haible <bruno@clisp.org>
parents:
diff changeset
5 This program is free software: you can redistribute it and/or modify it
8ad4ee9c2fd3 New module 'astrxfrm'.
Bruno Haible <bruno@clisp.org>
parents:
diff changeset
6 under the terms of the GNU Lesser General Public License as published
8ad4ee9c2fd3 New module 'astrxfrm'.
Bruno Haible <bruno@clisp.org>
parents:
diff changeset
7 by the Free Software Foundation; either version 3 of the License, or
8ad4ee9c2fd3 New module 'astrxfrm'.
Bruno Haible <bruno@clisp.org>
parents:
diff changeset
8 (at your option) any later version.
8ad4ee9c2fd3 New module 'astrxfrm'.
Bruno Haible <bruno@clisp.org>
parents:
diff changeset
9
8ad4ee9c2fd3 New module 'astrxfrm'.
Bruno Haible <bruno@clisp.org>
parents:
diff changeset
10 This program is distributed in the hope that it will be useful,
8ad4ee9c2fd3 New module 'astrxfrm'.
Bruno Haible <bruno@clisp.org>
parents:
diff changeset
11 but WITHOUT ANY WARRANTY; without even the implied warranty of
8ad4ee9c2fd3 New module 'astrxfrm'.
Bruno Haible <bruno@clisp.org>
parents:
diff changeset
12 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
8ad4ee9c2fd3 New module 'astrxfrm'.
Bruno Haible <bruno@clisp.org>
parents:
diff changeset
13 Lesser General Public License for more details.
8ad4ee9c2fd3 New module 'astrxfrm'.
Bruno Haible <bruno@clisp.org>
parents:
diff changeset
14
8ad4ee9c2fd3 New module 'astrxfrm'.
Bruno Haible <bruno@clisp.org>
parents:
diff changeset
15 You should have received a copy of the GNU Lesser General Public License
8ad4ee9c2fd3 New module 'astrxfrm'.
Bruno Haible <bruno@clisp.org>
parents:
diff changeset
16 along with this program. If not, see <http://www.gnu.org/licenses/>. */
8ad4ee9c2fd3 New module 'astrxfrm'.
Bruno Haible <bruno@clisp.org>
parents:
diff changeset
17
8ad4ee9c2fd3 New module 'astrxfrm'.
Bruno Haible <bruno@clisp.org>
parents:
diff changeset
18 #include <config.h>
8ad4ee9c2fd3 New module 'astrxfrm'.
Bruno Haible <bruno@clisp.org>
parents:
diff changeset
19
8ad4ee9c2fd3 New module 'astrxfrm'.
Bruno Haible <bruno@clisp.org>
parents:
diff changeset
20 /* Specification. */
8ad4ee9c2fd3 New module 'astrxfrm'.
Bruno Haible <bruno@clisp.org>
parents:
diff changeset
21 #include "astrxfrm.h"
8ad4ee9c2fd3 New module 'astrxfrm'.
Bruno Haible <bruno@clisp.org>
parents:
diff changeset
22
8ad4ee9c2fd3 New module 'astrxfrm'.
Bruno Haible <bruno@clisp.org>
parents:
diff changeset
23 #include <errno.h>
8ad4ee9c2fd3 New module 'astrxfrm'.
Bruno Haible <bruno@clisp.org>
parents:
diff changeset
24 #include <stdlib.h>
8ad4ee9c2fd3 New module 'astrxfrm'.
Bruno Haible <bruno@clisp.org>
parents:
diff changeset
25 #include <string.h>
8ad4ee9c2fd3 New module 'astrxfrm'.
Bruno Haible <bruno@clisp.org>
parents:
diff changeset
26
8ad4ee9c2fd3 New module 'astrxfrm'.
Bruno Haible <bruno@clisp.org>
parents:
diff changeset
27 char *
8ad4ee9c2fd3 New module 'astrxfrm'.
Bruno Haible <bruno@clisp.org>
parents:
diff changeset
28 astrxfrm (const char *s, char *resultbuf, size_t *lengthp)
8ad4ee9c2fd3 New module 'astrxfrm'.
Bruno Haible <bruno@clisp.org>
parents:
diff changeset
29 {
8ad4ee9c2fd3 New module 'astrxfrm'.
Bruno Haible <bruno@clisp.org>
parents:
diff changeset
30 char tmpbuf[4000];
8ad4ee9c2fd3 New module 'astrxfrm'.
Bruno Haible <bruno@clisp.org>
parents:
diff changeset
31 char *result; /* either == resultbuf or == tmpbuf or freshly allocated
8ad4ee9c2fd3 New module 'astrxfrm'.
Bruno Haible <bruno@clisp.org>
parents:
diff changeset
32 or NULL. */
8ad4ee9c2fd3 New module 'astrxfrm'.
Bruno Haible <bruno@clisp.org>
parents:
diff changeset
33 size_t allocated; /* number of bytes allocated at result */
8ad4ee9c2fd3 New module 'astrxfrm'.
Bruno Haible <bruno@clisp.org>
parents:
diff changeset
34 size_t length;
8ad4ee9c2fd3 New module 'astrxfrm'.
Bruno Haible <bruno@clisp.org>
parents:
diff changeset
35
8ad4ee9c2fd3 New module 'astrxfrm'.
Bruno Haible <bruno@clisp.org>
parents:
diff changeset
36 if (resultbuf != NULL)
8ad4ee9c2fd3 New module 'astrxfrm'.
Bruno Haible <bruno@clisp.org>
parents:
diff changeset
37 {
8ad4ee9c2fd3 New module 'astrxfrm'.
Bruno Haible <bruno@clisp.org>
parents:
diff changeset
38 result = resultbuf;
8ad4ee9c2fd3 New module 'astrxfrm'.
Bruno Haible <bruno@clisp.org>
parents:
diff changeset
39 allocated = *lengthp;
8ad4ee9c2fd3 New module 'astrxfrm'.
Bruno Haible <bruno@clisp.org>
parents:
diff changeset
40 }
8ad4ee9c2fd3 New module 'astrxfrm'.
Bruno Haible <bruno@clisp.org>
parents:
diff changeset
41 else
8ad4ee9c2fd3 New module 'astrxfrm'.
Bruno Haible <bruno@clisp.org>
parents:
diff changeset
42 {
8ad4ee9c2fd3 New module 'astrxfrm'.
Bruno Haible <bruno@clisp.org>
parents:
diff changeset
43 result = NULL;
8ad4ee9c2fd3 New module 'astrxfrm'.
Bruno Haible <bruno@clisp.org>
parents:
diff changeset
44 allocated = 0;
8ad4ee9c2fd3 New module 'astrxfrm'.
Bruno Haible <bruno@clisp.org>
parents:
diff changeset
45 }
8ad4ee9c2fd3 New module 'astrxfrm'.
Bruno Haible <bruno@clisp.org>
parents:
diff changeset
46
8ad4ee9c2fd3 New module 'astrxfrm'.
Bruno Haible <bruno@clisp.org>
parents:
diff changeset
47 {
8ad4ee9c2fd3 New module 'astrxfrm'.
Bruno Haible <bruno@clisp.org>
parents:
diff changeset
48 size_t l = strlen (s);
8ad4ee9c2fd3 New module 'astrxfrm'.
Bruno Haible <bruno@clisp.org>
parents:
diff changeset
49 size_t k;
8ad4ee9c2fd3 New module 'astrxfrm'.
Bruno Haible <bruno@clisp.org>
parents:
diff changeset
50
8ad4ee9c2fd3 New module 'astrxfrm'.
Bruno Haible <bruno@clisp.org>
parents:
diff changeset
51 /* A call to strxfrm costs about 20 times more than a call to strdup of
8ad4ee9c2fd3 New module 'astrxfrm'.
Bruno Haible <bruno@clisp.org>
parents:
diff changeset
52 the result. Therefore it is worth to try to avoid calling strxfrm
8ad4ee9c2fd3 New module 'astrxfrm'.
Bruno Haible <bruno@clisp.org>
parents:
diff changeset
53 more than once on a given string, by making enough room before calling
8ad4ee9c2fd3 New module 'astrxfrm'.
Bruno Haible <bruno@clisp.org>
parents:
diff changeset
54 strxfrm. The size of the strxfrm result, k, is likely to be between
8ad4ee9c2fd3 New module 'astrxfrm'.
Bruno Haible <bruno@clisp.org>
parents:
diff changeset
55 l and 3 * l. */
8ad4ee9c2fd3 New module 'astrxfrm'.
Bruno Haible <bruno@clisp.org>
parents:
diff changeset
56 if (3 * l + 1 > allocated)
8ad4ee9c2fd3 New module 'astrxfrm'.
Bruno Haible <bruno@clisp.org>
parents:
diff changeset
57 {
8ad4ee9c2fd3 New module 'astrxfrm'.
Bruno Haible <bruno@clisp.org>
parents:
diff changeset
58 /* Grow the result buffer. */
8ad4ee9c2fd3 New module 'astrxfrm'.
Bruno Haible <bruno@clisp.org>
parents:
diff changeset
59 if (3 * l + 1 <= sizeof (tmpbuf))
8ad4ee9c2fd3 New module 'astrxfrm'.
Bruno Haible <bruno@clisp.org>
parents:
diff changeset
60 {
8ad4ee9c2fd3 New module 'astrxfrm'.
Bruno Haible <bruno@clisp.org>
parents:
diff changeset
61 result = tmpbuf;
8ad4ee9c2fd3 New module 'astrxfrm'.
Bruno Haible <bruno@clisp.org>
parents:
diff changeset
62 allocated = sizeof (tmpbuf);
8ad4ee9c2fd3 New module 'astrxfrm'.
Bruno Haible <bruno@clisp.org>
parents:
diff changeset
63 }
8ad4ee9c2fd3 New module 'astrxfrm'.
Bruno Haible <bruno@clisp.org>
parents:
diff changeset
64 else
8ad4ee9c2fd3 New module 'astrxfrm'.
Bruno Haible <bruno@clisp.org>
parents:
diff changeset
65 {
8ad4ee9c2fd3 New module 'astrxfrm'.
Bruno Haible <bruno@clisp.org>
parents:
diff changeset
66 size_t new_allocated;
8ad4ee9c2fd3 New module 'astrxfrm'.
Bruno Haible <bruno@clisp.org>
parents:
diff changeset
67 char *new_result;
8ad4ee9c2fd3 New module 'astrxfrm'.
Bruno Haible <bruno@clisp.org>
parents:
diff changeset
68
8ad4ee9c2fd3 New module 'astrxfrm'.
Bruno Haible <bruno@clisp.org>
parents:
diff changeset
69 new_allocated = 3 * l + 1;
8ad4ee9c2fd3 New module 'astrxfrm'.
Bruno Haible <bruno@clisp.org>
parents:
diff changeset
70 if (new_allocated < 2 * allocated)
8ad4ee9c2fd3 New module 'astrxfrm'.
Bruno Haible <bruno@clisp.org>
parents:
diff changeset
71 new_allocated = 2 * allocated;
8ad4ee9c2fd3 New module 'astrxfrm'.
Bruno Haible <bruno@clisp.org>
parents:
diff changeset
72 new_result = (char *) malloc (new_allocated);
8ad4ee9c2fd3 New module 'astrxfrm'.
Bruno Haible <bruno@clisp.org>
parents:
diff changeset
73 if (new_result != NULL)
8ad4ee9c2fd3 New module 'astrxfrm'.
Bruno Haible <bruno@clisp.org>
parents:
diff changeset
74 {
8ad4ee9c2fd3 New module 'astrxfrm'.
Bruno Haible <bruno@clisp.org>
parents:
diff changeset
75 allocated = new_allocated;
8ad4ee9c2fd3 New module 'astrxfrm'.
Bruno Haible <bruno@clisp.org>
parents:
diff changeset
76 result = new_result;
8ad4ee9c2fd3 New module 'astrxfrm'.
Bruno Haible <bruno@clisp.org>
parents:
diff changeset
77 }
8ad4ee9c2fd3 New module 'astrxfrm'.
Bruno Haible <bruno@clisp.org>
parents:
diff changeset
78 }
8ad4ee9c2fd3 New module 'astrxfrm'.
Bruno Haible <bruno@clisp.org>
parents:
diff changeset
79 }
8ad4ee9c2fd3 New module 'astrxfrm'.
Bruno Haible <bruno@clisp.org>
parents:
diff changeset
80
8ad4ee9c2fd3 New module 'astrxfrm'.
Bruno Haible <bruno@clisp.org>
parents:
diff changeset
81 errno = 0;
8ad4ee9c2fd3 New module 'astrxfrm'.
Bruno Haible <bruno@clisp.org>
parents:
diff changeset
82 k = strxfrm (result, s, allocated);
8ad4ee9c2fd3 New module 'astrxfrm'.
Bruno Haible <bruno@clisp.org>
parents:
diff changeset
83 if (errno != 0)
8ad4ee9c2fd3 New module 'astrxfrm'.
Bruno Haible <bruno@clisp.org>
parents:
diff changeset
84 goto fail;
8ad4ee9c2fd3 New module 'astrxfrm'.
Bruno Haible <bruno@clisp.org>
parents:
diff changeset
85 if (k >= allocated)
8ad4ee9c2fd3 New module 'astrxfrm'.
Bruno Haible <bruno@clisp.org>
parents:
diff changeset
86 {
8ad4ee9c2fd3 New module 'astrxfrm'.
Bruno Haible <bruno@clisp.org>
parents:
diff changeset
87 /* Grow the result buffer. */
8ad4ee9c2fd3 New module 'astrxfrm'.
Bruno Haible <bruno@clisp.org>
parents:
diff changeset
88 if (result != resultbuf && result != tmpbuf)
8ad4ee9c2fd3 New module 'astrxfrm'.
Bruno Haible <bruno@clisp.org>
parents:
diff changeset
89 free (result);
8ad4ee9c2fd3 New module 'astrxfrm'.
Bruno Haible <bruno@clisp.org>
parents:
diff changeset
90 if (k + 1 <= sizeof (tmpbuf))
8ad4ee9c2fd3 New module 'astrxfrm'.
Bruno Haible <bruno@clisp.org>
parents:
diff changeset
91 {
8ad4ee9c2fd3 New module 'astrxfrm'.
Bruno Haible <bruno@clisp.org>
parents:
diff changeset
92 result = tmpbuf;
8ad4ee9c2fd3 New module 'astrxfrm'.
Bruno Haible <bruno@clisp.org>
parents:
diff changeset
93 allocated = sizeof (tmpbuf);
8ad4ee9c2fd3 New module 'astrxfrm'.
Bruno Haible <bruno@clisp.org>
parents:
diff changeset
94 }
8ad4ee9c2fd3 New module 'astrxfrm'.
Bruno Haible <bruno@clisp.org>
parents:
diff changeset
95 else
8ad4ee9c2fd3 New module 'astrxfrm'.
Bruno Haible <bruno@clisp.org>
parents:
diff changeset
96 {
8ad4ee9c2fd3 New module 'astrxfrm'.
Bruno Haible <bruno@clisp.org>
parents:
diff changeset
97 size_t new_allocated;
8ad4ee9c2fd3 New module 'astrxfrm'.
Bruno Haible <bruno@clisp.org>
parents:
diff changeset
98 char *new_result;
8ad4ee9c2fd3 New module 'astrxfrm'.
Bruno Haible <bruno@clisp.org>
parents:
diff changeset
99
8ad4ee9c2fd3 New module 'astrxfrm'.
Bruno Haible <bruno@clisp.org>
parents:
diff changeset
100 new_allocated = k + 1;
8ad4ee9c2fd3 New module 'astrxfrm'.
Bruno Haible <bruno@clisp.org>
parents:
diff changeset
101 new_result = (char *) malloc (new_allocated);
8ad4ee9c2fd3 New module 'astrxfrm'.
Bruno Haible <bruno@clisp.org>
parents:
diff changeset
102 if (new_result == NULL)
8ad4ee9c2fd3 New module 'astrxfrm'.
Bruno Haible <bruno@clisp.org>
parents:
diff changeset
103 goto out_of_memory;
8ad4ee9c2fd3 New module 'astrxfrm'.
Bruno Haible <bruno@clisp.org>
parents:
diff changeset
104 allocated = new_allocated;
8ad4ee9c2fd3 New module 'astrxfrm'.
Bruno Haible <bruno@clisp.org>
parents:
diff changeset
105 result = new_result;
8ad4ee9c2fd3 New module 'astrxfrm'.
Bruno Haible <bruno@clisp.org>
parents:
diff changeset
106 }
8ad4ee9c2fd3 New module 'astrxfrm'.
Bruno Haible <bruno@clisp.org>
parents:
diff changeset
107 /* Here k < allocated. */
8ad4ee9c2fd3 New module 'astrxfrm'.
Bruno Haible <bruno@clisp.org>
parents:
diff changeset
108
8ad4ee9c2fd3 New module 'astrxfrm'.
Bruno Haible <bruno@clisp.org>
parents:
diff changeset
109 /* Try again. */
8ad4ee9c2fd3 New module 'astrxfrm'.
Bruno Haible <bruno@clisp.org>
parents:
diff changeset
110 errno = 0;
8ad4ee9c2fd3 New module 'astrxfrm'.
Bruno Haible <bruno@clisp.org>
parents:
diff changeset
111 if (strxfrm (result, s, allocated) != k)
8ad4ee9c2fd3 New module 'astrxfrm'.
Bruno Haible <bruno@clisp.org>
parents:
diff changeset
112 /* strxfrm() is not producing reproducible results. */
8ad4ee9c2fd3 New module 'astrxfrm'.
Bruno Haible <bruno@clisp.org>
parents:
diff changeset
113 abort ();
8ad4ee9c2fd3 New module 'astrxfrm'.
Bruno Haible <bruno@clisp.org>
parents:
diff changeset
114 if (errno != 0)
8ad4ee9c2fd3 New module 'astrxfrm'.
Bruno Haible <bruno@clisp.org>
parents:
diff changeset
115 goto fail;
8ad4ee9c2fd3 New module 'astrxfrm'.
Bruno Haible <bruno@clisp.org>
parents:
diff changeset
116 }
8ad4ee9c2fd3 New module 'astrxfrm'.
Bruno Haible <bruno@clisp.org>
parents:
diff changeset
117
8ad4ee9c2fd3 New module 'astrxfrm'.
Bruno Haible <bruno@clisp.org>
parents:
diff changeset
118 /* Verify that strxfrm() has NUL-terminated the result. */
8ad4ee9c2fd3 New module 'astrxfrm'.
Bruno Haible <bruno@clisp.org>
parents:
diff changeset
119 if (result[k] != '\0')
8ad4ee9c2fd3 New module 'astrxfrm'.
Bruno Haible <bruno@clisp.org>
parents:
diff changeset
120 abort ();
8ad4ee9c2fd3 New module 'astrxfrm'.
Bruno Haible <bruno@clisp.org>
parents:
diff changeset
121 length = k + 1;
8ad4ee9c2fd3 New module 'astrxfrm'.
Bruno Haible <bruno@clisp.org>
parents:
diff changeset
122 }
8ad4ee9c2fd3 New module 'astrxfrm'.
Bruno Haible <bruno@clisp.org>
parents:
diff changeset
123
8ad4ee9c2fd3 New module 'astrxfrm'.
Bruno Haible <bruno@clisp.org>
parents:
diff changeset
124 /* Here length > 0. */
8ad4ee9c2fd3 New module 'astrxfrm'.
Bruno Haible <bruno@clisp.org>
parents:
diff changeset
125
8ad4ee9c2fd3 New module 'astrxfrm'.
Bruno Haible <bruno@clisp.org>
parents:
diff changeset
126 if (result == tmpbuf)
8ad4ee9c2fd3 New module 'astrxfrm'.
Bruno Haible <bruno@clisp.org>
parents:
diff changeset
127 {
8ad4ee9c2fd3 New module 'astrxfrm'.
Bruno Haible <bruno@clisp.org>
parents:
diff changeset
128 if (resultbuf != NULL && length <= *lengthp)
8ad4ee9c2fd3 New module 'astrxfrm'.
Bruno Haible <bruno@clisp.org>
parents:
diff changeset
129 {
8ad4ee9c2fd3 New module 'astrxfrm'.
Bruno Haible <bruno@clisp.org>
parents:
diff changeset
130 memcpy (resultbuf, result, length);
8ad4ee9c2fd3 New module 'astrxfrm'.
Bruno Haible <bruno@clisp.org>
parents:
diff changeset
131 result = resultbuf;
8ad4ee9c2fd3 New module 'astrxfrm'.
Bruno Haible <bruno@clisp.org>
parents:
diff changeset
132 }
8ad4ee9c2fd3 New module 'astrxfrm'.
Bruno Haible <bruno@clisp.org>
parents:
diff changeset
133 else
8ad4ee9c2fd3 New module 'astrxfrm'.
Bruno Haible <bruno@clisp.org>
parents:
diff changeset
134 {
8ad4ee9c2fd3 New module 'astrxfrm'.
Bruno Haible <bruno@clisp.org>
parents:
diff changeset
135 char *memory = (char *) malloc (length);
8ad4ee9c2fd3 New module 'astrxfrm'.
Bruno Haible <bruno@clisp.org>
parents:
diff changeset
136
8ad4ee9c2fd3 New module 'astrxfrm'.
Bruno Haible <bruno@clisp.org>
parents:
diff changeset
137 if (memory == NULL)
8ad4ee9c2fd3 New module 'astrxfrm'.
Bruno Haible <bruno@clisp.org>
parents:
diff changeset
138 goto out_of_memory;
8ad4ee9c2fd3 New module 'astrxfrm'.
Bruno Haible <bruno@clisp.org>
parents:
diff changeset
139 memcpy (memory, result, length);
8ad4ee9c2fd3 New module 'astrxfrm'.
Bruno Haible <bruno@clisp.org>
parents:
diff changeset
140 result = memory;
8ad4ee9c2fd3 New module 'astrxfrm'.
Bruno Haible <bruno@clisp.org>
parents:
diff changeset
141 }
8ad4ee9c2fd3 New module 'astrxfrm'.
Bruno Haible <bruno@clisp.org>
parents:
diff changeset
142 }
8ad4ee9c2fd3 New module 'astrxfrm'.
Bruno Haible <bruno@clisp.org>
parents:
diff changeset
143 else
8ad4ee9c2fd3 New module 'astrxfrm'.
Bruno Haible <bruno@clisp.org>
parents:
diff changeset
144 {
8ad4ee9c2fd3 New module 'astrxfrm'.
Bruno Haible <bruno@clisp.org>
parents:
diff changeset
145 /* Shrink the allocated memory if possible. */
8ad4ee9c2fd3 New module 'astrxfrm'.
Bruno Haible <bruno@clisp.org>
parents:
diff changeset
146 if (result != resultbuf && length < allocated)
8ad4ee9c2fd3 New module 'astrxfrm'.
Bruno Haible <bruno@clisp.org>
parents:
diff changeset
147 {
8ad4ee9c2fd3 New module 'astrxfrm'.
Bruno Haible <bruno@clisp.org>
parents:
diff changeset
148 if (length <= *lengthp)
8ad4ee9c2fd3 New module 'astrxfrm'.
Bruno Haible <bruno@clisp.org>
parents:
diff changeset
149 {
8ad4ee9c2fd3 New module 'astrxfrm'.
Bruno Haible <bruno@clisp.org>
parents:
diff changeset
150 memcpy (resultbuf, result, length);
8ad4ee9c2fd3 New module 'astrxfrm'.
Bruno Haible <bruno@clisp.org>
parents:
diff changeset
151 free (result);
8ad4ee9c2fd3 New module 'astrxfrm'.
Bruno Haible <bruno@clisp.org>
parents:
diff changeset
152 result = resultbuf;
8ad4ee9c2fd3 New module 'astrxfrm'.
Bruno Haible <bruno@clisp.org>
parents:
diff changeset
153 }
8ad4ee9c2fd3 New module 'astrxfrm'.
Bruno Haible <bruno@clisp.org>
parents:
diff changeset
154 else
8ad4ee9c2fd3 New module 'astrxfrm'.
Bruno Haible <bruno@clisp.org>
parents:
diff changeset
155 {
8ad4ee9c2fd3 New module 'astrxfrm'.
Bruno Haible <bruno@clisp.org>
parents:
diff changeset
156 char *memory = (char *) realloc (result, length);
8ad4ee9c2fd3 New module 'astrxfrm'.
Bruno Haible <bruno@clisp.org>
parents:
diff changeset
157 if (memory != NULL)
8ad4ee9c2fd3 New module 'astrxfrm'.
Bruno Haible <bruno@clisp.org>
parents:
diff changeset
158 {
8ad4ee9c2fd3 New module 'astrxfrm'.
Bruno Haible <bruno@clisp.org>
parents:
diff changeset
159 memcpy (memory, result, length);
8ad4ee9c2fd3 New module 'astrxfrm'.
Bruno Haible <bruno@clisp.org>
parents:
diff changeset
160 result = memory;
8ad4ee9c2fd3 New module 'astrxfrm'.
Bruno Haible <bruno@clisp.org>
parents:
diff changeset
161 }
8ad4ee9c2fd3 New module 'astrxfrm'.
Bruno Haible <bruno@clisp.org>
parents:
diff changeset
162 }
8ad4ee9c2fd3 New module 'astrxfrm'.
Bruno Haible <bruno@clisp.org>
parents:
diff changeset
163 }
8ad4ee9c2fd3 New module 'astrxfrm'.
Bruno Haible <bruno@clisp.org>
parents:
diff changeset
164 }
8ad4ee9c2fd3 New module 'astrxfrm'.
Bruno Haible <bruno@clisp.org>
parents:
diff changeset
165
8ad4ee9c2fd3 New module 'astrxfrm'.
Bruno Haible <bruno@clisp.org>
parents:
diff changeset
166 *lengthp = length;
8ad4ee9c2fd3 New module 'astrxfrm'.
Bruno Haible <bruno@clisp.org>
parents:
diff changeset
167 return result;
8ad4ee9c2fd3 New module 'astrxfrm'.
Bruno Haible <bruno@clisp.org>
parents:
diff changeset
168
8ad4ee9c2fd3 New module 'astrxfrm'.
Bruno Haible <bruno@clisp.org>
parents:
diff changeset
169 fail:
8ad4ee9c2fd3 New module 'astrxfrm'.
Bruno Haible <bruno@clisp.org>
parents:
diff changeset
170 {
8ad4ee9c2fd3 New module 'astrxfrm'.
Bruno Haible <bruno@clisp.org>
parents:
diff changeset
171 int saved_errno = errno;
8ad4ee9c2fd3 New module 'astrxfrm'.
Bruno Haible <bruno@clisp.org>
parents:
diff changeset
172 if (result != resultbuf && result != tmpbuf)
8ad4ee9c2fd3 New module 'astrxfrm'.
Bruno Haible <bruno@clisp.org>
parents:
diff changeset
173 free (result);
8ad4ee9c2fd3 New module 'astrxfrm'.
Bruno Haible <bruno@clisp.org>
parents:
diff changeset
174 errno = saved_errno;
8ad4ee9c2fd3 New module 'astrxfrm'.
Bruno Haible <bruno@clisp.org>
parents:
diff changeset
175 return NULL;
8ad4ee9c2fd3 New module 'astrxfrm'.
Bruno Haible <bruno@clisp.org>
parents:
diff changeset
176 }
8ad4ee9c2fd3 New module 'astrxfrm'.
Bruno Haible <bruno@clisp.org>
parents:
diff changeset
177
8ad4ee9c2fd3 New module 'astrxfrm'.
Bruno Haible <bruno@clisp.org>
parents:
diff changeset
178 out_of_memory:
8ad4ee9c2fd3 New module 'astrxfrm'.
Bruno Haible <bruno@clisp.org>
parents:
diff changeset
179 errno = ENOMEM;
8ad4ee9c2fd3 New module 'astrxfrm'.
Bruno Haible <bruno@clisp.org>
parents:
diff changeset
180 return NULL;
8ad4ee9c2fd3 New module 'astrxfrm'.
Bruno Haible <bruno@clisp.org>
parents:
diff changeset
181 }