summary refs log tree commit diff
path: root/posix/annexc.c
blob: 8b95311314ff3decfb2f1d98ebec3c97bd0b0b76 (plain) (blame)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
/* Copyright (C) 1998 Free Software Foundation, Inc.
   This file is part of the GNU C Library.

   The GNU C Library is free software; you can redistribute it and/or
   modify it under the terms of the GNU Library General Public License as
   published by the Free Software Foundation; either version 2 of the
   License, or (at your option) any later version.

   The GNU C Library is distributed in the hope that it will be useful,
   but WITHOUT ANY WARRANTY; without even the implied warranty of
   MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
   Library General Public License for more details.

   You should have received a copy of the GNU Library General Public
   License along with the GNU C Library; see the file COPYING.LIB.  If not,
   write to the Free Software Foundation, Inc., 59 Temple Place - Suite 330,
   Boston, MA 02111-1307, USA.  */

#include <ctype.h>
#include <fnmatch.h>
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <unistd.h>

#define TMPFILE             "/tmp/macros"
#define HEADER_MAX          256

/* <aio.h>.  */
static const char *const aio_syms[] =
{
  "AIO_ALLDONE", "AIO_CANCELED", "AIO_NOTCANCELED", "LIO_NOP", "LIO_NOWAIT",
  "LIO_READ", "LIO_WAIT", "LIO_WRITE",
  /* From <fcntl.h>.  */
  "FD_CLOEXEC", "F_DUPFD", "F_GETFD", "F_GETFL", "F_GETLK", "F_RDLCK",
  "F_SETFD", "F_SETFL", "F_SETLK", "F_SETLKW", "F_UNLCK", "F_WRLCK",
  "O_ACCMODE", "O_APPEND", "O_CREAT", "O_DSYNC", "O_EXCL", "O_NOCTTY",
  "O_NONBLOCK", "O_RDONLY", "O_RDWR", "O_RSYNC", "O_SYNC", "O_TRUNC",
  "O_WRONLY",
  /* From <signal.h>.  */
  "SA_NOCLDSTOP", "SA_SIGINFO", "SIGABRT", "SIGALRM", "SIGBUS", "SIGCHLD",
  "SIGCONT", "SIGEV_NONE", "SIGEV_SIGNAL", "SIGEV_SIGNAL", "SIGEV_THREAD",
  "SIGFPE", "SIGHUP", "SIGILL", "SIGINT", "SIGKILL", "SIGPIPE", "SIGQUIT",
  "SIGRTMAX", "SIGRTMIN", "SIGSEGV", "SIGSTOP", "SIGTERM", "SIGTSTP",
  "SIGTTIN", "SIGTTOU", "SIGUSR1", "SIGUSR2", "SIG_BLOCK", "SIG_DFL",
  "SIG_ERR", "SIG_IGN", "SIG_SETMASK", "SIG_UNBLOCK", "SI_ASYNCIO",
  "SI_MESGQ", "SI_QUEUE", "SI_TIMER", "SI_USER"
};
static const char *const aio_maybe[] =
{
  "aio_cancel", "aio_error", "aio_fsync", "aio_read", "aio_return",
  "aio_suspend", "aio_write", "lio_listio",
  /* From <fcntl.h>.  */
  "creat", "fcntl", "open", "SEEK_CUR", "SEEK_END", "SEEK_SET", "S_IRGRP",
  "S_IROTH", "S_IRUSR", "S_IRWXG", "S_IRWXO", "S_IRWXU", "S_ISBLK",
  "S_ISCHR", "S_ISDIR", "S_ISFIFO", "S_ISGID", "S_ISREG", "S_ISUID",
  "S_IWGRP", "S_IWOTH", "S_IWUSR", "S_IXGRP", "S_IXOTH", "S_IXUSR",
  /* From <signal.h>.  */
  "kill", "raise", "sigaction", "sigaddset", "sigdelset", "sigemptyset",
  "sigfillset", "sigismember", "signal", "sigpending", "sigprocmask",
  "sigqueue", "sigsuspend", "sigtimedwait", "sigwait", "sigwaitinfo"
};

/* <assert.h>.  */
static const char *const assert_syms[] =
{
  "assert"
};
static const char *const assert_maybe[] =
{
};

/* <ctype.h>.   */
static const char *const ctype_syms[] =
{
};
static const char *const ctype_maybe[] =
{
  "isalnum", "isalpha", "iscntrl", "isdigit", "isgraph", "islower",
  "isprint", "ispunct", "isspace", "isupper", "isxdigit", "tolower",
  "toupper"
};

/* <dirent.h>.  */
static const char *const dirent_syms[] =
{
};
static const char *const dirent_maybe[] =
{
  "closedir", "opendir", "readdir", "readdir_r", "rewinddir"
};

/* <errno.h>.  */
static const char *const errno_syms[] =
{
  "E2BIG", "EACCESS", "EAGAIN", "EBADF", "EBADMSG", "EBUSY", "ECANCELED",
  "ECHILD", "EDEADLK", "EDOM", "EEXIST", "EFAULT", "EFBIG", "EINPROGRESS",
  "EINTR", "EINVAL", "EIO", "EISDIR", "EMFILE", "EMLINK", "EMSGSIZE",
  "ENAMETOOLONG", "ENFILE", "ENODEV", "ENOENT", "ENOEXEC", "ENOLCK",
  "ENOMEM", "ENOSPC", "ENOSYS", "ENOTDIR", "ENOTEMPTY", "ENOTSUP",
  "ENOTTY", "ENXIO", "EPERM", "EPIPE", "ERANGE", "EROFS", "ESPIPE",
  "ESRCH", "ETIMEDOUT", "EXDEV"
};
static const char *const errno_maybe[] =
{
  "errno", "E*"
};

/* <fcntl.h>.  */
static const char *const fcntl_syms[] =
{
  "FD_CLOEXEC", "F_DUPFD", "F_GETFD", "F_GETFL", "F_GETLK", "F_RDLCK",
  "F_SETFD", "F_SETFL", "F_SETLK", "F_SETLKW", "F_UNLCK", "F_WRLCK",
  "O_ACCMODE", "O_APPEND", "O_CREAT", "O_DSYNC", "O_EXCL", "O_NOCTTY",
  "O_NONBLOCK", "O_RDONLY", "O_RDWR", "O_RSYNC", "O_SYNC", "O_TRUNC",
  "O_WRONLY"
};
static const char *const fcntl_maybe[] =
{
  "creat", "fcntl", "open", "SEEK_CUR", "SEEK_END", "SEEK_SET", "S_IRGRP",
  "S_IROTH", "S_IRUSR", "S_IRWXG", "S_IRWXO", "S_IRWXU", "S_ISBLK",
  "S_ISCHR", "S_ISDIR", "S_ISFIFO", "S_ISGID", "S_ISREG", "S_ISUID",
  "S_IWGRP", "S_IWOTH", "S_IWUSR", "S_IXGRP", "S_IXOTH", "S_IXUSR"
};

/* <float.h>.  */
static const char *const float_syms[] =
{
  "DBL_DIG", "DBL_EPSILON", "DBL_MANT_DIG", "DBL_MAX", "DBL_MAX_10_EXP",
  "DBL_MAX_EXP", "DBL_MIN", "DBL_MIN_10_EXP", "DBL_MIN_EXP", "FLT_DIG",
  "FLT_EPSILON", "FLT_MANT_DIG", "FLT_MAX", "FLT_MAX_10_EXP", "FLT_MAX_EXP",
  "FLT_MIN", "FLT_MIN_10_EXP", "FLT_MIN_EXP", "FLT_RADIX", "FLT_ROUNDS",
  "LDBL_DIG", "LDBL_EPSILON", "LDBL_MANT_DIG", "LDBL_MAX", "LDBL_MAX_10_EXP",
  "LDBL_MAX_EXP", "LDBL_MIN", "LDBL_MIN_10_EXP", "LDBL_MIN_EXP"
};
static const char *const float_maybe[] =
{
};

/* <grp.h>.  */
static const char *const grp_syms[] =
{
};
static const char *const grp_maybe[] =
{
  "getgrgid", "getgrgid_r", "getgrnam", "getgrnam_r"
};

/* <limits.h>.  */
static const char *const limits_syms[] =
{
  "_POSIX_AIO_LISTIO_MAX", "_POSIX_AIO_MAX", "_POSIX_ARG_MAX",
  "_POSIX_CHILD_MAX", "_POSIX_CLOCKRES_MAX", "_POSIX_DELAYTIMER_MAX",
  "_POSIX_LINK_MAX", "_POSIX_LOGIN_NAME_MAX", "_POSIX_MAX_CANON",
  "_POSIX_MAX_INPUT", "_POSIX_MQ_OPEN_MAX", "_POSIX_MQ_PRIO_MAX",
  "_POSIX_NAME_MAX", "_POSIX_NGROUPS_MAX", "_POSIX_OPEN_MAX",
  "_POSIX_PATH_MAX", "_POSIX_PIPE_BUF", "_POSIX_RTSIG_MAX",
  "_POSIX_SEM_NSEMS_MAX", "_POSIX_SEM_VALUE_MAX", "_POSIX_SIGQUEUE_MAX",
  "_POSIX_SSIZE_MAX", "_POSIX_STREAM_MAX",
  "_POSIX_THREAD_DESTRUCTOR_ITERATIONS", "_POSIX_THREAD_KEYS_MAX",
  "_POSIX_THREAD_THREADS_MAX", "_POSIX_TIMER_MAX", "_POSIX_TTY_NAME_MAX",
  "_POSIX_TZNAME_MAX", "_POSIX_THREAD_DESTRUCTOR_ITERATIONS",
  "CHAR_BIT", "CHAR_MAX", "CHAR_MIN", "INT_MAX", "INT_MIN", "MB_LEN_MAX",
  "NGROUPS_MAX", "PAGESIZE", "SCHAR_MIN", "SCHAR_MAX"
};
static const char *const limits_maybe[] =
{
  "AIO_LISTIO_MAX", "AIO_MAX", "ARG_MAX", "CHILD_MAX", "DELAYTIMER_MAX",
  "LINK_MAX", "LOGIN_NAME_MAX", "LONG_MAX", "LONG_MIN", "MAX_CANON",
  "MAX_INPUT", "MQ_OPEN_MAX", "MQ_PRIO_MAX", "NAME_MAX", "OPEN_MAX",
  "PATH_MAX", "PIPE_BUF", "RTSIG_MAX", "PTHREAD_DESTRUCTOR_ITERATIONS",
  "PTHREAD_KEYS_MAX", "PTHREAD_STACK_MIN", "PTHREAD_THREADS_MAX"
};

/* <locale.h>.  */
static const char *const locale_syms[] =
{
  "LC_ALL", "LC_COLLATE", "LC_CTYPE", "LC_MONETARY", "LC_NUMERIC",
  "LC_TIME", "NULL"
};
static const char *const locale_maybe[] =
{
  "LC_*", "localeconv", "setlocale"
};

/* <math.h>.  */
static const char *const math_syms[] =
{
  "HUGE_VAL"
};
static const char *const math_maybe[] =
{
  "acos", "asin", "atan2", "atan", "ceil", "cos", "cosh", "exp",
  "fabs", "floor", "fmod", "frexp", "ldexp", "log10", "log", "modf",
  "pow", "sin", "sinh", "sqrt", "tan", "tanh",
  "acosf", "asinf", "atan2f", "atanf", "ceilf", "cosf", "coshf", "expf",
  "fabsf", "floorf", "fmodf", "frexpf", "ldexpf", "log10f", "logf", "modff",
  "powf", "sinf", "sinhf", "sqrtf", "tanf", "tanhf",
  "acosl", "asinl", "atan2l", "atanl", "ceill", "cosl", "coshl", "expl",
  "fabsl", "floorl", "fmodl", "frexpl", "ldexpl", "log10l", "logl", "modfl",
  "powl", "sinl", "sinhl", "sqrtl", "tanl", "tanhl"
};

/* <mqueue.h>.  */
static const char *const mqueue_syms[] =
{
};
static const char *const mqueue_maybe[] =
{
  "mq_close", "mq_getattr", "mq_notify", "mq_open", "mq_receive",
  "mq_send", "mq_setattr", "mq_unlink"
};

/* <pthread.h>.  */
static const char *const pthread_syms[] =
{
  "PTHREAD_CANCELED", "PTHREAD_CANCEL_ASYNCHRONOUS",
  "PTHREAD_CANCEL_DEFERRED", "PTHREAD_CANCEL_DISABLE", "PTHREAD_CANCEL_ENABLE",
  "PTHREAD_COND_INITIALIZER", "PTHREAD_CREATE_DETACHED",
  "PTHREAD_CREATE_JOINABLE", "PTHREAD_EXPLICIT_SCHED",
  "PTHREAD_INHERIT_SCHED", "PTHREAD_MUTEX_INITIALIZER",
  "PTHREAD_ONCE_INIT", "PTHREAD_PRIO_INHERIT", "PTHREAD_PRIO_NONE",
  "PTHREAD_PRIO_PROTECT", "PTHREAD_PROCESS_PRIVATE",
  "PTHREAD_PROCESS_SHARED", "PTHREAD_SCOPE_PROCESS", "PTHREAD_SCOPE_SYSTEM",
  /* These come from <sched.h>.  */
  "SCHED_FIFO", "SCHED_OTHER", "SCHED_RR",
  /* These come from <time.h>.  */
  "CLK_TCK", "CLOCKS_PER_SEC", "CLOCK_REALTIME", "NULL", "TIMER_ABSTIME"
};
static const char *const pthread_maybe[] =
{
  "pthread_atfork", "pthread_attr_destroy", "pthread_attr_getdetachstate",
  "pthread_attr_getinheritsched", "pthread_attr_getschedparam",
  "pthread_attr_getschedpolicy", "pthread_attr_getscope",
  "pthread_attr_getstackaddr", "pthread_attr_getstacksize",
  "pthread_attr_init", "pthread_attr_setdetachstate",
  "pthread_attr_setinheritsched", "pthread_attr_setschedparam",
  "pthread_attr_setschedpolicy", "pthread_attr_setscope",
  "pthread_attr_setstackaddr", "pthread_attr_setstacksize",
  "pthread_cleanup_pop", "pthread_cleanup_push", "pthread_cond_broadcast",
  "pthread_cond_destroy", "pthread_cond_init", "pthread_cond_signal",
  "pthread_cond_timedwait", "pthread_cond_wait", "pthread_condattr_destroy",
  "pthread_condattr_getpshared", "pthread_condattr_init",
  "pthread_condattr_setpshared", "pthread_create", "pthread_detach",
  "pthread_equal", "pthread_exit", "pthread_getspecific", "pthread_join",
  "pthread_key_create", "pthread_key_destroy", "pthread_kill",
  "pthread_mutex_destroy", "pthread_mutex_getprioceiling",
  "pthread_mutex_init", "pthread_mutex_lock", "pthread_mutex_setprioceiling",
  "pthread_mutex_trylock", "pthread_mutex_unlock", "pthread_mutexattr_destroy",
  "pthread_mutexattr_getprioceiling", "pthread_mutexattr_getprotocol",
  "pthread_mutexattr_getpshared", "pthread_mutexattr_init",
  "pthread_mutexattr_setprioceiling", "pthread_mutexattr_setprotocol",
  "pthread_mutexattr_setpshared", "pthread_once", "pthread_self",
  "pthread_setcancelstate", "pthread_setcanceltype", "pthread_setspecific",
  "pthread_sigmask", "pthread_testcancel"
  /* These come from <sched.h>.  */
  "sched_get_priority_max", "sched_get_priority_min",
  "sched_get_rr_interval", "sched_getparam", "sched_getscheduler",
  "sched_setparam", "sched_setscheduler", "sched_yield",
  /* These come from <time.h>.  */
  "asctime", "asctime_r", "clock", "clock_getres", "clock_gettime",
  "clock_settime", "ctime", "ctime_r", "difftime", "gmtime", "gmtime_r",
  "localtime", "localtime_r", "mktime", "nanosleep", "strftime", "time",
  "timer_create", "timer_delete", "timer_getoverrun", "timer_gettime",
  "timer_settime", "tzset"
};

/* <pwd.h>.  */
static const char *const pwd_syms[] =
{
};
static const char *const pwd_maybe[] =
{
  "getpwnam", "getpwnam_r", "getpwuid", "getpwuid_r"
};

/* <sched.h>.  */
static const char *const sched_syms[] =
{
  "SCHED_FIFO", "SCHED_OTHER", "SCHED_RR",
};
static const char *const sched_maybe[] =
{
  "sched_get_priority_max", "sched_get_priority_min",
  "sched_get_rr_interval", "sched_getparam", "sched_getscheduler",
  "sched_setparam", "sched_setscheduler", "sched_yield",
  /* These come from <time.h>.  */
  "CLK_TCK", "CLOCKS_PER_SEC", "CLOCK_REALTIME", "NULL", "TIMER_ABSTIME"
  "asctime", "asctime_r", "clock", "clock_getres", "clock_gettime",
  "clock_settime", "ctime", "ctime_r", "difftime", "gmtime", "gmtime_r",
  "localtime", "localtime_r", "mktime", "nanosleep", "strftime", "time",
  "timer_create", "timer_delete", "timer_getoverrun", "timer_gettime",
  "timer_settime", "tzset"
};

/* <semaphore.h>.  */
static const char *const semaphore_syms[] =
{
};
static const char *const semaphore_maybe[] =
{
  "sem_close", "sem_destroy", "sem_getvalue", "sem_init", "sem_open",
  "sen_post", "sem_trywait", "sem_unlink", "sem_wait"
};

/* <setjmp.h>.  */
static const char *const setjmp_syms[] =
{
};
static const char *const setjmp_maybe[] =
{
  "longjmp", "setjmp", "siglongjmp", "sigsetjmp"
};

/* <signal.h>.  */
static const char *const signal_syms[] =
{
  "SA_NOCLDSTOP", "SA_SIGINFO", "SIGABRT", "SIGALRM", "SIGBUS", "SIGCHLD",
  "SIGCONT", "SIGEV_NONE", "SIGEV_SIGNAL", "SIGEV_SIGNAL", "SIGEV_THREAD",
  "SIGFPE", "SIGHUP", "SIGILL", "SIGINT", "SIGKILL", "SIGPIPE", "SIGQUIT",
  "SIGRTMAX", "SIGRTMIN", "SIGSEGV", "SIGSTOP", "SIGTERM", "SIGTSTP",
  "SIGTTIN", "SIGTTOU", "SIGUSR1", "SIGUSR2", "SIG_BLOCK", "SIG_DFL",
  "SIG_ERR", "SIG_IGN", "SIG_SETMASK", "SIG_UNBLOCK", "SI_ASYNCIO",
  "SI_MESGQ", "SI_QUEUE", "SI_TIMER", "SI_USER"
};
static const char *const signal_maybe[] =
{
  "kill", "raise", "sigaction", "sigaddset", "sigdelset", "sigemptyset",
  "sigfillset", "sigismember", "signal", "sigpending", "sigprocmask",
  "sigqueue", "sigsuspend", "sigtimedwait", "sigwait", "sigwaitinfo"
};

/* <stdarg.h>.  */
static const char *const stdarg_syms[] =
{
  "va_arg", "va_end", "va_list", "va_start"
};
static const char *const stdarg_maybe[] =
{
};

/* <stddef.h>.  */
static const char *const stddef_syms[] =
{
  "NULL", "offsetof"
};
static const char *const stddef_maybe[] =
{
};

/* <stdio.h>.  */
static const char *const stdio_syms[] =
{
  "BUFSIZ", "EOF", "FILENAME_MAX", "L_ctermid", "L_cuserid", "L_tmpnam",
  "NULL", "SEEK_CUR", "SEEK_END", "SEEK_SET", "STREAM_MAX", "TMP_MAX",
  "stderr", "stdin", "stdout", "_IOFBF", "_IOLBF", "_IONBF"
};
static const char *const stdio_maybe[] =
{
  "clearerr", "fclose", "fdopen", "feof", "ferror", "fflush", "fgetc",
  "fgetpos", "fgets", "fileno", "flockfile", "fopen", "fprintf", "fputc",
  "fputs", "fread", "freopen", "fscanf", "fseek", "fsetpos", "ftell",
  "ftrylockfile", "funlockfile", "fwrite", "getc", "getchar",
  "getchar_unlocked", "getc_unlocked", "gets", "perror", "printf", "putc",
  "putchar", "putchar_unlocked", "putc_unlocked", "puts", "remove", "rename",
  "rewind", "scanf", "setbuf", "setvbuf", "sprintf", "sscanf", "tmpfile",
  "tmpnam", "ungetc", "vfprintf", "vprintf", "vsprintf"
};

/* <stdlib.h>.  */
static const char *const stdlib_syms[] =
{
  "EXIT_FAILURE", "EXIT_SUCCESS", "MB_CUR_MAX", "NULL", "RAND_MAX"
};
static const char *const stdlib_maybe[] =
{
  "abort", "abs", "atexit", "atof", "atoi", "atol", "bsearch", "calloc",
  "div", "exit", "free", "getenv", "labs", "ldiv", "malloc", "mblen",
  "mbstowcs", "mbtowc", "qsort", "rand", "rand_r", "realloc", "srand",
  "strtod", "strtol", "strtoul", "system", "wcstombs", "wctomb"
};

/* <string.h>.  */
static const char *const string_syms[] =
{
  "NULL"
};
static const char *const string_maybe[] =
{
  "memchr", "memcmp", "memcpy", "memmove", "memset", "strcat", "strchr",
  "strcmp", "strcoll", "strcpy", "strcspn", "strerror", "strlen",
  "strncat", "strncmp", "strncpy", "strpbrk", "strrchr", "strspn",
  "strstr", "strtok", "strtok_r", "strxfrm"
};

/* <sys/mman.h>.  */
static const char *const mman_syms[] =
{
  "MAP_FAILED", "MAP_FIXED", "MAP_PRIVATE", "MAP_SHARED", "MCL_CURRENT",
  "MCL_FUTURE", "MS_ASYNC", "MS_INVALIDATE", "MS_ASYNC", "MS_INVALIDATE",
  "MS_SYNC", "PROT_EXEC", "PROT_NONE", "PROT_READ", "PROT_WRITE"
};
static const char *const mman_maybe[] =
{
  "mlock", "mlockall", "mmap", "mprotect", "msync", "munlock", "munlockall",
  "munmap", "shm_open", "shm_unlock"
};

/* <sys/stat.h>.  */
static const char *const stat_syms[] =
{
  "S_IRGRP", "S_IROTH", "S_IRUSR", "S_IRWXG", "S_IRWXO", "S_IRWXU",
  "S_ISBLK", "S_ISCHR", "S_ISDIR", "S_ISFIFO", "S_ISGID", "S_ISREG",
  "S_ISUID", "S_IWGRP", "S_IWOTH", "S_IWUSR", "S_IXGRP", "S_IXOTH",
  "S_IXUSR", "S_TYPEISMQ", "S_TYPEISSEM", "S_TYPEISSHM"
};
static const char *const stat_maybe[] =
{
  "chmod", "fchmod", "fstat", "mkdir", "mkfifo", "stat", "umask"
};

/* <sys/times.h>.  */
static const char *const times_syms[] =
{
};
static const char *const times_maybe[] =
{
  "times"
};

/* <sys/types.h>.  */
static const char *const types_syms[] =
{
};
static const char *const types_maybe[] =
{
};

/* <sys/utsname.h>.  */
static const char *const utsname_syms[] =
{
};
static const char *const utsname_maybe[] =
{
  "uname"
};

/* <sys/wait.h>.  */
static const char *const wait_syms[] =
{
  "WEXITSTATUS", "WIFEXITED", "WIFSIGNALED", "WIFSTOPPED", "WNOHANG",
  "WSTOPSIG", "WTERMSIG", "WUNTRACED"
};
static const char *const wait_maybe[] =
{
  "wait", "waitpid"
};

/* <termios.h>.  */
static const char *const termios_syms[] =
{
  "B0", "B110", "B1200", "B134", "B150", "B1800", "B19200", "B200", "B2400",
  "B300", "B38400", "B4800", "B50", "B600", "B75", "B9600", "BRKINT", "CLOCAL",
  "CREAD", "CS5", "CS6", "CS7", "CS8", "CSIZE", "CSTOPN", "ECHO", "ECHOE",
  "ECHOK", "ECHONL", "HUPCL", "ICANON", "ICRNL", "IEXTEN", "IGNBRK", "IGNCR",
  "IGNPAR", "INCLR", "INPCK", "ISIG", "ISTRIP", "IXOFF", "IXON", "NCCS",
  "NOFLSH", "OPOST", "PARENB", "PARMRK", "PARODD", "TCIFLUSH", "TCIOFF",
  "TCIOFLUSH", "TCOFLUSH", "TCOOFF", "TCOON", "TCSADRAIN", "TCSAFLUSH",
  "TCSANOW", "TOSTOP", "VEOF", "VEOL", "VERASE", "VINTR", "VKILL", "VMIN",
  "VQUIT", "VSTART", "VSTOP", "VSUSP", "VTIME"
};
static const char *const termios_maybe[] =
{
  "cfgetispeed", "cfgetospeed", "cfsetispeed", "cfsetospeed", "tcdrain",
  "tcflow", "tcflush", "tcgetattr", "tcsendbrk", "tcsetattr"
};

/* <time.h>.  */
static const char *const time_syms[] =
{
  "CLK_TCK", "CLOCKS_PER_SEC", "CLOCK_REALTIME", "NULL", "TIMER_ABSTIME"
};
static const char *const time_maybe[] =
{
  "asctime", "asctime_r", "clock", "clock_getres", "clock_gettime",
  "clock_settime", "ctime", "ctime_r", "difftime", "gmtime", "gmtime_r",
  "localtime", "localtime_r", "mktime", "nanosleep", "strftime", "time",
  "timer_create", "timer_delete", "timer_getoverrun", "timer_gettime",
  "timer_settime", "tzset"
};

/* <unistd.h>.  */
static const char *const unistd_syms[] =
{
  "F_OK", "NULL", "R_OK", "SEEK_CUR", "SEEK_END", "SEEK_SET", "STDERR_FILENO",
  "STDIN_FILENO", "STDOUT_FILENO", "W_OK", "X_OK",
  "_PC_ASYNC_IO", "_PC_CHOWN_RESTRICTED", "_PC_LINK_MAX", "_PC_MAX_CANON",
  "_PC_MAX_INPUT", "_PC_NAME_MAX", "_PC_NO_TRUNC", "_PC_PATH_MAX",
  "_PC_PIPE_BUF", "_PC_PRIO_IO", "_PC_SYNC_IO", "_PC_VDISABLE",
  "_SC_AIO_LISTIO_MAX", "_SC_AIO_MAX", "_SC_AIO_PRIO_DELTA_MAX",
  "_SC_ARG_MAX", "_SC_ASYNCHRONOUS_IO", "_SC_CHILD_MAX", "_SC_CLK_TCK",
  "_SC_DELAYTIMER_MAX", "_SC_FSYNC", "_SC_GETGR_R_SIZE_MAX",
  "_SC_GETPW_R_SIZE_MAX", "_SC_JOB_CONTROL", "_SC_LOGIN_NAME_MAX",
  "_SC_MAPPED_FILES", "_SC_MEMLOCK", "_SC_MEMLOCK_RANGE",
  "_SC_MEMORY_PROTECTION", "_SC_MESSAGE_PASSING", "_SC_MQ_OPEN_MAX",
  "_SC_MQ_PRIO_MAX", "_SC_NGROUPS_MAX", "_SC_OPEN_MAX", "_SC_PAGESIZE",
  "_SC_PRIORITIZED_IO", "_SC_PRIORITY_SCHEDULING", "_SC_REALTIME_SIGNALS",
  "_SC_RTSIG_MAX", "_SC_SAVED_IDS", "_SC_SEMAPHORES", "_SC_SEM_NSEMS_MAX",
  "_SC_SEM_VALUE_MAX", "_SC_SHARED_MEMORY_OBJECTS", "_SC_SIGQUEUE_MAX",
  "_SC_STREAM_MAX", "_SC_SYNCHRONIZED_IO", "_SC_THREADS",
  "_SC_THREAD_ATTR_STACKADDR", "_SC_THREAD_ATTR_STACKSIZE",
  "_SC_THREAD_DESTRUCTOR_ITERATIONS", "_SC_THREAD_PRIO_INHERIT",
  "_SC_THREAD_PRIORITY_SCHEDULING", "_SC_THREAD_PRIO_PROTECT",
  "_SC_THREAD_PROCESS_SHARED", "_SC_THREAD_SAFE_FUNCTIONS",
  "_SC_THREAD_STACK_MIN", "_SC_THREAD_THREADS_MAX", "_SC_TIMERS",
  "_SC_TIMER_MAX", "_SC_TTY_NAME_MAX", "_SC_TZNAME_MAX", "_SC_VERSION"
};
static const char *const unistd_maybe[] =
{
  "_POSIX_ASYNCHRONOUS_IO", "_POSIX_ASYNC_IO", "_POSIX_CHOWN_RESTRICTED",
  "_POSIX_FSYNC", "_POSIX_JOB_CONTROL", "_POSIX_MAPPED_FILES",
  "_POSIX_MEMLOCK", "_POSIX_MEMLOCK_RANGE", "_MEMORY_PROTECTION",
  "_POSIX_MESSAGE_PASSING", "_POSIX_NO_TRUNC", "_POSIX_PRIORITIZED_IO",
  "_POSIX_PRIORITY_SCHEDULING", "_POSIX_PRIO_IO", "_POSIX_REATIME_SIGNALS",
  "_POSIX_SAVED_IDS", "_POSIX_SEMAPHORES", "_POSIX_SHARED_MEMORY_OBJECTS",
  "_POSIX_SYNCHRONIZED_IO", "_POSIX_SYNC_IO", "_POSIX_THREADS",
  "_POSIX_THREAD_ATTR_STACKADDR", "_POSIX_THREAD_ATTR_STACKSIZE",
  "_POSIX_THREAD_PRIO_INHERIT", "_POSIX_THREAD_PRIO_PROTECT",
  "_POSIX_THREAD_PROCESS_SHARED", "_POSIX_THREAD_SAFE_FUNCTIONS",
  "_POSIX_THREAD_PRIORITY_SCHEDULING", "_POSIX_TIMERS",
  "_POSIX_VDISABLE", "_POSIX_VERSION",
  "_exit", "access", "alarm", "chdir", "chown", "close", "ctermid", "cuserid",
  "dup2", "dup", "execl", "execle", "execlp", "execv", "execve", "execvp",
  "fdatasync", "fork", "fpathconf", "fsync", "ftruncate", "getcwd", "getegid",
  "geteuid", "getgid", "getgroups", "getlogin", "getlogin_r", "getpgrp",
  "getpid", "getppid", "getuid", "isatty", "link", "lseek", "pathconf",
  "pause", "pipe", "read", "rmdir", "setgid", "setgpid", "setsid", "setuid",
  "sleep", "sleep", "sysconf", "tcgetpgrp", "tcsetpgrp", "ttyname",
  "ttyname_r", "unlink", "write"
};

/* <utime.h>.  */
static const char *const utime_syms[] =
{
};
static const char *const utime_maybe[] =
{
  "utime"
};


static struct header
{
  const char *name;
  const char *const *syms;
  size_t nsyms;
  const char *const *maybe;
  size_t nmaybe;
  const char *subset;
} headers[] =
{
#define H(n) \
  { #n ".h", n##_syms, sizeof (n##_syms) / sizeof (n##_syms[0]), \
    n##_maybe, sizeof (n##_maybe) / sizeof (n##_maybe[0]), NULL }
#define Hc(n, s) \
  { #n ".h", n##_syms, sizeof (n##_syms) / sizeof (n##_syms[0]), \
    n##_maybe, sizeof (n##_maybe) / sizeof (n##_maybe[0]), s }
#define Hs(n) \
  { "sys/" #n ".h", n##_syms, sizeof (n##_syms) / sizeof (n##_syms[0]), \
    n##_maybe, sizeof (n##_maybe) / sizeof (n##_maybe[0]), NULL }
  H(aio),
  H(assert),
  H(ctype),
  H(dirent),
  H(errno),
  H(fcntl),
  H(float),
  H(grp),
  H(limits),
  H(locale),
  H(math),
  Hc(mqueue, "_POSIX_MESSAGE_PASSING"),
  H(pthread),
  H(pwd),
  H(sched),
  H(semaphore),
  H(setjmp),
  H(signal),
  H(stdarg),
  H(stddef),
  H(stdio),
  H(stdlib),
  H(string),
  Hs(mman),
  Hs(stat),
  Hs(times),
  Hs(types),
  Hs(utsname),
  Hs(wait),
  H(termios),
  H(time),
  H(unistd),
  H(utime)
};

#define NUMBER_OF_HEADERS              (sizeof headers / sizeof *headers)


/* Format string to build command to invoke compiler.  */
static const char fmt[] = "\
echo \"#include <%s>\" |\
%s -E -dM -D_POSIX_SOURCE -D_LIBC %s \
-isystem `%s --print-prog-name=include` - > %s";

static const char testfmt[] = "\
echo \"#include <unistd.h>\n#ifndef %s\n#error not defined\n#endif\n\" |\
%s -E -dM -D_POSIX_SOURCE -D_LIBC %s \
-isystem `%s --print-prog-name=include` - 2> /dev/null > %s";


/* The compiler we use (given on the command line).  */
const char *CC;
/* The -I parameters for CC to find all headers.  */
const char *INC;

static char *xstrndup (const char *, size_t);
static const char **get_null_defines (void);
static int check_header (const struct header *, const char **);

int
main (int argc, char *argv[])
{
  int h;
  int result = 0;
  const char **ignore_list;

  CC = argc > 1 ? argv[1] : "gcc";
  INC = argc > 2 ? argv[2] : "";

  if (system (NULL) == 0)
    {
      puts ("Sorry, no command processor.");
      return EXIT_FAILURE;
    }

  /* First get list of symbols which are defined by the compiler.  */
  ignore_list = get_null_defines ();

  fputs ("Tested files:\n", stdout);

  for (h = 0; h < NUMBER_OF_HEADERS; ++h)
    result |= check_header (&headers[h], ignore_list);

  /* The test suite should return errors but for now this is not
     practical.  Give a warning and ask the user to correct the bugs.  */
  return result;
}


static char *
xstrndup (const char *s, size_t n)
{
  size_t len = n;
  char *new = malloc (len + 1);

  if (new == NULL)
    return NULL;

  new[len] = '\0';
  return memcpy (new, s, len);
}


static const char **
get_null_defines (void)
{
  char line[BUFSIZ], *command;
  char **result = NULL;
  size_t result_len = 0;
  size_t result_max = 0;
  FILE *input;
  int first = 1;

  command = malloc (sizeof fmt + sizeof "/dev/null" + 2 * strlen (CC)
		    + strlen (INC) + strlen (TMPFILE));

  if (command == NULL)
    {
      puts ("No more memory.");
      exit (1);
    }

  sprintf (command, fmt, "/dev/null", CC, INC, CC, TMPFILE);

  if (system (command))
    {
      puts ("system() returned nonzero");
      return NULL;
    }
  free (command);
  input = fopen (TMPFILE, "r");

  if (input == NULL)
    {
      printf ("Could not read %s: ", TMPFILE);
      perror (NULL);
      return NULL;
    }

  while (fgets (line, sizeof line, input) != NULL)
    {
      char *start;
      if (strlen (line) < 9 || line[7] != ' ')
	{ /* "#define A" */
	  printf ("Malformed input, expected '#define MACRO'\ngot '%s'\n",
		  line);
	  continue;
	}
      if (line[8] == '_')
	/* It's a safe identifier.  */
	continue;
      if (result_len == result_max)
	{
	  result_max += 10;
	  result = realloc (result, result_max * sizeof (char **));
	  if (result == NULL)
	    {
	      puts ("No more memory.");
	      exit (1);
	    }
	}
      start = &line[8];
      result[result_len++] = xstrndup (start, strcspn (start, " ("));

      if (first)
	{
	  fputs ("The following identifiers will be ignored since the compiler defines them\nby default:\n", stdout);
	  first = 0;
	}
      puts (result[result_len - 1]);
    }
  if (result_len == result_max)
    {
      result_max += 1;
      result = realloc (result, result_max * sizeof (char **));
      if (result == NULL)
	{
	  puts ("No more memory.");
	  exit (1);
	}
    }
  result[result_len] = NULL;
  fclose (input);
  remove (TMPFILE);

  return (const char **) result;
}


static int
check_header (const struct header *header, const char **except)
{
  char line[BUFSIZ], *command;
  FILE *input;
  int result = 0;
  int found[header->nsyms];
  int i;

  memset (found, '\0', header->nsyms * sizeof (int));
  command = alloca (sizeof fmt + strlen (header->name) + 2 * strlen (CC)
		    + strlen (INC) + strlen (TMPFILE));


  if (command == NULL)
    {
      puts ("No more memory.");
      exit (1);
    }

  printf ("=== %s ===\n", header->name);
  sprintf (command, fmt, header->name, CC, INC, CC, TMPFILE);

  /* First see whether this subset is supported at all.  */
  if (header->subset != NULL)
    {
      sprintf (line, testfmt, header->subset, CC, INC, CC, TMPFILE);
      if (system (line))
	{
	  printf ("!! not available\n");
	  return 0;
	}
    }

  if (system (command))
    {
      puts ("system() returned nonzero");
      result = 1;
    }
  input = fopen (TMPFILE, "r");

  if (input == NULL)
    {
      printf ("Could not read %s: ", TMPFILE);
      perror (NULL);
      return 1;
    }

  while (fgets (line, sizeof line, input) != NULL)
    {
      const char **ignore;
      if (strlen (line) < 9 || line[7] != ' ')
	{ /* "#define A" */
	  printf ("Malformed input, expected '#define MACRO'\ngot '%s'\n",
		  line);
	  result = 1;
	  continue;
	}

      /* Find next char after the macro identifier; this can be either
	 a space or an open parenthesis.  */
      line[8 + strcspn (&line[8], " (")] = '\0';

      /* Now check whether it's one of the required macros.  */
      for (i = 0; i < header->nsyms; ++i)
	if (!strcmp (&line[8], header->syms[i]))
	  break;
      if (i < header->nsyms)
	{
	  found[i] = 1;
	  continue;
	}

      /* Symbols starting with "_" are ok.  */
      if (line[8] == '_')
	continue;

      /* Maybe one of the symbols which are always defined.  */
      for (ignore = except; *ignore != NULL; ++ignore)
	if (! strcmp (&line[8], *ignore))
	  break;
      if (*ignore != NULL)
	continue;

      /* Otherwise the symbol better should match one of the following.  */
      for (i = 0; i < header->nmaybe; ++i)
	if (fnmatch (header->maybe[i], &line[8], 0) == 0)
	  break;
      if (i < header->nmaybe)
	continue;

      printf ("*  invalid macro `%s'\n", &line[8]);
      result |= 1;
    }
  fclose (input);
  remove (TMPFILE);

  for (i = 0; i < header->nsyms; ++i)
    if (found[i] == 0)
      printf ("** macro `%s' not defined\n", header->syms[i]);

  return result;
}