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