libbsd: Branch 'master' - 18 commits
Guillem Jover
guillem at kemper.freedesktop.org
Mon May 16 04:31:36 PDT 2011
.gitignore | 2
Makefile | 26 ++-
TODO | 1
Versions | 35 +++-
include/bsd/bsd.h | 2
include/bsd/err.h | 8 -
include/bsd/getopt.h | 5
include/bsd/libutil.h | 74 +++++++++
include/bsd/nlist.h | 38 ++++
include/bsd/stdio.h | 9 +
include/bsd/stdlib.h | 23 ++
include/bsd/string.h | 8 +
include/bsd/sys/bitstring.h | 146 ++++++++++++++++++
include/bsd/sys/cdefs.h | 4
include/bsd/unistd.h | 8 +
include/bsd/vis.h | 88 +++++++++++
include/libutil.h | 78 ++-------
include/nlist.h | 10 -
include/vis.h | 92 ++---------
libbsd-transparent.pc.in | 11 +
src/arc4random.3 | 2
src/bsd_getopt.c | 2
src/err.c | 2
src/fgetln.3 | 2
src/fgetln.c | 4
src/flopen.3 | 2
src/flopen.c | 1
src/fmtcheck.3 | 2
src/fpurge.c | 68 ++++++++
src/getpeereid.3 | 138 +++++++++++++++++
src/getpeereid.c | 132 ++++++++++++++++
src/heapsort.3 | 208 ++++++++++++++++++++++++++
src/humanize_number.3 | 2
src/mdX.3 | 2
src/merge.c | 351 ++++++++++++++++++++++++++++++++++++++++++++
src/mergesort.3 | 1
src/nlist.3 | 2
src/pidfile.3 | 2
src/progname.c | 11 +
src/radixsort.3 | 161 ++++++++++++++++++++
src/radixsort.c | 327 ++++++++++++++++++++++++++++++++++++++++
src/readpassphrase.3 | 2
src/readpassphrase.c | 2
src/reallocf.3 | 107 +++++++++++++
src/reallocf.c | 48 ++++++
src/setmode.3 | 2
src/sradixsort.3 | 1
src/strlcpy.3 | 2
src/strmode.3 | 2
src/strtonum.3 | 2
src/unvis.3 | 2
src/vis.3 | 2
src/vis.c | 1
53 files changed, 2088 insertions(+), 175 deletions(-)
New commits:
commit 94fe901edabdfbafc602e4d194a21c5e0f372cba
Author: Guillem Jover <guillem at hadrons.org>
Date: Sat May 14 18:12:48 2011 +0200
Abort compilation if fgetln cannot be wrapped
diff --git a/src/fgetln.c b/src/fgetln.c
index 9ccc1b9..d805ee1 100644
--- a/src/fgetln.c
+++ b/src/fgetln.c
@@ -1,6 +1,6 @@
/*
* Copyright © 2005 Hector Garcia Alvarez
- * Copyright © 2005, 2008, 2009 Guillem Jover
+ * Copyright © 2005, 2008, 2009, 2011 Guillem Jover
*
* Redistribution and use in source and binary forms, with or without
* modification, are permitted provided that the following conditions
@@ -48,5 +48,7 @@ fgetln(FILE *stream, size_t *len)
return line;
}
}
+#else
+#error "Function fgetln() needs to be ported."
#endif
commit 28585a58bdd0308c516d2b1caee76977283942da
Author: Guillem Jover <guillem at hadrons.org>
Date: Sat May 14 17:58:08 2011 +0200
Add fpurge function
diff --git a/Makefile b/Makefile
index 94cf55c..5a04ae3 100644
--- a/Makefile
+++ b/Makefile
@@ -27,6 +27,7 @@ LIB_SRCS := \
err.c \
fgetln.c \
flopen.c \
+ fpurge.c \
getpeereid.c \
heapsort.c \
merge.c \
diff --git a/Versions b/Versions
index 619da34..fde5a8a 100644
--- a/Versions
+++ b/Versions
@@ -86,5 +86,7 @@ LIBBSD_0.3 {
mergesort;
radixsort;
sradixsort;
+
+ fpurge;
} LIBBSD_0.2;
diff --git a/include/bsd/stdio.h b/include/bsd/stdio.h
index dfde0c3..cdf4e50 100644
--- a/include/bsd/stdio.h
+++ b/include/bsd/stdio.h
@@ -1,5 +1,5 @@
/*
- * Copyright © 2004, 2005, 2009 Guillem Jover
+ * Copyright © 2004, 2005, 2009, 2011 Guillem Jover
*
* Redistribution and use in source and binary forms, with or without
* modification, are permitted provided that the following conditions
@@ -40,6 +40,8 @@ __BEGIN_DECLS
const char *fmtcheck(const char *, const char *);
char *fgetln(FILE *fp, size_t *lenp);
+
+int fpurge(FILE *fp);
__END_DECLS
#endif
diff --git a/src/fpurge.c b/src/fpurge.c
new file mode 100644
index 0000000..19d6537
--- /dev/null
+++ b/src/fpurge.c
@@ -0,0 +1,68 @@
+/*
+ * Copyright © 2011 Guillem Jover
+ *
+ * Redistribution and use in source and binary forms, with or without
+ * modification, are permitted provided that the following conditions
+ * are met:
+ * 1. Redistributions of source code must retain the above copyright
+ * notice, this list of conditions and the following disclaimer.
+ * 2. Redistributions in binary form must reproduce the above copyright
+ * notice, this list of conditions and the following disclaimer in the
+ * documentation and/or other materials provided with the distribution.
+ * 3. The name of the author may not be used to endorse or promote products
+ * derived from this software without specific prior written permission.
+ *
+ * THIS SOFTWARE IS PROVIDED ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES,
+ * INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY
+ * AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL
+ * THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL,
+ * EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO,
+ * PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS;
+ * OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY,
+ * WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR
+ * OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF
+ * ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
+ */
+
+#define _GNU_SOURCE 1
+
+#include <errno.h>
+#include <stdio.h>
+#include <stdio_ext.h>
+
+#ifdef __GLIBC__
+int
+fpurge(FILE *fp)
+{
+ if (fp == NULL || fileno(fp) < 0) {
+ errno = EBADF;
+ return EOF;
+ }
+
+ __fpurge(fp);
+
+ return 0;
+}
+#else
+#error "Function fpurge() needs to be ported."
+#endif
+
+#ifdef TEST
+int
+main()
+{
+ static FILE fp_bad;
+ FILE *fp;
+
+ if (fpurge(&fp_bad) == 0)
+ return 1;
+
+ fp = fopen("/dev/zero", "r");
+ if (fpurge(fp) < 0)
+ return 1;
+
+ fclose(fp);
+
+ return 0;
+}
+#endif
commit b36c59c0edbd1c215737f16d4bb83feb035d785a
Author: Guillem Jover <guillem at hadrons.org>
Date: Wed Feb 23 12:39:58 2011 +0100
Conditionalize <time.h> inclusion from <bsd/bsd.h>
This was added long time ago to fix some software which was implicitly
depending on the header through some other header, and to avoid having
to modify such software. Conditionalize it on LIBBSD_CLEAN_INCLUDES,
so that buildability can be tested for its future removal.
diff --git a/include/bsd/bsd.h b/include/bsd/bsd.h
index 8ebb12c..bba4ac8 100644
--- a/include/bsd/bsd.h
+++ b/include/bsd/bsd.h
@@ -42,6 +42,8 @@
#include <bsd/md5.h>
/* FIXME: Will be removed in the future. */
+#ifndef LIBBSD_CLEAN_INCLUDES
#include <time.h>
+#endif
#endif
commit 8b6a74775b918f203ceee3d70ca8b9f7e0256b5f
Author: Guillem Jover <guillem at hadrons.org>
Date: Wed Feb 23 12:38:42 2011 +0100
Conditionalize temporary compatibility inclusions
These inclusions were in place for backward compatibility purposes,
when the headers were split so that code using them would not break.
Make it possible for applications to disable them by defining
LIBBSD_CLEAN_INCLUDES so that buildability can be tested and fixed
before they get removed in a subsequent release.
diff --git a/Makefile b/Makefile
index 05771b7..94cf55c 100644
--- a/Makefile
+++ b/Makefile
@@ -125,7 +125,9 @@ CFLAGS ?= -g -Wall -Wextra -Wno-unused-variable
LDFLAGS ?=
# Internal makefile variables
-MK_CPPFLAGS := -Iinclude/bsd/ -Iinclude/ -DLIBBSD_TRANSPARENT -D_GNU_SOURCE -D__REENTRANT
+MK_CPPFLAGS := -Iinclude/bsd/ -Iinclude/ \
+ -DLIBBSD_TRANSPARENT -DLIBBSD_CLEAN_INCLUDES \
+ -D_GNU_SOURCE -D__REENTRANT
MK_CFLAGS :=
MK_LDFLAGS :=
diff --git a/include/bsd/stdlib.h b/include/bsd/stdlib.h
index 6736ed1..4a185fc 100644
--- a/include/bsd/stdlib.h
+++ b/include/bsd/stdlib.h
@@ -47,8 +47,10 @@
#endif
/* FIXME: Temporary inclusions to avoid API breakage, will be removed soon. */
+#ifndef LIBBSD_CLEAN_INCLUDES
#include <bsd/stdio.h>
#include <bsd/unistd.h>
+#endif
__BEGIN_DECLS
u_int32_t arc4random();
diff --git a/include/bsd/string.h b/include/bsd/string.h
index edc16fc..f9e733f 100644
--- a/include/bsd/string.h
+++ b/include/bsd/string.h
@@ -36,8 +36,10 @@
#include <string.h>
#endif
+#ifndef LIBBSD_CLEAN_INCLUDES
/* FIXME: Temporary inclusion to avoid API breakage, will be removed soon. */
#include <bsd/stdio.h>
+#endif
__BEGIN_DECLS
size_t strlcpy(char *dst, const char *src, size_t siz);
diff --git a/src/vis.c b/src/vis.c
index 6153a7f..f869ed8 100644
--- a/src/vis.c
+++ b/src/vis.c
@@ -32,6 +32,7 @@
#include <limits.h>
#include <ctype.h>
#include <string.h>
+#include <stdio.h>
#include <vis.h>
#define isoctal(c) (((u_char)(c)) >= '0' && ((u_char)(c)) <= '7')
commit c594192bac39e8ec9487f729b22dc0037d58b8c5
Author: Guillem Jover <guillem at hadrons.org>
Date: Sat May 14 14:17:44 2011 +0200
Correct library name in man pages
diff --git a/src/heapsort.3 b/src/heapsort.3
index 39ed3e0..4261c84 100644
--- a/src/heapsort.3
+++ b/src/heapsort.3
@@ -39,7 +39,8 @@
.Nm heapsort , mergesort
.Nd sort functions
.Sh LIBRARY
-.Lb libc
+.ds str-Lb-libbsd Utility functions from BSD systems (libbsd, \-lbsd)
+.Lb libbsd
.Sh SYNOPSIS
.In bsd/stdlib.h
.Ft int
diff --git a/src/radixsort.3 b/src/radixsort.3
index 82a4be7..3884124 100644
--- a/src/radixsort.3
+++ b/src/radixsort.3
@@ -35,7 +35,8 @@
.Nm radixsort , sradixsort
.Nd radix sort
.Sh LIBRARY
-.Lb libc
+.ds str-Lb-libbsd Utility functions from BSD systems (libbsd, \-lbsd)
+.Lb libbsd
.Sh SYNOPSIS
.In limits.h
.In bsd/stdlib.h
commit 8478e57463db7a4f1b27d165d45de17c6d992ada
Author: Guillem Jover <guillem at hadrons.org>
Date: Wed Feb 23 13:14:22 2011 +0100
Update libbsd header references in man pages
Point to the namespaced path for libbsd specific headers.
diff --git a/TODO b/TODO
index 3a1e3dc..1e96cb5 100644
--- a/TODO
+++ b/TODO
@@ -1,4 +1,3 @@
* Add more functions used by ported packages (check openssh).
-* Fix includes on man pages.
* Add missing man pages.
* Add a README.import file.
diff --git a/src/arc4random.3 b/src/arc4random.3
index 1043602..eda74af 100644
--- a/src/arc4random.3
+++ b/src/arc4random.3
@@ -44,7 +44,7 @@
.ds str-Lb-libbsd Utility functions from BSD systems (libbsd, \-lbsd)
.Lb libbsd
.Sh SYNOPSIS
-.In stdlib.h
+.In bsd/stdlib.h
.Ft u_int32_t
.Fn arc4random "void"
.Ft void
diff --git a/src/fgetln.3 b/src/fgetln.3
index cc33a0a..7dd3fc8 100644
--- a/src/fgetln.3
+++ b/src/fgetln.3
@@ -38,7 +38,7 @@
.ds str-Lb-libbsd Utility functions from BSD systems (libbsd, \-lbsd)
.Lb libbsd
.Sh SYNOPSIS
-.In stdio.h
+.In bsd/stdio.h
.Ft char *
.Fn fgetln "FILE *stream" "size_t *len"
.Sh DESCRIPTION
diff --git a/src/flopen.3 b/src/flopen.3
index 4c53d46..8f81211 100644
--- a/src/flopen.3
+++ b/src/flopen.3
@@ -36,7 +36,7 @@
.Lb libbsd
.Sh SYNOPSIS
.In sys/fcntl.h
-.In libutil.h
+.In bsd/libutil.h
.Ft int
.Fn flopen "const char *path" "int flags"
.Ft int
diff --git a/src/fmtcheck.3 b/src/fmtcheck.3
index 86a3563..7bf5d39 100644
--- a/src/fmtcheck.3
+++ b/src/fmtcheck.3
@@ -37,7 +37,7 @@ format string
.ds str-Lb-libbsd Utility functions from BSD systems (libbsd, \-lbsd)
.Lb libbsd
.Sh SYNOPSIS
-.In stdio.h
+.In bsd/stdio.h
.Ft const char *
.Fn fmtcheck "const char *fmt_suspect" "const char *fmt_default"
.Sh DESCRIPTION
diff --git a/src/getpeereid.3 b/src/getpeereid.3
index 66ae6c2..84c776e 100644
--- a/src/getpeereid.3
+++ b/src/getpeereid.3
@@ -36,7 +36,7 @@
.Lb libbsd
.Sh SYNOPSIS
.In sys/types.h
-.In unistd.h
+.In bsd/unistd.h
.Ft int
.Fn getpeereid "int s" "uid_t *euid" "gid_t *egid"
.Sh DESCRIPTION
diff --git a/src/heapsort.3 b/src/heapsort.3
index bf6ce7f..39ed3e0 100644
--- a/src/heapsort.3
+++ b/src/heapsort.3
@@ -41,7 +41,7 @@
.Sh LIBRARY
.Lb libc
.Sh SYNOPSIS
-.In stdlib.h
+.In bsd/stdlib.h
.Ft int
.Fo heapsort
.Fa "void *base"
diff --git a/src/humanize_number.3 b/src/humanize_number.3
index 403e173..e0ffdf0 100644
--- a/src/humanize_number.3
+++ b/src/humanize_number.3
@@ -38,7 +38,7 @@
.ds str-Lb-libbsd Utility functions from BSD systems (libbsd, \-lbsd)
.Lb libbsd
.Sh SYNOPSIS
-.In stdlib.h
+.In bsd/stdlib.h
.Ft int
.Fn dehumanize_number "const char *str" "int64_t *result"
.Ft int
diff --git a/src/mdX.3 b/src/mdX.3
index 963d221..41bf329 100644
--- a/src/mdX.3
+++ b/src/mdX.3
@@ -28,7 +28,7 @@
.Lb libbsd
.Sh SYNOPSIS
.Fd #include <sys/types.h>
-.Fd #include <mdX.h>
+.Fd #include <bsd/mdX.h>
.Ft void
.Fn MDXInit "MDX_CTX *context"
.Ft void
diff --git a/src/nlist.3 b/src/nlist.3
index 3ee5404..25624c7 100644
--- a/src/nlist.3
+++ b/src/nlist.3
@@ -38,7 +38,7 @@
.ds str-Lb-libbsd Utility functions from BSD systems (libbsd, \-lbsd)
.Lb libbsd
.Sh SYNOPSIS
-.In nlist.h
+.In bsd/nlist.h
.Ft int
.Fn nlist "const char *filename" "struct nlist *nl"
.Sh DESCRIPTION
diff --git a/src/pidfile.3 b/src/pidfile.3
index 34214c0..2f84c44 100644
--- a/src/pidfile.3
+++ b/src/pidfile.3
@@ -37,7 +37,7 @@
.ds str-Lb-libbsd Utility functions from BSD systems (libbsd, \-lbsd)
.Lb libbsd
.Sh SYNOPSIS
-.In libutil.h
+.In bsd/libutil.h
.Ft "struct pidfh *"
.Fn pidfile_open "const char *path" "mode_t mode" "pid_t *pidptr"
.Ft int
diff --git a/src/radixsort.3 b/src/radixsort.3
index dfa65f1..82a4be7 100644
--- a/src/radixsort.3
+++ b/src/radixsort.3
@@ -38,7 +38,7 @@
.Lb libc
.Sh SYNOPSIS
.In limits.h
-.In stdlib.h
+.In bsd/stdlib.h
.Ft int
.Fn radixsort "const unsigned char **base" "int nmemb" "const unsigned char *table" "unsigned endbyte"
.Ft int
diff --git a/src/readpassphrase.3 b/src/readpassphrase.3
index c9d55a2..967f8d3 100644
--- a/src/readpassphrase.3
+++ b/src/readpassphrase.3
@@ -28,7 +28,7 @@
.ds str-Lb-libbsd Utility functions from BSD systems (libbsd, \-lbsd)
.Lb libbsd
.Sh SYNOPSIS
-.Fd #include <readpassphrase.h>
+.Fd #include <bsd/readpassphrase.h>
.Ft char *
.Fn readpassphrase "const char *prompt" "char *buf" "size_t bufsiz" "int flags"
.Sh DESCRIPTION
diff --git a/src/reallocf.3 b/src/reallocf.3
index eee010c..99152b6 100644
--- a/src/reallocf.3
+++ b/src/reallocf.3
@@ -42,7 +42,7 @@
.ds str-Lb-libbsd Utility functions from BSD systems (libbsd, \-lbsd)
.Lb libbsd
.Sh SYNOPSIS
-.In stdlib.h
+.In bsd/stdlib.h
.Ft void *
.Fn reallocf "void *ptr" "size_t size"
.Sh DESCRIPTION
diff --git a/src/setmode.3 b/src/setmode.3
index c0a2837..ae7c568 100644
--- a/src/setmode.3
+++ b/src/setmode.3
@@ -39,7 +39,7 @@
.ds str-Lb-libbsd Utility functions from BSD systems (libbsd, \-lbsd)
.Lb libbsd
.Sh SYNOPSIS
-.In unistd.h
+.In bsd/unistd.h
.Ft mode_t
.Fn getmode "const void *set" "mode_t mode"
.Ft void *
diff --git a/src/strlcpy.3 b/src/strlcpy.3
index 74fbafa..bf14b4f 100644
--- a/src/strlcpy.3
+++ b/src/strlcpy.3
@@ -25,7 +25,7 @@
.ds str-Lb-libbsd Utility functions from BSD systems (libbsd, \-lbsd)
.Lb libbsd
.Sh SYNOPSIS
-.In string.h
+.In bsd/string.h
.Ft size_t
.Fn strlcpy "char *dst" "const char *src" "size_t size"
.Ft size_t
diff --git a/src/strmode.3 b/src/strmode.3
index b2dfaf4..70264c0 100644
--- a/src/strmode.3
+++ b/src/strmode.3
@@ -38,7 +38,7 @@
.ds str-Lb-libbsd Utility functions from BSD systems (libbsd, \-lbsd)
.Lb libbsd
.Sh SYNOPSIS
-.In string.h
+.In bsd/string.h
.Ft void
.Fn strmode "mode_t mode" "char *bp"
.Sh DESCRIPTION
diff --git a/src/strtonum.3 b/src/strtonum.3
index c5f3111..42674f0 100644
--- a/src/strtonum.3
+++ b/src/strtonum.3
@@ -25,8 +25,8 @@
.ds str-Lb-libbsd Utility functions from BSD systems (libbsd, \-lbsd)
.Lb libbsd
.Sh SYNOPSIS
-.In stdlib.h
.In limits.h
+.In bsd/stdlib.h
.Ft long long
.Fo strtonum
.Fa "const char *nptr"
diff --git a/src/unvis.3 b/src/unvis.3
index f318ecf..5e01f88 100644
--- a/src/unvis.3
+++ b/src/unvis.3
@@ -39,7 +39,7 @@
.ds str-Lb-libbsd Utility functions from BSD systems (libbsd, \-lbsd)
.Lb libbsd
.Sh SYNOPSIS
-.In vis.h
+.In bsd/vis.h
.Ft int
.Fn unvis "char *cp" "char c" "int *astate" "int flag"
.Ft int
diff --git a/src/vis.3 b/src/vis.3
index 3da6eb1..65548f0 100644
--- a/src/vis.3
+++ b/src/vis.3
@@ -41,7 +41,7 @@
.Lb libbsd
.Sh SYNOPSIS
.In stdlib.h
-.In vis.h
+.In bsd/vis.h
.Ft char *
.Fn vis "char *dst" "int c" "int flag" "int nextc"
.Ft int
commit f7caf2b30da93d46528ec706dffaf5cbde5266d9
Author: Guillem Jover <guillem at hadrons.org>
Date: Wed Feb 23 11:07:30 2011 +0100
Move all header files to /usr/include/bsd/ and deprecate /usr/include/
First stage of the transition to avoid possible clashes with other
software by moving out of the way the remaining headers from
/usr/include/.
At least nlist.h is known to cause file conflicts with some libelf
implementations. libutil.h is not really complete and might cause
confusion if software detects its availability w/o someone actually
checking. And lastly vis.h is not known to cause any problem and it's
complete, but better be safe than sorry.
The compatibility headers will be removed in a later release.
diff --git a/Makefile b/Makefile
index 553b464..05771b7 100644
--- a/Makefile
+++ b/Makefile
@@ -71,6 +71,9 @@ LIB_INCLUDES := \
bsd/stdlib.h \
bsd/readpassphrase.h \
bsd/unistd.h \
+ bsd/nlist.h \
+ bsd/vis.h \
+ bsd/libutil.h \
nlist.h \
vis.h \
libutil.h
diff --git a/include/bsd/libutil.h b/include/bsd/libutil.h
new file mode 100644
index 0000000..21a22cd
--- /dev/null
+++ b/include/bsd/libutil.h
@@ -0,0 +1,74 @@
+/*
+ * Copyright (c) 1996 Peter Wemm <peter at FreeBSD.org>.
+ * All rights reserved.
+ * Copyright (c) 2002 Networks Associates Technology, Inc.
+ * All rights reserved.
+ *
+ * Portions of this software were developed for the FreeBSD Project by
+ * ThinkSec AS and NAI Labs, the Security Research Division of Network
+ * Associates, Inc. under DARPA/SPAWAR contract N66001-01-C-8035
+ * ("CBOSS"), as part of the DARPA CHATS research program.
+ *
+ * Redistribution and use in source and binary forms, with or without
+ * modification, is permitted provided that the following conditions
+ * are met:
+ * 1. Redistributions of source code must retain the above copyright
+ * notice, this list of conditions and the following disclaimer.
+ * 2. Redistributions in binary form must reproduce the above copyright
+ * notice, this list of conditions and the following disclaimer in the
+ * documentation and/or other materials provided with the distribution.
+ * 3. The name of the author may not be used to endorse or promote
+ * products derived from this software without specific prior written
+ * permission.
+ *
+ * THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND
+ * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
+ * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
+ * ARE DISCLAIMED. IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE
+ * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
+ * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
+ * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
+ * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
+ * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
+ * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
+ * SUCH DAMAGE.
+ *
+ * $FreeBSD: src/lib/libutil/libutil.h,v 1.47 2008/04/23 00:49:12 scf Exp $
+ */
+
+#ifndef _LIBUTIL_H_
+#define _LIBUTIL_H_
+
+#include <features.h>
+#include <sys/types.h>
+
+/* for pidfile.c */
+struct pidfh {
+ int pf_fd;
+ char *pf_path;
+ dev_t pf_dev;
+ ino_t pf_ino;
+};
+
+__BEGIN_DECLS
+int humanize_number(char *buf, size_t len, int64_t bytes,
+ const char *suffix, int scale, int flags);
+
+int flopen(const char *_path, int _flags, ...);
+
+struct pidfh *pidfile_open(const char *path, mode_t mode, pid_t *pidptr);
+int pidfile_write(struct pidfh *pfh);
+int pidfile_close(struct pidfh *pfh);
+int pidfile_remove(struct pidfh *pfh);
+__END_DECLS
+
+/* humanize_number(3) */
+#define HN_DECIMAL 0x01
+#define HN_NOSPACE 0x02
+#define HN_B 0x04
+#define HN_DIVISOR_1000 0x08
+
+#define HN_GETSCALE 0x10
+#define HN_AUTOSCALE 0x20
+
+#endif /* !_LIBUTIL_H_ */
diff --git a/include/bsd/nlist.h b/include/bsd/nlist.h
new file mode 100644
index 0000000..84fbc33
--- /dev/null
+++ b/include/bsd/nlist.h
@@ -0,0 +1,38 @@
+/*
+ * Copyright © 2009 Guillem Jover
+ *
+ * Redistribution and use in source and binary forms, with or without
+ * modification, are permitted provided that the following conditions
+ * are met:
+ * 1. Redistributions of source code must retain the above copyright
+ * notice, this list of conditions and the following disclaimer.
+ * 2. Redistributions in binary form must reproduce the above copyright
+ * notice, this list of conditions and the following disclaimer in the
+ * documentation and/or other materials provided with the distribution.
+ * 3. The name of the author may not be used to endorse or promote products
+ * derived from this software without specific prior written permission.
+ *
+ * THIS SOFTWARE IS PROVIDED ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES,
+ * INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY
+ * AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL
+ * THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL,
+ * EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO,
+ * PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS;
+ * OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY,
+ * WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR
+ * OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF
+ * ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
+ */
+
+#ifndef LIBBSD_NLIST_H
+#define LIBBSD_NLIST_H
+
+#include <sys/cdefs.h>
+#include <a.out.h>
+
+__BEGIN_DECLS
+extern int nlist(const char *filename, struct nlist *list);
+__END_DECLS
+
+#endif
+
diff --git a/include/bsd/vis.h b/include/bsd/vis.h
new file mode 100644
index 0000000..835d2d6
--- /dev/null
+++ b/include/bsd/vis.h
@@ -0,0 +1,88 @@
+/*-
+ * Copyright (c) 1990, 1993
+ * The Regents of the University of California. All rights reserved.
+ *
+ * Redistribution and use in source and binary forms, with or without
+ * modification, are permitted provided that the following conditions
+ * are met:
+ * 1. Redistributions of source code must retain the above copyright
+ * notice, this list of conditions and the following disclaimer.
+ * 2. Redistributions in binary form must reproduce the above copyright
+ * notice, this list of conditions and the following disclaimer in the
+ * documentation and/or other materials provided with the distribution.
+ * 3. Neither the name of the University nor the names of its contributors
+ * may be used to endorse or promote products derived from this software
+ * without specific prior written permission.
+ *
+ * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
+ * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
+ * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
+ * ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
+ * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
+ * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
+ * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
+ * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
+ * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
+ * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
+ * SUCH DAMAGE.
+ *
+ * @(#)vis.h 8.1 (Berkeley) 6/2/93
+ * $FreeBSD: src/include/vis.h,v 1.11 2003/10/30 10:40:49 phk Exp $
+ */
+
+#ifndef _VIS_H_
+#define _VIS_H_
+
+#include <sys/types.h>
+
+/*
+ * to select alternate encoding format
+ */
+#define VIS_OCTAL 0x01 /* use octal \ddd format */
+#define VIS_CSTYLE 0x02 /* use \[nrft0..] where appropriate */
+
+/*
+ * to alter set of characters encoded (default is to encode all
+ * non-graphic except space, tab, and newline).
+ */
+#define VIS_SP 0x04 /* also encode space */
+#define VIS_TAB 0x08 /* also encode tab */
+#define VIS_NL 0x10 /* also encode newline */
+#define VIS_WHITE (VIS_SP | VIS_TAB | VIS_NL)
+#define VIS_SAFE 0x20 /* only encode "unsafe" characters */
+
+/*
+ * other
+ */
+#define VIS_NOSLASH 0x40 /* inhibit printing '\' */
+#define VIS_HTTPSTYLE 0x80 /* http-style escape % HEX HEX */
+#define VIS_GLOB 0x100 /* encode glob(3) magics */
+
+/*
+ * unvis return codes
+ */
+#define UNVIS_VALID 1 /* character valid */
+#define UNVIS_VALIDPUSH 2 /* character valid, push back passed char */
+#define UNVIS_NOCHAR 3 /* valid sequence, no character produced */
+#define UNVIS_SYNBAD -1 /* unrecognized escape sequence */
+#define UNVIS_ERROR -2 /* decoder in unknown state (unrecoverable) */
+
+/*
+ * unvis flags
+ */
+#define UNVIS_END 1 /* no more characters */
+
+#include <sys/cdefs.h>
+
+__BEGIN_DECLS
+char *vis(char *, int, int, int);
+int strvis(char *, const char *, int);
+int strvisx(char *, const char *, size_t, int);
+int strnvis(char *, const char *, size_t, int);
+int strunvis(char *, const char *);
+int strunvisx(char *, const char *, int);
+ssize_t strnunvis(char *, const char *, size_t);
+int unvis(char *, int, int *, int);
+__END_DECLS
+
+#endif /* !_VIS_H_ */
diff --git a/include/libutil.h b/include/libutil.h
index 21a22cd..1d5f03a 100644
--- a/include/libutil.h
+++ b/include/libutil.h
@@ -1,74 +1,34 @@
/*
- * Copyright (c) 1996 Peter Wemm <peter at FreeBSD.org>.
- * All rights reserved.
- * Copyright (c) 2002 Networks Associates Technology, Inc.
- * All rights reserved.
- *
- * Portions of this software were developed for the FreeBSD Project by
- * ThinkSec AS and NAI Labs, the Security Research Division of Network
- * Associates, Inc. under DARPA/SPAWAR contract N66001-01-C-8035
- * ("CBOSS"), as part of the DARPA CHATS research program.
+ * Copyright © 2011 Guillem Jover
*
* Redistribution and use in source and binary forms, with or without
- * modification, is permitted provided that the following conditions
+ * modification, are permitted provided that the following conditions
* are met:
* 1. Redistributions of source code must retain the above copyright
* notice, this list of conditions and the following disclaimer.
* 2. Redistributions in binary form must reproduce the above copyright
* notice, this list of conditions and the following disclaimer in the
* documentation and/or other materials provided with the distribution.
- * 3. The name of the author may not be used to endorse or promote
- * products derived from this software without specific prior written
- * permission.
- *
- * THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND
- * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
- * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
- * ARE DISCLAIMED. IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE
- * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
- * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
- * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
- * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
- * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
- * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
- * SUCH DAMAGE.
+ * 3. The name of the author may not be used to endorse or promote products
+ * derived from this software without specific prior written permission.
*
- * $FreeBSD: src/lib/libutil/libutil.h,v 1.47 2008/04/23 00:49:12 scf Exp $
+ * THIS SOFTWARE IS PROVIDED ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES,
+ * INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY
+ * AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL
+ * THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL,
+ * EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO,
+ * PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS;
+ * OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY,
+ * WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR
+ * OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF
+ * ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
*/
-#ifndef _LIBUTIL_H_
-#define _LIBUTIL_H_
-
-#include <features.h>
-#include <sys/types.h>
-
-/* for pidfile.c */
-struct pidfh {
- int pf_fd;
- char *pf_path;
- dev_t pf_dev;
- ino_t pf_ino;
-};
-
-__BEGIN_DECLS
-int humanize_number(char *buf, size_t len, int64_t bytes,
- const char *suffix, int scale, int flags);
-
-int flopen(const char *_path, int _flags, ...);
-
-struct pidfh *pidfile_open(const char *path, mode_t mode, pid_t *pidptr);
-int pidfile_write(struct pidfh *pfh);
-int pidfile_close(struct pidfh *pfh);
-int pidfile_remove(struct pidfh *pfh);
-__END_DECLS
+#ifndef LIBBSD_LIBUTIL_H
+#define LIBBSD_LIBUTIL_H
-/* humanize_number(3) */
-#define HN_DECIMAL 0x01
-#define HN_NOSPACE 0x02
-#define HN_B 0x04
-#define HN_DIVISOR_1000 0x08
+#warning "This header is deprecated, use the one in bsd/libutil.h instead."
-#define HN_GETSCALE 0x10
-#define HN_AUTOSCALE 0x20
+#include <bsd/libutil.h>
-#endif /* !_LIBUTIL_H_ */
+#endif
diff --git a/include/nlist.h b/include/nlist.h
index 84fbc33..61be513 100644
--- a/include/nlist.h
+++ b/include/nlist.h
@@ -1,5 +1,5 @@
/*
- * Copyright © 2009 Guillem Jover
+ * Copyright © 2011 Guillem Jover
*
* Redistribution and use in source and binary forms, with or without
* modification, are permitted provided that the following conditions
@@ -27,12 +27,8 @@
#ifndef LIBBSD_NLIST_H
#define LIBBSD_NLIST_H
-#include <sys/cdefs.h>
-#include <a.out.h>
+#warning "This header is deprecated, use the one in bsd/nlist.h instead."
-__BEGIN_DECLS
-extern int nlist(const char *filename, struct nlist *list);
-__END_DECLS
+#include <bsd/nlist.h>
#endif
-
diff --git a/include/vis.h b/include/vis.h
index 835d2d6..2d46fab 100644
--- a/include/vis.h
+++ b/include/vis.h
@@ -1,6 +1,5 @@
-/*-
- * Copyright (c) 1990, 1993
- * The Regents of the University of California. All rights reserved.
+/*
+ * Copyright © 2011 Guillem Jover
*
* Redistribution and use in source and binary forms, with or without
* modification, are permitted provided that the following conditions
@@ -10,79 +9,26 @@
* 2. Redistributions in binary form must reproduce the above copyright
* notice, this list of conditions and the following disclaimer in the
* documentation and/or other materials provided with the distribution.
- * 3. Neither the name of the University nor the names of its contributors
- * may be used to endorse or promote products derived from this software
- * without specific prior written permission.
- *
- * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
- * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
- * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
- * ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
- * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
- * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
- * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
- * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
- * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
- * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
- * SUCH DAMAGE.
+ * 3. The name of the author may not be used to endorse or promote products
+ * derived from this software without specific prior written permission.
*
- * @(#)vis.h 8.1 (Berkeley) 6/2/93
- * $FreeBSD: src/include/vis.h,v 1.11 2003/10/30 10:40:49 phk Exp $
- */
-
-#ifndef _VIS_H_
-#define _VIS_H_
-
-#include <sys/types.h>
-
-/*
- * to select alternate encoding format
- */
-#define VIS_OCTAL 0x01 /* use octal \ddd format */
-#define VIS_CSTYLE 0x02 /* use \[nrft0..] where appropriate */
-
-/*
- * to alter set of characters encoded (default is to encode all
- * non-graphic except space, tab, and newline).
+ * THIS SOFTWARE IS PROVIDED ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES,
+ * INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY
+ * AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL
+ * THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL,
+ * EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO,
+ * PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS;
+ * OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY,
+ * WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR
+ * OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF
+ * ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
*/
-#define VIS_SP 0x04 /* also encode space */
-#define VIS_TAB 0x08 /* also encode tab */
-#define VIS_NL 0x10 /* also encode newline */
-#define VIS_WHITE (VIS_SP | VIS_TAB | VIS_NL)
-#define VIS_SAFE 0x20 /* only encode "unsafe" characters */
-/*
- * other
- */
-#define VIS_NOSLASH 0x40 /* inhibit printing '\' */
-#define VIS_HTTPSTYLE 0x80 /* http-style escape % HEX HEX */
-#define VIS_GLOB 0x100 /* encode glob(3) magics */
-
-/*
- * unvis return codes
- */
-#define UNVIS_VALID 1 /* character valid */
-#define UNVIS_VALIDPUSH 2 /* character valid, push back passed char */
-#define UNVIS_NOCHAR 3 /* valid sequence, no character produced */
-#define UNVIS_SYNBAD -1 /* unrecognized escape sequence */
-#define UNVIS_ERROR -2 /* decoder in unknown state (unrecoverable) */
-
-/*
- * unvis flags
- */
-#define UNVIS_END 1 /* no more characters */
+#ifndef LIBBSD_VIS_H
+#define LIBBSD_VIS_H
-#include <sys/cdefs.h>
+#warning "This header is deprecated, use the one in bsd/vis.h instead."
-__BEGIN_DECLS
-char *vis(char *, int, int, int);
-int strvis(char *, const char *, int);
-int strvisx(char *, const char *, size_t, int);
-int strnvis(char *, const char *, size_t, int);
-int strunvis(char *, const char *);
-int strunvisx(char *, const char *, int);
-ssize_t strnunvis(char *, const char *, size_t);
-int unvis(char *, int, int *, int);
-__END_DECLS
+#include <bsd/vis.h>
-#endif /* !_VIS_H_ */
+#endif
commit 520682e59647eadf697e6384021e6781164b11b0
Author: Guillem Jover <guillem at hadrons.org>
Date: Wed Feb 23 14:04:57 2011 +0100
Add support for transparent compilation
This means that software being ported should not need to be modified in
the usual case, as the libbsd headers will take over the standard
namespace and fill the missing gaps, and include the system headers.
To use this the new libbsd-transparent.pc file can be used through
pkg-config, which should end up doing the right thing.
diff --git a/Makefile b/Makefile
index b333ef7..553b464 100644
--- a/Makefile
+++ b/Makefile
@@ -7,6 +7,7 @@ LIB_VERSION_MICRO := 0
LIB_VERSION := $(LIB_VERSION_MAJOR).$(LIB_VERSION_MINOR).$(LIB_VERSION_MICRO)
LIB_PKGCONFIG := $(LIB_NAME).pc
+LIB_PKGCONFIG_TRANS := $(LIB_NAME)-transparent.pc
LIB_STATIC := $(LIB_NAME).a
LIB_SHARED_SO := $(LIB_NAME).so
LIB_SONAME := $(LIB_SHARED_SO).$(LIB_VERSION_MAJOR)
@@ -121,7 +122,7 @@ CFLAGS ?= -g -Wall -Wextra -Wno-unused-variable
LDFLAGS ?=
# Internal makefile variables
-MK_CPPFLAGS := -Iinclude/ -include bsd/bsd.h -D_GNU_SOURCE -D__REENTRANT
+MK_CPPFLAGS := -Iinclude/bsd/ -Iinclude/ -DLIBBSD_TRANSPARENT -D_GNU_SOURCE -D__REENTRANT
MK_CFLAGS :=
MK_LDFLAGS :=
@@ -137,7 +138,7 @@ pkgconfigdir = ${usrlibdir}/pkgconfig
mandir = ${prefix}/share/man
.PHONY: libs
-libs: $(LIB_STATIC) $(LIB_SHARED_SO) $(LIB_PKGCONFIG)
+libs: $(LIB_STATIC) $(LIB_SHARED_SO) $(LIB_PKGCONFIG) $(LIB_PKGCONFIG_TRANS)
.PHONY: man
man: $(LIB_MANS)
@@ -207,6 +208,7 @@ install: libs man
done
install -m644 $(LIB_MANS) $(DESTDIR)$(mandir)/man3
install -m644 $(LIB_PKGCONFIG) $(DESTDIR)$(pkgconfigdir)
+ install -m644 $(LIB_PKGCONFIG_TRANS) $(DESTDIR)$(pkgconfigdir)
ifeq ($(libdir),$(usrlibdir))
# If both dirs are the same, do a relative symlink.
ln -sf $(LIB_SHARED) $(DESTDIR)$(usrlibdir)/$(LIB_SHARED_SO)
@@ -219,6 +221,7 @@ endif
.PHONY: clean
clean:
rm -f $(LIB_PKGCONFIG)
+ rm -f $(LIB_PKGCONFIG_TRANS)
rm -f $(LIB_SRCS_GEN) $(LIB_MANS_GEN)
rm -f $(LIB_STATIC_OBJS)
rm -f $(LIB_STATIC)
diff --git a/include/bsd/err.h b/include/bsd/err.h
index 489138b..b622655 100644
--- a/include/bsd/err.h
+++ b/include/bsd/err.h
@@ -29,9 +29,15 @@
#define LIBBSD_ERR_H
#include <sys/cdefs.h>
-#include <err.h>
+
#include <stdarg.h>
+#ifdef LIBBSD_TRANSPARENT
+#include_next <err.h>
+#else
+#include <err.h>
+#endif
+
__BEGIN_DECLS
extern void warnc (int code, const char *format, ...);
extern void vwarnc (int code, const char *format, va_list ap);
diff --git a/include/bsd/getopt.h b/include/bsd/getopt.h
index 699a00e..b0057cf 100644
--- a/include/bsd/getopt.h
+++ b/include/bsd/getopt.h
@@ -29,7 +29,12 @@
#define LIBBSD_GETOPT_H
#include <sys/cdefs.h>
+
+#ifdef LIBBSD_TRANSPARENT
+#include_next <getopt.h>
+#else
#include <getopt.h>
+#endif
__BEGIN_DECLS
extern int optreset;
diff --git a/include/bsd/stdio.h b/include/bsd/stdio.h
index 882f374..dfde0c3 100644
--- a/include/bsd/stdio.h
+++ b/include/bsd/stdio.h
@@ -29,7 +29,12 @@
#include <sys/cdefs.h>
#include <sys/types.h>
+
+#ifdef LIBBSD_TRANSPARENT
+#include_next <stdio.h>
+#else
#include <stdio.h>
+#endif
__BEGIN_DECLS
const char *fmtcheck(const char *, const char *);
diff --git a/include/bsd/stdlib.h b/include/bsd/stdlib.h
index bf30f36..6736ed1 100644
--- a/include/bsd/stdlib.h
+++ b/include/bsd/stdlib.h
@@ -32,10 +32,19 @@
#include <sys/cdefs.h>
#include <sys/stat.h>
#include <stdint.h>
+
+#ifdef LIBBSD_TRANSPARENT
+#include_next <stdlib.h>
+#else
#include <stdlib.h>
+#endif
/* For compatibility with NetBSD, which defines humanize_number here. */
+#ifdef LIBBSD_TRANSPARENT
#include <libutil.h>
+#else
+#include <bsd/libutil.h>
+#endif
/* FIXME: Temporary inclusions to avoid API breakage, will be removed soon. */
#include <bsd/stdio.h>
diff --git a/include/bsd/string.h b/include/bsd/string.h
index cf72cc0..edc16fc 100644
--- a/include/bsd/string.h
+++ b/include/bsd/string.h
@@ -30,6 +30,12 @@
#include <sys/cdefs.h>
#include <sys/types.h>
+#ifdef LIBBSD_TRANSPARENT
+#include_next <string.h>
+#else
+#include <string.h>
+#endif
+
/* FIXME: Temporary inclusion to avoid API breakage, will be removed soon. */
#include <bsd/stdio.h>
diff --git a/include/bsd/sys/cdefs.h b/include/bsd/sys/cdefs.h
index 4d9aa49..87d8a48 100644
--- a/include/bsd/sys/cdefs.h
+++ b/include/bsd/sys/cdefs.h
@@ -27,7 +27,11 @@
#ifndef LIBBSD_CDEFS_H
#define LIBBSD_CDEFS_H
+#ifdef LIBBSD_TRANSPARENT
+#include_next <sys/cdefs.h>
+#else
#include <sys/cdefs.h>
+#endif
#ifndef __dead2
# define __dead2
diff --git a/include/bsd/unistd.h b/include/bsd/unistd.h
index 2a22fbc..ac56787 100644
--- a/include/bsd/unistd.h
+++ b/include/bsd/unistd.h
@@ -30,6 +30,12 @@
#include <sys/cdefs.h>
#include <sys/stat.h>
+#ifdef LIBBSD_TRANSPARENT
+#include_next <unistd.h>
+#else
+#include <unistd.h>
+#endif
+
#ifndef S_ISTXT
#define S_ISTXT S_ISVTX
#endif
diff --git a/libbsd-transparent.pc.in b/libbsd-transparent.pc.in
new file mode 100644
index 0000000..2549e1c
--- /dev/null
+++ b/libbsd-transparent.pc.in
@@ -0,0 +1,11 @@
+prefix=@prefix@
+exec_prefix=@exec_prefix@
+libdir=@libdir@
+includedir=@includedir@
+
+Name: libbsd
+Description: Utility functions from BSD systems (transparent)
+Version: @VERSION@
+URL: http://libbsd.freedesktop.org/
+Libs: -L${libdir} -lbsd
+Cflags: -isystem ${includedir}/bsd -DLIBBSD_TRANSPARENT
diff --git a/src/bsd_getopt.c b/src/bsd_getopt.c
index a213d9b..f5fb304 100644
--- a/src/bsd_getopt.c
+++ b/src/bsd_getopt.c
@@ -24,7 +24,7 @@
* ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
*/
-#include <bsd/getopt.h>
+#include <getopt.h>
int optreset = 0;
diff --git a/src/err.c b/src/err.c
index d33f08e..e5c604d 100644
--- a/src/err.c
+++ b/src/err.c
@@ -24,7 +24,7 @@
* ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
*/
-#include <bsd/err.h>
+#include <err.h>
#include <errno.h>
#include <stdarg.h>
diff --git a/src/flopen.c b/src/flopen.c
index 754c9c0..f5f7338 100644
--- a/src/flopen.c
+++ b/src/flopen.c
@@ -34,7 +34,6 @@ __FBSDID("$FreeBSD$");
#include <errno.h>
#include <stdarg.h>
#include <unistd.h>
-
#include <libutil.h>
int
diff --git a/src/progname.c b/src/progname.c
index 1079429..f24071a 100644
--- a/src/progname.c
+++ b/src/progname.c
@@ -31,8 +31,7 @@
#include <errno.h>
#include <string.h>
-
-#include <bsd/stdlib.h>
+#include <stdlib.h>
static const char *__progname = NULL;
diff --git a/src/readpassphrase.c b/src/readpassphrase.c
index 601da49..1f4fe0e 100644
--- a/src/readpassphrase.c
+++ b/src/readpassphrase.c
@@ -29,7 +29,7 @@
#include <string.h>
#include <termios.h>
#include <unistd.h>
-#include <bsd/readpassphrase.h>
+#include <readpassphrase.h>
#ifndef TCSASOFT
#define TCSASOFT 0
commit 4c01261f3963c51ac0ee0f2b539cbf9298e8cc26
Author: Guillem Jover <guillem at hadrons.org>
Date: Wed Feb 23 14:15:43 2011 +0100
Move each version symbol declaration into its own line
diff --git a/Versions b/Versions
index 51dc9ec..619da34 100644
--- a/Versions
+++ b/Versions
@@ -3,8 +3,15 @@ LIBBSD_0.0 {
arc4random;
arc4random_stir;
arc4random_addrandom;
- bsd_getopt; optreset;
- errc; warnc; verrc; vwarnc;
+
+ bsd_getopt;
+ optreset;
+
+ errc;
+ warnc;
+ verrc;
+ vwarnc;
+
fgetln;
fmtcheck;
heapsort;
@@ -12,15 +19,22 @@ LIBBSD_0.0 {
inet_net_pton; /* XXX: Already provided by glibc, remove. */
- getprogname; setprogname;
+ getprogname;
+ setprogname;
+
strlcpy;
strlcat;
setmode;
getmode;
- vis; strvis; strvisx;
- unvis; strunvis; strunvisx;
+ vis;
+ strvis;
+ strvisx;
+ unvis;
+ strunvis;
+ strunvisx;
+
MD5Init;
MD5Update;
MD5Pad;
commit 8a99226f16b3cf638d93b23a1c80f9df2d16fd0e
Author: Guillem Jover <guillem at hadrons.org>
Date: Wed Feb 23 14:01:06 2011 +0100
Generalize pkg-config file handling
Use a make pattern for the .pc rule to allow using more than one .pc
file. And generalize the .gitignore entry.
diff --git a/.gitignore b/.gitignore
index 29dbeb0..799fb3d 100644
--- a/.gitignore
+++ b/.gitignore
@@ -1,5 +1,5 @@
ChangeLog
-libbsd.pc
+*.pc
*.lo
*.o
*.so*
diff --git a/Makefile b/Makefile
index 9d3585f..b333ef7 100644
--- a/Makefile
+++ b/Makefile
@@ -154,7 +154,7 @@ src/md5.3bsd: src/mdX.3
src/hash/md5hl.c: src/hash/helper.c
sed -e 's:hashinc:bsd/md5.h:g' -e 's:HASH:MD5:g' $< > $@
-$(LIB_PKGCONFIG): $(LIB_PKGCONFIG).in
+%.pc: %.pc.in
sed -e 's:@VERSION@:$(VERSION):' \
-e 's:@prefix@:$(value prefix):' \
-e 's:@exec_prefix@:$(value exec_prefix):' \
commit 1497d34760d53777648f91b357762ba8bbb2d5ce
Author: Guillem Jover <guillem at hadrons.org>
Date: Fri Feb 25 13:52:59 2011 +0100
Initialize __progname to program_invocation_short_name
As we do not have cooperation from the crt0 code to set __progname, we
have to set it ourselves from getprogname() in case it's NULL. On GNU
systems we can use program_invocation_short_name which is actually set
on crt0.
diff --git a/src/progname.c b/src/progname.c
index ef56144..1079429 100644
--- a/src/progname.c
+++ b/src/progname.c
@@ -1,6 +1,6 @@
/*
* Copyright © 2006 Robert Millan
- * Copyright © 2010 Guillem Jover
+ * Copyright © 2010-2011 Guillem Jover
*
* Redistribution and use in source and binary forms, with or without
* modification, are permitted provided that the following conditions
@@ -29,6 +29,7 @@
Rejected in glibc (http://sourceware.org/ml/libc-alpha/2006-03/msg00125.html)
*/
+#include <errno.h>
#include <string.h>
#include <bsd/stdlib.h>
@@ -38,6 +39,11 @@ static const char *__progname = NULL;
const char *
getprogname(void)
{
+#ifdef __GLIBC__
+ if (__progname == NULL)
+ __progname = program_invocation_short_name;
+#endif
+
return __progname;
}
commit 741eb5876341c0310fe485cc9dd3af75bc9f4ab9
Author: Kevin McCarthy <signals at gentoo.org>
Date: Tue Feb 22 20:44:21 2011 +0100
Add missing prototypes for arc4random_buf and arc4random_uniform
Fixes: https://bugs.freedesktop.org/show_bug.cgi?id=34511
diff --git a/include/bsd/stdlib.h b/include/bsd/stdlib.h
index ea790bc..bf30f36 100644
--- a/include/bsd/stdlib.h
+++ b/include/bsd/stdlib.h
@@ -45,6 +45,8 @@ __BEGIN_DECLS
u_int32_t arc4random();
void arc4random_stir();
void arc4random_addrandom(u_char *dat, int datlen);
+void arc4random_buf(void *_buf, size_t n);
+u_int32_t arc4random_uniform(u_int32_t upper_bound);
int dehumanize_number(const char *str, int64_t *size);
commit 9baf9640b9af3bbdb041703e684d3928263fb363
Author: Guillem Jover <guillem at hadrons.org>
Date: Tue Feb 22 20:19:37 2011 +0100
Add new <bsd/sys/bitstring.h> header
Taken from FreeBSD.
diff --git a/Makefile b/Makefile
index ad73456..9d3585f 100644
--- a/Makefile
+++ b/Makefile
@@ -55,6 +55,7 @@ LIB_INCLUDES := \
bsd/queue.h \
bsd/ip_icmp.h \
bsd/sys/cdefs.h \
+ bsd/sys/bitstring.h \
bsd/sys/queue.h \
bsd/sys/tree.h \
bsd/netinet/ip_icmp.h \
diff --git a/include/bsd/sys/bitstring.h b/include/bsd/sys/bitstring.h
new file mode 100644
index 0000000..125ef51
--- /dev/null
+++ b/include/bsd/sys/bitstring.h
@@ -0,0 +1,146 @@
+/*-
+ * Copyright (c) 1989, 1993
+ * The Regents of the University of California. All rights reserved.
+ *
+ * This code is derived from software contributed to Berkeley by
+ * Paul Vixie.
+ *
+ * Redistribution and use in source and binary forms, with or without
+ * modification, are permitted provided that the following conditions
+ * are met:
+ * 1. Redistributions of source code must retain the above copyright
+ * notice, this list of conditions and the following disclaimer.
+ * 2. Redistributions in binary form must reproduce the above copyright
+ * notice, this list of conditions and the following disclaimer in the
+ * documentation and/or other materials provided with the distribution.
+ * 4. Neither the name of the University nor the names of its contributors
+ * may be used to endorse or promote products derived from this software
+ * without specific prior written permission.
+ *
+ * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
+ * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
+ * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
+ * ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
+ * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
+ * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
+ * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
+ * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
+ * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
+ * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
+ * SUCH DAMAGE.
+ *
+ * $FreeBSD$
+ */
+
+#ifndef _SYS_BITSTRING_H_
+#define _SYS_BITSTRING_H_
+
+typedef unsigned char bitstr_t;
+
+/* internal macros */
+ /* byte of the bitstring bit is in */
+#define _bit_byte(bit) \
+ ((bit) >> 3)
+
+ /* mask for the bit within its byte */
+#define _bit_mask(bit) \
+ (1 << ((bit)&0x7))
+
+/* external macros */
+ /* bytes in a bitstring of nbits bits */
+#define bitstr_size(nbits) \
+ (((nbits) + 7) >> 3)
+
+ /* allocate a bitstring */
+#define bit_alloc(nbits) \
+ (bitstr_t *)calloc((size_t)bitstr_size(nbits), sizeof(bitstr_t))
+
+ /* allocate a bitstring on the stack */
+#define bit_decl(name, nbits) \
+ ((name)[bitstr_size(nbits)])
+
+ /* is bit N of bitstring name set? */
+#define bit_test(name, bit) \
+ ((name)[_bit_byte(bit)] & _bit_mask(bit))
+
+ /* set bit N of bitstring name */
+#define bit_set(name, bit) \
+ ((name)[_bit_byte(bit)] |= _bit_mask(bit))
+
+ /* clear bit N of bitstring name */
+#define bit_clear(name, bit) \
+ ((name)[_bit_byte(bit)] &= ~_bit_mask(bit))
+
+ /* clear bits start ... stop in bitstring */
+#define bit_nclear(name, start, stop) do { \
+ register bitstr_t *_name = (name); \
+ register int _start = (start), _stop = (stop); \
+ register int _startbyte = _bit_byte(_start); \
+ register int _stopbyte = _bit_byte(_stop); \
+ if (_startbyte == _stopbyte) { \
+ _name[_startbyte] &= ((0xff >> (8 - (_start&0x7))) | \
+ (0xff << ((_stop&0x7) + 1))); \
+ } else { \
+ _name[_startbyte] &= 0xff >> (8 - (_start&0x7)); \
+ while (++_startbyte < _stopbyte) \
+ _name[_startbyte] = 0; \
+ _name[_stopbyte] &= 0xff << ((_stop&0x7) + 1); \
+ } \
+} while (0)
+
+ /* set bits start ... stop in bitstring */
+#define bit_nset(name, start, stop) do { \
+ register bitstr_t *_name = (name); \
+ register int _start = (start), _stop = (stop); \
+ register int _startbyte = _bit_byte(_start); \
+ register int _stopbyte = _bit_byte(_stop); \
+ if (_startbyte == _stopbyte) { \
+ _name[_startbyte] |= ((0xff << (_start&0x7)) & \
+ (0xff >> (7 - (_stop&0x7)))); \
+ } else { \
+ _name[_startbyte] |= 0xff << ((_start)&0x7); \
+ while (++_startbyte < _stopbyte) \
+ _name[_startbyte] = 0xff; \
+ _name[_stopbyte] |= 0xff >> (7 - (_stop&0x7)); \
+ } \
+} while (0)
+
+ /* find first bit clear in name */
+#define bit_ffc(name, nbits, value) do { \
+ register bitstr_t *_name = (name); \
+ register int _byte, _nbits = (nbits); \
+ register int _stopbyte = _bit_byte(_nbits - 1), _value = -1; \
+ if (_nbits > 0) \
+ for (_byte = 0; _byte <= _stopbyte; ++_byte) \
+ if (_name[_byte] != 0xff) { \
+ bitstr_t _lb; \
+ _value = _byte << 3; \
+ for (_lb = _name[_byte]; (_lb&0x1); \
+ ++_value, _lb >>= 1); \
+ break; \
+ } \
+ if (_value >= nbits) \
+ _value = -1; \
+ *(value) = _value; \
+} while (0)
+
+ /* find first bit set in name */
+#define bit_ffs(name, nbits, value) do { \
+ register bitstr_t *_name = (name); \
+ register int _byte, _nbits = (nbits); \
+ register int _stopbyte = _bit_byte(_nbits - 1), _value = -1; \
+ if (_nbits > 0) \
+ for (_byte = 0; _byte <= _stopbyte; ++_byte) \
+ if (_name[_byte]) { \
+ bitstr_t _lb; \
+ _value = _byte << 3; \
+ for (_lb = _name[_byte]; !(_lb&0x1); \
+ ++_value, _lb >>= 1); \
+ break; \
+ } \
+ if (_value >= nbits) \
+ _value = -1; \
+ *(value) = _value; \
+} while (0)
+
+#endif /* !_SYS_BITSTRING_H_ */
commit 4b95e82a32f07ec80445dd4103103ebaa356c89b
Author: Guillem Jover <guillem at hadrons.org>
Date: Fri Feb 25 18:48:10 2011 +0100
Add new radixsort and sradixsort functions
Taken from FreeBSD.
diff --git a/Makefile b/Makefile
index 315ad97..ad73456 100644
--- a/Makefile
+++ b/Makefile
@@ -44,6 +44,7 @@ LIB_SRCS := \
fmtcheck.c \
nlist.c \
progname.c \
+ radixsort.c \
vis.c unvis.c \
$(LIB_SRCS_GEN)
LIB_SRCS_GEN := $(patsubst %,src/%,$(LIB_SRCS_GEN))
@@ -93,6 +94,8 @@ LIB_MANS := \
humanize_number.3 \
fmtcheck.3 \
mergesort.3 \
+ radixsort.3 \
+ sradixsort.3 \
nlist.3 \
pidfile.3 \
setmode.3 \
diff --git a/Versions b/Versions
index cac2efe..51dc9ec 100644
--- a/Versions
+++ b/Versions
@@ -70,5 +70,7 @@ LIBBSD_0.3 {
getpeereid;
mergesort;
+ radixsort;
+ sradixsort;
} LIBBSD_0.2;
diff --git a/include/bsd/stdlib.h b/include/bsd/stdlib.h
index bfe3027..ea790bc 100644
--- a/include/bsd/stdlib.h
+++ b/include/bsd/stdlib.h
@@ -54,6 +54,10 @@ void setprogname(const char *);
int heapsort (void *, size_t, size_t, int (*)(const void *, const void *));
int mergesort(void *base, size_t nmemb, size_t size,
int (*cmp)(const void *, const void *));
+int radixsort(const unsigned char **base, int nmemb,
+ const unsigned char *table, unsigned endbyte);
+int sradixsort(const unsigned char **base, int nmemb,
+ const unsigned char *table, unsigned endbyte);
void *reallocf(void *ptr, size_t size);
diff --git a/src/radixsort.3 b/src/radixsort.3
new file mode 100644
index 0000000..dfa65f1
--- /dev/null
+++ b/src/radixsort.3
@@ -0,0 +1,160 @@
+.\" Copyright (c) 1990, 1991, 1993
+.\" The Regents of the University of California. All rights reserved.
+.\"
+.\" Redistribution and use in source and binary forms, with or without
+.\" modification, are permitted provided that the following conditions
+.\" are met:
+.\" 1. Redistributions of source code must retain the above copyright
+.\" notice, this list of conditions and the following disclaimer.
+.\" 2. Redistributions in binary form must reproduce the above copyright
+.\" notice, this list of conditions and the following disclaimer in the
+.\" documentation and/or other materials provided with the distribution.
+.\" 4. Neither the name of the University nor the names of its contributors
+.\" may be used to endorse or promote products derived from this software
+.\" without specific prior written permission.
+.\"
+.\" THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
+.\" ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
+.\" IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
+.\" ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
+.\" FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
+.\" DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
+.\" OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
+.\" HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
+.\" LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
+.\" OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
+.\" SUCH DAMAGE.
+.\"
+.\" @(#)radixsort.3 8.2 (Berkeley) 1/27/94
+.\" $FreeBSD$
+.\"
+.Dd January 27, 1994
+.Dt RADIXSORT 3
+.Os
+.Sh NAME
+.Nm radixsort , sradixsort
+.Nd radix sort
+.Sh LIBRARY
+.Lb libc
+.Sh SYNOPSIS
+.In limits.h
+.In stdlib.h
+.Ft int
+.Fn radixsort "const unsigned char **base" "int nmemb" "const unsigned char *table" "unsigned endbyte"
+.Ft int
+.Fn sradixsort "const unsigned char **base" "int nmemb" "const unsigned char *table" "unsigned endbyte"
+.Sh DESCRIPTION
+The
+.Fn radixsort
+and
+.Fn sradixsort
+functions
+are implementations of radix sort.
+.Pp
+These functions sort an array of pointers to byte strings, the initial
+member of which is referenced by
+.Fa base .
+The byte strings may contain any values; the end of each string
+is denoted by the user-specified value
+.Fa endbyte .
+.Pp
+Applications may specify a sort order by providing the
+.Fa table
+argument.
+If
+.Pf non- Dv NULL ,
+.Fa table
+must reference an array of
+.Dv UCHAR_MAX
++ 1 bytes which contains the sort
+weight of each possible byte value.
+The end-of-string byte must have a sort weight of 0 or 255
+(for sorting in reverse order).
+More than one byte may have the same sort weight.
+The
+.Fa table
+argument
+is useful for applications which wish to sort different characters
+equally, for example, providing a table with the same weights
+for A-Z as for a-z will result in a case-insensitive sort.
+If
+.Fa table
+is NULL, the contents of the array are sorted in ascending order
+according to the
+.Tn ASCII
+order of the byte strings they reference and
+.Fa endbyte
+has a sorting weight of 0.
+.Pp
+The
+.Fn sradixsort
+function is stable, that is, if two elements compare as equal, their
+order in the sorted array is unchanged.
+The
+.Fn sradixsort
+function uses additional memory sufficient to hold
+.Fa nmemb
+pointers.
+.Pp
+The
+.Fn radixsort
+function is not stable, but uses no additional memory.
+.Pp
+These functions are variants of most-significant-byte radix sorting; in
+particular, see
+.An "D.E. Knuth" Ns 's
+.%T "Algorithm R"
+and section 5.2.5, exercise 10.
+They take linear time relative to the number of bytes in the strings.
+.Sh RETURN VALUES
+.Rv -std radixsort
+.Sh ERRORS
+.Bl -tag -width Er
+.It Bq Er EINVAL
+The value of the
+.Fa endbyte
+element of
+.Fa table
+is not 0 or 255.
+.El
+.Pp
+Additionally, the
+.Fn sradixsort
+function
+may fail and set
+.Va errno
+for any of the errors specified for the library routine
+.Xr malloc 3 .
+.Sh SEE ALSO
+.Xr sort 1 ,
+.Xr qsort 3
+.Pp
+.Rs
+.%A Knuth, D.E.
+.%D 1968
+.%B "The Art of Computer Programming"
+.%T "Sorting and Searching"
+.%V Vol. 3
+.%P pp. 170-178
+.Re
+.Rs
+.%A Paige, R.
+.%D 1987
+.%T "Three Partition Refinement Algorithms"
+.%J "SIAM J. Comput."
+.%V Vol. 16
+.%N No. 6
+.Re
+.Rs
+.%A McIlroy, P.
+.%D 1993
+.%B "Engineering Radix Sort"
+.%T "Computing Systems"
+.%V Vol. 6:1
+.%P pp. 5-27
+.Re
+.Sh HISTORY
+The
+.Fn radixsort
+function first appeared in
+.Bx 4.4 .
diff --git a/src/radixsort.c b/src/radixsort.c
new file mode 100644
index 0000000..82ff1bc
--- /dev/null
+++ b/src/radixsort.c
@@ -0,0 +1,327 @@
+/*-
+ * Copyright (c) 1990, 1993
+ * The Regents of the University of California. All rights reserved.
+ *
+ * This code is derived from software contributed to Berkeley by
+ * Peter McIlroy and by Dan Bernstein at New York University,
+ *
+ * Redistribution and use in source and binary forms, with or without
+ * modification, are permitted provided that the following conditions
+ * are met:
+ * 1. Redistributions of source code must retain the above copyright
+ * notice, this list of conditions and the following disclaimer.
+ * 2. Redistributions in binary form must reproduce the above copyright
+ * notice, this list of conditions and the following disclaimer in the
+ * documentation and/or other materials provided with the distribution.
+ * 4. Neither the name of the University nor the names of its contributors
+ * may be used to endorse or promote products derived from this software
+ * without specific prior written permission.
+ *
+ * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
+ * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
+ * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
+ * ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
+ * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
+ * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
+ * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
+ * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
+ * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
+ * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
+ * SUCH DAMAGE.
+ */
+
+#if defined(LIBC_SCCS) && !defined(lint)
+static char sccsid[] = "@(#)radixsort.c 8.2 (Berkeley) 4/28/95";
+#endif /* LIBC_SCCS and not lint */
+#include <sys/cdefs.h>
+__FBSDID("$FreeBSD$");
+
+/*
+ * Radixsort routines.
+ *
+ * Program r_sort_a() is unstable but uses O(logN) extra memory for a stack.
+ * Use radixsort(a, n, trace, endchar) for this case.
+ *
+ * For stable sorting (using N extra pointers) use sradixsort(), which calls
+ * r_sort_b().
+ *
+ * For a description of this code, see D. McIlroy, P. McIlroy, K. Bostic,
+ * "Engineering Radix Sort".
+ */
+
+#include <sys/types.h>
+#include <stdlib.h>
+#include <stddef.h>
+#include <errno.h>
+
+typedef struct {
+ const u_char **sa;
+ int sn, si;
+} stack;
+
+static inline void simplesort
+(const u_char **, int, int, const u_char *, u_int);
+static void r_sort_a(const u_char **, int, int, const u_char *, u_int);
+static void r_sort_b(const u_char **, const u_char **, int, int,
+ const u_char *, u_int);
+
+#define THRESHOLD 20 /* Divert to simplesort(). */
+#define SIZE 512 /* Default stack size. */
+
+#define SETUP { \
+ if (tab == NULL) { \
+ tr = tr0; \
+ for (c = 0; c < endch; c++) \
+ tr0[c] = c + 1; \
+ tr0[c] = 0; \
+ for (c++; c < 256; c++) \
+ tr0[c] = c; \
+ endch = 0; \
+ } else { \
+ endch = tab[endch]; \
+ tr = tab; \
+ if (endch != 0 && endch != 255) { \
+ errno = EINVAL; \
+ return (-1); \
+ } \
+ } \
+}
+
+int
+radixsort(a, n, tab, endch)
+ const u_char **a, *tab;
+ int n;
+ u_int endch;
+{
+ const u_char *tr;
+ int c;
+ u_char tr0[256];
+
+ SETUP;
+ r_sort_a(a, n, 0, tr, endch);
+ return (0);
+}
+
+int
+sradixsort(a, n, tab, endch)
+ const u_char **a, *tab;
+ int n;
+ u_int endch;
+{
+ const u_char *tr, **ta;
+ int c;
+ u_char tr0[256];
+
+ SETUP;
+ if (n < THRESHOLD)
+ simplesort(a, n, 0, tr, endch);
+ else {
+ if ((ta = malloc(n * sizeof(a))) == NULL)
+ return (-1);
+ r_sort_b(a, ta, n, 0, tr, endch);
+ free(ta);
+ }
+ return (0);
+}
+
+#define empty(s) (s >= sp)
+#define pop(a, n, i) a = (--sp)->sa, n = sp->sn, i = sp->si
+#define push(a, n, i) sp->sa = a, sp->sn = n, (sp++)->si = i
+#define swap(a, b, t) t = a, a = b, b = t
+
+/* Unstable, in-place sort. */
+static void
+r_sort_a(a, n, i, tr, endch)
+ const u_char **a;
+ int n, i;
+ const u_char *tr;
+ u_int endch;
+{
+ static int count[256], nc, bmin;
+ int c;
+ const u_char **ak, *r;
+ stack s[SIZE], *sp, *sp0, *sp1, temp;
+ int *cp, bigc;
+ const u_char **an, *t, **aj, **top[256];
+
+ /* Set up stack. */
+ sp = s;
+ push(a, n, i);
+ while (!empty(s)) {
+ pop(a, n, i);
+ if (n < THRESHOLD) {
+ simplesort(a, n, i, tr, endch);
+ continue;
+ }
+ an = a + n;
+
+ /* Make character histogram. */
+ if (nc == 0) {
+ bmin = 255; /* First occupied bin, excluding eos. */
+ for (ak = a; ak < an;) {
+ c = tr[(*ak++)[i]];
+ if (++count[c] == 1 && c != endch) {
+ if (c < bmin)
+ bmin = c;
+ nc++;
+ }
+ }
+ if (sp + nc > s + SIZE) { /* Get more stack. */
+ r_sort_a(a, n, i, tr, endch);
+ continue;
+ }
+ }
+
+ /*
+ * Special case: if all strings have the same
+ * character at position i, move on to the next
+ * character.
+ */
+ if (nc == 1 && count[bmin] == n) {
+ push(a, n, i+1);
+ nc = count[bmin] = 0;
+ continue;
+ }
+
+ /*
+ * Set top[]; push incompletely sorted bins onto stack.
+ * top[] = pointers to last out-of-place element in bins.
+ * count[] = counts of elements in bins.
+ * Before permuting: top[c-1] + count[c] = top[c];
+ * during deal: top[c] counts down to top[c-1].
+ */
+ sp0 = sp1 = sp; /* Stack position of biggest bin. */
+ bigc = 2; /* Size of biggest bin. */
+ if (endch == 0) /* Special case: set top[eos]. */
+ top[0] = ak = a + count[0];
+ else {
+ ak = a;
+ top[255] = an;
+ }
+ for (cp = count + bmin; nc > 0; cp++) {
+ while (*cp == 0) /* Find next non-empty pile. */
+ cp++;
+ if (*cp > 1) {
+ if (*cp > bigc) {
+ bigc = *cp;
+ sp1 = sp;
+ }
+ push(ak, *cp, i+1);
+ }
+ top[cp-count] = ak += *cp;
+ nc--;
+ }
+ swap(*sp0, *sp1, temp); /* Play it safe -- biggest bin last. */
+
+ /*
+ * Permute misplacements home. Already home: everything
+ * before aj, and in bin[c], items from top[c] on.
+ * Inner loop:
+ * r = next element to put in place;
+ * ak = top[r[i]] = location to put the next element.
+ * aj = bottom of 1st disordered bin.
+ * Outer loop:
+ * Once the 1st disordered bin is done, ie. aj >= ak,
+ * aj<-aj + count[c] connects the bins in a linked list;
+ * reset count[c].
+ */
+ for (aj = a; aj < an; *aj = r, aj += count[c], count[c] = 0)
+ for (r = *aj; aj < (ak = --top[c = tr[r[i]]]);)
+ swap(*ak, r, t);
+ }
+}
+
+/* Stable sort, requiring additional memory. */
+static void
+r_sort_b(a, ta, n, i, tr, endch)
+ const u_char **a, **ta;
+ int n, i;
+ const u_char *tr;
+ u_int endch;
+{
+ static int count[256], nc, bmin;
+ int c;
+ const u_char **ak, **ai;
+ stack s[512], *sp, *sp0, *sp1, temp;
+ const u_char **top[256];
+ int *cp, bigc;
+
+ sp = s;
+ push(a, n, i);
+ while (!empty(s)) {
+ pop(a, n, i);
+ if (n < THRESHOLD) {
+ simplesort(a, n, i, tr, endch);
+ continue;
+ }
+
+ if (nc == 0) {
+ bmin = 255;
+ for (ak = a + n; --ak >= a;) {
+ c = tr[(*ak)[i]];
+ if (++count[c] == 1 && c != endch) {
+ if (c < bmin)
+ bmin = c;
+ nc++;
+ }
+ }
+ if (sp + nc > s + SIZE) {
+ r_sort_b(a, ta, n, i, tr, endch);
+ continue;
+ }
+ }
+
+ sp0 = sp1 = sp;
+ bigc = 2;
+ if (endch == 0) {
+ top[0] = ak = a + count[0];
+ count[0] = 0;
+ } else {
+ ak = a;
+ top[255] = a + n;
+ count[255] = 0;
+ }
+ for (cp = count + bmin; nc > 0; cp++) {
+ while (*cp == 0)
+ cp++;
+ if ((c = *cp) > 1) {
+ if (c > bigc) {
+ bigc = c;
+ sp1 = sp;
+ }
+ push(ak, c, i+1);
+ }
+ top[cp-count] = ak += c;
+ *cp = 0; /* Reset count[]. */
+ nc--;
+ }
+ swap(*sp0, *sp1, temp);
+
+ for (ak = ta + n, ai = a+n; ak > ta;) /* Copy to temp. */
+ *--ak = *--ai;
+ for (ak = ta+n; --ak >= ta;) /* Deal to piles. */
+ *--top[tr[(*ak)[i]]] = *ak;
+ }
+}
+
+static inline void
+simplesort(a, n, b, tr, endch) /* insertion sort */
+ const u_char **a;
+ int n, b;
+ const u_char *tr;
+ u_int endch;
+{
+ u_char ch;
+ const u_char **ak, **ai, *s, *t;
+
+ for (ak = a+1; --n >= 1; ak++)
+ for (ai = ak; ai > a; ai--) {
+ for (s = ai[0] + b, t = ai[-1] + b;
+ (ch = tr[*s]) != endch; s++, t++)
+ if (ch != tr[*t])
+ break;
+ if (ch >= tr[*t])
+ break;
+ swap(ai[0], ai[-1], s);
+ }
+}
diff --git a/src/sradixsort.3 b/src/sradixsort.3
new file mode 100644
index 0000000..86a95a3
--- /dev/null
+++ b/src/sradixsort.3
@@ -0,0 +1 @@
+.so man3/radixsort.3
commit c766e58acf3a894644291d21f9c98d322ef8cd11
Author: Guillem Jover <guillem at hadrons.org>
Date: Fri Feb 25 18:25:17 2011 +0100
Add man pages for heapsort and mergesort
Taken from FreeBSD, originally as qsort.3 but qsort references stripped.
diff --git a/Makefile b/Makefile
index 3c5cdce..315ad97 100644
--- a/Makefile
+++ b/Makefile
@@ -89,8 +89,10 @@ LIB_MANS := \
getpeereid.3 \
readpassphrase.3 \
reallocf.3 \
+ heapsort.3 \
humanize_number.3 \
fmtcheck.3 \
+ mergesort.3 \
nlist.3 \
pidfile.3 \
setmode.3 \
diff --git a/src/heapsort.3 b/src/heapsort.3
new file mode 100644
index 0000000..bf6ce7f
--- /dev/null
+++ b/src/heapsort.3
@@ -0,0 +1,207 @@
+.\" Copyright (c) 1990, 1991, 1993
+.\" The Regents of the University of California. All rights reserved.
+.\"
+.\" This code is derived from software contributed to Berkeley by
+.\" the American National Standards Committee X3, on Information
+.\" Processing Systems.
+.\"
+.\" Redistribution and use in source and binary forms, with or without
+.\" modification, are permitted provided that the following conditions
+.\" are met:
+.\" 1. Redistributions of source code must retain the above copyright
+.\" notice, this list of conditions and the following disclaimer.
+.\" 2. Redistributions in binary form must reproduce the above copyright
+.\" notice, this list of conditions and the following disclaimer in the
+.\" documentation and/or other materials provided with the distribution.
+.\" 4. Neither the name of the University nor the names of its contributors
+.\" may be used to endorse or promote products derived from this software
+.\" without specific prior written permission.
+.\"
+.\" THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
+.\" ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
+.\" IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
+.\" ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
+.\" FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
+.\" DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
+.\" OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
+.\" HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
+.\" LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
+.\" OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
+.\" SUCH DAMAGE.
+.\"
+.\" @(#)qsort.3 8.1 (Berkeley) 6/4/93
+.\" $FreeBSD$
+.\"
+.Dd September 30, 2003
+.Dt QSORT 3
+.Os
+.Sh NAME
+.Nm heapsort , mergesort
+.Nd sort functions
+.Sh LIBRARY
+.Lb libc
+.Sh SYNOPSIS
+.In stdlib.h
+.Ft int
+.Fo heapsort
+.Fa "void *base"
+.Fa "size_t nmemb"
+.Fa "size_t size"
+.Fa "int \*[lp]*compar\*[rp]\*[lp]const void *, const void *\*[rp]"
+.Fc
+.Ft int
+.Fo mergesort
+.Fa "void *base"
+.Fa "size_t nmemb"
+.Fa "size_t size"
+.Fa "int \*[lp]*compar\*[rp]\*[lp]const void *, const void *\*[rp]"
+.Fc
+.Sh DESCRIPTION
+The
+.Fn heapsort
+function is a modified selection sort.
+The
+.Fn mergesort
+function is a modified merge sort with exponential search
+intended for sorting data with pre-existing order.
+.Pp
+The
+.Fn heapsort
+function sorts an array of
+.Fa nmemb
+objects, the initial member of which is pointed to by
+.Fa base .
+The size of each object is specified by
+.Fa size .
+The
+.Fn mergesort
+function
+behaves similarly, but
+.Em requires
+that
+.Fa size
+be greater than
+.Dq "sizeof(void *) / 2" .
+.Pp
+The contents of the array
+.Fa base
+are sorted in ascending order according to
+a comparison function pointed to by
+.Fa compar ,
+which requires two arguments pointing to the objects being
+compared.
+.Pp
+The comparison function must return an integer less than, equal to, or
+greater than zero if the first argument is considered to be respectively
+less than, equal to, or greater than the second.
+.Pp
+The algorithm implemented by
+.Fn heapsort
+is
+.Em not
+stable, that is, if two members compare as equal, their order in
+the sorted array is undefined.
+The
+.Fn mergesort
+algorithm is stable.
+.Pp
+The
+.Fn heapsort
+function is an implementation of
+.An "J.W.J. William" Ns 's
+.Dq heapsort
+algorithm,
+a variant of selection sorting; in particular, see
+.An "D.E. Knuth" Ns 's
+.%T "Algorithm H" .
+.Sy Heapsort
+takes O N lg N worst-case time.
+Its
+.Em only
+advantage over
+.Fn qsort
+is that it uses almost no additional memory; while
+.Fn qsort
+does not allocate memory, it is implemented using recursion.
+.Pp
+The function
+.Fn mergesort
+requires additional memory of size
+.Fa nmemb *
+.Fa size
+bytes; it should be used only when space is not at a premium.
+The
+.Fn mergesort
+function
+is optimized for data with pre-existing order; its worst case
+time is O N lg N; its best case is O N.
+.Pp
+Normally,
+.Fn qsort
+is faster than
+.Fn mergesort
+is faster than
+.Fn heapsort .
+Memory availability and pre-existing order in the data can make this
+untrue.
+.Sh RETURN VALUES
+.Rv -std heapsort mergesort
+.Sh ERRORS
+The
+.Fn heapsort
+and
+.Fn mergesort
+functions succeed unless:
+.Bl -tag -width Er
+.It Bq Er EINVAL
+The
+.Fa size
+argument is zero, or,
+the
+.Fa size
+argument to
+.Fn mergesort
+is less than
+.Dq "sizeof(void *) / 2" .
+.It Bq Er ENOMEM
+The
+.Fn heapsort
+or
+.Fn mergesort
+functions
+were unable to allocate memory.
+.El
+.Sh SEE ALSO
+.Xr sort 1 ,
+.Xr radixsort 3
+.Rs
+.%A Williams, J.W.J
+.%D 1964
+.%T "Heapsort"
+.%J "Communications of the ACM"
+.%V 7:1
+.%P pp. 347-348
+.Re
+.Rs
+.%A Knuth, D.E.
+.%D 1968
+.%B "The Art of Computer Programming"
+.%V Vol. 3
+.%T "Sorting and Searching"
+.%P pp. 114-123, 145-149
+.Re
+.Rs
+.%A McIlroy, P.M.
+.%T "Optimistic Sorting and Information Theoretic Complexity"
+.%J "Fourth Annual ACM-SIAM Symposium on Discrete Algorithms"
+.%V January 1992
+.Re
+.Rs
+.%A Bentley, J.L.
+.%A McIlroy, M.D.
+.%T "Engineering a Sort Function"
+.%J "Software--Practice and Experience"
+.%V Vol. 23(11)
+.%P pp. 1249-1265
+.%D November\ 1993
+.Re
diff --git a/src/mergesort.3 b/src/mergesort.3
new file mode 100644
index 0000000..c1979c3
--- /dev/null
+++ b/src/mergesort.3
@@ -0,0 +1 @@
+.so man3/heapsort.3
commit be6ab549869d5d6e0ab52eac52537963fa6f71c4
Author: Guillem Jover <guillem at hadrons.org>
Date: Fri Feb 25 18:17:16 2011 +0100
Add new mergesort function
Taken from FreeBSD.
diff --git a/Makefile b/Makefile
index 4aa64ef..3c5cdce 100644
--- a/Makefile
+++ b/Makefile
@@ -28,6 +28,7 @@ LIB_SRCS := \
flopen.c \
getpeereid.c \
heapsort.c \
+ merge.c \
humanize_number.c \
dehumanize_number.c \
inet_net_pton.c \
diff --git a/Versions b/Versions
index 7c9437e..cac2efe 100644
--- a/Versions
+++ b/Versions
@@ -68,5 +68,7 @@ LIBBSD_0.2 {
LIBBSD_0.3 {
reallocf;
getpeereid;
+
+ mergesort;
} LIBBSD_0.2;
diff --git a/include/bsd/stdlib.h b/include/bsd/stdlib.h
index 9e1d640..bfe3027 100644
--- a/include/bsd/stdlib.h
+++ b/include/bsd/stdlib.h
@@ -1,7 +1,7 @@
/*
* Copyright © 2005 Aurelien Jarno
* Copyright © 2006 Robert Millan
- * Copyright © 2008, 2009, 2010 Guillem Jover
+ * Copyright © 2008-2011 Guillem Jover
*
* Redistribution and use in source and binary forms, with or without
* modification, are permitted provided that the following conditions
@@ -52,6 +52,8 @@ const char *getprogname(void);
void setprogname(const char *);
int heapsort (void *, size_t, size_t, int (*)(const void *, const void *));
+int mergesort(void *base, size_t nmemb, size_t size,
+ int (*cmp)(const void *, const void *));
void *reallocf(void *ptr, size_t size);
diff --git a/src/merge.c b/src/merge.c
new file mode 100644
index 0000000..e1078e7
--- /dev/null
+++ b/src/merge.c
@@ -0,0 +1,351 @@
+/*-
+ * Copyright (c) 1992, 1993
+ * The Regents of the University of California. All rights reserved.
+ *
+ * This code is derived from software contributed to Berkeley by
+ * Peter McIlroy.
+ *
+ * Redistribution and use in source and binary forms, with or without
+ * modification, are permitted provided that the following conditions
+ * are met:
+ * 1. Redistributions of source code must retain the above copyright
+ * notice, this list of conditions and the following disclaimer.
+ * 2. Redistributions in binary form must reproduce the above copyright
+ * notice, this list of conditions and the following disclaimer in the
+ * documentation and/or other materials provided with the distribution.
+ * 4. Neither the name of the University nor the names of its contributors
+ * may be used to endorse or promote products derived from this software
+ * without specific prior written permission.
+ *
+ * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
+ * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
+ * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
+ * ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
+ * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
+ * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
+ * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
+ * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
+ * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
+ * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
+ * SUCH DAMAGE.
+ */
+
+#if defined(LIBC_SCCS) && !defined(lint)
+static char sccsid[] = "@(#)merge.c 8.2 (Berkeley) 2/14/94";
+#endif /* LIBC_SCCS and not lint */
+#include <sys/cdefs.h>
+__FBSDID("$FreeBSD$");
+
+/*
+ * Hybrid exponential search/linear search merge sort with hybrid
+ * natural/pairwise first pass. Requires about .3% more comparisons
+ * for random data than LSMS with pairwise first pass alone.
+ * It works for objects as small as two bytes.
+ */
+
+#define NATURAL
+#define THRESHOLD 16 /* Best choice for natural merge cut-off. */
+
+/* #define NATURAL to get hybrid natural merge.
+ * (The default is pairwise merging.)
+ */
+
+#include <sys/types.h>
+
+#include <errno.h>
+#include <stdlib.h>
+#include <string.h>
+
+static void setup(u_char *, u_char *, size_t, size_t,
+ int (*)(const void *, const void *));
+static void insertionsort(u_char *, size_t, size_t,
+ int (*)(const void *, const void *));
+
+#define ISIZE sizeof(int)
+#define PSIZE sizeof(u_char *)
+#define ICOPY_LIST(src, dst, last) \
+ do \
+ *(int*)dst = *(int*)src, src += ISIZE, dst += ISIZE; \
+ while(src < last)
+#define ICOPY_ELT(src, dst, i) \
+ do \
+ *(int*) dst = *(int*) src, src += ISIZE, dst += ISIZE; \
+ while (i -= ISIZE)
+
+#define CCOPY_LIST(src, dst, last) \
+ do \
+ *dst++ = *src++; \
+ while (src < last)
+#define CCOPY_ELT(src, dst, i) \
+ do \
+ *dst++ = *src++; \
+ while (i -= 1)
+
+/*
+ * Find the next possible pointer head. (Trickery for forcing an array
+ * to do double duty as a linked list when objects do not align with word
+ * boundaries.
+ */
+/* Assumption: PSIZE is a power of 2. */
+#define EVAL(p) (u_char **) \
+ ((u_char *)0 + \
+ (((u_char *)p + PSIZE - 1 - (u_char *) 0) & ~(PSIZE - 1)))
+
+/*
+ * Arguments are as for qsort.
+ */
+int
+mergesort(base, nmemb, size, cmp)
+ void *base;
+ size_t nmemb;
+ size_t size;
+ int (*cmp)(const void *, const void *);
+{
+ size_t i;
+ int sense;
+ int big, iflag;
+ u_char *f1, *f2, *t, *b, *tp2, *q, *l1, *l2;
+ u_char *list2, *list1, *p2, *p, *last, **p1;
+
+ if (size < PSIZE / 2) { /* Pointers must fit into 2 * size. */
+ errno = EINVAL;
+ return (-1);
+ }
+
+ if (nmemb == 0)
+ return (0);
+
+ /*
+ * XXX
+ * Stupid subtraction for the Cray.
+ */
+ iflag = 0;
+ if (!(size % ISIZE) && !(((char *)base - (char *)0) % ISIZE))
+ iflag = 1;
+
+ if ((list2 = malloc(nmemb * size + PSIZE)) == NULL)
+ return (-1);
+
+ list1 = base;
+ setup(list1, list2, nmemb, size, cmp);
+ last = list2 + nmemb * size;
+ i = big = 0;
+ while (*EVAL(list2) != last) {
+ l2 = list1;
+ p1 = EVAL(list1);
+ for (tp2 = p2 = list2; p2 != last; p1 = EVAL(l2)) {
+ p2 = *EVAL(p2);
+ f1 = l2;
+ f2 = l1 = list1 + (p2 - list2);
+ if (p2 != last)
+ p2 = *EVAL(p2);
+ l2 = list1 + (p2 - list2);
+ while (f1 < l1 && f2 < l2) {
+ if ((*cmp)(f1, f2) <= 0) {
+ q = f2;
+ b = f1, t = l1;
+ sense = -1;
+ } else {
+ q = f1;
+ b = f2, t = l2;
+ sense = 0;
+ }
+ if (!big) { /* here i = 0 */
+ while ((b += size) < t && cmp(q, b) >sense)
+ if (++i == 6) {
+ big = 1;
+ goto EXPONENTIAL;
+ }
+ } else {
+EXPONENTIAL: for (i = size; ; i <<= 1)
+ if ((p = (b + i)) >= t) {
+ if ((p = t - size) > b &&
+ (*cmp)(q, p) <= sense)
+ t = p;
+ else
+ b = p;
+ break;
+ } else if ((*cmp)(q, p) <= sense) {
+ t = p;
+ if (i == size)
+ big = 0;
+ goto FASTCASE;
+ } else
+ b = p;
+ while (t > b+size) {
+ i = (((t - b) / size) >> 1) * size;
+ if ((*cmp)(q, p = b + i) <= sense)
+ t = p;
+ else
+ b = p;
+ }
+ goto COPY;
+FASTCASE: while (i > size)
+ if ((*cmp)(q,
+ p = b + (i >>= 1)) <= sense)
+ t = p;
+ else
+ b = p;
+COPY: b = t;
+ }
+ i = size;
+ if (q == f1) {
+ if (iflag) {
+ ICOPY_LIST(f2, tp2, b);
+ ICOPY_ELT(f1, tp2, i);
+ } else {
+ CCOPY_LIST(f2, tp2, b);
+ CCOPY_ELT(f1, tp2, i);
+ }
+ } else {
+ if (iflag) {
+ ICOPY_LIST(f1, tp2, b);
+ ICOPY_ELT(f2, tp2, i);
+ } else {
+ CCOPY_LIST(f1, tp2, b);
+ CCOPY_ELT(f2, tp2, i);
+ }
+ }
+ }
+ if (f2 < l2) {
+ if (iflag)
+ ICOPY_LIST(f2, tp2, l2);
+ else
+ CCOPY_LIST(f2, tp2, l2);
+ } else if (f1 < l1) {
+ if (iflag)
+ ICOPY_LIST(f1, tp2, l1);
+ else
+ CCOPY_LIST(f1, tp2, l1);
+ }
+ *p1 = l2;
+ }
+ tp2 = list1; /* swap list1, list2 */
+ list1 = list2;
+ list2 = tp2;
+ last = list2 + nmemb*size;
+ }
+ if (base == list2) {
+ memmove(list2, list1, nmemb*size);
+ list2 = list1;
+ }
+ free(list2);
+ return (0);
+}
+
+#define swap(a, b) { \
+ s = b; \
+ i = size; \
+ do { \
+ tmp = *a; *a++ = *s; *s++ = tmp; \
+ } while (--i); \
+ a -= size; \
+ }
+#define reverse(bot, top) { \
+ s = top; \
+ do { \
+ i = size; \
+ do { \
+ tmp = *bot; *bot++ = *s; *s++ = tmp; \
+ } while (--i); \
+ s -= size2; \
+ } while(bot < s); \
+}
+
+/*
+ * Optional hybrid natural/pairwise first pass. Eats up list1 in runs of
+ * increasing order, list2 in a corresponding linked list. Checks for runs
+ * when THRESHOLD/2 pairs compare with same sense. (Only used when NATURAL
+ * is defined. Otherwise simple pairwise merging is used.)
+ */
+void
+setup(list1, list2, n, size, cmp)
+ size_t n, size;
+ int (*cmp)(const void *, const void *);
+ u_char *list1, *list2;
+{
+ int i, length, size2, tmp, sense;
+ u_char *f1, *f2, *s, *l2, *last, *p2;
+
+ size2 = size*2;
+ if (n <= 5) {
+ insertionsort(list1, n, size, cmp);
+ *EVAL(list2) = (u_char*) list2 + n*size;
+ return;
+ }
+ /*
+ * Avoid running pointers out of bounds; limit n to evens
+ * for simplicity.
+ */
+ i = 4 + (n & 1);
+ insertionsort(list1 + (n - i) * size, i, size, cmp);
+ last = list1 + size * (n - i);
+ *EVAL(list2 + (last - list1)) = list2 + n * size;
+
+#ifdef NATURAL
+ p2 = list2;
+ f1 = list1;
+ sense = (cmp(f1, f1 + size) > 0);
+ for (; f1 < last; sense = !sense) {
+ length = 2;
+ /* Find pairs with same sense. */
+ for (f2 = f1 + size2; f2 < last; f2 += size2) {
+ if ((cmp(f2, f2+ size) > 0) != sense)
+ break;
+ length += 2;
+ }
+ if (length < THRESHOLD) { /* Pairwise merge */
+ do {
+ p2 = *EVAL(p2) = f1 + size2 - list1 + list2;
+ if (sense > 0)
+ swap (f1, f1 + size);
+ } while ((f1 += size2) < f2);
+ } else { /* Natural merge */
+ l2 = f2;
+ for (f2 = f1 + size2; f2 < l2; f2 += size2) {
+ if ((cmp(f2-size, f2) > 0) != sense) {
+ p2 = *EVAL(p2) = f2 - list1 + list2;
+ if (sense > 0)
+ reverse(f1, f2-size);
+ f1 = f2;
+ }
+ }
+ if (sense > 0)
+ reverse (f1, f2-size);
+ f1 = f2;
+ if (f2 < last || cmp(f2 - size, f2) > 0)
+ p2 = *EVAL(p2) = f2 - list1 + list2;
+ else
+ p2 = *EVAL(p2) = list2 + n*size;
+ }
+ }
+#else /* pairwise merge only. */
+ for (f1 = list1, p2 = list2; f1 < last; f1 += size2) {
+ p2 = *EVAL(p2) = p2 + size2;
+ if (cmp (f1, f1 + size) > 0)
+ swap(f1, f1 + size);
+ }
+#endif /* NATURAL */
+}
+
+/*
+ * This is to avoid out-of-bounds addresses in sorting the
+ * last 4 elements.
+ */
+static void
+insertionsort(a, n, size, cmp)
+ u_char *a;
+ size_t n, size;
+ int (*cmp)(const void *, const void *);
+{
+ u_char *ai, *s, *t, *u, tmp;
+ int i;
+
+ for (ai = a+size; --n >= 1; ai += size)
+ for (t = ai; t > a; t -= size) {
+ u = t - size;
+ if (cmp(u, t) <= 0)
+ break;
+ swap(u, t);
+ }
+}
commit 5b19adfa82b49bdb9b46c857b618c181f238290b
Author: Guillem Jover <guillem at hadrons.org>
Date: Fri Dec 17 10:50:56 2010 +0100
Add getpeereid function
diff --git a/Makefile b/Makefile
index fc4b9fd..4aa64ef 100644
--- a/Makefile
+++ b/Makefile
@@ -26,6 +26,7 @@ LIB_SRCS := \
err.c \
fgetln.c \
flopen.c \
+ getpeereid.c \
heapsort.c \
humanize_number.c \
dehumanize_number.c \
@@ -84,6 +85,7 @@ LIB_MANS := \
strlcat.3 \
fgetln.3 \
flopen.3 \
+ getpeereid.3 \
readpassphrase.3 \
reallocf.3 \
humanize_number.3 \
diff --git a/Versions b/Versions
index f2df70c..7c9437e 100644
--- a/Versions
+++ b/Versions
@@ -67,5 +67,6 @@ LIBBSD_0.2 {
LIBBSD_0.3 {
reallocf;
+ getpeereid;
} LIBBSD_0.2;
diff --git a/include/bsd/unistd.h b/include/bsd/unistd.h
index f9f7874..2a22fbc 100644
--- a/include/bsd/unistd.h
+++ b/include/bsd/unistd.h
@@ -39,6 +39,8 @@ mode_t getmode(const void *set, mode_t mode);
void *setmode(const char *mode_str);
void setproctitle(const char *fmt, ...);
+
+int getpeereid(int s, uid_t *euid, gid_t *egid);
__END_DECLS
#endif
diff --git a/src/getpeereid.3 b/src/getpeereid.3
new file mode 100644
index 0000000..66ae6c2
--- /dev/null
+++ b/src/getpeereid.3
@@ -0,0 +1,138 @@
+.\"
+.\" Copyright (c) 2001 Dima Dorfman.
+.\" All rights reserved.
+.\"
+.\" Redistribution and use in source and binary forms, with or without
+.\" modification, are permitted provided that the following conditions
+.\" are met:
+.\" 1. Redistributions of source code must retain the above copyright
+.\" notice, this list of conditions and the following disclaimer.
+.\" 2. Redistributions in binary form must reproduce the above copyright
+.\" notice, this list of conditions and the following disclaimer in the
+.\" documentation and/or other materials provided with the distribution.
+.\"
+.\" THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND
+.\" ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
+.\" IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
+.\" ARE DISCLAIMED. IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE
+.\" FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
+.\" DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
+.\" OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
+.\" HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
+.\" LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
+.\" OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
+.\" SUCH DAMAGE.
+.\"
+.\" $FreeBSD$
+.\"
+.Dd July 15, 2001
+.Dt GETPEEREID 3
+.Os
+.Sh NAME
+.Nm getpeereid
+.Nd get the effective credentials of a UNIX-domain peer
+.Sh LIBRARY
+.ds str-Lb-libbsd Utility functions from BSD systems (libbsd, \-lbsd)
+.Lb libbsd
+.Sh SYNOPSIS
+.In sys/types.h
+.In unistd.h
+.Ft int
+.Fn getpeereid "int s" "uid_t *euid" "gid_t *egid"
+.Sh DESCRIPTION
+The
+.Fn getpeereid
+function returns the effective user and group IDs of the
+peer connected to a
+.Ux Ns -domain
+socket.
+The argument
+.Fa s
+must be a
+.Ux Ns -domain
+socket
+.Pq Xr unix 4
+of type
+.Dv SOCK_STREAM
+on which either
+.Xr connect 2
+or
+.Xr listen 2
+have been called.
+The effective used ID is placed in
+.Fa euid ,
+and the effective group ID in
+.Fa egid .
+.Pp
+The credentials returned to the
+.Xr listen 2
+caller are those of its peer at the time it called
+.Xr connect 2 ;
+the credentials returned to the
+.Xr connect 2
+caller are those of its peer at the time it called
+.Xr listen 2 .
+This mechanism is reliable; there is no way for either side to influence
+the credentials returned to its peer except by calling the appropriate
+system call (i.e., either
+.Xr connect 2
+or
+.Xr listen 2 )
+under different effective credentials.
+.Pp
+One common use of this routine is for a
+.Ux Ns -domain
+server
+to verify the credentials of its client.
+Likewise, the client can verify the credentials of the server.
+.Sh IMPLEMENTATION NOTES
+On
+.Fx ,
+.Fn getpeereid
+is implemented in terms of the
+.Dv LOCAL_PEERCRED
+.Xr unix 4
+socket option.
+.Sh RETURN VALUES
+.Rv -std getpeereid
+.Sh ERRORS
+The
+.Fn getpeereid
+function
+fails if:
+.Bl -tag -width Er
+.It Bq Er EBADF
+The argument
+.Fa s
+is not a valid descriptor.
+.It Bq Er ENOTSOCK
+The argument
+.Fa s
+is a file, not a socket.
+.It Bq Er ENOTCONN
+The argument
+.Fa s
+does not refer to a socket on which
+.Xr connect 2
+or
+.Xr listen 2
+have been called.
+.It Bq Er EINVAL
+The argument
+.Fa s
+does not refer to a socket of type
+.Dv SOCK_STREAM ,
+or the kernel returned invalid data.
+.El
+.Sh SEE ALSO
+.Xr connect 2 ,
+.Xr getpeername 2 ,
+.Xr getsockname 2 ,
+.Xr getsockopt 2 ,
+.Xr listen 2 ,
+.Xr unix 4
+.Sh HISTORY
+The
+.Fn getpeereid
+function appeared in
+.Fx 4.6 .
diff --git a/src/getpeereid.c b/src/getpeereid.c
new file mode 100644
index 0000000..8990357
--- /dev/null
+++ b/src/getpeereid.c
@@ -0,0 +1,132 @@
+/*
+ * Copyright © 2010 Guillem Jover
+ *
+ * Redistribution and use in source and binary forms, with or without
+ * modification, are permitted provided that the following conditions
+ * are met:
+ * 1. Redistributions of source code must retain the above copyright
+ * notice, this list of conditions and the following disclaimer.
+ * 2. Redistributions in binary form must reproduce the above copyright
+ * notice, this list of conditions and the following disclaimer in the
+ * documentation and/or other materials provided with the distribution.
+ * 3. The name of the author may not be used to endorse or promote products
+ * derived from this software without specific prior written permission.
+ *
+ * THIS SOFTWARE IS PROVIDED ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES,
+ * INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY
+ * AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL
+ * THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL,
+ * EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO,
+ * PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS;
+ * OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY,
+ * WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR
+ * OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF
+ * ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
+ */
+
+#include <sys/cdefs.h>
+
+#include <sys/param.h>
+#include <sys/types.h>
+#include <sys/socket.h>
+#include <sys/un.h>
+
+#include <errno.h>
+#include <unistd.h>
+
+#if defined(SO_PEERCRED)
+/* Linux and OpenBSD */
+int
+getpeereid(int s, uid_t *euid, gid_t *egid)
+{
+/* XXX: This should be autodetected at build time instead. */
+#if defined(__linux__)
+ struct ucred cred;
+#elif defined(__OpenBSD__)
+ struct sockpeercred cred;
+#endif
+ socklen_t credlen = sizeof(cred);
+ int ret;
+
+ ret = getsockopt(s, SOL_SOCKET, SO_PEERCRED, &cred, &credlen);
+ if (ret != 0)
+ return ret;
+
+ *euid = cred.uid;
+ *egid = cred.gid;
+
+ return 0;
+}
+#elif defined(LOCAL_PEERCRED)
+/* FreeBSD */
+#include <sys/ucred.h>
+
+int
+getpeereid(int s, uid_t *euid, gid_t *egid)
+{
+ struct xucred cred;
+ socklen_t credlen = sizeof(cred);
+ int ret;
+
+ ret = getsockopt(s, 0, LOCAL_PEERCRED, &cred, &credlen);
+ if (ret != 0)
+ return ret;
+ if (cred.cr_version != XUCRED_VERSION)
+ return EINVAL;
+
+ *euid = cred.cr_uid;
+ *egid = cred.cr_gid;
+
+ return 0;
+}
+#elif defined(LOCAL_PEEREID)
+/* NetBSD */
+int
+getpeereid(int s, uid_t *euid, gid_t *egid)
+{
+ struct unpcbid cred;
+ socklen_t credlen = sizeof(cred);
+ int ret;
+
+ ret = getsockopt(s, 0, LOCAL_PEEREID, &cred, &credlen);
+ if (ret != 0)
+ return ret;
+
+ *euid = cred.unp_euid;
+ *egid = cred.unp_egid;
+
+ return 0;
+}
+#elif defined(__sun)
+/* Solaris */
+int
+getpeereid(int s, uid_t *euid, gid_t *egid)
+{
+ ucred_t cred_inst;
+ ucred_t *cred = &cred_inst;
+ int ret;
+
+ ret = getpeerucred(s, &cred);
+ if (ret != 0)
+ return ret;
+
+ *euid = ucred_geteuid(cred);
+ if (*euid < 0)
+ return -1;
+ *egid = ucred_getegid(cred);
+ if (*egid < 0)
+ return -1;
+
+ return 0;
+}
+#else
+#warning "This platform needs an implementation of getpeereid()"
+int
+getpeereid(int s, uid_t *euid, gid_t *egid)
+{
+ *euid = geteuid();
+ *egid = getegid();
+
+ return 0;
+}
+#endif
commit acb7c42d7c394acf9a59c14e5aaf4f43be309acb
Author: Aurelien Jarno <aurelien at aurel32.net>
Date: Fri Jul 30 08:00:41 2010 +0200
Add reallocf function
diff --git a/Makefile b/Makefile
index da3780a..fc4b9fd 100644
--- a/Makefile
+++ b/Makefile
@@ -33,6 +33,7 @@ LIB_SRCS := \
hash/md5.c \
pidfile.c \
readpassphrase.c \
+ reallocf.c \
setmode.c \
setproctitle.c \
strmode.c \
@@ -84,6 +85,7 @@ LIB_MANS := \
fgetln.3 \
flopen.3 \
readpassphrase.3 \
+ reallocf.3 \
humanize_number.3 \
fmtcheck.3 \
nlist.3 \
diff --git a/Versions b/Versions
index 471ffe2..f2df70c 100644
--- a/Versions
+++ b/Versions
@@ -65,3 +65,7 @@ LIBBSD_0.2 {
arc4random_uniform;
} LIBBSD_0.1;
+LIBBSD_0.3 {
+ reallocf;
+} LIBBSD_0.2;
+
diff --git a/include/bsd/stdlib.h b/include/bsd/stdlib.h
index 6e7a88c..9e1d640 100644
--- a/include/bsd/stdlib.h
+++ b/include/bsd/stdlib.h
@@ -53,6 +53,8 @@ void setprogname(const char *);
int heapsort (void *, size_t, size_t, int (*)(const void *, const void *));
+void *reallocf(void *ptr, size_t size);
+
long long strtonum(const char *nptr, long long minval, long long maxval,
const char **errstr);
__END_DECLS
diff --git a/src/reallocf.3 b/src/reallocf.3
new file mode 100644
index 0000000..eee010c
--- /dev/null
+++ b/src/reallocf.3
@@ -0,0 +1,107 @@
+.\" Copyright (c) 1980, 1991, 1993
+.\" The Regents of the University of California. All rights reserved.
+.\"
+.\" This code is derived from software contributed to Berkeley by
+.\" the American National Standards Committee X3, on Information
+.\" Processing Systems.
+.\"
+.\" Redistribution and use in source and binary forms, with or without
+.\" modification, are permitted provided that the following conditions
+.\" are met:
+.\" 1. Redistributions of source code must retain the above copyright
+.\" notice, this list of conditions and the following disclaimer.
+.\" 2. Redistributions in binary form must reproduce the above copyright
+.\" notice, this list of conditions and the following disclaimer in the
+.\" documentation and/or other materials provided with the distribution.
+.\" 3. Neither the name of the University nor the names of its contributors
+.\" may be used to endorse or promote products derived from this software
+.\" without specific prior written permission.
+.\"
+.\" THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
+.\" ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
+.\" IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
+.\" ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
+.\" FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
+.\" DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
+.\" OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
+.\" HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
+.\" LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
+.\" OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
+.\" SUCH DAMAGE.
+.\"
+.\" @(#)malloc.3 8.1 (Berkeley) 6/4/93
+.\" $FreeBSD: src/lib/libc/stdlib/malloc.3,v 1.80.2.2.2.1 2010/06/14 02:09:06 kensmith Exp $
+.\"
+.Dd September 26, 2009
+.Dt MALLOC 3
+.Os
+.Sh NAME
+.Nm reallocf
+.Nd general purpose memory allocation functions
+.Sh LIBRARY
+.ds str-Lb-libbsd Utility functions from BSD systems (libbsd, \-lbsd)
+.Lb libbsd
+.Sh SYNOPSIS
+.In stdlib.h
+.Ft void *
+.Fn reallocf "void *ptr" "size_t size"
+.Sh DESCRIPTION
+The
+.Fn reallocf
+function changes the size of the previously allocated memory referenced by
+.Fa ptr
+to
+.Fa size
+bytes.
+The contents of the memory are unchanged up to the lesser of the new and
+old sizes.
+If the new size is larger,
+the contents of the newly allocated portion of the memory are undefined.
+Upon success, the memory referenced by
+.Fa ptr
+is freed and a pointer to the newly allocated memory is returned.
+Note that
+.Fn reallocf
+may move the memory allocation, resulting in a different return value than
+.Fa ptr .
+If
+.Fa ptr
+is
+.Dv NULL ,
+the
+.Fn reallocf
+function behaves identically to
+.Fn malloc
+for the specified size.
+Upon failure, when the requested memory cannot be allocated, the passed pointer
+is freed to ease the problems with traditional coding styles for
+.Fn reallocf
+causing memory leaks in libraries.
+.Sh RETURN VALUES
+The
+.Fn reallocf
+function returns a pointer, possibly identical to
+.Fa ptr ,
+to the allocated memory
+if successful; otherwise a
+.Dv NULL
+pointer is returned, and
+.Va errno
+is set to
+.Er ENOMEM
+if the error was the result of an allocation failure.
+The buffer is deallocated in this case.
+.Sh SEE ALSO
+.Xr brk 2 ,
+.Xr mmap 2 ,
+.Xr alloca 3 ,
+.Xr calloc 3 ,
+.Xr free 3 ,
+.Xr malloc 3 ,
+.Xr posix_memalign 3 ,
+.Xr realloc 3 ,
+.Sh HISTORY
+The
+.Fn reallocf
+function first appeared in
+.Fx 3.0 .
diff --git a/src/reallocf.c b/src/reallocf.c
new file mode 100644
index 0000000..a85b5a3
--- /dev/null
+++ b/src/reallocf.c
@@ -0,0 +1,48 @@
+/*-
+ * Copyright (c) 1998, M. Warner Losh <imp at freebsd.org>
+ * All rights reserved.
+ *
+ * Redistribution and use in source and binary forms, with or without
+ * modification, are permitted provided that the following conditions
+ * are met:
+ * 1. Redistributions of source code must retain the above copyright
+ * notice, this list of conditions and the following disclaimer.
+ * 2. Redistributions in binary form must reproduce the above copyright
+ * notice, this list of conditions and the following disclaimer in the
+ * documentation and/or other materials provided with the distribution.
+ *
+ * THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND
+ * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
+ * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
+ * ARE DISCLAIMED. IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE
+ * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
+ * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
+ * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
+ * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
+ * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
+ * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
+ * SUCH DAMAGE.
+ */
+
+#include <sys/cdefs.h>
+__FBSDID("$FreeBSD$");
+
+#include <stdlib.h>
+
+void *
+reallocf(void *ptr, size_t size)
+{
+ void *nptr;
+
+ nptr = realloc(ptr, size);
+
+ /*
+ * When the System V compatibility option (malloc "V" flag) is
+ * in effect, realloc(ptr, 0) frees the memory and returns NULL.
+ * So, to avoid double free, call free() only when size != 0.
+ * realloc(ptr, 0) can't fail when ptr != NULL.
+ */
+ if (!nptr && ptr && size != 0)
+ free(ptr);
+ return (nptr);
+}
More information about the libbsd
mailing list