From b18129d8f21bbf637d56c43440292adf419827a7 Mon Sep 17 00:00:00 2001
From: Andre Maroneze <andre.oliveiramaroneze@cea.fr>
Date: Tue, 6 Aug 2019 08:54:44 +0200
Subject: [PATCH] [Libc] move declarations for better POSIX compliance; minor
 fix

---
 share/libc/arpa/inet.h  |  6 ++++++
 share/libc/getopt.h     |  9 +--------
 share/libc/netinet/in.h |  6 ++++++
 share/libc/sys/select.h |  2 +-
 share/libc/sys/socket.h |  2 +-
 share/libc/sys/time.h   |  5 +++++
 share/libc/unistd.h     | 13 +++++++++++--
 7 files changed, 31 insertions(+), 12 deletions(-)

diff --git a/share/libc/arpa/inet.h b/share/libc/arpa/inet.h
index 8e9cf21bdc5..773e7d87a2e 100644
--- a/share/libc/arpa/inet.h
+++ b/share/libc/arpa/inet.h
@@ -23,4 +23,10 @@
 // According to POSIX, definitions from both arpa/inet.h and netinet/in.h
 // may be exported by one another, so everything is defined in a common file.
 
+#ifndef __FC_ARPA_INET
+#define __FC_ARPA_INET
+#include "features.h"
+__PUSH_FC_STDLIB
 #include "../__fc_inet.h"
+__POP_FC_STDLIB
+#endif
diff --git a/share/libc/getopt.h b/share/libc/getopt.h
index 6788b728779..b7f2116363d 100644
--- a/share/libc/getopt.h
+++ b/share/libc/getopt.h
@@ -27,14 +27,7 @@ __PUSH_FC_STDLIB
 
 __BEGIN_DECLS
 
-extern char *optarg;
-extern int optind, opterr, optopt;
-
-/*@ 
-  assigns \result, *optarg, optind, opterr, optopt 
-             \from argc, argv[0..argc-1], optstring[0..];
- */
-extern int getopt(int argc, char * const argv[], const char *optstring);
+#include "unistd.h"
 
 /* GNU specific */
 struct option
diff --git a/share/libc/netinet/in.h b/share/libc/netinet/in.h
index 8e9cf21bdc5..e582651699c 100644
--- a/share/libc/netinet/in.h
+++ b/share/libc/netinet/in.h
@@ -23,4 +23,10 @@
 // According to POSIX, definitions from both arpa/inet.h and netinet/in.h
 // may be exported by one another, so everything is defined in a common file.
 
+#ifndef __FC_NETINET_IN
+#define __FC_NETINET_IN
+#include "features.h"
+__PUSH_FC_STDLIB
 #include "../__fc_inet.h"
+__POP_FC_STDLIB
+#endif
diff --git a/share/libc/sys/select.h b/share/libc/sys/select.h
index 8e1608d267e..dc854567f32 100644
--- a/share/libc/sys/select.h
+++ b/share/libc/sys/select.h
@@ -26,7 +26,7 @@
 __PUSH_FC_STDLIB
 __BEGIN_DECLS
 
-#include "__fc_select.h"
+#include "../__fc_select.h"
 
 __END_DECLS
 __POP_FC_STDLIB
diff --git a/share/libc/sys/socket.h b/share/libc/sys/socket.h
index 20b339c5ed8..c8d2a9ae4c3 100644
--- a/share/libc/sys/socket.h
+++ b/share/libc/sys/socket.h
@@ -46,7 +46,7 @@ struct sockaddr_storage {
   sa_family_t   ss_family;
 };
 
-#include "../__fc_define_iovec.h"
+#include "sys/uio.h"
 
 struct cmsghdr {
   socklen_t  cmsg_len;
diff --git a/share/libc/sys/time.h b/share/libc/sys/time.h
index b860763f4d3..426f6376887 100644
--- a/share/libc/sys/time.h
+++ b/share/libc/sys/time.h
@@ -205,6 +205,11 @@ extern int timerisset(struct timeval *tvp);
 #define timercmp(a, b, _CMP) _timercmp(a, b)
 extern int _timercmp(struct timeval *a, struct timeval *b);
 
+// From POSIX, and for better compatibility with existing code bases:
+// "Inclusion of the <sys/time.h> header may make visible all symbols
+// from the <sys/select.h> header."
+#include "select.h"
+
 __END_DECLS
 __POP_FC_STDLIB
 #endif
diff --git a/share/libc/unistd.h b/share/libc/unistd.h
index 9ee67070df7..e0c2a6817d3 100644
--- a/share/libc/unistd.h
+++ b/share/libc/unistd.h
@@ -34,7 +34,7 @@ __PUSH_FC_STDLIB
 #include "__fc_define_pid_t.h"
 #include "__fc_define_useconds_t.h"
 #include "__fc_define_intptr_t.h"
-#include "__fc_select.h"
+
 
 
 #include "limits.h"
@@ -1096,7 +1096,7 @@ extern int          tcsetpgrp(int, pid_t);
 extern int          truncate(const char *, off_t);
 
 extern volatile char __fc_ttyname[TTY_NAME_MAX];
-extern char *__fc_p_ttyname = __fc_ttyname;
+volatile char *__fc_p_ttyname = __fc_ttyname;
 
 /*@
   // missing: may assign to errno: EBADF, ENOTTY
@@ -1188,6 +1188,15 @@ int getresgid(gid_t *rgid, gid_t *egid, gid_t *sgid);
  */
 int setresgid(gid_t rgid, gid_t egid, gid_t sgid);
 
+extern char *optarg;
+extern int optind, opterr, optopt;
+
+/*@
+  assigns \result, *optarg, optind, opterr, optopt
+             \from argc, argv[0..argc-1], optstring[0..];
+ */
+extern int getopt(int argc, char * const argv[], const char *optstring);
+
 
 __END_DECLS
 
-- 
GitLab