mirror of
https://gitlab.freedesktop.org/libbsd/libbsd.git
synced 2025-10-19 21:16:55 +02:00
Compare commits
86 Commits
Author | SHA1 | Date | |
---|---|---|---|
![]() |
7446f029b5 | ||
![]() |
e80d338b18 | ||
![]() |
b891772ad6 | ||
![]() |
b0eb19970a | ||
![]() |
0bf3d3913f | ||
![]() |
913cdd91b1 | ||
![]() |
200eeb1265 | ||
![]() |
fbd622971d | ||
![]() |
755d86be01 | ||
![]() |
cd4996cebe | ||
![]() |
a7dd4457f5 | ||
![]() |
8be40010ce | ||
![]() |
e1f2a6f869 | ||
![]() |
87dd203c26 | ||
![]() |
de2062873f | ||
![]() |
71e5db4cde | ||
![]() |
9d04217174 | ||
![]() |
17a9a8472e | ||
![]() |
94fe901eda | ||
![]() |
28585a58bd | ||
![]() |
b36c59c0ed | ||
![]() |
8b6a74775b | ||
![]() |
c594192bac | ||
![]() |
8478e57463 | ||
![]() |
f7caf2b30d | ||
![]() |
520682e596 | ||
![]() |
4c01261f39 | ||
![]() |
8a99226f16 | ||
![]() |
1497d34760 | ||
![]() |
741eb58763 | ||
![]() |
9baf9640b9 | ||
![]() |
4b95e82a32 | ||
![]() |
c766e58acf | ||
![]() |
be6ab54986 | ||
![]() |
5b19adfa82 | ||
![]() |
acb7c42d7c | ||
![]() |
06a60a166a | ||
![]() |
51863b6cf9 | ||
![]() |
08afd5d4c9 | ||
![]() |
1f0b0b23cd | ||
![]() |
32d79b0310 | ||
![]() |
cd730a02c3 | ||
![]() |
11f2c32df2 | ||
![]() |
30c794083f | ||
![]() |
ddebbd6792 | ||
![]() |
abe0a4a7e6 | ||
![]() |
2872bfa151 | ||
![]() |
e544a41f62 | ||
![]() |
7b3873bc1e | ||
![]() |
8103fe1486 | ||
![]() |
d63e081303 | ||
![]() |
3fed78e5b0 | ||
![]() |
2a81893cc0 | ||
![]() |
98a2479f0b | ||
![]() |
57cc5326cf | ||
![]() |
9e4adc4633 | ||
![]() |
db406fe24c | ||
![]() |
9396cc62cf | ||
![]() |
7a70f1b019 | ||
![]() |
ca28f28046 | ||
![]() |
dd2756e000 | ||
![]() |
3c9182b85e | ||
![]() |
4d17a18db5 | ||
![]() |
7da57b293f | ||
![]() |
254808d9ef | ||
![]() |
183cc3cbf1 | ||
![]() |
c17c7e13c3 | ||
![]() |
a5dbef45e7 | ||
![]() |
27842d7f77 | ||
![]() |
c5398adfe2 | ||
![]() |
538bc87998 | ||
![]() |
04250f6a7c | ||
![]() |
5c078ce2f5 | ||
![]() |
614eb0402a | ||
![]() |
b6e8469059 | ||
![]() |
d3e14ea99e | ||
![]() |
e51be45c40 | ||
![]() |
56ddcfe65a | ||
![]() |
1bf8b96ac8 | ||
![]() |
16e6ac99fe | ||
![]() |
56f2e55b7a | ||
![]() |
5ac14531b5 | ||
![]() |
2543c5a78b | ||
![]() |
33ef70b9e1 | ||
![]() |
8ef0ecdf44 | ||
![]() |
6660397589 |
2
.gitignore
vendored
2
.gitignore
vendored
@@ -1,5 +1,5 @@
|
|||||||
ChangeLog
|
ChangeLog
|
||||||
libbsd.pc
|
*.pc
|
||||||
*.lo
|
*.lo
|
||||||
*.o
|
*.o
|
||||||
*.so*
|
*.so*
|
||||||
|
444
COPYING
Normal file
444
COPYING
Normal file
@@ -0,0 +1,444 @@
|
|||||||
|
Copyright 1997 Niels Provos <provos@physnet.uni-hamburg.de>
|
||||||
|
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. All advertising materials mentioning features or use of this software
|
||||||
|
must display the following acknowledgement:
|
||||||
|
This product includes software developed by Niels Provos.
|
||||||
|
4. 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 ``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.
|
||||||
|
|
||||||
|
--
|
||||||
|
|
||||||
|
Copyright © 2004-2006, 2008-2011 Guillem Jover
|
||||||
|
Copyright © 2005 Hector Garcia Alvarez
|
||||||
|
Copyright © 2005 Aurelien Jarno
|
||||||
|
Copyright © 2006 Robert Millan
|
||||||
|
|
||||||
|
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.
|
||||||
|
|
||||||
|
--
|
||||||
|
|
||||||
|
Copyright © 1980, 1982, 1986, 1989-1994
|
||||||
|
The Regents of the University of California. All rights reserved.
|
||||||
|
|
||||||
|
Some code is derived from software contributed to Berkeley by
|
||||||
|
the American National Standards Committee X3, on Information
|
||||||
|
Processing Systems.
|
||||||
|
|
||||||
|
Some code is derived from software contributed to Berkeley by
|
||||||
|
Peter McIlroy.
|
||||||
|
|
||||||
|
Some code is derived from software contributed to Berkeley by
|
||||||
|
Ronnie Kon at Mindcraft Inc., Kevin Lew and Elmer Yglesias.
|
||||||
|
|
||||||
|
Some code is derived from software contributed to Berkeley by
|
||||||
|
Dave Borman at Cray Research, Inc.
|
||||||
|
|
||||||
|
Some 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.
|
||||||
|
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.
|
||||||
|
|
||||||
|
--
|
||||||
|
|
||||||
|
Copyright © 1996 Peter Wemm <peter@FreeBSD.org>.
|
||||||
|
All rights reserved.
|
||||||
|
Copyright © 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.
|
||||||
|
|
||||||
|
--
|
||||||
|
|
||||||
|
Copyright © 1997-2000, 2002, 2005, 2006, 2008 The NetBSD Foundation, Inc.
|
||||||
|
All rights reserved.
|
||||||
|
|
||||||
|
Some code was contributed to The NetBSD Foundation by Allen Briggs.
|
||||||
|
|
||||||
|
Some code is derived from software contributed to The NetBSD Foundation
|
||||||
|
by Jason R. Thorpe of the Numerical Aerospace Simulation Facility,
|
||||||
|
NASA Ames Research Center, by Luke Mewburn and by Tomas Svensson.
|
||||||
|
|
||||||
|
Some code is derived from software contributed to The NetBSD Foundation
|
||||||
|
by Julio M. Merino Vidal, developed as part of Google's Summer of Code
|
||||||
|
2005 program.
|
||||||
|
|
||||||
|
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 NETBSD FOUNDATION, INC. 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 FOUNDATION 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.
|
||||||
|
|
||||||
|
--
|
||||||
|
|
||||||
|
Copyright © 1998, M. Warner Losh <imp@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.
|
||||||
|
|
||||||
|
--
|
||||||
|
|
||||||
|
Copyright © 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.
|
||||||
|
|
||||||
|
--
|
||||||
|
|
||||||
|
Copyright © 2002 Niels Provos <provos@citi.umich.edu>
|
||||||
|
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 ``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.
|
||||||
|
|
||||||
|
--
|
||||||
|
|
||||||
|
Copyright © 2005 Pawel Jakub Dawidek <pjd@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 AUTHORS 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 AUTHORS 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.
|
||||||
|
|
||||||
|
--
|
||||||
|
|
||||||
|
Copyright © 2007 Dag-Erling Coïdan Smørgrav
|
||||||
|
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
|
||||||
|
|
||||||
|
--
|
||||||
|
|
||||||
|
Copyright © 2007 Dag-Erling Coïdan Smørgrav
|
||||||
|
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
|
||||||
|
in this position and unchanged.
|
||||||
|
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.
|
||||||
|
|
||||||
|
--
|
||||||
|
|
||||||
|
Copyright © 1998, 2000 Todd C. Miller <Todd.Miller@courtesan.com>
|
||||||
|
Copyright © 2004 Ted Unangst
|
||||||
|
|
||||||
|
Copyright © 2004 Ted Unangst and Todd Miller
|
||||||
|
All rights reserved.
|
||||||
|
|
||||||
|
Permission to use, copy, modify, and distribute this software for any
|
||||||
|
purpose with or without fee is hereby granted, provided that the above
|
||||||
|
copyright notice and this permission notice appear in all copies.
|
||||||
|
|
||||||
|
THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES
|
||||||
|
WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF
|
||||||
|
MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR
|
||||||
|
ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES
|
||||||
|
WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN
|
||||||
|
ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF
|
||||||
|
OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE.
|
||||||
|
|
||||||
|
--
|
||||||
|
|
||||||
|
Copyright © 2000-2002, 2007 Todd C. Miller <Todd.Miller@courtesan.com>
|
||||||
|
|
||||||
|
Permission to use, copy, modify, and distribute this software for any
|
||||||
|
purpose with or without fee is hereby granted, provided that the above
|
||||||
|
copyright notice and this permission notice appear in all copies.
|
||||||
|
|
||||||
|
THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES
|
||||||
|
WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF
|
||||||
|
MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR
|
||||||
|
ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES
|
||||||
|
WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN
|
||||||
|
ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF
|
||||||
|
OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE.
|
||||||
|
|
||||||
|
Sponsored in part by the Defense Advanced Research Projects
|
||||||
|
Agency (DARPA) and Air Force Research Laboratory, Air Force
|
||||||
|
Materiel Command, USAF, under agreement number F39502-99-1-0512
|
||||||
|
|
||||||
|
--
|
||||||
|
|
||||||
|
Copyright © 1996 by Internet Software Consortium.
|
||||||
|
|
||||||
|
Permission to use, copy, modify, and distribute this software for any
|
||||||
|
purpose with or without fee is hereby granted, provided that the above
|
||||||
|
copyright notice and this permission notice appear in all copies.
|
||||||
|
|
||||||
|
THE SOFTWARE IS PROVIDED "AS IS" AND INTERNET SOFTWARE CONSORTIUM DISCLAIMS
|
||||||
|
ALL WARRANTIES WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES
|
||||||
|
OF MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL INTERNET SOFTWARE
|
||||||
|
CONSORTIUM BE LIABLE FOR ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL
|
||||||
|
DAMAGES OR ANY DAMAGES WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR
|
||||||
|
PROFITS, WHETHER IN AN ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS
|
||||||
|
ACTION, ARISING OUT OF OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS
|
||||||
|
SOFTWARE.
|
||||||
|
|
||||||
|
--
|
||||||
|
|
||||||
|
Copyright © 1996, David Mazieres <dm@uun.org>
|
||||||
|
Copyright © 2008, Damien Miller <djm@openbsd.org>
|
||||||
|
|
||||||
|
Permission to use, copy, modify, and distribute this software for any
|
||||||
|
purpose with or without fee is hereby granted, provided that the above
|
||||||
|
copyright notice and this permission notice appear in all copies.
|
||||||
|
|
||||||
|
Modification and redistribution in source and binary forms is
|
||||||
|
permitted provided that due credit is given to the author and the
|
||||||
|
OpenBSD project (for instance by leaving this copyright notice
|
||||||
|
intact).
|
||||||
|
|
||||||
|
THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES
|
||||||
|
WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF
|
||||||
|
MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR
|
||||||
|
ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES
|
||||||
|
WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN
|
||||||
|
ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF
|
||||||
|
OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE.
|
||||||
|
|
||||||
|
This code is derived from section 17.1 of Applied Cryptography,
|
||||||
|
second edition, which describes a stream cipher allegedly
|
||||||
|
compatible with RSA Labs "RC4" cipher (the actual description of
|
||||||
|
which is a trade secret). The same algorithm is used as a stream
|
||||||
|
cipher called "arcfour" in Tatu Ylonen's ssh package.
|
||||||
|
|
||||||
|
Here the stream cipher has been modified always to include the time
|
||||||
|
when initializing the state. That makes it impossible to
|
||||||
|
regenerate the same random sequence twice, so this can't be used
|
||||||
|
for encryption, but will generate good random numbers.
|
||||||
|
|
||||||
|
RC4 is a registered trademark of RSA Laboratories.
|
||||||
|
|
||||||
|
--
|
||||||
|
|
||||||
|
This code implements the MD5 message-digest algorithm.
|
||||||
|
The algorithm is due to Ron Rivest. This code was
|
||||||
|
written by Colin Plumb in 1993, no copyright is claimed.
|
||||||
|
This code is in the public domain; do with it what you wish.
|
||||||
|
|
||||||
|
Equivalent code is available from RSA Data Security, Inc.
|
||||||
|
This code has been tested against that, and is equivalent,
|
||||||
|
except that you don't need to include two pages of legalese
|
||||||
|
with every copy.
|
||||||
|
|
||||||
|
To compute the message digest of a chunk of bytes, declare an
|
||||||
|
MD5Context structure, pass it to MD5Init, call MD5Update as
|
||||||
|
needed on buffers full of bytes, and then call MD5Final, which
|
||||||
|
will fill a supplied 16-byte array with the digest.
|
||||||
|
|
||||||
|
--
|
||||||
|
|
||||||
|
"THE BEER-WARE LICENSE" (Revision 42):
|
||||||
|
<phk@login.dkuug.dk> wrote this file. As long as you retain this notice you
|
||||||
|
can do whatever you want with this stuff. If we meet some day, and you think
|
||||||
|
this stuff is worth it, you can buy me a beer in return. Poul-Henning Kamp
|
154
Makefile
154
Makefile
@@ -1,86 +1,141 @@
|
|||||||
|
VERSION := $(shell ./get-version)
|
||||||
|
|
||||||
LIB_NAME := libbsd
|
LIB_NAME := libbsd
|
||||||
LIB_VERSION_MAJOR := 0
|
LIB_VERSION_MAJOR := 0
|
||||||
LIB_VERSION_MINOR := 1
|
LIB_VERSION_MINOR := 3
|
||||||
LIB_VERSION_MICRO := 4
|
LIB_VERSION_MICRO := 0
|
||||||
LIB_VERSION := $(LIB_VERSION_MAJOR).$(LIB_VERSION_MINOR).$(LIB_VERSION_MICRO)
|
LIB_VERSION := $(LIB_VERSION_MAJOR).$(LIB_VERSION_MINOR).$(LIB_VERSION_MICRO)
|
||||||
|
|
||||||
LIB_PKGCONFIG := $(LIB_NAME).pc
|
LIB_PKGCONFIG := $(LIB_NAME).pc
|
||||||
|
LIB_PKGCONFIG_OVERLAY := $(LIB_NAME)-overlay.pc
|
||||||
LIB_STATIC := $(LIB_NAME).a
|
LIB_STATIC := $(LIB_NAME).a
|
||||||
LIB_SHARED_SO := $(LIB_NAME).so
|
LIB_SHARED_SO := $(LIB_NAME).so
|
||||||
LIB_SONAME := $(LIB_SHARED_SO).$(LIB_VERSION_MAJOR)
|
LIB_SONAME := $(LIB_SHARED_SO).$(LIB_VERSION_MAJOR)
|
||||||
LIB_SHARED := $(LIB_SONAME).$(LIB_VERSION_MINOR).$(LIB_VERSION_MICRO)
|
LIB_SHARED := $(LIB_SONAME).$(LIB_VERSION_MINOR).$(LIB_VERSION_MICRO)
|
||||||
|
|
||||||
TAR_NAME := $(LIB_NAME)-$(LIB_VERSION)
|
TAR_NAME := $(LIB_NAME)-$(VERSION)
|
||||||
TAR_FILE := $(TAR_NAME).tar.gz
|
TAR_FILE := $(TAR_NAME).tar.gz
|
||||||
|
|
||||||
LIB_DIST := \
|
LIB_DIST := \
|
||||||
Makefile \
|
ChangeLog
|
||||||
README \
|
|
||||||
ChangeLog \
|
|
||||||
Versions \
|
|
||||||
$(LIB_PKGCONFIG).in
|
|
||||||
|
|
||||||
|
LIB_SRCS_GEN := \
|
||||||
|
hash/md5hl.c
|
||||||
LIB_SRCS := \
|
LIB_SRCS := \
|
||||||
arc4random.c \
|
arc4random.c \
|
||||||
bsd_getopt.c \
|
bsd_getopt.c \
|
||||||
err.c \
|
err.c \
|
||||||
fgetln.c \
|
fgetln.c \
|
||||||
|
flopen.c \
|
||||||
|
fpurge.c \
|
||||||
|
getpeereid.c \
|
||||||
heapsort.c \
|
heapsort.c \
|
||||||
|
merge.c \
|
||||||
humanize_number.c \
|
humanize_number.c \
|
||||||
|
dehumanize_number.c \
|
||||||
inet_net_pton.c \
|
inet_net_pton.c \
|
||||||
hash/md5.c hash/md5hl.c \
|
hash/md5.c \
|
||||||
|
pidfile.c \
|
||||||
|
readpassphrase.c \
|
||||||
|
reallocf.c \
|
||||||
setmode.c \
|
setmode.c \
|
||||||
|
setproctitle.c \
|
||||||
strmode.c \
|
strmode.c \
|
||||||
|
strtonum.c \
|
||||||
strlcat.c strlcpy.c \
|
strlcat.c strlcpy.c \
|
||||||
fmtcheck.c \
|
fmtcheck.c \
|
||||||
nlist.c \
|
nlist.c \
|
||||||
progname.c \
|
progname.c \
|
||||||
vis.c unvis.c
|
radixsort.c \
|
||||||
|
vis.c unvis.c \
|
||||||
|
$(LIB_SRCS_GEN)
|
||||||
|
LIB_SRCS_GEN := $(patsubst %,src/%,$(LIB_SRCS_GEN))
|
||||||
LIB_SRCS := $(patsubst %,src/%,$(LIB_SRCS))
|
LIB_SRCS := $(patsubst %,src/%,$(LIB_SRCS))
|
||||||
|
|
||||||
LIB_GEN_SRCS := \
|
|
||||||
man/md5.3bsd \
|
|
||||||
src/hash/md5hl.c
|
|
||||||
|
|
||||||
LIB_INCLUDES := \
|
LIB_INCLUDES := \
|
||||||
|
bsd/cdefs.h \
|
||||||
|
bsd/queue.h \
|
||||||
|
bsd/ip_icmp.h \
|
||||||
|
bsd/sys/cdefs.h \
|
||||||
|
bsd/sys/bitstring.h \
|
||||||
|
bsd/sys/endian.h \
|
||||||
|
bsd/sys/poll.h \
|
||||||
|
bsd/sys/queue.h \
|
||||||
|
bsd/sys/tree.h \
|
||||||
|
bsd/netinet/ip_icmp.h \
|
||||||
bsd/err.h \
|
bsd/err.h \
|
||||||
bsd/getopt.h \
|
bsd/getopt.h \
|
||||||
bsd/inet.h \
|
bsd/inet.h \
|
||||||
bsd/ip_icmp.h \
|
|
||||||
bsd/random.h \
|
bsd/random.h \
|
||||||
bsd/queue.h \
|
|
||||||
bsd/md5.h \
|
bsd/md5.h \
|
||||||
bsd/string.h \
|
bsd/string.h \
|
||||||
bsd/bsd.h \
|
bsd/bsd.h \
|
||||||
bsd/cdefs.h \
|
bsd/stdio.h \
|
||||||
bsd/stdlib.h \
|
bsd/stdlib.h \
|
||||||
|
bsd/readpassphrase.h \
|
||||||
|
bsd/unistd.h \
|
||||||
|
bsd/nlist.h \
|
||||||
|
bsd/vis.h \
|
||||||
|
bsd/libutil.h \
|
||||||
nlist.h \
|
nlist.h \
|
||||||
vis.h \
|
vis.h \
|
||||||
libutil.h
|
libutil.h
|
||||||
|
|
||||||
|
LIB_MANS_GEN := \
|
||||||
|
md5.3bsd
|
||||||
LIB_MANS := \
|
LIB_MANS := \
|
||||||
arc4random.3 \
|
arc4random.3 \
|
||||||
arc4random_addrandom.3 \
|
arc4random_addrandom.3 \
|
||||||
|
arc4random_buf.3 \
|
||||||
arc4random_stir.3 \
|
arc4random_stir.3 \
|
||||||
|
arc4random_uniform.3 \
|
||||||
|
dehumanize_number.3 \
|
||||||
|
strtonum.3 \
|
||||||
strlcpy.3 \
|
strlcpy.3 \
|
||||||
strlcat.3 \
|
strlcat.3 \
|
||||||
fgetln.3 \
|
fgetln.3 \
|
||||||
|
flopen.3 \
|
||||||
|
getpeereid.3 \
|
||||||
|
readpassphrase.3 \
|
||||||
|
reallocf.3 \
|
||||||
|
heapsort.3 \
|
||||||
humanize_number.3 \
|
humanize_number.3 \
|
||||||
fmtcheck.3 \
|
fmtcheck.3 \
|
||||||
|
mergesort.3 \
|
||||||
|
radixsort.3 \
|
||||||
|
sradixsort.3 \
|
||||||
nlist.3 \
|
nlist.3 \
|
||||||
|
pidfile.3 \
|
||||||
setmode.3 \
|
setmode.3 \
|
||||||
getmode.3 \
|
getmode.3 \
|
||||||
strmode.3 \
|
strmode.3 \
|
||||||
md5.3bsd
|
unvis.3 \
|
||||||
LIB_MANS := $(patsubst %,man/%,$(LIB_MANS))
|
vis.3 \
|
||||||
|
$(LIB_MANS_GEN)
|
||||||
|
LIB_MANS_GEN := $(patsubst %,src/%,$(LIB_MANS_GEN))
|
||||||
|
LIB_MANS := $(patsubst %,src/%,$(LIB_MANS))
|
||||||
|
|
||||||
LIB_STATIC_OBJS := $(LIB_SRCS:%.c=%.o)
|
LIB_STATIC_OBJS := $(LIB_SRCS:%.c=%.o)
|
||||||
LIB_SHARED_OBJS := $(LIB_SRCS:%.c=%.lo)
|
LIB_SHARED_OBJS := $(LIB_SRCS:%.c=%.lo)
|
||||||
|
|
||||||
# Set default value for compilation
|
AR = ar
|
||||||
CFLAGS ?= -g -Wall -Wextra -Wno-unused-variable
|
CC = gcc
|
||||||
|
CCLD = $(CC)
|
||||||
|
|
||||||
MK_CFLAGS := -Iinclude/ -include bsd/bsd.h -D_GNU_SOURCE -D__REENTRANT
|
# Set default values for user variables
|
||||||
|
CPPFLAGS ?=
|
||||||
|
CFLAGS ?= -g -Wall -Wextra -Wno-unused-variable
|
||||||
|
LDFLAGS ?=
|
||||||
|
|
||||||
|
# Internal makefile variables
|
||||||
|
MK_CPPFLAGS := -Iinclude/bsd/ -Iinclude/ \
|
||||||
|
-DLIBBSD_OVERLAY -DLIBBSD_DISABLE_DEPRECATED \
|
||||||
|
-D_GNU_SOURCE -D__REENTRANT
|
||||||
|
MK_CFLAGS :=
|
||||||
|
MK_LDFLAGS :=
|
||||||
|
|
||||||
|
COMPILE = $(CC) $(MK_CPPFLAGS) $(CPPFLAGS) $(MK_CFLAGS) $(CFLAGS)
|
||||||
|
LINK = $(CCLD) $(MK_CFLAGS) $(CFLAGS) $(MK_LDFLAGS) $(LDFLAGS)
|
||||||
|
|
||||||
prefix = /usr
|
prefix = /usr
|
||||||
exec_prefix =
|
exec_prefix =
|
||||||
@@ -91,25 +146,25 @@ pkgconfigdir = ${usrlibdir}/pkgconfig
|
|||||||
mandir = ${prefix}/share/man
|
mandir = ${prefix}/share/man
|
||||||
|
|
||||||
.PHONY: libs
|
.PHONY: libs
|
||||||
libs: $(LIB_STATIC) $(LIB_SHARED_SO) $(LIB_PKGCONFIG)
|
libs: $(LIB_STATIC) $(LIB_SHARED_SO) $(LIB_PKGCONFIG) $(LIB_PKGCONFIG_OVERLAY)
|
||||||
|
|
||||||
.PHONY: man
|
.PHONY: man
|
||||||
man: $(LIB_MANS)
|
man: $(LIB_MANS)
|
||||||
|
|
||||||
%.lo: %.c
|
%.lo: %.c
|
||||||
$(CC) -o $@ $(MK_CFLAGS) $(CFLAGS) -DPIC -fPIC -c $<
|
$(COMPILE) -o $@ -DPIC -fPIC -c $<
|
||||||
|
|
||||||
%.o: %.c
|
%.o: %.c
|
||||||
$(CC) -o $@ $(MK_CFLAGS) $(CFLAGS) -c $<
|
$(COMPILE) -o $@ -c $<
|
||||||
|
|
||||||
man/md5.3bsd: man/mdX.3
|
src/md5.3bsd: src/mdX.3
|
||||||
sed -e 's/mdX/md5/g' -e 's/mdY/md4/g' -e 's/MDX/MD5/g' $< > $@
|
sed -e 's/mdX/md5/g' -e 's/mdY/md4/g' -e 's/MDX/MD5/g' $< > $@
|
||||||
|
|
||||||
src/hash/md5hl.c: src/hash/helper.c
|
src/hash/md5hl.c: src/hash/helper.c
|
||||||
sed -e 's:hashinc:bsd/md5.h:g' -e 's:HASH:MD5:g' $< > $@
|
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@:$(LIB_VERSION):' \
|
sed -e 's:@VERSION@:$(VERSION):' \
|
||||||
-e 's:@prefix@:$(value prefix):' \
|
-e 's:@prefix@:$(value prefix):' \
|
||||||
-e 's:@exec_prefix@:$(value exec_prefix):' \
|
-e 's:@exec_prefix@:$(value exec_prefix):' \
|
||||||
-e 's:@libdir@:$(value usrlibdir):' \
|
-e 's:@libdir@:$(value usrlibdir):' \
|
||||||
@@ -117,7 +172,7 @@ $(LIB_PKGCONFIG): $(LIB_PKGCONFIG).in
|
|||||||
$< > $@
|
$< > $@
|
||||||
|
|
||||||
$(LIB_STATIC): $(LIB_STATIC_OBJS)
|
$(LIB_STATIC): $(LIB_STATIC_OBJS)
|
||||||
ar rcs $@ $^
|
$(AR) rcs $@ $^
|
||||||
|
|
||||||
$(LIB_SHARED_SO): $(LIB_SONAME)
|
$(LIB_SHARED_SO): $(LIB_SONAME)
|
||||||
ln -fs $^ $@
|
ln -fs $^ $@
|
||||||
@@ -126,7 +181,8 @@ $(LIB_SONAME): $(LIB_SHARED)
|
|||||||
ln -fs $^ $@
|
ln -fs $^ $@
|
||||||
|
|
||||||
$(LIB_SHARED): $(LIB_SHARED_OBJS)
|
$(LIB_SHARED): $(LIB_SHARED_OBJS)
|
||||||
gcc -shared \
|
$(LINK) \
|
||||||
|
-shared \
|
||||||
-Wl,-soname -Wl,$(LIB_SONAME) \
|
-Wl,-soname -Wl,$(LIB_SONAME) \
|
||||||
-Wl,--version-script=Versions \
|
-Wl,--version-script=Versions \
|
||||||
-o $@ $^
|
-o $@ $^
|
||||||
@@ -136,42 +192,46 @@ ChangeLog:
|
|||||||
-git log --stat -C >$@
|
-git log --stat -C >$@
|
||||||
|
|
||||||
.PHONY: dist
|
.PHONY: dist
|
||||||
dist: ChangeLog
|
dist: $(LIB_DIST)
|
||||||
mkdir $(TAR_NAME)
|
mkdir $(TAR_NAME)
|
||||||
cp -a include src man $(LIB_DIST) $(TAR_NAME)
|
echo $(VERSION) >$(TAR_NAME)/.dist-version
|
||||||
|
cp -a --parents $(LIB_DIST) `git ls-files` $(TAR_NAME)
|
||||||
tar czf $(TAR_FILE) --exclude=.gitignore $(TAR_NAME)
|
tar czf $(TAR_FILE) --exclude=.gitignore $(TAR_NAME)
|
||||||
rm -rf $(TAR_NAME)
|
rm -rf $(TAR_NAME)
|
||||||
gpg -a -b $(TAR_FILE)
|
gpg -a -b $(TAR_FILE)
|
||||||
|
|
||||||
.PHONY: install
|
.PHONY: install
|
||||||
install: libs man
|
install: libs man
|
||||||
mkdir -p $(DESTDIR)/$(libdir)
|
mkdir -p $(DESTDIR)$(libdir)
|
||||||
mkdir -p $(DESTDIR)/$(usrlibdir)
|
mkdir -p $(DESTDIR)$(usrlibdir)
|
||||||
mkdir -p $(DESTDIR)/$(includedir)/bsd/
|
mkdir -p $(DESTDIR)$(includedir)/bsd/
|
||||||
mkdir -p $(DESTDIR)/$(mandir)/man3
|
mkdir -p $(DESTDIR)$(includedir)/bsd/sys/
|
||||||
mkdir -p $(DESTDIR)/$(pkgconfigdir)
|
mkdir -p $(DESTDIR)$(includedir)/bsd/netinet/
|
||||||
install -m644 $(LIB_STATIC) $(DESTDIR)/$(usrlibdir)
|
mkdir -p $(DESTDIR)$(mandir)/man3
|
||||||
install -m644 $(LIB_SHARED) $(DESTDIR)/$(libdir)
|
mkdir -p $(DESTDIR)$(pkgconfigdir)
|
||||||
|
install -m644 $(LIB_STATIC) $(DESTDIR)$(usrlibdir)
|
||||||
|
install -m755 $(LIB_SHARED) $(DESTDIR)$(libdir)
|
||||||
for i in $(LIB_INCLUDES); do \
|
for i in $(LIB_INCLUDES); do \
|
||||||
install -m644 include/$$i $(DESTDIR)/$(includedir)/$$i; \
|
install -m644 include/$$i $(DESTDIR)$(includedir)/$$i; \
|
||||||
done
|
done
|
||||||
install -m644 $(LIB_MANS) $(DESTDIR)/$(mandir)/man3
|
install -m644 $(LIB_MANS) $(DESTDIR)$(mandir)/man3
|
||||||
install -m644 $(LIB_PKGCONFIG) $(DESTDIR)/$(pkgconfigdir)
|
install -m644 $(LIB_PKGCONFIG) $(DESTDIR)$(pkgconfigdir)
|
||||||
|
install -m644 $(LIB_PKGCONFIG_OVERLAY) $(DESTDIR)$(pkgconfigdir)
|
||||||
ifeq ($(libdir),$(usrlibdir))
|
ifeq ($(libdir),$(usrlibdir))
|
||||||
# If both dirs are the same, do a relative symlink.
|
# If both dirs are the same, do a relative symlink.
|
||||||
ln -sf $(LIB_SHARED) $(DESTDIR)/$(usrlibdir)/$(LIB_SHARED_SO)
|
ln -sf $(LIB_SHARED) $(DESTDIR)$(usrlibdir)/$(LIB_SHARED_SO)
|
||||||
else
|
else
|
||||||
# Otherwise, do an absolute one.
|
# Otherwise, do an absolute one.
|
||||||
ln -sf $(libdir)/$(LIB_SHARED) $(DESTDIR)/$(usrlibdir)/$(LIB_SHARED_SO)
|
ln -sf $(libdir)/$(LIB_SHARED) $(DESTDIR)$(usrlibdir)/$(LIB_SHARED_SO)
|
||||||
endif
|
endif
|
||||||
ln -sf $(LIB_SHARED) $(DESTDIR)/$(libdir)/$(LIB_SONAME)
|
ln -sf $(LIB_SHARED) $(DESTDIR)$(libdir)/$(LIB_SONAME)
|
||||||
|
|
||||||
.PHONY: clean
|
.PHONY: clean
|
||||||
clean:
|
clean:
|
||||||
rm -f $(LIB_PKGCONFIG)
|
rm -f $(LIB_PKGCONFIG)
|
||||||
rm -f $(LIB_GEN_SRCS)
|
rm -f $(LIB_PKGCONFIG_OVERLAY)
|
||||||
|
rm -f $(LIB_SRCS_GEN) $(LIB_MANS_GEN)
|
||||||
rm -f $(LIB_STATIC_OBJS)
|
rm -f $(LIB_STATIC_OBJS)
|
||||||
rm -f $(LIB_STATIC)
|
rm -f $(LIB_STATIC)
|
||||||
rm -f $(LIB_SHARED_OBJS)
|
rm -f $(LIB_SHARED_OBJS)
|
||||||
rm -f $(LIB_SHARED) $(LIB_SONAME) $(LIB_SHARED_SO)
|
rm -f $(LIB_SHARED) $(LIB_SONAME) $(LIB_SHARED_SO)
|
||||||
|
|
||||||
|
3
README
3
README
@@ -27,6 +27,5 @@ The mail address is:
|
|||||||
Source Repository
|
Source Repository
|
||||||
-----------------
|
-----------------
|
||||||
|
|
||||||
<http://gitweb.freedesktop.org/?p=libbsd.git>
|
<http://cgit.freedesktop.org/libbsd>
|
||||||
<git://anongit.freedesktop.org/git/libbsd>
|
<git://anongit.freedesktop.org/git/libbsd>
|
||||||
|
|
||||||
|
1
TODO
1
TODO
@@ -1,4 +1,3 @@
|
|||||||
* Add more functions used by ported packages (check openssh).
|
* Add more functions used by ported packages (check openssh).
|
||||||
* Fix includes on man pages.
|
|
||||||
* Add missing man pages.
|
* Add missing man pages.
|
||||||
* Add a README.import file.
|
* Add a README.import file.
|
||||||
|
61
Versions
61
Versions
@@ -3,24 +3,38 @@ LIBBSD_0.0 {
|
|||||||
arc4random;
|
arc4random;
|
||||||
arc4random_stir;
|
arc4random_stir;
|
||||||
arc4random_addrandom;
|
arc4random_addrandom;
|
||||||
bsd_getopt; optreset;
|
|
||||||
errc; warnc; verrc; vwarnc;
|
bsd_getopt;
|
||||||
|
optreset;
|
||||||
|
|
||||||
|
errc;
|
||||||
|
warnc;
|
||||||
|
verrc;
|
||||||
|
vwarnc;
|
||||||
|
|
||||||
fgetln;
|
fgetln;
|
||||||
fgetwln;
|
|
||||||
fmtcheck;
|
fmtcheck;
|
||||||
heapsort;
|
heapsort;
|
||||||
humanize_number;
|
humanize_number;
|
||||||
inet_net_pton;
|
|
||||||
|
|
||||||
getprogname; setprogname;
|
inet_net_pton; /* XXX: Already provided by glibc, remove. */
|
||||||
|
|
||||||
|
getprogname;
|
||||||
|
setprogname;
|
||||||
|
|
||||||
strlcpy;
|
strlcpy;
|
||||||
strlcat;
|
strlcat;
|
||||||
|
|
||||||
setmode;
|
setmode;
|
||||||
getmode;
|
getmode;
|
||||||
|
|
||||||
vis; strvis; strvisx;
|
vis;
|
||||||
unvis; strunvis; strunvisx;
|
strvis;
|
||||||
|
strvisx;
|
||||||
|
unvis;
|
||||||
|
strunvis;
|
||||||
|
strunvisx;
|
||||||
|
|
||||||
MD5Init;
|
MD5Init;
|
||||||
MD5Update;
|
MD5Update;
|
||||||
MD5Pad;
|
MD5Pad;
|
||||||
@@ -42,3 +56,36 @@ LIBBSD_0.1 {
|
|||||||
nlist;
|
nlist;
|
||||||
} LIBBSD_0.0;
|
} LIBBSD_0.0;
|
||||||
|
|
||||||
|
LIBBSD_0.2 {
|
||||||
|
strtonum;
|
||||||
|
|
||||||
|
strnvis;
|
||||||
|
strnunvis;
|
||||||
|
|
||||||
|
dehumanize_number;
|
||||||
|
|
||||||
|
readpassphrase;
|
||||||
|
|
||||||
|
flopen;
|
||||||
|
|
||||||
|
pidfile_open;
|
||||||
|
pidfile_write;
|
||||||
|
pidfile_close;
|
||||||
|
pidfile_remove;
|
||||||
|
|
||||||
|
setproctitle;
|
||||||
|
|
||||||
|
arc4random_buf;
|
||||||
|
arc4random_uniform;
|
||||||
|
} LIBBSD_0.1;
|
||||||
|
|
||||||
|
LIBBSD_0.3 {
|
||||||
|
reallocf;
|
||||||
|
getpeereid;
|
||||||
|
|
||||||
|
mergesort;
|
||||||
|
radixsort;
|
||||||
|
sradixsort;
|
||||||
|
|
||||||
|
fpurge;
|
||||||
|
} LIBBSD_0.2;
|
||||||
|
47
get-version
Executable file
47
get-version
Executable file
@@ -0,0 +1,47 @@
|
|||||||
|
#!/bin/sh
|
||||||
|
#
|
||||||
|
# get-version
|
||||||
|
#
|
||||||
|
# Copyright © 2009 Guillem Jover <guillem@hadrons.org>
|
||||||
|
#
|
||||||
|
# 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.
|
||||||
|
|
||||||
|
if [ -f .dist-version ]; then
|
||||||
|
# Get the version from the file distributed in the tarball.
|
||||||
|
version=$(cat .dist-version)
|
||||||
|
elif [ -d .git ]; then
|
||||||
|
# Ger the version from the git repository.
|
||||||
|
version=$(git describe --abbrev=4 HEAD 2>/dev/null)
|
||||||
|
|
||||||
|
# Check if we are on a dirty checkout.
|
||||||
|
git update-index --refresh -q >/dev/null
|
||||||
|
dirty=$(git diff-index --name-only HEAD 2>/dev/null)
|
||||||
|
if [ -n "$dirty" ]; then
|
||||||
|
version="$version-dirty"
|
||||||
|
fi
|
||||||
|
else
|
||||||
|
echo "error: cannot get project version." 1>&2
|
||||||
|
exit 1
|
||||||
|
fi
|
||||||
|
|
||||||
|
echo "$version"
|
@@ -31,15 +31,19 @@
|
|||||||
* Include all bsd compat headers.
|
* Include all bsd compat headers.
|
||||||
*/
|
*/
|
||||||
|
|
||||||
#include <bsd/cdefs.h>
|
#include <bsd/sys/cdefs.h>
|
||||||
|
#include <bsd/sys/queue.h>
|
||||||
|
#include <bsd/sys/tree.h>
|
||||||
|
#include <bsd/netinet/ip_icmp.h>
|
||||||
#include <bsd/stdlib.h>
|
#include <bsd/stdlib.h>
|
||||||
#include <bsd/string.h>
|
#include <bsd/string.h>
|
||||||
#include <bsd/err.h>
|
#include <bsd/err.h>
|
||||||
#include <bsd/getopt.h>
|
#include <bsd/getopt.h>
|
||||||
#include <bsd/random.h>
|
|
||||||
#include <bsd/md5.h>
|
#include <bsd/md5.h>
|
||||||
#include <bsd/queue.h>
|
|
||||||
#include <bsd/ip_icmp.h>
|
/* FIXME: Will be removed in the future. */
|
||||||
|
#ifndef LIBBSD_DISABLE_DEPRECATED
|
||||||
#include <time.h>
|
#include <time.h>
|
||||||
|
#endif
|
||||||
|
|
||||||
#endif
|
#endif
|
||||||
|
@@ -1,5 +1,5 @@
|
|||||||
/*
|
/*
|
||||||
* Copyright © 2004, 2005, 2006, 2009 Guillem Jover
|
* Copyright © 2009 Guillem Jover
|
||||||
*
|
*
|
||||||
* Redistribution and use in source and binary forms, with or without
|
* Redistribution and use in source and binary forms, with or without
|
||||||
* modification, are permitted provided that the following conditions
|
* modification, are permitted provided that the following conditions
|
||||||
@@ -24,49 +24,19 @@
|
|||||||
* ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
|
* ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
|
||||||
*/
|
*/
|
||||||
|
|
||||||
#ifndef LIBBSD_CDEFS_H
|
#ifndef LIBBSD_BSD_CDEFS_H
|
||||||
#define LIBBSD_CDEFS_H
|
#define LIBBSD_BSD_CDEFS_H
|
||||||
|
|
||||||
|
#ifdef LIBBSD_DISABLE_DEPRECATED
|
||||||
|
#error "Deprecated header, use <bsd/sys/cdefs.h> or libbsd-overlay.pc instead."
|
||||||
|
#else
|
||||||
|
#warning "Deprecated header, use <bsd/sys/cdefs.h> or libbsd-overlay.pc instead."
|
||||||
|
#endif
|
||||||
|
|
||||||
|
#ifdef LIBBSD_OVERLAY
|
||||||
#include <sys/cdefs.h>
|
#include <sys/cdefs.h>
|
||||||
|
#else
|
||||||
#ifndef setproctitle
|
#include <bsd/sys/cdefs.h>
|
||||||
# define setproctitle(fmt, args...)
|
|
||||||
#endif
|
|
||||||
|
|
||||||
#ifndef __dead2
|
|
||||||
# define __dead2
|
|
||||||
#endif
|
|
||||||
|
|
||||||
/* Linux headers define a struct with a member names __unused.
|
|
||||||
* Disable for now. */
|
|
||||||
#if 0
|
|
||||||
#ifndef __unused
|
|
||||||
# ifdef __GNUC__
|
|
||||||
# define __unused __attribute__((unused))
|
|
||||||
# else
|
|
||||||
# define __unused
|
|
||||||
# endif
|
|
||||||
#endif
|
|
||||||
#endif
|
|
||||||
|
|
||||||
#ifndef __printflike
|
|
||||||
# ifdef __GNUC__
|
|
||||||
# define __printflike(x, y) __attribute((format(printf, (x), (y))))
|
|
||||||
# else
|
|
||||||
# define __printflike(x, y)
|
|
||||||
# endif
|
|
||||||
#endif
|
|
||||||
|
|
||||||
#ifndef __bounded__
|
|
||||||
# define __bounded__(x, y, z)
|
|
||||||
#endif
|
|
||||||
|
|
||||||
#ifndef __RCSID
|
|
||||||
# define __RCSID(x)
|
|
||||||
#endif
|
|
||||||
|
|
||||||
#ifndef __FBSDID
|
|
||||||
# define __FBSDID(x)
|
|
||||||
#endif
|
#endif
|
||||||
|
|
||||||
#endif
|
#endif
|
||||||
|
@@ -1,6 +1,6 @@
|
|||||||
/*
|
/*
|
||||||
* Copyright © 2006 Robert Millan
|
* Copyright © 2006 Robert Millan
|
||||||
* Copyright © 2009 Guillem Jover
|
* Copyright © 2009, 2011 Guillem Jover
|
||||||
*
|
*
|
||||||
* Redistribution and use in source and binary forms, with or without
|
* Redistribution and use in source and binary forms, with or without
|
||||||
* modification, are permitted provided that the following conditions
|
* modification, are permitted provided that the following conditions
|
||||||
@@ -29,9 +29,15 @@
|
|||||||
#define LIBBSD_ERR_H
|
#define LIBBSD_ERR_H
|
||||||
|
|
||||||
#include <sys/cdefs.h>
|
#include <sys/cdefs.h>
|
||||||
#include <err.h>
|
|
||||||
#include <stdarg.h>
|
#include <stdarg.h>
|
||||||
|
|
||||||
|
#ifdef LIBBSD_OVERLAY
|
||||||
|
#include_next <err.h>
|
||||||
|
#else
|
||||||
|
#include <err.h>
|
||||||
|
#endif
|
||||||
|
|
||||||
__BEGIN_DECLS
|
__BEGIN_DECLS
|
||||||
extern void warnc (int code, const char *format, ...);
|
extern void warnc (int code, const char *format, ...);
|
||||||
extern void vwarnc (int code, const char *format, va_list ap);
|
extern void vwarnc (int code, const char *format, va_list ap);
|
||||||
|
@@ -1,6 +1,5 @@
|
|||||||
/*
|
/*
|
||||||
* Copyright © 2006 Robert Millan
|
* Copyright © 2011 Guillem Jover
|
||||||
* Copyright © 2009 Guillem Jover
|
|
||||||
*
|
*
|
||||||
* Redistribution and use in source and binary forms, with or without
|
* Redistribution and use in source and binary forms, with or without
|
||||||
* modification, are permitted provided that the following conditions
|
* modification, are permitted provided that the following conditions
|
||||||
@@ -28,13 +27,13 @@
|
|||||||
#ifndef LIBBSD_GETOPT_H
|
#ifndef LIBBSD_GETOPT_H
|
||||||
#define LIBBSD_GETOPT_H
|
#define LIBBSD_GETOPT_H
|
||||||
|
|
||||||
#include <sys/cdefs.h>
|
#ifdef LIBBSD_OVERLAY
|
||||||
|
#include_next <getopt.h>
|
||||||
|
#include <unistd.h>
|
||||||
|
#else
|
||||||
|
#warning "Deprecated header, use <bsd/unistd.h> or <unistd.h> with libbsd-overlay.pc instead."
|
||||||
#include <getopt.h>
|
#include <getopt.h>
|
||||||
|
#include <bsd/unistd.h>
|
||||||
__BEGIN_DECLS
|
#endif
|
||||||
extern int optreset;
|
|
||||||
|
|
||||||
int bsd_getopt (int, char **, char *);
|
|
||||||
__END_DECLS
|
|
||||||
|
|
||||||
#endif
|
#endif
|
||||||
|
@@ -27,12 +27,12 @@
|
|||||||
#ifndef LIBBSD_INET_H
|
#ifndef LIBBSD_INET_H
|
||||||
#define LIBBSD_INET_H
|
#define LIBBSD_INET_H
|
||||||
|
|
||||||
#include <sys/cdefs.h>
|
#ifdef LIBBSD_DISABLE_DEPRECATED
|
||||||
#include <stddef.h>
|
#error "Deprecated header, use <arpa/inet.h> instead."
|
||||||
|
#else
|
||||||
__BEGIN_DECLS
|
#warning "Deprecated header, use <arpa/inet.h> instead."
|
||||||
int inet_net_pton(int af, const char *src, void *dst, siez_t size);
|
|
||||||
__END_DECLS
|
|
||||||
|
|
||||||
#endif
|
#endif
|
||||||
|
|
||||||
|
#include <arpa/inet.h>
|
||||||
|
|
||||||
|
#endif
|
||||||
|
@@ -1,6 +1,5 @@
|
|||||||
/*
|
/*
|
||||||
* Copyright (c) 1982, 1986, 1993
|
* Copyright © 2009 Guillem Jover
|
||||||
* The Regents of the University of California. All rights reserved.
|
|
||||||
*
|
*
|
||||||
* Redistribution and use in source and binary forms, with or without
|
* Redistribution and use in source and binary forms, with or without
|
||||||
* modification, are permitted provided that the following conditions
|
* modification, are permitted provided that the following conditions
|
||||||
@@ -10,194 +9,34 @@
|
|||||||
* 2. Redistributions in binary form must reproduce the above copyright
|
* 2. Redistributions in binary form must reproduce the above copyright
|
||||||
* notice, this list of conditions and the following disclaimer in the
|
* notice, this list of conditions and the following disclaimer in the
|
||||||
* documentation and/or other materials provided with the distribution.
|
* documentation and/or other materials provided with the distribution.
|
||||||
* 3. Neither the name of the University nor the names of its contributors
|
* 3. The name of the author may not be used to endorse or promote products
|
||||||
* may be used to endorse or promote products derived from this software
|
* derived from this software without specific prior written permission.
|
||||||
* without specific prior written permission.
|
|
||||||
*
|
*
|
||||||
* THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
|
* THIS SOFTWARE IS PROVIDED ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES,
|
||||||
* ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
|
* INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY
|
||||||
* IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
|
* AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL
|
||||||
* ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
|
* THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL,
|
||||||
* FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
|
* EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO,
|
||||||
* DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
|
* PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS;
|
||||||
* OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
|
* OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY,
|
||||||
* HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
|
* WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR
|
||||||
* LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
|
* OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF
|
||||||
* OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
|
* ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
|
||||||
* SUCH DAMAGE.
|
|
||||||
*
|
|
||||||
* @(#)ip_icmp.h 8.1 (Berkeley) 6/10/93
|
|
||||||
* $FreeBSD: src/sys/netinet/ip_icmp.h,v 1.22 2004/04/07 20:46:13 imp Exp $
|
|
||||||
*/
|
*/
|
||||||
|
|
||||||
#ifndef _NETINET_IP_ICMP_H_
|
#ifndef LIBBSD_BSD_IP_ICMP_H
|
||||||
#define _NETINET_IP_ICMP_H_
|
#define LIBBSD_BSD_IP_ICMP_H
|
||||||
|
|
||||||
#include <sys/types.h> /* u_int32_t, u_char */
|
#ifdef LIBBSD_DISABLE_DEPRECATED
|
||||||
#include <netinet/in.h> /* in_addr */
|
#error "Deprecated header, use <bsd/netinet/ip_icmp.h> or libbsd-overlay.pc instead."
|
||||||
#include <netinet/in_systm.h> /* n_short */
|
#else
|
||||||
#include <netinet/ip.h> /* idi_ip */
|
#warning "Deprecated header, use <bsd/netinet/ip_icmp.h> or libbsd-overlay.pc instead."
|
||||||
|
#endif
|
||||||
|
|
||||||
/*
|
#ifdef LIBBSD_OVERLAY
|
||||||
* Interface Control Message Protocol Definitions.
|
#include <netinet/ip_icmp.h>
|
||||||
* Per RFC 792, September 1981.
|
#else
|
||||||
*/
|
#include <bsd/netinet/ip_icmp.h>
|
||||||
|
|
||||||
/*
|
|
||||||
* Internal of an ICMP Router Advertisement
|
|
||||||
*/
|
|
||||||
struct icmp_ra_addr {
|
|
||||||
u_int32_t ira_addr;
|
|
||||||
u_int32_t ira_preference;
|
|
||||||
};
|
|
||||||
|
|
||||||
/*
|
|
||||||
* Structure of an icmp header.
|
|
||||||
*/
|
|
||||||
struct icmp {
|
|
||||||
u_char icmp_type; /* type of message, see below */
|
|
||||||
u_char icmp_code; /* type sub code */
|
|
||||||
u_short icmp_cksum; /* ones complement cksum of struct */
|
|
||||||
union {
|
|
||||||
u_char ih_pptr; /* ICMP_PARAMPROB */
|
|
||||||
struct in_addr ih_gwaddr; /* ICMP_REDIRECT */
|
|
||||||
struct ih_idseq {
|
|
||||||
n_short icd_id;
|
|
||||||
n_short icd_seq;
|
|
||||||
} ih_idseq;
|
|
||||||
int ih_void;
|
|
||||||
|
|
||||||
/* ICMP_UNREACH_NEEDFRAG -- Path MTU Discovery (RFC1191) */
|
|
||||||
struct ih_pmtu {
|
|
||||||
n_short ipm_void;
|
|
||||||
n_short ipm_nextmtu;
|
|
||||||
} ih_pmtu;
|
|
||||||
|
|
||||||
struct ih_rtradv {
|
|
||||||
u_char irt_num_addrs;
|
|
||||||
u_char irt_wpa;
|
|
||||||
u_int16_t irt_lifetime;
|
|
||||||
} ih_rtradv;
|
|
||||||
} icmp_hun;
|
|
||||||
#define icmp_pptr icmp_hun.ih_pptr
|
|
||||||
#define icmp_gwaddr icmp_hun.ih_gwaddr
|
|
||||||
#define icmp_id icmp_hun.ih_idseq.icd_id
|
|
||||||
#define icmp_seq icmp_hun.ih_idseq.icd_seq
|
|
||||||
#define icmp_void icmp_hun.ih_void
|
|
||||||
#define icmp_pmvoid icmp_hun.ih_pmtu.ipm_void
|
|
||||||
#define icmp_nextmtu icmp_hun.ih_pmtu.ipm_nextmtu
|
|
||||||
#define icmp_num_addrs icmp_hun.ih_rtradv.irt_num_addrs
|
|
||||||
#define icmp_wpa icmp_hun.ih_rtradv.irt_wpa
|
|
||||||
#define icmp_lifetime icmp_hun.ih_rtradv.irt_lifetime
|
|
||||||
union {
|
|
||||||
struct id_ts { /* ICMP Timestamp */
|
|
||||||
n_time its_otime; /* Originate */
|
|
||||||
n_time its_rtime; /* Receive */
|
|
||||||
n_time its_ttime; /* Transmit */
|
|
||||||
} id_ts;
|
|
||||||
struct id_ip {
|
|
||||||
struct ip idi_ip;
|
|
||||||
/* options and then 64 bits of data */
|
|
||||||
} id_ip;
|
|
||||||
struct icmp_ra_addr id_radv;
|
|
||||||
u_int32_t id_mask;
|
|
||||||
char id_data[1];
|
|
||||||
} icmp_dun;
|
|
||||||
#define icmp_otime icmp_dun.id_ts.its_otime
|
|
||||||
#define icmp_rtime icmp_dun.id_ts.its_rtime
|
|
||||||
#define icmp_ttime icmp_dun.id_ts.its_ttime
|
|
||||||
#define icmp_ip icmp_dun.id_ip.idi_ip
|
|
||||||
#define icmp_radv icmp_dun.id_radv
|
|
||||||
#define icmp_mask icmp_dun.id_mask
|
|
||||||
#define icmp_data icmp_dun.id_data
|
|
||||||
};
|
|
||||||
|
|
||||||
/*
|
|
||||||
* Lower bounds on packet lengths for various types.
|
|
||||||
* For the error advice packets must first insure that the
|
|
||||||
* packet is large enough to contain the returned ip header.
|
|
||||||
* Only then can we do the check to see if 64 bits of packet
|
|
||||||
* data have been returned, since we need to check the returned
|
|
||||||
* ip header length.
|
|
||||||
*/
|
|
||||||
#define ICMP_MINLEN 8 /* abs minimum */
|
|
||||||
#define ICMP_TSLEN (8 + 3 * sizeof (n_time)) /* timestamp */
|
|
||||||
#define ICMP_MASKLEN 12 /* address mask */
|
|
||||||
#define ICMP_ADVLENMIN (8 + sizeof (struct ip) + 8) /* min */
|
|
||||||
#define ICMP_ADVLEN(p) (8 + ((p)->icmp_ip.ip_hl << 2) + 8)
|
|
||||||
/* N.B.: must separately check that ip_hl >= 5 */
|
|
||||||
|
|
||||||
/*
|
|
||||||
* Definition of type and code field values.
|
|
||||||
*/
|
|
||||||
#define ICMP_ECHOREPLY 0 /* echo reply */
|
|
||||||
#define ICMP_UNREACH 3 /* dest unreachable, codes: */
|
|
||||||
#define ICMP_UNREACH_NET 0 /* bad net */
|
|
||||||
#define ICMP_UNREACH_HOST 1 /* bad host */
|
|
||||||
#define ICMP_UNREACH_PROTOCOL 2 /* bad protocol */
|
|
||||||
#define ICMP_UNREACH_PORT 3 /* bad port */
|
|
||||||
#define ICMP_UNREACH_NEEDFRAG 4 /* IP_DF caused drop */
|
|
||||||
#define ICMP_UNREACH_SRCFAIL 5 /* src route failed */
|
|
||||||
#define ICMP_UNREACH_NET_UNKNOWN 6 /* unknown net */
|
|
||||||
#define ICMP_UNREACH_HOST_UNKNOWN 7 /* unknown host */
|
|
||||||
#define ICMP_UNREACH_ISOLATED 8 /* src host isolated */
|
|
||||||
#define ICMP_UNREACH_NET_PROHIB 9 /* prohibited access */
|
|
||||||
#define ICMP_UNREACH_HOST_PROHIB 10 /* ditto */
|
|
||||||
#define ICMP_UNREACH_TOSNET 11 /* bad tos for net */
|
|
||||||
#define ICMP_UNREACH_TOSHOST 12 /* bad tos for host */
|
|
||||||
#define ICMP_UNREACH_FILTER_PROHIB 13 /* admin prohib */
|
|
||||||
#define ICMP_UNREACH_HOST_PRECEDENCE 14 /* host prec vio. */
|
|
||||||
#define ICMP_UNREACH_PRECEDENCE_CUTOFF 15 /* prec cutoff */
|
|
||||||
#define ICMP_SOURCEQUENCH 4 /* packet lost, slow down */
|
|
||||||
#define ICMP_REDIRECT 5 /* shorter route, codes: */
|
|
||||||
#define ICMP_REDIRECT_NET 0 /* for network */
|
|
||||||
#define ICMP_REDIRECT_HOST 1 /* for host */
|
|
||||||
#define ICMP_REDIRECT_TOSNET 2 /* for tos and net */
|
|
||||||
#define ICMP_REDIRECT_TOSHOST 3 /* for tos and host */
|
|
||||||
#define ICMP_ALTHOSTADDR 6 /* alternate host address */
|
|
||||||
#define ICMP_ECHO 8 /* echo service */
|
|
||||||
#define ICMP_ROUTERADVERT 9 /* router advertisement */
|
|
||||||
#define ICMP_ROUTERADVERT_NORMAL 0 /* normal advertisement */
|
|
||||||
#define ICMP_ROUTERADVERT_NOROUTE_COMMON 16 /* selective routing */
|
|
||||||
#define ICMP_ROUTERSOLICIT 10 /* router solicitation */
|
|
||||||
#define ICMP_TIMXCEED 11 /* time exceeded, code: */
|
|
||||||
#define ICMP_TIMXCEED_INTRANS 0 /* ttl==0 in transit */
|
|
||||||
#define ICMP_TIMXCEED_REASS 1 /* ttl==0 in reass */
|
|
||||||
#define ICMP_PARAMPROB 12 /* ip header bad */
|
|
||||||
#define ICMP_PARAMPROB_ERRATPTR 0 /* error at param ptr */
|
|
||||||
#define ICMP_PARAMPROB_OPTABSENT 1 /* req. opt. absent */
|
|
||||||
#define ICMP_PARAMPROB_LENGTH 2 /* bad length */
|
|
||||||
#define ICMP_TSTAMP 13 /* timestamp request */
|
|
||||||
#define ICMP_TSTAMPREPLY 14 /* timestamp reply */
|
|
||||||
#define ICMP_IREQ 15 /* information request */
|
|
||||||
#define ICMP_IREQREPLY 16 /* information reply */
|
|
||||||
#define ICMP_MASKREQ 17 /* address mask request */
|
|
||||||
#define ICMP_MASKREPLY 18 /* address mask reply */
|
|
||||||
#define ICMP_TRACEROUTE 30 /* traceroute */
|
|
||||||
#define ICMP_DATACONVERR 31 /* data conversion error */
|
|
||||||
#define ICMP_MOBILE_REDIRECT 32 /* mobile host redirect */
|
|
||||||
#define ICMP_IPV6_WHEREAREYOU 33 /* IPv6 where-are-you */
|
|
||||||
#define ICMP_IPV6_IAMHERE 34 /* IPv6 i-am-here */
|
|
||||||
#define ICMP_MOBILE_REGREQUEST 35 /* mobile registration req */
|
|
||||||
#define ICMP_MOBILE_REGREPLY 36 /* mobile registration reply */
|
|
||||||
#define ICMP_SKIP 39 /* SKIP */
|
|
||||||
#define ICMP_PHOTURIS 40 /* Photuris */
|
|
||||||
#define ICMP_PHOTURIS_UNKNOWN_INDEX 1 /* unknown sec index */
|
|
||||||
#define ICMP_PHOTURIS_AUTH_FAILED 2 /* auth failed */
|
|
||||||
#define ICMP_PHOTURIS_DECRYPT_FAILED 3 /* decrypt failed */
|
|
||||||
|
|
||||||
#define ICMP_MAXTYPE 40
|
|
||||||
|
|
||||||
#define ICMP_INFOTYPE(type) \
|
|
||||||
((type) == ICMP_ECHOREPLY || (type) == ICMP_ECHO || \
|
|
||||||
(type) == ICMP_ROUTERADVERT || (type) == ICMP_ROUTERSOLICIT || \
|
|
||||||
(type) == ICMP_TSTAMP || (type) == ICMP_TSTAMPREPLY || \
|
|
||||||
(type) == ICMP_IREQ || (type) == ICMP_IREQREPLY || \
|
|
||||||
(type) == ICMP_MASKREQ || (type) == ICMP_MASKREPLY)
|
|
||||||
|
|
||||||
#ifdef _KERNEL
|
|
||||||
void icmp_error(struct mbuf *, int, int, n_long, struct ifnet *);
|
|
||||||
void icmp_input(struct mbuf *, int);
|
|
||||||
#endif
|
#endif
|
||||||
|
|
||||||
#endif
|
#endif
|
||||||
|
74
include/bsd/libutil.h
Normal file
74
include/bsd/libutil.h
Normal file
@@ -0,0 +1,74 @@
|
|||||||
|
/*
|
||||||
|
* Copyright (c) 1996 Peter Wemm <peter@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_ */
|
203
include/bsd/netinet/ip_icmp.h
Normal file
203
include/bsd/netinet/ip_icmp.h
Normal file
@@ -0,0 +1,203 @@
|
|||||||
|
/*
|
||||||
|
* Copyright (c) 1982, 1986, 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.
|
||||||
|
*
|
||||||
|
* @(#)ip_icmp.h 8.1 (Berkeley) 6/10/93
|
||||||
|
* $FreeBSD: src/sys/netinet/ip_icmp.h,v 1.22 2004/04/07 20:46:13 imp Exp $
|
||||||
|
*/
|
||||||
|
|
||||||
|
#ifndef _NETINET_IP_ICMP_H_
|
||||||
|
#define _NETINET_IP_ICMP_H_
|
||||||
|
|
||||||
|
#include <sys/types.h> /* u_int32_t, u_char */
|
||||||
|
#include <netinet/in.h> /* in_addr */
|
||||||
|
#include <netinet/in_systm.h> /* n_short */
|
||||||
|
#include <netinet/ip.h> /* idi_ip */
|
||||||
|
|
||||||
|
/*
|
||||||
|
* Interface Control Message Protocol Definitions.
|
||||||
|
* Per RFC 792, September 1981.
|
||||||
|
*/
|
||||||
|
|
||||||
|
/*
|
||||||
|
* Internal of an ICMP Router Advertisement
|
||||||
|
*/
|
||||||
|
struct icmp_ra_addr {
|
||||||
|
u_int32_t ira_addr;
|
||||||
|
u_int32_t ira_preference;
|
||||||
|
};
|
||||||
|
|
||||||
|
/*
|
||||||
|
* Structure of an icmp header.
|
||||||
|
*/
|
||||||
|
struct icmp {
|
||||||
|
u_char icmp_type; /* type of message, see below */
|
||||||
|
u_char icmp_code; /* type sub code */
|
||||||
|
u_short icmp_cksum; /* ones complement cksum of struct */
|
||||||
|
union {
|
||||||
|
u_char ih_pptr; /* ICMP_PARAMPROB */
|
||||||
|
struct in_addr ih_gwaddr; /* ICMP_REDIRECT */
|
||||||
|
struct ih_idseq {
|
||||||
|
n_short icd_id;
|
||||||
|
n_short icd_seq;
|
||||||
|
} ih_idseq;
|
||||||
|
int ih_void;
|
||||||
|
|
||||||
|
/* ICMP_UNREACH_NEEDFRAG -- Path MTU Discovery (RFC1191) */
|
||||||
|
struct ih_pmtu {
|
||||||
|
n_short ipm_void;
|
||||||
|
n_short ipm_nextmtu;
|
||||||
|
} ih_pmtu;
|
||||||
|
|
||||||
|
struct ih_rtradv {
|
||||||
|
u_char irt_num_addrs;
|
||||||
|
u_char irt_wpa;
|
||||||
|
u_int16_t irt_lifetime;
|
||||||
|
} ih_rtradv;
|
||||||
|
} icmp_hun;
|
||||||
|
#define icmp_pptr icmp_hun.ih_pptr
|
||||||
|
#define icmp_gwaddr icmp_hun.ih_gwaddr
|
||||||
|
#define icmp_id icmp_hun.ih_idseq.icd_id
|
||||||
|
#define icmp_seq icmp_hun.ih_idseq.icd_seq
|
||||||
|
#define icmp_void icmp_hun.ih_void
|
||||||
|
#define icmp_pmvoid icmp_hun.ih_pmtu.ipm_void
|
||||||
|
#define icmp_nextmtu icmp_hun.ih_pmtu.ipm_nextmtu
|
||||||
|
#define icmp_num_addrs icmp_hun.ih_rtradv.irt_num_addrs
|
||||||
|
#define icmp_wpa icmp_hun.ih_rtradv.irt_wpa
|
||||||
|
#define icmp_lifetime icmp_hun.ih_rtradv.irt_lifetime
|
||||||
|
union {
|
||||||
|
struct id_ts { /* ICMP Timestamp */
|
||||||
|
n_time its_otime; /* Originate */
|
||||||
|
n_time its_rtime; /* Receive */
|
||||||
|
n_time its_ttime; /* Transmit */
|
||||||
|
} id_ts;
|
||||||
|
struct id_ip {
|
||||||
|
struct ip idi_ip;
|
||||||
|
/* options and then 64 bits of data */
|
||||||
|
} id_ip;
|
||||||
|
struct icmp_ra_addr id_radv;
|
||||||
|
u_int32_t id_mask;
|
||||||
|
char id_data[1];
|
||||||
|
} icmp_dun;
|
||||||
|
#define icmp_otime icmp_dun.id_ts.its_otime
|
||||||
|
#define icmp_rtime icmp_dun.id_ts.its_rtime
|
||||||
|
#define icmp_ttime icmp_dun.id_ts.its_ttime
|
||||||
|
#define icmp_ip icmp_dun.id_ip.idi_ip
|
||||||
|
#define icmp_radv icmp_dun.id_radv
|
||||||
|
#define icmp_mask icmp_dun.id_mask
|
||||||
|
#define icmp_data icmp_dun.id_data
|
||||||
|
};
|
||||||
|
|
||||||
|
/*
|
||||||
|
* Lower bounds on packet lengths for various types.
|
||||||
|
* For the error advice packets must first insure that the
|
||||||
|
* packet is large enough to contain the returned ip header.
|
||||||
|
* Only then can we do the check to see if 64 bits of packet
|
||||||
|
* data have been returned, since we need to check the returned
|
||||||
|
* ip header length.
|
||||||
|
*/
|
||||||
|
#define ICMP_MINLEN 8 /* abs minimum */
|
||||||
|
#define ICMP_TSLEN (8 + 3 * sizeof (n_time)) /* timestamp */
|
||||||
|
#define ICMP_MASKLEN 12 /* address mask */
|
||||||
|
#define ICMP_ADVLENMIN (8 + sizeof (struct ip) + 8) /* min */
|
||||||
|
#define ICMP_ADVLEN(p) (8 + ((p)->icmp_ip.ip_hl << 2) + 8)
|
||||||
|
/* N.B.: must separately check that ip_hl >= 5 */
|
||||||
|
|
||||||
|
/*
|
||||||
|
* Definition of type and code field values.
|
||||||
|
*/
|
||||||
|
#define ICMP_ECHOREPLY 0 /* echo reply */
|
||||||
|
#define ICMP_UNREACH 3 /* dest unreachable, codes: */
|
||||||
|
#define ICMP_UNREACH_NET 0 /* bad net */
|
||||||
|
#define ICMP_UNREACH_HOST 1 /* bad host */
|
||||||
|
#define ICMP_UNREACH_PROTOCOL 2 /* bad protocol */
|
||||||
|
#define ICMP_UNREACH_PORT 3 /* bad port */
|
||||||
|
#define ICMP_UNREACH_NEEDFRAG 4 /* IP_DF caused drop */
|
||||||
|
#define ICMP_UNREACH_SRCFAIL 5 /* src route failed */
|
||||||
|
#define ICMP_UNREACH_NET_UNKNOWN 6 /* unknown net */
|
||||||
|
#define ICMP_UNREACH_HOST_UNKNOWN 7 /* unknown host */
|
||||||
|
#define ICMP_UNREACH_ISOLATED 8 /* src host isolated */
|
||||||
|
#define ICMP_UNREACH_NET_PROHIB 9 /* prohibited access */
|
||||||
|
#define ICMP_UNREACH_HOST_PROHIB 10 /* ditto */
|
||||||
|
#define ICMP_UNREACH_TOSNET 11 /* bad tos for net */
|
||||||
|
#define ICMP_UNREACH_TOSHOST 12 /* bad tos for host */
|
||||||
|
#define ICMP_UNREACH_FILTER_PROHIB 13 /* admin prohib */
|
||||||
|
#define ICMP_UNREACH_HOST_PRECEDENCE 14 /* host prec vio. */
|
||||||
|
#define ICMP_UNREACH_PRECEDENCE_CUTOFF 15 /* prec cutoff */
|
||||||
|
#define ICMP_SOURCEQUENCH 4 /* packet lost, slow down */
|
||||||
|
#define ICMP_REDIRECT 5 /* shorter route, codes: */
|
||||||
|
#define ICMP_REDIRECT_NET 0 /* for network */
|
||||||
|
#define ICMP_REDIRECT_HOST 1 /* for host */
|
||||||
|
#define ICMP_REDIRECT_TOSNET 2 /* for tos and net */
|
||||||
|
#define ICMP_REDIRECT_TOSHOST 3 /* for tos and host */
|
||||||
|
#define ICMP_ALTHOSTADDR 6 /* alternate host address */
|
||||||
|
#define ICMP_ECHO 8 /* echo service */
|
||||||
|
#define ICMP_ROUTERADVERT 9 /* router advertisement */
|
||||||
|
#define ICMP_ROUTERADVERT_NORMAL 0 /* normal advertisement */
|
||||||
|
#define ICMP_ROUTERADVERT_NOROUTE_COMMON 16 /* selective routing */
|
||||||
|
#define ICMP_ROUTERSOLICIT 10 /* router solicitation */
|
||||||
|
#define ICMP_TIMXCEED 11 /* time exceeded, code: */
|
||||||
|
#define ICMP_TIMXCEED_INTRANS 0 /* ttl==0 in transit */
|
||||||
|
#define ICMP_TIMXCEED_REASS 1 /* ttl==0 in reass */
|
||||||
|
#define ICMP_PARAMPROB 12 /* ip header bad */
|
||||||
|
#define ICMP_PARAMPROB_ERRATPTR 0 /* error at param ptr */
|
||||||
|
#define ICMP_PARAMPROB_OPTABSENT 1 /* req. opt. absent */
|
||||||
|
#define ICMP_PARAMPROB_LENGTH 2 /* bad length */
|
||||||
|
#define ICMP_TSTAMP 13 /* timestamp request */
|
||||||
|
#define ICMP_TSTAMPREPLY 14 /* timestamp reply */
|
||||||
|
#define ICMP_IREQ 15 /* information request */
|
||||||
|
#define ICMP_IREQREPLY 16 /* information reply */
|
||||||
|
#define ICMP_MASKREQ 17 /* address mask request */
|
||||||
|
#define ICMP_MASKREPLY 18 /* address mask reply */
|
||||||
|
#define ICMP_TRACEROUTE 30 /* traceroute */
|
||||||
|
#define ICMP_DATACONVERR 31 /* data conversion error */
|
||||||
|
#define ICMP_MOBILE_REDIRECT 32 /* mobile host redirect */
|
||||||
|
#define ICMP_IPV6_WHEREAREYOU 33 /* IPv6 where-are-you */
|
||||||
|
#define ICMP_IPV6_IAMHERE 34 /* IPv6 i-am-here */
|
||||||
|
#define ICMP_MOBILE_REGREQUEST 35 /* mobile registration req */
|
||||||
|
#define ICMP_MOBILE_REGREPLY 36 /* mobile registration reply */
|
||||||
|
#define ICMP_SKIP 39 /* SKIP */
|
||||||
|
#define ICMP_PHOTURIS 40 /* Photuris */
|
||||||
|
#define ICMP_PHOTURIS_UNKNOWN_INDEX 1 /* unknown sec index */
|
||||||
|
#define ICMP_PHOTURIS_AUTH_FAILED 2 /* auth failed */
|
||||||
|
#define ICMP_PHOTURIS_DECRYPT_FAILED 3 /* decrypt failed */
|
||||||
|
|
||||||
|
#define ICMP_MAXTYPE 40
|
||||||
|
|
||||||
|
#define ICMP_INFOTYPE(type) \
|
||||||
|
((type) == ICMP_ECHOREPLY || (type) == ICMP_ECHO || \
|
||||||
|
(type) == ICMP_ROUTERADVERT || (type) == ICMP_ROUTERSOLICIT || \
|
||||||
|
(type) == ICMP_TSTAMP || (type) == ICMP_TSTAMPREPLY || \
|
||||||
|
(type) == ICMP_IREQ || (type) == ICMP_IREQREPLY || \
|
||||||
|
(type) == ICMP_MASKREQ || (type) == ICMP_MASKREPLY)
|
||||||
|
|
||||||
|
#ifdef _KERNEL
|
||||||
|
void icmp_error(struct mbuf *, int, int, n_long, struct ifnet *);
|
||||||
|
void icmp_input(struct mbuf *, int);
|
||||||
|
#endif
|
||||||
|
|
||||||
|
#endif
|
37
include/bsd/nlist.h
Normal file
37
include/bsd/nlist.h
Normal file
@@ -0,0 +1,37 @@
|
|||||||
|
/*
|
||||||
|
* 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
|
@@ -1,6 +1,5 @@
|
|||||||
/*
|
/*
|
||||||
* Copyright (c) 1991, 1993
|
* Copyright © 2009 Guillem Jover
|
||||||
* The Regents of the University of California. All rights reserved.
|
|
||||||
*
|
*
|
||||||
* Redistribution and use in source and binary forms, with or without
|
* Redistribution and use in source and binary forms, with or without
|
||||||
* modification, are permitted provided that the following conditions
|
* modification, are permitted provided that the following conditions
|
||||||
@@ -10,544 +9,34 @@
|
|||||||
* 2. Redistributions in binary form must reproduce the above copyright
|
* 2. Redistributions in binary form must reproduce the above copyright
|
||||||
* notice, this list of conditions and the following disclaimer in the
|
* notice, this list of conditions and the following disclaimer in the
|
||||||
* documentation and/or other materials provided with the distribution.
|
* documentation and/or other materials provided with the distribution.
|
||||||
* 3. Neither the name of the University nor the names of its contributors
|
* 3. The name of the author may not be used to endorse or promote products
|
||||||
* may be used to endorse or promote products derived from this software
|
* derived from this software without specific prior written permission.
|
||||||
* without specific prior written permission.
|
|
||||||
*
|
*
|
||||||
* THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
|
* THIS SOFTWARE IS PROVIDED ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES,
|
||||||
* ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
|
* INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY
|
||||||
* IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
|
* AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL
|
||||||
* ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
|
* THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL,
|
||||||
* FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
|
* EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO,
|
||||||
* DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
|
* PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS;
|
||||||
* OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
|
* OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY,
|
||||||
* HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
|
* WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR
|
||||||
* LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
|
* OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF
|
||||||
* OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
|
* ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
|
||||||
* SUCH DAMAGE.
|
|
||||||
*
|
|
||||||
* @(#)queue.h 8.5 (Berkeley) 8/20/94
|
|
||||||
* $FreeBSD: src/sys/sys/queue.h,v 1.58 2004/04/07 04:19:49 imp Exp $
|
|
||||||
*/
|
*/
|
||||||
|
|
||||||
#ifndef _SYS_QUEUE_H
|
#ifndef LIBBSD_BSD_QUEUE_H
|
||||||
#define _SYS_QUEUE_H
|
#define LIBBSD_BSD_QUEUE_H
|
||||||
|
|
||||||
#include <sys/cdefs.h>
|
|
||||||
|
|
||||||
/*
|
|
||||||
* This file defines four types of data structures: singly-linked lists,
|
|
||||||
* singly-linked tail queues, lists and tail queues.
|
|
||||||
*
|
|
||||||
* A singly-linked list is headed by a single forward pointer. The elements
|
|
||||||
* are singly linked for minimum space and pointer manipulation overhead at
|
|
||||||
* the expense of O(n) removal for arbitrary elements. New elements can be
|
|
||||||
* added to the list after an existing element or at the head of the list.
|
|
||||||
* Elements being removed from the head of the list should use the explicit
|
|
||||||
* macro for this purpose for optimum efficiency. A singly-linked list may
|
|
||||||
* only be traversed in the forward direction. Singly-linked lists are ideal
|
|
||||||
* for applications with large datasets and few or no removals or for
|
|
||||||
* implementing a LIFO queue.
|
|
||||||
*
|
|
||||||
* A singly-linked tail queue is headed by a pair of pointers, one to the
|
|
||||||
* head of the list and the other to the tail of the list. The elements are
|
|
||||||
* singly linked for minimum space and pointer manipulation overhead at the
|
|
||||||
* expense of O(n) removal for arbitrary elements. New elements can be added
|
|
||||||
* to the list after an existing element, at the head of the list, or at the
|
|
||||||
* end of the list. Elements being removed from the head of the tail queue
|
|
||||||
* should use the explicit macro for this purpose for optimum efficiency.
|
|
||||||
* A singly-linked tail queue may only be traversed in the forward direction.
|
|
||||||
* Singly-linked tail queues are ideal for applications with large datasets
|
|
||||||
* and few or no removals or for implementing a FIFO queue.
|
|
||||||
*
|
|
||||||
* A list is headed by a single forward pointer (or an array of forward
|
|
||||||
* pointers for a hash table header). The elements are doubly linked
|
|
||||||
* so that an arbitrary element can be removed without a need to
|
|
||||||
* traverse the list. New elements can be added to the list before
|
|
||||||
* or after an existing element or at the head of the list. A list
|
|
||||||
* may only be traversed in the forward direction.
|
|
||||||
*
|
|
||||||
* A tail queue is headed by a pair of pointers, one to the head of the
|
|
||||||
* list and the other to the tail of the list. The elements are doubly
|
|
||||||
* linked so that an arbitrary element can be removed without a need to
|
|
||||||
* traverse the list. New elements can be added to the list before or
|
|
||||||
* after an existing element, at the head of the list, or at the end of
|
|
||||||
* the list. A tail queue may be traversed in either direction.
|
|
||||||
*
|
|
||||||
* For details on the use of these macros, see the queue(3) manual page.
|
|
||||||
*
|
|
||||||
*
|
|
||||||
* SLIST LIST STAILQ TAILQ
|
|
||||||
* _HEAD + + + +
|
|
||||||
* _HEAD_INITIALIZER + + + +
|
|
||||||
* _ENTRY + + + +
|
|
||||||
* _INIT + + + +
|
|
||||||
* _EMPTY + + + +
|
|
||||||
* _FIRST + + + +
|
|
||||||
* _NEXT + + + +
|
|
||||||
* _PREV - - - +
|
|
||||||
* _LAST - - + +
|
|
||||||
* _FOREACH + + + +
|
|
||||||
* _FOREACH_SAFE + + + +
|
|
||||||
* _FOREACH_REVERSE - - - +
|
|
||||||
* _FOREACH_REVERSE_SAFE - - - +
|
|
||||||
* _INSERT_HEAD + + + +
|
|
||||||
* _INSERT_BEFORE - + - +
|
|
||||||
* _INSERT_AFTER + + + +
|
|
||||||
* _INSERT_TAIL - - + +
|
|
||||||
* _CONCAT - - + +
|
|
||||||
* _REMOVE_HEAD + - + -
|
|
||||||
* _REMOVE + + + +
|
|
||||||
*
|
|
||||||
*/
|
|
||||||
#define QUEUE_MACRO_DEBUG 0
|
|
||||||
#if QUEUE_MACRO_DEBUG
|
|
||||||
/* Store the last 2 places the queue element or head was altered */
|
|
||||||
struct qm_trace {
|
|
||||||
char * lastfile;
|
|
||||||
int lastline;
|
|
||||||
char * prevfile;
|
|
||||||
int prevline;
|
|
||||||
};
|
|
||||||
|
|
||||||
#define TRACEBUF struct qm_trace trace;
|
|
||||||
#define TRASHIT(x) do {(x) = (void *)-1;} while (0)
|
|
||||||
|
|
||||||
#define QMD_TRACE_HEAD(head) do { \
|
|
||||||
(head)->trace.prevline = (head)->trace.lastline; \
|
|
||||||
(head)->trace.prevfile = (head)->trace.lastfile; \
|
|
||||||
(head)->trace.lastline = __LINE__; \
|
|
||||||
(head)->trace.lastfile = __FILE__; \
|
|
||||||
} while (0)
|
|
||||||
|
|
||||||
#define QMD_TRACE_ELEM(elem) do { \
|
|
||||||
(elem)->trace.prevline = (elem)->trace.lastline; \
|
|
||||||
(elem)->trace.prevfile = (elem)->trace.lastfile; \
|
|
||||||
(elem)->trace.lastline = __LINE__; \
|
|
||||||
(elem)->trace.lastfile = __FILE__; \
|
|
||||||
} while (0)
|
|
||||||
|
|
||||||
|
#ifdef LIBBSD_DISABLE_DEPRECATED
|
||||||
|
#error "Deprecated header, use <bsd/sys/queue.h> or libbsd-overlay.pc instead."
|
||||||
#else
|
#else
|
||||||
#define QMD_TRACE_ELEM(elem)
|
#warning "Deprecated header, use <bsd/sys/queue.h> or libbsd-overlay.pc instead."
|
||||||
#define QMD_TRACE_HEAD(head)
|
#endif
|
||||||
#define TRACEBUF
|
|
||||||
#define TRASHIT(x)
|
|
||||||
#endif /* QUEUE_MACRO_DEBUG */
|
|
||||||
|
|
||||||
/*
|
#ifdef LIBBSD_OVERLAY
|
||||||
* Singly-linked List declarations.
|
#include <sys/queue.h>
|
||||||
*/
|
#else
|
||||||
#define SLIST_HEAD(name, type) \
|
#include <bsd/sys/queue.h>
|
||||||
struct name { \
|
#endif
|
||||||
struct type *slh_first; /* first element */ \
|
|
||||||
}
|
|
||||||
|
|
||||||
#define SLIST_HEAD_INITIALIZER(head) \
|
#endif
|
||||||
{ NULL }
|
|
||||||
|
|
||||||
#define SLIST_ENTRY(type) \
|
|
||||||
struct { \
|
|
||||||
struct type *sle_next; /* next element */ \
|
|
||||||
}
|
|
||||||
|
|
||||||
/*
|
|
||||||
* Singly-linked List functions.
|
|
||||||
*/
|
|
||||||
#define SLIST_EMPTY(head) ((head)->slh_first == NULL)
|
|
||||||
|
|
||||||
#define SLIST_FIRST(head) ((head)->slh_first)
|
|
||||||
|
|
||||||
#define SLIST_FOREACH(var, head, field) \
|
|
||||||
for ((var) = SLIST_FIRST((head)); \
|
|
||||||
(var); \
|
|
||||||
(var) = SLIST_NEXT((var), field))
|
|
||||||
|
|
||||||
#define SLIST_FOREACH_SAFE(var, head, field, tvar) \
|
|
||||||
for ((var) = SLIST_FIRST((head)); \
|
|
||||||
(var) && ((tvar) = SLIST_NEXT((var), field), 1); \
|
|
||||||
(var) = (tvar))
|
|
||||||
|
|
||||||
#define SLIST_FOREACH_PREVPTR(var, varp, head, field) \
|
|
||||||
for ((varp) = &SLIST_FIRST((head)); \
|
|
||||||
((var) = *(varp)) != NULL; \
|
|
||||||
(varp) = &SLIST_NEXT((var), field))
|
|
||||||
|
|
||||||
#define SLIST_INIT(head) do { \
|
|
||||||
SLIST_FIRST((head)) = NULL; \
|
|
||||||
} while (0)
|
|
||||||
|
|
||||||
#define SLIST_INSERT_AFTER(slistelm, elm, field) do { \
|
|
||||||
SLIST_NEXT((elm), field) = SLIST_NEXT((slistelm), field); \
|
|
||||||
SLIST_NEXT((slistelm), field) = (elm); \
|
|
||||||
} while (0)
|
|
||||||
|
|
||||||
#define SLIST_INSERT_HEAD(head, elm, field) do { \
|
|
||||||
SLIST_NEXT((elm), field) = SLIST_FIRST((head)); \
|
|
||||||
SLIST_FIRST((head)) = (elm); \
|
|
||||||
} while (0)
|
|
||||||
|
|
||||||
#define SLIST_NEXT(elm, field) ((elm)->field.sle_next)
|
|
||||||
|
|
||||||
#define SLIST_REMOVE(head, elm, type, field) do { \
|
|
||||||
if (SLIST_FIRST((head)) == (elm)) { \
|
|
||||||
SLIST_REMOVE_HEAD((head), field); \
|
|
||||||
} \
|
|
||||||
else { \
|
|
||||||
struct type *curelm = SLIST_FIRST((head)); \
|
|
||||||
while (SLIST_NEXT(curelm, field) != (elm)) \
|
|
||||||
curelm = SLIST_NEXT(curelm, field); \
|
|
||||||
SLIST_NEXT(curelm, field) = \
|
|
||||||
SLIST_NEXT(SLIST_NEXT(curelm, field), field); \
|
|
||||||
} \
|
|
||||||
} while (0)
|
|
||||||
|
|
||||||
#define SLIST_REMOVE_HEAD(head, field) do { \
|
|
||||||
SLIST_FIRST((head)) = SLIST_NEXT(SLIST_FIRST((head)), field); \
|
|
||||||
} while (0)
|
|
||||||
|
|
||||||
/*
|
|
||||||
* Singly-linked Tail queue declarations.
|
|
||||||
*/
|
|
||||||
#define STAILQ_HEAD(name, type) \
|
|
||||||
struct name { \
|
|
||||||
struct type *stqh_first;/* first element */ \
|
|
||||||
struct type **stqh_last;/* addr of last next element */ \
|
|
||||||
}
|
|
||||||
|
|
||||||
#define STAILQ_HEAD_INITIALIZER(head) \
|
|
||||||
{ NULL, &(head).stqh_first }
|
|
||||||
|
|
||||||
#define STAILQ_ENTRY(type) \
|
|
||||||
struct { \
|
|
||||||
struct type *stqe_next; /* next element */ \
|
|
||||||
}
|
|
||||||
|
|
||||||
/*
|
|
||||||
* Singly-linked Tail queue functions.
|
|
||||||
*/
|
|
||||||
#define STAILQ_CONCAT(head1, head2) do { \
|
|
||||||
if (!STAILQ_EMPTY((head2))) { \
|
|
||||||
*(head1)->stqh_last = (head2)->stqh_first; \
|
|
||||||
(head1)->stqh_last = (head2)->stqh_last; \
|
|
||||||
STAILQ_INIT((head2)); \
|
|
||||||
} \
|
|
||||||
} while (0)
|
|
||||||
|
|
||||||
#define STAILQ_EMPTY(head) ((head)->stqh_first == NULL)
|
|
||||||
|
|
||||||
#define STAILQ_FIRST(head) ((head)->stqh_first)
|
|
||||||
|
|
||||||
#define STAILQ_FOREACH(var, head, field) \
|
|
||||||
for((var) = STAILQ_FIRST((head)); \
|
|
||||||
(var); \
|
|
||||||
(var) = STAILQ_NEXT((var), field))
|
|
||||||
|
|
||||||
|
|
||||||
#define STAILQ_FOREACH_SAFE(var, head, field, tvar) \
|
|
||||||
for ((var) = STAILQ_FIRST((head)); \
|
|
||||||
(var) && ((tvar) = STAILQ_NEXT((var), field), 1); \
|
|
||||||
(var) = (tvar))
|
|
||||||
|
|
||||||
#define STAILQ_INIT(head) do { \
|
|
||||||
STAILQ_FIRST((head)) = NULL; \
|
|
||||||
(head)->stqh_last = &STAILQ_FIRST((head)); \
|
|
||||||
} while (0)
|
|
||||||
|
|
||||||
#define STAILQ_INSERT_AFTER(head, tqelm, elm, field) do { \
|
|
||||||
if ((STAILQ_NEXT((elm), field) = STAILQ_NEXT((tqelm), field)) == NULL)\
|
|
||||||
(head)->stqh_last = &STAILQ_NEXT((elm), field); \
|
|
||||||
STAILQ_NEXT((tqelm), field) = (elm); \
|
|
||||||
} while (0)
|
|
||||||
|
|
||||||
#define STAILQ_INSERT_HEAD(head, elm, field) do { \
|
|
||||||
if ((STAILQ_NEXT((elm), field) = STAILQ_FIRST((head))) == NULL) \
|
|
||||||
(head)->stqh_last = &STAILQ_NEXT((elm), field); \
|
|
||||||
STAILQ_FIRST((head)) = (elm); \
|
|
||||||
} while (0)
|
|
||||||
|
|
||||||
#define STAILQ_INSERT_TAIL(head, elm, field) do { \
|
|
||||||
STAILQ_NEXT((elm), field) = NULL; \
|
|
||||||
*(head)->stqh_last = (elm); \
|
|
||||||
(head)->stqh_last = &STAILQ_NEXT((elm), field); \
|
|
||||||
} while (0)
|
|
||||||
|
|
||||||
#define STAILQ_LAST(head, type, field) \
|
|
||||||
(STAILQ_EMPTY((head)) ? \
|
|
||||||
NULL : \
|
|
||||||
((struct type *)(void *) \
|
|
||||||
((char *)((head)->stqh_last) - __offsetof(struct type, field))))
|
|
||||||
|
|
||||||
#define STAILQ_NEXT(elm, field) ((elm)->field.stqe_next)
|
|
||||||
|
|
||||||
#define STAILQ_REMOVE(head, elm, type, field) do { \
|
|
||||||
if (STAILQ_FIRST((head)) == (elm)) { \
|
|
||||||
STAILQ_REMOVE_HEAD((head), field); \
|
|
||||||
} \
|
|
||||||
else { \
|
|
||||||
struct type *curelm = STAILQ_FIRST((head)); \
|
|
||||||
while (STAILQ_NEXT(curelm, field) != (elm)) \
|
|
||||||
curelm = STAILQ_NEXT(curelm, field); \
|
|
||||||
if ((STAILQ_NEXT(curelm, field) = \
|
|
||||||
STAILQ_NEXT(STAILQ_NEXT(curelm, field), field)) == NULL)\
|
|
||||||
(head)->stqh_last = &STAILQ_NEXT((curelm), field);\
|
|
||||||
} \
|
|
||||||
} while (0)
|
|
||||||
|
|
||||||
#define STAILQ_REMOVE_HEAD(head, field) do { \
|
|
||||||
if ((STAILQ_FIRST((head)) = \
|
|
||||||
STAILQ_NEXT(STAILQ_FIRST((head)), field)) == NULL) \
|
|
||||||
(head)->stqh_last = &STAILQ_FIRST((head)); \
|
|
||||||
} while (0)
|
|
||||||
|
|
||||||
#define STAILQ_REMOVE_HEAD_UNTIL(head, elm, field) do { \
|
|
||||||
if ((STAILQ_FIRST((head)) = STAILQ_NEXT((elm), field)) == NULL) \
|
|
||||||
(head)->stqh_last = &STAILQ_FIRST((head)); \
|
|
||||||
} while (0)
|
|
||||||
|
|
||||||
/*
|
|
||||||
* List declarations.
|
|
||||||
*/
|
|
||||||
#define LIST_HEAD(name, type) \
|
|
||||||
struct name { \
|
|
||||||
struct type *lh_first; /* first element */ \
|
|
||||||
}
|
|
||||||
|
|
||||||
#define LIST_HEAD_INITIALIZER(head) \
|
|
||||||
{ NULL }
|
|
||||||
|
|
||||||
#define LIST_ENTRY(type) \
|
|
||||||
struct { \
|
|
||||||
struct type *le_next; /* next element */ \
|
|
||||||
struct type **le_prev; /* address of previous next element */ \
|
|
||||||
}
|
|
||||||
|
|
||||||
/*
|
|
||||||
* List functions.
|
|
||||||
*/
|
|
||||||
|
|
||||||
#define LIST_EMPTY(head) ((head)->lh_first == NULL)
|
|
||||||
|
|
||||||
#define LIST_FIRST(head) ((head)->lh_first)
|
|
||||||
|
|
||||||
#define LIST_FOREACH(var, head, field) \
|
|
||||||
for ((var) = LIST_FIRST((head)); \
|
|
||||||
(var); \
|
|
||||||
(var) = LIST_NEXT((var), field))
|
|
||||||
|
|
||||||
#define LIST_FOREACH_SAFE(var, head, field, tvar) \
|
|
||||||
for ((var) = LIST_FIRST((head)); \
|
|
||||||
(var) && ((tvar) = LIST_NEXT((var), field), 1); \
|
|
||||||
(var) = (tvar))
|
|
||||||
|
|
||||||
#define LIST_INIT(head) do { \
|
|
||||||
LIST_FIRST((head)) = NULL; \
|
|
||||||
} while (0)
|
|
||||||
|
|
||||||
#define LIST_INSERT_AFTER(listelm, elm, field) do { \
|
|
||||||
if ((LIST_NEXT((elm), field) = LIST_NEXT((listelm), field)) != NULL)\
|
|
||||||
LIST_NEXT((listelm), field)->field.le_prev = \
|
|
||||||
&LIST_NEXT((elm), field); \
|
|
||||||
LIST_NEXT((listelm), field) = (elm); \
|
|
||||||
(elm)->field.le_prev = &LIST_NEXT((listelm), field); \
|
|
||||||
} while (0)
|
|
||||||
|
|
||||||
#define LIST_INSERT_BEFORE(listelm, elm, field) do { \
|
|
||||||
(elm)->field.le_prev = (listelm)->field.le_prev; \
|
|
||||||
LIST_NEXT((elm), field) = (listelm); \
|
|
||||||
*(listelm)->field.le_prev = (elm); \
|
|
||||||
(listelm)->field.le_prev = &LIST_NEXT((elm), field); \
|
|
||||||
} while (0)
|
|
||||||
|
|
||||||
#define LIST_INSERT_HEAD(head, elm, field) do { \
|
|
||||||
if ((LIST_NEXT((elm), field) = LIST_FIRST((head))) != NULL) \
|
|
||||||
LIST_FIRST((head))->field.le_prev = &LIST_NEXT((elm), field);\
|
|
||||||
LIST_FIRST((head)) = (elm); \
|
|
||||||
(elm)->field.le_prev = &LIST_FIRST((head)); \
|
|
||||||
} while (0)
|
|
||||||
|
|
||||||
#define LIST_NEXT(elm, field) ((elm)->field.le_next)
|
|
||||||
|
|
||||||
#define LIST_REMOVE(elm, field) do { \
|
|
||||||
if (LIST_NEXT((elm), field) != NULL) \
|
|
||||||
LIST_NEXT((elm), field)->field.le_prev = \
|
|
||||||
(elm)->field.le_prev; \
|
|
||||||
*(elm)->field.le_prev = LIST_NEXT((elm), field); \
|
|
||||||
} while (0)
|
|
||||||
|
|
||||||
/*
|
|
||||||
* Tail queue declarations.
|
|
||||||
*/
|
|
||||||
#define TAILQ_HEAD(name, type) \
|
|
||||||
struct name { \
|
|
||||||
struct type *tqh_first; /* first element */ \
|
|
||||||
struct type **tqh_last; /* addr of last next element */ \
|
|
||||||
TRACEBUF \
|
|
||||||
}
|
|
||||||
|
|
||||||
#define TAILQ_HEAD_INITIALIZER(head) \
|
|
||||||
{ NULL, &(head).tqh_first }
|
|
||||||
|
|
||||||
#define TAILQ_ENTRY(type) \
|
|
||||||
struct { \
|
|
||||||
struct type *tqe_next; /* next element */ \
|
|
||||||
struct type **tqe_prev; /* address of previous next element */ \
|
|
||||||
TRACEBUF \
|
|
||||||
}
|
|
||||||
|
|
||||||
/*
|
|
||||||
* Tail queue functions.
|
|
||||||
*/
|
|
||||||
#define TAILQ_CONCAT(head1, head2, field) do { \
|
|
||||||
if (!TAILQ_EMPTY(head2)) { \
|
|
||||||
*(head1)->tqh_last = (head2)->tqh_first; \
|
|
||||||
(head2)->tqh_first->field.tqe_prev = (head1)->tqh_last; \
|
|
||||||
(head1)->tqh_last = (head2)->tqh_last; \
|
|
||||||
TAILQ_INIT((head2)); \
|
|
||||||
QMD_TRACE_HEAD(head1); \
|
|
||||||
QMD_TRACE_HEAD(head2); \
|
|
||||||
} \
|
|
||||||
} while (0)
|
|
||||||
|
|
||||||
#define TAILQ_EMPTY(head) ((head)->tqh_first == NULL)
|
|
||||||
|
|
||||||
#define TAILQ_FIRST(head) ((head)->tqh_first)
|
|
||||||
|
|
||||||
#define TAILQ_FOREACH(var, head, field) \
|
|
||||||
for ((var) = TAILQ_FIRST((head)); \
|
|
||||||
(var); \
|
|
||||||
(var) = TAILQ_NEXT((var), field))
|
|
||||||
|
|
||||||
#define TAILQ_FOREACH_SAFE(var, head, field, tvar) \
|
|
||||||
for ((var) = TAILQ_FIRST((head)); \
|
|
||||||
(var) && ((tvar) = TAILQ_NEXT((var), field), 1); \
|
|
||||||
(var) = (tvar))
|
|
||||||
|
|
||||||
#define TAILQ_FOREACH_REVERSE(var, head, headname, field) \
|
|
||||||
for ((var) = TAILQ_LAST((head), headname); \
|
|
||||||
(var); \
|
|
||||||
(var) = TAILQ_PREV((var), headname, field))
|
|
||||||
|
|
||||||
#define TAILQ_FOREACH_REVERSE_SAFE(var, head, headname, field, tvar) \
|
|
||||||
for ((var) = TAILQ_LAST((head), headname); \
|
|
||||||
(var) && ((tvar) = TAILQ_PREV((var), headname, field), 1); \
|
|
||||||
(var) = (tvar))
|
|
||||||
|
|
||||||
#define TAILQ_INIT(head) do { \
|
|
||||||
TAILQ_FIRST((head)) = NULL; \
|
|
||||||
(head)->tqh_last = &TAILQ_FIRST((head)); \
|
|
||||||
QMD_TRACE_HEAD(head); \
|
|
||||||
} while (0)
|
|
||||||
|
|
||||||
#define TAILQ_INSERT_AFTER(head, listelm, elm, field) do { \
|
|
||||||
if ((TAILQ_NEXT((elm), field) = TAILQ_NEXT((listelm), field)) != NULL)\
|
|
||||||
TAILQ_NEXT((elm), field)->field.tqe_prev = \
|
|
||||||
&TAILQ_NEXT((elm), field); \
|
|
||||||
else { \
|
|
||||||
(head)->tqh_last = &TAILQ_NEXT((elm), field); \
|
|
||||||
QMD_TRACE_HEAD(head); \
|
|
||||||
} \
|
|
||||||
TAILQ_NEXT((listelm), field) = (elm); \
|
|
||||||
(elm)->field.tqe_prev = &TAILQ_NEXT((listelm), field); \
|
|
||||||
QMD_TRACE_ELEM(&(elm)->field); \
|
|
||||||
QMD_TRACE_ELEM(&listelm->field); \
|
|
||||||
} while (0)
|
|
||||||
|
|
||||||
#define TAILQ_INSERT_BEFORE(listelm, elm, field) do { \
|
|
||||||
(elm)->field.tqe_prev = (listelm)->field.tqe_prev; \
|
|
||||||
TAILQ_NEXT((elm), field) = (listelm); \
|
|
||||||
*(listelm)->field.tqe_prev = (elm); \
|
|
||||||
(listelm)->field.tqe_prev = &TAILQ_NEXT((elm), field); \
|
|
||||||
QMD_TRACE_ELEM(&(elm)->field); \
|
|
||||||
QMD_TRACE_ELEM(&listelm->field); \
|
|
||||||
} while (0)
|
|
||||||
|
|
||||||
#define TAILQ_INSERT_HEAD(head, elm, field) do { \
|
|
||||||
if ((TAILQ_NEXT((elm), field) = TAILQ_FIRST((head))) != NULL) \
|
|
||||||
TAILQ_FIRST((head))->field.tqe_prev = \
|
|
||||||
&TAILQ_NEXT((elm), field); \
|
|
||||||
else \
|
|
||||||
(head)->tqh_last = &TAILQ_NEXT((elm), field); \
|
|
||||||
TAILQ_FIRST((head)) = (elm); \
|
|
||||||
(elm)->field.tqe_prev = &TAILQ_FIRST((head)); \
|
|
||||||
QMD_TRACE_HEAD(head); \
|
|
||||||
QMD_TRACE_ELEM(&(elm)->field); \
|
|
||||||
} while (0)
|
|
||||||
|
|
||||||
#define TAILQ_INSERT_TAIL(head, elm, field) do { \
|
|
||||||
TAILQ_NEXT((elm), field) = NULL; \
|
|
||||||
(elm)->field.tqe_prev = (head)->tqh_last; \
|
|
||||||
*(head)->tqh_last = (elm); \
|
|
||||||
(head)->tqh_last = &TAILQ_NEXT((elm), field); \
|
|
||||||
QMD_TRACE_HEAD(head); \
|
|
||||||
QMD_TRACE_ELEM(&(elm)->field); \
|
|
||||||
} while (0)
|
|
||||||
|
|
||||||
#define TAILQ_LAST(head, headname) \
|
|
||||||
(*(((struct headname *)((head)->tqh_last))->tqh_last))
|
|
||||||
|
|
||||||
#define TAILQ_NEXT(elm, field) ((elm)->field.tqe_next)
|
|
||||||
|
|
||||||
#define TAILQ_PREV(elm, headname, field) \
|
|
||||||
(*(((struct headname *)((elm)->field.tqe_prev))->tqh_last))
|
|
||||||
|
|
||||||
#define TAILQ_REMOVE(head, elm, field) do { \
|
|
||||||
if ((TAILQ_NEXT((elm), field)) != NULL) \
|
|
||||||
TAILQ_NEXT((elm), field)->field.tqe_prev = \
|
|
||||||
(elm)->field.tqe_prev; \
|
|
||||||
else { \
|
|
||||||
(head)->tqh_last = (elm)->field.tqe_prev; \
|
|
||||||
QMD_TRACE_HEAD(head); \
|
|
||||||
} \
|
|
||||||
*(elm)->field.tqe_prev = TAILQ_NEXT((elm), field); \
|
|
||||||
TRASHIT((elm)->field.tqe_next); \
|
|
||||||
TRASHIT((elm)->field.tqe_prev); \
|
|
||||||
QMD_TRACE_ELEM(&(elm)->field); \
|
|
||||||
} while (0)
|
|
||||||
|
|
||||||
|
|
||||||
#ifdef _KERNEL
|
|
||||||
|
|
||||||
/*
|
|
||||||
* XXX insque() and remque() are an old way of handling certain queues.
|
|
||||||
* They bogusly assumes that all queue heads look alike.
|
|
||||||
*/
|
|
||||||
|
|
||||||
struct quehead {
|
|
||||||
struct quehead *qh_link;
|
|
||||||
struct quehead *qh_rlink;
|
|
||||||
};
|
|
||||||
|
|
||||||
#if defined(__GNUC__) || defined(__INTEL_COMPILER)
|
|
||||||
|
|
||||||
static __inline void
|
|
||||||
insque(void *a, void *b)
|
|
||||||
{
|
|
||||||
struct quehead *element = (struct quehead *)a,
|
|
||||||
*head = (struct quehead *)b;
|
|
||||||
|
|
||||||
element->qh_link = head->qh_link;
|
|
||||||
element->qh_rlink = head;
|
|
||||||
head->qh_link = element;
|
|
||||||
element->qh_link->qh_rlink = element;
|
|
||||||
}
|
|
||||||
|
|
||||||
static __inline void
|
|
||||||
remque(void *a)
|
|
||||||
{
|
|
||||||
struct quehead *element = (struct quehead *)a;
|
|
||||||
|
|
||||||
element->qh_link->qh_rlink = element->qh_rlink;
|
|
||||||
element->qh_rlink->qh_link = element->qh_link;
|
|
||||||
element->qh_rlink = 0;
|
|
||||||
}
|
|
||||||
|
|
||||||
#else /* !(__GNUC__ || __INTEL_COMPILER) */
|
|
||||||
|
|
||||||
void insque(void *a, void *b);
|
|
||||||
void remque(void *a);
|
|
||||||
|
|
||||||
#endif /* __GNUC__ || __INTEL_COMPILER */
|
|
||||||
|
|
||||||
#endif /* _KERNEL */
|
|
||||||
|
|
||||||
#endif /* !_SYS_QUEUE_H */
|
|
||||||
|
@@ -27,14 +27,16 @@
|
|||||||
#ifndef LIBBSD_RANDOM_H
|
#ifndef LIBBSD_RANDOM_H
|
||||||
#define LIBBSD_RANDOM_H
|
#define LIBBSD_RANDOM_H
|
||||||
|
|
||||||
#include <sys/cdefs.h>
|
#ifdef LIBBSD_DISABLE_DEPRECATED
|
||||||
#include <sys/types.h>
|
#error "Deprecated header, use <bsd/stdlib.h> instead."
|
||||||
|
#else
|
||||||
__BEGIN_DECLS
|
#warning "Deprecated header, use <bsd/stdlib.h> instead."
|
||||||
u_int32_t arc4random();
|
|
||||||
void arc4random_stir();
|
|
||||||
void arc4random_addrandom(u_char *dat, int datlen);
|
|
||||||
__END_DECLS
|
|
||||||
|
|
||||||
#endif
|
#endif
|
||||||
|
|
||||||
|
#ifdef LIBBSD_OVERLAY
|
||||||
|
#include <stdlib.h>
|
||||||
|
#else
|
||||||
|
#include <bsd/stdlib.h>
|
||||||
|
#endif
|
||||||
|
|
||||||
|
#endif
|
||||||
|
41
include/bsd/readpassphrase.h
Normal file
41
include/bsd/readpassphrase.h
Normal file
@@ -0,0 +1,41 @@
|
|||||||
|
/* $OpenBSD: readpassphrase.h,v 1.4 2003/06/03 01:52:39 millert Exp $ */
|
||||||
|
|
||||||
|
/*
|
||||||
|
* Copyright (c) 2000, 2002 Todd C. Miller <Todd.Miller@courtesan.com>
|
||||||
|
*
|
||||||
|
* Permission to use, copy, modify, and distribute this software for any
|
||||||
|
* purpose with or without fee is hereby granted, provided that the above
|
||||||
|
* copyright notice and this permission notice appear in all copies.
|
||||||
|
*
|
||||||
|
* THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES
|
||||||
|
* WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF
|
||||||
|
* MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR
|
||||||
|
* ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES
|
||||||
|
* WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN
|
||||||
|
* ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF
|
||||||
|
* OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE.
|
||||||
|
*
|
||||||
|
* Sponsored in part by the Defense Advanced Research Projects
|
||||||
|
* Agency (DARPA) and Air Force Research Laboratory, Air Force
|
||||||
|
* Materiel Command, USAF, under agreement number F39502-99-1-0512.
|
||||||
|
*/
|
||||||
|
|
||||||
|
#ifndef _READPASSPHRASE_H_
|
||||||
|
#define _READPASSPHRASE_H_
|
||||||
|
|
||||||
|
#define RPP_ECHO_OFF 0x00 /* Turn off echo (default). */
|
||||||
|
#define RPP_ECHO_ON 0x01 /* Leave echo on. */
|
||||||
|
#define RPP_REQUIRE_TTY 0x02 /* Fail if there is no tty. */
|
||||||
|
#define RPP_FORCELOWER 0x04 /* Force input to lower case. */
|
||||||
|
#define RPP_FORCEUPPER 0x08 /* Force input to upper case. */
|
||||||
|
#define RPP_SEVENBIT 0x10 /* Strip the high bit from input. */
|
||||||
|
#define RPP_STDIN 0x20 /* Read from stdin, not /dev/tty */
|
||||||
|
|
||||||
|
#include <sys/cdefs.h>
|
||||||
|
#include <sys/types.h>
|
||||||
|
|
||||||
|
__BEGIN_DECLS
|
||||||
|
char * readpassphrase(const char *, char *, size_t, int);
|
||||||
|
__END_DECLS
|
||||||
|
|
||||||
|
#endif /* !_READPASSPHRASE_H_ */
|
47
include/bsd/stdio.h
Normal file
47
include/bsd/stdio.h
Normal file
@@ -0,0 +1,47 @@
|
|||||||
|
/*
|
||||||
|
* 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
|
||||||
|
* 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_STDIO_H
|
||||||
|
#define LIBBSD_STDIO_H
|
||||||
|
|
||||||
|
#include <sys/cdefs.h>
|
||||||
|
#include <sys/types.h>
|
||||||
|
|
||||||
|
#ifdef LIBBSD_OVERLAY
|
||||||
|
#include_next <stdio.h>
|
||||||
|
#else
|
||||||
|
#include <stdio.h>
|
||||||
|
#endif
|
||||||
|
|
||||||
|
__BEGIN_DECLS
|
||||||
|
const char *fmtcheck(const char *, const char *);
|
||||||
|
|
||||||
|
char *fgetln(FILE *fp, size_t *lenp);
|
||||||
|
|
||||||
|
int fpurge(FILE *fp);
|
||||||
|
__END_DECLS
|
||||||
|
|
||||||
|
#endif
|
@@ -1,7 +1,7 @@
|
|||||||
/*
|
/*
|
||||||
* Copyright © 2005 Aurelien Jarno
|
* Copyright © 2005 Aurelien Jarno
|
||||||
* Copyright © 2006 Robert Millan
|
* Copyright © 2006 Robert Millan
|
||||||
* Copyright © 2008, 2009 Guillem Jover
|
* Copyright © 2008-2011 Guillem Jover
|
||||||
*
|
*
|
||||||
* Redistribution and use in source and binary forms, with or without
|
* Redistribution and use in source and binary forms, with or without
|
||||||
* modification, are permitted provided that the following conditions
|
* modification, are permitted provided that the following conditions
|
||||||
@@ -31,22 +31,56 @@
|
|||||||
|
|
||||||
#include <sys/cdefs.h>
|
#include <sys/cdefs.h>
|
||||||
#include <sys/stat.h>
|
#include <sys/stat.h>
|
||||||
|
#include <stdint.h>
|
||||||
|
|
||||||
|
#ifdef LIBBSD_OVERLAY
|
||||||
|
#include_next <stdlib.h>
|
||||||
|
#else
|
||||||
#include <stdlib.h>
|
#include <stdlib.h>
|
||||||
|
|
||||||
__BEGIN_DECLS
|
|
||||||
const char *fmtcheck (const char *, const char *);
|
|
||||||
|
|
||||||
char *getprogname ();
|
|
||||||
void setprogname (char *);
|
|
||||||
|
|
||||||
int heapsort (void *, size_t, size_t, int (*)(const void *, const void *));
|
|
||||||
|
|
||||||
#ifndef S_ISTXT
|
|
||||||
#define S_ISTXT S_ISVTX
|
|
||||||
#endif
|
#endif
|
||||||
|
|
||||||
mode_t getmode(const void *set, mode_t mode);
|
/* For compatibility with NetBSD, which defines humanize_number here. */
|
||||||
void *setmode(const char *mode_str);
|
#ifdef LIBBSD_OVERLAY
|
||||||
|
#include <libutil.h>
|
||||||
|
#else
|
||||||
|
#include <bsd/libutil.h>
|
||||||
|
#endif
|
||||||
|
|
||||||
|
/* FIXME: Temporary inclusions to avoid API breakage, will be removed soon. */
|
||||||
|
#ifndef LIBBSD_DISABLE_DEPRECATED
|
||||||
|
#ifdef LIBBSD_OVERLAY
|
||||||
|
#include <stdio.h>
|
||||||
|
#include <unistd.h>
|
||||||
|
#else
|
||||||
|
#include <bsd/stdio.h>
|
||||||
|
#include <bsd/unistd.h>
|
||||||
|
#endif
|
||||||
|
#endif
|
||||||
|
|
||||||
|
__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);
|
||||||
|
|
||||||
|
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 *));
|
||||||
|
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);
|
||||||
|
|
||||||
|
long long strtonum(const char *nptr, long long minval, long long maxval,
|
||||||
|
const char **errstr);
|
||||||
__END_DECLS
|
__END_DECLS
|
||||||
|
|
||||||
#endif
|
#endif
|
||||||
|
@@ -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
|
* Redistribution and use in source and binary forms, with or without
|
||||||
* modification, are permitted provided that the following conditions
|
* modification, are permitted provided that the following conditions
|
||||||
@@ -29,15 +29,25 @@
|
|||||||
|
|
||||||
#include <sys/cdefs.h>
|
#include <sys/cdefs.h>
|
||||||
#include <sys/types.h>
|
#include <sys/types.h>
|
||||||
#include <stddef.h>
|
|
||||||
|
#ifdef LIBBSD_OVERLAY
|
||||||
|
#include_next <string.h>
|
||||||
|
#else
|
||||||
|
#include <string.h>
|
||||||
|
#endif
|
||||||
|
|
||||||
|
#ifndef LIBBSD_DISABLE_DEPRECATED
|
||||||
|
/* FIXME: Temporary inclusion to avoid API breakage, will be removed soon. */
|
||||||
|
#ifdef LIBBSD_OVERLAY
|
||||||
#include <stdio.h>
|
#include <stdio.h>
|
||||||
|
#else
|
||||||
|
#include <bsd/stdio.h>
|
||||||
|
#endif
|
||||||
|
#endif
|
||||||
|
|
||||||
__BEGIN_DECLS
|
__BEGIN_DECLS
|
||||||
size_t strlcpy(char *dst, const char *src, size_t siz);
|
size_t strlcpy(char *dst, const char *src, size_t siz);
|
||||||
size_t strlcat(char *dst, const char *src, size_t siz);
|
size_t strlcat(char *dst, const char *src, size_t siz);
|
||||||
char *fgetln(FILE *fp, size_t *lenp);
|
|
||||||
wchar_t *fgetwln(FILE * __restrict fp, size_t *lenp);
|
|
||||||
|
|
||||||
void strmode(mode_t mode, char *str);
|
void strmode(mode_t mode, char *str);
|
||||||
__END_DECLS
|
__END_DECLS
|
||||||
|
|
||||||
|
146
include/bsd/sys/bitstring.h
Normal file
146
include/bsd/sys/bitstring.h
Normal file
@@ -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_ */
|
141
include/bsd/sys/cdefs.h
Normal file
141
include/bsd/sys/cdefs.h
Normal file
@@ -0,0 +1,141 @@
|
|||||||
|
/*
|
||||||
|
* Copyright © 2004-2006, 2009-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.
|
||||||
|
*/
|
||||||
|
|
||||||
|
#ifndef LIBBSD_SYS_CDEFS_H
|
||||||
|
#define LIBBSD_SYS_CDEFS_H
|
||||||
|
|
||||||
|
#ifdef LIBBSD_OVERLAY
|
||||||
|
#include_next <sys/cdefs.h>
|
||||||
|
#else
|
||||||
|
#include <sys/cdefs.h>
|
||||||
|
#endif
|
||||||
|
|
||||||
|
/*
|
||||||
|
* Some kFreeBSD headers expect those macros to be set for sanity checks.
|
||||||
|
*/
|
||||||
|
#ifndef _SYS_CDEFS_H_
|
||||||
|
#define _SYS_CDEFS_H_
|
||||||
|
#endif
|
||||||
|
#ifndef _SYS_CDEFS_H
|
||||||
|
#define _SYS_CDEFS_H
|
||||||
|
#endif
|
||||||
|
|
||||||
|
#ifdef __GNUC__
|
||||||
|
#define LIBBSD_GCC_VERSION (__GNUC__ << 8 | __GNUC_MINOR__)
|
||||||
|
#else
|
||||||
|
#define LIBBSD_GCC_VERSION 0
|
||||||
|
#endif
|
||||||
|
|
||||||
|
#ifndef __dead2
|
||||||
|
# if LIBBSD_GCC_VERSION >= 0x0207
|
||||||
|
# define __dead2 __attribute__((__noreturn__))
|
||||||
|
# else
|
||||||
|
# define __dead2
|
||||||
|
# endif
|
||||||
|
#endif
|
||||||
|
|
||||||
|
#ifndef __pure2
|
||||||
|
# if LIBBSD_GCC_VERSION >= 0x0207
|
||||||
|
# define __pure2 __attribute__((__const__))
|
||||||
|
# else
|
||||||
|
# define __pure2
|
||||||
|
# endif
|
||||||
|
#endif
|
||||||
|
|
||||||
|
#ifndef __packed
|
||||||
|
# if LIBBSD_GCC_VERSION >= 0x0207
|
||||||
|
# define __packed __attribute__((__packed__))
|
||||||
|
# else
|
||||||
|
# define __packed
|
||||||
|
# endif
|
||||||
|
#endif
|
||||||
|
|
||||||
|
#ifndef __aligned
|
||||||
|
# if LIBBSD_GCC_VERSION >= 0x0207
|
||||||
|
# define __aligned(x) __attribute__((__aligned__(x)))
|
||||||
|
# else
|
||||||
|
# define __aligned(x)
|
||||||
|
# endif
|
||||||
|
#endif
|
||||||
|
|
||||||
|
/* Linux headers define a struct with a member names __unused.
|
||||||
|
* Debian bugs: #522773 (linux), #522774 (libc).
|
||||||
|
* Disable for now. */
|
||||||
|
#if 0
|
||||||
|
#ifndef __unused
|
||||||
|
# if LIBBSD_GCC_VERSION >= 0x0300
|
||||||
|
# define __unused __attribute__((unused))
|
||||||
|
# else
|
||||||
|
# define __unused
|
||||||
|
# endif
|
||||||
|
#endif
|
||||||
|
#endif
|
||||||
|
|
||||||
|
#ifndef __printflike
|
||||||
|
# if LIBBSD_GCC_VERSION >= 0x0300
|
||||||
|
# define __printflike(x, y) __attribute((format(printf, (x), (y))))
|
||||||
|
# else
|
||||||
|
# define __printflike(x, y)
|
||||||
|
# endif
|
||||||
|
#endif
|
||||||
|
|
||||||
|
#ifndef __nonnull
|
||||||
|
# if LIBBSD_GCC_VERSION >= 0x0302
|
||||||
|
# define __nonnull(x) __attribute__((__nonnull__(x)))
|
||||||
|
# else
|
||||||
|
# define __nonnull(x)
|
||||||
|
# endif
|
||||||
|
#endif
|
||||||
|
|
||||||
|
#ifndef __bounded__
|
||||||
|
# define __bounded__(x, y, z)
|
||||||
|
#endif
|
||||||
|
|
||||||
|
#ifndef __RCSID
|
||||||
|
# define __RCSID(x)
|
||||||
|
#endif
|
||||||
|
|
||||||
|
#ifndef __FBSDID
|
||||||
|
# define __FBSDID(x)
|
||||||
|
#endif
|
||||||
|
|
||||||
|
#ifndef __RCSID
|
||||||
|
# define __RCSID(x)
|
||||||
|
#endif
|
||||||
|
|
||||||
|
#ifndef __RCSID_SOURCE
|
||||||
|
# define __RCSID_SOURCE(x)
|
||||||
|
#endif
|
||||||
|
|
||||||
|
#ifndef __SCCSID
|
||||||
|
# define __SCCSID(x)
|
||||||
|
#endif
|
||||||
|
|
||||||
|
#ifndef __COPYRIGHT
|
||||||
|
# define __COPYRIGHT(x)
|
||||||
|
#endif
|
||||||
|
|
||||||
|
#endif
|
52
include/bsd/sys/endian.h
Normal file
52
include/bsd/sys/endian.h
Normal file
@@ -0,0 +1,52 @@
|
|||||||
|
/*
|
||||||
|
* 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.
|
||||||
|
*/
|
||||||
|
|
||||||
|
#ifndef LIBBSD_SYS_ENDIAN_H
|
||||||
|
#define LIBBSD_SYS_ENDIAN_H
|
||||||
|
|
||||||
|
#ifdef LIBBSD_OVERLAY
|
||||||
|
#include_next <endian.h>
|
||||||
|
#else
|
||||||
|
#include <endian.h>
|
||||||
|
#endif
|
||||||
|
|
||||||
|
#ifndef _BYTE_ORDER
|
||||||
|
#define _BYTE_ORDER __BYTE_ORDER
|
||||||
|
#endif
|
||||||
|
|
||||||
|
#ifndef _LITTLE_ENDIAN
|
||||||
|
#define _LITTLE_ENDIAN __LITTLE_ENDIAN
|
||||||
|
#endif
|
||||||
|
|
||||||
|
#ifndef _BIG_ENDIAN
|
||||||
|
#define _BIG_ENDIAN __BIG_ENDIAN
|
||||||
|
#endif
|
||||||
|
|
||||||
|
#ifndef _PDP_ENDIAN
|
||||||
|
#define _PDP_ENDIAN __PDP_ENDIAN
|
||||||
|
#endif
|
||||||
|
|
||||||
|
#endif
|
40
include/bsd/sys/poll.h
Normal file
40
include/bsd/sys/poll.h
Normal file
@@ -0,0 +1,40 @@
|
|||||||
|
/*
|
||||||
|
* 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.
|
||||||
|
*/
|
||||||
|
|
||||||
|
#ifndef LIBBSD_SYS_POLL_H
|
||||||
|
#define LIBBSD_SYS_POLL_H
|
||||||
|
|
||||||
|
#ifdef LIBBSD_OVERLAY
|
||||||
|
#include_next <sys/poll.h>
|
||||||
|
#else
|
||||||
|
#include <sys/poll.h>
|
||||||
|
#endif
|
||||||
|
|
||||||
|
#ifndef INFTIM
|
||||||
|
#define INFTIM (-1)
|
||||||
|
#endif
|
||||||
|
|
||||||
|
#endif
|
622
include/bsd/sys/queue.h
Normal file
622
include/bsd/sys/queue.h
Normal file
@@ -0,0 +1,622 @@
|
|||||||
|
/*-
|
||||||
|
* Copyright (c) 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.
|
||||||
|
*
|
||||||
|
* @(#)queue.h 8.5 (Berkeley) 8/20/94
|
||||||
|
* $FreeBSD$
|
||||||
|
*/
|
||||||
|
|
||||||
|
#ifndef _SYS_QUEUE_H_
|
||||||
|
#define _SYS_QUEUE_H_
|
||||||
|
|
||||||
|
#include <sys/cdefs.h>
|
||||||
|
|
||||||
|
/*
|
||||||
|
* This file defines four types of data structures: singly-linked lists,
|
||||||
|
* singly-linked tail queues, lists and tail queues.
|
||||||
|
*
|
||||||
|
* A singly-linked list is headed by a single forward pointer. The elements
|
||||||
|
* are singly linked for minimum space and pointer manipulation overhead at
|
||||||
|
* the expense of O(n) removal for arbitrary elements. New elements can be
|
||||||
|
* added to the list after an existing element or at the head of the list.
|
||||||
|
* Elements being removed from the head of the list should use the explicit
|
||||||
|
* macro for this purpose for optimum efficiency. A singly-linked list may
|
||||||
|
* only be traversed in the forward direction. Singly-linked lists are ideal
|
||||||
|
* for applications with large datasets and few or no removals or for
|
||||||
|
* implementing a LIFO queue.
|
||||||
|
*
|
||||||
|
* A singly-linked tail queue is headed by a pair of pointers, one to the
|
||||||
|
* head of the list and the other to the tail of the list. The elements are
|
||||||
|
* singly linked for minimum space and pointer manipulation overhead at the
|
||||||
|
* expense of O(n) removal for arbitrary elements. New elements can be added
|
||||||
|
* to the list after an existing element, at the head of the list, or at the
|
||||||
|
* end of the list. Elements being removed from the head of the tail queue
|
||||||
|
* should use the explicit macro for this purpose for optimum efficiency.
|
||||||
|
* A singly-linked tail queue may only be traversed in the forward direction.
|
||||||
|
* Singly-linked tail queues are ideal for applications with large datasets
|
||||||
|
* and few or no removals or for implementing a FIFO queue.
|
||||||
|
*
|
||||||
|
* A list is headed by a single forward pointer (or an array of forward
|
||||||
|
* pointers for a hash table header). The elements are doubly linked
|
||||||
|
* so that an arbitrary element can be removed without a need to
|
||||||
|
* traverse the list. New elements can be added to the list before
|
||||||
|
* or after an existing element or at the head of the list. A list
|
||||||
|
* may only be traversed in the forward direction.
|
||||||
|
*
|
||||||
|
* A tail queue is headed by a pair of pointers, one to the head of the
|
||||||
|
* list and the other to the tail of the list. The elements are doubly
|
||||||
|
* linked so that an arbitrary element can be removed without a need to
|
||||||
|
* traverse the list. New elements can be added to the list before or
|
||||||
|
* after an existing element, at the head of the list, or at the end of
|
||||||
|
* the list. A tail queue may be traversed in either direction.
|
||||||
|
*
|
||||||
|
* For details on the use of these macros, see the queue(3) manual page.
|
||||||
|
*
|
||||||
|
*
|
||||||
|
* SLIST LIST STAILQ TAILQ
|
||||||
|
* _HEAD + + + +
|
||||||
|
* _HEAD_INITIALIZER + + + +
|
||||||
|
* _ENTRY + + + +
|
||||||
|
* _INIT + + + +
|
||||||
|
* _EMPTY + + + +
|
||||||
|
* _FIRST + + + +
|
||||||
|
* _NEXT + + + +
|
||||||
|
* _PREV - - - +
|
||||||
|
* _LAST - - + +
|
||||||
|
* _FOREACH + + + +
|
||||||
|
* _FOREACH_SAFE + + + +
|
||||||
|
* _FOREACH_REVERSE - - - +
|
||||||
|
* _FOREACH_REVERSE_SAFE - - - +
|
||||||
|
* _INSERT_HEAD + + + +
|
||||||
|
* _INSERT_BEFORE - + - +
|
||||||
|
* _INSERT_AFTER + + + +
|
||||||
|
* _INSERT_TAIL - - + +
|
||||||
|
* _CONCAT - - + +
|
||||||
|
* _REMOVE_AFTER + - + -
|
||||||
|
* _REMOVE_HEAD + - + -
|
||||||
|
* _REMOVE + + + +
|
||||||
|
*
|
||||||
|
*/
|
||||||
|
#ifdef QUEUE_MACRO_DEBUG
|
||||||
|
/* Store the last 2 places the queue element or head was altered */
|
||||||
|
struct qm_trace {
|
||||||
|
char * lastfile;
|
||||||
|
int lastline;
|
||||||
|
char * prevfile;
|
||||||
|
int prevline;
|
||||||
|
};
|
||||||
|
|
||||||
|
#define TRACEBUF struct qm_trace trace;
|
||||||
|
#define TRASHIT(x) do {(x) = (void *)-1;} while (0)
|
||||||
|
|
||||||
|
#define QMD_TRACE_HEAD(head) do { \
|
||||||
|
(head)->trace.prevline = (head)->trace.lastline; \
|
||||||
|
(head)->trace.prevfile = (head)->trace.lastfile; \
|
||||||
|
(head)->trace.lastline = __LINE__; \
|
||||||
|
(head)->trace.lastfile = __FILE__; \
|
||||||
|
} while (0)
|
||||||
|
|
||||||
|
#define QMD_TRACE_ELEM(elem) do { \
|
||||||
|
(elem)->trace.prevline = (elem)->trace.lastline; \
|
||||||
|
(elem)->trace.prevfile = (elem)->trace.lastfile; \
|
||||||
|
(elem)->trace.lastline = __LINE__; \
|
||||||
|
(elem)->trace.lastfile = __FILE__; \
|
||||||
|
} while (0)
|
||||||
|
|
||||||
|
#else
|
||||||
|
#define QMD_TRACE_ELEM(elem)
|
||||||
|
#define QMD_TRACE_HEAD(head)
|
||||||
|
#define TRACEBUF
|
||||||
|
#define TRASHIT(x)
|
||||||
|
#endif /* QUEUE_MACRO_DEBUG */
|
||||||
|
|
||||||
|
/*
|
||||||
|
* Singly-linked List declarations.
|
||||||
|
*/
|
||||||
|
#define SLIST_HEAD(name, type) \
|
||||||
|
struct name { \
|
||||||
|
struct type *slh_first; /* first element */ \
|
||||||
|
}
|
||||||
|
|
||||||
|
#define SLIST_HEAD_INITIALIZER(head) \
|
||||||
|
{ NULL }
|
||||||
|
|
||||||
|
#define SLIST_ENTRY(type) \
|
||||||
|
struct { \
|
||||||
|
struct type *sle_next; /* next element */ \
|
||||||
|
}
|
||||||
|
|
||||||
|
/*
|
||||||
|
* Singly-linked List functions.
|
||||||
|
*/
|
||||||
|
#define SLIST_EMPTY(head) ((head)->slh_first == NULL)
|
||||||
|
|
||||||
|
#define SLIST_FIRST(head) ((head)->slh_first)
|
||||||
|
|
||||||
|
#define SLIST_FOREACH(var, head, field) \
|
||||||
|
for ((var) = SLIST_FIRST((head)); \
|
||||||
|
(var); \
|
||||||
|
(var) = SLIST_NEXT((var), field))
|
||||||
|
|
||||||
|
#define SLIST_FOREACH_SAFE(var, head, field, tvar) \
|
||||||
|
for ((var) = SLIST_FIRST((head)); \
|
||||||
|
(var) && ((tvar) = SLIST_NEXT((var), field), 1); \
|
||||||
|
(var) = (tvar))
|
||||||
|
|
||||||
|
#define SLIST_FOREACH_PREVPTR(var, varp, head, field) \
|
||||||
|
for ((varp) = &SLIST_FIRST((head)); \
|
||||||
|
((var) = *(varp)) != NULL; \
|
||||||
|
(varp) = &SLIST_NEXT((var), field))
|
||||||
|
|
||||||
|
#define SLIST_INIT(head) do { \
|
||||||
|
SLIST_FIRST((head)) = NULL; \
|
||||||
|
} while (0)
|
||||||
|
|
||||||
|
#define SLIST_INSERT_AFTER(slistelm, elm, field) do { \
|
||||||
|
SLIST_NEXT((elm), field) = SLIST_NEXT((slistelm), field); \
|
||||||
|
SLIST_NEXT((slistelm), field) = (elm); \
|
||||||
|
} while (0)
|
||||||
|
|
||||||
|
#define SLIST_INSERT_HEAD(head, elm, field) do { \
|
||||||
|
SLIST_NEXT((elm), field) = SLIST_FIRST((head)); \
|
||||||
|
SLIST_FIRST((head)) = (elm); \
|
||||||
|
} while (0)
|
||||||
|
|
||||||
|
#define SLIST_NEXT(elm, field) ((elm)->field.sle_next)
|
||||||
|
|
||||||
|
#define SLIST_REMOVE(head, elm, type, field) do { \
|
||||||
|
if (SLIST_FIRST((head)) == (elm)) { \
|
||||||
|
SLIST_REMOVE_HEAD((head), field); \
|
||||||
|
} \
|
||||||
|
else { \
|
||||||
|
struct type *curelm = SLIST_FIRST((head)); \
|
||||||
|
while (SLIST_NEXT(curelm, field) != (elm)) \
|
||||||
|
curelm = SLIST_NEXT(curelm, field); \
|
||||||
|
SLIST_REMOVE_AFTER(curelm, field); \
|
||||||
|
} \
|
||||||
|
TRASHIT((elm)->field.sle_next); \
|
||||||
|
} while (0)
|
||||||
|
|
||||||
|
#define SLIST_REMOVE_AFTER(elm, field) do { \
|
||||||
|
SLIST_NEXT(elm, field) = \
|
||||||
|
SLIST_NEXT(SLIST_NEXT(elm, field), field); \
|
||||||
|
} while (0)
|
||||||
|
|
||||||
|
#define SLIST_REMOVE_HEAD(head, field) do { \
|
||||||
|
SLIST_FIRST((head)) = SLIST_NEXT(SLIST_FIRST((head)), field); \
|
||||||
|
} while (0)
|
||||||
|
|
||||||
|
/*
|
||||||
|
* Singly-linked Tail queue declarations.
|
||||||
|
*/
|
||||||
|
#define STAILQ_HEAD(name, type) \
|
||||||
|
struct name { \
|
||||||
|
struct type *stqh_first;/* first element */ \
|
||||||
|
struct type **stqh_last;/* addr of last next element */ \
|
||||||
|
}
|
||||||
|
|
||||||
|
#define STAILQ_HEAD_INITIALIZER(head) \
|
||||||
|
{ NULL, &(head).stqh_first }
|
||||||
|
|
||||||
|
#define STAILQ_ENTRY(type) \
|
||||||
|
struct { \
|
||||||
|
struct type *stqe_next; /* next element */ \
|
||||||
|
}
|
||||||
|
|
||||||
|
/*
|
||||||
|
* Singly-linked Tail queue functions.
|
||||||
|
*/
|
||||||
|
#define STAILQ_CONCAT(head1, head2) do { \
|
||||||
|
if (!STAILQ_EMPTY((head2))) { \
|
||||||
|
*(head1)->stqh_last = (head2)->stqh_first; \
|
||||||
|
(head1)->stqh_last = (head2)->stqh_last; \
|
||||||
|
STAILQ_INIT((head2)); \
|
||||||
|
} \
|
||||||
|
} while (0)
|
||||||
|
|
||||||
|
#define STAILQ_EMPTY(head) ((head)->stqh_first == NULL)
|
||||||
|
|
||||||
|
#define STAILQ_FIRST(head) ((head)->stqh_first)
|
||||||
|
|
||||||
|
#define STAILQ_FOREACH(var, head, field) \
|
||||||
|
for((var) = STAILQ_FIRST((head)); \
|
||||||
|
(var); \
|
||||||
|
(var) = STAILQ_NEXT((var), field))
|
||||||
|
|
||||||
|
|
||||||
|
#define STAILQ_FOREACH_SAFE(var, head, field, tvar) \
|
||||||
|
for ((var) = STAILQ_FIRST((head)); \
|
||||||
|
(var) && ((tvar) = STAILQ_NEXT((var), field), 1); \
|
||||||
|
(var) = (tvar))
|
||||||
|
|
||||||
|
#define STAILQ_INIT(head) do { \
|
||||||
|
STAILQ_FIRST((head)) = NULL; \
|
||||||
|
(head)->stqh_last = &STAILQ_FIRST((head)); \
|
||||||
|
} while (0)
|
||||||
|
|
||||||
|
#define STAILQ_INSERT_AFTER(head, tqelm, elm, field) do { \
|
||||||
|
if ((STAILQ_NEXT((elm), field) = STAILQ_NEXT((tqelm), field)) == NULL)\
|
||||||
|
(head)->stqh_last = &STAILQ_NEXT((elm), field); \
|
||||||
|
STAILQ_NEXT((tqelm), field) = (elm); \
|
||||||
|
} while (0)
|
||||||
|
|
||||||
|
#define STAILQ_INSERT_HEAD(head, elm, field) do { \
|
||||||
|
if ((STAILQ_NEXT((elm), field) = STAILQ_FIRST((head))) == NULL) \
|
||||||
|
(head)->stqh_last = &STAILQ_NEXT((elm), field); \
|
||||||
|
STAILQ_FIRST((head)) = (elm); \
|
||||||
|
} while (0)
|
||||||
|
|
||||||
|
#define STAILQ_INSERT_TAIL(head, elm, field) do { \
|
||||||
|
STAILQ_NEXT((elm), field) = NULL; \
|
||||||
|
*(head)->stqh_last = (elm); \
|
||||||
|
(head)->stqh_last = &STAILQ_NEXT((elm), field); \
|
||||||
|
} while (0)
|
||||||
|
|
||||||
|
#define STAILQ_LAST(head, type, field) \
|
||||||
|
(STAILQ_EMPTY((head)) ? \
|
||||||
|
NULL : \
|
||||||
|
((struct type *)(void *) \
|
||||||
|
((char *)((head)->stqh_last) - __offsetof(struct type, field))))
|
||||||
|
|
||||||
|
#define STAILQ_NEXT(elm, field) ((elm)->field.stqe_next)
|
||||||
|
|
||||||
|
#define STAILQ_REMOVE(head, elm, type, field) do { \
|
||||||
|
if (STAILQ_FIRST((head)) == (elm)) { \
|
||||||
|
STAILQ_REMOVE_HEAD((head), field); \
|
||||||
|
} \
|
||||||
|
else { \
|
||||||
|
struct type *curelm = STAILQ_FIRST((head)); \
|
||||||
|
while (STAILQ_NEXT(curelm, field) != (elm)) \
|
||||||
|
curelm = STAILQ_NEXT(curelm, field); \
|
||||||
|
STAILQ_REMOVE_AFTER(head, curelm, field); \
|
||||||
|
} \
|
||||||
|
TRASHIT((elm)->field.stqe_next); \
|
||||||
|
} while (0)
|
||||||
|
|
||||||
|
#define STAILQ_REMOVE_HEAD(head, field) do { \
|
||||||
|
if ((STAILQ_FIRST((head)) = \
|
||||||
|
STAILQ_NEXT(STAILQ_FIRST((head)), field)) == NULL) \
|
||||||
|
(head)->stqh_last = &STAILQ_FIRST((head)); \
|
||||||
|
} while (0)
|
||||||
|
|
||||||
|
#define STAILQ_REMOVE_AFTER(head, elm, field) do { \
|
||||||
|
if ((STAILQ_NEXT(elm, field) = \
|
||||||
|
STAILQ_NEXT(STAILQ_NEXT(elm, field), field)) == NULL) \
|
||||||
|
(head)->stqh_last = &STAILQ_NEXT((elm), field); \
|
||||||
|
} while (0)
|
||||||
|
|
||||||
|
#define STAILQ_SWAP(head1, head2, type) do { \
|
||||||
|
struct type *swap_first = STAILQ_FIRST(head1); \
|
||||||
|
struct type **swap_last = (head1)->stqh_last; \
|
||||||
|
STAILQ_FIRST(head1) = STAILQ_FIRST(head2); \
|
||||||
|
(head1)->stqh_last = (head2)->stqh_last; \
|
||||||
|
STAILQ_FIRST(head2) = swap_first; \
|
||||||
|
(head2)->stqh_last = swap_last; \
|
||||||
|
if (STAILQ_EMPTY(head1)) \
|
||||||
|
(head1)->stqh_last = &STAILQ_FIRST(head1); \
|
||||||
|
if (STAILQ_EMPTY(head2)) \
|
||||||
|
(head2)->stqh_last = &STAILQ_FIRST(head2); \
|
||||||
|
} while (0)
|
||||||
|
|
||||||
|
|
||||||
|
/*
|
||||||
|
* List declarations.
|
||||||
|
*/
|
||||||
|
#define LIST_HEAD(name, type) \
|
||||||
|
struct name { \
|
||||||
|
struct type *lh_first; /* first element */ \
|
||||||
|
}
|
||||||
|
|
||||||
|
#define LIST_HEAD_INITIALIZER(head) \
|
||||||
|
{ NULL }
|
||||||
|
|
||||||
|
#define LIST_ENTRY(type) \
|
||||||
|
struct { \
|
||||||
|
struct type *le_next; /* next element */ \
|
||||||
|
struct type **le_prev; /* address of previous next element */ \
|
||||||
|
}
|
||||||
|
|
||||||
|
/*
|
||||||
|
* List functions.
|
||||||
|
*/
|
||||||
|
|
||||||
|
#if (defined(_KERNEL) && defined(INVARIANTS))
|
||||||
|
#define QMD_LIST_CHECK_HEAD(head, field) do { \
|
||||||
|
if (LIST_FIRST((head)) != NULL && \
|
||||||
|
LIST_FIRST((head))->field.le_prev != \
|
||||||
|
&LIST_FIRST((head))) \
|
||||||
|
panic("Bad list head %p first->prev != head", (head)); \
|
||||||
|
} while (0)
|
||||||
|
|
||||||
|
#define QMD_LIST_CHECK_NEXT(elm, field) do { \
|
||||||
|
if (LIST_NEXT((elm), field) != NULL && \
|
||||||
|
LIST_NEXT((elm), field)->field.le_prev != \
|
||||||
|
&((elm)->field.le_next)) \
|
||||||
|
panic("Bad link elm %p next->prev != elm", (elm)); \
|
||||||
|
} while (0)
|
||||||
|
|
||||||
|
#define QMD_LIST_CHECK_PREV(elm, field) do { \
|
||||||
|
if (*(elm)->field.le_prev != (elm)) \
|
||||||
|
panic("Bad link elm %p prev->next != elm", (elm)); \
|
||||||
|
} while (0)
|
||||||
|
#else
|
||||||
|
#define QMD_LIST_CHECK_HEAD(head, field)
|
||||||
|
#define QMD_LIST_CHECK_NEXT(elm, field)
|
||||||
|
#define QMD_LIST_CHECK_PREV(elm, field)
|
||||||
|
#endif /* (_KERNEL && INVARIANTS) */
|
||||||
|
|
||||||
|
#define LIST_EMPTY(head) ((head)->lh_first == NULL)
|
||||||
|
|
||||||
|
#define LIST_FIRST(head) ((head)->lh_first)
|
||||||
|
|
||||||
|
#define LIST_FOREACH(var, head, field) \
|
||||||
|
for ((var) = LIST_FIRST((head)); \
|
||||||
|
(var); \
|
||||||
|
(var) = LIST_NEXT((var), field))
|
||||||
|
|
||||||
|
#define LIST_FOREACH_SAFE(var, head, field, tvar) \
|
||||||
|
for ((var) = LIST_FIRST((head)); \
|
||||||
|
(var) && ((tvar) = LIST_NEXT((var), field), 1); \
|
||||||
|
(var) = (tvar))
|
||||||
|
|
||||||
|
#define LIST_INIT(head) do { \
|
||||||
|
LIST_FIRST((head)) = NULL; \
|
||||||
|
} while (0)
|
||||||
|
|
||||||
|
#define LIST_INSERT_AFTER(listelm, elm, field) do { \
|
||||||
|
QMD_LIST_CHECK_NEXT(listelm, field); \
|
||||||
|
if ((LIST_NEXT((elm), field) = LIST_NEXT((listelm), field)) != NULL)\
|
||||||
|
LIST_NEXT((listelm), field)->field.le_prev = \
|
||||||
|
&LIST_NEXT((elm), field); \
|
||||||
|
LIST_NEXT((listelm), field) = (elm); \
|
||||||
|
(elm)->field.le_prev = &LIST_NEXT((listelm), field); \
|
||||||
|
} while (0)
|
||||||
|
|
||||||
|
#define LIST_INSERT_BEFORE(listelm, elm, field) do { \
|
||||||
|
QMD_LIST_CHECK_PREV(listelm, field); \
|
||||||
|
(elm)->field.le_prev = (listelm)->field.le_prev; \
|
||||||
|
LIST_NEXT((elm), field) = (listelm); \
|
||||||
|
*(listelm)->field.le_prev = (elm); \
|
||||||
|
(listelm)->field.le_prev = &LIST_NEXT((elm), field); \
|
||||||
|
} while (0)
|
||||||
|
|
||||||
|
#define LIST_INSERT_HEAD(head, elm, field) do { \
|
||||||
|
QMD_LIST_CHECK_HEAD((head), field); \
|
||||||
|
if ((LIST_NEXT((elm), field) = LIST_FIRST((head))) != NULL) \
|
||||||
|
LIST_FIRST((head))->field.le_prev = &LIST_NEXT((elm), field);\
|
||||||
|
LIST_FIRST((head)) = (elm); \
|
||||||
|
(elm)->field.le_prev = &LIST_FIRST((head)); \
|
||||||
|
} while (0)
|
||||||
|
|
||||||
|
#define LIST_NEXT(elm, field) ((elm)->field.le_next)
|
||||||
|
|
||||||
|
#define LIST_REMOVE(elm, field) do { \
|
||||||
|
QMD_LIST_CHECK_NEXT(elm, field); \
|
||||||
|
QMD_LIST_CHECK_PREV(elm, field); \
|
||||||
|
if (LIST_NEXT((elm), field) != NULL) \
|
||||||
|
LIST_NEXT((elm), field)->field.le_prev = \
|
||||||
|
(elm)->field.le_prev; \
|
||||||
|
*(elm)->field.le_prev = LIST_NEXT((elm), field); \
|
||||||
|
TRASHIT((elm)->field.le_next); \
|
||||||
|
TRASHIT((elm)->field.le_prev); \
|
||||||
|
} while (0)
|
||||||
|
|
||||||
|
#define LIST_SWAP(head1, head2, type, field) do { \
|
||||||
|
struct type *swap_tmp = LIST_FIRST((head1)); \
|
||||||
|
LIST_FIRST((head1)) = LIST_FIRST((head2)); \
|
||||||
|
LIST_FIRST((head2)) = swap_tmp; \
|
||||||
|
if ((swap_tmp = LIST_FIRST((head1))) != NULL) \
|
||||||
|
swap_tmp->field.le_prev = &LIST_FIRST((head1)); \
|
||||||
|
if ((swap_tmp = LIST_FIRST((head2))) != NULL) \
|
||||||
|
swap_tmp->field.le_prev = &LIST_FIRST((head2)); \
|
||||||
|
} while (0)
|
||||||
|
|
||||||
|
/*
|
||||||
|
* Tail queue declarations.
|
||||||
|
*/
|
||||||
|
#define TAILQ_HEAD(name, type) \
|
||||||
|
struct name { \
|
||||||
|
struct type *tqh_first; /* first element */ \
|
||||||
|
struct type **tqh_last; /* addr of last next element */ \
|
||||||
|
TRACEBUF \
|
||||||
|
}
|
||||||
|
|
||||||
|
#define TAILQ_HEAD_INITIALIZER(head) \
|
||||||
|
{ NULL, &(head).tqh_first }
|
||||||
|
|
||||||
|
#define TAILQ_ENTRY(type) \
|
||||||
|
struct { \
|
||||||
|
struct type *tqe_next; /* next element */ \
|
||||||
|
struct type **tqe_prev; /* address of previous next element */ \
|
||||||
|
TRACEBUF \
|
||||||
|
}
|
||||||
|
|
||||||
|
/*
|
||||||
|
* Tail queue functions.
|
||||||
|
*/
|
||||||
|
#if (defined(_KERNEL) && defined(INVARIANTS))
|
||||||
|
#define QMD_TAILQ_CHECK_HEAD(head, field) do { \
|
||||||
|
if (!TAILQ_EMPTY(head) && \
|
||||||
|
TAILQ_FIRST((head))->field.tqe_prev != \
|
||||||
|
&TAILQ_FIRST((head))) \
|
||||||
|
panic("Bad tailq head %p first->prev != head", (head)); \
|
||||||
|
} while (0)
|
||||||
|
|
||||||
|
#define QMD_TAILQ_CHECK_TAIL(head, field) do { \
|
||||||
|
if (*(head)->tqh_last != NULL) \
|
||||||
|
panic("Bad tailq NEXT(%p->tqh_last) != NULL", (head)); \
|
||||||
|
} while (0)
|
||||||
|
|
||||||
|
#define QMD_TAILQ_CHECK_NEXT(elm, field) do { \
|
||||||
|
if (TAILQ_NEXT((elm), field) != NULL && \
|
||||||
|
TAILQ_NEXT((elm), field)->field.tqe_prev != \
|
||||||
|
&((elm)->field.tqe_next)) \
|
||||||
|
panic("Bad link elm %p next->prev != elm", (elm)); \
|
||||||
|
} while (0)
|
||||||
|
|
||||||
|
#define QMD_TAILQ_CHECK_PREV(elm, field) do { \
|
||||||
|
if (*(elm)->field.tqe_prev != (elm)) \
|
||||||
|
panic("Bad link elm %p prev->next != elm", (elm)); \
|
||||||
|
} while (0)
|
||||||
|
#else
|
||||||
|
#define QMD_TAILQ_CHECK_HEAD(head, field)
|
||||||
|
#define QMD_TAILQ_CHECK_TAIL(head, headname)
|
||||||
|
#define QMD_TAILQ_CHECK_NEXT(elm, field)
|
||||||
|
#define QMD_TAILQ_CHECK_PREV(elm, field)
|
||||||
|
#endif /* (_KERNEL && INVARIANTS) */
|
||||||
|
|
||||||
|
#define TAILQ_CONCAT(head1, head2, field) do { \
|
||||||
|
if (!TAILQ_EMPTY(head2)) { \
|
||||||
|
*(head1)->tqh_last = (head2)->tqh_first; \
|
||||||
|
(head2)->tqh_first->field.tqe_prev = (head1)->tqh_last; \
|
||||||
|
(head1)->tqh_last = (head2)->tqh_last; \
|
||||||
|
TAILQ_INIT((head2)); \
|
||||||
|
QMD_TRACE_HEAD(head1); \
|
||||||
|
QMD_TRACE_HEAD(head2); \
|
||||||
|
} \
|
||||||
|
} while (0)
|
||||||
|
|
||||||
|
#define TAILQ_EMPTY(head) ((head)->tqh_first == NULL)
|
||||||
|
|
||||||
|
#define TAILQ_FIRST(head) ((head)->tqh_first)
|
||||||
|
|
||||||
|
#define TAILQ_FOREACH(var, head, field) \
|
||||||
|
for ((var) = TAILQ_FIRST((head)); \
|
||||||
|
(var); \
|
||||||
|
(var) = TAILQ_NEXT((var), field))
|
||||||
|
|
||||||
|
#define TAILQ_FOREACH_SAFE(var, head, field, tvar) \
|
||||||
|
for ((var) = TAILQ_FIRST((head)); \
|
||||||
|
(var) && ((tvar) = TAILQ_NEXT((var), field), 1); \
|
||||||
|
(var) = (tvar))
|
||||||
|
|
||||||
|
#define TAILQ_FOREACH_REVERSE(var, head, headname, field) \
|
||||||
|
for ((var) = TAILQ_LAST((head), headname); \
|
||||||
|
(var); \
|
||||||
|
(var) = TAILQ_PREV((var), headname, field))
|
||||||
|
|
||||||
|
#define TAILQ_FOREACH_REVERSE_SAFE(var, head, headname, field, tvar) \
|
||||||
|
for ((var) = TAILQ_LAST((head), headname); \
|
||||||
|
(var) && ((tvar) = TAILQ_PREV((var), headname, field), 1); \
|
||||||
|
(var) = (tvar))
|
||||||
|
|
||||||
|
#define TAILQ_INIT(head) do { \
|
||||||
|
TAILQ_FIRST((head)) = NULL; \
|
||||||
|
(head)->tqh_last = &TAILQ_FIRST((head)); \
|
||||||
|
QMD_TRACE_HEAD(head); \
|
||||||
|
} while (0)
|
||||||
|
|
||||||
|
#define TAILQ_INSERT_AFTER(head, listelm, elm, field) do { \
|
||||||
|
QMD_TAILQ_CHECK_NEXT(listelm, field); \
|
||||||
|
if ((TAILQ_NEXT((elm), field) = TAILQ_NEXT((listelm), field)) != NULL)\
|
||||||
|
TAILQ_NEXT((elm), field)->field.tqe_prev = \
|
||||||
|
&TAILQ_NEXT((elm), field); \
|
||||||
|
else { \
|
||||||
|
(head)->tqh_last = &TAILQ_NEXT((elm), field); \
|
||||||
|
QMD_TRACE_HEAD(head); \
|
||||||
|
} \
|
||||||
|
TAILQ_NEXT((listelm), field) = (elm); \
|
||||||
|
(elm)->field.tqe_prev = &TAILQ_NEXT((listelm), field); \
|
||||||
|
QMD_TRACE_ELEM(&(elm)->field); \
|
||||||
|
QMD_TRACE_ELEM(&listelm->field); \
|
||||||
|
} while (0)
|
||||||
|
|
||||||
|
#define TAILQ_INSERT_BEFORE(listelm, elm, field) do { \
|
||||||
|
QMD_TAILQ_CHECK_PREV(listelm, field); \
|
||||||
|
(elm)->field.tqe_prev = (listelm)->field.tqe_prev; \
|
||||||
|
TAILQ_NEXT((elm), field) = (listelm); \
|
||||||
|
*(listelm)->field.tqe_prev = (elm); \
|
||||||
|
(listelm)->field.tqe_prev = &TAILQ_NEXT((elm), field); \
|
||||||
|
QMD_TRACE_ELEM(&(elm)->field); \
|
||||||
|
QMD_TRACE_ELEM(&listelm->field); \
|
||||||
|
} while (0)
|
||||||
|
|
||||||
|
#define TAILQ_INSERT_HEAD(head, elm, field) do { \
|
||||||
|
QMD_TAILQ_CHECK_HEAD(head, field); \
|
||||||
|
if ((TAILQ_NEXT((elm), field) = TAILQ_FIRST((head))) != NULL) \
|
||||||
|
TAILQ_FIRST((head))->field.tqe_prev = \
|
||||||
|
&TAILQ_NEXT((elm), field); \
|
||||||
|
else \
|
||||||
|
(head)->tqh_last = &TAILQ_NEXT((elm), field); \
|
||||||
|
TAILQ_FIRST((head)) = (elm); \
|
||||||
|
(elm)->field.tqe_prev = &TAILQ_FIRST((head)); \
|
||||||
|
QMD_TRACE_HEAD(head); \
|
||||||
|
QMD_TRACE_ELEM(&(elm)->field); \
|
||||||
|
} while (0)
|
||||||
|
|
||||||
|
#define TAILQ_INSERT_TAIL(head, elm, field) do { \
|
||||||
|
QMD_TAILQ_CHECK_TAIL(head, field); \
|
||||||
|
TAILQ_NEXT((elm), field) = NULL; \
|
||||||
|
(elm)->field.tqe_prev = (head)->tqh_last; \
|
||||||
|
*(head)->tqh_last = (elm); \
|
||||||
|
(head)->tqh_last = &TAILQ_NEXT((elm), field); \
|
||||||
|
QMD_TRACE_HEAD(head); \
|
||||||
|
QMD_TRACE_ELEM(&(elm)->field); \
|
||||||
|
} while (0)
|
||||||
|
|
||||||
|
#define TAILQ_LAST(head, headname) \
|
||||||
|
(*(((struct headname *)((head)->tqh_last))->tqh_last))
|
||||||
|
|
||||||
|
#define TAILQ_NEXT(elm, field) ((elm)->field.tqe_next)
|
||||||
|
|
||||||
|
#define TAILQ_PREV(elm, headname, field) \
|
||||||
|
(*(((struct headname *)((elm)->field.tqe_prev))->tqh_last))
|
||||||
|
|
||||||
|
#define TAILQ_REMOVE(head, elm, field) do { \
|
||||||
|
QMD_TAILQ_CHECK_NEXT(elm, field); \
|
||||||
|
QMD_TAILQ_CHECK_PREV(elm, field); \
|
||||||
|
if ((TAILQ_NEXT((elm), field)) != NULL) \
|
||||||
|
TAILQ_NEXT((elm), field)->field.tqe_prev = \
|
||||||
|
(elm)->field.tqe_prev; \
|
||||||
|
else { \
|
||||||
|
(head)->tqh_last = (elm)->field.tqe_prev; \
|
||||||
|
QMD_TRACE_HEAD(head); \
|
||||||
|
} \
|
||||||
|
*(elm)->field.tqe_prev = TAILQ_NEXT((elm), field); \
|
||||||
|
TRASHIT((elm)->field.tqe_next); \
|
||||||
|
TRASHIT((elm)->field.tqe_prev); \
|
||||||
|
QMD_TRACE_ELEM(&(elm)->field); \
|
||||||
|
} while (0)
|
||||||
|
|
||||||
|
#define TAILQ_SWAP(head1, head2, type, field) do { \
|
||||||
|
struct type *swap_first = (head1)->tqh_first; \
|
||||||
|
struct type **swap_last = (head1)->tqh_last; \
|
||||||
|
(head1)->tqh_first = (head2)->tqh_first; \
|
||||||
|
(head1)->tqh_last = (head2)->tqh_last; \
|
||||||
|
(head2)->tqh_first = swap_first; \
|
||||||
|
(head2)->tqh_last = swap_last; \
|
||||||
|
if ((swap_first = (head1)->tqh_first) != NULL) \
|
||||||
|
swap_first->field.tqe_prev = &(head1)->tqh_first; \
|
||||||
|
else \
|
||||||
|
(head1)->tqh_last = &(head1)->tqh_first; \
|
||||||
|
if ((swap_first = (head2)->tqh_first) != NULL) \
|
||||||
|
swap_first->field.tqe_prev = &(head2)->tqh_first; \
|
||||||
|
else \
|
||||||
|
(head2)->tqh_last = &(head2)->tqh_first; \
|
||||||
|
} while (0)
|
||||||
|
|
||||||
|
#endif /* !_SYS_QUEUE_H_ */
|
765
include/bsd/sys/tree.h
Normal file
765
include/bsd/sys/tree.h
Normal file
@@ -0,0 +1,765 @@
|
|||||||
|
/* $NetBSD: tree.h,v 1.8 2004/03/28 19:38:30 provos Exp $ */
|
||||||
|
/* $OpenBSD: tree.h,v 1.7 2002/10/17 21:51:54 art Exp $ */
|
||||||
|
/* $FreeBSD$ */
|
||||||
|
|
||||||
|
/*-
|
||||||
|
* Copyright 2002 Niels Provos <provos@citi.umich.edu>
|
||||||
|
* 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 ``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 _SYS_TREE_H_
|
||||||
|
#define _SYS_TREE_H_
|
||||||
|
|
||||||
|
#include <sys/cdefs.h>
|
||||||
|
|
||||||
|
/*
|
||||||
|
* This file defines data structures for different types of trees:
|
||||||
|
* splay trees and red-black trees.
|
||||||
|
*
|
||||||
|
* A splay tree is a self-organizing data structure. Every operation
|
||||||
|
* on the tree causes a splay to happen. The splay moves the requested
|
||||||
|
* node to the root of the tree and partly rebalances it.
|
||||||
|
*
|
||||||
|
* This has the benefit that request locality causes faster lookups as
|
||||||
|
* the requested nodes move to the top of the tree. On the other hand,
|
||||||
|
* every lookup causes memory writes.
|
||||||
|
*
|
||||||
|
* The Balance Theorem bounds the total access time for m operations
|
||||||
|
* and n inserts on an initially empty tree as O((m + n)lg n). The
|
||||||
|
* amortized cost for a sequence of m accesses to a splay tree is O(lg n);
|
||||||
|
*
|
||||||
|
* A red-black tree is a binary search tree with the node color as an
|
||||||
|
* extra attribute. It fulfills a set of conditions:
|
||||||
|
* - every search path from the root to a leaf consists of the
|
||||||
|
* same number of black nodes,
|
||||||
|
* - each red node (except for the root) has a black parent,
|
||||||
|
* - each leaf node is black.
|
||||||
|
*
|
||||||
|
* Every operation on a red-black tree is bounded as O(lg n).
|
||||||
|
* The maximum height of a red-black tree is 2lg (n+1).
|
||||||
|
*/
|
||||||
|
|
||||||
|
#define SPLAY_HEAD(name, type) \
|
||||||
|
struct name { \
|
||||||
|
struct type *sph_root; /* root of the tree */ \
|
||||||
|
}
|
||||||
|
|
||||||
|
#define SPLAY_INITIALIZER(root) \
|
||||||
|
{ NULL }
|
||||||
|
|
||||||
|
#define SPLAY_INIT(root) do { \
|
||||||
|
(root)->sph_root = NULL; \
|
||||||
|
} while (/*CONSTCOND*/ 0)
|
||||||
|
|
||||||
|
#define SPLAY_ENTRY(type) \
|
||||||
|
struct { \
|
||||||
|
struct type *spe_left; /* left element */ \
|
||||||
|
struct type *spe_right; /* right element */ \
|
||||||
|
}
|
||||||
|
|
||||||
|
#define SPLAY_LEFT(elm, field) (elm)->field.spe_left
|
||||||
|
#define SPLAY_RIGHT(elm, field) (elm)->field.spe_right
|
||||||
|
#define SPLAY_ROOT(head) (head)->sph_root
|
||||||
|
#define SPLAY_EMPTY(head) (SPLAY_ROOT(head) == NULL)
|
||||||
|
|
||||||
|
/* SPLAY_ROTATE_{LEFT,RIGHT} expect that tmp hold SPLAY_{RIGHT,LEFT} */
|
||||||
|
#define SPLAY_ROTATE_RIGHT(head, tmp, field) do { \
|
||||||
|
SPLAY_LEFT((head)->sph_root, field) = SPLAY_RIGHT(tmp, field); \
|
||||||
|
SPLAY_RIGHT(tmp, field) = (head)->sph_root; \
|
||||||
|
(head)->sph_root = tmp; \
|
||||||
|
} while (/*CONSTCOND*/ 0)
|
||||||
|
|
||||||
|
#define SPLAY_ROTATE_LEFT(head, tmp, field) do { \
|
||||||
|
SPLAY_RIGHT((head)->sph_root, field) = SPLAY_LEFT(tmp, field); \
|
||||||
|
SPLAY_LEFT(tmp, field) = (head)->sph_root; \
|
||||||
|
(head)->sph_root = tmp; \
|
||||||
|
} while (/*CONSTCOND*/ 0)
|
||||||
|
|
||||||
|
#define SPLAY_LINKLEFT(head, tmp, field) do { \
|
||||||
|
SPLAY_LEFT(tmp, field) = (head)->sph_root; \
|
||||||
|
tmp = (head)->sph_root; \
|
||||||
|
(head)->sph_root = SPLAY_LEFT((head)->sph_root, field); \
|
||||||
|
} while (/*CONSTCOND*/ 0)
|
||||||
|
|
||||||
|
#define SPLAY_LINKRIGHT(head, tmp, field) do { \
|
||||||
|
SPLAY_RIGHT(tmp, field) = (head)->sph_root; \
|
||||||
|
tmp = (head)->sph_root; \
|
||||||
|
(head)->sph_root = SPLAY_RIGHT((head)->sph_root, field); \
|
||||||
|
} while (/*CONSTCOND*/ 0)
|
||||||
|
|
||||||
|
#define SPLAY_ASSEMBLE(head, node, left, right, field) do { \
|
||||||
|
SPLAY_RIGHT(left, field) = SPLAY_LEFT((head)->sph_root, field); \
|
||||||
|
SPLAY_LEFT(right, field) = SPLAY_RIGHT((head)->sph_root, field);\
|
||||||
|
SPLAY_LEFT((head)->sph_root, field) = SPLAY_RIGHT(node, field); \
|
||||||
|
SPLAY_RIGHT((head)->sph_root, field) = SPLAY_LEFT(node, field); \
|
||||||
|
} while (/*CONSTCOND*/ 0)
|
||||||
|
|
||||||
|
/* Generates prototypes and inline functions */
|
||||||
|
|
||||||
|
#define SPLAY_PROTOTYPE(name, type, field, cmp) \
|
||||||
|
void name##_SPLAY(struct name *, struct type *); \
|
||||||
|
void name##_SPLAY_MINMAX(struct name *, int); \
|
||||||
|
struct type *name##_SPLAY_INSERT(struct name *, struct type *); \
|
||||||
|
struct type *name##_SPLAY_REMOVE(struct name *, struct type *); \
|
||||||
|
\
|
||||||
|
/* Finds the node with the same key as elm */ \
|
||||||
|
static __inline struct type * \
|
||||||
|
name##_SPLAY_FIND(struct name *head, struct type *elm) \
|
||||||
|
{ \
|
||||||
|
if (SPLAY_EMPTY(head)) \
|
||||||
|
return(NULL); \
|
||||||
|
name##_SPLAY(head, elm); \
|
||||||
|
if ((cmp)(elm, (head)->sph_root) == 0) \
|
||||||
|
return (head->sph_root); \
|
||||||
|
return (NULL); \
|
||||||
|
} \
|
||||||
|
\
|
||||||
|
static __inline struct type * \
|
||||||
|
name##_SPLAY_NEXT(struct name *head, struct type *elm) \
|
||||||
|
{ \
|
||||||
|
name##_SPLAY(head, elm); \
|
||||||
|
if (SPLAY_RIGHT(elm, field) != NULL) { \
|
||||||
|
elm = SPLAY_RIGHT(elm, field); \
|
||||||
|
while (SPLAY_LEFT(elm, field) != NULL) { \
|
||||||
|
elm = SPLAY_LEFT(elm, field); \
|
||||||
|
} \
|
||||||
|
} else \
|
||||||
|
elm = NULL; \
|
||||||
|
return (elm); \
|
||||||
|
} \
|
||||||
|
\
|
||||||
|
static __inline struct type * \
|
||||||
|
name##_SPLAY_MIN_MAX(struct name *head, int val) \
|
||||||
|
{ \
|
||||||
|
name##_SPLAY_MINMAX(head, val); \
|
||||||
|
return (SPLAY_ROOT(head)); \
|
||||||
|
}
|
||||||
|
|
||||||
|
/* Main splay operation.
|
||||||
|
* Moves node close to the key of elm to top
|
||||||
|
*/
|
||||||
|
#define SPLAY_GENERATE(name, type, field, cmp) \
|
||||||
|
struct type * \
|
||||||
|
name##_SPLAY_INSERT(struct name *head, struct type *elm) \
|
||||||
|
{ \
|
||||||
|
if (SPLAY_EMPTY(head)) { \
|
||||||
|
SPLAY_LEFT(elm, field) = SPLAY_RIGHT(elm, field) = NULL; \
|
||||||
|
} else { \
|
||||||
|
int __comp; \
|
||||||
|
name##_SPLAY(head, elm); \
|
||||||
|
__comp = (cmp)(elm, (head)->sph_root); \
|
||||||
|
if(__comp < 0) { \
|
||||||
|
SPLAY_LEFT(elm, field) = SPLAY_LEFT((head)->sph_root, field);\
|
||||||
|
SPLAY_RIGHT(elm, field) = (head)->sph_root; \
|
||||||
|
SPLAY_LEFT((head)->sph_root, field) = NULL; \
|
||||||
|
} else if (__comp > 0) { \
|
||||||
|
SPLAY_RIGHT(elm, field) = SPLAY_RIGHT((head)->sph_root, field);\
|
||||||
|
SPLAY_LEFT(elm, field) = (head)->sph_root; \
|
||||||
|
SPLAY_RIGHT((head)->sph_root, field) = NULL; \
|
||||||
|
} else \
|
||||||
|
return ((head)->sph_root); \
|
||||||
|
} \
|
||||||
|
(head)->sph_root = (elm); \
|
||||||
|
return (NULL); \
|
||||||
|
} \
|
||||||
|
\
|
||||||
|
struct type * \
|
||||||
|
name##_SPLAY_REMOVE(struct name *head, struct type *elm) \
|
||||||
|
{ \
|
||||||
|
struct type *__tmp; \
|
||||||
|
if (SPLAY_EMPTY(head)) \
|
||||||
|
return (NULL); \
|
||||||
|
name##_SPLAY(head, elm); \
|
||||||
|
if ((cmp)(elm, (head)->sph_root) == 0) { \
|
||||||
|
if (SPLAY_LEFT((head)->sph_root, field) == NULL) { \
|
||||||
|
(head)->sph_root = SPLAY_RIGHT((head)->sph_root, field);\
|
||||||
|
} else { \
|
||||||
|
__tmp = SPLAY_RIGHT((head)->sph_root, field); \
|
||||||
|
(head)->sph_root = SPLAY_LEFT((head)->sph_root, field);\
|
||||||
|
name##_SPLAY(head, elm); \
|
||||||
|
SPLAY_RIGHT((head)->sph_root, field) = __tmp; \
|
||||||
|
} \
|
||||||
|
return (elm); \
|
||||||
|
} \
|
||||||
|
return (NULL); \
|
||||||
|
} \
|
||||||
|
\
|
||||||
|
void \
|
||||||
|
name##_SPLAY(struct name *head, struct type *elm) \
|
||||||
|
{ \
|
||||||
|
struct type __node, *__left, *__right, *__tmp; \
|
||||||
|
int __comp; \
|
||||||
|
\
|
||||||
|
SPLAY_LEFT(&__node, field) = SPLAY_RIGHT(&__node, field) = NULL;\
|
||||||
|
__left = __right = &__node; \
|
||||||
|
\
|
||||||
|
while ((__comp = (cmp)(elm, (head)->sph_root)) != 0) { \
|
||||||
|
if (__comp < 0) { \
|
||||||
|
__tmp = SPLAY_LEFT((head)->sph_root, field); \
|
||||||
|
if (__tmp == NULL) \
|
||||||
|
break; \
|
||||||
|
if ((cmp)(elm, __tmp) < 0){ \
|
||||||
|
SPLAY_ROTATE_RIGHT(head, __tmp, field); \
|
||||||
|
if (SPLAY_LEFT((head)->sph_root, field) == NULL)\
|
||||||
|
break; \
|
||||||
|
} \
|
||||||
|
SPLAY_LINKLEFT(head, __right, field); \
|
||||||
|
} else if (__comp > 0) { \
|
||||||
|
__tmp = SPLAY_RIGHT((head)->sph_root, field); \
|
||||||
|
if (__tmp == NULL) \
|
||||||
|
break; \
|
||||||
|
if ((cmp)(elm, __tmp) > 0){ \
|
||||||
|
SPLAY_ROTATE_LEFT(head, __tmp, field); \
|
||||||
|
if (SPLAY_RIGHT((head)->sph_root, field) == NULL)\
|
||||||
|
break; \
|
||||||
|
} \
|
||||||
|
SPLAY_LINKRIGHT(head, __left, field); \
|
||||||
|
} \
|
||||||
|
} \
|
||||||
|
SPLAY_ASSEMBLE(head, &__node, __left, __right, field); \
|
||||||
|
} \
|
||||||
|
\
|
||||||
|
/* Splay with either the minimum or the maximum element \
|
||||||
|
* Used to find minimum or maximum element in tree. \
|
||||||
|
*/ \
|
||||||
|
void name##_SPLAY_MINMAX(struct name *head, int __comp) \
|
||||||
|
{ \
|
||||||
|
struct type __node, *__left, *__right, *__tmp; \
|
||||||
|
\
|
||||||
|
SPLAY_LEFT(&__node, field) = SPLAY_RIGHT(&__node, field) = NULL;\
|
||||||
|
__left = __right = &__node; \
|
||||||
|
\
|
||||||
|
while (1) { \
|
||||||
|
if (__comp < 0) { \
|
||||||
|
__tmp = SPLAY_LEFT((head)->sph_root, field); \
|
||||||
|
if (__tmp == NULL) \
|
||||||
|
break; \
|
||||||
|
if (__comp < 0){ \
|
||||||
|
SPLAY_ROTATE_RIGHT(head, __tmp, field); \
|
||||||
|
if (SPLAY_LEFT((head)->sph_root, field) == NULL)\
|
||||||
|
break; \
|
||||||
|
} \
|
||||||
|
SPLAY_LINKLEFT(head, __right, field); \
|
||||||
|
} else if (__comp > 0) { \
|
||||||
|
__tmp = SPLAY_RIGHT((head)->sph_root, field); \
|
||||||
|
if (__tmp == NULL) \
|
||||||
|
break; \
|
||||||
|
if (__comp > 0) { \
|
||||||
|
SPLAY_ROTATE_LEFT(head, __tmp, field); \
|
||||||
|
if (SPLAY_RIGHT((head)->sph_root, field) == NULL)\
|
||||||
|
break; \
|
||||||
|
} \
|
||||||
|
SPLAY_LINKRIGHT(head, __left, field); \
|
||||||
|
} \
|
||||||
|
} \
|
||||||
|
SPLAY_ASSEMBLE(head, &__node, __left, __right, field); \
|
||||||
|
}
|
||||||
|
|
||||||
|
#define SPLAY_NEGINF -1
|
||||||
|
#define SPLAY_INF 1
|
||||||
|
|
||||||
|
#define SPLAY_INSERT(name, x, y) name##_SPLAY_INSERT(x, y)
|
||||||
|
#define SPLAY_REMOVE(name, x, y) name##_SPLAY_REMOVE(x, y)
|
||||||
|
#define SPLAY_FIND(name, x, y) name##_SPLAY_FIND(x, y)
|
||||||
|
#define SPLAY_NEXT(name, x, y) name##_SPLAY_NEXT(x, y)
|
||||||
|
#define SPLAY_MIN(name, x) (SPLAY_EMPTY(x) ? NULL \
|
||||||
|
: name##_SPLAY_MIN_MAX(x, SPLAY_NEGINF))
|
||||||
|
#define SPLAY_MAX(name, x) (SPLAY_EMPTY(x) ? NULL \
|
||||||
|
: name##_SPLAY_MIN_MAX(x, SPLAY_INF))
|
||||||
|
|
||||||
|
#define SPLAY_FOREACH(x, name, head) \
|
||||||
|
for ((x) = SPLAY_MIN(name, head); \
|
||||||
|
(x) != NULL; \
|
||||||
|
(x) = SPLAY_NEXT(name, head, x))
|
||||||
|
|
||||||
|
/* Macros that define a red-black tree */
|
||||||
|
#define RB_HEAD(name, type) \
|
||||||
|
struct name { \
|
||||||
|
struct type *rbh_root; /* root of the tree */ \
|
||||||
|
}
|
||||||
|
|
||||||
|
#define RB_INITIALIZER(root) \
|
||||||
|
{ NULL }
|
||||||
|
|
||||||
|
#define RB_INIT(root) do { \
|
||||||
|
(root)->rbh_root = NULL; \
|
||||||
|
} while (/*CONSTCOND*/ 0)
|
||||||
|
|
||||||
|
#define RB_BLACK 0
|
||||||
|
#define RB_RED 1
|
||||||
|
#define RB_ENTRY(type) \
|
||||||
|
struct { \
|
||||||
|
struct type *rbe_left; /* left element */ \
|
||||||
|
struct type *rbe_right; /* right element */ \
|
||||||
|
struct type *rbe_parent; /* parent element */ \
|
||||||
|
int rbe_color; /* node color */ \
|
||||||
|
}
|
||||||
|
|
||||||
|
#define RB_LEFT(elm, field) (elm)->field.rbe_left
|
||||||
|
#define RB_RIGHT(elm, field) (elm)->field.rbe_right
|
||||||
|
#define RB_PARENT(elm, field) (elm)->field.rbe_parent
|
||||||
|
#define RB_COLOR(elm, field) (elm)->field.rbe_color
|
||||||
|
#define RB_ROOT(head) (head)->rbh_root
|
||||||
|
#define RB_EMPTY(head) (RB_ROOT(head) == NULL)
|
||||||
|
|
||||||
|
#define RB_SET(elm, parent, field) do { \
|
||||||
|
RB_PARENT(elm, field) = parent; \
|
||||||
|
RB_LEFT(elm, field) = RB_RIGHT(elm, field) = NULL; \
|
||||||
|
RB_COLOR(elm, field) = RB_RED; \
|
||||||
|
} while (/*CONSTCOND*/ 0)
|
||||||
|
|
||||||
|
#define RB_SET_BLACKRED(black, red, field) do { \
|
||||||
|
RB_COLOR(black, field) = RB_BLACK; \
|
||||||
|
RB_COLOR(red, field) = RB_RED; \
|
||||||
|
} while (/*CONSTCOND*/ 0)
|
||||||
|
|
||||||
|
#ifndef RB_AUGMENT
|
||||||
|
#define RB_AUGMENT(x) do {} while (0)
|
||||||
|
#endif
|
||||||
|
|
||||||
|
#define RB_ROTATE_LEFT(head, elm, tmp, field) do { \
|
||||||
|
(tmp) = RB_RIGHT(elm, field); \
|
||||||
|
if ((RB_RIGHT(elm, field) = RB_LEFT(tmp, field)) != NULL) { \
|
||||||
|
RB_PARENT(RB_LEFT(tmp, field), field) = (elm); \
|
||||||
|
} \
|
||||||
|
RB_AUGMENT(elm); \
|
||||||
|
if ((RB_PARENT(tmp, field) = RB_PARENT(elm, field)) != NULL) { \
|
||||||
|
if ((elm) == RB_LEFT(RB_PARENT(elm, field), field)) \
|
||||||
|
RB_LEFT(RB_PARENT(elm, field), field) = (tmp); \
|
||||||
|
else \
|
||||||
|
RB_RIGHT(RB_PARENT(elm, field), field) = (tmp); \
|
||||||
|
} else \
|
||||||
|
(head)->rbh_root = (tmp); \
|
||||||
|
RB_LEFT(tmp, field) = (elm); \
|
||||||
|
RB_PARENT(elm, field) = (tmp); \
|
||||||
|
RB_AUGMENT(tmp); \
|
||||||
|
if ((RB_PARENT(tmp, field))) \
|
||||||
|
RB_AUGMENT(RB_PARENT(tmp, field)); \
|
||||||
|
} while (/*CONSTCOND*/ 0)
|
||||||
|
|
||||||
|
#define RB_ROTATE_RIGHT(head, elm, tmp, field) do { \
|
||||||
|
(tmp) = RB_LEFT(elm, field); \
|
||||||
|
if ((RB_LEFT(elm, field) = RB_RIGHT(tmp, field)) != NULL) { \
|
||||||
|
RB_PARENT(RB_RIGHT(tmp, field), field) = (elm); \
|
||||||
|
} \
|
||||||
|
RB_AUGMENT(elm); \
|
||||||
|
if ((RB_PARENT(tmp, field) = RB_PARENT(elm, field)) != NULL) { \
|
||||||
|
if ((elm) == RB_LEFT(RB_PARENT(elm, field), field)) \
|
||||||
|
RB_LEFT(RB_PARENT(elm, field), field) = (tmp); \
|
||||||
|
else \
|
||||||
|
RB_RIGHT(RB_PARENT(elm, field), field) = (tmp); \
|
||||||
|
} else \
|
||||||
|
(head)->rbh_root = (tmp); \
|
||||||
|
RB_RIGHT(tmp, field) = (elm); \
|
||||||
|
RB_PARENT(elm, field) = (tmp); \
|
||||||
|
RB_AUGMENT(tmp); \
|
||||||
|
if ((RB_PARENT(tmp, field))) \
|
||||||
|
RB_AUGMENT(RB_PARENT(tmp, field)); \
|
||||||
|
} while (/*CONSTCOND*/ 0)
|
||||||
|
|
||||||
|
/* Generates prototypes and inline functions */
|
||||||
|
#define RB_PROTOTYPE(name, type, field, cmp) \
|
||||||
|
RB_PROTOTYPE_INTERNAL(name, type, field, cmp,)
|
||||||
|
#define RB_PROTOTYPE_STATIC(name, type, field, cmp) \
|
||||||
|
RB_PROTOTYPE_INTERNAL(name, type, field, cmp, __unused static)
|
||||||
|
#define RB_PROTOTYPE_INTERNAL(name, type, field, cmp, attr) \
|
||||||
|
attr void name##_RB_INSERT_COLOR(struct name *, struct type *); \
|
||||||
|
attr void name##_RB_REMOVE_COLOR(struct name *, struct type *, struct type *);\
|
||||||
|
attr struct type *name##_RB_REMOVE(struct name *, struct type *); \
|
||||||
|
attr struct type *name##_RB_INSERT(struct name *, struct type *); \
|
||||||
|
attr struct type *name##_RB_FIND(struct name *, struct type *); \
|
||||||
|
attr struct type *name##_RB_NFIND(struct name *, struct type *); \
|
||||||
|
attr struct type *name##_RB_NEXT(struct type *); \
|
||||||
|
attr struct type *name##_RB_PREV(struct type *); \
|
||||||
|
attr struct type *name##_RB_MINMAX(struct name *, int); \
|
||||||
|
\
|
||||||
|
|
||||||
|
/* Main rb operation.
|
||||||
|
* Moves node close to the key of elm to top
|
||||||
|
*/
|
||||||
|
#define RB_GENERATE(name, type, field, cmp) \
|
||||||
|
RB_GENERATE_INTERNAL(name, type, field, cmp,)
|
||||||
|
#define RB_GENERATE_STATIC(name, type, field, cmp) \
|
||||||
|
RB_GENERATE_INTERNAL(name, type, field, cmp, __unused static)
|
||||||
|
#define RB_GENERATE_INTERNAL(name, type, field, cmp, attr) \
|
||||||
|
attr void \
|
||||||
|
name##_RB_INSERT_COLOR(struct name *head, struct type *elm) \
|
||||||
|
{ \
|
||||||
|
struct type *parent, *gparent, *tmp; \
|
||||||
|
while ((parent = RB_PARENT(elm, field)) != NULL && \
|
||||||
|
RB_COLOR(parent, field) == RB_RED) { \
|
||||||
|
gparent = RB_PARENT(parent, field); \
|
||||||
|
if (parent == RB_LEFT(gparent, field)) { \
|
||||||
|
tmp = RB_RIGHT(gparent, field); \
|
||||||
|
if (tmp && RB_COLOR(tmp, field) == RB_RED) { \
|
||||||
|
RB_COLOR(tmp, field) = RB_BLACK; \
|
||||||
|
RB_SET_BLACKRED(parent, gparent, field);\
|
||||||
|
elm = gparent; \
|
||||||
|
continue; \
|
||||||
|
} \
|
||||||
|
if (RB_RIGHT(parent, field) == elm) { \
|
||||||
|
RB_ROTATE_LEFT(head, parent, tmp, field);\
|
||||||
|
tmp = parent; \
|
||||||
|
parent = elm; \
|
||||||
|
elm = tmp; \
|
||||||
|
} \
|
||||||
|
RB_SET_BLACKRED(parent, gparent, field); \
|
||||||
|
RB_ROTATE_RIGHT(head, gparent, tmp, field); \
|
||||||
|
} else { \
|
||||||
|
tmp = RB_LEFT(gparent, field); \
|
||||||
|
if (tmp && RB_COLOR(tmp, field) == RB_RED) { \
|
||||||
|
RB_COLOR(tmp, field) = RB_BLACK; \
|
||||||
|
RB_SET_BLACKRED(parent, gparent, field);\
|
||||||
|
elm = gparent; \
|
||||||
|
continue; \
|
||||||
|
} \
|
||||||
|
if (RB_LEFT(parent, field) == elm) { \
|
||||||
|
RB_ROTATE_RIGHT(head, parent, tmp, field);\
|
||||||
|
tmp = parent; \
|
||||||
|
parent = elm; \
|
||||||
|
elm = tmp; \
|
||||||
|
} \
|
||||||
|
RB_SET_BLACKRED(parent, gparent, field); \
|
||||||
|
RB_ROTATE_LEFT(head, gparent, tmp, field); \
|
||||||
|
} \
|
||||||
|
} \
|
||||||
|
RB_COLOR(head->rbh_root, field) = RB_BLACK; \
|
||||||
|
} \
|
||||||
|
\
|
||||||
|
attr void \
|
||||||
|
name##_RB_REMOVE_COLOR(struct name *head, struct type *parent, struct type *elm) \
|
||||||
|
{ \
|
||||||
|
struct type *tmp; \
|
||||||
|
while ((elm == NULL || RB_COLOR(elm, field) == RB_BLACK) && \
|
||||||
|
elm != RB_ROOT(head)) { \
|
||||||
|
if (RB_LEFT(parent, field) == elm) { \
|
||||||
|
tmp = RB_RIGHT(parent, field); \
|
||||||
|
if (RB_COLOR(tmp, field) == RB_RED) { \
|
||||||
|
RB_SET_BLACKRED(tmp, parent, field); \
|
||||||
|
RB_ROTATE_LEFT(head, parent, tmp, field);\
|
||||||
|
tmp = RB_RIGHT(parent, field); \
|
||||||
|
} \
|
||||||
|
if ((RB_LEFT(tmp, field) == NULL || \
|
||||||
|
RB_COLOR(RB_LEFT(tmp, field), field) == RB_BLACK) &&\
|
||||||
|
(RB_RIGHT(tmp, field) == NULL || \
|
||||||
|
RB_COLOR(RB_RIGHT(tmp, field), field) == RB_BLACK)) {\
|
||||||
|
RB_COLOR(tmp, field) = RB_RED; \
|
||||||
|
elm = parent; \
|
||||||
|
parent = RB_PARENT(elm, field); \
|
||||||
|
} else { \
|
||||||
|
if (RB_RIGHT(tmp, field) == NULL || \
|
||||||
|
RB_COLOR(RB_RIGHT(tmp, field), field) == RB_BLACK) {\
|
||||||
|
struct type *oleft; \
|
||||||
|
if ((oleft = RB_LEFT(tmp, field)) \
|
||||||
|
!= NULL) \
|
||||||
|
RB_COLOR(oleft, field) = RB_BLACK;\
|
||||||
|
RB_COLOR(tmp, field) = RB_RED; \
|
||||||
|
RB_ROTATE_RIGHT(head, tmp, oleft, field);\
|
||||||
|
tmp = RB_RIGHT(parent, field); \
|
||||||
|
} \
|
||||||
|
RB_COLOR(tmp, field) = RB_COLOR(parent, field);\
|
||||||
|
RB_COLOR(parent, field) = RB_BLACK; \
|
||||||
|
if (RB_RIGHT(tmp, field)) \
|
||||||
|
RB_COLOR(RB_RIGHT(tmp, field), field) = RB_BLACK;\
|
||||||
|
RB_ROTATE_LEFT(head, parent, tmp, field);\
|
||||||
|
elm = RB_ROOT(head); \
|
||||||
|
break; \
|
||||||
|
} \
|
||||||
|
} else { \
|
||||||
|
tmp = RB_LEFT(parent, field); \
|
||||||
|
if (RB_COLOR(tmp, field) == RB_RED) { \
|
||||||
|
RB_SET_BLACKRED(tmp, parent, field); \
|
||||||
|
RB_ROTATE_RIGHT(head, parent, tmp, field);\
|
||||||
|
tmp = RB_LEFT(parent, field); \
|
||||||
|
} \
|
||||||
|
if ((RB_LEFT(tmp, field) == NULL || \
|
||||||
|
RB_COLOR(RB_LEFT(tmp, field), field) == RB_BLACK) &&\
|
||||||
|
(RB_RIGHT(tmp, field) == NULL || \
|
||||||
|
RB_COLOR(RB_RIGHT(tmp, field), field) == RB_BLACK)) {\
|
||||||
|
RB_COLOR(tmp, field) = RB_RED; \
|
||||||
|
elm = parent; \
|
||||||
|
parent = RB_PARENT(elm, field); \
|
||||||
|
} else { \
|
||||||
|
if (RB_LEFT(tmp, field) == NULL || \
|
||||||
|
RB_COLOR(RB_LEFT(tmp, field), field) == RB_BLACK) {\
|
||||||
|
struct type *oright; \
|
||||||
|
if ((oright = RB_RIGHT(tmp, field)) \
|
||||||
|
!= NULL) \
|
||||||
|
RB_COLOR(oright, field) = RB_BLACK;\
|
||||||
|
RB_COLOR(tmp, field) = RB_RED; \
|
||||||
|
RB_ROTATE_LEFT(head, tmp, oright, field);\
|
||||||
|
tmp = RB_LEFT(parent, field); \
|
||||||
|
} \
|
||||||
|
RB_COLOR(tmp, field) = RB_COLOR(parent, field);\
|
||||||
|
RB_COLOR(parent, field) = RB_BLACK; \
|
||||||
|
if (RB_LEFT(tmp, field)) \
|
||||||
|
RB_COLOR(RB_LEFT(tmp, field), field) = RB_BLACK;\
|
||||||
|
RB_ROTATE_RIGHT(head, parent, tmp, field);\
|
||||||
|
elm = RB_ROOT(head); \
|
||||||
|
break; \
|
||||||
|
} \
|
||||||
|
} \
|
||||||
|
} \
|
||||||
|
if (elm) \
|
||||||
|
RB_COLOR(elm, field) = RB_BLACK; \
|
||||||
|
} \
|
||||||
|
\
|
||||||
|
attr struct type * \
|
||||||
|
name##_RB_REMOVE(struct name *head, struct type *elm) \
|
||||||
|
{ \
|
||||||
|
struct type *child, *parent, *old = elm; \
|
||||||
|
int color; \
|
||||||
|
if (RB_LEFT(elm, field) == NULL) \
|
||||||
|
child = RB_RIGHT(elm, field); \
|
||||||
|
else if (RB_RIGHT(elm, field) == NULL) \
|
||||||
|
child = RB_LEFT(elm, field); \
|
||||||
|
else { \
|
||||||
|
struct type *left; \
|
||||||
|
elm = RB_RIGHT(elm, field); \
|
||||||
|
while ((left = RB_LEFT(elm, field)) != NULL) \
|
||||||
|
elm = left; \
|
||||||
|
child = RB_RIGHT(elm, field); \
|
||||||
|
parent = RB_PARENT(elm, field); \
|
||||||
|
color = RB_COLOR(elm, field); \
|
||||||
|
if (child) \
|
||||||
|
RB_PARENT(child, field) = parent; \
|
||||||
|
if (parent) { \
|
||||||
|
if (RB_LEFT(parent, field) == elm) \
|
||||||
|
RB_LEFT(parent, field) = child; \
|
||||||
|
else \
|
||||||
|
RB_RIGHT(parent, field) = child; \
|
||||||
|
RB_AUGMENT(parent); \
|
||||||
|
} else \
|
||||||
|
RB_ROOT(head) = child; \
|
||||||
|
if (RB_PARENT(elm, field) == old) \
|
||||||
|
parent = elm; \
|
||||||
|
(elm)->field = (old)->field; \
|
||||||
|
if (RB_PARENT(old, field)) { \
|
||||||
|
if (RB_LEFT(RB_PARENT(old, field), field) == old)\
|
||||||
|
RB_LEFT(RB_PARENT(old, field), field) = elm;\
|
||||||
|
else \
|
||||||
|
RB_RIGHT(RB_PARENT(old, field), field) = elm;\
|
||||||
|
RB_AUGMENT(RB_PARENT(old, field)); \
|
||||||
|
} else \
|
||||||
|
RB_ROOT(head) = elm; \
|
||||||
|
RB_PARENT(RB_LEFT(old, field), field) = elm; \
|
||||||
|
if (RB_RIGHT(old, field)) \
|
||||||
|
RB_PARENT(RB_RIGHT(old, field), field) = elm; \
|
||||||
|
if (parent) { \
|
||||||
|
left = parent; \
|
||||||
|
do { \
|
||||||
|
RB_AUGMENT(left); \
|
||||||
|
} while ((left = RB_PARENT(left, field)) != NULL); \
|
||||||
|
} \
|
||||||
|
goto color; \
|
||||||
|
} \
|
||||||
|
parent = RB_PARENT(elm, field); \
|
||||||
|
color = RB_COLOR(elm, field); \
|
||||||
|
if (child) \
|
||||||
|
RB_PARENT(child, field) = parent; \
|
||||||
|
if (parent) { \
|
||||||
|
if (RB_LEFT(parent, field) == elm) \
|
||||||
|
RB_LEFT(parent, field) = child; \
|
||||||
|
else \
|
||||||
|
RB_RIGHT(parent, field) = child; \
|
||||||
|
RB_AUGMENT(parent); \
|
||||||
|
} else \
|
||||||
|
RB_ROOT(head) = child; \
|
||||||
|
color: \
|
||||||
|
if (color == RB_BLACK) \
|
||||||
|
name##_RB_REMOVE_COLOR(head, parent, child); \
|
||||||
|
return (old); \
|
||||||
|
} \
|
||||||
|
\
|
||||||
|
/* Inserts a node into the RB tree */ \
|
||||||
|
attr struct type * \
|
||||||
|
name##_RB_INSERT(struct name *head, struct type *elm) \
|
||||||
|
{ \
|
||||||
|
struct type *tmp; \
|
||||||
|
struct type *parent = NULL; \
|
||||||
|
int comp = 0; \
|
||||||
|
tmp = RB_ROOT(head); \
|
||||||
|
while (tmp) { \
|
||||||
|
parent = tmp; \
|
||||||
|
comp = (cmp)(elm, parent); \
|
||||||
|
if (comp < 0) \
|
||||||
|
tmp = RB_LEFT(tmp, field); \
|
||||||
|
else if (comp > 0) \
|
||||||
|
tmp = RB_RIGHT(tmp, field); \
|
||||||
|
else \
|
||||||
|
return (tmp); \
|
||||||
|
} \
|
||||||
|
RB_SET(elm, parent, field); \
|
||||||
|
if (parent != NULL) { \
|
||||||
|
if (comp < 0) \
|
||||||
|
RB_LEFT(parent, field) = elm; \
|
||||||
|
else \
|
||||||
|
RB_RIGHT(parent, field) = elm; \
|
||||||
|
RB_AUGMENT(parent); \
|
||||||
|
} else \
|
||||||
|
RB_ROOT(head) = elm; \
|
||||||
|
name##_RB_INSERT_COLOR(head, elm); \
|
||||||
|
return (NULL); \
|
||||||
|
} \
|
||||||
|
\
|
||||||
|
/* Finds the node with the same key as elm */ \
|
||||||
|
attr struct type * \
|
||||||
|
name##_RB_FIND(struct name *head, struct type *elm) \
|
||||||
|
{ \
|
||||||
|
struct type *tmp = RB_ROOT(head); \
|
||||||
|
int comp; \
|
||||||
|
while (tmp) { \
|
||||||
|
comp = cmp(elm, tmp); \
|
||||||
|
if (comp < 0) \
|
||||||
|
tmp = RB_LEFT(tmp, field); \
|
||||||
|
else if (comp > 0) \
|
||||||
|
tmp = RB_RIGHT(tmp, field); \
|
||||||
|
else \
|
||||||
|
return (tmp); \
|
||||||
|
} \
|
||||||
|
return (NULL); \
|
||||||
|
} \
|
||||||
|
\
|
||||||
|
/* Finds the first node greater than or equal to the search key */ \
|
||||||
|
attr struct type * \
|
||||||
|
name##_RB_NFIND(struct name *head, struct type *elm) \
|
||||||
|
{ \
|
||||||
|
struct type *tmp = RB_ROOT(head); \
|
||||||
|
struct type *res = NULL; \
|
||||||
|
int comp; \
|
||||||
|
while (tmp) { \
|
||||||
|
comp = cmp(elm, tmp); \
|
||||||
|
if (comp < 0) { \
|
||||||
|
res = tmp; \
|
||||||
|
tmp = RB_LEFT(tmp, field); \
|
||||||
|
} \
|
||||||
|
else if (comp > 0) \
|
||||||
|
tmp = RB_RIGHT(tmp, field); \
|
||||||
|
else \
|
||||||
|
return (tmp); \
|
||||||
|
} \
|
||||||
|
return (res); \
|
||||||
|
} \
|
||||||
|
\
|
||||||
|
/* ARGSUSED */ \
|
||||||
|
attr struct type * \
|
||||||
|
name##_RB_NEXT(struct type *elm) \
|
||||||
|
{ \
|
||||||
|
if (RB_RIGHT(elm, field)) { \
|
||||||
|
elm = RB_RIGHT(elm, field); \
|
||||||
|
while (RB_LEFT(elm, field)) \
|
||||||
|
elm = RB_LEFT(elm, field); \
|
||||||
|
} else { \
|
||||||
|
if (RB_PARENT(elm, field) && \
|
||||||
|
(elm == RB_LEFT(RB_PARENT(elm, field), field))) \
|
||||||
|
elm = RB_PARENT(elm, field); \
|
||||||
|
else { \
|
||||||
|
while (RB_PARENT(elm, field) && \
|
||||||
|
(elm == RB_RIGHT(RB_PARENT(elm, field), field)))\
|
||||||
|
elm = RB_PARENT(elm, field); \
|
||||||
|
elm = RB_PARENT(elm, field); \
|
||||||
|
} \
|
||||||
|
} \
|
||||||
|
return (elm); \
|
||||||
|
} \
|
||||||
|
\
|
||||||
|
/* ARGSUSED */ \
|
||||||
|
attr struct type * \
|
||||||
|
name##_RB_PREV(struct type *elm) \
|
||||||
|
{ \
|
||||||
|
if (RB_LEFT(elm, field)) { \
|
||||||
|
elm = RB_LEFT(elm, field); \
|
||||||
|
while (RB_RIGHT(elm, field)) \
|
||||||
|
elm = RB_RIGHT(elm, field); \
|
||||||
|
} else { \
|
||||||
|
if (RB_PARENT(elm, field) && \
|
||||||
|
(elm == RB_RIGHT(RB_PARENT(elm, field), field))) \
|
||||||
|
elm = RB_PARENT(elm, field); \
|
||||||
|
else { \
|
||||||
|
while (RB_PARENT(elm, field) && \
|
||||||
|
(elm == RB_LEFT(RB_PARENT(elm, field), field)))\
|
||||||
|
elm = RB_PARENT(elm, field); \
|
||||||
|
elm = RB_PARENT(elm, field); \
|
||||||
|
} \
|
||||||
|
} \
|
||||||
|
return (elm); \
|
||||||
|
} \
|
||||||
|
\
|
||||||
|
attr struct type * \
|
||||||
|
name##_RB_MINMAX(struct name *head, int val) \
|
||||||
|
{ \
|
||||||
|
struct type *tmp = RB_ROOT(head); \
|
||||||
|
struct type *parent = NULL; \
|
||||||
|
while (tmp) { \
|
||||||
|
parent = tmp; \
|
||||||
|
if (val < 0) \
|
||||||
|
tmp = RB_LEFT(tmp, field); \
|
||||||
|
else \
|
||||||
|
tmp = RB_RIGHT(tmp, field); \
|
||||||
|
} \
|
||||||
|
return (parent); \
|
||||||
|
}
|
||||||
|
|
||||||
|
#define RB_NEGINF -1
|
||||||
|
#define RB_INF 1
|
||||||
|
|
||||||
|
#define RB_INSERT(name, x, y) name##_RB_INSERT(x, y)
|
||||||
|
#define RB_REMOVE(name, x, y) name##_RB_REMOVE(x, y)
|
||||||
|
#define RB_FIND(name, x, y) name##_RB_FIND(x, y)
|
||||||
|
#define RB_NFIND(name, x, y) name##_RB_NFIND(x, y)
|
||||||
|
#define RB_NEXT(name, x, y) name##_RB_NEXT(y)
|
||||||
|
#define RB_PREV(name, x, y) name##_RB_PREV(y)
|
||||||
|
#define RB_MIN(name, x) name##_RB_MINMAX(x, RB_NEGINF)
|
||||||
|
#define RB_MAX(name, x) name##_RB_MINMAX(x, RB_INF)
|
||||||
|
|
||||||
|
#define RB_FOREACH(x, name, head) \
|
||||||
|
for ((x) = RB_MIN(name, head); \
|
||||||
|
(x) != NULL; \
|
||||||
|
(x) = name##_RB_NEXT(x))
|
||||||
|
|
||||||
|
#define RB_FOREACH_FROM(x, name, y) \
|
||||||
|
for ((x) = (y); \
|
||||||
|
((x) != NULL) && ((y) = name##_RB_NEXT(x), (x) != NULL); \
|
||||||
|
(x) = (y))
|
||||||
|
|
||||||
|
#define RB_FOREACH_SAFE(x, name, head, y) \
|
||||||
|
for ((x) = RB_MIN(name, head); \
|
||||||
|
((x) != NULL) && ((y) = name##_RB_NEXT(x), (x) != NULL); \
|
||||||
|
(x) = (y))
|
||||||
|
|
||||||
|
#define RB_FOREACH_REVERSE(x, name, head) \
|
||||||
|
for ((x) = RB_MAX(name, head); \
|
||||||
|
(x) != NULL; \
|
||||||
|
(x) = name##_RB_PREV(x))
|
||||||
|
|
||||||
|
#define RB_FOREACH_REVERSE_FROM(x, name, y) \
|
||||||
|
for ((x) = (y); \
|
||||||
|
((x) != NULL) && ((y) = name##_RB_PREV(x), (x) != NULL); \
|
||||||
|
(x) = (y))
|
||||||
|
|
||||||
|
#define RB_FOREACH_REVERSE_SAFE(x, name, head, y) \
|
||||||
|
for ((x) = RB_MAX(name, head); \
|
||||||
|
((x) != NULL) && ((y) = name##_RB_PREV(x), (x) != NULL); \
|
||||||
|
(x) = (y))
|
||||||
|
|
||||||
|
#endif /* _SYS_TREE_H_ */
|
62
include/bsd/unistd.h
Normal file
62
include/bsd/unistd.h
Normal file
@@ -0,0 +1,62 @@
|
|||||||
|
/*
|
||||||
|
* Copyright © 2006 Robert Millan
|
||||||
|
* Copyright © 2008-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.
|
||||||
|
*/
|
||||||
|
|
||||||
|
#ifndef LIBBSD_UNISTD_H
|
||||||
|
#define LIBBSD_UNISTD_H
|
||||||
|
|
||||||
|
#include <sys/cdefs.h>
|
||||||
|
#include <sys/stat.h>
|
||||||
|
|
||||||
|
#ifdef LIBBSD_OVERLAY
|
||||||
|
#include_next <unistd.h>
|
||||||
|
#else
|
||||||
|
#include <unistd.h>
|
||||||
|
#endif
|
||||||
|
|
||||||
|
#ifndef S_ISTXT
|
||||||
|
#define S_ISTXT S_ISVTX
|
||||||
|
#endif
|
||||||
|
|
||||||
|
__BEGIN_DECLS
|
||||||
|
extern int optreset;
|
||||||
|
|
||||||
|
#ifdef LIBBSD_OVERLAY
|
||||||
|
#undef getopt
|
||||||
|
#define getopt(argc, argv, optstr) bsd_getopt(argc, argv, optstr)
|
||||||
|
#endif
|
||||||
|
|
||||||
|
int bsd_getopt(int, char **, char *);
|
||||||
|
|
||||||
|
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
|
88
include/bsd/vis.h
Normal file
88
include/bsd/vis.h
Normal file
@@ -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_ */
|
@@ -1,60 +1,38 @@
|
|||||||
/*
|
/*
|
||||||
* Copyright (c) 1996 Peter Wemm <peter@FreeBSD.org>.
|
* Copyright © 2011 Guillem Jover
|
||||||
* 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
|
* 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:
|
* are met:
|
||||||
* 1. Redistributions of source code must retain the above copyright
|
* 1. Redistributions of source code must retain the above copyright
|
||||||
* notice, this list of conditions and the following disclaimer.
|
* notice, this list of conditions and the following disclaimer.
|
||||||
* 2. Redistributions in binary form must reproduce the above copyright
|
* 2. Redistributions in binary form must reproduce the above copyright
|
||||||
* notice, this list of conditions and the following disclaimer in the
|
* notice, this list of conditions and the following disclaimer in the
|
||||||
* documentation and/or other materials provided with the distribution.
|
* documentation and/or other materials provided with the distribution.
|
||||||
* 3. The name of the author may not be used to endorse or promote
|
* 3. The name of the author may not be used to endorse or promote products
|
||||||
* products derived from this software without specific prior written
|
* derived from this software without specific prior written permission.
|
||||||
* permission.
|
|
||||||
*
|
*
|
||||||
* THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND
|
* THIS SOFTWARE IS PROVIDED ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES,
|
||||||
* ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
|
* INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY
|
||||||
* IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
|
* AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL
|
||||||
* ARE DISCLAIMED. IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE
|
* THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL,
|
||||||
* FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
|
* EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO,
|
||||||
* DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
|
* PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS;
|
||||||
* OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
|
* OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY,
|
||||||
* HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
|
* WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR
|
||||||
* LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
|
* OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF
|
||||||
* OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
|
* ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
|
||||||
* SUCH DAMAGE.
|
|
||||||
*
|
|
||||||
* $FreeBSD: src/lib/libutil/libutil.h,v 1.47 2008/04/23 00:49:12 scf Exp $
|
|
||||||
*/
|
*/
|
||||||
|
|
||||||
#ifndef _LIBUTIL_H_
|
#ifndef LIBBSD_LIBUTIL_H
|
||||||
#define _LIBUTIL_H_
|
#define LIBBSD_LIBUTIL_H
|
||||||
|
|
||||||
#include <features.h>
|
#ifdef LIBBSD_DISABLE_DEPRECATED
|
||||||
#include <sys/types.h>
|
#error "Deprecated header, use <bsd/libutil.h> or libbsd-overlay.pc instead."
|
||||||
|
#else
|
||||||
|
#warning "Deprecated header, use <bsd/libutil.h> or libbsd-overlay.pc instead."
|
||||||
|
#endif
|
||||||
|
|
||||||
|
#include <bsd/libutil.h>
|
||||||
|
|
||||||
__BEGIN_DECLS
|
#endif
|
||||||
int humanize_number(char *buf, size_t len, int64_t bytes,
|
|
||||||
const char *suffix, int scale, int flags);
|
|
||||||
__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_ */
|
|
||||||
|
@@ -1,5 +1,5 @@
|
|||||||
/*
|
/*
|
||||||
* Copyright © 2009 Guillem Jover
|
* Copyright © 2011 Guillem Jover
|
||||||
*
|
*
|
||||||
* Redistribution and use in source and binary forms, with or without
|
* Redistribution and use in source and binary forms, with or without
|
||||||
* modification, are permitted provided that the following conditions
|
* modification, are permitted provided that the following conditions
|
||||||
@@ -24,15 +24,15 @@
|
|||||||
* ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
|
* ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
|
||||||
*/
|
*/
|
||||||
|
|
||||||
#ifndef LIBBSD_NLIST_H
|
#ifndef LIBBSD_DEPRECATED_NLIST_H
|
||||||
#define LIBBSD_NLIST_H
|
#define LIBBSD_DEPRECATED_NLIST_H
|
||||||
|
|
||||||
#include <sys/cdefs.h>
|
|
||||||
#include <a.out.h>
|
|
||||||
|
|
||||||
__BEGIN_DECLS
|
|
||||||
extern int nlist(const char *filename, struct nlist *list);
|
|
||||||
__END_DECLS
|
|
||||||
|
|
||||||
|
#ifdef LIBBSD_DISABLE_DEPRECATED
|
||||||
|
#error "Deprecated header, use <bsd/nlist.h> or libbsd-overlay.pc instead."
|
||||||
|
#else
|
||||||
|
#warning "Deprecated header, use <bsd/nlist.h> or libbsd-overlay.pc instead."
|
||||||
#endif
|
#endif
|
||||||
|
|
||||||
|
#include <bsd/nlist.h>
|
||||||
|
|
||||||
|
#endif
|
||||||
|
@@ -1,6 +1,5 @@
|
|||||||
/*-
|
/*
|
||||||
* Copyright (c) 1990, 1993
|
* Copyright © 2011 Guillem Jover
|
||||||
* The Regents of the University of California. All rights reserved.
|
|
||||||
*
|
*
|
||||||
* Redistribution and use in source and binary forms, with or without
|
* Redistribution and use in source and binary forms, with or without
|
||||||
* modification, are permitted provided that the following conditions
|
* modification, are permitted provided that the following conditions
|
||||||
@@ -10,77 +9,30 @@
|
|||||||
* 2. Redistributions in binary form must reproduce the above copyright
|
* 2. Redistributions in binary form must reproduce the above copyright
|
||||||
* notice, this list of conditions and the following disclaimer in the
|
* notice, this list of conditions and the following disclaimer in the
|
||||||
* documentation and/or other materials provided with the distribution.
|
* documentation and/or other materials provided with the distribution.
|
||||||
* 3. Neither the name of the University nor the names of its contributors
|
* 3. The name of the author may not be used to endorse or promote products
|
||||||
* may be used to endorse or promote products derived from this software
|
* derived from this software without specific prior written permission.
|
||||||
* without specific prior written permission.
|
|
||||||
*
|
*
|
||||||
* THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
|
* THIS SOFTWARE IS PROVIDED ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES,
|
||||||
* ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
|
* INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY
|
||||||
* IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
|
* AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL
|
||||||
* ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
|
* THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL,
|
||||||
* FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
|
* EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO,
|
||||||
* DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
|
* PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS;
|
||||||
* OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
|
* OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY,
|
||||||
* HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
|
* WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR
|
||||||
* LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
|
* OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF
|
||||||
* OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
|
* ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
|
||||||
* 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_
|
#ifndef LIBBSD_VIS_H
|
||||||
#define _VIS_H_
|
#define LIBBSD_VIS_H
|
||||||
|
|
||||||
#include <sys/types.h>
|
#ifdef LIBBSD_DISABLE_DEPRECATED
|
||||||
|
#error "Deprecated header, use <bsd/vis.h> or libbsd-overlay.pc instead."
|
||||||
|
#else
|
||||||
|
#warning "Deprecated header, use <bsd/vis.h> or libbsd-overlay.pc instead."
|
||||||
|
#endif
|
||||||
|
|
||||||
/*
|
#include <bsd/vis.h>
|
||||||
* to select alternate encoding format
|
|
||||||
*/
|
|
||||||
#define VIS_OCTAL 0x01 /* use octal \ddd format */
|
|
||||||
#define VIS_CSTYLE 0x02 /* use \[nrft0..] where appropriate */
|
|
||||||
|
|
||||||
/*
|
#endif
|
||||||
* 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 strunvis(char *, const char *);
|
|
||||||
int strunvisx(char *, const char *, int);
|
|
||||||
int unvis(char *, int, int *, int);
|
|
||||||
__END_DECLS
|
|
||||||
|
|
||||||
#endif /* !_VIS_H_ */
|
|
||||||
|
11
libbsd-overlay.pc.in
Normal file
11
libbsd-overlay.pc.in
Normal file
@@ -0,0 +1,11 @@
|
|||||||
|
prefix=@prefix@
|
||||||
|
exec_prefix=@exec_prefix@
|
||||||
|
libdir=@libdir@
|
||||||
|
includedir=@includedir@
|
||||||
|
|
||||||
|
Name: libbsd
|
||||||
|
Description: Utility functions from BSD systems (overlay)
|
||||||
|
Version: @VERSION@
|
||||||
|
URL: http://libbsd.freedesktop.org/
|
||||||
|
Libs: -L${libdir} -lbsd
|
||||||
|
Cflags: -isystem ${includedir}/bsd -DLIBBSD_OVERLAY
|
0
man/.gitignore → src/.gitignore
vendored
0
man/.gitignore → src/.gitignore
vendored
@@ -28,23 +28,30 @@
|
|||||||
.\" THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
|
.\" THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
|
||||||
.\"
|
.\"
|
||||||
.\" Manual page, using -mandoc macros
|
.\" Manual page, using -mandoc macros
|
||||||
.\" $FreeBSD: /repoman/r/ncvs/src/lib/libc/gen/arc4random.3,v 1.16 2003/07/31 06:18:24 das Exp $
|
.\" $FreeBSD$
|
||||||
.\"
|
.\"
|
||||||
.Dd April 15, 1997
|
.Dd April 15, 1997
|
||||||
.Dt ARC4RANDOM 3
|
.Dt ARC4RANDOM 3
|
||||||
.Os
|
.Os
|
||||||
.Sh NAME
|
.Sh NAME
|
||||||
.Nm arc4random ,
|
.Nm arc4random ,
|
||||||
|
.Nm arc4random_buf ,
|
||||||
|
.Nm arc4random_uniform ,
|
||||||
.Nm arc4random_stir ,
|
.Nm arc4random_stir ,
|
||||||
.Nm arc4random_addrandom
|
.Nm arc4random_addrandom
|
||||||
.Nd arc4 random number generator
|
.Nd arc4 random number generator
|
||||||
.Sh LIBRARY
|
.Sh LIBRARY
|
||||||
.Lb libc
|
.ds str-Lb-libbsd Utility functions from BSD systems (libbsd, \-lbsd)
|
||||||
|
.Lb libbsd
|
||||||
.Sh SYNOPSIS
|
.Sh SYNOPSIS
|
||||||
.In stdlib.h
|
.In bsd/stdlib.h
|
||||||
.Ft u_int32_t
|
.Ft u_int32_t
|
||||||
.Fn arc4random "void"
|
.Fn arc4random "void"
|
||||||
.Ft void
|
.Ft void
|
||||||
|
.Fn arc4random_buf "void *buf" "size_t nbytes"
|
||||||
|
.Ft u_int32_t
|
||||||
|
.Fn arc4random_uniform "u_int32_t upper_bound"
|
||||||
|
.Ft void
|
||||||
.Fn arc4random_stir "void"
|
.Fn arc4random_stir "void"
|
||||||
.Ft void
|
.Ft void
|
||||||
.Fn arc4random_addrandom "unsigned char *dat" "int datlen"
|
.Fn arc4random_addrandom "unsigned char *dat" "int datlen"
|
||||||
@@ -68,6 +75,21 @@ and therefore has twice the range of
|
|||||||
and
|
and
|
||||||
.Xr random 3 .
|
.Xr random 3 .
|
||||||
.Pp
|
.Pp
|
||||||
|
.Fn arc4random_buf
|
||||||
|
function fills the region
|
||||||
|
.Fa buf
|
||||||
|
of length
|
||||||
|
.Fa nbytes
|
||||||
|
with ARC4-derived random data.
|
||||||
|
.Pp
|
||||||
|
.Fn arc4random_uniform
|
||||||
|
will return a uniformly distributed random number less than
|
||||||
|
.Fa upper_bound .
|
||||||
|
.Fn arc4random_uniform
|
||||||
|
is recommended over constructions like
|
||||||
|
.Dq Li arc4random() % upper_bound
|
||||||
|
as it avoids "modulo bias" when the upper bound is not a power of two.
|
||||||
|
.Pp
|
||||||
The
|
The
|
||||||
.Fn arc4random_stir
|
.Fn arc4random_stir
|
||||||
function reads data from
|
function reads data from
|
||||||
@@ -78,10 +100,9 @@ and uses it to permute the S-Boxes via
|
|||||||
There is no need to call
|
There is no need to call
|
||||||
.Fn arc4random_stir
|
.Fn arc4random_stir
|
||||||
before using
|
before using
|
||||||
.Fn arc4random ,
|
|
||||||
since
|
|
||||||
.Fn arc4random
|
.Fn arc4random
|
||||||
automatically initializes itself.
|
functions family, since
|
||||||
|
they automatically initialize themselves.
|
||||||
.Sh EXAMPLES
|
.Sh EXAMPLES
|
||||||
The following produces a drop-in replacement for the traditional
|
The following produces a drop-in replacement for the traditional
|
||||||
.Fn rand
|
.Fn rand
|
195
src/arc4random.c
195
src/arc4random.c
@@ -1,14 +1,23 @@
|
|||||||
/*
|
/*
|
||||||
* Arc4 random number generator for OpenBSD.
|
* Copyright (c) 1996, David Mazieres <dm@uun.org>
|
||||||
* Copyright 1996 David Mazieres <dm@lcs.mit.edu>.
|
* Copyright (c) 2008, Damien Miller <djm@openbsd.org>
|
||||||
*
|
*
|
||||||
* Modification and redistribution in source and binary forms is
|
* Permission to use, copy, modify, and distribute this software for any
|
||||||
* permitted provided that due credit is given to the author and the
|
* purpose with or without fee is hereby granted, provided that the above
|
||||||
* OpenBSD project (for instance by leaving this copyright notice
|
* copyright notice and this permission notice appear in all copies.
|
||||||
* intact).
|
*
|
||||||
|
* THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES
|
||||||
|
* WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF
|
||||||
|
* MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR
|
||||||
|
* ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES
|
||||||
|
* WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN
|
||||||
|
* ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF
|
||||||
|
* OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE.
|
||||||
*/
|
*/
|
||||||
|
|
||||||
/*
|
/*
|
||||||
|
* Arc4 random number generator for OpenBSD.
|
||||||
|
*
|
||||||
* This code is derived from section 17.1 of Applied Cryptography,
|
* This code is derived from section 17.1 of Applied Cryptography,
|
||||||
* second edition, which describes a stream cipher allegedly
|
* second edition, which describes a stream cipher allegedly
|
||||||
* compatible with RSA Labs "RC4" cipher (the actual description of
|
* compatible with RSA Labs "RC4" cipher (the actual description of
|
||||||
@@ -24,7 +33,7 @@
|
|||||||
*/
|
*/
|
||||||
|
|
||||||
#include <sys/cdefs.h>
|
#include <sys/cdefs.h>
|
||||||
__FBSDID("$FreeBSD: src/lib/libc/gen/arc4random.c,v 1.10 2004/03/24 14:44:57 green Exp $");
|
__FBSDID("$FreeBSD$");
|
||||||
|
|
||||||
#include <sys/types.h>
|
#include <sys/types.h>
|
||||||
#include <sys/time.h>
|
#include <sys/time.h>
|
||||||
@@ -40,6 +49,8 @@ struct arc4_stream {
|
|||||||
};
|
};
|
||||||
|
|
||||||
#define RANDOMDEV "/dev/urandom"
|
#define RANDOMDEV "/dev/urandom"
|
||||||
|
#define KEYSIZE 128
|
||||||
|
|
||||||
#ifdef __REENTRANT
|
#ifdef __REENTRANT
|
||||||
static pthread_mutex_t arc4random_mtx = PTHREAD_MUTEX_INITIALIZER;
|
static pthread_mutex_t arc4random_mtx = PTHREAD_MUTEX_INITIALIZER;
|
||||||
#define THREAD_LOCK() pthread_mutex_lock(&arc4random_mtx)
|
#define THREAD_LOCK() pthread_mutex_lock(&arc4random_mtx)
|
||||||
@@ -52,58 +63,63 @@ static pthread_mutex_t arc4random_mtx = PTHREAD_MUTEX_INITIALIZER;
|
|||||||
static struct arc4_stream rs;
|
static struct arc4_stream rs;
|
||||||
static int rs_initialized;
|
static int rs_initialized;
|
||||||
static int rs_stired;
|
static int rs_stired;
|
||||||
|
static int arc4_count;
|
||||||
|
|
||||||
static inline u_int8_t arc4_getbyte(struct arc4_stream *);
|
static inline u_int8_t arc4_getbyte(void);
|
||||||
static void arc4_stir(struct arc4_stream *);
|
static void arc4_stir(void);
|
||||||
|
|
||||||
static inline void
|
static inline void
|
||||||
arc4_init(struct arc4_stream *as)
|
arc4_init(void)
|
||||||
{
|
{
|
||||||
int n;
|
int n;
|
||||||
|
|
||||||
for (n = 0; n < 256; n++)
|
for (n = 0; n < 256; n++)
|
||||||
as->s[n] = n;
|
rs.s[n] = n;
|
||||||
as->i = 0;
|
rs.i = 0;
|
||||||
as->j = 0;
|
rs.j = 0;
|
||||||
}
|
}
|
||||||
|
|
||||||
static inline void
|
static inline void
|
||||||
arc4_addrandom(struct arc4_stream *as, u_char *dat, int datlen)
|
arc4_addrandom(u_char *dat, int datlen)
|
||||||
{
|
{
|
||||||
int n;
|
int n;
|
||||||
u_int8_t si;
|
u_int8_t si;
|
||||||
|
|
||||||
as->i--;
|
rs.i--;
|
||||||
for (n = 0; n < 256; n++) {
|
for (n = 0; n < 256; n++) {
|
||||||
as->i = (as->i + 1);
|
rs.i = (rs.i + 1);
|
||||||
si = as->s[as->i];
|
si = rs.s[rs.i];
|
||||||
as->j = (as->j + si + dat[n % datlen]);
|
rs.j = (rs.j + si + dat[n % datlen]);
|
||||||
as->s[as->i] = as->s[as->j];
|
rs.s[rs.i] = rs.s[rs.j];
|
||||||
as->s[as->j] = si;
|
rs.s[rs.j] = si;
|
||||||
}
|
}
|
||||||
|
rs.j = rs.i;
|
||||||
}
|
}
|
||||||
|
|
||||||
static void
|
static void
|
||||||
arc4_stir(struct arc4_stream *as)
|
arc4_stir(void)
|
||||||
{
|
{
|
||||||
int fd, n;
|
int done, fd, n;
|
||||||
struct {
|
struct {
|
||||||
struct timeval tv;
|
struct timeval tv;
|
||||||
pid_t pid;
|
pid_t pid;
|
||||||
u_int8_t rnd[128 - sizeof(struct timeval) - sizeof(pid_t)];
|
u_int8_t rnd[KEYSIZE];
|
||||||
} rdat;
|
} rdat;
|
||||||
|
|
||||||
gettimeofday(&rdat.tv, NULL);
|
|
||||||
rdat.pid = getpid();
|
|
||||||
fd = open(RANDOMDEV, O_RDONLY, 0);
|
fd = open(RANDOMDEV, O_RDONLY, 0);
|
||||||
|
done = 0;
|
||||||
if (fd >= 0) {
|
if (fd >= 0) {
|
||||||
(void) read(fd, rdat.rnd, sizeof(rdat.rnd));
|
if (read(fd, &rdat, KEYSIZE) == KEYSIZE)
|
||||||
close(fd);
|
done = 1;
|
||||||
|
(void)close(fd);
|
||||||
}
|
}
|
||||||
/* fd < 0? Ah, what the heck. We'll just take whatever was on the
|
if (!done) {
|
||||||
* stack... */
|
(void)gettimeofday(&rdat.tv, NULL);
|
||||||
|
rdat.pid = getpid();
|
||||||
|
/* We'll just take whatever was on the stack too... */
|
||||||
|
}
|
||||||
|
|
||||||
arc4_addrandom(as, (void *) &rdat, sizeof(rdat));
|
arc4_addrandom((u_char *)&rdat, KEYSIZE);
|
||||||
|
|
||||||
/*
|
/*
|
||||||
* Throw away the first N bytes of output, as suggested in the
|
* Throw away the first N bytes of output, as suggested in the
|
||||||
@@ -113,33 +129,34 @@ arc4_stir(struct arc4_stream *as)
|
|||||||
* by Ilya Mironov.
|
* by Ilya Mironov.
|
||||||
*/
|
*/
|
||||||
for (n = 0; n < 1024; n++)
|
for (n = 0; n < 1024; n++)
|
||||||
arc4_getbyte(as);
|
(void) arc4_getbyte();
|
||||||
|
arc4_count = 1600000;
|
||||||
}
|
}
|
||||||
|
|
||||||
static inline u_int8_t
|
static inline u_int8_t
|
||||||
arc4_getbyte(struct arc4_stream *as)
|
arc4_getbyte(void)
|
||||||
{
|
{
|
||||||
u_int8_t si, sj;
|
u_int8_t si, sj;
|
||||||
|
|
||||||
as->i = (as->i + 1);
|
rs.i = (rs.i + 1);
|
||||||
si = as->s[as->i];
|
si = rs.s[rs.i];
|
||||||
as->j = (as->j + si);
|
rs.j = (rs.j + si);
|
||||||
sj = as->s[as->j];
|
sj = rs.s[rs.j];
|
||||||
as->s[as->i] = sj;
|
rs.s[rs.i] = sj;
|
||||||
as->s[as->j] = si;
|
rs.s[rs.j] = si;
|
||||||
|
|
||||||
return (as->s[(si + sj) & 0xff]);
|
return (rs.s[(si + sj) & 0xff]);
|
||||||
}
|
}
|
||||||
|
|
||||||
static inline u_int32_t
|
static inline u_int32_t
|
||||||
arc4_getword(struct arc4_stream *as)
|
arc4_getword(void)
|
||||||
{
|
{
|
||||||
u_int32_t val;
|
u_int32_t val;
|
||||||
|
|
||||||
val = arc4_getbyte(as) << 24;
|
val = arc4_getbyte() << 24;
|
||||||
val |= arc4_getbyte(as) << 16;
|
val |= arc4_getbyte() << 16;
|
||||||
val |= arc4_getbyte(as) << 8;
|
val |= arc4_getbyte() << 8;
|
||||||
val |= arc4_getbyte(as);
|
val |= arc4_getbyte();
|
||||||
|
|
||||||
return (val);
|
return (val);
|
||||||
}
|
}
|
||||||
@@ -148,55 +165,115 @@ static void
|
|||||||
arc4_check_init(void)
|
arc4_check_init(void)
|
||||||
{
|
{
|
||||||
if (!rs_initialized) {
|
if (!rs_initialized) {
|
||||||
arc4_init(&rs);
|
arc4_init();
|
||||||
rs_initialized = 1;
|
rs_initialized = 1;
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
static void
|
static inline void
|
||||||
arc4_check_stir(void)
|
arc4_check_stir(void)
|
||||||
{
|
{
|
||||||
if (!rs_stired) {
|
if (!rs_stired || arc4_count <= 0) {
|
||||||
arc4_stir(&rs);
|
arc4_stir();
|
||||||
rs_stired = 1;
|
rs_stired = 1;
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
void
|
void
|
||||||
arc4random_stir()
|
arc4random_stir(void)
|
||||||
{
|
{
|
||||||
THREAD_LOCK();
|
THREAD_LOCK();
|
||||||
arc4_check_init();
|
arc4_check_init();
|
||||||
arc4_stir(&rs);
|
arc4_stir();
|
||||||
|
rs_stired = 1;
|
||||||
THREAD_UNLOCK();
|
THREAD_UNLOCK();
|
||||||
}
|
}
|
||||||
|
|
||||||
void
|
void
|
||||||
arc4random_addrandom(dat, datlen)
|
arc4random_addrandom(u_char *dat, int datlen)
|
||||||
u_char *dat;
|
|
||||||
int datlen;
|
|
||||||
{
|
{
|
||||||
THREAD_LOCK();
|
THREAD_LOCK();
|
||||||
arc4_check_init();
|
arc4_check_init();
|
||||||
arc4_check_stir();
|
arc4_check_stir();
|
||||||
arc4_addrandom(&rs, dat, datlen);
|
arc4_addrandom(dat, datlen);
|
||||||
THREAD_UNLOCK();
|
THREAD_UNLOCK();
|
||||||
}
|
}
|
||||||
|
|
||||||
u_int32_t
|
u_int32_t
|
||||||
arc4random()
|
arc4random(void)
|
||||||
{
|
{
|
||||||
u_int32_t rnd;
|
u_int32_t rnd;
|
||||||
|
|
||||||
THREAD_LOCK();
|
THREAD_LOCK();
|
||||||
arc4_check_init();
|
arc4_check_init();
|
||||||
arc4_check_stir();
|
arc4_check_stir();
|
||||||
rnd = arc4_getword(&rs);
|
rnd = arc4_getword();
|
||||||
|
arc4_count -= 4;
|
||||||
THREAD_UNLOCK();
|
THREAD_UNLOCK();
|
||||||
|
|
||||||
return (rnd);
|
return (rnd);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
void
|
||||||
|
arc4random_buf(void *_buf, size_t n)
|
||||||
|
{
|
||||||
|
u_char *buf = (u_char *)_buf;
|
||||||
|
|
||||||
|
THREAD_LOCK();
|
||||||
|
arc4_check_init();
|
||||||
|
while (n--) {
|
||||||
|
arc4_check_stir();
|
||||||
|
buf[n] = arc4_getbyte();
|
||||||
|
arc4_count--;
|
||||||
|
}
|
||||||
|
THREAD_UNLOCK();
|
||||||
|
}
|
||||||
|
|
||||||
|
/*
|
||||||
|
* Calculate a uniformly distributed random number less than upper_bound
|
||||||
|
* avoiding "modulo bias".
|
||||||
|
*
|
||||||
|
* Uniformity is achieved by generating new random numbers until the one
|
||||||
|
* returned is outside the range [0, 2**32 % upper_bound). This
|
||||||
|
* guarantees the selected random number will be inside
|
||||||
|
* [2**32 % upper_bound, 2**32) which maps back to [0, upper_bound)
|
||||||
|
* after reduction modulo upper_bound.
|
||||||
|
*/
|
||||||
|
u_int32_t
|
||||||
|
arc4random_uniform(u_int32_t upper_bound)
|
||||||
|
{
|
||||||
|
u_int32_t r, min;
|
||||||
|
|
||||||
|
if (upper_bound < 2)
|
||||||
|
return (0);
|
||||||
|
|
||||||
|
#if (ULONG_MAX > 0xffffffffUL)
|
||||||
|
min = 0x100000000UL % upper_bound;
|
||||||
|
#else
|
||||||
|
/* Calculate (2**32 % upper_bound) avoiding 64-bit math */
|
||||||
|
if (upper_bound > 0x80000000)
|
||||||
|
min = 1 + ~upper_bound; /* 2**32 - upper_bound */
|
||||||
|
else {
|
||||||
|
/* (2**32 - (x * 2)) % x == 2**32 % x when x <= 2**31 */
|
||||||
|
min = ((0xffffffff - (upper_bound * 2)) + 1) % upper_bound;
|
||||||
|
}
|
||||||
|
#endif
|
||||||
|
|
||||||
|
/*
|
||||||
|
* This could theoretically loop forever but each retry has
|
||||||
|
* p > 0.5 (worst case, usually far better) of selecting a
|
||||||
|
* number inside the range we need, so it should rarely need
|
||||||
|
* to re-roll.
|
||||||
|
*/
|
||||||
|
for (;;) {
|
||||||
|
r = arc4random();
|
||||||
|
if (r >= min)
|
||||||
|
break;
|
||||||
|
}
|
||||||
|
|
||||||
|
return (r % upper_bound);
|
||||||
|
}
|
||||||
|
|
||||||
#if 0
|
#if 0
|
||||||
/*-------- Test code for i386 --------*/
|
/*-------- Test code for i386 --------*/
|
||||||
#include <stdio.h>
|
#include <stdio.h>
|
||||||
|
1
src/arc4random_stir.3
Normal file
1
src/arc4random_stir.3
Normal file
@@ -0,0 +1 @@
|
|||||||
|
.so man3/arc4random.3
|
1
src/arc4random_uniform.3
Normal file
1
src/arc4random_uniform.3
Normal file
@@ -0,0 +1 @@
|
|||||||
|
.so man3/arc4random.3
|
@@ -24,18 +24,21 @@
|
|||||||
* ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
|
* ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
|
||||||
*/
|
*/
|
||||||
|
|
||||||
#include <bsd/getopt.h>
|
#include <getopt.h>
|
||||||
|
|
||||||
int optreset = 0;
|
int optreset = 0;
|
||||||
|
|
||||||
int
|
int
|
||||||
bsd_getopt (int argc, char **argv, char *shortopts)
|
bsd_getopt(int argc, char **argv, char *shortopts)
|
||||||
{
|
{
|
||||||
if (optreset == 1)
|
if (optreset == 1) {
|
||||||
{
|
optreset = 0;
|
||||||
optreset = 0;
|
optind = 0;
|
||||||
optind = 0;
|
}
|
||||||
}
|
|
||||||
|
|
||||||
return getopt (argc, argv, shortopts);
|
/*
|
||||||
|
* Make sure we are using the system getopt() and not a possible
|
||||||
|
* overlay macro.
|
||||||
|
*/
|
||||||
|
return (getopt)(argc, argv, shortopts);
|
||||||
}
|
}
|
||||||
|
1
src/dehumanize_number.3
Normal file
1
src/dehumanize_number.3
Normal file
@@ -0,0 +1 @@
|
|||||||
|
.so man3/humanize_number.3
|
114
src/dehumanize_number.c
Normal file
114
src/dehumanize_number.c
Normal file
@@ -0,0 +1,114 @@
|
|||||||
|
/* $NetBSD: dehumanize_number.c,v 1.2 2007/12/14 17:32:47 xtraeme Exp $ */
|
||||||
|
|
||||||
|
/*
|
||||||
|
* Copyright (c) 2005, 2006 The NetBSD Foundation, Inc.
|
||||||
|
* All rights reserved.
|
||||||
|
*
|
||||||
|
* This code is derived from software contributed to The NetBSD Foundation
|
||||||
|
* by Julio M. Merino Vidal, developed as part of Google's Summer of Code
|
||||||
|
* 2005 program.
|
||||||
|
*
|
||||||
|
* 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 NETBSD FOUNDATION, INC. 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 FOUNDATION 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>
|
||||||
|
#if defined(LIBC_SCCS) && !defined(lint)
|
||||||
|
__RCSID("$NetBSD: dehumanize_number.c,v 1.2 2007/12/14 17:32:47 xtraeme Exp $");
|
||||||
|
#endif /* LIBC_SCCS and not lint */
|
||||||
|
|
||||||
|
#include <inttypes.h>
|
||||||
|
#include <ctype.h>
|
||||||
|
#include <stdlib.h>
|
||||||
|
#include <string.h>
|
||||||
|
#include <errno.h>
|
||||||
|
#include <limits.h>
|
||||||
|
|
||||||
|
/*
|
||||||
|
* Converts the number given in 'str', which may be given in a humanized
|
||||||
|
* form (as described in humanize_number(3), but with some limitations),
|
||||||
|
* to an int64_t without units.
|
||||||
|
* In case of success, 0 is returned and *size holds the value.
|
||||||
|
* Otherwise, -1 is returned and *size is untouched.
|
||||||
|
*
|
||||||
|
* TODO: Internationalization, SI units.
|
||||||
|
*/
|
||||||
|
int
|
||||||
|
dehumanize_number(const char *str, int64_t *size)
|
||||||
|
{
|
||||||
|
char *ep, unit;
|
||||||
|
const char *delimit;
|
||||||
|
long multiplier;
|
||||||
|
long long tmp, tmp2;
|
||||||
|
size_t len;
|
||||||
|
|
||||||
|
len = strlen(str);
|
||||||
|
if (len == 0) {
|
||||||
|
errno = EINVAL;
|
||||||
|
return -1;
|
||||||
|
}
|
||||||
|
|
||||||
|
multiplier = 1;
|
||||||
|
|
||||||
|
unit = str[len - 1];
|
||||||
|
if (isalpha((unsigned char)unit)) {
|
||||||
|
switch (tolower((unsigned char)unit)) {
|
||||||
|
case 'b':
|
||||||
|
multiplier = 1;
|
||||||
|
break;
|
||||||
|
|
||||||
|
case 'k':
|
||||||
|
multiplier = 1024;
|
||||||
|
break;
|
||||||
|
|
||||||
|
case 'm':
|
||||||
|
multiplier = 1024 * 1024;
|
||||||
|
break;
|
||||||
|
|
||||||
|
case 'g':
|
||||||
|
multiplier = 1024 * 1024 * 1024;
|
||||||
|
break;
|
||||||
|
|
||||||
|
default:
|
||||||
|
errno = EINVAL;
|
||||||
|
return -1; /* Invalid suffix. */
|
||||||
|
}
|
||||||
|
|
||||||
|
delimit = &str[len - 1];
|
||||||
|
} else
|
||||||
|
delimit = NULL;
|
||||||
|
|
||||||
|
errno = 0;
|
||||||
|
tmp = strtoll(str, &ep, 10);
|
||||||
|
if (str[0] == '\0' || (ep != delimit && *ep != '\0'))
|
||||||
|
return -1; /* Not a number. */
|
||||||
|
else if (errno == ERANGE && (tmp == LLONG_MAX || tmp == LLONG_MIN))
|
||||||
|
return -1; /* Out of range. */
|
||||||
|
|
||||||
|
tmp2 = tmp * multiplier;
|
||||||
|
tmp2 = tmp2 / multiplier;
|
||||||
|
if (tmp != tmp2) {
|
||||||
|
errno = ERANGE;
|
||||||
|
return -1; /* Out of range. */
|
||||||
|
}
|
||||||
|
*size = tmp * multiplier;
|
||||||
|
|
||||||
|
return 0;
|
||||||
|
}
|
48
src/err.c
48
src/err.c
@@ -24,49 +24,51 @@
|
|||||||
* ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
|
* ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
|
||||||
*/
|
*/
|
||||||
|
|
||||||
#include <bsd/err.h>
|
#include <err.h>
|
||||||
#include <errno.h>
|
#include <errno.h>
|
||||||
#include <stdarg.h>
|
#include <stdarg.h>
|
||||||
|
|
||||||
void
|
void
|
||||||
warnc (int code, const char *format, ...)
|
warnc(int code, const char *format, ...)
|
||||||
{
|
{
|
||||||
int tmp = errno;
|
int tmp = errno;
|
||||||
va_list ap;
|
va_list ap;
|
||||||
va_start (ap, format);
|
|
||||||
|
|
||||||
errno = code;
|
va_start(ap, format);
|
||||||
warn (format, ap);
|
|
||||||
errno = tmp;
|
|
||||||
|
|
||||||
va_end (ap);
|
errno = code;
|
||||||
|
warn(format, ap);
|
||||||
|
errno = tmp;
|
||||||
|
|
||||||
|
va_end(ap);
|
||||||
}
|
}
|
||||||
|
|
||||||
void
|
void
|
||||||
vwarnc (int code, const char *format, va_list ap)
|
vwarnc(int code, const char *format, va_list ap)
|
||||||
{
|
{
|
||||||
int tmp = errno;
|
int tmp = errno;
|
||||||
|
|
||||||
errno = code;
|
errno = code;
|
||||||
vwarn (format, ap);
|
vwarn(format, ap);
|
||||||
errno = tmp;
|
errno = tmp;
|
||||||
}
|
}
|
||||||
|
|
||||||
void
|
void
|
||||||
errc (int status, int code, const char *format, ...)
|
errc(int status, int code, const char *format, ...)
|
||||||
{
|
{
|
||||||
va_list ap;
|
va_list ap;
|
||||||
va_start (ap, format);
|
|
||||||
|
|
||||||
errno = code;
|
va_start(ap, format);
|
||||||
err (status, format, ap);
|
|
||||||
|
|
||||||
va_end (ap);
|
errno = code;
|
||||||
|
err(status, format, ap);
|
||||||
|
|
||||||
|
va_end(ap);
|
||||||
}
|
}
|
||||||
|
|
||||||
void
|
void
|
||||||
verrc (int status, int code, const char *format, va_list ap)
|
verrc(int status, int code, const char *format, va_list ap)
|
||||||
{
|
{
|
||||||
errno = code;
|
errno = code;
|
||||||
verr (status, format, ap);
|
verr(status, format, ap);
|
||||||
}
|
}
|
||||||
|
@@ -35,9 +35,10 @@
|
|||||||
.Nm fgetln
|
.Nm fgetln
|
||||||
.Nd get a line from a stream
|
.Nd get a line from a stream
|
||||||
.Sh LIBRARY
|
.Sh LIBRARY
|
||||||
.Lb libc
|
.ds str-Lb-libbsd Utility functions from BSD systems (libbsd, \-lbsd)
|
||||||
|
.Lb libbsd
|
||||||
.Sh SYNOPSIS
|
.Sh SYNOPSIS
|
||||||
.In stdio.h
|
.In bsd/stdio.h
|
||||||
.Ft char *
|
.Ft char *
|
||||||
.Fn fgetln "FILE *stream" "size_t *len"
|
.Fn fgetln "FILE *stream" "size_t *len"
|
||||||
.Sh DESCRIPTION
|
.Sh DESCRIPTION
|
||||||
@@ -54,7 +55,7 @@ character.
|
|||||||
The length of the line, including the final newline,
|
The length of the line, including the final newline,
|
||||||
is stored in the memory location to which
|
is stored in the memory location to which
|
||||||
.Fa len
|
.Fa len
|
||||||
points.
|
points and is guaranteed to be greater than 0 upon successful completion.
|
||||||
(Note, however, that if the line is the last
|
(Note, however, that if the line is the last
|
||||||
in a file that does not end in a newline,
|
in a file that does not end in a newline,
|
||||||
the returned text will not contain a newline.)
|
the returned text will not contain a newline.)
|
@@ -1,6 +1,6 @@
|
|||||||
/*
|
/*
|
||||||
* Copyright © 2005 Hector Garcia Alvarez
|
* 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
|
* Redistribution and use in source and binary forms, with or without
|
||||||
* modification, are permitted provided that the following conditions
|
* modification, are permitted provided that the following conditions
|
||||||
@@ -32,13 +32,14 @@
|
|||||||
|
|
||||||
#ifdef __GLIBC__
|
#ifdef __GLIBC__
|
||||||
char *
|
char *
|
||||||
fgetln (FILE *stream, size_t *len)
|
fgetln(FILE *stream, size_t *len)
|
||||||
{
|
{
|
||||||
static char *line = NULL;
|
static char *line = NULL;
|
||||||
static size_t line_len = 0;
|
static size_t line_len = 0;
|
||||||
ssize_t nread;
|
ssize_t nread;
|
||||||
|
|
||||||
nread = getline(&line, &line_len, stream);
|
nread = getline(&line, &line_len, stream);
|
||||||
|
/* Note: the getdelim/getline API ensures nread != 0. */
|
||||||
if (nread == -1) {
|
if (nread == -1) {
|
||||||
*len = 0;
|
*len = 0;
|
||||||
return NULL;
|
return NULL;
|
||||||
@@ -47,5 +48,6 @@ fgetln (FILE *stream, size_t *len)
|
|||||||
return line;
|
return line;
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
#else
|
||||||
|
#error "Function fgetln() needs to be ported."
|
||||||
#endif
|
#endif
|
||||||
|
|
||||||
|
102
src/flopen.3
Normal file
102
src/flopen.3
Normal file
@@ -0,0 +1,102 @@
|
|||||||
|
.\"-
|
||||||
|
.\" Copyright (c) 2007 Dag-Erling Co<43>dan Sm<53>rgrav
|
||||||
|
.\" 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 June 6, 2009
|
||||||
|
.Dt FLOPEN 3
|
||||||
|
.Os
|
||||||
|
.Sh NAME
|
||||||
|
.Nm flopen
|
||||||
|
.Nd "Reliably open and lock a file"
|
||||||
|
.Sh LIBRARY
|
||||||
|
.ds str-Lb-libbsd Utility functions from BSD systems (libbsd, \-lbsd)
|
||||||
|
.Lb libbsd
|
||||||
|
.Sh SYNOPSIS
|
||||||
|
.In sys/fcntl.h
|
||||||
|
.In bsd/libutil.h
|
||||||
|
.Ft int
|
||||||
|
.Fn flopen "const char *path" "int flags"
|
||||||
|
.Ft int
|
||||||
|
.Fn flopen "const char *path" "int flags" "mode_t mode"
|
||||||
|
.Sh DESCRIPTION
|
||||||
|
The
|
||||||
|
.Fn flopen
|
||||||
|
function opens or creates a file and acquires an exclusive lock on it.
|
||||||
|
It is essentially equivalent with calling
|
||||||
|
.Fn open
|
||||||
|
with the same parameters followed by
|
||||||
|
.Fn flock
|
||||||
|
with an
|
||||||
|
.Va operation
|
||||||
|
argument of
|
||||||
|
.Dv LOCK_EX ,
|
||||||
|
except that
|
||||||
|
.Fn flopen
|
||||||
|
will attempt to detect and handle races that may occur between opening
|
||||||
|
/ creating the file and locking it.
|
||||||
|
Thus, it is well suited for opening lock files, PID files, spool
|
||||||
|
files, mailboxes and other kinds of files which are used for
|
||||||
|
synchronization between processes.
|
||||||
|
.Pp
|
||||||
|
If
|
||||||
|
.Va flags
|
||||||
|
includes
|
||||||
|
.Dv O_NONBLOCK
|
||||||
|
and the file is already locked,
|
||||||
|
.Fn flopen
|
||||||
|
will fail and set
|
||||||
|
.Va errno
|
||||||
|
to
|
||||||
|
.Dv EWOULDBLOCK .
|
||||||
|
.Pp
|
||||||
|
As with
|
||||||
|
.Fn open ,
|
||||||
|
the additional
|
||||||
|
.Va mode
|
||||||
|
argument is required if
|
||||||
|
.Va flags
|
||||||
|
includes
|
||||||
|
.Dv O_CREAT .
|
||||||
|
.Sh RETURN VALUES
|
||||||
|
If successful,
|
||||||
|
.Fn flopen
|
||||||
|
returns a valid file descriptor.
|
||||||
|
Otherwise, it returns -1, and sets
|
||||||
|
.Va errno
|
||||||
|
as described in
|
||||||
|
.Xr flock 2
|
||||||
|
and
|
||||||
|
.Xr open 2 .
|
||||||
|
.Sh SEE ALSO
|
||||||
|
.Xr errno 2 ,
|
||||||
|
.Xr flock 2 ,
|
||||||
|
.Xr open 2
|
||||||
|
.Sh AUTHORS
|
||||||
|
.An -nosplit
|
||||||
|
The
|
||||||
|
.Nm
|
||||||
|
function and this manual page were written by
|
||||||
|
.An Dag-Erling Sm\(/orgrav Aq des@FreeBSD.org .
|
104
src/flopen.c
Normal file
104
src/flopen.c
Normal file
@@ -0,0 +1,104 @@
|
|||||||
|
/*-
|
||||||
|
* Copyright (c) 2007 Dag-Erling Coïdan Smørgrav
|
||||||
|
* 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
|
||||||
|
* in this position and unchanged.
|
||||||
|
* 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 <sys/file.h>
|
||||||
|
#include <sys/stat.h>
|
||||||
|
|
||||||
|
#include <errno.h>
|
||||||
|
#include <stdarg.h>
|
||||||
|
#include <unistd.h>
|
||||||
|
#include <libutil.h>
|
||||||
|
|
||||||
|
int
|
||||||
|
flopen(const char *path, int flags, ...)
|
||||||
|
{
|
||||||
|
int fd, operation, serrno, trunc;
|
||||||
|
struct stat sb, fsb;
|
||||||
|
mode_t mode;
|
||||||
|
|
||||||
|
#ifdef O_EXLOCK
|
||||||
|
flags &= ~O_EXLOCK;
|
||||||
|
#endif
|
||||||
|
|
||||||
|
mode = 0;
|
||||||
|
if (flags & O_CREAT) {
|
||||||
|
va_list ap;
|
||||||
|
|
||||||
|
va_start(ap, flags);
|
||||||
|
mode = (mode_t)va_arg(ap, int); /* mode_t promoted to int */
|
||||||
|
va_end(ap);
|
||||||
|
}
|
||||||
|
|
||||||
|
operation = LOCK_EX;
|
||||||
|
if (flags & O_NONBLOCK)
|
||||||
|
operation |= LOCK_NB;
|
||||||
|
|
||||||
|
trunc = (flags & O_TRUNC);
|
||||||
|
flags &= ~O_TRUNC;
|
||||||
|
|
||||||
|
for (;;) {
|
||||||
|
if ((fd = open(path, flags, mode)) == -1)
|
||||||
|
/* non-existent or no access */
|
||||||
|
return (-1);
|
||||||
|
if (flock(fd, operation) == -1) {
|
||||||
|
/* unsupported or interrupted */
|
||||||
|
serrno = errno;
|
||||||
|
(void)close(fd);
|
||||||
|
errno = serrno;
|
||||||
|
return (-1);
|
||||||
|
}
|
||||||
|
if (stat(path, &sb) == -1) {
|
||||||
|
/* disappeared from under our feet */
|
||||||
|
(void)close(fd);
|
||||||
|
continue;
|
||||||
|
}
|
||||||
|
if (fstat(fd, &fsb) == -1) {
|
||||||
|
/* can't happen [tm] */
|
||||||
|
serrno = errno;
|
||||||
|
(void)close(fd);
|
||||||
|
errno = serrno;
|
||||||
|
return (-1);
|
||||||
|
}
|
||||||
|
if (sb.st_dev != fsb.st_dev ||
|
||||||
|
sb.st_ino != fsb.st_ino) {
|
||||||
|
/* changed under our feet */
|
||||||
|
(void)close(fd);
|
||||||
|
continue;
|
||||||
|
}
|
||||||
|
if (trunc && ftruncate(fd, 0) != 0) {
|
||||||
|
/* can't happen [tm] */
|
||||||
|
serrno = errno;
|
||||||
|
(void)close(fd);
|
||||||
|
errno = serrno;
|
||||||
|
return (-1);
|
||||||
|
}
|
||||||
|
return (fd);
|
||||||
|
}
|
||||||
|
}
|
@@ -34,9 +34,10 @@
|
|||||||
.Xr printf 3 Ns -style
|
.Xr printf 3 Ns -style
|
||||||
format string
|
format string
|
||||||
.Sh LIBRARY
|
.Sh LIBRARY
|
||||||
.Lb libc
|
.ds str-Lb-libbsd Utility functions from BSD systems (libbsd, \-lbsd)
|
||||||
|
.Lb libbsd
|
||||||
.Sh SYNOPSIS
|
.Sh SYNOPSIS
|
||||||
.In stdio.h
|
.In bsd/stdio.h
|
||||||
.Ft const char *
|
.Ft const char *
|
||||||
.Fn fmtcheck "const char *fmt_suspect" "const char *fmt_default"
|
.Fn fmtcheck "const char *fmt_suspect" "const char *fmt_default"
|
||||||
.Sh DESCRIPTION
|
.Sh DESCRIPTION
|
68
src/fpurge.c
Normal file
68
src/fpurge.c
Normal file
@@ -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
|
138
src/getpeereid.3
Normal file
138
src/getpeereid.3
Normal file
@@ -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 bsd/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 .
|
132
src/getpeereid.c
Normal file
132
src/getpeereid.c
Normal file
@@ -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
|
208
src/heapsort.3
Normal file
208
src/heapsort.3
Normal file
@@ -0,0 +1,208 @@
|
|||||||
|
.\" 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
|
||||||
|
.ds str-Lb-libbsd Utility functions from BSD systems (libbsd, \-lbsd)
|
||||||
|
.Lb libbsd
|
||||||
|
.Sh SYNOPSIS
|
||||||
|
.In bsd/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
|
@@ -34,8 +34,11 @@
|
|||||||
.Nm dehumanize_number ,
|
.Nm dehumanize_number ,
|
||||||
.Nm humanize_number
|
.Nm humanize_number
|
||||||
.Nd format a number into a human readable form and viceversa
|
.Nd format a number into a human readable form and viceversa
|
||||||
|
.Sh LIBRARY
|
||||||
|
.ds str-Lb-libbsd Utility functions from BSD systems (libbsd, \-lbsd)
|
||||||
|
.Lb libbsd
|
||||||
.Sh SYNOPSIS
|
.Sh SYNOPSIS
|
||||||
.In stdlib.h
|
.In bsd/stdlib.h
|
||||||
.Ft int
|
.Ft int
|
||||||
.Fn dehumanize_number "const char *str" "int64_t *result"
|
.Fn dehumanize_number "const char *str" "int64_t *result"
|
||||||
.Ft int
|
.Ft int
|
||||||
@@ -163,4 +166,6 @@ first appeared in
|
|||||||
.Pp
|
.Pp
|
||||||
.Fn dehumanize_number
|
.Fn dehumanize_number
|
||||||
first appeared in
|
first appeared in
|
||||||
|
.\" FIXME: This should be in groff, but for now it avoids the warning.
|
||||||
|
.ds operating-system-NetBSD-5.0 5.0
|
||||||
.Nx 5.0 .
|
.Nx 5.0 .
|
@@ -66,7 +66,7 @@
|
|||||||
#endif
|
#endif
|
||||||
#define ELF_TARG_MACH EM_AVR32
|
#define ELF_TARG_MACH EM_AVR32
|
||||||
#define ELF_TARG_CLASS ELFCLASS32
|
#define ELF_TARG_CLASS ELFCLASS32
|
||||||
#if define(__LITTLE_ENDIAN__)
|
#if defined(__LITTLE_ENDIAN__)
|
||||||
#define ELF_TARG_DATA ELFDATA2LSB
|
#define ELF_TARG_DATA ELFDATA2LSB
|
||||||
#elif defined(__BIG_ENDIAN__)
|
#elif defined(__BIG_ENDIAN__)
|
||||||
#define ELF_TARG_DATA ELFDATA2LMSB
|
#define ELF_TARG_DATA ELFDATA2LMSB
|
||||||
@@ -97,9 +97,9 @@
|
|||||||
#define ELF_TARG_MACH EM_M32R
|
#define ELF_TARG_MACH EM_M32R
|
||||||
#define ELF_TARG_CLASS ELFCLASS32
|
#define ELF_TARG_CLASS ELFCLASS32
|
||||||
#if defined(__LITTLE_ENDIAN__)
|
#if defined(__LITTLE_ENDIAN__)
|
||||||
#define ELF_DATA ELFDATA2LSB
|
#define ELF_TARG_DATA ELFDATA2LSB
|
||||||
#elif defined(__BIG_ENDIAN__)
|
#elif defined(__BIG_ENDIAN__)
|
||||||
#define ELF_DATA ELFDATA2MSB
|
#define ELF_TARG_DATA ELFDATA2MSB
|
||||||
#else
|
#else
|
||||||
#error Unknown M32R endianness
|
#error Unknown M32R endianness
|
||||||
#endif
|
#endif
|
||||||
@@ -147,7 +147,7 @@
|
|||||||
|
|
||||||
#define ELF_TARG_MACH EM_SH
|
#define ELF_TARG_MACH EM_SH
|
||||||
#define ELF_TARG_CLASS ELFCLASS32
|
#define ELF_TARG_CLASS ELFCLASS32
|
||||||
#if define(__LITTLE_ENDIAN__)
|
#if defined(__LITTLE_ENDIAN__)
|
||||||
#define ELF_TARG_DATA ELFDATA2LSB
|
#define ELF_TARG_DATA ELFDATA2LSB
|
||||||
#elif defined(__BIG_ENDIAN__)
|
#elif defined(__BIG_ENDIAN__)
|
||||||
#define ELF_TARG_DATA ELFDATA2LMSB
|
#define ELF_TARG_DATA ELFDATA2LMSB
|
||||||
@@ -194,4 +194,3 @@
|
|||||||
#endif
|
#endif
|
||||||
|
|
||||||
#endif
|
#endif
|
||||||
|
|
||||||
|
@@ -23,9 +23,12 @@
|
|||||||
.Nm MDXFileChunk ,
|
.Nm MDXFileChunk ,
|
||||||
.Nm MDXData
|
.Nm MDXData
|
||||||
.Nd calculate the RSA Data Security, Inc., ``MDX'' message digest
|
.Nd calculate the RSA Data Security, Inc., ``MDX'' message digest
|
||||||
|
.Sh LIBRARY
|
||||||
|
.ds str-Lb-libbsd Utility functions from BSD systems (libbsd, \-lbsd)
|
||||||
|
.Lb libbsd
|
||||||
.Sh SYNOPSIS
|
.Sh SYNOPSIS
|
||||||
.Fd #include <sys/types.h>
|
.Fd #include <sys/types.h>
|
||||||
.Fd #include <mdX.h>
|
.Fd #include <bsd/mdX.h>
|
||||||
.Ft void
|
.Ft void
|
||||||
.Fn MDXInit "MDX_CTX *context"
|
.Fn MDXInit "MDX_CTX *context"
|
||||||
.Ft void
|
.Ft void
|
351
src/merge.c
Normal file
351
src/merge.c
Normal file
@@ -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);
|
||||||
|
}
|
||||||
|
}
|
1
src/mergesort.3
Normal file
1
src/mergesort.3
Normal file
@@ -0,0 +1 @@
|
|||||||
|
.so man3/heapsort.3
|
@@ -35,9 +35,10 @@
|
|||||||
.Nm nlist
|
.Nm nlist
|
||||||
.Nd retrieve symbol table name list from an executable file
|
.Nd retrieve symbol table name list from an executable file
|
||||||
.Sh LIBRARY
|
.Sh LIBRARY
|
||||||
.Lb libc
|
.ds str-Lb-libbsd Utility functions from BSD systems (libbsd, \-lbsd)
|
||||||
|
.Lb libbsd
|
||||||
.Sh SYNOPSIS
|
.Sh SYNOPSIS
|
||||||
.In nlist.h
|
.In bsd/nlist.h
|
||||||
.Ft int
|
.Ft int
|
||||||
.Fn nlist "const char *filename" "struct nlist *nl"
|
.Fn nlist "const char *filename" "struct nlist *nl"
|
||||||
.Sh DESCRIPTION
|
.Sh DESCRIPTION
|
254
src/pidfile.3
Normal file
254
src/pidfile.3
Normal file
@@ -0,0 +1,254 @@
|
|||||||
|
.\" Copyright (c) 2005 Pawel Jakub Dawidek <pjd@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 AUTHORS 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 AUTHORS 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 October 20, 2008
|
||||||
|
.Dt PIDFILE 3
|
||||||
|
.Os
|
||||||
|
.Sh NAME
|
||||||
|
.Nm pidfile_open ,
|
||||||
|
.Nm pidfile_write ,
|
||||||
|
.Nm pidfile_close ,
|
||||||
|
.Nm pidfile_remove
|
||||||
|
.Nd "library for PID files handling"
|
||||||
|
.Sh LIBRARY
|
||||||
|
.ds str-Lb-libbsd Utility functions from BSD systems (libbsd, \-lbsd)
|
||||||
|
.Lb libbsd
|
||||||
|
.Sh SYNOPSIS
|
||||||
|
.In bsd/libutil.h
|
||||||
|
.Ft "struct pidfh *"
|
||||||
|
.Fn pidfile_open "const char *path" "mode_t mode" "pid_t *pidptr"
|
||||||
|
.Ft int
|
||||||
|
.Fn pidfile_write "struct pidfh *pfh"
|
||||||
|
.Ft int
|
||||||
|
.Fn pidfile_close "struct pidfh *pfh"
|
||||||
|
.Ft int
|
||||||
|
.Fn pidfile_remove "struct pidfh *pfh"
|
||||||
|
.Sh DESCRIPTION
|
||||||
|
The
|
||||||
|
.Nm pidfile
|
||||||
|
family of functions allows daemons to handle PID files.
|
||||||
|
It uses
|
||||||
|
.Xr flopen 3
|
||||||
|
to lock a pidfile and detect already running daemons.
|
||||||
|
.Pp
|
||||||
|
The
|
||||||
|
.Fn pidfile_open
|
||||||
|
function opens (or creates) a file specified by the
|
||||||
|
.Fa path
|
||||||
|
argument and locks it.
|
||||||
|
If a file can not be locked, a PID of an already running daemon is returned in
|
||||||
|
the
|
||||||
|
.Fa pidptr
|
||||||
|
argument (if it is not
|
||||||
|
.Dv NULL ) .
|
||||||
|
The function does not write process' PID into the file here, so it can be
|
||||||
|
used before
|
||||||
|
.Fn fork Ns ing
|
||||||
|
and exit with a proper error message when needed.
|
||||||
|
If the
|
||||||
|
.Fa path
|
||||||
|
argument is
|
||||||
|
.Dv NULL ,
|
||||||
|
.Pa /var/run/ Ns Ao Va progname Ac Ns Pa .pid
|
||||||
|
file will be used.
|
||||||
|
.Pp
|
||||||
|
The
|
||||||
|
.Fn pidfile_write
|
||||||
|
function writes process' PID into a previously opened file.
|
||||||
|
.Pp
|
||||||
|
The
|
||||||
|
.Fn pidfile_close
|
||||||
|
function closes a pidfile.
|
||||||
|
It should be used after daemon
|
||||||
|
.Fn fork Ns s
|
||||||
|
to start a child process.
|
||||||
|
.Pp
|
||||||
|
The
|
||||||
|
.Fn pidfile_remove
|
||||||
|
function closes and removes a pidfile.
|
||||||
|
.Sh RETURN VALUES
|
||||||
|
The
|
||||||
|
.Fn pidfile_open
|
||||||
|
function returns a valid pointer to a
|
||||||
|
.Vt pidfh
|
||||||
|
structure on success, or
|
||||||
|
.Dv NULL
|
||||||
|
if an error occurs.
|
||||||
|
If an error occurs,
|
||||||
|
.Va errno
|
||||||
|
will be set.
|
||||||
|
.Pp
|
||||||
|
.Rv -std pidfile_write pidfile_close pidfile_remove
|
||||||
|
.Sh EXAMPLES
|
||||||
|
The following example shows in which order these functions should be used.
|
||||||
|
Note that it is safe to pass
|
||||||
|
.Dv NULL
|
||||||
|
to
|
||||||
|
.Fn pidfile_write ,
|
||||||
|
.Fn pidfile_remove
|
||||||
|
and
|
||||||
|
.Fn pidfile_close
|
||||||
|
functions.
|
||||||
|
.Bd -literal
|
||||||
|
struct pidfh *pfh;
|
||||||
|
pid_t otherpid, childpid;
|
||||||
|
|
||||||
|
pfh = pidfile_open("/var/run/daemon.pid", 0600, &otherpid);
|
||||||
|
if (pfh == NULL) {
|
||||||
|
if (errno == EEXIST) {
|
||||||
|
errx(EXIT_FAILURE, "Daemon already running, pid: %jd.",
|
||||||
|
(intmax_t)otherpid);
|
||||||
|
}
|
||||||
|
/* If we cannot create pidfile from other reasons, only warn. */
|
||||||
|
warn("Cannot open or create pidfile");
|
||||||
|
}
|
||||||
|
|
||||||
|
if (daemon(0, 0) == -1) {
|
||||||
|
warn("Cannot daemonize");
|
||||||
|
pidfile_remove(pfh);
|
||||||
|
exit(EXIT_FAILURE);
|
||||||
|
}
|
||||||
|
|
||||||
|
pidfile_write(pfh);
|
||||||
|
|
||||||
|
for (;;) {
|
||||||
|
/* Do work. */
|
||||||
|
childpid = fork();
|
||||||
|
switch (childpid) {
|
||||||
|
case -1:
|
||||||
|
syslog(LOG_ERR, "Cannot fork(): %s.", strerror(errno));
|
||||||
|
break;
|
||||||
|
case 0:
|
||||||
|
pidfile_close(pfh);
|
||||||
|
/* Do child work. */
|
||||||
|
break;
|
||||||
|
default:
|
||||||
|
syslog(LOG_INFO, "Child %jd started.", (intmax_t)childpid);
|
||||||
|
break;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
pidfile_remove(pfh);
|
||||||
|
exit(EXIT_SUCCESS);
|
||||||
|
.Ed
|
||||||
|
.Sh ERRORS
|
||||||
|
The
|
||||||
|
.Fn pidfile_open
|
||||||
|
function will fail if:
|
||||||
|
.Bl -tag -width Er
|
||||||
|
.It Bq Er EEXIST
|
||||||
|
Some process already holds the lock on the given pidfile, meaning that a
|
||||||
|
daemon is already running.
|
||||||
|
.It Bq Er ENAMETOOLONG
|
||||||
|
Specified pidfile's name is too long.
|
||||||
|
.It Bq Er EINVAL
|
||||||
|
Some process already holds the lock on the given pidfile, but PID read
|
||||||
|
from there is invalid.
|
||||||
|
.It Bq Er EAGAIN
|
||||||
|
Some process already holds the lock on the given pidfile, but the file
|
||||||
|
is truncated.
|
||||||
|
Most likely, the existing daemon is writing new PID into
|
||||||
|
the file.
|
||||||
|
.El
|
||||||
|
.Pp
|
||||||
|
The
|
||||||
|
.Fn pidfile_open
|
||||||
|
function may also fail and set
|
||||||
|
.Va errno
|
||||||
|
for any errors specified for the
|
||||||
|
.Xr fstat 2 ,
|
||||||
|
.Xr open 2 ,
|
||||||
|
and
|
||||||
|
.Xr read 2
|
||||||
|
calls.
|
||||||
|
.Pp
|
||||||
|
The
|
||||||
|
.Fn pidfile_write
|
||||||
|
function will fail if:
|
||||||
|
.Bl -tag -width Er
|
||||||
|
.It Bq Er EINVAL
|
||||||
|
Improper function use.
|
||||||
|
Probably called before
|
||||||
|
.Fn pidfile_open .
|
||||||
|
.El
|
||||||
|
.Pp
|
||||||
|
The
|
||||||
|
.Fn pidfile_write
|
||||||
|
function may also fail and set
|
||||||
|
.Va errno
|
||||||
|
for any errors specified for the
|
||||||
|
.Xr fstat 2 ,
|
||||||
|
.Xr ftruncate 2 ,
|
||||||
|
and
|
||||||
|
.Xr write 2
|
||||||
|
calls.
|
||||||
|
.Pp
|
||||||
|
The
|
||||||
|
.Fn pidfile_close
|
||||||
|
function may fail and set
|
||||||
|
.Va errno
|
||||||
|
for any errors specified for the
|
||||||
|
.Xr close 2
|
||||||
|
and
|
||||||
|
.Xr fstat 2
|
||||||
|
calls.
|
||||||
|
.Pp
|
||||||
|
The
|
||||||
|
.Fn pidfile_remove
|
||||||
|
function will fail if:
|
||||||
|
.Bl -tag -width Er
|
||||||
|
.It Bq Er EINVAL
|
||||||
|
Improper function use.
|
||||||
|
Probably called not from the process which made
|
||||||
|
.Fn pidfile_write .
|
||||||
|
.El
|
||||||
|
.Pp
|
||||||
|
The
|
||||||
|
.Fn pidfile_remove
|
||||||
|
function may also fail and set
|
||||||
|
.Va errno
|
||||||
|
for any errors specified for the
|
||||||
|
.Xr close 2 ,
|
||||||
|
.Xr fstat 2 ,
|
||||||
|
.Xr write 2 ,
|
||||||
|
and
|
||||||
|
.Xr unlink 2
|
||||||
|
system calls and the
|
||||||
|
.Xr flopen 3
|
||||||
|
library function.
|
||||||
|
.Sh SEE ALSO
|
||||||
|
.Xr open 2 ,
|
||||||
|
.Xr daemon 3 ,
|
||||||
|
.Xr flopen 3
|
||||||
|
.Sh AUTHORS
|
||||||
|
.An -nosplit
|
||||||
|
The
|
||||||
|
.Nm pidfile
|
||||||
|
functionality is based on ideas from
|
||||||
|
.An John-Mark Gurney Aq jmg@FreeBSD.org .
|
||||||
|
.Pp
|
||||||
|
The code and manual page was written by
|
||||||
|
.An Pawel Jakub Dawidek Aq pjd@FreeBSD.org .
|
255
src/pidfile.c
Normal file
255
src/pidfile.c
Normal file
@@ -0,0 +1,255 @@
|
|||||||
|
/*-
|
||||||
|
* Copyright (c) 2005 Pawel Jakub Dawidek <pjd@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 AUTHORS 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 AUTHORS 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 <sys/param.h>
|
||||||
|
#include <sys/file.h>
|
||||||
|
#include <sys/stat.h>
|
||||||
|
|
||||||
|
#include <stdio.h>
|
||||||
|
#include <stdlib.h>
|
||||||
|
#include <unistd.h>
|
||||||
|
#include <fcntl.h>
|
||||||
|
#include <string.h>
|
||||||
|
#include <time.h>
|
||||||
|
#include <err.h>
|
||||||
|
#include <errno.h>
|
||||||
|
#include <libutil.h>
|
||||||
|
|
||||||
|
static int _pidfile_remove(struct pidfh *pfh, int freeit);
|
||||||
|
|
||||||
|
static int
|
||||||
|
pidfile_verify(struct pidfh *pfh)
|
||||||
|
{
|
||||||
|
struct stat sb;
|
||||||
|
|
||||||
|
if (pfh == NULL || pfh->pf_fd == -1)
|
||||||
|
return (EINVAL);
|
||||||
|
/*
|
||||||
|
* Check remembered descriptor.
|
||||||
|
*/
|
||||||
|
if (fstat(pfh->pf_fd, &sb) == -1)
|
||||||
|
return (errno);
|
||||||
|
if (sb.st_dev != pfh->pf_dev || sb.st_ino != pfh->pf_ino)
|
||||||
|
return (EINVAL);
|
||||||
|
return (0);
|
||||||
|
}
|
||||||
|
|
||||||
|
static int
|
||||||
|
pidfile_read(const char *path, pid_t *pidptr)
|
||||||
|
{
|
||||||
|
char buf[16], *endptr;
|
||||||
|
int error, fd, i;
|
||||||
|
|
||||||
|
fd = open(path, O_RDONLY);
|
||||||
|
if (fd == -1)
|
||||||
|
return (errno);
|
||||||
|
|
||||||
|
i = read(fd, buf, sizeof(buf) - 1);
|
||||||
|
error = errno; /* Remember errno in case close() wants to change it. */
|
||||||
|
close(fd);
|
||||||
|
if (i == -1)
|
||||||
|
return (error);
|
||||||
|
else if (i == 0)
|
||||||
|
return (EAGAIN);
|
||||||
|
buf[i] = '\0';
|
||||||
|
|
||||||
|
*pidptr = strtol(buf, &endptr, 10);
|
||||||
|
if (endptr != &buf[i])
|
||||||
|
return (EINVAL);
|
||||||
|
|
||||||
|
return (0);
|
||||||
|
}
|
||||||
|
|
||||||
|
struct pidfh *
|
||||||
|
pidfile_open(const char *path, mode_t mode, pid_t *pidptr)
|
||||||
|
{
|
||||||
|
struct pidfh *pfh;
|
||||||
|
struct stat sb;
|
||||||
|
int error, fd, len, count;
|
||||||
|
struct timespec rqtp;
|
||||||
|
|
||||||
|
pfh = malloc(sizeof(*pfh));
|
||||||
|
if (pfh == NULL)
|
||||||
|
return (NULL);
|
||||||
|
|
||||||
|
if (path == NULL) {
|
||||||
|
len = asprintf(&pfh->pf_path, "/var/run/%s.pid", getprogname());
|
||||||
|
if (len < 0) {
|
||||||
|
free(pfh);
|
||||||
|
return (NULL);
|
||||||
|
}
|
||||||
|
} else
|
||||||
|
pfh->pf_path = strdup(path);
|
||||||
|
|
||||||
|
/*
|
||||||
|
* Open the PID file and obtain exclusive lock.
|
||||||
|
* We truncate PID file here only to remove old PID immediatelly,
|
||||||
|
* PID file will be truncated again in pidfile_write(), so
|
||||||
|
* pidfile_write() can be called multiple times.
|
||||||
|
*/
|
||||||
|
fd = flopen(pfh->pf_path,
|
||||||
|
O_WRONLY | O_CREAT | O_TRUNC | O_NONBLOCK, mode);
|
||||||
|
if (fd == -1) {
|
||||||
|
count = 0;
|
||||||
|
rqtp.tv_sec = 0;
|
||||||
|
rqtp.tv_nsec = 5000000;
|
||||||
|
if (errno == EWOULDBLOCK && pidptr != NULL) {
|
||||||
|
again:
|
||||||
|
errno = pidfile_read(pfh->pf_path, pidptr);
|
||||||
|
if (errno == 0)
|
||||||
|
errno = EEXIST;
|
||||||
|
else if (errno == EAGAIN) {
|
||||||
|
if (++count <= 3) {
|
||||||
|
nanosleep(&rqtp, 0);
|
||||||
|
goto again;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
free(pfh->pf_path);
|
||||||
|
free(pfh);
|
||||||
|
return (NULL);
|
||||||
|
}
|
||||||
|
/*
|
||||||
|
* Remember file information, so in pidfile_write() we are sure we write
|
||||||
|
* to the proper descriptor.
|
||||||
|
*/
|
||||||
|
if (fstat(fd, &sb) == -1) {
|
||||||
|
error = errno;
|
||||||
|
unlink(pfh->pf_path);
|
||||||
|
free(pfh->pf_path);
|
||||||
|
close(fd);
|
||||||
|
free(pfh);
|
||||||
|
errno = error;
|
||||||
|
return (NULL);
|
||||||
|
}
|
||||||
|
|
||||||
|
pfh->pf_fd = fd;
|
||||||
|
pfh->pf_dev = sb.st_dev;
|
||||||
|
pfh->pf_ino = sb.st_ino;
|
||||||
|
|
||||||
|
return (pfh);
|
||||||
|
}
|
||||||
|
|
||||||
|
int
|
||||||
|
pidfile_write(struct pidfh *pfh)
|
||||||
|
{
|
||||||
|
char pidstr[16];
|
||||||
|
int error, fd;
|
||||||
|
|
||||||
|
/*
|
||||||
|
* Check remembered descriptor, so we don't overwrite some other
|
||||||
|
* file if pidfile was closed and descriptor reused.
|
||||||
|
*/
|
||||||
|
errno = pidfile_verify(pfh);
|
||||||
|
if (errno != 0) {
|
||||||
|
/*
|
||||||
|
* Don't close descriptor, because we are not sure if it's ours.
|
||||||
|
*/
|
||||||
|
return (-1);
|
||||||
|
}
|
||||||
|
fd = pfh->pf_fd;
|
||||||
|
|
||||||
|
/*
|
||||||
|
* Truncate PID file, so multiple calls of pidfile_write() are allowed.
|
||||||
|
*/
|
||||||
|
if (ftruncate(fd, 0) == -1) {
|
||||||
|
error = errno;
|
||||||
|
_pidfile_remove(pfh, 0);
|
||||||
|
errno = error;
|
||||||
|
return (-1);
|
||||||
|
}
|
||||||
|
|
||||||
|
snprintf(pidstr, sizeof(pidstr), "%u", getpid());
|
||||||
|
if (pwrite(fd, pidstr, strlen(pidstr), 0) != (ssize_t)strlen(pidstr)) {
|
||||||
|
error = errno;
|
||||||
|
_pidfile_remove(pfh, 0);
|
||||||
|
errno = error;
|
||||||
|
return (-1);
|
||||||
|
}
|
||||||
|
|
||||||
|
return (0);
|
||||||
|
}
|
||||||
|
|
||||||
|
int
|
||||||
|
pidfile_close(struct pidfh *pfh)
|
||||||
|
{
|
||||||
|
int error;
|
||||||
|
|
||||||
|
error = pidfile_verify(pfh);
|
||||||
|
if (error != 0) {
|
||||||
|
errno = error;
|
||||||
|
return (-1);
|
||||||
|
}
|
||||||
|
|
||||||
|
if (close(pfh->pf_fd) == -1)
|
||||||
|
error = errno;
|
||||||
|
free(pfh->pf_path);
|
||||||
|
free(pfh);
|
||||||
|
if (error != 0) {
|
||||||
|
errno = error;
|
||||||
|
return (-1);
|
||||||
|
}
|
||||||
|
return (0);
|
||||||
|
}
|
||||||
|
|
||||||
|
static int
|
||||||
|
_pidfile_remove(struct pidfh *pfh, int freeit)
|
||||||
|
{
|
||||||
|
int error;
|
||||||
|
|
||||||
|
error = pidfile_verify(pfh);
|
||||||
|
if (error != 0) {
|
||||||
|
errno = error;
|
||||||
|
return (-1);
|
||||||
|
}
|
||||||
|
|
||||||
|
if (unlink(pfh->pf_path) == -1)
|
||||||
|
error = errno;
|
||||||
|
if (close(pfh->pf_fd) == -1) {
|
||||||
|
if (error == 0)
|
||||||
|
error = errno;
|
||||||
|
}
|
||||||
|
if (freeit) {
|
||||||
|
free(pfh->pf_path);
|
||||||
|
free(pfh);
|
||||||
|
} else
|
||||||
|
pfh->pf_fd = -1;
|
||||||
|
if (error != 0) {
|
||||||
|
errno = error;
|
||||||
|
return (-1);
|
||||||
|
}
|
||||||
|
return (0);
|
||||||
|
}
|
||||||
|
|
||||||
|
int
|
||||||
|
pidfile_remove(struct pidfh *pfh)
|
||||||
|
{
|
||||||
|
|
||||||
|
return (_pidfile_remove(pfh, 1));
|
||||||
|
}
|
@@ -1,5 +1,6 @@
|
|||||||
/*
|
/*
|
||||||
* Copyright © 2006 Robert Millan
|
* Copyright © 2006 Robert Millan
|
||||||
|
* Copyright © 2010-2011 Guillem Jover
|
||||||
*
|
*
|
||||||
* Redistribution and use in source and binary forms, with or without
|
* Redistribution and use in source and binary forms, with or without
|
||||||
* modification, are permitted provided that the following conditions
|
* modification, are permitted provided that the following conditions
|
||||||
@@ -28,18 +29,31 @@
|
|||||||
Rejected in glibc (http://sourceware.org/ml/libc-alpha/2006-03/msg00125.html)
|
Rejected in glibc (http://sourceware.org/ml/libc-alpha/2006-03/msg00125.html)
|
||||||
*/
|
*/
|
||||||
|
|
||||||
#include <bsd/stdlib.h>
|
#include <errno.h>
|
||||||
|
#include <string.h>
|
||||||
|
#include <stdlib.h>
|
||||||
|
|
||||||
static char *__progname = NULL;
|
static const char *__progname = NULL;
|
||||||
|
|
||||||
char *
|
const char *
|
||||||
getprogname ()
|
getprogname(void)
|
||||||
{
|
{
|
||||||
return __progname;
|
#ifdef __GLIBC__
|
||||||
|
if (__progname == NULL)
|
||||||
|
__progname = program_invocation_short_name;
|
||||||
|
#endif
|
||||||
|
|
||||||
|
return __progname;
|
||||||
}
|
}
|
||||||
|
|
||||||
void
|
void
|
||||||
setprogname (char *new)
|
setprogname(const char *progname)
|
||||||
{
|
{
|
||||||
__progname = new;
|
const char *last_slash;
|
||||||
|
|
||||||
|
last_slash = strrchr(progname, '/');
|
||||||
|
if (last_slash == NULL)
|
||||||
|
__progname = progname;
|
||||||
|
else
|
||||||
|
__progname = last_slash + 1;
|
||||||
}
|
}
|
||||||
|
161
src/radixsort.3
Normal file
161
src/radixsort.3
Normal file
@@ -0,0 +1,161 @@
|
|||||||
|
.\" 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
|
||||||
|
.ds str-Lb-libbsd Utility functions from BSD systems (libbsd, \-lbsd)
|
||||||
|
.Lb libbsd
|
||||||
|
.Sh SYNOPSIS
|
||||||
|
.In limits.h
|
||||||
|
.In bsd/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 .
|
327
src/radixsort.c
Normal file
327
src/radixsort.c
Normal file
@@ -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);
|
||||||
|
}
|
||||||
|
}
|
168
src/readpassphrase.3
Normal file
168
src/readpassphrase.3
Normal file
@@ -0,0 +1,168 @@
|
|||||||
|
.\" $OpenBSD: readpassphrase.3,v 1.16 2005/07/22 03:16:58 jaredy Exp $
|
||||||
|
.\"
|
||||||
|
.\" Copyright (c) 2000, 2002 Todd C. Miller <Todd.Miller@courtesan.com>
|
||||||
|
.\"
|
||||||
|
.\" Permission to use, copy, modify, and distribute this software for any
|
||||||
|
.\" purpose with or without fee is hereby granted, provided that the above
|
||||||
|
.\" copyright notice and this permission notice appear in all copies.
|
||||||
|
.\"
|
||||||
|
.\" THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES
|
||||||
|
.\" WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF
|
||||||
|
.\" MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR
|
||||||
|
.\" ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES
|
||||||
|
.\" WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN
|
||||||
|
.\" ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF
|
||||||
|
.\" OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE.
|
||||||
|
.\"
|
||||||
|
.\" Sponsored in part by the Defense Advanced Research Projects
|
||||||
|
.\" Agency (DARPA) and Air Force Research Laboratory, Air Force
|
||||||
|
.\" Materiel Command, USAF, under agreement number F39502-99-1-0512.
|
||||||
|
.\"
|
||||||
|
.Dd $Mdocdate: May 31 2007 $
|
||||||
|
.Dt READPASSPHRASE 3
|
||||||
|
.Os
|
||||||
|
.Sh NAME
|
||||||
|
.Nm readpassphrase
|
||||||
|
.Nd get a passphrase from the user
|
||||||
|
.Sh LIBRARY
|
||||||
|
.ds str-Lb-libbsd Utility functions from BSD systems (libbsd, \-lbsd)
|
||||||
|
.Lb libbsd
|
||||||
|
.Sh SYNOPSIS
|
||||||
|
.Fd #include <bsd/readpassphrase.h>
|
||||||
|
.Ft char *
|
||||||
|
.Fn readpassphrase "const char *prompt" "char *buf" "size_t bufsiz" "int flags"
|
||||||
|
.Sh DESCRIPTION
|
||||||
|
The
|
||||||
|
.Fn readpassphrase
|
||||||
|
function displays a prompt to, and reads in a passphrase from,
|
||||||
|
.Pa /dev/tty .
|
||||||
|
If this file is inaccessible
|
||||||
|
and the
|
||||||
|
.Dv RPP_REQUIRE_TTY
|
||||||
|
flag is not set,
|
||||||
|
.Fn readpassphrase
|
||||||
|
displays the prompt on the standard error output and reads from the standard
|
||||||
|
input.
|
||||||
|
In this case it is generally not possible to turn off echo.
|
||||||
|
.Pp
|
||||||
|
Up to
|
||||||
|
.Fa bufsiz
|
||||||
|
- 1 characters (one is for the NUL) are read into the provided buffer
|
||||||
|
.Fa buf .
|
||||||
|
Any additional
|
||||||
|
characters and the terminating newline (or return) character are discarded.
|
||||||
|
.Pp
|
||||||
|
.Fn readpassphrase
|
||||||
|
takes the following optional
|
||||||
|
.Fa flags :
|
||||||
|
.Bd -literal -offset indent
|
||||||
|
RPP_ECHO_OFF turn off echo (default behavior)
|
||||||
|
RPP_ECHO_ON leave echo on
|
||||||
|
RPP_REQUIRE_TTY fail if there is no tty
|
||||||
|
RPP_FORCELOWER force input to lower case
|
||||||
|
RPP_FORCEUPPER force input to upper case
|
||||||
|
RPP_SEVENBIT strip the high bit from input
|
||||||
|
RPP_STDIN force read of passphrase from stdin
|
||||||
|
.Ed
|
||||||
|
.Pp
|
||||||
|
The calling process should zero the passphrase as soon as possible to
|
||||||
|
avoid leaving the cleartext passphrase visible in the process's address
|
||||||
|
space.
|
||||||
|
.Sh RETURN VALUES
|
||||||
|
Upon successful completion,
|
||||||
|
.Fn readpassphrase
|
||||||
|
returns a pointer to the NUL-terminated passphrase.
|
||||||
|
If an error is encountered, the terminal state is restored and
|
||||||
|
a null pointer is returned.
|
||||||
|
.Sh FILES
|
||||||
|
.Bl -tag -width /dev/tty -compact
|
||||||
|
.It Pa /dev/tty
|
||||||
|
.El
|
||||||
|
.Sh EXAMPLES
|
||||||
|
The following code fragment will read a passphrase from
|
||||||
|
.Pa /dev/tty
|
||||||
|
into the buffer
|
||||||
|
.Fa passbuf .
|
||||||
|
.Bd -literal -offset indent
|
||||||
|
char passbuf[1024];
|
||||||
|
|
||||||
|
\&...
|
||||||
|
|
||||||
|
if (readpassphrase("Response: ", passbuf, sizeof(passbuf),
|
||||||
|
RPP_REQUIRE_TTY) == NULL)
|
||||||
|
errx(1, "unable to read passphrase");
|
||||||
|
|
||||||
|
if (compare(transform(passbuf), epass) != 0)
|
||||||
|
errx(1, "bad passphrase");
|
||||||
|
|
||||||
|
\&...
|
||||||
|
|
||||||
|
memset(passbuf, 0, sizeof(passbuf));
|
||||||
|
.Ed
|
||||||
|
.Sh ERRORS
|
||||||
|
.Bl -tag -width Er
|
||||||
|
.It Bq Er EINTR
|
||||||
|
The
|
||||||
|
.Fn readpassphrase
|
||||||
|
function was interrupted by a signal.
|
||||||
|
.It Bq Er EINVAL
|
||||||
|
The
|
||||||
|
.Ar bufsiz
|
||||||
|
argument was zero.
|
||||||
|
.It Bq Er EIO
|
||||||
|
The process is a member of a background process attempting to read
|
||||||
|
from its controlling terminal, the process is ignoring or blocking
|
||||||
|
the
|
||||||
|
.Dv SIGTTIN
|
||||||
|
signal, or the process group is orphaned.
|
||||||
|
.It Bq Er EMFILE
|
||||||
|
The process has already reached its limit for open file descriptors.
|
||||||
|
.It Bq Er ENFILE
|
||||||
|
The system file table is full.
|
||||||
|
.It Bq Er ENOTTY
|
||||||
|
There is no controlling terminal and the
|
||||||
|
.Dv RPP_REQUIRE_TTY
|
||||||
|
flag was specified.
|
||||||
|
.El
|
||||||
|
.Sh SIGNALS
|
||||||
|
.Fn readpassphrase
|
||||||
|
will catch the following signals:
|
||||||
|
.Bd -literal -offset indent
|
||||||
|
SIGALRM SIGHUP SIGINT
|
||||||
|
SIGPIPE SIGQUIT SIGTERM
|
||||||
|
SIGTSTP SIGTTIN SIGTTOU
|
||||||
|
.Ed
|
||||||
|
.Pp
|
||||||
|
When one of the above signals is intercepted, terminal echo will
|
||||||
|
be restored if it had previously been turned off.
|
||||||
|
If a signal handler was installed for the signal when
|
||||||
|
.Fn readpassphrase
|
||||||
|
was called, that handler is then executed.
|
||||||
|
If no handler was previously installed for the signal then the
|
||||||
|
default action is taken as per
|
||||||
|
.Xr sigaction 2 .
|
||||||
|
.Pp
|
||||||
|
The
|
||||||
|
.Dv SIGTSTP ,
|
||||||
|
.Dv SIGTTIN ,
|
||||||
|
and
|
||||||
|
.Dv SIGTTOU
|
||||||
|
signals (stop signals generated from keyboard or due to terminal I/O
|
||||||
|
from a background process) are treated specially.
|
||||||
|
When the process is resumed after it has been stopped,
|
||||||
|
.Fn readpassphrase
|
||||||
|
will reprint the prompt and the user may then enter a passphrase.
|
||||||
|
.Sh SEE ALSO
|
||||||
|
.Xr sigaction 2 ,
|
||||||
|
.Xr getpass 3
|
||||||
|
.Sh STANDARDS
|
||||||
|
The
|
||||||
|
.Fn readpassphrase
|
||||||
|
function is an
|
||||||
|
.Ox
|
||||||
|
extension and should not be used if portability is desired.
|
||||||
|
.Sh HISTORY
|
||||||
|
The
|
||||||
|
.Fn readpassphrase
|
||||||
|
function first appeared in
|
||||||
|
.Ox 2.9 .
|
187
src/readpassphrase.c
Normal file
187
src/readpassphrase.c
Normal file
@@ -0,0 +1,187 @@
|
|||||||
|
/* $OpenBSD: readpassphrase.c,v 1.20 2007/10/30 12:03:48 millert Exp $ */
|
||||||
|
|
||||||
|
/*
|
||||||
|
* Copyright (c) 2000-2002, 2007 Todd C. Miller <Todd.Miller@courtesan.com>
|
||||||
|
*
|
||||||
|
* Permission to use, copy, modify, and distribute this software for any
|
||||||
|
* purpose with or without fee is hereby granted, provided that the above
|
||||||
|
* copyright notice and this permission notice appear in all copies.
|
||||||
|
*
|
||||||
|
* THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES
|
||||||
|
* WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF
|
||||||
|
* MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR
|
||||||
|
* ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES
|
||||||
|
* WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN
|
||||||
|
* ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF
|
||||||
|
* OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE.
|
||||||
|
*
|
||||||
|
* Sponsored in part by the Defense Advanced Research Projects
|
||||||
|
* Agency (DARPA) and Air Force Research Laboratory, Air Force
|
||||||
|
* Materiel Command, USAF, under agreement number F39502-99-1-0512.
|
||||||
|
*/
|
||||||
|
|
||||||
|
#include <ctype.h>
|
||||||
|
#include <errno.h>
|
||||||
|
#include <fcntl.h>
|
||||||
|
#include <paths.h>
|
||||||
|
#include <pwd.h>
|
||||||
|
#include <signal.h>
|
||||||
|
#include <string.h>
|
||||||
|
#include <termios.h>
|
||||||
|
#include <unistd.h>
|
||||||
|
#include <readpassphrase.h>
|
||||||
|
|
||||||
|
#ifndef TCSASOFT
|
||||||
|
#define TCSASOFT 0
|
||||||
|
#endif
|
||||||
|
|
||||||
|
static volatile sig_atomic_t signo;
|
||||||
|
|
||||||
|
static void handler(int);
|
||||||
|
|
||||||
|
char *
|
||||||
|
readpassphrase(const char *prompt, char *buf, size_t bufsiz, int flags)
|
||||||
|
{
|
||||||
|
ssize_t nr;
|
||||||
|
int input, output, save_errno;
|
||||||
|
char ch, *p, *end;
|
||||||
|
struct termios term, oterm;
|
||||||
|
struct sigaction sa, savealrm, saveint, savehup, savequit, saveterm;
|
||||||
|
struct sigaction savetstp, savettin, savettou, savepipe;
|
||||||
|
|
||||||
|
/* I suppose we could alloc on demand in this case (XXX). */
|
||||||
|
if (bufsiz == 0) {
|
||||||
|
errno = EINVAL;
|
||||||
|
return(NULL);
|
||||||
|
}
|
||||||
|
|
||||||
|
restart:
|
||||||
|
signo = 0;
|
||||||
|
nr = -1;
|
||||||
|
save_errno = 0;
|
||||||
|
/*
|
||||||
|
* Read and write to /dev/tty if available. If not, read from
|
||||||
|
* stdin and write to stderr unless a tty is required.
|
||||||
|
*/
|
||||||
|
if ((flags & RPP_STDIN) ||
|
||||||
|
(input = output = open(_PATH_TTY, O_RDWR)) == -1) {
|
||||||
|
if (flags & RPP_REQUIRE_TTY) {
|
||||||
|
errno = ENOTTY;
|
||||||
|
return(NULL);
|
||||||
|
}
|
||||||
|
input = STDIN_FILENO;
|
||||||
|
output = STDERR_FILENO;
|
||||||
|
}
|
||||||
|
|
||||||
|
/*
|
||||||
|
* Catch signals that would otherwise cause the user to end
|
||||||
|
* up with echo turned off in the shell. Don't worry about
|
||||||
|
* things like SIGXCPU and SIGVTALRM for now.
|
||||||
|
*/
|
||||||
|
sigemptyset(&sa.sa_mask);
|
||||||
|
sa.sa_flags = 0; /* don't restart system calls */
|
||||||
|
sa.sa_handler = handler;
|
||||||
|
(void)sigaction(SIGALRM, &sa, &savealrm);
|
||||||
|
(void)sigaction(SIGHUP, &sa, &savehup);
|
||||||
|
(void)sigaction(SIGINT, &sa, &saveint);
|
||||||
|
(void)sigaction(SIGPIPE, &sa, &savepipe);
|
||||||
|
(void)sigaction(SIGQUIT, &sa, &savequit);
|
||||||
|
(void)sigaction(SIGTERM, &sa, &saveterm);
|
||||||
|
(void)sigaction(SIGTSTP, &sa, &savetstp);
|
||||||
|
(void)sigaction(SIGTTIN, &sa, &savettin);
|
||||||
|
(void)sigaction(SIGTTOU, &sa, &savettou);
|
||||||
|
|
||||||
|
/* Turn off echo if possible. */
|
||||||
|
if (input != STDIN_FILENO && tcgetattr(input, &oterm) == 0) {
|
||||||
|
memcpy(&term, &oterm, sizeof(term));
|
||||||
|
if (!(flags & RPP_ECHO_ON))
|
||||||
|
term.c_lflag &= ~(ECHO | ECHONL);
|
||||||
|
#ifdef VSTATUS
|
||||||
|
if (term.c_cc[VSTATUS] != _POSIX_VDISABLE)
|
||||||
|
term.c_cc[VSTATUS] = _POSIX_VDISABLE;
|
||||||
|
#endif
|
||||||
|
(void)tcsetattr(input, TCSAFLUSH|TCSASOFT, &term);
|
||||||
|
} else {
|
||||||
|
memset(&term, 0, sizeof(term));
|
||||||
|
term.c_lflag |= ECHO;
|
||||||
|
memset(&oterm, 0, sizeof(oterm));
|
||||||
|
oterm.c_lflag |= ECHO;
|
||||||
|
}
|
||||||
|
|
||||||
|
/* No I/O if we are already backgrounded. */
|
||||||
|
if (signo != SIGTTOU && signo != SIGTTIN) {
|
||||||
|
if (!(flags & RPP_STDIN))
|
||||||
|
(void)write(output, prompt, strlen(prompt));
|
||||||
|
end = buf + bufsiz - 1;
|
||||||
|
p = buf;
|
||||||
|
while ((nr = read(input, &ch, 1)) == 1 && ch != '\n' && ch != '\r') {
|
||||||
|
if (p < end) {
|
||||||
|
if ((flags & RPP_SEVENBIT))
|
||||||
|
ch &= 0x7f;
|
||||||
|
if (isalpha(ch)) {
|
||||||
|
if ((flags & RPP_FORCELOWER))
|
||||||
|
ch = (char)tolower(ch);
|
||||||
|
if ((flags & RPP_FORCEUPPER))
|
||||||
|
ch = (char)toupper(ch);
|
||||||
|
}
|
||||||
|
*p++ = ch;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
*p = '\0';
|
||||||
|
save_errno = errno;
|
||||||
|
if (!(term.c_lflag & ECHO))
|
||||||
|
(void)write(output, "\n", 1);
|
||||||
|
}
|
||||||
|
|
||||||
|
/* Restore old terminal settings and signals. */
|
||||||
|
if (memcmp(&term, &oterm, sizeof(term)) != 0) {
|
||||||
|
while (tcsetattr(input, TCSAFLUSH|TCSASOFT, &oterm) == -1 &&
|
||||||
|
errno == EINTR)
|
||||||
|
continue;
|
||||||
|
}
|
||||||
|
(void)sigaction(SIGALRM, &savealrm, NULL);
|
||||||
|
(void)sigaction(SIGHUP, &savehup, NULL);
|
||||||
|
(void)sigaction(SIGINT, &saveint, NULL);
|
||||||
|
(void)sigaction(SIGQUIT, &savequit, NULL);
|
||||||
|
(void)sigaction(SIGPIPE, &savepipe, NULL);
|
||||||
|
(void)sigaction(SIGTERM, &saveterm, NULL);
|
||||||
|
(void)sigaction(SIGTSTP, &savetstp, NULL);
|
||||||
|
(void)sigaction(SIGTTIN, &savettin, NULL);
|
||||||
|
(void)sigaction(SIGTTOU, &savettou, NULL);
|
||||||
|
if (input != STDIN_FILENO)
|
||||||
|
(void)close(input);
|
||||||
|
|
||||||
|
/*
|
||||||
|
* If we were interrupted by a signal, resend it to ourselves
|
||||||
|
* now that we have restored the signal handlers.
|
||||||
|
*/
|
||||||
|
if (signo) {
|
||||||
|
kill(getpid(), signo);
|
||||||
|
switch (signo) {
|
||||||
|
case SIGTSTP:
|
||||||
|
case SIGTTIN:
|
||||||
|
case SIGTTOU:
|
||||||
|
goto restart;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
if (save_errno)
|
||||||
|
errno = save_errno;
|
||||||
|
return(nr == -1 ? NULL : buf);
|
||||||
|
}
|
||||||
|
|
||||||
|
#if 0
|
||||||
|
char *
|
||||||
|
getpass(const char *prompt)
|
||||||
|
{
|
||||||
|
static char buf[_PASSWORD_LEN + 1];
|
||||||
|
|
||||||
|
return(readpassphrase(prompt, buf, sizeof(buf), RPP_ECHO_OFF));
|
||||||
|
}
|
||||||
|
#endif
|
||||||
|
|
||||||
|
static void handler(int s)
|
||||||
|
{
|
||||||
|
|
||||||
|
signo = s;
|
||||||
|
}
|
107
src/reallocf.3
Normal file
107
src/reallocf.3
Normal file
@@ -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 bsd/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 .
|
48
src/reallocf.c
Normal file
48
src/reallocf.c
Normal file
@@ -0,0 +1,48 @@
|
|||||||
|
/*-
|
||||||
|
* Copyright (c) 1998, M. Warner Losh <imp@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);
|
||||||
|
}
|
@@ -36,9 +36,10 @@
|
|||||||
.Nm setmode
|
.Nm setmode
|
||||||
.Nd modify mode bits
|
.Nd modify mode bits
|
||||||
.Sh LIBRARY
|
.Sh LIBRARY
|
||||||
.Lb libc
|
.ds str-Lb-libbsd Utility functions from BSD systems (libbsd, \-lbsd)
|
||||||
|
.Lb libbsd
|
||||||
.Sh SYNOPSIS
|
.Sh SYNOPSIS
|
||||||
.In unistd.h
|
.In bsd/unistd.h
|
||||||
.Ft mode_t
|
.Ft mode_t
|
||||||
.Fn getmode "const void *set" "mode_t mode"
|
.Fn getmode "const void *set" "mode_t mode"
|
||||||
.Ft void *
|
.Ft void *
|
32
src/setproctitle.c
Normal file
32
src/setproctitle.c
Normal file
@@ -0,0 +1,32 @@
|
|||||||
|
/*
|
||||||
|
* 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.
|
||||||
|
*/
|
||||||
|
|
||||||
|
void
|
||||||
|
setproctitle(const char *fmt, ...)
|
||||||
|
{
|
||||||
|
/* Stub so that we can implement it later on and programs will
|
||||||
|
* automatically benefit from it, w/o needing to recompile. */
|
||||||
|
}
|
1
src/sradixsort.3
Normal file
1
src/sradixsort.3
Normal file
@@ -0,0 +1 @@
|
|||||||
|
.so man3/radixsort.3
|
@@ -1,37 +1,21 @@
|
|||||||
/* $OpenBSD: strlcat.c,v 1.2 1999/06/17 16:28:58 millert Exp $ */
|
/* $OpenBSD: strlcat.c,v 1.12 2005/03/30 20:13:52 otto Exp $ */
|
||||||
|
|
||||||
/*
|
/*
|
||||||
* Copyright (c) 1998 Todd C. Miller <Todd.Miller@courtesan.com>
|
* Copyright (c) 1998 Todd C. Miller <Todd.Miller@courtesan.com>
|
||||||
* All rights reserved.
|
|
||||||
*
|
*
|
||||||
* Redistribution and use in source and binary forms, with or without
|
* Permission to use, copy, modify, and distribute this software for any
|
||||||
* modification, are permitted provided that the following conditions
|
* purpose with or without fee is hereby granted, provided that the above
|
||||||
* are met:
|
* copyright notice and this permission notice appear in all copies.
|
||||||
* 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,
|
* THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES
|
||||||
* INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY
|
* WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF
|
||||||
* AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL
|
* MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR
|
||||||
* THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL,
|
* ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES
|
||||||
* EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO,
|
* WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN
|
||||||
* PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS;
|
* ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF
|
||||||
* OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY,
|
* OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE.
|
||||||
* 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 *rcsid = "$OpenBSD: strlcat.c,v 1.2 1999/06/17 16:28:58 millert Exp $");
|
|
||||||
#endif /* LIBC_SCCS and not lint */
|
|
||||||
#include <sys/cdefs.h>
|
|
||||||
|
|
||||||
#include <sys/types.h>
|
#include <sys/types.h>
|
||||||
#include <string.h>
|
#include <string.h>
|
||||||
|
|
||||||
@@ -43,10 +27,7 @@ static char *rcsid = "$OpenBSD: strlcat.c,v 1.2 1999/06/17 16:28:58 millert Exp
|
|||||||
* If retval >= siz, truncation occurred.
|
* If retval >= siz, truncation occurred.
|
||||||
*/
|
*/
|
||||||
size_t
|
size_t
|
||||||
strlcat(dst, src, siz)
|
strlcat(char *dst, const char *src, size_t siz)
|
||||||
char *dst;
|
|
||||||
const char *src;
|
|
||||||
size_t siz;
|
|
||||||
{
|
{
|
||||||
char *d = dst;
|
char *d = dst;
|
||||||
const char *s = src;
|
const char *s = src;
|
||||||
|
@@ -1,33 +1,20 @@
|
|||||||
.\" $OpenBSD: strlcpy.3,v 1.5 1999/06/06 15:17:32 aaron Exp $
|
.\" $OpenBSD: strlcpy.3,v 1.18 2005/08/06 03:24:19 jaredy Exp $
|
||||||
.\"
|
.\"
|
||||||
.\" Copyright (c) 1998 Todd C. Miller <Todd.Miller@courtesan.com>
|
.\" Copyright (c) 1998, 2000 Todd C. Miller <Todd.Miller@courtesan.com>
|
||||||
.\" All rights reserved.
|
|
||||||
.\"
|
.\"
|
||||||
.\" Redistribution and use in source and binary forms, with or without
|
.\" Permission to use, copy, modify, and distribute this software for any
|
||||||
.\" modification, are permitted provided that the following conditions
|
.\" purpose with or without fee is hereby granted, provided that the above
|
||||||
.\" are met:
|
.\" copyright notice and this permission notice appear in all copies.
|
||||||
.\" 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,
|
.\" THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES
|
||||||
.\" INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY
|
.\" WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF
|
||||||
.\" AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL
|
.\" MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR
|
||||||
.\" THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL,
|
.\" ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES
|
||||||
.\" EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO,
|
.\" WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN
|
||||||
.\" PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS;
|
.\" ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF
|
||||||
.\" OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY,
|
.\" OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE.
|
||||||
.\" 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: /repoman/r/ncvs/src/lib/libc/string/strlcpy.3,v 1.13 2004/07/02 23:52:13 ru Exp $
|
.Dd $Mdocdate: May 31 2007 $
|
||||||
.\"
|
|
||||||
.Dd June 22, 1998
|
|
||||||
.Dt STRLCPY 3
|
.Dt STRLCPY 3
|
||||||
.Os
|
.Os
|
||||||
.Sh NAME
|
.Sh NAME
|
||||||
@@ -35,9 +22,10 @@
|
|||||||
.Nm strlcat
|
.Nm strlcat
|
||||||
.Nd size-bounded string copying and concatenation
|
.Nd size-bounded string copying and concatenation
|
||||||
.Sh LIBRARY
|
.Sh LIBRARY
|
||||||
.Lb libc
|
.ds str-Lb-libbsd Utility functions from BSD systems (libbsd, \-lbsd)
|
||||||
|
.Lb libbsd
|
||||||
.Sh SYNOPSIS
|
.Sh SYNOPSIS
|
||||||
.In string.h
|
.In bsd/string.h
|
||||||
.Ft size_t
|
.Ft size_t
|
||||||
.Fn strlcpy "char *dst" "const char *src" "size_t size"
|
.Fn strlcpy "char *dst" "const char *src" "size_t size"
|
||||||
.Ft size_t
|
.Ft size_t
|
||||||
@@ -64,7 +52,7 @@ is larger than 0 or, in the case of
|
|||||||
.Fn strlcat ,
|
.Fn strlcat ,
|
||||||
as long as there is at least one byte free in
|
as long as there is at least one byte free in
|
||||||
.Fa dst ) .
|
.Fa dst ) .
|
||||||
Note that you should include a byte for the NUL in
|
Note that a byte for the NUL should be included in
|
||||||
.Fa size .
|
.Fa size .
|
||||||
Also note that
|
Also note that
|
||||||
.Fn strlcpy
|
.Fn strlcpy
|
||||||
@@ -108,8 +96,7 @@ The
|
|||||||
.Fn strlcpy
|
.Fn strlcpy
|
||||||
and
|
and
|
||||||
.Fn strlcat
|
.Fn strlcat
|
||||||
functions return the total length of the string they tried to
|
functions return the total length of the string they tried to create.
|
||||||
create.
|
|
||||||
For
|
For
|
||||||
.Fn strlcpy
|
.Fn strlcpy
|
||||||
that means the length of
|
that means the length of
|
||||||
@@ -121,10 +108,10 @@ that means the initial length of
|
|||||||
plus
|
plus
|
||||||
the length of
|
the length of
|
||||||
.Fa src .
|
.Fa src .
|
||||||
While this may seem somewhat confusing it was done to make
|
While this may seem somewhat confusing, it was done to make
|
||||||
truncation detection simple.
|
truncation detection simple.
|
||||||
.Pp
|
.Pp
|
||||||
Note however, that if
|
Note, however, that if
|
||||||
.Fn strlcat
|
.Fn strlcat
|
||||||
traverses
|
traverses
|
||||||
.Fa size
|
.Fa size
|
||||||
@@ -168,8 +155,8 @@ if (strlcat(pname, file, sizeof(pname)) >= sizeof(pname))
|
|||||||
goto toolong;
|
goto toolong;
|
||||||
.Ed
|
.Ed
|
||||||
.Pp
|
.Pp
|
||||||
Since we know how many characters we copied the first time, we can
|
Since it is known how many characters were copied the first time, things
|
||||||
speed things up a bit by using a copy instead of an append:
|
can be sped up a bit by using a copy instead of an append:
|
||||||
.Bd -literal -offset indent
|
.Bd -literal -offset indent
|
||||||
char *dir, *file, pname[MAXPATHLEN];
|
char *dir, *file, pname[MAXPATHLEN];
|
||||||
size_t n;
|
size_t n;
|
@@ -1,37 +1,21 @@
|
|||||||
/* $OpenBSD: strlcpy.c,v 1.4 1999/05/01 18:56:41 millert Exp $ */
|
/* $OpenBSD: strlcpy.c,v 1.10 2005/08/08 08:05:37 espie Exp $ */
|
||||||
|
|
||||||
/*
|
/*
|
||||||
* Copyright (c) 1998 Todd C. Miller <Todd.Miller@courtesan.com>
|
* Copyright (c) 1998 Todd C. Miller <Todd.Miller@courtesan.com>
|
||||||
* All rights reserved.
|
|
||||||
*
|
*
|
||||||
* Redistribution and use in source and binary forms, with or without
|
* Permission to use, copy, modify, and distribute this software for any
|
||||||
* modification, are permitted provided that the following conditions
|
* purpose with or without fee is hereby granted, provided that the above
|
||||||
* are met:
|
* copyright notice and this permission notice appear in all copies.
|
||||||
* 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,
|
* THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES
|
||||||
* INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY
|
* WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF
|
||||||
* AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL
|
* MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR
|
||||||
* THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL,
|
* ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES
|
||||||
* EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO,
|
* WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN
|
||||||
* PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS;
|
* ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF
|
||||||
* OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY,
|
* OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE.
|
||||||
* 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 *rcsid = "$OpenBSD: strlcpy.c,v 1.4 1999/05/01 18:56:41 millert Exp $");
|
|
||||||
#endif /* LIBC_SCCS and not lint */
|
|
||||||
#include <sys/cdefs.h>
|
|
||||||
|
|
||||||
#include <sys/types.h>
|
#include <sys/types.h>
|
||||||
#include <string.h>
|
#include <string.h>
|
||||||
|
|
||||||
@@ -40,21 +24,19 @@ static char *rcsid = "$OpenBSD: strlcpy.c,v 1.4 1999/05/01 18:56:41 millert Exp
|
|||||||
* will be copied. Always NUL terminates (unless siz == 0).
|
* will be copied. Always NUL terminates (unless siz == 0).
|
||||||
* Returns strlen(src); if retval >= siz, truncation occurred.
|
* Returns strlen(src); if retval >= siz, truncation occurred.
|
||||||
*/
|
*/
|
||||||
size_t strlcpy(dst, src, siz)
|
size_t
|
||||||
char *dst;
|
strlcpy(char *dst, const char *src, size_t siz)
|
||||||
const char *src;
|
|
||||||
size_t siz;
|
|
||||||
{
|
{
|
||||||
char *d = dst;
|
char *d = dst;
|
||||||
const char *s = src;
|
const char *s = src;
|
||||||
size_t n = siz;
|
size_t n = siz;
|
||||||
|
|
||||||
/* Copy as many bytes as will fit */
|
/* Copy as many bytes as will fit */
|
||||||
if (n != 0 && --n != 0) {
|
if (n != 0) {
|
||||||
do {
|
while (--n != 0) {
|
||||||
if ((*d++ = *s++) == 0)
|
if ((*d++ = *s++) == '\0')
|
||||||
break;
|
break;
|
||||||
} while (--n != 0);
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
/* Not enough room in dst, add NUL and traverse rest of src */
|
/* Not enough room in dst, add NUL and traverse rest of src */
|
||||||
|
@@ -35,9 +35,10 @@
|
|||||||
.Nm strmode
|
.Nm strmode
|
||||||
.Nd convert inode status information into a symbolic string
|
.Nd convert inode status information into a symbolic string
|
||||||
.Sh LIBRARY
|
.Sh LIBRARY
|
||||||
.Lb libc
|
.ds str-Lb-libbsd Utility functions from BSD systems (libbsd, \-lbsd)
|
||||||
|
.Lb libbsd
|
||||||
.Sh SYNOPSIS
|
.Sh SYNOPSIS
|
||||||
.In string.h
|
.In bsd/string.h
|
||||||
.Ft void
|
.Ft void
|
||||||
.Fn strmode "mode_t mode" "char *bp"
|
.Fn strmode "mode_t mode" "char *bp"
|
||||||
.Sh DESCRIPTION
|
.Sh DESCRIPTION
|
159
src/strtonum.3
Normal file
159
src/strtonum.3
Normal file
@@ -0,0 +1,159 @@
|
|||||||
|
.\" Copyright (c) 2004 Ted Unangst
|
||||||
|
.\"
|
||||||
|
.\" Permission to use, copy, modify, and distribute this software for any
|
||||||
|
.\" purpose with or without fee is hereby granted, provided that the above
|
||||||
|
.\" copyright notice and this permission notice appear in all copies.
|
||||||
|
.\"
|
||||||
|
.\" THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES
|
||||||
|
.\" WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF
|
||||||
|
.\" MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR
|
||||||
|
.\" ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES
|
||||||
|
.\" WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN
|
||||||
|
.\" ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF
|
||||||
|
.\" OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE.
|
||||||
|
.\"
|
||||||
|
.\" $OpenBSD: strtonum.3,v 1.12 2005/10/26 11:37:58 jmc Exp $
|
||||||
|
.\" $FreeBSD$
|
||||||
|
.\"
|
||||||
|
.Dd April 29, 2004
|
||||||
|
.Dt STRTONUM 3
|
||||||
|
.Os
|
||||||
|
.Sh NAME
|
||||||
|
.Nm strtonum
|
||||||
|
.Nd "reliably convert string value to an integer"
|
||||||
|
.Sh LIBRARY
|
||||||
|
.ds str-Lb-libbsd Utility functions from BSD systems (libbsd, \-lbsd)
|
||||||
|
.Lb libbsd
|
||||||
|
.Sh SYNOPSIS
|
||||||
|
.In limits.h
|
||||||
|
.In bsd/stdlib.h
|
||||||
|
.Ft long long
|
||||||
|
.Fo strtonum
|
||||||
|
.Fa "const char *nptr"
|
||||||
|
.Fa "long long minval"
|
||||||
|
.Fa "long long maxval"
|
||||||
|
.Fa "const char **errstr"
|
||||||
|
.Fc
|
||||||
|
.Sh DESCRIPTION
|
||||||
|
The
|
||||||
|
.Fn strtonum
|
||||||
|
function converts the string in
|
||||||
|
.Fa nptr
|
||||||
|
to a
|
||||||
|
.Vt "long long"
|
||||||
|
value.
|
||||||
|
The
|
||||||
|
.Fn strtonum
|
||||||
|
function was designed to facilitate safe, robust programming
|
||||||
|
and overcome the shortcomings of the
|
||||||
|
.Xr atoi 3
|
||||||
|
and
|
||||||
|
.Xr strtol 3
|
||||||
|
family of interfaces.
|
||||||
|
.Pp
|
||||||
|
The string may begin with an arbitrary amount of whitespace
|
||||||
|
(as determined by
|
||||||
|
.Xr isspace 3 )
|
||||||
|
followed by a single optional
|
||||||
|
.Ql +
|
||||||
|
or
|
||||||
|
.Ql -
|
||||||
|
sign.
|
||||||
|
.Pp
|
||||||
|
The remainder of the string is converted to a
|
||||||
|
.Vt "long long"
|
||||||
|
value according to base 10.
|
||||||
|
.Pp
|
||||||
|
The value obtained is then checked against the provided
|
||||||
|
.Fa minval
|
||||||
|
and
|
||||||
|
.Fa maxval
|
||||||
|
bounds.
|
||||||
|
If
|
||||||
|
.Fa errstr
|
||||||
|
is non-null,
|
||||||
|
.Fn strtonum
|
||||||
|
stores an error string in
|
||||||
|
.Fa *errstr
|
||||||
|
indicating the failure.
|
||||||
|
.Sh RETURN VALUES
|
||||||
|
The
|
||||||
|
.Fn strtonum
|
||||||
|
function returns the result of the conversion,
|
||||||
|
unless the value would exceed the provided bounds or is invalid.
|
||||||
|
On error, 0 is returned,
|
||||||
|
.Va errno
|
||||||
|
is set, and
|
||||||
|
.Fa errstr
|
||||||
|
will point to an error message.
|
||||||
|
On success,
|
||||||
|
.Fa *errstr
|
||||||
|
will be set to
|
||||||
|
.Dv NULL ;
|
||||||
|
this fact can be used to differentiate
|
||||||
|
a successful return of 0 from an error.
|
||||||
|
.Sh EXAMPLES
|
||||||
|
Using
|
||||||
|
.Fn strtonum
|
||||||
|
correctly is meant to be simpler than the alternative functions.
|
||||||
|
.Bd -literal -offset indent
|
||||||
|
int iterations;
|
||||||
|
const char *errstr;
|
||||||
|
|
||||||
|
iterations = strtonum(optarg, 1, 64, &errstr);
|
||||||
|
if (errstr)
|
||||||
|
errx(1, "number of iterations is %s: %s", errstr, optarg);
|
||||||
|
.Ed
|
||||||
|
.Pp
|
||||||
|
The above example will guarantee that the value of iterations is between
|
||||||
|
1 and 64 (inclusive).
|
||||||
|
.Sh ERRORS
|
||||||
|
.Bl -tag -width Er
|
||||||
|
.It Bq Er ERANGE
|
||||||
|
The given string was out of range.
|
||||||
|
.It Bq Er EINVAL
|
||||||
|
The given string did not consist solely of digit characters.
|
||||||
|
.It Bq Er EINVAL
|
||||||
|
The supplied
|
||||||
|
.Fa minval
|
||||||
|
was larger than
|
||||||
|
.Fa maxval .
|
||||||
|
.El
|
||||||
|
.Pp
|
||||||
|
If an error occurs,
|
||||||
|
.Fa errstr
|
||||||
|
will be set to one of the following strings:
|
||||||
|
.Pp
|
||||||
|
.Bl -tag -width ".Li too large" -compact
|
||||||
|
.It Li "too large"
|
||||||
|
The result was larger than the provided maximum value.
|
||||||
|
.It Li "too small"
|
||||||
|
The result was smaller than the provided minimum value.
|
||||||
|
.It Li invalid
|
||||||
|
The string did not consist solely of digit characters.
|
||||||
|
.El
|
||||||
|
.Sh SEE ALSO
|
||||||
|
.Xr atof 3 ,
|
||||||
|
.Xr atoi 3 ,
|
||||||
|
.Xr atol 3 ,
|
||||||
|
.Xr atoll 3 ,
|
||||||
|
.Xr sscanf 3 ,
|
||||||
|
.Xr strtod 3 ,
|
||||||
|
.Xr strtol 3 ,
|
||||||
|
.Xr strtoul 3
|
||||||
|
.Sh STANDARDS
|
||||||
|
The
|
||||||
|
.Fn strtonum
|
||||||
|
function is a
|
||||||
|
.Bx
|
||||||
|
extension.
|
||||||
|
The existing alternatives, such as
|
||||||
|
.Xr atoi 3
|
||||||
|
and
|
||||||
|
.Xr strtol 3 ,
|
||||||
|
are either impossible or difficult to use safely.
|
||||||
|
.Sh HISTORY
|
||||||
|
The
|
||||||
|
.Fn strtonum
|
||||||
|
function first appeared in
|
||||||
|
.Ox 3.6 .
|
68
src/strtonum.c
Normal file
68
src/strtonum.c
Normal file
@@ -0,0 +1,68 @@
|
|||||||
|
/*-
|
||||||
|
* Copyright (c) 2004 Ted Unangst and Todd Miller
|
||||||
|
* All rights reserved.
|
||||||
|
*
|
||||||
|
* Permission to use, copy, modify, and distribute this software for any
|
||||||
|
* purpose with or without fee is hereby granted, provided that the above
|
||||||
|
* copyright notice and this permission notice appear in all copies.
|
||||||
|
*
|
||||||
|
* THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES
|
||||||
|
* WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF
|
||||||
|
* MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR
|
||||||
|
* ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES
|
||||||
|
* WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN
|
||||||
|
* ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF
|
||||||
|
* OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE.
|
||||||
|
*
|
||||||
|
* $OpenBSD: strtonum.c,v 1.6 2004/08/03 19:38:01 millert Exp $
|
||||||
|
*/
|
||||||
|
|
||||||
|
#include <sys/cdefs.h>
|
||||||
|
__FBSDID("$FreeBSD$");
|
||||||
|
|
||||||
|
#include <errno.h>
|
||||||
|
#include <limits.h>
|
||||||
|
#include <stdlib.h>
|
||||||
|
|
||||||
|
#define INVALID 1
|
||||||
|
#define TOOSMALL 2
|
||||||
|
#define TOOLARGE 3
|
||||||
|
|
||||||
|
long long
|
||||||
|
strtonum(const char *numstr, long long minval, long long maxval,
|
||||||
|
const char **errstrp)
|
||||||
|
{
|
||||||
|
long long ll = 0;
|
||||||
|
char *ep;
|
||||||
|
int error = 0;
|
||||||
|
struct errval {
|
||||||
|
const char *errstr;
|
||||||
|
int err;
|
||||||
|
} ev[4] = {
|
||||||
|
{ NULL, 0 },
|
||||||
|
{ "invalid", EINVAL },
|
||||||
|
{ "too small", ERANGE },
|
||||||
|
{ "too large", ERANGE },
|
||||||
|
};
|
||||||
|
|
||||||
|
ev[0].err = errno;
|
||||||
|
errno = 0;
|
||||||
|
if (minval > maxval)
|
||||||
|
error = INVALID;
|
||||||
|
else {
|
||||||
|
ll = strtoll(numstr, &ep, 10);
|
||||||
|
if (errno == EINVAL || numstr == ep || *ep != '\0')
|
||||||
|
error = INVALID;
|
||||||
|
else if ((ll == LLONG_MIN && errno == ERANGE) || ll < minval)
|
||||||
|
error = TOOSMALL;
|
||||||
|
else if ((ll == LLONG_MAX && errno == ERANGE) || ll > maxval)
|
||||||
|
error = TOOLARGE;
|
||||||
|
}
|
||||||
|
if (errstrp != NULL)
|
||||||
|
*errstrp = ev[error].errstr;
|
||||||
|
errno = ev[error].err;
|
||||||
|
if (error)
|
||||||
|
ll = 0;
|
||||||
|
|
||||||
|
return (ll);
|
||||||
|
}
|
198
src/unvis.3
Normal file
198
src/unvis.3
Normal file
@@ -0,0 +1,198 @@
|
|||||||
|
.\" $OpenBSD: unvis.3,v 1.15 2005/07/22 03:16:58 jaredy Exp $
|
||||||
|
.\"
|
||||||
|
.\" Copyright (c) 1989, 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.
|
||||||
|
.\" 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.
|
||||||
|
.\"
|
||||||
|
.Dd $Mdocdate: May 31 2007 $
|
||||||
|
.Dt UNVIS 3
|
||||||
|
.Os
|
||||||
|
.Sh NAME
|
||||||
|
.Nm unvis ,
|
||||||
|
.Nm strunvis ,
|
||||||
|
.Nm strnunvis
|
||||||
|
.Nd decode a visual representation of characters
|
||||||
|
.Sh LIBRARY
|
||||||
|
.ds str-Lb-libbsd Utility functions from BSD systems (libbsd, \-lbsd)
|
||||||
|
.Lb libbsd
|
||||||
|
.Sh SYNOPSIS
|
||||||
|
.In bsd/vis.h
|
||||||
|
.Ft int
|
||||||
|
.Fn unvis "char *cp" "char c" "int *astate" "int flag"
|
||||||
|
.Ft int
|
||||||
|
.Fn strunvis "char *dst" "char *src"
|
||||||
|
.Ft ssize_t
|
||||||
|
.Fn strnunvis "char *dst" "char *src" "size_t size"
|
||||||
|
.Sh DESCRIPTION
|
||||||
|
The
|
||||||
|
.Fn unvis ,
|
||||||
|
.Fn strunvis
|
||||||
|
and
|
||||||
|
.Fn strnunvis
|
||||||
|
functions are used to decode a visual representation of characters,
|
||||||
|
as produced by the
|
||||||
|
.Xr vis 3
|
||||||
|
function, back into the original form.
|
||||||
|
.Fn unvis
|
||||||
|
is called with successive characters in
|
||||||
|
.Fa c
|
||||||
|
until a valid
|
||||||
|
sequence is recognized, at which time the decoded character is
|
||||||
|
available at the character pointed to by
|
||||||
|
.Fa cp .
|
||||||
|
.Pp
|
||||||
|
.Fn strunvis
|
||||||
|
decodes the characters pointed to by
|
||||||
|
.Fa src
|
||||||
|
into the buffer pointed to by
|
||||||
|
.Fa dst .
|
||||||
|
.Pp
|
||||||
|
.Fn strnunvis
|
||||||
|
decodes the characters pointed to by
|
||||||
|
.Fa src
|
||||||
|
into the buffer pointed to by
|
||||||
|
.Fa dst ,
|
||||||
|
writing a maximum of
|
||||||
|
.Fa size
|
||||||
|
bytes.
|
||||||
|
The
|
||||||
|
.Fn strunvis
|
||||||
|
function simply copies
|
||||||
|
.Fa src
|
||||||
|
to
|
||||||
|
.Fa dst ,
|
||||||
|
decoding any escape sequences along the way,
|
||||||
|
and returns the number of characters placed into
|
||||||
|
.Fa dst ,
|
||||||
|
or \-1 if an
|
||||||
|
invalid escape sequence was detected.
|
||||||
|
The size of
|
||||||
|
.Fa dst
|
||||||
|
should be
|
||||||
|
equal to the size of
|
||||||
|
.Fa src
|
||||||
|
(that is, no expansion takes place during decoding).
|
||||||
|
.Fn strunvis
|
||||||
|
terminates the destination string with a trailing NUL byte;
|
||||||
|
.Fn strnunvis
|
||||||
|
does so if
|
||||||
|
.Fa size
|
||||||
|
is larger than 0.
|
||||||
|
.Pp
|
||||||
|
The
|
||||||
|
.Fn unvis
|
||||||
|
function implements a state machine that can be used to decode an arbitrary
|
||||||
|
stream of bytes.
|
||||||
|
All state associated with the bytes being decoded is stored outside the
|
||||||
|
.Fn unvis
|
||||||
|
function (that is, a pointer to the state is passed in), so
|
||||||
|
calls decoding different streams can be freely intermixed.
|
||||||
|
To start decoding a stream of bytes, first initialize an integer
|
||||||
|
to zero.
|
||||||
|
Call
|
||||||
|
.Fn unvis
|
||||||
|
with each successive byte, along with a pointer
|
||||||
|
to this integer, and a pointer to a destination character.
|
||||||
|
.Sh RETURN VALUES
|
||||||
|
The
|
||||||
|
.Fn unvis
|
||||||
|
function has several return codes that must be handled properly.
|
||||||
|
They are:
|
||||||
|
.Bl -tag -width UNVIS_VALIDPUSH
|
||||||
|
.It Li \&0 (zero)
|
||||||
|
Another character is necessary; nothing has been recognized yet.
|
||||||
|
.It Dv UNVIS_VALID
|
||||||
|
A valid character has been recognized and is available at the location
|
||||||
|
pointed to by
|
||||||
|
.Fa cp .
|
||||||
|
.It Dv UNVIS_VALIDPUSH
|
||||||
|
A valid character has been recognized and is available at the location
|
||||||
|
pointed to by
|
||||||
|
.Fa cp ;
|
||||||
|
however, the character currently passed in should be passed in again.
|
||||||
|
.It Dv UNVIS_NOCHAR
|
||||||
|
A valid sequence was detected, but no character was produced.
|
||||||
|
This return code is necessary to indicate a logical break between characters.
|
||||||
|
.It Dv UNVIS_SYNBAD
|
||||||
|
An invalid escape sequence was detected, or the decoder is in an
|
||||||
|
unknown state.
|
||||||
|
The decoder is placed into the starting state.
|
||||||
|
.El
|
||||||
|
.Pp
|
||||||
|
When all bytes in the stream have been processed, call
|
||||||
|
.Fn unvis
|
||||||
|
one more time with
|
||||||
|
.Fa flag
|
||||||
|
set to
|
||||||
|
.Dv UNVIS_END
|
||||||
|
to extract any remaining character (the character passed in is ignored).
|
||||||
|
.Pp
|
||||||
|
The
|
||||||
|
.Fn strunvis
|
||||||
|
function returns the number of bytes written (not counting
|
||||||
|
the trailing NUL byte) or \-1 if an error occurred.
|
||||||
|
.Pp
|
||||||
|
The
|
||||||
|
.Fn strnunvis
|
||||||
|
function returns the number of bytes (not counting the trailing NUL byte)
|
||||||
|
that would be needed to fully convert the input string, or \-1 if an
|
||||||
|
error occurred.
|
||||||
|
.Sh EXAMPLES
|
||||||
|
The following code fragment illustrates a proper use of
|
||||||
|
.Fn unvis .
|
||||||
|
.Bd -literal -offset indent
|
||||||
|
int state = 0;
|
||||||
|
char out;
|
||||||
|
|
||||||
|
while ((ch = getchar()) != EOF) {
|
||||||
|
again:
|
||||||
|
switch(unvis(&out, ch, &state, 0)) {
|
||||||
|
case 0:
|
||||||
|
case UNVIS_NOCHAR:
|
||||||
|
break;
|
||||||
|
case UNVIS_VALID:
|
||||||
|
(void) putchar(out);
|
||||||
|
break;
|
||||||
|
case UNVIS_VALIDPUSH:
|
||||||
|
(void) putchar(out);
|
||||||
|
goto again;
|
||||||
|
case UNVIS_SYNBAD:
|
||||||
|
(void)fprintf(stderr, "bad sequence!\en");
|
||||||
|
exit(1);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
if (unvis(&out, (char)0, &state, UNVIS_END) == UNVIS_VALID)
|
||||||
|
(void) putchar(out);
|
||||||
|
.Ed
|
||||||
|
.Sh SEE ALSO
|
||||||
|
.Xr unvis 1 ,
|
||||||
|
.Xr vis 1 ,
|
||||||
|
.Xr vis 3
|
||||||
|
.Sh HISTORY
|
||||||
|
The
|
||||||
|
.Fn unvis
|
||||||
|
function first appeared in
|
||||||
|
.Bx 4.4 .
|
48
src/unvis.c
48
src/unvis.c
@@ -235,7 +235,7 @@ strunvis(char *dst, const char *src)
|
|||||||
char *start = dst;
|
char *start = dst;
|
||||||
int state = 0;
|
int state = 0;
|
||||||
|
|
||||||
while ( (c = *src++) ) {
|
while ((c = *src++)) {
|
||||||
again:
|
again:
|
||||||
switch (unvis(dst, c, &state, 0)) {
|
switch (unvis(dst, c, &state, 0)) {
|
||||||
case UNVIS_VALID:
|
case UNVIS_VALID:
|
||||||
@@ -248,6 +248,7 @@ strunvis(char *dst, const char *src)
|
|||||||
case UNVIS_NOCHAR:
|
case UNVIS_NOCHAR:
|
||||||
break;
|
break;
|
||||||
default:
|
default:
|
||||||
|
*dst = '\0';
|
||||||
return (-1);
|
return (-1);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
@@ -257,14 +258,55 @@ strunvis(char *dst, const char *src)
|
|||||||
return (dst - start);
|
return (dst - start);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
ssize_t
|
||||||
|
strnunvis(char *dst, const char *src, size_t sz)
|
||||||
|
{
|
||||||
|
char c, p;
|
||||||
|
char *start = dst, *end = dst + sz - 1;
|
||||||
|
int state = 0;
|
||||||
|
|
||||||
|
if (sz > 0)
|
||||||
|
*end = '\0';
|
||||||
|
while ((c = *src++)) {
|
||||||
|
again:
|
||||||
|
switch (unvis(&p, c, &state, 0)) {
|
||||||
|
case UNVIS_VALID:
|
||||||
|
if (dst < end)
|
||||||
|
*dst = p;
|
||||||
|
dst++;
|
||||||
|
break;
|
||||||
|
case UNVIS_VALIDPUSH:
|
||||||
|
if (dst < end)
|
||||||
|
*dst = p;
|
||||||
|
dst++;
|
||||||
|
goto again;
|
||||||
|
case 0:
|
||||||
|
case UNVIS_NOCHAR:
|
||||||
|
break;
|
||||||
|
default:
|
||||||
|
if (dst <= end)
|
||||||
|
*dst = '\0';
|
||||||
|
return (-1);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
if (unvis(&p, c, &state, UNVIS_END) == UNVIS_VALID) {
|
||||||
|
if (dst < end)
|
||||||
|
*dst = p;
|
||||||
|
dst++;
|
||||||
|
}
|
||||||
|
if (dst <= end)
|
||||||
|
*dst = '\0';
|
||||||
|
return (dst - start);
|
||||||
|
}
|
||||||
|
|
||||||
int
|
int
|
||||||
strunvisx(char *dst, const char *src, int flag)
|
strunvisx(char *dst, const char *src, int flag)
|
||||||
{
|
{
|
||||||
char c;
|
char c;
|
||||||
char *start = dst;
|
char *start = dst;
|
||||||
int state = 0;
|
int state = 0;
|
||||||
|
|
||||||
while ( (c = *src++) ) {
|
while ((c = *src++)) {
|
||||||
again:
|
again:
|
||||||
switch (unvis(dst, c, &state, flag)) {
|
switch (unvis(dst, c, &state, flag)) {
|
||||||
case UNVIS_VALID:
|
case UNVIS_VALID:
|
||||||
|
321
src/vis.3
Normal file
321
src/vis.3
Normal file
@@ -0,0 +1,321 @@
|
|||||||
|
.\" $OpenBSD: vis.3,v 1.23 2005/08/28 19:51:27 millert Exp $
|
||||||
|
.\"
|
||||||
|
.\" Copyright (c) 1989, 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.
|
||||||
|
.\" 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.
|
||||||
|
.\"
|
||||||
|
.Dd $Mdocdate: May 31 2007 $
|
||||||
|
.Dt VIS 3
|
||||||
|
.Os
|
||||||
|
.Sh NAME
|
||||||
|
.Nm vis ,
|
||||||
|
.Nm strvis ,
|
||||||
|
.Nm strnvis ,
|
||||||
|
.Nm strvisx
|
||||||
|
.Nd visually encode characters
|
||||||
|
.Sh LIBRARY
|
||||||
|
.ds str-Lb-libbsd Utility functions from BSD systems (libbsd, \-lbsd)
|
||||||
|
.Lb libbsd
|
||||||
|
.Sh SYNOPSIS
|
||||||
|
.In stdlib.h
|
||||||
|
.In bsd/vis.h
|
||||||
|
.Ft char *
|
||||||
|
.Fn vis "char *dst" "int c" "int flag" "int nextc"
|
||||||
|
.Ft int
|
||||||
|
.Fn strvis "char *dst" "const char *src" "int flag"
|
||||||
|
.Ft int
|
||||||
|
.Fn strnvis "char *dst" "const char *src" "size_t size" "int flag"
|
||||||
|
.Ft int
|
||||||
|
.Fn strvisx "char *dst" "const char *src" "size_t len" "int flag"
|
||||||
|
.Sh DESCRIPTION
|
||||||
|
The
|
||||||
|
.Fn vis
|
||||||
|
function copies into
|
||||||
|
.Fa dst
|
||||||
|
a string which represents the character
|
||||||
|
.Fa c .
|
||||||
|
If
|
||||||
|
.Fa c
|
||||||
|
needs no encoding, it is copied in unaltered.
|
||||||
|
The string is NUL terminated and a pointer to the end of the string is
|
||||||
|
returned.
|
||||||
|
The maximum length of any encoding is four
|
||||||
|
characters (not including the trailing NUL);
|
||||||
|
thus, when
|
||||||
|
encoding a set of characters into a buffer, the size of the buffer should
|
||||||
|
be four times the number of characters encoded, plus one for the trailing
|
||||||
|
NUL.
|
||||||
|
The
|
||||||
|
.Fa flag
|
||||||
|
parameter is used for altering the default range of
|
||||||
|
characters considered for encoding and for altering the visual
|
||||||
|
representation.
|
||||||
|
The additional character,
|
||||||
|
.Fa nextc ,
|
||||||
|
is only used when selecting the
|
||||||
|
.Dv VIS_CSTYLE
|
||||||
|
encoding format (explained below).
|
||||||
|
.Pp
|
||||||
|
The
|
||||||
|
.Fn strvis ,
|
||||||
|
.Fn strnvis
|
||||||
|
and
|
||||||
|
.Fn strvisx
|
||||||
|
functions copy into
|
||||||
|
.Fa dst
|
||||||
|
a visual representation of
|
||||||
|
the string
|
||||||
|
.Fa src .
|
||||||
|
The
|
||||||
|
.Fn strvis
|
||||||
|
function encodes characters from
|
||||||
|
.Fa src
|
||||||
|
up to the first NUL.
|
||||||
|
The
|
||||||
|
.Fn strnvis
|
||||||
|
function encodes characters from
|
||||||
|
.Fa src
|
||||||
|
up to the first NUL or the end of
|
||||||
|
.Fa dst ,
|
||||||
|
as indicated by
|
||||||
|
.Fa size .
|
||||||
|
The
|
||||||
|
.Fn strvisx
|
||||||
|
function encodes exactly
|
||||||
|
.Fa len
|
||||||
|
characters from
|
||||||
|
.Fa src
|
||||||
|
(this
|
||||||
|
is useful for encoding a block of data that may contain NULs).
|
||||||
|
All three forms NUL terminate
|
||||||
|
.Fa dst ,
|
||||||
|
except for
|
||||||
|
.Fn strnvis
|
||||||
|
when
|
||||||
|
.Fa size
|
||||||
|
is zero, in which case
|
||||||
|
.Fa dst
|
||||||
|
is not touched.
|
||||||
|
For
|
||||||
|
.Fn strvis
|
||||||
|
and
|
||||||
|
.Fn strvisx ,
|
||||||
|
the size of
|
||||||
|
.Fa dst
|
||||||
|
must be four times the number
|
||||||
|
of characters encoded from
|
||||||
|
.Fa src
|
||||||
|
(plus one for the NUL).
|
||||||
|
.Fn strvis
|
||||||
|
and
|
||||||
|
.Fn strvisx
|
||||||
|
return the number of characters in
|
||||||
|
.Fa dst
|
||||||
|
(not including the trailing NUL).
|
||||||
|
.Fn strnvis
|
||||||
|
returns the length that
|
||||||
|
.Fa dst
|
||||||
|
would become if it were of unlimited size (similar to
|
||||||
|
.Xr snprintf 3
|
||||||
|
or
|
||||||
|
.Xr strlcpy 3 ) .
|
||||||
|
This can be used to detect truncation but it also means that
|
||||||
|
the return value of
|
||||||
|
.Fn strnvis
|
||||||
|
must not be used without checking it against
|
||||||
|
.Fa size .
|
||||||
|
.Pp
|
||||||
|
The encoding is a unique, invertible representation composed entirely of
|
||||||
|
graphic characters; it can be decoded back into the original form using
|
||||||
|
the
|
||||||
|
.Xr unvis 3
|
||||||
|
or
|
||||||
|
.Xr strunvis 3
|
||||||
|
functions.
|
||||||
|
.Pp
|
||||||
|
There are two parameters that can be controlled: the range of
|
||||||
|
characters that are encoded, and the type
|
||||||
|
of representation used.
|
||||||
|
By default, all non-graphic characters
|
||||||
|
except space, tab, and newline are encoded
|
||||||
|
(see
|
||||||
|
.Xr isgraph 3 ) .
|
||||||
|
The following flags
|
||||||
|
alter this:
|
||||||
|
.Bl -tag -width VIS_WHITEX
|
||||||
|
.It Dv VIS_GLOB
|
||||||
|
Also encode magic characters recognized by
|
||||||
|
.Xr glob 3
|
||||||
|
.Pf ( Ql * ,
|
||||||
|
.Ql \&? ,
|
||||||
|
.Ql \&[ )
|
||||||
|
and
|
||||||
|
.Ql # .
|
||||||
|
.It Dv VIS_SP
|
||||||
|
Also encode space.
|
||||||
|
.It Dv VIS_TAB
|
||||||
|
Also encode tab.
|
||||||
|
.It Dv VIS_NL
|
||||||
|
Also encode newline.
|
||||||
|
.It Dv VIS_WHITE
|
||||||
|
Synonym for
|
||||||
|
.Dv VIS_SP
|
||||||
|
\&|
|
||||||
|
.Dv VIS_TAB
|
||||||
|
\&|
|
||||||
|
.Dv VIS_NL .
|
||||||
|
.It Dv VIS_SAFE
|
||||||
|
Only encode
|
||||||
|
.Dq unsafe
|
||||||
|
characters.
|
||||||
|
These are control characters which may cause common terminals to perform
|
||||||
|
unexpected functions.
|
||||||
|
Currently this form allows space,
|
||||||
|
tab, newline, backspace, bell, and return -- in addition
|
||||||
|
to all graphic characters -- unencoded.
|
||||||
|
.El
|
||||||
|
.Pp
|
||||||
|
There are three forms of encoding.
|
||||||
|
All forms use the backslash
|
||||||
|
.Ql \e
|
||||||
|
character to introduce a special
|
||||||
|
sequence; two backslashes are used to represent a real backslash.
|
||||||
|
These are the visual formats:
|
||||||
|
.Bl -tag -width VIS_CSTYLE
|
||||||
|
.It (default)
|
||||||
|
Use an
|
||||||
|
.Ql M
|
||||||
|
to represent meta characters (characters with the 8th
|
||||||
|
bit set), and use a caret
|
||||||
|
.Ql ^
|
||||||
|
to represent control characters (see
|
||||||
|
.Xr iscntrl 3 ) .
|
||||||
|
The following formats are used:
|
||||||
|
.Bl -tag -width xxxxx
|
||||||
|
.It Dv \e^C
|
||||||
|
Represents the control character
|
||||||
|
.Ql C .
|
||||||
|
Spans characters
|
||||||
|
.Ql \e000
|
||||||
|
through
|
||||||
|
.Ql \e037 ,
|
||||||
|
and
|
||||||
|
.Ql \e177
|
||||||
|
(as
|
||||||
|
.Ql \e^? ) .
|
||||||
|
.It Dv \eM-C
|
||||||
|
Represents character
|
||||||
|
.Ql C
|
||||||
|
with the 8th bit set.
|
||||||
|
Spans characters
|
||||||
|
.Ql \e241
|
||||||
|
through
|
||||||
|
.Ql \e376 .
|
||||||
|
.It Dv \eM^C
|
||||||
|
Represents control character
|
||||||
|
.Ql C
|
||||||
|
with the 8th bit set.
|
||||||
|
Spans characters
|
||||||
|
.Ql \e200
|
||||||
|
through
|
||||||
|
.Ql \e237 ,
|
||||||
|
and
|
||||||
|
.Ql \e377
|
||||||
|
(as
|
||||||
|
.Ql \eM^? ) .
|
||||||
|
.It Dv \e040
|
||||||
|
Represents
|
||||||
|
.Tn ASCII
|
||||||
|
space.
|
||||||
|
.It Dv \e240
|
||||||
|
Represents Meta-space.
|
||||||
|
.El
|
||||||
|
.Pp
|
||||||
|
.It Dv VIS_CSTYLE
|
||||||
|
Use C-style backslash sequences to represent standard non-printable
|
||||||
|
characters.
|
||||||
|
The following sequences are used to represent the indicated characters:
|
||||||
|
.Bd -unfilled -offset indent
|
||||||
|
.Li \ea Tn - BEL No (007)
|
||||||
|
.Li \eb Tn - BS No (010)
|
||||||
|
.Li \ef Tn - NP No (014)
|
||||||
|
.Li \en Tn - NL No (012)
|
||||||
|
.Li \er Tn - CR No (015)
|
||||||
|
.Li \es Tn - SP No (040)
|
||||||
|
.Li \et Tn - HT No (011)
|
||||||
|
.Li \ev Tn - VT No (013)
|
||||||
|
.Li \e0 Tn - NUL No (000)
|
||||||
|
.Ed
|
||||||
|
.Pp
|
||||||
|
When using this format, the
|
||||||
|
.Fa nextc
|
||||||
|
parameter is looked at to determine
|
||||||
|
if a NUL character can be encoded as
|
||||||
|
.Ql \e0
|
||||||
|
instead of
|
||||||
|
.Ql \e000 .
|
||||||
|
If
|
||||||
|
.Fa nextc
|
||||||
|
is an octal digit, the latter representation is used to
|
||||||
|
avoid ambiguity.
|
||||||
|
.It Dv VIS_OCTAL
|
||||||
|
Use a three digit octal sequence.
|
||||||
|
The form is
|
||||||
|
.Ql \eddd
|
||||||
|
where
|
||||||
|
.Ar d
|
||||||
|
represents an octal digit.
|
||||||
|
.El
|
||||||
|
.Pp
|
||||||
|
There is one additional flag,
|
||||||
|
.Dv VIS_NOSLASH ,
|
||||||
|
which inhibits the
|
||||||
|
doubling of backslashes and the backslash before the default
|
||||||
|
format (that is, control characters are represented by
|
||||||
|
.Ql ^C
|
||||||
|
and
|
||||||
|
meta characters as
|
||||||
|
.Ql M-C ) .
|
||||||
|
With this flag set, the encoding is
|
||||||
|
ambiguous and non-invertible.
|
||||||
|
.Sh SEE ALSO
|
||||||
|
.Xr unvis 1 ,
|
||||||
|
.Xr vis 1 ,
|
||||||
|
.Xr snprintf 3 ,
|
||||||
|
.Xr strlcpy 3 ,
|
||||||
|
.Xr unvis 3
|
||||||
|
.Sh HISTORY
|
||||||
|
The
|
||||||
|
.Fn vis ,
|
||||||
|
.Fn strvis
|
||||||
|
and
|
||||||
|
.Fn strvisx
|
||||||
|
functions first appeared in
|
||||||
|
.Bx 4.4 .
|
||||||
|
The
|
||||||
|
.Fn strnvis
|
||||||
|
function first appeared in
|
||||||
|
.Ox 2.9 .
|
77
src/vis.c
77
src/vis.c
@@ -1,3 +1,4 @@
|
|||||||
|
/* $OpenBSD: vis.c,v 1.18 2005/08/29 18:38:41 otto Exp $ */
|
||||||
/*-
|
/*-
|
||||||
* Copyright (c) 1989, 1993
|
* Copyright (c) 1989, 1993
|
||||||
* The Regents of the University of California. All rights reserved.
|
* The Regents of the University of California. All rights reserved.
|
||||||
@@ -30,19 +31,27 @@
|
|||||||
#include <sys/types.h>
|
#include <sys/types.h>
|
||||||
#include <limits.h>
|
#include <limits.h>
|
||||||
#include <ctype.h>
|
#include <ctype.h>
|
||||||
|
#include <string.h>
|
||||||
#include <stdio.h>
|
#include <stdio.h>
|
||||||
#include <vis.h>
|
#include <vis.h>
|
||||||
|
|
||||||
#define isoctal(c) (((u_char)(c)) >= '0' && ((u_char)(c)) <= '7')
|
#define isoctal(c) (((u_char)(c)) >= '0' && ((u_char)(c)) <= '7')
|
||||||
|
#define isvisible(c) \
|
||||||
|
(((u_int)(c) <= UCHAR_MAX && isascii((u_char)(c)) && \
|
||||||
|
(((c) != '*' && (c) != '?' && (c) != '[' && (c) != '#') || \
|
||||||
|
(flag & VIS_GLOB) == 0) && isgraph((u_char)(c))) || \
|
||||||
|
((flag & VIS_SP) == 0 && (c) == ' ') || \
|
||||||
|
((flag & VIS_TAB) == 0 && (c) == '\t') || \
|
||||||
|
((flag & VIS_NL) == 0 && (c) == '\n') || \
|
||||||
|
((flag & VIS_SAFE) && ((c) == '\b' || \
|
||||||
|
(c) == '\007' || (c) == '\r' || \
|
||||||
|
isgraph((u_char)(c)))))
|
||||||
|
|
||||||
/*
|
/*
|
||||||
* vis - visually encode characters
|
* vis - visually encode characters
|
||||||
*/
|
*/
|
||||||
char *
|
char *
|
||||||
vis(dst, c, flag, nextc)
|
vis(char *dst, int c, int flag, int nextc)
|
||||||
char *dst;
|
|
||||||
int c, nextc;
|
|
||||||
int flag;
|
|
||||||
{
|
{
|
||||||
c = (unsigned char)c;
|
c = (unsigned char)c;
|
||||||
|
|
||||||
@@ -149,20 +158,20 @@ done:
|
|||||||
}
|
}
|
||||||
|
|
||||||
/*
|
/*
|
||||||
* strvis, strvisx - visually encode characters from src into dst
|
* strvis, strnvis, strvisx - visually encode characters from src into dst
|
||||||
*
|
*
|
||||||
* Dst must be 4 times the size of src to account for possible
|
* Dst must be 4 times the size of src to account for possible
|
||||||
* expansion. The length of dst, not including the trailing NUL,
|
* expansion. The length of dst, not including the trailing NUL,
|
||||||
* is returned.
|
* is returned.
|
||||||
*
|
*
|
||||||
|
* Strnvis will write no more than siz-1 bytes (and will NULL terminate).
|
||||||
|
* The number of bytes needed to fully encode the string is returned.
|
||||||
|
*
|
||||||
* Strvisx encodes exactly len bytes from src into dst.
|
* Strvisx encodes exactly len bytes from src into dst.
|
||||||
* This is useful for encoding a block of data.
|
* This is useful for encoding a block of data.
|
||||||
*/
|
*/
|
||||||
int
|
int
|
||||||
strvis(dst, src, flag)
|
strvis(char *dst, const char *src, int flag)
|
||||||
char *dst;
|
|
||||||
const char *src;
|
|
||||||
int flag;
|
|
||||||
{
|
{
|
||||||
char c;
|
char c;
|
||||||
char *start;
|
char *start;
|
||||||
@@ -174,11 +183,51 @@ strvis(dst, src, flag)
|
|||||||
}
|
}
|
||||||
|
|
||||||
int
|
int
|
||||||
strvisx(dst, src, len, flag)
|
strnvis(char *dst, const char *src, size_t siz, int flag)
|
||||||
char *dst;
|
{
|
||||||
const char *src;
|
char *start, *end;
|
||||||
size_t len;
|
char tbuf[5];
|
||||||
int flag;
|
int c, i;
|
||||||
|
|
||||||
|
i = 0;
|
||||||
|
for (start = dst, end = start + siz - 1; (c = *src) && dst < end; ) {
|
||||||
|
if (isvisible(c)) {
|
||||||
|
i = 1;
|
||||||
|
*dst++ = c;
|
||||||
|
if (c == '\\' && (flag & VIS_NOSLASH) == 0) {
|
||||||
|
/* need space for the extra '\\' */
|
||||||
|
if (dst < end)
|
||||||
|
*dst++ = '\\';
|
||||||
|
else {
|
||||||
|
dst--;
|
||||||
|
i = 2;
|
||||||
|
break;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
src++;
|
||||||
|
} else {
|
||||||
|
i = vis(tbuf, c, flag, *++src) - tbuf;
|
||||||
|
if (dst + i <= end) {
|
||||||
|
memcpy(dst, tbuf, i);
|
||||||
|
dst += i;
|
||||||
|
} else {
|
||||||
|
src--;
|
||||||
|
break;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
if (siz > 0)
|
||||||
|
*dst = '\0';
|
||||||
|
if (dst + i > end) {
|
||||||
|
/* adjust return value for truncation */
|
||||||
|
while ((c = *src))
|
||||||
|
dst += vis(tbuf, c, flag, *++src) - tbuf;
|
||||||
|
}
|
||||||
|
return (dst - start);
|
||||||
|
}
|
||||||
|
|
||||||
|
int
|
||||||
|
strvisx(char *dst, const char *src, size_t len, int flag)
|
||||||
{
|
{
|
||||||
int c;
|
int c;
|
||||||
char *start;
|
char *start;
|
||||||
|
Reference in New Issue
Block a user