2525#include "strv.h"
2626#include "utf8.h"
2727
28- static char * normalize_locale (const char * name ) {
28+ static char * normalize_locale (const char * name ) {
2929 const char * e ;
3030
3131 /* Locale names are weird: glibc has some magic rules when looking for the charset name on disk: it
@@ -93,18 +93,15 @@ static int add_locales_from_archive(Set *locales) {
9393 uint32_t locrec_offset ;
9494 };
9595
96- const struct locarhead * h ;
97- const struct namehashent * e ;
98- const void * p = MAP_FAILED ;
99- _cleanup_close_ int fd = - EBADF ;
100- size_t sz = 0 ;
101- struct stat st ;
10296 int r ;
10397
104- fd = open ("/usr/lib/locale/locale-archive" , O_RDONLY |O_NOCTTY |O_CLOEXEC );
98+ assert (locales );
99+
100+ _cleanup_close_ int fd = open ("/usr/lib/locale/locale-archive" , O_RDONLY |O_NOCTTY |O_CLOEXEC );
105101 if (fd < 0 )
106102 return errno == ENOENT ? 0 : - errno ;
107103
104+ struct stat st ;
108105 if (fstat (fd , & st ) < 0 )
109106 return - errno ;
110107
@@ -117,11 +114,12 @@ static int add_locales_from_archive(Set *locales) {
117114 if (file_offset_beyond_memory_size (st .st_size ))
118115 return - EFBIG ;
119116
120- p = mmap (NULL , st .st_size , PROT_READ , MAP_SHARED , fd , 0 );
117+ void * p = mmap (NULL , st .st_size , PROT_READ , MAP_SHARED , fd , 0 );
121118 if (p == MAP_FAILED )
122119 return - errno ;
123120
124- h = (const struct locarhead * ) p ;
121+ const struct namehashent * e ;
122+ const struct locarhead * h = p ;
125123 if (h -> magic != 0xde020109 ||
126124 h -> namehash_offset + h -> namehash_size > st .st_size ||
127125 h -> string_offset + h -> string_size > st .st_size ||
@@ -154,9 +152,9 @@ static int add_locales_from_archive(Set *locales) {
154152
155153 r = 0 ;
156154
157- finish :
155+ finish :
158156 if (p != MAP_FAILED )
159- munmap ((void * ) p , sz );
157+ munmap ((void * ) p , st . st_size );
160158
161159 return r ;
162160}
@@ -165,6 +163,8 @@ static int add_locales_from_libdir(Set *locales) {
165163 _cleanup_closedir_ DIR * dir = NULL ;
166164 int r ;
167165
166+ assert (locales );
167+
168168 dir = opendir ("/usr/lib/locale" );
169169 if (!dir )
170170 return errno == ENOENT ? 0 : - errno ;
@@ -180,19 +180,18 @@ static int add_locales_from_libdir(Set *locales) {
180180 return - ENOMEM ;
181181
182182 r = set_consume (locales , z );
183- if (r < 0 && r != - EEXIST )
183+ if (r < 0 )
184184 return r ;
185185 }
186186
187187 return 0 ;
188188}
189189
190190int get_locales (char * * * ret ) {
191- _cleanup_set_free_free_ Set * locales = NULL ;
192- _cleanup_strv_free_ char * * l = NULL ;
191+ _cleanup_set_free_ Set * locales = NULL ;
193192 int r ;
194193
195- locales = set_new (& string_hash_ops );
194+ locales = set_new (& string_hash_ops_free );
196195 if (!locales )
197196 return - ENOMEM ;
198197
@@ -213,31 +212,25 @@ int get_locales(char ***ret) {
213212 free (set_remove (locales , locale ));
214213 }
215214
216- l = set_get_strv ( locales );
215+ _cleanup_strv_free_ char * * l = set_to_strv ( & locales );
217216 if (!l )
218217 return - ENOMEM ;
219218
220- /* Now, all elements are owned by strv 'l'. Hence, do not call set_free_free(). */
221- locales = set_free (locales );
222-
223219 r = getenv_bool ("SYSTEMD_LIST_NON_UTF8_LOCALES" );
224- if (IN_SET (r , - ENXIO , 0 )) {
225- char * * a , * * b ;
220+ if (r <= 0 ) {
221+ if (!IN_SET (r , - ENXIO , 0 ))
222+ log_debug_errno (r , "Failed to parse $SYSTEMD_LIST_NON_UTF8_LOCALES as boolean, ignoring: %m" );
226223
227224 /* Filter out non-UTF-8 locales, because it's 2019, by default */
228- for (a = b = l ; * a ; a ++ ) {
229-
230- if (endswith (* a , "UTF-8" ) ||
231- strstr (* a , ".UTF-8@" ))
225+ char * * b = l ;
226+ STRV_FOREACH (a , l )
227+ if (endswith (* a , "UTF-8" ) || strstr (* a , ".UTF-8@" ))
232228 * (b ++ ) = * a ;
233229 else
234230 free (* a );
235- }
236231
237232 * b = NULL ;
238-
239- } else if (r < 0 )
240- log_debug_errno (r , "Failed to parse $SYSTEMD_LIST_NON_UTF8_LOCALES as boolean" );
233+ }
241234
242235 strv_sort (l );
243236
0 commit comments