support _BSD_SOURCE feature test macro

patch by Isaac Dunham. matched closely (maybe not exact) to glibc's
idea of what _BSD_SOURCE should make visible.
diff --git a/include/ctype.h b/include/ctype.h
index a85e907..8ceaa9f 100644
--- a/include/ctype.h
+++ b/include/ctype.h
@@ -30,7 +30,8 @@
 
 
 #if defined(_POSIX_SOURCE) || defined(_POSIX_C_SOURCE) \
- || defined(_XOPEN_SOURCE) || defined(_GNU_SOURCE)
+ || defined(_XOPEN_SOURCE) || defined(_GNU_SOURCE) \
+ || defined(_BSD_SOURCE)
 
 #define __NEED_locale_t
 #include <bits/alltypes.h>
diff --git a/include/dirent.h b/include/dirent.h
index d0a6b44..8162356 100644
--- a/include/dirent.h
+++ b/include/dirent.h
@@ -36,7 +36,7 @@
 int alphasort(const struct dirent **, const struct dirent **);
 int scandir(const char *, struct dirent ***, int (*)(const struct dirent *), int (*)(const struct dirent **, const struct dirent **));
 
-#ifdef _GNU_SOURCE
+#if defined(_GNU_SOURCE) || defined(_BSD_SOURCE)
 #define DT_UNKNOWN 0
 #define DT_FIFO 1
 #define DT_CHR 2
diff --git a/include/fcntl.h b/include/fcntl.h
index 63f1beb..f826a5e 100644
--- a/include/fcntl.h
+++ b/include/fcntl.h
@@ -80,12 +80,24 @@
 #define S_IRWXO 0007
 #endif
 
-#ifdef _GNU_SOURCE
+#if defined(_GNU_SOURCE) || defined(_BSD_SOURCE)
 #define FAPPEND O_APPEND
 #define FFSYNC O_FSYNC
 #define FASYNC O_ASYNC
 #define FNONBLOCK O_NONBLOCK
 #define FNDELAY O_NDELAY
+
+#ifndef F_OK
+#define F_OK 0
+#define R_OK 4
+#define W_OK 2
+#define X_OK 1
+#define F_ULOCK 0
+#define F_LOCK  1
+#define F_TLOCK 2
+#define F_TEST  3
+int lockf(int, int, off_t);
+#endif
 #endif
 
 #ifdef _LARGEFILE64_SOURCE
diff --git a/include/grp.h b/include/grp.h
index 5185185..0a55c62 100644
--- a/include/grp.h
+++ b/include/grp.h
@@ -28,7 +28,7 @@
 void           endgrent(void);
 void           setgrent(void);
 
-#ifdef _GNU_SOURCE
+#if defined(_GNU_SOURCE) || defined(_BSD_SOURCE)
 int getgrouplist(const char *, gid_t, gid_t *, int *);
 int setgroups(size_t, const gid_t *);
 int initgroups(const char *, gid_t);
diff --git a/include/math.h b/include/math.h
index 61738e6..d732648 100644
--- a/include/math.h
+++ b/include/math.h
@@ -330,8 +330,7 @@
 float       truncf(float);
 long double truncl(long double);
 
-#if defined(_XOPEN_SOURCE) || defined(_GNU_SOURCE)
-#define MAXFLOAT        3.40282347e+38F
+#if defined(_XOPEN_SOURCE) || defined(_GNU_SOURCE) || defined(_BSD_SOURCE)
 #define M_E             2.7182818284590452354   /* e */
 #define M_LOG2E         1.4426950408889634074   /* log_2 e */
 #define M_LOG10E        0.43429448190325182765  /* log_10 e */
@@ -345,7 +344,13 @@
 #define M_2_SQRTPI      1.12837916709551257390  /* 2/sqrt(pi) */
 #define M_SQRT2         1.41421356237309504880  /* sqrt(2) */
 #define M_SQRT1_2       0.70710678118654752440  /* 1/sqrt(2) */
+#endif
 
+#if defined(_XOPEN_SOURCE)
+#define MAXFLOAT        3.40282347e+38F
+#endif
+
+#if defined(_XOPEN_SOURCE) || defined(_GNU_SOURCE)
 extern int signgam;
 
 double      j0(double);
@@ -358,6 +363,7 @@
 #endif
 
 #ifdef _GNU_SOURCE
+#define HUGE        3.40282347e+38F
 double      scalb(double, double);
 float       scalbf(float, float);
 long double scalbl(long double, long double);
diff --git a/include/net/if.h b/include/net/if.h
index 00c763a..e862c7c 100644
--- a/include/net/if.h
+++ b/include/net/if.h
@@ -21,7 +21,7 @@
 
 
 
-#ifdef _GNU_SOURCE
+#if defined(_GNU_SOURCE) || defined(_BSD_SOURCE)
 
 #include <sys/socket.h>
 
diff --git a/include/netdb.h b/include/netdb.h
index 33b7a0a..82c78dc 100644
--- a/include/netdb.h
+++ b/include/netdb.h
@@ -5,7 +5,7 @@
 extern "C" {
 #endif
 
-#ifdef _GNU_SOURCE
+#if defined(_GNU_SOURCE) || defined(_BSD_SOURCE)
 #define __NEED_size_t
 #endif
 
@@ -118,7 +118,7 @@
 struct protoent *getprotobyname (const char *);
 struct protoent *getprotobynumber (int);
 
-#ifdef _GNU_SOURCE
+#if defined(_GNU_SOURCE) || defined(_BSD_SOURCE)
 const char *hstrerror(int);
 struct hostent *gethostbyname (const char *);
 int gethostbyname_r(const char *, struct hostent *, char *, size_t, struct hostent **, int *);
diff --git a/include/netinet/tcp.h b/include/netinet/tcp.h
index c8a1a4b..797ce68 100644
--- a/include/netinet/tcp.h
+++ b/include/netinet/tcp.h
@@ -2,5 +2,22 @@
 #define _NETINET_TCP_H
 
 #define TCP_NODELAY 1
+#if defined(_GNU_SOURCE) || defined(_BSD_SOURCE)
+#include <sys/types.h>
+#include <sys/socket.h>
+#define TCP_MAXSEG	 2
+#define TCP_CORK	 3
+#define TCP_KEEPIDLE	 4
+#define TCP_KEEPINTVL	 5
+#define TCP_KEEPCNT	 6
+#define TCP_SYNCNT	 7
+#define TCP_LINGER2	 8
+#define TCP_DEFER_ACCEPT 9
+#define TCP_WINDOW_CLAMP 10
+#define TCP_INFO	 11
+#define	TCP_QUICKACK	 12
+#define TCP_CONGESTION	 13
+#define TCP_MD5SIG	 14
+#endif
 
 #endif
diff --git a/include/setjmp.h b/include/setjmp.h
index b024c44..88fb950 100644
--- a/include/setjmp.h
+++ b/include/setjmp.h
@@ -9,7 +9,8 @@
 
 
 #if defined(_POSIX_SOURCE) || defined(_POSIX_C_SOURCE) \
- || defined(_XOPEN_SOURCE) || defined(_GNU_SOURCE)
+ || defined(_XOPEN_SOURCE) || defined(_GNU_SOURCE) \
+ || defined(_BSD_SOURCE)
 typedef unsigned long sigjmp_buf[(128+sizeof(jmp_buf))/sizeof(long)];
 #ifdef _GNU_SOURCE
 #define jmp_buf sigjmp_buf
@@ -19,7 +20,8 @@
 #endif
 
 
-#if defined(_XOPEN_SOURCE) || defined(_GNU_SOURCE)
+#if defined(_XOPEN_SOURCE) || defined(_GNU_SOURCE) \
+ || defined(_BSD_SOURCE)
 int _setjmp (jmp_buf);
 void _longjmp (jmp_buf, int);
 #endif
diff --git a/include/signal.h b/include/signal.h
index 38f369b..616801b 100644
--- a/include/signal.h
+++ b/include/signal.h
@@ -6,7 +6,8 @@
 #endif
 
 #if defined(_POSIX_SOURCE) || defined(_POSIX_C_SOURCE) \
- || defined(_XOPEN_SOURCE) || defined(_GNU_SOURCE)
+ || defined(_XOPEN_SOURCE) || defined(_GNU_SOURCE) \
+ || defined(_BSD_SOURCE)
 
 #ifdef _GNU_SOURCE
 #define __siginfo siginfo
@@ -204,9 +205,16 @@
 #define SIGSTKSZ 8192
 #endif
 
+#if defined(_GNU_SOURCE) || defined(_BSD_SOURCE)
+void (*bsd_signal(int, void (*)(int)))(int);
+#endif
+
+#ifdef _BSD_SOURCE
+typedef void (*sig_t)(int);
+#endif
+
 #ifdef _GNU_SOURCE
 typedef void (*sighandler_t)(int);
-void (*bsd_signal(int, void (*)(int)))(int);
 int sigisemptyset(const sigset_t *);
 #define SA_NOMASK SA_NODEFER
 #define SA_ONESHOT SA_RESETHAND
diff --git a/include/stdio.h b/include/stdio.h
index 355f425..e4740eb 100644
--- a/include/stdio.h
+++ b/include/stdio.h
@@ -10,7 +10,8 @@
 #define __NEED_size_t
 
 #if defined(_POSIX_SOURCE) || defined(_POSIX_C_SOURCE) \
- || defined(_XOPEN_SOURCE) || defined(_GNU_SOURCE)
+ || defined(_XOPEN_SOURCE) || defined(_GNU_SOURCE) \
+ || defined(_BSD_SOURCE)
 #define __NEED_ssize_t
 #define __NEED_off_t
 #endif
@@ -120,7 +121,8 @@
 FILE *tmpfile(void);
 
 #if defined(_POSIX_SOURCE) || defined(_POSIX_C_SOURCE) \
- || defined(_XOPEN_SOURCE) || defined(_GNU_SOURCE)
+ || defined(_XOPEN_SOURCE) || defined(_GNU_SOURCE) \
+ || defined(_BSD_SOURCE)
 FILE *fmemopen(void *, size_t, const char *);
 FILE *open_memstream(char **, size_t *);
 FILE *fdopen(int, const char *);
@@ -146,12 +148,13 @@
 #endif
 
 
-#if defined(_XOPEN_SOURCE) || defined(_GNU_SOURCE)
+#if defined(_XOPEN_SOURCE) || defined(_GNU_SOURCE) \
+ || defined(_BSD_SOURCE)
 #define P_tmpdir "/tmp"
 char *tempnam(const char *, const char *);
 #endif
 
-#if defined(_GNU_SOURCE)
+#if defined(_GNU_SOURCE) || defined(_BSD_SOURCE)
 #define L_cuserid 20
 char *cuserid(char *);
 #undef off64_t
diff --git a/include/stdlib.h b/include/stdlib.h
index 11331d9..644325e 100644
--- a/include/stdlib.h
+++ b/include/stdlib.h
@@ -78,7 +78,8 @@
 
 
 #if defined(_POSIX_SOURCE) || defined(_POSIX_C_SOURCE) \
- || defined(_XOPEN_SOURCE) || defined(_GNU_SOURCE)
+ || defined(_XOPEN_SOURCE) || defined(_GNU_SOURCE) \
+ || defined(_BSD_SOURCE)
 
 #ifndef WEXITSTATUS
 #define WEXITSTATUS(s) (((s) & 0xff00) >> 8)
@@ -102,20 +103,24 @@
 #endif
 
 
+#if defined(_XOPEN_SOURCE) || defined(_GNU_SOURCE) \
+ || defined(_BSD_SOURCE)
+char *realpath (const char *, char *);
+long int random (void);
+void srandom (unsigned int);
+char *initstate (unsigned int, char *, size_t);
+char *setstate (char *);
+#endif
+
 #if defined(_XOPEN_SOURCE) || defined(_GNU_SOURCE)
 int putenv (char *);
 int posix_openpt (int);
 int grantpt (int);
 int unlockpt (int);
 char *ptsname (int);
-char *realpath (const char *, char *);
 char *l64a (long);
 long a64l (const char *);
 void setkey (const char *);
-long int random (void);
-void srandom (unsigned int);
-char *initstate (unsigned int, char *, size_t);
-char *setstate (char *);
 double drand48 (void);
 double erand48 (unsigned short [3]);
 long int lrand48 (void);
@@ -127,11 +132,14 @@
 void lcong48 (unsigned short [7]);
 #endif
 
-#if defined(_GNU_SOURCE)
+#if defined(_GNU_SOURCE) || defined(_BSD_SOURCE)
 #include <alloca.h>
 char *mktemp (char *);
 void *valloc (size_t);
 void *memalign(size_t, size_t);
+#endif
+
+#ifdef _GNU_SOURCE
 int clearenv(void);
 int ptsname_r(int, char *, size_t);
 char *ecvt(double, int, int *, int *);
diff --git a/include/string.h b/include/string.h
index 8cf0ee9..24cb1ca 100644
--- a/include/string.h
+++ b/include/string.h
@@ -14,7 +14,8 @@
 
 #define __NEED_size_t
 #if defined(_POSIX_SOURCE) || defined(_POSIX_C_SOURCE) \
- || defined(_XOPEN_SOURCE) || defined(_GNU_SOURCE)
+ || defined(_XOPEN_SOURCE) || defined(_GNU_SOURCE) \
+ || defined(_BSD_SOURCE)
 #define __NEED_locale_t
 #endif
 
@@ -51,9 +52,13 @@
 
 char *strerror (int);
 
+#if defined(_BSD_SOURCE) || defined(_GNU_SOURCE)
+#include <strings.h>
+#endif
 
 #if defined(_POSIX_SOURCE) || defined(_POSIX_C_SOURCE) \
- || defined(_XOPEN_SOURCE) || defined(_GNU_SOURCE)
+ || defined(_XOPEN_SOURCE) || defined(_GNU_SOURCE) \
+ || defined(_BSD_SOURCE)
 char *strtok_r (char *, const char *, char **);
 int strerror_r (int, char *, size_t);
 char *stpcpy(char *, const char *);
@@ -67,7 +72,8 @@
 size_t strxfrm_l (char *, const char *, size_t, locale_t);
 #endif
 
-#if defined(_XOPEN_SOURCE) || defined(_GNU_SOURCE)
+#if defined(_XOPEN_SOURCE) || defined(_GNU_SOURCE) \
+ || defined(_BSD_SOURCE)
 void *memccpy (void *, const void *, int, size_t);
 #endif
 
@@ -77,9 +83,10 @@
 #endif
 
 #ifdef _GNU_SOURCE
+#define	strdupa(x)	strcpy(alloca(strlen(x)+1),x)
 int strverscmp (const char *, const char *);
-int strcasecmp (const char *, const char *);
-int strncasecmp (const char *, const char *, size_t);
+int strcasecmp_l (const char *, const char *, locale_t);
+int strncasecmp_l (const char *, const char *, size_t, locale_t);
 char *strchrnul(const char *, int);
 char *strcasestr(const char *, const char *);
 char *strsep(char **, const char *);
diff --git a/include/sys/mman.h b/include/sys/mman.h
index a48c2b4..a45f11d 100644
--- a/include/sys/mman.h
+++ b/include/sys/mman.h
@@ -27,6 +27,9 @@
 
 #ifdef _GNU_SOURCE
 void *mremap (void *, size_t, size_t, int, ...);
+#endif
+
+#if defined(_GNU_SOURCE) || defined(_BSD_SOURCE)
 int madvise (void *, size_t, int);
 #endif
 
diff --git a/include/sys/stat.h b/include/sys/stat.h
index 5d50ae7..14a5424 100644
--- a/include/sys/stat.h
+++ b/include/sys/stat.h
@@ -90,7 +90,7 @@
 int lchmod(const char *, mode_t);
 #endif
 
-#ifdef _GNU_SOURCE
+#if defined(_GNU_SOURCE) || defined(_BSD_SOURCE)
 #define S_IREAD S_IRUSR
 #define S_IWRITE S_IWUSR
 #define S_IEXEC S_IXUSR
diff --git a/include/sys/time.h b/include/sys/time.h
index fa98944..144dd23 100644
--- a/include/sys/time.h
+++ b/include/sys/time.h
@@ -8,7 +8,8 @@
 
 int gettimeofday (struct timeval *, void *);
 
-#if defined(_XOPEN_SOURCE) || defined(_GNU_SOURCE)
+#if defined(_XOPEN_SOURCE) || defined(_GNU_SOURCE) \
+ || defined(_BSD_SOURCE)
 
 #define ITIMER_REAL    0
 #define ITIMER_VIRTUAL 1
@@ -26,7 +27,7 @@
 
 #endif
 
-#ifdef _GNU_SOURCE
+#if defined(_GNU_SOURCE) || defined(_BSD_SOURCE)
 int futimes(int, const struct timeval [2]);
 int lutimes(const char *, const struct timeval [2]);
 int settimeofday (const struct timeval *, void *);
diff --git a/include/sys/types.h b/include/sys/types.h
index b9b65f6..4b1e374 100644
--- a/include/sys/types.h
+++ b/include/sys/types.h
@@ -58,7 +58,7 @@
 typedef __uint32_t u_int32_t;
 typedef __uint64_t u_int64_t;
 
-#ifdef _GNU_SOURCE
+#if defined(_GNU_SOURCE) || defined(_BSD_SOURCE)
 typedef char *caddr_t;
 typedef unsigned char u_char;
 typedef unsigned short u_short, ushort;
diff --git a/include/sys/wait.h b/include/sys/wait.h
index e73594a..5e3012b 100644
--- a/include/sys/wait.h
+++ b/include/sys/wait.h
@@ -17,7 +17,7 @@
 int waitid (idtype_t, id_t, siginfo_t *, int);
 pid_t waitpid (pid_t, int *, int );
 
-#ifdef _GNU_SOURCE
+#if defined(_GNU_SOURCE) || defined(_BSD_SOURCE)
 #include <sys/resource.h>
 pid_t wait3 (int *, int, struct rusage *);
 pid_t wait4 (pid_t, int *, int, struct rusage *);
diff --git a/include/syslog.h b/include/syslog.h
index e0d86c0..278945f 100644
--- a/include/syslog.h
+++ b/include/syslog.h
@@ -59,7 +59,8 @@
 int setlogmask (int);
 void syslog (int, const char *, ...);
 
-#if defined(_GNU_SOURCE)
+#if defined(_GNU_SOURCE) || defined(_BSD_SOURCE)
+#define _PATH_LOG "/dev/log"
 #define __NEED_va_list
 #include <bits/alltypes.h>
 void vsyslog (int, const char *, va_list);
diff --git a/include/termios.h b/include/termios.h
index 7690883..1041759 100644
--- a/include/termios.h
+++ b/include/termios.h
@@ -32,7 +32,7 @@
 
 pid_t tcgetsid (int);
 
-#ifdef _GNU_SOURCE
+#if defined(_GNU_SOURCE) || defined(_BSD_SOURCE)
 void cfmakeraw(struct termios *);
 int cfsetspeed(struct termios *, speed_t);
 #endif
diff --git a/include/time.h b/include/time.h
index 98d0111..067b519 100644
--- a/include/time.h
+++ b/include/time.h
@@ -18,7 +18,8 @@
 #define __NEED_clock_t
 
 #if defined(_POSIX_SOURCE) || defined(_POSIX_C_SOURCE) \
- || defined(_XOPEN_SOURCE) || defined(_GNU_SOURCE)
+ || defined(_XOPEN_SOURCE) || defined(_GNU_SOURCE) \
+ || defined(_BSD_SOURCE)
 #define __NEED_struct_timespec
 #define __NEED_clockid_t
 #define __NEED_timer_t
@@ -57,7 +58,8 @@
 
 
 #if defined(_POSIX_SOURCE) || defined(_POSIX_C_SOURCE) \
- || defined(_XOPEN_SOURCE) || defined(_GNU_SOURCE)
+ || defined(_XOPEN_SOURCE) || defined(_GNU_SOURCE) \
+ || defined(_BSD_SOURCE)
 
 size_t strftime_l (char *, size_t, const char *, const struct tm *, locale_t);
 
diff --git a/include/unistd.h b/include/unistd.h
index b1a84d7..0112276 100644
--- a/include/unistd.h
+++ b/include/unistd.h
@@ -16,6 +16,12 @@
 #define SEEK_CUR 1
 #define SEEK_END 2
 
+#if defined(_BSD_SOURCE) && !defined(L_SET)
+#define L_SET	SEEK_SET
+#define L_INCR	SEEK_CUR
+#define L_XTND	SEEK_END
+#endif
+
 #undef NULL
 #ifdef __cplusplus
 #define NULL 0
@@ -63,10 +69,13 @@
 int truncate(const char *, off_t);
 int ftruncate(int, off_t);
 
+#ifndef F_OK
 #define F_OK 0
 #define R_OK 4
 #define W_OK 2
 #define X_OK 1
+#endif
+
 int access(const char *, int);
 int faccessat(int, const char *, int, int);
 
@@ -127,10 +136,12 @@
 long sysconf(int);
 size_t confstr(int, char *, size_t);
 
+#ifndef F_ULOCK
 #define F_ULOCK 0
 #define F_LOCK  1
 #define F_TLOCK 2
 #define F_TEST  3
+#endif
 
 #if defined(_XOPEN_SOURCE) || defined(_GNU_SOURCE)
 int lockf(int, int, off_t);
@@ -143,10 +154,9 @@
 void sync(void);
 #endif
 
-#ifdef _GNU_SOURCE
+#if defined(_GNU_SOURCE) || defined(_BSD_SOURCE)
 int brk(void *);
 void *sbrk(intptr_t);
-pid_t forkall(void);
 pid_t vfork(void);
 int vhangup(void);
 int chroot(const char *);
@@ -155,6 +165,11 @@
 int usleep(unsigned);
 unsigned ualarm(unsigned, unsigned);
 int setgroups(size_t, const gid_t []);
+char *getpass(const char *);
+#endif
+
+#ifdef _GNU_SOURCE
+pid_t forkall(void);
 int setresuid(uid_t, uid_t, uid_t);
 int setresgid(gid_t, gid_t, gid_t);
 int getresuid(uid_t *, uid_t *, uid_t *);
@@ -166,7 +181,6 @@
 void setusershell(void);
 void endusershell(void);
 char *getusershell(void);
-char *getpass(const char *);
 #endif
 
 #ifdef _LARGEFILE64_SOURCE