This source file includes following definitions.
- amemxfrm
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18 #include <config.h>
19
20
21 #include "amemxfrm.h"
22
23 #include <errno.h>
24 #include <stdlib.h>
25 #include <string.h>
26
27 char *
28 amemxfrm (char *s, size_t n, char *resultbuf, size_t *lengthp)
29 {
30
31 char *result;
32 size_t length;
33 size_t allocated;
34
35 char orig_sentinel;
36
37
38 if (resultbuf != NULL && *lengthp > 0)
39 {
40 result = resultbuf;
41 allocated = *lengthp;
42 }
43 else
44 {
45 allocated = (n > 0 ? n : 1);
46 result = (char *) malloc (allocated);
47 if (result == NULL)
48 goto out_of_memory_2;
49 }
50 length = 0;
51
52
53 orig_sentinel = s[n];
54 s[n] = '\0';
55
56
57
58
59 {
60 const char *p_end = s + n + 1;
61 const char *p;
62
63 p = s;
64 for (;;)
65 {
66
67 size_t l = strlen (p);
68
69 for (;;)
70 {
71 size_t k;
72
73
74
75
76
77
78
79 if (3 * l >= allocated - length)
80 {
81
82 size_t new_allocated;
83 char *new_result;
84
85 new_allocated = length + 3 * l + 1;
86 if (new_allocated < 2 * allocated)
87 new_allocated = 2 * allocated;
88 if (new_allocated < 64)
89 new_allocated = 64;
90 if (result == resultbuf)
91 new_result = (char *) malloc (new_allocated);
92 else
93 new_result = (char *) realloc (result, new_allocated);
94 if (new_result != NULL)
95 {
96 allocated = new_allocated;
97 result = new_result;
98 }
99 }
100
101 errno = 0;
102 k = strxfrm (result + length, p, allocated - length);
103 if (errno != 0)
104 goto fail;
105 if (k >= allocated - length)
106 {
107
108 size_t new_allocated;
109 char *new_result;
110
111 new_allocated = length + k + 1;
112 if (new_allocated < 2 * allocated)
113 new_allocated = 2 * allocated;
114 if (new_allocated < 64)
115 new_allocated = 64;
116 if (result == resultbuf)
117 new_result = (char *) malloc (new_allocated);
118 else
119 new_result = (char *) realloc (result, new_allocated);
120 if (new_result == NULL)
121 goto out_of_memory_1;
122 allocated = new_allocated;
123 result = new_result;
124 }
125 else
126 {
127 length += k;
128 break;
129 }
130 }
131
132 p = p + l + 1;
133 if (p == p_end)
134 break;
135 result[length] = '\0';
136 length++;
137 }
138 }
139
140
141
142
143 if (result != resultbuf && length + 1 < allocated)
144 {
145 if ((length > 0 ? length : 1) <= *lengthp)
146 {
147 memcpy (resultbuf, result, length);
148 free (result);
149 result = resultbuf;
150 }
151 else
152 {
153 char *memory = (char *) realloc (result, length > 0 ? length : 1);
154 if (memory != NULL)
155 result = memory;
156 }
157 }
158
159 s[n] = orig_sentinel;
160 *lengthp = length;
161 return result;
162
163 fail:
164 if (result != resultbuf)
165 free (result);
166 s[n] = orig_sentinel;
167 return NULL;
168
169 out_of_memory_1:
170 if (result != resultbuf)
171 free (result);
172 s[n] = orig_sentinel;
173 out_of_memory_2:
174 errno = ENOMEM;
175 return NULL;
176 }