This is a live mirror of the Perl 5 development currently hosted at https://github.com/perl/perl5
Integrate:
[perl5.git] / reentr.pl
1 #!/usr/bin/perl -w
2
3 #
4 # Generate the reentr.c and reentr.h,
5 # and optionally also the relevant metaconfig units (-U option).
6
7
8 BEGIN {
9     # Get function prototypes
10     require 'regen_lib.pl';
11 }
12
13 use strict;
14 use Getopt::Std;
15 my %opts;
16 getopts('U', \%opts);
17
18 my %map = (
19            V => "void",
20            A => "char*",        # as an input argument
21            B => "char*",        # as an output argument 
22            C => "const char*",  # as a read-only input argument
23            I => "int",
24            L => "long",
25            W => "size_t",
26            H => "FILE**",
27            E => "int*",
28           );
29
30 # (See the definitions after __DATA__.)
31 # In func|inc|type|... a "S" means "type*", and a "R" means "type**".
32 # (The "types" are often structs, such as "struct passwd".)
33 #
34 # After the prototypes one can have |X=...|Y=... to define more types.
35 # A commonly used extra type is to define D to be equal to "type_data",
36 # for example "struct_hostent_data to" go with "struct hostent".
37 #
38 # Example #1: I_XSBWR means int  func_r(X, type, char*, size_t, type**)
39 # Example #2: S_SBIE  means type func_r(type, char*, int, int*)
40 # Example #3: S_CBI   means type func_r(const char*, char*, int)
41
42
43 safer_unlink 'reentr.h';
44 die "reentr.h: $!" unless open(H, ">reentr.h");
45 binmode H;
46 select H;
47 print <<EOF;
48 /* -*- buffer-read-only: t -*-
49  *
50  *    reentr.h
51  *
52  *    Copyright (C) 2002, 2003, 2005, 2006 by Larry Wall and others
53  *
54  *    You may distribute under the terms of either the GNU General Public
55  *    License or the Artistic License, as specified in the README file.
56  *
57  *  !!!!!!!   DO NOT EDIT THIS FILE   !!!!!!!
58  *  This file is built by reentr.pl from data in reentr.pl.
59  */
60
61 #ifndef REENTR_H
62 #define REENTR_H
63
64 /* If compiling for a threaded perl, we will macro-wrap the system/library
65  * interfaces (e.g. getpwent()) which have threaded versions
66  * (e.g. getpwent_r()), which will handle things correctly for
67  * the Perl interpreter, but otherwise (for XS) the wrapping does
68  * not take place.  See L<perlxs/Thread-aware system interfaces>.
69  */
70
71 #ifndef PERL_REENTR_API
72 # if defined(PERL_CORE) || defined(PERL_EXT)
73 #  define PERL_REENTR_API 1
74 # else
75 #  define PERL_REENTR_API 0
76 # endif
77 #endif
78
79 #ifdef USE_REENTRANT_API
80
81 #ifdef PERL_CORE
82 #   define PL_REENTRANT_RETINT PL_reentrant_retint
83 #endif
84
85 /* Deprecations: some platforms have the said reentrant interfaces
86  * but they are declared obsolete and are not to be used.  Often this
87  * means that the platform has threadsafed the interfaces (hopefully).
88  * All this is OS version dependent, so we are of course fooling ourselves.
89  * If you know of more deprecations on some platforms, please add your own
90  * (by editing reentr.pl, mind!) */
91
92 #ifdef __hpux
93 #   undef HAS_CRYPT_R
94 #   undef HAS_DRAND48_R
95 #   undef HAS_ENDGRENT_R
96 #   undef HAS_ENDPWENT_R
97 #   undef HAS_GETGRENT_R
98 #   undef HAS_GETPWENT_R
99 #   undef HAS_SETLOCALE_R
100 #   undef HAS_SRAND48_R
101 #   undef HAS_STRERROR_R
102 #   define NETDB_R_OBSOLETE
103 #endif
104
105 #if defined(__osf__) && defined(__alpha) /* Tru64 aka Digital UNIX */
106 #   undef HAS_CRYPT_R
107 #   undef HAS_STRERROR_R
108 #   define NETDB_R_OBSOLETE
109 #endif
110
111 #ifdef NETDB_R_OBSOLETE
112 #   undef HAS_ENDHOSTENT_R
113 #   undef HAS_ENDNETENT_R
114 #   undef HAS_ENDPROTOENT_R
115 #   undef HAS_ENDSERVENT_R
116 #   undef HAS_GETHOSTBYADDR_R
117 #   undef HAS_GETHOSTBYNAME_R
118 #   undef HAS_GETHOSTENT_R
119 #   undef HAS_GETNETBYADDR_R
120 #   undef HAS_GETNETBYNAME_R
121 #   undef HAS_GETNETENT_R
122 #   undef HAS_GETPROTOBYNAME_R
123 #   undef HAS_GETPROTOBYNUMBER_R
124 #   undef HAS_GETPROTOENT_R
125 #   undef HAS_GETSERVBYNAME_R
126 #   undef HAS_GETSERVBYPORT_R
127 #   undef HAS_GETSERVENT_R
128 #   undef HAS_SETHOSTENT_R
129 #   undef HAS_SETNETENT_R
130 #   undef HAS_SETPROTOENT_R
131 #   undef HAS_SETSERVENT_R
132 #endif
133
134 #ifdef I_PWD
135 #   include <pwd.h>
136 #endif
137 #ifdef I_GRP
138 #   include <grp.h>
139 #endif
140 #ifdef I_NETDB
141 #   include <netdb.h>
142 #endif
143 #ifdef I_STDLIB
144 #   include <stdlib.h>  /* drand48_data */
145 #endif
146 #ifdef I_CRYPT
147 #   ifdef I_CRYPT
148 #       include <crypt.h>
149 #   endif
150 #endif
151 #ifdef HAS_GETSPNAM_R
152 #   ifdef I_SHADOW
153 #       include <shadow.h>
154 #   endif
155 #endif
156
157 EOF
158
159 my %seenh; # the different prototypes signatures for this function
160 my %seena; # the different prototypes signatures for this function in order
161 my @seenf; # all the seen functions
162 my %seenp; # the different prototype signatures for all functions
163 my %seent; # the return type of this function
164 my %seens; # the type of this function's "S"
165 my %seend; # the type of this function's "D"
166 my %seenm; # all the types
167 my %seenu; # the length of the argument list of this function
168 my %seenr; # the return type of this function
169
170 while (<DATA>) { # Read in the protypes.
171     next if /^\s+$/;
172     chomp;
173     my ($func, $hdr, $type, @p) = split(/\s*\|\s*/, $_, -1);
174     my ($r,$u);
175     # Split off the real function name and the argument list.
176     ($func, $u) = split(' ', $func);
177     $u = "V_V" unless $u;
178     ($r, $u) = ($u =~ /^(.)_(.+)/);
179     $seenu{$func} = $u eq 'V' ? 0 : length $u;
180     $seenr{$func} = $r;
181     my $FUNC = uc $func; # for output.
182     push @seenf, $func;
183     my %m = %map;
184     if ($type) {
185         $m{S} = "$type*";
186         $m{R} = "$type**";
187     }
188
189     # Set any special mapping variables (like X=x_t)
190     if (@p) {
191         while ($p[-1] =~ /=/) {
192             my ($k, $v) = ($p[-1] =~ /^([A-Za-z])\s*=\s*(.*)/);
193             $m{$k} = $v;
194             pop @p;
195         }
196     }
197
198     # If given the -U option open up the metaconfig unit for this function.
199     if ($opts{U} && open(U, ">d_${func}_r.U"))  {
200         binmode U;
201         select U;
202     }
203
204     if ($opts{U}) {
205         # The metaconfig units needs prerequisite dependencies.
206         my $prereqs  = '';
207         my $prereqh  = '';
208         my $prereqsh = '';
209         if ($hdr ne 'stdio') { # There's no i_stdio.
210             $prereqs  = "i_$hdr";
211             $prereqh  = "$hdr.h";
212             $prereqsh = "\$$prereqs $prereqh";
213         }
214         my @prereq = qw(Inlibc Protochk Hasproto i_systypes usethreads);
215         push @prereq, $prereqs;
216         my $hdrs = "\$i_systypes sys/types.h define stdio.h $prereqsh";
217         if ($hdr eq 'time') {
218             $hdrs .= " \$i_systime sys/time.h";
219             push @prereq, 'i_systime';
220         }
221         # Output the metaconfig unit header.
222         print <<EOF;
223 ?RCS: \$Id: d_${func}_r.U,v $
224 ?RCS:
225 ?RCS: Copyright (c) 2002,2003 Jarkko Hietaniemi
226 ?RCS:
227 ?RCS: You may distribute under the terms of either the GNU General Public
228 ?RCS: License or the Artistic License, as specified in the README file.
229 ?RCS:
230 ?RCS: Generated by the reentr.pl from the Perl 5.8 distribution.
231 ?RCS:
232 ?MAKE:d_${func}_r ${func}_r_proto: @prereq
233 ?MAKE:  -pick add \$@ %<
234 ?S:d_${func}_r:
235 ?S:     This variable conditionally defines the HAS_${FUNC}_R symbol,
236 ?S:     which indicates to the C program that the ${func}_r()
237 ?S:     routine is available.
238 ?S:.
239 ?S:${func}_r_proto:
240 ?S:     This variable encodes the prototype of ${func}_r.
241 ?S:     It is zero if d_${func}_r is undef, and one of the
242 ?S:     REENTRANT_PROTO_T_ABC macros of reentr.h if d_${func}_r
243 ?S:     is defined.
244 ?S:.
245 ?C:HAS_${FUNC}_R:
246 ?C:     This symbol, if defined, indicates that the ${func}_r routine
247 ?C:     is available to ${func} re-entrantly.
248 ?C:.
249 ?C:${FUNC}_R_PROTO:
250 ?C:     This symbol encodes the prototype of ${func}_r.
251 ?C:     It is zero if d_${func}_r is undef, and one of the
252 ?C:     REENTRANT_PROTO_T_ABC macros of reentr.h if d_${func}_r
253 ?C:     is defined.
254 ?C:.
255 ?H:#\$d_${func}_r HAS_${FUNC}_R    /**/
256 ?H:#define ${FUNC}_R_PROTO \$${func}_r_proto       /**/
257 ?H:.
258 ?T:try hdrs d_${func}_r_proto
259 ?LINT:set d_${func}_r
260 ?LINT:set ${func}_r_proto
261 : see if ${func}_r exists
262 set ${func}_r d_${func}_r
263 eval \$inlibc
264 case "\$d_${func}_r" in
265 "\$define")
266 EOF
267         print <<EOF;
268         hdrs="$hdrs"
269         case "\$d_${func}_r_proto:\$usethreads" in
270         ":define")      d_${func}_r_proto=define
271                 set d_${func}_r_proto ${func}_r \$hdrs
272                 eval \$hasproto ;;
273         *)      ;;
274         esac
275         case "\$d_${func}_r_proto" in
276         define)
277 EOF
278     }
279     for my $p (@p) {
280         my ($r, $a) = ($p =~ /^(.)_(.+)/);
281         my $v = join(", ", map { $m{$_} } split '', $a);
282         if ($opts{U}) {
283             print <<EOF ;
284         case "\$${func}_r_proto" in
285         ''|0) try='$m{$r} ${func}_r($v);'
286         ./protochk "extern \$try" \$hdrs && ${func}_r_proto=$p ;;
287         esac
288 EOF
289         }
290         $seenh{$func}->{$p}++;
291         push @{$seena{$func}}, $p;
292         $seenp{$p}++;
293         $seent{$func} = $type;
294         $seens{$func} = $m{S};
295         $seend{$func} = $m{D};
296         $seenm{$func} = \%m;
297     }
298     if ($opts{U}) {
299         print <<EOF;
300         case "\$${func}_r_proto" in
301         ''|0)   d_${func}_r=undef
302                 ${func}_r_proto=0
303                 echo "Disabling ${func}_r, cannot determine prototype." >&4 ;;
304         * )     case "\$${func}_r_proto" in
305                 REENTRANT_PROTO*) ;;
306                 *) ${func}_r_proto="REENTRANT_PROTO_\$${func}_r_proto" ;;
307                 esac
308                 echo "Prototype: \$try" ;;
309         esac
310         ;;
311         *)      case "\$usethreads" in
312                 define) echo "${func}_r has no prototype, not using it." >&4 ;;
313                 esac
314                 d_${func}_r=undef
315                 ${func}_r_proto=0
316                 ;;
317         esac
318         ;;
319 *)      ${func}_r_proto=0
320         ;;
321 esac
322
323 EOF
324         close(U);                   
325     }
326 }
327
328 close DATA;
329
330 # Prepare to continue writing the reentr.h.
331
332 select H;
333
334 {
335     # Write out all the known prototype signatures.
336     my $i = 1;
337     for my $p (sort keys %seenp) {
338         print "#define REENTRANT_PROTO_${p}     ${i}\n";
339         $i++;
340     }
341 }
342
343 my @struct; # REENTR struct members
344 my @size;   # struct member buffer size initialization code
345 my @init;   # struct member buffer initialization (malloc) code
346 my @free;   # struct member buffer release (free) code
347 my @wrap;   # the wrapper (foo(a) -> foo_r(a,...)) cpp code
348 my @define; # defines for optional features
349
350 sub ifprotomatch {
351     my $FUNC = shift;
352     join " || ", map { "${FUNC}_R_PROTO == REENTRANT_PROTO_$_" } @_;
353 }
354
355 sub pushssif {
356     push @struct, @_;
357     push @size, @_;
358     push @init, @_;
359     push @free, @_;
360 }
361
362 sub pushinitfree {
363     my $func = shift;
364     push @init, <<EOF;
365         Newx(PL_reentrant_buffer->_${func}_buffer, PL_reentrant_buffer->_${func}_size, char);
366 EOF
367     push @free, <<EOF;
368         Safefree(PL_reentrant_buffer->_${func}_buffer);
369 EOF
370 }
371
372 sub define {
373     my ($n, $p, @F) = @_;
374     my @H;
375     my $H = uc $F[0];
376     push @define, <<EOF;
377 /* The @F using \L$n? */
378
379 EOF
380     my $GENFUNC;
381     for my $func (@F) {
382         my $FUNC = uc $func;
383         my $HAS = "${FUNC}_R_HAS_$n";
384         push @H, $HAS;
385         my @h = grep { /$p/ } @{$seena{$func}};
386         unless (defined $GENFUNC) {
387             $GENFUNC = $FUNC;
388             $GENFUNC =~ s/^GET//;
389         }
390         if (@h) {
391             push @define, "#if defined(HAS_${FUNC}_R) && (" . join(" || ", map { "${FUNC}_R_PROTO == REENTRANT_PROTO_$_" } @h) . ")\n";
392
393             push @define, <<EOF;
394 #   define $HAS
395 #else
396 #   undef  $HAS
397 #endif
398 EOF
399         }
400     }
401     return if @F == 1;
402     push @define, <<EOF;
403
404 /* Any of the @F using \L$n? */
405
406 EOF
407     push @define, "#if (" . join(" || ", map { "defined($_)" } @H) . ")\n";
408     push @define, <<EOF;
409 #   define USE_${GENFUNC}_$n
410 #else
411 #   undef  USE_${GENFUNC}_$n
412 #endif
413
414 EOF
415 }
416
417 define('BUFFER',  'B',
418        qw(getgrent getgrgid getgrnam));
419
420 define('PTR',  'R',
421        qw(getgrent getgrgid getgrnam));
422 define('PTR',  'R',
423        qw(getpwent getpwnam getpwuid));
424 define('PTR',  'R',
425        qw(getspent getspnam));
426
427 define('FPTR', 'H',
428        qw(getgrent getgrgid getgrnam setgrent endgrent));
429 define('FPTR', 'H',
430        qw(getpwent getpwnam getpwuid setpwent endpwent));
431
432 define('BUFFER',  'B',
433        qw(getpwent getpwgid getpwnam));
434
435 define('PTR', 'R',
436        qw(gethostent gethostbyaddr gethostbyname));
437 define('PTR', 'R',
438        qw(getnetent getnetbyaddr getnetbyname));
439 define('PTR', 'R',
440        qw(getprotoent getprotobyname getprotobynumber));
441 define('PTR', 'R',
442        qw(getservent getservbyname getservbyport));
443
444 define('BUFFER', 'B',
445        qw(gethostent gethostbyaddr gethostbyname));
446 define('BUFFER', 'B',
447        qw(getnetent getnetbyaddr getnetbyname));
448 define('BUFFER', 'B',
449        qw(getprotoent getprotobyname getprotobynumber));
450 define('BUFFER', 'B',
451        qw(getservent getservbyname getservbyport));
452
453 define('ERRNO', 'E',
454        qw(gethostent gethostbyaddr gethostbyname));
455 define('ERRNO', 'E',
456        qw(getnetent getnetbyaddr getnetbyname));
457
458 # The following loop accumulates the "ssif" (struct, size, init, free)
459 # sections that declare the struct members (in reentr.h), and the buffer
460 # size initialization, buffer initialization (malloc), and buffer
461 # release (free) code (in reentr.c).
462 #
463 # The loop also contains a lot of intrinsic logic about groups of
464 # functions (since functions of certain kind operate the same way).
465
466 for my $func (@seenf) {
467     my $FUNC = uc $func;
468     my $ifdef = "#ifdef HAS_${FUNC}_R\n";
469     my $endif = "#endif /* HAS_${FUNC}_R */\n";
470     if (exists $seena{$func}) {
471         my @p = @{$seena{$func}};
472         if ($func =~ /^(asctime|ctime|getlogin|setlocale|strerror|ttyname)$/) {
473             pushssif $ifdef;
474             push @struct, <<EOF;
475         char*   _${func}_buffer;
476         size_t  _${func}_size;
477 EOF
478             push @size, <<EOF;
479         PL_reentrant_buffer->_${func}_size = REENTRANTSMALLSIZE;
480 EOF
481             pushinitfree $func;
482             pushssif $endif;
483         }
484         elsif ($func =~ /^(crypt)$/) {
485             pushssif $ifdef;
486             push @struct, <<EOF;
487 #if CRYPT_R_PROTO == REENTRANT_PROTO_B_CCD
488         $seend{$func} _${func}_data;
489 #else
490         $seent{$func} _${func}_struct;
491 #endif
492 EOF
493             push @init, <<EOF;
494 #if CRYPT_R_PROTO != REENTRANT_PROTO_B_CCD
495         PL_reentrant_buffer->_${func}_struct_buffer = 0;
496 #endif
497 EOF
498             push @free, <<EOF;
499 #if CRYPT_R_PROTO != REENTRANT_PROTO_B_CCD
500         Safefree(PL_reentrant_buffer->_${func}_struct_buffer);
501 #endif
502 EOF
503             pushssif $endif;
504         }
505         elsif ($func =~ /^(drand48|gmtime|localtime)$/) {
506             pushssif $ifdef;
507             push @struct, <<EOF;
508         $seent{$func} _${func}_struct;
509 EOF
510             if ($1 eq 'drand48') {
511                 push @struct, <<EOF;
512         double  _${func}_double;
513 EOF
514             }
515             pushssif $endif;
516         }
517         elsif ($func =~ /^random$/) {
518             pushssif $ifdef;
519             push @struct, <<EOF;
520 #   if RANDOM_R_PROTO != REENTRANT_PROTO_I_St
521         $seent{$func} _${func}_struct;
522 #   endif
523 EOF
524             pushssif $endif;
525         }
526         elsif ($func =~ /^(getgrnam|getpwnam|getspnam)$/) {
527             pushssif $ifdef;
528             # 'genfunc' can be read either as 'generic' or 'genre',
529             # it represents a group of functions.
530             my $genfunc = $func;
531             $genfunc =~ s/nam/ent/g;
532             $genfunc =~ s/^get//;
533             my $GENFUNC = uc $genfunc;
534             push @struct, <<EOF;
535         $seent{$func}   _${genfunc}_struct;
536         char*   _${genfunc}_buffer;
537         size_t  _${genfunc}_size;
538 EOF
539             push @struct, <<EOF;
540 #   ifdef USE_${GENFUNC}_PTR
541         $seent{$func}*  _${genfunc}_ptr;
542 #   endif
543 EOF
544             push @struct, <<EOF;
545 #   ifdef USE_${GENFUNC}_FPTR
546         FILE*   _${genfunc}_fptr;
547 #   endif
548 EOF
549             push @init, <<EOF;
550 #   ifdef USE_${GENFUNC}_FPTR
551         PL_reentrant_buffer->_${genfunc}_fptr = NULL;
552 #   endif
553 EOF
554             my $sc = $genfunc eq 'grent' ?
555                     '_SC_GETGR_R_SIZE_MAX' : '_SC_GETPW_R_SIZE_MAX';
556             my $sz = "_${genfunc}_size";
557             push @size, <<EOF;
558 #   if defined(HAS_SYSCONF) && defined($sc) && !defined(__GLIBC__)
559         PL_reentrant_buffer->$sz = sysconf($sc);
560         if (PL_reentrant_buffer->$sz == (size_t) -1)
561                 PL_reentrant_buffer->$sz = REENTRANTUSUALSIZE;
562 #   else
563 #       if defined(__osf__) && defined(__alpha) && defined(SIABUFSIZ)
564         PL_reentrant_buffer->$sz = SIABUFSIZ;
565 #       else
566 #           ifdef __sgi
567         PL_reentrant_buffer->$sz = BUFSIZ;
568 #           else
569         PL_reentrant_buffer->$sz = REENTRANTUSUALSIZE;
570 #           endif
571 #       endif
572 #   endif 
573 EOF
574             pushinitfree $genfunc;
575             pushssif $endif;
576         }
577         elsif ($func =~ /^(gethostbyname|getnetbyname|getservbyname|getprotobyname)$/) {
578             pushssif $ifdef;
579             my $genfunc = $func;
580             $genfunc =~ s/byname/ent/;
581             $genfunc =~ s/^get//;
582             my $GENFUNC = uc $genfunc;
583             my $D = ifprotomatch($FUNC, grep {/D/} @p);
584             my $d = $seend{$func};
585             $d =~ s/\*$//; # snip: we need need the base type.
586             push @struct, <<EOF;
587         $seent{$func}   _${genfunc}_struct;
588 #   if $D
589         $d      _${genfunc}_data;
590 #   else
591         char*   _${genfunc}_buffer;
592         size_t  _${genfunc}_size;
593 #   endif
594 #   ifdef USE_${GENFUNC}_PTR
595         $seent{$func}*  _${genfunc}_ptr;
596 #   endif
597 EOF
598             push @struct, <<EOF;
599 #   ifdef USE_${GENFUNC}_ERRNO
600         int     _${genfunc}_errno;
601 #   endif 
602 EOF
603             push @size, <<EOF;
604 #if   !($D)
605         PL_reentrant_buffer->_${genfunc}_size = REENTRANTUSUALSIZE;
606 #endif
607 EOF
608             push @init, <<EOF;
609 #if   !($D)
610         Newx(PL_reentrant_buffer->_${genfunc}_buffer, PL_reentrant_buffer->_${genfunc}_size, char);
611 #endif
612 EOF
613             push @free, <<EOF;
614 #if   !($D)
615         Safefree(PL_reentrant_buffer->_${genfunc}_buffer);
616 #endif
617 EOF
618             pushssif $endif;
619         }
620         elsif ($func =~ /^(readdir|readdir64)$/) {
621             pushssif $ifdef;
622             my $R = ifprotomatch($FUNC, grep {/R/} @p);
623             push @struct, <<EOF;
624         $seent{$func}*  _${func}_struct;
625         size_t  _${func}_size;
626 #   if $R
627         $seent{$func}*  _${func}_ptr;
628 #   endif
629 EOF
630             push @size, <<EOF;
631         /* This is the size Solaris recommends.
632          * (though we go static, should use pathconf() instead) */
633         PL_reentrant_buffer->_${func}_size = sizeof($seent{$func}) + MAXPATHLEN + 1;
634 EOF
635             push @init, <<EOF;
636         PL_reentrant_buffer->_${func}_struct = ($seent{$func}*)safemalloc(PL_reentrant_buffer->_${func}_size);
637 EOF
638             push @free, <<EOF;
639         Safefree(PL_reentrant_buffer->_${func}_struct);
640 EOF
641             pushssif $endif;
642         }
643
644         push @wrap, $ifdef;
645
646         push @wrap, <<EOF;
647 #  if defined(PERL_REENTR_API) && (PERL_REENTR_API == 1)
648 #   undef $func
649 EOF
650
651         # Write out what we have learned.
652         
653         my @v = 'a'..'z';
654         my $v = join(", ", @v[0..$seenu{$func}-1]);
655         for my $p (@p) {
656             my ($r, $a) = split '_', $p;
657             my $test = $r eq 'I' ? ' == 0' : '';
658             my $true  = 1;
659             my $genfunc = $func;
660             if ($genfunc =~ /^(?:get|set|end)(pw|gr|host|net|proto|serv|sp)/) {
661                 $genfunc = "${1}ent";
662             } elsif ($genfunc eq 'srand48') {
663                 $genfunc = "drand48";
664             }
665             my $b = $a;
666             my $w = '';
667             substr($b, 0, $seenu{$func}) = '';
668             if ($func =~ /^random$/) {
669                 $true = "PL_reentrant_buffer->_random_retval";
670             } elsif ($b =~ /R/) {
671                 $true = "PL_reentrant_buffer->_${genfunc}_ptr";
672             } elsif ($b =~ /T/ && $func eq 'drand48') {
673                 $true = "PL_reentrant_buffer->_${genfunc}_double";
674             } elsif ($b =~ /S/) {
675                 if ($func =~ /^readdir/) {
676                     $true = "PL_reentrant_buffer->_${genfunc}_struct";
677                 } else {
678                     $true = "&PL_reentrant_buffer->_${genfunc}_struct";
679                 }
680             } elsif ($b =~ /B/) {
681                 $true = "PL_reentrant_buffer->_${genfunc}_buffer";
682             }
683             if (length $b) {
684                 $w = join ", ",
685                          map {
686                              $_ eq 'R' ?
687                                  "&PL_reentrant_buffer->_${genfunc}_ptr" :
688                              $_ eq 'E' ?
689                                  "&PL_reentrant_buffer->_${genfunc}_errno" :
690                              $_ eq 'B' ?
691                                  "PL_reentrant_buffer->_${genfunc}_buffer" :
692                              $_ =~ /^[WI]$/ ?
693                                  "PL_reentrant_buffer->_${genfunc}_size" :
694                              $_ eq 'H' ?
695                                  "&PL_reentrant_buffer->_${genfunc}_fptr" :
696                              $_ eq 'D' ?
697                                  "&PL_reentrant_buffer->_${genfunc}_data" :
698                              $_ eq 'S' ?
699                                  ($func =~ /^readdir\d*$/ ?
700                                   "PL_reentrant_buffer->_${genfunc}_struct" :
701                                   $func =~ /^crypt$/ ?
702                                   "PL_reentrant_buffer->_${genfunc}_struct_buffer" :
703                                   "&PL_reentrant_buffer->_${genfunc}_struct") :
704                              $_ eq 'T' && $func eq 'drand48' ?
705                                  "&PL_reentrant_buffer->_${genfunc}_double" :
706                              $_ =~ /^[ilt]$/ && $func eq 'random' ?
707                                  "&PL_reentrant_buffer->_random_retval" :
708                                  $_
709                          } split '', $b;
710                 $w = ", $w" if length $v;
711             }
712
713             my $call = "${func}_r($v$w)";
714
715             # Must make OpenBSD happy
716             my $memzero = '';
717             if($p =~ /D$/ &&
718                 ($genfunc eq 'protoent' || $genfunc eq 'servent')) {
719                 $memzero = 'REENTR_MEMZERO(&PL_reentrant_buffer->_' . $genfunc . '_data, sizeof(PL_reentrant_buffer->_' . $genfunc . '_data))';
720             }
721             push @wrap, <<EOF;
722 #   if !defined($func) && ${FUNC}_R_PROTO == REENTRANT_PROTO_$p
723 EOF
724             if ($r eq 'V' || $r eq 'B') {
725                 push @wrap, <<EOF;
726 #       define $func($v) $call
727 EOF
728             } else {
729                 if ($func =~ /^get/) {
730                     my $rv = $v ? ", $v" : "";
731                     if ($r eq 'I') {
732                         $call = qq[((PL_REENTRANT_RETINT = $call)$test ? $true : (((PL_REENTRANT_RETINT == ERANGE) || (errno == ERANGE)) ? ($seenm{$func}{$seenr{$func}})Perl_reentrant_retry("$func"$rv) : 0))];
733                         my $arg = join(", ", map { $seenm{$func}{substr($a,$_,1)}." ".$v[$_] } 0..$seenu{$func}-1);
734                         my $ret = $seenr{$func} eq 'V' ? "" : "return ";
735                         my $memzero_ = $memzero ? "$memzero, " : "";
736                         push @wrap, <<EOF;
737 #       ifdef PERL_CORE
738 #           define $func($v) ($memzero_$call)
739 #       else
740 #           if defined(__GNUC__) && !defined(__STRICT_ANSI__) && !defined(PERL_GCC_PEDANTIC)
741 #               define $func($v) ({int PL_REENTRANT_RETINT; $memzero; $call;})
742 #           else
743 #               define $func($v) Perl_reentr_$func($v)
744                 static $seenm{$func}{$seenr{$func}} Perl_reentr_$func($arg) {
745                     dTHX;
746                     int PL_REENTRANT_RETINT;
747                     $memzero;
748                     $ret$call;
749                 }
750 #           endif
751 #       endif
752 EOF
753                     } else {
754                         push @wrap, <<EOF;
755 #       define $func($v) ($call$test ? $true : ((errno == ERANGE) ? ($seent{$func} *) Perl_reentrant_retry("$func"$rv) : 0))
756 EOF
757                     }
758                 } else {
759                 push @wrap, <<EOF;
760 #       define $func($v) ($call$test ? $true : 0)
761 EOF
762                 }
763             }
764             push @wrap, <<EOF;
765 #  endif /* if defined(PERL_REENTR_API) && (PERL_REENTR_API == 1) */
766 EOF
767         }
768
769             push @wrap, <<EOF;
770 #   endif /* HAS_\U$func */
771 EOF
772
773         push @wrap, $endif, "\n";
774     }
775 }
776
777 # New struct members added here to maintain binary compatibility with 5.8.0
778
779 if (exists $seena{crypt}) {
780     push @struct, <<EOF;
781 #ifdef HAS_CRYPT_R
782 #if CRYPT_R_PROTO == REENTRANT_PROTO_B_CCD
783 #else
784         struct crypt_data *_crypt_struct_buffer;
785 #endif
786 #endif /* HAS_CRYPT_R */
787 EOF
788 }
789
790 if (exists $seena{random}) {
791     push @struct, <<EOF;
792 #ifdef HAS_RANDOM_R
793 #   if RANDOM_R_PROTO == REENTRANT_PROTO_I_iS
794         int     _random_retval;
795 #   endif
796 #   if RANDOM_R_PROTO == REENTRANT_PROTO_I_lS
797         long    _random_retval;
798 #   endif
799 #   if RANDOM_R_PROTO == REENTRANT_PROTO_I_St
800         $seent{random} _random_struct;
801         int32_t _random_retval;
802 #   endif
803 #endif /* HAS_RANDOM_R */
804 EOF
805 }
806
807 if (exists $seena{srandom}) {
808     push @struct, <<EOF;
809 #ifdef HAS_SRANDOM_R
810         $seent{srandom} _srandom_struct;
811 #endif /* HAS_SRANDOM_R */
812 EOF
813 }
814
815
816 local $" = '';
817
818 print <<EOF;
819
820 /* Defines for indicating which special features are supported. */
821
822 @define
823 typedef struct {
824 @struct
825     int dummy; /* cannot have empty structs */
826 } REENTR;
827
828 #endif /* USE_REENTRANT_API */
829
830 #endif
831 EOF
832
833 close(H);
834
835 die "reentr.inc: $!" unless open(H, ">reentr.inc");
836 select H;
837
838 local $" = '';
839
840 print <<EOF;
841 /*
842  *    reentr.inc
843  *
844  *    You may distribute under the terms of either the GNU General Public
845  *    License or the Artistic License, as specified in the README file.
846  *
847  *  !!!!!!!   DO NOT EDIT THIS FILE   !!!!!!!
848  *  This file is built by reentrl.pl from data in reentr.pl.
849  */
850
851 #ifndef REENTRINC
852 #define REENTRINC
853
854 #ifdef USE_REENTRANT_API
855
856 /*
857  * As of OpenBSD 3.7, reentrant functions are now working, they just are
858  * incompatible with everyone else.  To make OpenBSD happy, we have to
859  * memzero out certain structures before calling the functions.
860  */
861 #if defined(__OpenBSD__)
862 #    define REENTR_MEMZERO(a,b) memzero(a,b)
863 #else
864 #    define REENTR_MEMZERO(a,b) 0
865 #endif
866
867 /* The reentrant wrappers. */
868
869 @wrap
870
871 #endif /* USE_REENTRANT_API */
872  
873 #endif
874
875 /* ex: set ro: */
876 EOF
877
878 close(H);
879
880 # Prepare to write the reentr.c.
881
882 safer_unlink 'reentr.c';
883 die "reentr.c: $!" unless open(C, ">reentr.c");
884 binmode C;
885 select C;
886 print <<EOF;
887 /* -*- buffer-read-only: t -*-
888  *
889  *    reentr.c
890  *
891  *    Copyright (C) 2002, 2003, 2005, 2006 by Larry Wall and others
892  *
893  *    You may distribute under the terms of either the GNU General Public
894  *    License or the Artistic License, as specified in the README file.
895  *
896  *  !!!!!!!   DO NOT EDIT THIS FILE   !!!!!!!
897  *  This file is built by reentr.pl from data in reentr.pl.
898  *
899  * "Saruman," I said, standing away from him, "only one hand at a time can
900  *  wield the One, and you know that well, so do not trouble to say we!"
901  *
902  * This file contains a collection of automatically created wrappers
903  * (created by running reentr.pl) for reentrant (thread-safe) versions of
904  * various library calls, such as getpwent_r.  The wrapping is done so
905  * that other files like pp_sys.c calling those library functions need not
906  * care about the differences between various platforms' idiosyncrasies
907  * regarding these reentrant interfaces.  
908  */
909
910 #include "EXTERN.h"
911 #define PERL_IN_REENTR_C
912 #include "perl.h"
913 #include "reentr.h"
914
915 void
916 Perl_reentrant_size(pTHX) {
917 #ifdef USE_REENTRANT_API
918 #define REENTRANTSMALLSIZE       256    /* Make something up. */
919 #define REENTRANTUSUALSIZE      4096    /* Make something up. */
920 @size
921 #endif /* USE_REENTRANT_API */
922 }
923
924 void
925 Perl_reentrant_init(pTHX) {
926 #ifdef USE_REENTRANT_API
927         Newx(PL_reentrant_buffer, 1, REENTR);
928         Perl_reentrant_size(aTHX);
929 @init
930 #endif /* USE_REENTRANT_API */
931 }
932
933 void
934 Perl_reentrant_free(pTHX) {
935 #ifdef USE_REENTRANT_API
936 @free
937         Safefree(PL_reentrant_buffer);
938 #endif /* USE_REENTRANT_API */
939 }
940
941 void*
942 Perl_reentrant_retry(const char *f, ...)
943 {
944     dTHX;
945     void *retptr = NULL;
946 #ifdef USE_REENTRANT_API
947 #  if defined(USE_HOSTENT_BUFFER) || defined(USE_GRENT_BUFFER) || defined(USE_NETENT_BUFFER) || defined(USE_PWENT_BUFFER) || defined(USE_PROTOENT_BUFFER) || defined(USE_SERVENT_BUFFER)
948     void *p0;
949 #  endif
950 #  if defined(USE_SERVENT_BUFFER)
951     void *p1;
952 #  endif
953 #  if defined(USE_HOSTENT_BUFFER)
954     size_t asize;
955 #  endif
956 #  if defined(USE_HOSTENT_BUFFER) || defined(USE_NETENT_BUFFER) || defined(USE_PROTOENT_BUFFER) || defined(USE_SERVENT_BUFFER)
957     int anint;
958 #  endif
959     va_list ap;
960
961     va_start(ap, f);
962
963     switch (PL_op->op_type) {
964 #ifdef USE_HOSTENT_BUFFER
965     case OP_GHBYADDR:
966     case OP_GHBYNAME:
967     case OP_GHOSTENT:
968         {
969 #ifdef PERL_REENTRANT_MAXSIZE
970             if (PL_reentrant_buffer->_hostent_size <=
971                 PERL_REENTRANT_MAXSIZE / 2)
972 #endif
973             {
974                 PL_reentrant_buffer->_hostent_size *= 2;
975                 Renew(PL_reentrant_buffer->_hostent_buffer,
976                       PL_reentrant_buffer->_hostent_size, char);
977                 switch (PL_op->op_type) {
978                 case OP_GHBYADDR:
979                     p0    = va_arg(ap, void *);
980                     asize = va_arg(ap, size_t);
981                     anint  = va_arg(ap, int);
982                     retptr = gethostbyaddr(p0, asize, anint); break;
983                 case OP_GHBYNAME:
984                     p0 = va_arg(ap, void *);
985                     retptr = gethostbyname((char *)p0); break;
986                 case OP_GHOSTENT:
987                     retptr = gethostent(); break;
988                 default:
989                     SETERRNO(ERANGE, LIB_INVARG);
990                     break;
991                 }
992             }
993         }
994         break;
995 #endif
996 #ifdef USE_GRENT_BUFFER
997     case OP_GGRNAM:
998     case OP_GGRGID:
999     case OP_GGRENT:
1000         {
1001 #ifdef PERL_REENTRANT_MAXSIZE
1002             if (PL_reentrant_buffer->_grent_size <=
1003                 PERL_REENTRANT_MAXSIZE / 2)
1004 #endif
1005             {
1006                 Gid_t gid;
1007                 PL_reentrant_buffer->_grent_size *= 2;
1008                 Renew(PL_reentrant_buffer->_grent_buffer,
1009                       PL_reentrant_buffer->_grent_size, char);
1010                 switch (PL_op->op_type) {
1011                 case OP_GGRNAM:
1012                     p0 = va_arg(ap, void *);
1013                     retptr = getgrnam((char *)p0); break;
1014                 case OP_GGRGID:
1015 #if Gid_t_size < INTSIZE
1016                     gid = (Gid_t)va_arg(ap, int);
1017 #else
1018                     gid = va_arg(ap, Gid_t);
1019 #endif
1020                     retptr = getgrgid(gid); break;
1021                 case OP_GGRENT:
1022                     retptr = getgrent(); break;
1023                 default:
1024                     SETERRNO(ERANGE, LIB_INVARG);
1025                     break;
1026                 }
1027             }
1028         }
1029         break;
1030 #endif
1031 #ifdef USE_NETENT_BUFFER
1032     case OP_GNBYADDR:
1033     case OP_GNBYNAME:
1034     case OP_GNETENT:
1035         {
1036 #ifdef PERL_REENTRANT_MAXSIZE
1037             if (PL_reentrant_buffer->_netent_size <=
1038                 PERL_REENTRANT_MAXSIZE / 2)
1039 #endif
1040             {
1041                 Netdb_net_t net;
1042                 PL_reentrant_buffer->_netent_size *= 2;
1043                 Renew(PL_reentrant_buffer->_netent_buffer,
1044                       PL_reentrant_buffer->_netent_size, char);
1045                 switch (PL_op->op_type) {
1046                 case OP_GNBYADDR:
1047                     net = va_arg(ap, Netdb_net_t);
1048                     anint = va_arg(ap, int);
1049                     retptr = getnetbyaddr(net, anint); break;
1050                 case OP_GNBYNAME:
1051                     p0 = va_arg(ap, void *);
1052                     retptr = getnetbyname((char *)p0); break;
1053                 case OP_GNETENT:
1054                     retptr = getnetent(); break;
1055                 default:
1056                     SETERRNO(ERANGE, LIB_INVARG);
1057                     break;
1058                 }
1059             }
1060         }
1061         break;
1062 #endif
1063 #ifdef USE_PWENT_BUFFER
1064     case OP_GPWNAM:
1065     case OP_GPWUID:
1066     case OP_GPWENT:
1067         {
1068 #ifdef PERL_REENTRANT_MAXSIZE
1069             if (PL_reentrant_buffer->_pwent_size <=
1070                 PERL_REENTRANT_MAXSIZE / 2)
1071 #endif
1072             {
1073                 Uid_t uid;
1074                 PL_reentrant_buffer->_pwent_size *= 2;
1075                 Renew(PL_reentrant_buffer->_pwent_buffer,
1076                       PL_reentrant_buffer->_pwent_size, char);
1077                 switch (PL_op->op_type) {
1078                 case OP_GPWNAM:
1079                     p0 = va_arg(ap, void *);
1080                     retptr = getpwnam((char *)p0); break;
1081                 case OP_GPWUID:
1082 #if Uid_t_size < INTSIZE
1083                     uid = (Uid_t)va_arg(ap, int);
1084 #else
1085                     uid = va_arg(ap, Uid_t);
1086 #endif
1087                     retptr = getpwuid(uid); break;
1088                 case OP_GPWENT:
1089                     retptr = getpwent(); break;
1090                 default:
1091                     SETERRNO(ERANGE, LIB_INVARG);
1092                     break;
1093                 }
1094             }
1095         }
1096         break;
1097 #endif
1098 #ifdef USE_PROTOENT_BUFFER
1099     case OP_GPBYNAME:
1100     case OP_GPBYNUMBER:
1101     case OP_GPROTOENT:
1102         {
1103 #ifdef PERL_REENTRANT_MAXSIZE
1104             if (PL_reentrant_buffer->_protoent_size <=
1105                 PERL_REENTRANT_MAXSIZE / 2)
1106 #endif
1107             {
1108                 PL_reentrant_buffer->_protoent_size *= 2;
1109                 Renew(PL_reentrant_buffer->_protoent_buffer,
1110                       PL_reentrant_buffer->_protoent_size, char);
1111                 switch (PL_op->op_type) {
1112                 case OP_GPBYNAME:
1113                     p0 = va_arg(ap, void *);
1114                     retptr = getprotobyname((char *)p0); break;
1115                 case OP_GPBYNUMBER:
1116                     anint = va_arg(ap, int);
1117                     retptr = getprotobynumber(anint); break;
1118                 case OP_GPROTOENT:
1119                     retptr = getprotoent(); break;
1120                 default:
1121                     SETERRNO(ERANGE, LIB_INVARG);
1122                     break;
1123                 }
1124             }
1125         }
1126         break;
1127 #endif
1128 #ifdef USE_SERVENT_BUFFER
1129     case OP_GSBYNAME:
1130     case OP_GSBYPORT:
1131     case OP_GSERVENT:
1132         {
1133 #ifdef PERL_REENTRANT_MAXSIZE
1134             if (PL_reentrant_buffer->_servent_size <=
1135                 PERL_REENTRANT_MAXSIZE / 2)
1136 #endif
1137             {
1138                 PL_reentrant_buffer->_servent_size *= 2;
1139                 Renew(PL_reentrant_buffer->_servent_buffer,
1140                       PL_reentrant_buffer->_servent_size, char);
1141                 switch (PL_op->op_type) {
1142                 case OP_GSBYNAME:
1143                     p0 = va_arg(ap, void *);
1144                     p1 = va_arg(ap, void *);
1145                     retptr = getservbyname((char *)p0, (char *)p1); break;
1146                 case OP_GSBYPORT:
1147                     anint = va_arg(ap, int);
1148                     p0 = va_arg(ap, void *);
1149                     retptr = getservbyport(anint, (char *)p0); break;
1150                 case OP_GSERVENT:
1151                     retptr = getservent(); break;
1152                 default:
1153                     SETERRNO(ERANGE, LIB_INVARG);
1154                     break;
1155                 }
1156             }
1157         }
1158         break;
1159 #endif
1160     default:
1161         /* Not known how to retry, so just fail. */
1162         break;
1163     }
1164
1165     va_end(ap);
1166 #else
1167     PERL_UNUSED_ARG(f);
1168 #endif
1169     return retptr;
1170 }
1171
1172 /* ex: set ro: */
1173 EOF
1174
1175 __DATA__
1176 asctime B_S     |time   |const struct tm|B_SB|B_SBI|I_SB|I_SBI
1177 crypt B_CC      |crypt  |struct crypt_data|B_CCS|B_CCD|D=CRYPTD*
1178 ctermid B_B     |stdio  |               |B_B
1179 ctime B_S       |time   |const time_t   |B_SB|B_SBI|I_SB|I_SBI
1180 drand48 d_V     |stdlib |struct drand48_data    |I_ST|T=double*|d=double
1181 endgrent        |grp    |               |I_H|V_H
1182 endhostent      |netdb  |               |I_D|V_D|D=struct hostent_data*
1183 endnetent       |netdb  |               |I_D|V_D|D=struct netent_data*
1184 endprotoent     |netdb  |               |I_D|V_D|D=struct protoent_data*
1185 endpwent        |pwd    |               |I_H|V_H
1186 endservent      |netdb  |               |I_D|V_D|D=struct servent_data*
1187 getgrent S_V    |grp    |struct group   |I_SBWR|I_SBIR|S_SBW|S_SBI|I_SBI|I_SBIH
1188 getgrgid S_T    |grp    |struct group   |I_TSBWR|I_TSBIR|I_TSBI|S_TSBI|T=gid_t
1189 getgrnam S_C    |grp    |struct group   |I_CSBWR|I_CSBIR|S_CBI|I_CSBI|S_CSBI
1190 gethostbyaddr S_CWI     |netdb  |struct hostent |I_CWISBWRE|S_CWISBWIE|S_CWISBIE|S_TWISBIE|S_CIISBIE|S_CSBIE|S_TSBIE|I_CWISD|I_CIISD|I_CII|I_TsISBWRE|D=struct hostent_data*|T=const void*|s=socklen_t
1191 gethostbyname S_C       |netdb  |struct hostent |I_CSBWRE|S_CSBIE|I_CSD|D=struct hostent_data*
1192 gethostent S_V  |netdb  |struct hostent |I_SBWRE|I_SBIE|S_SBIE|S_SBI|I_SBI|I_SD|D=struct hostent_data*
1193 getlogin B_V    |unistd |char           |I_BW|I_BI|B_BW|B_BI
1194 getnetbyaddr S_LI       |netdb  |struct netent  |I_UISBWRE|I_LISBI|S_TISBI|S_LISBI|I_TISD|I_LISD|I_IISD|I_uISBWRE|D=struct netent_data*|T=in_addr_t|U=unsigned long|u=uint32_t
1195 getnetbyname S_C        |netdb  |struct netent  |I_CSBWRE|I_CSBI|S_CSBI|I_CSD|D=struct netent_data*
1196 getnetent S_V   |netdb  |struct netent  |I_SBWRE|I_SBIE|S_SBIE|S_SBI|I_SBI|I_SD|D=struct netent_data*
1197 getprotobyname S_C      |netdb  |struct protoent|I_CSBWR|S_CSBI|I_CSD|D=struct protoent_data*
1198 getprotobynumber S_I    |netdb  |struct protoent|I_ISBWR|S_ISBI|I_ISD|D=struct protoent_data*
1199 getprotoent S_V |netdb  |struct protoent|I_SBWR|I_SBI|S_SBI|I_SD|D=struct protoent_data*
1200 getpwent S_V    |pwd    |struct passwd  |I_SBWR|I_SBIR|S_SBW|S_SBI|I_SBI|I_SBIH
1201 getpwnam S_C    |pwd    |struct passwd  |I_CSBWR|I_CSBIR|S_CSBI|I_CSBI
1202 getpwuid S_T    |pwd    |struct passwd  |I_TSBWR|I_TSBIR|I_TSBI|S_TSBI|T=uid_t
1203 getservbyname S_CC      |netdb  |struct servent |I_CCSBWR|S_CCSBI|I_CCSD|D=struct servent_data*
1204 getservbyport S_IC      |netdb  |struct servent |I_ICSBWR|S_ICSBI|I_ICSD|D=struct servent_data*
1205 getservent S_V  |netdb  |struct servent |I_SBWR|I_SBI|S_SBI|I_SD|D=struct servent_data*
1206 getspnam S_C    |shadow |struct spwd    |I_CSBWR|S_CSBI
1207 gmtime S_T      |time   |struct tm      |S_TS|I_TS|T=const time_t*
1208 localtime S_T   |time   |struct tm      |S_TS|I_TS|T=const time_t*
1209 random L_V      |stdlib |struct random_data|I_iS|I_lS|I_St|i=int*|l=long*|t=int32_t*
1210 readdir S_T     |dirent |struct dirent  |I_TSR|I_TS|T=DIR*
1211 readdir64 S_T   |dirent |struct dirent64|I_TSR|I_TS|T=DIR*
1212 setgrent        |grp    |               |I_H|V_H
1213 sethostent V_I  |netdb  |               |I_ID|V_ID|D=struct hostent_data*
1214 setlocale B_IC  |locale |               |I_ICBI
1215 setnetent V_I   |netdb  |               |I_ID|V_ID|D=struct netent_data*
1216 setprotoent V_I |netdb  |               |I_ID|V_ID|D=struct protoent_data*
1217 setpwent        |pwd    |               |I_H|V_H
1218 setservent V_I  |netdb  |               |I_ID|V_ID|D=struct servent_data*
1219 srand48 V_L     |stdlib |struct drand48_data    |I_LS
1220 srandom V_T     |stdlib |struct random_data|I_TS|T=unsigned int
1221 strerror B_I    |string |               |I_IBW|I_IBI|B_IBW
1222 tmpnam B_B      |stdio  |               |B_B
1223 ttyname B_I     |unistd |               |I_IBW|I_IBI|B_IBI