scummvm random work
[patches.git] / uClibc-mutex-cancel.patch
CommitLineData
5e993f12 1diff --git a/include/printf.h b/include/printf.h
2index 340b6cb..2dea58f 100644
3--- a/include/printf.h
4+++ b/include/printf.h
5@@ -75,6 +75,7 @@ struct printf_info
6 unsigned int is_short:1; /* h flag. */
7 unsigned int is_long:1; /* l flag. */
8 unsigned int is_long_double:1;/* L flag. */
9+ unsigned int __padding:20;/* non-gnu -- total of 32 bits on 32bit arch */
10
11 #elif __BYTE_ORDER == __BIG_ENDIAN
12
13diff --git a/include/pthread.h b/include/pthread.h
14index 8c01172..cee112b 100644
15--- a/include/pthread.h
16+++ b/include/pthread.h
17@@ -644,7 +644,8 @@ extern void _pthread_cleanup_pop (struct
18 /* Install a cleanup handler as pthread_cleanup_push does, but also
19 saves the current cancellation type and set it to deferred cancellation. */
20
21-#ifdef __USE_GNU
22+/* #ifdef __USE_GNU */
23+#if defined(__USE_GNU) || defined(_LIBC)
24 # define pthread_cleanup_push_defer_np(routine,arg) \
25 { struct _pthread_cleanup_buffer _buffer; \
26 _pthread_cleanup_push_defer (&_buffer, (routine), (arg));
27diff --git a/libc/inet/getnetent.c b/libc/inet/getnetent.c
28index 181c5ad..659bf5d 100644
29--- a/libc/inet/getnetent.c
30+++ b/libc/inet/getnetent.c
31@@ -22,18 +22,9 @@
32 #include <netdb.h>
33 #include <arpa/inet.h>
34
35+#include <bits/uClibc_mutex.h>
36
37-#ifdef __UCLIBC_HAS_THREADS__
38-#include <pthread.h>
39-static pthread_mutex_t mylock = PTHREAD_MUTEX_INITIALIZER;
40-# define LOCK __pthread_mutex_lock(&mylock)
41-# define UNLOCK __pthread_mutex_unlock(&mylock);
42-#else
43-# define LOCK
44-# define UNLOCK
45-#endif
46-
47-
48+__UCLIBC_MUTEX_STATIC(mylock, PTHREAD_MUTEX_INITIALIZER);
49
50 #define MAXALIASES 35
51 static const char NETDB[] = _PATH_NETWORKS;
52@@ -46,25 +37,25 @@ int _net_stayopen;
53
54 void setnetent(int f)
55 {
56- LOCK;
57+ __UCLIBC_MUTEX_LOCK(mylock);
58 if (netf == NULL)
59- netf = fopen(NETDB, "r" );
60+ netf = fopen(NETDB, "r" );
61 else
62- rewind(netf);
63+ rewind(netf);
64 _net_stayopen |= f;
65- UNLOCK;
66+ __UCLIBC_MUTEX_UNLOCK(mylock);
67 return;
68 }
69
70 void endnetent(void)
71 {
72- LOCK;
73+ __UCLIBC_MUTEX_LOCK(mylock);
74 if (netf) {
75- fclose(netf);
76- netf = NULL;
77+ fclose(netf);
78+ netf = NULL;
79 }
80 _net_stayopen = 0;
81- UNLOCK;
82+ __UCLIBC_MUTEX_UNLOCK(mylock);
83 }
84
85 static char * any(register char *cp, char *match)
86@@ -72,10 +63,10 @@ static char * any(register char *cp, cha
87 register char *mp, c;
88
89 while ((c = *cp)) {
90- for (mp = match; *mp; mp++)
91- if (*mp == c)
92- return (cp);
93- cp++;
94+ for (mp = match; *mp; mp++)
95+ if (*mp == c)
96+ return (cp);
97+ cp++;
98 }
99 return ((char *)0);
100 }
101@@ -84,59 +75,62 @@ struct netent * getnetent(void)
102 {
103 char *p;
104 register char *cp, **q;
105+ struct netent *rv = NULL;
106
107- LOCK;
108+ __UCLIBC_MUTEX_LOCK(mylock);
109 if (netf == NULL && (netf = fopen(NETDB, "r" )) == NULL) {
110- UNLOCK;
111- return (NULL);
112+ goto DONE;
113 }
114-again:
115+ again:
116
117 if (!line) {
118- line = malloc(BUFSIZ + 1);
119- if (!line)
120- abort();
121+ line = malloc(BUFSIZ + 1);
122+ if (!line)
123+ abort();
124 }
125
126 p = fgets(line, BUFSIZ, netf);
127 if (p == NULL) {
128- UNLOCK;
129- return (NULL);
130+ goto DONE;
131 }
132 if (*p == '#')
133- goto again;
134+ goto again;
135 cp = any(p, "#\n");
136 if (cp == NULL)
137- goto again;
138+ goto again;
139 *cp = '\0';
140 net.n_name = p;
141 cp = any(p, " \t");
142 if (cp == NULL)
143- goto again;
144+ goto again;
145 *cp++ = '\0';
146 while (*cp == ' ' || *cp == '\t')
147- cp++;
148+ cp++;
149 p = any(cp, " \t");
150 if (p != NULL)
151- *p++ = '\0';
152+ *p++ = '\0';
153 net.n_net = inet_network(cp);
154 net.n_addrtype = AF_INET;
155 q = net.n_aliases = net_aliases;
156 if (p != NULL)
157- cp = p;
158+ cp = p;
159 while (cp && *cp) {
160- if (*cp == ' ' || *cp == '\t') {
161- cp++;
162- continue;
163- }
164- if (q < &net_aliases[MAXALIASES - 1])
165- *q++ = cp;
166- cp = any(cp, " \t");
167- if (cp != NULL)
168- *cp++ = '\0';
169+ if (*cp == ' ' || *cp == '\t') {
170+ cp++;
171+ continue;
172+ }
173+ if (q < &net_aliases[MAXALIASES - 1])
174+ *q++ = cp;
175+ cp = any(cp, " \t");
176+ if (cp != NULL)
177+ *cp++ = '\0';
178 }
179 *q = NULL;
180- UNLOCK;
181- return (&net);
182+
183+ rv = &net;
184+
185+ DONE:
186+ __UCLIBC_MUTEX_UNLOCK(mylock);
187+ return rv;
188 }
189
190diff --git a/libc/inet/getproto.c b/libc/inet/getproto.c
191index c9f35f1..3665d89 100644
192--- a/libc/inet/getproto.c
193+++ b/libc/inet/getproto.c
194@@ -62,17 +62,9 @@
195 #include <string.h>
196 #include <errno.h>
197
198-#ifdef __UCLIBC_HAS_THREADS__
199-#include <pthread.h>
200-static pthread_mutex_t mylock = PTHREAD_RECURSIVE_MUTEX_INITIALIZER_NP;
201-# define LOCK __pthread_mutex_lock(&mylock)
202-# define UNLOCK __pthread_mutex_unlock(&mylock);
203-#else
204-# define LOCK
205-# define UNLOCK
206-#endif
207-
208+#include <bits/uClibc_mutex.h>
209
210+__UCLIBC_MUTEX_STATIC(mylock, PTHREAD_RECURSIVE_MUTEX_INITIALIZER_NP);
211
212 #define MAXALIASES 35
213 #define SBUFSIZE (BUFSIZ + 1 + (sizeof(char *) * MAXALIASES))
214@@ -85,109 +77,114 @@ static int proto_stayopen;
215 static void __initbuf(void)
216 {
217 if (!static_aliases) {
218- static_aliases = malloc(SBUFSIZE);
219- if (!static_aliases)
220- abort();
221+ static_aliases = malloc(SBUFSIZE);
222+ if (!static_aliases)
223+ abort();
224 }
225 }
226
227 void setprotoent(int f)
228 {
229- LOCK;
230+ __UCLIBC_MUTEX_LOCK(mylock);
231 if (protof == NULL)
232- protof = fopen(_PATH_PROTOCOLS, "r" );
233+ protof = fopen(_PATH_PROTOCOLS, "r" );
234 else
235- rewind(protof);
236+ rewind(protof);
237 proto_stayopen |= f;
238- UNLOCK;
239+ __UCLIBC_MUTEX_UNLOCK(mylock);
240 }
241
242 void endprotoent(void)
243 {
244- LOCK;
245+ __UCLIBC_MUTEX_LOCK(mylock);
246 if (protof) {
247- fclose(protof);
248- protof = NULL;
249+ fclose(protof);
250+ protof = NULL;
251 }
252 proto_stayopen = 0;
253- UNLOCK;
254+ __UCLIBC_MUTEX_UNLOCK(mylock);
255 }
256
257 int getprotoent_r(struct protoent *result_buf,
258- char *buf, size_t buflen,
259- struct protoent **result)
260+ char *buf, size_t buflen,
261+ struct protoent **result)
262 {
263 char *p;
264 register char *cp, **q;
265 char **proto_aliases;
266 char *line;
267+ int rv;
268
269 *result = NULL;
270
271 if (buflen < sizeof(*proto_aliases)*MAXALIASES) {
272- errno=ERANGE;
273- return errno;
274+ errno=ERANGE;
275+ return errno;
276 }
277- LOCK;
278+
279+ __UCLIBC_MUTEX_LOCK(mylock);
280 proto_aliases=(char **)buf;
281 buf+=sizeof(*proto_aliases)*MAXALIASES;
282 buflen-=sizeof(*proto_aliases)*MAXALIASES;
283
284 if (buflen < BUFSIZ+1) {
285- UNLOCK;
286- errno=ERANGE;
287- return errno;
288+ errno=rv=ERANGE;
289+ goto DONE;
290 }
291 line=buf;
292 buf+=BUFSIZ+1;
293 buflen-=BUFSIZ+1;
294
295 if (protof == NULL && (protof = fopen(_PATH_PROTOCOLS, "r" )) == NULL) {
296- UNLOCK;
297- return errno;
298+ rv=errno;
299+ goto DONE;
300 }
301-again:
302+ again:
303 if ((p = fgets(line, BUFSIZ, protof)) == NULL) {
304- UNLOCK;
305- return TRY_AGAIN;
306+ rv=TRY_AGAIN;
307+ goto DONE;
308 }
309
310 if (*p == '#')
311- goto again;
312+ goto again;
313 cp = strpbrk(p, "#\n");
314 if (cp == NULL)
315- goto again;
316+ goto again;
317 *cp = '\0';
318 result_buf->p_name = p;
319 cp = strpbrk(p, " \t");
320 if (cp == NULL)
321- goto again;
322+ goto again;
323 *cp++ = '\0';
324 while (*cp == ' ' || *cp == '\t')
325- cp++;
326+ cp++;
327 p = strpbrk(cp, " \t");
328 if (p != NULL)
329- *p++ = '\0';
330+ *p++ = '\0';
331 result_buf->p_proto = atoi(cp);
332 q = result_buf->p_aliases = proto_aliases;
333 if (p != NULL) {
334- cp = p;
335- while (cp && *cp) {
336- if (*cp == ' ' || *cp == '\t') {
337- cp++;
338- continue;
339- }
340- if (q < &proto_aliases[MAXALIASES - 1])
341- *q++ = cp;
342- cp = strpbrk(cp, " \t");
343- if (cp != NULL)
344- *cp++ = '\0';
345- }
346+ cp = p;
347+ while (cp && *cp) {
348+ if (*cp == ' ' || *cp == '\t') {
349+ cp++;
350+ continue;
351+ }
352+ if (q < &proto_aliases[MAXALIASES - 1])
353+ *q++ = cp;
354+ cp = strpbrk(cp, " \t");
355+ if (cp != NULL)
356+ *cp++ = '\0';
357+ }
358 }
359 *q = NULL;
360 *result=result_buf;
361- UNLOCK;
362- return 0;
363+
364+ rv = 0;
365+
366+ DONE:
367+ __UCLIBC_MUTEX_UNLOCK(mylock);
368+ return rv;
369 }
370
371 struct protoent * getprotoent(void)
372@@ -201,26 +198,26 @@ struct protoent * getprotoent(void)
373
374
375 int getprotobyname_r(const char *name,
376- struct protoent *result_buf,
377- char *buf, size_t buflen,
378- struct protoent **result)
379+ struct protoent *result_buf,
380+ char *buf, size_t buflen,
381+ struct protoent **result)
382 {
383 register char **cp;
384 int ret;
385
386- LOCK;
387+ __UCLIBC_MUTEX_LOCK(mylock);
388 setprotoent(proto_stayopen);
389 while (!(ret=getprotoent_r(result_buf, buf, buflen, result))) {
390- if (strcmp(result_buf->p_name, name) == 0)
391- break;
392- for (cp = result_buf->p_aliases; *cp != 0; cp++)
393- if (strcmp(*cp, name) == 0)
394- goto found;
395+ if (strcmp(result_buf->p_name, name) == 0)
396+ break;
397+ for (cp = result_buf->p_aliases; *cp != 0; cp++)
398+ if (strcmp(*cp, name) == 0)
399+ goto found;
400 }
401-found:
402+ found:
403 if (!proto_stayopen)
404- endprotoent();
405- UNLOCK;
406+ endprotoent();
407+ __UCLIBC_MUTEX_UNLOCK(mylock);
408 return *result?0:ret;
409 }
410
411@@ -236,20 +233,20 @@ struct protoent * getprotobyname(const c
412
413
414 int getprotobynumber_r (int proto_num,
415- struct protoent *result_buf,
416- char *buf, size_t buflen,
417- struct protoent **result)
418+ struct protoent *result_buf,
419+ char *buf, size_t buflen,
420+ struct protoent **result)
421 {
422 int ret;
423
424- LOCK;
425+ __UCLIBC_MUTEX_LOCK(mylock);
426 setprotoent(proto_stayopen);
427 while (!(ret=getprotoent_r(result_buf, buf, buflen, result)))
428- if (result_buf->p_proto == proto_num)
429- break;
430+ if (result_buf->p_proto == proto_num)
431+ break;
432 if (!proto_stayopen)
433- endprotoent();
434- UNLOCK;
435+ endprotoent();
436+ __UCLIBC_MUTEX_UNLOCK(mylock);
437 return *result?0:ret;
438 }
439
440diff --git a/libc/inet/getservice.c b/libc/inet/getservice.c
441index cbe5c50..b666057 100644
442--- a/libc/inet/getservice.c
443+++ b/libc/inet/getservice.c
444@@ -65,20 +65,9 @@
445 #include <arpa/inet.h>
446 #include <errno.h>
447
448+#include <bits/uClibc_mutex.h>
449
450-
451-#ifdef __UCLIBC_HAS_THREADS__
452-#include <pthread.h>
453-static pthread_mutex_t mylock = PTHREAD_RECURSIVE_MUTEX_INITIALIZER_NP;
454-# define LOCK __pthread_mutex_lock(&mylock)
455-# define UNLOCK __pthread_mutex_unlock(&mylock);
456-#else
457-# define LOCK
458-# define UNLOCK
459-#endif
460-
461-
462-
463+__UCLIBC_MUTEX_STATIC(mylock, PTHREAD_RECURSIVE_MUTEX_INITIALIZER_NP);
464
465 #define MAXALIASES 35
466 #define SBUFSIZE (BUFSIZ + 1 + (sizeof(char *) * MAXALIASES))
467@@ -91,32 +80,32 @@ static int serv_stayopen;
468 static void __initbuf(void)
469 {
470 if (!servbuf) {
471- servbuf = malloc(SBUFSIZE);
472- if (!servbuf)
473- abort();
474+ servbuf = malloc(SBUFSIZE);
475+ if (!servbuf)
476+ abort();
477 }
478 }
479
480 void setservent(int f)
481 {
482- LOCK;
483+ __UCLIBC_MUTEX_LOCK(mylock);
484 if (servf == NULL)
485- servf = fopen(_PATH_SERVICES, "r" );
486+ servf = fopen(_PATH_SERVICES, "r" );
487 else
488- rewind(servf);
489+ rewind(servf);
490 serv_stayopen |= f;
491- UNLOCK;
492+ __UCLIBC_MUTEX_UNLOCK(mylock);
493 }
494
495 void endservent(void)
496 {
497- LOCK;
498+ __UCLIBC_MUTEX_LOCK(mylock);
499 if (servf) {
500- fclose(servf);
501- servf = NULL;
502+ fclose(servf);
503+ servf = NULL;
504 }
505 serv_stayopen = 0;
506- UNLOCK;
507+ __UCLIBC_MUTEX_UNLOCK(mylock);
508 }
509
510 struct servent * getservent(void)
511@@ -149,127 +138,129 @@ struct servent * getservbyport(int port,
512 }
513
514 int getservent_r(struct servent * result_buf,
515- char * buf, size_t buflen,
516- struct servent ** result)
517+ char * buf, size_t buflen,
518+ struct servent ** result)
519 {
520 char *p;
521 register char *cp, **q;
522 char **serv_aliases;
523 char *line;
524+ int rv;
525
526 *result=NULL;
527
528 if (buflen < sizeof(*serv_aliases)*MAXALIASES) {
529- errno=ERANGE;
530- return errno;
531+ errno=ERANGE;
532+ return errno;
533 }
534- LOCK;
535+ __UCLIBC_MUTEX_LOCK(mylock);
536 serv_aliases=(char **)buf;
537 buf+=sizeof(*serv_aliases)*MAXALIASES;
538 buflen-=sizeof(*serv_aliases)*MAXALIASES;
539
540 if (buflen < BUFSIZ+1) {
541- UNLOCK;
542- errno=ERANGE;
543- return errno;
544+ errno=rv=ERANGE;
545+ goto DONE;
546 }
547 line=buf;
548 buf+=BUFSIZ+1;
549 buflen-=BUFSIZ+1;
550
551 if (servf == NULL && (servf = fopen(_PATH_SERVICES, "r" )) == NULL) {
552- UNLOCK;
553- errno=EIO;
554- return errno;
555+ errno=rv=EIO;
556+ goto DONE;
557 }
558-again:
559+ again:
560 if ((p = fgets(line, BUFSIZ, servf)) == NULL) {
561- UNLOCK;
562- errno=EIO;
563- return errno;
564+ errno=rv=EIO;
565+ goto DONE;
566 }
567 if (*p == '#')
568- goto again;
569+ goto again;
570 cp = strpbrk(p, "#\n");
571 if (cp == NULL)
572- goto again;
573+ goto again;
574 *cp = '\0';
575 result_buf->s_name = p;
576 p = strpbrk(p, " \t");
577 if (p == NULL)
578- goto again;
579+ goto again;
580 *p++ = '\0';
581 while (*p == ' ' || *p == '\t')
582- p++;
583+ p++;
584 cp = strpbrk(p, ",/");
585 if (cp == NULL)
586- goto again;
587+ goto again;
588 *cp++ = '\0';
589 result_buf->s_port = htons((u_short)atoi(p));
590 result_buf->s_proto = cp;
591 q = result_buf->s_aliases = serv_aliases;
592 cp = strpbrk(cp, " \t");
593 if (cp != NULL)
594- *cp++ = '\0';
595+ *cp++ = '\0';
596 while (cp && *cp) {
597- if (*cp == ' ' || *cp == '\t') {
598- cp++;
599- continue;
600- }
601- if (q < &serv_aliases[MAXALIASES - 1])
602- *q++ = cp;
603- cp = strpbrk(cp, " \t");
604- if (cp != NULL)
605- *cp++ = '\0';
606+ if (*cp == ' ' || *cp == '\t') {
607+ cp++;
608+ continue;
609+ }
610+ if (q < &serv_aliases[MAXALIASES - 1])
611+ *q++ = cp;
612+ cp = strpbrk(cp, " \t");
613+ if (cp != NULL)
614+ *cp++ = '\0';
615 }
616 *q = NULL;
617 *result=result_buf;
618- UNLOCK;
619- return 0;
620+
621+ rv = 0;
622+
623+ DONE:
624+ __UCLIBC_MUTEX_UNLOCK(mylock);
625+ return rv;
626 }
627
628 int getservbyname_r(const char *name, const char *proto,
629- struct servent * result_buf, char * buf, size_t buflen,
630- struct servent ** result)
631+ struct servent * result_buf, char * buf, size_t buflen,
632+ struct servent ** result)
633 {
634 register char **cp;
635 int ret;
636
637- LOCK;
638+ __UCLIBC_MUTEX_LOCK(mylock);
639 setservent(serv_stayopen);
640 while (!(ret=getservent_r(result_buf, buf, buflen, result))) {
641- if (strcmp(name, result_buf->s_name) == 0)
642- goto gotname;
643- for (cp = result_buf->s_aliases; *cp; cp++)
644- if (strcmp(name, *cp) == 0)
645- goto gotname;
646- continue;
647-gotname:
648- if (proto == 0 || strcmp(result_buf->s_proto, proto) == 0)
649- break;
650+ if (strcmp(name, result_buf->s_name) == 0)
651+ goto gotname;
652+ for (cp = result_buf->s_aliases; *cp; cp++)
653+ if (strcmp(name, *cp) == 0)
654+ goto gotname;
655+ continue;
656+ gotname:
657+ if (proto == 0 || strcmp(result_buf->s_proto, proto) == 0)
658+ break;
659 }
660 if (!serv_stayopen)
661- endservent();
662- UNLOCK;
663+ endservent();
664+ __UCLIBC_MUTEX_UNLOCK(mylock);
665 return *result?0:ret;
666 }
667
668 int getservbyport_r(int port, const char *proto,
669- struct servent * result_buf, char * buf,
670- size_t buflen, struct servent ** result)
671+ struct servent * result_buf, char * buf,
672+ size_t buflen, struct servent ** result)
673 {
674 int ret;
675
676- LOCK;
677+ __UCLIBC_MUTEX_LOCK(mylock);
678 setservent(serv_stayopen);
679 while (!(ret=getservent_r(result_buf, buf, buflen, result))) {
680- if (result_buf->s_port != port)
681- continue;
682- if (proto == 0 || strcmp(result_buf->s_proto, proto) == 0)
683- break;
684+ if (result_buf->s_port != port)
685+ continue;
686+ if (proto == 0 || strcmp(result_buf->s_proto, proto) == 0)
687+ break;
688 }
689 if (!serv_stayopen)
690- endservent();
691- UNLOCK;
692+ endservent();
693+ __UCLIBC_MUTEX_UNLOCK(mylock);
694 return *result?0:ret;
695 }
696diff --git a/libc/inet/resolv.c b/libc/inet/resolv.c
697index 27b60ef..0f583ab 100644
698--- a/libc/inet/resolv.c
699+++ b/libc/inet/resolv.c
700@@ -7,7 +7,7 @@
701 * modify it under the terms of the GNU Library General Public
702 * License as published by the Free Software Foundation; either
703 * version 2 of the License, or (at your option) any later version.
704-*/
705+ */
706
707 /*
708 * Portions Copyright (c) 1985, 1993
709@@ -153,6 +153,11 @@
710 #include <sys/utsname.h>
711 #include <sys/un.h>
712
713+#include <bits/uClibc_mutex.h>
714+
715+__UCLIBC_MUTEX_EXTERN(__resolv_lock);
716+
717+
718 #define MAX_RECURSE 5
719 #define REPLY_TIMEOUT 10
720 #define MAX_RETRIES 3
721@@ -180,18 +185,6 @@ extern char * __nameserver[MAX_SERVERS];
722 extern int __searchdomains;
723 extern char * __searchdomain[MAX_SEARCH];
724
725-#ifdef __UCLIBC_HAS_THREADS__
726-#include <pthread.h>
727-extern pthread_mutex_t __resolv_lock;
728-# define BIGLOCK __pthread_mutex_lock(&__resolv_lock)
729-# define BIGUNLOCK __pthread_mutex_unlock(&__resolv_lock);
730-#else
731-# define BIGLOCK
732-# define BIGUNLOCK
733-#endif
734-
735-
736-
737 /* Structs */
738 struct resolv_header {
739 int id;
740@@ -229,49 +222,49 @@ enum etc_hosts_action {
741
742 /* function prototypes */
743 extern int __get_hosts_byname_r(const char * name, int type,
744- struct hostent * result_buf,
745- char * buf, size_t buflen,
746- struct hostent ** result,
747- int * h_errnop);
748+ struct hostent * result_buf,
749+ char * buf, size_t buflen,
750+ struct hostent ** result,
751+ int * h_errnop);
752 extern int __get_hosts_byaddr_r(const char * addr, int len, int type,
753- struct hostent * result_buf,
754- char * buf, size_t buflen,
755- struct hostent ** result,
756- int * h_errnop);
757+ struct hostent * result_buf,
758+ char * buf, size_t buflen,
759+ struct hostent ** result,
760+ int * h_errnop);
761 extern void __open_etc_hosts(FILE **fp);
762 extern int __read_etc_hosts_r(FILE *fp, const char * name, int type,
763- enum etc_hosts_action action,
764- struct hostent * result_buf,
765- char * buf, size_t buflen,
766- struct hostent ** result,
767- int * h_errnop);
768+ enum etc_hosts_action action,
769+ struct hostent * result_buf,
770+ char * buf, size_t buflen,
771+ struct hostent ** result,
772+ int * h_errnop);
773 extern int __dns_lookup(const char * name, int type, int nscount,
774- char ** nsip, unsigned char ** outpacket, struct resolv_answer * a);
775+ char ** nsip, unsigned char ** outpacket, struct resolv_answer * a);
776
777 extern int __encode_dotted(const char * dotted, unsigned char * dest, int maxlen);
778 extern int __decode_dotted(const unsigned char * message, int offset,
779- char * dest, int maxlen);
780+ char * dest, int maxlen);
781 extern int __length_dotted(const unsigned char * message, int offset);
782 extern int __encode_header(struct resolv_header * h, unsigned char * dest, int maxlen);
783 extern int __decode_header(unsigned char * data, struct resolv_header * h);
784 extern int __encode_question(struct resolv_question * q,
785- unsigned char * dest, int maxlen);
786+ unsigned char * dest, int maxlen);
787 extern int __decode_question(unsigned char * message, int offset,
788- struct resolv_question * q);
789+ struct resolv_question * q);
790 extern int __encode_answer(struct resolv_answer * a,
791- unsigned char * dest, int maxlen);
792+ unsigned char * dest, int maxlen);
793 extern int __decode_answer(unsigned char * message, int offset,
794- struct resolv_answer * a);
795+ struct resolv_answer * a);
796 extern int __length_question(unsigned char * message, int offset);
797 extern int __open_nameservers(void);
798 extern void __close_nameservers(void);
799 extern int __dn_expand(const u_char *, const u_char *, const u_char *,
800- char *, int);
801+ char *, int);
802 extern int __ns_name_uncompress(const u_char *, const u_char *,
803- const u_char *, char *, size_t);
804+ const u_char *, char *, size_t);
805 extern int __ns_name_ntop(const u_char *, char *, size_t);
806 extern int __ns_name_unpack(const u_char *, const u_char *, const u_char *,
807- u_char *, size_t);
808+ u_char *, size_t);
809
810
811 #ifdef L_encodeh
812@@ -361,7 +354,7 @@ int __encode_dotted(const char *dotted,
813 This routine understands compressed data. */
814
815 int __decode_dotted(const unsigned char *data, int offset,
816- char *dest, int maxlen)
817+ char *dest, int maxlen)
818 {
819 int l;
820 int measure = 1;
821@@ -435,7 +428,7 @@ int __length_dotted(const unsigned char
822
823 #ifdef L_encodeq
824 int __encode_question(struct resolv_question *q,
825- unsigned char *dest, int maxlen)
826+ unsigned char *dest, int maxlen)
827 {
828 int i;
829
830@@ -460,7 +453,7 @@ int __encode_question(struct resolv_ques
831
832 #ifdef L_decodeq
833 int __decode_question(unsigned char *message, int offset,
834- struct resolv_question *q)
835+ struct resolv_question *q)
836 {
837 char temp[256];
838 int i;
839@@ -525,7 +518,7 @@ int __encode_answer(struct resolv_answer
840
841 #ifdef L_decodea
842 int __decode_answer(unsigned char *message, int offset,
843- struct resolv_answer *a)
844+ struct resolv_answer *a)
845 {
846 char temp[256];
847 int i;
848@@ -557,11 +550,11 @@ int __decode_answer(unsigned char *messa
849
850 #ifdef L_encodep
851 int __encode_packet(struct resolv_header *h,
852- struct resolv_question **q,
853- struct resolv_answer **an,
854- struct resolv_answer **ns,
855- struct resolv_answer **ar,
856- unsigned char *dest, int maxlen)
857+ struct resolv_question **q,
858+ struct resolv_answer **an,
859+ struct resolv_answer **ns,
860+ struct resolv_answer **ar,
861+ unsigned char *dest, int maxlen)
862 {
863 int i, total = 0;
864 int j;
865@@ -621,7 +614,7 @@ int __decode_packet(unsigned char *data,
866
867 #ifdef L_formquery
868 int __form_query(int id, const char *name, int type, unsigned char *packet,
869- int maxlen)
870+ int maxlen)
871 {
872 struct resolv_header h;
873 struct resolv_question q;
874@@ -649,14 +642,7 @@ int __form_query(int id, const char *nam
875
876 #ifdef L_dnslookup
877
878-#ifdef __UCLIBC_HAS_THREADS__
879-static pthread_mutex_t mylock = PTHREAD_MUTEX_INITIALIZER;
880-# define LOCK __pthread_mutex_lock(&mylock)
881-# define UNLOCK __pthread_mutex_unlock(&mylock);
882-#else
883-# define LOCK
884-# define UNLOCK
885-#endif
886+__UCLIBC_MUTEX_STATIC(mylock, PTHREAD_MUTEX_INITIALIZER);
887
888 /* Just for the record, having to lock __dns_lookup() just for these two globals
889 * is pretty lame. I think these two variables can probably be de-global-ized,
890@@ -665,7 +651,7 @@ static pthread_mutex_t mylock = PTHREAD_
891 static int ns=0, id=1;
892
893 int __dns_lookup(const char *name, int type, int nscount, char **nsip,
894- unsigned char **outpacket, struct resolv_answer *a)
895+ unsigned char **outpacket, struct resolv_answer *a)
896 {
897 int i, j, len, fd, pos, rc;
898 struct timeval tv;
899@@ -693,10 +679,10 @@ int __dns_lookup(const char *name, int t
900 DPRINTF("Looking up type %d answer for '%s'\n", type, name);
901
902 /* Mess with globals while under lock */
903- LOCK;
904+ __UCLIBC_MUTEX_LOCK(mylock);
905 local_ns = ns % nscount;
906 local_id = id;
907- UNLOCK;
908+ __UCLIBC_MUTEX_UNLOCK(mylock);
909
910 while (retries < MAX_RETRIES) {
911 if (fd != -1)
912@@ -722,13 +708,13 @@ int __dns_lookup(const char *name, int t
913
914 strncpy(lookup,name,MAXDNAME);
915 if (variant >= 0) {
916- BIGLOCK;
917- if (variant < __searchdomains) {
918- strncat(lookup,".", MAXDNAME);
919- strncat(lookup,__searchdomain[variant], MAXDNAME);
920- }
921- BIGUNLOCK;
922- }
923+ __UCLIBC_MUTEX_LOCK(__resolv_lock);
924+ if (variant < __searchdomains) {
925+ strncat(lookup,".", MAXDNAME);
926+ strncat(lookup,__searchdomain[variant], MAXDNAME);
927+ }
928+ __UCLIBC_MUTEX_UNLOCK(__resolv_lock);
929+ }
930 DPRINTF("lookup name: %s\n", lookup);
931 q.dotted = (char *)lookup;
932 q.qtype = type;
933@@ -750,7 +736,7 @@ int __dns_lookup(const char *name, int t
934 fd = socket(AF_INET, SOCK_DGRAM, IPPROTO_UDP);
935 #endif
936 if (fd < 0) {
937- retries++;
938+ retries++;
939 continue;
940 }
941
942@@ -772,11 +758,11 @@ int __dns_lookup(const char *name, int t
943 #endif
944 if (rc < 0) {
945 if (errno == ENETUNREACH) {
946- /* routing error, presume not transient */
947- goto tryall;
948+ /* routing error, presume not transient */
949+ goto tryall;
950 } else
951- /* retry */
952- retries++;
953+ /* retry */
954+ retries++;
955 continue;
956 }
957
958@@ -838,55 +824,55 @@ int __dns_lookup(const char *name, int t
959
960 first_answer = 1;
961 for (j=0;j<h.ancount;j++,pos += i)
962- {
963- i = __decode_answer(packet, pos, &ma);
964+ {
965+ i = __decode_answer(packet, pos, &ma);
966
967- if (i<0) {
968- DPRINTF("failed decode %d\n", i);
969- goto again;
970- }
971+ if (i<0) {
972+ DPRINTF("failed decode %d\n", i);
973+ goto again;
974+ }
975
976- if ( first_answer )
977- {
978- ma.buf = a->buf;
979- ma.buflen = a->buflen;
980- ma.add_count = a->add_count;
981- memcpy(a, &ma, sizeof(ma));
982- if (a->atype != T_SIG && (0 == a->buf || (type != T_A && type != T_AAAA)))
983- {
984- break;
985- }
986- if (a->atype != type)
987- {
988- free(a->dotted);
989- continue;
990- }
991- a->add_count = h.ancount - j - 1;
992- if ((a->rdlength + sizeof(struct in_addr*)) * a->add_count > a->buflen)
993- {
994- break;
995- }
996- a->add_count = 0;
997- first_answer = 0;
998- }
999- else
1000- {
1001- free(ma.dotted);
1002- if (ma.atype != type)
1003- {
1004- continue;
1005- }
1006- if (a->rdlength != ma.rdlength)
1007- {
1008- free(a->dotted);
1009- DPRINTF("Answer address len(%u) differs from original(%u)\n",
1010- ma.rdlength, a->rdlength);
1011- goto again;
1012+ if ( first_answer )
1013+ {
1014+ ma.buf = a->buf;
1015+ ma.buflen = a->buflen;
1016+ ma.add_count = a->add_count;
1017+ memcpy(a, &ma, sizeof(ma));
1018+ if (a->atype != T_SIG && (0 == a->buf || (type != T_A && type != T_AAAA)))
1019+ {
1020+ break;
1021+ }
1022+ if (a->atype != type)
1023+ {
1024+ free(a->dotted);
1025+ continue;
1026+ }
1027+ a->add_count = h.ancount - j - 1;
1028+ if ((a->rdlength + sizeof(struct in_addr*)) * a->add_count > a->buflen)
1029+ {
1030+ break;
1031+ }
1032+ a->add_count = 0;
1033+ first_answer = 0;
1034+ }
1035+ else
1036+ {
1037+ free(ma.dotted);
1038+ if (ma.atype != type)
1039+ {
1040+ continue;
1041+ }
1042+ if (a->rdlength != ma.rdlength)
1043+ {
1044+ free(a->dotted);
1045+ DPRINTF("Answer address len(%u) differs from original(%u)\n",
1046+ ma.rdlength, a->rdlength);
1047+ goto again;
1048+ }
1049+ memcpy(a->buf + (a->add_count * ma.rdlength), ma.rdata, ma.rdlength);
1050+ ++a->add_count;
1051+ }
1052 }
1053- memcpy(a->buf + (a->add_count * ma.rdlength), ma.rdata, ma.rdlength);
1054- ++a->add_count;
1055- }
1056- }
1057
1058 DPRINTF("Answer name = |%s|\n", a->dotted);
1059 DPRINTF("Answer type = |%d|\n", a->atype);
1060@@ -900,48 +886,48 @@ int __dns_lookup(const char *name, int t
1061 free(lookup);
1062
1063 /* Mess with globals while under lock */
1064- LOCK;
1065+ __UCLIBC_MUTEX_LOCK(mylock);
1066 ns = local_ns;
1067 id = local_id;
1068- UNLOCK;
1069+ __UCLIBC_MUTEX_UNLOCK(mylock);
1070
1071 return (len); /* success! */
1072
1073- tryall:
1074+ tryall:
1075 /* if there are other nameservers, give them a go,
1076 otherwise return with error */
1077 {
1078 variant = -1;
1079- local_ns = (local_ns + 1) % nscount;
1080- if (local_ns == 0)
1081- retries++;
1082+ local_ns = (local_ns + 1) % nscount;
1083+ if (local_ns == 0)
1084+ retries++;
1085
1086- continue;
1087+ continue;
1088 }
1089
1090- again:
1091+ again:
1092 /* if there are searchdomains, try them or fallback as passed */
1093 {
1094 int sdomains;
1095- BIGLOCK;
1096+ __UCLIBC_MUTEX_LOCK(__resolv_lock);
1097 sdomains=__searchdomains;
1098- BIGUNLOCK;
1099+ __UCLIBC_MUTEX_UNLOCK(__resolv_lock);
1100
1101 if (variant < sdomains - 1) {
1102- /* next search */
1103- variant++;
1104+ /* next search */
1105+ variant++;
1106 } else {
1107- /* next server, first search */
1108- local_ns = (local_ns + 1) % nscount;
1109- if (local_ns == 0)
1110- retries++;
1111+ /* next server, first search */
1112+ local_ns = (local_ns + 1) % nscount;
1113+ if (local_ns == 0)
1114+ retries++;
1115
1116- variant = -1;
1117+ variant = -1;
1118 }
1119 }
1120 }
1121
1122-fail:
1123+ fail:
1124 if (fd != -1)
1125 close(fd);
1126 if (lookup)
1127@@ -951,10 +937,10 @@ fail:
1128 h_errno = NETDB_INTERNAL;
1129 /* Mess with globals while under lock */
1130 if (local_ns != -1) {
1131- LOCK;
1132+ __UCLIBC_MUTEX_LOCK(mylock);
1133 ns = local_ns;
1134 id = local_id;
1135- UNLOCK;
1136+ __UCLIBC_MUTEX_UNLOCK(mylock);
1137 }
1138 return -1;
1139 }
1140@@ -966,9 +952,8 @@ int __nameservers;
1141 char * __nameserver[MAX_SERVERS];
1142 int __searchdomains;
1143 char * __searchdomain[MAX_SEARCH];
1144-#ifdef __UCLIBC_HAS_THREADS__
1145-pthread_mutex_t __resolv_lock = PTHREAD_MUTEX_INITIALIZER;
1146-#endif
1147+
1148+__UCLIBC_MUTEX_INIT(__resolv_lock, PTHREAD_MUTEX_INITIALIZER);
1149
1150 /*
1151 * we currently read formats not quite the same as that on normal
1152@@ -982,60 +967,63 @@ int __open_nameservers()
1153 #define RESOLV_ARGS 5
1154 char szBuffer[128], *p, *argv[RESOLV_ARGS];
1155 int argc;
1156+ int rv = 0;
1157
1158- BIGLOCK;
1159+ __UCLIBC_MUTEX_LOCK(__resolv_lock);
1160 if (__nameservers > 0) {
1161- BIGUNLOCK;
1162- return 0;
1163+ goto DONE;
1164 }
1165
1166 if ((fp = fopen("/etc/resolv.conf", "r")) ||
1167- (fp = fopen("/etc/config/resolv.conf", "r")))
1168- {
1169-
1170- while (fgets(szBuffer, sizeof(szBuffer), fp) != NULL) {
1171+ (fp = fopen("/etc/config/resolv.conf", "r")))
1172+ {
1173
1174- for (p = szBuffer; *p && isspace(*p); p++)
1175- /* skip white space */;
1176- if (*p == '\0' || *p == '\n' || *p == '#') /* skip comments etc */
1177- continue;
1178- argc = 0;
1179- while (*p && argc < RESOLV_ARGS) {
1180- argv[argc++] = p;
1181- while (*p && !isspace(*p) && *p != '\n')
1182- p++;
1183- while (*p && (isspace(*p) || *p == '\n')) /* remove spaces */
1184- *p++ = '\0';
1185- }
1186+ while (fgets(szBuffer, sizeof(szBuffer), fp) != NULL) {
1187
1188- if (strcmp(argv[0], "nameserver") == 0) {
1189- for (i = 1; i < argc && __nameservers < MAX_SERVERS; i++) {
1190- __nameserver[__nameservers++] = strdup(argv[i]);
1191- DPRINTF("adding nameserver %s\n", argv[i]);
1192+ for (p = szBuffer; *p && isspace(*p); p++)
1193+ /* skip white space */;
1194+ if (*p == '\0' || *p == '\n' || *p == '#') /* skip comments etc */
1195+ continue;
1196+ argc = 0;
1197+ while (*p && argc < RESOLV_ARGS) {
1198+ argv[argc++] = p;
1199+ while (*p && !isspace(*p) && *p != '\n')
1200+ p++;
1201+ while (*p && (isspace(*p) || *p == '\n')) /* remove spaces */
1202+ *p++ = '\0';
1203 }
1204- }
1205
1206- /* domain and search are mutually exclusive, the last one wins */
1207- if (strcmp(argv[0],"domain")==0 || strcmp(argv[0],"search")==0) {
1208- while (__searchdomains > 0) {
1209- free(__searchdomain[--__searchdomains]);
1210- __searchdomain[__searchdomains] = NULL;
1211+ if (strcmp(argv[0], "nameserver") == 0) {
1212+ for (i = 1; i < argc && __nameservers < MAX_SERVERS; i++) {
1213+ __nameserver[__nameservers++] = strdup(argv[i]);
1214+ DPRINTF("adding nameserver %s\n", argv[i]);
1215+ }
1216 }
1217- for (i=1; i < argc && __searchdomains < MAX_SEARCH; i++) {
1218- __searchdomain[__searchdomains++] = strdup(argv[i]);
1219- DPRINTF("adding search %s\n", argv[i]);
1220+
1221+ /* domain and search are mutually exclusive, the last one wins */
1222+ if (strcmp(argv[0],"domain")==0 || strcmp(argv[0],"search")==0) {
1223+ while (__searchdomains > 0) {
1224+ free(__searchdomain[--__searchdomains]);
1225+ __searchdomain[__searchdomains] = NULL;
1226+ }
1227+ for (i=1; i < argc && __searchdomains < MAX_SEARCH; i++) {
1228+ __searchdomain[__searchdomains++] = strdup(argv[i]);
1229+ DPRINTF("adding search %s\n", argv[i]);
1230+ }
1231 }
1232 }
1233+ fclose(fp);
1234+ DPRINTF("nameservers = %d\n", __nameservers);
1235+ goto DONE;
1236 }
1237- fclose(fp);
1238- DPRINTF("nameservers = %d\n", __nameservers);
1239- BIGUNLOCK;
1240- return 0;
1241- }
1242 DPRINTF("failed to open %s\n", "resolv.conf");
1243 h_errno = NO_RECOVERY;
1244- BIGUNLOCK;
1245- return -1;
1246+
1247+ rv = -1;
1248+
1249+ DONE:
1250+ __UCLIBC_MUTEX_UNLOCK(__resolv_lock);
1251+ return rv;
1252 }
1253 #endif
1254
1255@@ -1044,7 +1032,7 @@ int __open_nameservers()
1256
1257 void __close_nameservers(void)
1258 {
1259- BIGLOCK;
1260+ __UCLIBC_MUTEX_LOCK(__resolv_lock);
1261 while (__nameservers > 0) {
1262 free(__nameserver[--__nameservers]);
1263 __nameserver[__nameservers] = NULL;
1264@@ -1053,7 +1041,7 @@ void __close_nameservers(void)
1265 free(__searchdomain[--__searchdomains]);
1266 __searchdomain[__searchdomains] = NULL;
1267 }
1268- BIGUNLOCK;
1269+ __UCLIBC_MUTEX_UNLOCK(__resolv_lock);
1270 }
1271 #endif
1272
1273@@ -1063,8 +1051,8 @@ struct hostent *gethostbyname(const char
1274 {
1275 static struct hostent h;
1276 static char buf[sizeof(struct in_addr) +
1277- sizeof(struct in_addr *)*2 +
1278- sizeof(char *)*(ALIAS_DIM) + 384/*namebuffer*/ + 32/* margin */];
1279+ sizeof(struct in_addr *)*2 +
1280+ sizeof(char *)*(ALIAS_DIM) + 384/*namebuffer*/ + 32/* margin */];
1281 struct hostent *hp;
1282
1283 gethostbyname_r(name, &h, buf, sizeof(buf), &hp, &h_errno);
1284@@ -1082,8 +1070,8 @@ struct hostent *gethostbyname2(const cha
1285 #else /* __UCLIBC_HAS_IPV6__ */
1286 static struct hostent h;
1287 static char buf[sizeof(struct in6_addr) +
1288- sizeof(struct in6_addr *)*2 +
1289- sizeof(char *)*(ALIAS_DIM) + 384/*namebuffer*/ + 32/* margin */];
1290+ sizeof(struct in6_addr *)*2 +
1291+ sizeof(char *)*(ALIAS_DIM) + 384/*namebuffer*/ + 32/* margin */];
1292 struct hostent *hp;
1293
1294 gethostbyname2_r(name, family, &h, buf, sizeof(buf), &hp, &h_errno);
1295@@ -1119,7 +1107,7 @@ int res_init(void)
1296 /** rp->rhook = NULL; **/
1297 /** rp->_u._ext.nsinit = 0; **/
1298
1299- BIGLOCK;
1300+ __UCLIBC_MUTEX_LOCK(__resolv_lock);
1301 if(__searchdomains) {
1302 int i;
1303 for(i=0; i<__searchdomains; i++) {
1304@@ -1139,7 +1127,7 @@ int res_init(void)
1305 }
1306 }
1307 rp->nscount = __nameservers;
1308- BIGUNLOCK;
1309+ __UCLIBC_MUTEX_UNLOCK(__resolv_lock);
1310
1311 return(0);
1312 }
1313@@ -1175,10 +1163,10 @@ int res_query(const char *dname, int cla
1314
1315 memset((char *) &a, '\0', sizeof(a));
1316
1317- BIGLOCK;
1318+ __UCLIBC_MUTEX_LOCK(__resolv_lock);
1319 __nameserversXX=__nameservers;
1320 __nameserverXX=__nameserver;
1321- BIGUNLOCK;
1322+ __UCLIBC_MUTEX_UNLOCK(__resolv_lock);
1323 i = __dns_lookup(dname, type, __nameserversXX, __nameserverXX, &packet, &a);
1324
1325 if (i < 0) {
1326@@ -1207,10 +1195,10 @@ int res_query(const char *dname, int cla
1327 * is detected. Error code, if any, is left in h_errno.
1328 */
1329 int res_search(name, class, type, answer, anslen)
1330- const char *name; /* domain name */
1331- int class, type; /* class and type of query */
1332- u_char *answer; /* buffer to put answer */
1333- int anslen; /* size of answer */
1334+ const char *name; /* domain name */
1335+ int class, type; /* class and type of query */
1336+ u_char *answer; /* buffer to put answer */
1337+ int anslen; /* size of answer */
1338 {
1339 const char *cp, * const *domain;
1340 HEADER *hp = (HEADER *)(void *)answer;
1341@@ -1256,11 +1244,11 @@ int res_search(name, class, type, answer
1342 int done = 0;
1343
1344 for (domain = (const char * const *)_res.dnsrch;
1345- *domain && !done;
1346- domain++) {
1347+ *domain && !done;
1348+ domain++) {
1349
1350 ret = res_querydomain(name, *domain, class, type,
1351- answer, anslen);
1352+ answer, anslen);
1353 if (ret > 0)
1354 return (ret);
1355
1356@@ -1283,22 +1271,22 @@ int res_search(name, class, type, answer
1357 }
1358
1359 switch (h_errno) {
1360- case NO_DATA:
1361- got_nodata++;
1362- /* FALLTHROUGH */
1363- case HOST_NOT_FOUND:
1364- /* keep trying */
1365- break;
1366- case TRY_AGAIN:
1367- if (hp->rcode == SERVFAIL) {
1368- /* try next search element, if any */
1369- got_servfail++;
1370+ case NO_DATA:
1371+ got_nodata++;
1372+ /* FALLTHROUGH */
1373+ case HOST_NOT_FOUND:
1374+ /* keep trying */
1375 break;
1376- }
1377- /* FALLTHROUGH */
1378- default:
1379- /* anything else implies that we're done */
1380- done++;
1381+ case TRY_AGAIN:
1382+ if (hp->rcode == SERVFAIL) {
1383+ /* try next search element, if any */
1384+ got_servfail++;
1385+ break;
1386+ }
1387+ /* FALLTHROUGH */
1388+ default:
1389+ /* anything else implies that we're done */
1390+ done++;
1391 }
1392 /*
1393 * if we got here for some reason other than DNSRCH,
1394@@ -1342,10 +1330,10 @@ int res_search(name, class, type, answer
1395 * removing a trailing dot from name if domain is NULL.
1396 */
1397 int res_querydomain(name, domain, class, type, answer, anslen)
1398- const char *name, *domain;
1399- int class, type; /* class and type of query */
1400- u_char *answer; /* buffer to put answer */
1401- int anslen; /* size of answer */
1402+ const char *name, *domain;
1403+ int class, type; /* class and type of query */
1404+ u_char *answer; /* buffer to put answer */
1405+ int anslen; /* size of answer */
1406 {
1407 char nbuf[MAXDNAME];
1408 const char *longname = nbuf;
1409@@ -1359,7 +1347,7 @@ int res_querydomain(name, domain, class,
1410 #ifdef DEBUG
1411 if (_res.options & RES_DEBUG)
1412 printf(";; res_querydomain(%s, %s, %d, %d)\n",
1413- name, domain?domain:"<Nil>", class, type);
1414+ name, domain?domain:"<Nil>", class, type);
1415 #endif
1416 if (domain == NULL) {
1417 /*
1418@@ -1400,11 +1388,11 @@ struct hostent *gethostbyaddr (const voi
1419 static struct hostent h;
1420 static char buf[
1421 #ifndef __UCLIBC_HAS_IPV6__
1422- sizeof(struct in_addr) + sizeof(struct in_addr *)*2 +
1423+ sizeof(struct in_addr) + sizeof(struct in_addr *)*2 +
1424 #else
1425- sizeof(struct in6_addr) + sizeof(struct in6_addr *)*2 +
1426+ sizeof(struct in6_addr) + sizeof(struct in6_addr *)*2 +
1427 #endif /* __UCLIBC_HAS_IPV6__ */
1428- sizeof(char *)*(ALIAS_DIM) + 384/*namebuffer*/ + 32/* margin */];
1429+ sizeof(char *)*(ALIAS_DIM) + 384/*namebuffer*/ + 32/* margin */];
1430 struct hostent *hp;
1431
1432 gethostbyaddr_r(addr, len, type, &h, buf, sizeof(buf), &hp, &h_errno);
1433@@ -1425,11 +1413,11 @@ void __open_etc_hosts(FILE **fp)
1434 }
1435
1436 int __read_etc_hosts_r(FILE * fp, const char * name, int type,
1437- enum etc_hosts_action action,
1438- struct hostent * result_buf,
1439- char * buf, size_t buflen,
1440- struct hostent ** result,
1441- int * h_errnop)
1442+ enum etc_hosts_action action,
1443+ struct hostent * result_buf,
1444+ char * buf, size_t buflen,
1445+ struct hostent ** result,
1446+ int * h_errnop)
1447 {
1448 struct in_addr *in=NULL;
1449 struct in_addr **addr_list=NULL;
1450@@ -1576,56 +1564,49 @@ int __read_etc_hosts_r(FILE * fp, const
1451
1452 #ifdef L_gethostent
1453
1454-#ifdef __UCLIBC_HAS_THREADS__
1455-static pthread_mutex_t mylock = PTHREAD_MUTEX_INITIALIZER;
1456-# define LOCK __pthread_mutex_lock(&mylock)
1457-# define UNLOCK __pthread_mutex_unlock(&mylock);
1458-#else
1459-# define LOCK
1460-# define UNLOCK
1461-#endif
1462+__UCLIBC_MUTEX_STATIC(mylock, PTHREAD_MUTEX_INITIALIZER);
1463
1464 static int __stay_open;
1465 static FILE * __gethostent_fp;
1466
1467 void endhostent (void)
1468 {
1469- LOCK;
1470+ __UCLIBC_MUTEX_LOCK(mylock);
1471 __stay_open = 0;
1472 if (__gethostent_fp) {
1473- fclose(__gethostent_fp);
1474+ fclose(__gethostent_fp);
1475 }
1476- UNLOCK;
1477+ __UCLIBC_MUTEX_UNLOCK(mylock);
1478 }
1479
1480 void sethostent (int stay_open)
1481 {
1482- LOCK;
1483+ __UCLIBC_MUTEX_LOCK(mylock);
1484 __stay_open = stay_open;
1485- UNLOCK;
1486+ __UCLIBC_MUTEX_UNLOCK(mylock);
1487 }
1488
1489 int gethostent_r(struct hostent *result_buf, char *buf, size_t buflen,
1490- struct hostent **result, int *h_errnop)
1491+ struct hostent **result, int *h_errnop)
1492 {
1493- int ret;
1494+ int ret = 0;
1495
1496- LOCK;
1497+ __UCLIBC_MUTEX_LOCK(mylock);
1498 if (__gethostent_fp == NULL) {
1499- __open_etc_hosts(&__gethostent_fp);
1500- if (__gethostent_fp == NULL) {
1501- UNLOCK;
1502- *result=NULL;
1503- return 0;
1504- }
1505+ __open_etc_hosts(&__gethostent_fp);
1506+ if (__gethostent_fp == NULL) {
1507+ *result=NULL;
1508+ goto DONE;
1509+ }
1510 }
1511
1512 ret = __read_etc_hosts_r(__gethostent_fp, NULL, AF_INET, GETHOSTENT,
1513- result_buf, buf, buflen, result, h_errnop);
1514+ result_buf, buf, buflen, result, h_errnop);
1515 if (__stay_open==0) {
1516- fclose(__gethostent_fp);
1517+ fclose(__gethostent_fp);
1518 }
1519- UNLOCK;
1520+ DONE:
1521+ __UCLIBC_MUTEX_UNLOCK(mylock);
1522 return(ret);
1523 }
1524
1525@@ -1634,17 +1615,17 @@ struct hostent *gethostent (void)
1526 static struct hostent h;
1527 static char buf[
1528 #ifndef __UCLIBC_HAS_IPV6__
1529- sizeof(struct in_addr) + sizeof(struct in_addr *)*2 +
1530+ sizeof(struct in_addr) + sizeof(struct in_addr *)*2 +
1531 #else
1532- sizeof(struct in6_addr) + sizeof(struct in6_addr *)*2 +
1533+ sizeof(struct in6_addr) + sizeof(struct in6_addr *)*2 +
1534 #endif /* __UCLIBC_HAS_IPV6__ */
1535- sizeof(char *)*(ALIAS_DIM) +
1536- 80/*namebuffer*/ + 2/* margin */];
1537+ sizeof(char *)*(ALIAS_DIM) +
1538+ 80/*namebuffer*/ + 2/* margin */];
1539 struct hostent *host;
1540
1541- LOCK;
1542+ __UCLIBC_MUTEX_LOCK(mylock);
1543 gethostent_r(&h, buf, sizeof(buf), &host, &h_errno);
1544- UNLOCK;
1545+ __UCLIBC_MUTEX_UNLOCK(mylock);
1546 return(host);
1547 }
1548 #endif
1549@@ -1652,23 +1633,23 @@ struct hostent *gethostent (void)
1550 #ifdef L_get_hosts_byname_r
1551
1552 int __get_hosts_byname_r(const char * name, int type,
1553- struct hostent * result_buf,
1554- char * buf, size_t buflen,
1555- struct hostent ** result,
1556- int * h_errnop)
1557+ struct hostent * result_buf,
1558+ char * buf, size_t buflen,
1559+ struct hostent ** result,
1560+ int * h_errnop)
1561 {
1562 return(__read_etc_hosts_r(NULL, name, type, GET_HOSTS_BYNAME,
1563- result_buf, buf, buflen, result, h_errnop));
1564+ result_buf, buf, buflen, result, h_errnop));
1565 }
1566 #endif
1567
1568 #ifdef L_get_hosts_byaddr_r
1569
1570 int __get_hosts_byaddr_r(const char * addr, int len, int type,
1571- struct hostent * result_buf,
1572- char * buf, size_t buflen,
1573- struct hostent ** result,
1574- int * h_errnop)
1575+ struct hostent * result_buf,
1576+ char * buf, size_t buflen,
1577+ struct hostent ** result,
1578+ int * h_errnop)
1579 {
1580 #ifndef __UCLIBC_HAS_IPV6__
1581 char ipaddr[INET_ADDRSTRLEN];
1582@@ -1677,24 +1658,24 @@ int __get_hosts_byaddr_r(const char * ad
1583 #endif /* __UCLIBC_HAS_IPV6__ */
1584
1585 switch (type) {
1586- case AF_INET:
1587- if (len != sizeof(struct in_addr))
1588- return 0;
1589- break;
1590+ case AF_INET:
1591+ if (len != sizeof(struct in_addr))
1592+ return 0;
1593+ break;
1594 #ifdef __UCLIBC_HAS_IPV6__
1595- case AF_INET6:
1596- if (len != sizeof(struct in6_addr))
1597- return 0;
1598- break;
1599+ case AF_INET6:
1600+ if (len != sizeof(struct in6_addr))
1601+ return 0;
1602+ break;
1603 #endif /* __UCLIBC_HAS_IPV6__ */
1604- default:
1605- return 0;
1606+ default:
1607+ return 0;
1608 }
1609
1610 inet_ntop(type, addr, ipaddr, sizeof(ipaddr));
1611
1612 return(__read_etc_hosts_r(NULL, ipaddr, type, GET_HOSTS_BYADDR,
1613- result_buf, buf, buflen, result, h_errnop));
1614+ result_buf, buf, buflen, result, h_errnop));
1615 }
1616 #endif
1617
1618@@ -1705,8 +1686,8 @@ int __get_hosts_byaddr_r(const char * ad
1619 #endif /* min */
1620
1621 int getnameinfo (const struct sockaddr *sa, socklen_t addrlen, char *host,
1622- socklen_t hostlen, char *serv, socklen_t servlen,
1623- unsigned int flags)
1624+ socklen_t hostlen, char *serv, socklen_t servlen,
1625+ unsigned int flags)
1626 {
1627 int serrno = errno;
1628 int ok = 0;
1629@@ -1720,167 +1701,167 @@ int getnameinfo (const struct sockaddr *
1630 return EAI_FAMILY;
1631
1632 switch (sa->sa_family) {
1633- case AF_LOCAL:
1634- break;
1635- case AF_INET:
1636- if (addrlen < sizeof (struct sockaddr_in))
1637- return EAI_FAMILY;
1638- break;
1639+ case AF_LOCAL:
1640+ break;
1641+ case AF_INET:
1642+ if (addrlen < sizeof (struct sockaddr_in))
1643+ return EAI_FAMILY;
1644+ break;
1645 #ifdef __UCLIBC_HAS_IPV6__
1646- case AF_INET6:
1647- if (addrlen < sizeof (struct sockaddr_in6))
1648- return EAI_FAMILY;
1649- break;
1650+ case AF_INET6:
1651+ if (addrlen < sizeof (struct sockaddr_in6))
1652+ return EAI_FAMILY;
1653+ break;
1654 #endif /* __UCLIBC_HAS_IPV6__ */
1655- default:
1656- return EAI_FAMILY;
1657+ default:
1658+ return EAI_FAMILY;
1659 }
1660
1661 if (host != NULL && hostlen > 0)
1662 switch (sa->sa_family) {
1663- case AF_INET:
1664+ case AF_INET:
1665 #ifdef __UCLIBC_HAS_IPV6__
1666- case AF_INET6:
1667+ case AF_INET6:
1668 #endif /* __UCLIBC_HAS_IPV6__ */
1669- if (!(flags & NI_NUMERICHOST)) {
1670+ if (!(flags & NI_NUMERICHOST)) {
1671 #ifdef __UCLIBC_HAS_IPV6__
1672- if (sa->sa_family == AF_INET6)
1673- h = gethostbyaddr ((const void *)
1674- &(((const struct sockaddr_in6 *) sa)->sin6_addr),
1675- sizeof(struct in6_addr), AF_INET6);
1676- else
1677-#endif /* __UCLIBC_HAS_IPV6__ */
1678- h = gethostbyaddr ((const void *) &(((const struct sockaddr_in *)sa)->sin_addr),
1679- sizeof(struct in_addr), AF_INET);
1680-
1681- if (h) {
1682- char *c;
1683- if ((flags & NI_NOFQDN)
1684- && (getdomainname (domain, sizeof(domain)) == 0)
1685- && (c = strstr (h->h_name, domain))
1686- && (c != h->h_name) && (*(--c) == '.')) {
1687- strncpy (host, h->h_name,
1688- min(hostlen, (size_t) (c - h->h_name)));
1689- host[min(hostlen - 1, (size_t) (c - h->h_name))] = '\0';
1690- ok = 1;
1691- } else {
1692- strncpy (host, h->h_name, hostlen);
1693- ok = 1;
1694+ if (sa->sa_family == AF_INET6)
1695+ h = gethostbyaddr ((const void *)
1696+ &(((const struct sockaddr_in6 *) sa)->sin6_addr),
1697+ sizeof(struct in6_addr), AF_INET6);
1698+ else
1699+#endif /* __UCLIBC_HAS_IPV6__ */
1700+ h = gethostbyaddr ((const void *) &(((const struct sockaddr_in *)sa)->sin_addr),
1701+ sizeof(struct in_addr), AF_INET);
1702+
1703+ if (h) {
1704+ char *c;
1705+ if ((flags & NI_NOFQDN)
1706+ && (getdomainname (domain, sizeof(domain)) == 0)
1707+ && (c = strstr (h->h_name, domain))
1708+ && (c != h->h_name) && (*(--c) == '.')) {
1709+ strncpy (host, h->h_name,
1710+ min(hostlen, (size_t) (c - h->h_name)));
1711+ host[min(hostlen - 1, (size_t) (c - h->h_name))] = '\0';
1712+ ok = 1;
1713+ } else {
1714+ strncpy (host, h->h_name, hostlen);
1715+ ok = 1;
1716+ }
1717 }
1718- }
1719- }
1720+ }
1721
1722- if (!ok) {
1723- if (flags & NI_NAMEREQD) {
1724- errno = serrno;
1725- return EAI_NONAME;
1726- } else {
1727- const char *c;
1728+ if (!ok) {
1729+ if (flags & NI_NAMEREQD) {
1730+ errno = serrno;
1731+ return EAI_NONAME;
1732+ } else {
1733+ const char *c;
1734 #ifdef __UCLIBC_HAS_IPV6__
1735- if (sa->sa_family == AF_INET6) {
1736- const struct sockaddr_in6 *sin6p;
1737+ if (sa->sa_family == AF_INET6) {
1738+ const struct sockaddr_in6 *sin6p;
1739
1740- sin6p = (const struct sockaddr_in6 *) sa;
1741+ sin6p = (const struct sockaddr_in6 *) sa;
1742
1743- c = inet_ntop (AF_INET6,
1744- (const void *) &sin6p->sin6_addr, host, hostlen);
1745+ c = inet_ntop (AF_INET6,
1746+ (const void *) &sin6p->sin6_addr, host, hostlen);
1747 #if 0
1748- /* Does scope id need to be supported? */
1749- uint32_t scopeid;
1750- scopeid = sin6p->sin6_scope_id;
1751- if (scopeid != 0) {
1752- /* Buffer is >= IFNAMSIZ+1. */
1753- char scopebuf[IFNAMSIZ + 1];
1754- char *scopeptr;
1755- int ni_numericscope = 0;
1756- size_t real_hostlen = __strnlen (host, hostlen);
1757- size_t scopelen = 0;
1758-
1759- scopebuf[0] = SCOPE_DELIMITER;
1760- scopebuf[1] = '\0';
1761- scopeptr = &scopebuf[1];
1762-
1763- if (IN6_IS_ADDR_LINKLOCAL (&sin6p->sin6_addr)
1764- || IN6_IS_ADDR_MC_LINKLOCAL (&sin6p->sin6_addr)) {
1765- if (if_indextoname (scopeid, scopeptr) == NULL)
1766+ /* Does scope id need to be supported? */
1767+ uint32_t scopeid;
1768+ scopeid = sin6p->sin6_scope_id;
1769+ if (scopeid != 0) {
1770+ /* Buffer is >= IFNAMSIZ+1. */
1771+ char scopebuf[IFNAMSIZ + 1];
1772+ char *scopeptr;
1773+ int ni_numericscope = 0;
1774+ size_t real_hostlen = __strnlen (host, hostlen);
1775+ size_t scopelen = 0;
1776+
1777+ scopebuf[0] = SCOPE_DELIMITER;
1778+ scopebuf[1] = '\0';
1779+ scopeptr = &scopebuf[1];
1780+
1781+ if (IN6_IS_ADDR_LINKLOCAL (&sin6p->sin6_addr)
1782+ || IN6_IS_ADDR_MC_LINKLOCAL (&sin6p->sin6_addr)) {
1783+ if (if_indextoname (scopeid, scopeptr) == NULL)
1784+ ++ni_numericscope;
1785+ else
1786+ scopelen = strlen (scopebuf);
1787+ } else {
1788 ++ni_numericscope;
1789- else
1790- scopelen = strlen (scopebuf);
1791- } else {
1792- ++ni_numericscope;
1793- }
1794+ }
1795
1796- if (ni_numericscope)
1797- scopelen = 1 + snprintf (scopeptr,
1798- (scopebuf
1799- + sizeof scopebuf
1800- - scopeptr),
1801- "%u", scopeid);
1802-
1803- if (real_hostlen + scopelen + 1 > hostlen)
1804- return EAI_SYSTEM;
1805- memcpy (host + real_hostlen, scopebuf, scopelen + 1);
1806- }
1807+ if (ni_numericscope)
1808+ scopelen = 1 + snprintf (scopeptr,
1809+ (scopebuf
1810+ + sizeof scopebuf
1811+ - scopeptr),
1812+ "%u", scopeid);
1813+
1814+ if (real_hostlen + scopelen + 1 > hostlen)
1815+ return EAI_SYSTEM;
1816+ memcpy (host + real_hostlen, scopebuf, scopelen + 1);
1817+ }
1818 #endif
1819- } else
1820+ } else
1821 #endif /* __UCLIBC_HAS_IPV6__ */
1822- c = inet_ntop (AF_INET, (const void *)
1823- &(((const struct sockaddr_in *) sa)->sin_addr),
1824- host, hostlen);
1825-
1826- if (c == NULL) {
1827- errno = serrno;
1828- return EAI_SYSTEM;
1829+ c = inet_ntop (AF_INET, (const void *)
1830+ &(((const struct sockaddr_in *) sa)->sin_addr),
1831+ host, hostlen);
1832+
1833+ if (c == NULL) {
1834+ errno = serrno;
1835+ return EAI_SYSTEM;
1836+ }
1837 }
1838+ ok = 1;
1839 }
1840- ok = 1;
1841- }
1842- break;
1843-
1844- case AF_LOCAL:
1845- if (!(flags & NI_NUMERICHOST)) {
1846- struct utsname utsname;
1847+ break;
1848
1849- if (!uname (&utsname)) {
1850- strncpy (host, utsname.nodename, hostlen);
1851- break;
1852+ case AF_LOCAL:
1853+ if (!(flags & NI_NUMERICHOST)) {
1854+ struct utsname utsname;
1855+
1856+ if (!uname (&utsname)) {
1857+ strncpy (host, utsname.nodename, hostlen);
1858+ break;
1859+ };
1860 };
1861- };
1862
1863- if (flags & NI_NAMEREQD) {
1864- errno = serrno;
1865- return EAI_NONAME;
1866- }
1867+ if (flags & NI_NAMEREQD) {
1868+ errno = serrno;
1869+ return EAI_NONAME;
1870+ }
1871
1872- strncpy (host, "localhost", hostlen);
1873- break;
1874+ strncpy (host, "localhost", hostlen);
1875+ break;
1876
1877- default:
1878- return EAI_FAMILY;
1879- }
1880+ default:
1881+ return EAI_FAMILY;
1882+ }
1883
1884 if (serv && (servlen > 0)) {
1885 switch (sa->sa_family) {
1886- case AF_INET:
1887+ case AF_INET:
1888 #ifdef __UCLIBC_HAS_IPV6__
1889- case AF_INET6:
1890+ case AF_INET6:
1891 #endif /* __UCLIBC_HAS_IPV6__ */
1892- if (!(flags & NI_NUMERICSERV)) {
1893- struct servent *s;
1894- s = getservbyport (((const struct sockaddr_in *) sa)->sin_port,
1895- ((flags & NI_DGRAM) ? "udp" : "tcp"));
1896- if (s) {
1897- strncpy (serv, s->s_name, servlen);
1898- break;
1899+ if (!(flags & NI_NUMERICSERV)) {
1900+ struct servent *s;
1901+ s = getservbyport (((const struct sockaddr_in *) sa)->sin_port,
1902+ ((flags & NI_DGRAM) ? "udp" : "tcp"));
1903+ if (s) {
1904+ strncpy (serv, s->s_name, servlen);
1905+ break;
1906+ }
1907 }
1908- }
1909- snprintf (serv, servlen, "%d",
1910- ntohs (((const struct sockaddr_in *) sa)->sin_port));
1911- break;
1912+ snprintf (serv, servlen, "%d",
1913+ ntohs (((const struct sockaddr_in *) sa)->sin_port));
1914+ break;
1915
1916- case AF_LOCAL:
1917- strncpy (serv, ((const struct sockaddr_un *) sa)->sun_path, servlen);
1918- break;
1919+ case AF_LOCAL:
1920+ strncpy (serv, ((const struct sockaddr_un *) sa)->sun_path, servlen);
1921+ break;
1922 }
1923 }
1924 if (host && (hostlen > 0))
1925@@ -1896,10 +1877,10 @@ int getnameinfo (const struct sockaddr *
1926 #ifdef L_gethostbyname_r
1927
1928 int gethostbyname_r(const char * name,
1929- struct hostent * result_buf,
1930- char * buf, size_t buflen,
1931- struct hostent ** result,
1932- int * h_errnop)
1933+ struct hostent * result_buf,
1934+ char * buf, size_t buflen,
1935+ struct hostent ** result,
1936+ int * h_errnop)
1937 {
1938 struct in_addr *in;
1939 struct in_addr **addr_list;
1940@@ -1921,7 +1902,7 @@ int gethostbyname_r(const char * name,
1941 __set_errno(0); /* to check for missing /etc/hosts. */
1942
1943 if ((i=__get_hosts_byname_r(name, AF_INET, result_buf,
1944- buf, buflen, result, h_errnop))==0)
1945+ buf, buflen, result, h_errnop))==0)
1946 return i;
1947 switch (*h_errnop) {
1948 case HOST_NOT_FOUND:
1949@@ -1983,60 +1964,60 @@ int gethostbyname_r(const char * name,
1950
1951 for (;;) {
1952
1953- BIGLOCK;
1954+ __UCLIBC_MUTEX_LOCK(__resolv_lock);
1955 __nameserversXX=__nameservers;
1956 __nameserverXX=__nameserver;
1957- BIGUNLOCK;
1958+ __UCLIBC_MUTEX_UNLOCK(__resolv_lock);
1959 a.buf = buf;
1960 a.buflen = buflen;
1961 a.add_count = 0;
1962 i = __dns_lookup(name, T_A, __nameserversXX, __nameserverXX, &packet, &a);
1963
1964 if (i < 0) {
1965- *h_errnop = HOST_NOT_FOUND;
1966- DPRINTF("__dns_lookup\n");
1967- return TRY_AGAIN;
1968+ *h_errnop = HOST_NOT_FOUND;
1969+ DPRINTF("__dns_lookup\n");
1970+ return TRY_AGAIN;
1971 }
1972
1973 if ((a.rdlength + sizeof(struct in_addr*)) * a.add_count + 256 > buflen)
1974- {
1975- free(a.dotted);
1976- free(packet);
1977- *h_errnop = NETDB_INTERNAL;
1978- DPRINTF("buffer too small for all addresses\n");
1979- return ERANGE;
1980- }
1981+ {
1982+ free(a.dotted);
1983+ free(packet);
1984+ *h_errnop = NETDB_INTERNAL;
1985+ DPRINTF("buffer too small for all addresses\n");
1986+ return ERANGE;
1987+ }
1988 else if(a.add_count > 0)
1989- {
1990- memmove(buf - sizeof(struct in_addr*)*2, buf, a.add_count * a.rdlength);
1991- addr_list = (struct in_addr**)(buf + a.add_count * a.rdlength);
1992- addr_list[0] = in;
1993- for (i = a.add_count-1; i>=0; --i)
1994- addr_list[i+1] = (struct in_addr*)(buf - sizeof(struct in_addr*)*2 + a.rdlength * i);
1995- addr_list[a.add_count + 1] = 0;
1996- buflen -= (((char*)&(addr_list[a.add_count + 2])) - buf);
1997- buf = (char*)&addr_list[a.add_count + 2];
1998- }
1999+ {
2000+ memmove(buf - sizeof(struct in_addr*)*2, buf, a.add_count * a.rdlength);
2001+ addr_list = (struct in_addr**)(buf + a.add_count * a.rdlength);
2002+ addr_list[0] = in;
2003+ for (i = a.add_count-1; i>=0; --i)
2004+ addr_list[i+1] = (struct in_addr*)(buf - sizeof(struct in_addr*)*2 + a.rdlength * i);
2005+ addr_list[a.add_count + 1] = 0;
2006+ buflen -= (((char*)&(addr_list[a.add_count + 2])) - buf);
2007+ buf = (char*)&addr_list[a.add_count + 2];
2008+ }
2009
2010 strncpy(buf, a.dotted, buflen);
2011 free(a.dotted);
2012
2013 if (a.atype == T_A) { /* ADDRESS */
2014- memcpy(in, a.rdata, sizeof(*in));
2015- result_buf->h_name = buf;
2016- result_buf->h_addrtype = AF_INET;
2017- result_buf->h_length = sizeof(*in);
2018- result_buf->h_addr_list = (char **) addr_list;
2019+ memcpy(in, a.rdata, sizeof(*in));
2020+ result_buf->h_name = buf;
2021+ result_buf->h_addrtype = AF_INET;
2022+ result_buf->h_length = sizeof(*in);
2023+ result_buf->h_addr_list = (char **) addr_list;
2024 #ifdef __UCLIBC_MJN3_ONLY__
2025 #warning TODO -- generate the full list
2026 #endif
2027- result_buf->h_aliases = alias; /* TODO: generate the full list */
2028- free(packet);
2029- break;
2030+ result_buf->h_aliases = alias; /* TODO: generate the full list */
2031+ free(packet);
2032+ break;
2033 } else {
2034- free(packet);
2035- *h_errnop=HOST_NOT_FOUND;
2036- return TRY_AGAIN;
2037+ free(packet);
2038+ *h_errnop=HOST_NOT_FOUND;
2039+ return TRY_AGAIN;
2040 }
2041 }
2042
2043@@ -2049,14 +2030,14 @@ int gethostbyname_r(const char * name,
2044 #ifdef L_gethostbyname2_r
2045
2046 int gethostbyname2_r(const char *name, int family,
2047- struct hostent * result_buf,
2048- char * buf, size_t buflen,
2049- struct hostent ** result,
2050- int * h_errnop)
2051+ struct hostent * result_buf,
2052+ char * buf, size_t buflen,
2053+ struct hostent ** result,
2054+ int * h_errnop)
2055 {
2056 #ifndef __UCLIBC_HAS_IPV6__
2057 return family == (AF_INET)? gethostbyname_r(name, result_buf,
2058- buf, buflen, result, h_errnop) : HOST_NOT_FOUND;
2059+ buf, buflen, result, h_errnop) : HOST_NOT_FOUND;
2060 #else /* __UCLIBC_HAS_IPV6__ */
2061 struct in6_addr *in;
2062 struct in6_addr **addr_list;
2063@@ -2084,7 +2065,7 @@ int gethostbyname2_r(const char *name, i
2064 __set_errno(0); /* to check for missing /etc/hosts. */
2065
2066 if ((i=__get_hosts_byname_r(name, AF_INET, result_buf,
2067- buf, buflen, result, h_errnop))==0)
2068+ buf, buflen, result, h_errnop))==0)
2069 return i;
2070 switch (*h_errnop) {
2071 case HOST_NOT_FOUND:
2072@@ -2137,10 +2118,10 @@ int gethostbyname2_r(const char *name, i
2073 memset((char *) &a, '\0', sizeof(a));
2074
2075 for (;;) {
2076- BIGLOCK;
2077- __nameserversXX=__nameservers;
2078- __nameserverXX=__nameserver;
2079- BIGUNLOCK;
2080+ __UCLIBC_MUTEX_LOCK(__resolv_lock);
2081+ __nameserversXX=__nameservers;
2082+ __nameserverXX=__nameserver;
2083+ __UCLIBC_MUTEX_UNLOCK(__resolv_lock);
2084
2085 i = __dns_lookup(buf, T_AAAA, __nameserversXX, __nameserverXX, &packet, &a);
2086
2087@@ -2190,10 +2171,10 @@ int gethostbyname2_r(const char *name, i
2088
2089 #ifdef L_gethostbyaddr_r
2090 int gethostbyaddr_r (const void *addr, socklen_t len, int type,
2091- struct hostent * result_buf,
2092- char * buf, size_t buflen,
2093- struct hostent ** result,
2094- int * h_errnop)
2095+ struct hostent * result_buf,
2096+ char * buf, size_t buflen,
2097+ struct hostent ** result,
2098+ int * h_errnop)
2099
2100 {
2101 struct in_addr *in;
2102@@ -2234,7 +2215,7 @@ int gethostbyaddr_r (const void *addr, s
2103
2104 /* do /etc/hosts first */
2105 if ((i=__get_hosts_byaddr_r(addr, len, type, result_buf,
2106- buf, buflen, result, h_errnop))==0)
2107+ buf, buflen, result, h_errnop))==0)
2108 return i;
2109 switch (*h_errnop) {
2110 case HOST_NOT_FOUND:
2111@@ -2294,7 +2275,7 @@ int gethostbyaddr_r (const void *addr, s
2112 addr_list[0] = in;
2113
2114 sprintf(buf, "%u.%u.%u.%u.in-addr.arpa",
2115- tmp_addr[3], tmp_addr[2], tmp_addr[1], tmp_addr[0]);
2116+ tmp_addr[3], tmp_addr[2], tmp_addr[1], tmp_addr[0]);
2117 #ifdef __UCLIBC_HAS_IPV6__
2118 } else {
2119 memcpy(in6->s6_addr, addr, len);
2120@@ -2304,7 +2285,7 @@ int gethostbyaddr_r (const void *addr, s
2121
2122 for (i = len - 1; i >= 0; i--) {
2123 qp += sprintf(qp, "%x.%x.", in6->s6_addr[i] & 0xf,
2124- (in6->s6_addr[i] >> 4) & 0xf);
2125+ (in6->s6_addr[i] >> 4) & 0xf);
2126 }
2127 strcpy(qp, "ip6.int");
2128 #endif /* __UCLIBC_HAS_IPV6__ */
2129@@ -2314,10 +2295,10 @@ int gethostbyaddr_r (const void *addr, s
2130
2131 for (;;) {
2132
2133- BIGLOCK;
2134- __nameserversXX=__nameservers;
2135- __nameserverXX=__nameserver;
2136- BIGUNLOCK;
2137+ __UCLIBC_MUTEX_LOCK(__resolv_lock);
2138+ __nameserversXX=__nameservers;
2139+ __nameserverXX=__nameserver;
2140+ __UCLIBC_MUTEX_UNLOCK(__resolv_lock);
2141 i = __dns_lookup(buf, T_PTR, __nameserversXX, __nameserverXX, &packet, &a);
2142
2143 if (i < 0) {
2144@@ -2381,7 +2362,7 @@ int gethostbyaddr_r (const void *addr, s
2145 * Return size of compressed name or -1 if there was an error.
2146 */
2147 int __dn_expand(const u_char *msg, const u_char *eom, const u_char *src,
2148- char *dst, int dstsiz)
2149+ char *dst, int dstsiz)
2150 {
2151 int n = ns_name_uncompress(msg, eom, src, dst, (size_t)dstsiz);
2152
2153@@ -2401,7 +2382,7 @@ int __dn_expand(const u_char *msg, const
2154 */
2155 static int printable(int ch)
2156 {
2157- return (ch > 0x20 && ch < 0x7f);
2158+ return (ch > 0x20 && ch < 0x7f);
2159 }
2160
2161 /*
2162@@ -2413,18 +2394,18 @@ static int printable(int ch)
2163 */
2164 static int special(int ch)
2165 {
2166- switch (ch) {
2167+ switch (ch) {
2168 case 0x22: /* '"' */
2169 case 0x2E: /* '.' */
2170 case 0x3B: /* ';' */
2171 case 0x5C: /* '\\' */
2172- /* Special modifiers in zone files. */
2173+ /* Special modifiers in zone files. */
2174 case 0x40: /* '@' */
2175 case 0x24: /* '$' */
2176- return (1);
2177+ return (1);
2178 default:
2179- return (0);
2180- }
2181+ return (0);
2182+ }
2183 }
2184
2185 /*
2186@@ -2436,7 +2417,7 @@ static int special(int ch)
2187 * Root domain returns as "." not "".
2188 */
2189 int __ns_name_uncompress(const u_char *msg, const u_char *eom,
2190- const u_char *src, char *dst, size_t dstsiz)
2191+ const u_char *src, char *dst, size_t dstsiz)
2192 {
2193 u_char tmp[NS_MAXCDNAME];
2194 int n;
2195@@ -2525,7 +2506,7 @@ int __ns_name_ntop(const u_char *src, ch
2196 return (-1);
2197 }
2198 *dn++ = '\0';
2199- return (dn - dst);
2200+ return (dn - dst);
2201 }
2202
2203 /*
2204@@ -2535,7 +2516,7 @@ int __ns_name_ntop(const u_char *src, ch
2205 * -1 if it fails, or consumed octets if it succeeds.
2206 */
2207 int __ns_name_unpack(const u_char *msg, const u_char *eom, const u_char *src,
2208- u_char *dst, size_t dstsiz)
2209+ u_char *dst, size_t dstsiz)
2210 {
2211 const u_char *srcp, *dstlim;
2212 u_char *dstp;
2213@@ -2554,46 +2535,46 @@ int __ns_name_unpack(const u_char *msg,
2214 while ((n = *srcp++) != 0) {
2215 /* Check for indirection. */
2216 switch (n & NS_CMPRSFLGS) {
2217- case 0:
2218- /* Limit checks. */
2219- if (dstp + n + 1 >= dstlim || srcp + n >= eom) {
2220- __set_errno (EMSGSIZE);
2221- return (-1);
2222- }
2223- checked += n + 1;
2224- *dstp++ = n;
2225- memcpy(dstp, srcp, n);
2226- dstp += n;
2227- srcp += n;
2228- break;
2229+ case 0:
2230+ /* Limit checks. */
2231+ if (dstp + n + 1 >= dstlim || srcp + n >= eom) {
2232+ __set_errno (EMSGSIZE);
2233+ return (-1);
2234+ }
2235+ checked += n + 1;
2236+ *dstp++ = n;
2237+ memcpy(dstp, srcp, n);
2238+ dstp += n;
2239+ srcp += n;
2240+ break;
2241
2242- case NS_CMPRSFLGS:
2243- if (srcp >= eom) {
2244- __set_errno (EMSGSIZE);
2245- return (-1);
2246- }
2247- if (len < 0)
2248- len = srcp - src + 1;
2249- srcp = msg + (((n & 0x3f) << 8) | (*srcp & 0xff));
2250- if (srcp < msg || srcp >= eom) { /* Out of range. */
2251- __set_errno (EMSGSIZE);
2252- return (-1);
2253- }
2254- checked += 2;
2255- /*
2256- * Check for loops in the compressed name;
2257- * if we've looked at the whole message,
2258- * there must be a loop.
2259- */
2260- if (checked >= eom - msg) {
2261- __set_errno (EMSGSIZE);
2262- return (-1);
2263- }
2264- break;
2265+ case NS_CMPRSFLGS:
2266+ if (srcp >= eom) {
2267+ __set_errno (EMSGSIZE);
2268+ return (-1);
2269+ }
2270+ if (len < 0)
2271+ len = srcp - src + 1;
2272+ srcp = msg + (((n & 0x3f) << 8) | (*srcp & 0xff));
2273+ if (srcp < msg || srcp >= eom) { /* Out of range. */
2274+ __set_errno (EMSGSIZE);
2275+ return (-1);
2276+ }
2277+ checked += 2;
2278+ /*
2279+ * Check for loops in the compressed name;
2280+ * if we've looked at the whole message,
2281+ * there must be a loop.
2282+ */
2283+ if (checked >= eom - msg) {
2284+ __set_errno (EMSGSIZE);
2285+ return (-1);
2286+ }
2287+ break;
2288
2289- default:
2290- __set_errno (EMSGSIZE);
2291- return (-1); /* flag error */
2292+ default:
2293+ __set_errno (EMSGSIZE);
2294+ return (-1); /* flag error */
2295 }
2296 }
2297 *dstp = '\0';
2298diff --git a/libc/inet/rpc/create_xid.c b/libc/inet/rpc/create_xid.c
2299index cbb961e..c86cbb4 100644
2300--- a/libc/inet/rpc/create_xid.c
2301+++ b/libc/inet/rpc/create_xid.c
2302@@ -27,15 +27,7 @@
2303
2304 /* The RPC code is not threadsafe, but new code should be threadsafe. */
2305
2306-#ifdef __UCLIBC_HAS_THREADS__
2307-#include <pthread.h>
2308-static pthread_mutex_t createxid_lock = PTHREAD_MUTEX_INITIALIZER;
2309-# define LOCK __pthread_mutex_lock(&createxid_lock)
2310-# define UNLOCK __pthread_mutex_unlock(&createxid_lock);
2311-#else
2312-# define LOCK
2313-# define UNLOCK
2314-#endif
2315+__UCLIBC_MUTEX_STATIC(mylock, PTHREAD_MUTEX_INITIALIZER);
2316
2317 static int is_initialized;
2318 static struct drand48_data __rpc_lrand48_data;
2319@@ -43,22 +35,22 @@ static struct drand48_data __rpc_lrand48
2320 unsigned long
2321 _create_xid (void)
2322 {
2323- unsigned long res;
2324+ unsigned long res;
2325
2326- LOCK;
2327+ __UCLIBC_MUTEX_LOCK(mylock);
2328
2329- if (!is_initialized)
2330- {
2331- struct timeval now;
2332+ if (!is_initialized)
2333+ {
2334+ struct timeval now;
2335
2336- gettimeofday (&now, (struct timezone *) 0);
2337- srand48_r (now.tv_sec ^ now.tv_usec, &__rpc_lrand48_data);
2338- is_initialized = 1;
2339- }
2340+ gettimeofday (&now, (struct timezone *) 0);
2341+ srand48_r (now.tv_sec ^ now.tv_usec, &__rpc_lrand48_data);
2342+ is_initialized = 1;
2343+ }
2344
2345- lrand48_r (&__rpc_lrand48_data, &res);
2346+ lrand48_r (&__rpc_lrand48_data, &res);
2347
2348- UNLOCK;
2349+ __UCLIBC_MUTEX_UNLOCK(mylock);
2350
2351- return res;
2352+ return res;
2353 }
2354diff --git a/libc/misc/dirent/closedir.c b/libc/misc/dirent/closedir.c
2355index 068e2d3..56adb23 100644
2356--- a/libc/misc/dirent/closedir.c
2357+++ b/libc/misc/dirent/closedir.c
2358@@ -4,7 +4,6 @@
2359 #include <unistd.h>
2360 #include "dirstream.h"
2361
2362-
2363 int closedir(DIR * dir)
2364 {
2365 int fd;
2366@@ -19,14 +18,10 @@ int closedir(DIR * dir)
2367 __set_errno(EBADF);
2368 return -1;
2369 }
2370-#ifdef __UCLIBC_HAS_THREADS__
2371- __pthread_mutex_lock(&(dir->dd_lock));
2372-#endif
2373+ __UCLIBC_MUTEX_LOCK(dir->dd_lock);
2374 fd = dir->dd_fd;
2375 dir->dd_fd = -1;
2376-#ifdef __UCLIBC_HAS_THREADS__
2377- __pthread_mutex_unlock(&(dir->dd_lock));
2378-#endif
2379+ __UCLIBC_MUTEX_UNLOCK(dir->dd_lock);
2380 free(dir->dd_buf);
2381 free(dir);
2382 return close(fd);
2383diff --git a/libc/misc/dirent/dirstream.h b/libc/misc/dirent/dirstream.h
2384index 2dd0264..bd721c5 100644
2385--- a/libc/misc/dirent/dirstream.h
2386+++ b/libc/misc/dirent/dirstream.h
2387@@ -26,9 +26,8 @@ Cambridge, MA 02139, USA. */
2388
2389 #include <features.h>
2390 #include <sys/types.h>
2391-#ifdef __UCLIBC_HAS_THREADS__
2392-#include <pthread.h>
2393-#endif
2394+
2395+#include <bits/uClibc_mutex.h>
2396
2397 /* For now, syscall readdir () only supports one entry at a time. It
2398 * will be changed in the future.
2399@@ -63,11 +62,7 @@ struct __dirstream {
2400 size_t dd_max;
2401
2402 /* lock */
2403-#ifdef __UCLIBC_HAS_THREADS__
2404- pthread_mutex_t dd_lock;
2405-#else
2406- void *dd_lock;
2407-#endif
2408+ __UCLIBC_MUTEX(dd_lock);
2409 }; /* stream data from opendir() */
2410
2411
2412diff --git a/libc/misc/dirent/readdir.c b/libc/misc/dirent/readdir.c
2413index 1f196e1..c55317a 100644
2414--- a/libc/misc/dirent/readdir.c
2415+++ b/libc/misc/dirent/readdir.c
2416@@ -5,7 +5,6 @@
2417 #include <dirent.h>
2418 #include "dirstream.h"
2419
2420-
2421 struct dirent *readdir(DIR * dir)
2422 {
2423 ssize_t bytes;
2424@@ -16,9 +15,7 @@ struct dirent *readdir(DIR * dir)
2425 return NULL;
2426 }
2427
2428-#ifdef __UCLIBC_HAS_THREADS__
2429- __pthread_mutex_lock(&(dir->dd_lock));
2430-#endif
2431+ __UCLIBC_MUTEX_LOCK(dir->dd_lock);
2432
2433 do {
2434 if (dir->dd_size <= dir->dd_nextloc) {
2435@@ -44,8 +41,6 @@ struct dirent *readdir(DIR * dir)
2436 } while (de->d_ino == 0);
2437
2438 all_done:
2439-#ifdef __UCLIBC_HAS_THREADS__
2440- __pthread_mutex_unlock(&(dir->dd_lock));
2441-#endif
2442+ __UCLIBC_MUTEX_UNLOCK(dir->dd_lock);
2443 return de;
2444 }
2445diff --git a/libc/misc/dirent/readdir64.c b/libc/misc/dirent/readdir64.c
2446index f798c6f..6da3b0d 100644
2447--- a/libc/misc/dirent/readdir64.c
2448+++ b/libc/misc/dirent/readdir64.c
2449@@ -20,7 +20,6 @@
2450 #include <dirent.h>
2451 #include "dirstream.h"
2452
2453-
2454 struct dirent64 *readdir64(DIR * dir)
2455 {
2456 ssize_t bytes;
2457@@ -31,9 +30,7 @@ struct dirent64 *readdir64(DIR * dir)
2458 return NULL;
2459 }
2460
2461-#ifdef __UCLIBC_HAS_THREADS__
2462- __pthread_mutex_lock(&(dir->dd_lock));
2463-#endif
2464+ __UCLIBC_MUTEX_LOCK(dir->dd_lock);
2465
2466 do {
2467 if (dir->dd_size <= dir->dd_nextloc) {
2468@@ -59,9 +56,7 @@ struct dirent64 *readdir64(DIR * dir)
2469 } while (de->d_ino == 0);
2470
2471 all_done:
2472-#ifdef __UCLIBC_HAS_THREADS__
2473- __pthread_mutex_unlock(&(dir->dd_lock));
2474-#endif
2475+ __UCLIBC_MUTEX_UNLOCK(dir->dd_lock);
2476
2477 return de;
2478 }
2479diff --git a/libc/misc/dirent/readdir64_r.c b/libc/misc/dirent/readdir64_r.c
2480index da3564e..cc96eff 100644
2481--- a/libc/misc/dirent/readdir64_r.c
2482+++ b/libc/misc/dirent/readdir64_r.c
2483@@ -19,7 +19,6 @@
2484 #include <dirent.h>
2485 #include "dirstream.h"
2486
2487-
2488 int readdir64_r(DIR *dir, struct dirent64 *entry, struct dirent64 **result)
2489 {
2490 int ret;
2491@@ -32,21 +31,19 @@ int readdir64_r(DIR *dir, struct dirent6
2492 }
2493 de = NULL;
2494
2495-#ifdef __UCLIBC_HAS_THREADS__
2496- __pthread_mutex_lock(&(dir->dd_lock));
2497-#endif
2498+ __UCLIBC_MUTEX_LOCK(dir->dd_lock);
2499
2500 do {
2501 if (dir->dd_size <= dir->dd_nextloc) {
2502- /* read dir->dd_max bytes of directory entries. */
2503- bytes = __getdents64(dir->dd_fd, dir->dd_buf, dir->dd_max);
2504- if (bytes <= 0) {
2505- *result = NULL;
2506- ret = errno;
2507- goto all_done;
2508- }
2509- dir->dd_size = bytes;
2510- dir->dd_nextloc = 0;
2511+ /* read dir->dd_max bytes of directory entries. */
2512+ bytes = __getdents64(dir->dd_fd, dir->dd_buf, dir->dd_max);
2513+ if (bytes <= 0) {
2514+ *result = NULL;
2515+ ret = errno;
2516+ goto all_done;
2517+ }
2518+ dir->dd_size = bytes;
2519+ dir->dd_nextloc = 0;
2520 }
2521
2522 de = (struct dirent64 *) (((char *) dir->dd_buf) + dir->dd_nextloc);
2523@@ -66,12 +63,10 @@ int readdir64_r(DIR *dir, struct dirent6
2524 }
2525 ret = 0;
2526
2527-all_done:
2528+ all_done:
2529
2530-#ifdef __UCLIBC_HAS_THREADS__
2531- __pthread_mutex_unlock(&(dir->dd_lock));
2532-#endif
2533- return((de != NULL)? 0 : ret);
2534+ __UCLIBC_MUTEX_UNLOCK(dir->dd_lock);
2535+ return((de != NULL)? 0 : ret);
2536 }
2537 #endif /* __UCLIBC_HAS_LFS__ */
2538
2539diff --git a/libc/misc/dirent/readdir_r.c b/libc/misc/dirent/readdir_r.c
2540index 245dcbd..aeccdd8 100644
2541--- a/libc/misc/dirent/readdir_r.c
2542+++ b/libc/misc/dirent/readdir_r.c
2543@@ -5,7 +5,6 @@
2544 #include <dirent.h>
2545 #include "dirstream.h"
2546
2547-
2548 int readdir_r(DIR *dir, struct dirent *entry, struct dirent **result)
2549 {
2550 int ret;
2551@@ -18,21 +17,19 @@ int readdir_r(DIR *dir, struct dirent *e
2552 }
2553 de = NULL;
2554
2555-#ifdef __UCLIBC_HAS_THREADS__
2556- __pthread_mutex_lock(&(dir->dd_lock));
2557-#endif
2558+ __UCLIBC_MUTEX_LOCK(dir->dd_lock);
2559
2560 do {
2561 if (dir->dd_size <= dir->dd_nextloc) {
2562- /* read dir->dd_max bytes of directory entries. */
2563- bytes = __getdents(dir->dd_fd, dir->dd_buf, dir->dd_max);
2564- if (bytes <= 0) {
2565- *result = NULL;
2566- ret = errno;
2567- goto all_done;
2568- }
2569- dir->dd_size = bytes;
2570- dir->dd_nextloc = 0;
2571+ /* read dir->dd_max bytes of directory entries. */
2572+ bytes = __getdents(dir->dd_fd, dir->dd_buf, dir->dd_max);
2573+ if (bytes <= 0) {
2574+ *result = NULL;
2575+ ret = errno;
2576+ goto all_done;
2577+ }
2578+ dir->dd_size = bytes;
2579+ dir->dd_nextloc = 0;
2580 }
2581
2582 de = (struct dirent *) (((char *) dir->dd_buf) + dir->dd_nextloc);
2583@@ -52,10 +49,8 @@ int readdir_r(DIR *dir, struct dirent *e
2584 }
2585 ret = 0;
2586
2587-all_done:
2588+ all_done:
2589
2590-#ifdef __UCLIBC_HAS_THREADS__
2591- __pthread_mutex_unlock(&(dir->dd_lock));
2592-#endif
2593- return((de != NULL)? 0 : ret);
2594+ __UCLIBC_MUTEX_UNLOCK(dir->dd_lock);
2595+ return((de != NULL)? 0 : ret);
2596 }
2597diff --git a/libc/misc/dirent/rewinddir.c b/libc/misc/dirent/rewinddir.c
2598index 60ef71d..fe8fc2a 100644
2599--- a/libc/misc/dirent/rewinddir.c
2600+++ b/libc/misc/dirent/rewinddir.c
2601@@ -3,7 +3,6 @@
2602 #include <unistd.h>
2603 #include "dirstream.h"
2604
2605-
2606 /* rewinddir() just does an lseek(fd,0,0) - see close for comments */
2607 void rewinddir(DIR * dir)
2608 {
2609@@ -11,12 +10,8 @@ void rewinddir(DIR * dir)
2610 __set_errno(EBADF);
2611 return;
2612 }
2613-#ifdef __UCLIBC_HAS_THREADS__
2614- __pthread_mutex_lock(&(dir->dd_lock));
2615-#endif
2616+ __UCLIBC_MUTEX_LOCK(dir->dd_lock);
2617 lseek(dir->dd_fd, 0, SEEK_SET);
2618 dir->dd_nextoff = dir->dd_nextloc = dir->dd_size = 0;
2619-#ifdef __UCLIBC_HAS_THREADS__
2620- __pthread_mutex_unlock(&(dir->dd_lock));
2621-#endif
2622+ __UCLIBC_MUTEX_UNLOCK(dir->dd_lock);
2623 }
2624diff --git a/libc/misc/dirent/seekdir.c b/libc/misc/dirent/seekdir.c
2625index 139f1e1..6d6f5f0 100644
2626--- a/libc/misc/dirent/seekdir.c
2627+++ b/libc/misc/dirent/seekdir.c
2628@@ -3,19 +3,14 @@
2629 #include <unistd.h>
2630 #include "dirstream.h"
2631
2632-
2633 void seekdir(DIR * dir, long int offset)
2634 {
2635 if (!dir) {
2636 __set_errno(EBADF);
2637 return;
2638 }
2639-#ifdef __UCLIBC_HAS_THREADS__
2640- __pthread_mutex_lock(&(dir->dd_lock));
2641-#endif
2642+ __UCLIBC_MUTEX_LOCK(dir->dd_lock);
2643 dir->dd_nextoff = lseek(dir->dd_fd, offset, SEEK_SET);
2644 dir->dd_size = dir->dd_nextloc = 0;
2645-#ifdef __UCLIBC_HAS_THREADS__
2646- __pthread_mutex_unlock(&(dir->dd_lock));
2647-#endif
2648+ __UCLIBC_MUTEX_UNLOCK(dir->dd_lock);
2649 }
2650diff --git a/libc/misc/mntent/mntent.c b/libc/misc/mntent/mntent.c
2651index d98a687..af6d848 100644
2652--- a/libc/misc/mntent/mntent.c
2653+++ b/libc/misc/mntent/mntent.c
2654@@ -3,15 +3,9 @@
2655 #include <string.h>
2656 #include <mntent.h>
2657
2658-#ifdef __UCLIBC_HAS_THREADS__
2659-#include <pthread.h>
2660-static pthread_mutex_t mylock = PTHREAD_MUTEX_INITIALIZER;
2661-# define LOCK __pthread_mutex_lock(&mylock)
2662-# define UNLOCK __pthread_mutex_unlock(&mylock);
2663-#else
2664-# define LOCK
2665-# define UNLOCK
2666-#endif
2667+#include <bits/uClibc_mutex.h>
2668+
2669+__UCLIBC_MUTEX_STATIC(mylock, PTHREAD_MUTEX_INITIALIZER);
2670
2671 /* Reentrant version of getmntent. */
2672 struct mntent *getmntent_r (FILE *filep,
2673@@ -67,7 +61,7 @@ struct mntent *getmntent(FILE * filep)
2674 struct mntent *tmp;
2675 static char *buff = NULL;
2676 static struct mntent mnt;
2677- LOCK;
2678+ __UCLIBC_MUTEX_LOCK(mylock);
2679
2680 if (!buff) {
2681 buff = malloc(BUFSIZ);
2682@@ -76,7 +70,7 @@ struct mntent *getmntent(FILE * filep)
2683 }
2684
2685 tmp = getmntent_r(filep, &mnt, buff, BUFSIZ);
2686- UNLOCK;
2687+ __UCLIBC_MUTEX_UNLOCK(mylock);
2688 return(tmp);
2689 }
2690
2691diff --git a/libc/misc/pthread/weaks.c b/libc/misc/pthread/weaks.c
2692index 89c2611..c27bd10 100644
2693--- a/libc/misc/pthread/weaks.c
2694+++ b/libc/misc/pthread/weaks.c
2695@@ -21,6 +21,7 @@
2696 #include <limits.h>
2697 #include <stdlib.h>
2698
2699+static void __pthread_return_void __P ((void));
2700 static int __pthread_return_0 __P ((void));
2701 static int __pthread_return_1 __P ((void));
2702
2703@@ -104,8 +105,17 @@ weak_alias (__pthread_return_0, __pthrea
2704 weak_alias (__pthread_return_0, __pthread_mutex_trylock)
2705 weak_alias (__pthread_return_0, __pthread_mutex_unlock)
2706
2707+weak_alias (__pthread_return_void, _pthread_cleanup_push_defer)
2708+weak_alias (__pthread_return_void, _pthread_cleanup_pop_restore)
2709+
2710 /**********************************************************************/
2711
2712+static void
2713+__pthread_return_void (void)
2714+{
2715+ return;
2716+}
2717+
2718 static int
2719 __pthread_return_0 (void)
2720 {
2721diff --git a/libc/misc/syslog/syslog.c b/libc/misc/syslog/syslog.c
2722index 2b478e1..9e9ddbf 100644
2723--- a/libc/misc/syslog/syslog.c
2724+++ b/libc/misc/syslog/syslog.c
2725@@ -80,17 +80,9 @@
2726 #include <ctype.h>
2727 #include <signal.h>
2728
2729+#include <bits/uClibc_mutex.h>
2730
2731-#ifdef __UCLIBC_HAS_THREADS__
2732-#include <pthread.h>
2733-static pthread_mutex_t mylock = PTHREAD_RECURSIVE_MUTEX_INITIALIZER_NP;
2734-# define LOCK __pthread_mutex_lock(&mylock)
2735-# define UNLOCK __pthread_mutex_unlock(&mylock);
2736-#else
2737-# define LOCK
2738-# define UNLOCK
2739-#endif
2740-
2741+__UCLIBC_MUTEX_STATIC(mylock, PTHREAD_RECURSIVE_MUTEX_INITIALIZER_NP);
2742
2743 static int LogFile = -1; /* fd for log */
2744 static int connected; /* have done connect */
2745@@ -110,26 +102,26 @@ int setlogmask(int pmask);
2746 static void
2747 closelog_intern(int to_default)
2748 {
2749- LOCK;
2750+ __UCLIBC_MUTEX_LOCK(mylock);
2751 if (LogFile != -1) {
2752 (void) close(LogFile);
2753 }
2754 LogFile = -1;
2755 connected = 0;
2756 if (to_default)
2757- {
2758- LogStat = 0;
2759- LogTag = "syslog";
2760- LogFacility = LOG_USER;
2761- LogMask = 0xff;
2762- }
2763- UNLOCK;
2764+ {
2765+ LogStat = 0;
2766+ LogTag = "syslog";
2767+ LogFacility = LOG_USER;
2768+ LogMask = 0xff;
2769+ }
2770+ __UCLIBC_MUTEX_UNLOCK(mylock);
2771 }
2772
2773 static void
2774 sigpipe_handler (int sig)
2775 {
2776- closelog_intern (0);
2777+ closelog_intern (0);
2778 }
2779
2780 /*
2781@@ -165,7 +157,7 @@ vsyslog( int pri, const char *fmt, va_li
2782
2783 saved_errno = errno;
2784
2785- LOCK;
2786+ __UCLIBC_MUTEX_LOCK(mylock);
2787
2788 /* See if we should just throw out this message. */
2789 if (!(LogMask & LOG_MASK(LOG_PRI(pri))) || (pri &~ (LOG_PRIMASK|LOG_FACMASK)))
2790@@ -208,7 +200,7 @@ vsyslog( int pri, const char *fmt, va_li
2791 if (p >= end || p < head_end) { /* Returned -1 in case of error... */
2792 static const char truncate_msg[12] = "[truncated] ";
2793 memmove(head_end + sizeof(truncate_msg), head_end,
2794- end - head_end - sizeof(truncate_msg));
2795+ end - head_end - sizeof(truncate_msg));
2796 memcpy(head_end, truncate_msg, sizeof(truncate_msg));
2797 if (p < head_end) {
2798 while (p < end && *p) {
2799@@ -261,11 +253,11 @@ vsyslog( int pri, const char *fmt, va_li
2800 (void)close(fd);
2801 }
2802
2803-getout:
2804- UNLOCK;
2805+ getout:
2806+ __UCLIBC_MUTEX_UNLOCK(mylock);
2807 if (sigpipe == 0)
2808 sigaction (SIGPIPE, &oldaction,
2809- (struct sigaction *) NULL);
2810+ (struct sigaction *) NULL);
2811 }
2812
2813 /*
2814@@ -276,48 +268,48 @@ openlog( const char *ident, int logstat,
2815 {
2816 int logType = SOCK_DGRAM;
2817
2818- LOCK;
2819+ __UCLIBC_MUTEX_LOCK(mylock);
2820
2821 if (ident != NULL)
2822- LogTag = ident;
2823+ LogTag = ident;
2824 LogStat = logstat;
2825 if (logfac != 0 && (logfac &~ LOG_FACMASK) == 0)
2826- LogFacility = logfac;
2827+ LogFacility = logfac;
2828 if (LogFile == -1) {
2829- SyslogAddr.sa_family = AF_UNIX;
2830- (void)strncpy(SyslogAddr.sa_data, _PATH_LOG,
2831- sizeof(SyslogAddr.sa_data));
2832-retry:
2833- if (LogStat & LOG_NDELAY) {
2834- if ((LogFile = socket(AF_UNIX, logType, 0)) == -1){
2835- UNLOCK;
2836- return;
2837- }
2838- /* fcntl(LogFile, F_SETFD, 1); */
2839- }
2840+ SyslogAddr.sa_family = AF_UNIX;
2841+ (void)strncpy(SyslogAddr.sa_data, _PATH_LOG,
2842+ sizeof(SyslogAddr.sa_data));
2843+ retry:
2844+ if (LogStat & LOG_NDELAY) {
2845+ if ((LogFile = socket(AF_UNIX, logType, 0)) == -1){
2846+ goto DONE;
2847+ }
2848+ /* fcntl(LogFile, F_SETFD, 1); */
2849+ }
2850 }
2851
2852 if (LogFile != -1 && !connected) {
2853- if (connect(LogFile, &SyslogAddr, sizeof(SyslogAddr) -
2854- sizeof(SyslogAddr.sa_data) + strlen(SyslogAddr.sa_data)) != -1)
2855- {
2856- connected = 1;
2857- } else if (logType == SOCK_DGRAM) {
2858- logType = SOCK_STREAM;
2859- if (LogFile != -1) {
2860- close(LogFile);
2861- LogFile = -1;
2862- }
2863- goto retry;
2864- } else {
2865- if (LogFile != -1) {
2866- close(LogFile);
2867- LogFile = -1;
2868- }
2869- }
2870+ if (connect(LogFile, &SyslogAddr, sizeof(SyslogAddr) -
2871+ sizeof(SyslogAddr.sa_data) + strlen(SyslogAddr.sa_data)) != -1)
2872+ {
2873+ connected = 1;
2874+ } else if (logType == SOCK_DGRAM) {
2875+ logType = SOCK_STREAM;
2876+ if (LogFile != -1) {
2877+ close(LogFile);
2878+ LogFile = -1;
2879+ }
2880+ goto retry;
2881+ } else {
2882+ if (LogFile != -1) {
2883+ close(LogFile);
2884+ LogFile = -1;
2885+ }
2886+ }
2887 }
2888
2889- UNLOCK;
2890+ DONE:
2891+ __UCLIBC_MUTEX_UNLOCK(mylock);
2892 }
2893
2894 /*
2895@@ -335,10 +327,10 @@ int setlogmask(int pmask)
2896 int omask;
2897
2898 omask = LogMask;
2899- LOCK;
2900+ __UCLIBC_MUTEX_LOCK(mylock);
2901 if (pmask != 0)
2902- LogMask = pmask;
2903- UNLOCK;
2904+ LogMask = pmask;
2905+ __UCLIBC_MUTEX_UNLOCK(mylock);
2906 return (omask);
2907 }
2908
2909diff --git a/libc/misc/time/time.c b/libc/misc/time/time.c
2910index f43bb8a..6165a52 100644
2911--- a/libc/misc/time/time.c
2912+++ b/libc/misc/time/time.c
2913@@ -143,6 +143,8 @@
2914 #include <locale.h>
2915 #include <bits/uClibc_uintmaxtostr.h>
2916
2917+#include <bits/uClibc_mutex.h>
2918+
2919 #ifdef __UCLIBC_HAS_XLOCALE__
2920 #include <xlocale.h>
2921 #endif
2922@@ -191,21 +193,7 @@ typedef struct {
2923 char tzname[TZNAME_MAX+1];
2924 } rule_struct;
2925
2926-#ifdef __UCLIBC_HAS_THREADS__
2927-
2928-#include <pthread.h>
2929-
2930-extern pthread_mutex_t _time_tzlock;
2931-
2932-#define TZLOCK __pthread_mutex_lock(&_time_tzlock)
2933-#define TZUNLOCK __pthread_mutex_unlock(&_time_tzlock)
2934-
2935-#else
2936-
2937-#define TZLOCK ((void) 0)
2938-#define TZUNLOCK ((void) 0)
2939-
2940-#endif
2941+__UCLIBC_MUTEX_EXTERN(_time_tzlock);
2942
2943 extern rule_struct _time_tzinfo[2];
2944
2945@@ -542,13 +530,13 @@ struct tm *localtime(const time_t *timer
2946 struct tm *localtime_r(register const time_t *__restrict timer,
2947 register struct tm *__restrict result)
2948 {
2949- TZLOCK;
2950+ __UCLIBC_MUTEX_LOCK(_time_tzlock);
2951
2952 tzset();
2953
2954 __time_localtime_tzi(timer, result, _time_tzinfo);
2955
2956- TZUNLOCK;
2957+ __UCLIBC_MUTEX_UNLOCK(_time_tzlock);
2958
2959 return result;
2960 }
2961@@ -1037,7 +1025,7 @@ size_t __XL(strftime)(char *__restrict s
2962 goto LOOP;
2963 }
2964
2965- o = spec + 26; /* set to "????" */
2966+ o = ((const char *) spec) + 26; /* set to "????" */
2967 if ((code & MASK_SPEC) == CALC_SPEC) {
2968
2969 if (*p == 's') {
2970@@ -1073,17 +1061,15 @@ size_t __XL(strftime)(char *__restrict s
2971
2972 #ifdef __UCLIBC_HAS_TM_EXTENSIONS__
2973
2974-#define RSP_TZUNLOCK ((void) 0)
2975 #define RSP_TZNAME timeptr->tm_zone
2976 #define RSP_GMT_OFFSET (-timeptr->tm_gmtoff)
2977
2978 #else
2979
2980-#define RSP_TZUNLOCK TZUNLOCK
2981 #define RSP_TZNAME rsp->tzname
2982 #define RSP_GMT_OFFSET rsp->gmt_offset
2983
2984- TZLOCK;
2985+ __UCLIBC_MUTEX_LOCK(_time_tzlock);
2986
2987 rsp = _time_tzinfo;
2988 if (timeptr->tm_isdst > 0) {
2989@@ -1114,15 +1100,17 @@ size_t __XL(strftime)(char *__restrict s
2990 }
2991 #endif
2992 o_count = SIZE_MAX;
2993- RSP_TZUNLOCK;
2994+/* RSP_TZUNLOCK; */
2995+#ifdef __UCLIBC_HAS_TM_EXTENSIONS__
2996 goto OUTPUT;
2997+#endif
2998 } else { /* z */
2999 *s = '+';
3000 if ((tzo = -RSP_GMT_OFFSET) < 0) {
3001 tzo = -tzo;
3002 *s = '-';
3003 }
3004- RSP_TZUNLOCK;
3005+/* RSP_TZUNLOCK; */
3006 ++s;
3007 --count;
3008
3009@@ -1131,7 +1119,13 @@ size_t __XL(strftime)(char *__restrict s
3010
3011 i = 16 + 6; /* 0-fill, width = 4 */
3012 }
3013-
3014+#ifdef __UCLIBC_HAS_TM_EXTENSIONS__
3015+#else
3016+ __UCLIBC_MUTEX_UNLOCK(_time_tzlock);
3017+ if (*p == 'Z') {
3018+ goto OUTPUT;
3019+ }
3020+#endif
3021 } else {
3022 /* TODO: don't need year for U, W */
3023 for (i=0 ; i < 3 ; i++) {
3024@@ -1664,9 +1658,7 @@ int daylight = 0;
3025 long timezone = 0;
3026 char *tzname[2] = { (char *) UTC, (char *) (UTC-1) };
3027
3028-#ifdef __UCLIBC_HAS_THREADS__
3029-pthread_mutex_t _time_tzlock = PTHREAD_RECURSIVE_MUTEX_INITIALIZER_NP;
3030-#endif
3031+__UCLIBC_MUTEX_INIT(_time_tzlock, PTHREAD_RECURSIVE_MUTEX_INITIALIZER_NP);
3032
3033 rule_struct _time_tzinfo[2];
3034
3035@@ -1796,7 +1788,7 @@ void tzset(void)
3036 static char oldval[TZ_BUFLEN]; /* BSS-zero'd. */
3037 #endif /* __UCLIBC_HAS_TZ_CACHING__ */
3038
3039- TZLOCK;
3040+ __UCLIBC_MUTEX_LOCK(_time_tzlock);
3041
3042 e = getenv(TZ); /* TZ env var always takes precedence. */
3043
3044@@ -1962,10 +1954,10 @@ void tzset(void)
3045 daylight = !!_time_tzinfo[1].tzname[0];
3046 timezone = _time_tzinfo[0].gmt_offset;
3047
3048-#if defined(__UCLIBC_HAS_TZ_FILE__)
3049+#if defined(__UCLIBC_HAS_TZ_FILE__) || defined(__UCLIBC_HAS_TZ_CACHING__)
3050 FAST_DONE:
3051 #endif
3052- TZUNLOCK;
3053+ __UCLIBC_MUTEX_UNLOCK(_time_tzlock);
3054 }
3055
3056 #endif
3057@@ -2167,13 +2159,13 @@ time_t _time_mktime(struct tm *timeptr,
3058 {
3059 time_t t;
3060
3061- TZLOCK;
3062+ __UCLIBC_MUTEX_LOCK(_time_tzlock);
3063
3064 tzset();
3065
3066 t = _time_mktime_tzi(timeptr, store_on_success, _time_tzinfo);
3067
3068- TZUNLOCK;
3069+ __UCLIBC_MUTEX_UNLOCK(_time_tzlock);
3070
3071 return t;
3072 }
3073diff --git a/libc/misc/ttyent/getttyent.c b/libc/misc/ttyent/getttyent.c
3074index 6e2fbd2..c85c73a 100644
3075--- a/libc/misc/ttyent/getttyent.c
3076+++ b/libc/misc/ttyent/getttyent.c
3077@@ -35,9 +35,6 @@
3078 #include <ctype.h>
3079 #include <string.h>
3080 #include <stdlib.h>
3081-#ifdef __UCLIBC_HAS_THREADS__
3082-#include <pthread.h>
3083-#endif
3084
3085 static char zapchar;
3086 static FILE *tf;
3087@@ -50,8 +47,8 @@ struct ttyent * getttynam(const char *tt
3088
3089 setttyent();
3090 while ((t = getttyent()))
3091- if (!strcmp(tty, t->ty_name))
3092- break;
3093+ if (!strcmp(tty, t->ty_name))
3094+ break;
3095 endttyent();
3096 return (t);
3097 }
3098@@ -67,27 +64,27 @@ static char * skip(register char *p)
3099 register int c, q;
3100
3101 for (q = 0, t = p; (c = *p) != '\0'; p++) {
3102- if (c == '"') {
3103- q ^= QUOTED; /* obscure, but nice */
3104- continue;
3105- }
3106- if (q == QUOTED && *p == '\\' && *(p+1) == '"')
3107- p++;
3108- *t++ = *p;
3109- if (q == QUOTED)
3110- continue;
3111- if (c == '#') {
3112- zapchar = c;
3113- *p = 0;
3114- break;
3115- }
3116- if (c == '\t' || c == ' ' || c == '\n') {
3117- zapchar = c;
3118- *p++ = 0;
3119- while ((c = *p) == '\t' || c == ' ' || c == '\n')
3120- p++;
3121- break;
3122- }
3123+ if (c == '"') {
3124+ q ^= QUOTED; /* obscure, but nice */
3125+ continue;
3126+ }
3127+ if (q == QUOTED && *p == '\\' && *(p+1) == '"')
3128+ p++;
3129+ *t++ = *p;
3130+ if (q == QUOTED)
3131+ continue;
3132+ if (c == '#') {
3133+ zapchar = c;
3134+ *p = 0;
3135+ break;
3136+ }
3137+ if (c == '\t' || c == ' ' || c == '\n') {
3138+ zapchar = c;
3139+ *p++ = 0;
3140+ while ((c = *p) == '\t' || c == ' ' || c == '\n')
3141+ p++;
3142+ break;
3143+ }
3144 }
3145 *--t = '\0';
3146 return (p);
3147@@ -104,46 +101,46 @@ struct ttyent * getttyent(void)
3148 register int c;
3149 register char *p;
3150 static char *line = NULL;
3151+ struct ttyent *retval = NULL;
3152
3153 if (!tf && !setttyent())
3154- return (NULL);
3155+ return (NULL);
3156
3157 if (!line) {
3158- line = malloc(BUFSIZ);
3159+ line = malloc(BUFSIZ);
3160 if (!line)
3161 abort();
3162 }
3163
3164- __STDIO_ALWAYS_THREADLOCK(tf);
3165+ __STDIO_ALWAYS_THREADLOCK(tf);
3166
3167 for (;;) {
3168- if (!fgets_unlocked(p = line, BUFSIZ, tf)) {
3169- __STDIO_ALWAYS_THREADUNLOCK(tf);
3170- return (NULL);
3171- }
3172- /* skip lines that are too big */
3173- if (!index(p, '\n')) {
3174- while ((c = getc_unlocked(tf)) != '\n' && c != EOF)
3175- ;
3176- continue;
3177- }
3178- while (isspace(*p))
3179- ++p;
3180- if (*p && *p != '#')
3181- break;
3182+ if (!fgets_unlocked(p = line, BUFSIZ, tf)) {
3183+ goto DONE;
3184+ }
3185+ /* skip lines that are too big */
3186+ if (!index(p, '\n')) {
3187+ while ((c = getc_unlocked(tf)) != '\n' && c != EOF)
3188+ ;
3189+ continue;
3190+ }
3191+ while (isspace(*p))
3192+ ++p;
3193+ if (*p && *p != '#')
3194+ break;
3195 }
3196
3197 zapchar = 0;
3198 tty.ty_name = p;
3199 p = skip(p);
3200 if (!*(tty.ty_getty = p))
3201- tty.ty_getty = tty.ty_type = NULL;
3202+ tty.ty_getty = tty.ty_type = NULL;
3203 else {
3204- p = skip(p);
3205- if (!*(tty.ty_type = p))
3206- tty.ty_type = NULL;
3207- else
3208- p = skip(p);
3209+ p = skip(p);
3210+ if (!*(tty.ty_type = p))
3211+ tty.ty_type = NULL;
3212+ else
3213+ p = skip(p);
3214 }
3215 tty.ty_status = 0;
3216 tty.ty_window = NULL;
3217@@ -151,43 +148,45 @@ struct ttyent * getttyent(void)
3218 #define scmp(e) !strncmp(p, e, sizeof(e) - 1) && isspace(p[sizeof(e) - 1])
3219 #define vcmp(e) !strncmp(p, e, sizeof(e) - 1) && p[sizeof(e) - 1] == '='
3220 for (; *p; p = skip(p)) {
3221- if (scmp(_TTYS_OFF))
3222- tty.ty_status &= ~TTY_ON;
3223- else if (scmp(_TTYS_ON))
3224- tty.ty_status |= TTY_ON;
3225- else if (scmp(_TTYS_SECURE))
3226- tty.ty_status |= TTY_SECURE;
3227- else if (vcmp(_TTYS_WINDOW))
3228- tty.ty_window = value(p);
3229- else
3230- break;
3231+ if (scmp(_TTYS_OFF))
3232+ tty.ty_status &= ~TTY_ON;
3233+ else if (scmp(_TTYS_ON))
3234+ tty.ty_status |= TTY_ON;
3235+ else if (scmp(_TTYS_SECURE))
3236+ tty.ty_status |= TTY_SECURE;
3237+ else if (vcmp(_TTYS_WINDOW))
3238+ tty.ty_window = value(p);
3239+ else
3240+ break;
3241 }
3242- /* We can release the lock only here since `zapchar' is global. */
3243- __STDIO_ALWAYS_THREADUNLOCK(tf);
3244
3245 if (zapchar == '#' || *p == '#')
3246- while ((c = *++p) == ' ' || c == '\t')
3247- ;
3248+ while ((c = *++p) == ' ' || c == '\t')
3249+ ;
3250 tty.ty_comment = p;
3251 if (*p == 0)
3252- tty.ty_comment = 0;
3253+ tty.ty_comment = 0;
3254 if ((p = index(p, '\n')))
3255- *p = '\0';
3256- return (&tty);
3257+ *p = '\0';
3258+ retval = &tty;
3259+
3260+ DONE:
3261+ __STDIO_ALWAYS_THREADUNLOCK(tf);
3262+ return retval;
3263 }
3264
3265 int setttyent(void)
3266 {
3267
3268 if (tf) {
3269- rewind(tf);
3270- return (1);
3271+ rewind(tf);
3272+ return (1);
3273 } else if ((tf = fopen(_PATH_TTYS, "r"))) {
3274- /* We do the locking ourselves. */
3275+ /* We do the locking ourselves. */
3276 #ifdef __UCLIBC_HAS_THREADS__
3277- __fsetlocking (tf, FSETLOCKING_BYCALLER);
3278+ __fsetlocking (tf, FSETLOCKING_BYCALLER);
3279 #endif
3280- return (1);
3281+ return (1);
3282 }
3283 return (0);
3284 }
3285@@ -197,9 +196,9 @@ int endttyent(void)
3286 int rval;
3287
3288 if (tf) {
3289- rval = !(fclose(tf) == EOF);
3290- tf = NULL;
3291- return (rval);
3292+ rval = !(fclose(tf) == EOF);
3293+ tf = NULL;
3294+ return (rval);
3295 }
3296 return (1);
3297 }
3298diff --git a/libc/misc/utmp/utent.c b/libc/misc/utmp/utent.c
3299index c1d8d6f..0fc6df4 100644
3300--- a/libc/misc/utmp/utent.c
3301+++ b/libc/misc/utmp/utent.c
3302@@ -20,19 +20,9 @@
3303 #include <string.h>
3304 #include <utmp.h>
3305
3306+#include <bits/uClibc_mutex.h>
3307
3308-
3309-#ifdef __UCLIBC_HAS_THREADS__
3310-#include <pthread.h>
3311-static pthread_mutex_t utmplock = PTHREAD_MUTEX_INITIALIZER;
3312-# define LOCK __pthread_mutex_lock(&utmplock)
3313-# define UNLOCK __pthread_mutex_unlock(&utmplock)
3314-#else
3315-# define LOCK
3316-# define UNLOCK
3317-#endif
3318-
3319-
3320+__UCLIBC_MUTEX_STATIC(utmplock, PTHREAD_MUTEX_INITIALIZER);
3321
3322 /* Some global crap */
3323 static int static_fd = -1;
3324@@ -46,19 +36,19 @@ static struct utmp *__getutent(int utmp_
3325
3326 {
3327 if (utmp_fd == -1) {
3328- setutent();
3329+ setutent();
3330 }
3331 if (utmp_fd == -1) {
3332- return NULL;
3333+ return NULL;
3334 }
3335
3336- LOCK;
3337+ __UCLIBC_MUTEX_LOCK(utmplock);
3338 if (read(utmp_fd, (char *) &static_utmp, sizeof(struct utmp)) != sizeof(struct utmp))
3339- {
3340- return NULL;
3341- }
3342+ {
3343+ return NULL;
3344+ }
3345
3346- UNLOCK;
3347+ __UCLIBC_MUTEX_UNLOCK(utmplock);
3348 return &static_utmp;
3349 }
3350
3351@@ -66,39 +56,39 @@ void setutent(void)
3352 {
3353 int ret;
3354
3355- LOCK;
3356+ __UCLIBC_MUTEX_LOCK(utmplock);
3357 if (static_fd == -1) {
3358- if ((static_fd = open(static_ut_name, O_RDWR)) < 0) {
3359- if ((static_fd = open(static_ut_name, O_RDONLY)) < 0) {
3360- goto bummer;
3361- }
3362- }
3363- /* Make sure the file will be closed on exec() */
3364- ret = fcntl(static_fd, F_GETFD, 0);
3365- if (ret >= 0) {
3366- ret = fcntl(static_fd, F_GETFD, 0);
3367- }
3368- if (ret < 0) {
3369-bummer:
3370- UNLOCK;
3371- static_fd = -1;
3372- close(static_fd);
3373- return;
3374- }
3375+ if ((static_fd = open(static_ut_name, O_RDWR)) < 0) {
3376+ if ((static_fd = open(static_ut_name, O_RDONLY)) < 0) {
3377+ goto bummer;
3378+ }
3379+ }
3380+ /* Make sure the file will be closed on exec() */
3381+ ret = fcntl(static_fd, F_GETFD, 0);
3382+ if (ret >= 0) {
3383+ ret = fcntl(static_fd, F_GETFD, 0);
3384+ }
3385+ if (ret < 0) {
3386+ bummer:
3387+ close(static_fd);
3388+ static_fd = -1;
3389+ goto DONE;
3390+ }
3391 }
3392 lseek(static_fd, 0, SEEK_SET);
3393- UNLOCK;
3394+ DONE:
3395+ __UCLIBC_MUTEX_UNLOCK(utmplock);
3396 return;
3397 }
3398
3399 void endutent(void)
3400 {
3401- LOCK;
3402+ __UCLIBC_MUTEX_LOCK(utmplock);
3403 if (static_fd != -1) {
3404- close(static_fd);
3405+ close(static_fd);
3406 }
3407 static_fd = -1;
3408- UNLOCK;
3409+ __UCLIBC_MUTEX_UNLOCK(utmplock);
3410 }
3411
3412 /* Locking is done in __getutent */
3413@@ -113,22 +103,22 @@ struct utmp *getutid (const struct utmp
3414 struct utmp *lutmp;
3415
3416 while ((lutmp = __getutent(static_fd)) != NULL) {
3417- if ( (utmp_entry->ut_type == RUN_LVL ||
3418- utmp_entry->ut_type == BOOT_TIME ||
3419- utmp_entry->ut_type == NEW_TIME ||
3420- utmp_entry->ut_type == OLD_TIME) &&
3421- lutmp->ut_type == utmp_entry->ut_type)
3422- {
3423- return lutmp;
3424- }
3425- if ( (utmp_entry->ut_type == INIT_PROCESS ||
3426- utmp_entry->ut_type == DEAD_PROCESS ||
3427- utmp_entry->ut_type == LOGIN_PROCESS ||
3428- utmp_entry->ut_type == USER_PROCESS) &&
3429- !strncmp(lutmp->ut_id, utmp_entry->ut_id, sizeof(lutmp->ut_id)))
3430- {
3431- return lutmp;
3432- }
3433+ if ( (utmp_entry->ut_type == RUN_LVL ||
3434+ utmp_entry->ut_type == BOOT_TIME ||
3435+ utmp_entry->ut_type == NEW_TIME ||
3436+ utmp_entry->ut_type == OLD_TIME) &&
3437+ lutmp->ut_type == utmp_entry->ut_type)
3438+ {
3439+ return lutmp;
3440+ }
3441+ if ( (utmp_entry->ut_type == INIT_PROCESS ||
3442+ utmp_entry->ut_type == DEAD_PROCESS ||
3443+ utmp_entry->ut_type == LOGIN_PROCESS ||
3444+ utmp_entry->ut_type == USER_PROCESS) &&
3445+ !strncmp(lutmp->ut_id, utmp_entry->ut_id, sizeof(lutmp->ut_id)))
3446+ {
3447+ return lutmp;
3448+ }
3449 }
3450
3451 return NULL;
3452@@ -140,11 +130,11 @@ struct utmp *getutline(const struct utmp
3453 struct utmp *lutmp;
3454
3455 while ((lutmp = __getutent(static_fd)) != NULL) {
3456- if ((lutmp->ut_type == USER_PROCESS || lutmp->ut_type == LOGIN_PROCESS) &&
3457- !strcmp(lutmp->ut_line, utmp_entry->ut_line))
3458- {
3459- return lutmp;
3460- }
3461+ if ((lutmp->ut_type == USER_PROCESS || lutmp->ut_type == LOGIN_PROCESS) &&
3462+ !strcmp(lutmp->ut_line, utmp_entry->ut_line))
3463+ {
3464+ return lutmp;
3465+ }
3466 }
3467
3468 return NULL;
3469@@ -152,42 +142,42 @@ struct utmp *getutline(const struct utmp
3470
3471 struct utmp *pututline (const struct utmp *utmp_entry)
3472 {
3473- LOCK;
3474+ __UCLIBC_MUTEX_LOCK(utmplock);
3475 /* Ignore the return value. That way, if they've already positioned
3476 the file pointer where they want it, everything will work out. */
3477 lseek(static_fd, (off_t) - sizeof(struct utmp), SEEK_CUR);
3478
3479 if (getutid(utmp_entry) != NULL) {
3480- lseek(static_fd, (off_t) - sizeof(struct utmp), SEEK_CUR);
3481- if (write(static_fd, utmp_entry, sizeof(struct utmp)) != sizeof(struct utmp))
3482- return NULL;
3483+ lseek(static_fd, (off_t) - sizeof(struct utmp), SEEK_CUR);
3484+ if (write(static_fd, utmp_entry, sizeof(struct utmp)) != sizeof(struct utmp))
3485+ return NULL;
3486 } else {
3487- lseek(static_fd, (off_t) 0, SEEK_END);
3488- if (write(static_fd, utmp_entry, sizeof(struct utmp)) != sizeof(struct utmp))
3489- return NULL;
3490+ lseek(static_fd, (off_t) 0, SEEK_END);
3491+ if (write(static_fd, utmp_entry, sizeof(struct utmp)) != sizeof(struct utmp))
3492+ return NULL;
3493 }
3494
3495- UNLOCK;
3496+ __UCLIBC_MUTEX_UNLOCK(utmplock);
3497 return (struct utmp *)utmp_entry;
3498 }
3499
3500 int utmpname (const char *new_ut_name)
3501 {
3502- LOCK;
3503+ __UCLIBC_MUTEX_LOCK(utmplock);
3504 if (new_ut_name != NULL) {
3505- if (static_ut_name != default_file_name)
3506- free((char *)static_ut_name);
3507- static_ut_name = strdup(new_ut_name);
3508- if (static_ut_name == NULL) {
3509- /* We should probably whine about out-of-memory
3510- * errors here... Instead just reset to the default */
3511- static_ut_name = default_file_name;
3512- }
3513+ if (static_ut_name != default_file_name)
3514+ free((char *)static_ut_name);
3515+ static_ut_name = strdup(new_ut_name);
3516+ if (static_ut_name == NULL) {
3517+ /* We should probably whine about out-of-memory
3518+ * errors here... Instead just reset to the default */
3519+ static_ut_name = default_file_name;
3520+ }
3521 }
3522
3523 if (static_fd != -1)
3524- close(static_fd);
3525- UNLOCK;
3526+ close(static_fd);
3527+ __UCLIBC_MUTEX_UNLOCK(utmplock);
3528 return 0;
3529 }
3530
3531diff --git a/libc/misc/wchar/wstdio.c b/libc/misc/wchar/wstdio.c
3532index b49494f..408c57a 100644
3533--- a/libc/misc/wchar/wstdio.c
3534+++ b/libc/misc/wchar/wstdio.c
3535@@ -82,9 +82,6 @@ strong_alias(NAME,NAME##_unlocked) \
3536 void NAME PARAMS
3537 #endif
3538
3539-#define __STDIO_THREADLOCK_OPENLIST
3540-#define __STDIO_THREADUNLOCK_OPENLIST
3541-
3542 #else /* __UCLIBC_HAS_THREADS__ */
3543
3544 #include <pthread.h>
3545@@ -112,15 +109,6 @@ void NAME PARAMS \
3546 } \
3547 void NAME##_unlocked PARAMS
3548
3549-#define __STDIO_THREADLOCK_OPENLIST \
3550- __pthread_mutex_lock(&_stdio_openlist_lock)
3551-
3552-#define __STDIO_THREADUNLOCK_OPENLIST \
3553- __pthread_mutex_unlock(&_stdio_openlist_lock)
3554-
3555-#define __STDIO_THREADTRYLOCK_OPENLIST \
3556- __pthread_mutex_trylock(&_stdio_openlist_lock)
3557-
3558 #endif /* __UCLIBC_HAS_THREADS__ */
3559
3560 #ifndef __STDIO_BUFFERS
3561diff --git a/libc/pwd_grp/lckpwdf.c b/libc/pwd_grp/lckpwdf.c
3562index 6b9c251..063fed4 100644
3563--- a/libc/pwd_grp/lckpwdf.c
3564+++ b/libc/pwd_grp/lckpwdf.c
3565@@ -27,15 +27,9 @@
3566 #include <sys/file.h>
3567 #include <paths.h>
3568
3569-#ifdef __UCLIBC_HAS_THREADS__
3570-#include <pthread.h>
3571-static pthread_mutex_t mylock = PTHREAD_MUTEX_INITIALIZER;
3572-# define LOCK __pthread_mutex_lock(&mylock)
3573-# define UNLOCK __pthread_mutex_unlock(&mylock);
3574-#else
3575-# define LOCK
3576-# define UNLOCK
3577-#endif
3578+#include <bits/uClibc_mutex.h>
3579+
3580+__UCLIBC_MUTEX_STATIC(mylock, PTHREAD_MUTEX_INITIALIZER);
3581
3582 /* How long to wait for getting the lock before returning with an
3583 error. */
3584@@ -57,18 +51,18 @@ int lckpwdf (void)
3585 struct sigaction new_act; /* New signal action. */
3586 struct flock fl; /* Information struct for locking. */
3587 int result;
3588+ int rv = -1;
3589
3590 if (lock_fd != -1)
3591 /* Still locked by own process. */
3592 return -1;
3593
3594- LOCK;
3595+ __UCLIBC_MUTEX_LOCK(mylock);
3596
3597 lock_fd = open (_PATH_PASSWD, O_WRONLY);
3598 if (lock_fd == -1) {
3599 /* Cannot create lock file. */
3600- UNLOCK;
3601- return -1;
3602+ goto DONE;
3603 }
3604
3605 /* Make sure file gets correctly closed when process finished. */
3606@@ -77,16 +71,14 @@ int lckpwdf (void)
3607 /* Cannot get file flags. */
3608 close(lock_fd);
3609 lock_fd = -1;
3610- UNLOCK;
3611- return -1;
3612+ goto DONE;
3613 }
3614 flags |= FD_CLOEXEC; /* Close on exit. */
3615 if (fcntl (lock_fd, F_SETFD, flags) < 0) {
3616 /* Cannot set new flags. */
3617 close(lock_fd);
3618 lock_fd = -1;
3619- UNLOCK;
3620- return -1;
3621+ goto DONE;
3622 }
3623
3624 /* Now we have to get exclusive write access. Since multiple
3625@@ -107,8 +99,7 @@ int lckpwdf (void)
3626 /* Cannot install signal handler. */
3627 close(lock_fd);
3628 lock_fd = -1;
3629- UNLOCK;
3630- return -1;
3631+ goto DONE;
3632 }
3633
3634 /* Now make sure the alarm signal is not blocked. */
3635@@ -118,8 +109,7 @@ int lckpwdf (void)
3636 sigaction (SIGALRM, &saved_act, NULL);
3637 close(lock_fd);
3638 lock_fd = -1;
3639- UNLOCK;
3640- return -1;
3641+ goto DONE;
3642 }
3643
3644 /* Start timer. If we cannot get the lock in the specified time we
3645@@ -146,12 +136,14 @@ int lckpwdf (void)
3646 if (result < 0) {
3647 close(lock_fd);
3648 lock_fd = -1;
3649- UNLOCK;
3650- return -1;
3651+ goto DONE;
3652 }
3653
3654- UNLOCK;
3655- return 0;
3656+ rv = 0;
3657+
3658+ DONE:
3659+ __UCLIBC_MUTEX_UNLOCK(mylock);
3660+ return rv;
3661 }
3662
3663
3664@@ -164,11 +156,11 @@ int ulckpwdf (void)
3665 result = -1;
3666 }
3667 else {
3668- LOCK;
3669+ __UCLIBC_MUTEX_LOCK(mylock);
3670 result = close (lock_fd);
3671 /* Mark descriptor as unused. */
3672 lock_fd = -1;
3673- UNLOCK;
3674+ __UCLIBC_MUTEX_UNLOCK(mylock);
3675 }
3676
3677 return result;
3678diff --git a/libc/pwd_grp/pwd_grp.c b/libc/pwd_grp/pwd_grp.c
3679index 91c0d83..a302c7c 100644
3680--- a/libc/pwd_grp/pwd_grp.c
3681+++ b/libc/pwd_grp/pwd_grp.c
3682@@ -42,9 +42,8 @@
3683 #include <pwd.h>
3684 #include <grp.h>
3685 #include <shadow.h>
3686-#ifdef __UCLIBC_HAS_THREADS__
3687-#include <pthread.h>
3688-#endif
3689+
3690+#include <bits/uClibc_mutex.h>
3691
3692 /**********************************************************************/
3693 /* Sizes for staticly allocated buffers. */
3694@@ -445,34 +444,27 @@ int getpw(uid_t uid, char *buf)
3695 /**********************************************************************/
3696 #ifdef L_getpwent_r
3697
3698-#ifdef __UCLIBC_HAS_THREADS__
3699-static pthread_mutex_t mylock = PTHREAD_MUTEX_INITIALIZER;
3700-# define LOCK __pthread_mutex_lock(&mylock)
3701-# define UNLOCK __pthread_mutex_unlock(&mylock);
3702-#else
3703-# define LOCK ((void) 0)
3704-# define UNLOCK ((void) 0)
3705-#endif
3706+__UCLIBC_MUTEX_STATIC(mylock, PTHREAD_MUTEX_INITIALIZER);
3707
3708 static FILE *pwf /*= NULL*/;
3709
3710 void setpwent(void)
3711 {
3712- LOCK;
3713+ __UCLIBC_MUTEX_LOCK(mylock);
3714 if (pwf) {
3715 rewind(pwf);
3716 }
3717- UNLOCK;
3718+ __UCLIBC_MUTEX_UNLOCK(mylock);
3719 }
3720
3721 void endpwent(void)
3722 {
3723- LOCK;
3724+ __UCLIBC_MUTEX_LOCK(mylock);
3725 if (pwf) {
3726 fclose(pwf);
3727 pwf = NULL;
3728 }
3729- UNLOCK;
3730+ __UCLIBC_MUTEX_UNLOCK(mylock);
3731 }
3732
3733
3734@@ -482,7 +474,7 @@ int getpwent_r(struct passwd *__restrict
3735 {
3736 int rv;
3737
3738- LOCK;
3739+ __UCLIBC_MUTEX_LOCK(mylock);
3740
3741 *result = NULL; /* In case of error... */
3742
3743@@ -500,7 +492,7 @@ int getpwent_r(struct passwd *__restrict
3744 }
3745
3746 ERR:
3747- UNLOCK;
3748+ __UCLIBC_MUTEX_UNLOCK(mylock);
3749
3750 return rv;
3751 }
3752@@ -509,34 +501,27 @@ int getpwent_r(struct passwd *__restrict
3753 /**********************************************************************/
3754 #ifdef L_getgrent_r
3755
3756-#ifdef __UCLIBC_HAS_THREADS__
3757-static pthread_mutex_t mylock = PTHREAD_MUTEX_INITIALIZER;
3758-# define LOCK __pthread_mutex_lock(&mylock)
3759-# define UNLOCK __pthread_mutex_unlock(&mylock);
3760-#else
3761-# define LOCK ((void) 0)
3762-# define UNLOCK ((void) 0)
3763-#endif
3764+__UCLIBC_MUTEX_STATIC(mylock, PTHREAD_MUTEX_INITIALIZER);
3765
3766 static FILE *grf /*= NULL*/;
3767
3768 void setgrent(void)
3769 {
3770- LOCK;
3771+ __UCLIBC_MUTEX_LOCK(mylock);
3772 if (grf) {
3773 rewind(grf);
3774 }
3775- UNLOCK;
3776+ __UCLIBC_MUTEX_UNLOCK(mylock);
3777 }
3778
3779 void endgrent(void)
3780 {
3781- LOCK;
3782+ __UCLIBC_MUTEX_LOCK(mylock);
3783 if (grf) {
3784 fclose(grf);
3785 grf = NULL;
3786 }
3787- UNLOCK;
3788+ __UCLIBC_MUTEX_UNLOCK(mylock);
3789 }
3790
3791 int getgrent_r(struct group *__restrict resultbuf,
3792@@ -545,7 +530,7 @@ int getgrent_r(struct group *__restrict
3793 {
3794 int rv;
3795
3796- LOCK;
3797+ __UCLIBC_MUTEX_LOCK(mylock);
3798
3799 *result = NULL; /* In case of error... */
3800
3801@@ -563,7 +548,7 @@ int getgrent_r(struct group *__restrict
3802 }
3803
3804 ERR:
3805- UNLOCK;
3806+ __UCLIBC_MUTEX_UNLOCK(mylock);
3807
3808 return rv;
3809 }
3810@@ -572,34 +557,27 @@ int getgrent_r(struct group *__restrict
3811 /**********************************************************************/
3812 #ifdef L_getspent_r
3813
3814-#ifdef __UCLIBC_HAS_THREADS__
3815-static pthread_mutex_t mylock = PTHREAD_MUTEX_INITIALIZER;
3816-# define LOCK __pthread_mutex_lock(&mylock)
3817-# define UNLOCK __pthread_mutex_unlock(&mylock);
3818-#else
3819-# define LOCK ((void) 0)
3820-# define UNLOCK ((void) 0)
3821-#endif
3822+__UCLIBC_MUTEX_STATIC(mylock, PTHREAD_MUTEX_INITIALIZER);
3823
3824 static FILE *spf /*= NULL*/;
3825
3826 void setspent(void)
3827 {
3828- LOCK;
3829+ __UCLIBC_MUTEX_LOCK(mylock);
3830 if (spf) {
3831 rewind(spf);
3832 }
3833- UNLOCK;
3834+ __UCLIBC_MUTEX_UNLOCK(mylock);
3835 }
3836
3837 void endspent(void)
3838 {
3839- LOCK;
3840+ __UCLIBC_MUTEX_LOCK(mylock);
3841 if (spf) {
3842 fclose(spf);
3843 spf = NULL;
3844 }
3845- UNLOCK;
3846+ __UCLIBC_MUTEX_UNLOCK(mylock);
3847 }
3848
3849 int getspent_r(struct spwd *resultbuf, char *buffer,
3850@@ -607,7 +585,7 @@ int getspent_r(struct spwd *resultbuf, c
3851 {
3852 int rv;
3853
3854- LOCK;
3855+ __UCLIBC_MUTEX_LOCK(mylock);
3856
3857 *result = NULL; /* In case of error... */
3858
3859@@ -625,7 +603,7 @@ int getspent_r(struct spwd *resultbuf, c
3860 }
3861
3862 ERR:
3863- UNLOCK;
3864+ __UCLIBC_MUTEX_UNLOCK(mylock);
3865
3866 return rv;
3867 }
3868diff --git a/libc/stdio/_READ.c b/libc/stdio/_READ.c
3869index 7d3c38c..fe1bc91 100644
3870--- a/libc/stdio/_READ.c
3871+++ b/libc/stdio/_READ.c
3872@@ -41,7 +41,7 @@ size_t __stdio_READ(register FILE *strea
3873 #warning EINTR?
3874 #endif
3875 /* RETRY: */
3876- if ((rv = __READ(stream, buf, bufsize)) <= 0) {
3877+ if ((rv = __READ(stream, (char *) buf, bufsize)) <= 0) {
3878 if (rv == 0) {
3879 __STDIO_STREAM_SET_EOF(stream);
3880 } else {
3881diff --git a/libc/stdio/_WRITE.c b/libc/stdio/_WRITE.c
3882index d300d39..4131eb7 100644
3883--- a/libc/stdio/_WRITE.c
3884+++ b/libc/stdio/_WRITE.c
3885@@ -47,7 +47,7 @@ size_t __stdio_WRITE(register FILE *stre
3886 return bufsize;
3887 }
3888 stodo = (todo <= SSIZE_MAX) ? todo : SSIZE_MAX;
3889- if ((rv = __WRITE(stream, buf, stodo)) >= 0) {
3890+ if ((rv = __WRITE(stream, (char *) buf, stodo)) >= 0) {
3891 #ifdef __UCLIBC_MJN3_ONLY__
3892 #warning TODO: Make custom stream write return check optional.
3893 #endif
3894diff --git a/libc/stdio/_fopen.c b/libc/stdio/_fopen.c
3895index f7f5bb6..4984f11 100644
3896--- a/libc/stdio/_fopen.c
3897+++ b/libc/stdio/_fopen.c
3898@@ -194,10 +194,23 @@ FILE *_stdio_fopen(intptr_t fname_or_mod
3899 #endif
3900
3901 #ifdef __STDIO_HAS_OPENLIST
3902- __STDIO_THREADLOCK_OPENLIST;
3903- stream->__nextopen = _stdio_openlist; /* New files are inserted at */
3904- _stdio_openlist = stream; /* the head of the list. */
3905- __STDIO_THREADUNLOCK_OPENLIST;
3906+#if defined(__UCLIBC_HAS_THREADS__) && defined(__STDIO_BUFFERS)
3907+ if (!(stream->__modeflags & __FLAG_FREEFILE))
3908+ {
3909+ /* An freopen call so the file was never removed from the list. */
3910+ }
3911+ else
3912+#endif
3913+ {
3914+ /* We have to lock the del mutex in case another thread wants to fclose()
3915+ * the last file. */
3916+ __STDIO_THREADLOCK_OPENLIST_DEL;
3917+ __STDIO_THREADLOCK_OPENLIST_ADD;
3918+ stream->__nextopen = _stdio_openlist; /* New files are inserted at */
3919+ _stdio_openlist = stream; /* the head of the list. */
3920+ __STDIO_THREADUNLOCK_OPENLIST_ADD;
3921+ __STDIO_THREADUNLOCK_OPENLIST_DEL;
3922+ }
3923 #endif
3924
3925 __STDIO_STREAM_VALIDATE(stream);
3926diff --git a/libc/stdio/_stdio.c b/libc/stdio/_stdio.c
3927index 4aae3c4..9cfe02c 100644
3928--- a/libc/stdio/_stdio.c
3929+++ b/libc/stdio/_stdio.c
3930@@ -151,8 +151,12 @@ FILE *__stdout = _stdio_streams + 1; /*
3931 FILE *_stdio_openlist = _stdio_streams;
3932
3933 # ifdef __UCLIBC_HAS_THREADS__
3934-pthread_mutex_t _stdio_openlist_lock = PTHREAD_RECURSIVE_MUTEX_INITIALIZER_NP;
3935-int _stdio_openlist_delflag = 0;
3936+__UCLIBC_MUTEX_INIT(_stdio_openlist_add_lock, PTHREAD_RECURSIVE_MUTEX_INITIALIZER_NP);
3937+#ifdef __STDIO_BUFFERS
3938+__UCLIBC_MUTEX_INIT(_stdio_openlist_del_lock, PTHREAD_RECURSIVE_MUTEX_INITIALIZER_NP);
3939+volatile int _stdio_openlist_use_count = 0;
3940+int _stdio_openlist_del_count = 0;
3941+#endif
3942 # endif
3943
3944 #endif
3945@@ -162,10 +166,10 @@ int _stdio_openlist_delflag = 0;
3946 /* 2 if threading not initialized and 0 otherwise; */
3947 int _stdio_user_locking = 2;
3948
3949-void __stdio_init_mutex(pthread_mutex_t *m)
3950+void __stdio_init_mutex(__UCLIBC_MUTEX_TYPE *m)
3951 {
3952- static const pthread_mutex_t __stdio_mutex_initializer
3953- = PTHREAD_RECURSIVE_MUTEX_INITIALIZER_NP;
3954+ const __UCLIBC_MUTEX_STATIC(__stdio_mutex_initializer,
3955+ PTHREAD_RECURSIVE_MUTEX_INITIALIZER_NP);
3956
3957 memcpy(m, &__stdio_mutex_initializer, sizeof(__stdio_mutex_initializer));
3958 }
3959@@ -184,7 +188,11 @@ void _stdio_term(void)
3960 * locked, then I suppose there is a chance that a pointer in the
3961 * chain might be corrupt due to a partial store.
3962 */
3963- __stdio_init_mutex(&_stdio_openlist_lock);
3964+ __stdio_init_mutex(&_stdio_openlist_add_lock);
3965+#warning check
3966+#ifdef __STDIO_BUFFERS
3967+ __stdio_init_mutex(&_stdio_openlist_del_lock);
3968+#endif
3969
3970 /* Next we need to worry about the streams themselves. If a stream
3971 * is currently locked, then it may be in an invalid state. So we
3972@@ -192,7 +200,7 @@ void _stdio_term(void)
3973 * Then we reinitialize the locks.
3974 */
3975 for (ptr = _stdio_openlist ; ptr ; ptr = ptr->__nextopen ) {
3976- if (__STDIO_ALWAYS_THREADTRYLOCK(ptr)) {
3977+ if (__STDIO_ALWAYS_THREADTRYLOCK_CANCEL_UNSAFE(ptr)) {
3978 /* The stream is already locked, so we don't want to touch it.
3979 * However, if we have custom streams, we can't just close it
3980 * or leave it locked since a custom stream may be stacked
3981@@ -258,10 +266,6 @@ void _stdio_init(void)
3982 #error Assumption violated about __MASK_READING and __FLAG_UNGOT
3983 #endif
3984
3985-#ifdef __UCLIBC_HAS