diff options
Diffstat (limited to '')
201 files changed, 24687 insertions, 0 deletions
diff --git a/src/lib/libc/include/namespace.h b/src/lib/libc/include/namespace.h new file mode 100644 index 0000000000..803657e006 --- /dev/null +++ b/src/lib/libc/include/namespace.h | |||
| @@ -0,0 +1,18 @@ | |||
| 1 | /* $NetBSD: namespace.h,v 1.2 1995/02/27 13:02:12 cgd Exp $ */ | ||
| 2 | |||
| 3 | #define catclose _catclose | ||
| 4 | #define catgets _catgets | ||
| 5 | #define catopen _catopen | ||
| 6 | #define err _err | ||
| 7 | #define errx _errx | ||
| 8 | #define strtoq _strtoq | ||
| 9 | #define strtouq _strtouq | ||
| 10 | #define sys_errlist _sys_errlist | ||
| 11 | #define sys_nerr _sys_nerr | ||
| 12 | #define sys_siglist _sys_siglist | ||
| 13 | #define verr _verr | ||
| 14 | #define verrx _verrx | ||
| 15 | #define vwarn _vwarn | ||
| 16 | #define vwarnx _vwarnx | ||
| 17 | #define warn _warn | ||
| 18 | #define warnx _warnx | ||
diff --git a/src/lib/libc/net/Makefile.inc b/src/lib/libc/net/Makefile.inc new file mode 100644 index 0000000000..2d220067e4 --- /dev/null +++ b/src/lib/libc/net/Makefile.inc | |||
| @@ -0,0 +1,46 @@ | |||
| 1 | # $NetBSD: Makefile.inc,v 1.23 1995/03/02 09:09:07 chopps Exp $ | ||
| 2 | # @(#)Makefile.inc 8.2 (Berkeley) 9/5/93 | ||
| 3 | |||
| 4 | # net sources | ||
| 5 | .PATH: ${.CURDIR}/arch/${MACHINE_ARCH}/net ${.CURDIR}/net | ||
| 6 | |||
| 7 | SRCS+= gethostnamadr.c getnetbyaddr.c getnetbyname.c getnetent.c \ | ||
| 8 | getproto.c getprotoent.c getprotoname.c getservbyname.c \ | ||
| 9 | getservbyport.c getservent.c herror.c inet_addr.c inet_lnaof.c \ | ||
| 10 | inet_makeaddr.c inet_netof.c inet_network.c inet_ntoa.c \ | ||
| 11 | iso_addr.c linkaddr.c ns_addr.c ns_ntoa.c rcmd.c recv.c res_comp.c \ | ||
| 12 | res_debug.c res_init.c res_mkquery.c res_query.c res_send.c \ | ||
| 13 | send.c sethostent.c ethers.c | ||
| 14 | |||
| 15 | # machine-dependent net sources | ||
| 16 | # m-d Makefile.inc must include sources for: | ||
| 17 | # htonl() htons() ntohl() ntohs() | ||
| 18 | |||
| 19 | .include "${.CURDIR}/arch/${MACHINE_ARCH}/net/Makefile.inc" | ||
| 20 | |||
| 21 | MAN+= byteorder.3 gethostbyname.3 getnetent.3 getprotoent.3 getservent.3 \ | ||
| 22 | inet.3 linkaddr.3 ns.3 rcmd.3 resolver.3 ethers.3 | ||
| 23 | |||
| 24 | MLINKS+=byteorder.3 htonl.3 byteorder.3 htons.3 byteorder.3 ntohl.3 \ | ||
| 25 | byteorder.3 ntohs.3 | ||
| 26 | MLINKS+=ethers.3 ether_aton.3 ethers.3 ether_hostton.3 ethers.3 ether_line.3 \ | ||
| 27 | ethers.3 ether_ntoa.3 ethers.3 ether_ntohost.3 | ||
| 28 | MLINKS+=gethostbyname.3 endhostent.3 gethostbyname.3 gethostbyaddr.3 \ | ||
| 29 | gethostbyname.3 sethostent.3 gethostbyname.3 gethostent.3 \ | ||
| 30 | gethostbyname.3 herror.3 | ||
| 31 | MLINKS+=getnetent.3 endnetent.3 getnetent.3 getnetbyaddr.3 \ | ||
| 32 | getnetent.3 getnetbyname.3 getnetent.3 setnetent.3 | ||
| 33 | MLINKS+=getprotoent.3 endprotoent.3 getprotoent.3 getprotobyname.3 \ | ||
| 34 | getprotoent.3 getprotobynumber.3 getprotoent.3 setprotoent.3 | ||
| 35 | MLINKS+=getservent.3 endservent.3 getservent.3 getservbyname.3 \ | ||
| 36 | getservent.3 getservbyport.3 getservent.3 setservent.3 | ||
| 37 | MLINKS+=inet.3 addr.3 inet.3 inet_addr.3 inet.3 inet_aton.3 \ | ||
| 38 | inet.3 inet_lnaof.3 inet.3 inet_makeaddr.3 inet.3 inet_netof.3 \ | ||
| 39 | inet.3 inet_network.3 inet.3 inet_ntoa.3 inet.3 network.3 \ | ||
| 40 | inet.3 ntoa.3 | ||
| 41 | MLINKS+=linkaddr.3 linkntoa.3 | ||
| 42 | MLINKS+=ns.3 ns_addr.3 ns.3 ns_ntoa.3 | ||
| 43 | MLINKS+=rcmd.3 iruserok.3 rcmd.3 rresvport.3 rcmd.3 ruserok.3 | ||
| 44 | MLINKS+=resolver.3 dn_comp.3 resolver.3 dn_expand.3 resolver.3 res_init.3 \ | ||
| 45 | resolver.3 res_mkquery.3 resolver.3 res_send.3 resolver.3 res_query.3 \ | ||
| 46 | resolver.3 res_search.3 | ||
diff --git a/src/lib/libc/net/byteorder.3 b/src/lib/libc/net/byteorder.3 new file mode 100644 index 0000000000..701a69f688 --- /dev/null +++ b/src/lib/libc/net/byteorder.3 | |||
| @@ -0,0 +1,78 @@ | |||
| 1 | .\" $NetBSD: byteorder.3,v 1.3 1995/02/25 06:20:27 cgd Exp $ | ||
| 2 | .\" | ||
| 3 | .\" Copyright (c) 1983, 1991, 1993 | ||
| 4 | .\" The Regents of the University of California. All rights reserved. | ||
| 5 | .\" | ||
| 6 | .\" Redistribution and use in source and binary forms, with or without | ||
| 7 | .\" modification, are permitted provided that the following conditions | ||
| 8 | .\" are met: | ||
| 9 | .\" 1. Redistributions of source code must retain the above copyright | ||
| 10 | .\" notice, this list of conditions and the following disclaimer. | ||
| 11 | .\" 2. Redistributions in binary form must reproduce the above copyright | ||
| 12 | .\" notice, this list of conditions and the following disclaimer in the | ||
| 13 | .\" documentation and/or other materials provided with the distribution. | ||
| 14 | .\" 3. All advertising materials mentioning features or use of this software | ||
| 15 | .\" must display the following acknowledgement: | ||
| 16 | .\" This product includes software developed by the University of | ||
| 17 | .\" California, Berkeley and its contributors. | ||
| 18 | .\" 4. Neither the name of the University nor the names of its contributors | ||
| 19 | .\" may be used to endorse or promote products derived from this software | ||
| 20 | .\" without specific prior written permission. | ||
| 21 | .\" | ||
| 22 | .\" THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND | ||
| 23 | .\" ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE | ||
| 24 | .\" IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE | ||
| 25 | .\" ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE | ||
| 26 | .\" FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL | ||
| 27 | .\" DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS | ||
| 28 | .\" OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) | ||
| 29 | .\" HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT | ||
| 30 | .\" LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY | ||
| 31 | .\" OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF | ||
| 32 | .\" SUCH DAMAGE. | ||
| 33 | .\" | ||
| 34 | .\" @(#)byteorder.3 8.1 (Berkeley) 6/4/93 | ||
| 35 | .\" | ||
| 36 | .Dd June 4, 1993 | ||
| 37 | .Dt BYTEORDER 3 | ||
| 38 | .Os BSD 4.2 | ||
| 39 | .Sh NAME | ||
| 40 | .Nm htonl , | ||
| 41 | .Nm htons , | ||
| 42 | .Nm ntohl , | ||
| 43 | .Nm ntohs | ||
| 44 | .Nd convert values between host and network byte order | ||
| 45 | .Sh SYNOPSIS | ||
| 46 | .Fd #include <sys/param.h> | ||
| 47 | .Ft u_long | ||
| 48 | .Fn htonl "u_long hostlong" | ||
| 49 | .Ft u_short | ||
| 50 | .Fn htons "u_short hostshort" | ||
| 51 | .Ft u_long | ||
| 52 | .Fn ntohl "u_long netlong" | ||
| 53 | .Ft u_short | ||
| 54 | .Fn ntohs "u_short netshort" | ||
| 55 | .Sh DESCRIPTION | ||
| 56 | These routines convert 16 and 32 bit quantities between network | ||
| 57 | byte order and host byte order. | ||
| 58 | On machines which have a byte order which is the same as the network | ||
| 59 | order, routines are defined as null macros. | ||
| 60 | .Pp | ||
| 61 | These routines are most often used in conjunction with Internet | ||
| 62 | addresses and ports as returned by | ||
| 63 | .Xr gethostbyname 3 | ||
| 64 | and | ||
| 65 | .Xr getservent 3 . | ||
| 66 | .Sh SEE ALSO | ||
| 67 | .Xr gethostbyname 3 , | ||
| 68 | .Xr getservent 3 | ||
| 69 | .Sh HISTORY | ||
| 70 | The | ||
| 71 | .Nm byteorder | ||
| 72 | functions appeared in | ||
| 73 | .Bx 4.2 . | ||
| 74 | .Sh BUGS | ||
| 75 | On the | ||
| 76 | .Tn VAX | ||
| 77 | bytes are handled backwards from most everyone else in | ||
| 78 | the world. This is not expected to be fixed in the near future. | ||
diff --git a/src/lib/libc/net/ethers.3 b/src/lib/libc/net/ethers.3 new file mode 100644 index 0000000000..81e6c65935 --- /dev/null +++ b/src/lib/libc/net/ethers.3 | |||
| @@ -0,0 +1,102 @@ | |||
| 1 | .\" | ||
| 2 | .\" Written by roland@frob.com. Public domain. | ||
| 3 | .\" | ||
| 4 | .Dd December 16, 1993 | ||
| 5 | .Dt ETHERS 3 | ||
| 6 | .Os NetBSD | ||
| 7 | .Sh NAME | ||
| 8 | .Nm ether_ntoa , | ||
| 9 | .Nm ether_addr , | ||
| 10 | .Nm ether_ntohost , | ||
| 11 | .Nm ether_hostton , | ||
| 12 | .Nm ether_line , | ||
| 13 | .Nd get ethers entry | ||
| 14 | .Sh SYNOPSIS | ||
| 15 | .Fd #include <netinet/if_ether.h> | ||
| 16 | .Ft char * | ||
| 17 | .Fn ether_ntoa "struct ether_addr *e" | ||
| 18 | .Ft struct ether_addr * | ||
| 19 | .Fn ether_aton "char *s" | ||
| 20 | .Fn ether_ntohost "char *hostname" "struct ether_addr *e" | ||
| 21 | .Fn ether_hostton "char *hostname" "struct ether_addr *e" | ||
| 22 | .Fn ether_line "char *l" "struct ether_addr *e" "char *hostname" | ||
| 23 | .Sh DESCRIPTION | ||
| 24 | Ethernet addresses are represented by the | ||
| 25 | following structure: | ||
| 26 | .Bd -literal -offset indent | ||
| 27 | struct ether_addr { | ||
| 28 | u_char ether_addr_octet[6]; | ||
| 29 | }; | ||
| 30 | .Ed | ||
| 31 | .Pp | ||
| 32 | The | ||
| 33 | .Fn ether_ntoa | ||
| 34 | function converts this structure into an ASCII string of the form | ||
| 35 | ``xx:xx:xx:xx:xx:xx'', consisting of 6 hexadecimal numbers separated | ||
| 36 | by colons. It returns a pointer to a static buffer that is reused for | ||
| 37 | each call. | ||
| 38 | The | ||
| 39 | .Fn ether_aton | ||
| 40 | converts an ASCII string of the same form and to a structure | ||
| 41 | containing the 6 octets of the address. It returns a pointer to a | ||
| 42 | static structure that is reused for each call. | ||
| 43 | .Pp | ||
| 44 | The | ||
| 45 | .Fn ether_ntohost | ||
| 46 | and | ||
| 47 | .Fn ether_hostton | ||
| 48 | functions interrogate the data base mapping host names to Ethernet | ||
| 49 | addresses, | ||
| 50 | .Pa /etc/ethers . | ||
| 51 | The | ||
| 52 | .Fn ether_ntohost | ||
| 53 | function looks up the given Ethernet address and writes the associated | ||
| 54 | host name into the character buffer passed. | ||
| 55 | The | ||
| 56 | .Fn ether_hostton | ||
| 57 | function looks up the given host name and writes the associated | ||
| 58 | Ethernet address into the structure passed. Both functions return | ||
| 59 | zero if they find the requested host name or address, and -1 if not. | ||
| 60 | Each call reads | ||
| 61 | .Pa /etc/ethers | ||
| 62 | from the beginning; if a + appears alone on a line in the file, then | ||
| 63 | .Fn ether_hostton | ||
| 64 | will consult the | ||
| 65 | .Pa ethers.byname | ||
| 66 | YP map, and | ||
| 67 | .Fn ether_ntohost | ||
| 68 | will consult the | ||
| 69 | .Pa ethers.byaddr | ||
| 70 | YP map. | ||
| 71 | .Pp | ||
| 72 | The | ||
| 73 | .Fn ether_line | ||
| 74 | function parses a line from the | ||
| 75 | .Pa /etc/ethers | ||
| 76 | file and fills in the passed ``struct ether_addr'' and character | ||
| 77 | buffer with the Ethernet address and host name on the line. It | ||
| 78 | returns zero if the line was successfully parsed and -1 if not. | ||
| 79 | .Sh FILES | ||
| 80 | .Bl -tag -width /etc/ethers -compact | ||
| 81 | .It Pa /etc/ethers | ||
| 82 | .El | ||
| 83 | .Sh SEE ALSO | ||
| 84 | .Xr ethers 5 | ||
| 85 | .Sh HISTORY | ||
| 86 | The | ||
| 87 | .Fn ether_ntoa , | ||
| 88 | .Fn ether_aton , | ||
| 89 | .Fn ether_ntohost , | ||
| 90 | .Fn ether_hostton , | ||
| 91 | and | ||
| 92 | .Fn ether_line | ||
| 93 | functions were adopted from SunOS and appeared in | ||
| 94 | NetBSD 0.9b. | ||
| 95 | .Sh BUGS | ||
| 96 | The data space used by these functions is static; if future use | ||
| 97 | requires the data, it should be copied before any subsequent calls to | ||
| 98 | these functions overwrite it. There is no way to restrict how many | ||
| 99 | character will be written into the host name buffer passed. A very | ||
| 100 | long line in | ||
| 101 | .Pa /etc/ethers | ||
| 102 | could overflow your buffer. | ||
diff --git a/src/lib/libc/net/ethers.c b/src/lib/libc/net/ethers.c new file mode 100644 index 0000000000..0f32b9b71b --- /dev/null +++ b/src/lib/libc/net/ethers.c | |||
| @@ -0,0 +1,178 @@ | |||
| 1 | /* $NetBSD: ethers.c,v 1.5 1995/02/25 06:20:28 cgd Exp $ */ | ||
| 2 | |||
| 3 | /* | ||
| 4 | * ethers(3N) a la Sun. | ||
| 5 | * | ||
| 6 | * Written by Roland McGrath <roland@frob.com> 10/14/93. | ||
| 7 | * Public domain. | ||
| 8 | */ | ||
| 9 | |||
| 10 | #include <sys/types.h> | ||
| 11 | #include <sys/socket.h> | ||
| 12 | #include <net/if.h> | ||
| 13 | #include <netinet/in.h> | ||
| 14 | #include <netinet/if_ether.h> | ||
| 15 | #include <sys/param.h> | ||
| 16 | #include <paths.h> | ||
| 17 | #include <errno.h> | ||
| 18 | #include <stdio.h> | ||
| 19 | #include <stdlib.h> | ||
| 20 | #include <string.h> | ||
| 21 | |||
| 22 | #ifndef _PATH_ETHERS | ||
| 23 | #define _PATH_ETHERS "/etc/ethers" | ||
| 24 | #endif | ||
| 25 | |||
| 26 | char * | ||
| 27 | ether_ntoa(e) | ||
| 28 | struct ether_addr *e; | ||
| 29 | { | ||
| 30 | static char a[] = "xx:xx:xx:xx:xx:xx"; | ||
| 31 | |||
| 32 | sprintf(a, "%02x:%02x:%02x:%02x:%02x:%02x", | ||
| 33 | e->ether_addr_octet[0], e->ether_addr_octet[1], | ||
| 34 | e->ether_addr_octet[2], e->ether_addr_octet[3], | ||
| 35 | e->ether_addr_octet[4], e->ether_addr_octet[5]); | ||
| 36 | return a; | ||
| 37 | } | ||
| 38 | |||
| 39 | struct ether_addr * | ||
| 40 | ether_aton(s) | ||
| 41 | char *s; | ||
| 42 | { | ||
| 43 | static struct ether_addr n; | ||
| 44 | u_int i[6]; | ||
| 45 | |||
| 46 | if (sscanf(s, " %x:%x:%x:%x:%x:%x ", &i[0], &i[1], | ||
| 47 | &i[2], &i[3], &i[4], &i[5]) == 6) { | ||
| 48 | n.ether_addr_octet[0] = (u_char)i[0]; | ||
| 49 | n.ether_addr_octet[1] = (u_char)i[1]; | ||
| 50 | n.ether_addr_octet[2] = (u_char)i[2]; | ||
| 51 | n.ether_addr_octet[3] = (u_char)i[3]; | ||
| 52 | n.ether_addr_octet[4] = (u_char)i[4]; | ||
| 53 | n.ether_addr_octet[5] = (u_char)i[5]; | ||
| 54 | return &n; | ||
| 55 | } | ||
| 56 | return NULL; | ||
| 57 | } | ||
| 58 | |||
| 59 | ether_ntohost(hostname, e) | ||
| 60 | char *hostname; | ||
| 61 | struct ether_addr *e; | ||
| 62 | { | ||
| 63 | FILE *f; | ||
| 64 | char buf[BUFSIZ]; | ||
| 65 | struct ether_addr try; | ||
| 66 | |||
| 67 | #ifdef YP | ||
| 68 | char trybuf[sizeof "xx:xx:xx:xx:xx:xx"]; | ||
| 69 | int trylen; | ||
| 70 | |||
| 71 | sprintf(trybuf, "%x:%x:%x:%x:%x:%x", | ||
| 72 | e->ether_addr_octet[0], e->ether_addr_octet[1], | ||
| 73 | e->ether_addr_octet[2], e->ether_addr_octet[3], | ||
| 74 | e->ether_addr_octet[4], e->ether_addr_octet[5]); | ||
| 75 | trylen = strlen(trybuf); | ||
| 76 | #endif | ||
| 77 | |||
| 78 | f = fopen(_PATH_ETHERS, "r"); | ||
| 79 | if (f==NULL) | ||
| 80 | return -1; | ||
| 81 | while (fgets(buf, sizeof buf, f)) { | ||
| 82 | #ifdef YP | ||
| 83 | /* A + in the file means try YP now. */ | ||
| 84 | if (!strncmp(buf, "+\n", sizeof buf)) { | ||
| 85 | char *ypbuf, *ypdom; | ||
| 86 | int ypbuflen; | ||
| 87 | |||
| 88 | if (yp_get_default_domain(&ypdom)) | ||
| 89 | continue; | ||
| 90 | if (yp_match(ypdom, "ethers.byaddr", trybuf, | ||
| 91 | trylen, &ypbuf, &ypbuflen)) | ||
| 92 | continue; | ||
| 93 | if (ether_line(ypbuf, &try, hostname) == 0) { | ||
| 94 | free(ypbuf); | ||
| 95 | (void)fclose(f); | ||
| 96 | return 0; | ||
| 97 | } | ||
| 98 | free(ypbuf); | ||
| 99 | continue; | ||
| 100 | } | ||
| 101 | #endif | ||
| 102 | if (ether_line(buf, &try, hostname) == 0 && | ||
| 103 | bcmp((char *)&try, (char *)e, sizeof try) == 0) { | ||
| 104 | (void)fclose(f); | ||
| 105 | return 0; | ||
| 106 | } | ||
| 107 | } | ||
| 108 | (void)fclose(f); | ||
| 109 | errno = ENOENT; | ||
| 110 | return -1; | ||
| 111 | } | ||
| 112 | |||
| 113 | ether_hostton(hostname, e) | ||
| 114 | char *hostname; | ||
| 115 | struct ether_addr *e; | ||
| 116 | { | ||
| 117 | FILE *f; | ||
| 118 | char buf[BUFSIZ]; | ||
| 119 | char try[MAXHOSTNAMELEN]; | ||
| 120 | #ifdef YP | ||
| 121 | int hostlen = strlen(hostname); | ||
| 122 | #endif | ||
| 123 | |||
| 124 | f = fopen(_PATH_ETHERS, "r"); | ||
| 125 | if (f==NULL) | ||
| 126 | return -1; | ||
| 127 | |||
| 128 | while (fgets(buf, sizeof buf, f)) { | ||
| 129 | #ifdef YP | ||
| 130 | /* A + in the file means try YP now. */ | ||
| 131 | if (!strncmp(buf, "+\n", sizeof buf)) { | ||
| 132 | char *ypbuf, *ypdom; | ||
| 133 | int ypbuflen; | ||
| 134 | |||
| 135 | if (yp_get_default_domain(&ypdom)) | ||
| 136 | continue; | ||
| 137 | if (yp_match(ypdom, "ethers.byname", hostname, hostlen, | ||
| 138 | &ypbuf, &ypbuflen)) | ||
| 139 | continue; | ||
| 140 | if (ether_line(ypbuf, e, try) == 0) { | ||
| 141 | free(ypbuf); | ||
| 142 | (void)fclose(f); | ||
| 143 | return 0; | ||
| 144 | } | ||
| 145 | free(ypbuf); | ||
| 146 | continue; | ||
| 147 | } | ||
| 148 | #endif | ||
| 149 | if (ether_line(buf, e, try) == 0 && strcmp(hostname, try) == 0) { | ||
| 150 | (void)fclose(f); | ||
| 151 | return 0; | ||
| 152 | } | ||
| 153 | } | ||
| 154 | (void)fclose(f); | ||
| 155 | errno = ENOENT; | ||
| 156 | return -1; | ||
| 157 | } | ||
| 158 | |||
| 159 | ether_line(l, e, hostname) | ||
| 160 | char *l; | ||
| 161 | struct ether_addr *e; | ||
| 162 | char *hostname; | ||
| 163 | { | ||
| 164 | u_int i[6]; | ||
| 165 | |||
| 166 | if (sscanf(l, " %x:%x:%x:%x:%x:%x %s\n", &i[0], &i[1], | ||
| 167 | &i[2], &i[3], &i[4], &i[5], hostname) == 7) { | ||
| 168 | e->ether_addr_octet[0] = (u_char)i[0]; | ||
| 169 | e->ether_addr_octet[1] = (u_char)i[1]; | ||
| 170 | e->ether_addr_octet[2] = (u_char)i[2]; | ||
| 171 | e->ether_addr_octet[3] = (u_char)i[3]; | ||
| 172 | e->ether_addr_octet[4] = (u_char)i[4]; | ||
| 173 | e->ether_addr_octet[5] = (u_char)i[5]; | ||
| 174 | return 0; | ||
| 175 | } | ||
| 176 | errno = EINVAL; | ||
| 177 | return -1; | ||
| 178 | } | ||
diff --git a/src/lib/libc/net/gethostbyname.3 b/src/lib/libc/net/gethostbyname.3 new file mode 100644 index 0000000000..bac0368296 --- /dev/null +++ b/src/lib/libc/net/gethostbyname.3 | |||
| @@ -0,0 +1,254 @@ | |||
| 1 | .\" $NetBSD: gethostbyname.3,v 1.6 1995/02/25 06:20:28 cgd Exp $ | ||
| 2 | .\" | ||
| 3 | .\" Copyright (c) 1983, 1987, 1991, 1993 | ||
| 4 | .\" The Regents of the University of California. All rights reserved. | ||
| 5 | .\" | ||
| 6 | .\" Redistribution and use in source and binary forms, with or without | ||
| 7 | .\" modification, are permitted provided that the following conditions | ||
| 8 | .\" are met: | ||
| 9 | .\" 1. Redistributions of source code must retain the above copyright | ||
| 10 | .\" notice, this list of conditions and the following disclaimer. | ||
| 11 | .\" 2. Redistributions in binary form must reproduce the above copyright | ||
| 12 | .\" notice, this list of conditions and the following disclaimer in the | ||
| 13 | .\" documentation and/or other materials provided with the distribution. | ||
| 14 | .\" 3. All advertising materials mentioning features or use of this software | ||
| 15 | .\" must display the following acknowledgement: | ||
| 16 | .\" This product includes software developed by the University of | ||
| 17 | .\" California, Berkeley and its contributors. | ||
| 18 | .\" 4. Neither the name of the University nor the names of its contributors | ||
| 19 | .\" may be used to endorse or promote products derived from this software | ||
| 20 | .\" without specific prior written permission. | ||
| 21 | .\" | ||
| 22 | .\" THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND | ||
| 23 | .\" ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE | ||
| 24 | .\" IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE | ||
| 25 | .\" ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE | ||
| 26 | .\" FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL | ||
| 27 | .\" DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS | ||
| 28 | .\" OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) | ||
| 29 | .\" HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT | ||
| 30 | .\" LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY | ||
| 31 | .\" OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF | ||
| 32 | .\" SUCH DAMAGE. | ||
| 33 | .\" | ||
| 34 | .\" @(#)gethostbyname.3 8.2 (Berkeley) 4/19/94 | ||
| 35 | .\" | ||
| 36 | .Dd April 19, 1994 | ||
| 37 | .Dt GETHOSTBYNAME 3 | ||
| 38 | .Os BSD 4.2 | ||
| 39 | .Sh NAME | ||
| 40 | .Nm gethostbyname , | ||
| 41 | .Nm gethostbyaddr , | ||
| 42 | .Nm gethostent , | ||
| 43 | .Nm sethostent , | ||
| 44 | .Nm endhostent , | ||
| 45 | .Nm herror | ||
| 46 | .Nd get network host entry | ||
| 47 | .Sh SYNOPSIS | ||
| 48 | .Fd #include <netdb.h> | ||
| 49 | .Fd extern int h_errno; | ||
| 50 | .Ft struct hostent * | ||
| 51 | .Fn gethostbyname "const char *name" | ||
| 52 | .Ft struct hostent * | ||
| 53 | .Fn gethostbyaddr "const char *addr" "int len" "int type" | ||
| 54 | .Ft struct hostent * | ||
| 55 | .Fn gethostent void | ||
| 56 | .Fn sethostent "int stayopen" | ||
| 57 | .Fn endhostent void | ||
| 58 | .Fn herror "char *string" | ||
| 59 | .Sh DESCRIPTION | ||
| 60 | The | ||
| 61 | .Fn gethostbyname | ||
| 62 | and | ||
| 63 | .Fn gethostbyaddr | ||
| 64 | functions | ||
| 65 | each return a pointer to an object with the | ||
| 66 | following structure describing an internet host | ||
| 67 | referenced by name or by address, respectively. | ||
| 68 | This structure contains either the information obtained from the name server, | ||
| 69 | .Xr named 8 , | ||
| 70 | broken-out fields from a line in | ||
| 71 | .Pa /etc/hosts , | ||
| 72 | or database entries supplied by the | ||
| 73 | .Xr yp 8 | ||
| 74 | system . | ||
| 75 | If the local name server is not running these routines do a lookup in | ||
| 76 | .Pa /etc/hosts . | ||
| 77 | .Bd -literal | ||
| 78 | struct hostent { | ||
| 79 | char *h_name; /* official name of host */ | ||
| 80 | char **h_aliases; /* alias list */ | ||
| 81 | int h_addrtype; /* host address type */ | ||
| 82 | int h_length; /* length of address */ | ||
| 83 | char **h_addr_list; /* list of addresses from name server */ | ||
| 84 | }; | ||
| 85 | #define h_addr h_addr_list[0] /* address, for backward compatibility */ | ||
| 86 | .Ed | ||
| 87 | .Pp | ||
| 88 | The members of this structure are: | ||
| 89 | .Bl -tag -width h_addr_list | ||
| 90 | .It Fa h_name | ||
| 91 | Official name of the host. | ||
| 92 | .It Fa h_aliases | ||
| 93 | A zero terminated array of alternate names for the host. | ||
| 94 | .It Fa h_addrtype | ||
| 95 | The type of address being returned; currently always | ||
| 96 | .Dv AF_INET . | ||
| 97 | .It Fa h_length | ||
| 98 | The length, in bytes, of the address. | ||
| 99 | .It Fa h_addr_list | ||
| 100 | A zero terminated array of network addresses for the host. | ||
| 101 | Host addresses are returned in network byte order. | ||
| 102 | .It Fa h_addr | ||
| 103 | The first address in | ||
| 104 | .Fa h_addr_list ; | ||
| 105 | this is for backward compatibility. | ||
| 106 | .Pp | ||
| 107 | When using the nameserver, | ||
| 108 | .Fn gethostbyname | ||
| 109 | will search for the named host in the current domain and its parents | ||
| 110 | unless the name ends in a dot. | ||
| 111 | If the name contains no dot, and if the environment variable | ||
| 112 | .Dq Ev HOSTALIASES | ||
| 113 | contains the name of an alias file, the alias file will first be searched | ||
| 114 | for an alias matching the input name. | ||
| 115 | See | ||
| 116 | .Xr hostname 7 | ||
| 117 | for the domain search procedure and the alias file format. | ||
| 118 | .Pp | ||
| 119 | The | ||
| 120 | .Fn sethostent | ||
| 121 | function | ||
| 122 | may be used to request the use of a connected | ||
| 123 | .Tn TCP | ||
| 124 | socket for queries. | ||
| 125 | If the | ||
| 126 | .Fa stayopen | ||
| 127 | flag is non-zero, | ||
| 128 | this sets the option to send all queries to the name server using | ||
| 129 | .Tn TCP | ||
| 130 | and to retain the connection after each call to | ||
| 131 | .Fn gethostbyname | ||
| 132 | or | ||
| 133 | .Fn gethostbyaddr . | ||
| 134 | Otherwise, queries are performed using | ||
| 135 | .Tn UDP | ||
| 136 | datagrams. | ||
| 137 | .Pp | ||
| 138 | The | ||
| 139 | .Fn endhostent | ||
| 140 | function | ||
| 141 | closes the | ||
| 142 | .Tn TCP | ||
| 143 | connection. | ||
| 144 | .Sh FILES | ||
| 145 | .Bl -tag -width /etc/hosts -compact | ||
| 146 | .It Pa /etc/hosts | ||
| 147 | .El | ||
| 148 | .Sh DIAGNOSTICS | ||
| 149 | Error return status from | ||
| 150 | .Fn gethostbyname | ||
| 151 | and | ||
| 152 | .Fn gethostbyaddr | ||
| 153 | is indicated by return of a null pointer. | ||
| 154 | The external integer | ||
| 155 | .Va h_errno | ||
| 156 | may then be checked to see whether this is a temporary failure | ||
| 157 | or an invalid or unknown host. | ||
| 158 | The routine | ||
| 159 | .Fn herror | ||
| 160 | can be used to print an error message describing the failure. | ||
| 161 | If its argument | ||
| 162 | .Fa string | ||
| 163 | is | ||
| 164 | .Pf non Dv -NULL , | ||
| 165 | it is printed, followed by a colon and a space. | ||
| 166 | The error message is printed with a trailing newline. | ||
| 167 | .Pp | ||
| 168 | The variable | ||
| 169 | .Va h_errno | ||
| 170 | can have the following values: | ||
| 171 | .Bl -tag -width HOST_NOT_FOUND | ||
| 172 | .It Dv HOST_NOT_FOUND | ||
| 173 | No such host is known. | ||
| 174 | .It Dv TRY_AGAIN | ||
| 175 | This is usually a temporary error | ||
| 176 | and means that the local server did not receive | ||
| 177 | a response from an authoritative server. | ||
| 178 | A retry at some later time may succeed. | ||
| 179 | .It Dv NO_RECOVERY | ||
| 180 | Some unexpected server failure was encountered. | ||
| 181 | This is a non-recoverable error. | ||
| 182 | .It Dv NO_DATA | ||
| 183 | The requested name is valid but does not have an IP address; | ||
| 184 | this is not a temporary error. | ||
| 185 | This means that the name is known to the name server but there is no address | ||
| 186 | associated with this name. | ||
| 187 | Another type of request to the name server using this domain name | ||
| 188 | will result in an answer; | ||
| 189 | for example, a mail-forwarder may be registered for this domain. | ||
| 190 | .El | ||
| 191 | .Sh SEE ALSO | ||
| 192 | .Xr resolver 3 , | ||
| 193 | .Xr hosts 5 , | ||
| 194 | .Xr hostname 7 , | ||
| 195 | .Xr named 8 | ||
| 196 | .Sh CAVEAT | ||
| 197 | The | ||
| 198 | .Fn gethostent | ||
| 199 | function | ||
| 200 | is defined, and | ||
| 201 | .Fn sethostent | ||
| 202 | and | ||
| 203 | .Fn endhostent | ||
| 204 | are redefined, | ||
| 205 | when | ||
| 206 | .Xr libc 3 | ||
| 207 | is built to use only the routines to lookup in | ||
| 208 | .Pa /etc/hosts | ||
| 209 | and not the name server. | ||
| 210 | .Pp | ||
| 211 | The | ||
| 212 | .Fn gethostent | ||
| 213 | function | ||
| 214 | reads the next line of | ||
| 215 | .Pa /etc/hosts , | ||
| 216 | opening the file if necessary. | ||
| 217 | .Pp | ||
| 218 | The | ||
| 219 | .Fn sethostent | ||
| 220 | function | ||
| 221 | opens and/or rewinds the file | ||
| 222 | .Pa /etc/hosts . | ||
| 223 | If the | ||
| 224 | .Fa stayopen | ||
| 225 | argument is non-zero, | ||
| 226 | the file will not be closed after each call to | ||
| 227 | .Fn gethostbyname | ||
| 228 | or | ||
| 229 | .Fn gethostbyaddr . | ||
| 230 | .Pp | ||
| 231 | The | ||
| 232 | .Fn endhostent | ||
| 233 | function | ||
| 234 | closes the file. | ||
| 235 | .Sh HISTORY | ||
| 236 | The | ||
| 237 | .Fn herror | ||
| 238 | function appeared in | ||
| 239 | .Bx 4.3 . | ||
| 240 | The | ||
| 241 | .Fn endhostent , | ||
| 242 | .Fn gethostbyaddr , | ||
| 243 | .Fn gethostbyname , | ||
| 244 | .Fn gethostent , | ||
| 245 | and | ||
| 246 | .Fn sethostent | ||
| 247 | functions appeared in | ||
| 248 | .Bx 4.2 . | ||
| 249 | .Sh BUGS | ||
| 250 | These functions use static data storage; | ||
| 251 | if the data is needed for future use, it should be | ||
| 252 | copied before any subsequent calls overwrite it. | ||
| 253 | Only the Internet | ||
| 254 | address format is currently understood. | ||
diff --git a/src/lib/libc/net/gethostnamadr.c b/src/lib/libc/net/gethostnamadr.c new file mode 100644 index 0000000000..ec3f14a900 --- /dev/null +++ b/src/lib/libc/net/gethostnamadr.c | |||
| @@ -0,0 +1,654 @@ | |||
| 1 | /* $NetBSD: gethostnamadr.c,v 1.13 1995/05/21 16:21:14 mycroft Exp $ */ | ||
| 2 | |||
| 3 | /*- | ||
| 4 | * Copyright (c) 1985, 1988, 1993 | ||
| 5 | * The Regents of the University of California. All rights reserved. | ||
| 6 | * | ||
| 7 | * Redistribution and use in source and binary forms, with or without | ||
| 8 | * modification, are permitted provided that the following conditions | ||
| 9 | * are met: | ||
| 10 | * 1. Redistributions of source code must retain the above copyright | ||
| 11 | * notice, this list of conditions and the following disclaimer. | ||
| 12 | * 2. Redistributions in binary form must reproduce the above copyright | ||
| 13 | * notice, this list of conditions and the following disclaimer in the | ||
| 14 | * documentation and/or other materials provided with the distribution. | ||
| 15 | * 3. All advertising materials mentioning features or use of this software | ||
| 16 | * must display the following acknowledgement: | ||
| 17 | * This product includes software developed by the University of | ||
| 18 | * California, Berkeley and its contributors. | ||
| 19 | * 4. Neither the name of the University nor the names of its contributors | ||
| 20 | * may be used to endorse or promote products derived from this software | ||
| 21 | * without specific prior written permission. | ||
| 22 | * | ||
| 23 | * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND | ||
| 24 | * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE | ||
| 25 | * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE | ||
| 26 | * ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE | ||
| 27 | * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL | ||
| 28 | * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS | ||
| 29 | * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) | ||
| 30 | * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT | ||
| 31 | * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY | ||
| 32 | * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF | ||
| 33 | * SUCH DAMAGE. | ||
| 34 | * - | ||
| 35 | * Portions Copyright (c) 1993 by Digital Equipment Corporation. | ||
| 36 | * | ||
| 37 | * Permission to use, copy, modify, and distribute this software for any | ||
| 38 | * purpose with or without fee is hereby granted, provided that the above | ||
| 39 | * copyright notice and this permission notice appear in all copies, and that | ||
| 40 | * the name of Digital Equipment Corporation not be used in advertising or | ||
| 41 | * publicity pertaining to distribution of the document or software without | ||
| 42 | * specific, written prior permission. | ||
| 43 | * | ||
| 44 | * THE SOFTWARE IS PROVIDED "AS IS" AND DIGITAL EQUIPMENT CORP. DISCLAIMS ALL | ||
| 45 | * WARRANTIES WITH REGARD TO THIS SOFTWARE, INCLUDING ALL IMPLIED WARRANTIES | ||
| 46 | * OF MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL DIGITAL EQUIPMENT | ||
| 47 | * CORPORATION BE LIABLE FOR ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL | ||
| 48 | * DAMAGES OR ANY DAMAGES WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR | ||
| 49 | * PROFITS, WHETHER IN AN ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS | ||
| 50 | * ACTION, ARISING OUT OF OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS | ||
| 51 | * SOFTWARE. | ||
| 52 | * - | ||
| 53 | * --Copyright-- | ||
| 54 | */ | ||
| 55 | |||
| 56 | #if defined(LIBC_SCCS) && !defined(lint) | ||
| 57 | #if 0 | ||
| 58 | static char sccsid[] = "@(#)gethostnamadr.c 8.1 (Berkeley) 6/4/93"; | ||
| 59 | static char rcsid[] = "$Id: gethnamaddr.c,v 4.9.1.1 1993/05/02 22:43:03 vixie Rel "; | ||
| 60 | #else | ||
| 61 | static char rcsid[] = "$NetBSD: gethostnamadr.c,v 1.13 1995/05/21 16:21:14 mycroft Exp $"; | ||
| 62 | #endif | ||
| 63 | #endif /* LIBC_SCCS and not lint */ | ||
| 64 | |||
| 65 | #include <sys/param.h> | ||
| 66 | #include <sys/socket.h> | ||
| 67 | #include <netinet/in.h> | ||
| 68 | #include <arpa/inet.h> | ||
| 69 | #include <arpa/nameser.h> | ||
| 70 | #include <netdb.h> | ||
| 71 | #include <resolv.h> | ||
| 72 | #include <stdio.h> | ||
| 73 | #include <ctype.h> | ||
| 74 | #include <errno.h> | ||
| 75 | #include <string.h> | ||
| 76 | #ifdef YP | ||
| 77 | #include <rpc/rpc.h> | ||
| 78 | #include <rpcsvc/yp_prot.h> | ||
| 79 | #include <rpcsvc/ypclnt.h> | ||
| 80 | #endif | ||
| 81 | |||
| 82 | #define MAXALIASES 35 | ||
| 83 | #define MAXADDRS 35 | ||
| 84 | |||
| 85 | static char *h_addr_ptrs[MAXADDRS + 1]; | ||
| 86 | |||
| 87 | #ifdef YP | ||
| 88 | static char *__ypdomain; | ||
| 89 | #endif | ||
| 90 | |||
| 91 | static struct hostent host; | ||
| 92 | static char *host_aliases[MAXALIASES]; | ||
| 93 | static char hostbuf[BUFSIZ+1]; | ||
| 94 | static struct in_addr host_addr; | ||
| 95 | static FILE *hostf = NULL; | ||
| 96 | static int stayopen = 0; | ||
| 97 | |||
| 98 | #if PACKETSZ > 1024 | ||
| 99 | #define MAXPACKET PACKETSZ | ||
| 100 | #else | ||
| 101 | #define MAXPACKET 1024 | ||
| 102 | #endif | ||
| 103 | |||
| 104 | typedef union { | ||
| 105 | HEADER hdr; | ||
| 106 | u_char buf[MAXPACKET]; | ||
| 107 | } querybuf; | ||
| 108 | |||
| 109 | typedef union { | ||
| 110 | int32_t al; | ||
| 111 | char ac; | ||
| 112 | } align; | ||
| 113 | |||
| 114 | static int qcomp __P((struct in_addr **, struct in_addr **)); | ||
| 115 | static struct hostent *getanswer __P((querybuf *, int, int)); | ||
| 116 | |||
| 117 | extern int h_errno; | ||
| 118 | |||
| 119 | static struct hostent * | ||
| 120 | getanswer(answer, anslen, iquery) | ||
| 121 | querybuf *answer; | ||
| 122 | int anslen; | ||
| 123 | int iquery; | ||
| 124 | { | ||
| 125 | register HEADER *hp; | ||
| 126 | register u_char *cp; | ||
| 127 | register int n; | ||
| 128 | u_char *eom; | ||
| 129 | char *bp, **ap; | ||
| 130 | int type, class, buflen, ancount, qdcount; | ||
| 131 | int haveanswer, getclass = C_ANY; | ||
| 132 | char **hap; | ||
| 133 | |||
| 134 | eom = answer->buf + anslen; | ||
| 135 | /* | ||
| 136 | * find first satisfactory answer | ||
| 137 | */ | ||
| 138 | hp = &answer->hdr; | ||
| 139 | ancount = ntohs(hp->ancount); | ||
| 140 | qdcount = ntohs(hp->qdcount); | ||
| 141 | bp = hostbuf; | ||
| 142 | buflen = sizeof(hostbuf); | ||
| 143 | cp = answer->buf + sizeof(HEADER); | ||
| 144 | if (qdcount) { | ||
| 145 | if (iquery) { | ||
| 146 | if ((n = dn_expand((u_char *)answer->buf, | ||
| 147 | (u_char *)eom, (u_char *)cp, (u_char *)bp, | ||
| 148 | buflen)) < 0) { | ||
| 149 | h_errno = NO_RECOVERY; | ||
| 150 | return ((struct hostent *) NULL); | ||
| 151 | } | ||
| 152 | cp += n + QFIXEDSZ; | ||
| 153 | host.h_name = bp; | ||
| 154 | n = strlen(bp) + 1; | ||
| 155 | bp += n; | ||
| 156 | buflen -= n; | ||
| 157 | } else | ||
| 158 | cp += __dn_skipname(cp, eom) + QFIXEDSZ; | ||
| 159 | while (--qdcount > 0) | ||
| 160 | cp += __dn_skipname(cp, eom) + QFIXEDSZ; | ||
| 161 | } else if (iquery) { | ||
| 162 | if (hp->aa) | ||
| 163 | h_errno = HOST_NOT_FOUND; | ||
| 164 | else | ||
| 165 | h_errno = TRY_AGAIN; | ||
| 166 | return ((struct hostent *) NULL); | ||
| 167 | } | ||
| 168 | ap = host_aliases; | ||
| 169 | *ap = NULL; | ||
| 170 | host.h_aliases = host_aliases; | ||
| 171 | hap = h_addr_ptrs; | ||
| 172 | *hap = NULL; | ||
| 173 | host.h_addr_list = h_addr_ptrs; | ||
| 174 | haveanswer = 0; | ||
| 175 | while (--ancount >= 0 && cp < eom) { | ||
| 176 | if ((n = dn_expand((u_char *)answer->buf, (u_char *)eom, | ||
| 177 | (u_char *)cp, (u_char *)bp, buflen)) < 0) | ||
| 178 | break; | ||
| 179 | cp += n; | ||
| 180 | type = _getshort(cp); | ||
| 181 | cp += sizeof(u_int16_t); | ||
| 182 | class = _getshort(cp); | ||
| 183 | cp += sizeof(u_int16_t) + sizeof(u_int32_t); | ||
| 184 | n = _getshort(cp); | ||
| 185 | cp += sizeof(u_int16_t); | ||
| 186 | if (type == T_CNAME) { | ||
| 187 | cp += n; | ||
| 188 | if (ap >= &host_aliases[MAXALIASES-1]) | ||
| 189 | continue; | ||
| 190 | *ap++ = bp; | ||
| 191 | n = strlen(bp) + 1; | ||
| 192 | bp += n; | ||
| 193 | buflen -= n; | ||
| 194 | continue; | ||
| 195 | } | ||
| 196 | if (iquery && type == T_PTR) { | ||
| 197 | if ((n = dn_expand((u_char *)answer->buf, | ||
| 198 | (u_char *)eom, (u_char *)cp, (u_char *)bp, | ||
| 199 | buflen)) < 0) | ||
| 200 | break; | ||
| 201 | cp += n; | ||
| 202 | host.h_name = bp; | ||
| 203 | return(&host); | ||
| 204 | } | ||
| 205 | if (iquery || type != T_A) { | ||
| 206 | #ifdef DEBUG | ||
| 207 | if (_res.options & RES_DEBUG) | ||
| 208 | printf("unexpected answer type %d, size %d\n", | ||
| 209 | type, n); | ||
| 210 | #endif | ||
| 211 | cp += n; | ||
| 212 | continue; | ||
| 213 | } | ||
| 214 | if (haveanswer) { | ||
| 215 | if (n != host.h_length) { | ||
| 216 | cp += n; | ||
| 217 | continue; | ||
| 218 | } | ||
| 219 | if (class != getclass) { | ||
| 220 | cp += n; | ||
| 221 | continue; | ||
| 222 | } | ||
| 223 | } else { | ||
| 224 | host.h_length = n; | ||
| 225 | getclass = class; | ||
| 226 | host.h_addrtype = (class == C_IN) ? AF_INET : AF_UNSPEC; | ||
| 227 | if (!iquery) { | ||
| 228 | host.h_name = bp; | ||
| 229 | bp += strlen(bp) + 1; | ||
| 230 | } | ||
| 231 | } | ||
| 232 | |||
| 233 | bp += sizeof(align) - ((u_long)bp % sizeof(align)); | ||
| 234 | |||
| 235 | if (bp + n >= &hostbuf[sizeof(hostbuf)]) { | ||
| 236 | #ifdef DEBUG | ||
| 237 | if (_res.options & RES_DEBUG) | ||
| 238 | printf("size (%d) too big\n", n); | ||
| 239 | #endif | ||
| 240 | break; | ||
| 241 | } | ||
| 242 | bcopy(cp, *hap++ = bp, n); | ||
| 243 | bp +=n; | ||
| 244 | cp += n; | ||
| 245 | haveanswer++; | ||
| 246 | } | ||
| 247 | if (haveanswer) { | ||
| 248 | *ap = NULL; | ||
| 249 | *hap = NULL; | ||
| 250 | if (_res.nsort) { | ||
| 251 | qsort(host.h_addr_list, haveanswer, | ||
| 252 | sizeof(struct in_addr), | ||
| 253 | (int (*)__P((const void *, const void *)))qcomp); | ||
| 254 | } | ||
| 255 | return (&host); | ||
| 256 | } else { | ||
| 257 | h_errno = TRY_AGAIN; | ||
| 258 | return ((struct hostent *) NULL); | ||
| 259 | } | ||
| 260 | } | ||
| 261 | |||
| 262 | struct hostent * | ||
| 263 | gethostbyname(name) | ||
| 264 | const char *name; | ||
| 265 | { | ||
| 266 | querybuf buf; | ||
| 267 | register const char *cp; | ||
| 268 | int n, i; | ||
| 269 | extern struct hostent *_gethtbyname(), *_yp_gethtbyname(); | ||
| 270 | register struct hostent *hp; | ||
| 271 | char lookups[MAXDNSLUS]; | ||
| 272 | |||
| 273 | /* | ||
| 274 | * disallow names consisting only of digits/dots, unless | ||
| 275 | * they end in a dot. | ||
| 276 | */ | ||
| 277 | if (isdigit(name[0])) | ||
| 278 | for (cp = name;; ++cp) { | ||
| 279 | if (!*cp) { | ||
| 280 | if (*--cp == '.') | ||
| 281 | break; | ||
| 282 | /* | ||
| 283 | * All-numeric, no dot at the end. | ||
| 284 | * Fake up a hostent as if we'd actually | ||
| 285 | * done a lookup. | ||
| 286 | */ | ||
| 287 | if (!inet_aton(name, &host_addr)) { | ||
| 288 | h_errno = HOST_NOT_FOUND; | ||
| 289 | return((struct hostent *) NULL); | ||
| 290 | } | ||
| 291 | host.h_name = (char *)name; | ||
| 292 | host.h_aliases = host_aliases; | ||
| 293 | host_aliases[0] = NULL; | ||
| 294 | host.h_addrtype = AF_INET; | ||
| 295 | host.h_length = sizeof(u_int32_t); | ||
| 296 | h_addr_ptrs[0] = (char *)&host_addr; | ||
| 297 | h_addr_ptrs[1] = NULL; | ||
| 298 | host.h_addr_list = h_addr_ptrs; | ||
| 299 | return (&host); | ||
| 300 | } | ||
| 301 | if (!isdigit(*cp) && *cp != '.') | ||
| 302 | break; | ||
| 303 | } | ||
| 304 | |||
| 305 | if ((_res.options & RES_INIT) == 0 && res_init() == -1) | ||
| 306 | return (_gethtbyname(name)); | ||
| 307 | |||
| 308 | bcopy(_res.lookups, lookups, sizeof lookups); | ||
| 309 | if (lookups[0] == '\0') | ||
| 310 | strncpy(lookups, "bf", sizeof lookups); | ||
| 311 | |||
| 312 | hp = (struct hostent *)NULL; | ||
| 313 | for (i = 0; i < MAXDNSLUS && hp == NULL && lookups[i]; i++) { | ||
| 314 | switch (lookups[i]) { | ||
| 315 | #ifdef YP | ||
| 316 | case 'y': | ||
| 317 | hp = _yp_gethtbyname(name); | ||
| 318 | break; | ||
| 319 | #endif | ||
| 320 | case 'b': | ||
| 321 | if ((n = res_search(name, C_IN, T_A, buf.buf, | ||
| 322 | sizeof(buf))) < 0) { | ||
| 323 | #ifdef DEBUG | ||
| 324 | if (_res.options & RES_DEBUG) | ||
| 325 | printf("res_search failed\n"); | ||
| 326 | #endif | ||
| 327 | break; | ||
| 328 | } | ||
| 329 | hp = getanswer(&buf, n, 0); | ||
| 330 | break; | ||
| 331 | case 'f': | ||
| 332 | hp = _gethtbyname(name); | ||
| 333 | break; | ||
| 334 | } | ||
| 335 | } | ||
| 336 | return (hp); | ||
| 337 | } | ||
| 338 | |||
| 339 | struct hostent * | ||
| 340 | gethostbyaddr(addr, len, type) | ||
| 341 | const char *addr; | ||
| 342 | int len, type; | ||
| 343 | { | ||
| 344 | int n, i; | ||
| 345 | querybuf buf; | ||
| 346 | register struct hostent *hp; | ||
| 347 | char qbuf[MAXDNAME]; | ||
| 348 | extern struct hostent *_gethtbyaddr(), *_yp_gethtbyaddr(); | ||
| 349 | char lookups[MAXDNSLUS]; | ||
| 350 | |||
| 351 | if (type != AF_INET) | ||
| 352 | return ((struct hostent *) NULL); | ||
| 353 | (void)sprintf(qbuf, "%u.%u.%u.%u.in-addr.arpa", | ||
| 354 | ((unsigned)addr[3] & 0xff), | ||
| 355 | ((unsigned)addr[2] & 0xff), | ||
| 356 | ((unsigned)addr[1] & 0xff), | ||
| 357 | ((unsigned)addr[0] & 0xff)); | ||
| 358 | |||
| 359 | if ((_res.options & RES_INIT) == 0 && res_init() == -1) | ||
| 360 | return (_gethtbyaddr(addr, len, type)); | ||
| 361 | |||
| 362 | bcopy(_res.lookups, lookups, sizeof lookups); | ||
| 363 | if (lookups[0] == '\0') | ||
| 364 | strncpy(lookups, "bf", sizeof lookups); | ||
| 365 | |||
| 366 | hp = (struct hostent *)NULL; | ||
| 367 | for (i = 0; i < MAXDNSLUS && hp == NULL && lookups[i]; i++) { | ||
| 368 | switch (lookups[i]) { | ||
| 369 | #ifdef YP | ||
| 370 | case 'y': | ||
| 371 | hp = _yp_gethtbyaddr(addr, len, type); | ||
| 372 | break; | ||
| 373 | #endif | ||
| 374 | case 'b': | ||
| 375 | n = res_query(qbuf, C_IN, T_PTR, (char *)&buf, sizeof(buf)); | ||
| 376 | if (n < 0) { | ||
| 377 | #ifdef DEBUG | ||
| 378 | if (_res.options & RES_DEBUG) | ||
| 379 | printf("res_query failed\n"); | ||
| 380 | #endif | ||
| 381 | break; | ||
| 382 | } | ||
| 383 | hp = getanswer(&buf, n, 1); | ||
| 384 | if (hp == NULL) | ||
| 385 | break; | ||
| 386 | hp->h_addrtype = type; | ||
| 387 | hp->h_length = len; | ||
| 388 | h_addr_ptrs[0] = (char *)&host_addr; | ||
| 389 | h_addr_ptrs[1] = (char *)0; | ||
| 390 | host_addr = *(struct in_addr *)addr; | ||
| 391 | break; | ||
| 392 | case 'f': | ||
| 393 | hp = _gethtbyaddr(addr, len, type); | ||
| 394 | break; | ||
| 395 | } | ||
| 396 | } | ||
| 397 | return (hp); | ||
| 398 | } | ||
| 399 | |||
| 400 | void | ||
| 401 | _sethtent(f) | ||
| 402 | int f; | ||
| 403 | { | ||
| 404 | if (hostf == NULL) | ||
| 405 | hostf = fopen(_PATH_HOSTS, "r" ); | ||
| 406 | else | ||
| 407 | rewind(hostf); | ||
| 408 | stayopen = f; | ||
| 409 | } | ||
| 410 | |||
| 411 | void | ||
| 412 | _endhtent() | ||
| 413 | { | ||
| 414 | if (hostf && !stayopen) { | ||
| 415 | (void) fclose(hostf); | ||
| 416 | hostf = NULL; | ||
| 417 | } | ||
| 418 | } | ||
| 419 | |||
| 420 | struct hostent * | ||
| 421 | _gethtent() | ||
| 422 | { | ||
| 423 | char *p; | ||
| 424 | register char *cp, **q; | ||
| 425 | |||
| 426 | if (hostf == NULL && (hostf = fopen(_PATH_HOSTS, "r" )) == NULL) | ||
| 427 | return (NULL); | ||
| 428 | again: | ||
| 429 | if ((p = fgets(hostbuf, BUFSIZ, hostf)) == NULL) | ||
| 430 | return (NULL); | ||
| 431 | if (*p == '#') | ||
| 432 | goto again; | ||
| 433 | cp = strpbrk(p, "#\n"); | ||
| 434 | if (cp == NULL) | ||
| 435 | goto again; | ||
| 436 | *cp = '\0'; | ||
| 437 | cp = strpbrk(p, " \t"); | ||
| 438 | if (cp == NULL) | ||
| 439 | goto again; | ||
| 440 | *cp++ = '\0'; | ||
| 441 | /* THIS STUFF IS INTERNET SPECIFIC */ | ||
| 442 | h_addr_ptrs[0] = (char *)&host_addr; | ||
| 443 | h_addr_ptrs[1] = NULL; | ||
| 444 | (void) inet_aton(p, &host_addr); | ||
| 445 | host.h_addr_list = h_addr_ptrs; | ||
| 446 | host.h_length = sizeof(u_int32_t); | ||
| 447 | host.h_addrtype = AF_INET; | ||
| 448 | while (*cp == ' ' || *cp == '\t') | ||
| 449 | cp++; | ||
| 450 | host.h_name = cp; | ||
| 451 | q = host.h_aliases = host_aliases; | ||
| 452 | cp = strpbrk(cp, " \t"); | ||
| 453 | if (cp != NULL) | ||
| 454 | *cp++ = '\0'; | ||
| 455 | while (cp && *cp) { | ||
| 456 | if (*cp == ' ' || *cp == '\t') { | ||
| 457 | cp++; | ||
| 458 | continue; | ||
| 459 | } | ||
| 460 | if (q < &host_aliases[MAXALIASES - 1]) | ||
| 461 | *q++ = cp; | ||
| 462 | cp = strpbrk(cp, " \t"); | ||
| 463 | if (cp != NULL) | ||
| 464 | *cp++ = '\0'; | ||
| 465 | } | ||
| 466 | *q = NULL; | ||
| 467 | return (&host); | ||
| 468 | } | ||
| 469 | |||
| 470 | struct hostent * | ||
| 471 | _gethtbyname(name) | ||
| 472 | char *name; | ||
| 473 | { | ||
| 474 | register struct hostent *p; | ||
| 475 | register char **cp; | ||
| 476 | |||
| 477 | _sethtent(0); | ||
| 478 | while (p = _gethtent()) { | ||
| 479 | if (strcasecmp(p->h_name, name) == 0) | ||
| 480 | break; | ||
| 481 | for (cp = p->h_aliases; *cp != 0; cp++) | ||
| 482 | if (strcasecmp(*cp, name) == 0) | ||
| 483 | goto found; | ||
| 484 | } | ||
| 485 | found: | ||
| 486 | _endhtent(); | ||
| 487 | if (p==NULL) | ||
| 488 | h_errno = HOST_NOT_FOUND; | ||
| 489 | return (p); | ||
| 490 | } | ||
| 491 | |||
| 492 | struct hostent * | ||
| 493 | _gethtbyaddr(addr, len, type) | ||
| 494 | const char *addr; | ||
| 495 | int len, type; | ||
| 496 | { | ||
| 497 | register struct hostent *p; | ||
| 498 | |||
| 499 | _sethtent(0); | ||
| 500 | while (p = _gethtent()) | ||
| 501 | if (p->h_addrtype == type && !bcmp(p->h_addr, addr, len)) | ||
| 502 | break; | ||
| 503 | _endhtent(); | ||
| 504 | if (p==NULL) | ||
| 505 | h_errno = HOST_NOT_FOUND; | ||
| 506 | return (p); | ||
| 507 | } | ||
| 508 | |||
| 509 | static int | ||
| 510 | qcomp(a1, a2) | ||
| 511 | struct in_addr **a1, **a2; | ||
| 512 | { | ||
| 513 | int pos1, pos2; | ||
| 514 | |||
| 515 | for (pos1 = 0; pos1 < _res.nsort; pos1++) | ||
| 516 | if (_res.sort_list[pos1].addr.s_addr == | ||
| 517 | ((*a1)->s_addr & _res.sort_list[pos1].mask)) | ||
| 518 | break; | ||
| 519 | for (pos2 = 0; pos2 < _res.nsort; pos2++) | ||
| 520 | if (_res.sort_list[pos2].addr.s_addr == | ||
| 521 | ((*a2)->s_addr & _res.sort_list[pos2].mask)) | ||
| 522 | break; | ||
| 523 | return pos1 - pos2; | ||
| 524 | } | ||
| 525 | |||
| 526 | #ifdef YP | ||
| 527 | struct hostent * | ||
| 528 | _yphostent(line) | ||
| 529 | char *line; | ||
| 530 | { | ||
| 531 | static struct in_addr host_addrs[MAXADDRS]; | ||
| 532 | char *p = line; | ||
| 533 | char *cp, **q; | ||
| 534 | char **hap; | ||
| 535 | struct in_addr *buf; | ||
| 536 | int more; | ||
| 537 | |||
| 538 | host.h_name = NULL; | ||
| 539 | host.h_addr_list = h_addr_ptrs; | ||
| 540 | host.h_length = sizeof(u_int32_t); | ||
| 541 | host.h_addrtype = AF_INET; | ||
| 542 | hap = h_addr_ptrs; | ||
| 543 | buf = host_addrs; | ||
| 544 | q = host.h_aliases = host_aliases; | ||
| 545 | |||
| 546 | nextline: | ||
| 547 | more = 0; | ||
| 548 | cp = strpbrk(p, " \t"); | ||
| 549 | if (cp == NULL) { | ||
| 550 | if (host.h_name == NULL) | ||
| 551 | return (NULL); | ||
| 552 | else | ||
| 553 | goto done; | ||
| 554 | } | ||
| 555 | *cp++ = '\0'; | ||
| 556 | |||
| 557 | *hap++ = (char *)buf; | ||
| 558 | (void) inet_aton(p, buf++); | ||
| 559 | |||
| 560 | while (*cp == ' ' || *cp == '\t') | ||
| 561 | cp++; | ||
| 562 | p = cp; | ||
| 563 | cp = strpbrk(p, " \t\n"); | ||
| 564 | if (cp != NULL) { | ||
| 565 | if (*cp == '\n') | ||
| 566 | more = 1; | ||
| 567 | *cp++ = '\0'; | ||
| 568 | } | ||
| 569 | if (!host.h_name) | ||
| 570 | host.h_name = p; | ||
| 571 | else if (strcmp(host.h_name, p)==0) | ||
| 572 | ; | ||
| 573 | else if (q < &host_aliases[MAXALIASES - 1]) | ||
| 574 | *q++ = p; | ||
| 575 | p = cp; | ||
| 576 | if (more) | ||
| 577 | goto nextline; | ||
| 578 | |||
| 579 | while (cp && *cp) { | ||
| 580 | if (*cp == ' ' || *cp == '\t') { | ||
| 581 | cp++; | ||
| 582 | continue; | ||
| 583 | } | ||
| 584 | if (*cp == '\n') { | ||
| 585 | cp++; | ||
| 586 | goto nextline; | ||
| 587 | } | ||
| 588 | if (q < &host_aliases[MAXALIASES - 1]) | ||
| 589 | *q++ = cp; | ||
| 590 | cp = strpbrk(cp, " \t"); | ||
| 591 | if (cp != NULL) | ||
| 592 | *cp++ = '\0'; | ||
| 593 | } | ||
| 594 | done: | ||
| 595 | *q = NULL; | ||
| 596 | *hap = NULL; | ||
| 597 | return (&host); | ||
| 598 | } | ||
| 599 | |||
| 600 | struct hostent * | ||
| 601 | _yp_gethtbyaddr(addr, len, type) | ||
| 602 | const char *addr; | ||
| 603 | int len, type; | ||
| 604 | { | ||
| 605 | struct hostent *hp = (struct hostent *)NULL; | ||
| 606 | static char *__ypcurrent; | ||
| 607 | int __ypcurrentlen, r; | ||
| 608 | char name[sizeof("xxx.xxx.xxx.xxx") + 1]; | ||
| 609 | |||
| 610 | if (!__ypdomain) { | ||
| 611 | if (_yp_check(&__ypdomain) == 0) | ||
| 612 | return (hp); | ||
| 613 | } | ||
| 614 | sprintf(name, "%u.%u.%u.%u", | ||
| 615 | ((unsigned)addr[0] & 0xff), | ||
| 616 | ((unsigned)addr[1] & 0xff), | ||
| 617 | ((unsigned)addr[2] & 0xff), | ||
| 618 | ((unsigned)addr[3] & 0xff)); | ||
| 619 | if (__ypcurrent) | ||
| 620 | free(__ypcurrent); | ||
| 621 | __ypcurrent = NULL; | ||
| 622 | r = yp_match(__ypdomain, "hosts.byaddr", name, | ||
| 623 | strlen(name), &__ypcurrent, &__ypcurrentlen); | ||
| 624 | if (r==0) | ||
| 625 | hp = _yphostent(__ypcurrent); | ||
| 626 | if (hp==NULL) | ||
| 627 | h_errno = HOST_NOT_FOUND; | ||
| 628 | return (hp); | ||
| 629 | } | ||
| 630 | |||
| 631 | struct hostent * | ||
| 632 | _yp_gethtbyname(name) | ||
| 633 | const char *name; | ||
| 634 | { | ||
| 635 | struct hostent *hp = (struct hostent *)NULL; | ||
| 636 | static char *__ypcurrent; | ||
| 637 | int __ypcurrentlen, r; | ||
| 638 | |||
| 639 | if (!__ypdomain) { | ||
| 640 | if (_yp_check(&__ypdomain) == 0) | ||
| 641 | return (hp); | ||
| 642 | } | ||
| 643 | if (__ypcurrent) | ||
| 644 | free(__ypcurrent); | ||
| 645 | __ypcurrent = NULL; | ||
| 646 | r = yp_match(__ypdomain, "hosts.byname", name, | ||
| 647 | strlen(name), &__ypcurrent, &__ypcurrentlen); | ||
| 648 | if (r==0) | ||
| 649 | hp = _yphostent(__ypcurrent); | ||
| 650 | if (hp==NULL) | ||
| 651 | h_errno = HOST_NOT_FOUND; | ||
| 652 | return (hp); | ||
| 653 | } | ||
| 654 | #endif | ||
diff --git a/src/lib/libc/net/getnetbyaddr.c b/src/lib/libc/net/getnetbyaddr.c new file mode 100644 index 0000000000..c193860e36 --- /dev/null +++ b/src/lib/libc/net/getnetbyaddr.c | |||
| @@ -0,0 +1,62 @@ | |||
| 1 | /* $NetBSD: getnetbyaddr.c,v 1.4 1995/02/25 06:20:30 cgd Exp $ */ | ||
| 2 | |||
| 3 | /* | ||
| 4 | * Copyright (c) 1983, 1993 | ||
| 5 | * The Regents of the University of California. All rights reserved. | ||
| 6 | * | ||
| 7 | * Redistribution and use in source and binary forms, with or without | ||
| 8 | * modification, are permitted provided that the following conditions | ||
| 9 | * are met: | ||
| 10 | * 1. Redistributions of source code must retain the above copyright | ||
| 11 | * notice, this list of conditions and the following disclaimer. | ||
| 12 | * 2. Redistributions in binary form must reproduce the above copyright | ||
| 13 | * notice, this list of conditions and the following disclaimer in the | ||
| 14 | * documentation and/or other materials provided with the distribution. | ||
| 15 | * 3. All advertising materials mentioning features or use of this software | ||
| 16 | * must display the following acknowledgement: | ||
| 17 | * This product includes software developed by the University of | ||
| 18 | * California, Berkeley and its contributors. | ||
| 19 | * 4. Neither the name of the University nor the names of its contributors | ||
| 20 | * may be used to endorse or promote products derived from this software | ||
| 21 | * without specific prior written permission. | ||
| 22 | * | ||
| 23 | * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND | ||
| 24 | * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE | ||
| 25 | * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE | ||
| 26 | * ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE | ||
| 27 | * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL | ||
| 28 | * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS | ||
| 29 | * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) | ||
| 30 | * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT | ||
| 31 | * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY | ||
| 32 | * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF | ||
| 33 | * SUCH DAMAGE. | ||
| 34 | */ | ||
| 35 | |||
| 36 | #if defined(LIBC_SCCS) && !defined(lint) | ||
| 37 | #if 0 | ||
| 38 | static char sccsid[] = "@(#)getnetbyaddr.c 8.1 (Berkeley) 6/4/93"; | ||
| 39 | #else | ||
| 40 | static char rcsid[] = "$NetBSD: getnetbyaddr.c,v 1.4 1995/02/25 06:20:30 cgd Exp $"; | ||
| 41 | #endif | ||
| 42 | #endif /* LIBC_SCCS and not lint */ | ||
| 43 | |||
| 44 | #include <netdb.h> | ||
| 45 | |||
| 46 | extern int _net_stayopen; | ||
| 47 | |||
| 48 | struct netent * | ||
| 49 | getnetbyaddr(net, type) | ||
| 50 | register long net; | ||
| 51 | register int type; | ||
| 52 | { | ||
| 53 | register struct netent *p; | ||
| 54 | |||
| 55 | setnetent(_net_stayopen); | ||
| 56 | while (p = getnetent()) | ||
| 57 | if (p->n_addrtype == type && p->n_net == net) | ||
| 58 | break; | ||
| 59 | if (!_net_stayopen) | ||
| 60 | endnetent(); | ||
| 61 | return (p); | ||
| 62 | } | ||
diff --git a/src/lib/libc/net/getnetbyname.c b/src/lib/libc/net/getnetbyname.c new file mode 100644 index 0000000000..93a2e1256c --- /dev/null +++ b/src/lib/libc/net/getnetbyname.c | |||
| @@ -0,0 +1,68 @@ | |||
| 1 | /* $NetBSD: getnetbyname.c,v 1.4 1995/02/25 06:20:31 cgd Exp $ */ | ||
| 2 | |||
| 3 | /* | ||
| 4 | * Copyright (c) 1983, 1993 | ||
| 5 | * The Regents of the University of California. All rights reserved. | ||
| 6 | * | ||
| 7 | * Redistribution and use in source and binary forms, with or without | ||
| 8 | * modification, are permitted provided that the following conditions | ||
| 9 | * are met: | ||
| 10 | * 1. Redistributions of source code must retain the above copyright | ||
| 11 | * notice, this list of conditions and the following disclaimer. | ||
| 12 | * 2. Redistributions in binary form must reproduce the above copyright | ||
| 13 | * notice, this list of conditions and the following disclaimer in the | ||
| 14 | * documentation and/or other materials provided with the distribution. | ||
| 15 | * 3. All advertising materials mentioning features or use of this software | ||
| 16 | * must display the following acknowledgement: | ||
| 17 | * This product includes software developed by the University of | ||
| 18 | * California, Berkeley and its contributors. | ||
| 19 | * 4. Neither the name of the University nor the names of its contributors | ||
| 20 | * may be used to endorse or promote products derived from this software | ||
| 21 | * without specific prior written permission. | ||
| 22 | * | ||
| 23 | * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND | ||
| 24 | * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE | ||
| 25 | * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE | ||
| 26 | * ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE | ||
| 27 | * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL | ||
| 28 | * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS | ||
| 29 | * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) | ||
| 30 | * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT | ||
| 31 | * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY | ||
| 32 | * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF | ||
| 33 | * SUCH DAMAGE. | ||
| 34 | */ | ||
| 35 | |||
| 36 | #if defined(LIBC_SCCS) && !defined(lint) | ||
| 37 | #if 0 | ||
| 38 | static char sccsid[] = "@(#)getnetbyname.c 8.1 (Berkeley) 6/4/93"; | ||
| 39 | #else | ||
| 40 | static char rcsid[] = "$NetBSD: getnetbyname.c,v 1.4 1995/02/25 06:20:31 cgd Exp $"; | ||
| 41 | #endif | ||
| 42 | #endif /* LIBC_SCCS and not lint */ | ||
| 43 | |||
| 44 | #include <netdb.h> | ||
| 45 | #include <string.h> | ||
| 46 | |||
| 47 | extern int _net_stayopen; | ||
| 48 | |||
| 49 | struct netent * | ||
| 50 | getnetbyname(name) | ||
| 51 | register const char *name; | ||
| 52 | { | ||
| 53 | register struct netent *p; | ||
| 54 | register char **cp; | ||
| 55 | |||
| 56 | setnetent(_net_stayopen); | ||
| 57 | while (p = getnetent()) { | ||
| 58 | if (strcmp(p->n_name, name) == 0) | ||
| 59 | break; | ||
| 60 | for (cp = p->n_aliases; *cp != 0; cp++) | ||
| 61 | if (strcmp(*cp, name) == 0) | ||
| 62 | goto found; | ||
| 63 | } | ||
| 64 | found: | ||
| 65 | if (!_net_stayopen) | ||
| 66 | endnetent(); | ||
| 67 | return (p); | ||
| 68 | } | ||
diff --git a/src/lib/libc/net/getnetent.3 b/src/lib/libc/net/getnetent.3 new file mode 100644 index 0000000000..d4f0bedbf9 --- /dev/null +++ b/src/lib/libc/net/getnetent.3 | |||
| @@ -0,0 +1,153 @@ | |||
| 1 | .\" $NetBSD: getnetent.3,v 1.3 1995/02/25 06:20:32 cgd Exp $ | ||
| 2 | .\" | ||
| 3 | .\" Copyright (c) 1983, 1991, 1993 | ||
| 4 | .\" The Regents of the University of California. All rights reserved. | ||
| 5 | .\" | ||
| 6 | .\" Redistribution and use in source and binary forms, with or without | ||
| 7 | .\" modification, are permitted provided that the following conditions | ||
| 8 | .\" are met: | ||
| 9 | .\" 1. Redistributions of source code must retain the above copyright | ||
| 10 | .\" notice, this list of conditions and the following disclaimer. | ||
| 11 | .\" 2. Redistributions in binary form must reproduce the above copyright | ||
| 12 | .\" notice, this list of conditions and the following disclaimer in the | ||
| 13 | .\" documentation and/or other materials provided with the distribution. | ||
| 14 | .\" 3. All advertising materials mentioning features or use of this software | ||
| 15 | .\" must display the following acknowledgement: | ||
| 16 | .\" This product includes software developed by the University of | ||
| 17 | .\" California, Berkeley and its contributors. | ||
| 18 | .\" 4. Neither the name of the University nor the names of its contributors | ||
| 19 | .\" may be used to endorse or promote products derived from this software | ||
| 20 | .\" without specific prior written permission. | ||
| 21 | .\" | ||
| 22 | .\" THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND | ||
| 23 | .\" ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE | ||
| 24 | .\" IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE | ||
| 25 | .\" ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE | ||
| 26 | .\" FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL | ||
| 27 | .\" DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS | ||
| 28 | .\" OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) | ||
| 29 | .\" HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT | ||
| 30 | .\" LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY | ||
| 31 | .\" OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF | ||
| 32 | .\" SUCH DAMAGE. | ||
| 33 | .\" | ||
| 34 | .\" @(#)getnetent.3 8.1 (Berkeley) 6/4/93 | ||
| 35 | .\" | ||
| 36 | .Dd June 4, 1993 | ||
| 37 | .Dt GETNETENT 3 | ||
| 38 | .Os BSD 4.2 | ||
| 39 | .Sh NAME | ||
| 40 | .Nm getnetent , | ||
| 41 | .Nm getnetbyaddr , | ||
| 42 | .Nm getnetbyname , | ||
| 43 | .Nm setnetent , | ||
| 44 | .Nm endnetent | ||
| 45 | .Nd get network entry | ||
| 46 | .Sh SYNOPSIS | ||
| 47 | .Fd #include <netdb.h> | ||
| 48 | .Ft struct netent * | ||
| 49 | .Fn getnetent | ||
| 50 | .Ft struct netent * | ||
| 51 | .Fn getnetbyname "char *name" | ||
| 52 | .Ft struct netent * | ||
| 53 | .Fn getnetbyaddr "long net" "int type" | ||
| 54 | .Fn setnetent "int stayopen" | ||
| 55 | .Fn endnetent | ||
| 56 | .Sh DESCRIPTION | ||
| 57 | The | ||
| 58 | .Fn getnetent , | ||
| 59 | .Fn getnetbyname , | ||
| 60 | and | ||
| 61 | .Fn getnetbyaddr | ||
| 62 | functions | ||
| 63 | each return a pointer to an object with the | ||
| 64 | following structure | ||
| 65 | containing the broken-out | ||
| 66 | fields of a line in the network data base, | ||
| 67 | .Pa /etc/networks . | ||
| 68 | .Bd -literal -offset indent | ||
| 69 | struct netent { | ||
| 70 | char *n_name; /* official name of net */ | ||
| 71 | char **n_aliases; /* alias list */ | ||
| 72 | int n_addrtype; /* net number type */ | ||
| 73 | unsigned long n_net; /* net number */ | ||
| 74 | }; | ||
| 75 | .Ed | ||
| 76 | .Pp | ||
| 77 | The members of this structure are: | ||
| 78 | .Bl -tag -width n_addrtype | ||
| 79 | .It Fa n_name | ||
| 80 | The official name of the network. | ||
| 81 | .It Fa n_aliases | ||
| 82 | A zero terminated list of alternate names for the network. | ||
| 83 | .It Fa n_addrtype | ||
| 84 | The type of the network number returned; currently only AF_INET. | ||
| 85 | .It Fa n_net | ||
| 86 | The network number. Network numbers are returned in machine byte | ||
| 87 | order. | ||
| 88 | .El | ||
| 89 | .Pp | ||
| 90 | The | ||
| 91 | .Fn getnetent | ||
| 92 | function | ||
| 93 | reads the next line of the file, opening the file if necessary. | ||
| 94 | .Pp | ||
| 95 | The | ||
| 96 | .Fn setnetent | ||
| 97 | function | ||
| 98 | opens and rewinds the file. If the | ||
| 99 | .Fa stayopen | ||
| 100 | flag is non-zero, | ||
| 101 | the net data base will not be closed after each call to | ||
| 102 | .Fn getnetbyname | ||
| 103 | or | ||
| 104 | .Fn getnetbyaddr . | ||
| 105 | .Pp | ||
| 106 | The | ||
| 107 | .Fn endnetent | ||
| 108 | function | ||
| 109 | closes the file. | ||
| 110 | .Pp | ||
| 111 | The | ||
| 112 | .Fn getnetbyname | ||
| 113 | function | ||
| 114 | and | ||
| 115 | .Fn getnetbyaddr | ||
| 116 | sequentially search from the beginning | ||
| 117 | of the file until a matching | ||
| 118 | net name or | ||
| 119 | net address and type is found, | ||
| 120 | or until | ||
| 121 | .Dv EOF | ||
| 122 | is encountered. | ||
| 123 | Network numbers are supplied in host order. | ||
| 124 | .Sh FILES | ||
| 125 | .Bl -tag -width /etc/networks -compact | ||
| 126 | .It Pa /etc/networks | ||
| 127 | .El | ||
| 128 | .Sh DIAGNOSTICS | ||
| 129 | Null pointer | ||
| 130 | (0) returned on | ||
| 131 | .Dv EOF | ||
| 132 | or error. | ||
| 133 | .Sh SEE ALSO | ||
| 134 | .Xr networks 5 | ||
| 135 | .Sh HISTORY | ||
| 136 | The | ||
| 137 | .Fn getnetent , | ||
| 138 | .Fn getnetbyaddr , | ||
| 139 | .Fn getnetbyname , | ||
| 140 | .Fn setnetent , | ||
| 141 | and | ||
| 142 | .Fn endnetent | ||
| 143 | functions appeared in | ||
| 144 | .Bx 4.2 . | ||
| 145 | .Sh BUGS | ||
| 146 | The data space used by | ||
| 147 | these functions is static; if future use requires the data, it should be | ||
| 148 | copied before any subsequent calls to these functions overwrite it. | ||
| 149 | Only Internet network | ||
| 150 | numbers are currently understood. | ||
| 151 | Expecting network numbers to fit | ||
| 152 | in no more than 32 bits is probably | ||
| 153 | naive. | ||
diff --git a/src/lib/libc/net/getnetent.c b/src/lib/libc/net/getnetent.c new file mode 100644 index 0000000000..b4e16b8f5d --- /dev/null +++ b/src/lib/libc/net/getnetent.c | |||
| @@ -0,0 +1,127 @@ | |||
| 1 | /* $NetBSD: getnetent.c,v 1.4 1995/02/25 06:20:33 cgd Exp $ */ | ||
| 2 | |||
| 3 | /* | ||
| 4 | * Copyright (c) 1983, 1993 | ||
| 5 | * The Regents of the University of California. All rights reserved. | ||
| 6 | * | ||
| 7 | * Redistribution and use in source and binary forms, with or without | ||
| 8 | * modification, are permitted provided that the following conditions | ||
| 9 | * are met: | ||
| 10 | * 1. Redistributions of source code must retain the above copyright | ||
| 11 | * notice, this list of conditions and the following disclaimer. | ||
| 12 | * 2. Redistributions in binary form must reproduce the above copyright | ||
| 13 | * notice, this list of conditions and the following disclaimer in the | ||
| 14 | * documentation and/or other materials provided with the distribution. | ||
| 15 | * 3. All advertising materials mentioning features or use of this software | ||
| 16 | * must display the following acknowledgement: | ||
| 17 | * This product includes software developed by the University of | ||
| 18 | * California, Berkeley and its contributors. | ||
| 19 | * 4. Neither the name of the University nor the names of its contributors | ||
| 20 | * may be used to endorse or promote products derived from this software | ||
| 21 | * without specific prior written permission. | ||
| 22 | * | ||
| 23 | * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND | ||
| 24 | * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE | ||
| 25 | * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE | ||
| 26 | * ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE | ||
| 27 | * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL | ||
| 28 | * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS | ||
| 29 | * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) | ||
| 30 | * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT | ||
| 31 | * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY | ||
| 32 | * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF | ||
| 33 | * SUCH DAMAGE. | ||
| 34 | */ | ||
| 35 | |||
| 36 | #if defined(LIBC_SCCS) && !defined(lint) | ||
| 37 | #if 0 | ||
| 38 | static char sccsid[] = "@(#)getnetent.c 8.1 (Berkeley) 6/4/93"; | ||
| 39 | #else | ||
| 40 | static char rcsid[] = "$NetBSD: getnetent.c,v 1.4 1995/02/25 06:20:33 cgd Exp $"; | ||
| 41 | #endif | ||
| 42 | #endif /* LIBC_SCCS and not lint */ | ||
| 43 | |||
| 44 | #include <sys/types.h> | ||
| 45 | #include <sys/socket.h> | ||
| 46 | #include <netinet/in.h> | ||
| 47 | #include <arpa/inet.h> | ||
| 48 | #include <netdb.h> | ||
| 49 | #include <stdio.h> | ||
| 50 | #include <string.h> | ||
| 51 | |||
| 52 | #define MAXALIASES 35 | ||
| 53 | |||
| 54 | static FILE *netf; | ||
| 55 | static char line[BUFSIZ+1]; | ||
| 56 | static struct netent net; | ||
| 57 | static char *net_aliases[MAXALIASES]; | ||
| 58 | int _net_stayopen; | ||
| 59 | |||
| 60 | void | ||
| 61 | setnetent(f) | ||
| 62 | int f; | ||
| 63 | { | ||
| 64 | if (netf == NULL) | ||
| 65 | netf = fopen(_PATH_NETWORKS, "r" ); | ||
| 66 | else | ||
| 67 | rewind(netf); | ||
| 68 | _net_stayopen |= f; | ||
| 69 | } | ||
| 70 | |||
| 71 | void | ||
| 72 | endnetent() | ||
| 73 | { | ||
| 74 | if (netf) { | ||
| 75 | fclose(netf); | ||
| 76 | netf = NULL; | ||
| 77 | } | ||
| 78 | _net_stayopen = 0; | ||
| 79 | } | ||
| 80 | |||
| 81 | struct netent * | ||
| 82 | getnetent() | ||
| 83 | { | ||
| 84 | char *p; | ||
| 85 | register char *cp, **q; | ||
| 86 | |||
| 87 | if (netf == NULL && (netf = fopen(_PATH_NETWORKS, "r" )) == NULL) | ||
| 88 | return (NULL); | ||
| 89 | again: | ||
| 90 | p = fgets(line, BUFSIZ, netf); | ||
| 91 | if (p == NULL) | ||
| 92 | return (NULL); | ||
| 93 | if (*p == '#') | ||
| 94 | goto again; | ||
| 95 | cp = strpbrk(p, "#\n"); | ||
| 96 | if (cp == NULL) | ||
| 97 | goto again; | ||
| 98 | *cp = '\0'; | ||
| 99 | net.n_name = p; | ||
| 100 | cp = strpbrk(p, " \t"); | ||
| 101 | if (cp == NULL) | ||
| 102 | goto again; | ||
| 103 | *cp++ = '\0'; | ||
| 104 | while (*cp == ' ' || *cp == '\t') | ||
| 105 | cp++; | ||
| 106 | p = strpbrk(cp, " \t"); | ||
| 107 | if (p != NULL) | ||
| 108 | *p++ = '\0'; | ||
| 109 | net.n_net = inet_network(cp); | ||
| 110 | net.n_addrtype = AF_INET; | ||
| 111 | q = net.n_aliases = net_aliases; | ||
| 112 | if (p != NULL) | ||
| 113 | cp = p; | ||
| 114 | while (cp && *cp) { | ||
| 115 | if (*cp == ' ' || *cp == '\t') { | ||
| 116 | cp++; | ||
| 117 | continue; | ||
| 118 | } | ||
| 119 | if (q < &net_aliases[MAXALIASES - 1]) | ||
| 120 | *q++ = cp; | ||
| 121 | cp = strpbrk(cp, " \t"); | ||
| 122 | if (cp != NULL) | ||
| 123 | *cp++ = '\0'; | ||
| 124 | } | ||
| 125 | *q = NULL; | ||
| 126 | return (&net); | ||
| 127 | } | ||
diff --git a/src/lib/libc/net/getproto.c b/src/lib/libc/net/getproto.c new file mode 100644 index 0000000000..49c09b0806 --- /dev/null +++ b/src/lib/libc/net/getproto.c | |||
| @@ -0,0 +1,61 @@ | |||
| 1 | /* $NetBSD: getproto.c,v 1.4 1995/02/25 06:20:33 cgd Exp $ */ | ||
| 2 | |||
| 3 | /* | ||
| 4 | * Copyright (c) 1983, 1993 | ||
| 5 | * The Regents of the University of California. All rights reserved. | ||
| 6 | * | ||
| 7 | * Redistribution and use in source and binary forms, with or without | ||
| 8 | * modification, are permitted provided that the following conditions | ||
| 9 | * are met: | ||
| 10 | * 1. Redistributions of source code must retain the above copyright | ||
| 11 | * notice, this list of conditions and the following disclaimer. | ||
| 12 | * 2. Redistributions in binary form must reproduce the above copyright | ||
| 13 | * notice, this list of conditions and the following disclaimer in the | ||
| 14 | * documentation and/or other materials provided with the distribution. | ||
| 15 | * 3. All advertising materials mentioning features or use of this software | ||
| 16 | * must display the following acknowledgement: | ||
| 17 | * This product includes software developed by the University of | ||
| 18 | * California, Berkeley and its contributors. | ||
| 19 | * 4. Neither the name of the University nor the names of its contributors | ||
| 20 | * may be used to endorse or promote products derived from this software | ||
| 21 | * without specific prior written permission. | ||
| 22 | * | ||
| 23 | * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND | ||
| 24 | * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE | ||
| 25 | * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE | ||
| 26 | * ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE | ||
| 27 | * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL | ||
| 28 | * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS | ||
| 29 | * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) | ||
| 30 | * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT | ||
| 31 | * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY | ||
| 32 | * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF | ||
| 33 | * SUCH DAMAGE. | ||
| 34 | */ | ||
| 35 | |||
| 36 | #if defined(LIBC_SCCS) && !defined(lint) | ||
| 37 | #if 0 | ||
| 38 | static char sccsid[] = "@(#)getproto.c 8.1 (Berkeley) 6/4/93"; | ||
| 39 | #else | ||
| 40 | static char rcsid[] = "$NetBSD: getproto.c,v 1.4 1995/02/25 06:20:33 cgd Exp $"; | ||
| 41 | #endif | ||
| 42 | #endif /* LIBC_SCCS and not lint */ | ||
| 43 | |||
| 44 | #include <netdb.h> | ||
| 45 | |||
| 46 | extern int _proto_stayopen; | ||
| 47 | |||
| 48 | struct protoent * | ||
| 49 | getprotobynumber(proto) | ||
| 50 | register int proto; | ||
| 51 | { | ||
| 52 | register struct protoent *p; | ||
| 53 | |||
| 54 | setprotoent(_proto_stayopen); | ||
| 55 | while (p = getprotoent()) | ||
| 56 | if (p->p_proto == proto) | ||
| 57 | break; | ||
| 58 | if (!_proto_stayopen) | ||
| 59 | endprotoent(); | ||
| 60 | return (p); | ||
| 61 | } | ||
diff --git a/src/lib/libc/net/getprotoent.3 b/src/lib/libc/net/getprotoent.3 new file mode 100644 index 0000000000..8d607199ef --- /dev/null +++ b/src/lib/libc/net/getprotoent.3 | |||
| @@ -0,0 +1,146 @@ | |||
| 1 | .\" $NetBSD: getprotoent.3,v 1.3 1995/02/25 06:20:34 cgd Exp $ | ||
| 2 | .\" | ||
| 3 | .\" Copyright (c) 1983, 1991, 1993 | ||
| 4 | .\" The Regents of the University of California. All rights reserved. | ||
| 5 | .\" | ||
| 6 | .\" Redistribution and use in source and binary forms, with or without | ||
| 7 | .\" modification, are permitted provided that the following conditions | ||
| 8 | .\" are met: | ||
| 9 | .\" 1. Redistributions of source code must retain the above copyright | ||
| 10 | .\" notice, this list of conditions and the following disclaimer. | ||
| 11 | .\" 2. Redistributions in binary form must reproduce the above copyright | ||
| 12 | .\" notice, this list of conditions and the following disclaimer in the | ||
| 13 | .\" documentation and/or other materials provided with the distribution. | ||
| 14 | .\" 3. All advertising materials mentioning features or use of this software | ||
| 15 | .\" must display the following acknowledgement: | ||
| 16 | .\" This product includes software developed by the University of | ||
| 17 | .\" California, Berkeley and its contributors. | ||
| 18 | .\" 4. Neither the name of the University nor the names of its contributors | ||
| 19 | .\" may be used to endorse or promote products derived from this software | ||
| 20 | .\" without specific prior written permission. | ||
| 21 | .\" | ||
| 22 | .\" THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND | ||
| 23 | .\" ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE | ||
| 24 | .\" IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE | ||
| 25 | .\" ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE | ||
| 26 | .\" FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL | ||
| 27 | .\" DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS | ||
| 28 | .\" OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) | ||
| 29 | .\" HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT | ||
| 30 | .\" LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY | ||
| 31 | .\" OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF | ||
| 32 | .\" SUCH DAMAGE. | ||
| 33 | .\" | ||
| 34 | .\" @(#)getprotoent.3 8.1 (Berkeley) 6/4/93 | ||
| 35 | .\" | ||
| 36 | .Dd June 4, 1993 | ||
| 37 | .Dt GETPROTOENT 3 | ||
| 38 | .Os BSD 4.2 | ||
| 39 | .Sh NAME | ||
| 40 | .Nm getprotoent , | ||
| 41 | .Nm getprotobynumber , | ||
| 42 | .Nm getprotobyname , | ||
| 43 | .Nm setprotoent , | ||
| 44 | .Nm endprotoent | ||
| 45 | .Nd get protocol entry | ||
| 46 | .Sh SYNOPSIS | ||
| 47 | .Fd #include <netdb.h> | ||
| 48 | .Ft struct protoent * | ||
| 49 | .Fn getprotoent | ||
| 50 | .Ft struct protoent * | ||
| 51 | .Fn getprotobyname "char *name" | ||
| 52 | .Ft struct protoent * | ||
| 53 | .Fn getprotobynumber "int proto" | ||
| 54 | .Fn setprotoent "int stayopen" | ||
| 55 | .Fn endprotoent | ||
| 56 | .Sh DESCRIPTION | ||
| 57 | The | ||
| 58 | .Fn getprotoent , | ||
| 59 | .Fn getprotobyname , | ||
| 60 | and | ||
| 61 | .Fn getprotobynumber | ||
| 62 | functions | ||
| 63 | each return a pointer to an object with the | ||
| 64 | following structure | ||
| 65 | containing the broken-out | ||
| 66 | fields of a line in the network protocol data base, | ||
| 67 | .Pa /etc/protocols . | ||
| 68 | .Bd -literal -offset indent | ||
| 69 | .Pp | ||
| 70 | struct protoent { | ||
| 71 | char *p_name; /* official name of protocol */ | ||
| 72 | char **p_aliases; /* alias list */ | ||
| 73 | int p_proto; /* protocol number */ | ||
| 74 | }; | ||
| 75 | .Ed | ||
| 76 | .Pp | ||
| 77 | The members of this structure are: | ||
| 78 | .Bl -tag -width p_aliases | ||
| 79 | .It Fa p_name | ||
| 80 | The official name of the protocol. | ||
| 81 | .It Fa p_aliases | ||
| 82 | A zero terminated list of alternate names for the protocol. | ||
| 83 | .It Fa p_proto | ||
| 84 | The protocol number. | ||
| 85 | .El | ||
| 86 | .Pp | ||
| 87 | The | ||
| 88 | .Fn getprotoent | ||
| 89 | function | ||
| 90 | reads the next line of the file, opening the file if necessary. | ||
| 91 | .Pp | ||
| 92 | The | ||
| 93 | .Fn setprotoent | ||
| 94 | function | ||
| 95 | opens and rewinds the file. If the | ||
| 96 | .Fa stayopen | ||
| 97 | flag is non-zero, | ||
| 98 | the net data base will not be closed after each call to | ||
| 99 | .Fn getprotobyname | ||
| 100 | or | ||
| 101 | .Fn getprotobynumber . | ||
| 102 | .Pp | ||
| 103 | The | ||
| 104 | .Fn endprotoent | ||
| 105 | function | ||
| 106 | closes the file. | ||
| 107 | .Pp | ||
| 108 | The | ||
| 109 | .Fn getprotobyname | ||
| 110 | function | ||
| 111 | and | ||
| 112 | .Fn getprotobynumber | ||
| 113 | sequentially search from the beginning | ||
| 114 | of the file until a matching | ||
| 115 | protocol name or | ||
| 116 | protocol number is found, | ||
| 117 | or until | ||
| 118 | .Dv EOF | ||
| 119 | is encountered. | ||
| 120 | .Sh RETURN VALUES | ||
| 121 | Null pointer | ||
| 122 | (0) returned on | ||
| 123 | .Dv EOF | ||
| 124 | or error. | ||
| 125 | .Sh FILES | ||
| 126 | .Bl -tag -width /etc/protocols -compact | ||
| 127 | .It Pa /etc/protocols | ||
| 128 | .El | ||
| 129 | .Sh SEE ALSO | ||
| 130 | .Xr protocols 5 | ||
| 131 | .Sh HISTORY | ||
| 132 | The | ||
| 133 | .Fn getprotoent , | ||
| 134 | .Fn getprotobynumber , | ||
| 135 | .Fn getprotobyname , | ||
| 136 | .Fn setprotoent , | ||
| 137 | and | ||
| 138 | .Fn endprotoent | ||
| 139 | functions appeared in | ||
| 140 | .Bx 4.2 . | ||
| 141 | .Sh BUGS | ||
| 142 | These functions use a static data space; | ||
| 143 | if the data is needed for future use, it should be | ||
| 144 | copied before any subsequent calls overwrite it. | ||
| 145 | Only the Internet | ||
| 146 | protocols are currently understood. | ||
diff --git a/src/lib/libc/net/getprotoent.c b/src/lib/libc/net/getprotoent.c new file mode 100644 index 0000000000..1179b9029b --- /dev/null +++ b/src/lib/libc/net/getprotoent.c | |||
| @@ -0,0 +1,125 @@ | |||
| 1 | /* $NetBSD: getprotoent.c,v 1.4 1995/02/25 06:20:35 cgd Exp $ */ | ||
| 2 | |||
| 3 | /* | ||
| 4 | * Copyright (c) 1983, 1993 | ||
| 5 | * The Regents of the University of California. All rights reserved. | ||
| 6 | * | ||
| 7 | * Redistribution and use in source and binary forms, with or without | ||
| 8 | * modification, are permitted provided that the following conditions | ||
| 9 | * are met: | ||
| 10 | * 1. Redistributions of source code must retain the above copyright | ||
| 11 | * notice, this list of conditions and the following disclaimer. | ||
| 12 | * 2. Redistributions in binary form must reproduce the above copyright | ||
| 13 | * notice, this list of conditions and the following disclaimer in the | ||
| 14 | * documentation and/or other materials provided with the distribution. | ||
| 15 | * 3. All advertising materials mentioning features or use of this software | ||
| 16 | * must display the following acknowledgement: | ||
| 17 | * This product includes software developed by the University of | ||
| 18 | * California, Berkeley and its contributors. | ||
| 19 | * 4. Neither the name of the University nor the names of its contributors | ||
| 20 | * may be used to endorse or promote products derived from this software | ||
| 21 | * without specific prior written permission. | ||
| 22 | * | ||
| 23 | * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND | ||
| 24 | * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE | ||
| 25 | * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE | ||
| 26 | * ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE | ||
| 27 | * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL | ||
| 28 | * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS | ||
| 29 | * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) | ||
| 30 | * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT | ||
| 31 | * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY | ||
| 32 | * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF | ||
| 33 | * SUCH DAMAGE. | ||
| 34 | */ | ||
| 35 | |||
| 36 | #if defined(LIBC_SCCS) && !defined(lint) | ||
| 37 | #if 0 | ||
| 38 | static char sccsid[] = "@(#)getprotoent.c 8.1 (Berkeley) 6/4/93"; | ||
| 39 | #else | ||
| 40 | static char rcsid[] = "$NetBSD: getprotoent.c,v 1.4 1995/02/25 06:20:35 cgd Exp $"; | ||
| 41 | #endif | ||
| 42 | #endif /* LIBC_SCCS and not lint */ | ||
| 43 | |||
| 44 | #include <sys/types.h> | ||
| 45 | #include <sys/socket.h> | ||
| 46 | #include <netdb.h> | ||
| 47 | #include <stdio.h> | ||
| 48 | #include <stdlib.h> | ||
| 49 | #include <string.h> | ||
| 50 | |||
| 51 | #define MAXALIASES 35 | ||
| 52 | |||
| 53 | static FILE *protof = NULL; | ||
| 54 | static char line[BUFSIZ+1]; | ||
| 55 | static struct protoent proto; | ||
| 56 | static char *proto_aliases[MAXALIASES]; | ||
| 57 | int _proto_stayopen; | ||
| 58 | |||
| 59 | void | ||
| 60 | setprotoent(f) | ||
| 61 | int f; | ||
| 62 | { | ||
| 63 | if (protof == NULL) | ||
| 64 | protof = fopen(_PATH_PROTOCOLS, "r" ); | ||
| 65 | else | ||
| 66 | rewind(protof); | ||
| 67 | _proto_stayopen |= f; | ||
| 68 | } | ||
| 69 | |||
| 70 | void | ||
| 71 | endprotoent() | ||
| 72 | { | ||
| 73 | if (protof) { | ||
| 74 | fclose(protof); | ||
| 75 | protof = NULL; | ||
| 76 | } | ||
| 77 | _proto_stayopen = 0; | ||
| 78 | } | ||
| 79 | |||
| 80 | struct protoent * | ||
| 81 | getprotoent() | ||
| 82 | { | ||
| 83 | char *p; | ||
| 84 | register char *cp, **q; | ||
| 85 | |||
| 86 | if (protof == NULL && (protof = fopen(_PATH_PROTOCOLS, "r" )) == NULL) | ||
| 87 | return (NULL); | ||
| 88 | again: | ||
| 89 | if ((p = fgets(line, BUFSIZ, protof)) == NULL) | ||
| 90 | return (NULL); | ||
| 91 | if (*p == '#') | ||
| 92 | goto again; | ||
| 93 | cp = strpbrk(p, "#\n"); | ||
| 94 | if (cp == NULL) | ||
| 95 | goto again; | ||
| 96 | *cp = '\0'; | ||
| 97 | proto.p_name = p; | ||
| 98 | cp = strpbrk(p, " \t"); | ||
| 99 | if (cp == NULL) | ||
| 100 | goto again; | ||
| 101 | *cp++ = '\0'; | ||
| 102 | while (*cp == ' ' || *cp == '\t') | ||
| 103 | cp++; | ||
| 104 | p = strpbrk(cp, " \t"); | ||
| 105 | if (p != NULL) | ||
| 106 | *p++ = '\0'; | ||
| 107 | proto.p_proto = atoi(cp); | ||
| 108 | q = proto.p_aliases = proto_aliases; | ||
| 109 | if (p != NULL) { | ||
| 110 | cp = p; | ||
| 111 | while (cp && *cp) { | ||
| 112 | if (*cp == ' ' || *cp == '\t') { | ||
| 113 | cp++; | ||
| 114 | continue; | ||
| 115 | } | ||
| 116 | if (q < &proto_aliases[MAXALIASES - 1]) | ||
| 117 | *q++ = cp; | ||
| 118 | cp = strpbrk(cp, " \t"); | ||
| 119 | if (cp != NULL) | ||
| 120 | *cp++ = '\0'; | ||
| 121 | } | ||
| 122 | } | ||
| 123 | *q = NULL; | ||
| 124 | return (&proto); | ||
| 125 | } | ||
diff --git a/src/lib/libc/net/getprotoname.c b/src/lib/libc/net/getprotoname.c new file mode 100644 index 0000000000..4f8cf21c3f --- /dev/null +++ b/src/lib/libc/net/getprotoname.c | |||
| @@ -0,0 +1,68 @@ | |||
| 1 | /* $NetBSD: getprotoname.c,v 1.4 1995/02/25 06:20:36 cgd Exp $ */ | ||
| 2 | |||
| 3 | /* | ||
| 4 | * Copyright (c) 1983, 1993 | ||
| 5 | * The Regents of the University of California. All rights reserved. | ||
| 6 | * | ||
| 7 | * Redistribution and use in source and binary forms, with or without | ||
| 8 | * modification, are permitted provided that the following conditions | ||
| 9 | * are met: | ||
| 10 | * 1. Redistributions of source code must retain the above copyright | ||
| 11 | * notice, this list of conditions and the following disclaimer. | ||
| 12 | * 2. Redistributions in binary form must reproduce the above copyright | ||
| 13 | * notice, this list of conditions and the following disclaimer in the | ||
| 14 | * documentation and/or other materials provided with the distribution. | ||
| 15 | * 3. All advertising materials mentioning features or use of this software | ||
| 16 | * must display the following acknowledgement: | ||
| 17 | * This product includes software developed by the University of | ||
| 18 | * California, Berkeley and its contributors. | ||
| 19 | * 4. Neither the name of the University nor the names of its contributors | ||
| 20 | * may be used to endorse or promote products derived from this software | ||
| 21 | * without specific prior written permission. | ||
| 22 | * | ||
| 23 | * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND | ||
| 24 | * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE | ||
| 25 | * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE | ||
| 26 | * ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE | ||
| 27 | * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL | ||
| 28 | * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS | ||
| 29 | * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) | ||
| 30 | * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT | ||
| 31 | * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY | ||
| 32 | * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF | ||
| 33 | * SUCH DAMAGE. | ||
| 34 | */ | ||
| 35 | |||
| 36 | #if defined(LIBC_SCCS) && !defined(lint) | ||
| 37 | #if 0 | ||
| 38 | static char sccsid[] = "@(#)getprotoname.c 8.1 (Berkeley) 6/4/93"; | ||
| 39 | #else | ||
| 40 | static char rcsid[] = "$NetBSD: getprotoname.c,v 1.4 1995/02/25 06:20:36 cgd Exp $"; | ||
| 41 | #endif | ||
| 42 | #endif /* LIBC_SCCS and not lint */ | ||
| 43 | |||
| 44 | #include <netdb.h> | ||
| 45 | #include <string.h> | ||
| 46 | |||
| 47 | extern int _proto_stayopen; | ||
| 48 | |||
| 49 | struct protoent * | ||
| 50 | getprotobyname(name) | ||
| 51 | register const char *name; | ||
| 52 | { | ||
| 53 | register struct protoent *p; | ||
| 54 | register char **cp; | ||
| 55 | |||
| 56 | setprotoent(_proto_stayopen); | ||
| 57 | while (p = getprotoent()) { | ||
| 58 | if (strcmp(p->p_name, name) == 0) | ||
| 59 | break; | ||
| 60 | for (cp = p->p_aliases; *cp != 0; cp++) | ||
| 61 | if (strcmp(*cp, name) == 0) | ||
| 62 | goto found; | ||
| 63 | } | ||
| 64 | found: | ||
| 65 | if (!_proto_stayopen) | ||
| 66 | endprotoent(); | ||
| 67 | return (p); | ||
| 68 | } | ||
diff --git a/src/lib/libc/net/getservbyname.c b/src/lib/libc/net/getservbyname.c new file mode 100644 index 0000000000..b4a6311966 --- /dev/null +++ b/src/lib/libc/net/getservbyname.c | |||
| @@ -0,0 +1,71 @@ | |||
| 1 | /* $NetBSD: getservbyname.c,v 1.4 1995/02/25 06:20:36 cgd Exp $ */ | ||
| 2 | |||
| 3 | /* | ||
| 4 | * Copyright (c) 1983, 1993 | ||
| 5 | * The Regents of the University of California. All rights reserved. | ||
| 6 | * | ||
| 7 | * Redistribution and use in source and binary forms, with or without | ||
| 8 | * modification, are permitted provided that the following conditions | ||
| 9 | * are met: | ||
| 10 | * 1. Redistributions of source code must retain the above copyright | ||
| 11 | * notice, this list of conditions and the following disclaimer. | ||
| 12 | * 2. Redistributions in binary form must reproduce the above copyright | ||
| 13 | * notice, this list of conditions and the following disclaimer in the | ||
| 14 | * documentation and/or other materials provided with the distribution. | ||
| 15 | * 3. All advertising materials mentioning features or use of this software | ||
| 16 | * must display the following acknowledgement: | ||
| 17 | * This product includes software developed by the University of | ||
| 18 | * California, Berkeley and its contributors. | ||
| 19 | * 4. Neither the name of the University nor the names of its contributors | ||
| 20 | * may be used to endorse or promote products derived from this software | ||
| 21 | * without specific prior written permission. | ||
| 22 | * | ||
| 23 | * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND | ||
| 24 | * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE | ||
| 25 | * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE | ||
| 26 | * ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE | ||
| 27 | * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL | ||
| 28 | * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS | ||
| 29 | * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) | ||
| 30 | * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT | ||
| 31 | * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY | ||
| 32 | * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF | ||
| 33 | * SUCH DAMAGE. | ||
| 34 | */ | ||
| 35 | |||
| 36 | #if defined(LIBC_SCCS) && !defined(lint) | ||
| 37 | #if 0 | ||
| 38 | static char sccsid[] = "@(#)getservbyname.c 8.1 (Berkeley) 6/4/93"; | ||
| 39 | #else | ||
| 40 | static char rcsid[] = "$NetBSD: getservbyname.c,v 1.4 1995/02/25 06:20:36 cgd Exp $"; | ||
| 41 | #endif | ||
| 42 | #endif /* LIBC_SCCS and not lint */ | ||
| 43 | |||
| 44 | #include <netdb.h> | ||
| 45 | #include <string.h> | ||
| 46 | |||
| 47 | extern int _serv_stayopen; | ||
| 48 | |||
| 49 | struct servent * | ||
| 50 | getservbyname(name, proto) | ||
| 51 | const char *name, *proto; | ||
| 52 | { | ||
| 53 | register struct servent *p; | ||
| 54 | register char **cp; | ||
| 55 | |||
| 56 | setservent(_serv_stayopen); | ||
| 57 | while (p = getservent()) { | ||
| 58 | if (strcmp(name, p->s_name) == 0) | ||
| 59 | goto gotname; | ||
| 60 | for (cp = p->s_aliases; *cp; cp++) | ||
| 61 | if (strcmp(name, *cp) == 0) | ||
| 62 | goto gotname; | ||
| 63 | continue; | ||
| 64 | gotname: | ||
| 65 | if (proto == 0 || strcmp(p->s_proto, proto) == 0) | ||
| 66 | break; | ||
| 67 | } | ||
| 68 | if (!_serv_stayopen) | ||
| 69 | endservent(); | ||
| 70 | return (p); | ||
| 71 | } | ||
diff --git a/src/lib/libc/net/getservbyport.c b/src/lib/libc/net/getservbyport.c new file mode 100644 index 0000000000..c34790737b --- /dev/null +++ b/src/lib/libc/net/getservbyport.c | |||
| @@ -0,0 +1,66 @@ | |||
| 1 | /* $NetBSD: getservbyport.c,v 1.4 1995/02/25 06:20:37 cgd Exp $ */ | ||
| 2 | |||
| 3 | /* | ||
| 4 | * Copyright (c) 1983, 1993 | ||
| 5 | * The Regents of the University of California. All rights reserved. | ||
| 6 | * | ||
| 7 | * Redistribution and use in source and binary forms, with or without | ||
| 8 | * modification, are permitted provided that the following conditions | ||
| 9 | * are met: | ||
| 10 | * 1. Redistributions of source code must retain the above copyright | ||
| 11 | * notice, this list of conditions and the following disclaimer. | ||
| 12 | * 2. Redistributions in binary form must reproduce the above copyright | ||
| 13 | * notice, this list of conditions and the following disclaimer in the | ||
| 14 | * documentation and/or other materials provided with the distribution. | ||
| 15 | * 3. All advertising materials mentioning features or use of this software | ||
| 16 | * must display the following acknowledgement: | ||
| 17 | * This product includes software developed by the University of | ||
| 18 | * California, Berkeley and its contributors. | ||
| 19 | * 4. Neither the name of the University nor the names of its contributors | ||
| 20 | * may be used to endorse or promote products derived from this software | ||
| 21 | * without specific prior written permission. | ||
| 22 | * | ||
| 23 | * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND | ||
| 24 | * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE | ||
| 25 | * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE | ||
| 26 | * ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE | ||
| 27 | * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL | ||
| 28 | * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS | ||
| 29 | * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) | ||
| 30 | * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT | ||
| 31 | * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY | ||
| 32 | * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF | ||
| 33 | * SUCH DAMAGE. | ||
| 34 | */ | ||
| 35 | |||
| 36 | #if defined(LIBC_SCCS) && !defined(lint) | ||
| 37 | #if 0 | ||
| 38 | static char sccsid[] = "@(#)getservbyport.c 8.1 (Berkeley) 6/4/93"; | ||
| 39 | #else | ||
| 40 | static char rcsid[] = "$NetBSD: getservbyport.c,v 1.4 1995/02/25 06:20:37 cgd Exp $"; | ||
| 41 | #endif | ||
| 42 | #endif /* LIBC_SCCS and not lint */ | ||
| 43 | |||
| 44 | #include <netdb.h> | ||
| 45 | #include <string.h> | ||
| 46 | |||
| 47 | extern int _serv_stayopen; | ||
| 48 | |||
| 49 | struct servent * | ||
| 50 | getservbyport(port, proto) | ||
| 51 | int port; | ||
| 52 | const char *proto; | ||
| 53 | { | ||
| 54 | register struct servent *p; | ||
| 55 | |||
| 56 | setservent(_serv_stayopen); | ||
| 57 | while (p = getservent()) { | ||
| 58 | if (p->s_port != port) | ||
| 59 | continue; | ||
| 60 | if (proto == 0 || strcmp(p->s_proto, proto) == 0) | ||
| 61 | break; | ||
| 62 | } | ||
| 63 | if (!_serv_stayopen) | ||
| 64 | endservent(); | ||
| 65 | return (p); | ||
| 66 | } | ||
diff --git a/src/lib/libc/net/getservent.3 b/src/lib/libc/net/getservent.3 new file mode 100644 index 0000000000..9e0656be00 --- /dev/null +++ b/src/lib/libc/net/getservent.3 | |||
| @@ -0,0 +1,157 @@ | |||
| 1 | .\" $NetBSD: getservent.3,v 1.3 1995/02/25 06:20:38 cgd Exp $ | ||
| 2 | .\" | ||
| 3 | .\" Copyright (c) 1983, 1991, 1993 | ||
| 4 | .\" The Regents of the University of California. All rights reserved. | ||
| 5 | .\" | ||
| 6 | .\" Redistribution and use in source and binary forms, with or without | ||
| 7 | .\" modification, are permitted provided that the following conditions | ||
| 8 | .\" are met: | ||
| 9 | .\" 1. Redistributions of source code must retain the above copyright | ||
| 10 | .\" notice, this list of conditions and the following disclaimer. | ||
| 11 | .\" 2. Redistributions in binary form must reproduce the above copyright | ||
| 12 | .\" notice, this list of conditions and the following disclaimer in the | ||
| 13 | .\" documentation and/or other materials provided with the distribution. | ||
| 14 | .\" 3. All advertising materials mentioning features or use of this software | ||
| 15 | .\" must display the following acknowledgement: | ||
| 16 | .\" This product includes software developed by the University of | ||
| 17 | .\" California, Berkeley and its contributors. | ||
| 18 | .\" 4. Neither the name of the University nor the names of its contributors | ||
| 19 | .\" may be used to endorse or promote products derived from this software | ||
| 20 | .\" without specific prior written permission. | ||
| 21 | .\" | ||
| 22 | .\" THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND | ||
| 23 | .\" ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE | ||
| 24 | .\" IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE | ||
| 25 | .\" ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE | ||
| 26 | .\" FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL | ||
| 27 | .\" DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS | ||
| 28 | .\" OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) | ||
| 29 | .\" HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT | ||
| 30 | .\" LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY | ||
| 31 | .\" OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF | ||
| 32 | .\" SUCH DAMAGE. | ||
| 33 | .\" | ||
| 34 | .\" @(#)getservent.3 8.3 (Berkeley) 1/12/94 | ||
| 35 | .\" | ||
| 36 | .Dd January 12, 1994 | ||
| 37 | .Dt GETSERVENT 3 | ||
| 38 | .Os BSD 4.2 | ||
| 39 | .Sh NAME | ||
| 40 | .Nm getservent , | ||
| 41 | .Nm getservbyport , | ||
| 42 | .Nm getservbyname , | ||
| 43 | .Nm setservent , | ||
| 44 | .Nm endservent | ||
| 45 | .Nd get service entry | ||
| 46 | .Sh SYNOPSIS | ||
| 47 | .Fd #include <netdb.h> | ||
| 48 | .Ft struct servent * | ||
| 49 | .Fn getservent | ||
| 50 | .Ft struct servent * | ||
| 51 | .Fn getservbyname "char *name" "char *proto" | ||
| 52 | .Ft struct servent * | ||
| 53 | .Fn getservbyport "int port" proto | ||
| 54 | .Ft void | ||
| 55 | .Fn setservent "int stayopen" | ||
| 56 | .Ft void | ||
| 57 | .Fn endservent void | ||
| 58 | .Sh DESCRIPTION | ||
| 59 | The | ||
| 60 | .Fn getservent , | ||
| 61 | .Fn getservbyname , | ||
| 62 | and | ||
| 63 | .Fn getservbyport | ||
| 64 | functions | ||
| 65 | each return a pointer to an object with the | ||
| 66 | following structure | ||
| 67 | containing the broken-out | ||
| 68 | fields of a line in the network services data base, | ||
| 69 | .Pa /etc/services . | ||
| 70 | .Bd -literal -offset indent | ||
| 71 | struct servent { | ||
| 72 | char *s_name; /* official name of service */ | ||
| 73 | char **s_aliases; /* alias list */ | ||
| 74 | int s_port; /* port service resides at */ | ||
| 75 | char *s_proto; /* protocol to use */ | ||
| 76 | }; | ||
| 77 | .Ed | ||
| 78 | .Pp | ||
| 79 | The members of this structure are: | ||
| 80 | .Bl -tag -width s_aliases | ||
| 81 | .It Fa s_name | ||
| 82 | The official name of the service. | ||
| 83 | .It Fa s_aliases | ||
| 84 | A zero terminated list of alternate names for the service. | ||
| 85 | .It Fa s_port | ||
| 86 | The port number at which the service resides. | ||
| 87 | Port numbers are returned in network byte order. | ||
| 88 | .It Fa s_proto | ||
| 89 | The name of the protocol to use when contacting the | ||
| 90 | service. | ||
| 91 | .El | ||
| 92 | .Pp | ||
| 93 | The | ||
| 94 | .Fn getservent | ||
| 95 | function | ||
| 96 | reads the next line of the file, opening the file if necessary. | ||
| 97 | .Pp | ||
| 98 | The | ||
| 99 | .Fn setservent | ||
| 100 | function | ||
| 101 | opens and rewinds the file. If the | ||
| 102 | .Fa stayopen | ||
| 103 | flag is non-zero, | ||
| 104 | the net data base will not be closed after each call to | ||
| 105 | .Fn getservbyname | ||
| 106 | or | ||
| 107 | .Fn getservbyport . | ||
| 108 | .Pp | ||
| 109 | The | ||
| 110 | .Fn endservent | ||
| 111 | function | ||
| 112 | closes the file. | ||
| 113 | .Pp | ||
| 114 | The | ||
| 115 | .Fn getservbyname | ||
| 116 | and | ||
| 117 | .Fn getservbyport | ||
| 118 | functions | ||
| 119 | sequentially search from the beginning | ||
| 120 | of the file until a matching | ||
| 121 | protocol name or | ||
| 122 | port number is found, | ||
| 123 | or until | ||
| 124 | .Dv EOF | ||
| 125 | is encountered. | ||
| 126 | If a protocol name is also supplied (non- | ||
| 127 | .Dv NULL ) , | ||
| 128 | searches must also match the protocol. | ||
| 129 | .ne 1i | ||
| 130 | .Sh FILES | ||
| 131 | .Bl -tag -width /etc/services -compact | ||
| 132 | .It Pa /etc/services | ||
| 133 | .El | ||
| 134 | .Sh DIAGNOSTICS | ||
| 135 | Null pointer | ||
| 136 | (0) returned on | ||
| 137 | .Dv EOF | ||
| 138 | or error. | ||
| 139 | .Sh SEE ALSO | ||
| 140 | .Xr getprotoent 3 , | ||
| 141 | .Xr services 5 | ||
| 142 | .Sh HISTORY | ||
| 143 | The | ||
| 144 | .Fn getservent , | ||
| 145 | .Fn getservbyport , | ||
| 146 | .Fn getservbyname , | ||
| 147 | .Fn setservent , | ||
| 148 | and | ||
| 149 | .Fn endservent | ||
| 150 | functions appeared in | ||
| 151 | .Bx 4.2 . | ||
| 152 | .Sh BUGS | ||
| 153 | These functions use static data storage; | ||
| 154 | if the data is needed for future use, it should be | ||
| 155 | copied before any subsequent calls overwrite it. | ||
| 156 | Expecting port numbers to fit in a 32 bit | ||
| 157 | quantity is probably naive. | ||
diff --git a/src/lib/libc/net/getservent.c b/src/lib/libc/net/getservent.c new file mode 100644 index 0000000000..316891450e --- /dev/null +++ b/src/lib/libc/net/getservent.c | |||
| @@ -0,0 +1,127 @@ | |||
| 1 | /* $NetBSD: getservent.c,v 1.4 1995/02/25 06:20:38 cgd Exp $ */ | ||
| 2 | |||
| 3 | /* | ||
| 4 | * Copyright (c) 1983, 1993 | ||
| 5 | * The Regents of the University of California. All rights reserved. | ||
| 6 | * | ||
| 7 | * Redistribution and use in source and binary forms, with or without | ||
| 8 | * modification, are permitted provided that the following conditions | ||
| 9 | * are met: | ||
| 10 | * 1. Redistributions of source code must retain the above copyright | ||
| 11 | * notice, this list of conditions and the following disclaimer. | ||
| 12 | * 2. Redistributions in binary form must reproduce the above copyright | ||
| 13 | * notice, this list of conditions and the following disclaimer in the | ||
| 14 | * documentation and/or other materials provided with the distribution. | ||
| 15 | * 3. All advertising materials mentioning features or use of this software | ||
| 16 | * must display the following acknowledgement: | ||
| 17 | * This product includes software developed by the University of | ||
| 18 | * California, Berkeley and its contributors. | ||
| 19 | * 4. Neither the name of the University nor the names of its contributors | ||
| 20 | * may be used to endorse or promote products derived from this software | ||
| 21 | * without specific prior written permission. | ||
| 22 | * | ||
| 23 | * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND | ||
| 24 | * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE | ||
| 25 | * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE | ||
| 26 | * ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE | ||
| 27 | * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL | ||
| 28 | * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS | ||
| 29 | * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) | ||
| 30 | * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT | ||
| 31 | * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY | ||
| 32 | * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF | ||
| 33 | * SUCH DAMAGE. | ||
| 34 | */ | ||
| 35 | |||
| 36 | #if defined(LIBC_SCCS) && !defined(lint) | ||
| 37 | #if 0 | ||
| 38 | static char sccsid[] = "@(#)getservent.c 8.1 (Berkeley) 6/4/93"; | ||
| 39 | #else | ||
| 40 | static char rcsid[] = "$NetBSD: getservent.c,v 1.4 1995/02/25 06:20:38 cgd Exp $"; | ||
| 41 | #endif | ||
| 42 | #endif /* LIBC_SCCS and not lint */ | ||
| 43 | |||
| 44 | #include <sys/types.h> | ||
| 45 | #include <sys/socket.h> | ||
| 46 | #include <netdb.h> | ||
| 47 | #include <stdio.h> | ||
| 48 | #include <string.h> | ||
| 49 | #include <stdlib.h> | ||
| 50 | |||
| 51 | #define MAXALIASES 35 | ||
| 52 | |||
| 53 | static FILE *servf = NULL; | ||
| 54 | static char line[BUFSIZ+1]; | ||
| 55 | static struct servent serv; | ||
| 56 | static char *serv_aliases[MAXALIASES]; | ||
| 57 | int _serv_stayopen; | ||
| 58 | |||
| 59 | void | ||
| 60 | setservent(f) | ||
| 61 | int f; | ||
| 62 | { | ||
| 63 | if (servf == NULL) | ||
| 64 | servf = fopen(_PATH_SERVICES, "r" ); | ||
| 65 | else | ||
| 66 | rewind(servf); | ||
| 67 | _serv_stayopen |= f; | ||
| 68 | } | ||
| 69 | |||
| 70 | void | ||
| 71 | endservent() | ||
| 72 | { | ||
| 73 | if (servf) { | ||
| 74 | fclose(servf); | ||
| 75 | servf = NULL; | ||
| 76 | } | ||
| 77 | _serv_stayopen = 0; | ||
| 78 | } | ||
| 79 | |||
| 80 | struct servent * | ||
| 81 | getservent() | ||
| 82 | { | ||
| 83 | char *p; | ||
| 84 | register char *cp, **q; | ||
| 85 | |||
| 86 | if (servf == NULL && (servf = fopen(_PATH_SERVICES, "r" )) == NULL) | ||
| 87 | return (NULL); | ||
| 88 | again: | ||
| 89 | if ((p = fgets(line, BUFSIZ, servf)) == NULL) | ||
| 90 | return (NULL); | ||
| 91 | if (*p == '#') | ||
| 92 | goto again; | ||
| 93 | cp = strpbrk(p, "#\n"); | ||
| 94 | if (cp == NULL) | ||
| 95 | goto again; | ||
| 96 | *cp = '\0'; | ||
| 97 | serv.s_name = p; | ||
| 98 | p = strpbrk(p, " \t"); | ||
| 99 | if (p == NULL) | ||
| 100 | goto again; | ||
| 101 | *p++ = '\0'; | ||
| 102 | while (*p == ' ' || *p == '\t') | ||
| 103 | p++; | ||
| 104 | cp = strpbrk(p, ",/"); | ||
| 105 | if (cp == NULL) | ||
| 106 | goto again; | ||
| 107 | *cp++ = '\0'; | ||
| 108 | serv.s_port = htons((u_short)atoi(p)); | ||
| 109 | serv.s_proto = cp; | ||
| 110 | q = serv.s_aliases = serv_aliases; | ||
| 111 | cp = strpbrk(cp, " \t"); | ||
| 112 | if (cp != NULL) | ||
| 113 | *cp++ = '\0'; | ||
| 114 | while (cp && *cp) { | ||
| 115 | if (*cp == ' ' || *cp == '\t') { | ||
| 116 | cp++; | ||
| 117 | continue; | ||
| 118 | } | ||
| 119 | if (q < &serv_aliases[MAXALIASES - 1]) | ||
| 120 | *q++ = cp; | ||
| 121 | cp = strpbrk(cp, " \t"); | ||
| 122 | if (cp != NULL) | ||
| 123 | *cp++ = '\0'; | ||
| 124 | } | ||
| 125 | *q = NULL; | ||
| 126 | return (&serv); | ||
| 127 | } | ||
diff --git a/src/lib/libc/net/herror.c b/src/lib/libc/net/herror.c new file mode 100644 index 0000000000..41adbf1055 --- /dev/null +++ b/src/lib/libc/net/herror.c | |||
| @@ -0,0 +1,115 @@ | |||
| 1 | /* $NetBSD: herror.c,v 1.5 1995/02/25 06:20:39 cgd Exp $ */ | ||
| 2 | |||
| 3 | /*- | ||
| 4 | * Copyright (c) 1987, 1993 | ||
| 5 | * The Regents of the University of California. All rights reserved. | ||
| 6 | * | ||
| 7 | * Redistribution and use in source and binary forms, with or without | ||
| 8 | * modification, are permitted provided that the following conditions | ||
| 9 | * are met: | ||
| 10 | * 1. Redistributions of source code must retain the above copyright | ||
| 11 | * notice, this list of conditions and the following disclaimer. | ||
| 12 | * 2. Redistributions in binary form must reproduce the above copyright | ||
| 13 | * notice, this list of conditions and the following disclaimer in the | ||
| 14 | * documentation and/or other materials provided with the distribution. | ||
| 15 | * 3. All advertising materials mentioning features or use of this software | ||
| 16 | * must display the following acknowledgement: | ||
| 17 | * This product includes software developed by the University of | ||
| 18 | * California, Berkeley and its contributors. | ||
| 19 | * 4. Neither the name of the University nor the names of its contributors | ||
| 20 | * may be used to endorse or promote products derived from this software | ||
| 21 | * without specific prior written permission. | ||
| 22 | * | ||
| 23 | * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND | ||
| 24 | * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE | ||
| 25 | * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE | ||
| 26 | * ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE | ||
| 27 | * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL | ||
| 28 | * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS | ||
| 29 | * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) | ||
| 30 | * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT | ||
| 31 | * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY | ||
| 32 | * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF | ||
| 33 | * SUCH DAMAGE. | ||
| 34 | * - | ||
| 35 | * Portions Copyright (c) 1993 by Digital Equipment Corporation. | ||
| 36 | * | ||
| 37 | * Permission to use, copy, modify, and distribute this software for any | ||
| 38 | * purpose with or without fee is hereby granted, provided that the above | ||
| 39 | * copyright notice and this permission notice appear in all copies, and that | ||
| 40 | * the name of Digital Equipment Corporation not be used in advertising or | ||
| 41 | * publicity pertaining to distribution of the document or software without | ||
| 42 | * specific, written prior permission. | ||
| 43 | * | ||
| 44 | * THE SOFTWARE IS PROVIDED "AS IS" AND DIGITAL EQUIPMENT CORP. DISCLAIMS ALL | ||
| 45 | * WARRANTIES WITH REGARD TO THIS SOFTWARE, INCLUDING ALL IMPLIED WARRANTIES | ||
| 46 | * OF MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL DIGITAL EQUIPMENT | ||
| 47 | * CORPORATION BE LIABLE FOR ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL | ||
| 48 | * DAMAGES OR ANY DAMAGES WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR | ||
| 49 | * PROFITS, WHETHER IN AN ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS | ||
| 50 | * ACTION, ARISING OUT OF OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS | ||
| 51 | * SOFTWARE. | ||
| 52 | * - | ||
| 53 | * --Copyright-- | ||
| 54 | */ | ||
| 55 | |||
| 56 | #if defined(LIBC_SCCS) && !defined(lint) | ||
| 57 | #if 0 | ||
| 58 | static char sccsid[] = "@(#)herror.c 8.1 (Berkeley) 6/4/93"; | ||
| 59 | static char rcsid[] = "$Id: herror.c,v 4.9.1.1 1993/05/02 23:14:35 vixie Rel "; | ||
| 60 | #else | ||
| 61 | static char rcsid[] = "$NetBSD: herror.c,v 1.5 1995/02/25 06:20:39 cgd Exp $"; | ||
| 62 | #endif | ||
| 63 | #endif /* LIBC_SCCS and not lint */ | ||
| 64 | |||
| 65 | #include <sys/types.h> | ||
| 66 | #include <sys/uio.h> | ||
| 67 | #include <netdb.h> | ||
| 68 | #include <unistd.h> | ||
| 69 | #include <string.h> | ||
| 70 | |||
| 71 | char *h_errlist[] = { | ||
| 72 | "Error 0", | ||
| 73 | "Unknown host", /* 1 HOST_NOT_FOUND */ | ||
| 74 | "Host name lookup failure", /* 2 TRY_AGAIN */ | ||
| 75 | "Unknown server error", /* 3 NO_RECOVERY */ | ||
| 76 | "No address associated with name", /* 4 NO_ADDRESS */ | ||
| 77 | }; | ||
| 78 | int h_nerr = { sizeof(h_errlist)/sizeof(h_errlist[0]) }; | ||
| 79 | |||
| 80 | extern int h_errno; | ||
| 81 | |||
| 82 | /* | ||
| 83 | * herror -- | ||
| 84 | * print the error indicated by the h_errno value. | ||
| 85 | */ | ||
| 86 | void | ||
| 87 | herror(s) | ||
| 88 | const char *s; | ||
| 89 | { | ||
| 90 | struct iovec iov[4]; | ||
| 91 | register struct iovec *v = iov; | ||
| 92 | |||
| 93 | if (s && *s) { | ||
| 94 | v->iov_base = (char *)s; | ||
| 95 | v->iov_len = strlen(s); | ||
| 96 | v++; | ||
| 97 | v->iov_base = ": "; | ||
| 98 | v->iov_len = 2; | ||
| 99 | v++; | ||
| 100 | } | ||
| 101 | v->iov_base = (u_int)h_errno < h_nerr ? | ||
| 102 | h_errlist[h_errno] : "Unknown error"; | ||
| 103 | v->iov_len = strlen(v->iov_base); | ||
| 104 | v++; | ||
| 105 | v->iov_base = "\n"; | ||
| 106 | v->iov_len = 1; | ||
| 107 | writev(STDERR_FILENO, iov, (v - iov) + 1); | ||
| 108 | } | ||
| 109 | |||
| 110 | char * | ||
| 111 | hstrerror(err) | ||
| 112 | int err; | ||
| 113 | { | ||
| 114 | return (u_int)err < h_nerr ? h_errlist[err] : "Unknown resolver error"; | ||
| 115 | } | ||
diff --git a/src/lib/libc/net/htonl.c b/src/lib/libc/net/htonl.c new file mode 100644 index 0000000000..ac85cb0fd7 --- /dev/null +++ b/src/lib/libc/net/htonl.c | |||
| @@ -0,0 +1,29 @@ | |||
| 1 | /* $NetBSD: htonl.c,v 1.5 1995/04/28 23:25:14 jtc Exp $ */ | ||
| 2 | |||
| 3 | /* | ||
| 4 | * Written by J.T. Conklin <jtc@netbsd.org>. | ||
| 5 | * Public domain. | ||
| 6 | */ | ||
| 7 | |||
| 8 | #if defined(LIBC_SCCS) && !defined(lint) | ||
| 9 | static char *rcsid = "$NetBSD: htonl.c,v 1.5 1995/04/28 23:25:14 jtc Exp $"; | ||
| 10 | #endif | ||
| 11 | |||
| 12 | #include <sys/types.h> | ||
| 13 | #include <machine/endian.h> | ||
| 14 | |||
| 15 | #undef htonl | ||
| 16 | |||
| 17 | unsigned long | ||
| 18 | htonl(x) | ||
| 19 | unsigned long x; | ||
| 20 | { | ||
| 21 | u_int32_t y = x; | ||
| 22 | |||
| 23 | #if BYTE_ORDER == LITTLE_ENDIAN | ||
| 24 | u_char *s = (u_char *)&y; | ||
| 25 | return s[0] << 24 | s[1] << 16 | s[2] << 8 | s[3]; | ||
| 26 | #else | ||
| 27 | return y; | ||
| 28 | #endif | ||
| 29 | } | ||
diff --git a/src/lib/libc/net/htons.c b/src/lib/libc/net/htons.c new file mode 100644 index 0000000000..b2500a51d1 --- /dev/null +++ b/src/lib/libc/net/htons.c | |||
| @@ -0,0 +1,27 @@ | |||
| 1 | /* $NetBSD: htons.c,v 1.5 1995/04/28 23:25:19 jtc Exp $ */ | ||
| 2 | |||
| 3 | /* | ||
| 4 | * Written by J.T. Conklin <jtc@netbsd.org>. | ||
| 5 | * Public domain. | ||
| 6 | */ | ||
| 7 | |||
| 8 | #if defined(LIBC_SCCS) && !defined(lint) | ||
| 9 | static char *rcsid = "$NetBSD: htons.c,v 1.5 1995/04/28 23:25:19 jtc Exp $"; | ||
| 10 | #endif | ||
| 11 | |||
| 12 | #include <sys/types.h> | ||
| 13 | #include <machine/endian.h> | ||
| 14 | |||
| 15 | #undef htons | ||
| 16 | |||
| 17 | unsigned short | ||
| 18 | htons(x) | ||
| 19 | unsigned short x; | ||
| 20 | { | ||
| 21 | #if BYTE_ORDER == LITTLE_ENDIAN | ||
| 22 | u_char *s = (u_char *) &x; | ||
| 23 | return s[0] << 8 | s[1]; | ||
| 24 | #else | ||
| 25 | return x; | ||
| 26 | #endif | ||
| 27 | } | ||
diff --git a/src/lib/libc/net/inet.3 b/src/lib/libc/net/inet.3 new file mode 100644 index 0000000000..49bac97e96 --- /dev/null +++ b/src/lib/libc/net/inet.3 | |||
| @@ -0,0 +1,197 @@ | |||
| 1 | .\" $NetBSD: inet.3,v 1.4 1995/02/27 09:45:26 chopps Exp $ | ||
| 2 | .\" | ||
| 3 | .\" Copyright (c) 1983, 1990, 1991, 1993 | ||
| 4 | .\" The Regents of the University of California. All rights reserved. | ||
| 5 | .\" | ||
| 6 | .\" Redistribution and use in source and binary forms, with or without | ||
| 7 | .\" modification, are permitted provided that the following conditions | ||
| 8 | .\" are met: | ||
| 9 | .\" 1. Redistributions of source code must retain the above copyright | ||
| 10 | .\" notice, this list of conditions and the following disclaimer. | ||
| 11 | .\" 2. Redistributions in binary form must reproduce the above copyright | ||
| 12 | .\" notice, this list of conditions and the following disclaimer in the | ||
| 13 | .\" documentation and/or other materials provided with the distribution. | ||
| 14 | .\" 3. All advertising materials mentioning features or use of this software | ||
| 15 | .\" must display the following acknowledgement: | ||
| 16 | .\" This product includes software developed by the University of | ||
| 17 | .\" California, Berkeley and its contributors. | ||
| 18 | .\" 4. Neither the name of the University nor the names of its contributors | ||
| 19 | .\" may be used to endorse or promote products derived from this software | ||
| 20 | .\" without specific prior written permission. | ||
| 21 | .\" | ||
| 22 | .\" THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND | ||
| 23 | .\" ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE | ||
| 24 | .\" IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE | ||
| 25 | .\" ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE | ||
| 26 | .\" FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL | ||
| 27 | .\" DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS | ||
| 28 | .\" OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) | ||
| 29 | .\" HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT | ||
| 30 | .\" LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY | ||
| 31 | .\" OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF | ||
| 32 | .\" SUCH DAMAGE. | ||
| 33 | .\" | ||
| 34 | .\" @(#)inet.3 8.1 (Berkeley) 6/4/93 | ||
| 35 | .\" | ||
| 36 | .Dd June 4, 1993 | ||
| 37 | .Dt INET 3 | ||
| 38 | .Os BSD 4.2 | ||
| 39 | .Sh NAME | ||
| 40 | .Nm inet_aton , | ||
| 41 | .Nm inet_addr , | ||
| 42 | .Nm inet_network , | ||
| 43 | .Nm inet_ntoa , | ||
| 44 | .Nm inet_makeaddr , | ||
| 45 | .Nm inet_lnaof , | ||
| 46 | .Nm inet_netof | ||
| 47 | .Nd Internet address manipulation routines | ||
| 48 | .Sh SYNOPSIS | ||
| 49 | .Fd #include <sys/socket.h> | ||
| 50 | .Fd #include <netinet/in.h> | ||
| 51 | .Fd #include <arpa/inet.h> | ||
| 52 | .Ft int | ||
| 53 | .Fn inet_aton "const char *cp" "struct in_addr *pin" | ||
| 54 | .Ft unsigned long | ||
| 55 | .Fn inet_addr "const char *cp" | ||
| 56 | .Ft unsigned long | ||
| 57 | .Fn inet_network "const char *cp" | ||
| 58 | .Ft char * | ||
| 59 | .Fn inet_ntoa "struct in_addr in" | ||
| 60 | .Ft struct in_addr | ||
| 61 | .Fn inet_makeaddr "int net" "int lna" | ||
| 62 | .Ft unsigned long | ||
| 63 | .Fn inet_lnaof "struct in_addr in" | ||
| 64 | .Ft unsigned long | ||
| 65 | .Fn inet_netof "struct in_addr in" | ||
| 66 | .Sh DESCRIPTION | ||
| 67 | The routines | ||
| 68 | .Fn inet_aton , | ||
| 69 | .Fn inet_addr | ||
| 70 | and | ||
| 71 | .Fn inet_network | ||
| 72 | interpret character strings representing | ||
| 73 | numbers expressed in the Internet standard | ||
| 74 | .Ql \&. | ||
| 75 | notation. | ||
| 76 | The | ||
| 77 | .Fn inet_aton | ||
| 78 | routine interprets the specified character string as an Internet address, | ||
| 79 | placing the address into the structure provided. | ||
| 80 | It returns 1 if the string was successfully interpreted, | ||
| 81 | or 0 if the string is invalid. | ||
| 82 | The | ||
| 83 | .Fn inet_addr | ||
| 84 | and | ||
| 85 | .Fn inet_network | ||
| 86 | functions return numbers suitable for use | ||
| 87 | as Internet addresses and Internet network | ||
| 88 | numbers, respectively. | ||
| 89 | The routine | ||
| 90 | .Fn inet_ntoa | ||
| 91 | takes an Internet address and returns an | ||
| 92 | .Tn ASCII | ||
| 93 | string representing the address in | ||
| 94 | .Ql \&. | ||
| 95 | notation. The routine | ||
| 96 | .Fn inet_makeaddr | ||
| 97 | takes an Internet network number and a local | ||
| 98 | network address and constructs an Internet address | ||
| 99 | from it. The routines | ||
| 100 | .Fn inet_netof | ||
| 101 | and | ||
| 102 | .Fn inet_lnaof | ||
| 103 | break apart Internet host addresses, returning | ||
| 104 | the network number and local network address part, | ||
| 105 | respectively. | ||
| 106 | .Pp | ||
| 107 | All Internet addresses are returned in network | ||
| 108 | order (bytes ordered from left to right). | ||
| 109 | All network numbers and local address parts are | ||
| 110 | returned as machine format integer values. | ||
| 111 | .Sh INTERNET ADDRESSES | ||
| 112 | Values specified using the | ||
| 113 | .Ql \&. | ||
| 114 | notation take one | ||
| 115 | of the following forms: | ||
| 116 | .Bd -literal -offset indent | ||
| 117 | a.b.c.d | ||
| 118 | a.b.c | ||
| 119 | a.b | ||
| 120 | a | ||
| 121 | .Ed | ||
| 122 | .Pp | ||
| 123 | When four parts are specified, each is interpreted | ||
| 124 | as a byte of data and assigned, from left to right, | ||
| 125 | to the four bytes of an Internet address. Note | ||
| 126 | that when an Internet address is viewed as a 32-bit | ||
| 127 | integer quantity on the | ||
| 128 | .Tn VAX | ||
| 129 | the bytes referred to | ||
| 130 | above appear as | ||
| 131 | .Dq Li d.c.b.a . | ||
| 132 | That is, | ||
| 133 | .Tn VAX | ||
| 134 | bytes are | ||
| 135 | ordered from right to left. | ||
| 136 | .Pp | ||
| 137 | When a three part address is specified, the last | ||
| 138 | part is interpreted as a 16-bit quantity and placed | ||
| 139 | in the right-most two bytes of the network address. | ||
| 140 | This makes the three part address format convenient | ||
| 141 | for specifying Class B network addresses as | ||
| 142 | .Dq Li 128.net.host . | ||
| 143 | .Pp | ||
| 144 | When a two part address is supplied, the last part | ||
| 145 | is interpreted as a 24-bit quantity and placed in | ||
| 146 | the right most three bytes of the network address. | ||
| 147 | This makes the two part address format convenient | ||
| 148 | for specifying Class A network addresses as | ||
| 149 | .Dq Li net.host . | ||
| 150 | .Pp | ||
| 151 | When only one part is given, the value is stored | ||
| 152 | directly in the network address without any byte | ||
| 153 | rearrangement. | ||
| 154 | .Pp | ||
| 155 | All numbers supplied as | ||
| 156 | .Dq parts | ||
| 157 | in a | ||
| 158 | .Ql \&. | ||
| 159 | notation | ||
| 160 | may be decimal, octal, or hexadecimal, as specified | ||
| 161 | in the C language (i.e., a leading 0x or 0X implies | ||
| 162 | hexadecimal; otherwise, a leading 0 implies octal; | ||
| 163 | otherwise, the number is interpreted as decimal). | ||
| 164 | .Sh DIAGNOSTICS | ||
| 165 | The constant | ||
| 166 | .Dv INADDR_NONE | ||
| 167 | is returned by | ||
| 168 | .Fn inet_addr | ||
| 169 | and | ||
| 170 | .Fn inet_network | ||
| 171 | for malformed requests. | ||
| 172 | .Sh SEE ALSO | ||
| 173 | .Xr gethostbyname 3 , | ||
| 174 | .Xr getnetent 3 , | ||
| 175 | .Xr hosts 5 , | ||
| 176 | .Xr networks 5 , | ||
| 177 | .Sh HISTORY | ||
| 178 | These | ||
| 179 | functions appeared in | ||
| 180 | .Bx 4.2 . | ||
| 181 | .Sh BUGS | ||
| 182 | The value | ||
| 183 | .Dv INADDR_NONE | ||
| 184 | (0xffffffff) is a valid broadcast address, but | ||
| 185 | .Fn inet_addr | ||
| 186 | cannot return that value without indicating failure. | ||
| 187 | The newer | ||
| 188 | .Fn inet_aton | ||
| 189 | function does not share this problem. | ||
| 190 | The problem of host byte ordering versus network byte ordering is | ||
| 191 | confusing. | ||
| 192 | The string returned by | ||
| 193 | .Fn inet_ntoa | ||
| 194 | resides in a static memory area. | ||
| 195 | .Pp | ||
| 196 | Inet_addr should return a | ||
| 197 | .Fa struct in_addr . | ||
diff --git a/src/lib/libc/net/inet_addr.c b/src/lib/libc/net/inet_addr.c new file mode 100644 index 0000000000..b5b9d8302f --- /dev/null +++ b/src/lib/libc/net/inet_addr.c | |||
| @@ -0,0 +1,158 @@ | |||
| 1 | /* $NetBSD: inet_addr.c,v 1.5 1995/02/25 06:20:41 cgd Exp $ */ | ||
| 2 | |||
| 3 | /* | ||
| 4 | * Copyright (c) 1983, 1990, 1993 | ||
| 5 | * The Regents of the University of California. All rights reserved. | ||
| 6 | * | ||
| 7 | * Redistribution and use in source and binary forms, with or without | ||
| 8 | * modification, are permitted provided that the following conditions | ||
| 9 | * are met: | ||
| 10 | * 1. Redistributions of source code must retain the above copyright | ||
| 11 | * notice, this list of conditions and the following disclaimer. | ||
| 12 | * 2. Redistributions in binary form must reproduce the above copyright | ||
| 13 | * notice, this list of conditions and the following disclaimer in the | ||
| 14 | * documentation and/or other materials provided with the distribution. | ||
| 15 | * 3. All advertising materials mentioning features or use of this software | ||
| 16 | * must display the following acknowledgement: | ||
| 17 | * This product includes software developed by the University of | ||
| 18 | * California, Berkeley and its contributors. | ||
| 19 | * 4. Neither the name of the University nor the names of its contributors | ||
| 20 | * may be used to endorse or promote products derived from this software | ||
| 21 | * without specific prior written permission. | ||
| 22 | * | ||
| 23 | * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND | ||
| 24 | * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE | ||
| 25 | * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE | ||
| 26 | * ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE | ||
| 27 | * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL | ||
| 28 | * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS | ||
| 29 | * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) | ||
| 30 | * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT | ||
| 31 | * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY | ||
| 32 | * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF | ||
| 33 | * SUCH DAMAGE. | ||
| 34 | */ | ||
| 35 | |||
| 36 | #if defined(LIBC_SCCS) && !defined(lint) | ||
| 37 | #if 0 | ||
| 38 | static char sccsid[] = "@(#)inet_addr.c 8.1 (Berkeley) 6/17/93"; | ||
| 39 | #else | ||
| 40 | static char rcsid[] = "$NetBSD: inet_addr.c,v 1.5 1995/02/25 06:20:41 cgd Exp $"; | ||
| 41 | #endif | ||
| 42 | #endif /* LIBC_SCCS and not lint */ | ||
| 43 | |||
| 44 | #include <sys/param.h> | ||
| 45 | #include <netinet/in.h> | ||
| 46 | #include <arpa/inet.h> | ||
| 47 | #include <ctype.h> | ||
| 48 | |||
| 49 | /* | ||
| 50 | * Ascii internet address interpretation routine. | ||
| 51 | * The value returned is in network order. | ||
| 52 | */ | ||
| 53 | u_long | ||
| 54 | inet_addr(cp) | ||
| 55 | register const char *cp; | ||
| 56 | { | ||
| 57 | struct in_addr val; | ||
| 58 | |||
| 59 | if (inet_aton(cp, &val)) | ||
| 60 | return (val.s_addr); | ||
| 61 | return (INADDR_NONE); | ||
| 62 | } | ||
| 63 | |||
| 64 | /* | ||
| 65 | * Check whether "cp" is a valid ascii representation | ||
| 66 | * of an Internet address and convert to a binary address. | ||
| 67 | * Returns 1 if the address is valid, 0 if not. | ||
| 68 | * This replaces inet_addr, the return value from which | ||
| 69 | * cannot distinguish between failure and a local broadcast address. | ||
| 70 | */ | ||
| 71 | int | ||
| 72 | inet_aton(cp, addr) | ||
| 73 | register const char *cp; | ||
| 74 | struct in_addr *addr; | ||
| 75 | { | ||
| 76 | register u_long val; | ||
| 77 | register int base, n; | ||
| 78 | register char c; | ||
| 79 | u_int parts[4]; | ||
| 80 | register u_int *pp = parts; | ||
| 81 | |||
| 82 | for (;;) { | ||
| 83 | /* | ||
| 84 | * Collect number up to ``.''. | ||
| 85 | * Values are specified as for C: | ||
| 86 | * 0x=hex, 0=octal, other=decimal. | ||
| 87 | */ | ||
| 88 | val = 0; base = 10; | ||
| 89 | if (*cp == '0') { | ||
| 90 | if (*++cp == 'x' || *cp == 'X') | ||
| 91 | base = 16, cp++; | ||
| 92 | else | ||
| 93 | base = 8; | ||
| 94 | } | ||
| 95 | while ((c = *cp) != '\0') { | ||
| 96 | if (isascii(c) && isdigit(c)) { | ||
| 97 | val = (val * base) + (c - '0'); | ||
| 98 | cp++; | ||
| 99 | continue; | ||
| 100 | } | ||
| 101 | if (base == 16 && isascii(c) && isxdigit(c)) { | ||
| 102 | val = (val << 4) + | ||
| 103 | (c + 10 - (islower(c) ? 'a' : 'A')); | ||
| 104 | cp++; | ||
| 105 | continue; | ||
| 106 | } | ||
| 107 | break; | ||
| 108 | } | ||
| 109 | if (*cp == '.') { | ||
| 110 | /* | ||
| 111 | * Internet format: | ||
| 112 | * a.b.c.d | ||
| 113 | * a.b.c (with c treated as 16-bits) | ||
| 114 | * a.b (with b treated as 24 bits) | ||
| 115 | */ | ||
| 116 | if (pp >= parts + 3 || val > 0xff) | ||
| 117 | return (0); | ||
| 118 | *pp++ = val, cp++; | ||
| 119 | } else | ||
| 120 | break; | ||
| 121 | } | ||
| 122 | /* | ||
| 123 | * Check for trailing characters. | ||
| 124 | */ | ||
| 125 | if (*cp && (!isascii(*cp) || !isspace(*cp))) | ||
| 126 | return (0); | ||
| 127 | /* | ||
| 128 | * Concoct the address according to | ||
| 129 | * the number of parts specified. | ||
| 130 | */ | ||
| 131 | n = pp - parts + 1; | ||
| 132 | switch (n) { | ||
| 133 | |||
| 134 | case 1: /* a -- 32 bits */ | ||
| 135 | break; | ||
| 136 | |||
| 137 | case 2: /* a.b -- 8.24 bits */ | ||
| 138 | if (val > 0xffffff) | ||
| 139 | return (0); | ||
| 140 | val |= parts[0] << 24; | ||
| 141 | break; | ||
| 142 | |||
| 143 | case 3: /* a.b.c -- 8.8.16 bits */ | ||
| 144 | if (val > 0xffff) | ||
| 145 | return (0); | ||
| 146 | val |= (parts[0] << 24) | (parts[1] << 16); | ||
| 147 | break; | ||
| 148 | |||
| 149 | case 4: /* a.b.c.d -- 8.8.8.8 bits */ | ||
| 150 | if (val > 0xff) | ||
| 151 | return (0); | ||
| 152 | val |= (parts[0] << 24) | (parts[1] << 16) | (parts[2] << 8); | ||
| 153 | break; | ||
| 154 | } | ||
| 155 | if (addr) | ||
| 156 | addr->s_addr = htonl(val); | ||
| 157 | return (1); | ||
| 158 | } | ||
diff --git a/src/lib/libc/net/inet_lnaof.c b/src/lib/libc/net/inet_lnaof.c new file mode 100644 index 0000000000..ce1257bf68 --- /dev/null +++ b/src/lib/libc/net/inet_lnaof.c | |||
| @@ -0,0 +1,65 @@ | |||
| 1 | /* $NetBSD: inet_lnaof.c,v 1.4 1995/02/25 06:20:42 cgd Exp $ */ | ||
| 2 | |||
| 3 | /* | ||
| 4 | * Copyright (c) 1983, 1993 | ||
| 5 | * The Regents of the University of California. All rights reserved. | ||
| 6 | * | ||
| 7 | * Redistribution and use in source and binary forms, with or without | ||
| 8 | * modification, are permitted provided that the following conditions | ||
| 9 | * are met: | ||
| 10 | * 1. Redistributions of source code must retain the above copyright | ||
| 11 | * notice, this list of conditions and the following disclaimer. | ||
| 12 | * 2. Redistributions in binary form must reproduce the above copyright | ||
| 13 | * notice, this list of conditions and the following disclaimer in the | ||
| 14 | * documentation and/or other materials provided with the distribution. | ||
| 15 | * 3. All advertising materials mentioning features or use of this software | ||
| 16 | * must display the following acknowledgement: | ||
| 17 | * This product includes software developed by the University of | ||
| 18 | * California, Berkeley and its contributors. | ||
| 19 | * 4. Neither the name of the University nor the names of its contributors | ||
| 20 | * may be used to endorse or promote products derived from this software | ||
| 21 | * without specific prior written permission. | ||
| 22 | * | ||
| 23 | * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND | ||
| 24 | * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE | ||
| 25 | * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE | ||
| 26 | * ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE | ||
| 27 | * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL | ||
| 28 | * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS | ||
| 29 | * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) | ||
| 30 | * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT | ||
| 31 | * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY | ||
| 32 | * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF | ||
| 33 | * SUCH DAMAGE. | ||
| 34 | */ | ||
| 35 | |||
| 36 | #if defined(LIBC_SCCS) && !defined(lint) | ||
| 37 | #if 0 | ||
| 38 | static char sccsid[] = "@(#)inet_lnaof.c 8.1 (Berkeley) 6/4/93"; | ||
| 39 | #else | ||
| 40 | static char rcsid[] = "$NetBSD: inet_lnaof.c,v 1.4 1995/02/25 06:20:42 cgd Exp $"; | ||
| 41 | #endif | ||
| 42 | #endif /* LIBC_SCCS and not lint */ | ||
| 43 | |||
| 44 | #include <sys/param.h> | ||
| 45 | #include <netinet/in.h> | ||
| 46 | #include <arpa/inet.h> | ||
| 47 | |||
| 48 | /* | ||
| 49 | * Return the local network address portion of an | ||
| 50 | * internet address; handles class a/b/c network | ||
| 51 | * number formats. | ||
| 52 | */ | ||
| 53 | u_long | ||
| 54 | inet_lnaof(in) | ||
| 55 | struct in_addr in; | ||
| 56 | { | ||
| 57 | register u_long i = ntohl(in.s_addr); | ||
| 58 | |||
| 59 | if (IN_CLASSA(i)) | ||
| 60 | return ((i)&IN_CLASSA_HOST); | ||
| 61 | else if (IN_CLASSB(i)) | ||
| 62 | return ((i)&IN_CLASSB_HOST); | ||
| 63 | else | ||
| 64 | return ((i)&IN_CLASSC_HOST); | ||
| 65 | } | ||
diff --git a/src/lib/libc/net/inet_makeaddr.c b/src/lib/libc/net/inet_makeaddr.c new file mode 100644 index 0000000000..84d366e03a --- /dev/null +++ b/src/lib/libc/net/inet_makeaddr.c | |||
| @@ -0,0 +1,68 @@ | |||
| 1 | /* $NetBSD: inet_makeaddr.c,v 1.4 1995/02/25 06:20:42 cgd Exp $ */ | ||
| 2 | |||
| 3 | /* | ||
| 4 | * Copyright (c) 1983, 1993 | ||
| 5 | * The Regents of the University of California. All rights reserved. | ||
| 6 | * | ||
| 7 | * Redistribution and use in source and binary forms, with or without | ||
| 8 | * modification, are permitted provided that the following conditions | ||
| 9 | * are met: | ||
| 10 | * 1. Redistributions of source code must retain the above copyright | ||
| 11 | * notice, this list of conditions and the following disclaimer. | ||
| 12 | * 2. Redistributions in binary form must reproduce the above copyright | ||
| 13 | * notice, this list of conditions and the following disclaimer in the | ||
| 14 | * documentation and/or other materials provided with the distribution. | ||
| 15 | * 3. All advertising materials mentioning features or use of this software | ||
| 16 | * must display the following acknowledgement: | ||
| 17 | * This product includes software developed by the University of | ||
| 18 | * California, Berkeley and its contributors. | ||
| 19 | * 4. Neither the name of the University nor the names of its contributors | ||
| 20 | * may be used to endorse or promote products derived from this software | ||
| 21 | * without specific prior written permission. | ||
| 22 | * | ||
| 23 | * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND | ||
| 24 | * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE | ||
| 25 | * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE | ||
| 26 | * ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE | ||
| 27 | * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL | ||
| 28 | * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS | ||
| 29 | * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) | ||
| 30 | * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT | ||
| 31 | * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY | ||
| 32 | * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF | ||
| 33 | * SUCH DAMAGE. | ||
| 34 | */ | ||
| 35 | |||
| 36 | #if defined(LIBC_SCCS) && !defined(lint) | ||
| 37 | #if 0 | ||
| 38 | static char sccsid[] = "@(#)inet_makeaddr.c 8.1 (Berkeley) 6/4/93"; | ||
| 39 | #else | ||
| 40 | static char rcsid[] = "$NetBSD: inet_makeaddr.c,v 1.4 1995/02/25 06:20:42 cgd Exp $"; | ||
| 41 | #endif | ||
| 42 | #endif /* LIBC_SCCS and not lint */ | ||
| 43 | |||
| 44 | #include <sys/param.h> | ||
| 45 | #include <netinet/in.h> | ||
| 46 | #include <arpa/inet.h> | ||
| 47 | |||
| 48 | /* | ||
| 49 | * Formulate an Internet address from network + host. Used in | ||
| 50 | * building addresses stored in the ifnet structure. | ||
| 51 | */ | ||
| 52 | struct in_addr | ||
| 53 | inet_makeaddr(net, host) | ||
| 54 | u_long net, host; | ||
| 55 | { | ||
| 56 | u_long addr; | ||
| 57 | |||
| 58 | if (net < 128) | ||
| 59 | addr = (net << IN_CLASSA_NSHIFT) | (host & IN_CLASSA_HOST); | ||
| 60 | else if (net < 65536) | ||
| 61 | addr = (net << IN_CLASSB_NSHIFT) | (host & IN_CLASSB_HOST); | ||
| 62 | else if (net < 16777216L) | ||
| 63 | addr = (net << IN_CLASSC_NSHIFT) | (host & IN_CLASSC_HOST); | ||
| 64 | else | ||
| 65 | addr = net | host; | ||
| 66 | addr = htonl(addr); | ||
| 67 | return (*(struct in_addr *)&addr); | ||
| 68 | } | ||
diff --git a/src/lib/libc/net/inet_netof.c b/src/lib/libc/net/inet_netof.c new file mode 100644 index 0000000000..02f52ca318 --- /dev/null +++ b/src/lib/libc/net/inet_netof.c | |||
| @@ -0,0 +1,64 @@ | |||
| 1 | /* $NetBSD: inet_netof.c,v 1.4 1995/02/25 06:20:43 cgd Exp $ */ | ||
| 2 | |||
| 3 | /* | ||
| 4 | * Copyright (c) 1983, 1993 | ||
| 5 | * The Regents of the University of California. All rights reserved. | ||
| 6 | * | ||
| 7 | * Redistribution and use in source and binary forms, with or without | ||
| 8 | * modification, are permitted provided that the following conditions | ||
| 9 | * are met: | ||
| 10 | * 1. Redistributions of source code must retain the above copyright | ||
| 11 | * notice, this list of conditions and the following disclaimer. | ||
| 12 | * 2. Redistributions in binary form must reproduce the above copyright | ||
| 13 | * notice, this list of conditions and the following disclaimer in the | ||
| 14 | * documentation and/or other materials provided with the distribution. | ||
| 15 | * 3. All advertising materials mentioning features or use of this software | ||
| 16 | * must display the following acknowledgement: | ||
| 17 | * This product includes software developed by the University of | ||
| 18 | * California, Berkeley and its contributors. | ||
| 19 | * 4. Neither the name of the University nor the names of its contributors | ||
| 20 | * may be used to endorse or promote products derived from this software | ||
| 21 | * without specific prior written permission. | ||
| 22 | * | ||
| 23 | * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND | ||
| 24 | * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE | ||
| 25 | * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE | ||
| 26 | * ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE | ||
| 27 | * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL | ||
| 28 | * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS | ||
| 29 | * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) | ||
| 30 | * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT | ||
| 31 | * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY | ||
| 32 | * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF | ||
| 33 | * SUCH DAMAGE. | ||
| 34 | */ | ||
| 35 | |||
| 36 | #if defined(LIBC_SCCS) && !defined(lint) | ||
| 37 | #if 0 | ||
| 38 | static char sccsid[] = "@(#)inet_netof.c 8.1 (Berkeley) 6/4/93"; | ||
| 39 | #else | ||
| 40 | static char rcsid[] = "$NetBSD: inet_netof.c,v 1.4 1995/02/25 06:20:43 cgd Exp $"; | ||
| 41 | #endif | ||
| 42 | #endif /* LIBC_SCCS and not lint */ | ||
| 43 | |||
| 44 | #include <sys/param.h> | ||
| 45 | #include <netinet/in.h> | ||
| 46 | #include <arpa/inet.h> | ||
| 47 | |||
| 48 | /* | ||
| 49 | * Return the network number from an internet | ||
| 50 | * address; handles class a/b/c network #'s. | ||
| 51 | */ | ||
| 52 | u_long | ||
| 53 | inet_netof(in) | ||
| 54 | struct in_addr in; | ||
| 55 | { | ||
| 56 | register u_long i = ntohl(in.s_addr); | ||
| 57 | |||
| 58 | if (IN_CLASSA(i)) | ||
| 59 | return (((i)&IN_CLASSA_NET) >> IN_CLASSA_NSHIFT); | ||
| 60 | else if (IN_CLASSB(i)) | ||
| 61 | return (((i)&IN_CLASSB_NET) >> IN_CLASSB_NSHIFT); | ||
| 62 | else | ||
| 63 | return (((i)&IN_CLASSC_NET) >> IN_CLASSC_NSHIFT); | ||
| 64 | } | ||
diff --git a/src/lib/libc/net/inet_network.c b/src/lib/libc/net/inet_network.c new file mode 100644 index 0000000000..35105fa75a --- /dev/null +++ b/src/lib/libc/net/inet_network.c | |||
| @@ -0,0 +1,99 @@ | |||
| 1 | /* $NetBSD: inet_network.c,v 1.4 1995/02/25 06:20:45 cgd Exp $ */ | ||
| 2 | |||
| 3 | /* | ||
| 4 | * Copyright (c) 1983, 1993 | ||
| 5 | * The Regents of the University of California. All rights reserved. | ||
| 6 | * | ||
| 7 | * Redistribution and use in source and binary forms, with or without | ||
| 8 | * modification, are permitted provided that the following conditions | ||
| 9 | * are met: | ||
| 10 | * 1. Redistributions of source code must retain the above copyright | ||
| 11 | * notice, this list of conditions and the following disclaimer. | ||
| 12 | * 2. Redistributions in binary form must reproduce the above copyright | ||
| 13 | * notice, this list of conditions and the following disclaimer in the | ||
| 14 | * documentation and/or other materials provided with the distribution. | ||
| 15 | * 3. All advertising materials mentioning features or use of this software | ||
| 16 | * must display the following acknowledgement: | ||
| 17 | * This product includes software developed by the University of | ||
| 18 | * California, Berkeley and its contributors. | ||
| 19 | * 4. Neither the name of the University nor the names of its contributors | ||
| 20 | * may be used to endorse or promote products derived from this software | ||
| 21 | * without specific prior written permission. | ||
| 22 | * | ||
| 23 | * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND | ||
| 24 | * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE | ||
| 25 | * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE | ||
| 26 | * ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE | ||
| 27 | * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL | ||
| 28 | * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS | ||
| 29 | * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) | ||
| 30 | * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT | ||
| 31 | * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY | ||
| 32 | * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF | ||
| 33 | * SUCH DAMAGE. | ||
| 34 | */ | ||
| 35 | |||
| 36 | #if defined(LIBC_SCCS) && !defined(lint) | ||
| 37 | #if 0 | ||
| 38 | static char sccsid[] = "@(#)inet_network.c 8.1 (Berkeley) 6/4/93"; | ||
| 39 | #else | ||
| 40 | static char rcsid[] = "$NetBSD: inet_network.c,v 1.4 1995/02/25 06:20:45 cgd Exp $"; | ||
| 41 | #endif | ||
| 42 | #endif /* LIBC_SCCS and not lint */ | ||
| 43 | |||
| 44 | #include <sys/types.h> | ||
| 45 | #include <netinet/in.h> | ||
| 46 | #include <arpa/inet.h> | ||
| 47 | #include <ctype.h> | ||
| 48 | |||
| 49 | /* | ||
| 50 | * Internet network address interpretation routine. | ||
| 51 | * The library routines call this routine to interpret | ||
| 52 | * network numbers. | ||
| 53 | */ | ||
| 54 | u_long | ||
| 55 | inet_network(cp) | ||
| 56 | register const char *cp; | ||
| 57 | { | ||
| 58 | register u_long val, base, n; | ||
| 59 | register char c; | ||
| 60 | u_long parts[4], *pp = parts; | ||
| 61 | register int i; | ||
| 62 | |||
| 63 | again: | ||
| 64 | val = 0; base = 10; | ||
| 65 | if (*cp == '0') | ||
| 66 | base = 8, cp++; | ||
| 67 | if (*cp == 'x' || *cp == 'X') | ||
| 68 | base = 16, cp++; | ||
| 69 | while (c = *cp) { | ||
| 70 | if (isdigit(c)) { | ||
| 71 | val = (val * base) + (c - '0'); | ||
| 72 | cp++; | ||
| 73 | continue; | ||
| 74 | } | ||
| 75 | if (base == 16 && isxdigit(c)) { | ||
| 76 | val = (val << 4) + (c + 10 - (islower(c) ? 'a' : 'A')); | ||
| 77 | cp++; | ||
| 78 | continue; | ||
| 79 | } | ||
| 80 | break; | ||
| 81 | } | ||
| 82 | if (*cp == '.') { | ||
| 83 | if (pp >= parts + 4) | ||
| 84 | return (INADDR_NONE); | ||
| 85 | *pp++ = val, cp++; | ||
| 86 | goto again; | ||
| 87 | } | ||
| 88 | if (*cp && !isspace(*cp)) | ||
| 89 | return (INADDR_NONE); | ||
| 90 | *pp++ = val; | ||
| 91 | n = pp - parts; | ||
| 92 | if (n > 4) | ||
| 93 | return (INADDR_NONE); | ||
| 94 | for (val = 0, i = 0; i < n; i++) { | ||
| 95 | val <<= 8; | ||
| 96 | val |= parts[i] & 0xff; | ||
| 97 | } | ||
| 98 | return (val); | ||
| 99 | } | ||
diff --git a/src/lib/libc/net/inet_ntoa.c b/src/lib/libc/net/inet_ntoa.c new file mode 100644 index 0000000000..2da0ab00ff --- /dev/null +++ b/src/lib/libc/net/inet_ntoa.c | |||
| @@ -0,0 +1,65 @@ | |||
| 1 | /* $NetBSD: inet_ntoa.c,v 1.4 1995/02/25 06:20:46 cgd Exp $ */ | ||
| 2 | |||
| 3 | /* | ||
| 4 | * Copyright (c) 1983, 1993 | ||
| 5 | * The Regents of the University of California. All rights reserved. | ||
| 6 | * | ||
| 7 | * Redistribution and use in source and binary forms, with or without | ||
| 8 | * modification, are permitted provided that the following conditions | ||
| 9 | * are met: | ||
| 10 | * 1. Redistributions of source code must retain the above copyright | ||
| 11 | * notice, this list of conditions and the following disclaimer. | ||
| 12 | * 2. Redistributions in binary form must reproduce the above copyright | ||
| 13 | * notice, this list of conditions and the following disclaimer in the | ||
| 14 | * documentation and/or other materials provided with the distribution. | ||
| 15 | * 3. All advertising materials mentioning features or use of this software | ||
| 16 | * must display the following acknowledgement: | ||
| 17 | * This product includes software developed by the University of | ||
| 18 | * California, Berkeley and its contributors. | ||
| 19 | * 4. Neither the name of the University nor the names of its contributors | ||
| 20 | * may be used to endorse or promote products derived from this software | ||
| 21 | * without specific prior written permission. | ||
| 22 | * | ||
| 23 | * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND | ||
| 24 | * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE | ||
| 25 | * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE | ||
| 26 | * ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE | ||
| 27 | * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL | ||
| 28 | * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS | ||
| 29 | * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) | ||
| 30 | * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT | ||
| 31 | * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY | ||
| 32 | * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF | ||
| 33 | * SUCH DAMAGE. | ||
| 34 | */ | ||
| 35 | |||
| 36 | #if defined(LIBC_SCCS) && !defined(lint) | ||
| 37 | #if 0 | ||
| 38 | static char sccsid[] = "@(#)inet_ntoa.c 8.1 (Berkeley) 6/4/93"; | ||
| 39 | #else | ||
| 40 | static char rcsid[] = "$NetBSD: inet_ntoa.c,v 1.4 1995/02/25 06:20:46 cgd Exp $"; | ||
| 41 | #endif | ||
| 42 | #endif /* LIBC_SCCS and not lint */ | ||
| 43 | |||
| 44 | /* | ||
| 45 | * Convert network-format internet address | ||
| 46 | * to base 256 d.d.d.d representation. | ||
| 47 | */ | ||
| 48 | #include <sys/types.h> | ||
| 49 | #include <netinet/in.h> | ||
| 50 | #include <arpa/inet.h> | ||
| 51 | #include <stdio.h> | ||
| 52 | |||
| 53 | char * | ||
| 54 | inet_ntoa(in) | ||
| 55 | struct in_addr in; | ||
| 56 | { | ||
| 57 | static char b[18]; | ||
| 58 | register char *p; | ||
| 59 | |||
| 60 | p = (char *)∈ | ||
| 61 | #define UC(b) (((int)b)&0xff) | ||
| 62 | (void)snprintf(b, sizeof(b), | ||
| 63 | "%d.%d.%d.%d", UC(p[0]), UC(p[1]), UC(p[2]), UC(p[3])); | ||
| 64 | return (b); | ||
| 65 | } | ||
diff --git a/src/lib/libc/net/iso_addr.3 b/src/lib/libc/net/iso_addr.3 new file mode 100644 index 0000000000..95c136e5fc --- /dev/null +++ b/src/lib/libc/net/iso_addr.3 | |||
| @@ -0,0 +1,112 @@ | |||
| 1 | .\" $NetBSD: iso_addr.3,v 1.2 1995/02/25 06:20:46 cgd Exp $ | ||
| 2 | .\" | ||
| 3 | .\" Copyright (c) 1993 | ||
| 4 | .\" The Regents of the University of California. All rights reserved. | ||
| 5 | .\" | ||
| 6 | .\" Redistribution and use in source and binary forms, with or without | ||
| 7 | .\" modification, are permitted provided that the following conditions | ||
| 8 | .\" are met: | ||
| 9 | .\" 1. Redistributions of source code must retain the above copyright | ||
| 10 | .\" notice, this list of conditions and the following disclaimer. | ||
| 11 | .\" 2. Redistributions in binary form must reproduce the above copyright | ||
| 12 | .\" notice, this list of conditions and the following disclaimer in the | ||
| 13 | .\" documentation and/or other materials provided with the distribution. | ||
| 14 | .\" 3. All advertising materials mentioning features or use of this software | ||
| 15 | .\" must display the following acknowledgement: | ||
| 16 | .\" This product includes software developed by the University of | ||
| 17 | .\" California, Berkeley and its contributors. | ||
| 18 | .\" 4. Neither the name of the University nor the names of its contributors | ||
| 19 | .\" may be used to endorse or promote products derived from this software | ||
| 20 | .\" without specific prior written permission. | ||
| 21 | .\" | ||
| 22 | .\" THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND | ||
| 23 | .\" ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE | ||
| 24 | .\" IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE | ||
| 25 | .\" ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE | ||
| 26 | .\" FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL | ||
| 27 | .\" DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS | ||
| 28 | .\" OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) | ||
| 29 | .\" HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT | ||
| 30 | .\" LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY | ||
| 31 | .\" OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF | ||
| 32 | .\" SUCH DAMAGE. | ||
| 33 | .\" | ||
| 34 | .\" @(#)iso_addr.3 8.1 (Berkeley) 6/4/93 | ||
| 35 | .\" | ||
| 36 | .Dd June 4, 1993 | ||
| 37 | .Dt ISO_ADDR 3 | ||
| 38 | .Os | ||
| 39 | .Sh NAME | ||
| 40 | .Nm iso_addr , | ||
| 41 | .Nm iso_ntoa | ||
| 42 | .Nd "elementary network address conversion routines for Open System Interconnection | ||
| 43 | .Sh SYNOPSIS | ||
| 44 | .Fd #include <sys/types.h> | ||
| 45 | .Fd #include <netiso/iso.h> | ||
| 46 | .Ft struct iso_addr * | ||
| 47 | .Fn iso_addr "char *cp" | ||
| 48 | .Ft char * | ||
| 49 | .Fn iso_ntoa "struct iso_addr *isoa" | ||
| 50 | .Sh DESCRIPTION | ||
| 51 | The routine | ||
| 52 | .Fn iso_addr | ||
| 53 | interprets character strings representing | ||
| 54 | .Tn OSI | ||
| 55 | addresses, returning binary information suitable | ||
| 56 | for use in system calls. | ||
| 57 | The routine | ||
| 58 | .Fn iso_ntoa | ||
| 59 | takes | ||
| 60 | .Tn OSI | ||
| 61 | addresses and returns | ||
| 62 | .Tn ASCII | ||
| 63 | strings representing NSAPs (network service | ||
| 64 | access points) in a | ||
| 65 | notation inverse to that accepted by | ||
| 66 | .Fn iso_addr . | ||
| 67 | .Pp | ||
| 68 | Unfortunately, no universal standard exists for representing | ||
| 69 | .Tn OSI | ||
| 70 | network addresses. | ||
| 71 | .Pp | ||
| 72 | The format employed by | ||
| 73 | .Fn iso_addr | ||
| 74 | is a sequence of hexadecimal | ||
| 75 | .Dq digits | ||
| 76 | (optionally separated by periods), | ||
| 77 | of the form: | ||
| 78 | .Bd -filled -offset indent | ||
| 79 | <hex digits>.<hex digits>.<hex digits> | ||
| 80 | .Ed | ||
| 81 | .Pp | ||
| 82 | Each pair of hexadecimal digits represents a byte | ||
| 83 | with the leading digit indicating the higher-ordered bits. | ||
| 84 | A period following an even number of bytes has no | ||
| 85 | effect (but may be used to increase legibility). | ||
| 86 | A period following an odd number of bytes has the | ||
| 87 | effect of causing the byte of address being translated | ||
| 88 | to have its higher order bits filled with zeros. | ||
| 89 | .Sh RETURN VALUES | ||
| 90 | .Fn iso_ntoa | ||
| 91 | always returns a null terminated string. | ||
| 92 | .Fn iso_addr | ||
| 93 | always returns a pointer to a struct iso_addr. | ||
| 94 | (See | ||
| 95 | .Sx BUGS . ) | ||
| 96 | .Sh SEE ALSO | ||
| 97 | .Xr iso 4 | ||
| 98 | .Sh HISTORY | ||
| 99 | The | ||
| 100 | .Fn iso_addr | ||
| 101 | and | ||
| 102 | .Fn iso_ntoa | ||
| 103 | functions appeared in | ||
| 104 | .Bx 4.3 Reno . | ||
| 105 | .Sh BUGS | ||
| 106 | The returned values | ||
| 107 | reside in a static memory area. | ||
| 108 | .Pp | ||
| 109 | The function | ||
| 110 | .Fn iso_addr | ||
| 111 | should diagnose improperly formed input, and there should be an unambiguous | ||
| 112 | way to recognize this. | ||
diff --git a/src/lib/libc/net/iso_addr.c b/src/lib/libc/net/iso_addr.c new file mode 100644 index 0000000000..c26ec1a64a --- /dev/null +++ b/src/lib/libc/net/iso_addr.c | |||
| @@ -0,0 +1,125 @@ | |||
| 1 | /* $NetBSD: iso_addr.c,v 1.4 1995/02/25 06:20:47 cgd Exp $ */ | ||
| 2 | |||
| 3 | /* | ||
| 4 | * Copyright (c) 1989, 1993 | ||
| 5 | * The Regents of the University of California. All rights reserved. | ||
| 6 | * | ||
| 7 | * Redistribution and use in source and binary forms, with or without | ||
| 8 | * modification, are permitted provided that the following conditions | ||
| 9 | * are met: | ||
| 10 | * 1. Redistributions of source code must retain the above copyright | ||
| 11 | * notice, this list of conditions and the following disclaimer. | ||
| 12 | * 2. Redistributions in binary form must reproduce the above copyright | ||
| 13 | * notice, this list of conditions and the following disclaimer in the | ||
| 14 | * documentation and/or other materials provided with the distribution. | ||
| 15 | * 3. All advertising materials mentioning features or use of this software | ||
| 16 | * must display the following acknowledgement: | ||
| 17 | * This product includes software developed by the University of | ||
| 18 | * California, Berkeley and its contributors. | ||
| 19 | * 4. Neither the name of the University nor the names of its contributors | ||
| 20 | * may be used to endorse or promote products derived from this software | ||
| 21 | * without specific prior written permission. | ||
| 22 | * | ||
| 23 | * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND | ||
| 24 | * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE | ||
| 25 | * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE | ||
| 26 | * ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE | ||
| 27 | * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL | ||
| 28 | * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS | ||
| 29 | * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) | ||
| 30 | * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT | ||
| 31 | * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY | ||
| 32 | * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF | ||
| 33 | * SUCH DAMAGE. | ||
| 34 | */ | ||
| 35 | |||
| 36 | #if defined(LIBC_SCCS) && !defined(lint) | ||
| 37 | #if 0 | ||
| 38 | static char sccsid[] = "@(#)iso_addr.c 8.1 (Berkeley) 6/4/93"; | ||
| 39 | #else | ||
| 40 | static char rcsid[] = "$NetBSD: iso_addr.c,v 1.4 1995/02/25 06:20:47 cgd Exp $"; | ||
| 41 | #endif | ||
| 42 | #endif /* LIBC_SCCS and not lint */ | ||
| 43 | |||
| 44 | #include <sys/types.h> | ||
| 45 | #include <netiso/iso.h> | ||
| 46 | #include <string.h> | ||
| 47 | |||
| 48 | /* States*/ | ||
| 49 | #define VIRGIN 0 | ||
| 50 | #define GOTONE 1 | ||
| 51 | #define GOTTWO 2 | ||
| 52 | /* Inputs */ | ||
| 53 | #define DIGIT (4*0) | ||
| 54 | #define END (4*1) | ||
| 55 | #define DELIM (4*2) | ||
| 56 | |||
| 57 | struct iso_addr * | ||
| 58 | iso_addr(addr) | ||
| 59 | register const char *addr; | ||
| 60 | { | ||
| 61 | static struct iso_addr out_addr; | ||
| 62 | register char *cp = out_addr.isoa_genaddr; | ||
| 63 | char *cplim = cp + sizeof(out_addr.isoa_genaddr); | ||
| 64 | register int byte = 0, state = VIRGIN, new; | ||
| 65 | |||
| 66 | bzero((char *)&out_addr, sizeof(out_addr)); | ||
| 67 | do { | ||
| 68 | if ((*addr >= '0') && (*addr <= '9')) { | ||
| 69 | new = *addr - '0'; | ||
| 70 | } else if ((*addr >= 'a') && (*addr <= 'f')) { | ||
| 71 | new = *addr - 'a' + 10; | ||
| 72 | } else if ((*addr >= 'A') && (*addr <= 'F')) { | ||
| 73 | new = *addr - 'A' + 10; | ||
| 74 | } else if (*addr == 0) | ||
| 75 | state |= END; | ||
| 76 | else | ||
| 77 | state |= DELIM; | ||
| 78 | addr++; | ||
| 79 | switch (state /* | INPUT */) { | ||
| 80 | case GOTTWO | DIGIT: | ||
| 81 | *cp++ = byte; /*FALLTHROUGH*/ | ||
| 82 | case VIRGIN | DIGIT: | ||
| 83 | state = GOTONE; byte = new; continue; | ||
| 84 | case GOTONE | DIGIT: | ||
| 85 | state = GOTTWO; byte = new + (byte << 4); continue; | ||
| 86 | default: /* | DELIM */ | ||
| 87 | state = VIRGIN; *cp++ = byte; byte = 0; continue; | ||
| 88 | case GOTONE | END: | ||
| 89 | case GOTTWO | END: | ||
| 90 | *cp++ = byte; /* FALLTHROUGH */ | ||
| 91 | case VIRGIN | END: | ||
| 92 | break; | ||
| 93 | } | ||
| 94 | break; | ||
| 95 | } while (cp < cplim); | ||
| 96 | out_addr.isoa_len = cp - out_addr.isoa_genaddr; | ||
| 97 | return (&out_addr); | ||
| 98 | } | ||
| 99 | static char hexlist[] = "0123456789abcdef"; | ||
| 100 | |||
| 101 | char * | ||
| 102 | iso_ntoa(isoa) | ||
| 103 | const struct iso_addr *isoa; | ||
| 104 | { | ||
| 105 | static char obuf[64]; | ||
| 106 | register char *out = obuf; | ||
| 107 | register int i; | ||
| 108 | register u_char *in = (u_char *)isoa->isoa_genaddr; | ||
| 109 | u_char *inlim = in + isoa->isoa_len; | ||
| 110 | |||
| 111 | out[1] = 0; | ||
| 112 | while (in < inlim) { | ||
| 113 | i = *in++; | ||
| 114 | *out++ = '.'; | ||
| 115 | if (i > 0xf) { | ||
| 116 | out[1] = hexlist[i & 0xf]; | ||
| 117 | i >>= 4; | ||
| 118 | out[0] = hexlist[i]; | ||
| 119 | out += 2; | ||
| 120 | } else | ||
| 121 | *out++ = hexlist[i]; | ||
| 122 | } | ||
| 123 | *out = 0; | ||
| 124 | return(obuf + 1); | ||
| 125 | } | ||
diff --git a/src/lib/libc/net/linkaddr.3 b/src/lib/libc/net/linkaddr.3 new file mode 100644 index 0000000000..1a2af9b30d --- /dev/null +++ b/src/lib/libc/net/linkaddr.3 | |||
| @@ -0,0 +1,133 @@ | |||
| 1 | .\" $NetBSD: linkaddr.3,v 1.2 1995/02/25 06:20:48 cgd Exp $ | ||
| 2 | .\" | ||
| 3 | .\" Copyright (c) 1993 | ||
| 4 | .\" The Regents of the University of California. All rights reserved. | ||
| 5 | .\" | ||
| 6 | .\" This code is derived from software contributed to Berkeley by | ||
| 7 | .\" Donn Seeley at BSDI. | ||
| 8 | .\" | ||
| 9 | .\" Redistribution and use in source and binary forms, with or without | ||
| 10 | .\" modification, are permitted provided that the following conditions | ||
| 11 | .\" are met: | ||
| 12 | .\" 1. Redistributions of source code must retain the above copyright | ||
| 13 | .\" notice, this list of conditions and the following disclaimer. | ||
| 14 | .\" 2. Redistributions in binary form must reproduce the above copyright | ||
| 15 | .\" notice, this list of conditions and the following disclaimer in the | ||
| 16 | .\" documentation and/or other materials provided with the distribution. | ||
| 17 | .\" 3. All advertising materials mentioning features or use of this software | ||
| 18 | .\" must display the following acknowledgement: | ||
| 19 | .\" This product includes software developed by the University of | ||
| 20 | .\" California, Berkeley and its contributors. | ||
| 21 | .\" 4. Neither the name of the University nor the names of its contributors | ||
| 22 | .\" may be used to endorse or promote products derived from this software | ||
| 23 | .\" without specific prior written permission. | ||
| 24 | .\" | ||
| 25 | .\" THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND | ||
| 26 | .\" ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE | ||
| 27 | .\" IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE | ||
| 28 | .\" ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE | ||
| 29 | .\" FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL | ||
| 30 | .\" DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS | ||
| 31 | .\" OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) | ||
| 32 | .\" HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT | ||
| 33 | .\" LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY | ||
| 34 | .\" OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF | ||
| 35 | .\" SUCH DAMAGE. | ||
| 36 | .\" | ||
| 37 | .\" @(#)linkaddr.3 8.1 (Berkeley) 7/28/93 | ||
| 38 | .\" | ||
| 39 | .Dd July 28, 1993 | ||
| 40 | .Dt LINK_ADDR 3 | ||
| 41 | .Os BSD 4.4 | ||
| 42 | .Sh NAME | ||
| 43 | .Nm link_addr , | ||
| 44 | .Nm link_ntoa | ||
| 45 | .Nd elementary address specification routines for link level access | ||
| 46 | .Sh SYNOPSIS | ||
| 47 | .Fd #include <sys/types.h> | ||
| 48 | .Fd #include <sys/socket.h> | ||
| 49 | .Fd #include <net/if_dl.h> | ||
| 50 | .Ft void | ||
| 51 | .Fn link_addr "const char *addr" "struct sockaddr_dl *sdl" | ||
| 52 | .Ft char * | ||
| 53 | .Fn link_ntoa "const struct sockaddr_dl *sdl" | ||
| 54 | .Sh DESCRIPTION | ||
| 55 | The routine | ||
| 56 | .Fn link_addr | ||
| 57 | interprets character strings representing | ||
| 58 | link-level addresses, returning binary information suitable | ||
| 59 | for use in system calls. | ||
| 60 | The routine | ||
| 61 | .Fn link_ntoa | ||
| 62 | takes | ||
| 63 | a link-level | ||
| 64 | address and returns an | ||
| 65 | .Tn ASCII | ||
| 66 | string representing some of the information present, | ||
| 67 | including the link level address itself, and the interface name | ||
| 68 | or number, if present. | ||
| 69 | This facility is experimental and is | ||
| 70 | still subject to change. | ||
| 71 | .Pp | ||
| 72 | For | ||
| 73 | .Fn link_addr , | ||
| 74 | the string | ||
| 75 | .Fa addr | ||
| 76 | may contain | ||
| 77 | an optional network interface identifier of the form | ||
| 78 | .Dq "name unit-number" , | ||
| 79 | suitable for the first argument to | ||
| 80 | .Xr ifconfig 4 , | ||
| 81 | followed in all cases by a colon and | ||
| 82 | an interface address in the form of | ||
| 83 | groups of hexadecimal digits | ||
| 84 | separated by periods. | ||
| 85 | Each group represents a byte of address; | ||
| 86 | address bytes are filled left to right from | ||
| 87 | low order bytes through high order bytes. | ||
| 88 | .Pp | ||
| 89 | .\" A regular expression may make this format clearer: | ||
| 90 | .\" .Bd -literal -offset indent | ||
| 91 | .\" ([a-z]+[0-9]+:)?[0-9a-f]+(\e.[0-9a-f]+)* | ||
| 92 | .\" .Ed | ||
| 93 | .\" .Pp | ||
| 94 | Thus | ||
| 95 | .Li le0:8.0.9.13.d.30 | ||
| 96 | represents an ethernet address | ||
| 97 | to be transmitted on the first Lance ethernet interface. | ||
| 98 | .Sh RETURN VALUES | ||
| 99 | .Fn link_ntoa | ||
| 100 | always returns a null terminated string. | ||
| 101 | .Fn link_addr | ||
| 102 | has no return value. | ||
| 103 | (See | ||
| 104 | .Sx BUGS . ) | ||
| 105 | .Sh SEE ALSO | ||
| 106 | .Xr iso 4 , | ||
| 107 | .Sh HISTORY | ||
| 108 | The | ||
| 109 | .Fn link_addr | ||
| 110 | and | ||
| 111 | .Fn link_ntoa | ||
| 112 | functions appeared in | ||
| 113 | .Bx 4.3 Reno . | ||
| 114 | .Sh BUGS | ||
| 115 | The returned values for link_ntoa | ||
| 116 | reside in a static memory area. | ||
| 117 | .Pp | ||
| 118 | The function | ||
| 119 | .Fn link_addr | ||
| 120 | should diagnose improperly formed input, and there should be an unambiguous | ||
| 121 | way to recognize this. | ||
| 122 | .Pp | ||
| 123 | If the | ||
| 124 | .Va sdl_len | ||
| 125 | field of the link socket address | ||
| 126 | .Fa sdl | ||
| 127 | is 0, | ||
| 128 | .Fn link_ntoa | ||
| 129 | will not insert a colon before the interface address bytes. | ||
| 130 | If this translated address is given to | ||
| 131 | .Fn link_addr | ||
| 132 | without inserting an initial colon, | ||
| 133 | the latter will not interpret it correctly. | ||
diff --git a/src/lib/libc/net/linkaddr.c b/src/lib/libc/net/linkaddr.c new file mode 100644 index 0000000000..19a0de3abd --- /dev/null +++ b/src/lib/libc/net/linkaddr.c | |||
| @@ -0,0 +1,164 @@ | |||
| 1 | /* $NetBSD: linkaddr.c,v 1.5 1995/02/25 06:20:49 cgd Exp $ */ | ||
| 2 | |||
| 3 | /*- | ||
| 4 | * Copyright (c) 1990, 1993 | ||
| 5 | * The Regents of the University of California. All rights reserved. | ||
| 6 | * | ||
| 7 | * Redistribution and use in source and binary forms, with or without | ||
| 8 | * modification, are permitted provided that the following conditions | ||
| 9 | * are met: | ||
| 10 | * 1. Redistributions of source code must retain the above copyright | ||
| 11 | * notice, this list of conditions and the following disclaimer. | ||
| 12 | * 2. Redistributions in binary form must reproduce the above copyright | ||
| 13 | * notice, this list of conditions and the following disclaimer in the | ||
| 14 | * documentation and/or other materials provided with the distribution. | ||
| 15 | * 3. All advertising materials mentioning features or use of this software | ||
| 16 | * must display the following acknowledgement: | ||
| 17 | * This product includes software developed by the University of | ||
| 18 | * California, Berkeley and its contributors. | ||
| 19 | * 4. Neither the name of the University nor the names of its contributors | ||
| 20 | * may be used to endorse or promote products derived from this software | ||
| 21 | * without specific prior written permission. | ||
| 22 | * | ||
| 23 | * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND | ||
| 24 | * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE | ||
| 25 | * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE | ||
| 26 | * ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE | ||
| 27 | * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL | ||
| 28 | * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS | ||
| 29 | * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) | ||
| 30 | * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT | ||
| 31 | * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY | ||
| 32 | * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF | ||
| 33 | * SUCH DAMAGE. | ||
| 34 | */ | ||
| 35 | |||
| 36 | #if defined(LIBC_SCCS) && !defined(lint) | ||
| 37 | #if 0 | ||
| 38 | static char sccsid[] = "@(#)linkaddr.c 8.1 (Berkeley) 6/4/93"; | ||
| 39 | #else | ||
| 40 | static char rcsid[] = "$NetBSD: linkaddr.c,v 1.5 1995/02/25 06:20:49 cgd Exp $"; | ||
| 41 | #endif | ||
| 42 | #endif /* LIBC_SCCS and not lint */ | ||
| 43 | |||
| 44 | #include <sys/types.h> | ||
| 45 | #include <sys/socket.h> | ||
| 46 | #include <net/if_dl.h> | ||
| 47 | #include <string.h> | ||
| 48 | |||
| 49 | /* States*/ | ||
| 50 | #define NAMING 0 | ||
| 51 | #define GOTONE 1 | ||
| 52 | #define GOTTWO 2 | ||
| 53 | #define RESET 3 | ||
| 54 | /* Inputs */ | ||
| 55 | #define DIGIT (4*0) | ||
| 56 | #define END (4*1) | ||
| 57 | #define DELIM (4*2) | ||
| 58 | #define LETTER (4*3) | ||
| 59 | |||
| 60 | void | ||
| 61 | link_addr(addr, sdl) | ||
| 62 | register const char *addr; | ||
| 63 | register struct sockaddr_dl *sdl; | ||
| 64 | { | ||
| 65 | register char *cp = sdl->sdl_data; | ||
| 66 | char *cplim = sdl->sdl_len + (char *)sdl; | ||
| 67 | register int byte = 0, state = NAMING, new; | ||
| 68 | |||
| 69 | bzero((char *)&sdl->sdl_family, sdl->sdl_len - 1); | ||
| 70 | sdl->sdl_family = AF_LINK; | ||
| 71 | do { | ||
| 72 | state &= ~LETTER; | ||
| 73 | if ((*addr >= '0') && (*addr <= '9')) { | ||
| 74 | new = *addr - '0'; | ||
| 75 | } else if ((*addr >= 'a') && (*addr <= 'f')) { | ||
| 76 | new = *addr - 'a' + 10; | ||
| 77 | } else if ((*addr >= 'A') && (*addr <= 'F')) { | ||
| 78 | new = *addr - 'A' + 10; | ||
| 79 | } else if (*addr == 0) { | ||
| 80 | state |= END; | ||
| 81 | } else if (state == NAMING && | ||
| 82 | (((*addr >= 'A') && (*addr <= 'Z')) || | ||
| 83 | ((*addr >= 'a') && (*addr <= 'z')))) | ||
| 84 | state |= LETTER; | ||
| 85 | else | ||
| 86 | state |= DELIM; | ||
| 87 | addr++; | ||
| 88 | switch (state /* | INPUT */) { | ||
| 89 | case NAMING | DIGIT: | ||
| 90 | case NAMING | LETTER: | ||
| 91 | *cp++ = addr[-1]; | ||
| 92 | continue; | ||
| 93 | case NAMING | DELIM: | ||
| 94 | state = RESET; | ||
| 95 | sdl->sdl_nlen = cp - sdl->sdl_data; | ||
| 96 | continue; | ||
| 97 | case GOTTWO | DIGIT: | ||
| 98 | *cp++ = byte; | ||
| 99 | /* FALLTHROUGH */ | ||
| 100 | case RESET | DIGIT: | ||
| 101 | state = GOTONE; | ||
| 102 | byte = new; | ||
| 103 | continue; | ||
| 104 | case GOTONE | DIGIT: | ||
| 105 | state = GOTTWO; | ||
| 106 | byte = new + (byte << 4); | ||
| 107 | continue; | ||
| 108 | default: /* | DELIM */ | ||
| 109 | state = RESET; | ||
| 110 | *cp++ = byte; | ||
| 111 | byte = 0; | ||
| 112 | continue; | ||
| 113 | case GOTONE | END: | ||
| 114 | case GOTTWO | END: | ||
| 115 | *cp++ = byte; | ||
| 116 | /* FALLTHROUGH */ | ||
| 117 | case RESET | END: | ||
| 118 | break; | ||
| 119 | } | ||
| 120 | break; | ||
| 121 | } while (cp < cplim); | ||
| 122 | sdl->sdl_alen = cp - LLADDR(sdl); | ||
| 123 | new = cp - (char *)sdl; | ||
| 124 | if (new > sizeof(*sdl)) | ||
| 125 | sdl->sdl_len = new; | ||
| 126 | return; | ||
| 127 | } | ||
| 128 | |||
| 129 | static char hexlist[] = "0123456789abcdef"; | ||
| 130 | |||
| 131 | char * | ||
| 132 | link_ntoa(sdl) | ||
| 133 | register const struct sockaddr_dl *sdl; | ||
| 134 | { | ||
| 135 | static char obuf[64]; | ||
| 136 | register char *out = obuf; | ||
| 137 | register int i; | ||
| 138 | register u_char *in = (u_char *)LLADDR(sdl); | ||
| 139 | u_char *inlim = in + sdl->sdl_alen; | ||
| 140 | int firsttime = 1; | ||
| 141 | |||
| 142 | if (sdl->sdl_nlen) { | ||
| 143 | bcopy(sdl->sdl_data, obuf, sdl->sdl_nlen); | ||
| 144 | out += sdl->sdl_nlen; | ||
| 145 | if (sdl->sdl_alen) | ||
| 146 | *out++ = ':'; | ||
| 147 | } | ||
| 148 | while (in < inlim) { | ||
| 149 | if (firsttime) | ||
| 150 | firsttime = 0; | ||
| 151 | else | ||
| 152 | *out++ = '.'; | ||
| 153 | i = *in++; | ||
| 154 | if (i > 0xf) { | ||
| 155 | out[1] = hexlist[i & 0xf]; | ||
| 156 | i >>= 4; | ||
| 157 | out[0] = hexlist[i]; | ||
| 158 | out += 2; | ||
| 159 | } else | ||
| 160 | *out++ = hexlist[i]; | ||
| 161 | } | ||
| 162 | *out = 0; | ||
| 163 | return (obuf); | ||
| 164 | } | ||
diff --git a/src/lib/libc/net/ns.3 b/src/lib/libc/net/ns.3 new file mode 100644 index 0000000000..f89b4fe042 --- /dev/null +++ b/src/lib/libc/net/ns.3 | |||
| @@ -0,0 +1,132 @@ | |||
| 1 | .\" $NetBSD: ns.3,v 1.3 1995/02/25 06:20:50 cgd Exp $ | ||
| 2 | .\" | ||
| 3 | .\" Copyright (c) 1986, 1991, 1993 | ||
| 4 | .\" The Regents of the University of California. All rights reserved. | ||
| 5 | .\" | ||
| 6 | .\" Redistribution and use in source and binary forms, with or without | ||
| 7 | .\" modification, are permitted provided that the following conditions | ||
| 8 | .\" are met: | ||
| 9 | .\" 1. Redistributions of source code must retain the above copyright | ||
| 10 | .\" notice, this list of conditions and the following disclaimer. | ||
| 11 | .\" 2. Redistributions in binary form must reproduce the above copyright | ||
| 12 | .\" notice, this list of conditions and the following disclaimer in the | ||
| 13 | .\" documentation and/or other materials provided with the distribution. | ||
| 14 | .\" 3. All advertising materials mentioning features or use of this software | ||
| 15 | .\" must display the following acknowledgement: | ||
| 16 | .\" This product includes software developed by the University of | ||
| 17 | .\" California, Berkeley and its contributors. | ||
| 18 | .\" 4. Neither the name of the University nor the names of its contributors | ||
| 19 | .\" may be used to endorse or promote products derived from this software | ||
| 20 | .\" without specific prior written permission. | ||
| 21 | .\" | ||
| 22 | .\" THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND | ||
| 23 | .\" ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE | ||
| 24 | .\" IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE | ||
| 25 | .\" ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE | ||
| 26 | .\" FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL | ||
| 27 | .\" DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS | ||
| 28 | .\" OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) | ||
| 29 | .\" HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT | ||
| 30 | .\" LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY | ||
| 31 | .\" OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF | ||
| 32 | .\" SUCH DAMAGE. | ||
| 33 | .\" | ||
| 34 | .\" @(#)ns.3 8.1 (Berkeley) 6/4/93 | ||
| 35 | .\" | ||
| 36 | .Dd June 4, 1993 | ||
| 37 | .Dt NS 3 | ||
| 38 | .Os BSD 4.3 | ||
| 39 | .Sh NAME | ||
| 40 | .Nm ns_addr , | ||
| 41 | .Nm ns_ntoa | ||
| 42 | .Nd Xerox | ||
| 43 | .Tn NS Ns (tm) | ||
| 44 | address conversion routines | ||
| 45 | .Sh SYNOPSIS | ||
| 46 | .Fd #include <sys/types.h> | ||
| 47 | .Fd #include <netns/ns.h> | ||
| 48 | .Ft struct ns_addr | ||
| 49 | .Fn ns_addr "char *cp" | ||
| 50 | .Ft char * | ||
| 51 | .Fn ns_ntoa "struct ns_addr ns" | ||
| 52 | .Sh DESCRIPTION | ||
| 53 | The routine | ||
| 54 | .Fn ns_addr | ||
| 55 | interprets character strings representing | ||
| 56 | .Tn XNS | ||
| 57 | addresses, returning binary information suitable | ||
| 58 | for use in system calls. | ||
| 59 | The routine | ||
| 60 | .Fn ns_ntoa | ||
| 61 | takes | ||
| 62 | .Tn XNS | ||
| 63 | addresses and returns | ||
| 64 | .Tn ASCII | ||
| 65 | strings representing the address in a | ||
| 66 | notation in common use in the Xerox Development Environment: | ||
| 67 | .Bd -filled -offset indent | ||
| 68 | <network number>.<host number>.<port number> | ||
| 69 | .Ed | ||
| 70 | .Pp | ||
| 71 | Trailing zero fields are suppressed, and each number is printed in hexadecimal, | ||
| 72 | in a format suitable for input to | ||
| 73 | .Fn ns_addr . | ||
| 74 | Any fields lacking super-decimal digits will have a | ||
| 75 | trailing | ||
| 76 | .Ql H | ||
| 77 | appended. | ||
| 78 | .Pp | ||
| 79 | Unfortunately, no universal standard exists for representing | ||
| 80 | .Tn XNS | ||
| 81 | addresses. | ||
| 82 | An effort has been made to insure that | ||
| 83 | .Fn ns_addr | ||
| 84 | be compatible with most formats in common use. | ||
| 85 | It will first separate an address into 1 to 3 fields using a single delimiter | ||
| 86 | chosen from | ||
| 87 | period | ||
| 88 | .Ql \&. , | ||
| 89 | colon | ||
| 90 | .Ql \&: | ||
| 91 | or pound-sign | ||
| 92 | .Ql \&# . | ||
| 93 | Each field is then examined for byte separators (colon or period). | ||
| 94 | If there are byte separators, each subfield separated is taken to be | ||
| 95 | a small hexadecimal number, and the entirety is taken as a network-byte-ordered | ||
| 96 | quantity to be zero extended in the high-network-order bytes. | ||
| 97 | Next, the field is inspected for hyphens, in which case | ||
| 98 | the field is assumed to be a number in decimal notation | ||
| 99 | with hyphens separating the millenia. | ||
| 100 | Next, the field is assumed to be a number: | ||
| 101 | It is interpreted | ||
| 102 | as hexadecimal if there is a leading | ||
| 103 | .Ql 0x | ||
| 104 | (as in C), | ||
| 105 | a trailing | ||
| 106 | .Ql H | ||
| 107 | (as in Mesa), or there are any super-decimal digits present. | ||
| 108 | It is interpreted as octal is there is a leading | ||
| 109 | .Ql 0 | ||
| 110 | and there are no super-octal digits. | ||
| 111 | Otherwise, it is converted as a decimal number. | ||
| 112 | .Sh RETURN VALUES | ||
| 113 | None. (See | ||
| 114 | .Sx BUGS . ) | ||
| 115 | .Sh SEE ALSO | ||
| 116 | .Xr hosts 5 , | ||
| 117 | .Xr networks 5 , | ||
| 118 | .Sh HISTORY | ||
| 119 | The | ||
| 120 | .Fn ns_addr | ||
| 121 | and | ||
| 122 | .Fn ns_toa | ||
| 123 | functions appeared in | ||
| 124 | .Bx 4.3 . | ||
| 125 | .Sh BUGS | ||
| 126 | The string returned by | ||
| 127 | .Fn ns_ntoa | ||
| 128 | resides in a static memory area. | ||
| 129 | The function | ||
| 130 | .Fn ns_addr | ||
| 131 | should diagnose improperly formed input, and there should be an unambiguous | ||
| 132 | way to recognize this. | ||
diff --git a/src/lib/libc/net/ns_addr.c b/src/lib/libc/net/ns_addr.c new file mode 100644 index 0000000000..f75ddb23b7 --- /dev/null +++ b/src/lib/libc/net/ns_addr.c | |||
| @@ -0,0 +1,233 @@ | |||
| 1 | /* $NetBSD: ns_addr.c,v 1.5 1995/02/25 06:20:51 cgd Exp $ */ | ||
| 2 | |||
| 3 | /* | ||
| 4 | * Copyright (c) 1986, 1993 | ||
| 5 | * The Regents of the University of California. All rights reserved. | ||
| 6 | * | ||
| 7 | * This code is derived from software contributed to Berkeley by | ||
| 8 | * J.Q. Johnson. | ||
| 9 | * | ||
| 10 | * Redistribution and use in source and binary forms, with or without | ||
| 11 | * modification, are permitted provided that the following conditions | ||
| 12 | * are met: | ||
| 13 | * 1. Redistributions of source code must retain the above copyright | ||
| 14 | * notice, this list of conditions and the following disclaimer. | ||
| 15 | * 2. Redistributions in binary form must reproduce the above copyright | ||
| 16 | * notice, this list of conditions and the following disclaimer in the | ||
| 17 | * documentation and/or other materials provided with the distribution. | ||
| 18 | * 3. All advertising materials mentioning features or use of this software | ||
| 19 | * must display the following acknowledgement: | ||
| 20 | * This product includes software developed by the University of | ||
| 21 | * California, Berkeley and its contributors. | ||
| 22 | * 4. Neither the name of the University nor the names of its contributors | ||
| 23 | * may be used to endorse or promote products derived from this software | ||
| 24 | * without specific prior written permission. | ||
| 25 | * | ||
| 26 | * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND | ||
| 27 | * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE | ||
| 28 | * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE | ||
| 29 | * ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE | ||
| 30 | * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL | ||
| 31 | * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS | ||
| 32 | * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) | ||
| 33 | * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT | ||
| 34 | * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY | ||
| 35 | * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF | ||
| 36 | * SUCH DAMAGE. | ||
| 37 | */ | ||
| 38 | |||
| 39 | #if defined(LIBC_SCCS) && !defined(lint) | ||
| 40 | #if 0 | ||
| 41 | static char sccsid[] = "@(#)ns_addr.c 8.1 (Berkeley) 6/7/93"; | ||
| 42 | #else | ||
| 43 | static char rcsid[] = "$NetBSD: ns_addr.c,v 1.5 1995/02/25 06:20:51 cgd Exp $"; | ||
| 44 | #endif | ||
| 45 | #endif /* LIBC_SCCS and not lint */ | ||
| 46 | |||
| 47 | #include <sys/param.h> | ||
| 48 | #include <netns/ns.h> | ||
| 49 | #include <stdio.h> | ||
| 50 | #include <string.h> | ||
| 51 | |||
| 52 | static struct ns_addr addr, zero_addr; | ||
| 53 | |||
| 54 | static void Field(), cvtbase(); | ||
| 55 | |||
| 56 | struct ns_addr | ||
| 57 | ns_addr(name) | ||
| 58 | const char *name; | ||
| 59 | { | ||
| 60 | char separator; | ||
| 61 | char *hostname, *socketname, *cp; | ||
| 62 | char buf[50]; | ||
| 63 | |||
| 64 | (void)strncpy(buf, name, sizeof(buf) - 1); | ||
| 65 | buf[sizeof(buf) - 1] = '\0'; | ||
| 66 | |||
| 67 | /* | ||
| 68 | * First, figure out what he intends as a field separtor. | ||
| 69 | * Despite the way this routine is written, the prefered | ||
| 70 | * form 2-272.AA001234H.01777, i.e. XDE standard. | ||
| 71 | * Great efforts are made to insure backward compatability. | ||
| 72 | */ | ||
| 73 | if (hostname = strchr(buf, '#')) | ||
| 74 | separator = '#'; | ||
| 75 | else { | ||
| 76 | hostname = strchr(buf, '.'); | ||
| 77 | if ((cp = strchr(buf, ':')) && | ||
| 78 | ((hostname && cp < hostname) || (hostname == 0))) { | ||
| 79 | hostname = cp; | ||
| 80 | separator = ':'; | ||
| 81 | } else | ||
| 82 | separator = '.'; | ||
| 83 | } | ||
| 84 | if (hostname) | ||
| 85 | *hostname++ = 0; | ||
| 86 | |||
| 87 | addr = zero_addr; | ||
| 88 | Field(buf, addr.x_net.c_net, 4); | ||
| 89 | if (hostname == 0) | ||
| 90 | return (addr); /* No separator means net only */ | ||
| 91 | |||
| 92 | socketname = strchr(hostname, separator); | ||
| 93 | if (socketname) { | ||
| 94 | *socketname++ = 0; | ||
| 95 | Field(socketname, (u_char *)&addr.x_port, 2); | ||
| 96 | } | ||
| 97 | |||
| 98 | Field(hostname, addr.x_host.c_host, 6); | ||
| 99 | |||
| 100 | return (addr); | ||
| 101 | } | ||
| 102 | |||
| 103 | static void | ||
| 104 | Field(buf, out, len) | ||
| 105 | char *buf; | ||
| 106 | u_char *out; | ||
| 107 | int len; | ||
| 108 | { | ||
| 109 | register char *bp = buf; | ||
| 110 | int i, ibase, base16 = 0, base10 = 0, clen = 0; | ||
| 111 | int hb[6], *hp; | ||
| 112 | char *fmt; | ||
| 113 | |||
| 114 | /* | ||
| 115 | * first try 2-273#2-852-151-014#socket | ||
| 116 | */ | ||
| 117 | if ((*buf != '-') && | ||
| 118 | (1 < (i = sscanf(buf, "%d-%d-%d-%d-%d", | ||
| 119 | &hb[0], &hb[1], &hb[2], &hb[3], &hb[4])))) { | ||
| 120 | cvtbase(1000L, 256, hb, i, out, len); | ||
| 121 | return; | ||
| 122 | } | ||
| 123 | /* | ||
| 124 | * try form 8E1#0.0.AA.0.5E.E6#socket | ||
| 125 | */ | ||
| 126 | if (1 < (i = sscanf(buf,"%x.%x.%x.%x.%x.%x", | ||
| 127 | &hb[0], &hb[1], &hb[2], &hb[3], &hb[4], &hb[5]))) { | ||
| 128 | cvtbase(256L, 256, hb, i, out, len); | ||
| 129 | return; | ||
| 130 | } | ||
| 131 | /* | ||
| 132 | * try form 8E1#0:0:AA:0:5E:E6#socket | ||
| 133 | */ | ||
| 134 | if (1 < (i = sscanf(buf,"%x:%x:%x:%x:%x:%x", | ||
| 135 | &hb[0], &hb[1], &hb[2], &hb[3], &hb[4], &hb[5]))) { | ||
| 136 | cvtbase(256L, 256, hb, i, out, len); | ||
| 137 | return; | ||
| 138 | } | ||
| 139 | /* | ||
| 140 | * This is REALLY stretching it but there was a | ||
| 141 | * comma notation separting shorts -- definitely non standard | ||
| 142 | */ | ||
| 143 | if (1 < (i = sscanf(buf,"%x,%x,%x", | ||
| 144 | &hb[0], &hb[1], &hb[2]))) { | ||
| 145 | hb[0] = htons(hb[0]); hb[1] = htons(hb[1]); | ||
| 146 | hb[2] = htons(hb[2]); | ||
| 147 | cvtbase(65536L, 256, hb, i, out, len); | ||
| 148 | return; | ||
| 149 | } | ||
| 150 | |||
| 151 | /* Need to decide if base 10, 16 or 8 */ | ||
| 152 | while (*bp) switch (*bp++) { | ||
| 153 | |||
| 154 | case '0': case '1': case '2': case '3': case '4': case '5': | ||
| 155 | case '6': case '7': case '-': | ||
| 156 | break; | ||
| 157 | |||
| 158 | case '8': case '9': | ||
| 159 | base10 = 1; | ||
| 160 | break; | ||
| 161 | |||
| 162 | case 'a': case 'b': case 'c': case 'd': case 'e': case 'f': | ||
| 163 | case 'A': case 'B': case 'C': case 'D': case 'E': case 'F': | ||
| 164 | base16 = 1; | ||
| 165 | break; | ||
| 166 | |||
| 167 | case 'x': case 'X': | ||
| 168 | *--bp = '0'; | ||
| 169 | base16 = 1; | ||
| 170 | break; | ||
| 171 | |||
| 172 | case 'h': case 'H': | ||
| 173 | base16 = 1; | ||
| 174 | /* fall into */ | ||
| 175 | |||
| 176 | default: | ||
| 177 | *--bp = 0; /* Ends Loop */ | ||
| 178 | } | ||
| 179 | if (base16) { | ||
| 180 | fmt = "%3x"; | ||
| 181 | ibase = 4096; | ||
| 182 | } else if (base10 == 0 && *buf == '0') { | ||
| 183 | fmt = "%3o"; | ||
| 184 | ibase = 512; | ||
| 185 | } else { | ||
| 186 | fmt = "%3d"; | ||
| 187 | ibase = 1000; | ||
| 188 | } | ||
| 189 | |||
| 190 | for (bp = buf; *bp++; ) clen++; | ||
| 191 | if (clen == 0) clen++; | ||
| 192 | if (clen > 18) clen = 18; | ||
| 193 | i = ((clen - 1) / 3) + 1; | ||
| 194 | bp = clen + buf - 3; | ||
| 195 | hp = hb + i - 1; | ||
| 196 | |||
| 197 | while (hp > hb) { | ||
| 198 | (void)sscanf(bp, fmt, hp); | ||
| 199 | bp[0] = 0; | ||
| 200 | hp--; | ||
| 201 | bp -= 3; | ||
| 202 | } | ||
| 203 | (void)sscanf(buf, fmt, hp); | ||
| 204 | cvtbase((long)ibase, 256, hb, i, out, len); | ||
| 205 | } | ||
| 206 | |||
| 207 | static void | ||
| 208 | cvtbase(oldbase,newbase,input,inlen,result,reslen) | ||
| 209 | long oldbase; | ||
| 210 | int newbase; | ||
| 211 | int input[]; | ||
| 212 | int inlen; | ||
| 213 | unsigned char result[]; | ||
| 214 | int reslen; | ||
| 215 | { | ||
| 216 | int d, e; | ||
| 217 | long sum; | ||
| 218 | |||
| 219 | e = 1; | ||
| 220 | while (e > 0 && reslen > 0) { | ||
| 221 | d = 0; e = 0; sum = 0; | ||
| 222 | /* long division: input=input/newbase */ | ||
| 223 | while (d < inlen) { | ||
| 224 | sum = sum*oldbase + (long) input[d]; | ||
| 225 | e += (sum > 0); | ||
| 226 | input[d++] = sum / newbase; | ||
| 227 | sum %= newbase; | ||
| 228 | } | ||
| 229 | result[--reslen] = sum; /* accumulate remainder */ | ||
| 230 | } | ||
| 231 | for (d=0; d < reslen; d++) | ||
| 232 | result[d] = 0; | ||
| 233 | } | ||
diff --git a/src/lib/libc/net/ns_ntoa.c b/src/lib/libc/net/ns_ntoa.c new file mode 100644 index 0000000000..ad3265399b --- /dev/null +++ b/src/lib/libc/net/ns_ntoa.c | |||
| @@ -0,0 +1,106 @@ | |||
| 1 | /* $NetBSD: ns_ntoa.c,v 1.4 1995/02/25 06:20:51 cgd Exp $ */ | ||
| 2 | |||
| 3 | /* | ||
| 4 | * Copyright (c) 1986, 1993 | ||
| 5 | * The Regents of the University of California. All rights reserved. | ||
| 6 | * | ||
| 7 | * Redistribution and use in source and binary forms, with or without | ||
| 8 | * modification, are permitted provided that the following conditions | ||
| 9 | * are met: | ||
| 10 | * 1. Redistributions of source code must retain the above copyright | ||
| 11 | * notice, this list of conditions and the following disclaimer. | ||
| 12 | * 2. Redistributions in binary form must reproduce the above copyright | ||
| 13 | * notice, this list of conditions and the following disclaimer in the | ||
| 14 | * documentation and/or other materials provided with the distribution. | ||
| 15 | * 3. All advertising materials mentioning features or use of this software | ||
| 16 | * must display the following acknowledgement: | ||
| 17 | * This product includes software developed by the University of | ||
| 18 | * California, Berkeley and its contributors. | ||
| 19 | * 4. Neither the name of the University nor the names of its contributors | ||
| 20 | * may be used to endorse or promote products derived from this software | ||
| 21 | * without specific prior written permission. | ||
| 22 | * | ||
| 23 | * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND | ||
| 24 | * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE | ||
| 25 | * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE | ||
| 26 | * ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE | ||
| 27 | * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL | ||
| 28 | * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS | ||
| 29 | * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) | ||
| 30 | * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT | ||
| 31 | * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY | ||
| 32 | * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF | ||
| 33 | * SUCH DAMAGE. | ||
| 34 | */ | ||
| 35 | |||
| 36 | #if defined(LIBC_SCCS) && !defined(lint) | ||
| 37 | #if 0 | ||
| 38 | static char sccsid[] = "@(#)ns_ntoa.c 8.1 (Berkeley) 6/4/93"; | ||
| 39 | #else | ||
| 40 | static char rcsid[] = "$NetBSD: ns_ntoa.c,v 1.4 1995/02/25 06:20:51 cgd Exp $"; | ||
| 41 | #endif | ||
| 42 | #endif /* LIBC_SCCS and not lint */ | ||
| 43 | |||
| 44 | #include <sys/param.h> | ||
| 45 | #include <netns/ns.h> | ||
| 46 | #include <stdio.h> | ||
| 47 | |||
| 48 | char * | ||
| 49 | ns_ntoa(addr) | ||
| 50 | struct ns_addr addr; | ||
| 51 | { | ||
| 52 | static char obuf[40]; | ||
| 53 | union { union ns_net net_e; u_long long_e; } net; | ||
| 54 | u_short port = htons(addr.x_port); | ||
| 55 | register char *cp; | ||
| 56 | char *cp2; | ||
| 57 | register u_char *up = addr.x_host.c_host; | ||
| 58 | u_char *uplim = up + 6; | ||
| 59 | static char *spectHex(); | ||
| 60 | |||
| 61 | net.net_e = addr.x_net; | ||
| 62 | sprintf(obuf, "%lx", ntohl(net.long_e)); | ||
| 63 | cp = spectHex(obuf); | ||
| 64 | cp2 = cp + 1; | ||
| 65 | while (*up==0 && up < uplim) up++; | ||
| 66 | if (up == uplim) { | ||
| 67 | if (port) { | ||
| 68 | sprintf(cp, ".0"); | ||
| 69 | cp += 2; | ||
| 70 | } | ||
| 71 | } else { | ||
| 72 | sprintf(cp, ".%x", *up++); | ||
| 73 | while (up < uplim) { | ||
| 74 | while (*cp) cp++; | ||
| 75 | sprintf(cp, "%02x", *up++); | ||
| 76 | } | ||
| 77 | cp = spectHex(cp2); | ||
| 78 | } | ||
| 79 | if (port) { | ||
| 80 | sprintf(cp, ".%x", port); | ||
| 81 | spectHex(cp + 1); | ||
| 82 | } | ||
| 83 | return (obuf); | ||
| 84 | } | ||
| 85 | |||
| 86 | static char * | ||
| 87 | spectHex(p0) | ||
| 88 | char *p0; | ||
| 89 | { | ||
| 90 | int ok = 0; | ||
| 91 | int nonzero = 0; | ||
| 92 | register char *p = p0; | ||
| 93 | for (; *p; p++) switch (*p) { | ||
| 94 | |||
| 95 | case 'a': case 'b': case 'c': case 'd': case 'e': case 'f': | ||
| 96 | *p += ('A' - 'a'); | ||
| 97 | /* fall into . . . */ | ||
| 98 | case 'A': case 'B': case 'C': case 'D': case 'E': case 'F': | ||
| 99 | ok = 1; | ||
| 100 | case '1': case '2': case '3': case '4': case '5': | ||
| 101 | case '6': case '7': case '8': case '9': | ||
| 102 | nonzero = 1; | ||
| 103 | } | ||
| 104 | if (nonzero && !ok) { *p++ = 'H'; *p = 0; } | ||
| 105 | return (p); | ||
| 106 | } | ||
diff --git a/src/lib/libc/net/ntohl.c b/src/lib/libc/net/ntohl.c new file mode 100644 index 0000000000..05b7f4c9a3 --- /dev/null +++ b/src/lib/libc/net/ntohl.c | |||
| @@ -0,0 +1,29 @@ | |||
| 1 | /* $NetBSD: ntohl.c,v 1.5 1995/04/28 23:25:21 jtc Exp $ */ | ||
| 2 | |||
| 3 | /* | ||
| 4 | * Written by J.T. Conklin <jtc@netbsd.org>. | ||
| 5 | * Public domain. | ||
| 6 | */ | ||
| 7 | |||
| 8 | #if defined(LIBC_SCCS) && !defined(lint) | ||
| 9 | static char *rcsid = "$NetBSD: ntohl.c,v 1.5 1995/04/28 23:25:21 jtc Exp $"; | ||
| 10 | #endif | ||
| 11 | |||
| 12 | #include <sys/types.h> | ||
| 13 | #include <machine/endian.h> | ||
| 14 | |||
| 15 | #undef ntohl | ||
| 16 | |||
| 17 | unsigned long | ||
| 18 | ntohl(x) | ||
| 19 | unsigned long x; | ||
| 20 | { | ||
| 21 | u_int32_t y = x; | ||
| 22 | |||
| 23 | #if BYTE_ORDER == LITTLE_ENDIAN | ||
| 24 | u_char *s = (u_char *)&y; | ||
| 25 | return s[0] << 24 | s[1] << 16 | s[2] << 8 | s[3]; | ||
| 26 | #else | ||
| 27 | return y; | ||
| 28 | #endif | ||
| 29 | } | ||
diff --git a/src/lib/libc/net/ntohs.c b/src/lib/libc/net/ntohs.c new file mode 100644 index 0000000000..93ab83ee4d --- /dev/null +++ b/src/lib/libc/net/ntohs.c | |||
| @@ -0,0 +1,27 @@ | |||
| 1 | /* $NetBSD: ntohs.c,v 1.5 1995/04/28 23:25:23 jtc Exp $ */ | ||
| 2 | |||
| 3 | /* | ||
| 4 | * Written by J.T. Conklin <jtc@netbsd.org>. | ||
| 5 | * Public domain. | ||
| 6 | */ | ||
| 7 | |||
| 8 | #if defined(LIBC_SCCS) && !defined(lint) | ||
| 9 | static char *rcsid = "$NetBSD: ntohs.c,v 1.5 1995/04/28 23:25:23 jtc Exp $"; | ||
| 10 | #endif | ||
| 11 | |||
| 12 | #include <sys/types.h> | ||
| 13 | #include <machine/endian.h> | ||
| 14 | |||
| 15 | #undef ntohs | ||
| 16 | |||
| 17 | unsigned short | ||
| 18 | ntohs(x) | ||
| 19 | unsigned short x; | ||
| 20 | { | ||
| 21 | #if BYTE_ORDER == LITTLE_ENDIAN | ||
| 22 | u_char *s = (u_char *) &x; | ||
| 23 | return s[0] << 8 | s[1]; | ||
| 24 | #else | ||
| 25 | return x; | ||
| 26 | #endif | ||
| 27 | } | ||
diff --git a/src/lib/libc/net/rcmd.3 b/src/lib/libc/net/rcmd.3 new file mode 100644 index 0000000000..4db847c392 --- /dev/null +++ b/src/lib/libc/net/rcmd.3 | |||
| @@ -0,0 +1,204 @@ | |||
| 1 | .\" $NetBSD: rcmd.3,v 1.8 1995/02/25 06:20:52 cgd Exp $ | ||
| 2 | .\" | ||
| 3 | .\" Copyright (c) 1983, 1991, 1993 | ||
| 4 | .\" The Regents of the University of California. All rights reserved. | ||
| 5 | .\" | ||
| 6 | .\" Redistribution and use in source and binary forms, with or without | ||
| 7 | .\" modification, are permitted provided that the following conditions | ||
| 8 | .\" are met: | ||
| 9 | .\" 1. Redistributions of source code must retain the above copyright | ||
| 10 | .\" notice, this list of conditions and the following disclaimer. | ||
| 11 | .\" 2. Redistributions in binary form must reproduce the above copyright | ||
| 12 | .\" notice, this list of conditions and the following disclaimer in the | ||
| 13 | .\" documentation and/or other materials provided with the distribution. | ||
| 14 | .\" 3. All advertising materials mentioning features or use of this software | ||
| 15 | .\" must display the following acknowledgement: | ||
| 16 | .\" This product includes software developed by the University of | ||
| 17 | .\" California, Berkeley and its contributors. | ||
| 18 | .\" 4. Neither the name of the University nor the names of its contributors | ||
| 19 | .\" may be used to endorse or promote products derived from this software | ||
| 20 | .\" without specific prior written permission. | ||
| 21 | .\" | ||
| 22 | .\" THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND | ||
| 23 | .\" ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE | ||
| 24 | .\" IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE | ||
| 25 | .\" ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE | ||
| 26 | .\" FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL | ||
| 27 | .\" DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS | ||
| 28 | .\" OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) | ||
| 29 | .\" HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT | ||
| 30 | .\" LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY | ||
| 31 | .\" OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF | ||
| 32 | .\" SUCH DAMAGE. | ||
| 33 | .\" | ||
| 34 | .\" @(#)rcmd.3 8.1 (Berkeley) 6/4/93 | ||
| 35 | .\" | ||
| 36 | .Dd June 4, 1993 | ||
| 37 | .Dt RCMD 3 | ||
| 38 | .Os BSD 4.2 | ||
| 39 | .Sh NAME | ||
| 40 | .Nm rcmd , | ||
| 41 | .Nm rresvport , | ||
| 42 | .Nm iruserok , | ||
| 43 | .Nm ruserok | ||
| 44 | .Nd routines for returning a stream to a remote command | ||
| 45 | .Sh SYNOPSIS | ||
| 46 | .Fd #include <unistd.h> | ||
| 47 | .Ft int | ||
| 48 | .Fn rcmd "char **ahost" "int inport" "const char *locuser" "const char *remuser" "const char *cmd" "int *fd2p" | ||
| 49 | .Ft int | ||
| 50 | .Fn rresvport "int *port" | ||
| 51 | .Ft int | ||
| 52 | .Fn iruserok "u_long raddr" "int superuser" "const char *ruser" "const char *luser" | ||
| 53 | .Ft int | ||
| 54 | .Fn ruserok "const char *rhost" "int superuser" "const char *ruser" "const char *luser" | ||
| 55 | .Sh DESCRIPTION | ||
| 56 | The | ||
| 57 | .Fn rcmd | ||
| 58 | function | ||
| 59 | is used by the super-user to execute a command on | ||
| 60 | a remote machine using an authentication scheme based | ||
| 61 | on reserved port numbers. | ||
| 62 | The | ||
| 63 | .Fn rresvport | ||
| 64 | function | ||
| 65 | returns a descriptor to a socket | ||
| 66 | with an address in the privileged port space. | ||
| 67 | The | ||
| 68 | .Fn iruserok | ||
| 69 | and | ||
| 70 | .Fn ruserok | ||
| 71 | functions are used by servers | ||
| 72 | to authenticate clients requesting service with | ||
| 73 | .Fn rcmd . | ||
| 74 | All four functions are present in the same file and are used | ||
| 75 | by the | ||
| 76 | .Xr rshd 8 | ||
| 77 | server (among others). | ||
| 78 | .Pp | ||
| 79 | The | ||
| 80 | .Fn rcmd | ||
| 81 | function | ||
| 82 | looks up the host | ||
| 83 | .Fa *ahost | ||
| 84 | using | ||
| 85 | .Xr gethostbyname 3 , | ||
| 86 | returning \-1 if the host does not exist. | ||
| 87 | Otherwise | ||
| 88 | .Fa *ahost | ||
| 89 | is set to the standard name of the host | ||
| 90 | and a connection is established to a server | ||
| 91 | residing at the well-known Internet port | ||
| 92 | .Fa inport . | ||
| 93 | .Pp | ||
| 94 | If the connection succeeds, | ||
| 95 | a socket in the Internet domain of type | ||
| 96 | .Dv SOCK_STREAM | ||
| 97 | is returned to the caller, and given to the remote | ||
| 98 | command as | ||
| 99 | .Em stdin | ||
| 100 | and | ||
| 101 | .Em stdout . | ||
| 102 | If | ||
| 103 | .Fa fd2p | ||
| 104 | is non-zero, then an auxiliary channel to a control | ||
| 105 | process will be set up, and a descriptor for it will be placed | ||
| 106 | in | ||
| 107 | .Fa *fd2p . | ||
| 108 | The control process will return diagnostic | ||
| 109 | output from the command (unit 2) on this channel, and will also | ||
| 110 | accept bytes on this channel as being | ||
| 111 | .Tn UNIX | ||
| 112 | signal numbers, to be | ||
| 113 | forwarded to the process group of the command. | ||
| 114 | If | ||
| 115 | .Fa fd2p | ||
| 116 | is 0, then the | ||
| 117 | .Em stderr | ||
| 118 | (unit 2 of the remote | ||
| 119 | command) will be made the same as the | ||
| 120 | .Em stdout | ||
| 121 | and no | ||
| 122 | provision is made for sending arbitrary signals to the remote process, | ||
| 123 | although you may be able to get its attention by using out-of-band data. | ||
| 124 | .Pp | ||
| 125 | The protocol is described in detail in | ||
| 126 | .Xr rshd 8 . | ||
| 127 | .Pp | ||
| 128 | The | ||
| 129 | .Fn rresvport | ||
| 130 | function is used to obtain a socket with a privileged | ||
| 131 | address bound to it. This socket is suitable for use | ||
| 132 | by | ||
| 133 | .Fn rcmd | ||
| 134 | and several other functions. Privileged Internet ports are those | ||
| 135 | in the range 0 to 1023. Only the super-user | ||
| 136 | is allowed to bind an address of this sort to a socket. | ||
| 137 | .Pp | ||
| 138 | The | ||
| 139 | .Fn iruserok | ||
| 140 | and | ||
| 141 | .Fn ruserok | ||
| 142 | functions take a remote host's IP address or name, respectively, | ||
| 143 | two user names and a flag indicating whether the local user's | ||
| 144 | name is that of the super-user. | ||
| 145 | Then, if the user is | ||
| 146 | .Em NOT | ||
| 147 | the super-user, it checks the | ||
| 148 | .Pa /etc/hosts.equiv | ||
| 149 | file. | ||
| 150 | If that lookup is not done, or is unsuccessful, the | ||
| 151 | .Pa .rhosts | ||
| 152 | in the local user's home directory is checked to see if the request for | ||
| 153 | service is allowed. | ||
| 154 | .Pp | ||
| 155 | If this file does not exist, is not a regular file, is owned by anyone | ||
| 156 | other than the user or the super-user, or is writeable by anyone other | ||
| 157 | than the owner, the check automatically fails. | ||
| 158 | Zero is returned if the machine name is listed in the | ||
| 159 | .Dq Pa hosts.equiv | ||
| 160 | file, or the host and remote user name are found in the | ||
| 161 | .Dq Pa .rhosts | ||
| 162 | file; otherwise | ||
| 163 | .Fn iruserok | ||
| 164 | and | ||
| 165 | .Fn ruserok | ||
| 166 | return \-1. | ||
| 167 | If the local domain (as obtained from | ||
| 168 | .Xr gethostname 2 ) | ||
| 169 | is the same as the remote domain, only the machine name need be specified. | ||
| 170 | .Pp | ||
| 171 | If the IP address of the remote host is known, | ||
| 172 | .Fn iruserok | ||
| 173 | should be used in preference to | ||
| 174 | .Fn ruserok , | ||
| 175 | as it does not require trusting the DNS server for the remote host's domain. | ||
| 176 | .Sh DIAGNOSTICS | ||
| 177 | The | ||
| 178 | .Fn rcmd | ||
| 179 | function | ||
| 180 | returns a valid socket descriptor on success. | ||
| 181 | It returns \-1 on error and prints a diagnostic message on the standard error. | ||
| 182 | .Pp | ||
| 183 | The | ||
| 184 | .Fn rresvport | ||
| 185 | function | ||
| 186 | returns a valid, bound socket descriptor on success. | ||
| 187 | It returns \-1 on error with the global value | ||
| 188 | .Va errno | ||
| 189 | set according to the reason for failure. | ||
| 190 | The error code | ||
| 191 | .Dv EAGAIN | ||
| 192 | is overloaded to mean ``All network ports in use.'' | ||
| 193 | .Sh SEE ALSO | ||
| 194 | .Xr rlogin 1 , | ||
| 195 | .Xr rsh 1 , | ||
| 196 | .Xr intro 2 , | ||
| 197 | .Xr rexec 3 , | ||
| 198 | .Xr rexecd 8 , | ||
| 199 | .Xr rlogind 8 , | ||
| 200 | .Xr rshd 8 | ||
| 201 | .Sh HISTORY | ||
| 202 | These | ||
| 203 | functions appeared in | ||
| 204 | .Bx 4.2 . | ||
diff --git a/src/lib/libc/net/rcmd.c b/src/lib/libc/net/rcmd.c new file mode 100644 index 0000000000..e0310031b0 --- /dev/null +++ b/src/lib/libc/net/rcmd.c | |||
| @@ -0,0 +1,517 @@ | |||
| 1 | /* $NetBSD: rcmd.c,v 1.12 1995/06/03 22:33:34 mycroft Exp $ */ | ||
| 2 | |||
| 3 | /* | ||
| 4 | * Copyright (c) 1983, 1993, 1994 | ||
| 5 | * The Regents of the University of California. All rights reserved. | ||
| 6 | * | ||
| 7 | * Redistribution and use in source and binary forms, with or without | ||
| 8 | * modification, are permitted provided that the following conditions | ||
| 9 | * are met: | ||
| 10 | * 1. Redistributions of source code must retain the above copyright | ||
| 11 | * notice, this list of conditions and the following disclaimer. | ||
| 12 | * 2. Redistributions in binary form must reproduce the above copyright | ||
| 13 | * notice, this list of conditions and the following disclaimer in the | ||
| 14 | * documentation and/or other materials provided with the distribution. | ||
| 15 | * 3. All advertising materials mentioning features or use of this software | ||
| 16 | * must display the following acknowledgement: | ||
| 17 | * This product includes software developed by the University of | ||
| 18 | * California, Berkeley and its contributors. | ||
| 19 | * 4. Neither the name of the University nor the names of its contributors | ||
| 20 | * may be used to endorse or promote products derived from this software | ||
| 21 | * without specific prior written permission. | ||
| 22 | * | ||
| 23 | * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND | ||
| 24 | * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE | ||
| 25 | * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE | ||
| 26 | * ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE | ||
| 27 | * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL | ||
| 28 | * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS | ||
| 29 | * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) | ||
| 30 | * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT | ||
| 31 | * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY | ||
| 32 | * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF | ||
| 33 | * SUCH DAMAGE. | ||
| 34 | */ | ||
| 35 | |||
| 36 | #if defined(LIBC_SCCS) && !defined(lint) | ||
| 37 | #if 0 | ||
| 38 | static char sccsid[] = "@(#)rcmd.c 8.3 (Berkeley) 3/26/94"; | ||
| 39 | #else | ||
| 40 | static char *rcsid = "$NetBSD: rcmd.c,v 1.12 1995/06/03 22:33:34 mycroft Exp $"; | ||
| 41 | #endif | ||
| 42 | #endif /* LIBC_SCCS and not lint */ | ||
| 43 | |||
| 44 | #include <sys/param.h> | ||
| 45 | #include <sys/socket.h> | ||
| 46 | #include <sys/stat.h> | ||
| 47 | |||
| 48 | #include <netinet/in.h> | ||
| 49 | #include <arpa/inet.h> | ||
| 50 | |||
| 51 | #include <signal.h> | ||
| 52 | #include <fcntl.h> | ||
| 53 | #include <netdb.h> | ||
| 54 | #include <unistd.h> | ||
| 55 | #include <pwd.h> | ||
| 56 | #include <errno.h> | ||
| 57 | #include <stdio.h> | ||
| 58 | #include <ctype.h> | ||
| 59 | #include <string.h> | ||
| 60 | |||
| 61 | int __ivaliduser __P((FILE *, u_long, const char *, const char *)); | ||
| 62 | static int __icheckhost __P((u_long, const char *)); | ||
| 63 | |||
| 64 | int | ||
| 65 | rcmd(ahost, rport, locuser, remuser, cmd, fd2p) | ||
| 66 | char **ahost; | ||
| 67 | u_short rport; | ||
| 68 | const char *locuser, *remuser, *cmd; | ||
| 69 | int *fd2p; | ||
| 70 | { | ||
| 71 | struct hostent *hp; | ||
| 72 | struct sockaddr_in sin, from; | ||
| 73 | fd_set reads; | ||
| 74 | long oldmask; | ||
| 75 | pid_t pid; | ||
| 76 | int s, lport, timo; | ||
| 77 | char c; | ||
| 78 | |||
| 79 | pid = getpid(); | ||
| 80 | hp = gethostbyname(*ahost); | ||
| 81 | if (hp == NULL) { | ||
| 82 | herror(*ahost); | ||
| 83 | return (-1); | ||
| 84 | } | ||
| 85 | *ahost = hp->h_name; | ||
| 86 | oldmask = sigblock(sigmask(SIGURG)); | ||
| 87 | for (timo = 1, lport = IPPORT_RESERVED - 1;;) { | ||
| 88 | s = rresvport(&lport); | ||
| 89 | if (s < 0) { | ||
| 90 | if (errno == EAGAIN) | ||
| 91 | (void)fprintf(stderr, | ||
| 92 | "rcmd: socket: All ports in use\n"); | ||
| 93 | else | ||
| 94 | (void)fprintf(stderr, "rcmd: socket: %s\n", | ||
| 95 | strerror(errno)); | ||
| 96 | sigsetmask(oldmask); | ||
| 97 | return (-1); | ||
| 98 | } | ||
| 99 | fcntl(s, F_SETOWN, pid); | ||
| 100 | sin.sin_len = sizeof(struct sockaddr_in); | ||
| 101 | sin.sin_family = hp->h_addrtype; | ||
| 102 | sin.sin_port = rport; | ||
| 103 | bcopy(hp->h_addr_list[0], &sin.sin_addr, hp->h_length); | ||
| 104 | if (connect(s, (struct sockaddr *)&sin, sizeof(sin)) >= 0) | ||
| 105 | break; | ||
| 106 | (void)close(s); | ||
| 107 | if (errno == EADDRINUSE) { | ||
| 108 | lport--; | ||
| 109 | continue; | ||
| 110 | } | ||
| 111 | if (errno == ECONNREFUSED && timo <= 16) { | ||
| 112 | (void)sleep(timo); | ||
| 113 | timo *= 2; | ||
| 114 | continue; | ||
| 115 | } | ||
| 116 | if (hp->h_addr_list[1] != NULL) { | ||
| 117 | int oerrno = errno; | ||
| 118 | |||
| 119 | (void)fprintf(stderr, "connect to address %s: ", | ||
| 120 | inet_ntoa(sin.sin_addr)); | ||
| 121 | errno = oerrno; | ||
| 122 | perror(0); | ||
| 123 | hp->h_addr_list++; | ||
| 124 | bcopy(hp->h_addr_list[0], &sin.sin_addr, hp->h_length); | ||
| 125 | (void)fprintf(stderr, "Trying %s...\n", | ||
| 126 | inet_ntoa(sin.sin_addr)); | ||
| 127 | continue; | ||
| 128 | } | ||
| 129 | (void)fprintf(stderr, "%s: %s\n", hp->h_name, strerror(errno)); | ||
| 130 | sigsetmask(oldmask); | ||
| 131 | return (-1); | ||
| 132 | } | ||
| 133 | lport--; | ||
| 134 | if (fd2p == 0) { | ||
| 135 | write(s, "", 1); | ||
| 136 | lport = 0; | ||
| 137 | } else { | ||
| 138 | char num[8]; | ||
| 139 | int s2 = rresvport(&lport), s3; | ||
| 140 | int len = sizeof(from); | ||
| 141 | |||
| 142 | if (s2 < 0) | ||
| 143 | goto bad; | ||
| 144 | listen(s2, 1); | ||
| 145 | (void)snprintf(num, sizeof(num), "%d", lport); | ||
| 146 | if (write(s, num, strlen(num)+1) != strlen(num)+1) { | ||
| 147 | (void)fprintf(stderr, | ||
| 148 | "rcmd: write (setting up stderr): %s\n", | ||
| 149 | strerror(errno)); | ||
| 150 | (void)close(s2); | ||
| 151 | goto bad; | ||
| 152 | } | ||
| 153 | FD_ZERO(&reads); | ||
| 154 | FD_SET(s, &reads); | ||
| 155 | FD_SET(s2, &reads); | ||
| 156 | errno = 0; | ||
| 157 | if (select(MAX(s, s2) + 1, &reads, 0, 0, 0) < 1 || | ||
| 158 | !FD_ISSET(s2, &reads)) { | ||
| 159 | if (errno != 0) | ||
| 160 | (void)fprintf(stderr, | ||
| 161 | "rcmd: select (setting up stderr): %s\n", | ||
| 162 | strerror(errno)); | ||
| 163 | else | ||
| 164 | (void)fprintf(stderr, | ||
| 165 | "select: protocol failure in circuit setup\n"); | ||
| 166 | (void)close(s2); | ||
| 167 | goto bad; | ||
| 168 | } | ||
| 169 | s3 = accept(s2, (struct sockaddr *)&from, &len); | ||
| 170 | (void)close(s2); | ||
| 171 | if (s3 < 0) { | ||
| 172 | (void)fprintf(stderr, | ||
| 173 | "rcmd: accept: %s\n", strerror(errno)); | ||
| 174 | lport = 0; | ||
| 175 | goto bad; | ||
| 176 | } | ||
| 177 | *fd2p = s3; | ||
| 178 | from.sin_port = ntohs(from.sin_port); | ||
| 179 | if (from.sin_family != AF_INET || | ||
| 180 | from.sin_port >= IPPORT_RESERVED || | ||
| 181 | from.sin_port < IPPORT_RESERVED / 2) { | ||
| 182 | (void)fprintf(stderr, | ||
| 183 | "socket: protocol failure in circuit setup.\n"); | ||
| 184 | goto bad2; | ||
| 185 | } | ||
| 186 | } | ||
| 187 | (void)write(s, locuser, strlen(locuser)+1); | ||
| 188 | (void)write(s, remuser, strlen(remuser)+1); | ||
| 189 | (void)write(s, cmd, strlen(cmd)+1); | ||
| 190 | if (read(s, &c, 1) != 1) { | ||
| 191 | (void)fprintf(stderr, | ||
| 192 | "rcmd: %s: %s\n", *ahost, strerror(errno)); | ||
| 193 | goto bad2; | ||
| 194 | } | ||
| 195 | if (c != 0) { | ||
| 196 | while (read(s, &c, 1) == 1) { | ||
| 197 | (void)write(STDERR_FILENO, &c, 1); | ||
| 198 | if (c == '\n') | ||
| 199 | break; | ||
| 200 | } | ||
| 201 | goto bad2; | ||
| 202 | } | ||
| 203 | sigsetmask(oldmask); | ||
| 204 | return (s); | ||
| 205 | bad2: | ||
| 206 | if (lport) | ||
| 207 | (void)close(*fd2p); | ||
| 208 | bad: | ||
| 209 | (void)close(s); | ||
| 210 | sigsetmask(oldmask); | ||
| 211 | return (-1); | ||
| 212 | } | ||
| 213 | |||
| 214 | int | ||
| 215 | rresvport(alport) | ||
| 216 | int *alport; | ||
| 217 | { | ||
| 218 | struct sockaddr_in sin; | ||
| 219 | int s; | ||
| 220 | |||
| 221 | sin.sin_len = sizeof(struct sockaddr_in); | ||
| 222 | sin.sin_family = AF_INET; | ||
| 223 | sin.sin_addr.s_addr = INADDR_ANY; | ||
| 224 | s = socket(AF_INET, SOCK_STREAM, 0); | ||
| 225 | if (s < 0) | ||
| 226 | return (-1); | ||
| 227 | for (;;) { | ||
| 228 | sin.sin_port = htons((u_short)*alport); | ||
| 229 | if (bind(s, (struct sockaddr *)&sin, sizeof(sin)) >= 0) | ||
| 230 | return (s); | ||
| 231 | if (errno != EADDRINUSE) { | ||
| 232 | (void)close(s); | ||
| 233 | return (-1); | ||
| 234 | } | ||
| 235 | (*alport)--; | ||
| 236 | if (*alport == IPPORT_RESERVED/2) { | ||
| 237 | (void)close(s); | ||
| 238 | errno = EAGAIN; /* close */ | ||
| 239 | return (-1); | ||
| 240 | } | ||
| 241 | } | ||
| 242 | } | ||
| 243 | |||
| 244 | int __check_rhosts_file = 1; | ||
| 245 | char *__rcmd_errstr; | ||
| 246 | |||
| 247 | int | ||
| 248 | ruserok(rhost, superuser, ruser, luser) | ||
| 249 | const char *rhost, *ruser, *luser; | ||
| 250 | int superuser; | ||
| 251 | { | ||
| 252 | struct hostent *hp; | ||
| 253 | char **ap; | ||
| 254 | int i; | ||
| 255 | #define MAXADDRS 35 | ||
| 256 | u_long addrs[MAXADDRS + 1]; | ||
| 257 | |||
| 258 | if ((hp = gethostbyname(rhost)) == NULL) | ||
| 259 | return (-1); | ||
| 260 | for (i = 0, ap = hp->h_addr_list; *ap && i < MAXADDRS; ++ap, ++i) | ||
| 261 | bcopy(*ap, &addrs[i], sizeof(addrs[i])); | ||
| 262 | addrs[i] = 0; | ||
| 263 | |||
| 264 | for (i = 0; i < MAXADDRS && addrs[i]; i++) | ||
| 265 | if (iruserok(addrs[i], superuser, ruser, luser) == 0) | ||
| 266 | return (0); | ||
| 267 | return (-1); | ||
| 268 | } | ||
| 269 | |||
| 270 | /* | ||
| 271 | * New .rhosts strategy: We are passed an ip address. We spin through | ||
| 272 | * hosts.equiv and .rhosts looking for a match. When the .rhosts only | ||
| 273 | * has ip addresses, we don't have to trust a nameserver. When it | ||
| 274 | * contains hostnames, we spin through the list of addresses the nameserver | ||
| 275 | * gives us and look for a match. | ||
| 276 | * | ||
| 277 | * Returns 0 if ok, -1 if not ok. | ||
| 278 | */ | ||
| 279 | int | ||
| 280 | iruserok(raddr, superuser, ruser, luser) | ||
| 281 | u_long raddr; | ||
| 282 | int superuser; | ||
| 283 | const char *ruser, *luser; | ||
| 284 | { | ||
| 285 | register char *cp; | ||
| 286 | struct stat sbuf; | ||
| 287 | struct passwd *pwd; | ||
| 288 | FILE *hostf; | ||
| 289 | uid_t uid; | ||
| 290 | int first; | ||
| 291 | char pbuf[MAXPATHLEN]; | ||
| 292 | |||
| 293 | first = 1; | ||
| 294 | hostf = superuser ? NULL : fopen(_PATH_HEQUIV, "r"); | ||
| 295 | again: | ||
| 296 | if (hostf) { | ||
| 297 | if (__ivaliduser(hostf, raddr, luser, ruser) == 0) { | ||
| 298 | (void)fclose(hostf); | ||
| 299 | return (0); | ||
| 300 | } | ||
| 301 | (void)fclose(hostf); | ||
| 302 | } | ||
| 303 | if (first == 1 && (__check_rhosts_file || superuser)) { | ||
| 304 | first = 0; | ||
| 305 | if ((pwd = getpwnam(luser)) == NULL) | ||
| 306 | return (-1); | ||
| 307 | (void)strcpy(pbuf, pwd->pw_dir); | ||
| 308 | (void)strcat(pbuf, "/.rhosts"); | ||
| 309 | |||
| 310 | /* | ||
| 311 | * Change effective uid while opening .rhosts. If root and | ||
| 312 | * reading an NFS mounted file system, can't read files that | ||
| 313 | * are protected read/write owner only. | ||
| 314 | */ | ||
| 315 | uid = geteuid(); | ||
| 316 | (void)seteuid(pwd->pw_uid); | ||
| 317 | hostf = fopen(pbuf, "r"); | ||
| 318 | (void)seteuid(uid); | ||
| 319 | |||
| 320 | if (hostf == NULL) | ||
| 321 | return (-1); | ||
| 322 | /* | ||
| 323 | * If not a regular file, or is owned by someone other than | ||
| 324 | * user or root or if writeable by anyone but the owner, quit. | ||
| 325 | */ | ||
| 326 | cp = NULL; | ||
| 327 | if (lstat(pbuf, &sbuf) < 0) | ||
| 328 | cp = ".rhosts lstat failed"; | ||
| 329 | else if (!S_ISREG(sbuf.st_mode)) | ||
| 330 | cp = ".rhosts not regular file"; | ||
| 331 | else if (fstat(fileno(hostf), &sbuf) < 0) | ||
| 332 | cp = ".rhosts fstat failed"; | ||
| 333 | else if (sbuf.st_uid && sbuf.st_uid != pwd->pw_uid) | ||
| 334 | cp = "bad .rhosts owner"; | ||
| 335 | else if (sbuf.st_mode & (S_IWGRP|S_IWOTH)) | ||
| 336 | cp = ".rhosts writeable by other than owner"; | ||
| 337 | /* If there were any problems, quit. */ | ||
| 338 | if (cp) { | ||
| 339 | __rcmd_errstr = cp; | ||
| 340 | (void)fclose(hostf); | ||
| 341 | return (-1); | ||
| 342 | } | ||
| 343 | goto again; | ||
| 344 | } | ||
| 345 | return (-1); | ||
| 346 | } | ||
| 347 | |||
| 348 | /* | ||
| 349 | * XXX | ||
| 350 | * Don't make static, used by lpd(8). | ||
| 351 | * | ||
| 352 | * Returns 0 if ok, -1 if not ok. | ||
| 353 | */ | ||
| 354 | int | ||
| 355 | __ivaliduser(hostf, raddr, luser, ruser) | ||
| 356 | FILE *hostf; | ||
| 357 | u_long raddr; | ||
| 358 | const char *luser, *ruser; | ||
| 359 | { | ||
| 360 | register char *user, *p; | ||
| 361 | int ch; | ||
| 362 | char buf[MAXHOSTNAMELEN + 128]; /* host + login */ | ||
| 363 | const char *auser, *ahost; | ||
| 364 | int hostok, userok; | ||
| 365 | char rhost[MAXHOSTNAMELEN]; | ||
| 366 | struct hostent *hp; | ||
| 367 | char domain[MAXHOSTNAMELEN]; | ||
| 368 | |||
| 369 | getdomainname(domain, sizeof(domain)); | ||
| 370 | |||
| 371 | while (fgets(buf, sizeof(buf), hostf)) { | ||
| 372 | p = buf; | ||
| 373 | /* Skip lines that are too long. */ | ||
| 374 | if (strchr(p, '\n') == NULL) { | ||
| 375 | while ((ch = getc(hostf)) != '\n' && ch != EOF); | ||
| 376 | continue; | ||
| 377 | } | ||
| 378 | while (*p != '\n' && *p != ' ' && *p != '\t' && *p != '\0') { | ||
| 379 | *p = isupper(*p) ? tolower(*p) : *p; | ||
| 380 | p++; | ||
| 381 | } | ||
| 382 | if (*p == ' ' || *p == '\t') { | ||
| 383 | *p++ = '\0'; | ||
| 384 | while (*p == ' ' || *p == '\t') | ||
| 385 | p++; | ||
| 386 | user = p; | ||
| 387 | while (*p != '\n' && *p != ' ' && | ||
| 388 | *p != '\t' && *p != '\0') | ||
| 389 | p++; | ||
| 390 | } else | ||
| 391 | user = p; | ||
| 392 | *p = '\0'; | ||
| 393 | |||
| 394 | if (p == buf) | ||
| 395 | continue; | ||
| 396 | |||
| 397 | auser = *user ? user : luser; | ||
| 398 | ahost = buf; | ||
| 399 | |||
| 400 | if ((hp = gethostbyaddr((char *) &raddr, | ||
| 401 | sizeof(raddr), AF_INET)) == NULL) { | ||
| 402 | abort(); | ||
| 403 | return -1; | ||
| 404 | } | ||
| 405 | (void) strncpy(rhost, hp->h_name, sizeof(rhost)); | ||
| 406 | rhost[sizeof(rhost) - 1] = '\0'; | ||
| 407 | |||
| 408 | if (ahost[0] == '+') | ||
| 409 | switch (ahost[1]) { | ||
| 410 | case '\0': | ||
| 411 | hostok = 1; | ||
| 412 | break; | ||
| 413 | |||
| 414 | case '@': | ||
| 415 | hostok = innetgr(&ahost[2], rhost, NULL, | ||
| 416 | domain); | ||
| 417 | break; | ||
| 418 | |||
| 419 | default: | ||
| 420 | hostok = __icheckhost(raddr, &ahost[1]); | ||
| 421 | break; | ||
| 422 | } | ||
| 423 | else if (ahost[0] == '-') | ||
| 424 | switch (ahost[1]) { | ||
| 425 | case '\0': | ||
| 426 | hostok = -1; | ||
| 427 | break; | ||
| 428 | |||
| 429 | case '@': | ||
| 430 | hostok = -innetgr(&ahost[2], rhost, NULL, | ||
| 431 | domain); | ||
| 432 | break; | ||
| 433 | |||
| 434 | default: | ||
| 435 | hostok = -__icheckhost(raddr, &ahost[1]); | ||
| 436 | break; | ||
| 437 | } | ||
| 438 | else | ||
| 439 | hostok = __icheckhost(raddr, ahost); | ||
| 440 | |||
| 441 | |||
| 442 | if (auser[0] == '+') | ||
| 443 | switch (auser[1]) { | ||
| 444 | case '\0': | ||
| 445 | userok = 1; | ||
| 446 | break; | ||
| 447 | |||
| 448 | case '@': | ||
| 449 | userok = innetgr(&auser[2], NULL, ruser, | ||
| 450 | domain); | ||
| 451 | break; | ||
| 452 | |||
| 453 | default: | ||
| 454 | userok = strcmp(ruser, &auser[1]) == 0; | ||
| 455 | break; | ||
| 456 | } | ||
| 457 | else if (auser[0] == '-') | ||
| 458 | switch (auser[1]) { | ||
| 459 | case '\0': | ||
| 460 | userok = -1; | ||
| 461 | break; | ||
| 462 | |||
| 463 | case '@': | ||
| 464 | userok = -innetgr(&auser[2], NULL, ruser, | ||
| 465 | domain); | ||
| 466 | break; | ||
| 467 | |||
| 468 | default: | ||
| 469 | userok = -(strcmp(ruser, &auser[1]) == 0); | ||
| 470 | break; | ||
| 471 | } | ||
| 472 | else | ||
| 473 | userok = strcmp(ruser, auser) == 0; | ||
| 474 | |||
| 475 | /* Check if one component did not match */ | ||
| 476 | if (hostok == 0 || userok == 0) | ||
| 477 | continue; | ||
| 478 | |||
| 479 | /* Check if we got a forbidden pair */ | ||
| 480 | if (userok == -1 || hostok == -1) | ||
| 481 | return -1; | ||
| 482 | |||
| 483 | /* Check if we got a valid pair */ | ||
| 484 | if (hostok == 1 && userok == 1) | ||
| 485 | return 0; | ||
| 486 | } | ||
| 487 | return -1; | ||
| 488 | } | ||
| 489 | |||
| 490 | /* | ||
| 491 | * Returns "true" if match, 0 if no match. | ||
| 492 | */ | ||
| 493 | static int | ||
| 494 | __icheckhost(raddr, lhost) | ||
| 495 | u_long raddr; | ||
| 496 | const char *lhost; | ||
| 497 | { | ||
| 498 | register struct hostent *hp; | ||
| 499 | register u_long laddr; | ||
| 500 | register char **pp; | ||
| 501 | |||
| 502 | /* Try for raw ip address first. */ | ||
| 503 | if (isdigit(*lhost) && (long)(laddr = inet_addr(lhost)) != -1) | ||
| 504 | return (raddr == laddr); | ||
| 505 | |||
| 506 | /* Better be a hostname. */ | ||
| 507 | if ((hp = gethostbyname(lhost)) == NULL) | ||
| 508 | return (0); | ||
| 509 | |||
| 510 | /* Spin through ip addresses. */ | ||
| 511 | for (pp = hp->h_addr_list; *pp; ++pp) | ||
| 512 | if (!bcmp(&raddr, *pp, sizeof(u_long))) | ||
| 513 | return (1); | ||
| 514 | |||
| 515 | /* No match. */ | ||
| 516 | return (0); | ||
| 517 | } | ||
diff --git a/src/lib/libc/net/recv.c b/src/lib/libc/net/recv.c new file mode 100644 index 0000000000..44296378cb --- /dev/null +++ b/src/lib/libc/net/recv.c | |||
| @@ -0,0 +1,56 @@ | |||
| 1 | /* $NetBSD: recv.c,v 1.6 1995/02/25 06:20:54 cgd Exp $ */ | ||
| 2 | |||
| 3 | /* | ||
| 4 | * Copyright (c) 1988, 1993 | ||
| 5 | * The Regents of the University of California. All rights reserved. | ||
| 6 | * | ||
| 7 | * Redistribution and use in source and binary forms, with or without | ||
| 8 | * modification, are permitted provided that the following conditions | ||
| 9 | * are met: | ||
| 10 | * 1. Redistributions of source code must retain the above copyright | ||
| 11 | * notice, this list of conditions and the following disclaimer. | ||
| 12 | * 2. Redistributions in binary form must reproduce the above copyright | ||
| 13 | * notice, this list of conditions and the following disclaimer in the | ||
| 14 | * documentation and/or other materials provided with the distribution. | ||
| 15 | * 3. All advertising materials mentioning features or use of this software | ||
| 16 | * must display the following acknowledgement: | ||
| 17 | * This product includes software developed by the University of | ||
| 18 | * California, Berkeley and its contributors. | ||
| 19 | * 4. Neither the name of the University nor the names of its contributors | ||
| 20 | * may be used to endorse or promote products derived from this software | ||
| 21 | * without specific prior written permission. | ||
| 22 | * | ||
| 23 | * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND | ||
| 24 | * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE | ||
| 25 | * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE | ||
| 26 | * ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE | ||
| 27 | * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL | ||
| 28 | * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS | ||
| 29 | * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) | ||
| 30 | * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT | ||
| 31 | * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY | ||
| 32 | * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF | ||
| 33 | * SUCH DAMAGE. | ||
| 34 | */ | ||
| 35 | |||
| 36 | #if defined(LIBC_SCCS) && !defined(lint) | ||
| 37 | #if 0 | ||
| 38 | static char sccsid[] = "@(#)recv.c 8.2 (Berkeley) 2/21/94"; | ||
| 39 | #else | ||
| 40 | static char rcsid[] = "$NetBSD: recv.c,v 1.6 1995/02/25 06:20:54 cgd Exp $"; | ||
| 41 | #endif | ||
| 42 | #endif /* LIBC_SCCS and not lint */ | ||
| 43 | |||
| 44 | #include <sys/types.h> | ||
| 45 | #include <sys/socket.h> | ||
| 46 | |||
| 47 | #include <stddef.h> | ||
| 48 | |||
| 49 | ssize_t | ||
| 50 | recv(s, buf, len, flags) | ||
| 51 | int s, flags; | ||
| 52 | size_t len; | ||
| 53 | void *buf; | ||
| 54 | { | ||
| 55 | return (recvfrom(s, buf, len, flags, NULL, 0)); | ||
| 56 | } | ||
diff --git a/src/lib/libc/net/res_comp.c b/src/lib/libc/net/res_comp.c new file mode 100644 index 0000000000..9d7bbecdda --- /dev/null +++ b/src/lib/libc/net/res_comp.c | |||
| @@ -0,0 +1,364 @@ | |||
| 1 | /* $NetBSD: res_comp.c,v 1.6 1995/02/25 06:20:55 cgd Exp $ */ | ||
| 2 | |||
| 3 | /*- | ||
| 4 | * Copyright (c) 1985, 1993 | ||
| 5 | * The Regents of the University of California. All rights reserved. | ||
| 6 | * | ||
| 7 | * Redistribution and use in source and binary forms, with or without | ||
| 8 | * modification, are permitted provided that the following conditions | ||
| 9 | * are met: | ||
| 10 | * 1. Redistributions of source code must retain the above copyright | ||
| 11 | * notice, this list of conditions and the following disclaimer. | ||
| 12 | * 2. Redistributions in binary form must reproduce the above copyright | ||
| 13 | * notice, this list of conditions and the following disclaimer in the | ||
| 14 | * documentation and/or other materials provided with the distribution. | ||
| 15 | * 3. All advertising materials mentioning features or use of this software | ||
| 16 | * must display the following acknowledgement: | ||
| 17 | * This product includes software developed by the University of | ||
| 18 | * California, Berkeley and its contributors. | ||
| 19 | * 4. Neither the name of the University nor the names of its contributors | ||
| 20 | * may be used to endorse or promote products derived from this software | ||
| 21 | * without specific prior written permission. | ||
| 22 | * | ||
| 23 | * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND | ||
| 24 | * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE | ||
| 25 | * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE | ||
| 26 | * ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE | ||
| 27 | * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL | ||
| 28 | * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS | ||
| 29 | * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) | ||
| 30 | * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT | ||
| 31 | * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY | ||
| 32 | * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF | ||
| 33 | * SUCH DAMAGE. | ||
| 34 | * - | ||
| 35 | * Portions Copyright (c) 1993 by Digital Equipment Corporation. | ||
| 36 | * | ||
| 37 | * Permission to use, copy, modify, and distribute this software for any | ||
| 38 | * purpose with or without fee is hereby granted, provided that the above | ||
| 39 | * copyright notice and this permission notice appear in all copies, and that | ||
| 40 | * the name of Digital Equipment Corporation not be used in advertising or | ||
| 41 | * publicity pertaining to distribution of the document or software without | ||
| 42 | * specific, written prior permission. | ||
| 43 | * | ||
| 44 | * THE SOFTWARE IS PROVIDED "AS IS" AND DIGITAL EQUIPMENT CORP. DISCLAIMS ALL | ||
| 45 | * WARRANTIES WITH REGARD TO THIS SOFTWARE, INCLUDING ALL IMPLIED WARRANTIES | ||
| 46 | * OF MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL DIGITAL EQUIPMENT | ||
| 47 | * CORPORATION BE LIABLE FOR ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL | ||
| 48 | * DAMAGES OR ANY DAMAGES WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR | ||
| 49 | * PROFITS, WHETHER IN AN ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS | ||
| 50 | * ACTION, ARISING OUT OF OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS | ||
| 51 | * SOFTWARE. | ||
| 52 | * - | ||
| 53 | * --Copyright-- | ||
| 54 | */ | ||
| 55 | |||
| 56 | #if defined(LIBC_SCCS) && !defined(lint) | ||
| 57 | #if 0 | ||
| 58 | static char sccsid[] = "@(#)res_comp.c 8.1 (Berkeley) 6/4/93"; | ||
| 59 | static char rcsid[] = "$Id: res_comp.c,v 4.9.1.1 1993/05/02 22:43:03 vixie Rel "; | ||
| 60 | #else | ||
| 61 | static char rcsid[] = "$NetBSD: res_comp.c,v 1.6 1995/02/25 06:20:55 cgd Exp $"; | ||
| 62 | #endif | ||
| 63 | #endif /* LIBC_SCCS and not lint */ | ||
| 64 | |||
| 65 | #include <sys/param.h> | ||
| 66 | #include <arpa/nameser.h> | ||
| 67 | #include <netinet/in.h> | ||
| 68 | #include <resolv.h> | ||
| 69 | #include <stdio.h> | ||
| 70 | |||
| 71 | static int dn_find(); | ||
| 72 | |||
| 73 | /* | ||
| 74 | * Expand compressed domain name 'comp_dn' to full domain name. | ||
| 75 | * 'msg' is a pointer to the begining of the message, | ||
| 76 | * 'eomorig' points to the first location after the message, | ||
| 77 | * 'exp_dn' is a pointer to a buffer of size 'length' for the result. | ||
| 78 | * Return size of compressed name or -1 if there was an error. | ||
| 79 | */ | ||
| 80 | dn_expand(msg, eomorig, comp_dn, exp_dn, length) | ||
| 81 | const u_char *msg, *eomorig, *comp_dn; | ||
| 82 | u_char *exp_dn; | ||
| 83 | int length; | ||
| 84 | { | ||
| 85 | register u_char *cp, *dn; | ||
| 86 | register int n, c; | ||
| 87 | u_char *eom; | ||
| 88 | int len = -1, checked = 0; | ||
| 89 | |||
| 90 | dn = exp_dn; | ||
| 91 | cp = (u_char *)comp_dn; | ||
| 92 | eom = exp_dn + length; | ||
| 93 | /* | ||
| 94 | * fetch next label in domain name | ||
| 95 | */ | ||
| 96 | while (n = *cp++) { | ||
| 97 | /* | ||
| 98 | * Check for indirection | ||
| 99 | */ | ||
| 100 | switch (n & INDIR_MASK) { | ||
| 101 | case 0: | ||
| 102 | if (dn != exp_dn) { | ||
| 103 | if (dn >= eom) | ||
| 104 | return (-1); | ||
| 105 | *dn++ = '.'; | ||
| 106 | } | ||
| 107 | if (dn+n >= eom) | ||
| 108 | return (-1); | ||
| 109 | checked += n + 1; | ||
| 110 | while (--n >= 0) { | ||
| 111 | if ((c = *cp++) == '.') { | ||
| 112 | if (dn + n + 2 >= eom) | ||
| 113 | return (-1); | ||
| 114 | *dn++ = '\\'; | ||
| 115 | } | ||
| 116 | *dn++ = c; | ||
| 117 | if (cp >= eomorig) /* out of range */ | ||
| 118 | return(-1); | ||
| 119 | } | ||
| 120 | break; | ||
| 121 | |||
| 122 | case INDIR_MASK: | ||
| 123 | if (len < 0) | ||
| 124 | len = cp - comp_dn + 1; | ||
| 125 | cp = (u_char *)msg + (((n & 0x3f) << 8) | (*cp & 0xff)); | ||
| 126 | if (cp < msg || cp >= eomorig) /* out of range */ | ||
| 127 | return(-1); | ||
| 128 | checked += 2; | ||
| 129 | /* | ||
| 130 | * Check for loops in the compressed name; | ||
| 131 | * if we've looked at the whole message, | ||
| 132 | * there must be a loop. | ||
| 133 | */ | ||
| 134 | if (checked >= eomorig - msg) | ||
| 135 | return (-1); | ||
| 136 | break; | ||
| 137 | |||
| 138 | default: | ||
| 139 | return (-1); /* flag error */ | ||
| 140 | } | ||
| 141 | } | ||
| 142 | *dn = '\0'; | ||
| 143 | if (len < 0) | ||
| 144 | len = cp - comp_dn; | ||
| 145 | return (len); | ||
| 146 | } | ||
| 147 | |||
| 148 | /* | ||
| 149 | * Compress domain name 'exp_dn' into 'comp_dn'. | ||
| 150 | * Return the size of the compressed name or -1. | ||
| 151 | * 'length' is the size of the array pointed to by 'comp_dn'. | ||
| 152 | * 'dnptrs' is a list of pointers to previous compressed names. dnptrs[0] | ||
| 153 | * is a pointer to the beginning of the message. The list ends with NULL. | ||
| 154 | * 'lastdnptr' is a pointer to the end of the arrary pointed to | ||
| 155 | * by 'dnptrs'. Side effect is to update the list of pointers for | ||
| 156 | * labels inserted into the message as we compress the name. | ||
| 157 | * If 'dnptr' is NULL, we don't try to compress names. If 'lastdnptr' | ||
| 158 | * is NULL, we don't update the list. | ||
| 159 | */ | ||
| 160 | dn_comp(exp_dn, comp_dn, length, dnptrs, lastdnptr) | ||
| 161 | const u_char *exp_dn; | ||
| 162 | u_char *comp_dn, **dnptrs, **lastdnptr; | ||
| 163 | int length; | ||
| 164 | { | ||
| 165 | register u_char *cp, *dn; | ||
| 166 | register int c, l; | ||
| 167 | u_char **cpp, **lpp, *sp, *eob; | ||
| 168 | u_char *msg; | ||
| 169 | |||
| 170 | dn = (u_char *)exp_dn; | ||
| 171 | cp = comp_dn; | ||
| 172 | eob = cp + length; | ||
| 173 | if (dnptrs != NULL) { | ||
| 174 | if ((msg = *dnptrs++) != NULL) { | ||
| 175 | for (cpp = dnptrs; *cpp != NULL; cpp++) | ||
| 176 | ; | ||
| 177 | lpp = cpp; /* end of list to search */ | ||
| 178 | } | ||
| 179 | } else | ||
| 180 | msg = NULL; | ||
| 181 | for (c = *dn++; c != '\0'; ) { | ||
| 182 | /* look to see if we can use pointers */ | ||
| 183 | if (msg != NULL) { | ||
| 184 | if ((l = dn_find(dn-1, msg, dnptrs, lpp)) >= 0) { | ||
| 185 | if (cp+1 >= eob) | ||
| 186 | return (-1); | ||
| 187 | *cp++ = (l >> 8) | INDIR_MASK; | ||
| 188 | *cp++ = l % 256; | ||
| 189 | return (cp - comp_dn); | ||
| 190 | } | ||
| 191 | /* not found, save it */ | ||
| 192 | if (lastdnptr != NULL && cpp < lastdnptr-1) { | ||
| 193 | *cpp++ = cp; | ||
| 194 | *cpp = NULL; | ||
| 195 | } | ||
| 196 | } | ||
| 197 | sp = cp++; /* save ptr to length byte */ | ||
| 198 | do { | ||
| 199 | if (c == '.') { | ||
| 200 | c = *dn++; | ||
| 201 | break; | ||
| 202 | } | ||
| 203 | if (c == '\\') { | ||
| 204 | if ((c = *dn++) == '\0') | ||
| 205 | break; | ||
| 206 | } | ||
| 207 | if (cp >= eob) { | ||
| 208 | if (msg != NULL) | ||
| 209 | *lpp = NULL; | ||
| 210 | return (-1); | ||
| 211 | } | ||
| 212 | *cp++ = c; | ||
| 213 | } while ((c = *dn++) != '\0'); | ||
| 214 | /* catch trailing '.'s but not '..' */ | ||
| 215 | if ((l = cp - sp - 1) == 0 && c == '\0') { | ||
| 216 | cp--; | ||
| 217 | break; | ||
| 218 | } | ||
| 219 | if (l <= 0 || l > MAXLABEL) { | ||
| 220 | if (msg != NULL) | ||
| 221 | *lpp = NULL; | ||
| 222 | return (-1); | ||
| 223 | } | ||
| 224 | *sp = l; | ||
| 225 | } | ||
| 226 | if (cp >= eob) { | ||
| 227 | if (msg != NULL) | ||
| 228 | *lpp = NULL; | ||
| 229 | return (-1); | ||
| 230 | } | ||
| 231 | *cp++ = '\0'; | ||
| 232 | return (cp - comp_dn); | ||
| 233 | } | ||
| 234 | |||
| 235 | /* | ||
| 236 | * Skip over a compressed domain name. Return the size or -1. | ||
| 237 | */ | ||
| 238 | __dn_skipname(comp_dn, eom) | ||
| 239 | const u_char *comp_dn, *eom; | ||
| 240 | { | ||
| 241 | register u_char *cp; | ||
| 242 | register int n; | ||
| 243 | |||
| 244 | cp = (u_char *)comp_dn; | ||
| 245 | while (cp < eom && (n = *cp++)) { | ||
| 246 | /* | ||
| 247 | * check for indirection | ||
| 248 | */ | ||
| 249 | switch (n & INDIR_MASK) { | ||
| 250 | case 0: /* normal case, n == len */ | ||
| 251 | cp += n; | ||
| 252 | continue; | ||
| 253 | case INDIR_MASK: /* indirection */ | ||
| 254 | cp++; | ||
| 255 | break; | ||
| 256 | default: /* illegal type */ | ||
| 257 | return (-1); | ||
| 258 | } | ||
| 259 | break; | ||
| 260 | } | ||
| 261 | if (cp > eom) | ||
| 262 | return -1; | ||
| 263 | return (cp - comp_dn); | ||
| 264 | } | ||
| 265 | |||
| 266 | /* | ||
| 267 | * Search for expanded name from a list of previously compressed names. | ||
| 268 | * Return the offset from msg if found or -1. | ||
| 269 | * dnptrs is the pointer to the first name on the list, | ||
| 270 | * not the pointer to the start of the message. | ||
| 271 | */ | ||
| 272 | static int | ||
| 273 | dn_find(exp_dn, msg, dnptrs, lastdnptr) | ||
| 274 | u_char *exp_dn, *msg; | ||
| 275 | u_char **dnptrs, **lastdnptr; | ||
| 276 | { | ||
| 277 | register u_char *dn, *cp, **cpp; | ||
| 278 | register int n; | ||
| 279 | u_char *sp; | ||
| 280 | |||
| 281 | for (cpp = dnptrs; cpp < lastdnptr; cpp++) { | ||
| 282 | dn = exp_dn; | ||
| 283 | sp = cp = *cpp; | ||
| 284 | while (n = *cp++) { | ||
| 285 | /* | ||
| 286 | * check for indirection | ||
| 287 | */ | ||
| 288 | switch (n & INDIR_MASK) { | ||
| 289 | case 0: /* normal case, n == len */ | ||
| 290 | while (--n >= 0) { | ||
| 291 | if (*dn == '.') | ||
| 292 | goto next; | ||
| 293 | if (*dn == '\\') | ||
| 294 | dn++; | ||
| 295 | if (*dn++ != *cp++) | ||
| 296 | goto next; | ||
| 297 | } | ||
| 298 | if ((n = *dn++) == '\0' && *cp == '\0') | ||
| 299 | return (sp - msg); | ||
| 300 | if (n == '.') | ||
| 301 | continue; | ||
| 302 | goto next; | ||
| 303 | |||
| 304 | default: /* illegal type */ | ||
| 305 | return (-1); | ||
| 306 | |||
| 307 | case INDIR_MASK: /* indirection */ | ||
| 308 | cp = msg + (((n & 0x3f) << 8) | *cp); | ||
| 309 | } | ||
| 310 | } | ||
| 311 | if (*dn == '\0') | ||
| 312 | return (sp - msg); | ||
| 313 | next: ; | ||
| 314 | } | ||
| 315 | return (-1); | ||
| 316 | } | ||
| 317 | |||
| 318 | /* | ||
| 319 | * Routines to insert/extract short/long's. Must account for byte | ||
| 320 | * order and non-alignment problems. This code at least has the | ||
| 321 | * advantage of being portable. | ||
| 322 | * | ||
| 323 | * used by sendmail. | ||
| 324 | */ | ||
| 325 | |||
| 326 | u_short | ||
| 327 | _getshort(msgp) | ||
| 328 | register u_char *msgp; | ||
| 329 | { | ||
| 330 | register u_int16_t u; | ||
| 331 | |||
| 332 | GETSHORT(u, msgp); | ||
| 333 | return (u); | ||
| 334 | } | ||
| 335 | |||
| 336 | u_int32_t | ||
| 337 | _getlong(msgp) | ||
| 338 | register u_char *msgp; | ||
| 339 | { | ||
| 340 | register u_int32_t u; | ||
| 341 | |||
| 342 | GETLONG(u, msgp); | ||
| 343 | return (u); | ||
| 344 | } | ||
| 345 | |||
| 346 | void | ||
| 347 | #if defined(__STDC__) || defined(__cplusplus) | ||
| 348 | __putshort(register u_int16_t s, register u_char *msgp) | ||
| 349 | #else | ||
| 350 | __putshort(s, msgp) | ||
| 351 | register u_int16_t s; | ||
| 352 | register u_char *msgp; | ||
| 353 | #endif | ||
| 354 | { | ||
| 355 | PUTSHORT(s, msgp); | ||
| 356 | } | ||
| 357 | |||
| 358 | void | ||
| 359 | __putlong(l, msgp) | ||
| 360 | register u_int32_t l; | ||
| 361 | register u_char *msgp; | ||
| 362 | { | ||
| 363 | PUTLONG(l, msgp); | ||
| 364 | } | ||
diff --git a/src/lib/libc/net/res_debug.c b/src/lib/libc/net/res_debug.c new file mode 100644 index 0000000000..d841293f18 --- /dev/null +++ b/src/lib/libc/net/res_debug.c | |||
| @@ -0,0 +1,749 @@ | |||
| 1 | /* $NetBSD: res_debug.c,v 1.7 1995/02/25 06:20:56 cgd Exp $ */ | ||
| 2 | |||
| 3 | /*- | ||
| 4 | * Copyright (c) 1985, 1990, 1993 | ||
| 5 | * The Regents of the University of California. All rights reserved. | ||
| 6 | * | ||
| 7 | * Redistribution and use in source and binary forms, with or without | ||
| 8 | * modification, are permitted provided that the following conditions | ||
| 9 | * are met: | ||
| 10 | * 1. Redistributions of source code must retain the above copyright | ||
| 11 | * notice, this list of conditions and the following disclaimer. | ||
| 12 | * 2. Redistributions in binary form must reproduce the above copyright | ||
| 13 | * notice, this list of conditions and the following disclaimer in the | ||
| 14 | * documentation and/or other materials provided with the distribution. | ||
| 15 | * 3. All advertising materials mentioning features or use of this software | ||
| 16 | * must display the following acknowledgement: | ||
| 17 | * This product includes software developed by the University of | ||
| 18 | * California, Berkeley and its contributors. | ||
| 19 | * 4. Neither the name of the University nor the names of its contributors | ||
| 20 | * may be used to endorse or promote products derived from this software | ||
| 21 | * without specific prior written permission. | ||
| 22 | * | ||
| 23 | * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND | ||
| 24 | * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE | ||
| 25 | * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE | ||
| 26 | * ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE | ||
| 27 | * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL | ||
| 28 | * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS | ||
| 29 | * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) | ||
| 30 | * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT | ||
| 31 | * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY | ||
| 32 | * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF | ||
| 33 | * SUCH DAMAGE. | ||
| 34 | * - | ||
| 35 | * Portions Copyright (c) 1993 by Digital Equipment Corporation. | ||
| 36 | * | ||
| 37 | * Permission to use, copy, modify, and distribute this software for any | ||
| 38 | * purpose with or without fee is hereby granted, provided that the above | ||
| 39 | * copyright notice and this permission notice appear in all copies, and that | ||
| 40 | * the name of Digital Equipment Corporation not be used in advertising or | ||
| 41 | * publicity pertaining to distribution of the document or software without | ||
| 42 | * specific, written prior permission. | ||
| 43 | * | ||
| 44 | * THE SOFTWARE IS PROVIDED "AS IS" AND DIGITAL EQUIPMENT CORP. DISCLAIMS ALL | ||
| 45 | * WARRANTIES WITH REGARD TO THIS SOFTWARE, INCLUDING ALL IMPLIED WARRANTIES | ||
| 46 | * OF MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL DIGITAL EQUIPMENT | ||
| 47 | * CORPORATION BE LIABLE FOR ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL | ||
| 48 | * DAMAGES OR ANY DAMAGES WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR | ||
| 49 | * PROFITS, WHETHER IN AN ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS | ||
| 50 | * ACTION, ARISING OUT OF OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS | ||
| 51 | * SOFTWARE. | ||
| 52 | * - | ||
| 53 | * --Copyright-- | ||
| 54 | */ | ||
| 55 | |||
| 56 | #if defined(LIBC_SCCS) && !defined(lint) | ||
| 57 | #if 0 | ||
| 58 | static char sccsid[] = "@(#)res_debug.c 8.1 (Berkeley) 6/4/93"; | ||
| 59 | #else | ||
| 60 | static char rcsid[] = "$NetBSD: res_debug.c,v 1.7 1995/02/25 06:20:56 cgd Exp $"; | ||
| 61 | #endif | ||
| 62 | #endif /* LIBC_SCCS and not lint */ | ||
| 63 | |||
| 64 | #include <sys/param.h> | ||
| 65 | #include <netinet/in.h> | ||
| 66 | #include <arpa/inet.h> | ||
| 67 | #include <arpa/nameser.h> | ||
| 68 | #include <resolv.h> | ||
| 69 | #include <stdio.h> | ||
| 70 | #include <string.h> | ||
| 71 | |||
| 72 | void __fp_query(); | ||
| 73 | char *__p_class(), *__p_time(), *__p_type(); | ||
| 74 | char *p_cdname(), *p_fqname(), *p_rr(); | ||
| 75 | static char *p_option __P((u_int32_t)); | ||
| 76 | |||
| 77 | char *_res_opcodes[] = { | ||
| 78 | "QUERY", | ||
| 79 | "IQUERY", | ||
| 80 | "CQUERYM", | ||
| 81 | "CQUERYU", | ||
| 82 | "4", | ||
| 83 | "5", | ||
| 84 | "6", | ||
| 85 | "7", | ||
| 86 | "8", | ||
| 87 | "UPDATEA", | ||
| 88 | "UPDATED", | ||
| 89 | "UPDATEDA", | ||
| 90 | "UPDATEM", | ||
| 91 | "UPDATEMA", | ||
| 92 | "ZONEINIT", | ||
| 93 | "ZONEREF", | ||
| 94 | }; | ||
| 95 | |||
| 96 | char *_res_resultcodes[] = { | ||
| 97 | "NOERROR", | ||
| 98 | "FORMERR", | ||
| 99 | "SERVFAIL", | ||
| 100 | "NXDOMAIN", | ||
| 101 | "NOTIMP", | ||
| 102 | "REFUSED", | ||
| 103 | "6", | ||
| 104 | "7", | ||
| 105 | "8", | ||
| 106 | "9", | ||
| 107 | "10", | ||
| 108 | "11", | ||
| 109 | "12", | ||
| 110 | "13", | ||
| 111 | "14", | ||
| 112 | "NOCHANGE", | ||
| 113 | }; | ||
| 114 | |||
| 115 | static char retbuf[16]; | ||
| 116 | |||
| 117 | static char * | ||
| 118 | dewks(wks) | ||
| 119 | int wks; | ||
| 120 | { | ||
| 121 | switch (wks) { | ||
| 122 | case 5: return("rje"); | ||
| 123 | case 7: return("echo"); | ||
| 124 | case 9: return("discard"); | ||
| 125 | case 11: return("systat"); | ||
| 126 | case 13: return("daytime"); | ||
| 127 | case 15: return("netstat"); | ||
| 128 | case 17: return("qotd"); | ||
| 129 | case 19: return("chargen"); | ||
| 130 | case 20: return("ftp-data"); | ||
| 131 | case 21: return("ftp"); | ||
| 132 | case 23: return("telnet"); | ||
| 133 | case 25: return("smtp"); | ||
| 134 | case 37: return("time"); | ||
| 135 | case 39: return("rlp"); | ||
| 136 | case 42: return("name"); | ||
| 137 | case 43: return("whois"); | ||
| 138 | case 53: return("domain"); | ||
| 139 | case 57: return("apts"); | ||
| 140 | case 59: return("apfs"); | ||
| 141 | case 67: return("bootps"); | ||
| 142 | case 68: return("bootpc"); | ||
| 143 | case 69: return("tftp"); | ||
| 144 | case 77: return("rje"); | ||
| 145 | case 79: return("finger"); | ||
| 146 | case 87: return("link"); | ||
| 147 | case 95: return("supdup"); | ||
| 148 | case 100: return("newacct"); | ||
| 149 | case 101: return("hostnames"); | ||
| 150 | case 102: return("iso-tsap"); | ||
| 151 | case 103: return("x400"); | ||
| 152 | case 104: return("x400-snd"); | ||
| 153 | case 105: return("csnet-ns"); | ||
| 154 | case 109: return("pop-2"); | ||
| 155 | case 111: return("sunrpc"); | ||
| 156 | case 113: return("auth"); | ||
| 157 | case 115: return("sftp"); | ||
| 158 | case 117: return("uucp-path"); | ||
| 159 | case 119: return("nntp"); | ||
| 160 | case 121: return("erpc"); | ||
| 161 | case 123: return("ntp"); | ||
| 162 | case 133: return("statsrv"); | ||
| 163 | case 136: return("profile"); | ||
| 164 | case 144: return("NeWS"); | ||
| 165 | case 161: return("snmp"); | ||
| 166 | case 162: return("snmp-trap"); | ||
| 167 | case 170: return("print-srv"); | ||
| 168 | default: (void) sprintf(retbuf, "%d", wks); return(retbuf); | ||
| 169 | } | ||
| 170 | } | ||
| 171 | |||
| 172 | static char * | ||
| 173 | deproto(protonum) | ||
| 174 | int protonum; | ||
| 175 | { | ||
| 176 | switch (protonum) { | ||
| 177 | case 1: return("icmp"); | ||
| 178 | case 2: return("igmp"); | ||
| 179 | case 3: return("ggp"); | ||
| 180 | case 5: return("st"); | ||
| 181 | case 6: return("tcp"); | ||
| 182 | case 7: return("ucl"); | ||
| 183 | case 8: return("egp"); | ||
| 184 | case 9: return("igp"); | ||
| 185 | case 11: return("nvp-II"); | ||
| 186 | case 12: return("pup"); | ||
| 187 | case 16: return("chaos"); | ||
| 188 | case 17: return("udp"); | ||
| 189 | default: (void) sprintf(retbuf, "%d", protonum); return(retbuf); | ||
| 190 | } | ||
| 191 | } | ||
| 192 | |||
| 193 | static char * | ||
| 194 | do_rrset(msg, cp, cnt, pflag, file, hs) | ||
| 195 | int cnt, pflag; | ||
| 196 | char *cp,*msg, *hs; | ||
| 197 | FILE *file; | ||
| 198 | { | ||
| 199 | int n; | ||
| 200 | int sflag; | ||
| 201 | /* | ||
| 202 | * Print answer records | ||
| 203 | */ | ||
| 204 | sflag = (_res.pfcode & pflag); | ||
| 205 | if (n = ntohs(cnt)) { | ||
| 206 | if ((!_res.pfcode) || ((sflag) && (_res.pfcode & RES_PRF_HEAD1))) | ||
| 207 | fprintf(file, hs); | ||
| 208 | while (--n >= 0) { | ||
| 209 | cp = p_rr(cp, msg, file); | ||
| 210 | if ((cp-msg) > PACKETSZ) | ||
| 211 | return (NULL); | ||
| 212 | } | ||
| 213 | if ((!_res.pfcode) || ((sflag) && (_res.pfcode & RES_PRF_HEAD1))) | ||
| 214 | putc('\n', file); | ||
| 215 | } | ||
| 216 | return(cp); | ||
| 217 | } | ||
| 218 | |||
| 219 | __p_query(msg) | ||
| 220 | char *msg; | ||
| 221 | { | ||
| 222 | __fp_query(msg, stdout); | ||
| 223 | } | ||
| 224 | |||
| 225 | /* | ||
| 226 | * Print the current options. | ||
| 227 | * This is intended to be primarily a debugging routine. | ||
| 228 | */ | ||
| 229 | void | ||
| 230 | __fp_resstat(statp, file) | ||
| 231 | struct __res_state *statp; | ||
| 232 | FILE *file; | ||
| 233 | { | ||
| 234 | int bit; | ||
| 235 | |||
| 236 | fprintf(file, ";; res options:"); | ||
| 237 | if (!statp) | ||
| 238 | statp = &_res; | ||
| 239 | for (bit = 0; bit < 32; bit++) { /* XXX 32 - bad assumption! */ | ||
| 240 | if (statp->options & (1<<bit)) | ||
| 241 | fprintf(file, " %s", p_option(1<<bit)); | ||
| 242 | } | ||
| 243 | putc('\n', file); | ||
| 244 | } | ||
| 245 | |||
| 246 | /* | ||
| 247 | * Print the contents of a query. | ||
| 248 | * This is intended to be primarily a debugging routine. | ||
| 249 | */ | ||
| 250 | void | ||
| 251 | __fp_query(msg,file) | ||
| 252 | char *msg; | ||
| 253 | FILE *file; | ||
| 254 | { | ||
| 255 | register char *cp; | ||
| 256 | register HEADER *hp; | ||
| 257 | register int n; | ||
| 258 | |||
| 259 | /* | ||
| 260 | * Print header fields. | ||
| 261 | */ | ||
| 262 | hp = (HEADER *)msg; | ||
| 263 | cp = msg + sizeof(HEADER); | ||
| 264 | if ((!_res.pfcode) || (_res.pfcode & RES_PRF_HEADX) || hp->rcode) { | ||
| 265 | fprintf(file,";; ->>HEADER<<- opcode: %s, status: %s, id: %d", | ||
| 266 | _res_opcodes[hp->opcode], | ||
| 267 | _res_resultcodes[hp->rcode], | ||
| 268 | ntohs(hp->id)); | ||
| 269 | putc('\n', file); | ||
| 270 | } | ||
| 271 | putc(';', file); | ||
| 272 | if ((!_res.pfcode) || (_res.pfcode & RES_PRF_HEAD2)) { | ||
| 273 | fprintf(file,"; flags:"); | ||
| 274 | if (hp->qr) | ||
| 275 | fprintf(file," qr"); | ||
| 276 | if (hp->aa) | ||
| 277 | fprintf(file," aa"); | ||
| 278 | if (hp->tc) | ||
| 279 | fprintf(file," tc"); | ||
| 280 | if (hp->rd) | ||
| 281 | fprintf(file," rd"); | ||
| 282 | if (hp->ra) | ||
| 283 | fprintf(file," ra"); | ||
| 284 | if (hp->pr) | ||
| 285 | fprintf(file," pr"); | ||
| 286 | } | ||
| 287 | if ((!_res.pfcode) || (_res.pfcode & RES_PRF_HEAD1)) { | ||
| 288 | fprintf(file,"; Ques: %d", ntohs(hp->qdcount)); | ||
| 289 | fprintf(file,", Ans: %d", ntohs(hp->ancount)); | ||
| 290 | fprintf(file,", Auth: %d", ntohs(hp->nscount)); | ||
| 291 | fprintf(file,", Addit: %d\n", ntohs(hp->arcount)); | ||
| 292 | } | ||
| 293 | #if 0 | ||
| 294 | if (_res.pfcode & (RES_PRF_HEADX | RES_PRF_HEAD2 | RES_PRF_HEAD1)) { | ||
| 295 | putc('\n',file); | ||
| 296 | } | ||
| 297 | #endif | ||
| 298 | /* | ||
| 299 | * Print question records. | ||
| 300 | */ | ||
| 301 | if (n = ntohs(hp->qdcount)) { | ||
| 302 | if ((!_res.pfcode) || (_res.pfcode & RES_PRF_QUES)) | ||
| 303 | fprintf(file,";; QUESTIONS:\n"); | ||
| 304 | while (--n >= 0) { | ||
| 305 | fprintf(file,";;\t"); | ||
| 306 | cp = p_cdname(cp, msg, file); | ||
| 307 | if (cp == NULL) | ||
| 308 | return; | ||
| 309 | if ((!_res.pfcode) || (_res.pfcode & RES_PRF_QUES)) | ||
| 310 | fprintf(file, ", type = %s", | ||
| 311 | __p_type(_getshort(cp))); | ||
| 312 | cp += sizeof(u_int16_t); | ||
| 313 | if ((!_res.pfcode) || (_res.pfcode & RES_PRF_QUES)) | ||
| 314 | fprintf(file, ", class = %s\n\n", | ||
| 315 | __p_class(_getshort(cp))); | ||
| 316 | cp += sizeof(u_int16_t); | ||
| 317 | } | ||
| 318 | } | ||
| 319 | /* | ||
| 320 | * Print authoritative answer records | ||
| 321 | */ | ||
| 322 | cp = do_rrset(msg, cp, hp->ancount, RES_PRF_ANS, file, | ||
| 323 | ";; ANSWERS:\n"); | ||
| 324 | if (cp == NULL) | ||
| 325 | return; | ||
| 326 | |||
| 327 | /* | ||
| 328 | * print name server records | ||
| 329 | */ | ||
| 330 | cp = do_rrset(msg, cp, hp->nscount, RES_PRF_AUTH, file, | ||
| 331 | ";; AUTHORITY RECORDS:\n"); | ||
| 332 | if (!cp) | ||
| 333 | return; | ||
| 334 | |||
| 335 | /* | ||
| 336 | * print additional records | ||
| 337 | */ | ||
| 338 | cp = do_rrset(msg, cp, hp->arcount, RES_PRF_ADD, file, | ||
| 339 | ";; ADDITIONAL RECORDS:\n"); | ||
| 340 | if (!cp) | ||
| 341 | return; | ||
| 342 | } | ||
| 343 | |||
| 344 | char * | ||
| 345 | p_cdname(cp, msg, file) | ||
| 346 | char *cp, *msg; | ||
| 347 | FILE *file; | ||
| 348 | { | ||
| 349 | char name[MAXDNAME]; | ||
| 350 | int n; | ||
| 351 | |||
| 352 | if ((n = dn_expand((u_char *)msg, (u_char *)cp + MAXCDNAME, | ||
| 353 | (u_char *)cp, (u_char *)name, sizeof(name))) < 0) | ||
| 354 | return (NULL); | ||
| 355 | if (name[0] == '\0') | ||
| 356 | putc('.', file); | ||
| 357 | else | ||
| 358 | fputs(name, file); | ||
| 359 | return (cp + n); | ||
| 360 | } | ||
| 361 | |||
| 362 | char * | ||
| 363 | p_fqname(cp, msg, file) | ||
| 364 | char *cp, *msg; | ||
| 365 | FILE *file; | ||
| 366 | { | ||
| 367 | char name[MAXDNAME]; | ||
| 368 | int n, len; | ||
| 369 | |||
| 370 | if ((n = dn_expand((u_char *)msg, (u_char *)cp + MAXCDNAME, | ||
| 371 | (u_char *)cp, (u_char *)name, sizeof(name))) < 0) | ||
| 372 | return (NULL); | ||
| 373 | if (name[0] == '\0') { | ||
| 374 | putc('.', file); | ||
| 375 | } else { | ||
| 376 | fputs(name, file); | ||
| 377 | if (name[strlen(name) - 1] != '.') | ||
| 378 | putc('.', file); | ||
| 379 | } | ||
| 380 | return (cp + n); | ||
| 381 | } | ||
| 382 | |||
| 383 | /* | ||
| 384 | * Print resource record fields in human readable form. | ||
| 385 | */ | ||
| 386 | char * | ||
| 387 | p_rr(cp, msg, file) | ||
| 388 | char *cp, *msg; | ||
| 389 | FILE *file; | ||
| 390 | { | ||
| 391 | int type, class, dlen, n, c; | ||
| 392 | struct in_addr inaddr; | ||
| 393 | char *cp1, *cp2; | ||
| 394 | u_int32_t tmpttl, t; | ||
| 395 | int lcnt; | ||
| 396 | |||
| 397 | if ((cp = p_fqname(cp, msg, file)) == NULL) | ||
| 398 | return (NULL); /* compression error */ | ||
| 399 | type = _getshort(cp); | ||
| 400 | cp += sizeof(u_int16_t); | ||
| 401 | class = _getshort(cp); | ||
| 402 | cp += sizeof(u_int16_t); | ||
| 403 | tmpttl = _getlong(cp); | ||
| 404 | cp += sizeof(u_int32_t); | ||
| 405 | dlen = _getshort(cp); | ||
| 406 | cp += sizeof(u_int16_t); | ||
| 407 | cp1 = cp; | ||
| 408 | if ((!_res.pfcode) || (_res.pfcode & RES_PRF_TTLID)) | ||
| 409 | fprintf(file, "\t%lu", tmpttl); | ||
| 410 | if ((!_res.pfcode) || (_res.pfcode & RES_PRF_CLASS)) | ||
| 411 | fprintf(file, "\t%s", __p_class(class)); | ||
| 412 | fprintf(file, "\t%s", __p_type(type)); | ||
| 413 | /* | ||
| 414 | * Print type specific data, if appropriate | ||
| 415 | */ | ||
| 416 | switch (type) { | ||
| 417 | case T_A: | ||
| 418 | switch (class) { | ||
| 419 | case C_IN: | ||
| 420 | case C_HS: | ||
| 421 | bcopy(cp, (char *)&inaddr, sizeof(inaddr)); | ||
| 422 | if (dlen == 4) { | ||
| 423 | fprintf(file,"\t%s", inet_ntoa(inaddr)); | ||
| 424 | cp += dlen; | ||
| 425 | } else if (dlen == 7) { | ||
| 426 | char *address; | ||
| 427 | u_char protocol; | ||
| 428 | u_short port; | ||
| 429 | |||
| 430 | address = inet_ntoa(inaddr); | ||
| 431 | cp += sizeof(inaddr); | ||
| 432 | protocol = *(u_char*)cp; | ||
| 433 | cp += sizeof(u_char); | ||
| 434 | port = _getshort(cp); | ||
| 435 | cp += sizeof(u_int16_t); | ||
| 436 | fprintf(file, "\t%s\t; proto %d, port %d", | ||
| 437 | address, protocol, port); | ||
| 438 | } | ||
| 439 | break; | ||
| 440 | default: | ||
| 441 | cp += dlen; | ||
| 442 | } | ||
| 443 | break; | ||
| 444 | case T_CNAME: | ||
| 445 | case T_MB: | ||
| 446 | case T_MG: | ||
| 447 | case T_MR: | ||
| 448 | case T_NS: | ||
| 449 | case T_PTR: | ||
| 450 | putc('\t', file); | ||
| 451 | cp = p_fqname(cp, msg, file); | ||
| 452 | break; | ||
| 453 | |||
| 454 | case T_HINFO: | ||
| 455 | if (n = *cp++) { | ||
| 456 | fprintf(file,"\t%.*s", n, cp); | ||
| 457 | cp += n; | ||
| 458 | } | ||
| 459 | if (n = *cp++) { | ||
| 460 | fprintf(file,"\t%.*s", n, cp); | ||
| 461 | cp += n; | ||
| 462 | } | ||
| 463 | break; | ||
| 464 | |||
| 465 | case T_SOA: | ||
| 466 | putc('\t', file); | ||
| 467 | cp = p_fqname(cp, msg, file); /* origin */ | ||
| 468 | putc(' ', file); | ||
| 469 | cp = p_fqname(cp, msg, file); /* mail addr */ | ||
| 470 | fputs(" (\n", file); | ||
| 471 | t = _getlong(cp); cp += sizeof(u_int32_t); | ||
| 472 | fprintf(file,"\t\t\t%lu\t; serial\n", t); | ||
| 473 | t = _getlong(cp); cp += sizeof(u_int32_t); | ||
| 474 | fprintf(file,"\t\t\t%lu\t; refresh (%s)\n", t, __p_time(t)); | ||
| 475 | t = _getlong(cp); cp += sizeof(u_int32_t); | ||
| 476 | fprintf(file,"\t\t\t%lu\t; retry (%s)\n", t, __p_time(t)); | ||
| 477 | t = _getlong(cp); cp += sizeof(u_int32_t); | ||
| 478 | fprintf(file,"\t\t\t%lu\t; expire (%s)\n", t, __p_time(t)); | ||
| 479 | t = _getlong(cp); cp += sizeof(u_int32_t); | ||
| 480 | fprintf(file,"\t\t\t%lu )\t; minimum (%s)", t, __p_time(t)); | ||
| 481 | break; | ||
| 482 | |||
| 483 | case T_MX: | ||
| 484 | case T_AFSDB: | ||
| 485 | fprintf(file,"\t%d ", _getshort(cp)); | ||
| 486 | cp += sizeof(u_int16_t); | ||
| 487 | cp = p_fqname(cp, msg, file); | ||
| 488 | break; | ||
| 489 | |||
| 490 | case T_TXT: | ||
| 491 | (void) fputs("\t\"", file); | ||
| 492 | cp2 = cp1 + dlen; | ||
| 493 | while (cp < cp2) { | ||
| 494 | if (n = (unsigned char) *cp++) { | ||
| 495 | for (c = n; c > 0 && cp < cp2; c--) | ||
| 496 | if (*cp == '\n') { | ||
| 497 | (void) putc('\\', file); | ||
| 498 | (void) putc(*cp++, file); | ||
| 499 | } else | ||
| 500 | (void) putc(*cp++, file); | ||
| 501 | } | ||
| 502 | } | ||
| 503 | putc('"', file); | ||
| 504 | break; | ||
| 505 | |||
| 506 | case T_MINFO: | ||
| 507 | case T_RP: | ||
| 508 | putc('\t', file); | ||
| 509 | cp = p_fqname(cp, msg, file); | ||
| 510 | putc(' ', file); | ||
| 511 | cp = p_fqname(cp, msg, file); | ||
| 512 | break; | ||
| 513 | |||
| 514 | case T_UINFO: | ||
| 515 | putc('\t', file); | ||
| 516 | fputs(cp, file); | ||
| 517 | cp += dlen; | ||
| 518 | break; | ||
| 519 | |||
| 520 | case T_UID: | ||
| 521 | case T_GID: | ||
| 522 | if (dlen == 4) { | ||
| 523 | fprintf(file,"\t%u", _getlong(cp)); | ||
| 524 | cp += sizeof(int32_t); | ||
| 525 | } | ||
| 526 | break; | ||
| 527 | |||
| 528 | case T_WKS: | ||
| 529 | if (dlen < sizeof(u_int32_t) + 1) | ||
| 530 | break; | ||
| 531 | bcopy(cp, (char *)&inaddr, sizeof(inaddr)); | ||
| 532 | cp += sizeof(u_int32_t); | ||
| 533 | fprintf(file, "\t%s %s ( ", | ||
| 534 | inet_ntoa(inaddr), | ||
| 535 | deproto((int) *cp)); | ||
| 536 | cp += sizeof(u_char); | ||
| 537 | n = 0; | ||
| 538 | lcnt = 0; | ||
| 539 | while (cp < cp1 + dlen) { | ||
| 540 | c = *cp++; | ||
| 541 | do { | ||
| 542 | if (c & 0200) { | ||
| 543 | if (lcnt == 0) { | ||
| 544 | fputs("\n\t\t\t", file); | ||
| 545 | lcnt = 5; | ||
| 546 | } | ||
| 547 | fputs(dewks(n), file); | ||
| 548 | putc(' ', file); | ||
| 549 | lcnt--; | ||
| 550 | } | ||
| 551 | c <<= 1; | ||
| 552 | } while (++n & 07); | ||
| 553 | } | ||
| 554 | putc(')', file); | ||
| 555 | break; | ||
| 556 | |||
| 557 | #ifdef ALLOW_T_UNSPEC | ||
| 558 | case T_UNSPEC: | ||
| 559 | { | ||
| 560 | int NumBytes = 8; | ||
| 561 | char *DataPtr; | ||
| 562 | int i; | ||
| 563 | |||
| 564 | if (dlen < NumBytes) NumBytes = dlen; | ||
| 565 | fprintf(file, "\tFirst %d bytes of hex data:", | ||
| 566 | NumBytes); | ||
| 567 | for (i = 0, DataPtr = cp; i < NumBytes; i++, DataPtr++) | ||
| 568 | fprintf(file, " %x", *DataPtr); | ||
| 569 | cp += dlen; | ||
| 570 | } | ||
| 571 | break; | ||
| 572 | #endif /* ALLOW_T_UNSPEC */ | ||
| 573 | |||
| 574 | default: | ||
| 575 | fprintf(file,"\t?%d?", type); | ||
| 576 | cp += dlen; | ||
| 577 | } | ||
| 578 | #if 0 | ||
| 579 | fprintf(file, "\t; dlen=%d, ttl %s\n", dlen, __p_time(tmpttl)); | ||
| 580 | #else | ||
| 581 | putc('\n', file); | ||
| 582 | #endif | ||
| 583 | if (cp - cp1 != dlen) { | ||
| 584 | fprintf(file,";; packet size error (found %d, dlen was %d)\n", | ||
| 585 | cp - cp1, dlen); | ||
| 586 | cp = NULL; | ||
| 587 | } | ||
| 588 | return (cp); | ||
| 589 | } | ||
| 590 | |||
| 591 | static char nbuf[40]; | ||
| 592 | |||
| 593 | /* | ||
| 594 | * Return a string for the type | ||
| 595 | */ | ||
| 596 | char * | ||
| 597 | __p_type(type) | ||
| 598 | int type; | ||
| 599 | { | ||
| 600 | switch (type) { | ||
| 601 | case T_A: | ||
| 602 | return("A"); | ||
| 603 | case T_NS: /* authoritative server */ | ||
| 604 | return("NS"); | ||
| 605 | case T_CNAME: /* canonical name */ | ||
| 606 | return("CNAME"); | ||
| 607 | case T_SOA: /* start of authority zone */ | ||
| 608 | return("SOA"); | ||
| 609 | case T_MB: /* mailbox domain name */ | ||
| 610 | return("MB"); | ||
| 611 | case T_MG: /* mail group member */ | ||
| 612 | return("MG"); | ||
| 613 | case T_MR: /* mail rename name */ | ||
| 614 | return("MR"); | ||
| 615 | case T_NULL: /* null resource record */ | ||
| 616 | return("NULL"); | ||
| 617 | case T_WKS: /* well known service */ | ||
| 618 | return("WKS"); | ||
| 619 | case T_PTR: /* domain name pointer */ | ||
| 620 | return("PTR"); | ||
| 621 | case T_HINFO: /* host information */ | ||
| 622 | return("HINFO"); | ||
| 623 | case T_MINFO: /* mailbox information */ | ||
| 624 | return("MINFO"); | ||
| 625 | case T_MX: /* mail routing info */ | ||
| 626 | return("MX"); | ||
| 627 | case T_TXT: /* text */ | ||
| 628 | return("TXT"); | ||
| 629 | case T_RP: /* responsible person */ | ||
| 630 | return("RP"); | ||
| 631 | case T_AFSDB: /* AFS cell database */ | ||
| 632 | return("AFSDB"); | ||
| 633 | case T_AXFR: /* zone transfer */ | ||
| 634 | return("AXFR"); | ||
| 635 | case T_MAILB: /* mail box */ | ||
| 636 | return("MAILB"); | ||
| 637 | case T_MAILA: /* mail address */ | ||
| 638 | return("MAILA"); | ||
| 639 | case T_ANY: /* matches any type */ | ||
| 640 | return("ANY"); | ||
| 641 | case T_UINFO: | ||
| 642 | return("UINFO"); | ||
| 643 | case T_UID: | ||
| 644 | return("UID"); | ||
| 645 | case T_GID: | ||
| 646 | return("GID"); | ||
| 647 | #ifdef ALLOW_T_UNSPEC | ||
| 648 | case T_UNSPEC: | ||
| 649 | return("UNSPEC"); | ||
| 650 | #endif /* ALLOW_T_UNSPEC */ | ||
| 651 | |||
| 652 | default: | ||
| 653 | (void)sprintf(nbuf, "%d", type); | ||
| 654 | return(nbuf); | ||
| 655 | } | ||
| 656 | } | ||
| 657 | |||
| 658 | /* | ||
| 659 | * Return a mnemonic for class | ||
| 660 | */ | ||
| 661 | char * | ||
| 662 | __p_class(class) | ||
| 663 | int class; | ||
| 664 | { | ||
| 665 | |||
| 666 | switch (class) { | ||
| 667 | case C_IN: /* internet class */ | ||
| 668 | return("IN"); | ||
| 669 | case C_HS: /* hesiod class */ | ||
| 670 | return("HS"); | ||
| 671 | case C_ANY: /* matches any class */ | ||
| 672 | return("ANY"); | ||
| 673 | default: | ||
| 674 | (void)sprintf(nbuf, "%d", class); | ||
| 675 | return(nbuf); | ||
| 676 | } | ||
| 677 | } | ||
| 678 | |||
| 679 | /* | ||
| 680 | * Return a mnemonic for an option | ||
| 681 | */ | ||
| 682 | static char * | ||
| 683 | p_option(option) | ||
| 684 | u_int32_t option; | ||
| 685 | { | ||
| 686 | switch (option) { | ||
| 687 | case RES_INIT: return "init"; | ||
| 688 | case RES_DEBUG: return "debug"; | ||
| 689 | case RES_AAONLY: return "aaonly"; | ||
| 690 | case RES_USEVC: return "usevc"; | ||
| 691 | case RES_PRIMARY: return "primry"; | ||
| 692 | case RES_IGNTC: return "igntc"; | ||
| 693 | case RES_RECURSE: return "recurs"; | ||
| 694 | case RES_DEFNAMES: return "defnam"; | ||
| 695 | case RES_STAYOPEN: return "styopn"; | ||
| 696 | case RES_DNSRCH: return "dnsrch"; | ||
| 697 | default: sprintf(nbuf, "?0x%x?", option); return nbuf; | ||
| 698 | } | ||
| 699 | } | ||
| 700 | |||
| 701 | /* | ||
| 702 | * Return a mnemonic for a time to live | ||
| 703 | */ | ||
| 704 | char * | ||
| 705 | __p_time(value) | ||
| 706 | u_int32_t value; | ||
| 707 | { | ||
| 708 | int secs, mins, hours, days; | ||
| 709 | register char *p; | ||
| 710 | |||
| 711 | if (value == 0) { | ||
| 712 | strcpy(nbuf, "0 secs"); | ||
| 713 | return(nbuf); | ||
| 714 | } | ||
| 715 | |||
| 716 | secs = value % 60; | ||
| 717 | value /= 60; | ||
| 718 | mins = value % 60; | ||
| 719 | value /= 60; | ||
| 720 | hours = value % 24; | ||
| 721 | value /= 24; | ||
| 722 | days = value; | ||
| 723 | value = 0; | ||
| 724 | |||
| 725 | #define PLURALIZE(x) x, (x == 1) ? "" : "s" | ||
| 726 | p = nbuf; | ||
| 727 | if (days) { | ||
| 728 | (void)sprintf(p, "%d day%s", PLURALIZE(days)); | ||
| 729 | while (*++p); | ||
| 730 | } | ||
| 731 | if (hours) { | ||
| 732 | if (days) | ||
| 733 | *p++ = ' '; | ||
| 734 | (void)sprintf(p, "%d hour%s", PLURALIZE(hours)); | ||
| 735 | while (*++p); | ||
| 736 | } | ||
| 737 | if (mins) { | ||
| 738 | if (days || hours) | ||
| 739 | *p++ = ' '; | ||
| 740 | (void)sprintf(p, "%d min%s", PLURALIZE(mins)); | ||
| 741 | while (*++p); | ||
| 742 | } | ||
| 743 | if (secs || ! (days || hours || mins)) { | ||
| 744 | if (days || hours || mins) | ||
| 745 | *p++ = ' '; | ||
| 746 | (void)sprintf(p, "%d sec%s", PLURALIZE(secs)); | ||
| 747 | } | ||
| 748 | return(nbuf); | ||
| 749 | } | ||
diff --git a/src/lib/libc/net/res_init.c b/src/lib/libc/net/res_init.c new file mode 100644 index 0000000000..33cc8d39f1 --- /dev/null +++ b/src/lib/libc/net/res_init.c | |||
| @@ -0,0 +1,394 @@ | |||
| 1 | /* $NetBSD: res_init.c,v 1.8 1995/06/03 22:33:36 mycroft Exp $ */ | ||
| 2 | |||
| 3 | /*- | ||
| 4 | * Copyright (c) 1985, 1989, 1993 | ||
| 5 | * The Regents of the University of California. All rights reserved. | ||
| 6 | * | ||
| 7 | * Redistribution and use in source and binary forms, with or without | ||
| 8 | * modification, are permitted provided that the following conditions | ||
| 9 | * are met: | ||
| 10 | * 1. Redistributions of source code must retain the above copyright | ||
| 11 | * notice, this list of conditions and the following disclaimer. | ||
| 12 | * 2. Redistributions in binary form must reproduce the above copyright | ||
| 13 | * notice, this list of conditions and the following disclaimer in the | ||
| 14 | * documentation and/or other materials provided with the distribution. | ||
| 15 | * 3. All advertising materials mentioning features or use of this software | ||
| 16 | * must display the following acknowledgement: | ||
| 17 | * This product includes software developed by the University of | ||
| 18 | * California, Berkeley and its contributors. | ||
| 19 | * 4. Neither the name of the University nor the names of its contributors | ||
| 20 | * may be used to endorse or promote products derived from this software | ||
| 21 | * without specific prior written permission. | ||
| 22 | * | ||
| 23 | * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND | ||
| 24 | * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE | ||
| 25 | * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE | ||
| 26 | * ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE | ||
| 27 | * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL | ||
| 28 | * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS | ||
| 29 | * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) | ||
| 30 | * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT | ||
| 31 | * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY | ||
| 32 | * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF | ||
| 33 | * SUCH DAMAGE. | ||
| 34 | * - | ||
| 35 | * Portions Copyright (c) 1993 by Digital Equipment Corporation. | ||
| 36 | * | ||
| 37 | * Permission to use, copy, modify, and distribute this software for any | ||
| 38 | * purpose with or without fee is hereby granted, provided that the above | ||
| 39 | * copyright notice and this permission notice appear in all copies, and that | ||
| 40 | * the name of Digital Equipment Corporation not be used in advertising or | ||
| 41 | * publicity pertaining to distribution of the document or software without | ||
| 42 | * specific, written prior permission. | ||
| 43 | * | ||
| 44 | * THE SOFTWARE IS PROVIDED "AS IS" AND DIGITAL EQUIPMENT CORP. DISCLAIMS ALL | ||
| 45 | * WARRANTIES WITH REGARD TO THIS SOFTWARE, INCLUDING ALL IMPLIED WARRANTIES | ||
| 46 | * OF MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL DIGITAL EQUIPMENT | ||
| 47 | * CORPORATION BE LIABLE FOR ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL | ||
| 48 | * DAMAGES OR ANY DAMAGES WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR | ||
| 49 | * PROFITS, WHETHER IN AN ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS | ||
| 50 | * ACTION, ARISING OUT OF OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS | ||
| 51 | * SOFTWARE. | ||
| 52 | * - | ||
| 53 | * --Copyright-- | ||
| 54 | */ | ||
| 55 | |||
| 56 | #if defined(LIBC_SCCS) && !defined(lint) | ||
| 57 | #if 0 | ||
| 58 | static char sccsid[] = "@(#)res_init.c 8.1 (Berkeley) 6/7/93"; | ||
| 59 | static char rcsid[] = "$Id: res_init.c,v 4.9.1.1 1993/05/02 22:43:03 vixie Rel "; | ||
| 60 | #else | ||
| 61 | static char rcsid[] = "$NetBSD: res_init.c,v 1.8 1995/06/03 22:33:36 mycroft Exp $"; | ||
| 62 | #endif | ||
| 63 | #endif /* LIBC_SCCS and not lint */ | ||
| 64 | |||
| 65 | #include <sys/param.h> | ||
| 66 | #include <sys/socket.h> | ||
| 67 | #include <netinet/in.h> | ||
| 68 | #include <arpa/inet.h> | ||
| 69 | #include <arpa/nameser.h> | ||
| 70 | #include <resolv.h> | ||
| 71 | #include <unistd.h> | ||
| 72 | #include <stdio.h> | ||
| 73 | #include <stdlib.h> | ||
| 74 | #include <string.h> | ||
| 75 | |||
| 76 | static void res_setoptions __P((char *, char *)); | ||
| 77 | static u_int32_t net_mask __P((struct in_addr)); | ||
| 78 | |||
| 79 | /* | ||
| 80 | * Resolver state default settings | ||
| 81 | */ | ||
| 82 | |||
| 83 | struct __res_state _res = { | ||
| 84 | RES_TIMEOUT, /* retransmition time interval */ | ||
| 85 | 4, /* number of times to retransmit */ | ||
| 86 | RES_DEFAULT, /* options flags */ | ||
| 87 | 1, /* number of name servers */ | ||
| 88 | }; | ||
| 89 | |||
| 90 | /* | ||
| 91 | * Set up default settings. If the configuration file exist, the values | ||
| 92 | * there will have precedence. Otherwise, the server address is set to | ||
| 93 | * INADDR_ANY and the default domain name comes from the gethostname(). | ||
| 94 | * | ||
| 95 | * The configuration file should only be used if you want to redefine your | ||
| 96 | * domain or run without a server on your machine. | ||
| 97 | * | ||
| 98 | * Return 0 if completes successfully, -1 on error | ||
| 99 | */ | ||
| 100 | res_init() | ||
| 101 | { | ||
| 102 | register FILE *fp; | ||
| 103 | register char *cp, **pp, *net; | ||
| 104 | register int n; | ||
| 105 | char buf[BUFSIZ], buf2[BUFSIZ]; | ||
| 106 | int nserv = 0; /* number of nameserver records read from file */ | ||
| 107 | int haveenv = 0; | ||
| 108 | int havesearch = 0; | ||
| 109 | int nsort = 0; | ||
| 110 | u_long mask; | ||
| 111 | |||
| 112 | _res.nsaddr.sin_len = sizeof(struct sockaddr_in); | ||
| 113 | _res.nsaddr.sin_family = AF_INET; | ||
| 114 | _res.nsaddr.sin_port = htons(NAMESERVER_PORT); | ||
| 115 | #ifdef USELOOPBACK | ||
| 116 | _res.nsaddr.sin_addr = inet_makeaddr(IN_LOOPBACKNET, 1); | ||
| 117 | #else | ||
| 118 | _res.nsaddr.sin_addr.s_addr = INADDR_ANY; | ||
| 119 | #endif | ||
| 120 | _res.nscount = 1; | ||
| 121 | _res.ndots = 1; | ||
| 122 | _res.pfcode = 0; | ||
| 123 | strncpy(_res.lookups, "f", sizeof _res.lookups); | ||
| 124 | |||
| 125 | /* Allow user to override the local domain definition */ | ||
| 126 | if ((cp = getenv("LOCALDOMAIN")) != NULL) { | ||
| 127 | (void)strncpy(_res.defdname, cp, sizeof(_res.defdname) - 1); | ||
| 128 | if ((cp = strpbrk(_res.defdname, " \t\n")) != NULL) | ||
| 129 | *cp = '\0'; | ||
| 130 | haveenv++; | ||
| 131 | |||
| 132 | /* | ||
| 133 | * Set search list to be blank-separated strings | ||
| 134 | * from rest of env value. Permits users of LOCALDOMAIN | ||
| 135 | * to still have a search list, and anyone to set the | ||
| 136 | * one that they want to use as an individual (even more | ||
| 137 | * important now that the rfc1535 stuff restricts searches) | ||
| 138 | */ | ||
| 139 | cp = _res.defdname; | ||
| 140 | pp = _res.dnsrch; | ||
| 141 | *pp++ = cp; | ||
| 142 | for (n = 0; *cp && pp < _res.dnsrch + MAXDNSRCH; cp++) { | ||
| 143 | if (*cp == '\n') /* silly backwards compat */ | ||
| 144 | break; | ||
| 145 | else if (*cp == ' ' || *cp == '\t') { | ||
| 146 | *cp = 0; | ||
| 147 | n = 1; | ||
| 148 | } else if (n) { | ||
| 149 | *pp++ = cp; | ||
| 150 | n = 0; | ||
| 151 | havesearch = 1; | ||
| 152 | } | ||
| 153 | } | ||
| 154 | /* null terminate last domain if there are excess */ | ||
| 155 | while (*cp != '\0' && *cp != ' ' && *cp != '\t' && *cp != '\n') | ||
| 156 | cp++; | ||
| 157 | *cp = '\0'; | ||
| 158 | *pp++ = 0; | ||
| 159 | } | ||
| 160 | |||
| 161 | if ((fp = fopen(_PATH_RESCONF, "r")) != NULL) { | ||
| 162 | strncpy(_res.lookups, "bf", sizeof _res.lookups); | ||
| 163 | |||
| 164 | /* read the config file */ | ||
| 165 | while (fgets(buf, sizeof(buf), fp) != NULL) { | ||
| 166 | /* skip comments */ | ||
| 167 | if ((*buf == ';') || (*buf == '#')) | ||
| 168 | continue; | ||
| 169 | /* read default domain name */ | ||
| 170 | if (!strncmp(buf, "domain", sizeof("domain") - 1)) { | ||
| 171 | if (haveenv) /* skip if have from environ */ | ||
| 172 | continue; | ||
| 173 | cp = buf + sizeof("domain") - 1; | ||
| 174 | while (*cp == ' ' || *cp == '\t') | ||
| 175 | cp++; | ||
| 176 | if ((*cp == '\0') || (*cp == '\n')) | ||
| 177 | continue; | ||
| 178 | (void)strncpy(_res.defdname, cp, | ||
| 179 | sizeof(_res.defdname) - 1); | ||
| 180 | if ((cp = strpbrk(_res.defdname, " \t\n")) != NULL) | ||
| 181 | *cp = '\0'; | ||
| 182 | havesearch = 0; | ||
| 183 | continue; | ||
| 184 | } | ||
| 185 | /* lookup types */ | ||
| 186 | if (!strncmp(buf, "lookup", sizeof("lookup") -1)) { | ||
| 187 | char *sp = NULL; | ||
| 188 | |||
| 189 | bzero(_res.lookups, sizeof _res.lookups); | ||
| 190 | cp = buf + sizeof("lookup") - 1; | ||
| 191 | for (n = 0;; cp++) { | ||
| 192 | if (n == MAXDNSLUS) | ||
| 193 | break; | ||
| 194 | if ((*cp == '\0') || (*cp == '\n')) { | ||
| 195 | if (sp) { | ||
| 196 | if (*sp=='y' || *sp=='b' || *sp=='f') | ||
| 197 | _res.lookups[n++] = *sp; | ||
| 198 | sp = NULL; | ||
| 199 | } | ||
| 200 | break; | ||
| 201 | } else if ((*cp == ' ') || (*cp == '\t') || (*cp == ',')) { | ||
| 202 | if (sp) { | ||
| 203 | if (*sp=='y' || *sp=='b' || *sp=='f') | ||
| 204 | _res.lookups[n++] = *sp; | ||
| 205 | sp = NULL; | ||
| 206 | } | ||
| 207 | } else if (sp == NULL) | ||
| 208 | sp = cp; | ||
| 209 | } | ||
| 210 | continue; | ||
| 211 | } | ||
| 212 | /* set search list */ | ||
| 213 | if (!strncmp(buf, "search", sizeof("search") - 1)) { | ||
| 214 | if (haveenv) /* skip if have from environ */ | ||
| 215 | continue; | ||
| 216 | cp = buf + sizeof("search") - 1; | ||
| 217 | while (*cp == ' ' || *cp == '\t') | ||
| 218 | cp++; | ||
| 219 | if ((*cp == '\0') || (*cp == '\n')) | ||
| 220 | continue; | ||
| 221 | (void)strncpy(_res.defdname, cp, | ||
| 222 | sizeof(_res.defdname) - 1); | ||
| 223 | if ((cp = strchr(_res.defdname, '\n')) != NULL) | ||
| 224 | *cp = '\0'; | ||
| 225 | /* | ||
| 226 | * Set search list to be blank-separated strings | ||
| 227 | * on rest of line. | ||
| 228 | */ | ||
| 229 | cp = _res.defdname; | ||
| 230 | pp = _res.dnsrch; | ||
| 231 | *pp++ = cp; | ||
| 232 | for (n = 0; *cp && pp < _res.dnsrch + MAXDNSRCH; cp++) { | ||
| 233 | if (*cp == ' ' || *cp == '\t') { | ||
| 234 | *cp = 0; | ||
| 235 | n = 1; | ||
| 236 | } else if (n) { | ||
| 237 | *pp++ = cp; | ||
| 238 | n = 0; | ||
| 239 | } | ||
| 240 | } | ||
| 241 | /* null terminate last domain if there are excess */ | ||
| 242 | while (*cp != '\0' && *cp != ' ' && *cp != '\t') | ||
| 243 | cp++; | ||
| 244 | *cp = '\0'; | ||
| 245 | *pp++ = 0; | ||
| 246 | havesearch = 1; | ||
| 247 | continue; | ||
| 248 | } | ||
| 249 | /* read nameservers to query */ | ||
| 250 | if (!strncmp(buf, "nameserver", sizeof("nameserver") - 1) && | ||
| 251 | nserv < MAXNS) { | ||
| 252 | struct in_addr a; | ||
| 253 | |||
| 254 | cp = buf + sizeof("nameserver") - 1; | ||
| 255 | while (*cp == ' ' || *cp == '\t') | ||
| 256 | cp++; | ||
| 257 | if ((*cp != '\0') && (*cp != '\n') && inet_aton(cp, &a)) { | ||
| 258 | _res.nsaddr_list[nserv].sin_len = sizeof(struct sockaddr_in); | ||
| 259 | _res.nsaddr_list[nserv].sin_family = AF_INET; | ||
| 260 | _res.nsaddr_list[nserv].sin_port = | ||
| 261 | htons(NAMESERVER_PORT); | ||
| 262 | _res.nsaddr_list[nserv].sin_addr = a; | ||
| 263 | nserv++; | ||
| 264 | } | ||
| 265 | continue; | ||
| 266 | } | ||
| 267 | if (!strncmp(buf, "sortlist", sizeof("sortlist") - 1)) { | ||
| 268 | struct in_addr a; | ||
| 269 | |||
| 270 | cp = buf + sizeof("sortlist") - 1; | ||
| 271 | while (*cp == ' ' || *cp == '\t') | ||
| 272 | cp++; | ||
| 273 | while (sscanf(cp,"%[0-9./]s", buf2) && nsort < MAXRESOLVSORT) { | ||
| 274 | if (net = strchr(buf2, '/')) | ||
| 275 | *net = '\0'; | ||
| 276 | if (inet_aton(buf2, &a)) { | ||
| 277 | _res.sort_list[nsort].addr = a; | ||
| 278 | if (net && inet_aton(net+1, &a)) { | ||
| 279 | _res.sort_list[nsort].mask = a.s_addr; | ||
| 280 | } else { | ||
| 281 | _res.sort_list[nsort].mask = | ||
| 282 | net_mask(_res.sort_list[nsort].addr); | ||
| 283 | } | ||
| 284 | nsort++; | ||
| 285 | } | ||
| 286 | if (net) | ||
| 287 | *net = '/'; | ||
| 288 | cp += strlen(buf2); | ||
| 289 | while (*cp == ' ' || *cp == '\t') | ||
| 290 | cp++; | ||
| 291 | } | ||
| 292 | continue; | ||
| 293 | } | ||
| 294 | if (!strncmp(buf, "options", sizeof("options") -1)) { | ||
| 295 | res_setoptions(buf + sizeof("options") - 1, "conf"); | ||
| 296 | continue; | ||
| 297 | } | ||
| 298 | } | ||
| 299 | if (nserv > 1) | ||
| 300 | _res.nscount = nserv; | ||
| 301 | _res.nsort = nsort; | ||
| 302 | (void) fclose(fp); | ||
| 303 | } | ||
| 304 | if (_res.defdname[0] == 0) { | ||
| 305 | if (gethostname(buf, sizeof(_res.defdname) - 1) == 0 && | ||
| 306 | (cp = strchr(buf, '.'))) | ||
| 307 | (void)strcpy(_res.defdname, cp + 1); | ||
| 308 | } | ||
| 309 | |||
| 310 | /* find components of local domain that might be searched */ | ||
| 311 | if (havesearch == 0) { | ||
| 312 | pp = _res.dnsrch; | ||
| 313 | *pp++ = _res.defdname; | ||
| 314 | #ifndef SEARCH_LOCAL_DOMAINS | ||
| 315 | *pp = NULL; | ||
| 316 | #else | ||
| 317 | for (cp = _res.defdname, n = 0; *cp; cp++) | ||
| 318 | if (*cp == '.') | ||
| 319 | n++; | ||
| 320 | cp = _res.defdname; | ||
| 321 | for (; n >= LOCALDOMAINPARTS && pp < _res.dnsrch + MAXDFLSRCH; | ||
| 322 | n--) { | ||
| 323 | cp = strchr(cp, '.'); | ||
| 324 | *pp++ = ++cp; | ||
| 325 | } | ||
| 326 | *pp++ = 0; | ||
| 327 | #endif | ||
| 328 | } | ||
| 329 | |||
| 330 | if ((cp = getenv("RES_OPTIONS")) != NULL) | ||
| 331 | res_setoptions(cp, "env"); | ||
| 332 | _res.options |= RES_INIT; | ||
| 333 | return (0); | ||
| 334 | } | ||
| 335 | |||
| 336 | static void | ||
| 337 | res_setoptions(options, source) | ||
| 338 | char *options, *source; | ||
| 339 | { | ||
| 340 | char *cp = options; | ||
| 341 | int i; | ||
| 342 | |||
| 343 | #ifdef DEBUG | ||
| 344 | if (_res.options & RES_DEBUG) { | ||
| 345 | printf(";; res_setoptions(\"%s\", \"%s\")...\n", | ||
| 346 | options, source); | ||
| 347 | } | ||
| 348 | #endif | ||
| 349 | while (*cp) { | ||
| 350 | /* skip leading and inner runs of spaces */ | ||
| 351 | while (*cp == ' ' || *cp == '\t') | ||
| 352 | cp++; | ||
| 353 | /* search for and process individual options */ | ||
| 354 | if (!strncmp(cp, "ndots:", sizeof("ndots:")-1)) { | ||
| 355 | i = atoi(cp + sizeof("ndots:") - 1); | ||
| 356 | if (i <= RES_MAXNDOTS) | ||
| 357 | _res.ndots = i; | ||
| 358 | else | ||
| 359 | _res.ndots = RES_MAXNDOTS; | ||
| 360 | #ifdef DEBUG | ||
| 361 | if (_res.options & RES_DEBUG) { | ||
| 362 | printf(";;\tndots=%d\n", _res.ndots); | ||
| 363 | } | ||
| 364 | #endif | ||
| 365 | } else if (!strncmp(cp, "debug", sizeof("debug")-1)) { | ||
| 366 | #ifdef DEBUG | ||
| 367 | if (!(_res.options & RES_DEBUG)) { | ||
| 368 | printf(";; res_setoptions(\"%s\", \"%s\")..\n", | ||
| 369 | options, source); | ||
| 370 | _res.options |= RES_DEBUG; | ||
| 371 | } | ||
| 372 | printf(";;\tdebug\n"); | ||
| 373 | #endif | ||
| 374 | } else { | ||
| 375 | /* XXX - print a warning here? */ | ||
| 376 | } | ||
| 377 | /* skip to next run of spaces */ | ||
| 378 | while (*cp && *cp != ' ' && *cp != '\t') | ||
| 379 | cp++; | ||
| 380 | } | ||
| 381 | } | ||
| 382 | |||
| 383 | static u_int32_t | ||
| 384 | net_mask(in) /* XXX - should really use system's version of this */ | ||
| 385 | struct in_addr in; | ||
| 386 | { | ||
| 387 | register u_int32_t i = ntohl(in.s_addr); | ||
| 388 | |||
| 389 | if (IN_CLASSA(i)) | ||
| 390 | return (htonl(IN_CLASSA_NET)); | ||
| 391 | if (IN_CLASSB(i)) | ||
| 392 | return (htonl(IN_CLASSB_NET)); | ||
| 393 | return (htonl(IN_CLASSC_NET)); | ||
| 394 | } | ||
diff --git a/src/lib/libc/net/res_mkquery.c b/src/lib/libc/net/res_mkquery.c new file mode 100644 index 0000000000..25f025e147 --- /dev/null +++ b/src/lib/libc/net/res_mkquery.c | |||
| @@ -0,0 +1,236 @@ | |||
| 1 | /* $NetBSD: res_mkquery.c,v 1.5 1995/02/25 06:20:58 cgd Exp $ */ | ||
| 2 | |||
| 3 | /*- | ||
| 4 | * Copyright (c) 1985, 1993 | ||
| 5 | * The Regents of the University of California. All rights reserved. | ||
| 6 | * | ||
| 7 | * Redistribution and use in source and binary forms, with or without | ||
| 8 | * modification, are permitted provided that the following conditions | ||
| 9 | * are met: | ||
| 10 | * 1. Redistributions of source code must retain the above copyright | ||
| 11 | * notice, this list of conditions and the following disclaimer. | ||
| 12 | * 2. Redistributions in binary form must reproduce the above copyright | ||
| 13 | * notice, this list of conditions and the following disclaimer in the | ||
| 14 | * documentation and/or other materials provided with the distribution. | ||
| 15 | * 3. All advertising materials mentioning features or use of this software | ||
| 16 | * must display the following acknowledgement: | ||
| 17 | * This product includes software developed by the University of | ||
| 18 | * California, Berkeley and its contributors. | ||
| 19 | * 4. Neither the name of the University nor the names of its contributors | ||
| 20 | * may be used to endorse or promote products derived from this software | ||
| 21 | * without specific prior written permission. | ||
| 22 | * | ||
| 23 | * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND | ||
| 24 | * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE | ||
| 25 | * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE | ||
| 26 | * ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE | ||
| 27 | * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL | ||
| 28 | * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS | ||
| 29 | * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) | ||
| 30 | * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT | ||
| 31 | * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY | ||
| 32 | * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF | ||
| 33 | * SUCH DAMAGE. | ||
| 34 | * - | ||
| 35 | * Portions Copyright (c) 1993 by Digital Equipment Corporation. | ||
| 36 | * | ||
| 37 | * Permission to use, copy, modify, and distribute this software for any | ||
| 38 | * purpose with or without fee is hereby granted, provided that the above | ||
| 39 | * copyright notice and this permission notice appear in all copies, and that | ||
| 40 | * the name of Digital Equipment Corporation not be used in advertising or | ||
| 41 | * publicity pertaining to distribution of the document or software without | ||
| 42 | * specific, written prior permission. | ||
| 43 | * | ||
| 44 | * THE SOFTWARE IS PROVIDED "AS IS" AND DIGITAL EQUIPMENT CORP. DISCLAIMS ALL | ||
| 45 | * WARRANTIES WITH REGARD TO THIS SOFTWARE, INCLUDING ALL IMPLIED WARRANTIES | ||
| 46 | * OF MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL DIGITAL EQUIPMENT | ||
| 47 | * CORPORATION BE LIABLE FOR ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL | ||
| 48 | * DAMAGES OR ANY DAMAGES WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR | ||
| 49 | * PROFITS, WHETHER IN AN ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS | ||
| 50 | * ACTION, ARISING OUT OF OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS | ||
| 51 | * SOFTWARE. | ||
| 52 | * - | ||
| 53 | * --Copyright-- | ||
| 54 | */ | ||
| 55 | |||
| 56 | #if defined(LIBC_SCCS) && !defined(lint) | ||
| 57 | #if 0 | ||
| 58 | static char sccsid[] = "@(#)res_mkquery.c 8.1 (Berkeley) 6/4/93"; | ||
| 59 | static char rcsid[] = "$Id: res_mkquery.c,v 4.9.1.2 1993/05/17 10:00:01 vixie Exp "; | ||
| 60 | #else | ||
| 61 | static char rcsid[] = "$NetBSD: res_mkquery.c,v 1.5 1995/02/25 06:20:58 cgd Exp $"; | ||
| 62 | #endif | ||
| 63 | #endif /* LIBC_SCCS and not lint */ | ||
| 64 | |||
| 65 | #include <sys/param.h> | ||
| 66 | #include <netinet/in.h> | ||
| 67 | #include <arpa/nameser.h> | ||
| 68 | #include <resolv.h> | ||
| 69 | #include <stdio.h> | ||
| 70 | #include <string.h> | ||
| 71 | |||
| 72 | /* | ||
| 73 | * Form all types of queries. | ||
| 74 | * Returns the size of the result or -1. | ||
| 75 | */ | ||
| 76 | res_mkquery(op, dname, class, type, data, datalen, newrr_in, buf, buflen) | ||
| 77 | int op; /* opcode of query */ | ||
| 78 | const char *dname; /* domain name */ | ||
| 79 | int class, type; /* class and type of query */ | ||
| 80 | const char *data; /* resource record data */ | ||
| 81 | int datalen; /* length of data */ | ||
| 82 | const char *newrr_in; /* new rr for modify or append */ | ||
| 83 | char *buf; /* buffer to put query */ | ||
| 84 | int buflen; /* size of buffer */ | ||
| 85 | { | ||
| 86 | register HEADER *hp; | ||
| 87 | register char *cp; | ||
| 88 | register int n; | ||
| 89 | struct rrec *newrr = (struct rrec *) newrr_in; | ||
| 90 | char *dnptrs[10], **dpp, **lastdnptr; | ||
| 91 | |||
| 92 | #ifdef DEBUG | ||
| 93 | if (_res.options & RES_DEBUG) | ||
| 94 | printf(";; res_mkquery(%d, %s, %d, %d)\n", | ||
| 95 | op, dname, class, type); | ||
| 96 | #endif | ||
| 97 | /* | ||
| 98 | * Initialize header fields. | ||
| 99 | */ | ||
| 100 | if ((buf == NULL) || (buflen < sizeof(HEADER))) | ||
| 101 | return(-1); | ||
| 102 | bzero(buf, sizeof(HEADER)); | ||
| 103 | hp = (HEADER *) buf; | ||
| 104 | hp->id = htons(++_res.id); | ||
| 105 | hp->opcode = op; | ||
| 106 | hp->pr = (_res.options & RES_PRIMARY) != 0; | ||
| 107 | hp->rd = (_res.options & RES_RECURSE) != 0; | ||
| 108 | hp->rcode = NOERROR; | ||
| 109 | cp = buf + sizeof(HEADER); | ||
| 110 | buflen -= sizeof(HEADER); | ||
| 111 | dpp = dnptrs; | ||
| 112 | *dpp++ = buf; | ||
| 113 | *dpp++ = NULL; | ||
| 114 | lastdnptr = dnptrs + sizeof(dnptrs)/sizeof(dnptrs[0]); | ||
| 115 | /* | ||
| 116 | * perform opcode specific processing | ||
| 117 | */ | ||
| 118 | switch (op) { | ||
| 119 | case QUERY: | ||
| 120 | if ((buflen -= QFIXEDSZ) < 0) | ||
| 121 | return(-1); | ||
| 122 | if ((n = dn_comp((u_char *)dname, (u_char *)cp, buflen, | ||
| 123 | (u_char **)dnptrs, (u_char **)lastdnptr)) < 0) | ||
| 124 | return (-1); | ||
| 125 | cp += n; | ||
| 126 | buflen -= n; | ||
| 127 | __putshort(type, (u_char *)cp); | ||
| 128 | cp += sizeof(u_int16_t); | ||
| 129 | __putshort(class, (u_char *)cp); | ||
| 130 | cp += sizeof(u_int16_t); | ||
| 131 | hp->qdcount = htons(1); | ||
| 132 | if (op == QUERY || data == NULL) | ||
| 133 | break; | ||
| 134 | /* | ||
| 135 | * Make an additional record for completion domain. | ||
| 136 | */ | ||
| 137 | buflen -= RRFIXEDSZ; | ||
| 138 | if ((n = dn_comp((u_char *)data, (u_char *)cp, buflen, | ||
| 139 | (u_char **)dnptrs, (u_char **)lastdnptr)) < 0) | ||
| 140 | return (-1); | ||
| 141 | cp += n; | ||
| 142 | buflen -= n; | ||
| 143 | __putshort(T_NULL, (u_char *)cp); | ||
| 144 | cp += sizeof(u_int16_t); | ||
| 145 | __putshort(class, (u_char *)cp); | ||
| 146 | cp += sizeof(u_int16_t); | ||
| 147 | __putlong(0, (u_char *)cp); | ||
| 148 | cp += sizeof(u_int32_t); | ||
| 149 | __putshort(0, (u_char *)cp); | ||
| 150 | cp += sizeof(u_int16_t); | ||
| 151 | hp->arcount = htons(1); | ||
| 152 | break; | ||
| 153 | |||
| 154 | case IQUERY: | ||
| 155 | /* | ||
| 156 | * Initialize answer section | ||
| 157 | */ | ||
| 158 | if (buflen < 1 + RRFIXEDSZ + datalen) | ||
| 159 | return (-1); | ||
| 160 | *cp++ = '\0'; /* no domain name */ | ||
| 161 | __putshort(type, (u_char *)cp); | ||
| 162 | cp += sizeof(u_int16_t); | ||
| 163 | __putshort(class, (u_char *)cp); | ||
| 164 | cp += sizeof(u_int16_t); | ||
| 165 | __putlong(0, (u_char *)cp); | ||
| 166 | cp += sizeof(u_int32_t); | ||
| 167 | __putshort(datalen, (u_char *)cp); | ||
| 168 | cp += sizeof(u_int16_t); | ||
| 169 | if (datalen) { | ||
| 170 | bcopy(data, cp, datalen); | ||
| 171 | cp += datalen; | ||
| 172 | } | ||
| 173 | hp->ancount = htons(1); | ||
| 174 | break; | ||
| 175 | |||
| 176 | #ifdef ALLOW_UPDATES | ||
| 177 | /* | ||
| 178 | * For UPDATEM/UPDATEMA, do UPDATED/UPDATEDA followed by UPDATEA | ||
| 179 | * (Record to be modified is followed by its replacement in msg.) | ||
| 180 | */ | ||
| 181 | case UPDATEM: | ||
| 182 | case UPDATEMA: | ||
| 183 | |||
| 184 | case UPDATED: | ||
| 185 | /* | ||
| 186 | * The res code for UPDATED and UPDATEDA is the same; user | ||
| 187 | * calls them differently: specifies data for UPDATED; server | ||
| 188 | * ignores data if specified for UPDATEDA. | ||
| 189 | */ | ||
| 190 | case UPDATEDA: | ||
| 191 | buflen -= RRFIXEDSZ + datalen; | ||
| 192 | if ((n = dn_comp(dname, cp, buflen, dnptrs, lastdnptr)) < 0) | ||
| 193 | return (-1); | ||
| 194 | cp += n; | ||
| 195 | __putshort(type, cp); | ||
| 196 | cp += sizeof(u_int16_t); | ||
| 197 | __putshort(class, cp); | ||
| 198 | cp += sizeof(u_int16_t); | ||
| 199 | __putlong(0, cp); | ||
| 200 | cp += sizeof(u_int32_t); | ||
| 201 | __putshort(datalen, cp); | ||
| 202 | cp += sizeof(u_int16_t); | ||
| 203 | if (datalen) { | ||
| 204 | bcopy(data, cp, datalen); | ||
| 205 | cp += datalen; | ||
| 206 | } | ||
| 207 | if ( (op == UPDATED) || (op == UPDATEDA) ) { | ||
| 208 | hp->ancount = htons(0); | ||
| 209 | break; | ||
| 210 | } | ||
| 211 | /* Else UPDATEM/UPDATEMA, so drop into code for UPDATEA */ | ||
| 212 | |||
| 213 | case UPDATEA: /* Add new resource record */ | ||
| 214 | buflen -= RRFIXEDSZ + datalen; | ||
| 215 | if ((n = dn_comp(dname, cp, buflen, dnptrs, lastdnptr)) < 0) | ||
| 216 | return (-1); | ||
| 217 | cp += n; | ||
| 218 | __putshort(newrr->r_type, cp); | ||
| 219 | cp += sizeof(u_int16_t); | ||
| 220 | __putshort(newrr->r_class, cp); | ||
| 221 | cp += sizeof(u_int16_t); | ||
| 222 | __putlong(0, cp); | ||
| 223 | cp += sizeof(u_int32_t); | ||
| 224 | __putshort(newrr->r_size, cp); | ||
| 225 | cp += sizeof(u_int16_t); | ||
| 226 | if (newrr->r_size) { | ||
| 227 | bcopy(newrr->r_data, cp, newrr->r_size); | ||
| 228 | cp += newrr->r_size; | ||
| 229 | } | ||
| 230 | hp->ancount = htons(0); | ||
| 231 | break; | ||
| 232 | |||
| 233 | #endif /* ALLOW_UPDATES */ | ||
| 234 | } | ||
| 235 | return (cp - buf); | ||
| 236 | } | ||
diff --git a/src/lib/libc/net/res_query.c b/src/lib/libc/net/res_query.c new file mode 100644 index 0000000000..7649462e56 --- /dev/null +++ b/src/lib/libc/net/res_query.c | |||
| @@ -0,0 +1,366 @@ | |||
| 1 | /* $NetBSD: res_query.c,v 1.9 1995/02/25 06:58:58 cgd Exp $ */ | ||
| 2 | |||
| 3 | /*- | ||
| 4 | * Copyright (c) 1988, 1993 | ||
| 5 | * The Regents of the University of California. All rights reserved. | ||
| 6 | * | ||
| 7 | * Redistribution and use in source and binary forms, with or without | ||
| 8 | * modification, are permitted provided that the following conditions | ||
| 9 | * are met: | ||
| 10 | * 1. Redistributions of source code must retain the above copyright | ||
| 11 | * notice, this list of conditions and the following disclaimer. | ||
| 12 | * 2. Redistributions in binary form must reproduce the above copyright | ||
| 13 | * notice, this list of conditions and the following disclaimer in the | ||
| 14 | * documentation and/or other materials provided with the distribution. | ||
| 15 | * 3. All advertising materials mentioning features or use of this software | ||
| 16 | * must display the following acknowledgement: | ||
| 17 | * This product includes software developed by the University of | ||
| 18 | * California, Berkeley and its contributors. | ||
| 19 | * 4. Neither the name of the University nor the names of its contributors | ||
| 20 | * may be used to endorse or promote products derived from this software | ||
| 21 | * without specific prior written permission. | ||
| 22 | * | ||
| 23 | * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND | ||
| 24 | * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE | ||
| 25 | * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE | ||
| 26 | * ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE | ||
| 27 | * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL | ||
| 28 | * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS | ||
| 29 | * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) | ||
| 30 | * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT | ||
| 31 | * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY | ||
| 32 | * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF | ||
| 33 | * SUCH DAMAGE. | ||
| 34 | * - | ||
| 35 | * Portions Copyright (c) 1993 by Digital Equipment Corporation. | ||
| 36 | * | ||
| 37 | * Permission to use, copy, modify, and distribute this software for any | ||
| 38 | * purpose with or without fee is hereby granted, provided that the above | ||
| 39 | * copyright notice and this permission notice appear in all copies, and that | ||
| 40 | * the name of Digital Equipment Corporation not be used in advertising or | ||
| 41 | * publicity pertaining to distribution of the document or software without | ||
| 42 | * specific, written prior permission. | ||
| 43 | * | ||
| 44 | * THE SOFTWARE IS PROVIDED "AS IS" AND DIGITAL EQUIPMENT CORP. DISCLAIMS ALL | ||
| 45 | * WARRANTIES WITH REGARD TO THIS SOFTWARE, INCLUDING ALL IMPLIED WARRANTIES | ||
| 46 | * OF MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL DIGITAL EQUIPMENT | ||
| 47 | * CORPORATION BE LIABLE FOR ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL | ||
| 48 | * DAMAGES OR ANY DAMAGES WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR | ||
| 49 | * PROFITS, WHETHER IN AN ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS | ||
| 50 | * ACTION, ARISING OUT OF OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS | ||
| 51 | * SOFTWARE. | ||
| 52 | * - | ||
| 53 | * --Copyright-- | ||
| 54 | */ | ||
| 55 | |||
| 56 | #if defined(LIBC_SCCS) && !defined(lint) | ||
| 57 | #if 0 | ||
| 58 | static char sccsid[] = "@(#)res_query.c 8.1 (Berkeley) 6/4/93"; | ||
| 59 | static char rcsid[] = "$Id: res_query.c,v 1.1 1993/06/01 09:42:14 vixie Exp vixie "; | ||
| 60 | #else | ||
| 61 | static char rcsid[] = "$NetBSD: res_query.c,v 1.9 1995/02/25 06:58:58 cgd Exp $"; | ||
| 62 | #endif | ||
| 63 | #endif /* LIBC_SCCS and not lint */ | ||
| 64 | |||
| 65 | #include <sys/param.h> | ||
| 66 | #include <netinet/in.h> | ||
| 67 | #include <arpa/inet.h> | ||
| 68 | #include <arpa/nameser.h> | ||
| 69 | #include <netdb.h> | ||
| 70 | #include <resolv.h> | ||
| 71 | #include <stdio.h> | ||
| 72 | #include <ctype.h> | ||
| 73 | #include <errno.h> | ||
| 74 | #include <stdlib.h> | ||
| 75 | #include <string.h> | ||
| 76 | |||
| 77 | #if PACKETSZ > 1024 | ||
| 78 | #define MAXPACKET PACKETSZ | ||
| 79 | #else | ||
| 80 | #define MAXPACKET 1024 | ||
| 81 | #endif | ||
| 82 | |||
| 83 | char *__hostalias __P((const char *)); | ||
| 84 | int h_errno; | ||
| 85 | |||
| 86 | /* | ||
| 87 | * Formulate a normal query, send, and await answer. | ||
| 88 | * Returned answer is placed in supplied buffer "answer". | ||
| 89 | * Perform preliminary check of answer, returning success only | ||
| 90 | * if no error is indicated and the answer count is nonzero. | ||
| 91 | * Return the size of the response on success, -1 on error. | ||
| 92 | * Error number is left in h_errno. | ||
| 93 | * Caller must parse answer and determine whether it answers the question. | ||
| 94 | */ | ||
| 95 | res_query(name, class, type, answer, anslen) | ||
| 96 | char *name; /* domain name */ | ||
| 97 | int class, type; /* class and type of query */ | ||
| 98 | u_char *answer; /* buffer to put answer */ | ||
| 99 | int anslen; /* size of answer buffer */ | ||
| 100 | { | ||
| 101 | char buf[MAXPACKET]; | ||
| 102 | HEADER *hp; | ||
| 103 | int n; | ||
| 104 | |||
| 105 | if ((_res.options & RES_INIT) == 0 && res_init() == -1) | ||
| 106 | return (-1); | ||
| 107 | #ifdef DEBUG | ||
| 108 | if (_res.options & RES_DEBUG) | ||
| 109 | printf(";; res_query(%s, %d, %d)\n", name, class, type); | ||
| 110 | #endif | ||
| 111 | n = res_mkquery(QUERY, name, class, type, (char *)NULL, 0, NULL, | ||
| 112 | buf, sizeof(buf)); | ||
| 113 | |||
| 114 | if (n <= 0) { | ||
| 115 | #ifdef DEBUG | ||
| 116 | if (_res.options & RES_DEBUG) | ||
| 117 | printf(";; res_query: mkquery failed\n"); | ||
| 118 | #endif | ||
| 119 | h_errno = NO_RECOVERY; | ||
| 120 | return (n); | ||
| 121 | } | ||
| 122 | n = res_send(buf, n, (char *)answer, anslen); | ||
| 123 | if (n < 0) { | ||
| 124 | #ifdef DEBUG | ||
| 125 | if (_res.options & RES_DEBUG) | ||
| 126 | printf(";; res_query: send error\n"); | ||
| 127 | #endif | ||
| 128 | h_errno = TRY_AGAIN; | ||
| 129 | return (n); | ||
| 130 | } | ||
| 131 | |||
| 132 | hp = (HEADER *) answer; | ||
| 133 | if (hp->rcode != NOERROR || ntohs(hp->ancount) == 0) { | ||
| 134 | #ifdef DEBUG | ||
| 135 | if (_res.options & RES_DEBUG) | ||
| 136 | printf(";; rcode = %d, ancount=%d\n", hp->rcode, | ||
| 137 | ntohs(hp->ancount)); | ||
| 138 | #endif | ||
| 139 | switch (hp->rcode) { | ||
| 140 | case NXDOMAIN: | ||
| 141 | h_errno = HOST_NOT_FOUND; | ||
| 142 | break; | ||
| 143 | case SERVFAIL: | ||
| 144 | h_errno = TRY_AGAIN; | ||
| 145 | break; | ||
| 146 | case NOERROR: | ||
| 147 | h_errno = NO_DATA; | ||
| 148 | break; | ||
| 149 | case FORMERR: | ||
| 150 | case NOTIMP: | ||
| 151 | case REFUSED: | ||
| 152 | default: | ||
| 153 | h_errno = NO_RECOVERY; | ||
| 154 | break; | ||
| 155 | } | ||
| 156 | return (-1); | ||
| 157 | } | ||
| 158 | return (n); | ||
| 159 | } | ||
| 160 | |||
| 161 | /* | ||
| 162 | * Formulate a normal query, send, and retrieve answer in supplied buffer. | ||
| 163 | * Return the size of the response on success, -1 on error. | ||
| 164 | * If enabled, implement search rules until answer or unrecoverable failure | ||
| 165 | * is detected. Error number is left in h_errno. | ||
| 166 | * Only useful for queries in the same name hierarchy as the local host | ||
| 167 | * (not, for example, for host address-to-name lookups in domain in-addr.arpa). | ||
| 168 | */ | ||
| 169 | int | ||
| 170 | res_search(name, class, type, answer, anslen) | ||
| 171 | const char *name; /* domain name */ | ||
| 172 | int class, type; /* class and type of query */ | ||
| 173 | u_char *answer; /* buffer to put answer */ | ||
| 174 | int anslen; /* size of answer */ | ||
| 175 | { | ||
| 176 | register char *cp, **domain; | ||
| 177 | int dots, trailing_dot, ret, got_nodata, saved_herrno, tried_as_is; | ||
| 178 | |||
| 179 | if ((_res.options & RES_INIT) == 0 && res_init() == -1) | ||
| 180 | return (-1); | ||
| 181 | |||
| 182 | got_nodata = 0; | ||
| 183 | errno = 0; | ||
| 184 | h_errno = HOST_NOT_FOUND; /* default, if we never query */ | ||
| 185 | dots = 0; | ||
| 186 | for (cp = (char *)name; *cp; cp++) { | ||
| 187 | if (*cp == '.') | ||
| 188 | dots++; | ||
| 189 | } | ||
| 190 | trailing_dot = 0; | ||
| 191 | if ((cp > name) && (*--cp == '.')) | ||
| 192 | trailing_dot++; | ||
| 193 | |||
| 194 | /* | ||
| 195 | * if there aren't any dots, it could be a user-level alias | ||
| 196 | */ | ||
| 197 | if (!dots && (cp = __hostalias(name))) | ||
| 198 | return (res_query(cp, class, type, answer, anslen)); | ||
| 199 | |||
| 200 | /* | ||
| 201 | * If there are dots in the name already, let's just give it a try | ||
| 202 | * 'as is'. The threshold can be set with the "ndots" option. | ||
| 203 | */ | ||
| 204 | saved_herrno = -1; | ||
| 205 | tried_as_is = 0; | ||
| 206 | if (dots >= _res.ndots) { | ||
| 207 | ret = res_querydomain(name, NULL, class, type, answer, anslen); | ||
| 208 | if (ret > 0) | ||
| 209 | return (ret); | ||
| 210 | saved_herrno = h_errno; | ||
| 211 | tried_as_is++; | ||
| 212 | } | ||
| 213 | |||
| 214 | /* | ||
| 215 | * We do at least one level of search if | ||
| 216 | * - there is no dot and RES_DEFNAME is set, or | ||
| 217 | * - there is at least one dot, there is no trailing dot, | ||
| 218 | * and RES_DNSRCH is set. | ||
| 219 | */ | ||
| 220 | if ((!dots && (_res.options & RES_DEFNAMES)) || | ||
| 221 | (dots && !trailing_dot && (_res.options & RES_DNSRCH))) { | ||
| 222 | for (domain = _res.dnsrch; *domain; domain++) { | ||
| 223 | int done = 0; | ||
| 224 | |||
| 225 | ret = res_querydomain(name, *domain, class, type, | ||
| 226 | answer, anslen); | ||
| 227 | if (ret > 0) | ||
| 228 | return (ret); | ||
| 229 | /* | ||
| 230 | * If no server present, give up. | ||
| 231 | * If name isn't found in this domain, | ||
| 232 | * keep trying higher domains in the search list | ||
| 233 | * (if that's enabled). | ||
| 234 | * On a NO_DATA error, keep trying, otherwise | ||
| 235 | * a wildcard entry of another type could keep us | ||
| 236 | * from finding this entry higher in the domain. | ||
| 237 | * If we get some other error (negative answer or | ||
| 238 | * server failure), then stop searching up, | ||
| 239 | * but try the input name below in case it's | ||
| 240 | * fully-qualified. | ||
| 241 | */ | ||
| 242 | if (errno == ECONNREFUSED) { | ||
| 243 | h_errno = TRY_AGAIN; | ||
| 244 | return (-1); | ||
| 245 | } | ||
| 246 | |||
| 247 | switch (h_errno) { | ||
| 248 | case NO_DATA: | ||
| 249 | got_nodata++; | ||
| 250 | /* FALLTHROUGH */ | ||
| 251 | case HOST_NOT_FOUND: | ||
| 252 | /* keep trying */ | ||
| 253 | break; | ||
| 254 | default: | ||
| 255 | /* anything else implies that we're done */ | ||
| 256 | done++; | ||
| 257 | } | ||
| 258 | /* | ||
| 259 | * if we got here for some reason other than DNSRCH, | ||
| 260 | * we only wanted one iteration of the loop, so stop. | ||
| 261 | */ | ||
| 262 | if (!(_res.options & RES_DNSRCH)) | ||
| 263 | done++; | ||
| 264 | |||
| 265 | if (done) | ||
| 266 | break; | ||
| 267 | } | ||
| 268 | } | ||
| 269 | |||
| 270 | /* | ||
| 271 | * if we have not already tried the name "as is", do that now. | ||
| 272 | * note that we do this regardless of how many dots were in the | ||
| 273 | * name or whether it ends with a dot. | ||
| 274 | */ | ||
| 275 | if (!tried_as_is) { | ||
| 276 | ret = res_querydomain(name, NULL, class, type, answer, anslen); | ||
| 277 | if (ret > 0) | ||
| 278 | return (ret); | ||
| 279 | saved_herrno = h_errno; | ||
| 280 | } | ||
| 281 | |||
| 282 | /* | ||
| 283 | * if we got here, we didn't satisfy the search. | ||
| 284 | * if we did an initial full query, return that query's h_errno | ||
| 285 | * (note that we wouldn't be here if that query had succeeded). | ||
| 286 | * else if we ever got a nodata, send that back as the reason. | ||
| 287 | * else send back meaningless h_errno, that being the one from | ||
| 288 | * the last DNSRCH we did. | ||
| 289 | */ | ||
| 290 | if (saved_herrno != -1) | ||
| 291 | h_errno = saved_herrno; | ||
| 292 | else if (got_nodata) | ||
| 293 | h_errno = NO_DATA; | ||
| 294 | return (-1); | ||
| 295 | } | ||
| 296 | |||
| 297 | /* | ||
| 298 | * Perform a call on res_query on the concatenation of name and domain, | ||
| 299 | * removing a trailing dot from name if domain is NULL. | ||
| 300 | */ | ||
| 301 | res_querydomain(name, domain, class, type, answer, anslen) | ||
| 302 | char *name, *domain; | ||
| 303 | int class, type; /* class and type of query */ | ||
| 304 | u_char *answer; /* buffer to put answer */ | ||
| 305 | int anslen; /* size of answer */ | ||
| 306 | { | ||
| 307 | char nbuf[2*MAXDNAME+2]; | ||
| 308 | char *longname = nbuf; | ||
| 309 | int n; | ||
| 310 | |||
| 311 | #ifdef DEBUG | ||
| 312 | if (_res.options & RES_DEBUG) | ||
| 313 | printf(";; res_querydomain(%s, %s, %d, %d)\n", | ||
| 314 | name, domain, class, type); | ||
| 315 | #endif | ||
| 316 | if (domain == NULL) { | ||
| 317 | /* | ||
| 318 | * Check for trailing '.'; | ||
| 319 | * copy without '.' if present. | ||
| 320 | */ | ||
| 321 | n = strlen(name) - 1; | ||
| 322 | if (n != (0 - 1) && name[n] == '.' && n < sizeof(nbuf) - 1) { | ||
| 323 | bcopy(name, nbuf, n); | ||
| 324 | nbuf[n] = '\0'; | ||
| 325 | } else | ||
| 326 | longname = name; | ||
| 327 | } else | ||
| 328 | (void)sprintf(nbuf, "%.*s.%.*s", | ||
| 329 | MAXDNAME, name, MAXDNAME, domain); | ||
| 330 | |||
| 331 | return (res_query(longname, class, type, answer, anslen)); | ||
| 332 | } | ||
| 333 | |||
| 334 | char * | ||
| 335 | __hostalias(name) | ||
| 336 | register const char *name; | ||
| 337 | { | ||
| 338 | register char *cp1, *cp2; | ||
| 339 | FILE *fp; | ||
| 340 | char *file, *getenv(), *strcpy(), *strncpy(); | ||
| 341 | char buf[BUFSIZ]; | ||
| 342 | static char abuf[MAXDNAME]; | ||
| 343 | |||
| 344 | file = getenv("HOSTALIASES"); | ||
| 345 | if (file == NULL || (fp = fopen(file, "r")) == NULL) | ||
| 346 | return (NULL); | ||
| 347 | buf[sizeof(buf) - 1] = '\0'; | ||
| 348 | while (fgets(buf, sizeof(buf), fp)) { | ||
| 349 | for (cp1 = buf; *cp1 && !isspace(*cp1); ++cp1); | ||
| 350 | if (!*cp1) | ||
| 351 | break; | ||
| 352 | *cp1 = '\0'; | ||
| 353 | if (!strcasecmp(buf, name)) { | ||
| 354 | while (isspace(*++cp1)); | ||
| 355 | if (!*cp1) | ||
| 356 | break; | ||
| 357 | for (cp2 = cp1 + 1; *cp2 && !isspace(*cp2); ++cp2); | ||
| 358 | abuf[sizeof(abuf) - 1] = *cp2 = '\0'; | ||
| 359 | (void)strncpy(abuf, cp1, sizeof(abuf) - 1); | ||
| 360 | fclose(fp); | ||
| 361 | return (abuf); | ||
| 362 | } | ||
| 363 | } | ||
| 364 | fclose(fp); | ||
| 365 | return (NULL); | ||
| 366 | } | ||
diff --git a/src/lib/libc/net/res_send.c b/src/lib/libc/net/res_send.c new file mode 100644 index 0000000000..e608358180 --- /dev/null +++ b/src/lib/libc/net/res_send.c | |||
| @@ -0,0 +1,474 @@ | |||
| 1 | /* $NetBSD: res_send.c,v 1.4 1995/02/25 06:21:01 cgd Exp $ */ | ||
| 2 | |||
| 3 | /*- | ||
| 4 | * Copyright (c) 1985, 1989, 1993 | ||
| 5 | * The Regents of the University of California. All rights reserved. | ||
| 6 | * | ||
| 7 | * Redistribution and use in source and binary forms, with or without | ||
| 8 | * modification, are permitted provided that the following conditions | ||
| 9 | * are met: | ||
| 10 | * 1. Redistributions of source code must retain the above copyright | ||
| 11 | * notice, this list of conditions and the following disclaimer. | ||
| 12 | * 2. Redistributions in binary form must reproduce the above copyright | ||
| 13 | * notice, this list of conditions and the following disclaimer in the | ||
| 14 | * documentation and/or other materials provided with the distribution. | ||
| 15 | * 3. All advertising materials mentioning features or use of this software | ||
| 16 | * must display the following acknowledgement: | ||
| 17 | * This product includes software developed by the University of | ||
| 18 | * California, Berkeley and its contributors. | ||
| 19 | * 4. Neither the name of the University nor the names of its contributors | ||
| 20 | * may be used to endorse or promote products derived from this software | ||
| 21 | * without specific prior written permission. | ||
| 22 | * | ||
| 23 | * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND | ||
| 24 | * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE | ||
| 25 | * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE | ||
| 26 | * ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE | ||
| 27 | * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL | ||
| 28 | * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS | ||
| 29 | * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) | ||
| 30 | * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT | ||
| 31 | * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY | ||
| 32 | * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF | ||
| 33 | * SUCH DAMAGE. | ||
| 34 | * - | ||
| 35 | * Portions Copyright (c) 1993 by Digital Equipment Corporation. | ||
| 36 | * | ||
| 37 | * Permission to use, copy, modify, and distribute this software for any | ||
| 38 | * purpose with or without fee is hereby granted, provided that the above | ||
| 39 | * copyright notice and this permission notice appear in all copies, and that | ||
| 40 | * the name of Digital Equipment Corporation not be used in advertising or | ||
| 41 | * publicity pertaining to distribution of the document or software without | ||
| 42 | * specific, written prior permission. | ||
| 43 | * | ||
| 44 | * THE SOFTWARE IS PROVIDED "AS IS" AND DIGITAL EQUIPMENT CORP. DISCLAIMS ALL | ||
| 45 | * WARRANTIES WITH REGARD TO THIS SOFTWARE, INCLUDING ALL IMPLIED WARRANTIES | ||
| 46 | * OF MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL DIGITAL EQUIPMENT | ||
| 47 | * CORPORATION BE LIABLE FOR ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL | ||
| 48 | * DAMAGES OR ANY DAMAGES WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR | ||
| 49 | * PROFITS, WHETHER IN AN ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS | ||
| 50 | * ACTION, ARISING OUT OF OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS | ||
| 51 | * SOFTWARE. | ||
| 52 | * - | ||
| 53 | * --Copyright-- | ||
| 54 | */ | ||
| 55 | |||
| 56 | #if defined(LIBC_SCCS) && !defined(lint) | ||
| 57 | #if 0 | ||
| 58 | static char sccsid[] = "@(#)res_send.c 8.1 (Berkeley) 6/4/93"; | ||
| 59 | static char rcsid[] = "$Id: res_send.c,v 4.9.1.1 1993/05/02 22:43:03 vixie Rel "; | ||
| 60 | #else | ||
| 61 | static char rcsid[] = "$NetBSD: res_send.c,v 1.4 1995/02/25 06:21:01 cgd Exp $"; | ||
| 62 | #endif | ||
| 63 | #endif /* LIBC_SCCS and not lint */ | ||
| 64 | |||
| 65 | /* | ||
| 66 | * Send query to name server and wait for reply. | ||
| 67 | */ | ||
| 68 | |||
| 69 | #include <sys/param.h> | ||
| 70 | #include <sys/time.h> | ||
| 71 | #include <sys/socket.h> | ||
| 72 | #include <sys/uio.h> | ||
| 73 | #include <netinet/in.h> | ||
| 74 | #include <arpa/nameser.h> | ||
| 75 | #include <arpa/inet.h> | ||
| 76 | #include <stdio.h> | ||
| 77 | #include <errno.h> | ||
| 78 | #include <resolv.h> | ||
| 79 | #include <unistd.h> | ||
| 80 | #include <string.h> | ||
| 81 | |||
| 82 | static int s = -1; /* socket used for communications */ | ||
| 83 | static struct sockaddr no_addr; | ||
| 84 | |||
| 85 | #ifndef FD_SET | ||
| 86 | #define NFDBITS 32 | ||
| 87 | #define FD_SETSIZE 32 | ||
| 88 | #define FD_SET(n, p) ((p)->fds_bits[(n)/NFDBITS] |= (1 << ((n) % NFDBITS))) | ||
| 89 | #define FD_CLR(n, p) ((p)->fds_bits[(n)/NFDBITS] &= ~(1 << ((n) % NFDBITS))) | ||
| 90 | #define FD_ISSET(n, p) ((p)->fds_bits[(n)/NFDBITS] & (1 << ((n) % NFDBITS))) | ||
| 91 | #define FD_ZERO(p) bzero((char *)(p), sizeof(*(p))) | ||
| 92 | #endif | ||
| 93 | |||
| 94 | res_send(buf, buflen, answer, anslen) | ||
| 95 | const char *buf; | ||
| 96 | int buflen; | ||
| 97 | char *answer; | ||
| 98 | int anslen; | ||
| 99 | { | ||
| 100 | register int n; | ||
| 101 | int try, v_circuit, resplen, ns; | ||
| 102 | int gotsomewhere = 0, connected = 0; | ||
| 103 | int connreset = 0; | ||
| 104 | u_short id, len; | ||
| 105 | char *cp; | ||
| 106 | fd_set dsmask; | ||
| 107 | struct timeval timeout; | ||
| 108 | HEADER *hp = (HEADER *) buf; | ||
| 109 | HEADER *anhp = (HEADER *) answer; | ||
| 110 | u_int badns; /* XXX NSMAX can't exceed #/bits per this */ | ||
| 111 | struct iovec iov[2]; | ||
| 112 | int terrno = ETIMEDOUT; | ||
| 113 | char junk[512]; | ||
| 114 | |||
| 115 | #ifdef DEBUG | ||
| 116 | if ((_res.options & RES_DEBUG) || (_res.pfcode & RES_PRF_QUERY)) { | ||
| 117 | printf(";; res_send()\n"); | ||
| 118 | __p_query(buf); | ||
| 119 | } | ||
| 120 | #endif | ||
| 121 | if (!(_res.options & RES_INIT)) | ||
| 122 | if (res_init() == -1) { | ||
| 123 | return(-1); | ||
| 124 | } | ||
| 125 | v_circuit = (_res.options & RES_USEVC) || buflen > PACKETSZ; | ||
| 126 | id = hp->id; | ||
| 127 | badns = 0; | ||
| 128 | /* | ||
| 129 | * Send request, RETRY times, or until successful | ||
| 130 | */ | ||
| 131 | for (try = 0; try < _res.retry; try++) { | ||
| 132 | for (ns = 0; ns < _res.nscount; ns++) { | ||
| 133 | if (badns & (1<<ns)) | ||
| 134 | continue; | ||
| 135 | #ifdef DEBUG | ||
| 136 | if (_res.options & RES_DEBUG) | ||
| 137 | printf(";; Querying server (# %d) address = %s\n", | ||
| 138 | ns+1, | ||
| 139 | inet_ntoa(_res.nsaddr_list[ns].sin_addr)); | ||
| 140 | #endif | ||
| 141 | usevc: | ||
| 142 | if (v_circuit) { | ||
| 143 | int truncated = 0; | ||
| 144 | |||
| 145 | /* | ||
| 146 | * Use virtual circuit; | ||
| 147 | * at most one attempt per server. | ||
| 148 | */ | ||
| 149 | try = _res.retry; | ||
| 150 | if (s < 0) { | ||
| 151 | s = socket(AF_INET, SOCK_STREAM, 0); | ||
| 152 | if (s < 0) { | ||
| 153 | terrno = errno; | ||
| 154 | #ifdef DEBUG | ||
| 155 | if (_res.options & RES_DEBUG) | ||
| 156 | perror("socket (vc) failed"); | ||
| 157 | #endif | ||
| 158 | continue; | ||
| 159 | } | ||
| 160 | if (connect(s, | ||
| 161 | (struct sockaddr *)&(_res.nsaddr_list[ns]), | ||
| 162 | sizeof(struct sockaddr)) < 0) { | ||
| 163 | terrno = errno; | ||
| 164 | #ifdef DEBUG | ||
| 165 | if (_res.options & RES_DEBUG) | ||
| 166 | perror("connect failed"); | ||
| 167 | #endif | ||
| 168 | (void) close(s); | ||
| 169 | s = -1; | ||
| 170 | continue; | ||
| 171 | } | ||
| 172 | } | ||
| 173 | /* | ||
| 174 | * Send length & message | ||
| 175 | */ | ||
| 176 | len = htons((u_short)buflen); | ||
| 177 | iov[0].iov_base = (caddr_t)&len; | ||
| 178 | iov[0].iov_len = sizeof(len); | ||
| 179 | iov[1].iov_base = (char *)buf; | ||
| 180 | iov[1].iov_len = buflen; | ||
| 181 | if (writev(s, iov, 2) != sizeof(len) + buflen) { | ||
| 182 | terrno = errno; | ||
| 183 | #ifdef DEBUG | ||
| 184 | if (_res.options & RES_DEBUG) | ||
| 185 | perror("write failed"); | ||
| 186 | #endif | ||
| 187 | (void) close(s); | ||
| 188 | s = -1; | ||
| 189 | continue; | ||
| 190 | } | ||
| 191 | /* | ||
| 192 | * Receive length & response | ||
| 193 | */ | ||
| 194 | cp = answer; | ||
| 195 | len = sizeof(short); | ||
| 196 | while (len != 0 && | ||
| 197 | (n = read(s, (char *)cp, (int)len)) > 0) { | ||
| 198 | cp += n; | ||
| 199 | len -= n; | ||
| 200 | } | ||
| 201 | if (n <= 0) { | ||
| 202 | terrno = errno; | ||
| 203 | #ifdef DEBUG | ||
| 204 | if (_res.options & RES_DEBUG) | ||
| 205 | perror("read failed"); | ||
| 206 | #endif | ||
| 207 | (void) close(s); | ||
| 208 | s = -1; | ||
| 209 | /* | ||
| 210 | * A long running process might get its TCP | ||
| 211 | * connection reset if the remote server was | ||
| 212 | * restarted. Requery the server instead of | ||
| 213 | * trying a new one. When there is only one | ||
| 214 | * server, this means that a query might work | ||
| 215 | * instead of failing. We only allow one reset | ||
| 216 | * per query to prevent looping. | ||
| 217 | */ | ||
| 218 | if (terrno == ECONNRESET && !connreset) { | ||
| 219 | connreset = 1; | ||
| 220 | ns--; | ||
| 221 | } | ||
| 222 | continue; | ||
| 223 | } | ||
| 224 | cp = answer; | ||
| 225 | if ((resplen = ntohs(*(u_short *)cp)) > anslen) { | ||
| 226 | #ifdef DEBUG | ||
| 227 | if (_res.options & RES_DEBUG) | ||
| 228 | fprintf(stderr, | ||
| 229 | ";; response truncated\n"); | ||
| 230 | #endif | ||
| 231 | len = anslen; | ||
| 232 | truncated = 1; | ||
| 233 | } else | ||
| 234 | len = resplen; | ||
| 235 | while (len != 0 && | ||
| 236 | (n = read(s, (char *)cp, (int)len)) > 0) { | ||
| 237 | cp += n; | ||
| 238 | len -= n; | ||
| 239 | } | ||
| 240 | if (n <= 0) { | ||
| 241 | terrno = errno; | ||
| 242 | #ifdef DEBUG | ||
| 243 | if (_res.options & RES_DEBUG) | ||
| 244 | perror("read failed"); | ||
| 245 | #endif | ||
| 246 | (void) close(s); | ||
| 247 | s = -1; | ||
| 248 | continue; | ||
| 249 | } | ||
| 250 | if (truncated) { | ||
| 251 | /* | ||
| 252 | * Flush rest of answer | ||
| 253 | * so connection stays in synch. | ||
| 254 | */ | ||
| 255 | anhp->tc = 1; | ||
| 256 | len = resplen - anslen; | ||
| 257 | while (len != 0) { | ||
| 258 | n = (len > sizeof(junk) ? | ||
| 259 | sizeof(junk) : len); | ||
| 260 | if ((n = read(s, junk, n)) > 0) | ||
| 261 | len -= n; | ||
| 262 | else | ||
| 263 | break; | ||
| 264 | } | ||
| 265 | } | ||
| 266 | } else { | ||
| 267 | /* | ||
| 268 | * Use datagrams. | ||
| 269 | */ | ||
| 270 | if (s < 0) { | ||
| 271 | s = socket(AF_INET, SOCK_DGRAM, 0); | ||
| 272 | if (s < 0) { | ||
| 273 | terrno = errno; | ||
| 274 | #ifdef DEBUG | ||
| 275 | if (_res.options & RES_DEBUG) | ||
| 276 | perror("socket (dg) failed"); | ||
| 277 | #endif | ||
| 278 | continue; | ||
| 279 | } | ||
| 280 | } | ||
| 281 | /* | ||
| 282 | * I'm tired of answering this question, so: | ||
| 283 | * On a 4.3BSD+ machine (client and server, | ||
| 284 | * actually), sending to a nameserver datagram | ||
| 285 | * port with no nameserver will cause an | ||
| 286 | * ICMP port unreachable message to be returned. | ||
| 287 | * If our datagram socket is "connected" to the | ||
| 288 | * server, we get an ECONNREFUSED error on the next | ||
| 289 | * socket operation, and select returns if the | ||
| 290 | * error message is received. We can thus detect | ||
| 291 | * the absence of a nameserver without timing out. | ||
| 292 | * If we have sent queries to at least two servers, | ||
| 293 | * however, we don't want to remain connected, | ||
| 294 | * as we wish to receive answers from the first | ||
| 295 | * server to respond. | ||
| 296 | */ | ||
| 297 | if (_res.nscount == 1 || (try == 0 && ns == 0)) { | ||
| 298 | /* | ||
| 299 | * Don't use connect if we might | ||
| 300 | * still receive a response | ||
| 301 | * from another server. | ||
| 302 | */ | ||
| 303 | if (connected == 0) { | ||
| 304 | if (connect(s, | ||
| 305 | (struct sockaddr *) | ||
| 306 | &_res.nsaddr_list[ns], | ||
| 307 | sizeof(struct sockaddr)) < 0) { | ||
| 308 | #ifdef DEBUG | ||
| 309 | if (_res.options & RES_DEBUG) | ||
| 310 | perror("connect"); | ||
| 311 | #endif | ||
| 312 | continue; | ||
| 313 | } | ||
| 314 | connected = 1; | ||
| 315 | } | ||
| 316 | if (send(s, buf, buflen, 0) != buflen) { | ||
| 317 | #ifdef DEBUG | ||
| 318 | if (_res.options & RES_DEBUG) | ||
| 319 | perror("send"); | ||
| 320 | #endif | ||
| 321 | continue; | ||
| 322 | } | ||
| 323 | } else { | ||
| 324 | /* | ||
| 325 | * Disconnect if we want to listen | ||
| 326 | * for responses from more than one server. | ||
| 327 | */ | ||
| 328 | if (connected) { | ||
| 329 | (void) connect(s, &no_addr, | ||
| 330 | sizeof(no_addr)); | ||
| 331 | connected = 0; | ||
| 332 | } | ||
| 333 | if (sendto(s, buf, buflen, 0, | ||
| 334 | (struct sockaddr *)&_res.nsaddr_list[ns], | ||
| 335 | sizeof(struct sockaddr)) != buflen) { | ||
| 336 | #ifdef DEBUG | ||
| 337 | if (_res.options & RES_DEBUG) | ||
| 338 | perror("sendto"); | ||
| 339 | #endif | ||
| 340 | continue; | ||
| 341 | } | ||
| 342 | } | ||
| 343 | |||
| 344 | /* | ||
| 345 | * Wait for reply | ||
| 346 | */ | ||
| 347 | timeout.tv_sec = (_res.retrans << try); | ||
| 348 | if (try > 0) | ||
| 349 | timeout.tv_sec /= _res.nscount; | ||
| 350 | if ((long) timeout.tv_sec <= 0) | ||
| 351 | timeout.tv_sec = 1; | ||
| 352 | timeout.tv_usec = 0; | ||
| 353 | wait: | ||
| 354 | FD_ZERO(&dsmask); | ||
| 355 | FD_SET(s, &dsmask); | ||
| 356 | n = select(s+1, &dsmask, (fd_set *)NULL, | ||
| 357 | (fd_set *)NULL, &timeout); | ||
| 358 | if (n < 0) { | ||
| 359 | #ifdef DEBUG | ||
| 360 | if (_res.options & RES_DEBUG) | ||
| 361 | perror("select"); | ||
| 362 | #endif | ||
| 363 | continue; | ||
| 364 | } | ||
| 365 | if (n == 0) { | ||
| 366 | /* | ||
| 367 | * timeout | ||
| 368 | */ | ||
| 369 | #ifdef DEBUG | ||
| 370 | if (_res.options & RES_DEBUG) | ||
| 371 | printf(";; timeout\n"); | ||
| 372 | #endif | ||
| 373 | gotsomewhere = 1; | ||
| 374 | continue; | ||
| 375 | } | ||
| 376 | if ((resplen = recv(s, answer, anslen, 0)) <= 0) { | ||
| 377 | #ifdef DEBUG | ||
| 378 | if (_res.options & RES_DEBUG) | ||
| 379 | perror("recvfrom"); | ||
| 380 | #endif | ||
| 381 | continue; | ||
| 382 | } | ||
| 383 | gotsomewhere = 1; | ||
| 384 | if (id != anhp->id) { | ||
| 385 | /* | ||
| 386 | * response from old query, ignore it | ||
| 387 | */ | ||
| 388 | #ifdef DEBUG | ||
| 389 | if ((_res.options & RES_DEBUG) || | ||
| 390 | (_res.pfcode & RES_PRF_REPLY)) { | ||
| 391 | printf(";; old answer:\n"); | ||
| 392 | __p_query(answer); | ||
| 393 | } | ||
| 394 | #endif | ||
| 395 | goto wait; | ||
| 396 | } | ||
| 397 | if (anhp->rcode == SERVFAIL || anhp->rcode == NOTIMP || | ||
| 398 | anhp->rcode == REFUSED) { | ||
| 399 | #ifdef DEBUG | ||
| 400 | if (_res.options & RES_DEBUG) { | ||
| 401 | printf("server rejected query:\n"); | ||
| 402 | __p_query(answer); | ||
| 403 | } | ||
| 404 | #endif | ||
| 405 | badns |= (1<<ns); | ||
| 406 | continue; | ||
| 407 | } | ||
| 408 | if (!(_res.options & RES_IGNTC) && anhp->tc) { | ||
| 409 | /* | ||
| 410 | * get rest of answer; | ||
| 411 | * use TCP with same server. | ||
| 412 | */ | ||
| 413 | #ifdef DEBUG | ||
| 414 | if (_res.options & RES_DEBUG) | ||
| 415 | printf(";; truncated answer\n"); | ||
| 416 | #endif | ||
| 417 | (void) close(s); | ||
| 418 | s = -1; | ||
| 419 | v_circuit = 1; | ||
| 420 | goto usevc; | ||
| 421 | } | ||
| 422 | } | ||
| 423 | #ifdef DEBUG | ||
| 424 | if (_res.options & RES_DEBUG) | ||
| 425 | printf(";; got answer:\n"); | ||
| 426 | if ((_res.options & RES_DEBUG) || | ||
| 427 | (_res.pfcode & RES_PRF_REPLY)) | ||
| 428 | __p_query(answer); | ||
| 429 | #endif | ||
| 430 | /* | ||
| 431 | * If using virtual circuits, we assume that the first server | ||
| 432 | * is preferred * over the rest (i.e. it is on the local | ||
| 433 | * machine) and only keep that one open. | ||
| 434 | * If we have temporarily opened a virtual circuit, | ||
| 435 | * or if we haven't been asked to keep a socket open, | ||
| 436 | * close the socket. | ||
| 437 | */ | ||
| 438 | if ((v_circuit && | ||
| 439 | ((_res.options & RES_USEVC) == 0 || ns != 0)) || | ||
| 440 | (_res.options & RES_STAYOPEN) == 0) { | ||
| 441 | (void) close(s); | ||
| 442 | s = -1; | ||
| 443 | } | ||
| 444 | return (resplen); | ||
| 445 | } | ||
| 446 | } | ||
| 447 | if (s >= 0) { | ||
| 448 | (void) close(s); | ||
| 449 | s = -1; | ||
| 450 | } | ||
| 451 | if (v_circuit == 0) | ||
| 452 | if (gotsomewhere == 0) | ||
| 453 | errno = ECONNREFUSED; /* no nameservers found */ | ||
| 454 | else | ||
| 455 | errno = ETIMEDOUT; /* no answer obtained */ | ||
| 456 | else | ||
| 457 | errno = terrno; | ||
| 458 | return (-1); | ||
| 459 | } | ||
| 460 | |||
| 461 | /* | ||
| 462 | * This routine is for closing the socket if a virtual circuit is used and | ||
| 463 | * the program wants to close it. This provides support for endhostent() | ||
| 464 | * which expects to close the socket. | ||
| 465 | * | ||
| 466 | * This routine is not expected to be user visible. | ||
| 467 | */ | ||
| 468 | _res_close() | ||
| 469 | { | ||
| 470 | if (s != -1) { | ||
| 471 | (void) close(s); | ||
| 472 | s = -1; | ||
| 473 | } | ||
| 474 | } | ||
diff --git a/src/lib/libc/net/resolver.3 b/src/lib/libc/net/resolver.3 new file mode 100644 index 0000000000..99abe17f03 --- /dev/null +++ b/src/lib/libc/net/resolver.3 | |||
| @@ -0,0 +1,344 @@ | |||
| 1 | .\" $NetBSD: resolver.3,v 1.5 1995/02/25 06:21:02 cgd Exp $ | ||
| 2 | .\" | ||
| 3 | .\" Copyright (c) 1985, 1991, 1993 | ||
| 4 | .\" The Regents of the University of California. All rights reserved. | ||
| 5 | .\" | ||
| 6 | .\" Redistribution and use in source and binary forms, with or without | ||
| 7 | .\" modification, are permitted provided that the following conditions | ||
| 8 | .\" are met: | ||
| 9 | .\" 1. Redistributions of source code must retain the above copyright | ||
| 10 | .\" notice, this list of conditions and the following disclaimer. | ||
| 11 | .\" 2. Redistributions in binary form must reproduce the above copyright | ||
| 12 | .\" notice, this list of conditions and the following disclaimer in the | ||
| 13 | .\" documentation and/or other materials provided with the distribution. | ||
| 14 | .\" 3. All advertising materials mentioning features or use of this software | ||
| 15 | .\" must display the following acknowledgement: | ||
| 16 | .\" This product includes software developed by the University of | ||
| 17 | .\" California, Berkeley and its contributors. | ||
| 18 | .\" 4. Neither the name of the University nor the names of its contributors | ||
| 19 | .\" may be used to endorse or promote products derived from this software | ||
| 20 | .\" without specific prior written permission. | ||
| 21 | .\" | ||
| 22 | .\" THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND | ||
| 23 | .\" ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE | ||
| 24 | .\" IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE | ||
| 25 | .\" ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE | ||
| 26 | .\" FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL | ||
| 27 | .\" DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS | ||
| 28 | .\" OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) | ||
| 29 | .\" HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT | ||
| 30 | .\" LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY | ||
| 31 | .\" OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF | ||
| 32 | .\" SUCH DAMAGE. | ||
| 33 | .\" | ||
| 34 | .\" @(#)resolver.3 8.1 (Berkeley) 6/4/93 | ||
| 35 | .\" | ||
| 36 | .Dd June 4, 1993 | ||
| 37 | .Dt RESOLVER 3 | ||
| 38 | .Os BSD 4.3 | ||
| 39 | .Sh NAME | ||
| 40 | .Nm res_query , | ||
| 41 | .Nm res_search , | ||
| 42 | .Nm res_mkquery , | ||
| 43 | .Nm res_send , | ||
| 44 | .Nm res_init , | ||
| 45 | .Nm dn_comp , | ||
| 46 | .Nm dn_expand | ||
| 47 | .Nd resolver routines | ||
| 48 | .Sh SYNOPSIS | ||
| 49 | .Fd #include <sys/types.h> | ||
| 50 | .Fd #include <netinet/in.h> | ||
| 51 | .Fd #include <arpa/nameser.h> | ||
| 52 | .Fd #include <resolv.h> | ||
| 53 | .Fo res_query | ||
| 54 | .Fa "char *dname" | ||
| 55 | .Fa "int class" | ||
| 56 | .Fa "int type" | ||
| 57 | .Fa "u_char *answer" | ||
| 58 | .Fa "int anslen" | ||
| 59 | .Fc | ||
| 60 | .Fo res_search | ||
| 61 | .Fa "char *dname" | ||
| 62 | .Fa "int class" | ||
| 63 | .Fa "int type" | ||
| 64 | .Fa "u_char *answer" | ||
| 65 | .Fa "int anslen" | ||
| 66 | .Fc | ||
| 67 | .Fo res_mkquery | ||
| 68 | .Fa "int op" | ||
| 69 | .Fa "char *dname" | ||
| 70 | .Fa "int class" | ||
| 71 | .Fa "int type" | ||
| 72 | .Fa "char *data" | ||
| 73 | .Fa "int datalen" | ||
| 74 | .Fa "struct rrec *newrr" | ||
| 75 | .Fa "char *buf" | ||
| 76 | .Fa "int buflen" | ||
| 77 | .Fc | ||
| 78 | .Fo res_send | ||
| 79 | .Fa "char *msg" | ||
| 80 | .Fa "int msglen" | ||
| 81 | .Fa "char *answer" | ||
| 82 | .Fa "int anslen" | ||
| 83 | .Fc | ||
| 84 | .Fn res_init | ||
| 85 | .Fo dn_comp | ||
| 86 | .Fa "char *exp_dn" | ||
| 87 | .Fa "char *comp_dn" | ||
| 88 | .Fa "int length" | ||
| 89 | .Fa "char **dnptrs" | ||
| 90 | .Fa "char **lastdnptr" | ||
| 91 | .Fc | ||
| 92 | .Fo dn_expand | ||
| 93 | .Fa "u_char *msg" | ||
| 94 | .Fa "u_char *eomorig" | ||
| 95 | .Fa "u_char *comp_dn" | ||
| 96 | .Fa "u_char *exp_dn" | ||
| 97 | .Fa "int length" | ||
| 98 | .Fc | ||
| 99 | .Sh DESCRIPTION | ||
| 100 | These routines are used for making, sending and interpreting | ||
| 101 | query and reply messages with Internet domain name servers. | ||
| 102 | .Pp | ||
| 103 | Global configuration and state information that is used by the | ||
| 104 | resolver routines is kept in the structure | ||
| 105 | .Em _res . | ||
| 106 | Most of the values have reasonable defaults and can be ignored. | ||
| 107 | Options | ||
| 108 | stored in | ||
| 109 | .Em _res.options | ||
| 110 | are defined in | ||
| 111 | .Pa resolv.h | ||
| 112 | and are as follows. | ||
| 113 | Options are stored as a simple bit mask containing the bitwise ``or'' | ||
| 114 | of the options enabled. | ||
| 115 | .Bl -tag -width RES_DEFNAMES | ||
| 116 | .It Dv RES_INIT | ||
| 117 | True if the initial name server address and default domain name are | ||
| 118 | initialized (i.e., | ||
| 119 | .Fn res_init | ||
| 120 | has been called). | ||
| 121 | .It Dv RES_DEBUG | ||
| 122 | Print debugging messages. | ||
| 123 | .It Dv RES_AAONLY | ||
| 124 | Accept authoritative answers only. | ||
| 125 | With this option, | ||
| 126 | .Fn res_send | ||
| 127 | should continue until it finds an authoritative answer or finds an error. | ||
| 128 | Currently this is not implemented. | ||
| 129 | .It Dv RES_USEVC | ||
| 130 | Use | ||
| 131 | .Tn TCP | ||
| 132 | connections for queries instead of | ||
| 133 | .Tn UDP | ||
| 134 | datagrams. | ||
| 135 | .It Dv RES_STAYOPEN | ||
| 136 | Used with | ||
| 137 | .Dv RES_USEVC | ||
| 138 | to keep the | ||
| 139 | .Tn TCP | ||
| 140 | connection open between | ||
| 141 | queries. | ||
| 142 | This is useful only in programs that regularly do many queries. | ||
| 143 | .Tn UDP | ||
| 144 | should be the normal mode used. | ||
| 145 | .It Dv RES_IGNTC | ||
| 146 | Unused currently (ignore truncation errors, i.e., don't retry with | ||
| 147 | .Tn TCP ) . | ||
| 148 | .It Dv RES_RECURSE | ||
| 149 | Set the recursion-desired bit in queries. | ||
| 150 | This is the default. | ||
| 151 | .Pf ( Fn res_send | ||
| 152 | does not do iterative queries and expects the name server | ||
| 153 | to handle recursion.) | ||
| 154 | .It Dv RES_DEFNAMES | ||
| 155 | If set, | ||
| 156 | .Fn res_search | ||
| 157 | will append the default domain name to single-component names | ||
| 158 | (those that do not contain a dot). | ||
| 159 | This option is enabled by default. | ||
| 160 | .It Dv RES_DNSRCH | ||
| 161 | If this option is set, | ||
| 162 | .Fn res_search | ||
| 163 | will search for host names in the current domain and in parent domains; see | ||
| 164 | .Xr hostname 7 . | ||
| 165 | This is used by the standard host lookup routine | ||
| 166 | .Xr gethostbyname 3 . | ||
| 167 | This option is enabled by default. | ||
| 168 | .El | ||
| 169 | .Pp | ||
| 170 | The | ||
| 171 | .Fn res_init | ||
| 172 | routine | ||
| 173 | reads the configuration file (if any; see | ||
| 174 | .Xr resolv.conf 5 ) | ||
| 175 | to get the default domain name, | ||
| 176 | search list and | ||
| 177 | the Internet address of the local name server(s). | ||
| 178 | If no server is configured, the host running | ||
| 179 | the resolver is tried. | ||
| 180 | The current domain name is defined by the hostname | ||
| 181 | if not specified in the configuration file; | ||
| 182 | it can be overridden by the environment variable | ||
| 183 | .Ev LOCALDOMAIN . | ||
| 184 | This environment variable may contain several blank-separated | ||
| 185 | tokens if you wish to override the | ||
| 186 | .Fa search list | ||
| 187 | on a per-process basis. | ||
| 188 | This is similar to the | ||
| 189 | .Fa search | ||
| 190 | command in the configuration file. | ||
| 191 | Another environment variable | ||
| 192 | .Ev RES_OPTIONS | ||
| 193 | can be set to override certain internal resolver options which | ||
| 194 | are otherwise set by changing fields in the | ||
| 195 | .Fa _res | ||
| 196 | structure or are inherited from the configuration file's | ||
| 197 | .Fa options | ||
| 198 | command. | ||
| 199 | The syntax of the | ||
| 200 | .Ev RES_OPTIONS | ||
| 201 | environment variable is explained in | ||
| 202 | .Xr resolv.conf 5 . | ||
| 203 | Initialization normally occurs on the first call | ||
| 204 | to one of the following routines. | ||
| 205 | .Pp | ||
| 206 | The | ||
| 207 | .Fn res_query | ||
| 208 | function provides an interface to the server query mechanism. | ||
| 209 | It constructs a query, sends it to the local server, | ||
| 210 | awaits a response, and makes preliminary checks on the reply. | ||
| 211 | The query requests information of the specified | ||
| 212 | .Fa type | ||
| 213 | and | ||
| 214 | .Fa class | ||
| 215 | for the specified fully-qualified domain name | ||
| 216 | .Fa dname . | ||
| 217 | The reply message is left in the | ||
| 218 | .Fa answer | ||
| 219 | buffer with length | ||
| 220 | .Fa anslen | ||
| 221 | supplied by the caller. | ||
| 222 | .Pp | ||
| 223 | The | ||
| 224 | .Fn res_search | ||
| 225 | routine makes a query and awaits a response like | ||
| 226 | .Fn res_query , | ||
| 227 | but in addition, it implements the default and search rules | ||
| 228 | controlled by the | ||
| 229 | .Dv RES_DEFNAMES | ||
| 230 | and | ||
| 231 | .Dv RES_DNSRCH | ||
| 232 | options. | ||
| 233 | It returns the first successful reply. | ||
| 234 | .Pp | ||
| 235 | The remaining routines are lower-level routines used by | ||
| 236 | .Fn res_query . | ||
| 237 | The | ||
| 238 | .Fn res_mkquery | ||
| 239 | function | ||
| 240 | constructs a standard query message and places it in | ||
| 241 | .Fa buf . | ||
| 242 | It returns the size of the query, or \-1 if the query is | ||
| 243 | larger than | ||
| 244 | .Fa buflen . | ||
| 245 | The query type | ||
| 246 | .Fa op | ||
| 247 | is usually | ||
| 248 | .Dv QUERY , | ||
| 249 | but can be any of the query types defined in | ||
| 250 | .Aq Pa arpa/nameser.h . | ||
| 251 | The domain name for the query is given by | ||
| 252 | .Fa dname . | ||
| 253 | .Fa Newrr | ||
| 254 | is currently unused but is intended for making update messages. | ||
| 255 | .Pp | ||
| 256 | The | ||
| 257 | .Fn res_send | ||
| 258 | routine | ||
| 259 | sends a pre-formatted query and returns an answer. | ||
| 260 | It will call | ||
| 261 | .Fn res_init | ||
| 262 | if | ||
| 263 | .Dv RES_INIT | ||
| 264 | is not set, send the query to the local name server, and | ||
| 265 | handle timeouts and retries. | ||
| 266 | The length of the reply message is returned, or | ||
| 267 | \-1 if there were errors. | ||
| 268 | .Pp | ||
| 269 | The | ||
| 270 | .Fn dn_comp | ||
| 271 | function | ||
| 272 | compresses the domain name | ||
| 273 | .Fa exp_dn | ||
| 274 | and stores it in | ||
| 275 | .Fa comp_dn . | ||
| 276 | The size of the compressed name is returned or \-1 if there were errors. | ||
| 277 | The size of the array pointed to by | ||
| 278 | .Fa comp_dn | ||
| 279 | is given by | ||
| 280 | .Fa length . | ||
| 281 | The compression uses | ||
| 282 | an array of pointers | ||
| 283 | .Fa dnptrs | ||
| 284 | to previously-compressed names in the current message. | ||
| 285 | The first pointer points to | ||
| 286 | to the beginning of the message and the list ends with | ||
| 287 | .Dv NULL . | ||
| 288 | The limit to the array is specified by | ||
| 289 | .Fa lastdnptr . | ||
| 290 | A side effect of | ||
| 291 | .Fn dn_comp | ||
| 292 | is to update the list of pointers for | ||
| 293 | labels inserted into the message | ||
| 294 | as the name is compressed. | ||
| 295 | If | ||
| 296 | .Em dnptr | ||
| 297 | is | ||
| 298 | .Dv NULL, names are not compressed. | ||
| 299 | If | ||
| 300 | .Fa lastdnptr | ||
| 301 | is | ||
| 302 | .Dv NULL , | ||
| 303 | the list of labels is not updated. | ||
| 304 | .Pp | ||
| 305 | The | ||
| 306 | .Fn dn_expand | ||
| 307 | entry | ||
| 308 | expands the compressed domain name | ||
| 309 | .Fa comp_dn | ||
| 310 | to a full domain name | ||
| 311 | The compressed name is contained in a query or reply message; | ||
| 312 | .Fa msg | ||
| 313 | is a pointer to the beginning of the message. | ||
| 314 | The uncompressed name is placed in the buffer indicated by | ||
| 315 | .Fa exp_dn | ||
| 316 | which is of size | ||
| 317 | .Fa length . | ||
| 318 | The size of compressed name is returned or \-1 if there was an error. | ||
| 319 | .Sh FILES | ||
| 320 | .Bl -tag -width Pa | ||
| 321 | /etc/resolv.conf | ||
| 322 | The configuration file | ||
| 323 | see | ||
| 324 | .Xr resolv.conf 5 . | ||
| 325 | .El | ||
| 326 | .Sh SEE ALSO | ||
| 327 | .Xr gethostbyname 3 , | ||
| 328 | .Xr named 8 , | ||
| 329 | .Xr resolv.conf 5 , | ||
| 330 | .Xr hostname 7 , | ||
| 331 | .Pp | ||
| 332 | .%T RFC1032 , | ||
| 333 | .%T RFC1033 , | ||
| 334 | .%T RFC1034 , | ||
| 335 | .%T RFC1035 , | ||
| 336 | .%T RFC974 | ||
| 337 | .Rs | ||
| 338 | .%T "Name Server Operations Guide for BIND" | ||
| 339 | .Re | ||
| 340 | .Sh HISTORY | ||
| 341 | The | ||
| 342 | .Nm | ||
| 343 | function appeared in | ||
| 344 | .Bx 4.3 . | ||
diff --git a/src/lib/libc/net/send.c b/src/lib/libc/net/send.c new file mode 100644 index 0000000000..88f3550ec6 --- /dev/null +++ b/src/lib/libc/net/send.c | |||
| @@ -0,0 +1,56 @@ | |||
| 1 | /* $NetBSD: send.c,v 1.6 1995/02/25 06:21:02 cgd Exp $ */ | ||
| 2 | |||
| 3 | /* | ||
| 4 | * Copyright (c) 1988, 1993 | ||
| 5 | * The Regents of the University of California. All rights reserved. | ||
| 6 | * | ||
| 7 | * Redistribution and use in source and binary forms, with or without | ||
| 8 | * modification, are permitted provided that the following conditions | ||
| 9 | * are met: | ||
| 10 | * 1. Redistributions of source code must retain the above copyright | ||
| 11 | * notice, this list of conditions and the following disclaimer. | ||
| 12 | * 2. Redistributions in binary form must reproduce the above copyright | ||
| 13 | * notice, this list of conditions and the following disclaimer in the | ||
| 14 | * documentation and/or other materials provided with the distribution. | ||
| 15 | * 3. All advertising materials mentioning features or use of this software | ||
| 16 | * must display the following acknowledgement: | ||
| 17 | * This product includes software developed by the University of | ||
| 18 | * California, Berkeley and its contributors. | ||
| 19 | * 4. Neither the name of the University nor the names of its contributors | ||
| 20 | * may be used to endorse or promote products derived from this software | ||
| 21 | * without specific prior written permission. | ||
| 22 | * | ||
| 23 | * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND | ||
| 24 | * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE | ||
| 25 | * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE | ||
| 26 | * ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE | ||
| 27 | * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL | ||
| 28 | * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS | ||
| 29 | * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) | ||
| 30 | * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT | ||
| 31 | * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY | ||
| 32 | * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF | ||
| 33 | * SUCH DAMAGE. | ||
| 34 | */ | ||
| 35 | |||
| 36 | #if defined(LIBC_SCCS) && !defined(lint) | ||
| 37 | #if 0 | ||
| 38 | static char sccsid[] = "@(#)send.c 8.2 (Berkeley) 2/21/94"; | ||
| 39 | #else | ||
| 40 | static char rcsid[] = "$NetBSD: send.c,v 1.6 1995/02/25 06:21:02 cgd Exp $"; | ||
| 41 | #endif | ||
| 42 | #endif /* LIBC_SCCS and not lint */ | ||
| 43 | |||
| 44 | #include <sys/types.h> | ||
| 45 | #include <sys/socket.h> | ||
| 46 | |||
| 47 | #include <stddef.h> | ||
| 48 | |||
| 49 | ssize_t | ||
| 50 | send(s, msg, len, flags) | ||
| 51 | int s, flags; | ||
| 52 | size_t len; | ||
| 53 | const void *msg; | ||
| 54 | { | ||
| 55 | return (sendto(s, msg, len, flags, NULL, 0)); | ||
| 56 | } | ||
diff --git a/src/lib/libc/net/sethostent.c b/src/lib/libc/net/sethostent.c new file mode 100644 index 0000000000..00f6499695 --- /dev/null +++ b/src/lib/libc/net/sethostent.c | |||
| @@ -0,0 +1,62 @@ | |||
| 1 | /* $NetBSD: sethostent.c,v 1.4 1995/02/25 06:21:03 cgd Exp $ */ | ||
| 2 | |||
| 3 | /* | ||
| 4 | * Copyright (c) 1985, 1993 | ||
| 5 | * The Regents of the University of California. All rights reserved. | ||
| 6 | * | ||
| 7 | * Redistribution and use in source and binary forms, with or without | ||
| 8 | * modification, are permitted provided that the following conditions | ||
| 9 | * are met: | ||
| 10 | * 1. Redistributions of source code must retain the above copyright | ||
| 11 | * notice, this list of conditions and the following disclaimer. | ||
| 12 | * 2. Redistributions in binary form must reproduce the above copyright | ||
| 13 | * notice, this list of conditions and the following disclaimer in the | ||
| 14 | * documentation and/or other materials provided with the distribution. | ||
| 15 | * 3. All advertising materials mentioning features or use of this software | ||
| 16 | * must display the following acknowledgement: | ||
| 17 | * This product includes software developed by the University of | ||
| 18 | * California, Berkeley and its contributors. | ||
| 19 | * 4. Neither the name of the University nor the names of its contributors | ||
| 20 | * may be used to endorse or promote products derived from this software | ||
| 21 | * without specific prior written permission. | ||
| 22 | * | ||
| 23 | * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND | ||
| 24 | * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE | ||
| 25 | * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE | ||
| 26 | * ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE | ||
| 27 | * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL | ||
| 28 | * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS | ||
| 29 | * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) | ||
| 30 | * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT | ||
| 31 | * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY | ||
| 32 | * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF | ||
| 33 | * SUCH DAMAGE. | ||
| 34 | */ | ||
| 35 | |||
| 36 | #if defined(LIBC_SCCS) && !defined(lint) | ||
| 37 | #if 0 | ||
| 38 | static char sccsid[] = "@(#)sethostent.c 8.1 (Berkeley) 6/4/93"; | ||
| 39 | #else | ||
| 40 | static char rcsid[] = "$NetBSD: sethostent.c,v 1.4 1995/02/25 06:21:03 cgd Exp $"; | ||
| 41 | #endif | ||
| 42 | #endif /* LIBC_SCCS and not lint */ | ||
| 43 | |||
| 44 | #include <sys/param.h> | ||
| 45 | #include <netinet/in.h> | ||
| 46 | #include <arpa/nameser.h> | ||
| 47 | #include <netdb.h> | ||
| 48 | #include <resolv.h> | ||
| 49 | |||
| 50 | void | ||
| 51 | sethostent(stayopen) | ||
| 52 | { | ||
| 53 | if (stayopen) | ||
| 54 | _res.options |= RES_STAYOPEN | RES_USEVC; | ||
| 55 | } | ||
| 56 | |||
| 57 | void | ||
| 58 | endhostent() | ||
| 59 | { | ||
| 60 | _res.options &= ~(RES_STAYOPEN | RES_USEVC); | ||
| 61 | _res_close(); | ||
| 62 | } | ||
diff --git a/src/lib/libc/stdlib/Makefile.inc b/src/lib/libc/stdlib/Makefile.inc new file mode 100644 index 0000000000..782a4ab022 --- /dev/null +++ b/src/lib/libc/stdlib/Makefile.inc | |||
| @@ -0,0 +1,45 @@ | |||
| 1 | # from: @(#)Makefile.inc 5.6 (Berkeley) 6/4/91 | ||
| 2 | # $Id: Makefile.inc,v 1.1.1.1 1995/10/18 08:42:16 deraadt Exp $ | ||
| 3 | |||
| 4 | # stdlib sources | ||
| 5 | .PATH: ${.CURDIR}/arch/${MACHINE_ARCH}/stdlib ${.CURDIR}/stdlib | ||
| 6 | |||
| 7 | SRCS+= a64l.c abort.c atexit.c atoi.c atof.c atol.c bsearch.c calloc.c \ | ||
| 8 | exit.c getenv.c getopt.c heapsort.c l64a.c malloc.c merge.c \ | ||
| 9 | multibyte.c putenv.c qsort.c radixsort.c rand.c random.c realpath.c \ | ||
| 10 | setenv.c strtod.c strtol.c strtoq.c strtoul.c strtouq.c system.c \ | ||
| 11 | _rand48.c drand48.c erand48.c jrand48.c lcong48.c lrand48.c \ | ||
| 12 | mrand48.c nrand48.c seed48.c srand48.c qabs.c qdiv.c | ||
| 13 | |||
| 14 | .if (${MACHINE_ARCH} == "m68k") | ||
| 15 | SRCS+= abs.S div.c labs.c ldiv.c | ||
| 16 | .elif (${MACHINE_ARCH} == "i386") | ||
| 17 | SRCS+= abs.S div.S labs.S ldiv.S | ||
| 18 | .elif (${MACHINE_ARCH} == "ns32k") | ||
| 19 | SRCS+= abs.S div.c labs.c ldiv.c | ||
| 20 | .elif (${MACHINE_ARCH} == "tahoe") | ||
| 21 | SRCS+= abs.S div.c labs.c ldiv.c | ||
| 22 | .elif (${MACHINE_ARCH} == "vax") | ||
| 23 | SRCS+= abs.c div.c labs.c ldiv.c | ||
| 24 | .elif (${MACHINE_ARCH} == "alpha") | ||
| 25 | # XXX should be .S's | ||
| 26 | SRCS+= abs.c div.c labs.c ldiv.c | ||
| 27 | .else | ||
| 28 | SRCS+= abs.c div.c labs.c ldiv.c | ||
| 29 | .endif | ||
| 30 | |||
| 31 | MAN+= abort.3 abs.3 alloca.3 atexit.3 atof.3 atoi.3 atol.3 bsearch.3 \ | ||
| 32 | calloc.3 div.3 exit.3 free.3 getenv.3 getopt.3 labs.3 ldiv.3 \ | ||
| 33 | malloc.3 memory.3 qabs.3 qdiv.3 qsort.3 radixsort.3 rand48.3 \ | ||
| 34 | rand.3 random.3 realloc.3 realpath.3 strtod.3 strtol.3 strtoul.3 \ | ||
| 35 | system.3 | ||
| 36 | |||
| 37 | MLINKS+=getenv.3 setenv.3 getenv.3 unsetenv.3 getenv.3 putenv.3 | ||
| 38 | MLINKS+=qsort.3 heapsort.3 qsort.3 mergesort.3 | ||
| 39 | MLINKS+=rand.3 srand.3 | ||
| 40 | MLINKS+=strtol.3 strtoq.3 | ||
| 41 | MLINKS+=strtoul.3 strtouq.3 | ||
| 42 | MLINKS+=random.3 initstate.3 random.3 setstate.3 random.3 srandom.3 | ||
| 43 | MLINKS+=rand48.3 drand48.3 rand48.3 erand48.3 rand48.3 lrand48.3 | ||
| 44 | MLINKS+=rand48.3 mrand48.3 rand48.3 nrand48.3 rand48.3 jrand48.3 | ||
| 45 | MLINKS+=rand48.3 srand48.3 rand48.3 seed48.3 rand48.3 lcong48.3 | ||
diff --git a/src/lib/libc/stdlib/_rand48.c b/src/lib/libc/stdlib/_rand48.c new file mode 100644 index 0000000000..83ade4645a --- /dev/null +++ b/src/lib/libc/stdlib/_rand48.c | |||
| @@ -0,0 +1,46 @@ | |||
| 1 | /* | ||
| 2 | * Copyright (c) 1993 Martin Birgmeier | ||
| 3 | * All rights reserved. | ||
| 4 | * | ||
| 5 | * You may redistribute unmodified or modified versions of this source | ||
| 6 | * code provided that the above copyright notice and this and the | ||
| 7 | * following conditions are retained. | ||
| 8 | * | ||
| 9 | * This software is provided ``as is'', and comes with no warranties | ||
| 10 | * of any kind. I shall in no event be liable for anything that happens | ||
| 11 | * to anyone/anything when using this software. | ||
| 12 | */ | ||
| 13 | |||
| 14 | #include "rand48.h" | ||
| 15 | |||
| 16 | unsigned short __rand48_seed[3] = { | ||
| 17 | RAND48_SEED_0, | ||
| 18 | RAND48_SEED_1, | ||
| 19 | RAND48_SEED_2 | ||
| 20 | }; | ||
| 21 | unsigned short __rand48_mult[3] = { | ||
| 22 | RAND48_MULT_0, | ||
| 23 | RAND48_MULT_1, | ||
| 24 | RAND48_MULT_2 | ||
| 25 | }; | ||
| 26 | unsigned short __rand48_add = RAND48_ADD; | ||
| 27 | |||
| 28 | void | ||
| 29 | __dorand48(unsigned short xseed[3]) | ||
| 30 | { | ||
| 31 | unsigned long accu; | ||
| 32 | unsigned short temp[2]; | ||
| 33 | |||
| 34 | accu = (unsigned long) __rand48_mult[0] * (unsigned long) xseed[0] + | ||
| 35 | (unsigned long) __rand48_add; | ||
| 36 | temp[0] = (unsigned short) accu; /* lower 16 bits */ | ||
| 37 | accu >>= sizeof(unsigned short) * 8; | ||
| 38 | accu += (unsigned long) __rand48_mult[0] * (unsigned long) xseed[1] + | ||
| 39 | (unsigned long) __rand48_mult[1] * (unsigned long) xseed[0]; | ||
| 40 | temp[1] = (unsigned short) accu; /* middle 16 bits */ | ||
| 41 | accu >>= sizeof(unsigned short) * 8; | ||
| 42 | accu += __rand48_mult[0] * xseed[2] + __rand48_mult[1] * xseed[1] + __rand48_mult[2] * xseed[0]; | ||
| 43 | xseed[0] = temp[0]; | ||
| 44 | xseed[1] = temp[1]; | ||
| 45 | xseed[2] = (unsigned short) accu; | ||
| 46 | } | ||
diff --git a/src/lib/libc/stdlib/a64l.c b/src/lib/libc/stdlib/a64l.c new file mode 100644 index 0000000000..03fc77e034 --- /dev/null +++ b/src/lib/libc/stdlib/a64l.c | |||
| @@ -0,0 +1,34 @@ | |||
| 1 | /* | ||
| 2 | * Written by J.T. Conklin <jtc@netbsd.org>. | ||
| 3 | * Public domain. | ||
| 4 | */ | ||
| 5 | |||
| 6 | #if defined(LIBC_SCCS) && !defined(lint) | ||
| 7 | static char *rcsid = "$NetBSD: a64l.c,v 1.3 1995/05/11 23:04:47 jtc Exp $"; | ||
| 8 | #endif | ||
| 9 | |||
| 10 | long | ||
| 11 | a64l(s) | ||
| 12 | const char *s; | ||
| 13 | { | ||
| 14 | long value, digit, shift; | ||
| 15 | int i; | ||
| 16 | |||
| 17 | value = 0; | ||
| 18 | shift = 0; | ||
| 19 | for (i = 0; *s && i < 6; i++, s++) { | ||
| 20 | if (*s <= '/') | ||
| 21 | digit = *s - '.'; | ||
| 22 | else if (*s <= '9') | ||
| 23 | digit = *s - '0' + 2; | ||
| 24 | else if (*s <= 'Z') | ||
| 25 | digit = *s - 'A' + 12; | ||
| 26 | else | ||
| 27 | digit = *s - 'a' + 38; | ||
| 28 | |||
| 29 | value |= digit << shift; | ||
| 30 | shift += 6; | ||
| 31 | } | ||
| 32 | |||
| 33 | return (long) value; | ||
| 34 | } | ||
diff --git a/src/lib/libc/stdlib/abort.3 b/src/lib/libc/stdlib/abort.3 new file mode 100644 index 0000000000..ab57327585 --- /dev/null +++ b/src/lib/libc/stdlib/abort.3 | |||
| @@ -0,0 +1,71 @@ | |||
| 1 | .\" Copyright (c) 1990, 1991 The Regents of the University of California. | ||
| 2 | .\" All rights reserved. | ||
| 3 | .\" | ||
| 4 | .\" This code is derived from software contributed to Berkeley by | ||
| 5 | .\" the American National Standards Committee X3, on Information | ||
| 6 | .\" Processing Systems. | ||
| 7 | .\" | ||
| 8 | .\" Redistribution and use in source and binary forms, with or without | ||
| 9 | .\" modification, are permitted provided that the following conditions | ||
| 10 | .\" are met: | ||
| 11 | .\" 1. Redistributions of source code must retain the above copyright | ||
| 12 | .\" notice, this list of conditions and the following disclaimer. | ||
| 13 | .\" 2. Redistributions in binary form must reproduce the above copyright | ||
| 14 | .\" notice, this list of conditions and the following disclaimer in the | ||
| 15 | .\" documentation and/or other materials provided with the distribution. | ||
| 16 | .\" 3. All advertising materials mentioning features or use of this software | ||
| 17 | .\" must display the following acknowledgement: | ||
| 18 | .\" This product includes software developed by the University of | ||
| 19 | .\" California, Berkeley and its contributors. | ||
| 20 | .\" 4. Neither the name of the University nor the names of its contributors | ||
| 21 | .\" may be used to endorse or promote products derived from this software | ||
| 22 | .\" without specific prior written permission. | ||
| 23 | .\" | ||
| 24 | .\" THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND | ||
| 25 | .\" ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE | ||
| 26 | .\" IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE | ||
| 27 | .\" ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE | ||
| 28 | .\" FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL | ||
| 29 | .\" DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS | ||
| 30 | .\" OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) | ||
| 31 | .\" HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT | ||
| 32 | .\" LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY | ||
| 33 | .\" OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF | ||
| 34 | .\" SUCH DAMAGE. | ||
| 35 | .\" | ||
| 36 | .\" from: @(#)abort.3 6.7 (Berkeley) 6/29/91 | ||
| 37 | .\" $Id: abort.3,v 1.1.1.1 1995/10/18 08:42:16 deraadt Exp $ | ||
| 38 | .\" | ||
| 39 | .Dd June 29, 1991 | ||
| 40 | .Dt ABORT 3 | ||
| 41 | .Os | ||
| 42 | .Sh NAME | ||
| 43 | .Nm abort | ||
| 44 | .Nd cause abnormal program termination | ||
| 45 | .Sh SYNOPSIS | ||
| 46 | .Fd #include <stdlib.h> | ||
| 47 | .Ft void | ||
| 48 | .Fn abort void | ||
| 49 | .Sh DESCRIPTION | ||
| 50 | The | ||
| 51 | .Fn abort | ||
| 52 | function causes abnormal program termination to occur, unless the | ||
| 53 | signal | ||
| 54 | .Dv SIGABRT | ||
| 55 | is being caught and the signal handler does not return. | ||
| 56 | .Pp | ||
| 57 | No open streams are closed or flushed. | ||
| 58 | .Sh RETURN VALUES | ||
| 59 | The | ||
| 60 | .Nm abort | ||
| 61 | function | ||
| 62 | never returns. | ||
| 63 | .Sh SEE ALSO | ||
| 64 | .Xr sigaction 2 , | ||
| 65 | .Xr exit 2 | ||
| 66 | .Sh STANDARDS | ||
| 67 | The | ||
| 68 | .Fn abort | ||
| 69 | function | ||
| 70 | conforms to | ||
| 71 | .St -ansiC . | ||
diff --git a/src/lib/libc/stdlib/abort.c b/src/lib/libc/stdlib/abort.c new file mode 100644 index 0000000000..c298e016b4 --- /dev/null +++ b/src/lib/libc/stdlib/abort.c | |||
| @@ -0,0 +1,65 @@ | |||
| 1 | /* | ||
| 2 | * Copyright (c) 1985 Regents of the University of California. | ||
| 3 | * All rights reserved. | ||
| 4 | * | ||
| 5 | * Redistribution and use in source and binary forms, with or without | ||
| 6 | * modification, are permitted provided that the following conditions | ||
| 7 | * are met: | ||
| 8 | * 1. Redistributions of source code must retain the above copyright | ||
| 9 | * notice, this list of conditions and the following disclaimer. | ||
| 10 | * 2. Redistributions in binary form must reproduce the above copyright | ||
| 11 | * notice, this list of conditions and the following disclaimer in the | ||
| 12 | * documentation and/or other materials provided with the distribution. | ||
| 13 | * 3. All advertising materials mentioning features or use of this software | ||
| 14 | * must display the following acknowledgement: | ||
| 15 | * This product includes software developed by the University of | ||
| 16 | * California, Berkeley and its contributors. | ||
| 17 | * 4. Neither the name of the University nor the names of its contributors | ||
| 18 | * may be used to endorse or promote products derived from this software | ||
| 19 | * without specific prior written permission. | ||
| 20 | * | ||
| 21 | * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND | ||
| 22 | * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE | ||
| 23 | * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE | ||
| 24 | * ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE | ||
| 25 | * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL | ||
| 26 | * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS | ||
| 27 | * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) | ||
| 28 | * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT | ||
| 29 | * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY | ||
| 30 | * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF | ||
| 31 | * SUCH DAMAGE. | ||
| 32 | */ | ||
| 33 | |||
| 34 | #if defined(LIBC_SCCS) && !defined(lint) | ||
| 35 | /*static char *sccsid = "from: @(#)abort.c 5.11 (Berkeley) 2/23/91";*/ | ||
| 36 | static char *rcsid = "$Id: abort.c,v 1.1.1.1 1995/10/18 08:42:16 deraadt Exp $"; | ||
| 37 | #endif /* LIBC_SCCS and not lint */ | ||
| 38 | |||
| 39 | #include <signal.h> | ||
| 40 | #include <stdlib.h> | ||
| 41 | #include <unistd.h> | ||
| 42 | |||
| 43 | void | ||
| 44 | abort() | ||
| 45 | { | ||
| 46 | sigset_t mask; | ||
| 47 | |||
| 48 | sigfillset(&mask); | ||
| 49 | /* | ||
| 50 | * don't block SIGABRT to give any handler a chance; we ignore | ||
| 51 | * any errors -- X311J doesn't allow abort to return anyway. | ||
| 52 | */ | ||
| 53 | sigdelset(&mask, SIGABRT); | ||
| 54 | (void)sigprocmask(SIG_SETMASK, &mask, (sigset_t *)NULL); | ||
| 55 | (void)kill(getpid(), SIGABRT); | ||
| 56 | |||
| 57 | /* | ||
| 58 | * if SIGABRT ignored, or caught and the handler returns, do | ||
| 59 | * it again, only harder. | ||
| 60 | */ | ||
| 61 | (void)signal(SIGABRT, SIG_DFL); | ||
| 62 | (void)sigprocmask(SIG_SETMASK, &mask, (sigset_t *)NULL); | ||
| 63 | (void)kill(getpid(), SIGABRT); | ||
| 64 | exit(1); | ||
| 65 | } | ||
diff --git a/src/lib/libc/stdlib/abs.3 b/src/lib/libc/stdlib/abs.3 new file mode 100644 index 0000000000..4748d89e77 --- /dev/null +++ b/src/lib/libc/stdlib/abs.3 | |||
| @@ -0,0 +1,74 @@ | |||
| 1 | .\" Copyright (c) 1990, 1991 The Regents of the University of California. | ||
| 2 | .\" All rights reserved. | ||
| 3 | .\" | ||
| 4 | .\" This code is derived from software contributed to Berkeley by | ||
| 5 | .\" the American National Standards Committee X3, on Information | ||
| 6 | .\" Processing Systems. | ||
| 7 | .\" | ||
| 8 | .\" Redistribution and use in source and binary forms, with or without | ||
| 9 | .\" modification, are permitted provided that the following conditions | ||
| 10 | .\" are met: | ||
| 11 | .\" 1. Redistributions of source code must retain the above copyright | ||
| 12 | .\" notice, this list of conditions and the following disclaimer. | ||
| 13 | .\" 2. Redistributions in binary form must reproduce the above copyright | ||
| 14 | .\" notice, this list of conditions and the following disclaimer in the | ||
| 15 | .\" documentation and/or other materials provided with the distribution. | ||
| 16 | .\" 3. All advertising materials mentioning features or use of this software | ||
| 17 | .\" must display the following acknowledgement: | ||
| 18 | .\" This product includes software developed by the University of | ||
| 19 | .\" California, Berkeley and its contributors. | ||
| 20 | .\" 4. Neither the name of the University nor the names of its contributors | ||
| 21 | .\" may be used to endorse or promote products derived from this software | ||
| 22 | .\" without specific prior written permission. | ||
| 23 | .\" | ||
| 24 | .\" THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND | ||
| 25 | .\" ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE | ||
| 26 | .\" IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE | ||
| 27 | .\" ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE | ||
| 28 | .\" FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL | ||
| 29 | .\" DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS | ||
| 30 | .\" OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) | ||
| 31 | .\" HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT | ||
| 32 | .\" LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY | ||
| 33 | .\" OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF | ||
| 34 | .\" SUCH DAMAGE. | ||
| 35 | .\" | ||
| 36 | .\" from: @(#)abs.3 6.4 (Berkeley) 6/29/91 | ||
| 37 | .\" $Id: abs.3,v 1.1.1.1 1995/10/18 08:42:16 deraadt Exp $ | ||
| 38 | .\" | ||
| 39 | .Dd June 29, 1991 | ||
| 40 | .Dt ABS 3 | ||
| 41 | .Os | ||
| 42 | .Sh NAME | ||
| 43 | .Nm abs | ||
| 44 | .Nd integer absolute value function | ||
| 45 | .Sh SYNOPSIS | ||
| 46 | .Fd #include <stdlib.h> | ||
| 47 | .Ft int | ||
| 48 | .Fn abs "int j" | ||
| 49 | .Sh DESCRIPTION | ||
| 50 | The | ||
| 51 | .Fn abs | ||
| 52 | function | ||
| 53 | computes | ||
| 54 | the absolute value of the integer | ||
| 55 | .Ar j . | ||
| 56 | .Sh RETURN VALUES | ||
| 57 | The | ||
| 58 | .Fn abs | ||
| 59 | function | ||
| 60 | returns | ||
| 61 | the absolute value. | ||
| 62 | .Sh SEE ALSO | ||
| 63 | .Xr floor 3 , | ||
| 64 | .Xr labs 3 , | ||
| 65 | .Xr cabs 3 , | ||
| 66 | .Xr hypot 3 , | ||
| 67 | .Xr math 3 | ||
| 68 | .Sh STANDARDS | ||
| 69 | The | ||
| 70 | .Fn abs | ||
| 71 | function conforms to | ||
| 72 | .St -ansiC . | ||
| 73 | .Sh BUGS | ||
| 74 | The absolute value of the most negative integer remains negative. | ||
diff --git a/src/lib/libc/stdlib/abs.c b/src/lib/libc/stdlib/abs.c new file mode 100644 index 0000000000..64468e0224 --- /dev/null +++ b/src/lib/libc/stdlib/abs.c | |||
| @@ -0,0 +1,46 @@ | |||
| 1 | /*- | ||
| 2 | * Copyright (c) 1990 The Regents of the University of California. | ||
| 3 | * All rights reserved. | ||
| 4 | * | ||
| 5 | * Redistribution and use in source and binary forms, with or without | ||
| 6 | * modification, are permitted provided that the following conditions | ||
| 7 | * are met: | ||
| 8 | * 1. Redistributions of source code must retain the above copyright | ||
| 9 | * notice, this list of conditions and the following disclaimer. | ||
| 10 | * 2. Redistributions in binary form must reproduce the above copyright | ||
| 11 | * notice, this list of conditions and the following disclaimer in the | ||
| 12 | * documentation and/or other materials provided with the distribution. | ||
| 13 | * 3. All advertising materials mentioning features or use of this software | ||
| 14 | * must display the following acknowledgement: | ||
| 15 | * This product includes software developed by the University of | ||
| 16 | * California, Berkeley and its contributors. | ||
| 17 | * 4. Neither the name of the University nor the names of its contributors | ||
| 18 | * may be used to endorse or promote products derived from this software | ||
| 19 | * without specific prior written permission. | ||
| 20 | * | ||
| 21 | * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND | ||
| 22 | * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE | ||
| 23 | * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE | ||
| 24 | * ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE | ||
| 25 | * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL | ||
| 26 | * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS | ||
| 27 | * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) | ||
| 28 | * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT | ||
| 29 | * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY | ||
| 30 | * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF | ||
| 31 | * SUCH DAMAGE. | ||
| 32 | */ | ||
| 33 | |||
| 34 | #if defined(LIBC_SCCS) && !defined(lint) | ||
| 35 | /*static char *sccsid = "from: @(#)abs.c 5.2 (Berkeley) 5/17/90";*/ | ||
| 36 | static char *rcsid = "$Id: abs.c,v 1.1.1.1 1995/10/18 08:42:16 deraadt Exp $"; | ||
| 37 | #endif /* LIBC_SCCS and not lint */ | ||
| 38 | |||
| 39 | #include <stdlib.h> | ||
| 40 | |||
| 41 | int | ||
| 42 | abs(j) | ||
| 43 | int j; | ||
| 44 | { | ||
| 45 | return(j < 0 ? -j : j); | ||
| 46 | } | ||
diff --git a/src/lib/libc/stdlib/alloca.3 b/src/lib/libc/stdlib/alloca.3 new file mode 100644 index 0000000000..dcb97ab11c --- /dev/null +++ b/src/lib/libc/stdlib/alloca.3 | |||
| @@ -0,0 +1,79 @@ | |||
| 1 | .\" Copyright (c) 1980, 1991 Regents of the University of California. | ||
| 2 | .\" All rights reserved. | ||
| 3 | .\" | ||
| 4 | .\" Redistribution and use in source and binary forms, with or without | ||
| 5 | .\" modification, are permitted provided that the following conditions | ||
| 6 | .\" are met: | ||
| 7 | .\" 1. Redistributions of source code must retain the above copyright | ||
| 8 | .\" notice, this list of conditions and the following disclaimer. | ||
| 9 | .\" 2. Redistributions in binary form must reproduce the above copyright | ||
| 10 | .\" notice, this list of conditions and the following disclaimer in the | ||
| 11 | .\" documentation and/or other materials provided with the distribution. | ||
| 12 | .\" 3. All advertising materials mentioning features or use of this software | ||
| 13 | .\" must display the following acknowledgement: | ||
| 14 | .\" This product includes software developed by the University of | ||
| 15 | .\" California, Berkeley and its contributors. | ||
| 16 | .\" 4. Neither the name of the University nor the names of its contributors | ||
| 17 | .\" may be used to endorse or promote products derived from this software | ||
| 18 | .\" without specific prior written permission. | ||
| 19 | .\" | ||
| 20 | .\" THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND | ||
| 21 | .\" ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE | ||
| 22 | .\" IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE | ||
| 23 | .\" ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE | ||
| 24 | .\" FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL | ||
| 25 | .\" DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS | ||
| 26 | .\" OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) | ||
| 27 | .\" HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT | ||
| 28 | .\" LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY | ||
| 29 | .\" OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF | ||
| 30 | .\" SUCH DAMAGE. | ||
| 31 | .\" | ||
| 32 | .\" from: @(#)alloca.3 5.1 (Berkeley) 5/2/91 | ||
| 33 | .\" $Id: alloca.3,v 1.1.1.1 1995/10/18 08:42:16 deraadt Exp $ | ||
| 34 | .\" | ||
| 35 | .Dd May 2, 1991 | ||
| 36 | .Dt ALLOCA 3 | ||
| 37 | .Os BSD 4 | ||
| 38 | .Sh NAME | ||
| 39 | .Nm alloca | ||
| 40 | .Nd memory allocator | ||
| 41 | .Sh SYNOPSIS | ||
| 42 | .Fd #include <stdlib.h> | ||
| 43 | .Ft void * | ||
| 44 | .Fn alloca "size_t size" | ||
| 45 | .Sh DESCRIPTION | ||
| 46 | The | ||
| 47 | .Fn alloca | ||
| 48 | function | ||
| 49 | allocates | ||
| 50 | .Fa size | ||
| 51 | bytes of space in the stack frame of the caller. | ||
| 52 | This temporary space is automatically freed on | ||
| 53 | return. | ||
| 54 | .Sh RETURN VALUES | ||
| 55 | The | ||
| 56 | .Fn alloca | ||
| 57 | function returns a pointer to the beginning of the allocated space. | ||
| 58 | If the allocation failed, a | ||
| 59 | .Dv NULL | ||
| 60 | pointer is returned. | ||
| 61 | .Sh SEE ALSO | ||
| 62 | .Xr brk 2 , | ||
| 63 | .Xr pagesize 2 | ||
| 64 | .Xr calloc 3 , | ||
| 65 | .Xr malloc 3 , | ||
| 66 | .Xr realloc 3 , | ||
| 67 | .Sh BUGS | ||
| 68 | The | ||
| 69 | .Fn alloca | ||
| 70 | function | ||
| 71 | is machine dependent; its use is discouraged. | ||
| 72 | .\" .Sh HISTORY | ||
| 73 | .\" The | ||
| 74 | .\" .Fn alloca | ||
| 75 | .\" function appeared in | ||
| 76 | .\" .Bx ?? . | ||
| 77 | .\" The function appeared in 32v, pwb and pwb.2 and in 3bsd 4bsd | ||
| 78 | .\" The first man page (or link to a man page that I can find at the | ||
| 79 | .\" moment is 4.3... | ||
diff --git a/src/lib/libc/stdlib/atexit.3 b/src/lib/libc/stdlib/atexit.3 new file mode 100644 index 0000000000..07de054d3c --- /dev/null +++ b/src/lib/libc/stdlib/atexit.3 | |||
| @@ -0,0 +1,78 @@ | |||
| 1 | .\" Copyright (c) 1990, 1991 The Regents of the University of California. | ||
| 2 | .\" All rights reserved. | ||
| 3 | .\" | ||
| 4 | .\" This code is derived from software contributed to Berkeley by | ||
| 5 | .\" Chris Torek and the American National Standards Committee X3, | ||
| 6 | .\" on Information Processing Systems. | ||
| 7 | .\" | ||
| 8 | .\" Redistribution and use in source and binary forms, with or without | ||
| 9 | .\" modification, are permitted provided that the following conditions | ||
| 10 | .\" are met: | ||
| 11 | .\" 1. Redistributions of source code must retain the above copyright | ||
| 12 | .\" notice, this list of conditions and the following disclaimer. | ||
| 13 | .\" 2. Redistributions in binary form must reproduce the above copyright | ||
| 14 | .\" notice, this list of conditions and the following disclaimer in the | ||
| 15 | .\" documentation and/or other materials provided with the distribution. | ||
| 16 | .\" 3. All advertising materials mentioning features or use of this software | ||
| 17 | .\" must display the following acknowledgement: | ||
| 18 | .\" This product includes software developed by the University of | ||
| 19 | .\" California, Berkeley and its contributors. | ||
| 20 | .\" 4. Neither the name of the University nor the names of its contributors | ||
| 21 | .\" may be used to endorse or promote products derived from this software | ||
| 22 | .\" without specific prior written permission. | ||
| 23 | .\" | ||
| 24 | .\" THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND | ||
| 25 | .\" ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE | ||
| 26 | .\" IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE | ||
| 27 | .\" ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE | ||
| 28 | .\" FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL | ||
| 29 | .\" DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS | ||
| 30 | .\" OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) | ||
| 31 | .\" HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT | ||
| 32 | .\" LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY | ||
| 33 | .\" OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF | ||
| 34 | .\" SUCH DAMAGE. | ||
| 35 | .\" | ||
| 36 | .\" from: @(#)atexit.3 5.3 (Berkeley) 6/29/91 | ||
| 37 | .\" $Id: atexit.3,v 1.1.1.1 1995/10/18 08:42:16 deraadt Exp $ | ||
| 38 | .\" | ||
| 39 | .Dd June 29, 1991 | ||
| 40 | .Dt ATEXIT 3 | ||
| 41 | .Os | ||
| 42 | .Sh NAME | ||
| 43 | .Nm atexit | ||
| 44 | .Nd register a function to be called on exit | ||
| 45 | .Sh SYNOPSIS | ||
| 46 | .Fd #include <stdlib.h> | ||
| 47 | .Ft int | ||
| 48 | .Fn atexit "void (*function)(void)" | ||
| 49 | .Sh DESCRIPTION | ||
| 50 | The | ||
| 51 | .Fn atexit | ||
| 52 | function | ||
| 53 | registers the given | ||
| 54 | .Ar function | ||
| 55 | to be called at program exit, whether via | ||
| 56 | .Xr exit 3 | ||
| 57 | or via return from the program's | ||
| 58 | .Em main . | ||
| 59 | Functions so registered are called in reverse order; | ||
| 60 | no arguments are passed. | ||
| 61 | At least 32 functions can always be registered, | ||
| 62 | and more are allowed as long as sufficient memory can be allocated. | ||
| 63 | .Sh RETURN VALUES | ||
| 64 | .Rv -std atexit | ||
| 65 | .Sh ERRORS | ||
| 66 | .Bl -tag -width Er | ||
| 67 | .It Bq Er ENOMEM | ||
| 68 | No memory was available to add the function to the list. | ||
| 69 | The existing list of functions is unmodified. | ||
| 70 | .El | ||
| 71 | .Sh SEE ALSO | ||
| 72 | .Xr exit 3 | ||
| 73 | .Sh STANDARDS | ||
| 74 | The | ||
| 75 | .Fn atexit | ||
| 76 | function | ||
| 77 | conforms to | ||
| 78 | .St -ansiC . | ||
diff --git a/src/lib/libc/stdlib/atexit.c b/src/lib/libc/stdlib/atexit.c new file mode 100644 index 0000000000..4da1eb0d9c --- /dev/null +++ b/src/lib/libc/stdlib/atexit.c | |||
| @@ -0,0 +1,68 @@ | |||
| 1 | /*- | ||
| 2 | * Copyright (c) 1990 The Regents of the University of California. | ||
| 3 | * All rights reserved. | ||
| 4 | * | ||
| 5 | * This code is derived from software contributed to Berkeley by | ||
| 6 | * Chris Torek. | ||
| 7 | * | ||
| 8 | * Redistribution and use in source and binary forms, with or without | ||
| 9 | * modification, are permitted provided that the following conditions | ||
| 10 | * are met: | ||
| 11 | * 1. Redistributions of source code must retain the above copyright | ||
| 12 | * notice, this list of conditions and the following disclaimer. | ||
| 13 | * 2. Redistributions in binary form must reproduce the above copyright | ||
| 14 | * notice, this list of conditions and the following disclaimer in the | ||
| 15 | * documentation and/or other materials provided with the distribution. | ||
| 16 | * 3. All advertising materials mentioning features or use of this software | ||
| 17 | * must display the following acknowledgement: | ||
| 18 | * This product includes software developed by the University of | ||
| 19 | * California, Berkeley and its contributors. | ||
| 20 | * 4. Neither the name of the University nor the names of its contributors | ||
| 21 | * may be used to endorse or promote products derived from this software | ||
| 22 | * without specific prior written permission. | ||
| 23 | * | ||
| 24 | * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND | ||
| 25 | * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE | ||
| 26 | * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE | ||
| 27 | * ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE | ||
| 28 | * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL | ||
| 29 | * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS | ||
| 30 | * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) | ||
| 31 | * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT | ||
| 32 | * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY | ||
| 33 | * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF | ||
| 34 | * SUCH DAMAGE. | ||
| 35 | */ | ||
| 36 | |||
| 37 | #if defined(LIBC_SCCS) && !defined(lint) | ||
| 38 | /*static char *sccsid = "from: @(#)atexit.c 5.2 (Berkeley) 11/14/90";*/ | ||
| 39 | static char *rcsid = "$Id: atexit.c,v 1.1.1.1 1995/10/18 08:42:16 deraadt Exp $"; | ||
| 40 | #endif /* LIBC_SCCS and not lint */ | ||
| 41 | |||
| 42 | #include <stdlib.h> | ||
| 43 | #include "atexit.h" | ||
| 44 | |||
| 45 | struct atexit *__atexit; | ||
| 46 | |||
| 47 | /* | ||
| 48 | * Register a function to be performed at exit. | ||
| 49 | */ | ||
| 50 | int | ||
| 51 | atexit(fn) | ||
| 52 | void (*fn)(); | ||
| 53 | { | ||
| 54 | static struct atexit __atexit0; /* one guaranteed table */ | ||
| 55 | register struct atexit *p; | ||
| 56 | |||
| 57 | if ((p = __atexit) == NULL) | ||
| 58 | __atexit = p = &__atexit0; | ||
| 59 | else if (p->ind >= ATEXIT_SIZE) { | ||
| 60 | if ((p = malloc(sizeof(*p))) == NULL) | ||
| 61 | return (-1); | ||
| 62 | p->ind = 0; | ||
| 63 | p->next = __atexit; | ||
| 64 | __atexit = p; | ||
| 65 | } | ||
| 66 | p->fns[p->ind++] = fn; | ||
| 67 | return (0); | ||
| 68 | } | ||
diff --git a/src/lib/libc/stdlib/atexit.h b/src/lib/libc/stdlib/atexit.h new file mode 100644 index 0000000000..8b756e8fe2 --- /dev/null +++ b/src/lib/libc/stdlib/atexit.h | |||
| @@ -0,0 +1,46 @@ | |||
| 1 | /*- | ||
| 2 | * Copyright (c) 1990 The Regents of the University of California. | ||
| 3 | * All rights reserved. | ||
| 4 | * | ||
| 5 | * Redistribution and use in source and binary forms, with or without | ||
| 6 | * modification, are permitted provided that the following conditions | ||
| 7 | * are met: | ||
| 8 | * 1. Redistributions of source code must retain the above copyright | ||
| 9 | * notice, this list of conditions and the following disclaimer. | ||
| 10 | * 2. Redistributions in binary form must reproduce the above copyright | ||
| 11 | * notice, this list of conditions and the following disclaimer in the | ||
| 12 | * documentation and/or other materials provided with the distribution. | ||
| 13 | * 3. All advertising materials mentioning features or use of this software | ||
| 14 | * must display the following acknowledgement: | ||
| 15 | * This product includes software developed by the University of | ||
| 16 | * California, Berkeley and its contributors. | ||
| 17 | * 4. Neither the name of the University nor the names of its contributors | ||
| 18 | * may be used to endorse or promote products derived from this software | ||
| 19 | * without specific prior written permission. | ||
| 20 | * | ||
| 21 | * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND | ||
| 22 | * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE | ||
| 23 | * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE | ||
| 24 | * ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE | ||
| 25 | * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL | ||
| 26 | * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS | ||
| 27 | * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) | ||
| 28 | * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT | ||
| 29 | * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY | ||
| 30 | * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF | ||
| 31 | * SUCH DAMAGE. | ||
| 32 | * | ||
| 33 | * from: @(#)atexit.h 5.1 (Berkeley) 5/15/90 | ||
| 34 | * $Id: atexit.h,v 1.1.1.1 1995/10/18 08:42:16 deraadt Exp $ | ||
| 35 | */ | ||
| 36 | |||
| 37 | /* must be at least 32 to guarantee ANSI conformance */ | ||
| 38 | #define ATEXIT_SIZE 32 | ||
| 39 | |||
| 40 | struct atexit { | ||
| 41 | struct atexit *next; /* next in list */ | ||
| 42 | int ind; /* next index in this table */ | ||
| 43 | void (*fns[ATEXIT_SIZE])(); /* the table itself */ | ||
| 44 | }; | ||
| 45 | |||
| 46 | extern struct atexit *__atexit; /* points to head of LIFO stack */ | ||
diff --git a/src/lib/libc/stdlib/atof.3 b/src/lib/libc/stdlib/atof.3 new file mode 100644 index 0000000000..53e04f71c5 --- /dev/null +++ b/src/lib/libc/stdlib/atof.3 | |||
| @@ -0,0 +1,74 @@ | |||
| 1 | .\" Copyright (c) 1991 The Regents of the University of California. | ||
| 2 | .\" All rights reserved. | ||
| 3 | .\" | ||
| 4 | .\" This code is derived from software contributed to Berkeley by | ||
| 5 | .\" the American National Standards Committee X3, on Information | ||
| 6 | .\" Processing Systems. | ||
| 7 | .\" | ||
| 8 | .\" Redistribution and use in source and binary forms, with or without | ||
| 9 | .\" modification, are permitted provided that the following conditions | ||
| 10 | .\" are met: | ||
| 11 | .\" 1. Redistributions of source code must retain the above copyright | ||
| 12 | .\" notice, this list of conditions and the following disclaimer. | ||
| 13 | .\" 2. Redistributions in binary form must reproduce the above copyright | ||
| 14 | .\" notice, this list of conditions and the following disclaimer in the | ||
| 15 | .\" documentation and/or other materials provided with the distribution. | ||
| 16 | .\" 3. All advertising materials mentioning features or use of this software | ||
| 17 | .\" must display the following acknowledgement: | ||
| 18 | .\" This product includes software developed by the University of | ||
| 19 | .\" California, Berkeley and its contributors. | ||
| 20 | .\" 4. Neither the name of the University nor the names of its contributors | ||
| 21 | .\" may be used to endorse or promote products derived from this software | ||
| 22 | .\" without specific prior written permission. | ||
| 23 | .\" | ||
| 24 | .\" THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND | ||
| 25 | .\" ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE | ||
| 26 | .\" IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE | ||
| 27 | .\" ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE | ||
| 28 | .\" FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL | ||
| 29 | .\" DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS | ||
| 30 | .\" OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) | ||
| 31 | .\" HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT | ||
| 32 | .\" LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY | ||
| 33 | .\" OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF | ||
| 34 | .\" SUCH DAMAGE. | ||
| 35 | .\" | ||
| 36 | .\" from: @(#)atof.3 6.4 (Berkeley) 6/29/91 | ||
| 37 | .\" $Id: atof.3,v 1.1.1.1 1995/10/18 08:42:16 deraadt Exp $ | ||
| 38 | .\" | ||
| 39 | .Dd June 29, 1991 | ||
| 40 | .Dt ATOF 3 | ||
| 41 | .Os | ||
| 42 | .Sh NAME | ||
| 43 | .Nm atof | ||
| 44 | .Nd convert | ||
| 45 | .Tn ASCII | ||
| 46 | string to double | ||
| 47 | .Sh SYNOPSIS | ||
| 48 | .Fd #include <stdlib.h> | ||
| 49 | .Ft double | ||
| 50 | .Fn atof "const char *nptr" | ||
| 51 | .Sh DESCRIPTION | ||
| 52 | The | ||
| 53 | .Fn atof | ||
| 54 | function converts the initial portion of the string pointed to by | ||
| 55 | .Ar nptr | ||
| 56 | to | ||
| 57 | .Ar double | ||
| 58 | representation. | ||
| 59 | .Pp | ||
| 60 | It is equivalent to: | ||
| 61 | .Bd -literal -offset indent | ||
| 62 | strtod(nptr, (char **)NULL); | ||
| 63 | .Ed | ||
| 64 | .Sh SEE ALSO | ||
| 65 | .Xr atoi 3 , | ||
| 66 | .Xr atol 3 , | ||
| 67 | .Xr strtod 3 , | ||
| 68 | .Xr strtol 3 , | ||
| 69 | .Xr strtoul 3 | ||
| 70 | .Sh STANDARDS | ||
| 71 | The | ||
| 72 | .Fn atof | ||
| 73 | function conforms to | ||
| 74 | .St -ansiC . | ||
diff --git a/src/lib/libc/stdlib/atof.c b/src/lib/libc/stdlib/atof.c new file mode 100644 index 0000000000..9202de50bb --- /dev/null +++ b/src/lib/libc/stdlib/atof.c | |||
| @@ -0,0 +1,46 @@ | |||
| 1 | /* | ||
| 2 | * Copyright (c) 1988 The Regents of the University of California. | ||
| 3 | * All rights reserved. | ||
| 4 | * | ||
| 5 | * Redistribution and use in source and binary forms, with or without | ||
| 6 | * modification, are permitted provided that the following conditions | ||
| 7 | * are met: | ||
| 8 | * 1. Redistributions of source code must retain the above copyright | ||
| 9 | * notice, this list of conditions and the following disclaimer. | ||
| 10 | * 2. Redistributions in binary form must reproduce the above copyright | ||
| 11 | * notice, this list of conditions and the following disclaimer in the | ||
| 12 | * documentation and/or other materials provided with the distribution. | ||
| 13 | * 3. All advertising materials mentioning features or use of this software | ||
| 14 | * must display the following acknowledgement: | ||
| 15 | * This product includes software developed by the University of | ||
| 16 | * California, Berkeley and its contributors. | ||
| 17 | * 4. Neither the name of the University nor the names of its contributors | ||
| 18 | * may be used to endorse or promote products derived from this software | ||
| 19 | * without specific prior written permission. | ||
| 20 | * | ||
| 21 | * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND | ||
| 22 | * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE | ||
| 23 | * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE | ||
| 24 | * ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE | ||
| 25 | * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL | ||
| 26 | * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS | ||
| 27 | * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) | ||
| 28 | * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT | ||
| 29 | * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY | ||
| 30 | * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF | ||
| 31 | * SUCH DAMAGE. | ||
| 32 | */ | ||
| 33 | |||
| 34 | #if defined(LIBC_SCCS) && !defined(lint) | ||
| 35 | /*static char *sccsid = "from: @(#)atof.c 5.3 (Berkeley) 1/8/93";*/ | ||
| 36 | static char *rcsid = "$Id: atof.c,v 1.1.1.1 1995/10/18 08:42:16 deraadt Exp $"; | ||
| 37 | #endif /* LIBC_SCCS and not lint */ | ||
| 38 | |||
| 39 | #include <stdlib.h> | ||
| 40 | |||
| 41 | double | ||
| 42 | atof(ascii) | ||
| 43 | const char *ascii; | ||
| 44 | { | ||
| 45 | return(strtod(ascii, (char **)NULL)); | ||
| 46 | } | ||
diff --git a/src/lib/libc/stdlib/atoi.3 b/src/lib/libc/stdlib/atoi.3 new file mode 100644 index 0000000000..219ba73c00 --- /dev/null +++ b/src/lib/libc/stdlib/atoi.3 | |||
| @@ -0,0 +1,75 @@ | |||
| 1 | .\" Copyright (c) 1990, 1991 The Regents of the University of California. | ||
| 2 | .\" All rights reserved. | ||
| 3 | .\" | ||
| 4 | .\" This code is derived from software contributed to Berkeley by | ||
| 5 | .\" the American National Standards Committee X3, on Information | ||
| 6 | .\" Processing Systems. | ||
| 7 | .\" | ||
| 8 | .\" Redistribution and use in source and binary forms, with or without | ||
| 9 | .\" modification, are permitted provided that the following conditions | ||
| 10 | .\" are met: | ||
| 11 | .\" 1. Redistributions of source code must retain the above copyright | ||
| 12 | .\" notice, this list of conditions and the following disclaimer. | ||
| 13 | .\" 2. Redistributions in binary form must reproduce the above copyright | ||
| 14 | .\" notice, this list of conditions and the following disclaimer in the | ||
| 15 | .\" documentation and/or other materials provided with the distribution. | ||
| 16 | .\" 3. All advertising materials mentioning features or use of this software | ||
| 17 | .\" must display the following acknowledgement: | ||
| 18 | .\" This product includes software developed by the University of | ||
| 19 | .\" California, Berkeley and its contributors. | ||
| 20 | .\" 4. Neither the name of the University nor the names of its contributors | ||
| 21 | .\" may be used to endorse or promote products derived from this software | ||
| 22 | .\" without specific prior written permission. | ||
| 23 | .\" | ||
| 24 | .\" THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND | ||
| 25 | .\" ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE | ||
| 26 | .\" IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE | ||
| 27 | .\" ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE | ||
| 28 | .\" FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL | ||
| 29 | .\" DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS | ||
| 30 | .\" OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) | ||
| 31 | .\" HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT | ||
| 32 | .\" LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY | ||
| 33 | .\" OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF | ||
| 34 | .\" SUCH DAMAGE. | ||
| 35 | .\" | ||
| 36 | .\" from: @(#)atoi.3 5.3 (Berkeley) 6/29/91 | ||
| 37 | .\" $Id: atoi.3,v 1.1.1.1 1995/10/18 08:42:16 deraadt Exp $ | ||
| 38 | .\" | ||
| 39 | .Dd June 29, 1991 | ||
| 40 | .Dt ATOI 3 | ||
| 41 | .Os | ||
| 42 | .Sh NAME | ||
| 43 | .Nm atoi | ||
| 44 | .Nd convert | ||
| 45 | .Tn ASCII | ||
| 46 | string to integer | ||
| 47 | .Sh SYNOPSIS | ||
| 48 | .Fd #include <stdlib.h> | ||
| 49 | .Ft int | ||
| 50 | .Fn atoi "const char *nptr" | ||
| 51 | .Sh DESCRIPTION | ||
| 52 | The | ||
| 53 | .Fn atoi | ||
| 54 | function converts the initial portion of the string pointed to by | ||
| 55 | .Em nptr | ||
| 56 | to | ||
| 57 | .Em integer | ||
| 58 | representation. | ||
| 59 | .Pp | ||
| 60 | It is equivalent to: | ||
| 61 | .Pp | ||
| 62 | .Bd -literal -offset indent | ||
| 63 | (int)strtol(nptr, (char **)NULL, 10); | ||
| 64 | .Ed | ||
| 65 | .Sh SEE ALSO | ||
| 66 | .Xr atof 3 , | ||
| 67 | .Xr atol 3 , | ||
| 68 | .Xr strtod 3 , | ||
| 69 | .Xr strtol 3 , | ||
| 70 | .Xr strtoul 3 | ||
| 71 | .Sh STANDARDS | ||
| 72 | The | ||
| 73 | .Fn atoi | ||
| 74 | function conforms to | ||
| 75 | .St -ansiC . | ||
diff --git a/src/lib/libc/stdlib/atoi.c b/src/lib/libc/stdlib/atoi.c new file mode 100644 index 0000000000..df7845f90c --- /dev/null +++ b/src/lib/libc/stdlib/atoi.c | |||
| @@ -0,0 +1,46 @@ | |||
| 1 | /* | ||
| 2 | * Copyright (c) 1988 Regents of the University of California. | ||
| 3 | * All rights reserved. | ||
| 4 | * | ||
| 5 | * Redistribution and use in source and binary forms, with or without | ||
| 6 | * modification, are permitted provided that the following conditions | ||
| 7 | * are met: | ||
| 8 | * 1. Redistributions of source code must retain the above copyright | ||
| 9 | * notice, this list of conditions and the following disclaimer. | ||
| 10 | * 2. Redistributions in binary form must reproduce the above copyright | ||
| 11 | * notice, this list of conditions and the following disclaimer in the | ||
| 12 | * documentation and/or other materials provided with the distribution. | ||
| 13 | * 3. All advertising materials mentioning features or use of this software | ||
| 14 | * must display the following acknowledgement: | ||
| 15 | * This product includes software developed by the University of | ||
| 16 | * California, Berkeley and its contributors. | ||
| 17 | * 4. Neither the name of the University nor the names of its contributors | ||
| 18 | * may be used to endorse or promote products derived from this software | ||
| 19 | * without specific prior written permission. | ||
| 20 | * | ||
| 21 | * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND | ||
| 22 | * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE | ||
| 23 | * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE | ||
| 24 | * ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE | ||
| 25 | * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL | ||
| 26 | * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS | ||
| 27 | * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) | ||
| 28 | * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT | ||
| 29 | * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY | ||
| 30 | * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF | ||
| 31 | * SUCH DAMAGE. | ||
| 32 | */ | ||
| 33 | |||
| 34 | #if defined(LIBC_SCCS) && !defined(lint) | ||
| 35 | /*static char *sccsid = "from: @(#)atoi.c 5.7 (Berkeley) 2/23/91";*/ | ||
| 36 | static char *rcsid = "$Id: atoi.c,v 1.1.1.1 1995/10/18 08:42:16 deraadt Exp $"; | ||
| 37 | #endif /* LIBC_SCCS and not lint */ | ||
| 38 | |||
| 39 | #include <stdlib.h> | ||
| 40 | |||
| 41 | int | ||
| 42 | atoi(str) | ||
| 43 | const char *str; | ||
| 44 | { | ||
| 45 | return((int)strtol(str, (char **)NULL, 10)); | ||
| 46 | } | ||
diff --git a/src/lib/libc/stdlib/atol.3 b/src/lib/libc/stdlib/atol.3 new file mode 100644 index 0000000000..86e3d324a3 --- /dev/null +++ b/src/lib/libc/stdlib/atol.3 | |||
| @@ -0,0 +1,75 @@ | |||
| 1 | .\" Copyright (c) 1990, 1991 The Regents of the University of California. | ||
| 2 | .\" All rights reserved. | ||
| 3 | .\" | ||
| 4 | .\" This code is derived from software contributed to Berkeley by | ||
| 5 | .\" the American National Standards Committee X3, on Information | ||
| 6 | .\" Processing Systems. | ||
| 7 | .\" | ||
| 8 | .\" Redistribution and use in source and binary forms, with or without | ||
| 9 | .\" modification, are permitted provided that the following conditions | ||
| 10 | .\" are met: | ||
| 11 | .\" 1. Redistributions of source code must retain the above copyright | ||
| 12 | .\" notice, this list of conditions and the following disclaimer. | ||
| 13 | .\" 2. Redistributions in binary form must reproduce the above copyright | ||
| 14 | .\" notice, this list of conditions and the following disclaimer in the | ||
| 15 | .\" documentation and/or other materials provided with the distribution. | ||
| 16 | .\" 3. All advertising materials mentioning features or use of this software | ||
| 17 | .\" must display the following acknowledgement: | ||
| 18 | .\" This product includes software developed by the University of | ||
| 19 | .\" California, Berkeley and its contributors. | ||
| 20 | .\" 4. Neither the name of the University nor the names of its contributors | ||
| 21 | .\" may be used to endorse or promote products derived from this software | ||
| 22 | .\" without specific prior written permission. | ||
| 23 | .\" | ||
| 24 | .\" THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND | ||
| 25 | .\" ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE | ||
| 26 | .\" IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE | ||
| 27 | .\" ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE | ||
| 28 | .\" FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL | ||
| 29 | .\" DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS | ||
| 30 | .\" OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) | ||
| 31 | .\" HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT | ||
| 32 | .\" LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY | ||
| 33 | .\" OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF | ||
| 34 | .\" SUCH DAMAGE. | ||
| 35 | .\" | ||
| 36 | .\" from: @(#)atol.3 5.3 (Berkeley) 6/29/91 | ||
| 37 | .\" $Id: atol.3,v 1.1.1.1 1995/10/18 08:42:16 deraadt Exp $ | ||
| 38 | .\" | ||
| 39 | .Dd June 29, 1991 | ||
| 40 | .Dt ATOL 3 | ||
| 41 | .Os | ||
| 42 | .Sh NAME | ||
| 43 | .Nm atol | ||
| 44 | .Nd convert | ||
| 45 | .Tn ASCII | ||
| 46 | string to long integer | ||
| 47 | .Sh SYNOPSIS | ||
| 48 | .Fd #include <stdlib.h> | ||
| 49 | .Ft long | ||
| 50 | .Fn atol "const char *nptr" | ||
| 51 | .Sh DESCRIPTION | ||
| 52 | The | ||
| 53 | .Fn atol | ||
| 54 | function converts the initial portion of the string pointed to by | ||
| 55 | .Ar nptr | ||
| 56 | to | ||
| 57 | .Em long integer | ||
| 58 | representation. | ||
| 59 | .Pp | ||
| 60 | It is equivalent to: | ||
| 61 | .Bd -literal -offset indent | ||
| 62 | strtol(nptr, (char **)NULL, 10); | ||
| 63 | .Ed | ||
| 64 | .Sh SEE ALSO | ||
| 65 | .Xr atof 3 , | ||
| 66 | .Xr atoi 3 , | ||
| 67 | .Xr strtod 3 , | ||
| 68 | .Xr strtol 3 , | ||
| 69 | .Xr strtoul 3 | ||
| 70 | .Sh STANDARDS | ||
| 71 | The | ||
| 72 | .Fn atol | ||
| 73 | function | ||
| 74 | conforms to | ||
| 75 | .St -ansiC . | ||
diff --git a/src/lib/libc/stdlib/atol.c b/src/lib/libc/stdlib/atol.c new file mode 100644 index 0000000000..31ed06298b --- /dev/null +++ b/src/lib/libc/stdlib/atol.c | |||
| @@ -0,0 +1,46 @@ | |||
| 1 | /* | ||
| 2 | * Copyright (c) 1988 Regents of the University of California. | ||
| 3 | * All rights reserved. | ||
| 4 | * | ||
| 5 | * Redistribution and use in source and binary forms, with or without | ||
| 6 | * modification, are permitted provided that the following conditions | ||
| 7 | * are met: | ||
| 8 | * 1. Redistributions of source code must retain the above copyright | ||
| 9 | * notice, this list of conditions and the following disclaimer. | ||
| 10 | * 2. Redistributions in binary form must reproduce the above copyright | ||
| 11 | * notice, this list of conditions and the following disclaimer in the | ||
| 12 | * documentation and/or other materials provided with the distribution. | ||
| 13 | * 3. All advertising materials mentioning features or use of this software | ||
| 14 | * must display the following acknowledgement: | ||
| 15 | * This product includes software developed by the University of | ||
| 16 | * California, Berkeley and its contributors. | ||
| 17 | * 4. Neither the name of the University nor the names of its contributors | ||
| 18 | * may be used to endorse or promote products derived from this software | ||
| 19 | * without specific prior written permission. | ||
| 20 | * | ||
| 21 | * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND | ||
| 22 | * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE | ||
| 23 | * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE | ||
| 24 | * ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE | ||
| 25 | * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL | ||
| 26 | * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS | ||
| 27 | * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) | ||
| 28 | * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT | ||
| 29 | * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY | ||
| 30 | * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF | ||
| 31 | * SUCH DAMAGE. | ||
| 32 | */ | ||
| 33 | |||
| 34 | #if defined(LIBC_SCCS) && !defined(lint) | ||
| 35 | /*static char *sccsid = "from: @(#)atol.c 5.7 (Berkeley) 2/23/91";*/ | ||
| 36 | static char *rcsid = "$Id: atol.c,v 1.1.1.1 1995/10/18 08:42:16 deraadt Exp $"; | ||
| 37 | #endif /* LIBC_SCCS and not lint */ | ||
| 38 | |||
| 39 | #include <stdlib.h> | ||
| 40 | |||
| 41 | long | ||
| 42 | atol(str) | ||
| 43 | const char *str; | ||
| 44 | { | ||
| 45 | return(strtol(str, (char **)NULL, 10)); | ||
| 46 | } | ||
diff --git a/src/lib/libc/stdlib/bsearch.3 b/src/lib/libc/stdlib/bsearch.3 new file mode 100644 index 0000000000..1622c96c6b --- /dev/null +++ b/src/lib/libc/stdlib/bsearch.3 | |||
| @@ -0,0 +1,91 @@ | |||
| 1 | .\" Copyright (c) 1990, 1991 The Regents of the University of California. | ||
| 2 | .\" All rights reserved. | ||
| 3 | .\" | ||
| 4 | .\" This code is derived from software contributed to Berkeley by | ||
| 5 | .\" the American National Standards Committee X3, on Information | ||
| 6 | .\" Processing Systems. | ||
| 7 | .\" | ||
| 8 | .\" Redistribution and use in source and binary forms, with or without | ||
| 9 | .\" modification, are permitted provided that the following conditions | ||
| 10 | .\" are met: | ||
| 11 | .\" 1. Redistributions of source code must retain the above copyright | ||
| 12 | .\" notice, this list of conditions and the following disclaimer. | ||
| 13 | .\" 2. Redistributions in binary form must reproduce the above copyright | ||
| 14 | .\" notice, this list of conditions and the following disclaimer in the | ||
| 15 | .\" documentation and/or other materials provided with the distribution. | ||
| 16 | .\" 3. All advertising materials mentioning features or use of this software | ||
| 17 | .\" must display the following acknowledgement: | ||
| 18 | .\" This product includes software developed by the University of | ||
| 19 | .\" California, Berkeley and its contributors. | ||
| 20 | .\" 4. Neither the name of the University nor the names of its contributors | ||
| 21 | .\" may be used to endorse or promote products derived from this software | ||
| 22 | .\" without specific prior written permission. | ||
| 23 | .\" | ||
| 24 | .\" THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND | ||
| 25 | .\" ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE | ||
| 26 | .\" IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE | ||
| 27 | .\" ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE | ||
| 28 | .\" FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL | ||
| 29 | .\" DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS | ||
| 30 | .\" OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) | ||
| 31 | .\" HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT | ||
| 32 | .\" LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY | ||
| 33 | .\" OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF | ||
| 34 | .\" SUCH DAMAGE. | ||
| 35 | .\" | ||
| 36 | .\" from: @(#)bsearch.3 5.6 (Berkeley) 6/29/91 | ||
| 37 | .\" $Id: bsearch.3,v 1.1.1.1 1995/10/18 08:42:16 deraadt Exp $ | ||
| 38 | .\" | ||
| 39 | .Dd June 29, 1991 | ||
| 40 | .Dt BSEARCH 3 | ||
| 41 | .Os | ||
| 42 | .Sh NAME | ||
| 43 | .Nm bsearch | ||
| 44 | .Nd binary search of a sorted table | ||
| 45 | .Sh SYNOPSIS | ||
| 46 | .Fd #include <stdlib.h> | ||
| 47 | .Ft void * | ||
| 48 | .Fn bsearch "const void *key" "const void *base" "size_t nmemb" "size_t size" "int (*compar) (const void *, const void *)" | ||
| 49 | .Sh DESCRIPTION | ||
| 50 | The | ||
| 51 | .Fn bsearch | ||
| 52 | function searches an array of | ||
| 53 | .Fa nmemb | ||
| 54 | objects, the initial member of which is | ||
| 55 | pointed to by | ||
| 56 | .Fa base , | ||
| 57 | for a member that matches the object pointed to by | ||
| 58 | .Fa key . | ||
| 59 | The size of each member of the array is specified by | ||
| 60 | .Fa size . | ||
| 61 | .Pp | ||
| 62 | The contents of the array should be in ascending sorted order according | ||
| 63 | to the comparison function referenced by | ||
| 64 | .Fa compar . | ||
| 65 | The | ||
| 66 | .Fa compar | ||
| 67 | routine | ||
| 68 | is expected to have two | ||
| 69 | two arguments which point to the | ||
| 70 | .Fa key | ||
| 71 | object and to an array member, in that order, and should return an integer | ||
| 72 | less than, equal to, or greater than zero if the | ||
| 73 | .Fa key | ||
| 74 | object is found, respectively, to be less than, to match, or be | ||
| 75 | greater than the array member. | ||
| 76 | .Sh RETURN VALUES | ||
| 77 | The | ||
| 78 | .Fn bsearch | ||
| 79 | function returns a pointer to a matching member of the array, or a null | ||
| 80 | pointer if no match is found. | ||
| 81 | If two members compare as equal, which member is matched is unspecified. | ||
| 82 | .Sh SEE ALSO | ||
| 83 | .Xr db 3 , | ||
| 84 | .Xr lsearch 3 , | ||
| 85 | .Xr qsort 3 , | ||
| 86 | .\" .Xr tsearch 3 | ||
| 87 | .Sh STANDARDS | ||
| 88 | The | ||
| 89 | .Fn bsearch | ||
| 90 | function conforms to | ||
| 91 | .St -ansiC . | ||
diff --git a/src/lib/libc/stdlib/bsearch.c b/src/lib/libc/stdlib/bsearch.c new file mode 100644 index 0000000000..fac03f694f --- /dev/null +++ b/src/lib/libc/stdlib/bsearch.c | |||
| @@ -0,0 +1,80 @@ | |||
| 1 | /* | ||
| 2 | * Copyright (c) 1990 Regents of the University of California. | ||
| 3 | * All rights reserved. | ||
| 4 | * | ||
| 5 | * Redistribution and use in source and binary forms, with or without | ||
| 6 | * modification, are permitted provided that the following conditions | ||
| 7 | * are met: | ||
| 8 | * 1. Redistributions of source code must retain the above copyright | ||
| 9 | * notice, this list of conditions and the following disclaimer. | ||
| 10 | * 2. Redistributions in binary form must reproduce the above copyright | ||
| 11 | * notice, this list of conditions and the following disclaimer in the | ||
| 12 | * documentation and/or other materials provided with the distribution. | ||
| 13 | * 3. All advertising materials mentioning features or use of this software | ||
| 14 | * must display the following acknowledgement: | ||
| 15 | * This product includes software developed by the University of | ||
| 16 | * California, Berkeley and its contributors. | ||
| 17 | * 4. Neither the name of the University nor the names of its contributors | ||
| 18 | * may be used to endorse or promote products derived from this software | ||
| 19 | * without specific prior written permission. | ||
| 20 | * | ||
| 21 | * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND | ||
| 22 | * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE | ||
| 23 | * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE | ||
| 24 | * ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE | ||
| 25 | * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL | ||
| 26 | * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS | ||
| 27 | * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) | ||
| 28 | * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT | ||
| 29 | * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY | ||
| 30 | * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF | ||
| 31 | * SUCH DAMAGE. | ||
| 32 | */ | ||
| 33 | |||
| 34 | #if defined(LIBC_SCCS) && !defined(lint) | ||
| 35 | /*static char *sccsid = "from: @(#)bsearch.c 5.4 (Berkeley) 2/23/91";*/ | ||
| 36 | static char *rcsid = "$Id: bsearch.c,v 1.1.1.1 1995/10/18 08:42:16 deraadt Exp $"; | ||
| 37 | #endif /* LIBC_SCCS and not lint */ | ||
| 38 | |||
| 39 | #include <stdlib.h> | ||
| 40 | |||
| 41 | /* | ||
| 42 | * Perform a binary search. | ||
| 43 | * | ||
| 44 | * The code below is a bit sneaky. After a comparison fails, we | ||
| 45 | * divide the work in half by moving either left or right. If lim | ||
| 46 | * is odd, moving left simply involves halving lim: e.g., when lim | ||
| 47 | * is 5 we look at item 2, so we change lim to 2 so that we will | ||
| 48 | * look at items 0 & 1. If lim is even, the same applies. If lim | ||
| 49 | * is odd, moving right again involes halving lim, this time moving | ||
| 50 | * the base up one item past p: e.g., when lim is 5 we change base | ||
| 51 | * to item 3 and make lim 2 so that we will look at items 3 and 4. | ||
| 52 | * If lim is even, however, we have to shrink it by one before | ||
| 53 | * halving: e.g., when lim is 4, we still looked at item 2, so we | ||
| 54 | * have to make lim 3, then halve, obtaining 1, so that we will only | ||
| 55 | * look at item 3. | ||
| 56 | */ | ||
| 57 | void * | ||
| 58 | bsearch(key, base0, nmemb, size, compar) | ||
| 59 | register const void *key; | ||
| 60 | const void *base0; | ||
| 61 | size_t nmemb; | ||
| 62 | register size_t size; | ||
| 63 | register int (*compar) __P((const void *, const void *)); | ||
| 64 | { | ||
| 65 | register const char *base = base0; | ||
| 66 | register int lim, cmp; | ||
| 67 | register const void *p; | ||
| 68 | |||
| 69 | for (lim = nmemb; lim != 0; lim >>= 1) { | ||
| 70 | p = base + (lim >> 1) * size; | ||
| 71 | cmp = (*compar)(key, p); | ||
| 72 | if (cmp == 0) | ||
| 73 | return ((void *)p); | ||
| 74 | if (cmp > 0) { /* key > p: move right */ | ||
| 75 | base = (char *)p + size; | ||
| 76 | lim--; | ||
| 77 | } /* else move left */ | ||
| 78 | } | ||
| 79 | return (NULL); | ||
| 80 | } | ||
diff --git a/src/lib/libc/stdlib/calloc.3 b/src/lib/libc/stdlib/calloc.3 new file mode 100644 index 0000000000..d0754b46a0 --- /dev/null +++ b/src/lib/libc/stdlib/calloc.3 | |||
| @@ -0,0 +1,71 @@ | |||
| 1 | .\" Copyright (c) 1991 The Regents of the University of California. | ||
| 2 | .\" All rights reserved. | ||
| 3 | .\" | ||
| 4 | .\" This code is derived from software contributed to Berkeley by | ||
| 5 | .\" the American National Standards Committee X3, on Information | ||
| 6 | .\" Processing Systems. | ||
| 7 | .\" | ||
| 8 | .\" Redistribution and use in source and binary forms, with or without | ||
| 9 | .\" modification, are permitted provided that the following conditions | ||
| 10 | .\" are met: | ||
| 11 | .\" 1. Redistributions of source code must retain the above copyright | ||
| 12 | .\" notice, this list of conditions and the following disclaimer. | ||
| 13 | .\" 2. Redistributions in binary form must reproduce the above copyright | ||
| 14 | .\" notice, this list of conditions and the following disclaimer in the | ||
| 15 | .\" documentation and/or other materials provided with the distribution. | ||
| 16 | .\" 3. All advertising materials mentioning features or use of this software | ||
| 17 | .\" must display the following acknowledgement: | ||
| 18 | .\" This product includes software developed by the University of | ||
| 19 | .\" California, Berkeley and its contributors. | ||
| 20 | .\" 4. Neither the name of the University nor the names of its contributors | ||
| 21 | .\" may be used to endorse or promote products derived from this software | ||
| 22 | .\" without specific prior written permission. | ||
| 23 | .\" | ||
| 24 | .\" THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND | ||
| 25 | .\" ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE | ||
| 26 | .\" IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE | ||
| 27 | .\" ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE | ||
| 28 | .\" FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL | ||
| 29 | .\" DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS | ||
| 30 | .\" OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) | ||
| 31 | .\" HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT | ||
| 32 | .\" LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY | ||
| 33 | .\" OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF | ||
| 34 | .\" SUCH DAMAGE. | ||
| 35 | .\" | ||
| 36 | .\" from: @(#)calloc.3 5.2 (Berkeley) 6/29/91 | ||
| 37 | .\" $Id: calloc.3,v 1.1.1.1 1995/10/18 08:42:17 deraadt Exp $ | ||
| 38 | .\" | ||
| 39 | .Dd June 29, 1991 | ||
| 40 | .Dt CALLOC 3 | ||
| 41 | .Os | ||
| 42 | .Sh NAME | ||
| 43 | .Nm calloc | ||
| 44 | .Nd allocate clean memory (zero initialized space) | ||
| 45 | .Sh SYNOPSIS | ||
| 46 | .Fd #include <stdlib.h> | ||
| 47 | .Ft void * | ||
| 48 | .Fn calloc "size_t nmemb" "size_t size" | ||
| 49 | .Sh DESCRIPTION | ||
| 50 | The | ||
| 51 | .Fn calloc | ||
| 52 | function allocates space for an array of | ||
| 53 | .Fa nmemb | ||
| 54 | objects, each of whose size is | ||
| 55 | .Fa size . | ||
| 56 | The space is initialized to all bits zero. | ||
| 57 | .Sh RETURN VALUES | ||
| 58 | The | ||
| 59 | .Fn calloc | ||
| 60 | function returns | ||
| 61 | a pointer to the | ||
| 62 | the allocated space if successful; otherwise a null pointer is returned. | ||
| 63 | .Sh SEE ALSO | ||
| 64 | .Xr malloc 3 , | ||
| 65 | .Xr realloc 3 , | ||
| 66 | .Xr free 3 | ||
| 67 | .Sh STANDARDS | ||
| 68 | The | ||
| 69 | .Fn calloc | ||
| 70 | function conforms to | ||
| 71 | .St -ansiC . | ||
diff --git a/src/lib/libc/stdlib/calloc.c b/src/lib/libc/stdlib/calloc.c new file mode 100644 index 0000000000..3353fab052 --- /dev/null +++ b/src/lib/libc/stdlib/calloc.c | |||
| @@ -0,0 +1,53 @@ | |||
| 1 | /*- | ||
| 2 | * Copyright (c) 1990 The Regents of the University of California. | ||
| 3 | * All rights reserved. | ||
| 4 | * | ||
| 5 | * Redistribution and use in source and binary forms, with or without | ||
| 6 | * modification, are permitted provided that the following conditions | ||
| 7 | * are met: | ||
| 8 | * 1. Redistributions of source code must retain the above copyright | ||
| 9 | * notice, this list of conditions and the following disclaimer. | ||
| 10 | * 2. Redistributions in binary form must reproduce the above copyright | ||
| 11 | * notice, this list of conditions and the following disclaimer in the | ||
| 12 | * documentation and/or other materials provided with the distribution. | ||
| 13 | * 3. All advertising materials mentioning features or use of this software | ||
| 14 | * must display the following acknowledgement: | ||
| 15 | * This product includes software developed by the University of | ||
| 16 | * California, Berkeley and its contributors. | ||
| 17 | * 4. Neither the name of the University nor the names of its contributors | ||
| 18 | * may be used to endorse or promote products derived from this software | ||
| 19 | * without specific prior written permission. | ||
| 20 | * | ||
| 21 | * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND | ||
| 22 | * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE | ||
| 23 | * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE | ||
| 24 | * ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE | ||
| 25 | * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL | ||
| 26 | * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS | ||
| 27 | * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) | ||
| 28 | * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT | ||
| 29 | * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY | ||
| 30 | * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF | ||
| 31 | * SUCH DAMAGE. | ||
| 32 | */ | ||
| 33 | |||
| 34 | #if defined(LIBC_SCCS) && !defined(lint) | ||
| 35 | /*static char *sccsid = "from: @(#)calloc.c 5.6 (Berkeley) 2/23/91";*/ | ||
| 36 | static char *rcsid = "$Id: calloc.c,v 1.1.1.1 1995/10/18 08:42:17 deraadt Exp $"; | ||
| 37 | #endif /* LIBC_SCCS and not lint */ | ||
| 38 | |||
| 39 | #include <stdlib.h> | ||
| 40 | #include <string.h> | ||
| 41 | |||
| 42 | void * | ||
| 43 | calloc(num, size) | ||
| 44 | size_t num; | ||
| 45 | register size_t size; | ||
| 46 | { | ||
| 47 | register void *p; | ||
| 48 | |||
| 49 | size *= num; | ||
| 50 | if (p = malloc(size)) | ||
| 51 | memset(p, '\0', size); | ||
| 52 | return(p); | ||
| 53 | } | ||
diff --git a/src/lib/libc/stdlib/div.3 b/src/lib/libc/stdlib/div.3 new file mode 100644 index 0000000000..a4730694a5 --- /dev/null +++ b/src/lib/libc/stdlib/div.3 | |||
| @@ -0,0 +1,70 @@ | |||
| 1 | .\" Copyright (c) 1990, 1991 The Regents of the University of California. | ||
| 2 | .\" All rights reserved. | ||
| 3 | .\" | ||
| 4 | .\" This code is derived from software contributed to Berkeley by | ||
| 5 | .\" Chris Torek. | ||
| 6 | .\" Redistribution and use in source and binary forms, with or without | ||
| 7 | .\" modification, are permitted provided that the following conditions | ||
| 8 | .\" are met: | ||
| 9 | .\" 1. Redistributions of source code must retain the above copyright | ||
| 10 | .\" notice, this list of conditions and the following disclaimer. | ||
| 11 | .\" 2. Redistributions in binary form must reproduce the above copyright | ||
| 12 | .\" notice, this list of conditions and the following disclaimer in the | ||
| 13 | .\" documentation and/or other materials provided with the distribution. | ||
| 14 | .\" 3. All advertising materials mentioning features or use of this software | ||
| 15 | .\" must display the following acknowledgement: | ||
| 16 | .\" This product includes software developed by the University of | ||
| 17 | .\" California, Berkeley and its contributors. | ||
| 18 | .\" 4. Neither the name of the University nor the names of its contributors | ||
| 19 | .\" may be used to endorse or promote products derived from this software | ||
| 20 | .\" without specific prior written permission. | ||
| 21 | .\" | ||
| 22 | .\" THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND | ||
| 23 | .\" ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE | ||
| 24 | .\" IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE | ||
| 25 | .\" ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE | ||
| 26 | .\" FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL | ||
| 27 | .\" DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS | ||
| 28 | .\" OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) | ||
| 29 | .\" HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT | ||
| 30 | .\" LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY | ||
| 31 | .\" OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF | ||
| 32 | .\" SUCH DAMAGE. | ||
| 33 | .\" | ||
| 34 | .\" from: @(#)div.3 5.2 (Berkeley) 4/19/91 | ||
| 35 | .\" $Id: div.3,v 1.1.1.1 1995/10/18 08:42:17 deraadt Exp $ | ||
| 36 | .\" | ||
| 37 | .Dd April 19, 1991 | ||
| 38 | .Dt DIV 3 | ||
| 39 | .Os | ||
| 40 | .Sh NAME | ||
| 41 | .Nm div | ||
| 42 | .Nd return quotient and remainder from division | ||
| 43 | .Sh SYNOPSIS | ||
| 44 | .Fd #include <stdlib.h> | ||
| 45 | .Ft div_t | ||
| 46 | .Fn div "int num" "int denom" | ||
| 47 | .Sh DESCRIPTION | ||
| 48 | The | ||
| 49 | .Fn div | ||
| 50 | function | ||
| 51 | computes the value | ||
| 52 | .Fa num/denom | ||
| 53 | and returns the quotient and remainder in a structure named | ||
| 54 | .Fa div_t | ||
| 55 | that contains two | ||
| 56 | .Em int | ||
| 57 | members named | ||
| 58 | .Fa quot | ||
| 59 | and | ||
| 60 | .Fa rem . | ||
| 61 | .Sh SEE ALSO | ||
| 62 | .Xr ldiv 3 , | ||
| 63 | .Xr qdiv 3 , | ||
| 64 | .Xr math 3 | ||
| 65 | .Sh STANDARDS | ||
| 66 | The | ||
| 67 | .Fn div | ||
| 68 | function | ||
| 69 | conforms to | ||
| 70 | .St -ansiC . | ||
diff --git a/src/lib/libc/stdlib/div.c b/src/lib/libc/stdlib/div.c new file mode 100644 index 0000000000..122ac0deec --- /dev/null +++ b/src/lib/libc/stdlib/div.c | |||
| @@ -0,0 +1,80 @@ | |||
| 1 | /* | ||
| 2 | * Copyright (c) 1990 Regents of the University of California. | ||
| 3 | * All rights reserved. | ||
| 4 | * | ||
| 5 | * This code is derived from software contributed to Berkeley by | ||
| 6 | * Chris Torek. | ||
| 7 | * | ||
| 8 | * Redistribution and use in source and binary forms, with or without | ||
| 9 | * modification, are permitted provided that the following conditions | ||
| 10 | * are met: | ||
| 11 | * 1. Redistributions of source code must retain the above copyright | ||
| 12 | * notice, this list of conditions and the following disclaimer. | ||
| 13 | * 2. Redistributions in binary form must reproduce the above copyright | ||
| 14 | * notice, this list of conditions and the following disclaimer in the | ||
| 15 | * documentation and/or other materials provided with the distribution. | ||
| 16 | * 3. All advertising materials mentioning features or use of this software | ||
| 17 | * must display the following acknowledgement: | ||
| 18 | * This product includes software developed by the University of | ||
| 19 | * California, Berkeley and its contributors. | ||
| 20 | * 4. Neither the name of the University nor the names of its contributors | ||
| 21 | * may be used to endorse or promote products derived from this software | ||
| 22 | * without specific prior written permission. | ||
| 23 | * | ||
| 24 | * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND | ||
| 25 | * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE | ||
| 26 | * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE | ||
| 27 | * ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE | ||
| 28 | * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL | ||
| 29 | * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS | ||
| 30 | * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) | ||
| 31 | * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT | ||
| 32 | * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY | ||
| 33 | * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF | ||
| 34 | * SUCH DAMAGE. | ||
| 35 | */ | ||
| 36 | |||
| 37 | #if defined(LIBC_SCCS) && !defined(lint) | ||
| 38 | /*static char *sccsid = "from: @(#)div.c 5.2 (Berkeley) 4/16/91";*/ | ||
| 39 | static char *rcsid = "$Id: div.c,v 1.1.1.1 1995/10/18 08:42:17 deraadt Exp $"; | ||
| 40 | #endif /* LIBC_SCCS and not lint */ | ||
| 41 | |||
| 42 | #include <stdlib.h> /* div_t */ | ||
| 43 | |||
| 44 | div_t | ||
| 45 | div(num, denom) | ||
| 46 | int num, denom; | ||
| 47 | { | ||
| 48 | div_t r; | ||
| 49 | |||
| 50 | r.quot = num / denom; | ||
| 51 | r.rem = num % denom; | ||
| 52 | /* | ||
| 53 | * The ANSI standard says that |r.quot| <= |n/d|, where | ||
| 54 | * n/d is to be computed in infinite precision. In other | ||
| 55 | * words, we should always truncate the quotient towards | ||
| 56 | * 0, never -infinity. | ||
| 57 | * | ||
| 58 | * Machine division and remainer may work either way when | ||
| 59 | * one or both of n or d is negative. If only one is | ||
| 60 | * negative and r.quot has been truncated towards -inf, | ||
| 61 | * r.rem will have the same sign as denom and the opposite | ||
| 62 | * sign of num; if both are negative and r.quot has been | ||
| 63 | * truncated towards -inf, r.rem will be positive (will | ||
| 64 | * have the opposite sign of num). These are considered | ||
| 65 | * `wrong'. | ||
| 66 | * | ||
| 67 | * If both are num and denom are positive, r will always | ||
| 68 | * be positive. | ||
| 69 | * | ||
| 70 | * This all boils down to: | ||
| 71 | * if num >= 0, but r.rem < 0, we got the wrong answer. | ||
| 72 | * In that case, to get the right answer, add 1 to r.quot and | ||
| 73 | * subtract denom from r.rem. | ||
| 74 | */ | ||
| 75 | if (num >= 0 && r.rem < 0) { | ||
| 76 | r.quot++; | ||
| 77 | r.rem -= denom; | ||
| 78 | } | ||
| 79 | return (r); | ||
| 80 | } | ||
diff --git a/src/lib/libc/stdlib/drand48.c b/src/lib/libc/stdlib/drand48.c new file mode 100644 index 0000000000..ae1a8634dc --- /dev/null +++ b/src/lib/libc/stdlib/drand48.c | |||
| @@ -0,0 +1,22 @@ | |||
| 1 | /* | ||
| 2 | * Copyright (c) 1993 Martin Birgmeier | ||
| 3 | * All rights reserved. | ||
| 4 | * | ||
| 5 | * You may redistribute unmodified or modified versions of this source | ||
| 6 | * code provided that the above copyright notice and this and the | ||
| 7 | * following conditions are retained. | ||
| 8 | * | ||
| 9 | * This software is provided ``as is'', and comes with no warranties | ||
| 10 | * of any kind. I shall in no event be liable for anything that happens | ||
| 11 | * to anyone/anything when using this software. | ||
| 12 | */ | ||
| 13 | |||
| 14 | #include "rand48.h" | ||
| 15 | |||
| 16 | extern unsigned short __rand48_seed[3]; | ||
| 17 | |||
| 18 | double | ||
| 19 | drand48(void) | ||
| 20 | { | ||
| 21 | return erand48(__rand48_seed); | ||
| 22 | } | ||
diff --git a/src/lib/libc/stdlib/erand48.c b/src/lib/libc/stdlib/erand48.c new file mode 100644 index 0000000000..cc9fbf770c --- /dev/null +++ b/src/lib/libc/stdlib/erand48.c | |||
| @@ -0,0 +1,23 @@ | |||
| 1 | /* | ||
| 2 | * Copyright (c) 1993 Martin Birgmeier | ||
| 3 | * All rights reserved. | ||
| 4 | * | ||
| 5 | * You may redistribute unmodified or modified versions of this source | ||
| 6 | * code provided that the above copyright notice and this and the | ||
| 7 | * following conditions are retained. | ||
| 8 | * | ||
| 9 | * This software is provided ``as is'', and comes with no warranties | ||
| 10 | * of any kind. I shall in no event be liable for anything that happens | ||
| 11 | * to anyone/anything when using this software. | ||
| 12 | */ | ||
| 13 | |||
| 14 | #include "rand48.h" | ||
| 15 | |||
| 16 | double | ||
| 17 | erand48(unsigned short xseed[3]) | ||
| 18 | { | ||
| 19 | __dorand48(xseed); | ||
| 20 | return ldexp((double) xseed[0], -48) + | ||
| 21 | ldexp((double) xseed[1], -32) + | ||
| 22 | ldexp((double) xseed[2], -16); | ||
| 23 | } | ||
diff --git a/src/lib/libc/stdlib/exit.3 b/src/lib/libc/stdlib/exit.3 new file mode 100644 index 0000000000..adb81ffcb4 --- /dev/null +++ b/src/lib/libc/stdlib/exit.3 | |||
| @@ -0,0 +1,84 @@ | |||
| 1 | .\" Copyright (c) 1990, 1991 The Regents of the University of California. | ||
| 2 | .\" All rights reserved. | ||
| 3 | .\" | ||
| 4 | .\" This code is derived from software contributed to Berkeley by | ||
| 5 | .\" the American National Standards Committee X3, on Information | ||
| 6 | .\" Processing Systems. | ||
| 7 | .\" | ||
| 8 | .\" Redistribution and use in source and binary forms, with or without | ||
| 9 | .\" modification, are permitted provided that the following conditions | ||
| 10 | .\" are met: | ||
| 11 | .\" 1. Redistributions of source code must retain the above copyright | ||
| 12 | .\" notice, this list of conditions and the following disclaimer. | ||
| 13 | .\" 2. Redistributions in binary form must reproduce the above copyright | ||
| 14 | .\" notice, this list of conditions and the following disclaimer in the | ||
| 15 | .\" documentation and/or other materials provided with the distribution. | ||
| 16 | .\" 3. All advertising materials mentioning features or use of this software | ||
| 17 | .\" must display the following acknowledgement: | ||
| 18 | .\" This product includes software developed by the University of | ||
| 19 | .\" California, Berkeley and its contributors. | ||
| 20 | .\" 4. Neither the name of the University nor the names of its contributors | ||
| 21 | .\" may be used to endorse or promote products derived from this software | ||
| 22 | .\" without specific prior written permission. | ||
| 23 | .\" | ||
| 24 | .\" THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND | ||
| 25 | .\" ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE | ||
| 26 | .\" IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE | ||
| 27 | .\" ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE | ||
| 28 | .\" FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL | ||
| 29 | .\" DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS | ||
| 30 | .\" OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) | ||
| 31 | .\" HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT | ||
| 32 | .\" LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY | ||
| 33 | .\" OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF | ||
| 34 | .\" SUCH DAMAGE. | ||
| 35 | .\" | ||
| 36 | .\" from: @(#)exit.3 6.6 (Berkeley) 6/29/91 | ||
| 37 | .\" $Id: exit.3,v 1.1.1.1 1995/10/18 08:42:17 deraadt Exp $ | ||
| 38 | .\" | ||
| 39 | .Dd June 29, 1991 | ||
| 40 | .Dt EXIT 3 | ||
| 41 | .Os | ||
| 42 | .Sh NAME | ||
| 43 | .Nm exit | ||
| 44 | .Nd perform normal program termination | ||
| 45 | .Sh SYNOPSIS | ||
| 46 | .Fd #include <stdlib.h> | ||
| 47 | .Ft void | ||
| 48 | .Fn exit "int status" | ||
| 49 | .Sh DESCRIPTION | ||
| 50 | .Fn Exit | ||
| 51 | terminates a process. | ||
| 52 | .Pp | ||
| 53 | Before termination it performs the following functions in the | ||
| 54 | order listed: | ||
| 55 | .Bl -enum -offset indent | ||
| 56 | .It | ||
| 57 | Call the functions registered with the | ||
| 58 | .Xr atexit 3 | ||
| 59 | function, in the reverse order of their registration. | ||
| 60 | .It | ||
| 61 | Flush all open output streams. | ||
| 62 | .It | ||
| 63 | Close all open streams. | ||
| 64 | .It | ||
| 65 | Unlink all files created with the | ||
| 66 | .Xr tmpfile 3 | ||
| 67 | function. | ||
| 68 | .El | ||
| 69 | .Sh RETURN VALUES | ||
| 70 | The | ||
| 71 | .Fn exit | ||
| 72 | function | ||
| 73 | never returns. | ||
| 74 | .Sh SEE ALSO | ||
| 75 | .Xr _exit 2 , | ||
| 76 | .Xr atexit 3 , | ||
| 77 | .Xr intro 3 , | ||
| 78 | .Xr tmpfile 3 | ||
| 79 | .Sh STANDARDS | ||
| 80 | The | ||
| 81 | .Fn exit | ||
| 82 | function | ||
| 83 | conforms to | ||
| 84 | .St -ansiC . | ||
diff --git a/src/lib/libc/stdlib/exit.c b/src/lib/libc/stdlib/exit.c new file mode 100644 index 0000000000..b1412f42bb --- /dev/null +++ b/src/lib/libc/stdlib/exit.c | |||
| @@ -0,0 +1,61 @@ | |||
| 1 | /*- | ||
| 2 | * Copyright (c) 1990 The Regents of the University of California. | ||
| 3 | * All rights reserved. | ||
| 4 | * | ||
| 5 | * Redistribution and use in source and binary forms, with or without | ||
| 6 | * modification, are permitted provided that the following conditions | ||
| 7 | * are met: | ||
| 8 | * 1. Redistributions of source code must retain the above copyright | ||
| 9 | * notice, this list of conditions and the following disclaimer. | ||
| 10 | * 2. Redistributions in binary form must reproduce the above copyright | ||
| 11 | * notice, this list of conditions and the following disclaimer in the | ||
| 12 | * documentation and/or other materials provided with the distribution. | ||
| 13 | * 3. All advertising materials mentioning features or use of this software | ||
| 14 | * must display the following acknowledgement: | ||
| 15 | * This product includes software developed by the University of | ||
| 16 | * California, Berkeley and its contributors. | ||
| 17 | * 4. Neither the name of the University nor the names of its contributors | ||
| 18 | * may be used to endorse or promote products derived from this software | ||
| 19 | * without specific prior written permission. | ||
| 20 | * | ||
| 21 | * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND | ||
| 22 | * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE | ||
| 23 | * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE | ||
| 24 | * ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE | ||
| 25 | * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL | ||
| 26 | * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS | ||
| 27 | * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) | ||
| 28 | * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT | ||
| 29 | * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY | ||
| 30 | * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF | ||
| 31 | * SUCH DAMAGE. | ||
| 32 | */ | ||
| 33 | |||
| 34 | #if defined(LIBC_SCCS) && !defined(lint) | ||
| 35 | /*static char *sccsid = "from: @(#)exit.c 5.4 (Berkeley) 2/23/91";*/ | ||
| 36 | static char *rcsid = "$Id: exit.c,v 1.1.1.1 1995/10/18 08:42:17 deraadt Exp $"; | ||
| 37 | #endif /* LIBC_SCCS and not lint */ | ||
| 38 | |||
| 39 | #include <stdlib.h> | ||
| 40 | #include <unistd.h> | ||
| 41 | #include "atexit.h" | ||
| 42 | |||
| 43 | void (*__cleanup)(); | ||
| 44 | |||
| 45 | /* | ||
| 46 | * Exit, flushing stdio buffers if necessary. | ||
| 47 | */ | ||
| 48 | void | ||
| 49 | exit(status) | ||
| 50 | int status; | ||
| 51 | { | ||
| 52 | register struct atexit *p; | ||
| 53 | register int n; | ||
| 54 | |||
| 55 | for (p = __atexit; p; p = p->next) | ||
| 56 | for (n = p->ind; --n >= 0;) | ||
| 57 | (*p->fns[n])(); | ||
| 58 | if (__cleanup) | ||
| 59 | (*__cleanup)(); | ||
| 60 | _exit(status); | ||
| 61 | } | ||
diff --git a/src/lib/libc/stdlib/free.3 b/src/lib/libc/stdlib/free.3 new file mode 100644 index 0000000000..3d0131d7de --- /dev/null +++ b/src/lib/libc/stdlib/free.3 | |||
| @@ -0,0 +1,82 @@ | |||
| 1 | .\" Copyright (c) 1991 The Regents of the University of California. | ||
| 2 | .\" All rights reserved. | ||
| 3 | .\" | ||
| 4 | .\" This code is derived from software contributed to Berkeley by | ||
| 5 | .\" the American National Standards Committee X3, on Information | ||
| 6 | .\" Processing Systems. | ||
| 7 | .\" | ||
| 8 | .\" Redistribution and use in source and binary forms, with or without | ||
| 9 | .\" modification, are permitted provided that the following conditions | ||
| 10 | .\" are met: | ||
| 11 | .\" 1. Redistributions of source code must retain the above copyright | ||
| 12 | .\" notice, this list of conditions and the following disclaimer. | ||
| 13 | .\" 2. Redistributions in binary form must reproduce the above copyright | ||
| 14 | .\" notice, this list of conditions and the following disclaimer in the | ||
| 15 | .\" documentation and/or other materials provided with the distribution. | ||
| 16 | .\" 3. All advertising materials mentioning features or use of this software | ||
| 17 | .\" must display the following acknowledgement: | ||
| 18 | .\" This product includes software developed by the University of | ||
| 19 | .\" California, Berkeley and its contributors. | ||
| 20 | .\" 4. Neither the name of the University nor the names of its contributors | ||
| 21 | .\" may be used to endorse or promote products derived from this software | ||
| 22 | .\" without specific prior written permission. | ||
| 23 | .\" | ||
| 24 | .\" THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND | ||
| 25 | .\" ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE | ||
| 26 | .\" IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE | ||
| 27 | .\" ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE | ||
| 28 | .\" FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL | ||
| 29 | .\" DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS | ||
| 30 | .\" OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) | ||
| 31 | .\" HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT | ||
| 32 | .\" LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY | ||
| 33 | .\" OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF | ||
| 34 | .\" SUCH DAMAGE. | ||
| 35 | .\" | ||
| 36 | .\" from: @(#)free.3 5.2 (Berkeley) 6/29/91 | ||
| 37 | .\" $Id: free.3,v 1.1.1.1 1995/10/18 08:42:17 deraadt Exp $ | ||
| 38 | .\" | ||
| 39 | .Dd June 29, 1991 | ||
| 40 | .Dt FREE 3 | ||
| 41 | .Os | ||
| 42 | .Sh NAME | ||
| 43 | .Nm free | ||
| 44 | .Nd free up memory allocated with malloc, calloc or realloc | ||
| 45 | .Sh SYNOPSIS | ||
| 46 | .Fd #include <stdlib.h> | ||
| 47 | .Ft void | ||
| 48 | .Fn free "void *ptr" | ||
| 49 | .Sh DESCRIPTION | ||
| 50 | The | ||
| 51 | .Fn free | ||
| 52 | function causes the space pointed to by | ||
| 53 | .Fa ptr | ||
| 54 | to be deallocated, that is, made available | ||
| 55 | for further allocation. | ||
| 56 | If | ||
| 57 | .Fa ptr | ||
| 58 | is a null pointer, no action occurs. | ||
| 59 | Otherwise, if the argument does not match a pointer earlier | ||
| 60 | returned by the | ||
| 61 | .Xr calloc , | ||
| 62 | .Xr malloc , | ||
| 63 | or | ||
| 64 | .Xr realloc | ||
| 65 | function, or if the space has been deallocated by a call to | ||
| 66 | .Fn free | ||
| 67 | or | ||
| 68 | .Xr realloc , | ||
| 69 | general havoc may occur. | ||
| 70 | .Sh RETURN VALUES | ||
| 71 | The | ||
| 72 | .Fn free | ||
| 73 | function returns no value. | ||
| 74 | .Sh SEE ALSO | ||
| 75 | .Xr calloc 3 , | ||
| 76 | .Xr malloc 3 , | ||
| 77 | .Xr realloc 3 | ||
| 78 | .Sh STANDARDS | ||
| 79 | The | ||
| 80 | .Fn free | ||
| 81 | function conforms to | ||
| 82 | .St -ansiC . | ||
diff --git a/src/lib/libc/stdlib/getenv.3 b/src/lib/libc/stdlib/getenv.3 new file mode 100644 index 0000000000..411eb35da4 --- /dev/null +++ b/src/lib/libc/stdlib/getenv.3 | |||
| @@ -0,0 +1,152 @@ | |||
| 1 | .\" Copyright (c) 1988, 1991 The Regents of the University of California. | ||
| 2 | .\" All rights reserved. | ||
| 3 | .\" | ||
| 4 | .\" This code is derived from software contributed to Berkeley by | ||
| 5 | .\" the American National Standards Committee X3, on Information | ||
| 6 | .\" Processing Systems. | ||
| 7 | .\" | ||
| 8 | .\" Redistribution and use in source and binary forms, with or without | ||
| 9 | .\" modification, are permitted provided that the following conditions | ||
| 10 | .\" are met: | ||
| 11 | .\" 1. Redistributions of source code must retain the above copyright | ||
| 12 | .\" notice, this list of conditions and the following disclaimer. | ||
| 13 | .\" 2. Redistributions in binary form must reproduce the above copyright | ||
| 14 | .\" notice, this list of conditions and the following disclaimer in the | ||
| 15 | .\" documentation and/or other materials provided with the distribution. | ||
| 16 | .\" 3. All advertising materials mentioning features or use of this software | ||
| 17 | .\" must display the following acknowledgement: | ||
| 18 | .\" This product includes software developed by the University of | ||
| 19 | .\" California, Berkeley and its contributors. | ||
| 20 | .\" 4. Neither the name of the University nor the names of its contributors | ||
| 21 | .\" may be used to endorse or promote products derived from this software | ||
| 22 | .\" without specific prior written permission. | ||
| 23 | .\" | ||
| 24 | .\" THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND | ||
| 25 | .\" ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE | ||
| 26 | .\" IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE | ||
| 27 | .\" ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE | ||
| 28 | .\" FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL | ||
| 29 | .\" DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS | ||
| 30 | .\" OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) | ||
| 31 | .\" HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT | ||
| 32 | .\" LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY | ||
| 33 | .\" OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF | ||
| 34 | .\" SUCH DAMAGE. | ||
| 35 | .\" | ||
| 36 | .\" from: @(#)getenv.3 6.11 (Berkeley) 6/29/91 | ||
| 37 | .\" $Id: getenv.3,v 1.1.1.1 1995/10/18 08:42:17 deraadt Exp $ | ||
| 38 | .\" | ||
| 39 | .Dd June 29, 1991 | ||
| 40 | .Dt GETENV 3 | ||
| 41 | .Os | ||
| 42 | .Sh NAME | ||
| 43 | .Nm getenv , | ||
| 44 | .Nm putenv , | ||
| 45 | .Nm setenv , | ||
| 46 | .Nm unsetenv | ||
| 47 | .Nd environment variable functions | ||
| 48 | .Sh SYNOPSIS | ||
| 49 | .Fd #include <stdlib.h> | ||
| 50 | .Ft char * | ||
| 51 | .Fn getenv "const char *name" | ||
| 52 | .Ft int | ||
| 53 | .Fn setenv "const char *name" "const char *value" "int overwrite" | ||
| 54 | .Ft int | ||
| 55 | .Fn putenv "const char *string" | ||
| 56 | .Ft void | ||
| 57 | .Fn unsetenv "const char *name" | ||
| 58 | .Sh DESCRIPTION | ||
| 59 | These functions set, unset and fetch environment variables from the | ||
| 60 | host | ||
| 61 | .Em environment list . | ||
| 62 | For compatibility with differing environment conventions, | ||
| 63 | the given arguments | ||
| 64 | .Ar name | ||
| 65 | and | ||
| 66 | .Ar value | ||
| 67 | may be appended and prepended, | ||
| 68 | respectively, | ||
| 69 | with an equal sign | ||
| 70 | .Dq Li \&= . | ||
| 71 | .Pp | ||
| 72 | The | ||
| 73 | .Fn getenv | ||
| 74 | function obtains the current value of the environment variable, | ||
| 75 | .Ar name . | ||
| 76 | If the variable | ||
| 77 | .Ar name | ||
| 78 | is not in the current environment , | ||
| 79 | a null pointer is returned. | ||
| 80 | .Pp | ||
| 81 | The | ||
| 82 | .Fn setenv | ||
| 83 | function inserts or resets the environment variable | ||
| 84 | .Ar name | ||
| 85 | in the current environment list. | ||
| 86 | If the variable | ||
| 87 | .Ar name | ||
| 88 | does not exist in the list, | ||
| 89 | it is inserted with the given | ||
| 90 | .Ar value. | ||
| 91 | If the variable does exist, the argument | ||
| 92 | .Ar overwrite | ||
| 93 | is tested; if | ||
| 94 | .Ar overwrite is | ||
| 95 | zero, the | ||
| 96 | variable is not reset, otherwise it is reset | ||
| 97 | to the given | ||
| 98 | .Ar value . | ||
| 99 | .Pp | ||
| 100 | The | ||
| 101 | .Fn putenv | ||
| 102 | function takes an argument of the form ``name=value'' and is | ||
| 103 | equivalent to: | ||
| 104 | .Bd -literal -offset indent | ||
| 105 | setenv(name, value, 1); | ||
| 106 | .Ed | ||
| 107 | .Pp | ||
| 108 | The | ||
| 109 | .Fn unsetenv | ||
| 110 | function | ||
| 111 | deletes all instances of the variable name pointed to by | ||
| 112 | .Fa name | ||
| 113 | from the list. | ||
| 114 | .Sh RETURN VALUES | ||
| 115 | The functions | ||
| 116 | .Fn setenv | ||
| 117 | and | ||
| 118 | .Fn putenv | ||
| 119 | return zero if successful; otherwise the global variable | ||
| 120 | .Va errno | ||
| 121 | is set to indicate the error and a | ||
| 122 | \-1 is returned. | ||
| 123 | .Sh ERRORS | ||
| 124 | .Bl -tag -width Er | ||
| 125 | .It Bq Er ENOMEM | ||
| 126 | The function | ||
| 127 | .Fn setenv | ||
| 128 | or | ||
| 129 | .Fn putenv | ||
| 130 | failed because they were unable to allocate memory for the environment. | ||
| 131 | .El | ||
| 132 | .Sh SEE ALSO | ||
| 133 | .Xr csh 1 , | ||
| 134 | .Xr sh 1 , | ||
| 135 | .Xr execve 2 , | ||
| 136 | .Xr environ 7 | ||
| 137 | .Sh STANDARDS | ||
| 138 | The | ||
| 139 | .Fn getenv | ||
| 140 | function conforms to | ||
| 141 | .St -ansiC . | ||
| 142 | .Sh HISTORY | ||
| 143 | The functions | ||
| 144 | .Fn setenv | ||
| 145 | and | ||
| 146 | .Fn unsetenv | ||
| 147 | appeared in | ||
| 148 | .At v7 . | ||
| 149 | The | ||
| 150 | .Fn putenv | ||
| 151 | function appeared in | ||
| 152 | .Bx 4.3 Reno . | ||
diff --git a/src/lib/libc/stdlib/getenv.c b/src/lib/libc/stdlib/getenv.c new file mode 100644 index 0000000000..09d47f2149 --- /dev/null +++ b/src/lib/libc/stdlib/getenv.c | |||
| @@ -0,0 +1,82 @@ | |||
| 1 | /* | ||
| 2 | * Copyright (c) 1987 Regents of the University of California. | ||
| 3 | * All rights reserved. | ||
| 4 | * | ||
| 5 | * Redistribution and use in source and binary forms, with or without | ||
| 6 | * modification, are permitted provided that the following conditions | ||
| 7 | * are met: | ||
| 8 | * 1. Redistributions of source code must retain the above copyright | ||
| 9 | * notice, this list of conditions and the following disclaimer. | ||
| 10 | * 2. Redistributions in binary form must reproduce the above copyright | ||
| 11 | * notice, this list of conditions and the following disclaimer in the | ||
| 12 | * documentation and/or other materials provided with the distribution. | ||
| 13 | * 3. All advertising materials mentioning features or use of this software | ||
| 14 | * must display the following acknowledgement: | ||
| 15 | * This product includes software developed by the University of | ||
| 16 | * California, Berkeley and its contributors. | ||
| 17 | * 4. Neither the name of the University nor the names of its contributors | ||
| 18 | * may be used to endorse or promote products derived from this software | ||
| 19 | * without specific prior written permission. | ||
| 20 | * | ||
| 21 | * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND | ||
| 22 | * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE | ||
| 23 | * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE | ||
| 24 | * ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE | ||
| 25 | * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL | ||
| 26 | * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS | ||
| 27 | * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) | ||
| 28 | * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT | ||
| 29 | * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY | ||
| 30 | * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF | ||
| 31 | * SUCH DAMAGE. | ||
| 32 | */ | ||
| 33 | |||
| 34 | #if defined(LIBC_SCCS) && !defined(lint) | ||
| 35 | /*static char *sccsid = "from: @(#)getenv.c 5.8 (Berkeley) 2/23/91";*/ | ||
| 36 | static char *rcsid = "$Id: getenv.c,v 1.1.1.1 1995/10/18 08:42:17 deraadt Exp $"; | ||
| 37 | #endif /* LIBC_SCCS and not lint */ | ||
| 38 | |||
| 39 | #include <stdlib.h> | ||
| 40 | #include <string.h> | ||
| 41 | |||
| 42 | /* | ||
| 43 | * getenv -- | ||
| 44 | * Returns ptr to value associated with name, if any, else NULL. | ||
| 45 | */ | ||
| 46 | char * | ||
| 47 | getenv(name) | ||
| 48 | const char *name; | ||
| 49 | { | ||
| 50 | int offset; | ||
| 51 | char *__findenv(); | ||
| 52 | |||
| 53 | return(__findenv(name, &offset)); | ||
| 54 | } | ||
| 55 | |||
| 56 | /* | ||
| 57 | * __findenv -- | ||
| 58 | * Returns pointer to value associated with name, if any, else NULL. | ||
| 59 | * Sets offset to be the offset of the name/value combination in the | ||
| 60 | * environmental array, for use by setenv(3) and unsetenv(3). | ||
| 61 | * Explicitly removes '=' in argument name. | ||
| 62 | * | ||
| 63 | * This routine *should* be a static; don't use it. | ||
| 64 | */ | ||
| 65 | char * | ||
| 66 | __findenv(name, offset) | ||
| 67 | register char *name; | ||
| 68 | int *offset; | ||
| 69 | { | ||
| 70 | extern char **environ; | ||
| 71 | register int len; | ||
| 72 | register char **P, *C; | ||
| 73 | |||
| 74 | for (C = name, len = 0; *C && *C != '='; ++C, ++len); | ||
| 75 | for (P = environ; *P; ++P) | ||
| 76 | if (!strncmp(*P, name, len)) | ||
| 77 | if (*(C = *P + len) == '=') { | ||
| 78 | *offset = P - environ; | ||
| 79 | return(++C); | ||
| 80 | } | ||
| 81 | return(NULL); | ||
| 82 | } | ||
diff --git a/src/lib/libc/stdlib/getopt.3 b/src/lib/libc/stdlib/getopt.3 new file mode 100644 index 0000000000..f843881afd --- /dev/null +++ b/src/lib/libc/stdlib/getopt.3 | |||
| @@ -0,0 +1,260 @@ | |||
| 1 | .\" Copyright (c) 1988, 1991, 1993 | ||
| 2 | .\" The Regents of the University of California. All rights reserved. | ||
| 3 | .\" | ||
| 4 | .\" Redistribution and use in source and binary forms, with or without | ||
| 5 | .\" modification, are permitted provided that the following conditions | ||
| 6 | .\" are met: | ||
| 7 | .\" 1. Redistributions of source code must retain the above copyright | ||
| 8 | .\" notice, this list of conditions and the following disclaimer. | ||
| 9 | .\" 2. Redistributions in binary form must reproduce the above copyright | ||
| 10 | .\" notice, this list of conditions and the following disclaimer in the | ||
| 11 | .\" documentation and/or other materials provided with the distribution. | ||
| 12 | .\" 3. All advertising materials mentioning features or use of this software | ||
| 13 | .\" must display the following acknowledgement: | ||
| 14 | .\" This product includes software developed by the University of | ||
| 15 | .\" California, Berkeley and its contributors. | ||
| 16 | .\" 4. Neither the name of the University nor the names of its contributors | ||
| 17 | .\" may be used to endorse or promote products derived from this software | ||
| 18 | .\" without specific prior written permission. | ||
| 19 | .\" | ||
| 20 | .\" THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND | ||
| 21 | .\" ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE | ||
| 22 | .\" IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE | ||
| 23 | .\" ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE | ||
| 24 | .\" FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL | ||
| 25 | .\" DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS | ||
| 26 | .\" OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) | ||
| 27 | .\" HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT | ||
| 28 | .\" LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY | ||
| 29 | .\" OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF | ||
| 30 | .\" SUCH DAMAGE. | ||
| 31 | .\" | ||
| 32 | .\" @(#)getopt.3 8.4 (Berkeley) 4/19/94 | ||
| 33 | .\" | ||
| 34 | .Dd April 19, 1994 | ||
| 35 | .Dt GETOPT 3 | ||
| 36 | .Os BSD 4.3 | ||
| 37 | .Sh NAME | ||
| 38 | .Nm getopt | ||
| 39 | .Nd get option character from command line argument list | ||
| 40 | .Sh SYNOPSIS | ||
| 41 | .Fd #include <unistd.h> | ||
| 42 | .Vt extern char *optarg; | ||
| 43 | .Vt extern int optind; | ||
| 44 | .Vt extern int optopt; | ||
| 45 | .Vt extern int opterr; | ||
| 46 | .Vt extern int optreset; | ||
| 47 | .Ft int | ||
| 48 | .Fn getopt "int argc" "char * const *argv" "const char *optstring" | ||
| 49 | .Sh DESCRIPTION | ||
| 50 | The | ||
| 51 | .Fn getopt | ||
| 52 | function incrementally parses a command line argument list | ||
| 53 | .Fa argv | ||
| 54 | and returns the next | ||
| 55 | .Em known | ||
| 56 | option character. | ||
| 57 | An option character is | ||
| 58 | .Em known | ||
| 59 | if it has been specified in the string of accepted option characters, | ||
| 60 | .Fa optstring . | ||
| 61 | .Pp | ||
| 62 | The option string | ||
| 63 | .Fa optstring | ||
| 64 | may contain the following elements: individual characters, and | ||
| 65 | characters followed by a colon to indicate an option argument | ||
| 66 | is to follow. | ||
| 67 | For example, an option string | ||
| 68 | .Li "\&""x"" | ||
| 69 | recognizes an option | ||
| 70 | .Dq Fl x , | ||
| 71 | and an option string | ||
| 72 | .Li "\&""x:"" | ||
| 73 | recognizes an option and argument | ||
| 74 | .Dq Fl x Ar argument . | ||
| 75 | It does not matter to | ||
| 76 | .Fn getopt | ||
| 77 | if a following argument has leading white space. | ||
| 78 | .Pp | ||
| 79 | On return from | ||
| 80 | .Fn getopt , | ||
| 81 | .Va optarg | ||
| 82 | points to an option argument, if it is anticipated, | ||
| 83 | and the variable | ||
| 84 | .Va optind | ||
| 85 | contains the index to the next | ||
| 86 | .Fa argv | ||
| 87 | argument for a subsequent call | ||
| 88 | to | ||
| 89 | .Fn getopt . | ||
| 90 | The variable | ||
| 91 | .Va optopt | ||
| 92 | saves the last | ||
| 93 | .Em known | ||
| 94 | option character returned by | ||
| 95 | .Fn getopt . | ||
| 96 | .Pp | ||
| 97 | The variable | ||
| 98 | .Va opterr | ||
| 99 | and | ||
| 100 | .Va optind | ||
| 101 | are both initialized to 1. | ||
| 102 | The | ||
| 103 | .Va optind | ||
| 104 | variable may be set to another value before a set of calls to | ||
| 105 | .Fn getopt | ||
| 106 | in order to skip over more or less argv entries. | ||
| 107 | .Pp | ||
| 108 | In order to use | ||
| 109 | .Fn getopt | ||
| 110 | to evaluate multiple sets of arguments, or to evaluate a single set of | ||
| 111 | arguments multiple times, | ||
| 112 | the variable | ||
| 113 | .Va optreset | ||
| 114 | must be set to 1 before the second and each additional set of calls to | ||
| 115 | .Fn getopt , | ||
| 116 | and the variable | ||
| 117 | .Va optind | ||
| 118 | must be reinitialized. | ||
| 119 | .Pp | ||
| 120 | The | ||
| 121 | .Fn getopt | ||
| 122 | function | ||
| 123 | returns \-1 | ||
| 124 | when the argument list is exhausted, or a non-recognized | ||
| 125 | option is encountered. | ||
| 126 | The interpretation of options in the argument list may be cancelled | ||
| 127 | by the option | ||
| 128 | .Ql -- | ||
| 129 | (double dash) which causes | ||
| 130 | .Fn getopt | ||
| 131 | to signal the end of argument processing and returns \-1. | ||
| 132 | When all options have been processed (i.e., up to the first non-option | ||
| 133 | argument), | ||
| 134 | .Fn getopt | ||
| 135 | returns \-1. | ||
| 136 | .Sh DIAGNOSTICS | ||
| 137 | If the | ||
| 138 | .Fn getopt | ||
| 139 | function encounters a character not found in the string | ||
| 140 | .Va optarg | ||
| 141 | or detects | ||
| 142 | a missing option argument it writes an error message and returns | ||
| 143 | .Ql ? | ||
| 144 | to the | ||
| 145 | .Em stderr . | ||
| 146 | Setting | ||
| 147 | .Va opterr | ||
| 148 | to a zero will disable these error messages. | ||
| 149 | If | ||
| 150 | .Va optstring | ||
| 151 | has a leading | ||
| 152 | .Ql \&: | ||
| 153 | then a missing option argument causes a | ||
| 154 | .Ql \&: | ||
| 155 | to be returned in addition to suppressing any error messages. | ||
| 156 | .Pp | ||
| 157 | Option arguments are allowed to begin with | ||
| 158 | .Dq Li \- ; | ||
| 159 | this is reasonable but | ||
| 160 | reduces the amount of error checking possible. | ||
| 161 | .Sh EXTENSIONS | ||
| 162 | The | ||
| 163 | .Va optreset | ||
| 164 | variable was added to make it possible to call the | ||
| 165 | .Fn getopt | ||
| 166 | function multiple times. | ||
| 167 | This is an extension to the | ||
| 168 | .St -p1003.2 | ||
| 169 | specification. | ||
| 170 | .Sh EXAMPLE | ||
| 171 | .Bd -literal -compact | ||
| 172 | extern char *optarg; | ||
| 173 | extern int optind; | ||
| 174 | int bflag, ch, fd; | ||
| 175 | |||
| 176 | bflag = 0; | ||
| 177 | while ((ch = getopt(argc, argv, "bf:")) != -1) | ||
| 178 | switch(ch) { | ||
| 179 | case 'b': | ||
| 180 | bflag = 1; | ||
| 181 | break; | ||
| 182 | case 'f': | ||
| 183 | if ((fd = open(optarg, O_RDONLY, 0)) < 0) { | ||
| 184 | (void)fprintf(stderr, | ||
| 185 | "myname: %s: %s\en", optarg, strerror(errno)); | ||
| 186 | exit(1); | ||
| 187 | } | ||
| 188 | break; | ||
| 189 | case '?': | ||
| 190 | default: | ||
| 191 | usage(); | ||
| 192 | } | ||
| 193 | argc -= optind; | ||
| 194 | argv += optind; | ||
| 195 | .Ed | ||
| 196 | .Sh HISTORY | ||
| 197 | The | ||
| 198 | .Fn getopt | ||
| 199 | function appeared | ||
| 200 | .Bx 4.3 . | ||
| 201 | .Sh BUGS | ||
| 202 | The | ||
| 203 | .Fn getopt | ||
| 204 | function was once specified to return | ||
| 205 | .Dv EOF | ||
| 206 | instead of \-1. | ||
| 207 | This was changed by | ||
| 208 | .St -p1003.2-92 | ||
| 209 | to decouple | ||
| 210 | .Fn getopt | ||
| 211 | from | ||
| 212 | .Pa <stdio.h> . | ||
| 213 | .Pp | ||
| 214 | A single dash | ||
| 215 | .Dq Li - | ||
| 216 | may be specified as an character in | ||
| 217 | .Fa optstring , | ||
| 218 | however it should | ||
| 219 | .Em never | ||
| 220 | have an argument associated with it. | ||
| 221 | This allows | ||
| 222 | .Fn getopt | ||
| 223 | to be used with programs that expect | ||
| 224 | .Dq Li - | ||
| 225 | as an option flag. | ||
| 226 | This practice is wrong, and should not be used in any current development. | ||
| 227 | It is provided for backward compatibility | ||
| 228 | .Em only . | ||
| 229 | By default, a single dash causes | ||
| 230 | .Fn getopt | ||
| 231 | to return \-1. | ||
| 232 | This is, we believe, compatible with System V. | ||
| 233 | .Pp | ||
| 234 | It is also possible to handle digits as option letters. | ||
| 235 | This allows | ||
| 236 | .Fn getopt | ||
| 237 | to be used with programs that expect a number | ||
| 238 | .Pq Dq Li \&-\&3 | ||
| 239 | as an option. | ||
| 240 | This practice is wrong, and should not be used in any current development. | ||
| 241 | It is provided for backward compatibility | ||
| 242 | .Em only . | ||
| 243 | The following code fragment works in most cases. | ||
| 244 | .Bd -literal -offset indent | ||
| 245 | int length; | ||
| 246 | char *p; | ||
| 247 | |||
| 248 | while ((c = getopt(argc, argv, "0123456789")) != -1) | ||
| 249 | switch (c) { | ||
| 250 | case '0': case '1': case '2': case '3': case '4': | ||
| 251 | case '5': case '6': case '7': case '8': case '9': | ||
| 252 | p = argv[optind - 1]; | ||
| 253 | if (p[0] == '-' && p[1] == ch && !p[2]) | ||
| 254 | length = atoi(++p); | ||
| 255 | else | ||
| 256 | length = atoi(argv[optind] + 1); | ||
| 257 | break; | ||
| 258 | } | ||
| 259 | } | ||
| 260 | .Ed | ||
diff --git a/src/lib/libc/stdlib/getopt.c b/src/lib/libc/stdlib/getopt.c new file mode 100644 index 0000000000..63c5e6a479 --- /dev/null +++ b/src/lib/libc/stdlib/getopt.c | |||
| @@ -0,0 +1,118 @@ | |||
| 1 | /* | ||
| 2 | * Copyright (c) 1987, 1993, 1994 | ||
| 3 | * The Regents of the University of California. All rights reserved. | ||
| 4 | * | ||
| 5 | * Redistribution and use in source and binary forms, with or without | ||
| 6 | * modification, are permitted provided that the following conditions | ||
| 7 | * are met: | ||
| 8 | * 1. Redistributions of source code must retain the above copyright | ||
| 9 | * notice, this list of conditions and the following disclaimer. | ||
| 10 | * 2. Redistributions in binary form must reproduce the above copyright | ||
| 11 | * notice, this list of conditions and the following disclaimer in the | ||
| 12 | * documentation and/or other materials provided with the distribution. | ||
| 13 | * 3. All advertising materials mentioning features or use of this software | ||
| 14 | * must display the following acknowledgement: | ||
| 15 | * This product includes software developed by the University of | ||
| 16 | * California, Berkeley and its contributors. | ||
| 17 | * 4. Neither the name of the University nor the names of its contributors | ||
| 18 | * may be used to endorse or promote products derived from this software | ||
| 19 | * without specific prior written permission. | ||
| 20 | * | ||
| 21 | * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND | ||
| 22 | * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE | ||
| 23 | * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE | ||
| 24 | * ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE | ||
| 25 | * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL | ||
| 26 | * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS | ||
| 27 | * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) | ||
| 28 | * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT | ||
| 29 | * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY | ||
| 30 | * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF | ||
| 31 | * SUCH DAMAGE. | ||
| 32 | */ | ||
| 33 | |||
| 34 | #if defined(LIBC_SCCS) && !defined(lint) | ||
| 35 | /* static char sccsid[] = "from: @(#)getopt.c 8.2 (Berkeley) 4/2/94"; */ | ||
| 36 | static char *rcsid = "$Id: getopt.c,v 1.1.1.1 1995/10/18 08:42:17 deraadt Exp $"; | ||
| 37 | #endif /* LIBC_SCCS and not lint */ | ||
| 38 | |||
| 39 | #include <stdio.h> | ||
| 40 | #include <stdlib.h> | ||
| 41 | #include <string.h> | ||
| 42 | |||
| 43 | int opterr = 1, /* if error message should be printed */ | ||
| 44 | optind = 1, /* index into parent argv vector */ | ||
| 45 | optopt, /* character checked for validity */ | ||
| 46 | optreset; /* reset getopt */ | ||
| 47 | char *optarg; /* argument associated with option */ | ||
| 48 | |||
| 49 | #define BADCH (int)'?' | ||
| 50 | #define BADARG (int)':' | ||
| 51 | #define EMSG "" | ||
| 52 | |||
| 53 | /* | ||
| 54 | * getopt -- | ||
| 55 | * Parse argc/argv argument vector. | ||
| 56 | */ | ||
| 57 | int | ||
| 58 | getopt(nargc, nargv, ostr) | ||
| 59 | int nargc; | ||
| 60 | char * const *nargv; | ||
| 61 | const char *ostr; | ||
| 62 | { | ||
| 63 | extern char *__progname; | ||
| 64 | static char *place = EMSG; /* option letter processing */ | ||
| 65 | char *oli; /* option letter list index */ | ||
| 66 | |||
| 67 | if (optreset || !*place) { /* update scanning pointer */ | ||
| 68 | optreset = 0; | ||
| 69 | if (optind >= nargc || *(place = nargv[optind]) != '-') { | ||
| 70 | place = EMSG; | ||
| 71 | return (-1); | ||
| 72 | } | ||
| 73 | if (place[1] && *++place == '-') { /* found "--" */ | ||
| 74 | ++optind; | ||
| 75 | place = EMSG; | ||
| 76 | return (-1); | ||
| 77 | } | ||
| 78 | } /* option letter okay? */ | ||
| 79 | if ((optopt = (int)*place++) == (int)':' || | ||
| 80 | !(oli = strchr(ostr, optopt))) { | ||
| 81 | /* | ||
| 82 | * if the user didn't specify '-' as an option, | ||
| 83 | * assume it means -1. | ||
| 84 | */ | ||
| 85 | if (optopt == (int)'-') | ||
| 86 | return (-1); | ||
| 87 | if (!*place) | ||
| 88 | ++optind; | ||
| 89 | if (opterr && *ostr != ':') | ||
| 90 | (void)fprintf(stderr, | ||
| 91 | "%s: illegal option -- %c\n", __progname, optopt); | ||
| 92 | return (BADCH); | ||
| 93 | } | ||
| 94 | if (*++oli != ':') { /* don't need argument */ | ||
| 95 | optarg = NULL; | ||
| 96 | if (!*place) | ||
| 97 | ++optind; | ||
| 98 | } | ||
| 99 | else { /* need an argument */ | ||
| 100 | if (*place) /* no white space */ | ||
| 101 | optarg = place; | ||
| 102 | else if (nargc <= ++optind) { /* no arg */ | ||
| 103 | place = EMSG; | ||
| 104 | if (*ostr == ':') | ||
| 105 | return (BADARG); | ||
| 106 | if (opterr) | ||
| 107 | (void)fprintf(stderr, | ||
| 108 | "%s: option requires an argument -- %c\n", | ||
| 109 | __progname, optopt); | ||
| 110 | return (BADCH); | ||
| 111 | } | ||
| 112 | else /* white space */ | ||
| 113 | optarg = nargv[optind]; | ||
| 114 | place = EMSG; | ||
| 115 | ++optind; | ||
| 116 | } | ||
| 117 | return (optopt); /* dump back option letter */ | ||
| 118 | } | ||
diff --git a/src/lib/libc/stdlib/heapsort.c b/src/lib/libc/stdlib/heapsort.c new file mode 100644 index 0000000000..bd998fa357 --- /dev/null +++ b/src/lib/libc/stdlib/heapsort.c | |||
| @@ -0,0 +1,184 @@ | |||
| 1 | /*- | ||
| 2 | * Copyright (c) 1991, 1993 | ||
| 3 | * The Regents of the University of California. All rights reserved. | ||
| 4 | * | ||
| 5 | * This code is derived from software contributed to Berkeley by | ||
| 6 | * Ronnie Kon at Mindcraft Inc., Kevin Lew and Elmer Yglesias. | ||
| 7 | * | ||
| 8 | * Redistribution and use in source and binary forms, with or without | ||
| 9 | * modification, are permitted provided that the following conditions | ||
| 10 | * are met: | ||
| 11 | * 1. Redistributions of source code must retain the above copyright | ||
| 12 | * notice, this list of conditions and the following disclaimer. | ||
| 13 | * 2. Redistributions in binary form must reproduce the above copyright | ||
| 14 | * notice, this list of conditions and the following disclaimer in the | ||
| 15 | * documentation and/or other materials provided with the distribution. | ||
| 16 | * 3. All advertising materials mentioning features or use of this software | ||
| 17 | * must display the following acknowledgement: | ||
| 18 | * This product includes software developed by the University of | ||
| 19 | * California, Berkeley and its contributors. | ||
| 20 | * 4. Neither the name of the University nor the names of its contributors | ||
| 21 | * may be used to endorse or promote products derived from this software | ||
| 22 | * without specific prior written permission. | ||
| 23 | * | ||
| 24 | * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND | ||
| 25 | * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE | ||
| 26 | * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE | ||
| 27 | * ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE | ||
| 28 | * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL | ||
| 29 | * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS | ||
| 30 | * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) | ||
| 31 | * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT | ||
| 32 | * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY | ||
| 33 | * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF | ||
| 34 | * SUCH DAMAGE. | ||
| 35 | */ | ||
| 36 | |||
| 37 | #if defined(LIBC_SCCS) && !defined(lint) | ||
| 38 | /*static char sccsid[] = "from: @(#)heapsort.c 8.1 (Berkeley) 6/4/93";*/ | ||
| 39 | static char *rcsid = "$Id: heapsort.c,v 1.1.1.1 1995/10/18 08:42:17 deraadt Exp $"; | ||
| 40 | #endif /* LIBC_SCCS and not lint */ | ||
| 41 | |||
| 42 | #include <sys/types.h> | ||
| 43 | #include <errno.h> | ||
| 44 | #include <stdlib.h> | ||
| 45 | |||
| 46 | /* | ||
| 47 | * Swap two areas of size number of bytes. Although qsort(3) permits random | ||
| 48 | * blocks of memory to be sorted, sorting pointers is almost certainly the | ||
| 49 | * common case (and, were it not, could easily be made so). Regardless, it | ||
| 50 | * isn't worth optimizing; the SWAP's get sped up by the cache, and pointer | ||
| 51 | * arithmetic gets lost in the time required for comparison function calls. | ||
| 52 | */ | ||
| 53 | #define SWAP(a, b, count, size, tmp) { \ | ||
| 54 | count = size; \ | ||
| 55 | do { \ | ||
| 56 | tmp = *a; \ | ||
| 57 | *a++ = *b; \ | ||
| 58 | *b++ = tmp; \ | ||
| 59 | } while (--count); \ | ||
| 60 | } | ||
| 61 | |||
| 62 | /* Copy one block of size size to another. */ | ||
| 63 | #define COPY(a, b, count, size, tmp1, tmp2) { \ | ||
| 64 | count = size; \ | ||
| 65 | tmp1 = a; \ | ||
| 66 | tmp2 = b; \ | ||
| 67 | do { \ | ||
| 68 | *tmp1++ = *tmp2++; \ | ||
| 69 | } while (--count); \ | ||
| 70 | } | ||
| 71 | |||
| 72 | /* | ||
| 73 | * Build the list into a heap, where a heap is defined such that for | ||
| 74 | * the records K1 ... KN, Kj/2 >= Kj for 1 <= j/2 <= j <= N. | ||
| 75 | * | ||
| 76 | * There two cases. If j == nmemb, select largest of Ki and Kj. If | ||
| 77 | * j < nmemb, select largest of Ki, Kj and Kj+1. | ||
| 78 | */ | ||
| 79 | #define CREATE(initval, nmemb, par_i, child_i, par, child, size, count, tmp) { \ | ||
| 80 | for (par_i = initval; (child_i = par_i * 2) <= nmemb; \ | ||
| 81 | par_i = child_i) { \ | ||
| 82 | child = base + child_i * size; \ | ||
| 83 | if (child_i < nmemb && compar(child, child + size) < 0) { \ | ||
| 84 | child += size; \ | ||
| 85 | ++child_i; \ | ||
| 86 | } \ | ||
| 87 | par = base + par_i * size; \ | ||
| 88 | if (compar(child, par) <= 0) \ | ||
| 89 | break; \ | ||
| 90 | SWAP(par, child, count, size, tmp); \ | ||
| 91 | } \ | ||
| 92 | } | ||
| 93 | |||
| 94 | /* | ||
| 95 | * Select the top of the heap and 'heapify'. Since by far the most expensive | ||
| 96 | * action is the call to the compar function, a considerable optimization | ||
| 97 | * in the average case can be achieved due to the fact that k, the displaced | ||
| 98 | * elememt, is ususally quite small, so it would be preferable to first | ||
| 99 | * heapify, always maintaining the invariant that the larger child is copied | ||
| 100 | * over its parent's record. | ||
| 101 | * | ||
| 102 | * Then, starting from the *bottom* of the heap, finding k's correct place, | ||
| 103 | * again maintianing the invariant. As a result of the invariant no element | ||
| 104 | * is 'lost' when k is assigned its correct place in the heap. | ||
| 105 | * | ||
| 106 | * The time savings from this optimization are on the order of 15-20% for the | ||
| 107 | * average case. See Knuth, Vol. 3, page 158, problem 18. | ||
| 108 | * | ||
| 109 | * XXX Don't break the #define SELECT line, below. Reiser cpp gets upset. | ||
| 110 | */ | ||
| 111 | #define SELECT(par_i, child_i, nmemb, par, child, size, k, count, tmp1, tmp2) { \ | ||
| 112 | for (par_i = 1; (child_i = par_i * 2) <= nmemb; par_i = child_i) { \ | ||
| 113 | child = base + child_i * size; \ | ||
| 114 | if (child_i < nmemb && compar(child, child + size) < 0) { \ | ||
| 115 | child += size; \ | ||
| 116 | ++child_i; \ | ||
| 117 | } \ | ||
| 118 | par = base + par_i * size; \ | ||
| 119 | COPY(par, child, count, size, tmp1, tmp2); \ | ||
| 120 | } \ | ||
| 121 | for (;;) { \ | ||
| 122 | child_i = par_i; \ | ||
| 123 | par_i = child_i / 2; \ | ||
| 124 | child = base + child_i * size; \ | ||
| 125 | par = base + par_i * size; \ | ||
| 126 | if (child_i == 1 || compar(k, par) < 0) { \ | ||
| 127 | COPY(child, k, count, size, tmp1, tmp2); \ | ||
| 128 | break; \ | ||
| 129 | } \ | ||
| 130 | COPY(child, par, count, size, tmp1, tmp2); \ | ||
| 131 | } \ | ||
| 132 | } | ||
| 133 | |||
| 134 | /* | ||
| 135 | * Heapsort -- Knuth, Vol. 3, page 145. Runs in O (N lg N), both average | ||
| 136 | * and worst. While heapsort is faster than the worst case of quicksort, | ||
| 137 | * the BSD quicksort does median selection so that the chance of finding | ||
| 138 | * a data set that will trigger the worst case is nonexistent. Heapsort's | ||
| 139 | * only advantage over quicksort is that it requires little additional memory. | ||
| 140 | */ | ||
| 141 | int | ||
| 142 | heapsort(vbase, nmemb, size, compar) | ||
| 143 | void *vbase; | ||
| 144 | size_t nmemb, size; | ||
| 145 | int (*compar) __P((const void *, const void *)); | ||
| 146 | { | ||
| 147 | register int cnt, i, j, l; | ||
| 148 | register char tmp, *tmp1, *tmp2; | ||
| 149 | char *base, *k, *p, *t; | ||
| 150 | |||
| 151 | if (nmemb <= 1) | ||
| 152 | return (0); | ||
| 153 | |||
| 154 | if (!size) { | ||
| 155 | errno = EINVAL; | ||
| 156 | return (-1); | ||
| 157 | } | ||
| 158 | |||
| 159 | if ((k = malloc(size)) == NULL) | ||
| 160 | return (-1); | ||
| 161 | |||
| 162 | /* | ||
| 163 | * Items are numbered from 1 to nmemb, so offset from size bytes | ||
| 164 | * below the starting address. | ||
| 165 | */ | ||
| 166 | base = (char *)vbase - size; | ||
| 167 | |||
| 168 | for (l = nmemb / 2 + 1; --l;) | ||
| 169 | CREATE(l, nmemb, i, j, t, p, size, cnt, tmp); | ||
| 170 | |||
| 171 | /* | ||
| 172 | * For each element of the heap, save the largest element into its | ||
| 173 | * final slot, save the displaced element (k), then recreate the | ||
| 174 | * heap. | ||
| 175 | */ | ||
| 176 | while (nmemb > 1) { | ||
| 177 | COPY(k, base + nmemb * size, cnt, size, tmp1, tmp2); | ||
| 178 | COPY(base + nmemb * size, base + size, cnt, size, tmp1, tmp2); | ||
| 179 | --nmemb; | ||
| 180 | SELECT(i, j, nmemb, t, p, size, k, cnt, tmp1, tmp2); | ||
| 181 | } | ||
| 182 | free(k); | ||
| 183 | return (0); | ||
| 184 | } | ||
diff --git a/src/lib/libc/stdlib/jrand48.c b/src/lib/libc/stdlib/jrand48.c new file mode 100644 index 0000000000..205781e0ee --- /dev/null +++ b/src/lib/libc/stdlib/jrand48.c | |||
| @@ -0,0 +1,21 @@ | |||
| 1 | /* | ||
| 2 | * Copyright (c) 1993 Martin Birgmeier | ||
| 3 | * All rights reserved. | ||
| 4 | * | ||
| 5 | * You may redistribute unmodified or modified versions of this source | ||
| 6 | * code provided that the above copyright notice and this and the | ||
| 7 | * following conditions are retained. | ||
| 8 | * | ||
| 9 | * This software is provided ``as is'', and comes with no warranties | ||
| 10 | * of any kind. I shall in no event be liable for anything that happens | ||
| 11 | * to anyone/anything when using this software. | ||
| 12 | */ | ||
| 13 | |||
| 14 | #include "rand48.h" | ||
| 15 | |||
| 16 | long | ||
| 17 | jrand48(unsigned short xseed[3]) | ||
| 18 | { | ||
| 19 | __dorand48(xseed); | ||
| 20 | return ((long) xseed[2] << 16) + (long) xseed[1]; | ||
| 21 | } | ||
diff --git a/src/lib/libc/stdlib/l64a.c b/src/lib/libc/stdlib/l64a.c new file mode 100644 index 0000000000..3069b31bf6 --- /dev/null +++ b/src/lib/libc/stdlib/l64a.c | |||
| @@ -0,0 +1,43 @@ | |||
| 1 | /* | ||
| 2 | * Written by J.T. Conklin <jtc@netbsd.org>. | ||
| 3 | * Public domain. | ||
| 4 | */ | ||
| 5 | |||
| 6 | #if defined(LIBC_SCCS) && !defined(lint) | ||
| 7 | static char *rcsid = "$NetBSD: l64a.c,v 1.4 1995/05/11 23:04:52 jtc Exp $"; | ||
| 8 | #endif | ||
| 9 | |||
| 10 | #include <stdlib.h> | ||
| 11 | |||
| 12 | char * | ||
| 13 | l64a (value) | ||
| 14 | long value; | ||
| 15 | { | ||
| 16 | static char buf[8]; | ||
| 17 | char *s = buf; | ||
| 18 | int digit; | ||
| 19 | int i; | ||
| 20 | |||
| 21 | if (!value) | ||
| 22 | return NULL; | ||
| 23 | |||
| 24 | for (i = 0; value != 0 && i < 6; i++) { | ||
| 25 | digit = value & 0x3f; | ||
| 26 | |||
| 27 | if (digit < 2) | ||
| 28 | *s = digit + '.'; | ||
| 29 | else if (digit < 12) | ||
| 30 | *s = digit + '0' - 2; | ||
| 31 | else if (digit < 38) | ||
| 32 | *s = digit + 'A' - 12; | ||
| 33 | else | ||
| 34 | *s = digit + 'a' - 38; | ||
| 35 | |||
| 36 | value >>= 6; | ||
| 37 | s++; | ||
| 38 | } | ||
| 39 | |||
| 40 | *s = '\0'; | ||
| 41 | |||
| 42 | return buf; | ||
| 43 | } | ||
diff --git a/src/lib/libc/stdlib/labs.3 b/src/lib/libc/stdlib/labs.3 new file mode 100644 index 0000000000..28e4d2053c --- /dev/null +++ b/src/lib/libc/stdlib/labs.3 | |||
| @@ -0,0 +1,67 @@ | |||
| 1 | .\" Copyright (c) 1990, 1991 The Regents of the University of California. | ||
| 2 | .\" All rights reserved. | ||
| 3 | .\" | ||
| 4 | .\" This code is derived from software contributed to Berkeley by | ||
| 5 | .\" the American National Standards Committee X3, on Information | ||
| 6 | .\" Processing Systems. | ||
| 7 | .\" | ||
| 8 | .\" Redistribution and use in source and binary forms, with or without | ||
| 9 | .\" modification, are permitted provided that the following conditions | ||
| 10 | .\" are met: | ||
| 11 | .\" 1. Redistributions of source code must retain the above copyright | ||
| 12 | .\" notice, this list of conditions and the following disclaimer. | ||
| 13 | .\" 2. Redistributions in binary form must reproduce the above copyright | ||
| 14 | .\" notice, this list of conditions and the following disclaimer in the | ||
| 15 | .\" documentation and/or other materials provided with the distribution. | ||
| 16 | .\" 3. All advertising materials mentioning features or use of this software | ||
| 17 | .\" must display the following acknowledgement: | ||
| 18 | .\" This product includes software developed by the University of | ||
| 19 | .\" California, Berkeley and its contributors. | ||
| 20 | .\" 4. Neither the name of the University nor the names of its contributors | ||
| 21 | .\" may be used to endorse or promote products derived from this software | ||
| 22 | .\" without specific prior written permission. | ||
| 23 | .\" | ||
| 24 | .\" THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND | ||
| 25 | .\" ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE | ||
| 26 | .\" IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE | ||
| 27 | .\" ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE | ||
| 28 | .\" FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL | ||
| 29 | .\" DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS | ||
| 30 | .\" OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) | ||
| 31 | .\" HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT | ||
| 32 | .\" LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY | ||
| 33 | .\" OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF | ||
| 34 | .\" SUCH DAMAGE. | ||
| 35 | .\" | ||
| 36 | .\" from: @(#)labs.3 5.3 (Berkeley) 6/29/91 | ||
| 37 | .\" $Id: labs.3,v 1.1.1.1 1995/10/18 08:42:17 deraadt Exp $ | ||
| 38 | .\" | ||
| 39 | .Dd June 29, 1991 | ||
| 40 | .Dt LABS 3 | ||
| 41 | .Os | ||
| 42 | .Sh NAME | ||
| 43 | .Nm labs | ||
| 44 | .Nd return the absolute value of a long integer | ||
| 45 | .Sh SYNOPSIS | ||
| 46 | .Fd #include <stdlib.h> | ||
| 47 | .Ft long | ||
| 48 | .Fn labs "long j" | ||
| 49 | .Sh DESCRIPTION | ||
| 50 | The | ||
| 51 | .Fn labs | ||
| 52 | function | ||
| 53 | returns the absolute value of the long integer | ||
| 54 | .Ar j . | ||
| 55 | .Sh SEE ALSO | ||
| 56 | .Xr abs 3 , | ||
| 57 | .Xr floor 3 , | ||
| 58 | .Xr cabs 3 , | ||
| 59 | .Xr math 3 | ||
| 60 | .Sh STANDARDS | ||
| 61 | The | ||
| 62 | .Fn labs | ||
| 63 | function | ||
| 64 | conforms to | ||
| 65 | .St -ansiC . | ||
| 66 | .Sh BUGS | ||
| 67 | The absolute value of the most negative integer remains negative. | ||
diff --git a/src/lib/libc/stdlib/labs.c b/src/lib/libc/stdlib/labs.c new file mode 100644 index 0000000000..ccf1415792 --- /dev/null +++ b/src/lib/libc/stdlib/labs.c | |||
| @@ -0,0 +1,46 @@ | |||
| 1 | /*- | ||
| 2 | * Copyright (c) 1990 The Regents of the University of California. | ||
| 3 | * All rights reserved. | ||
| 4 | * | ||
| 5 | * Redistribution and use in source and binary forms, with or without | ||
| 6 | * modification, are permitted provided that the following conditions | ||
| 7 | * are met: | ||
| 8 | * 1. Redistributions of source code must retain the above copyright | ||
| 9 | * notice, this list of conditions and the following disclaimer. | ||
| 10 | * 2. Redistributions in binary form must reproduce the above copyright | ||
| 11 | * notice, this list of conditions and the following disclaimer in the | ||
| 12 | * documentation and/or other materials provided with the distribution. | ||
| 13 | * 3. All advertising materials mentioning features or use of this software | ||
| 14 | * must display the following acknowledgement: | ||
| 15 | * This product includes software developed by the University of | ||
| 16 | * California, Berkeley and its contributors. | ||
| 17 | * 4. Neither the name of the University nor the names of its contributors | ||
| 18 | * may be used to endorse or promote products derived from this software | ||
| 19 | * without specific prior written permission. | ||
| 20 | * | ||
| 21 | * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND | ||
| 22 | * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE | ||
| 23 | * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE | ||
| 24 | * ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE | ||
| 25 | * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL | ||
| 26 | * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS | ||
| 27 | * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) | ||
| 28 | * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT | ||
| 29 | * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY | ||
| 30 | * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF | ||
| 31 | * SUCH DAMAGE. | ||
| 32 | */ | ||
| 33 | |||
| 34 | #if defined(LIBC_SCCS) && !defined(lint) | ||
| 35 | /*static char *sccsid = "from: @(#)labs.c 5.2 (Berkeley) 5/17/90";*/ | ||
| 36 | static char *rcsid = "$Id: labs.c,v 1.1.1.1 1995/10/18 08:42:17 deraadt Exp $"; | ||
| 37 | #endif /* LIBC_SCCS and not lint */ | ||
| 38 | |||
| 39 | #include <stdlib.h> | ||
| 40 | |||
| 41 | long | ||
| 42 | labs(j) | ||
| 43 | long j; | ||
| 44 | { | ||
| 45 | return(j < 0 ? -j : j); | ||
| 46 | } | ||
diff --git a/src/lib/libc/stdlib/lcong48.c b/src/lib/libc/stdlib/lcong48.c new file mode 100644 index 0000000000..965d46b17a --- /dev/null +++ b/src/lib/libc/stdlib/lcong48.c | |||
| @@ -0,0 +1,30 @@ | |||
| 1 | /* | ||
| 2 | * Copyright (c) 1993 Martin Birgmeier | ||
| 3 | * All rights reserved. | ||
| 4 | * | ||
| 5 | * You may redistribute unmodified or modified versions of this source | ||
| 6 | * code provided that the above copyright notice and this and the | ||
| 7 | * following conditions are retained. | ||
| 8 | * | ||
| 9 | * This software is provided ``as is'', and comes with no warranties | ||
| 10 | * of any kind. I shall in no event be liable for anything that happens | ||
| 11 | * to anyone/anything when using this software. | ||
| 12 | */ | ||
| 13 | |||
| 14 | #include "rand48.h" | ||
| 15 | |||
| 16 | extern unsigned short __rand48_seed[3]; | ||
| 17 | extern unsigned short __rand48_mult[3]; | ||
| 18 | extern unsigned short __rand48_add; | ||
| 19 | |||
| 20 | void | ||
| 21 | lcong48(unsigned short p[7]) | ||
| 22 | { | ||
| 23 | __rand48_seed[0] = p[0]; | ||
| 24 | __rand48_seed[1] = p[1]; | ||
| 25 | __rand48_seed[2] = p[2]; | ||
| 26 | __rand48_mult[0] = p[3]; | ||
| 27 | __rand48_mult[1] = p[4]; | ||
| 28 | __rand48_mult[2] = p[5]; | ||
| 29 | __rand48_add = p[6]; | ||
| 30 | } | ||
diff --git a/src/lib/libc/stdlib/ldiv.3 b/src/lib/libc/stdlib/ldiv.3 new file mode 100644 index 0000000000..a7b5ccf878 --- /dev/null +++ b/src/lib/libc/stdlib/ldiv.3 | |||
| @@ -0,0 +1,72 @@ | |||
| 1 | .\" Copyright (c) 1990, 1991 The Regents of the University of California. | ||
| 2 | .\" All rights reserved. | ||
| 3 | .\" | ||
| 4 | .\" This code is derived from software contributed to Berkeley by | ||
| 5 | .\" Chris Torek and the American National Standards Committee X3, | ||
| 6 | .\" on Information Processing Systems. | ||
| 7 | .\" | ||
| 8 | .\" Redistribution and use in source and binary forms, with or without | ||
| 9 | .\" modification, are permitted provided that the following conditions | ||
| 10 | .\" are met: | ||
| 11 | .\" 1. Redistributions of source code must retain the above copyright | ||
| 12 | .\" notice, this list of conditions and the following disclaimer. | ||
| 13 | .\" 2. Redistributions in binary form must reproduce the above copyright | ||
| 14 | .\" notice, this list of conditions and the following disclaimer in the | ||
| 15 | .\" documentation and/or other materials provided with the distribution. | ||
| 16 | .\" 3. All advertising materials mentioning features or use of this software | ||
| 17 | .\" must display the following acknowledgement: | ||
| 18 | .\" This product includes software developed by the University of | ||
| 19 | .\" California, Berkeley and its contributors. | ||
| 20 | .\" 4. Neither the name of the University nor the names of its contributors | ||
| 21 | .\" may be used to endorse or promote products derived from this software | ||
| 22 | .\" without specific prior written permission. | ||
| 23 | .\" | ||
| 24 | .\" THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND | ||
| 25 | .\" ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE | ||
| 26 | .\" IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE | ||
| 27 | .\" ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE | ||
| 28 | .\" FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL | ||
| 29 | .\" DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS | ||
| 30 | .\" OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) | ||
| 31 | .\" HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT | ||
| 32 | .\" LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY | ||
| 33 | .\" OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF | ||
| 34 | .\" SUCH DAMAGE. | ||
| 35 | .\" | ||
| 36 | .\" from: @(#)ldiv.3 5.3 (Berkeley) 6/29/91 | ||
| 37 | .\" $Id: ldiv.3,v 1.1.1.1 1995/10/18 08:42:18 deraadt Exp $ | ||
| 38 | .\" | ||
| 39 | .Dd June 29, 1991 | ||
| 40 | .Dt LDIV 3 | ||
| 41 | .Os | ||
| 42 | .Sh NAME | ||
| 43 | .Nm ldiv | ||
| 44 | .Nd return quotient and remainder from division | ||
| 45 | .Sh SYNOPSIS | ||
| 46 | .Fd #include <stdlib.h> | ||
| 47 | .Ft ldiv_t | ||
| 48 | .Fn ldiv "long num" "long denom" | ||
| 49 | .Sh DESCRIPTION | ||
| 50 | The | ||
| 51 | .Fn ldiv | ||
| 52 | function | ||
| 53 | computes the value | ||
| 54 | .Ar num/denom | ||
| 55 | and returns the quotient and remainder in a structure named | ||
| 56 | .Ar ldiv_t | ||
| 57 | that contains two | ||
| 58 | .Em long integer | ||
| 59 | members named | ||
| 60 | .Ar quot | ||
| 61 | and | ||
| 62 | .Ar rem . | ||
| 63 | .Sh SEE ALSO | ||
| 64 | .Xr div 3 , | ||
| 65 | .Xr qdiv 3 , | ||
| 66 | .Xr math 3 | ||
| 67 | .Sh STANDARDS | ||
| 68 | The | ||
| 69 | .Fn ldiv | ||
| 70 | function | ||
| 71 | conforms to | ||
| 72 | .St -ansiC . | ||
diff --git a/src/lib/libc/stdlib/ldiv.c b/src/lib/libc/stdlib/ldiv.c new file mode 100644 index 0000000000..f7074507e5 --- /dev/null +++ b/src/lib/libc/stdlib/ldiv.c | |||
| @@ -0,0 +1,59 @@ | |||
| 1 | /* | ||
| 2 | * Copyright (c) 1990 Regents of the University of California. | ||
| 3 | * All rights reserved. | ||
| 4 | * | ||
| 5 | * This code is derived from software contributed to Berkeley by | ||
| 6 | * Chris Torek. | ||
| 7 | * | ||
| 8 | * Redistribution and use in source and binary forms, with or without | ||
| 9 | * modification, are permitted provided that the following conditions | ||
| 10 | * are met: | ||
| 11 | * 1. Redistributions of source code must retain the above copyright | ||
| 12 | * notice, this list of conditions and the following disclaimer. | ||
| 13 | * 2. Redistributions in binary form must reproduce the above copyright | ||
| 14 | * notice, this list of conditions and the following disclaimer in the | ||
| 15 | * documentation and/or other materials provided with the distribution. | ||
| 16 | * 3. All advertising materials mentioning features or use of this software | ||
| 17 | * must display the following acknowledgement: | ||
| 18 | * This product includes software developed by the University of | ||
| 19 | * California, Berkeley and its contributors. | ||
| 20 | * 4. Neither the name of the University nor the names of its contributors | ||
| 21 | * may be used to endorse or promote products derived from this software | ||
| 22 | * without specific prior written permission. | ||
| 23 | * | ||
| 24 | * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND | ||
| 25 | * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE | ||
| 26 | * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE | ||
| 27 | * ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE | ||
| 28 | * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL | ||
| 29 | * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS | ||
| 30 | * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) | ||
| 31 | * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT | ||
| 32 | * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY | ||
| 33 | * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF | ||
| 34 | * SUCH DAMAGE. | ||
| 35 | */ | ||
| 36 | |||
| 37 | #if defined(LIBC_SCCS) && !defined(lint) | ||
| 38 | /*static char *sccsid = "from: @(#)ldiv.c 5.2 (Berkeley) 4/16/91";*/ | ||
| 39 | static char *rcsid = "$Id: ldiv.c,v 1.1.1.1 1995/10/18 08:42:18 deraadt Exp $"; | ||
| 40 | #endif /* LIBC_SCCS and not lint */ | ||
| 41 | |||
| 42 | #include <stdlib.h> /* ldiv_t */ | ||
| 43 | |||
| 44 | ldiv_t | ||
| 45 | ldiv(num, denom) | ||
| 46 | long num, denom; | ||
| 47 | { | ||
| 48 | ldiv_t r; | ||
| 49 | |||
| 50 | /* see div.c for comments */ | ||
| 51 | |||
| 52 | r.quot = num / denom; | ||
| 53 | r.rem = num % denom; | ||
| 54 | if (num >= 0 && r.rem < 0) { | ||
| 55 | r.quot++; | ||
| 56 | r.rem -= denom; | ||
| 57 | } | ||
| 58 | return (r); | ||
| 59 | } | ||
diff --git a/src/lib/libc/stdlib/lrand48.c b/src/lib/libc/stdlib/lrand48.c new file mode 100644 index 0000000000..8e7f26237f --- /dev/null +++ b/src/lib/libc/stdlib/lrand48.c | |||
| @@ -0,0 +1,23 @@ | |||
| 1 | /* | ||
| 2 | * Copyright (c) 1993 Martin Birgmeier | ||
| 3 | * All rights reserved. | ||
| 4 | * | ||
| 5 | * You may redistribute unmodified or modified versions of this source | ||
| 6 | * code provided that the above copyright notice and this and the | ||
| 7 | * following conditions are retained. | ||
| 8 | * | ||
| 9 | * This software is provided ``as is'', and comes with no warranties | ||
| 10 | * of any kind. I shall in no event be liable for anything that happens | ||
| 11 | * to anyone/anything when using this software. | ||
| 12 | */ | ||
| 13 | |||
| 14 | #include "rand48.h" | ||
| 15 | |||
| 16 | extern unsigned short __rand48_seed[3]; | ||
| 17 | |||
| 18 | long | ||
| 19 | lrand48(void) | ||
| 20 | { | ||
| 21 | __dorand48(__rand48_seed); | ||
| 22 | return ((long) __rand48_seed[2] << 15) + ((long) __rand48_seed[1] >> 1); | ||
| 23 | } | ||
diff --git a/src/lib/libc/stdlib/malloc.3 b/src/lib/libc/stdlib/malloc.3 new file mode 100644 index 0000000000..3bbf2bf65e --- /dev/null +++ b/src/lib/libc/stdlib/malloc.3 | |||
| @@ -0,0 +1,91 @@ | |||
| 1 | .\" Copyright (c) 1980, 1991 Regents of the University of California. | ||
| 2 | .\" All rights reserved. | ||
| 3 | .\" | ||
| 4 | .\" This code is derived from software contributed to Berkeley by | ||
| 5 | .\" the American National Standards Committee X3, on Information | ||
| 6 | .\" Processing Systems. | ||
| 7 | .\" | ||
| 8 | .\" Redistribution and use in source and binary forms, with or without | ||
| 9 | .\" modification, are permitted provided that the following conditions | ||
| 10 | .\" are met: | ||
| 11 | .\" 1. Redistributions of source code must retain the above copyright | ||
| 12 | .\" notice, this list of conditions and the following disclaimer. | ||
| 13 | .\" 2. Redistributions in binary form must reproduce the above copyright | ||
| 14 | .\" notice, this list of conditions and the following disclaimer in the | ||
| 15 | .\" documentation and/or other materials provided with the distribution. | ||
| 16 | .\" 3. All advertising materials mentioning features or use of this software | ||
| 17 | .\" must display the following acknowledgement: | ||
| 18 | .\" This product includes software developed by the University of | ||
| 19 | .\" California, Berkeley and its contributors. | ||
| 20 | .\" 4. Neither the name of the University nor the names of its contributors | ||
| 21 | .\" may be used to endorse or promote products derived from this software | ||
| 22 | .\" without specific prior written permission. | ||
| 23 | .\" | ||
| 24 | .\" THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND | ||
| 25 | .\" ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE | ||
| 26 | .\" IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE | ||
| 27 | .\" ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE | ||
| 28 | .\" FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL | ||
| 29 | .\" DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS | ||
| 30 | .\" OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) | ||
| 31 | .\" HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT | ||
| 32 | .\" LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY | ||
| 33 | .\" OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF | ||
| 34 | .\" SUCH DAMAGE. | ||
| 35 | .\" | ||
| 36 | .\" from: @(#)malloc.3 6.7 (Berkeley) 6/29/91 | ||
| 37 | .\" $Id: malloc.3,v 1.1.1.1 1995/10/18 08:42:18 deraadt Exp $ | ||
| 38 | .\" | ||
| 39 | .Dd June 29, 1991 | ||
| 40 | .Dt MALLOC 3 | ||
| 41 | .Os BSD 4 | ||
| 42 | .Sh NAME | ||
| 43 | .Nm malloc | ||
| 44 | .Nd general memory allocation function | ||
| 45 | .Sh SYNOPSIS | ||
| 46 | .Fd #include <stdlib.h> | ||
| 47 | .Ft void * | ||
| 48 | .Fn malloc "size_t size" | ||
| 49 | .Sh DESCRIPTION | ||
| 50 | The | ||
| 51 | .Fn malloc | ||
| 52 | function allocates uninitialized space for an object whose | ||
| 53 | size is specified by | ||
| 54 | .Fa size . | ||
| 55 | The | ||
| 56 | .Fn malloc | ||
| 57 | function maintains multiple lists of free blocks according to size, allocating | ||
| 58 | space from the appropriate list. | ||
| 59 | .Pp | ||
| 60 | The allocated space is | ||
| 61 | suitably aligned (after possible pointer | ||
| 62 | coercion) for storage of any type of object. If the space is of | ||
| 63 | .Em pagesize | ||
| 64 | or larger, the memory returned will be page-aligned. | ||
| 65 | .Sh RETURN VALUES | ||
| 66 | The | ||
| 67 | .Fn malloc | ||
| 68 | function returns | ||
| 69 | a pointer to the allocated space if successful; otherwise | ||
| 70 | a null pointer is returned. | ||
| 71 | .Sh SEE ALSO | ||
| 72 | .Xr brk 2 , | ||
| 73 | .Xr getpagesize 2 , | ||
| 74 | .Xr free 3 , | ||
| 75 | .Xr calloc 3 , | ||
| 76 | .Xr alloca 3 , | ||
| 77 | .Xr realloc 3 , | ||
| 78 | .Xr memory 3 | ||
| 79 | .Sh STANDARDS | ||
| 80 | The | ||
| 81 | .Fn malloc | ||
| 82 | function conforms to | ||
| 83 | .St -ansiC . | ||
| 84 | .Sh BUGS | ||
| 85 | The current implementation of | ||
| 86 | .Xr malloc | ||
| 87 | does not always fail gracefully when system | ||
| 88 | memory limits are approached. | ||
| 89 | It may fail to allocate memory when larger free blocks could be broken | ||
| 90 | up, or when limits are exceeded because the size is rounded up. | ||
| 91 | It is optimized for sizes that are powers of two. | ||
diff --git a/src/lib/libc/stdlib/malloc.c b/src/lib/libc/stdlib/malloc.c new file mode 100644 index 0000000000..3c57fad024 --- /dev/null +++ b/src/lib/libc/stdlib/malloc.c | |||
| @@ -0,0 +1,421 @@ | |||
| 1 | /* | ||
| 2 | * Copyright (c) 1983 Regents of the University of California. | ||
| 3 | * All rights reserved. | ||
| 4 | * | ||
| 5 | * Redistribution and use in source and binary forms, with or without | ||
| 6 | * modification, are permitted provided that the following conditions | ||
| 7 | * are met: | ||
| 8 | * 1. Redistributions of source code must retain the above copyright | ||
| 9 | * notice, this list of conditions and the following disclaimer. | ||
| 10 | * 2. Redistributions in binary form must reproduce the above copyright | ||
| 11 | * notice, this list of conditions and the following disclaimer in the | ||
| 12 | * documentation and/or other materials provided with the distribution. | ||
| 13 | * 3. All advertising materials mentioning features or use of this software | ||
| 14 | * must display the following acknowledgement: | ||
| 15 | * This product includes software developed by the University of | ||
| 16 | * California, Berkeley and its contributors. | ||
| 17 | * 4. Neither the name of the University nor the names of its contributors | ||
| 18 | * may be used to endorse or promote products derived from this software | ||
| 19 | * without specific prior written permission. | ||
| 20 | * | ||
| 21 | * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND | ||
| 22 | * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE | ||
| 23 | * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE | ||
| 24 | * ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE | ||
| 25 | * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL | ||
| 26 | * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS | ||
| 27 | * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) | ||
| 28 | * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT | ||
| 29 | * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY | ||
| 30 | * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF | ||
| 31 | * SUCH DAMAGE. | ||
| 32 | */ | ||
| 33 | |||
| 34 | #if defined(LIBC_SCCS) && !defined(lint) | ||
| 35 | /*static char *sccsid = "from: @(#)malloc.c 5.11 (Berkeley) 2/23/91";*/ | ||
| 36 | static char *rcsid = "$Id: malloc.c,v 1.1.1.1 1995/10/18 08:42:18 deraadt Exp $"; | ||
| 37 | #endif /* LIBC_SCCS and not lint */ | ||
| 38 | |||
| 39 | /* | ||
| 40 | * malloc.c (Caltech) 2/21/82 | ||
| 41 | * Chris Kingsley, kingsley@cit-20. | ||
| 42 | * | ||
| 43 | * This is a very fast storage allocator. It allocates blocks of a small | ||
| 44 | * number of different sizes, and keeps free lists of each size. Blocks that | ||
| 45 | * don't exactly fit are passed up to the next larger size. In this | ||
| 46 | * implementation, the available sizes are 2^n-4 (or 2^n-10) bytes long. | ||
| 47 | * This is designed for use in a virtual memory environment. | ||
| 48 | */ | ||
| 49 | |||
| 50 | #include <sys/types.h> | ||
| 51 | #include <stdlib.h> | ||
| 52 | #include <string.h> | ||
| 53 | #include <unistd.h> | ||
| 54 | |||
| 55 | #define NULL 0 | ||
| 56 | |||
| 57 | static void morecore(); | ||
| 58 | static int findbucket(); | ||
| 59 | |||
| 60 | /* | ||
| 61 | * The overhead on a block is at least 4 bytes. When free, this space | ||
| 62 | * contains a pointer to the next free block, and the bottom two bits must | ||
| 63 | * be zero. When in use, the first byte is set to MAGIC, and the second | ||
| 64 | * byte is the size index. The remaining bytes are for alignment. | ||
| 65 | * If range checking is enabled then a second word holds the size of the | ||
| 66 | * requested block, less 1, rounded up to a multiple of sizeof(RMAGIC). | ||
| 67 | * The order of elements is critical: ov_magic must overlay the low order | ||
| 68 | * bits of ov_next, and ov_magic can not be a valid ov_next bit pattern. | ||
| 69 | */ | ||
| 70 | union overhead { | ||
| 71 | union overhead *ov_next; /* when free */ | ||
| 72 | struct { | ||
| 73 | u_char ovu_magic; /* magic number */ | ||
| 74 | u_char ovu_index; /* bucket # */ | ||
| 75 | #ifdef RCHECK | ||
| 76 | u_short ovu_rmagic; /* range magic number */ | ||
| 77 | u_long ovu_size; /* actual block size */ | ||
| 78 | #endif | ||
| 79 | } ovu; | ||
| 80 | #define ov_magic ovu.ovu_magic | ||
| 81 | #define ov_index ovu.ovu_index | ||
| 82 | #define ov_rmagic ovu.ovu_rmagic | ||
| 83 | #define ov_size ovu.ovu_size | ||
| 84 | }; | ||
| 85 | |||
| 86 | #define MAGIC 0xef /* magic # on accounting info */ | ||
| 87 | #define RMAGIC 0x5555 /* magic # on range info */ | ||
| 88 | |||
| 89 | #ifdef RCHECK | ||
| 90 | #define RSLOP sizeof (u_short) | ||
| 91 | #else | ||
| 92 | #define RSLOP 0 | ||
| 93 | #endif | ||
| 94 | |||
| 95 | /* | ||
| 96 | * nextf[i] is the pointer to the next free block of size 2^(i+3). The | ||
| 97 | * smallest allocatable block is 8 bytes. The overhead information | ||
| 98 | * precedes the data area returned to the user. | ||
| 99 | */ | ||
| 100 | #define NBUCKETS 30 | ||
| 101 | static union overhead *nextf[NBUCKETS]; | ||
| 102 | extern char *sbrk(); | ||
| 103 | |||
| 104 | static int pagesz; /* page size */ | ||
| 105 | static int pagebucket; /* page size bucket */ | ||
| 106 | |||
| 107 | #ifdef MSTATS | ||
| 108 | /* | ||
| 109 | * nmalloc[i] is the difference between the number of mallocs and frees | ||
| 110 | * for a given block size. | ||
| 111 | */ | ||
| 112 | static u_int nmalloc[NBUCKETS]; | ||
| 113 | #include <stdio.h> | ||
| 114 | #endif | ||
| 115 | |||
| 116 | #if defined(DEBUG) || defined(RCHECK) | ||
| 117 | #define ASSERT(p) if (!(p)) botch("p") | ||
| 118 | #include <stdio.h> | ||
| 119 | static | ||
| 120 | botch(s) | ||
| 121 | char *s; | ||
| 122 | { | ||
| 123 | fprintf(stderr, "\r\nassertion botched: %s\r\n", s); | ||
| 124 | (void) fflush(stderr); /* just in case user buffered it */ | ||
| 125 | abort(); | ||
| 126 | } | ||
| 127 | #else | ||
| 128 | #define ASSERT(p) | ||
| 129 | #endif | ||
| 130 | |||
| 131 | void * | ||
| 132 | malloc(nbytes) | ||
| 133 | size_t nbytes; | ||
| 134 | { | ||
| 135 | register union overhead *op; | ||
| 136 | register long bucket, n; | ||
| 137 | register unsigned amt; | ||
| 138 | |||
| 139 | /* | ||
| 140 | * First time malloc is called, setup page size and | ||
| 141 | * align break pointer so all data will be page aligned. | ||
| 142 | */ | ||
| 143 | if (pagesz == 0) { | ||
| 144 | pagesz = n = getpagesize(); | ||
| 145 | op = (union overhead *)sbrk(0); | ||
| 146 | n = n - sizeof (*op) - ((long)op & (n - 1)); | ||
| 147 | if (n < 0) | ||
| 148 | n += pagesz; | ||
| 149 | if (n) { | ||
| 150 | if (sbrk(n) == (char *)-1) | ||
| 151 | return (NULL); | ||
| 152 | } | ||
| 153 | bucket = 0; | ||
| 154 | amt = 8; | ||
| 155 | while (pagesz > amt) { | ||
| 156 | amt <<= 1; | ||
| 157 | bucket++; | ||
| 158 | } | ||
| 159 | pagebucket = bucket; | ||
| 160 | } | ||
| 161 | /* | ||
| 162 | * Convert amount of memory requested into closest block size | ||
| 163 | * stored in hash buckets which satisfies request. | ||
| 164 | * Account for space used per block for accounting. | ||
| 165 | */ | ||
| 166 | if (nbytes <= (n = pagesz - sizeof (*op) - RSLOP)) { | ||
| 167 | #ifndef RCHECK | ||
| 168 | amt = 8; /* size of first bucket */ | ||
| 169 | bucket = 0; | ||
| 170 | #else | ||
| 171 | amt = 16; /* size of first bucket */ | ||
| 172 | bucket = 1; | ||
| 173 | #endif | ||
| 174 | n = -((long)sizeof (*op) + RSLOP); | ||
| 175 | } else { | ||
| 176 | amt = pagesz; | ||
| 177 | bucket = pagebucket; | ||
| 178 | } | ||
| 179 | while (nbytes > amt + n) { | ||
| 180 | amt <<= 1; | ||
| 181 | if (amt == 0) | ||
| 182 | return (NULL); | ||
| 183 | bucket++; | ||
| 184 | } | ||
| 185 | /* | ||
| 186 | * If nothing in hash bucket right now, | ||
| 187 | * request more memory from the system. | ||
| 188 | */ | ||
| 189 | if ((op = nextf[bucket]) == NULL) { | ||
| 190 | morecore(bucket); | ||
| 191 | if ((op = nextf[bucket]) == NULL) | ||
| 192 | return (NULL); | ||
| 193 | } | ||
| 194 | /* remove from linked list */ | ||
| 195 | nextf[bucket] = op->ov_next; | ||
| 196 | op->ov_magic = MAGIC; | ||
| 197 | op->ov_index = bucket; | ||
| 198 | #ifdef MSTATS | ||
| 199 | nmalloc[bucket]++; | ||
| 200 | #endif | ||
| 201 | #ifdef RCHECK | ||
| 202 | /* | ||
| 203 | * Record allocated size of block and | ||
| 204 | * bound space with magic numbers. | ||
| 205 | */ | ||
| 206 | op->ov_size = (nbytes + RSLOP - 1) & ~(RSLOP - 1); | ||
| 207 | op->ov_rmagic = RMAGIC; | ||
| 208 | *(u_short *)((caddr_t)(op + 1) + op->ov_size) = RMAGIC; | ||
| 209 | #endif | ||
| 210 | return ((char *)(op + 1)); | ||
| 211 | } | ||
| 212 | |||
| 213 | /* | ||
| 214 | * Allocate more memory to the indicated bucket. | ||
| 215 | */ | ||
| 216 | static void | ||
| 217 | morecore(bucket) | ||
| 218 | int bucket; | ||
| 219 | { | ||
| 220 | register union overhead *op; | ||
| 221 | register long sz; /* size of desired block */ | ||
| 222 | long amt; /* amount to allocate */ | ||
| 223 | int nblks; /* how many blocks we get */ | ||
| 224 | |||
| 225 | /* | ||
| 226 | * sbrk_size <= 0 only for big, FLUFFY, requests (about | ||
| 227 | * 2^30 bytes on a VAX, I think) or for a negative arg. | ||
| 228 | */ | ||
| 229 | sz = 1 << (bucket + 3); | ||
| 230 | #ifdef DEBUG | ||
| 231 | ASSERT(sz > 0); | ||
| 232 | #else | ||
| 233 | if (sz <= 0) | ||
| 234 | return; | ||
| 235 | #endif | ||
| 236 | if (sz < pagesz) { | ||
| 237 | amt = pagesz; | ||
| 238 | nblks = amt / sz; | ||
| 239 | } else { | ||
| 240 | amt = sz + pagesz; | ||
| 241 | nblks = 1; | ||
| 242 | } | ||
| 243 | op = (union overhead *)sbrk(amt); | ||
| 244 | /* no more room! */ | ||
| 245 | if ((long)op == -1) | ||
| 246 | return; | ||
| 247 | /* | ||
| 248 | * Add new memory allocated to that on | ||
| 249 | * free list for this hash bucket. | ||
| 250 | */ | ||
| 251 | nextf[bucket] = op; | ||
| 252 | while (--nblks > 0) { | ||
| 253 | op->ov_next = (union overhead *)((caddr_t)op + sz); | ||
| 254 | op = (union overhead *)((caddr_t)op + sz); | ||
| 255 | } | ||
| 256 | } | ||
| 257 | |||
| 258 | void | ||
| 259 | free(cp) | ||
| 260 | void *cp; | ||
| 261 | { | ||
| 262 | register long size; | ||
| 263 | register union overhead *op; | ||
| 264 | |||
| 265 | if (cp == NULL) | ||
| 266 | return; | ||
| 267 | op = (union overhead *)((caddr_t)cp - sizeof (union overhead)); | ||
| 268 | #ifdef DEBUG | ||
| 269 | ASSERT(op->ov_magic == MAGIC); /* make sure it was in use */ | ||
| 270 | #else | ||
| 271 | if (op->ov_magic != MAGIC) | ||
| 272 | return; /* sanity */ | ||
| 273 | #endif | ||
| 274 | #ifdef RCHECK | ||
| 275 | ASSERT(op->ov_rmagic == RMAGIC); | ||
| 276 | ASSERT(*(u_short *)((caddr_t)(op + 1) + op->ov_size) == RMAGIC); | ||
| 277 | #endif | ||
| 278 | size = op->ov_index; | ||
| 279 | ASSERT(size < NBUCKETS); | ||
| 280 | op->ov_next = nextf[size]; /* also clobbers ov_magic */ | ||
| 281 | nextf[size] = op; | ||
| 282 | #ifdef MSTATS | ||
| 283 | nmalloc[size]--; | ||
| 284 | #endif | ||
| 285 | } | ||
| 286 | |||
| 287 | /* | ||
| 288 | * When a program attempts "storage compaction" as mentioned in the | ||
| 289 | * old malloc man page, it realloc's an already freed block. Usually | ||
| 290 | * this is the last block it freed; occasionally it might be farther | ||
| 291 | * back. We have to search all the free lists for the block in order | ||
| 292 | * to determine its bucket: 1st we make one pass thru the lists | ||
| 293 | * checking only the first block in each; if that fails we search | ||
| 294 | * ``realloc_srchlen'' blocks in each list for a match (the variable | ||
| 295 | * is extern so the caller can modify it). If that fails we just copy | ||
| 296 | * however many bytes was given to realloc() and hope it's not huge. | ||
| 297 | */ | ||
| 298 | int realloc_srchlen = 4; /* 4 should be plenty, -1 =>'s whole list */ | ||
| 299 | |||
| 300 | void * | ||
| 301 | realloc(cp, nbytes) | ||
| 302 | void *cp; | ||
| 303 | size_t nbytes; | ||
| 304 | { | ||
| 305 | register u_long onb; | ||
| 306 | register long i; | ||
| 307 | union overhead *op; | ||
| 308 | char *res; | ||
| 309 | int was_alloced = 0; | ||
| 310 | |||
| 311 | if (cp == NULL) | ||
| 312 | return (malloc(nbytes)); | ||
| 313 | op = (union overhead *)((caddr_t)cp - sizeof (union overhead)); | ||
| 314 | if (op->ov_magic == MAGIC) { | ||
| 315 | was_alloced++; | ||
| 316 | i = op->ov_index; | ||
| 317 | } else { | ||
| 318 | /* | ||
| 319 | * Already free, doing "compaction". | ||
| 320 | * | ||
| 321 | * Search for the old block of memory on the | ||
| 322 | * free list. First, check the most common | ||
| 323 | * case (last element free'd), then (this failing) | ||
| 324 | * the last ``realloc_srchlen'' items free'd. | ||
| 325 | * If all lookups fail, then assume the size of | ||
| 326 | * the memory block being realloc'd is the | ||
| 327 | * largest possible (so that all "nbytes" of new | ||
| 328 | * memory are copied into). Note that this could cause | ||
| 329 | * a memory fault if the old area was tiny, and the moon | ||
| 330 | * is gibbous. However, that is very unlikely. | ||
| 331 | */ | ||
| 332 | if ((i = findbucket(op, 1)) < 0 && | ||
| 333 | (i = findbucket(op, realloc_srchlen)) < 0) | ||
| 334 | i = NBUCKETS; | ||
| 335 | } | ||
| 336 | onb = 1 << (i + 3); | ||
| 337 | if (onb < pagesz) | ||
| 338 | onb -= sizeof (*op) + RSLOP; | ||
| 339 | else | ||
| 340 | onb += pagesz - sizeof (*op) - RSLOP; | ||
| 341 | /* avoid the copy if same size block */ | ||
| 342 | if (was_alloced) { | ||
| 343 | if (i) { | ||
| 344 | i = 1 << (i + 2); | ||
| 345 | if (i < pagesz) | ||
| 346 | i -= sizeof (*op) + RSLOP; | ||
| 347 | else | ||
| 348 | i += pagesz - sizeof (*op) - RSLOP; | ||
| 349 | } | ||
| 350 | if (nbytes <= onb && nbytes > i) { | ||
| 351 | #ifdef RCHECK | ||
| 352 | op->ov_size = (nbytes + RSLOP - 1) & ~(RSLOP - 1); | ||
| 353 | *(u_short *)((caddr_t)(op + 1) + op->ov_size) = RMAGIC; | ||
| 354 | #endif | ||
| 355 | return(cp); | ||
| 356 | } else | ||
| 357 | free(cp); | ||
| 358 | } | ||
| 359 | if ((res = malloc(nbytes)) == NULL) | ||
| 360 | return (NULL); | ||
| 361 | if (cp != res) /* common optimization if "compacting" */ | ||
| 362 | bcopy(cp, res, (nbytes < onb) ? nbytes : onb); | ||
| 363 | return (res); | ||
| 364 | } | ||
| 365 | |||
| 366 | /* | ||
| 367 | * Search ``srchlen'' elements of each free list for a block whose | ||
| 368 | * header starts at ``freep''. If srchlen is -1 search the whole list. | ||
| 369 | * Return bucket number, or -1 if not found. | ||
| 370 | */ | ||
| 371 | static | ||
| 372 | findbucket(freep, srchlen) | ||
| 373 | union overhead *freep; | ||
| 374 | int srchlen; | ||
| 375 | { | ||
| 376 | register union overhead *p; | ||
| 377 | register int i, j; | ||
| 378 | |||
| 379 | for (i = 0; i < NBUCKETS; i++) { | ||
| 380 | j = 0; | ||
| 381 | for (p = nextf[i]; p && j != srchlen; p = p->ov_next) { | ||
| 382 | if (p == freep) | ||
| 383 | return (i); | ||
| 384 | j++; | ||
| 385 | } | ||
| 386 | } | ||
| 387 | return (-1); | ||
| 388 | } | ||
| 389 | |||
| 390 | #ifdef MSTATS | ||
| 391 | /* | ||
| 392 | * mstats - print out statistics about malloc | ||
| 393 | * | ||
| 394 | * Prints two lines of numbers, one showing the length of the free list | ||
| 395 | * for each size category, the second showing the number of mallocs - | ||
| 396 | * frees for each size category. | ||
| 397 | */ | ||
| 398 | mstats(s) | ||
| 399 | char *s; | ||
| 400 | { | ||
| 401 | register int i, j; | ||
| 402 | register union overhead *p; | ||
| 403 | int totfree = 0, | ||
| 404 | totused = 0; | ||
| 405 | |||
| 406 | fprintf(stderr, "Memory allocation statistics %s\nfree:\t", s); | ||
| 407 | for (i = 0; i < NBUCKETS; i++) { | ||
| 408 | for (j = 0, p = nextf[i]; p; p = p->ov_next, j++) | ||
| 409 | ; | ||
| 410 | fprintf(stderr, " %d", j); | ||
| 411 | totfree += j * (1 << (i + 3)); | ||
| 412 | } | ||
| 413 | fprintf(stderr, "\nused:\t"); | ||
| 414 | for (i = 0; i < NBUCKETS; i++) { | ||
| 415 | fprintf(stderr, " %d", nmalloc[i]); | ||
| 416 | totused += nmalloc[i] * (1 << (i + 3)); | ||
| 417 | } | ||
| 418 | fprintf(stderr, "\n\tTotal in use: %d, total free: %d\n", | ||
| 419 | totused, totfree); | ||
| 420 | } | ||
| 421 | #endif | ||
diff --git a/src/lib/libc/stdlib/memory.3 b/src/lib/libc/stdlib/memory.3 new file mode 100644 index 0000000000..735252c837 --- /dev/null +++ b/src/lib/libc/stdlib/memory.3 | |||
| @@ -0,0 +1,71 @@ | |||
| 1 | .\" Copyright (c) 1991 Regents of the University of California. | ||
| 2 | .\" All rights reserved. | ||
| 3 | .\" | ||
| 4 | .\" Redistribution and use in source and binary forms, with or without | ||
| 5 | .\" modification, are permitted provided that the following conditions | ||
| 6 | .\" are met: | ||
| 7 | .\" 1. Redistributions of source code must retain the above copyright | ||
| 8 | .\" notice, this list of conditions and the following disclaimer. | ||
| 9 | .\" 2. Redistributions in binary form must reproduce the above copyright | ||
| 10 | .\" notice, this list of conditions and the following disclaimer in the | ||
| 11 | .\" documentation and/or other materials provided with the distribution. | ||
| 12 | .\" 3. All advertising materials mentioning features or use of this software | ||
| 13 | .\" must display the following acknowledgement: | ||
| 14 | .\" This product includes software developed by the University of | ||
| 15 | .\" California, Berkeley and its contributors. | ||
| 16 | .\" 4. Neither the name of the University nor the names of its contributors | ||
| 17 | .\" may be used to endorse or promote products derived from this software | ||
| 18 | .\" without specific prior written permission. | ||
| 19 | .\" | ||
| 20 | .\" THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND | ||
| 21 | .\" ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE | ||
| 22 | .\" IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE | ||
| 23 | .\" ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE | ||
| 24 | .\" FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL | ||
| 25 | .\" DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS | ||
| 26 | .\" OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) | ||
| 27 | .\" HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT | ||
| 28 | .\" LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY | ||
| 29 | .\" OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF | ||
| 30 | .\" SUCH DAMAGE. | ||
| 31 | .\" | ||
| 32 | .\" from: @(#)memory.3 5.1 (Berkeley) 5/2/91 | ||
| 33 | .\" $Id: memory.3,v 1.1.1.1 1995/10/18 08:42:18 deraadt Exp $ | ||
| 34 | .\" | ||
| 35 | .Dd May 2, 1991 | ||
| 36 | .Dt MEMORY 3 | ||
| 37 | .Os BSD 4 | ||
| 38 | .Sh NAME | ||
| 39 | .Nm malloc , | ||
| 40 | .Nm free , | ||
| 41 | .Nm realloc , | ||
| 42 | .Nm calloc , | ||
| 43 | .Nm alloca | ||
| 44 | .Nd general memory allocation operations | ||
| 45 | .Sh SYNOPSIS | ||
| 46 | .Fd #include <stdlib.h> | ||
| 47 | .Ft void * | ||
| 48 | .Fn malloc "size_t size" | ||
| 49 | .Ft void | ||
| 50 | .Fn free "void *ptr" | ||
| 51 | .Ft void * | ||
| 52 | .Fn realloc "void *ptr" "size_t size" | ||
| 53 | .Ft void * | ||
| 54 | .Fn calloc "size_t nelem" "size_t elsize" | ||
| 55 | .Ft void * | ||
| 56 | .Fn alloca "size_t size" | ||
| 57 | .Sh DESCRIPTION | ||
| 58 | These functions allocate and free memory for the calling process. | ||
| 59 | They are described in the | ||
| 60 | individual manual pages. | ||
| 61 | .Sh SEE ALSO | ||
| 62 | .Xr calloc 3 , | ||
| 63 | .Xr free 3 , | ||
| 64 | .Xr malloc 3 , | ||
| 65 | .Xr realloc 3 , | ||
| 66 | .Xr alloca 3 , | ||
| 67 | .Sh STANDARDS | ||
| 68 | These functions, with the exception of | ||
| 69 | .Fn alloca | ||
| 70 | conform to | ||
| 71 | .St -ansiC . | ||
diff --git a/src/lib/libc/stdlib/merge.c b/src/lib/libc/stdlib/merge.c new file mode 100644 index 0000000000..381fdc0830 --- /dev/null +++ b/src/lib/libc/stdlib/merge.c | |||
| @@ -0,0 +1,348 @@ | |||
| 1 | /*- | ||
| 2 | * Copyright (c) 1992, 1993 | ||
| 3 | * The Regents of the University of California. All rights reserved. | ||
| 4 | * | ||
| 5 | * This code is derived from software contributed to Berkeley by | ||
| 6 | * Peter McIlroy. | ||
| 7 | * | ||
| 8 | * Redistribution and use in source and binary forms, with or without | ||
| 9 | * modification, are permitted provided that the following conditions | ||
| 10 | * are met: | ||
| 11 | * 1. Redistributions of source code must retain the above copyright | ||
| 12 | * notice, this list of conditions and the following disclaimer. | ||
| 13 | * 2. Redistributions in binary form must reproduce the above copyright | ||
| 14 | * notice, this list of conditions and the following disclaimer in the | ||
| 15 | * documentation and/or other materials provided with the distribution. | ||
| 16 | * 3. All advertising materials mentioning features or use of this software | ||
| 17 | * must display the following acknowledgement: | ||
| 18 | * This product includes software developed by the University of | ||
| 19 | * California, Berkeley and its contributors. | ||
| 20 | * 4. Neither the name of the University nor the names of its contributors | ||
| 21 | * may be used to endorse or promote products derived from this software | ||
| 22 | * without specific prior written permission. | ||
| 23 | * | ||
| 24 | * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND | ||
| 25 | * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE | ||
| 26 | * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE | ||
| 27 | * ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE | ||
| 28 | * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL | ||
| 29 | * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS | ||
| 30 | * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) | ||
| 31 | * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT | ||
| 32 | * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY | ||
| 33 | * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF | ||
| 34 | * SUCH DAMAGE. | ||
| 35 | */ | ||
| 36 | |||
| 37 | #if defined(LIBC_SCCS) && !defined(lint) | ||
| 38 | /*static char sccsid[] = "from: @(#)merge.c 8.2 (Berkeley) 2/14/94";*/ | ||
| 39 | static char *rcsid = "$Id: merge.c,v 1.1.1.1 1995/10/18 08:42:18 deraadt Exp $"; | ||
| 40 | #endif /* LIBC_SCCS and not lint */ | ||
| 41 | |||
| 42 | /* | ||
| 43 | * Hybrid exponential search/linear search merge sort with hybrid | ||
| 44 | * natural/pairwise first pass. Requires about .3% more comparisons | ||
| 45 | * for random data than LSMS with pairwise first pass alone. | ||
| 46 | * It works for objects as small as two bytes. | ||
| 47 | */ | ||
| 48 | |||
| 49 | #define NATURAL | ||
| 50 | #define THRESHOLD 16 /* Best choice for natural merge cut-off. */ | ||
| 51 | |||
| 52 | /* #define NATURAL to get hybrid natural merge. | ||
| 53 | * (The default is pairwise merging.) | ||
| 54 | */ | ||
| 55 | |||
| 56 | #include <sys/types.h> | ||
| 57 | |||
| 58 | #include <errno.h> | ||
| 59 | #include <stdlib.h> | ||
| 60 | #include <string.h> | ||
| 61 | |||
| 62 | static void setup __P((u_char *, u_char *, size_t, size_t, int (*)())); | ||
| 63 | static void insertionsort __P((u_char *, size_t, size_t, int (*)())); | ||
| 64 | |||
| 65 | #define ISIZE sizeof(int) | ||
| 66 | #define PSIZE sizeof(u_char *) | ||
| 67 | #define ICOPY_LIST(src, dst, last) \ | ||
| 68 | do \ | ||
| 69 | *(int*)dst = *(int*)src, src += ISIZE, dst += ISIZE; \ | ||
| 70 | while(src < last) | ||
| 71 | #define ICOPY_ELT(src, dst, i) \ | ||
| 72 | do \ | ||
| 73 | *(int*) dst = *(int*) src, src += ISIZE, dst += ISIZE; \ | ||
| 74 | while (i -= ISIZE) | ||
| 75 | |||
| 76 | #define CCOPY_LIST(src, dst, last) \ | ||
| 77 | do \ | ||
| 78 | *dst++ = *src++; \ | ||
| 79 | while (src < last) | ||
| 80 | #define CCOPY_ELT(src, dst, i) \ | ||
| 81 | do \ | ||
| 82 | *dst++ = *src++; \ | ||
| 83 | while (i -= 1) | ||
| 84 | |||
| 85 | /* | ||
| 86 | * Find the next possible pointer head. (Trickery for forcing an array | ||
| 87 | * to do double duty as a linked list when objects do not align with word | ||
| 88 | * boundaries. | ||
| 89 | */ | ||
| 90 | /* Assumption: PSIZE is a power of 2. */ | ||
| 91 | #define EVAL(p) (u_char **) \ | ||
| 92 | ((u_char *)0 + \ | ||
| 93 | (((u_char *)p + PSIZE - 1 - (u_char *) 0) & ~(PSIZE - 1))) | ||
| 94 | |||
| 95 | /* | ||
| 96 | * Arguments are as for qsort. | ||
| 97 | */ | ||
| 98 | int | ||
| 99 | mergesort(base, nmemb, size, cmp) | ||
| 100 | void *base; | ||
| 101 | size_t nmemb; | ||
| 102 | register size_t size; | ||
| 103 | int (*cmp) __P((const void *, const void *)); | ||
| 104 | { | ||
| 105 | register int i, sense; | ||
| 106 | int big, iflag; | ||
| 107 | register u_char *f1, *f2, *t, *b, *tp2, *q, *l1, *l2; | ||
| 108 | u_char *list2, *list1, *p2, *p, *last, **p1; | ||
| 109 | |||
| 110 | if (size < PSIZE / 2) { /* Pointers must fit into 2 * size. */ | ||
| 111 | errno = EINVAL; | ||
| 112 | return (-1); | ||
| 113 | } | ||
| 114 | |||
| 115 | /* | ||
| 116 | * XXX | ||
| 117 | * Stupid subtraction for the Cray. | ||
| 118 | */ | ||
| 119 | iflag = 0; | ||
| 120 | if (!(size % ISIZE) && !(((char *)base - (char *)0) % ISIZE)) | ||
| 121 | iflag = 1; | ||
| 122 | |||
| 123 | if ((list2 = malloc(nmemb * size + PSIZE)) == NULL) | ||
| 124 | return (-1); | ||
| 125 | |||
| 126 | list1 = base; | ||
| 127 | setup(list1, list2, nmemb, size, cmp); | ||
| 128 | last = list2 + nmemb * size; | ||
| 129 | i = big = 0; | ||
| 130 | while (*EVAL(list2) != last) { | ||
| 131 | l2 = list1; | ||
| 132 | p1 = EVAL(list1); | ||
| 133 | for (tp2 = p2 = list2; p2 != last; p1 = EVAL(l2)) { | ||
| 134 | p2 = *EVAL(p2); | ||
| 135 | f1 = l2; | ||
| 136 | f2 = l1 = list1 + (p2 - list2); | ||
| 137 | if (p2 != last) | ||
| 138 | p2 = *EVAL(p2); | ||
| 139 | l2 = list1 + (p2 - list2); | ||
| 140 | while (f1 < l1 && f2 < l2) { | ||
| 141 | if ((*cmp)(f1, f2) <= 0) { | ||
| 142 | q = f2; | ||
| 143 | b = f1, t = l1; | ||
| 144 | sense = -1; | ||
| 145 | } else { | ||
| 146 | q = f1; | ||
| 147 | b = f2, t = l2; | ||
| 148 | sense = 0; | ||
| 149 | } | ||
| 150 | if (!big) { /* here i = 0 */ | ||
| 151 | LINEAR: while ((b += size) < t && cmp(q, b) >sense) | ||
| 152 | if (++i == 6) { | ||
| 153 | big = 1; | ||
| 154 | goto EXPONENTIAL; | ||
| 155 | } | ||
| 156 | } else { | ||
| 157 | EXPONENTIAL: for (i = size; ; i <<= 1) | ||
| 158 | if ((p = (b + i)) >= t) { | ||
| 159 | if ((p = t - size) > b && | ||
| 160 | (*cmp)(q, p) <= sense) | ||
| 161 | t = p; | ||
| 162 | else | ||
| 163 | b = p; | ||
| 164 | break; | ||
| 165 | } else if ((*cmp)(q, p) <= sense) { | ||
| 166 | t = p; | ||
| 167 | if (i == size) | ||
| 168 | big = 0; | ||
| 169 | goto FASTCASE; | ||
| 170 | } else | ||
| 171 | b = p; | ||
| 172 | SLOWCASE: while (t > b+size) { | ||
| 173 | i = (((t - b) / size) >> 1) * size; | ||
| 174 | if ((*cmp)(q, p = b + i) <= sense) | ||
| 175 | t = p; | ||
| 176 | else | ||
| 177 | b = p; | ||
| 178 | } | ||
| 179 | goto COPY; | ||
| 180 | FASTCASE: while (i > size) | ||
| 181 | if ((*cmp)(q, | ||
| 182 | p = b + (i >>= 1)) <= sense) | ||
| 183 | t = p; | ||
| 184 | else | ||
| 185 | b = p; | ||
| 186 | COPY: b = t; | ||
| 187 | } | ||
| 188 | i = size; | ||
| 189 | if (q == f1) { | ||
| 190 | if (iflag) { | ||
| 191 | ICOPY_LIST(f2, tp2, b); | ||
| 192 | ICOPY_ELT(f1, tp2, i); | ||
| 193 | } else { | ||
| 194 | CCOPY_LIST(f2, tp2, b); | ||
| 195 | CCOPY_ELT(f1, tp2, i); | ||
| 196 | } | ||
| 197 | } else { | ||
| 198 | if (iflag) { | ||
| 199 | ICOPY_LIST(f1, tp2, b); | ||
| 200 | ICOPY_ELT(f2, tp2, i); | ||
| 201 | } else { | ||
| 202 | CCOPY_LIST(f1, tp2, b); | ||
| 203 | CCOPY_ELT(f2, tp2, i); | ||
| 204 | } | ||
| 205 | } | ||
| 206 | } | ||
| 207 | if (f2 < l2) { | ||
| 208 | if (iflag) | ||
| 209 | ICOPY_LIST(f2, tp2, l2); | ||
| 210 | else | ||
| 211 | CCOPY_LIST(f2, tp2, l2); | ||
| 212 | } else if (f1 < l1) { | ||
| 213 | if (iflag) | ||
| 214 | ICOPY_LIST(f1, tp2, l1); | ||
| 215 | else | ||
| 216 | CCOPY_LIST(f1, tp2, l1); | ||
| 217 | } | ||
| 218 | *p1 = l2; | ||
| 219 | } | ||
| 220 | tp2 = list1; /* swap list1, list2 */ | ||
| 221 | list1 = list2; | ||
| 222 | list2 = tp2; | ||
| 223 | last = list2 + nmemb*size; | ||
| 224 | } | ||
| 225 | if (base == list2) { | ||
| 226 | memmove(list2, list1, nmemb*size); | ||
| 227 | list2 = list1; | ||
| 228 | } | ||
| 229 | free(list2); | ||
| 230 | return (0); | ||
| 231 | } | ||
| 232 | |||
| 233 | #define swap(a, b) { \ | ||
| 234 | s = b; \ | ||
| 235 | i = size; \ | ||
| 236 | do { \ | ||
| 237 | tmp = *a; *a++ = *s; *s++ = tmp; \ | ||
| 238 | } while (--i); \ | ||
| 239 | a -= size; \ | ||
| 240 | } | ||
| 241 | #define reverse(bot, top) { \ | ||
| 242 | s = top; \ | ||
| 243 | do { \ | ||
| 244 | i = size; \ | ||
| 245 | do { \ | ||
| 246 | tmp = *bot; *bot++ = *s; *s++ = tmp; \ | ||
| 247 | } while (--i); \ | ||
| 248 | s -= size2; \ | ||
| 249 | } while(bot < s); \ | ||
| 250 | } | ||
| 251 | |||
| 252 | /* | ||
| 253 | * Optional hybrid natural/pairwise first pass. Eats up list1 in runs of | ||
| 254 | * increasing order, list2 in a corresponding linked list. Checks for runs | ||
| 255 | * when THRESHOLD/2 pairs compare with same sense. (Only used when NATURAL | ||
| 256 | * is defined. Otherwise simple pairwise merging is used.) | ||
| 257 | */ | ||
| 258 | void | ||
| 259 | setup(list1, list2, n, size, cmp) | ||
| 260 | size_t n, size; | ||
| 261 | int (*cmp) __P((const void *, const void *)); | ||
| 262 | u_char *list1, *list2; | ||
| 263 | { | ||
| 264 | int i, length, size2, tmp, sense; | ||
| 265 | u_char *f1, *f2, *s, *l2, *last, *p2; | ||
| 266 | |||
| 267 | size2 = size*2; | ||
| 268 | if (n <= 5) { | ||
| 269 | insertionsort(list1, n, size, cmp); | ||
| 270 | *EVAL(list2) = (u_char*) list2 + n*size; | ||
| 271 | return; | ||
| 272 | } | ||
| 273 | /* | ||
| 274 | * Avoid running pointers out of bounds; limit n to evens | ||
| 275 | * for simplicity. | ||
| 276 | */ | ||
| 277 | i = 4 + (n & 1); | ||
| 278 | insertionsort(list1 + (n - i) * size, i, size, cmp); | ||
| 279 | last = list1 + size * (n - i); | ||
| 280 | *EVAL(list2 + (last - list1)) = list2 + n * size; | ||
| 281 | |||
| 282 | #ifdef NATURAL | ||
| 283 | p2 = list2; | ||
| 284 | f1 = list1; | ||
| 285 | sense = (cmp(f1, f1 + size) > 0); | ||
| 286 | for (; f1 < last; sense = !sense) { | ||
| 287 | length = 2; | ||
| 288 | /* Find pairs with same sense. */ | ||
| 289 | for (f2 = f1 + size2; f2 < last; f2 += size2) { | ||
| 290 | if ((cmp(f2, f2+ size) > 0) != sense) | ||
| 291 | break; | ||
| 292 | length += 2; | ||
| 293 | } | ||
| 294 | if (length < THRESHOLD) { /* Pairwise merge */ | ||
| 295 | do { | ||
| 296 | p2 = *EVAL(p2) = f1 + size2 - list1 + list2; | ||
| 297 | if (sense > 0) | ||
| 298 | swap (f1, f1 + size); | ||
| 299 | } while ((f1 += size2) < f2); | ||
| 300 | } else { /* Natural merge */ | ||
| 301 | l2 = f2; | ||
| 302 | for (f2 = f1 + size2; f2 < l2; f2 += size2) { | ||
| 303 | if ((cmp(f2-size, f2) > 0) != sense) { | ||
| 304 | p2 = *EVAL(p2) = f2 - list1 + list2; | ||
| 305 | if (sense > 0) | ||
| 306 | reverse(f1, f2-size); | ||
| 307 | f1 = f2; | ||
| 308 | } | ||
| 309 | } | ||
| 310 | if (sense > 0) | ||
| 311 | reverse (f1, f2-size); | ||
| 312 | f1 = f2; | ||
| 313 | if (f2 < last || cmp(f2 - size, f2) > 0) | ||
| 314 | p2 = *EVAL(p2) = f2 - list1 + list2; | ||
| 315 | else | ||
| 316 | p2 = *EVAL(p2) = list2 + n*size; | ||
| 317 | } | ||
| 318 | } | ||
| 319 | #else /* pairwise merge only. */ | ||
| 320 | for (f1 = list1, p2 = list2; f1 < last; f1 += size2) { | ||
| 321 | p2 = *EVAL(p2) = p2 + size2; | ||
| 322 | if (cmp (f1, f1 + size) > 0) | ||
| 323 | swap(f1, f1 + size); | ||
| 324 | } | ||
| 325 | #endif /* NATURAL */ | ||
| 326 | } | ||
| 327 | |||
| 328 | /* | ||
| 329 | * This is to avoid out-of-bounds addresses in sorting the | ||
| 330 | * last 4 elements. | ||
| 331 | */ | ||
| 332 | static void | ||
| 333 | insertionsort(a, n, size, cmp) | ||
| 334 | u_char *a; | ||
| 335 | size_t n, size; | ||
| 336 | int (*cmp) __P((const void *, const void *)); | ||
| 337 | { | ||
| 338 | u_char *ai, *s, *t, *u, tmp; | ||
| 339 | int i; | ||
| 340 | |||
| 341 | for (ai = a+size; --n >= 1; ai += size) | ||
| 342 | for (t = ai; t > a; t -= size) { | ||
| 343 | u = t - size; | ||
| 344 | if (cmp(u, t) <= 0) | ||
| 345 | break; | ||
| 346 | swap(u, t); | ||
| 347 | } | ||
| 348 | } | ||
diff --git a/src/lib/libc/stdlib/mrand48.c b/src/lib/libc/stdlib/mrand48.c new file mode 100644 index 0000000000..43356e66b3 --- /dev/null +++ b/src/lib/libc/stdlib/mrand48.c | |||
| @@ -0,0 +1,23 @@ | |||
| 1 | /* | ||
| 2 | * Copyright (c) 1993 Martin Birgmeier | ||
| 3 | * All rights reserved. | ||
| 4 | * | ||
| 5 | * You may redistribute unmodified or modified versions of this source | ||
| 6 | * code provided that the above copyright notice and this and the | ||
| 7 | * following conditions are retained. | ||
| 8 | * | ||
| 9 | * This software is provided ``as is'', and comes with no warranties | ||
| 10 | * of any kind. I shall in no event be liable for anything that happens | ||
| 11 | * to anyone/anything when using this software. | ||
| 12 | */ | ||
| 13 | |||
| 14 | #include "rand48.h" | ||
| 15 | |||
| 16 | extern unsigned short __rand48_seed[3]; | ||
| 17 | |||
| 18 | long | ||
| 19 | mrand48(void) | ||
| 20 | { | ||
| 21 | __dorand48(__rand48_seed); | ||
| 22 | return ((long) __rand48_seed[2] << 16) + (long) __rand48_seed[1]; | ||
| 23 | } | ||
diff --git a/src/lib/libc/stdlib/multibyte.c b/src/lib/libc/stdlib/multibyte.c new file mode 100644 index 0000000000..fe1cd5781b --- /dev/null +++ b/src/lib/libc/stdlib/multibyte.c | |||
| @@ -0,0 +1,131 @@ | |||
| 1 | /* | ||
| 2 | * Copyright (c) 1991 The Regents of the University of California. | ||
| 3 | * All rights reserved. | ||
| 4 | * | ||
| 5 | * Redistribution and use in source and binary forms, with or without | ||
| 6 | * modification, are permitted provided that the following conditions | ||
| 7 | * are met: | ||
| 8 | * 1. Redistributions of source code must retain the above copyright | ||
| 9 | * notice, this list of conditions and the following disclaimer. | ||
| 10 | * 2. Redistributions in binary form must reproduce the above copyright | ||
| 11 | * notice, this list of conditions and the following disclaimer in the | ||
| 12 | * documentation and/or other materials provided with the distribution. | ||
| 13 | * 3. All advertising materials mentioning features or use of this software | ||
| 14 | * must display the following acknowledgement: | ||
| 15 | * This product includes software developed by the University of | ||
| 16 | * California, Berkeley and its contributors. | ||
| 17 | * 4. Neither the name of the University nor the names of its contributors | ||
| 18 | * may be used to endorse or promote products derived from this software | ||
| 19 | * without specific prior written permission. | ||
| 20 | * | ||
| 21 | * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND | ||
| 22 | * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE | ||
| 23 | * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE | ||
| 24 | * ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE | ||
| 25 | * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL | ||
| 26 | * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS | ||
| 27 | * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) | ||
| 28 | * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT | ||
| 29 | * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY | ||
| 30 | * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF | ||
| 31 | * SUCH DAMAGE. | ||
| 32 | */ | ||
| 33 | |||
| 34 | #if defined(LIBC_SCCS) && !defined(lint) | ||
| 35 | /*static char *sccsid = "from: @(#)multibyte.c 5.1 (Berkeley) 2/18/91";*/ | ||
| 36 | static char *rcsid = "$Id: multibyte.c,v 1.1.1.1 1995/10/18 08:42:18 deraadt Exp $"; | ||
| 37 | #endif /* LIBC_SCCS and not lint */ | ||
| 38 | |||
| 39 | #include <stdlib.h> | ||
| 40 | |||
| 41 | /* | ||
| 42 | * Stub multibyte character functions. | ||
| 43 | * This cheezy implementation is fixed to the native single-byte | ||
| 44 | * character set. | ||
| 45 | */ | ||
| 46 | |||
| 47 | int | ||
| 48 | mblen(s, n) | ||
| 49 | const char *s; | ||
| 50 | size_t n; | ||
| 51 | { | ||
| 52 | if (s == NULL || *s == '\0') | ||
| 53 | return 0; | ||
| 54 | if (n == 0) | ||
| 55 | return -1; | ||
| 56 | return 1; | ||
| 57 | } | ||
| 58 | |||
| 59 | /*ARGSUSED*/ | ||
| 60 | int | ||
| 61 | mbtowc(pwc, s, n) | ||
| 62 | wchar_t *pwc; | ||
| 63 | const char *s; | ||
| 64 | size_t n; | ||
| 65 | { | ||
| 66 | if (s == NULL) | ||
| 67 | return 0; | ||
| 68 | if (n == 0) | ||
| 69 | return -1; | ||
| 70 | if (pwc) | ||
| 71 | *pwc = (wchar_t) *s; | ||
| 72 | return (*s != '\0'); | ||
| 73 | } | ||
| 74 | |||
| 75 | /*ARGSUSED*/ | ||
| 76 | int | ||
| 77 | #ifdef __STDC__ | ||
| 78 | wctomb(char *s, wchar_t wchar) | ||
| 79 | #else | ||
| 80 | wctomb(s, wchar) | ||
| 81 | char *s; | ||
| 82 | wchar_t wchar; | ||
| 83 | #endif | ||
| 84 | { | ||
| 85 | if (s == NULL) | ||
| 86 | return 0; | ||
| 87 | |||
| 88 | *s = (char) wchar; | ||
| 89 | return 1; | ||
| 90 | } | ||
| 91 | |||
| 92 | /*ARGSUSED*/ | ||
| 93 | size_t | ||
| 94 | mbstowcs(pwcs, s, n) | ||
| 95 | wchar_t *pwcs; | ||
| 96 | const char *s; | ||
| 97 | size_t n; | ||
| 98 | { | ||
| 99 | int count = 0; | ||
| 100 | |||
| 101 | if (n != 0) { | ||
| 102 | do { | ||
| 103 | if ((*pwcs++ = (wchar_t) *s++) == 0) | ||
| 104 | break; | ||
| 105 | count++; | ||
| 106 | } while (--n != 0); | ||
| 107 | } | ||
| 108 | |||
| 109 | return count; | ||
| 110 | } | ||
| 111 | |||
| 112 | /*ARGSUSED*/ | ||
| 113 | size_t | ||
| 114 | wcstombs(s, pwcs, n) | ||
| 115 | char *s; | ||
| 116 | const wchar_t *pwcs; | ||
| 117 | size_t n; | ||
| 118 | { | ||
| 119 | int count = 0; | ||
| 120 | |||
| 121 | if (n != 0) { | ||
| 122 | do { | ||
| 123 | if ((*s++ = (char) *pwcs++) == 0) | ||
| 124 | break; | ||
| 125 | count++; | ||
| 126 | } while (--n != 0); | ||
| 127 | } | ||
| 128 | |||
| 129 | return count; | ||
| 130 | } | ||
| 131 | |||
diff --git a/src/lib/libc/stdlib/nrand48.c b/src/lib/libc/stdlib/nrand48.c new file mode 100644 index 0000000000..63f839cb05 --- /dev/null +++ b/src/lib/libc/stdlib/nrand48.c | |||
| @@ -0,0 +1,21 @@ | |||
| 1 | /* | ||
| 2 | * Copyright (c) 1993 Martin Birgmeier | ||
| 3 | * All rights reserved. | ||
| 4 | * | ||
| 5 | * You may redistribute unmodified or modified versions of this source | ||
| 6 | * code provided that the above copyright notice and this and the | ||
| 7 | * following conditions are retained. | ||
| 8 | * | ||
| 9 | * This software is provided ``as is'', and comes with no warranties | ||
| 10 | * of any kind. I shall in no event be liable for anything that happens | ||
| 11 | * to anyone/anything when using this software. | ||
| 12 | */ | ||
| 13 | |||
| 14 | #include "rand48.h" | ||
| 15 | |||
| 16 | long | ||
| 17 | nrand48(unsigned short xseed[3]) | ||
| 18 | { | ||
| 19 | __dorand48(xseed); | ||
| 20 | return ((long) xseed[2] << 15) + ((long) xseed[1] >> 1); | ||
| 21 | } | ||
diff --git a/src/lib/libc/stdlib/putenv.c b/src/lib/libc/stdlib/putenv.c new file mode 100644 index 0000000000..2194c2c608 --- /dev/null +++ b/src/lib/libc/stdlib/putenv.c | |||
| @@ -0,0 +1,59 @@ | |||
| 1 | /*- | ||
| 2 | * Copyright (c) 1988 The Regents of the University of California. | ||
| 3 | * All rights reserved. | ||
| 4 | * | ||
| 5 | * Redistribution and use in source and binary forms, with or without | ||
| 6 | * modification, are permitted provided that the following conditions | ||
| 7 | * are met: | ||
| 8 | * 1. Redistributions of source code must retain the above copyright | ||
| 9 | * notice, this list of conditions and the following disclaimer. | ||
| 10 | * 2. Redistributions in binary form must reproduce the above copyright | ||
| 11 | * notice, this list of conditions and the following disclaimer in the | ||
| 12 | * documentation and/or other materials provided with the distribution. | ||
| 13 | * 3. All advertising materials mentioning features or use of this software | ||
| 14 | * must display the following acknowledgement: | ||
| 15 | * This product includes software developed by the University of | ||
| 16 | * California, Berkeley and its contributors. | ||
| 17 | * 4. Neither the name of the University nor the names of its contributors | ||
| 18 | * may be used to endorse or promote products derived from this software | ||
| 19 | * without specific prior written permission. | ||
| 20 | * | ||
| 21 | * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND | ||
| 22 | * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE | ||
| 23 | * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE | ||
| 24 | * ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE | ||
| 25 | * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL | ||
| 26 | * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS | ||
| 27 | * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) | ||
| 28 | * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT | ||
| 29 | * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY | ||
| 30 | * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF | ||
| 31 | * SUCH DAMAGE. | ||
| 32 | */ | ||
| 33 | |||
| 34 | #if defined(LIBC_SCCS) && !defined(lint) | ||
| 35 | /*static char *sccsid = "from: @(#)putenv.c 5.4 (Berkeley) 2/23/91";*/ | ||
| 36 | static char *rcsid = "$Id: putenv.c,v 1.1.1.1 1995/10/18 08:42:18 deraadt Exp $"; | ||
| 37 | #endif /* LIBC_SCCS and not lint */ | ||
| 38 | |||
| 39 | #include <stdlib.h> | ||
| 40 | #include <string.h> | ||
| 41 | |||
| 42 | int | ||
| 43 | putenv(str) | ||
| 44 | const char *str; | ||
| 45 | { | ||
| 46 | register char *p, *equal; | ||
| 47 | int rval; | ||
| 48 | |||
| 49 | if (!(p = strdup(str))) | ||
| 50 | return(1); | ||
| 51 | if (!(equal = strchr(p, '='))) { | ||
| 52 | (void)free(p); | ||
| 53 | return(1); | ||
| 54 | } | ||
| 55 | *equal = '\0'; | ||
| 56 | rval = setenv(p, equal + 1, 1); | ||
| 57 | (void)free(p); | ||
| 58 | return(rval); | ||
| 59 | } | ||
diff --git a/src/lib/libc/stdlib/qabs.3 b/src/lib/libc/stdlib/qabs.3 new file mode 100644 index 0000000000..cb1e052191 --- /dev/null +++ b/src/lib/libc/stdlib/qabs.3 | |||
| @@ -0,0 +1,62 @@ | |||
| 1 | .\" Copyright (c) 1990, 1991 The Regents of the University of California. | ||
| 2 | .\" All rights reserved. | ||
| 3 | .\" | ||
| 4 | .\" This code is derived from software contributed to Berkeley by | ||
| 5 | .\" the American National Standards Committee X3, on Information | ||
| 6 | .\" Processing Systems. | ||
| 7 | .\" | ||
| 8 | .\" Redistribution and use in source and binary forms, with or without | ||
| 9 | .\" modification, are permitted provided that the following conditions | ||
| 10 | .\" are met: | ||
| 11 | .\" 1. Redistributions of source code must retain the above copyright | ||
| 12 | .\" notice, this list of conditions and the following disclaimer. | ||
| 13 | .\" 2. Redistributions in binary form must reproduce the above copyright | ||
| 14 | .\" notice, this list of conditions and the following disclaimer in the | ||
| 15 | .\" documentation and/or other materials provided with the distribution. | ||
| 16 | .\" 3. All advertising materials mentioning features or use of this software | ||
| 17 | .\" must display the following acknowledgement: | ||
| 18 | .\" This product includes software developed by the University of | ||
| 19 | .\" California, Berkeley and its contributors. | ||
| 20 | .\" 4. Neither the name of the University nor the names of its contributors | ||
| 21 | .\" may be used to endorse or promote products derived from this software | ||
| 22 | .\" without specific prior written permission. | ||
| 23 | .\" | ||
| 24 | .\" THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND | ||
| 25 | .\" ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE | ||
| 26 | .\" IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE | ||
| 27 | .\" ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE | ||
| 28 | .\" FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL | ||
| 29 | .\" DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS | ||
| 30 | .\" OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) | ||
| 31 | .\" HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT | ||
| 32 | .\" LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY | ||
| 33 | .\" OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF | ||
| 34 | .\" SUCH DAMAGE. | ||
| 35 | .\" | ||
| 36 | .\" from: @(#)labs.3 5.3 (Berkeley) 6/29/91 | ||
| 37 | .\" $Id: qabs.3,v 1.1.1.1 1995/10/18 08:42:18 deraadt Exp $ | ||
| 38 | .\" | ||
| 39 | .Dd June 29, 1991 | ||
| 40 | .Dt QABS 3 | ||
| 41 | .Os | ||
| 42 | .Sh NAME | ||
| 43 | .Nm qabs | ||
| 44 | .Nd return the absolute value of a quad integer | ||
| 45 | .Sh SYNOPSIS | ||
| 46 | .Fd #include <stdlib.h> | ||
| 47 | .Ft quad_t | ||
| 48 | .Fn qabs "quad_t j" | ||
| 49 | .Sh DESCRIPTION | ||
| 50 | The | ||
| 51 | .Fn qabs | ||
| 52 | function | ||
| 53 | returns the absolute value of the quad integer | ||
| 54 | .Ar j . | ||
| 55 | .Sh SEE ALSO | ||
| 56 | .Xr abs 3 , | ||
| 57 | .Xr labs 3 , | ||
| 58 | .Xr floor 3 , | ||
| 59 | .Xr cabs 3 , | ||
| 60 | .Xr math 3 | ||
| 61 | .Sh BUGS | ||
| 62 | The absolute value of the most negative integer remains negative. | ||
diff --git a/src/lib/libc/stdlib/qabs.c b/src/lib/libc/stdlib/qabs.c new file mode 100644 index 0000000000..9c51a8baa9 --- /dev/null +++ b/src/lib/libc/stdlib/qabs.c | |||
| @@ -0,0 +1,46 @@ | |||
| 1 | /*- | ||
| 2 | * Copyright (c) 1990 The Regents of the University of California. | ||
| 3 | * All rights reserved. | ||
| 4 | * | ||
| 5 | * Redistribution and use in source and binary forms, with or without | ||
| 6 | * modification, are permitted provided that the following conditions | ||
| 7 | * are met: | ||
| 8 | * 1. Redistributions of source code must retain the above copyright | ||
| 9 | * notice, this list of conditions and the following disclaimer. | ||
| 10 | * 2. Redistributions in binary form must reproduce the above copyright | ||
| 11 | * notice, this list of conditions and the following disclaimer in the | ||
| 12 | * documentation and/or other materials provided with the distribution. | ||
| 13 | * 3. All advertising materials mentioning features or use of this software | ||
| 14 | * must display the following acknowledgement: | ||
| 15 | * This product includes software developed by the University of | ||
| 16 | * California, Berkeley and its contributors. | ||
| 17 | * 4. Neither the name of the University nor the names of its contributors | ||
| 18 | * may be used to endorse or promote products derived from this software | ||
| 19 | * without specific prior written permission. | ||
| 20 | * | ||
| 21 | * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND | ||
| 22 | * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE | ||
| 23 | * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE | ||
| 24 | * ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE | ||
| 25 | * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL | ||
| 26 | * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS | ||
| 27 | * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) | ||
| 28 | * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT | ||
| 29 | * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY | ||
| 30 | * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF | ||
| 31 | * SUCH DAMAGE. | ||
| 32 | */ | ||
| 33 | |||
| 34 | #if defined(LIBC_SCCS) && !defined(lint) | ||
| 35 | /*static char *sccsid = "from: @(#)labs.c 5.2 (Berkeley) 5/17/90";*/ | ||
| 36 | static char *rcsid = "$Id: qabs.c,v 1.1.1.1 1995/10/18 08:42:18 deraadt Exp $"; | ||
| 37 | #endif /* LIBC_SCCS and not lint */ | ||
| 38 | |||
| 39 | #include <stdlib.h> | ||
| 40 | |||
| 41 | quad_t | ||
| 42 | qabs(j) | ||
| 43 | quad_t j; | ||
| 44 | { | ||
| 45 | return(j < 0 ? -j : j); | ||
| 46 | } | ||
diff --git a/src/lib/libc/stdlib/qdiv.3 b/src/lib/libc/stdlib/qdiv.3 new file mode 100644 index 0000000000..0efcfc96ef --- /dev/null +++ b/src/lib/libc/stdlib/qdiv.3 | |||
| @@ -0,0 +1,66 @@ | |||
| 1 | .\" Copyright (c) 1990, 1991 The Regents of the University of California. | ||
| 2 | .\" All rights reserved. | ||
| 3 | .\" | ||
| 4 | .\" This code is derived from software contributed to Berkeley by | ||
| 5 | .\" Chris Torek and the American National Standards Committee X3, | ||
| 6 | .\" on Information Processing Systems. | ||
| 7 | .\" | ||
| 8 | .\" Redistribution and use in source and binary forms, with or without | ||
| 9 | .\" modification, are permitted provided that the following conditions | ||
| 10 | .\" are met: | ||
| 11 | .\" 1. Redistributions of source code must retain the above copyright | ||
| 12 | .\" notice, this list of conditions and the following disclaimer. | ||
| 13 | .\" 2. Redistributions in binary form must reproduce the above copyright | ||
| 14 | .\" notice, this list of conditions and the following disclaimer in the | ||
| 15 | .\" documentation and/or other materials provided with the distribution. | ||
| 16 | .\" 3. All advertising materials mentioning features or use of this software | ||
| 17 | .\" must display the following acknowledgement: | ||
| 18 | .\" This product includes software developed by the University of | ||
| 19 | .\" California, Berkeley and its contributors. | ||
| 20 | .\" 4. Neither the name of the University nor the names of its contributors | ||
| 21 | .\" may be used to endorse or promote products derived from this software | ||
| 22 | .\" without specific prior written permission. | ||
| 23 | .\" | ||
| 24 | .\" THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND | ||
| 25 | .\" ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE | ||
| 26 | .\" IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE | ||
| 27 | .\" ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE | ||
| 28 | .\" FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL | ||
| 29 | .\" DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS | ||
| 30 | .\" OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) | ||
| 31 | .\" HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT | ||
| 32 | .\" LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY | ||
| 33 | .\" OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF | ||
| 34 | .\" SUCH DAMAGE. | ||
| 35 | .\" | ||
| 36 | .\" from: @(#)qdiv.3 5.3 (Berkeley) 6/29/91 | ||
| 37 | .\" $Id: qdiv.3,v 1.1.1.1 1995/10/18 08:42:18 deraadt Exp $ | ||
| 38 | .\" | ||
| 39 | .Dd June 29, 1991 | ||
| 40 | .Dt QDIV 3 | ||
| 41 | .Os | ||
| 42 | .Sh NAME | ||
| 43 | .Nm qdiv | ||
| 44 | .Nd return quotient and remainder from division | ||
| 45 | .Sh SYNOPSIS | ||
| 46 | .Fd #include <stdlib.h> | ||
| 47 | .Ft qdiv_t | ||
| 48 | .Fn qdiv "quad_t num" "quad_t denom" | ||
| 49 | .Sh DESCRIPTION | ||
| 50 | The | ||
| 51 | .Fn qdiv | ||
| 52 | function | ||
| 53 | computes the value | ||
| 54 | .Ar num/denom | ||
| 55 | and returns the quotient and remainder in a structure named | ||
| 56 | .Ar qdiv_t | ||
| 57 | that contains two | ||
| 58 | .Em quad integer | ||
| 59 | members named | ||
| 60 | .Ar quot | ||
| 61 | and | ||
| 62 | .Ar rem . | ||
| 63 | .Sh SEE ALSO | ||
| 64 | .Xr div 3 , | ||
| 65 | .Xr ldiv 3 , | ||
| 66 | .Xr math 3 | ||
diff --git a/src/lib/libc/stdlib/qdiv.c b/src/lib/libc/stdlib/qdiv.c new file mode 100644 index 0000000000..8f8e3f89c4 --- /dev/null +++ b/src/lib/libc/stdlib/qdiv.c | |||
| @@ -0,0 +1,59 @@ | |||
| 1 | /* | ||
| 2 | * Copyright (c) 1990 Regents of the University of California. | ||
| 3 | * All rights reserved. | ||
| 4 | * | ||
| 5 | * This code is derived from software contributed to Berkeley by | ||
| 6 | * Chris Torek. | ||
| 7 | * | ||
| 8 | * Redistribution and use in source and binary forms, with or without | ||
| 9 | * modification, are permitted provided that the following conditions | ||
| 10 | * are met: | ||
| 11 | * 1. Redistributions of source code must retain the above copyright | ||
| 12 | * notice, this list of conditions and the following disclaimer. | ||
| 13 | * 2. Redistributions in binary form must reproduce the above copyright | ||
| 14 | * notice, this list of conditions and the following disclaimer in the | ||
| 15 | * documentation and/or other materials provided with the distribution. | ||
| 16 | * 3. All advertising materials mentioning features or use of this software | ||
| 17 | * must display the following acknowledgement: | ||
| 18 | * This product includes software developed by the University of | ||
| 19 | * California, Berkeley and its contributors. | ||
| 20 | * 4. Neither the name of the University nor the names of its contributors | ||
| 21 | * may be used to endorse or promote products derived from this software | ||
| 22 | * without specific prior written permission. | ||
| 23 | * | ||
| 24 | * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND | ||
| 25 | * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE | ||
| 26 | * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE | ||
| 27 | * ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE | ||
| 28 | * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL | ||
| 29 | * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS | ||
| 30 | * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) | ||
| 31 | * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT | ||
| 32 | * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY | ||
| 33 | * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF | ||
| 34 | * SUCH DAMAGE. | ||
| 35 | */ | ||
| 36 | |||
| 37 | #if defined(LIBC_SCCS) && !defined(lint) | ||
| 38 | /*static char *sccsid = "from: @(#)ldiv.c 5.2 (Berkeley) 4/16/91";*/ | ||
| 39 | static char *rcsid = "$Id: qdiv.c,v 1.1.1.1 1995/10/18 08:42:18 deraadt Exp $"; | ||
| 40 | #endif /* LIBC_SCCS and not lint */ | ||
| 41 | |||
| 42 | #include <stdlib.h> /* qdiv_t */ | ||
| 43 | |||
| 44 | qdiv_t | ||
| 45 | qdiv(num, denom) | ||
| 46 | quad_t num, denom; | ||
| 47 | { | ||
| 48 | qdiv_t r; | ||
| 49 | |||
| 50 | /* see div.c for comments */ | ||
| 51 | |||
| 52 | r.quot = num / denom; | ||
| 53 | r.rem = num % denom; | ||
| 54 | if (num >= 0 && r.rem < 0) { | ||
| 55 | r.quot++; | ||
| 56 | r.rem -= denom; | ||
| 57 | } | ||
| 58 | return (r); | ||
| 59 | } | ||
diff --git a/src/lib/libc/stdlib/qsort.3 b/src/lib/libc/stdlib/qsort.3 new file mode 100644 index 0000000000..eb122cde12 --- /dev/null +++ b/src/lib/libc/stdlib/qsort.3 | |||
| @@ -0,0 +1,234 @@ | |||
| 1 | .\" Copyright (c) 1990, 1991, 1993 | ||
| 2 | .\" The Regents of the University of California. All rights reserved. | ||
| 3 | .\" | ||
| 4 | .\" This code is derived from software contributed to Berkeley by | ||
| 5 | .\" the American National Standards Committee X3, on Information | ||
| 6 | .\" Processing Systems. | ||
| 7 | .\" | ||
| 8 | .\" Redistribution and use in source and binary forms, with or without | ||
| 9 | .\" modification, are permitted provided that the following conditions | ||
| 10 | .\" are met: | ||
| 11 | .\" 1. Redistributions of source code must retain the above copyright | ||
| 12 | .\" notice, this list of conditions and the following disclaimer. | ||
| 13 | .\" 2. Redistributions in binary form must reproduce the above copyright | ||
| 14 | .\" notice, this list of conditions and the following disclaimer in the | ||
| 15 | .\" documentation and/or other materials provided with the distribution. | ||
| 16 | .\" 3. All advertising materials mentioning features or use of this software | ||
| 17 | .\" must display the following acknowledgement: | ||
| 18 | .\" This product includes software developed by the University of | ||
| 19 | .\" California, Berkeley and its contributors. | ||
| 20 | .\" 4. Neither the name of the University nor the names of its contributors | ||
| 21 | .\" may be used to endorse or promote products derived from this software | ||
| 22 | .\" without specific prior written permission. | ||
| 23 | .\" | ||
| 24 | .\" THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND | ||
| 25 | .\" ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE | ||
| 26 | .\" IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE | ||
| 27 | .\" ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE | ||
| 28 | .\" FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL | ||
| 29 | .\" DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS | ||
| 30 | .\" OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) | ||
| 31 | .\" HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT | ||
| 32 | .\" LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY | ||
| 33 | .\" OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF | ||
| 34 | .\" SUCH DAMAGE. | ||
| 35 | .\" | ||
| 36 | .\" from: @(#)qsort.3 8.1 (Berkeley) 6/4/93 | ||
| 37 | .\" $Id: qsort.3,v 1.1.1.1 1995/10/18 08:42:18 deraadt Exp $ | ||
| 38 | .\" | ||
| 39 | .Dd June 4, 1993 | ||
| 40 | .Dt QSORT 3 | ||
| 41 | .Os | ||
| 42 | .Sh NAME | ||
| 43 | .Nm qsort, heapsort, mergesort | ||
| 44 | .Nd sort functions | ||
| 45 | .Sh SYNOPSIS | ||
| 46 | .Fd #include <stdlib.h> | ||
| 47 | .Ft void | ||
| 48 | .Fn qsort "void *base" "size_t nmemb" "size_t size" "int (*compar)(const void *, const void *)" | ||
| 49 | .Ft int | ||
| 50 | .Fn heapsort "void *base" "size_t nmemb" "size_t size" "int (*compar)(const void *, const void *)" | ||
| 51 | .Ft int | ||
| 52 | .Fn mergesort "void *base" "size_t nmemb" "size_t size" "int (*compar)(const void *, const void *)" | ||
| 53 | .Sh DESCRIPTION | ||
| 54 | The | ||
| 55 | .Fn qsort | ||
| 56 | function is a modified partition-exchange sort, or quicksort. | ||
| 57 | The | ||
| 58 | .Fn heapsort | ||
| 59 | function is a modified selection sort. | ||
| 60 | The | ||
| 61 | .Fn mergesort | ||
| 62 | function is a modified merge sort with exponential search | ||
| 63 | intended for sorting data with pre-existing order. | ||
| 64 | .Pp | ||
| 65 | The | ||
| 66 | .Fn qsort | ||
| 67 | and | ||
| 68 | .Fn heapsort | ||
| 69 | functions sort an array of | ||
| 70 | .Fa nmemb | ||
| 71 | objects, the initial member of which is pointed to by | ||
| 72 | .Fa base . | ||
| 73 | The size of each object is specified by | ||
| 74 | .Fa size . | ||
| 75 | .Fn Mergesort | ||
| 76 | behaves similarly, but | ||
| 77 | .Em requires | ||
| 78 | that | ||
| 79 | .Fa size | ||
| 80 | be greater than | ||
| 81 | .Dq "sizeof(void *) / 2" . | ||
| 82 | .Pp | ||
| 83 | The contents of the array | ||
| 84 | .Fa base | ||
| 85 | are sorted in ascending order according to | ||
| 86 | a comparison function pointed to by | ||
| 87 | .Fa compar , | ||
| 88 | which requires two arguments pointing to the objects being | ||
| 89 | compared. | ||
| 90 | .Pp | ||
| 91 | The comparison function must return an integer less than, equal to, or | ||
| 92 | greater than zero if the first argument is considered to be respectively | ||
| 93 | less than, equal to, or greater than the second. | ||
| 94 | .Pp | ||
| 95 | The functions | ||
| 96 | .Fn qsort | ||
| 97 | and | ||
| 98 | .Fn heapsort | ||
| 99 | are | ||
| 100 | .Em not | ||
| 101 | stable, that is, if two members compare as equal, their order in | ||
| 102 | the sorted array is undefined. | ||
| 103 | The function | ||
| 104 | .Fn mergesort | ||
| 105 | is stable. | ||
| 106 | .Pp | ||
| 107 | The | ||
| 108 | .Fn qsort | ||
| 109 | function is an implementation of C.A.R. Hoare's ``quicksort'' algorithm, | ||
| 110 | a variant of partition-exchange sorting; in particular, see D.E. Knuth's | ||
| 111 | Algorithm Q. | ||
| 112 | .Fn Qsort | ||
| 113 | takes O N lg N average time. | ||
| 114 | This implementation uses median selection to avoid its | ||
| 115 | O N**2 worst-case behavior. | ||
| 116 | .Pp | ||
| 117 | The | ||
| 118 | .Fn heapsort | ||
| 119 | function is an implementation of J.W.J. William's ``heapsort'' algorithm, | ||
| 120 | a variant of selection sorting; in particular, see D.E. Knuth's Algorithm H. | ||
| 121 | .Fn Heapsort | ||
| 122 | takes O N lg N worst-case time. | ||
| 123 | Its | ||
| 124 | .Em only | ||
| 125 | advantage over | ||
| 126 | .Fn qsort | ||
| 127 | is that it uses almost no additional memory; while | ||
| 128 | .Fn qsort | ||
| 129 | does not allocate memory, it is implemented using recursion. | ||
| 130 | .Pp | ||
| 131 | The function | ||
| 132 | .Fn mergesort | ||
| 133 | requires additional memory of size | ||
| 134 | .Fa nmemb * | ||
| 135 | .Fa size | ||
| 136 | bytes; it should be used only when space is not at a premium. | ||
| 137 | .Fn Mergesort | ||
| 138 | is optimized for data with pre-existing order; its worst case | ||
| 139 | time is O N lg N; its best case is O N. | ||
| 140 | .Pp | ||
| 141 | Normally, | ||
| 142 | .Fn qsort | ||
| 143 | is faster than | ||
| 144 | .Fn mergesort | ||
| 145 | is faster than | ||
| 146 | .Fn heapsort . | ||
| 147 | Memory availability and pre-existing order in the data can make this | ||
| 148 | untrue. | ||
| 149 | .Sh RETURN VALUES | ||
| 150 | The | ||
| 151 | .Fn qsort | ||
| 152 | function | ||
| 153 | returns no value. | ||
| 154 | .Pp | ||
| 155 | Upon successful completion, | ||
| 156 | .Fn heapsort | ||
| 157 | and | ||
| 158 | .Fn mergesort | ||
| 159 | return 0. | ||
| 160 | Otherwise, they return \-1 and the global variable | ||
| 161 | .Va errno | ||
| 162 | is set to indicate the error. | ||
| 163 | .Sh ERRORS | ||
| 164 | The | ||
| 165 | .Fn heapsort | ||
| 166 | function succeeds unless: | ||
| 167 | .Bl -tag -width Er | ||
| 168 | .It Bq Er EINVAL | ||
| 169 | The | ||
| 170 | .Fa size | ||
| 171 | argument is zero, or, | ||
| 172 | the | ||
| 173 | .Fa size | ||
| 174 | argument to | ||
| 175 | .Fn mergesort | ||
| 176 | is less than | ||
| 177 | .Dq "sizeof(void *) / 2" . | ||
| 178 | .It Bq Er ENOMEM | ||
| 179 | .Fn Heapsort | ||
| 180 | or | ||
| 181 | .Fn mergesort | ||
| 182 | were unable to allocate memory. | ||
| 183 | .El | ||
| 184 | .Sh COMPATIBILITY | ||
| 185 | Previous versions of | ||
| 186 | .Fn qsort | ||
| 187 | did not permit the comparison routine itself to call | ||
| 188 | .Fn qsort 3 . | ||
| 189 | This is no longer true. | ||
| 190 | .Sh SEE ALSO | ||
| 191 | .Xr sort 1 , | ||
| 192 | .Xr radixsort 3 | ||
| 193 | .Rs | ||
| 194 | .%A Hoare, C.A.R. | ||
| 195 | .%D 1962 | ||
| 196 | .%T "Quicksort" | ||
| 197 | .%J "The Computer Journal" | ||
| 198 | .%V 5:1 | ||
| 199 | .%P pp. 10-15 | ||
| 200 | .Re | ||
| 201 | .Rs | ||
| 202 | .%A Williams, J.W.J | ||
| 203 | .%D 1964 | ||
| 204 | .%T "Heapsort" | ||
| 205 | .%J "Communications of the ACM" | ||
| 206 | .%V 7:1 | ||
| 207 | .%P pp. 347-348 | ||
| 208 | .Re | ||
| 209 | .Rs | ||
| 210 | .%A Knuth, D.E. | ||
| 211 | .%D 1968 | ||
| 212 | .%B "The Art of Computer Programming" | ||
| 213 | .%V Vol. 3 | ||
| 214 | .%T "Sorting and Searching" | ||
| 215 | .%P pp. 114-123, 145-149 | ||
| 216 | .Re | ||
| 217 | .Rs | ||
| 218 | .%A Mcilroy, P.M. | ||
| 219 | .%T "Optimistic Sorting and Information Theoretic Complexity" | ||
| 220 | .%J "Fourth Annual ACM-SIAM Symposium on Discrete Algorithms" | ||
| 221 | .%V January 1992 | ||
| 222 | .Re | ||
| 223 | .Rs | ||
| 224 | .%A Bentley, J.L. | ||
| 225 | .%T "Engineering a Sort Function" | ||
| 226 | .%J "bentley@research.att.com" | ||
| 227 | .%V January 1992 | ||
| 228 | .Re | ||
| 229 | .Sh STANDARDS | ||
| 230 | The | ||
| 231 | .Fn qsort | ||
| 232 | function | ||
| 233 | conforms to | ||
| 234 | .St -ansiC . | ||
diff --git a/src/lib/libc/stdlib/qsort.c b/src/lib/libc/stdlib/qsort.c new file mode 100644 index 0000000000..c06bd54054 --- /dev/null +++ b/src/lib/libc/stdlib/qsort.c | |||
| @@ -0,0 +1,175 @@ | |||
| 1 | /*- | ||
| 2 | * Copyright (c) 1992, 1993 | ||
| 3 | * The Regents of the University of California. All rights reserved. | ||
| 4 | * | ||
| 5 | * Redistribution and use in source and binary forms, with or without | ||
| 6 | * modification, are permitted provided that the following conditions | ||
| 7 | * are met: | ||
| 8 | * 1. Redistributions of source code must retain the above copyright | ||
| 9 | * notice, this list of conditions and the following disclaimer. | ||
| 10 | * 2. Redistributions in binary form must reproduce the above copyright | ||
| 11 | * notice, this list of conditions and the following disclaimer in the | ||
| 12 | * documentation and/or other materials provided with the distribution. | ||
| 13 | * 3. All advertising materials mentioning features or use of this software | ||
| 14 | * must display the following acknowledgement: | ||
| 15 | * This product includes software developed by the University of | ||
| 16 | * California, Berkeley and its contributors. | ||
| 17 | * 4. Neither the name of the University nor the names of its contributors | ||
| 18 | * may be used to endorse or promote products derived from this software | ||
| 19 | * without specific prior written permission. | ||
| 20 | * | ||
| 21 | * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND | ||
| 22 | * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE | ||
| 23 | * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE | ||
| 24 | * ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE | ||
| 25 | * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL | ||
| 26 | * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS | ||
| 27 | * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) | ||
| 28 | * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT | ||
| 29 | * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY | ||
| 30 | * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF | ||
| 31 | * SUCH DAMAGE. | ||
| 32 | */ | ||
| 33 | |||
| 34 | #if defined(LIBC_SCCS) && !defined(lint) | ||
| 35 | /*static char sccsid[] = "from: @(#)qsort.c 8.1 (Berkeley) 6/4/93";*/ | ||
| 36 | static char *rcsid = "$Id: qsort.c,v 1.1.1.1 1995/10/18 08:42:18 deraadt Exp $"; | ||
| 37 | #endif /* LIBC_SCCS and not lint */ | ||
| 38 | |||
| 39 | #include <sys/types.h> | ||
| 40 | #include <stdlib.h> | ||
| 41 | |||
| 42 | static inline char *med3 __P((char *, char *, char *, int (*)())); | ||
| 43 | static inline void swapfunc __P((char *, char *, int, int)); | ||
| 44 | |||
| 45 | #define min(a, b) (a) < (b) ? a : b | ||
| 46 | |||
| 47 | /* | ||
| 48 | * Qsort routine from Bentley & McIlroy's "Engineering a Sort Function". | ||
| 49 | */ | ||
| 50 | #define swapcode(TYPE, parmi, parmj, n) { \ | ||
| 51 | long i = (n) / sizeof (TYPE); \ | ||
| 52 | register TYPE *pi = (TYPE *) (parmi); \ | ||
| 53 | register TYPE *pj = (TYPE *) (parmj); \ | ||
| 54 | do { \ | ||
| 55 | register TYPE t = *pi; \ | ||
| 56 | *pi++ = *pj; \ | ||
| 57 | *pj++ = t; \ | ||
| 58 | } while (--i > 0); \ | ||
| 59 | } | ||
| 60 | |||
| 61 | #define SWAPINIT(a, es) swaptype = ((char *)a - (char *)0) % sizeof(long) || \ | ||
| 62 | es % sizeof(long) ? 2 : es == sizeof(long)? 0 : 1; | ||
| 63 | |||
| 64 | static inline void | ||
| 65 | swapfunc(a, b, n, swaptype) | ||
| 66 | char *a, *b; | ||
| 67 | int n, swaptype; | ||
| 68 | { | ||
| 69 | if(swaptype <= 1) | ||
| 70 | swapcode(long, a, b, n) | ||
| 71 | else | ||
| 72 | swapcode(char, a, b, n) | ||
| 73 | } | ||
| 74 | |||
| 75 | #define swap(a, b) \ | ||
| 76 | if (swaptype == 0) { \ | ||
| 77 | long t = *(long *)(a); \ | ||
| 78 | *(long *)(a) = *(long *)(b); \ | ||
| 79 | *(long *)(b) = t; \ | ||
| 80 | } else \ | ||
| 81 | swapfunc(a, b, es, swaptype) | ||
| 82 | |||
| 83 | #define vecswap(a, b, n) if ((n) > 0) swapfunc(a, b, n, swaptype) | ||
| 84 | |||
| 85 | static inline char * | ||
| 86 | med3(a, b, c, cmp) | ||
| 87 | char *a, *b, *c; | ||
| 88 | int (*cmp)(); | ||
| 89 | { | ||
| 90 | return cmp(a, b) < 0 ? | ||
| 91 | (cmp(b, c) < 0 ? b : (cmp(a, c) < 0 ? c : a )) | ||
| 92 | :(cmp(b, c) > 0 ? b : (cmp(a, c) < 0 ? a : c )); | ||
| 93 | } | ||
| 94 | |||
| 95 | void | ||
| 96 | qsort(a, n, es, cmp) | ||
| 97 | void *a; | ||
| 98 | size_t n, es; | ||
| 99 | int (*cmp)(); | ||
| 100 | { | ||
| 101 | char *pa, *pb, *pc, *pd, *pl, *pm, *pn; | ||
| 102 | int d, r, swaptype, swap_cnt; | ||
| 103 | |||
| 104 | loop: SWAPINIT(a, es); | ||
| 105 | swap_cnt = 0; | ||
| 106 | if (n < 7) { | ||
| 107 | for (pm = a + es; pm < (char *) a + n * es; pm += es) | ||
| 108 | for (pl = pm; pl > (char *) a && cmp(pl - es, pl) > 0; | ||
| 109 | pl -= es) | ||
| 110 | swap(pl, pl - es); | ||
| 111 | return; | ||
| 112 | } | ||
| 113 | pm = a + (n / 2) * es; | ||
| 114 | if (n > 7) { | ||
| 115 | pl = a; | ||
| 116 | pn = a + (n - 1) * es; | ||
| 117 | if (n > 40) { | ||
| 118 | d = (n / 8) * es; | ||
| 119 | pl = med3(pl, pl + d, pl + 2 * d, cmp); | ||
| 120 | pm = med3(pm - d, pm, pm + d, cmp); | ||
| 121 | pn = med3(pn - 2 * d, pn - d, pn, cmp); | ||
| 122 | } | ||
| 123 | pm = med3(pl, pm, pn, cmp); | ||
| 124 | } | ||
| 125 | swap(a, pm); | ||
| 126 | pa = pb = a + es; | ||
| 127 | |||
| 128 | pc = pd = a + (n - 1) * es; | ||
| 129 | for (;;) { | ||
| 130 | while (pb <= pc && (r = cmp(pb, a)) <= 0) { | ||
| 131 | if (r == 0) { | ||
| 132 | swap_cnt = 1; | ||
| 133 | swap(pa, pb); | ||
| 134 | pa += es; | ||
| 135 | } | ||
| 136 | pb += es; | ||
| 137 | } | ||
| 138 | while (pb <= pc && (r = cmp(pc, a)) >= 0) { | ||
| 139 | if (r == 0) { | ||
| 140 | swap_cnt = 1; | ||
| 141 | swap(pc, pd); | ||
| 142 | pd -= es; | ||
| 143 | } | ||
| 144 | pc -= es; | ||
| 145 | } | ||
| 146 | if (pb > pc) | ||
| 147 | break; | ||
| 148 | swap(pb, pc); | ||
| 149 | swap_cnt = 1; | ||
| 150 | pb += es; | ||
| 151 | pc -= es; | ||
| 152 | } | ||
| 153 | if (swap_cnt == 0) { /* Switch to insertion sort */ | ||
| 154 | for (pm = a + es; pm < (char *) a + n * es; pm += es) | ||
| 155 | for (pl = pm; pl > (char *) a && cmp(pl - es, pl) > 0; | ||
| 156 | pl -= es) | ||
| 157 | swap(pl, pl - es); | ||
| 158 | return; | ||
| 159 | } | ||
| 160 | |||
| 161 | pn = a + n * es; | ||
| 162 | r = min(pa - (char *)a, pb - pa); | ||
| 163 | vecswap(a, pb - r, r); | ||
| 164 | r = min(pd - pc, pn - pd - es); | ||
| 165 | vecswap(pb, pn - r, r); | ||
| 166 | if ((r = pb - pa) > es) | ||
| 167 | qsort(a, r / es, es, cmp); | ||
| 168 | if ((r = pd - pc) > es) { | ||
| 169 | /* Iterate rather than recurse to save stack space */ | ||
| 170 | a = pn - r; | ||
| 171 | n = r / es; | ||
| 172 | goto loop; | ||
| 173 | } | ||
| 174 | /* qsort(pn - r, r / es, es, cmp);*/ | ||
| 175 | } | ||
diff --git a/src/lib/libc/stdlib/radixsort.3 b/src/lib/libc/stdlib/radixsort.3 new file mode 100644 index 0000000000..a2af9f17a4 --- /dev/null +++ b/src/lib/libc/stdlib/radixsort.3 | |||
| @@ -0,0 +1,161 @@ | |||
| 1 | .\" Copyright (c) 1990, 1991, 1993 | ||
| 2 | .\" The Regents of the University of California. All rights reserved. | ||
| 3 | .\" | ||
| 4 | .\" Redistribution and use in source and binary forms, with or without | ||
| 5 | .\" modification, are permitted provided that the following conditions | ||
| 6 | .\" are met: | ||
| 7 | .\" 1. Redistributions of source code must retain the above copyright | ||
| 8 | .\" notice, this list of conditions and the following disclaimer. | ||
| 9 | .\" 2. Redistributions in binary form must reproduce the above copyright | ||
| 10 | .\" notice, this list of conditions and the following disclaimer in the | ||
| 11 | .\" documentation and/or other materials provided with the distribution. | ||
| 12 | .\" 3. All advertising materials mentioning features or use of this software | ||
| 13 | .\" must display the following acknowledgement: | ||
| 14 | .\" This product includes software developed by the University of | ||
| 15 | .\" California, Berkeley and its contributors. | ||
| 16 | .\" 4. Neither the name of the University nor the names of its contributors | ||
| 17 | .\" may be used to endorse or promote products derived from this software | ||
| 18 | .\" without specific prior written permission. | ||
| 19 | .\" | ||
| 20 | .\" THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND | ||
| 21 | .\" ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE | ||
| 22 | .\" IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE | ||
| 23 | .\" ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE | ||
| 24 | .\" FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL | ||
| 25 | .\" DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS | ||
| 26 | .\" OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) | ||
| 27 | .\" HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT | ||
| 28 | .\" LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY | ||
| 29 | .\" OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF | ||
| 30 | .\" SUCH DAMAGE. | ||
| 31 | .\" | ||
| 32 | .\" from: @(#)radixsort.3 8.2 (Berkeley) 1/27/94 | ||
| 33 | .\" $Id: radixsort.3,v 1.1.1.1 1995/10/18 08:42:18 deraadt Exp $ | ||
| 34 | .\" | ||
| 35 | .Dd January 27, 1994 | ||
| 36 | .Dt RADIXSORT 3 | ||
| 37 | .Os | ||
| 38 | .Sh NAME | ||
| 39 | .Nm radixsort | ||
| 40 | .Nd radix sort | ||
| 41 | .Sh SYNOPSIS | ||
| 42 | .Fd #include <limits.h> | ||
| 43 | .Fd #include <stdlib.h> | ||
| 44 | .Ft int | ||
| 45 | .Fn radixsort "u_char **base" "int nmemb" "u_char *table" "u_int endbyte" | ||
| 46 | .Ft int | ||
| 47 | .Fn sradixsort "u_char **base" "int nmemb" "u_char *table" "u_int endbyte" | ||
| 48 | .Sh DESCRIPTION | ||
| 49 | The | ||
| 50 | .Fn radixsort | ||
| 51 | and | ||
| 52 | .Fn sradixsort | ||
| 53 | functions | ||
| 54 | are implementations of radix sort. | ||
| 55 | .Pp | ||
| 56 | These functions sort an array of pointers to byte strings, the initial | ||
| 57 | member of which is referenced by | ||
| 58 | .Fa base . | ||
| 59 | The byte strings may contain any values; the end of each string | ||
| 60 | is denoted by the user-specified value | ||
| 61 | .Fa endbyte . | ||
| 62 | .Pp | ||
| 63 | Applications may specify a sort order by providing the | ||
| 64 | .Fa table | ||
| 65 | argument. | ||
| 66 | If | ||
| 67 | .Pf non- Dv NULL , | ||
| 68 | .Fa table | ||
| 69 | must reference an array of | ||
| 70 | .Dv UCHAR_MAX | ||
| 71 | + 1 bytes which contains the sort | ||
| 72 | weight of each possible byte value. | ||
| 73 | The end-of-string byte must have a sort weight of 0 or 255 | ||
| 74 | (for sorting in reverse order). | ||
| 75 | More than one byte may have the same sort weight. | ||
| 76 | The | ||
| 77 | .Fa table | ||
| 78 | argument | ||
| 79 | is useful for applications which wish to sort different characters | ||
| 80 | equally, for example, providing a table with the same weights | ||
| 81 | for A-Z as for a-z will result in a case-insensitive sort. | ||
| 82 | If | ||
| 83 | .Fa table | ||
| 84 | is NULL, the contents of the array are sorted in ascending order | ||
| 85 | according to the | ||
| 86 | .Tn ASCII | ||
| 87 | order of the byte strings they reference and | ||
| 88 | .Fa endbyte | ||
| 89 | has a sorting weight of 0. | ||
| 90 | .Pp | ||
| 91 | The | ||
| 92 | .Fn sradixsort | ||
| 93 | function is stable, that is, if two elements compare as equal, their | ||
| 94 | order in the sorted array is unchanged. | ||
| 95 | The | ||
| 96 | .Fn sradixsort | ||
| 97 | function uses additional memory sufficient to hold | ||
| 98 | .Fa nmemb | ||
| 99 | pointers. | ||
| 100 | .Pp | ||
| 101 | The | ||
| 102 | .Fn radixsort | ||
| 103 | function is not stable, but uses no additional memory. | ||
| 104 | .Pp | ||
| 105 | These functions are variants of most-significant-byte radix sorting; in | ||
| 106 | particular, see D.E. Knuth's Algorithm R and section 5.2.5, exercise 10. | ||
| 107 | They take linear time relative to the number of bytes in the strings. | ||
| 108 | .Sh RETURN VALUES | ||
| 109 | Upon successful completion 0 is returned. | ||
| 110 | Otherwise, \-1 is returned and the global variable | ||
| 111 | .Va errno | ||
| 112 | is set to indicate the error. | ||
| 113 | .Sh ERRORS | ||
| 114 | .Bl -tag -width Er | ||
| 115 | .It Bq Er EINVAL | ||
| 116 | The value of the | ||
| 117 | .Fa endbyte | ||
| 118 | element of | ||
| 119 | .Fa table | ||
| 120 | is not 0 or 255. | ||
| 121 | .El | ||
| 122 | .Pp | ||
| 123 | Additionally, the | ||
| 124 | .Fn sradixsort | ||
| 125 | function | ||
| 126 | may fail and set | ||
| 127 | .Va errno | ||
| 128 | for any of the errors specified for the library routine | ||
| 129 | .Xr malloc 3 . | ||
| 130 | .Sh SEE ALSO | ||
| 131 | .Xr sort 1 , | ||
| 132 | .Xr qsort 3 | ||
| 133 | .Pp | ||
| 134 | .Rs | ||
| 135 | .%A Knuth, D.E. | ||
| 136 | .%D 1968 | ||
| 137 | .%B "The Art of Computer Programming" | ||
| 138 | .%T "Sorting and Searching" | ||
| 139 | .%V Vol. 3 | ||
| 140 | .%P pp. 170-178 | ||
| 141 | .Re | ||
| 142 | .Rs | ||
| 143 | .%A Paige, R. | ||
| 144 | .%D 1987 | ||
| 145 | .%T "Three Partition Refinement Algorithms" | ||
| 146 | .%J "SIAM J. Comput." | ||
| 147 | .%V Vol. 16 | ||
| 148 | .%N No. 6 | ||
| 149 | .Re | ||
| 150 | .Rs | ||
| 151 | .%A McIlroy, P. | ||
| 152 | .%D 1993 | ||
| 153 | .%B "Engineering Radix Sort" | ||
| 154 | .%T "Computing Systems" | ||
| 155 | .%V Vol. 6:1 | ||
| 156 | .%P pp. 5-27 | ||
| 157 | .Re | ||
| 158 | .Sh HISTORY | ||
| 159 | The | ||
| 160 | .Fn radixsort | ||
| 161 | function first appeared in 4.4BSD. | ||
diff --git a/src/lib/libc/stdlib/radixsort.c b/src/lib/libc/stdlib/radixsort.c new file mode 100644 index 0000000000..dd51013c94 --- /dev/null +++ b/src/lib/libc/stdlib/radixsort.c | |||
| @@ -0,0 +1,318 @@ | |||
| 1 | /*- | ||
| 2 | * Copyright (c) 1990, 1993 | ||
| 3 | * The Regents of the University of California. All rights reserved. | ||
| 4 | * | ||
| 5 | * This code is derived from software contributed to Berkeley by | ||
| 6 | * Peter McIlroy and by Dan Bernstein at New York University, | ||
| 7 | * | ||
| 8 | * Redistribution and use in source and binary forms, with or without | ||
| 9 | * modification, are permitted provided that the following conditions | ||
| 10 | * are met: | ||
| 11 | * 1. Redistributions of source code must retain the above copyright | ||
| 12 | * notice, this list of conditions and the following disclaimer. | ||
| 13 | * 2. Redistributions in binary form must reproduce the above copyright | ||
| 14 | * notice, this list of conditions and the following disclaimer in the | ||
| 15 | * documentation and/or other materials provided with the distribution. | ||
| 16 | * 3. All advertising materials mentioning features or use of this software | ||
| 17 | * must display the following acknowledgement: | ||
| 18 | * This product includes software developed by the University of | ||
| 19 | * California, Berkeley and its contributors. | ||
| 20 | * 4. Neither the name of the University nor the names of its contributors | ||
| 21 | * may be used to endorse or promote products derived from this software | ||
| 22 | * without specific prior written permission. | ||
| 23 | * | ||
| 24 | * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND | ||
| 25 | * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE | ||
| 26 | * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE | ||
| 27 | * ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE | ||
| 28 | * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL | ||
| 29 | * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS | ||
| 30 | * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) | ||
| 31 | * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT | ||
| 32 | * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY | ||
| 33 | * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF | ||
| 34 | * SUCH DAMAGE. | ||
| 35 | */ | ||
| 36 | |||
| 37 | #if defined(LIBC_SCCS) && !defined(lint) | ||
| 38 | /*static char sccsid[] = "from: @(#)radixsort.c 8.1 (Berkeley) 6/4/93";*/ | ||
| 39 | static char *rcsid = "$Id: radixsort.c,v 1.1.1.1 1995/10/18 08:42:19 deraadt Exp $"; | ||
| 40 | #endif /* LIBC_SCCS and not lint */ | ||
| 41 | |||
| 42 | /* | ||
| 43 | * Radixsort routines. | ||
| 44 | * | ||
| 45 | * Program r_sort_a() is unstable but uses O(logN) extra memory for a stack. | ||
| 46 | * Use radixsort(a, n, trace, endchar) for this case. | ||
| 47 | * | ||
| 48 | * For stable sorting (using N extra pointers) use sradixsort(), which calls | ||
| 49 | * r_sort_b(). | ||
| 50 | * | ||
| 51 | * For a description of this code, see D. McIlroy, P. McIlroy, K. Bostic, | ||
| 52 | * "Engineering Radix Sort". | ||
| 53 | */ | ||
| 54 | |||
| 55 | #include <sys/types.h> | ||
| 56 | #include <stdlib.h> | ||
| 57 | #include <errno.h> | ||
| 58 | |||
| 59 | typedef struct { | ||
| 60 | const u_char **sa; | ||
| 61 | int sn, si; | ||
| 62 | } stack; | ||
| 63 | |||
| 64 | static inline void simplesort | ||
| 65 | __P((const u_char **, int, int, const u_char *, u_int)); | ||
| 66 | static void r_sort_a __P((const u_char **, int, int, const u_char *, u_int)); | ||
| 67 | static void r_sort_b __P((const u_char **, | ||
| 68 | const u_char **, int, int, const u_char *, u_int)); | ||
| 69 | |||
| 70 | #define THRESHOLD 20 /* Divert to simplesort(). */ | ||
| 71 | #define SIZE 512 /* Default stack size. */ | ||
| 72 | |||
| 73 | #define SETUP { \ | ||
| 74 | if (tab == NULL) { \ | ||
| 75 | tr = tr0; \ | ||
| 76 | for (c = 0; c < endch; c++) \ | ||
| 77 | tr0[c] = c + 1; \ | ||
| 78 | tr0[c] = 0; \ | ||
| 79 | for (c++; c < 256; c++) \ | ||
| 80 | tr0[c] = c; \ | ||
| 81 | endch = 0; \ | ||
| 82 | } else { \ | ||
| 83 | endch = tab[endch]; \ | ||
| 84 | tr = tab; \ | ||
| 85 | if (endch != 0 && endch != 255) { \ | ||
| 86 | errno = EINVAL; \ | ||
| 87 | return (-1); \ | ||
| 88 | } \ | ||
| 89 | } \ | ||
| 90 | } | ||
| 91 | |||
| 92 | int | ||
| 93 | radixsort(a, n, tab, endch) | ||
| 94 | const u_char **a, *tab; | ||
| 95 | int n; | ||
| 96 | u_int endch; | ||
| 97 | { | ||
| 98 | const u_char *tr; | ||
| 99 | int c; | ||
| 100 | u_char tr0[256]; | ||
| 101 | |||
| 102 | SETUP; | ||
| 103 | r_sort_a(a, n, 0, tr, endch); | ||
| 104 | return (0); | ||
| 105 | } | ||
| 106 | |||
| 107 | int | ||
| 108 | sradixsort(a, n, tab, endch) | ||
| 109 | const u_char **a, *tab; | ||
| 110 | int n; | ||
| 111 | u_int endch; | ||
| 112 | { | ||
| 113 | const u_char *tr, **ta; | ||
| 114 | int c; | ||
| 115 | u_char tr0[256]; | ||
| 116 | |||
| 117 | SETUP; | ||
| 118 | if (n < THRESHOLD) | ||
| 119 | simplesort(a, n, 0, tr, endch); | ||
| 120 | else { | ||
| 121 | if ((ta = malloc(n * sizeof(a))) == NULL) | ||
| 122 | return (-1); | ||
| 123 | r_sort_b(a, ta, n, 0, tr, endch); | ||
| 124 | free(ta); | ||
| 125 | } | ||
| 126 | return (0); | ||
| 127 | } | ||
| 128 | |||
| 129 | #define empty(s) (s >= sp) | ||
| 130 | #define pop(a, n, i) a = (--sp)->sa, n = sp->sn, i = sp->si | ||
| 131 | #define push(a, n, i) sp->sa = a, sp->sn = n, (sp++)->si = i | ||
| 132 | #define swap(a, b, t) t = a, a = b, b = t | ||
| 133 | |||
| 134 | /* Unstable, in-place sort. */ | ||
| 135 | void | ||
| 136 | r_sort_a(a, n, i, tr, endch) | ||
| 137 | const u_char **a; | ||
| 138 | int n, i; | ||
| 139 | const u_char *tr; | ||
| 140 | u_int endch; | ||
| 141 | { | ||
| 142 | static int count[256], nc, bmin; | ||
| 143 | register int c; | ||
| 144 | register const u_char **ak, *r; | ||
| 145 | stack s[SIZE], *sp, *sp0, *sp1, temp; | ||
| 146 | int *cp, bigc; | ||
| 147 | const u_char **an, *t, **aj, **top[256]; | ||
| 148 | |||
| 149 | /* Set up stack. */ | ||
| 150 | sp = s; | ||
| 151 | push(a, n, i); | ||
| 152 | while (!empty(s)) { | ||
| 153 | pop(a, n, i); | ||
| 154 | if (n < THRESHOLD) { | ||
| 155 | simplesort(a, n, i, tr, endch); | ||
| 156 | continue; | ||
| 157 | } | ||
| 158 | an = a + n; | ||
| 159 | |||
| 160 | /* Make character histogram. */ | ||
| 161 | if (nc == 0) { | ||
| 162 | bmin = 255; /* First occupied bin, excluding eos. */ | ||
| 163 | for (ak = a; ak < an;) { | ||
| 164 | c = tr[(*ak++)[i]]; | ||
| 165 | if (++count[c] == 1 && c != endch) { | ||
| 166 | if (c < bmin) | ||
| 167 | bmin = c; | ||
| 168 | nc++; | ||
| 169 | } | ||
| 170 | } | ||
| 171 | if (sp + nc > s + SIZE) { /* Get more stack. */ | ||
| 172 | r_sort_a(a, n, i, tr, endch); | ||
| 173 | continue; | ||
| 174 | } | ||
| 175 | } | ||
| 176 | |||
| 177 | /* | ||
| 178 | * Set top[]; push incompletely sorted bins onto stack. | ||
| 179 | * top[] = pointers to last out-of-place element in bins. | ||
| 180 | * count[] = counts of elements in bins. | ||
| 181 | * Before permuting: top[c-1] + count[c] = top[c]; | ||
| 182 | * during deal: top[c] counts down to top[c-1]. | ||
| 183 | */ | ||
| 184 | sp0 = sp1 = sp; /* Stack position of biggest bin. */ | ||
| 185 | bigc = 2; /* Size of biggest bin. */ | ||
| 186 | if (endch == 0) /* Special case: set top[eos]. */ | ||
| 187 | top[0] = ak = a + count[0]; | ||
| 188 | else { | ||
| 189 | ak = a; | ||
| 190 | top[255] = an; | ||
| 191 | } | ||
| 192 | for (cp = count + bmin; nc > 0; cp++) { | ||
| 193 | while (*cp == 0) /* Find next non-empty pile. */ | ||
| 194 | cp++; | ||
| 195 | if (*cp > 1) { | ||
| 196 | if (*cp > bigc) { | ||
| 197 | bigc = *cp; | ||
| 198 | sp1 = sp; | ||
| 199 | } | ||
| 200 | push(ak, *cp, i+1); | ||
| 201 | } | ||
| 202 | top[cp-count] = ak += *cp; | ||
| 203 | nc--; | ||
| 204 | } | ||
| 205 | swap(*sp0, *sp1, temp); /* Play it safe -- biggest bin last. */ | ||
| 206 | |||
| 207 | /* | ||
| 208 | * Permute misplacements home. Already home: everything | ||
| 209 | * before aj, and in bin[c], items from top[c] on. | ||
| 210 | * Inner loop: | ||
| 211 | * r = next element to put in place; | ||
| 212 | * ak = top[r[i]] = location to put the next element. | ||
| 213 | * aj = bottom of 1st disordered bin. | ||
| 214 | * Outer loop: | ||
| 215 | * Once the 1st disordered bin is done, ie. aj >= ak, | ||
| 216 | * aj<-aj + count[c] connects the bins in a linked list; | ||
| 217 | * reset count[c]. | ||
| 218 | */ | ||
| 219 | for (aj = a; aj < an; *aj = r, aj += count[c], count[c] = 0) | ||
| 220 | for (r = *aj; aj < (ak = --top[c = tr[r[i]]]);) | ||
| 221 | swap(*ak, r, t); | ||
| 222 | } | ||
| 223 | } | ||
| 224 | |||
| 225 | /* Stable sort, requiring additional memory. */ | ||
| 226 | void | ||
| 227 | r_sort_b(a, ta, n, i, tr, endch) | ||
| 228 | const u_char **a, **ta; | ||
| 229 | int n, i; | ||
| 230 | const u_char *tr; | ||
| 231 | u_int endch; | ||
| 232 | { | ||
| 233 | static int count[256], nc, bmin; | ||
| 234 | register int c; | ||
| 235 | register const u_char **ak, **ai; | ||
| 236 | stack s[512], *sp, *sp0, *sp1, temp; | ||
| 237 | const u_char **top[256]; | ||
| 238 | int *cp, bigc; | ||
| 239 | |||
| 240 | sp = s; | ||
| 241 | push(a, n, i); | ||
| 242 | while (!empty(s)) { | ||
| 243 | pop(a, n, i); | ||
| 244 | if (n < THRESHOLD) { | ||
| 245 | simplesort(a, n, i, tr, endch); | ||
| 246 | continue; | ||
| 247 | } | ||
| 248 | |||
| 249 | if (nc == 0) { | ||
| 250 | bmin = 255; | ||
| 251 | for (ak = a + n; --ak >= a;) { | ||
| 252 | c = tr[(*ak)[i]]; | ||
| 253 | if (++count[c] == 1 && c != endch) { | ||
| 254 | if (c < bmin) | ||
| 255 | bmin = c; | ||
| 256 | nc++; | ||
| 257 | } | ||
| 258 | } | ||
| 259 | if (sp + nc > s + SIZE) { | ||
| 260 | r_sort_b(a, ta, n, i, tr, endch); | ||
| 261 | continue; | ||
| 262 | } | ||
| 263 | } | ||
| 264 | |||
| 265 | sp0 = sp1 = sp; | ||
| 266 | bigc = 2; | ||
| 267 | if (endch == 0) { | ||
| 268 | top[0] = ak = a + count[0]; | ||
| 269 | count[0] = 0; | ||
| 270 | } else { | ||
| 271 | ak = a; | ||
| 272 | top[255] = a + n; | ||
| 273 | count[255] = 0; | ||
| 274 | } | ||
| 275 | for (cp = count + bmin; nc > 0; cp++) { | ||
| 276 | while (*cp == 0) | ||
| 277 | cp++; | ||
| 278 | if ((c = *cp) > 1) { | ||
| 279 | if (c > bigc) { | ||
| 280 | bigc = c; | ||
| 281 | sp1 = sp; | ||
| 282 | } | ||
| 283 | push(ak, c, i+1); | ||
| 284 | } | ||
| 285 | top[cp-count] = ak += c; | ||
| 286 | *cp = 0; /* Reset count[]. */ | ||
| 287 | nc--; | ||
| 288 | } | ||
| 289 | swap(*sp0, *sp1, temp); | ||
| 290 | |||
| 291 | for (ak = ta + n, ai = a+n; ak > ta;) /* Copy to temp. */ | ||
| 292 | *--ak = *--ai; | ||
| 293 | for (ak = ta+n; --ak >= ta;) /* Deal to piles. */ | ||
| 294 | *--top[tr[(*ak)[i]]] = *ak; | ||
| 295 | } | ||
| 296 | } | ||
| 297 | |||
| 298 | static inline void | ||
| 299 | simplesort(a, n, b, tr, endch) /* insertion sort */ | ||
| 300 | register const u_char **a; | ||
| 301 | int n, b; | ||
| 302 | register const u_char *tr; | ||
| 303 | u_int endch; | ||
| 304 | { | ||
| 305 | register u_char ch; | ||
| 306 | const u_char **ak, **ai, *s, *t; | ||
| 307 | |||
| 308 | for (ak = a+1; --n >= 1; ak++) | ||
| 309 | for (ai = ak; ai > a; ai--) { | ||
| 310 | for (s = ai[0] + b, t = ai[-1] + b; | ||
| 311 | (ch = tr[*s]) != endch; s++, t++) | ||
| 312 | if (ch != tr[*t]) | ||
| 313 | break; | ||
| 314 | if (ch >= tr[*t]) | ||
| 315 | break; | ||
| 316 | swap(ai[0], ai[-1], s); | ||
| 317 | } | ||
| 318 | } | ||
diff --git a/src/lib/libc/stdlib/rand.3 b/src/lib/libc/stdlib/rand.3 new file mode 100644 index 0000000000..a0e7740e66 --- /dev/null +++ b/src/lib/libc/stdlib/rand.3 | |||
| @@ -0,0 +1,85 @@ | |||
| 1 | .\" Copyright (c) 1990, 1991 The Regents of the University of California. | ||
| 2 | .\" All rights reserved. | ||
| 3 | .\" | ||
| 4 | .\" This code is derived from software contributed to Berkeley by | ||
| 5 | .\" the American National Standards Committee X3, on Information | ||
| 6 | .\" Processing Systems. | ||
| 7 | .\" | ||
| 8 | .\" Redistribution and use in source and binary forms, with or without | ||
| 9 | .\" modification, are permitted provided that the following conditions | ||
| 10 | .\" are met: | ||
| 11 | .\" 1. Redistributions of source code must retain the above copyright | ||
| 12 | .\" notice, this list of conditions and the following disclaimer. | ||
| 13 | .\" 2. Redistributions in binary form must reproduce the above copyright | ||
| 14 | .\" notice, this list of conditions and the following disclaimer in the | ||
| 15 | .\" documentation and/or other materials provided with the distribution. | ||
| 16 | .\" 3. All advertising materials mentioning features or use of this software | ||
| 17 | .\" must display the following acknowledgement: | ||
| 18 | .\" This product includes software developed by the University of | ||
| 19 | .\" California, Berkeley and its contributors. | ||
| 20 | .\" 4. Neither the name of the University nor the names of its contributors | ||
| 21 | .\" may be used to endorse or promote products derived from this software | ||
| 22 | .\" without specific prior written permission. | ||
| 23 | .\" | ||
| 24 | .\" THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND | ||
| 25 | .\" ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE | ||
| 26 | .\" IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE | ||
| 27 | .\" ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE | ||
| 28 | .\" FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL | ||
| 29 | .\" DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS | ||
| 30 | .\" OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) | ||
| 31 | .\" HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT | ||
| 32 | .\" LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY | ||
| 33 | .\" OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF | ||
| 34 | .\" SUCH DAMAGE. | ||
| 35 | .\" | ||
| 36 | .\" from: @(#)rand.3 6.7 (Berkeley) 6/29/91 | ||
| 37 | .\" $Id: rand.3,v 1.1.1.1 1995/10/18 08:42:19 deraadt Exp $ | ||
| 38 | .\" | ||
| 39 | .Dd June 29, 1991 | ||
| 40 | .Dt RAND 3 | ||
| 41 | .Os | ||
| 42 | .Sh NAME | ||
| 43 | .Nm rand , | ||
| 44 | .Nm srand | ||
| 45 | .Nd bad random number generator | ||
| 46 | .Sh SYNOPSIS | ||
| 47 | .Fd #include <stdlib.h> | ||
| 48 | .Ft void | ||
| 49 | .Fn srand "unsigned seed" | ||
| 50 | .Ft int | ||
| 51 | .Fn rand void | ||
| 52 | .Sh DESCRIPTION | ||
| 53 | .Bf -symbolic | ||
| 54 | These interfaces are obsoleted by random(3). | ||
| 55 | .Ef | ||
| 56 | .Pp | ||
| 57 | The | ||
| 58 | .Fn rand | ||
| 59 | function computes a sequence of pseudo-random integers in the range | ||
| 60 | of 0 to | ||
| 61 | .Dv RAND_MAX | ||
| 62 | (as defined by the header file | ||
| 63 | .Aq Pa stdlib.h ) . | ||
| 64 | .Pp | ||
| 65 | The | ||
| 66 | .Fn srand | ||
| 67 | function sets its argument as the seed for a new sequence of | ||
| 68 | pseudo-random numbers to be returned by | ||
| 69 | .Fn rand . | ||
| 70 | These sequences are repeatable by calling | ||
| 71 | .Fn srand | ||
| 72 | with the same seed value. | ||
| 73 | .Pp | ||
| 74 | If no seed value is provided, the functions are automatically | ||
| 75 | seeded with a value of 1. | ||
| 76 | .Sh SEE ALSO | ||
| 77 | .Xr random 3 | ||
| 78 | .Sh STANDARDS | ||
| 79 | The | ||
| 80 | .Fn rand | ||
| 81 | and | ||
| 82 | .Fn srand | ||
| 83 | functions | ||
| 84 | conform to | ||
| 85 | .St -ansiC . | ||
diff --git a/src/lib/libc/stdlib/rand.c b/src/lib/libc/stdlib/rand.c new file mode 100644 index 0000000000..361d473448 --- /dev/null +++ b/src/lib/libc/stdlib/rand.c | |||
| @@ -0,0 +1,55 @@ | |||
| 1 | /*- | ||
| 2 | * Copyright (c) 1990 The Regents of the University of California. | ||
| 3 | * All rights reserved. | ||
| 4 | * | ||
| 5 | * Redistribution and use in source and binary forms, with or without | ||
| 6 | * modification, are permitted provided that the following conditions | ||
| 7 | * are met: | ||
| 8 | * 1. Redistributions of source code must retain the above copyright | ||
| 9 | * notice, this list of conditions and the following disclaimer. | ||
| 10 | * 2. Redistributions in binary form must reproduce the above copyright | ||
| 11 | * notice, this list of conditions and the following disclaimer in the | ||
| 12 | * documentation and/or other materials provided with the distribution. | ||
| 13 | * 3. All advertising materials mentioning features or use of this software | ||
| 14 | * must display the following acknowledgement: | ||
| 15 | * This product includes software developed by the University of | ||
| 16 | * California, Berkeley and its contributors. | ||
| 17 | * 4. Neither the name of the University nor the names of its contributors | ||
| 18 | * may be used to endorse or promote products derived from this software | ||
| 19 | * without specific prior written permission. | ||
| 20 | * | ||
| 21 | * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND | ||
| 22 | * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE | ||
| 23 | * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE | ||
| 24 | * ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE | ||
| 25 | * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL | ||
| 26 | * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS | ||
| 27 | * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) | ||
| 28 | * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT | ||
| 29 | * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY | ||
| 30 | * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF | ||
| 31 | * SUCH DAMAGE. | ||
| 32 | */ | ||
| 33 | |||
| 34 | #if defined(LIBC_SCCS) && !defined(lint) | ||
| 35 | /*static char *sccsid = "from: @(#)rand.c 5.6 (Berkeley) 6/24/91";*/ | ||
| 36 | static char *rcsid = "$Id: rand.c,v 1.1.1.1 1995/10/18 08:42:19 deraadt Exp $"; | ||
| 37 | #endif /* LIBC_SCCS and not lint */ | ||
| 38 | |||
| 39 | #include <sys/types.h> | ||
| 40 | #include <stdlib.h> | ||
| 41 | |||
| 42 | static u_long next = 1; | ||
| 43 | |||
| 44 | int | ||
| 45 | rand() | ||
| 46 | { | ||
| 47 | return ((next = next * 1103515245 + 12345) % ((u_int)RAND_MAX + 1)); | ||
| 48 | } | ||
| 49 | |||
| 50 | void | ||
| 51 | srand(seed) | ||
| 52 | u_int seed; | ||
| 53 | { | ||
| 54 | next = seed; | ||
| 55 | } | ||
diff --git a/src/lib/libc/stdlib/rand48.3 b/src/lib/libc/stdlib/rand48.3 new file mode 100644 index 0000000000..5a772c9a8c --- /dev/null +++ b/src/lib/libc/stdlib/rand48.3 | |||
| @@ -0,0 +1,160 @@ | |||
| 1 | \" Copyright (c) 1993 Martin Birgmeier | ||
| 2 | .\" All rights reserved. | ||
| 3 | .\" | ||
| 4 | .\" You may redistribute unmodified or modified versions of this source | ||
| 5 | .\" code provided that the above copyright notice and this and the | ||
| 6 | .\" following conditions are retained. | ||
| 7 | .\" | ||
| 8 | .\" This software is provided ``as is'', and comes with no warranties | ||
| 9 | .\" of any kind. I shall in no event be liable for anything that happens | ||
| 10 | .\" to anyone/anything when using this software. | ||
| 11 | .\" | ||
| 12 | .\" $Id: rand48.3,v 1.1.1.1 1995/10/18 08:42:19 deraadt Exp $ | ||
| 13 | .\" | ||
| 14 | .Dd October 8, 1993 | ||
| 15 | .Dt RAND48 3 | ||
| 16 | .Os | ||
| 17 | .Sh NAME | ||
| 18 | .Nm drand48 , | ||
| 19 | .Nm erand48 , | ||
| 20 | .Nm lrand48 , | ||
| 21 | .Nm nrand48 , | ||
| 22 | .Nm mrand48 , | ||
| 23 | .Nm jrand48 , | ||
| 24 | .Nm srand48 , | ||
| 25 | .Nm seed48 , | ||
| 26 | .Nm lcong48 | ||
| 27 | .Nd pseudo random number generators and initialization routines | ||
| 28 | .Sh SYNOPSIS | ||
| 29 | .Fd #include <stdlib.h> | ||
| 30 | .Ft double | ||
| 31 | .Fn drand48 void | ||
| 32 | .Ft double | ||
| 33 | .Fn erand48 "unsigned short xseed[3]" | ||
| 34 | .Ft long | ||
| 35 | .Fn lrand48 void | ||
| 36 | .Ft long | ||
| 37 | .Fn nrand48 "unsigned short xseed[3]" | ||
| 38 | .Ft long | ||
| 39 | .Fn mrand48 void | ||
| 40 | .Ft long | ||
| 41 | .Fn jrand48 "unsigned short xseed[3]" | ||
| 42 | .Ft void | ||
| 43 | .Fn srand48 "long seed" | ||
| 44 | .Ft "unsigned short *" | ||
| 45 | .Fn seed48 "unsigned short xseed[3]" | ||
| 46 | .Ft void | ||
| 47 | .Fn lcong48 "unsigned short p[7]" | ||
| 48 | .Sh DESCRIPTION | ||
| 49 | The | ||
| 50 | .Fn rand48 | ||
| 51 | family of functions generates pseudo-random numbers using a linear | ||
| 52 | congruential algorithm working on integers 48 bits in size. The | ||
| 53 | particular formula employed is | ||
| 54 | r(n+1) = (a * r(n) + c) mod m | ||
| 55 | where the default values are | ||
| 56 | for the multiplicand a = 0xfdeece66d = 25214903917 and | ||
| 57 | the addend c = 0xb = 11. The modulus is always fixed at m = 2 ** 48. | ||
| 58 | r(n) is called the seed of the random number generator. | ||
| 59 | .Pp | ||
| 60 | For all the six generator routines described next, the first | ||
| 61 | computational step is to perform a single iteration of the algorithm. | ||
| 62 | .Pp | ||
| 63 | .Fn drand48 | ||
| 64 | and | ||
| 65 | .Fn erand48 | ||
| 66 | return values of type double. The full 48 bits of r(n+1) are | ||
| 67 | loaded into the mantissa of the returned value, with the exponent set | ||
| 68 | such that the values produced lie in the interval [0.0, 1.0). | ||
| 69 | .Pp | ||
| 70 | .Fn lrand48 | ||
| 71 | and | ||
| 72 | .Fn nrand48 | ||
| 73 | return values of type long in the range | ||
| 74 | [0, 2**31-1]. The high-order (31) bits of | ||
| 75 | r(n+1) are loaded into the lower bits of the returned value, with | ||
| 76 | the topmost (sign) bit set to zero. | ||
| 77 | .Pp | ||
| 78 | .Fn mrand48 | ||
| 79 | and | ||
| 80 | .Fn jrand48 | ||
| 81 | return values of type long in the range | ||
| 82 | [-2**31, 2**31-1]. The high-order (32) bits of | ||
| 83 | r(n+1) are loaded into the returned value. | ||
| 84 | .Pp | ||
| 85 | .Fn drand48 , | ||
| 86 | .Fn lrand48 , | ||
| 87 | and | ||
| 88 | .Fn mrand48 | ||
| 89 | use an internal buffer to store r(n). For these functions | ||
| 90 | the initial value of r(0) = 0x1234abcd330e = 20017429951246. | ||
| 91 | .Pp | ||
| 92 | On the other hand, | ||
| 93 | .Fn erand48 , | ||
| 94 | .Fn nrand48 , | ||
| 95 | and | ||
| 96 | .Fn jrand48 | ||
| 97 | use a user-supplied buffer to store the seed r(n), | ||
| 98 | which consists of an array of 3 shorts, where the zeroth member | ||
| 99 | holds the least significant bits. | ||
| 100 | .Pp | ||
| 101 | All functions share the same multiplicand and addend. | ||
| 102 | .Pp | ||
| 103 | .Fn srand48 | ||
| 104 | is used to initialize the internal buffer r(n) of | ||
| 105 | .Fn drand48 , | ||
| 106 | .Fn lrand48 , | ||
| 107 | and | ||
| 108 | .Fn mrand48 | ||
| 109 | such that the 32 bits of the seed value are copied into the upper 32 bits | ||
| 110 | of r(n), with the lower 16 bits of r(n) arbitrarily being set to 0x330e. | ||
| 111 | Additionally, the constant multiplicand and addend of the algorithm are | ||
| 112 | reset to the default values given above. | ||
| 113 | .Pp | ||
| 114 | .Fn seed48 | ||
| 115 | also initializes the internal buffer r(n) of | ||
| 116 | .Fn drand48 , | ||
| 117 | .Fn lrand48 , | ||
| 118 | and | ||
| 119 | .Fn mrand48 , | ||
| 120 | but here all 48 bits of the seed can be specified in an array of 3 shorts, | ||
| 121 | where the zeroth member specifies the lowest bits. Again, | ||
| 122 | the constant multiplicand and addend of the algorithm are | ||
| 123 | reset to the default values given above. | ||
| 124 | .Fn seed48 | ||
| 125 | returns a pointer to an array of 3 shorts which contains the old seed. | ||
| 126 | This array is statically allocated, thus its contents are lost after | ||
| 127 | each new call to | ||
| 128 | .Fn seed48 . | ||
| 129 | .Pp | ||
| 130 | Finally, | ||
| 131 | .Fn lcong48 | ||
| 132 | allows full control over the multiplicand and addend used in | ||
| 133 | .Fn drand48 , | ||
| 134 | .Fn erand48 , | ||
| 135 | .Fn lrand48 , | ||
| 136 | .Fn nrand48 , | ||
| 137 | .Fn mrand48 , | ||
| 138 | and | ||
| 139 | .Fn jrand48 , | ||
| 140 | and the seed used in | ||
| 141 | .Fn drand48 , | ||
| 142 | .Fn lrand48 , | ||
| 143 | and | ||
| 144 | .Fn mrand48 . | ||
| 145 | An array of 7 shorts is passed as parameter; the first three shorts are | ||
| 146 | used to initialize the seed; the second three are used to initialize the | ||
| 147 | multiplicand; and the last short is used to initialize the addend. | ||
| 148 | It is thus not possible to use values greater than 0xffff as the addend. | ||
| 149 | .Pp | ||
| 150 | Note that all three methods of seeding the random number generator | ||
| 151 | always also set the multiplicand and addend for any of the six | ||
| 152 | generator calls. | ||
| 153 | .Pp | ||
| 154 | For a more powerful random number generator, see | ||
| 155 | .Xr random 3 | ||
| 156 | .Sh AUTHOR | ||
| 157 | Martin Birgmeier | ||
| 158 | .Sh SEE ALSO | ||
| 159 | .Xr rand 3 , | ||
| 160 | .Xr random 3 . | ||
diff --git a/src/lib/libc/stdlib/rand48.h b/src/lib/libc/stdlib/rand48.h new file mode 100644 index 0000000000..12496d1c8c --- /dev/null +++ b/src/lib/libc/stdlib/rand48.h | |||
| @@ -0,0 +1,30 @@ | |||
| 1 | /* | ||
| 2 | * Copyright (c) 1993 Martin Birgmeier | ||
| 3 | * All rights reserved. | ||
| 4 | * | ||
| 5 | * You may redistribute unmodified or modified versions of this source | ||
| 6 | * code provided that the above copyright notice and this and the | ||
| 7 | * following conditions are retained. | ||
| 8 | * | ||
| 9 | * This software is provided ``as is'', and comes with no warranties | ||
| 10 | * of any kind. I shall in no event be liable for anything that happens | ||
| 11 | * to anyone/anything when using this software. | ||
| 12 | */ | ||
| 13 | |||
| 14 | #ifndef _RAND48_H_ | ||
| 15 | #define _RAND48_H_ | ||
| 16 | |||
| 17 | #include <math.h> | ||
| 18 | #include <stdlib.h> | ||
| 19 | |||
| 20 | void __dorand48 __P((unsigned short[3])); | ||
| 21 | |||
| 22 | #define RAND48_SEED_0 (0x330e) | ||
| 23 | #define RAND48_SEED_1 (0xabcd) | ||
| 24 | #define RAND48_SEED_2 (0x1234) | ||
| 25 | #define RAND48_MULT_0 (0xe66d) | ||
| 26 | #define RAND48_MULT_1 (0xdeec) | ||
| 27 | #define RAND48_MULT_2 (0x0005) | ||
| 28 | #define RAND48_ADD (0x000b) | ||
| 29 | |||
| 30 | #endif /* _RAND48_H_ */ | ||
diff --git a/src/lib/libc/stdlib/random.3 b/src/lib/libc/stdlib/random.3 new file mode 100644 index 0000000000..38c15a9803 --- /dev/null +++ b/src/lib/libc/stdlib/random.3 | |||
| @@ -0,0 +1,167 @@ | |||
| 1 | .\" Copyright (c) 1983, 1991 The Regents of the University of California. | ||
| 2 | .\" All rights reserved. | ||
| 3 | .\" | ||
| 4 | .\" Redistribution and use in source and binary forms, with or without | ||
| 5 | .\" modification, are permitted provided that the following conditions | ||
| 6 | .\" are met: | ||
| 7 | .\" 1. Redistributions of source code must retain the above copyright | ||
| 8 | .\" notice, this list of conditions and the following disclaimer. | ||
| 9 | .\" 2. Redistributions in binary form must reproduce the above copyright | ||
| 10 | .\" notice, this list of conditions and the following disclaimer in the | ||
| 11 | .\" documentation and/or other materials provided with the distribution. | ||
| 12 | .\" 3. All advertising materials mentioning features or use of this software | ||
| 13 | .\" must display the following acknowledgement: | ||
| 14 | .\" This product includes software developed by the University of | ||
| 15 | .\" California, Berkeley and its contributors. | ||
| 16 | .\" 4. Neither the name of the University nor the names of its contributors | ||
| 17 | .\" may be used to endorse or promote products derived from this software | ||
| 18 | .\" without specific prior written permission. | ||
| 19 | .\" | ||
| 20 | .\" THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND | ||
| 21 | .\" ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE | ||
| 22 | .\" IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE | ||
| 23 | .\" ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE | ||
| 24 | .\" FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL | ||
| 25 | .\" DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS | ||
| 26 | .\" OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) | ||
| 27 | .\" HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT | ||
| 28 | .\" LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY | ||
| 29 | .\" OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF | ||
| 30 | .\" SUCH DAMAGE. | ||
| 31 | .\" | ||
| 32 | .\" from: @(#)random.3 6.5 (Berkeley) 4/19/91 | ||
| 33 | .\" $Id: random.3,v 1.1.1.1 1995/10/18 08:42:19 deraadt Exp $ | ||
| 34 | .\" | ||
| 35 | .Dd April 19, 1991 | ||
| 36 | .Dt RANDOM 3 | ||
| 37 | .Os BSD 4.2 | ||
| 38 | .Sh NAME | ||
| 39 | .Nm random , | ||
| 40 | .Nm srandom , | ||
| 41 | .Nm initstate , | ||
| 42 | .Nm setstate | ||
| 43 | .Nd better random number generator; routines for changing generators | ||
| 44 | .Sh SYNOPSIS | ||
| 45 | .Fd #include <stdlib.h> | ||
| 46 | .Ft long | ||
| 47 | .Fn random void | ||
| 48 | .Ft void | ||
| 49 | .Fn srandom "unsigned seed" | ||
| 50 | .Ft char * | ||
| 51 | .Fn initstate "unsigned seed" "char *state" "int n" | ||
| 52 | .Ft char * | ||
| 53 | .Fn setstate "char *state" | ||
| 54 | .Sh DESCRIPTION | ||
| 55 | The | ||
| 56 | .Fn random | ||
| 57 | function | ||
| 58 | uses a non-linear additive feedback random number generator employing a | ||
| 59 | default table of size 31 long integers to return successive pseudo-random | ||
| 60 | numbers in the range from 0 to | ||
| 61 | .if t 2\u\s731\s10\d\(mi1. | ||
| 62 | .if n (2**31)\(mi1. | ||
| 63 | The period of this random number generator is very large, approximately | ||
| 64 | .if t 16\(mu(2\u\s731\s10\d\(mi1). | ||
| 65 | .if n 16*((2**31)\(mi1). | ||
| 66 | .Pp | ||
| 67 | The | ||
| 68 | .Fn random Ns / Fn srandom | ||
| 69 | have (almost) the same calling sequence and initialization properties as | ||
| 70 | .Xr rand 3 Ns / Xr srand 3 . | ||
| 71 | The difference is that | ||
| 72 | .Xr rand | ||
| 73 | produces a much less random sequence \(em in fact, the low dozen bits | ||
| 74 | generated by rand go through a cyclic pattern. All the bits generated by | ||
| 75 | .Fn random | ||
| 76 | are usable. For example, | ||
| 77 | .Sq Li random()&01 | ||
| 78 | will produce a random binary | ||
| 79 | value. | ||
| 80 | .Pp | ||
| 81 | Unlike | ||
| 82 | .Xr srand , | ||
| 83 | .Fn srandom | ||
| 84 | does not return the old seed; the reason for this is that the amount of | ||
| 85 | state information used is much more than a single word. (Two other | ||
| 86 | routines are provided to deal with restarting/changing random | ||
| 87 | number generators). Like | ||
| 88 | .Xr rand 3 , | ||
| 89 | however, | ||
| 90 | .Fn random | ||
| 91 | will by default produce a sequence of numbers that can be duplicated | ||
| 92 | by calling | ||
| 93 | .Fn srandom | ||
| 94 | with | ||
| 95 | .Ql 1 | ||
| 96 | as the seed. | ||
| 97 | .Pp | ||
| 98 | The | ||
| 99 | .Fn initstate | ||
| 100 | routine allows a state array, passed in as an argument, to be initialized | ||
| 101 | for future use. The size of the state array (in bytes) is used by | ||
| 102 | .Fn initstate | ||
| 103 | to decide how sophisticated a random number generator it should use \(em the | ||
| 104 | more state, the better the random numbers will be. | ||
| 105 | (Current "optimal" values for the amount of state information are | ||
| 106 | 8, 32, 64, 128, and 256 bytes; other amounts will be rounded down to | ||
| 107 | the nearest known amount. Using less than 8 bytes will cause an error.) | ||
| 108 | The seed for the initialization (which specifies a starting point for | ||
| 109 | the random number sequence, and provides for restarting at the same | ||
| 110 | point) is also an argument. | ||
| 111 | The | ||
| 112 | .Fn initstate | ||
| 113 | function | ||
| 114 | returns a pointer to the previous state information array. | ||
| 115 | .Pp | ||
| 116 | Once a state has been initialized, the | ||
| 117 | .Fn setstate | ||
| 118 | routine provides for rapid switching between states. | ||
| 119 | The | ||
| 120 | .Fn setstate | ||
| 121 | function | ||
| 122 | returns a pointer to the previous state array; its | ||
| 123 | argument state array is used for further random number generation | ||
| 124 | until the next call to | ||
| 125 | .Fn initstate | ||
| 126 | or | ||
| 127 | .Fn setstate . | ||
| 128 | .Pp | ||
| 129 | Once a state array has been initialized, it may be restarted at a | ||
| 130 | different point either by calling | ||
| 131 | .Fn initstate | ||
| 132 | (with the desired seed, the state array, and its size) or by calling | ||
| 133 | both | ||
| 134 | .Fn setstate | ||
| 135 | (with the state array) and | ||
| 136 | .Fn srandom | ||
| 137 | (with the desired seed). | ||
| 138 | The advantage of calling both | ||
| 139 | .Fn setstate | ||
| 140 | and | ||
| 141 | .Fn srandom | ||
| 142 | is that the size of the state array does not have to be remembered after | ||
| 143 | it is initialized. | ||
| 144 | .Pp | ||
| 145 | With 256 bytes of state information, the period of the random number | ||
| 146 | generator is greater than | ||
| 147 | .if t 2\u\s769\s10\d, | ||
| 148 | .if n 2**69 | ||
| 149 | which should be sufficient for most purposes. | ||
| 150 | .Sh AUTHOR | ||
| 151 | Earl T. Cohen | ||
| 152 | .Sh DIAGNOSTICS | ||
| 153 | If | ||
| 154 | .Fn initstate | ||
| 155 | is called with less than 8 bytes of state information, or if | ||
| 156 | .Fn setstate | ||
| 157 | detects that the state information has been garbled, error | ||
| 158 | messages are printed on the standard error output. | ||
| 159 | .Sh SEE ALSO | ||
| 160 | .Xr rand 3 | ||
| 161 | .Sh HISTORY | ||
| 162 | These | ||
| 163 | functions appeared in | ||
| 164 | .Bx 4.2 . | ||
| 165 | .Sh BUGS | ||
| 166 | About 2/3 the speed of | ||
| 167 | .Xr rand 3 . | ||
diff --git a/src/lib/libc/stdlib/random.c b/src/lib/libc/stdlib/random.c new file mode 100644 index 0000000000..469b6d976a --- /dev/null +++ b/src/lib/libc/stdlib/random.c | |||
| @@ -0,0 +1,364 @@ | |||
| 1 | /* | ||
| 2 | * Copyright (c) 1983 Regents of the University of California. | ||
| 3 | * All rights reserved. | ||
| 4 | * | ||
| 5 | * Redistribution and use in source and binary forms, with or without | ||
| 6 | * modification, are permitted provided that the following conditions | ||
| 7 | * are met: | ||
| 8 | * 1. Redistributions of source code must retain the above copyright | ||
| 9 | * notice, this list of conditions and the following disclaimer. | ||
| 10 | * 2. Redistributions in binary form must reproduce the above copyright | ||
| 11 | * notice, this list of conditions and the following disclaimer in the | ||
| 12 | * documentation and/or other materials provided with the distribution. | ||
| 13 | * 3. All advertising materials mentioning features or use of this software | ||
| 14 | * must display the following acknowledgement: | ||
| 15 | * This product includes software developed by the University of | ||
| 16 | * California, Berkeley and its contributors. | ||
| 17 | * 4. Neither the name of the University nor the names of its contributors | ||
| 18 | * may be used to endorse or promote products derived from this software | ||
| 19 | * without specific prior written permission. | ||
| 20 | * | ||
| 21 | * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND | ||
| 22 | * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE | ||
| 23 | * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE | ||
| 24 | * ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE | ||
| 25 | * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL | ||
| 26 | * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS | ||
| 27 | * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) | ||
| 28 | * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT | ||
| 29 | * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY | ||
| 30 | * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF | ||
| 31 | * SUCH DAMAGE. | ||
| 32 | */ | ||
| 33 | |||
| 34 | #if defined(LIBC_SCCS) && !defined(lint) | ||
| 35 | /*static char *sccsid = "from: @(#)random.c 5.9 (Berkeley) 2/23/91";*/ | ||
| 36 | static char *rcsid = "$Id: random.c,v 1.1.1.1 1995/10/18 08:42:19 deraadt Exp $"; | ||
| 37 | #endif /* LIBC_SCCS and not lint */ | ||
| 38 | |||
| 39 | #include <stdio.h> | ||
| 40 | #include <stdlib.h> | ||
| 41 | |||
| 42 | /* | ||
| 43 | * random.c: | ||
| 44 | * | ||
| 45 | * An improved random number generation package. In addition to the standard | ||
| 46 | * rand()/srand() like interface, this package also has a special state info | ||
| 47 | * interface. The initstate() routine is called with a seed, an array of | ||
| 48 | * bytes, and a count of how many bytes are being passed in; this array is | ||
| 49 | * then initialized to contain information for random number generation with | ||
| 50 | * that much state information. Good sizes for the amount of state | ||
| 51 | * information are 32, 64, 128, and 256 bytes. The state can be switched by | ||
| 52 | * calling the setstate() routine with the same array as was initiallized | ||
| 53 | * with initstate(). By default, the package runs with 128 bytes of state | ||
| 54 | * information and generates far better random numbers than a linear | ||
| 55 | * congruential generator. If the amount of state information is less than | ||
| 56 | * 32 bytes, a simple linear congruential R.N.G. is used. | ||
| 57 | * | ||
| 58 | * Internally, the state information is treated as an array of longs; the | ||
| 59 | * zeroeth element of the array is the type of R.N.G. being used (small | ||
| 60 | * integer); the remainder of the array is the state information for the | ||
| 61 | * R.N.G. Thus, 32 bytes of state information will give 7 longs worth of | ||
| 62 | * state information, which will allow a degree seven polynomial. (Note: | ||
| 63 | * the zeroeth word of state information also has some other information | ||
| 64 | * stored in it -- see setstate() for details). | ||
| 65 | * | ||
| 66 | * The random number generation technique is a linear feedback shift register | ||
| 67 | * approach, employing trinomials (since there are fewer terms to sum up that | ||
| 68 | * way). In this approach, the least significant bit of all the numbers in | ||
| 69 | * the state table will act as a linear feedback shift register, and will | ||
| 70 | * have period 2^deg - 1 (where deg is the degree of the polynomial being | ||
| 71 | * used, assuming that the polynomial is irreducible and primitive). The | ||
| 72 | * higher order bits will have longer periods, since their values are also | ||
| 73 | * influenced by pseudo-random carries out of the lower bits. The total | ||
| 74 | * period of the generator is approximately deg*(2**deg - 1); thus doubling | ||
| 75 | * the amount of state information has a vast influence on the period of the | ||
| 76 | * generator. Note: the deg*(2**deg - 1) is an approximation only good for | ||
| 77 | * large deg, when the period of the shift register is the dominant factor. | ||
| 78 | * With deg equal to seven, the period is actually much longer than the | ||
| 79 | * 7*(2**7 - 1) predicted by this formula. | ||
| 80 | */ | ||
| 81 | |||
| 82 | /* | ||
| 83 | * For each of the currently supported random number generators, we have a | ||
| 84 | * break value on the amount of state information (you need at least this | ||
| 85 | * many bytes of state info to support this random number generator), a degree | ||
| 86 | * for the polynomial (actually a trinomial) that the R.N.G. is based on, and | ||
| 87 | * the separation between the two lower order coefficients of the trinomial. | ||
| 88 | */ | ||
| 89 | #define TYPE_0 0 /* linear congruential */ | ||
| 90 | #define BREAK_0 8 | ||
| 91 | #define DEG_0 0 | ||
| 92 | #define SEP_0 0 | ||
| 93 | |||
| 94 | #define TYPE_1 1 /* x**7 + x**3 + 1 */ | ||
| 95 | #define BREAK_1 32 | ||
| 96 | #define DEG_1 7 | ||
| 97 | #define SEP_1 3 | ||
| 98 | |||
| 99 | #define TYPE_2 2 /* x**15 + x + 1 */ | ||
| 100 | #define BREAK_2 64 | ||
| 101 | #define DEG_2 15 | ||
| 102 | #define SEP_2 1 | ||
| 103 | |||
| 104 | #define TYPE_3 3 /* x**31 + x**3 + 1 */ | ||
| 105 | #define BREAK_3 128 | ||
| 106 | #define DEG_3 31 | ||
| 107 | #define SEP_3 3 | ||
| 108 | |||
| 109 | #define TYPE_4 4 /* x**63 + x + 1 */ | ||
| 110 | #define BREAK_4 256 | ||
| 111 | #define DEG_4 63 | ||
| 112 | #define SEP_4 1 | ||
| 113 | |||
| 114 | /* | ||
| 115 | * Array versions of the above information to make code run faster -- | ||
| 116 | * relies on fact that TYPE_i == i. | ||
| 117 | */ | ||
| 118 | #define MAX_TYPES 5 /* max number of types above */ | ||
| 119 | |||
| 120 | static int degrees[MAX_TYPES] = { DEG_0, DEG_1, DEG_2, DEG_3, DEG_4 }; | ||
| 121 | static int seps [MAX_TYPES] = { SEP_0, SEP_1, SEP_2, SEP_3, SEP_4 }; | ||
| 122 | |||
| 123 | /* | ||
| 124 | * Initially, everything is set up as if from: | ||
| 125 | * | ||
| 126 | * initstate(1, &randtbl, 128); | ||
| 127 | * | ||
| 128 | * Note that this initialization takes advantage of the fact that srandom() | ||
| 129 | * advances the front and rear pointers 10*rand_deg times, and hence the | ||
| 130 | * rear pointer which starts at 0 will also end up at zero; thus the zeroeth | ||
| 131 | * element of the state information, which contains info about the current | ||
| 132 | * position of the rear pointer is just | ||
| 133 | * | ||
| 134 | * MAX_TYPES * (rptr - state) + TYPE_3 == TYPE_3. | ||
| 135 | */ | ||
| 136 | |||
| 137 | static long randtbl[DEG_3 + 1] = { | ||
| 138 | TYPE_3, | ||
| 139 | 0x9a319039, 0x32d9c024, 0x9b663182, 0x5da1f342, 0xde3b81e0, 0xdf0a6fb5, | ||
| 140 | 0xf103bc02, 0x48f340fb, 0x7449e56b, 0xbeb1dbb0, 0xab5c5918, 0x946554fd, | ||
| 141 | 0x8c2e680f, 0xeb3d799f, 0xb11ee0b7, 0x2d436b86, 0xda672e2a, 0x1588ca88, | ||
| 142 | 0xe369735d, 0x904f35f7, 0xd7158fd6, 0x6fa6f051, 0x616e6b96, 0xac94efdc, | ||
| 143 | 0x36413f93, 0xc622c298, 0xf5a42ab8, 0x8a88d77b, 0xf5ad9d0e, 0x8999220b, | ||
| 144 | 0x27fb47b9, | ||
| 145 | }; | ||
| 146 | |||
| 147 | /* | ||
| 148 | * fptr and rptr are two pointers into the state info, a front and a rear | ||
| 149 | * pointer. These two pointers are always rand_sep places aparts, as they | ||
| 150 | * cycle cyclically through the state information. (Yes, this does mean we | ||
| 151 | * could get away with just one pointer, but the code for random() is more | ||
| 152 | * efficient this way). The pointers are left positioned as they would be | ||
| 153 | * from the call | ||
| 154 | * | ||
| 155 | * initstate(1, randtbl, 128); | ||
| 156 | * | ||
| 157 | * (The position of the rear pointer, rptr, is really 0 (as explained above | ||
| 158 | * in the initialization of randtbl) because the state table pointer is set | ||
| 159 | * to point to randtbl[1] (as explained below). | ||
| 160 | */ | ||
| 161 | static long *fptr = &randtbl[SEP_3 + 1]; | ||
| 162 | static long *rptr = &randtbl[1]; | ||
| 163 | |||
| 164 | /* | ||
| 165 | * The following things are the pointer to the state information table, the | ||
| 166 | * type of the current generator, the degree of the current polynomial being | ||
| 167 | * used, and the separation between the two pointers. Note that for efficiency | ||
| 168 | * of random(), we remember the first location of the state information, not | ||
| 169 | * the zeroeth. Hence it is valid to access state[-1], which is used to | ||
| 170 | * store the type of the R.N.G. Also, we remember the last location, since | ||
| 171 | * this is more efficient than indexing every time to find the address of | ||
| 172 | * the last element to see if the front and rear pointers have wrapped. | ||
| 173 | */ | ||
| 174 | static long *state = &randtbl[1]; | ||
| 175 | static int rand_type = TYPE_3; | ||
| 176 | static int rand_deg = DEG_3; | ||
| 177 | static int rand_sep = SEP_3; | ||
| 178 | static long *end_ptr = &randtbl[DEG_3 + 1]; | ||
| 179 | |||
| 180 | /* | ||
| 181 | * srandom: | ||
| 182 | * | ||
| 183 | * Initialize the random number generator based on the given seed. If the | ||
| 184 | * type is the trivial no-state-information type, just remember the seed. | ||
| 185 | * Otherwise, initializes state[] based on the given "seed" via a linear | ||
| 186 | * congruential generator. Then, the pointers are set to known locations | ||
| 187 | * that are exactly rand_sep places apart. Lastly, it cycles the state | ||
| 188 | * information a given number of times to get rid of any initial dependencies | ||
| 189 | * introduced by the L.C.R.N.G. Note that the initialization of randtbl[] | ||
| 190 | * for default usage relies on values produced by this routine. | ||
| 191 | */ | ||
| 192 | void | ||
| 193 | srandom(x) | ||
| 194 | u_int x; | ||
| 195 | { | ||
| 196 | register int i, j; | ||
| 197 | |||
| 198 | if (rand_type == TYPE_0) | ||
| 199 | state[0] = x; | ||
| 200 | else { | ||
| 201 | j = 1; | ||
| 202 | state[0] = x; | ||
| 203 | for (i = 1; i < rand_deg; i++) | ||
| 204 | state[i] = 1103515245 * state[i - 1] + 12345; | ||
| 205 | fptr = &state[rand_sep]; | ||
| 206 | rptr = &state[0]; | ||
| 207 | for (i = 0; i < 10 * rand_deg; i++) | ||
| 208 | (void)random(); | ||
| 209 | } | ||
| 210 | } | ||
| 211 | |||
| 212 | /* | ||
| 213 | * initstate: | ||
| 214 | * | ||
| 215 | * Initialize the state information in the given array of n bytes for future | ||
| 216 | * random number generation. Based on the number of bytes we are given, and | ||
| 217 | * the break values for the different R.N.G.'s, we choose the best (largest) | ||
| 218 | * one we can and set things up for it. srandom() is then called to | ||
| 219 | * initialize the state information. | ||
| 220 | * | ||
| 221 | * Note that on return from srandom(), we set state[-1] to be the type | ||
| 222 | * multiplexed with the current value of the rear pointer; this is so | ||
| 223 | * successive calls to initstate() won't lose this information and will be | ||
| 224 | * able to restart with setstate(). | ||
| 225 | * | ||
| 226 | * Note: the first thing we do is save the current state, if any, just like | ||
| 227 | * setstate() so that it doesn't matter when initstate is called. | ||
| 228 | * | ||
| 229 | * Returns a pointer to the old state. | ||
| 230 | */ | ||
| 231 | char * | ||
| 232 | initstate(seed, arg_state, n) | ||
| 233 | u_int seed; /* seed for R.N.G. */ | ||
| 234 | char *arg_state; /* pointer to state array */ | ||
| 235 | int n; /* # bytes of state info */ | ||
| 236 | { | ||
| 237 | register char *ostate = (char *)(&state[-1]); | ||
| 238 | |||
| 239 | if (rand_type == TYPE_0) | ||
| 240 | state[-1] = rand_type; | ||
| 241 | else | ||
| 242 | state[-1] = MAX_TYPES * (rptr - state) + rand_type; | ||
| 243 | if (n < BREAK_0) { | ||
| 244 | (void)fprintf(stderr, | ||
| 245 | "random: not enough state (%d bytes); ignored.\n", n); | ||
| 246 | return(0); | ||
| 247 | } | ||
| 248 | if (n < BREAK_1) { | ||
| 249 | rand_type = TYPE_0; | ||
| 250 | rand_deg = DEG_0; | ||
| 251 | rand_sep = SEP_0; | ||
| 252 | } else if (n < BREAK_2) { | ||
| 253 | rand_type = TYPE_1; | ||
| 254 | rand_deg = DEG_1; | ||
| 255 | rand_sep = SEP_1; | ||
| 256 | } else if (n < BREAK_3) { | ||
| 257 | rand_type = TYPE_2; | ||
| 258 | rand_deg = DEG_2; | ||
| 259 | rand_sep = SEP_2; | ||
| 260 | } else if (n < BREAK_4) { | ||
| 261 | rand_type = TYPE_3; | ||
| 262 | rand_deg = DEG_3; | ||
| 263 | rand_sep = SEP_3; | ||
| 264 | } else { | ||
| 265 | rand_type = TYPE_4; | ||
| 266 | rand_deg = DEG_4; | ||
| 267 | rand_sep = SEP_4; | ||
| 268 | } | ||
| 269 | state = &(((long *)arg_state)[1]); /* first location */ | ||
| 270 | end_ptr = &state[rand_deg]; /* must set end_ptr before srandom */ | ||
| 271 | srandom(seed); | ||
| 272 | if (rand_type == TYPE_0) | ||
| 273 | state[-1] = rand_type; | ||
| 274 | else | ||
| 275 | state[-1] = MAX_TYPES*(rptr - state) + rand_type; | ||
| 276 | return(ostate); | ||
| 277 | } | ||
| 278 | |||
| 279 | /* | ||
| 280 | * setstate: | ||
| 281 | * | ||
| 282 | * Restore the state from the given state array. | ||
| 283 | * | ||
| 284 | * Note: it is important that we also remember the locations of the pointers | ||
| 285 | * in the current state information, and restore the locations of the pointers | ||
| 286 | * from the old state information. This is done by multiplexing the pointer | ||
| 287 | * location into the zeroeth word of the state information. | ||
| 288 | * | ||
| 289 | * Note that due to the order in which things are done, it is OK to call | ||
| 290 | * setstate() with the same state as the current state. | ||
| 291 | * | ||
| 292 | * Returns a pointer to the old state information. | ||
| 293 | */ | ||
| 294 | char * | ||
| 295 | setstate(arg_state) | ||
| 296 | char *arg_state; | ||
| 297 | { | ||
| 298 | register long *new_state = (long *)arg_state; | ||
| 299 | register int type = new_state[0] % MAX_TYPES; | ||
| 300 | register int rear = new_state[0] / MAX_TYPES; | ||
| 301 | char *ostate = (char *)(&state[-1]); | ||
| 302 | |||
| 303 | if (rand_type == TYPE_0) | ||
| 304 | state[-1] = rand_type; | ||
| 305 | else | ||
| 306 | state[-1] = MAX_TYPES * (rptr - state) + rand_type; | ||
| 307 | switch(type) { | ||
| 308 | case TYPE_0: | ||
| 309 | case TYPE_1: | ||
| 310 | case TYPE_2: | ||
| 311 | case TYPE_3: | ||
| 312 | case TYPE_4: | ||
| 313 | rand_type = type; | ||
| 314 | rand_deg = degrees[type]; | ||
| 315 | rand_sep = seps[type]; | ||
| 316 | break; | ||
| 317 | default: | ||
| 318 | (void)fprintf(stderr, | ||
| 319 | "random: state info corrupted; not changed.\n"); | ||
| 320 | } | ||
| 321 | state = &new_state[1]; | ||
| 322 | if (rand_type != TYPE_0) { | ||
| 323 | rptr = &state[rear]; | ||
| 324 | fptr = &state[(rear + rand_sep) % rand_deg]; | ||
| 325 | } | ||
| 326 | end_ptr = &state[rand_deg]; /* set end_ptr too */ | ||
| 327 | return(ostate); | ||
| 328 | } | ||
| 329 | |||
| 330 | /* | ||
| 331 | * random: | ||
| 332 | * | ||
| 333 | * If we are using the trivial TYPE_0 R.N.G., just do the old linear | ||
| 334 | * congruential bit. Otherwise, we do our fancy trinomial stuff, which is | ||
| 335 | * the same in all the other cases due to all the global variables that have | ||
| 336 | * been set up. The basic operation is to add the number at the rear pointer | ||
| 337 | * into the one at the front pointer. Then both pointers are advanced to | ||
| 338 | * the next location cyclically in the table. The value returned is the sum | ||
| 339 | * generated, reduced to 31 bits by throwing away the "least random" low bit. | ||
| 340 | * | ||
| 341 | * Note: the code takes advantage of the fact that both the front and | ||
| 342 | * rear pointers can't wrap on the same call by not testing the rear | ||
| 343 | * pointer if the front one has wrapped. | ||
| 344 | * | ||
| 345 | * Returns a 31-bit random number. | ||
| 346 | */ | ||
| 347 | long | ||
| 348 | random() | ||
| 349 | { | ||
| 350 | long i; | ||
| 351 | |||
| 352 | if (rand_type == TYPE_0) | ||
| 353 | i = state[0] = (state[0] * 1103515245 + 12345) & 0x7fffffff; | ||
| 354 | else { | ||
| 355 | *fptr += *rptr; | ||
| 356 | i = (*fptr >> 1) & 0x7fffffff; /* chucking least random bit */ | ||
| 357 | if (++fptr >= end_ptr) { | ||
| 358 | fptr = state; | ||
| 359 | ++rptr; | ||
| 360 | } else if (++rptr >= end_ptr) | ||
| 361 | rptr = state; | ||
| 362 | } | ||
| 363 | return(i); | ||
| 364 | } | ||
diff --git a/src/lib/libc/stdlib/realloc.3 b/src/lib/libc/stdlib/realloc.3 new file mode 100644 index 0000000000..66f09b2081 --- /dev/null +++ b/src/lib/libc/stdlib/realloc.3 | |||
| @@ -0,0 +1,100 @@ | |||
| 1 | .\" Copyright (c) 1991 The Regents of the University of California. | ||
| 2 | .\" All rights reserved. | ||
| 3 | .\" | ||
| 4 | .\" Redistribution and use in source and binary forms, with or without | ||
| 5 | .\" modification, are permitted provided that the following conditions | ||
| 6 | .\" are met: | ||
| 7 | .\" 1. Redistributions of source code must retain the above copyright | ||
| 8 | .\" notice, this list of conditions and the following disclaimer. | ||
| 9 | .\" 2. Redistributions in binary form must reproduce the above copyright | ||
| 10 | .\" notice, this list of conditions and the following disclaimer in the | ||
| 11 | .\" documentation and/or other materials provided with the distribution. | ||
| 12 | .\" 3. All advertising materials mentioning features or use of this software | ||
| 13 | .\" must display the following acknowledgement: | ||
| 14 | .\" This product includes software developed by the University of | ||
| 15 | .\" California, Berkeley and its contributors. | ||
| 16 | .\" 4. Neither the name of the University nor the names of its contributors | ||
| 17 | .\" may be used to endorse or promote products derived from this software | ||
| 18 | .\" without specific prior written permission. | ||
| 19 | .\" | ||
| 20 | .\" THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND | ||
| 21 | .\" ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE | ||
| 22 | .\" IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE | ||
| 23 | .\" ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE | ||
| 24 | .\" FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL | ||
| 25 | .\" DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS | ||
| 26 | .\" OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) | ||
| 27 | .\" HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT | ||
| 28 | .\" LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY | ||
| 29 | .\" OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF | ||
| 30 | .\" SUCH DAMAGE. | ||
| 31 | .\" | ||
| 32 | .\" from: @(#)realloc.3 5.1 (Berkeley) 5/2/91 | ||
| 33 | .\" $Id: realloc.3,v 1.1.1.1 1995/10/18 08:42:19 deraadt Exp $ | ||
| 34 | .\" | ||
| 35 | .Dd May 2, 1991 | ||
| 36 | .Dt REALLOC 3 | ||
| 37 | .Os | ||
| 38 | .Sh NAME | ||
| 39 | .Nm realloc | ||
| 40 | .Nd reallocation of memory function | ||
| 41 | .Sh SYNOPSIS | ||
| 42 | .Fd #include <stdlib.h> | ||
| 43 | .Ft void * | ||
| 44 | .Fn realloc "void *ptr" "size_t size" | ||
| 45 | .Sh DESCRIPTION | ||
| 46 | The | ||
| 47 | .Fn realloc | ||
| 48 | function changes the size of the object pointed to by | ||
| 49 | .Fa ptr | ||
| 50 | to the size specified by | ||
| 51 | .Fa size . | ||
| 52 | The contents of the object are unchanged up to the lesser | ||
| 53 | of the new and old sizes. | ||
| 54 | If the new size is larger, the value of the newly allocated portion | ||
| 55 | of the object is indeterminate. | ||
| 56 | If | ||
| 57 | .Fa ptr | ||
| 58 | is a null pointer, the | ||
| 59 | .Fn realloc | ||
| 60 | function behaves like the | ||
| 61 | .Xr malloc 3 | ||
| 62 | function for the specified size. | ||
| 63 | Otherwise, if | ||
| 64 | .Fa ptr | ||
| 65 | does not match a pointer earlier returned by the | ||
| 66 | .Xr calloc 3 , | ||
| 67 | .Xr malloc 3 , | ||
| 68 | or | ||
| 69 | .Fn realloc | ||
| 70 | function, or if the space has been deallocated | ||
| 71 | by a call to the | ||
| 72 | .Xr free | ||
| 73 | or | ||
| 74 | .Fn realloc | ||
| 75 | function, unpredictable and usually detrimental | ||
| 76 | behavior will occur. | ||
| 77 | If the space cannot be allocated, the object | ||
| 78 | pointed to by | ||
| 79 | .Fa ptr | ||
| 80 | is unchanged. | ||
| 81 | If | ||
| 82 | .Fa size | ||
| 83 | is zero and | ||
| 84 | .Fa ptr | ||
| 85 | is not a null pointer, the object it points to is freed. | ||
| 86 | .Pp | ||
| 87 | The | ||
| 88 | .Fn realloc | ||
| 89 | function returns either a null pointer or a pointer | ||
| 90 | to the possibly moved allocated space. | ||
| 91 | .Sh SEE ALSO | ||
| 92 | .Xr alloca 3 , | ||
| 93 | .Xr calloc 3 , | ||
| 94 | .Xr free 3 , | ||
| 95 | .Xr malloc 3 , | ||
| 96 | .Sh STANDARDS | ||
| 97 | The | ||
| 98 | .Fn realloc | ||
| 99 | function conforms to | ||
| 100 | .St -ansiC . | ||
diff --git a/src/lib/libc/stdlib/realpath.3 b/src/lib/libc/stdlib/realpath.3 new file mode 100644 index 0000000000..9d8b1ff2ce --- /dev/null +++ b/src/lib/libc/stdlib/realpath.3 | |||
| @@ -0,0 +1,126 @@ | |||
| 1 | .\" Copyright (c) 1994 | ||
| 2 | .\" The Regents of the University of California. All rights reserved. | ||
| 3 | .\" | ||
| 4 | .\" This code is derived from software contributed to Berkeley by | ||
| 5 | .\" Jan-Simon Pendry. | ||
| 6 | .\" | ||
| 7 | .\" Redistribution and use in source and binary forms, with or without | ||
| 8 | .\" modification, are permitted provided that the following conditions | ||
| 9 | .\" are met: | ||
| 10 | .\" 1. Redistributions of source code must retain the above copyright | ||
| 11 | .\" notice, this list of conditions and the following disclaimer. | ||
| 12 | .\" 2. Redistributions in binary form must reproduce the above copyright | ||
| 13 | .\" notice, this list of conditions and the following disclaimer in the | ||
| 14 | .\" documentation and/or other materials provided with the distribution. | ||
| 15 | .\" 3. All advertising materials mentioning features or use of this software | ||
| 16 | .\" must display the following acknowledgement: | ||
| 17 | .\" This product includes software developed by the University of | ||
| 18 | .\" California, Berkeley and its contributors. | ||
| 19 | .\" 4. Neither the name of the University nor the names of its contributors | ||
| 20 | .\" may be used to endorse or promote products derived from this software | ||
| 21 | .\" without specific prior written permission. | ||
| 22 | .\" | ||
| 23 | .\" THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND | ||
| 24 | .\" ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE | ||
| 25 | .\" IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE | ||
| 26 | .\" ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE | ||
| 27 | .\" FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL | ||
| 28 | .\" DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS | ||
| 29 | .\" OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) | ||
| 30 | .\" HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT | ||
| 31 | .\" LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY | ||
| 32 | .\" OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF | ||
| 33 | .\" SUCH DAMAGE. | ||
| 34 | .\" | ||
| 35 | .\" from: @(#)realpath.3 8.2 (Berkeley) 2/16/94 | ||
| 36 | .\" $Id: realpath.3,v 1.1.1.1 1995/10/18 08:42:19 deraadt Exp $ | ||
| 37 | .\" | ||
| 38 | .Dd "February 16, 1994" | ||
| 39 | .Dt REALPATH 3 | ||
| 40 | .Os | ||
| 41 | .Sh NAME | ||
| 42 | .Nm realpath | ||
| 43 | .Nd returns the canonicalized absolute pathname | ||
| 44 | .Sh SYNOPSIS | ||
| 45 | .Fd #include <sys/param.h> | ||
| 46 | .Fd #include <stdlib.h> | ||
| 47 | .Ft "char *" | ||
| 48 | .Fn realpath "const char *pathname" "char resolvedname[MAXPATHLEN]" | ||
| 49 | .Sh DESCRIPTION | ||
| 50 | The | ||
| 51 | .Fn realpath | ||
| 52 | function resolves all symbolic links, extra | ||
| 53 | .Dq / | ||
| 54 | characters and references to | ||
| 55 | .Pa /./ | ||
| 56 | and | ||
| 57 | .Pa /../ | ||
| 58 | in | ||
| 59 | .Fa pathname , | ||
| 60 | and copies the resulting absolute pathname into | ||
| 61 | the memory referenced by | ||
| 62 | .Fa resolvedname . | ||
| 63 | The | ||
| 64 | .Fa resolvedname | ||
| 65 | argument | ||
| 66 | .Em must | ||
| 67 | refer to a buffer capable of storing at least | ||
| 68 | .Dv MAXPATHLEN | ||
| 69 | characters. | ||
| 70 | .Pp | ||
| 71 | The | ||
| 72 | .Fn realpath | ||
| 73 | function will resolve both absolute and relative paths | ||
| 74 | and return the absolute pathname corresponding to | ||
| 75 | .Fa pathname . | ||
| 76 | All but the last component of | ||
| 77 | .Fa pathname | ||
| 78 | must exist when | ||
| 79 | .Fn realpath | ||
| 80 | is called. | ||
| 81 | .Sh "RETURN VALUES" | ||
| 82 | The | ||
| 83 | .Fn realpath | ||
| 84 | function returns | ||
| 85 | .Fa resolved_name | ||
| 86 | on success. | ||
| 87 | If an error occurs, | ||
| 88 | .Fn realpath | ||
| 89 | returns | ||
| 90 | .Dv NULL , | ||
| 91 | and | ||
| 92 | .Fa resolved_name | ||
| 93 | contains the pathname which caused the problem. | ||
| 94 | .Sh ERRORS | ||
| 95 | The function | ||
| 96 | .Fn realpath | ||
| 97 | may fail and set the external variable | ||
| 98 | .Va errno | ||
| 99 | for any of the errors specified for the library functions | ||
| 100 | .Xr chdir 2 , | ||
| 101 | .Xr close 2 , | ||
| 102 | .Xr fchdir 2 , | ||
| 103 | .Xr lstat 2 , | ||
| 104 | .Xr open 2 , | ||
| 105 | .Xr readlink 2 | ||
| 106 | and | ||
| 107 | .Xr getcwd 3 . | ||
| 108 | .Sh CAVEATS | ||
| 109 | This implementation of | ||
| 110 | .Fn realpath | ||
| 111 | differs slightly from the Solaris implementation. | ||
| 112 | The | ||
| 113 | .Bx 4.4 | ||
| 114 | version always returns absolute pathnames, | ||
| 115 | whereas the Solaris implementation will, | ||
| 116 | under certain circumstances, return a relative | ||
| 117 | .Fa resolved_path | ||
| 118 | when given a relative | ||
| 119 | .Fa pathname . | ||
| 120 | .Sh "SEE ALSO" | ||
| 121 | .Xr getcwd 3 | ||
| 122 | .Sh HISTORY | ||
| 123 | The | ||
| 124 | .Fn realpath | ||
| 125 | function call first appeared in | ||
| 126 | .Bx 4.4 . | ||
diff --git a/src/lib/libc/stdlib/realpath.c b/src/lib/libc/stdlib/realpath.c new file mode 100644 index 0000000000..e349b7e068 --- /dev/null +++ b/src/lib/libc/stdlib/realpath.c | |||
| @@ -0,0 +1,159 @@ | |||
| 1 | /* | ||
| 2 | * Copyright (c) 1994 | ||
| 3 | * The Regents of the University of California. All rights reserved. | ||
| 4 | * | ||
| 5 | * This code is derived from software contributed to Berkeley by | ||
| 6 | * Jan-Simon Pendry. | ||
| 7 | * | ||
| 8 | * Redistribution and use in source and binary forms, with or without | ||
| 9 | * modification, are permitted provided that the following conditions | ||
| 10 | * are met: | ||
| 11 | * 1. Redistributions of source code must retain the above copyright | ||
| 12 | * notice, this list of conditions and the following disclaimer. | ||
| 13 | * 2. Redistributions in binary form must reproduce the above copyright | ||
| 14 | * notice, this list of conditions and the following disclaimer in the | ||
| 15 | * documentation and/or other materials provided with the distribution. | ||
| 16 | * 3. All advertising materials mentioning features or use of this software | ||
| 17 | * must display the following acknowledgement: | ||
| 18 | * This product includes software developed by the University of | ||
| 19 | * California, Berkeley and its contributors. | ||
| 20 | * 4. Neither the name of the University nor the names of its contributors | ||
| 21 | * may be used to endorse or promote products derived from this software | ||
| 22 | * without specific prior written permission. | ||
| 23 | * | ||
| 24 | * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND | ||
| 25 | * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE | ||
| 26 | * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE | ||
| 27 | * ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE | ||
| 28 | * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL | ||
| 29 | * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS | ||
| 30 | * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) | ||
| 31 | * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT | ||
| 32 | * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY | ||
| 33 | * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF | ||
| 34 | * SUCH DAMAGE. | ||
| 35 | */ | ||
| 36 | |||
| 37 | #if defined(LIBC_SCCS) && !defined(lint) | ||
| 38 | /*static char sccsid[] = "from: @(#)realpath.c 8.1 (Berkeley) 2/16/94";*/ | ||
| 39 | static char *rcsid = "$Id: realpath.c,v 1.1.1.1 1995/10/18 08:42:19 deraadt Exp $"; | ||
| 40 | #endif /* LIBC_SCCS and not lint */ | ||
| 41 | |||
| 42 | #include <sys/param.h> | ||
| 43 | #include <sys/stat.h> | ||
| 44 | |||
| 45 | #include <errno.h> | ||
| 46 | #include <fcntl.h> | ||
| 47 | #include <stdlib.h> | ||
| 48 | #include <string.h> | ||
| 49 | #include <unistd.h> | ||
| 50 | |||
| 51 | /* | ||
| 52 | * char *realpath(const char *path, char resolved_path[MAXPATHLEN]); | ||
| 53 | * | ||
| 54 | * Find the real name of path, by removing all ".", ".." and symlink | ||
| 55 | * components. Returns (resolved) on success, or (NULL) on failure, | ||
| 56 | * in which case the path which caused trouble is left in (resolved). | ||
| 57 | */ | ||
| 58 | char * | ||
| 59 | realpath(path, resolved) | ||
| 60 | const char *path; | ||
| 61 | char *resolved; | ||
| 62 | { | ||
| 63 | struct stat sb; | ||
| 64 | int fd, n, rootd, serrno; | ||
| 65 | char *p, *q, wbuf[MAXPATHLEN]; | ||
| 66 | |||
| 67 | /* Save the starting point. */ | ||
| 68 | if ((fd = open(".", O_RDONLY)) < 0) { | ||
| 69 | (void)strcpy(resolved, "."); | ||
| 70 | return (NULL); | ||
| 71 | } | ||
| 72 | |||
| 73 | /* | ||
| 74 | * Find the dirname and basename from the path to be resolved. | ||
| 75 | * Change directory to the dirname component. | ||
| 76 | * lstat the basename part. | ||
| 77 | * if it is a symlink, read in the value and loop. | ||
| 78 | * if it is a directory, then change to that directory. | ||
| 79 | * get the current directory name and append the basename. | ||
| 80 | */ | ||
| 81 | (void)strncpy(resolved, path, MAXPATHLEN - 1); | ||
| 82 | resolved[MAXPATHLEN - 1] = '\0'; | ||
| 83 | loop: | ||
| 84 | q = strrchr(resolved, '/'); | ||
| 85 | if (q != NULL) { | ||
| 86 | p = q + 1; | ||
| 87 | if (q == resolved) | ||
| 88 | q = "/"; | ||
| 89 | else { | ||
| 90 | do { | ||
| 91 | --q; | ||
| 92 | } while (q > resolved && *q == '/'); | ||
| 93 | q[1] = '\0'; | ||
| 94 | q = resolved; | ||
| 95 | } | ||
| 96 | if (chdir(q) < 0) | ||
| 97 | goto err1; | ||
| 98 | } else | ||
| 99 | p = resolved; | ||
| 100 | |||
| 101 | /* Deal with the last component. */ | ||
| 102 | if (lstat(p, &sb) == 0) { | ||
| 103 | if (S_ISLNK(sb.st_mode)) { | ||
| 104 | n = readlink(p, resolved, MAXPATHLEN); | ||
| 105 | if (n < 0) | ||
| 106 | goto err1; | ||
| 107 | resolved[n] = '\0'; | ||
| 108 | goto loop; | ||
| 109 | } | ||
| 110 | if (S_ISDIR(sb.st_mode)) { | ||
| 111 | if (chdir(p) < 0) | ||
| 112 | goto err1; | ||
| 113 | p = ""; | ||
| 114 | } | ||
| 115 | } | ||
| 116 | |||
| 117 | /* | ||
| 118 | * Save the last component name and get the full pathname of | ||
| 119 | * the current directory. | ||
| 120 | */ | ||
| 121 | (void)strcpy(wbuf, p); | ||
| 122 | if (getcwd(resolved, MAXPATHLEN) == 0) | ||
| 123 | goto err1; | ||
| 124 | |||
| 125 | /* | ||
| 126 | * Join the two strings together, ensuring that the right thing | ||
| 127 | * happens if the last component is empty, or the dirname is root. | ||
| 128 | */ | ||
| 129 | if (resolved[0] == '/' && resolved[1] == '\0') | ||
| 130 | rootd = 1; | ||
| 131 | else | ||
| 132 | rootd = 0; | ||
| 133 | |||
| 134 | if (*wbuf) { | ||
| 135 | if (strlen(resolved) + strlen(wbuf) + rootd + 1 > MAXPATHLEN) { | ||
| 136 | errno = ENAMETOOLONG; | ||
| 137 | goto err1; | ||
| 138 | } | ||
| 139 | if (rootd == 0) | ||
| 140 | (void)strcat(resolved, "/"); | ||
| 141 | (void)strcat(resolved, wbuf); | ||
| 142 | } | ||
| 143 | |||
| 144 | /* Go back to where we came from. */ | ||
| 145 | if (fchdir(fd) < 0) { | ||
| 146 | serrno = errno; | ||
| 147 | goto err2; | ||
| 148 | } | ||
| 149 | |||
| 150 | /* It's okay if the close fails, what's an fd more or less? */ | ||
| 151 | (void)close(fd); | ||
| 152 | return (resolved); | ||
| 153 | |||
| 154 | err1: serrno = errno; | ||
| 155 | (void)fchdir(fd); | ||
| 156 | err2: (void)close(fd); | ||
| 157 | errno = serrno; | ||
| 158 | return (NULL); | ||
| 159 | } | ||
diff --git a/src/lib/libc/stdlib/seed48.c b/src/lib/libc/stdlib/seed48.c new file mode 100644 index 0000000000..e3d31901dd --- /dev/null +++ b/src/lib/libc/stdlib/seed48.c | |||
| @@ -0,0 +1,36 @@ | |||
| 1 | /* | ||
| 2 | * Copyright (c) 1993 Martin Birgmeier | ||
| 3 | * All rights reserved. | ||
| 4 | * | ||
| 5 | * You may redistribute unmodified or modified versions of this source | ||
| 6 | * code provided that the above copyright notice and this and the | ||
| 7 | * following conditions are retained. | ||
| 8 | * | ||
| 9 | * This software is provided ``as is'', and comes with no warranties | ||
| 10 | * of any kind. I shall in no event be liable for anything that happens | ||
| 11 | * to anyone/anything when using this software. | ||
| 12 | */ | ||
| 13 | |||
| 14 | #include "rand48.h" | ||
| 15 | |||
| 16 | extern unsigned short __rand48_seed[3]; | ||
| 17 | extern unsigned short __rand48_mult[3]; | ||
| 18 | extern unsigned short __rand48_add; | ||
| 19 | |||
| 20 | unsigned short * | ||
| 21 | seed48(unsigned short xseed[3]) | ||
| 22 | { | ||
| 23 | static unsigned short sseed[3]; | ||
| 24 | |||
| 25 | sseed[0] = __rand48_seed[0]; | ||
| 26 | sseed[1] = __rand48_seed[1]; | ||
| 27 | sseed[2] = __rand48_seed[2]; | ||
| 28 | __rand48_seed[0] = xseed[0]; | ||
| 29 | __rand48_seed[1] = xseed[1]; | ||
| 30 | __rand48_seed[2] = xseed[2]; | ||
| 31 | __rand48_mult[0] = RAND48_MULT_0; | ||
| 32 | __rand48_mult[1] = RAND48_MULT_1; | ||
| 33 | __rand48_mult[2] = RAND48_MULT_2; | ||
| 34 | __rand48_add = RAND48_ADD; | ||
| 35 | return sseed; | ||
| 36 | } | ||
diff --git a/src/lib/libc/stdlib/setenv.c b/src/lib/libc/stdlib/setenv.c new file mode 100644 index 0000000000..a36669888d --- /dev/null +++ b/src/lib/libc/stdlib/setenv.c | |||
| @@ -0,0 +1,120 @@ | |||
| 1 | /* | ||
| 2 | * Copyright (c) 1987 Regents of the University of California. | ||
| 3 | * All rights reserved. | ||
| 4 | * | ||
| 5 | * Redistribution and use in source and binary forms, with or without | ||
| 6 | * modification, are permitted provided that the following conditions | ||
| 7 | * are met: | ||
| 8 | * 1. Redistributions of source code must retain the above copyright | ||
| 9 | * notice, this list of conditions and the following disclaimer. | ||
| 10 | * 2. Redistributions in binary form must reproduce the above copyright | ||
| 11 | * notice, this list of conditions and the following disclaimer in the | ||
| 12 | * documentation and/or other materials provided with the distribution. | ||
| 13 | * 3. All advertising materials mentioning features or use of this software | ||
| 14 | * must display the following acknowledgement: | ||
| 15 | * This product includes software developed by the University of | ||
| 16 | * California, Berkeley and its contributors. | ||
| 17 | * 4. Neither the name of the University nor the names of its contributors | ||
| 18 | * may be used to endorse or promote products derived from this software | ||
| 19 | * without specific prior written permission. | ||
| 20 | * | ||
| 21 | * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND | ||
| 22 | * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE | ||
| 23 | * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE | ||
| 24 | * ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE | ||
| 25 | * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL | ||
| 26 | * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS | ||
| 27 | * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) | ||
| 28 | * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT | ||
| 29 | * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY | ||
| 30 | * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF | ||
| 31 | * SUCH DAMAGE. | ||
| 32 | */ | ||
| 33 | |||
| 34 | #if defined(LIBC_SCCS) && !defined(lint) | ||
| 35 | /*static char *sccsid = "from: @(#)setenv.c 5.6 (Berkeley) 6/4/91";*/ | ||
| 36 | static char *rcsid = "$Id: setenv.c,v 1.1.1.1 1995/10/18 08:42:19 deraadt Exp $"; | ||
| 37 | #endif /* LIBC_SCCS and not lint */ | ||
| 38 | |||
| 39 | #include <stdlib.h> | ||
| 40 | #include <string.h> | ||
| 41 | |||
| 42 | /* | ||
| 43 | * setenv -- | ||
| 44 | * Set the value of the environmental variable "name" to be | ||
| 45 | * "value". If rewrite is set, replace any current value. | ||
| 46 | */ | ||
| 47 | int | ||
| 48 | setenv(name, value, rewrite) | ||
| 49 | register const char *name; | ||
| 50 | register const char *value; | ||
| 51 | int rewrite; | ||
| 52 | { | ||
| 53 | extern char **environ; | ||
| 54 | static int alloced; /* if allocated space before */ | ||
| 55 | register char *C; | ||
| 56 | int l_value, offset; | ||
| 57 | char *__findenv(); | ||
| 58 | |||
| 59 | if (*value == '=') /* no `=' in value */ | ||
| 60 | ++value; | ||
| 61 | l_value = strlen(value); | ||
| 62 | if ((C = __findenv(name, &offset))) { /* find if already exists */ | ||
| 63 | if (!rewrite) | ||
| 64 | return (0); | ||
| 65 | if (strlen(C) >= l_value) { /* old larger; copy over */ | ||
| 66 | while (*C++ = *value++); | ||
| 67 | return (0); | ||
| 68 | } | ||
| 69 | } else { /* create new slot */ | ||
| 70 | register int cnt; | ||
| 71 | register char **P; | ||
| 72 | |||
| 73 | for (P = environ, cnt = 0; *P; ++P, ++cnt); | ||
| 74 | if (alloced) { /* just increase size */ | ||
| 75 | environ = (char **)realloc((char *)environ, | ||
| 76 | (size_t)(sizeof(char *) * (cnt + 2))); | ||
| 77 | if (!environ) | ||
| 78 | return (-1); | ||
| 79 | } | ||
| 80 | else { /* get new space */ | ||
| 81 | alloced = 1; /* copy old entries into it */ | ||
| 82 | P = (char **)malloc((size_t)(sizeof(char *) * | ||
| 83 | (cnt + 2))); | ||
| 84 | if (!P) | ||
| 85 | return (-1); | ||
| 86 | bcopy(environ, P, cnt * sizeof(char *)); | ||
| 87 | environ = P; | ||
| 88 | } | ||
| 89 | environ[cnt + 1] = NULL; | ||
| 90 | offset = cnt; | ||
| 91 | } | ||
| 92 | for (C = (char *)name; *C && *C != '='; ++C); /* no `=' in name */ | ||
| 93 | if (!(environ[offset] = /* name + `=' + value */ | ||
| 94 | malloc((size_t)((int)(C - name) + l_value + 2)))) | ||
| 95 | return (-1); | ||
| 96 | for (C = environ[offset]; (*C = *name++) && *C != '='; ++C) | ||
| 97 | ; | ||
| 98 | for (*C++ = '='; *C++ = *value++; ) | ||
| 99 | ; | ||
| 100 | return (0); | ||
| 101 | } | ||
| 102 | |||
| 103 | /* | ||
| 104 | * unsetenv(name) -- | ||
| 105 | * Delete environmental variable "name". | ||
| 106 | */ | ||
| 107 | void | ||
| 108 | unsetenv(name) | ||
| 109 | const char *name; | ||
| 110 | { | ||
| 111 | extern char **environ; | ||
| 112 | register char **P; | ||
| 113 | int offset; | ||
| 114 | char *__findenv(); | ||
| 115 | |||
| 116 | while (__findenv(name, &offset)) /* if set multiple times */ | ||
| 117 | for (P = &environ[offset];; ++P) | ||
| 118 | if (!(*P = *(P + 1))) | ||
| 119 | break; | ||
| 120 | } | ||
diff --git a/src/lib/libc/stdlib/srand48.c b/src/lib/libc/stdlib/srand48.c new file mode 100644 index 0000000000..daf733f93e --- /dev/null +++ b/src/lib/libc/stdlib/srand48.c | |||
| @@ -0,0 +1,30 @@ | |||
| 1 | /* | ||
| 2 | * Copyright (c) 1993 Martin Birgmeier | ||
| 3 | * All rights reserved. | ||
| 4 | * | ||
| 5 | * You may redistribute unmodified or modified versions of this source | ||
| 6 | * code provided that the above copyright notice and this and the | ||
| 7 | * following conditions are retained. | ||
| 8 | * | ||
| 9 | * This software is provided ``as is'', and comes with no warranties | ||
| 10 | * of any kind. I shall in no event be liable for anything that happens | ||
| 11 | * to anyone/anything when using this software. | ||
| 12 | */ | ||
| 13 | |||
| 14 | #include "rand48.h" | ||
| 15 | |||
| 16 | extern unsigned short __rand48_seed[3]; | ||
| 17 | extern unsigned short __rand48_mult[3]; | ||
| 18 | extern unsigned short __rand48_add; | ||
| 19 | |||
| 20 | void | ||
| 21 | srand48(long seed) | ||
| 22 | { | ||
| 23 | __rand48_seed[0] = RAND48_SEED_0; | ||
| 24 | __rand48_seed[1] = (unsigned short) seed; | ||
| 25 | __rand48_seed[2] = (unsigned short) (seed >> 16); | ||
| 26 | __rand48_mult[0] = RAND48_MULT_0; | ||
| 27 | __rand48_mult[1] = RAND48_MULT_1; | ||
| 28 | __rand48_mult[2] = RAND48_MULT_2; | ||
| 29 | __rand48_add = RAND48_ADD; | ||
| 30 | } | ||
diff --git a/src/lib/libc/stdlib/strtod.3 b/src/lib/libc/stdlib/strtod.3 new file mode 100644 index 0000000000..0b7f973857 --- /dev/null +++ b/src/lib/libc/stdlib/strtod.3 | |||
| @@ -0,0 +1,114 @@ | |||
| 1 | .\" Copyright (c) 1990, 1991 The Regents of the University of California. | ||
| 2 | .\" All rights reserved. | ||
| 3 | .\" | ||
| 4 | .\" This code is derived from software contributed to Berkeley by | ||
| 5 | .\" the American National Standards Committee X3, on Information | ||
| 6 | .\" Processing Systems. | ||
| 7 | .\" | ||
| 8 | .\" Redistribution and use in source and binary forms, with or without | ||
| 9 | .\" modification, are permitted provided that the following conditions | ||
| 10 | .\" are met: | ||
| 11 | .\" 1. Redistributions of source code must retain the above copyright | ||
| 12 | .\" notice, this list of conditions and the following disclaimer. | ||
| 13 | .\" 2. Redistributions in binary form must reproduce the above copyright | ||
| 14 | .\" notice, this list of conditions and the following disclaimer in the | ||
| 15 | .\" documentation and/or other materials provided with the distribution. | ||
| 16 | .\" 3. All advertising materials mentioning features or use of this software | ||
| 17 | .\" must display the following acknowledgement: | ||
| 18 | .\" This product includes software developed by the University of | ||
| 19 | .\" California, Berkeley and its contributors. | ||
| 20 | .\" 4. Neither the name of the University nor the names of its contributors | ||
| 21 | .\" may be used to endorse or promote products derived from this software | ||
| 22 | .\" without specific prior written permission. | ||
| 23 | .\" | ||
| 24 | .\" THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND | ||
| 25 | .\" ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE | ||
| 26 | .\" IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE | ||
| 27 | .\" ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE | ||
| 28 | .\" FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL | ||
| 29 | .\" DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS | ||
| 30 | .\" OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) | ||
| 31 | .\" HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT | ||
| 32 | .\" LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY | ||
| 33 | .\" OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF | ||
| 34 | .\" SUCH DAMAGE. | ||
| 35 | .\" | ||
| 36 | .\" from: @(#)strtod.3 5.3 (Berkeley) 6/29/91 | ||
| 37 | .\" $Id: strtod.3,v 1.1.1.1 1995/10/18 08:42:19 deraadt Exp $ | ||
| 38 | .\" | ||
| 39 | .Dd June 29, 1991 | ||
| 40 | .Dt STRTOD 3 | ||
| 41 | .Os | ||
| 42 | .Sh NAME | ||
| 43 | .Nm strtod | ||
| 44 | .Nd convert | ||
| 45 | .Tn ASCII | ||
| 46 | string to double | ||
| 47 | .Sh SYNOPSIS | ||
| 48 | .Fd #include <stdlib.h> | ||
| 49 | .Ft double | ||
| 50 | .Fn strtod "const char *nptr" "char **endptr" | ||
| 51 | .Sh DESCRIPTION | ||
| 52 | The | ||
| 53 | .Fn strtod | ||
| 54 | function converts the initial portion of the string | ||
| 55 | pointed to by | ||
| 56 | .Fa nptr | ||
| 57 | to | ||
| 58 | .Em double | ||
| 59 | representation. | ||
| 60 | .Pp | ||
| 61 | The expected form of the string is an optional plus (``+'') or minus | ||
| 62 | sign (``-'') followed by a sequence of digits optionally containing | ||
| 63 | a decimal-point character, optionally followed by an exponent. | ||
| 64 | An exponent consists of an ``E'' or ``e'', followed by an optional plus | ||
| 65 | or minus sign, followed by a sequence of digits. | ||
| 66 | .Pp | ||
| 67 | Leading white-space characters in the string (as defined by the | ||
| 68 | .Xr isspace 3 | ||
| 69 | function) are skipped. | ||
| 70 | .Sh RETURN VALUES | ||
| 71 | The | ||
| 72 | .Fn strtod | ||
| 73 | function returns the converted value, if any. | ||
| 74 | .Pp | ||
| 75 | If | ||
| 76 | .Fa endptr | ||
| 77 | is not | ||
| 78 | .Dv NULL , | ||
| 79 | a pointer to the character after the last character used | ||
| 80 | in the conversion is stored in the location referenced by | ||
| 81 | .Fa endptr . | ||
| 82 | .Pp | ||
| 83 | If no conversion is performed, zero is returned and the value of | ||
| 84 | .Fa nptr | ||
| 85 | is stored in the location referenced by | ||
| 86 | .Fa endptr . | ||
| 87 | .Pp | ||
| 88 | If the correct value would cause overflow, plus or minus | ||
| 89 | .Dv HUGE_VAL | ||
| 90 | is returned (according to the sign of the value), and | ||
| 91 | .Dv ERANGE | ||
| 92 | is stored in | ||
| 93 | .Va errno . | ||
| 94 | If the correct value would cause underflow, zero is | ||
| 95 | returned and | ||
| 96 | .Dv ERANGE | ||
| 97 | is stored in | ||
| 98 | .Va errno . | ||
| 99 | .Sh ERRORS | ||
| 100 | .Bl -tag -width Er | ||
| 101 | .It Bq Er ERANGE | ||
| 102 | Overflow or underflow occurred. | ||
| 103 | .Sh SEE ALSO | ||
| 104 | .Xr atof 3 , | ||
| 105 | .Xr atoi 3 , | ||
| 106 | .Xr atol 3 , | ||
| 107 | .Xr strtol 3 , | ||
| 108 | .Xr strtoul 3 | ||
| 109 | .Sh STANDARDS | ||
| 110 | The | ||
| 111 | .Fn strtod | ||
| 112 | function | ||
| 113 | conforms to | ||
| 114 | .St -ansiC . | ||
diff --git a/src/lib/libc/stdlib/strtod.c b/src/lib/libc/stdlib/strtod.c new file mode 100644 index 0000000000..b13fa128f5 --- /dev/null +++ b/src/lib/libc/stdlib/strtod.c | |||
| @@ -0,0 +1,2499 @@ | |||
| 1 | /**************************************************************** | ||
| 2 | * | ||
| 3 | * The author of this software is David M. Gay. | ||
| 4 | * | ||
| 5 | * Copyright (c) 1991 by AT&T. | ||
| 6 | * | ||
| 7 | * Permission to use, copy, modify, and distribute this software for any | ||
| 8 | * purpose without fee is hereby granted, provided that this entire notice | ||
| 9 | * is included in all copies of any software which is or includes a copy | ||
| 10 | * or modification of this software and in all copies of the supporting | ||
| 11 | * documentation for such software. | ||
| 12 | * | ||
| 13 | * THIS SOFTWARE IS BEING PROVIDED "AS IS", WITHOUT ANY EXPRESS OR IMPLIED | ||
| 14 | * WARRANTY. IN PARTICULAR, NEITHER THE AUTHOR NOR AT&T MAKES ANY | ||
| 15 | * REPRESENTATION OR WARRANTY OF ANY KIND CONCERNING THE MERCHANTABILITY | ||
| 16 | * OF THIS SOFTWARE OR ITS FITNESS FOR ANY PARTICULAR PURPOSE. | ||
| 17 | * | ||
| 18 | ***************************************************************/ | ||
| 19 | |||
| 20 | /* Please send bug reports to | ||
| 21 | David M. Gay | ||
| 22 | AT&T Bell Laboratories, Room 2C-463 | ||
| 23 | 600 Mountain Avenue | ||
| 24 | Murray Hill, NJ 07974-2070 | ||
| 25 | U.S.A. | ||
| 26 | dmg@research.att.com or research!dmg | ||
| 27 | */ | ||
| 28 | |||
| 29 | /* strtod for IEEE-, VAX-, and IBM-arithmetic machines. | ||
| 30 | * | ||
| 31 | * This strtod returns a nearest machine number to the input decimal | ||
| 32 | * string (or sets errno to ERANGE). With IEEE arithmetic, ties are | ||
| 33 | * broken by the IEEE round-even rule. Otherwise ties are broken by | ||
| 34 | * biased rounding (add half and chop). | ||
| 35 | * | ||
| 36 | * Inspired loosely by William D. Clinger's paper "How to Read Floating | ||
| 37 | * Point Numbers Accurately" [Proc. ACM SIGPLAN '90, pp. 92-101]. | ||
| 38 | * | ||
| 39 | * Modifications: | ||
| 40 | * | ||
| 41 | * 1. We only require IEEE, IBM, or VAX double-precision | ||
| 42 | * arithmetic (not IEEE double-extended). | ||
| 43 | * 2. We get by with floating-point arithmetic in a case that | ||
| 44 | * Clinger missed -- when we're computing d * 10^n | ||
| 45 | * for a small integer d and the integer n is not too | ||
| 46 | * much larger than 22 (the maximum integer k for which | ||
| 47 | * we can represent 10^k exactly), we may be able to | ||
| 48 | * compute (d*10^k) * 10^(e-k) with just one roundoff. | ||
| 49 | * 3. Rather than a bit-at-a-time adjustment of the binary | ||
| 50 | * result in the hard case, we use floating-point | ||
| 51 | * arithmetic to determine the adjustment to within | ||
| 52 | * one bit; only in really hard cases do we need to | ||
| 53 | * compute a second residual. | ||
| 54 | * 4. Because of 3., we don't need a large table of powers of 10 | ||
| 55 | * for ten-to-e (just some small tables, e.g. of 10^k | ||
| 56 | * for 0 <= k <= 22). | ||
| 57 | */ | ||
| 58 | |||
| 59 | /* | ||
| 60 | * #define IEEE_LITTLE_ENDIAN for IEEE-arithmetic machines where the least | ||
| 61 | * significant byte has the lowest address. | ||
| 62 | * #define IEEE_BIG_ENDIAN for IEEE-arithmetic machines where the most | ||
| 63 | * significant byte has the lowest address. | ||
| 64 | * #define Long int on machines with 32-bit ints and 64-bit longs. | ||
| 65 | * #define Sudden_Underflow for IEEE-format machines without gradual | ||
| 66 | * underflow (i.e., that flush to zero on underflow). | ||
| 67 | * #define IBM for IBM mainframe-style floating-point arithmetic. | ||
| 68 | * #define VAX for VAX-style floating-point arithmetic. | ||
| 69 | * #define Unsigned_Shifts if >> does treats its left operand as unsigned. | ||
| 70 | * #define No_leftright to omit left-right logic in fast floating-point | ||
| 71 | * computation of dtoa. | ||
| 72 | * #define Check_FLT_ROUNDS if FLT_ROUNDS can assume the values 2 or 3. | ||
| 73 | * #define RND_PRODQUOT to use rnd_prod and rnd_quot (assembly routines | ||
| 74 | * that use extended-precision instructions to compute rounded | ||
| 75 | * products and quotients) with IBM. | ||
| 76 | * #define ROUND_BIASED for IEEE-format with biased rounding. | ||
| 77 | * #define Inaccurate_Divide for IEEE-format with correctly rounded | ||
| 78 | * products but inaccurate quotients, e.g., for Intel i860. | ||
| 79 | * #define Just_16 to store 16 bits per 32-bit Long when doing high-precision | ||
| 80 | * integer arithmetic. Whether this speeds things up or slows things | ||
| 81 | * down depends on the machine and the number being converted. | ||
| 82 | * #define KR_headers for old-style C function headers. | ||
| 83 | * #define Bad_float_h if your system lacks a float.h or if it does not | ||
| 84 | * define some or all of DBL_DIG, DBL_MAX_10_EXP, DBL_MAX_EXP, | ||
| 85 | * FLT_RADIX, FLT_ROUNDS, and DBL_MAX. | ||
| 86 | * #define MALLOC your_malloc, where your_malloc(n) acts like malloc(n) | ||
| 87 | * if memory is available and otherwise does something you deem | ||
| 88 | * appropriate. If MALLOC is undefined, malloc will be invoked | ||
| 89 | * directly -- and assumed always to succeed. | ||
| 90 | */ | ||
| 91 | |||
| 92 | #if defined(LIBC_SCCS) && !defined(lint) | ||
| 93 | static char *rcsid = "$Id: strtod.c,v 1.1.1.1 1995/10/18 08:42:19 deraadt Exp $"; | ||
| 94 | #endif /* LIBC_SCCS and not lint */ | ||
| 95 | |||
| 96 | #if defined(__m68k__) || defined(__sparc__) || defined(__i386__) || \ | ||
| 97 | defined(__mips__) || defined(__ns32k__) || defined(__alpha__) | ||
| 98 | #include <machine/endian.h> | ||
| 99 | #if BYTE_ORDER == BIG_ENDIAN | ||
| 100 | #define IEEE_BIG_ENDIAN | ||
| 101 | #else | ||
| 102 | #define IEEE_LITTLE_ENDIAN | ||
| 103 | #endif | ||
| 104 | #endif | ||
| 105 | |||
| 106 | #ifdef vax | ||
| 107 | #define VAX | ||
| 108 | #endif | ||
| 109 | |||
| 110 | #define Long int32_t | ||
| 111 | #define ULong u_int32_t | ||
| 112 | |||
| 113 | #ifdef DEBUG | ||
| 114 | #include "stdio.h" | ||
| 115 | #define Bug(x) {fprintf(stderr, "%s\n", x); exit(1);} | ||
| 116 | #endif | ||
| 117 | |||
| 118 | #ifdef __cplusplus | ||
| 119 | #include "malloc.h" | ||
| 120 | #include "memory.h" | ||
| 121 | #else | ||
| 122 | #ifndef KR_headers | ||
| 123 | #include "stdlib.h" | ||
| 124 | #include "string.h" | ||
| 125 | #include "locale.h" | ||
| 126 | #else | ||
| 127 | #include "malloc.h" | ||
| 128 | #include "memory.h" | ||
| 129 | #endif | ||
| 130 | #endif | ||
| 131 | |||
| 132 | #ifdef MALLOC | ||
| 133 | #ifdef KR_headers | ||
| 134 | extern char *MALLOC(); | ||
| 135 | #else | ||
| 136 | extern void *MALLOC(size_t); | ||
| 137 | #endif | ||
| 138 | #else | ||
| 139 | #define MALLOC malloc | ||
| 140 | #endif | ||
| 141 | |||
| 142 | #include "ctype.h" | ||
| 143 | #include "errno.h" | ||
| 144 | |||
| 145 | #ifdef Bad_float_h | ||
| 146 | #undef __STDC__ | ||
| 147 | #ifdef IEEE_BIG_ENDIAN | ||
| 148 | #define IEEE_ARITHMETIC | ||
| 149 | #endif | ||
| 150 | #ifdef IEEE_LITTLE_ENDIAN | ||
| 151 | #define IEEE_ARITHMETIC | ||
| 152 | #endif | ||
| 153 | |||
| 154 | #ifdef IEEE_ARITHMETIC | ||
| 155 | #define DBL_DIG 15 | ||
| 156 | #define DBL_MAX_10_EXP 308 | ||
| 157 | #define DBL_MAX_EXP 1024 | ||
| 158 | #define FLT_RADIX 2 | ||
| 159 | #define FLT_ROUNDS 1 | ||
| 160 | #define DBL_MAX 1.7976931348623157e+308 | ||
| 161 | #endif | ||
| 162 | |||
| 163 | #ifdef IBM | ||
| 164 | #define DBL_DIG 16 | ||
| 165 | #define DBL_MAX_10_EXP 75 | ||
| 166 | #define DBL_MAX_EXP 63 | ||
| 167 | #define FLT_RADIX 16 | ||
| 168 | #define FLT_ROUNDS 0 | ||
| 169 | #define DBL_MAX 7.2370055773322621e+75 | ||
| 170 | #endif | ||
| 171 | |||
| 172 | #ifdef VAX | ||
| 173 | #define DBL_DIG 16 | ||
| 174 | #define DBL_MAX_10_EXP 38 | ||
| 175 | #define DBL_MAX_EXP 127 | ||
| 176 | #define FLT_RADIX 2 | ||
| 177 | #define FLT_ROUNDS 1 | ||
| 178 | #define DBL_MAX 1.7014118346046923e+38 | ||
| 179 | #endif | ||
| 180 | |||
| 181 | #ifndef LONG_MAX | ||
| 182 | #define LONG_MAX 2147483647 | ||
| 183 | #endif | ||
| 184 | #else | ||
| 185 | #include "float.h" | ||
| 186 | #endif | ||
| 187 | #ifndef __MATH_H__ | ||
| 188 | #include "math.h" | ||
| 189 | #endif | ||
| 190 | |||
| 191 | #ifdef __cplusplus | ||
| 192 | extern "C" { | ||
| 193 | #endif | ||
| 194 | |||
| 195 | #ifndef CONST | ||
| 196 | #ifdef KR_headers | ||
| 197 | #define CONST /* blank */ | ||
| 198 | #else | ||
| 199 | #define CONST const | ||
| 200 | #endif | ||
| 201 | #endif | ||
| 202 | |||
| 203 | #ifdef Unsigned_Shifts | ||
| 204 | #define Sign_Extend(a,b) if (b < 0) a |= 0xffff0000; | ||
| 205 | #else | ||
| 206 | #define Sign_Extend(a,b) /*no-op*/ | ||
| 207 | #endif | ||
| 208 | |||
| 209 | #if defined(IEEE_LITTLE_ENDIAN) + defined(IEEE_BIG_ENDIAN) + defined(VAX) + \ | ||
| 210 | defined(IBM) != 1 | ||
| 211 | Exactly one of IEEE_LITTLE_ENDIAN IEEE_BIG_ENDIAN, VAX, or | ||
| 212 | IBM should be defined. | ||
| 213 | #endif | ||
| 214 | |||
| 215 | #ifdef IEEE_LITTLE_ENDIAN | ||
| 216 | #define word0(x) ((ULong *)&x)[1] | ||
| 217 | #define word1(x) ((ULong *)&x)[0] | ||
| 218 | #else | ||
| 219 | #define word0(x) ((ULong *)&x)[0] | ||
| 220 | #define word1(x) ((ULong *)&x)[1] | ||
| 221 | #endif | ||
| 222 | |||
| 223 | /* The following definition of Storeinc is appropriate for MIPS processors. | ||
| 224 | * An alternative that might be better on some machines is | ||
| 225 | * #define Storeinc(a,b,c) (*a++ = b << 16 | c & 0xffff) | ||
| 226 | */ | ||
| 227 | #if defined(IEEE_LITTLE_ENDIAN) + defined(VAX) | ||
| 228 | #define Storeinc(a,b,c) (((unsigned short *)a)[1] = (unsigned short)b, \ | ||
| 229 | ((unsigned short *)a)[0] = (unsigned short)c, a++) | ||
| 230 | #else | ||
| 231 | #define Storeinc(a,b,c) (((unsigned short *)a)[0] = (unsigned short)b, \ | ||
| 232 | ((unsigned short *)a)[1] = (unsigned short)c, a++) | ||
| 233 | #endif | ||
| 234 | |||
| 235 | /* #define P DBL_MANT_DIG */ | ||
| 236 | /* Ten_pmax = floor(P*log(2)/log(5)) */ | ||
| 237 | /* Bletch = (highest power of 2 < DBL_MAX_10_EXP) / 16 */ | ||
| 238 | /* Quick_max = floor((P-1)*log(FLT_RADIX)/log(10) - 1) */ | ||
| 239 | /* Int_max = floor(P*log(FLT_RADIX)/log(10) - 1) */ | ||
| 240 | |||
| 241 | #if defined(IEEE_LITTLE_ENDIAN) + defined(IEEE_BIG_ENDIAN) | ||
| 242 | #define Exp_shift 20 | ||
| 243 | #define Exp_shift1 20 | ||
| 244 | #define Exp_msk1 0x100000 | ||
| 245 | #define Exp_msk11 0x100000 | ||
| 246 | #define Exp_mask 0x7ff00000 | ||
| 247 | #define P 53 | ||
| 248 | #define Bias 1023 | ||
| 249 | #define IEEE_Arith | ||
| 250 | #define Emin (-1022) | ||
| 251 | #define Exp_1 0x3ff00000 | ||
| 252 | #define Exp_11 0x3ff00000 | ||
| 253 | #define Ebits 11 | ||
| 254 | #define Frac_mask 0xfffff | ||
| 255 | #define Frac_mask1 0xfffff | ||
| 256 | #define Ten_pmax 22 | ||
| 257 | #define Bletch 0x10 | ||
| 258 | #define Bndry_mask 0xfffff | ||
| 259 | #define Bndry_mask1 0xfffff | ||
| 260 | #define LSB 1 | ||
| 261 | #define Sign_bit 0x80000000 | ||
| 262 | #define Log2P 1 | ||
| 263 | #define Tiny0 0 | ||
| 264 | #define Tiny1 1 | ||
| 265 | #define Quick_max 14 | ||
| 266 | #define Int_max 14 | ||
| 267 | #define Infinite(x) (word0(x) == 0x7ff00000) /* sufficient test for here */ | ||
| 268 | #else | ||
| 269 | #undef Sudden_Underflow | ||
| 270 | #define Sudden_Underflow | ||
| 271 | #ifdef IBM | ||
| 272 | #define Exp_shift 24 | ||
| 273 | #define Exp_shift1 24 | ||
| 274 | #define Exp_msk1 0x1000000 | ||
| 275 | #define Exp_msk11 0x1000000 | ||
| 276 | #define Exp_mask 0x7f000000 | ||
| 277 | #define P 14 | ||
| 278 | #define Bias 65 | ||
| 279 | #define Exp_1 0x41000000 | ||
| 280 | #define Exp_11 0x41000000 | ||
| 281 | #define Ebits 8 /* exponent has 7 bits, but 8 is the right value in b2d */ | ||
| 282 | #define Frac_mask 0xffffff | ||
| 283 | #define Frac_mask1 0xffffff | ||
| 284 | #define Bletch 4 | ||
| 285 | #define Ten_pmax 22 | ||
| 286 | #define Bndry_mask 0xefffff | ||
| 287 | #define Bndry_mask1 0xffffff | ||
| 288 | #define LSB 1 | ||
| 289 | #define Sign_bit 0x80000000 | ||
| 290 | #define Log2P 4 | ||
| 291 | #define Tiny0 0x100000 | ||
| 292 | #define Tiny1 0 | ||
| 293 | #define Quick_max 14 | ||
| 294 | #define Int_max 15 | ||
| 295 | #else /* VAX */ | ||
| 296 | #define Exp_shift 23 | ||
| 297 | #define Exp_shift1 7 | ||
| 298 | #define Exp_msk1 0x80 | ||
| 299 | #define Exp_msk11 0x800000 | ||
| 300 | #define Exp_mask 0x7f80 | ||
| 301 | #define P 56 | ||
| 302 | #define Bias 129 | ||
| 303 | #define Exp_1 0x40800000 | ||
| 304 | #define Exp_11 0x4080 | ||
| 305 | #define Ebits 8 | ||
| 306 | #define Frac_mask 0x7fffff | ||
| 307 | #define Frac_mask1 0xffff007f | ||
| 308 | #define Ten_pmax 24 | ||
| 309 | #define Bletch 2 | ||
| 310 | #define Bndry_mask 0xffff007f | ||
| 311 | #define Bndry_mask1 0xffff007f | ||
| 312 | #define LSB 0x10000 | ||
| 313 | #define Sign_bit 0x8000 | ||
| 314 | #define Log2P 1 | ||
| 315 | #define Tiny0 0x80 | ||
| 316 | #define Tiny1 0 | ||
| 317 | #define Quick_max 15 | ||
| 318 | #define Int_max 15 | ||
| 319 | #endif | ||
| 320 | #endif | ||
| 321 | |||
| 322 | #ifndef IEEE_Arith | ||
| 323 | #define ROUND_BIASED | ||
| 324 | #endif | ||
| 325 | |||
| 326 | #ifdef RND_PRODQUOT | ||
| 327 | #define rounded_product(a,b) a = rnd_prod(a, b) | ||
| 328 | #define rounded_quotient(a,b) a = rnd_quot(a, b) | ||
| 329 | #ifdef KR_headers | ||
| 330 | extern double rnd_prod(), rnd_quot(); | ||
| 331 | #else | ||
| 332 | extern double rnd_prod(double, double), rnd_quot(double, double); | ||
| 333 | #endif | ||
| 334 | #else | ||
| 335 | #define rounded_product(a,b) a *= b | ||
| 336 | #define rounded_quotient(a,b) a /= b | ||
| 337 | #endif | ||
| 338 | |||
| 339 | #define Big0 (Frac_mask1 | Exp_msk1*(DBL_MAX_EXP+Bias-1)) | ||
| 340 | #define Big1 0xffffffff | ||
| 341 | |||
| 342 | #ifndef Just_16 | ||
| 343 | /* When Pack_32 is not defined, we store 16 bits per 32-bit Long. | ||
| 344 | * This makes some inner loops simpler and sometimes saves work | ||
| 345 | * during multiplications, but it often seems to make things slightly | ||
| 346 | * slower. Hence the default is now to store 32 bits per Long. | ||
| 347 | */ | ||
| 348 | #ifndef Pack_32 | ||
| 349 | #define Pack_32 | ||
| 350 | #endif | ||
| 351 | #endif | ||
| 352 | |||
| 353 | #define Kmax 15 | ||
| 354 | |||
| 355 | #ifdef __cplusplus | ||
| 356 | extern "C" double strtod(const char *s00, char **se); | ||
| 357 | extern "C" char *__dtoa(double d, int mode, int ndigits, | ||
| 358 | int *decpt, int *sign, char **rve); | ||
| 359 | #endif | ||
| 360 | |||
| 361 | struct | ||
| 362 | Bigint { | ||
| 363 | struct Bigint *next; | ||
| 364 | int k, maxwds, sign, wds; | ||
| 365 | ULong x[1]; | ||
| 366 | }; | ||
| 367 | |||
| 368 | typedef struct Bigint Bigint; | ||
| 369 | |||
| 370 | static Bigint *freelist[Kmax+1]; | ||
| 371 | |||
| 372 | static Bigint * | ||
| 373 | Balloc | ||
| 374 | #ifdef KR_headers | ||
| 375 | (k) int k; | ||
| 376 | #else | ||
| 377 | (int k) | ||
| 378 | #endif | ||
| 379 | { | ||
| 380 | int x; | ||
| 381 | Bigint *rv; | ||
| 382 | |||
| 383 | if (rv = freelist[k]) { | ||
| 384 | freelist[k] = rv->next; | ||
| 385 | } | ||
| 386 | else { | ||
| 387 | x = 1 << k; | ||
| 388 | rv = (Bigint *)MALLOC(sizeof(Bigint) + (x-1)*sizeof(Long)); | ||
| 389 | rv->k = k; | ||
| 390 | rv->maxwds = x; | ||
| 391 | } | ||
| 392 | rv->sign = rv->wds = 0; | ||
| 393 | return rv; | ||
| 394 | } | ||
| 395 | |||
| 396 | static void | ||
| 397 | Bfree | ||
| 398 | #ifdef KR_headers | ||
| 399 | (v) Bigint *v; | ||
| 400 | #else | ||
| 401 | (Bigint *v) | ||
| 402 | #endif | ||
| 403 | { | ||
| 404 | if (v) { | ||
| 405 | v->next = freelist[v->k]; | ||
| 406 | freelist[v->k] = v; | ||
| 407 | } | ||
| 408 | } | ||
| 409 | |||
| 410 | #define Bcopy(x,y) memcpy((char *)&x->sign, (char *)&y->sign, \ | ||
| 411 | y->wds*sizeof(Long) + 2*sizeof(int)) | ||
| 412 | |||
| 413 | static Bigint * | ||
| 414 | multadd | ||
| 415 | #ifdef KR_headers | ||
| 416 | (b, m, a) Bigint *b; int m, a; | ||
| 417 | #else | ||
| 418 | (Bigint *b, int m, int a) /* multiply by m and add a */ | ||
| 419 | #endif | ||
| 420 | { | ||
| 421 | int i, wds; | ||
| 422 | ULong *x, y; | ||
| 423 | #ifdef Pack_32 | ||
| 424 | ULong xi, z; | ||
| 425 | #endif | ||
| 426 | Bigint *b1; | ||
| 427 | |||
| 428 | wds = b->wds; | ||
| 429 | x = b->x; | ||
| 430 | i = 0; | ||
| 431 | do { | ||
| 432 | #ifdef Pack_32 | ||
| 433 | xi = *x; | ||
| 434 | y = (xi & 0xffff) * m + a; | ||
| 435 | z = (xi >> 16) * m + (y >> 16); | ||
| 436 | a = (int)(z >> 16); | ||
| 437 | *x++ = (z << 16) + (y & 0xffff); | ||
| 438 | #else | ||
| 439 | y = *x * m + a; | ||
| 440 | a = (int)(y >> 16); | ||
| 441 | *x++ = y & 0xffff; | ||
| 442 | #endif | ||
| 443 | } | ||
| 444 | while(++i < wds); | ||
| 445 | if (a) { | ||
| 446 | if (wds >= b->maxwds) { | ||
| 447 | b1 = Balloc(b->k+1); | ||
| 448 | Bcopy(b1, b); | ||
| 449 | Bfree(b); | ||
| 450 | b = b1; | ||
| 451 | } | ||
| 452 | b->x[wds++] = a; | ||
| 453 | b->wds = wds; | ||
| 454 | } | ||
| 455 | return b; | ||
| 456 | } | ||
| 457 | |||
| 458 | static Bigint * | ||
| 459 | s2b | ||
| 460 | #ifdef KR_headers | ||
| 461 | (s, nd0, nd, y9) CONST char *s; int nd0, nd; ULong y9; | ||
| 462 | #else | ||
| 463 | (CONST char *s, int nd0, int nd, ULong y9) | ||
| 464 | #endif | ||
| 465 | { | ||
| 466 | Bigint *b; | ||
| 467 | int i, k; | ||
| 468 | Long x, y; | ||
| 469 | |||
| 470 | x = (nd + 8) / 9; | ||
| 471 | for(k = 0, y = 1; x > y; y <<= 1, k++) ; | ||
| 472 | #ifdef Pack_32 | ||
| 473 | b = Balloc(k); | ||
| 474 | b->x[0] = y9; | ||
| 475 | b->wds = 1; | ||
| 476 | #else | ||
| 477 | b = Balloc(k+1); | ||
| 478 | b->x[0] = y9 & 0xffff; | ||
| 479 | b->wds = (b->x[1] = y9 >> 16) ? 2 : 1; | ||
| 480 | #endif | ||
| 481 | |||
| 482 | i = 9; | ||
| 483 | if (9 < nd0) { | ||
| 484 | s += 9; | ||
| 485 | do b = multadd(b, 10, *s++ - '0'); | ||
| 486 | while(++i < nd0); | ||
| 487 | s++; | ||
| 488 | } | ||
| 489 | else | ||
| 490 | s += 10; | ||
| 491 | for(; i < nd; i++) | ||
| 492 | b = multadd(b, 10, *s++ - '0'); | ||
| 493 | return b; | ||
| 494 | } | ||
| 495 | |||
| 496 | static int | ||
| 497 | hi0bits | ||
| 498 | #ifdef KR_headers | ||
| 499 | (x) register ULong x; | ||
| 500 | #else | ||
| 501 | (register ULong x) | ||
| 502 | #endif | ||
| 503 | { | ||
| 504 | register int k = 0; | ||
| 505 | |||
| 506 | if (!(x & 0xffff0000)) { | ||
| 507 | k = 16; | ||
| 508 | x <<= 16; | ||
| 509 | } | ||
| 510 | if (!(x & 0xff000000)) { | ||
| 511 | k += 8; | ||
| 512 | x <<= 8; | ||
| 513 | } | ||
| 514 | if (!(x & 0xf0000000)) { | ||
| 515 | k += 4; | ||
| 516 | x <<= 4; | ||
| 517 | } | ||
| 518 | if (!(x & 0xc0000000)) { | ||
| 519 | k += 2; | ||
| 520 | x <<= 2; | ||
| 521 | } | ||
| 522 | if (!(x & 0x80000000)) { | ||
| 523 | k++; | ||
| 524 | if (!(x & 0x40000000)) | ||
| 525 | return 32; | ||
| 526 | } | ||
| 527 | return k; | ||
| 528 | } | ||
| 529 | |||
| 530 | static int | ||
| 531 | lo0bits | ||
| 532 | #ifdef KR_headers | ||
| 533 | (y) ULong *y; | ||
| 534 | #else | ||
| 535 | (ULong *y) | ||
| 536 | #endif | ||
| 537 | { | ||
| 538 | register int k; | ||
| 539 | register ULong x = *y; | ||
| 540 | |||
| 541 | if (x & 7) { | ||
| 542 | if (x & 1) | ||
| 543 | return 0; | ||
| 544 | if (x & 2) { | ||
| 545 | *y = x >> 1; | ||
| 546 | return 1; | ||
| 547 | } | ||
| 548 | *y = x >> 2; | ||
| 549 | return 2; | ||
| 550 | } | ||
| 551 | k = 0; | ||
| 552 | if (!(x & 0xffff)) { | ||
| 553 | k = 16; | ||
| 554 | x >>= 16; | ||
| 555 | } | ||
| 556 | if (!(x & 0xff)) { | ||
| 557 | k += 8; | ||
| 558 | x >>= 8; | ||
| 559 | } | ||
| 560 | if (!(x & 0xf)) { | ||
| 561 | k += 4; | ||
| 562 | x >>= 4; | ||
| 563 | } | ||
| 564 | if (!(x & 0x3)) { | ||
| 565 | k += 2; | ||
| 566 | x >>= 2; | ||
| 567 | } | ||
| 568 | if (!(x & 1)) { | ||
| 569 | k++; | ||
| 570 | x >>= 1; | ||
| 571 | if (!x & 1) | ||
| 572 | return 32; | ||
| 573 | } | ||
| 574 | *y = x; | ||
| 575 | return k; | ||
| 576 | } | ||
| 577 | |||
| 578 | static Bigint * | ||
| 579 | i2b | ||
| 580 | #ifdef KR_headers | ||
| 581 | (i) int i; | ||
| 582 | #else | ||
| 583 | (int i) | ||
| 584 | #endif | ||
| 585 | { | ||
| 586 | Bigint *b; | ||
| 587 | |||
| 588 | b = Balloc(1); | ||
| 589 | b->x[0] = i; | ||
| 590 | b->wds = 1; | ||
| 591 | return b; | ||
| 592 | } | ||
| 593 | |||
| 594 | static Bigint * | ||
| 595 | mult | ||
| 596 | #ifdef KR_headers | ||
| 597 | (a, b) Bigint *a, *b; | ||
| 598 | #else | ||
| 599 | (Bigint *a, Bigint *b) | ||
| 600 | #endif | ||
| 601 | { | ||
| 602 | Bigint *c; | ||
| 603 | int k, wa, wb, wc; | ||
| 604 | ULong carry, y, z; | ||
| 605 | ULong *x, *xa, *xae, *xb, *xbe, *xc, *xc0; | ||
| 606 | #ifdef Pack_32 | ||
| 607 | ULong z2; | ||
| 608 | #endif | ||
| 609 | |||
| 610 | if (a->wds < b->wds) { | ||
| 611 | c = a; | ||
| 612 | a = b; | ||
| 613 | b = c; | ||
| 614 | } | ||
| 615 | k = a->k; | ||
| 616 | wa = a->wds; | ||
| 617 | wb = b->wds; | ||
| 618 | wc = wa + wb; | ||
| 619 | if (wc > a->maxwds) | ||
| 620 | k++; | ||
| 621 | c = Balloc(k); | ||
| 622 | for(x = c->x, xa = x + wc; x < xa; x++) | ||
| 623 | *x = 0; | ||
| 624 | xa = a->x; | ||
| 625 | xae = xa + wa; | ||
| 626 | xb = b->x; | ||
| 627 | xbe = xb + wb; | ||
| 628 | xc0 = c->x; | ||
| 629 | #ifdef Pack_32 | ||
| 630 | for(; xb < xbe; xb++, xc0++) { | ||
| 631 | if (y = *xb & 0xffff) { | ||
| 632 | x = xa; | ||
| 633 | xc = xc0; | ||
| 634 | carry = 0; | ||
| 635 | do { | ||
| 636 | z = (*x & 0xffff) * y + (*xc & 0xffff) + carry; | ||
| 637 | carry = z >> 16; | ||
| 638 | z2 = (*x++ >> 16) * y + (*xc >> 16) + carry; | ||
| 639 | carry = z2 >> 16; | ||
| 640 | Storeinc(xc, z2, z); | ||
| 641 | } | ||
| 642 | while(x < xae); | ||
| 643 | *xc = carry; | ||
| 644 | } | ||
| 645 | if (y = *xb >> 16) { | ||
| 646 | x = xa; | ||
| 647 | xc = xc0; | ||
| 648 | carry = 0; | ||
| 649 | z2 = *xc; | ||
| 650 | do { | ||
| 651 | z = (*x & 0xffff) * y + (*xc >> 16) + carry; | ||
| 652 | carry = z >> 16; | ||
| 653 | Storeinc(xc, z, z2); | ||
| 654 | z2 = (*x++ >> 16) * y + (*xc & 0xffff) + carry; | ||
| 655 | carry = z2 >> 16; | ||
| 656 | } | ||
| 657 | while(x < xae); | ||
| 658 | *xc = z2; | ||
| 659 | } | ||
| 660 | } | ||
| 661 | #else | ||
| 662 | for(; xb < xbe; xc0++) { | ||
| 663 | if (y = *xb++) { | ||
| 664 | x = xa; | ||
| 665 | xc = xc0; | ||
| 666 | carry = 0; | ||
| 667 | do { | ||
| 668 | z = *x++ * y + *xc + carry; | ||
| 669 | carry = z >> 16; | ||
| 670 | *xc++ = z & 0xffff; | ||
| 671 | } | ||
| 672 | while(x < xae); | ||
| 673 | *xc = carry; | ||
| 674 | } | ||
| 675 | } | ||
| 676 | #endif | ||
| 677 | for(xc0 = c->x, xc = xc0 + wc; wc > 0 && !*--xc; --wc) ; | ||
| 678 | c->wds = wc; | ||
| 679 | return c; | ||
| 680 | } | ||
| 681 | |||
| 682 | static Bigint *p5s; | ||
| 683 | |||
| 684 | static Bigint * | ||
| 685 | pow5mult | ||
| 686 | #ifdef KR_headers | ||
| 687 | (b, k) Bigint *b; int k; | ||
| 688 | #else | ||
| 689 | (Bigint *b, int k) | ||
| 690 | #endif | ||
| 691 | { | ||
| 692 | Bigint *b1, *p5, *p51; | ||
| 693 | int i; | ||
| 694 | static int p05[3] = { 5, 25, 125 }; | ||
| 695 | |||
| 696 | if (i = k & 3) | ||
| 697 | b = multadd(b, p05[i-1], 0); | ||
| 698 | |||
| 699 | if (!(k >>= 2)) | ||
| 700 | return b; | ||
| 701 | if (!(p5 = p5s)) { | ||
| 702 | /* first time */ | ||
| 703 | p5 = p5s = i2b(625); | ||
| 704 | p5->next = 0; | ||
| 705 | } | ||
| 706 | for(;;) { | ||
| 707 | if (k & 1) { | ||
| 708 | b1 = mult(b, p5); | ||
| 709 | Bfree(b); | ||
| 710 | b = b1; | ||
| 711 | } | ||
| 712 | if (!(k >>= 1)) | ||
| 713 | break; | ||
| 714 | if (!(p51 = p5->next)) { | ||
| 715 | p51 = p5->next = mult(p5,p5); | ||
| 716 | p51->next = 0; | ||
| 717 | } | ||
| 718 | p5 = p51; | ||
| 719 | } | ||
| 720 | return b; | ||
| 721 | } | ||
| 722 | |||
| 723 | static Bigint * | ||
| 724 | lshift | ||
| 725 | #ifdef KR_headers | ||
| 726 | (b, k) Bigint *b; int k; | ||
| 727 | #else | ||
| 728 | (Bigint *b, int k) | ||
| 729 | #endif | ||
| 730 | { | ||
| 731 | int i, k1, n, n1; | ||
| 732 | Bigint *b1; | ||
| 733 | ULong *x, *x1, *xe, z; | ||
| 734 | |||
| 735 | #ifdef Pack_32 | ||
| 736 | n = k >> 5; | ||
| 737 | #else | ||
| 738 | n = k >> 4; | ||
| 739 | #endif | ||
| 740 | k1 = b->k; | ||
| 741 | n1 = n + b->wds + 1; | ||
| 742 | for(i = b->maxwds; n1 > i; i <<= 1) | ||
| 743 | k1++; | ||
| 744 | b1 = Balloc(k1); | ||
| 745 | x1 = b1->x; | ||
| 746 | for(i = 0; i < n; i++) | ||
| 747 | *x1++ = 0; | ||
| 748 | x = b->x; | ||
| 749 | xe = x + b->wds; | ||
| 750 | #ifdef Pack_32 | ||
| 751 | if (k &= 0x1f) { | ||
| 752 | k1 = 32 - k; | ||
| 753 | z = 0; | ||
| 754 | do { | ||
| 755 | *x1++ = *x << k | z; | ||
| 756 | z = *x++ >> k1; | ||
| 757 | } | ||
| 758 | while(x < xe); | ||
| 759 | if (*x1 = z) | ||
| 760 | ++n1; | ||
| 761 | } | ||
| 762 | #else | ||
| 763 | if (k &= 0xf) { | ||
| 764 | k1 = 16 - k; | ||
| 765 | z = 0; | ||
| 766 | do { | ||
| 767 | *x1++ = *x << k & 0xffff | z; | ||
| 768 | z = *x++ >> k1; | ||
| 769 | } | ||
| 770 | while(x < xe); | ||
| 771 | if (*x1 = z) | ||
| 772 | ++n1; | ||
| 773 | } | ||
| 774 | #endif | ||
| 775 | else do | ||
| 776 | *x1++ = *x++; | ||
| 777 | while(x < xe); | ||
| 778 | b1->wds = n1 - 1; | ||
| 779 | Bfree(b); | ||
| 780 | return b1; | ||
| 781 | } | ||
| 782 | |||
| 783 | static int | ||
| 784 | cmp | ||
| 785 | #ifdef KR_headers | ||
| 786 | (a, b) Bigint *a, *b; | ||
| 787 | #else | ||
| 788 | (Bigint *a, Bigint *b) | ||
| 789 | #endif | ||
| 790 | { | ||
| 791 | ULong *xa, *xa0, *xb, *xb0; | ||
| 792 | int i, j; | ||
| 793 | |||
| 794 | i = a->wds; | ||
| 795 | j = b->wds; | ||
| 796 | #ifdef DEBUG | ||
| 797 | if (i > 1 && !a->x[i-1]) | ||
| 798 | Bug("cmp called with a->x[a->wds-1] == 0"); | ||
| 799 | if (j > 1 && !b->x[j-1]) | ||
| 800 | Bug("cmp called with b->x[b->wds-1] == 0"); | ||
| 801 | #endif | ||
| 802 | if (i -= j) | ||
| 803 | return i; | ||
| 804 | xa0 = a->x; | ||
| 805 | xa = xa0 + j; | ||
| 806 | xb0 = b->x; | ||
| 807 | xb = xb0 + j; | ||
| 808 | for(;;) { | ||
| 809 | if (*--xa != *--xb) | ||
| 810 | return *xa < *xb ? -1 : 1; | ||
| 811 | if (xa <= xa0) | ||
| 812 | break; | ||
| 813 | } | ||
| 814 | return 0; | ||
| 815 | } | ||
| 816 | |||
| 817 | static Bigint * | ||
| 818 | diff | ||
| 819 | #ifdef KR_headers | ||
| 820 | (a, b) Bigint *a, *b; | ||
| 821 | #else | ||
| 822 | (Bigint *a, Bigint *b) | ||
| 823 | #endif | ||
| 824 | { | ||
| 825 | Bigint *c; | ||
| 826 | int i, wa, wb; | ||
| 827 | Long borrow, y; /* We need signed shifts here. */ | ||
| 828 | ULong *xa, *xae, *xb, *xbe, *xc; | ||
| 829 | #ifdef Pack_32 | ||
| 830 | Long z; | ||
| 831 | #endif | ||
| 832 | |||
| 833 | i = cmp(a,b); | ||
| 834 | if (!i) { | ||
| 835 | c = Balloc(0); | ||
| 836 | c->wds = 1; | ||
| 837 | c->x[0] = 0; | ||
| 838 | return c; | ||
| 839 | } | ||
| 840 | if (i < 0) { | ||
| 841 | c = a; | ||
| 842 | a = b; | ||
| 843 | b = c; | ||
| 844 | i = 1; | ||
| 845 | } | ||
| 846 | else | ||
| 847 | i = 0; | ||
| 848 | c = Balloc(a->k); | ||
| 849 | c->sign = i; | ||
| 850 | wa = a->wds; | ||
| 851 | xa = a->x; | ||
| 852 | xae = xa + wa; | ||
| 853 | wb = b->wds; | ||
| 854 | xb = b->x; | ||
| 855 | xbe = xb + wb; | ||
| 856 | xc = c->x; | ||
| 857 | borrow = 0; | ||
| 858 | #ifdef Pack_32 | ||
| 859 | do { | ||
| 860 | y = (*xa & 0xffff) - (*xb & 0xffff) + borrow; | ||
| 861 | borrow = y >> 16; | ||
| 862 | Sign_Extend(borrow, y); | ||
| 863 | z = (*xa++ >> 16) - (*xb++ >> 16) + borrow; | ||
| 864 | borrow = z >> 16; | ||
| 865 | Sign_Extend(borrow, z); | ||
| 866 | Storeinc(xc, z, y); | ||
| 867 | } | ||
| 868 | while(xb < xbe); | ||
| 869 | while(xa < xae) { | ||
| 870 | y = (*xa & 0xffff) + borrow; | ||
| 871 | borrow = y >> 16; | ||
| 872 | Sign_Extend(borrow, y); | ||
| 873 | z = (*xa++ >> 16) + borrow; | ||
| 874 | borrow = z >> 16; | ||
| 875 | Sign_Extend(borrow, z); | ||
| 876 | Storeinc(xc, z, y); | ||
| 877 | } | ||
| 878 | #else | ||
| 879 | do { | ||
| 880 | y = *xa++ - *xb++ + borrow; | ||
| 881 | borrow = y >> 16; | ||
| 882 | Sign_Extend(borrow, y); | ||
| 883 | *xc++ = y & 0xffff; | ||
| 884 | } | ||
| 885 | while(xb < xbe); | ||
| 886 | while(xa < xae) { | ||
| 887 | y = *xa++ + borrow; | ||
| 888 | borrow = y >> 16; | ||
| 889 | Sign_Extend(borrow, y); | ||
| 890 | *xc++ = y & 0xffff; | ||
| 891 | } | ||
| 892 | #endif | ||
| 893 | while(!*--xc) | ||
| 894 | wa--; | ||
| 895 | c->wds = wa; | ||
| 896 | return c; | ||
| 897 | } | ||
| 898 | |||
| 899 | static double | ||
| 900 | ulp | ||
| 901 | #ifdef KR_headers | ||
| 902 | (x) double x; | ||
| 903 | #else | ||
| 904 | (double x) | ||
| 905 | #endif | ||
| 906 | { | ||
| 907 | register Long L; | ||
| 908 | double a; | ||
| 909 | |||
| 910 | L = (word0(x) & Exp_mask) - (P-1)*Exp_msk1; | ||
| 911 | #ifndef Sudden_Underflow | ||
| 912 | if (L > 0) { | ||
| 913 | #endif | ||
| 914 | #ifdef IBM | ||
| 915 | L |= Exp_msk1 >> 4; | ||
| 916 | #endif | ||
| 917 | word0(a) = L; | ||
| 918 | word1(a) = 0; | ||
| 919 | #ifndef Sudden_Underflow | ||
| 920 | } | ||
| 921 | else { | ||
| 922 | L = -L >> Exp_shift; | ||
| 923 | if (L < Exp_shift) { | ||
| 924 | word0(a) = 0x80000 >> L; | ||
| 925 | word1(a) = 0; | ||
| 926 | } | ||
| 927 | else { | ||
| 928 | word0(a) = 0; | ||
| 929 | L -= Exp_shift; | ||
| 930 | word1(a) = L >= 31 ? 1 : 1 << 31 - L; | ||
| 931 | } | ||
| 932 | } | ||
| 933 | #endif | ||
| 934 | return a; | ||
| 935 | } | ||
| 936 | |||
| 937 | static double | ||
| 938 | b2d | ||
| 939 | #ifdef KR_headers | ||
| 940 | (a, e) Bigint *a; int *e; | ||
| 941 | #else | ||
| 942 | (Bigint *a, int *e) | ||
| 943 | #endif | ||
| 944 | { | ||
| 945 | ULong *xa, *xa0, w, y, z; | ||
| 946 | int k; | ||
| 947 | double d; | ||
| 948 | #ifdef VAX | ||
| 949 | ULong d0, d1; | ||
| 950 | #else | ||
| 951 | #define d0 word0(d) | ||
| 952 | #define d1 word1(d) | ||
| 953 | #endif | ||
| 954 | |||
| 955 | xa0 = a->x; | ||
| 956 | xa = xa0 + a->wds; | ||
| 957 | y = *--xa; | ||
| 958 | #ifdef DEBUG | ||
| 959 | if (!y) Bug("zero y in b2d"); | ||
| 960 | #endif | ||
| 961 | k = hi0bits(y); | ||
| 962 | *e = 32 - k; | ||
| 963 | #ifdef Pack_32 | ||
| 964 | if (k < Ebits) { | ||
| 965 | d0 = Exp_1 | y >> Ebits - k; | ||
| 966 | w = xa > xa0 ? *--xa : 0; | ||
| 967 | d1 = y << (32-Ebits) + k | w >> Ebits - k; | ||
| 968 | goto ret_d; | ||
| 969 | } | ||
| 970 | z = xa > xa0 ? *--xa : 0; | ||
| 971 | if (k -= Ebits) { | ||
| 972 | d0 = Exp_1 | y << k | z >> 32 - k; | ||
| 973 | y = xa > xa0 ? *--xa : 0; | ||
| 974 | d1 = z << k | y >> 32 - k; | ||
| 975 | } | ||
| 976 | else { | ||
| 977 | d0 = Exp_1 | y; | ||
| 978 | d1 = z; | ||
| 979 | } | ||
| 980 | #else | ||
| 981 | if (k < Ebits + 16) { | ||
| 982 | z = xa > xa0 ? *--xa : 0; | ||
| 983 | d0 = Exp_1 | y << k - Ebits | z >> Ebits + 16 - k; | ||
| 984 | w = xa > xa0 ? *--xa : 0; | ||
| 985 | y = xa > xa0 ? *--xa : 0; | ||
| 986 | d1 = z << k + 16 - Ebits | w << k - Ebits | y >> 16 + Ebits - k; | ||
| 987 | goto ret_d; | ||
| 988 | } | ||
| 989 | z = xa > xa0 ? *--xa : 0; | ||
| 990 | w = xa > xa0 ? *--xa : 0; | ||
| 991 | k -= Ebits + 16; | ||
| 992 | d0 = Exp_1 | y << k + 16 | z << k | w >> 16 - k; | ||
| 993 | y = xa > xa0 ? *--xa : 0; | ||
| 994 | d1 = w << k + 16 | y << k; | ||
| 995 | #endif | ||
| 996 | ret_d: | ||
| 997 | #ifdef VAX | ||
| 998 | word0(d) = d0 >> 16 | d0 << 16; | ||
| 999 | word1(d) = d1 >> 16 | d1 << 16; | ||
| 1000 | #else | ||
| 1001 | #undef d0 | ||
| 1002 | #undef d1 | ||
| 1003 | #endif | ||
| 1004 | return d; | ||
| 1005 | } | ||
| 1006 | |||
| 1007 | static Bigint * | ||
| 1008 | d2b | ||
| 1009 | #ifdef KR_headers | ||
| 1010 | (d, e, bits) double d; int *e, *bits; | ||
| 1011 | #else | ||
| 1012 | (double d, int *e, int *bits) | ||
| 1013 | #endif | ||
| 1014 | { | ||
| 1015 | Bigint *b; | ||
| 1016 | int de, i, k; | ||
| 1017 | ULong *x, y, z; | ||
| 1018 | #ifdef VAX | ||
| 1019 | ULong d0, d1; | ||
| 1020 | d0 = word0(d) >> 16 | word0(d) << 16; | ||
| 1021 | d1 = word1(d) >> 16 | word1(d) << 16; | ||
| 1022 | #else | ||
| 1023 | #define d0 word0(d) | ||
| 1024 | #define d1 word1(d) | ||
| 1025 | #endif | ||
| 1026 | |||
| 1027 | #ifdef Pack_32 | ||
| 1028 | b = Balloc(1); | ||
| 1029 | #else | ||
| 1030 | b = Balloc(2); | ||
| 1031 | #endif | ||
| 1032 | x = b->x; | ||
| 1033 | |||
| 1034 | z = d0 & Frac_mask; | ||
| 1035 | d0 &= 0x7fffffff; /* clear sign bit, which we ignore */ | ||
| 1036 | #ifdef Sudden_Underflow | ||
| 1037 | de = (int)(d0 >> Exp_shift); | ||
| 1038 | #ifndef IBM | ||
| 1039 | z |= Exp_msk11; | ||
| 1040 | #endif | ||
| 1041 | #else | ||
| 1042 | if (de = (int)(d0 >> Exp_shift)) | ||
| 1043 | z |= Exp_msk1; | ||
| 1044 | #endif | ||
| 1045 | #ifdef Pack_32 | ||
| 1046 | if (y = d1) { | ||
| 1047 | if (k = lo0bits(&y)) { | ||
| 1048 | x[0] = y | z << 32 - k; | ||
| 1049 | z >>= k; | ||
| 1050 | } | ||
| 1051 | else | ||
| 1052 | x[0] = y; | ||
| 1053 | i = b->wds = (x[1] = z) ? 2 : 1; | ||
| 1054 | } | ||
| 1055 | else { | ||
| 1056 | #ifdef DEBUG | ||
| 1057 | if (!z) | ||
| 1058 | Bug("Zero passed to d2b"); | ||
| 1059 | #endif | ||
| 1060 | k = lo0bits(&z); | ||
| 1061 | x[0] = z; | ||
| 1062 | i = b->wds = 1; | ||
| 1063 | k += 32; | ||
| 1064 | } | ||
| 1065 | #else | ||
| 1066 | if (y = d1) { | ||
| 1067 | if (k = lo0bits(&y)) | ||
| 1068 | if (k >= 16) { | ||
| 1069 | x[0] = y | z << 32 - k & 0xffff; | ||
| 1070 | x[1] = z >> k - 16 & 0xffff; | ||
| 1071 | x[2] = z >> k; | ||
| 1072 | i = 2; | ||
| 1073 | } | ||
| 1074 | else { | ||
| 1075 | x[0] = y & 0xffff; | ||
| 1076 | x[1] = y >> 16 | z << 16 - k & 0xffff; | ||
| 1077 | x[2] = z >> k & 0xffff; | ||
| 1078 | x[3] = z >> k+16; | ||
| 1079 | i = 3; | ||
| 1080 | } | ||
| 1081 | else { | ||
| 1082 | x[0] = y & 0xffff; | ||
| 1083 | x[1] = y >> 16; | ||
| 1084 | x[2] = z & 0xffff; | ||
| 1085 | x[3] = z >> 16; | ||
| 1086 | i = 3; | ||
| 1087 | } | ||
| 1088 | } | ||
| 1089 | else { | ||
| 1090 | #ifdef DEBUG | ||
| 1091 | if (!z) | ||
| 1092 | Bug("Zero passed to d2b"); | ||
| 1093 | #endif | ||
| 1094 | k = lo0bits(&z); | ||
| 1095 | if (k >= 16) { | ||
| 1096 | x[0] = z; | ||
| 1097 | i = 0; | ||
| 1098 | } | ||
| 1099 | else { | ||
| 1100 | x[0] = z & 0xffff; | ||
| 1101 | x[1] = z >> 16; | ||
| 1102 | i = 1; | ||
| 1103 | } | ||
| 1104 | k += 32; | ||
| 1105 | } | ||
| 1106 | while(!x[i]) | ||
| 1107 | --i; | ||
| 1108 | b->wds = i + 1; | ||
| 1109 | #endif | ||
| 1110 | #ifndef Sudden_Underflow | ||
| 1111 | if (de) { | ||
| 1112 | #endif | ||
| 1113 | #ifdef IBM | ||
| 1114 | *e = (de - Bias - (P-1) << 2) + k; | ||
| 1115 | *bits = 4*P + 8 - k - hi0bits(word0(d) & Frac_mask); | ||
| 1116 | #else | ||
| 1117 | *e = de - Bias - (P-1) + k; | ||
| 1118 | *bits = P - k; | ||
| 1119 | #endif | ||
| 1120 | #ifndef Sudden_Underflow | ||
| 1121 | } | ||
| 1122 | else { | ||
| 1123 | *e = de - Bias - (P-1) + 1 + k; | ||
| 1124 | #ifdef Pack_32 | ||
| 1125 | *bits = 32*i - hi0bits(x[i-1]); | ||
| 1126 | #else | ||
| 1127 | *bits = (i+2)*16 - hi0bits(x[i]); | ||
| 1128 | #endif | ||
| 1129 | } | ||
| 1130 | #endif | ||
| 1131 | return b; | ||
| 1132 | } | ||
| 1133 | #undef d0 | ||
| 1134 | #undef d1 | ||
| 1135 | |||
| 1136 | static double | ||
| 1137 | ratio | ||
| 1138 | #ifdef KR_headers | ||
| 1139 | (a, b) Bigint *a, *b; | ||
| 1140 | #else | ||
| 1141 | (Bigint *a, Bigint *b) | ||
| 1142 | #endif | ||
| 1143 | { | ||
| 1144 | double da, db; | ||
| 1145 | int k, ka, kb; | ||
| 1146 | |||
| 1147 | da = b2d(a, &ka); | ||
| 1148 | db = b2d(b, &kb); | ||
| 1149 | #ifdef Pack_32 | ||
| 1150 | k = ka - kb + 32*(a->wds - b->wds); | ||
| 1151 | #else | ||
| 1152 | k = ka - kb + 16*(a->wds - b->wds); | ||
| 1153 | #endif | ||
| 1154 | #ifdef IBM | ||
| 1155 | if (k > 0) { | ||
| 1156 | word0(da) += (k >> 2)*Exp_msk1; | ||
| 1157 | if (k &= 3) | ||
| 1158 | da *= 1 << k; | ||
| 1159 | } | ||
| 1160 | else { | ||
| 1161 | k = -k; | ||
| 1162 | word0(db) += (k >> 2)*Exp_msk1; | ||
| 1163 | if (k &= 3) | ||
| 1164 | db *= 1 << k; | ||
| 1165 | } | ||
| 1166 | #else | ||
| 1167 | if (k > 0) | ||
| 1168 | word0(da) += k*Exp_msk1; | ||
| 1169 | else { | ||
| 1170 | k = -k; | ||
| 1171 | word0(db) += k*Exp_msk1; | ||
| 1172 | } | ||
| 1173 | #endif | ||
| 1174 | return da / db; | ||
| 1175 | } | ||
| 1176 | |||
| 1177 | static CONST double | ||
| 1178 | tens[] = { | ||
| 1179 | 1e0, 1e1, 1e2, 1e3, 1e4, 1e5, 1e6, 1e7, 1e8, 1e9, | ||
| 1180 | 1e10, 1e11, 1e12, 1e13, 1e14, 1e15, 1e16, 1e17, 1e18, 1e19, | ||
| 1181 | 1e20, 1e21, 1e22 | ||
| 1182 | #ifdef VAX | ||
| 1183 | , 1e23, 1e24 | ||
| 1184 | #endif | ||
| 1185 | }; | ||
| 1186 | |||
| 1187 | #ifdef IEEE_Arith | ||
| 1188 | static CONST double bigtens[] = { 1e16, 1e32, 1e64, 1e128, 1e256 }; | ||
| 1189 | static CONST double tinytens[] = { 1e-16, 1e-32, 1e-64, 1e-128, 1e-256 }; | ||
| 1190 | #define n_bigtens 5 | ||
| 1191 | #else | ||
| 1192 | #ifdef IBM | ||
| 1193 | static CONST double bigtens[] = { 1e16, 1e32, 1e64 }; | ||
| 1194 | static CONST double tinytens[] = { 1e-16, 1e-32, 1e-64 }; | ||
| 1195 | #define n_bigtens 3 | ||
| 1196 | #else | ||
| 1197 | static CONST double bigtens[] = { 1e16, 1e32 }; | ||
| 1198 | static CONST double tinytens[] = { 1e-16, 1e-32 }; | ||
| 1199 | #define n_bigtens 2 | ||
| 1200 | #endif | ||
| 1201 | #endif | ||
| 1202 | |||
| 1203 | double | ||
| 1204 | strtod | ||
| 1205 | #ifdef KR_headers | ||
| 1206 | (s00, se) CONST char *s00; char **se; | ||
| 1207 | #else | ||
| 1208 | (CONST char *s00, char **se) | ||
| 1209 | #endif | ||
| 1210 | { | ||
| 1211 | int bb2, bb5, bbe, bd2, bd5, bbbits, bs2, c, dsign, | ||
| 1212 | e, e1, esign, i, j, k, nd, nd0, nf, nz, nz0, sign; | ||
| 1213 | CONST char *s, *s0, *s1; | ||
| 1214 | double aadj, aadj1, adj, rv, rv0; | ||
| 1215 | Long L; | ||
| 1216 | ULong y, z; | ||
| 1217 | Bigint *bb, *bb1, *bd, *bd0, *bs, *delta; | ||
| 1218 | |||
| 1219 | #ifndef KR_headers | ||
| 1220 | CONST char decimal_point = localeconv()->decimal_point[0]; | ||
| 1221 | #else | ||
| 1222 | CONST char decimal_point = '.'; | ||
| 1223 | #endif | ||
| 1224 | |||
| 1225 | sign = nz0 = nz = 0; | ||
| 1226 | rv = 0.; | ||
| 1227 | |||
| 1228 | |||
| 1229 | for(s = s00; isspace(*s); s++) | ||
| 1230 | ; | ||
| 1231 | |||
| 1232 | if (*s == '-') { | ||
| 1233 | sign = 1; | ||
| 1234 | s++; | ||
| 1235 | } else if (*s == '+') { | ||
| 1236 | s++; | ||
| 1237 | } | ||
| 1238 | |||
| 1239 | if (*s == '\0') { | ||
| 1240 | s = s00; | ||
| 1241 | goto ret; | ||
| 1242 | } | ||
| 1243 | |||
| 1244 | if (*s == '0') { | ||
| 1245 | nz0 = 1; | ||
| 1246 | while(*++s == '0') ; | ||
| 1247 | if (!*s) | ||
| 1248 | goto ret; | ||
| 1249 | } | ||
| 1250 | s0 = s; | ||
| 1251 | y = z = 0; | ||
| 1252 | for(nd = nf = 0; (c = *s) >= '0' && c <= '9'; nd++, s++) | ||
| 1253 | if (nd < 9) | ||
| 1254 | y = 10*y + c - '0'; | ||
| 1255 | else if (nd < 16) | ||
| 1256 | z = 10*z + c - '0'; | ||
| 1257 | nd0 = nd; | ||
| 1258 | if (c == decimal_point) { | ||
| 1259 | c = *++s; | ||
| 1260 | if (!nd) { | ||
| 1261 | for(; c == '0'; c = *++s) | ||
| 1262 | nz++; | ||
| 1263 | if (c > '0' && c <= '9') { | ||
| 1264 | s0 = s; | ||
| 1265 | nf += nz; | ||
| 1266 | nz = 0; | ||
| 1267 | goto have_dig; | ||
| 1268 | } | ||
| 1269 | goto dig_done; | ||
| 1270 | } | ||
| 1271 | for(; c >= '0' && c <= '9'; c = *++s) { | ||
| 1272 | have_dig: | ||
| 1273 | nz++; | ||
| 1274 | if (c -= '0') { | ||
| 1275 | nf += nz; | ||
| 1276 | for(i = 1; i < nz; i++) | ||
| 1277 | if (nd++ < 9) | ||
| 1278 | y *= 10; | ||
| 1279 | else if (nd <= DBL_DIG + 1) | ||
| 1280 | z *= 10; | ||
| 1281 | if (nd++ < 9) | ||
| 1282 | y = 10*y + c; | ||
| 1283 | else if (nd <= DBL_DIG + 1) | ||
| 1284 | z = 10*z + c; | ||
| 1285 | nz = 0; | ||
| 1286 | } | ||
| 1287 | } | ||
| 1288 | } | ||
| 1289 | dig_done: | ||
| 1290 | e = 0; | ||
| 1291 | if (c == 'e' || c == 'E') { | ||
| 1292 | if (!nd && !nz && !nz0) { | ||
| 1293 | s = s00; | ||
| 1294 | goto ret; | ||
| 1295 | } | ||
| 1296 | s00 = s; | ||
| 1297 | esign = 0; | ||
| 1298 | switch(c = *++s) { | ||
| 1299 | case '-': | ||
| 1300 | esign = 1; | ||
| 1301 | case '+': | ||
| 1302 | c = *++s; | ||
| 1303 | } | ||
| 1304 | if (c >= '0' && c <= '9') { | ||
| 1305 | while(c == '0') | ||
| 1306 | c = *++s; | ||
| 1307 | if (c > '0' && c <= '9') { | ||
| 1308 | L = c - '0'; | ||
| 1309 | s1 = s; | ||
| 1310 | while((c = *++s) >= '0' && c <= '9') | ||
| 1311 | L = 10*L + c - '0'; | ||
| 1312 | if (s - s1 > 8 || L > 19999) | ||
| 1313 | /* Avoid confusion from exponents | ||
| 1314 | * so large that e might overflow. | ||
| 1315 | */ | ||
| 1316 | e = 19999; /* safe for 16 bit ints */ | ||
| 1317 | else | ||
| 1318 | e = (int)L; | ||
| 1319 | if (esign) | ||
| 1320 | e = -e; | ||
| 1321 | } | ||
| 1322 | else | ||
| 1323 | e = 0; | ||
| 1324 | } | ||
| 1325 | else | ||
| 1326 | s = s00; | ||
| 1327 | } | ||
| 1328 | if (!nd) { | ||
| 1329 | if (!nz && !nz0) | ||
| 1330 | s = s00; | ||
| 1331 | goto ret; | ||
| 1332 | } | ||
| 1333 | e1 = e -= nf; | ||
| 1334 | |||
| 1335 | /* Now we have nd0 digits, starting at s0, followed by a | ||
| 1336 | * decimal point, followed by nd-nd0 digits. The number we're | ||
| 1337 | * after is the integer represented by those digits times | ||
| 1338 | * 10**e */ | ||
| 1339 | |||
| 1340 | if (!nd0) | ||
| 1341 | nd0 = nd; | ||
| 1342 | k = nd < DBL_DIG + 1 ? nd : DBL_DIG + 1; | ||
| 1343 | rv = y; | ||
| 1344 | if (k > 9) | ||
| 1345 | rv = tens[k - 9] * rv + z; | ||
| 1346 | bd0 = 0; | ||
| 1347 | if (nd <= DBL_DIG | ||
| 1348 | #ifndef RND_PRODQUOT | ||
| 1349 | && FLT_ROUNDS == 1 | ||
| 1350 | #endif | ||
| 1351 | ) { | ||
| 1352 | if (!e) | ||
| 1353 | goto ret; | ||
| 1354 | if (e > 0) { | ||
| 1355 | if (e <= Ten_pmax) { | ||
| 1356 | #ifdef VAX | ||
| 1357 | goto vax_ovfl_check; | ||
| 1358 | #else | ||
| 1359 | /* rv = */ rounded_product(rv, tens[e]); | ||
| 1360 | goto ret; | ||
| 1361 | #endif | ||
| 1362 | } | ||
| 1363 | i = DBL_DIG - nd; | ||
| 1364 | if (e <= Ten_pmax + i) { | ||
| 1365 | /* A fancier test would sometimes let us do | ||
| 1366 | * this for larger i values. | ||
| 1367 | */ | ||
| 1368 | e -= i; | ||
| 1369 | rv *= tens[i]; | ||
| 1370 | #ifdef VAX | ||
| 1371 | /* VAX exponent range is so narrow we must | ||
| 1372 | * worry about overflow here... | ||
| 1373 | */ | ||
| 1374 | vax_ovfl_check: | ||
| 1375 | word0(rv) -= P*Exp_msk1; | ||
| 1376 | /* rv = */ rounded_product(rv, tens[e]); | ||
| 1377 | if ((word0(rv) & Exp_mask) | ||
| 1378 | > Exp_msk1*(DBL_MAX_EXP+Bias-1-P)) | ||
| 1379 | goto ovfl; | ||
| 1380 | word0(rv) += P*Exp_msk1; | ||
| 1381 | #else | ||
| 1382 | /* rv = */ rounded_product(rv, tens[e]); | ||
| 1383 | #endif | ||
| 1384 | goto ret; | ||
| 1385 | } | ||
| 1386 | } | ||
| 1387 | #ifndef Inaccurate_Divide | ||
| 1388 | else if (e >= -Ten_pmax) { | ||
| 1389 | /* rv = */ rounded_quotient(rv, tens[-e]); | ||
| 1390 | goto ret; | ||
| 1391 | } | ||
| 1392 | #endif | ||
| 1393 | } | ||
| 1394 | e1 += nd - k; | ||
| 1395 | |||
| 1396 | /* Get starting approximation = rv * 10**e1 */ | ||
| 1397 | |||
| 1398 | if (e1 > 0) { | ||
| 1399 | if (i = e1 & 15) | ||
| 1400 | rv *= tens[i]; | ||
| 1401 | if (e1 &= ~15) { | ||
| 1402 | if (e1 > DBL_MAX_10_EXP) { | ||
| 1403 | ovfl: | ||
| 1404 | errno = ERANGE; | ||
| 1405 | #ifdef __STDC__ | ||
| 1406 | rv = HUGE_VAL; | ||
| 1407 | #else | ||
| 1408 | /* Can't trust HUGE_VAL */ | ||
| 1409 | #ifdef IEEE_Arith | ||
| 1410 | word0(rv) = Exp_mask; | ||
| 1411 | word1(rv) = 0; | ||
| 1412 | #else | ||
| 1413 | word0(rv) = Big0; | ||
| 1414 | word1(rv) = Big1; | ||
| 1415 | #endif | ||
| 1416 | #endif | ||
| 1417 | if (bd0) | ||
| 1418 | goto retfree; | ||
| 1419 | goto ret; | ||
| 1420 | } | ||
| 1421 | if (e1 >>= 4) { | ||
| 1422 | for(j = 0; e1 > 1; j++, e1 >>= 1) | ||
| 1423 | if (e1 & 1) | ||
| 1424 | rv *= bigtens[j]; | ||
| 1425 | /* The last multiplication could overflow. */ | ||
| 1426 | word0(rv) -= P*Exp_msk1; | ||
| 1427 | rv *= bigtens[j]; | ||
| 1428 | if ((z = word0(rv) & Exp_mask) | ||
| 1429 | > Exp_msk1*(DBL_MAX_EXP+Bias-P)) | ||
| 1430 | goto ovfl; | ||
| 1431 | if (z > Exp_msk1*(DBL_MAX_EXP+Bias-1-P)) { | ||
| 1432 | /* set to largest number */ | ||
| 1433 | /* (Can't trust DBL_MAX) */ | ||
| 1434 | word0(rv) = Big0; | ||
| 1435 | word1(rv) = Big1; | ||
| 1436 | } | ||
| 1437 | else | ||
| 1438 | word0(rv) += P*Exp_msk1; | ||
| 1439 | } | ||
| 1440 | |||
| 1441 | } | ||
| 1442 | } | ||
| 1443 | else if (e1 < 0) { | ||
| 1444 | e1 = -e1; | ||
| 1445 | if (i = e1 & 15) | ||
| 1446 | rv /= tens[i]; | ||
| 1447 | if (e1 &= ~15) { | ||
| 1448 | e1 >>= 4; | ||
| 1449 | if (e1 >= 1 << n_bigtens) | ||
| 1450 | goto undfl; | ||
| 1451 | for(j = 0; e1 > 1; j++, e1 >>= 1) | ||
| 1452 | if (e1 & 1) | ||
| 1453 | rv *= tinytens[j]; | ||
| 1454 | /* The last multiplication could underflow. */ | ||
| 1455 | rv0 = rv; | ||
| 1456 | rv *= tinytens[j]; | ||
| 1457 | if (!rv) { | ||
| 1458 | rv = 2.*rv0; | ||
| 1459 | rv *= tinytens[j]; | ||
| 1460 | if (!rv) { | ||
| 1461 | undfl: | ||
| 1462 | rv = 0.; | ||
| 1463 | errno = ERANGE; | ||
| 1464 | if (bd0) | ||
| 1465 | goto retfree; | ||
| 1466 | goto ret; | ||
| 1467 | } | ||
| 1468 | word0(rv) = Tiny0; | ||
| 1469 | word1(rv) = Tiny1; | ||
| 1470 | /* The refinement below will clean | ||
| 1471 | * this approximation up. | ||
| 1472 | */ | ||
| 1473 | } | ||
| 1474 | } | ||
| 1475 | } | ||
| 1476 | |||
| 1477 | /* Now the hard part -- adjusting rv to the correct value.*/ | ||
| 1478 | |||
| 1479 | /* Put digits into bd: true value = bd * 10^e */ | ||
| 1480 | |||
| 1481 | bd0 = s2b(s0, nd0, nd, y); | ||
| 1482 | |||
| 1483 | for(;;) { | ||
| 1484 | bd = Balloc(bd0->k); | ||
| 1485 | Bcopy(bd, bd0); | ||
| 1486 | bb = d2b(rv, &bbe, &bbbits); /* rv = bb * 2^bbe */ | ||
| 1487 | bs = i2b(1); | ||
| 1488 | |||
| 1489 | if (e >= 0) { | ||
| 1490 | bb2 = bb5 = 0; | ||
| 1491 | bd2 = bd5 = e; | ||
| 1492 | } | ||
| 1493 | else { | ||
| 1494 | bb2 = bb5 = -e; | ||
| 1495 | bd2 = bd5 = 0; | ||
| 1496 | } | ||
| 1497 | if (bbe >= 0) | ||
| 1498 | bb2 += bbe; | ||
| 1499 | else | ||
| 1500 | bd2 -= bbe; | ||
| 1501 | bs2 = bb2; | ||
| 1502 | #ifdef Sudden_Underflow | ||
| 1503 | #ifdef IBM | ||
| 1504 | j = 1 + 4*P - 3 - bbbits + ((bbe + bbbits - 1) & 3); | ||
| 1505 | #else | ||
| 1506 | j = P + 1 - bbbits; | ||
| 1507 | #endif | ||
| 1508 | #else | ||
| 1509 | i = bbe + bbbits - 1; /* logb(rv) */ | ||
| 1510 | if (i < Emin) /* denormal */ | ||
| 1511 | j = bbe + (P-Emin); | ||
| 1512 | else | ||
| 1513 | j = P + 1 - bbbits; | ||
| 1514 | #endif | ||
| 1515 | bb2 += j; | ||
| 1516 | bd2 += j; | ||
| 1517 | i = bb2 < bd2 ? bb2 : bd2; | ||
| 1518 | if (i > bs2) | ||
| 1519 | i = bs2; | ||
| 1520 | if (i > 0) { | ||
| 1521 | bb2 -= i; | ||
| 1522 | bd2 -= i; | ||
| 1523 | bs2 -= i; | ||
| 1524 | } | ||
| 1525 | if (bb5 > 0) { | ||
| 1526 | bs = pow5mult(bs, bb5); | ||
| 1527 | bb1 = mult(bs, bb); | ||
| 1528 | Bfree(bb); | ||
| 1529 | bb = bb1; | ||
| 1530 | } | ||
| 1531 | if (bb2 > 0) | ||
| 1532 | bb = lshift(bb, bb2); | ||
| 1533 | if (bd5 > 0) | ||
| 1534 | bd = pow5mult(bd, bd5); | ||
| 1535 | if (bd2 > 0) | ||
| 1536 | bd = lshift(bd, bd2); | ||
| 1537 | if (bs2 > 0) | ||
| 1538 | bs = lshift(bs, bs2); | ||
| 1539 | delta = diff(bb, bd); | ||
| 1540 | dsign = delta->sign; | ||
| 1541 | delta->sign = 0; | ||
| 1542 | i = cmp(delta, bs); | ||
| 1543 | if (i < 0) { | ||
| 1544 | /* Error is less than half an ulp -- check for | ||
| 1545 | * special case of mantissa a power of two. | ||
| 1546 | */ | ||
| 1547 | if (dsign || word1(rv) || word0(rv) & Bndry_mask) | ||
| 1548 | break; | ||
| 1549 | delta = lshift(delta,Log2P); | ||
| 1550 | if (cmp(delta, bs) > 0) | ||
| 1551 | goto drop_down; | ||
| 1552 | break; | ||
| 1553 | } | ||
| 1554 | if (i == 0) { | ||
| 1555 | /* exactly half-way between */ | ||
| 1556 | if (dsign) { | ||
| 1557 | if ((word0(rv) & Bndry_mask1) == Bndry_mask1 | ||
| 1558 | && word1(rv) == 0xffffffff) { | ||
| 1559 | /*boundary case -- increment exponent*/ | ||
| 1560 | word0(rv) = (word0(rv) & Exp_mask) | ||
| 1561 | + Exp_msk1 | ||
| 1562 | #ifdef IBM | ||
| 1563 | | Exp_msk1 >> 4 | ||
| 1564 | #endif | ||
| 1565 | ; | ||
| 1566 | word1(rv) = 0; | ||
| 1567 | break; | ||
| 1568 | } | ||
| 1569 | } | ||
| 1570 | else if (!(word0(rv) & Bndry_mask) && !word1(rv)) { | ||
| 1571 | drop_down: | ||
| 1572 | /* boundary case -- decrement exponent */ | ||
| 1573 | #ifdef Sudden_Underflow | ||
| 1574 | L = word0(rv) & Exp_mask; | ||
| 1575 | #ifdef IBM | ||
| 1576 | if (L < Exp_msk1) | ||
| 1577 | #else | ||
| 1578 | if (L <= Exp_msk1) | ||
| 1579 | #endif | ||
| 1580 | goto undfl; | ||
| 1581 | L -= Exp_msk1; | ||
| 1582 | #else | ||
| 1583 | L = (word0(rv) & Exp_mask) - Exp_msk1; | ||
| 1584 | #endif | ||
| 1585 | word0(rv) = L | Bndry_mask1; | ||
| 1586 | word1(rv) = 0xffffffff; | ||
| 1587 | #ifdef IBM | ||
| 1588 | goto cont; | ||
| 1589 | #else | ||
| 1590 | break; | ||
| 1591 | #endif | ||
| 1592 | } | ||
| 1593 | #ifndef ROUND_BIASED | ||
| 1594 | if (!(word1(rv) & LSB)) | ||
| 1595 | break; | ||
| 1596 | #endif | ||
| 1597 | if (dsign) | ||
| 1598 | rv += ulp(rv); | ||
| 1599 | #ifndef ROUND_BIASED | ||
| 1600 | else { | ||
| 1601 | rv -= ulp(rv); | ||
| 1602 | #ifndef Sudden_Underflow | ||
| 1603 | if (!rv) | ||
| 1604 | goto undfl; | ||
| 1605 | #endif | ||
| 1606 | } | ||
| 1607 | #endif | ||
| 1608 | break; | ||
| 1609 | } | ||
| 1610 | if ((aadj = ratio(delta, bs)) <= 2.) { | ||
| 1611 | if (dsign) | ||
| 1612 | aadj = aadj1 = 1.; | ||
| 1613 | else if (word1(rv) || word0(rv) & Bndry_mask) { | ||
| 1614 | #ifndef Sudden_Underflow | ||
| 1615 | if (word1(rv) == Tiny1 && !word0(rv)) | ||
| 1616 | goto undfl; | ||
| 1617 | #endif | ||
| 1618 | aadj = 1.; | ||
| 1619 | aadj1 = -1.; | ||
| 1620 | } | ||
| 1621 | else { | ||
| 1622 | /* special case -- power of FLT_RADIX to be */ | ||
| 1623 | /* rounded down... */ | ||
| 1624 | |||
| 1625 | if (aadj < 2./FLT_RADIX) | ||
| 1626 | aadj = 1./FLT_RADIX; | ||
| 1627 | else | ||
| 1628 | aadj *= 0.5; | ||
| 1629 | aadj1 = -aadj; | ||
| 1630 | } | ||
| 1631 | } | ||
| 1632 | else { | ||
| 1633 | aadj *= 0.5; | ||
| 1634 | aadj1 = dsign ? aadj : -aadj; | ||
| 1635 | #ifdef Check_FLT_ROUNDS | ||
| 1636 | switch(FLT_ROUNDS) { | ||
| 1637 | case 2: /* towards +infinity */ | ||
| 1638 | aadj1 -= 0.5; | ||
| 1639 | break; | ||
| 1640 | case 0: /* towards 0 */ | ||
| 1641 | case 3: /* towards -infinity */ | ||
| 1642 | aadj1 += 0.5; | ||
| 1643 | } | ||
| 1644 | #else | ||
| 1645 | if (FLT_ROUNDS == 0) | ||
| 1646 | aadj1 += 0.5; | ||
| 1647 | #endif | ||
| 1648 | } | ||
| 1649 | y = word0(rv) & Exp_mask; | ||
| 1650 | |||
| 1651 | /* Check for overflow */ | ||
| 1652 | |||
| 1653 | if (y == Exp_msk1*(DBL_MAX_EXP+Bias-1)) { | ||
| 1654 | rv0 = rv; | ||
| 1655 | word0(rv) -= P*Exp_msk1; | ||
| 1656 | adj = aadj1 * ulp(rv); | ||
| 1657 | rv += adj; | ||
| 1658 | if ((word0(rv) & Exp_mask) >= | ||
| 1659 | Exp_msk1*(DBL_MAX_EXP+Bias-P)) { | ||
| 1660 | if (word0(rv0) == Big0 && word1(rv0) == Big1) | ||
| 1661 | goto ovfl; | ||
| 1662 | word0(rv) = Big0; | ||
| 1663 | word1(rv) = Big1; | ||
| 1664 | goto cont; | ||
| 1665 | } | ||
| 1666 | else | ||
| 1667 | word0(rv) += P*Exp_msk1; | ||
| 1668 | } | ||
| 1669 | else { | ||
| 1670 | #ifdef Sudden_Underflow | ||
| 1671 | if ((word0(rv) & Exp_mask) <= P*Exp_msk1) { | ||
| 1672 | rv0 = rv; | ||
| 1673 | word0(rv) += P*Exp_msk1; | ||
| 1674 | adj = aadj1 * ulp(rv); | ||
| 1675 | rv += adj; | ||
| 1676 | #ifdef IBM | ||
| 1677 | if ((word0(rv) & Exp_mask) < P*Exp_msk1) | ||
| 1678 | #else | ||
| 1679 | if ((word0(rv) & Exp_mask) <= P*Exp_msk1) | ||
| 1680 | #endif | ||
| 1681 | { | ||
| 1682 | if (word0(rv0) == Tiny0 | ||
| 1683 | && word1(rv0) == Tiny1) | ||
| 1684 | goto undfl; | ||
| 1685 | word0(rv) = Tiny0; | ||
| 1686 | word1(rv) = Tiny1; | ||
| 1687 | goto cont; | ||
| 1688 | } | ||
| 1689 | else | ||
| 1690 | word0(rv) -= P*Exp_msk1; | ||
| 1691 | } | ||
| 1692 | else { | ||
| 1693 | adj = aadj1 * ulp(rv); | ||
| 1694 | rv += adj; | ||
| 1695 | } | ||
| 1696 | #else | ||
| 1697 | /* Compute adj so that the IEEE rounding rules will | ||
| 1698 | * correctly round rv + adj in some half-way cases. | ||
| 1699 | * If rv * ulp(rv) is denormalized (i.e., | ||
| 1700 | * y <= (P-1)*Exp_msk1), we must adjust aadj to avoid | ||
| 1701 | * trouble from bits lost to denormalization; | ||
| 1702 | * example: 1.2e-307 . | ||
| 1703 | */ | ||
| 1704 | if (y <= (P-1)*Exp_msk1 && aadj >= 1.) { | ||
| 1705 | aadj1 = (double)(int)(aadj + 0.5); | ||
| 1706 | if (!dsign) | ||
| 1707 | aadj1 = -aadj1; | ||
| 1708 | } | ||
| 1709 | adj = aadj1 * ulp(rv); | ||
| 1710 | rv += adj; | ||
| 1711 | #endif | ||
| 1712 | } | ||
| 1713 | z = word0(rv) & Exp_mask; | ||
| 1714 | if (y == z) { | ||
| 1715 | /* Can we stop now? */ | ||
| 1716 | L = aadj; | ||
| 1717 | aadj -= L; | ||
| 1718 | /* The tolerances below are conservative. */ | ||
| 1719 | if (dsign || word1(rv) || word0(rv) & Bndry_mask) { | ||
| 1720 | if (aadj < .4999999 || aadj > .5000001) | ||
| 1721 | break; | ||
| 1722 | } | ||
| 1723 | else if (aadj < .4999999/FLT_RADIX) | ||
| 1724 | break; | ||
| 1725 | } | ||
| 1726 | cont: | ||
| 1727 | Bfree(bb); | ||
| 1728 | Bfree(bd); | ||
| 1729 | Bfree(bs); | ||
| 1730 | Bfree(delta); | ||
| 1731 | } | ||
| 1732 | retfree: | ||
| 1733 | Bfree(bb); | ||
| 1734 | Bfree(bd); | ||
| 1735 | Bfree(bs); | ||
| 1736 | Bfree(bd0); | ||
| 1737 | Bfree(delta); | ||
| 1738 | ret: | ||
| 1739 | if (se) | ||
| 1740 | *se = (char *)s; | ||
| 1741 | return sign ? -rv : rv; | ||
| 1742 | } | ||
| 1743 | |||
| 1744 | static int | ||
| 1745 | quorem | ||
| 1746 | #ifdef KR_headers | ||
| 1747 | (b, S) Bigint *b, *S; | ||
| 1748 | #else | ||
| 1749 | (Bigint *b, Bigint *S) | ||
| 1750 | #endif | ||
| 1751 | { | ||
| 1752 | int n; | ||
| 1753 | Long borrow, y; | ||
| 1754 | ULong carry, q, ys; | ||
| 1755 | ULong *bx, *bxe, *sx, *sxe; | ||
| 1756 | #ifdef Pack_32 | ||
| 1757 | Long z; | ||
| 1758 | ULong si, zs; | ||
| 1759 | #endif | ||
| 1760 | |||
| 1761 | n = S->wds; | ||
| 1762 | #ifdef DEBUG | ||
| 1763 | /*debug*/ if (b->wds > n) | ||
| 1764 | /*debug*/ Bug("oversize b in quorem"); | ||
| 1765 | #endif | ||
| 1766 | if (b->wds < n) | ||
| 1767 | return 0; | ||
| 1768 | sx = S->x; | ||
| 1769 | sxe = sx + --n; | ||
| 1770 | bx = b->x; | ||
| 1771 | bxe = bx + n; | ||
| 1772 | q = *bxe / (*sxe + 1); /* ensure q <= true quotient */ | ||
| 1773 | #ifdef DEBUG | ||
| 1774 | /*debug*/ if (q > 9) | ||
| 1775 | /*debug*/ Bug("oversized quotient in quorem"); | ||
| 1776 | #endif | ||
| 1777 | if (q) { | ||
| 1778 | borrow = 0; | ||
| 1779 | carry = 0; | ||
| 1780 | do { | ||
| 1781 | #ifdef Pack_32 | ||
| 1782 | si = *sx++; | ||
| 1783 | ys = (si & 0xffff) * q + carry; | ||
| 1784 | zs = (si >> 16) * q + (ys >> 16); | ||
| 1785 | carry = zs >> 16; | ||
| 1786 | y = (*bx & 0xffff) - (ys & 0xffff) + borrow; | ||
| 1787 | borrow = y >> 16; | ||
| 1788 | Sign_Extend(borrow, y); | ||
| 1789 | z = (*bx >> 16) - (zs & 0xffff) + borrow; | ||
| 1790 | borrow = z >> 16; | ||
| 1791 | Sign_Extend(borrow, z); | ||
| 1792 | Storeinc(bx, z, y); | ||
| 1793 | #else | ||
| 1794 | ys = *sx++ * q + carry; | ||
| 1795 | carry = ys >> 16; | ||
| 1796 | y = *bx - (ys & 0xffff) + borrow; | ||
| 1797 | borrow = y >> 16; | ||
| 1798 | Sign_Extend(borrow, y); | ||
| 1799 | *bx++ = y & 0xffff; | ||
| 1800 | #endif | ||
| 1801 | } | ||
| 1802 | while(sx <= sxe); | ||
| 1803 | if (!*bxe) { | ||
| 1804 | bx = b->x; | ||
| 1805 | while(--bxe > bx && !*bxe) | ||
| 1806 | --n; | ||
| 1807 | b->wds = n; | ||
| 1808 | } | ||
| 1809 | } | ||
| 1810 | if (cmp(b, S) >= 0) { | ||
| 1811 | q++; | ||
| 1812 | borrow = 0; | ||
| 1813 | carry = 0; | ||
| 1814 | bx = b->x; | ||
| 1815 | sx = S->x; | ||
| 1816 | do { | ||
| 1817 | #ifdef Pack_32 | ||
| 1818 | si = *sx++; | ||
| 1819 | ys = (si & 0xffff) + carry; | ||
| 1820 | zs = (si >> 16) + (ys >> 16); | ||
| 1821 | carry = zs >> 16; | ||
| 1822 | y = (*bx & 0xffff) - (ys & 0xffff) + borrow; | ||
| 1823 | borrow = y >> 16; | ||
| 1824 | Sign_Extend(borrow, y); | ||
| 1825 | z = (*bx >> 16) - (zs & 0xffff) + borrow; | ||
| 1826 | borrow = z >> 16; | ||
| 1827 | Sign_Extend(borrow, z); | ||
| 1828 | Storeinc(bx, z, y); | ||
| 1829 | #else | ||
| 1830 | ys = *sx++ + carry; | ||
| 1831 | carry = ys >> 16; | ||
| 1832 | y = *bx - (ys & 0xffff) + borrow; | ||
| 1833 | borrow = y >> 16; | ||
| 1834 | Sign_Extend(borrow, y); | ||
| 1835 | *bx++ = y & 0xffff; | ||
| 1836 | #endif | ||
| 1837 | } | ||
| 1838 | while(sx <= sxe); | ||
| 1839 | bx = b->x; | ||
| 1840 | bxe = bx + n; | ||
| 1841 | if (!*bxe) { | ||
| 1842 | while(--bxe > bx && !*bxe) | ||
| 1843 | --n; | ||
| 1844 | b->wds = n; | ||
| 1845 | } | ||
| 1846 | } | ||
| 1847 | return q; | ||
| 1848 | } | ||
| 1849 | |||
| 1850 | /* dtoa for IEEE arithmetic (dmg): convert double to ASCII string. | ||
| 1851 | * | ||
| 1852 | * Inspired by "How to Print Floating-Point Numbers Accurately" by | ||
| 1853 | * Guy L. Steele, Jr. and Jon L. White [Proc. ACM SIGPLAN '90, pp. 92-101]. | ||
| 1854 | * | ||
| 1855 | * Modifications: | ||
| 1856 | * 1. Rather than iterating, we use a simple numeric overestimate | ||
| 1857 | * to determine k = floor(log10(d)). We scale relevant | ||
| 1858 | * quantities using O(log2(k)) rather than O(k) multiplications. | ||
| 1859 | * 2. For some modes > 2 (corresponding to ecvt and fcvt), we don't | ||
| 1860 | * try to generate digits strictly left to right. Instead, we | ||
| 1861 | * compute with fewer bits and propagate the carry if necessary | ||
| 1862 | * when rounding the final digit up. This is often faster. | ||
| 1863 | * 3. Under the assumption that input will be rounded nearest, | ||
| 1864 | * mode 0 renders 1e23 as 1e23 rather than 9.999999999999999e22. | ||
| 1865 | * That is, we allow equality in stopping tests when the | ||
| 1866 | * round-nearest rule will give the same floating-point value | ||
| 1867 | * as would satisfaction of the stopping test with strict | ||
| 1868 | * inequality. | ||
| 1869 | * 4. We remove common factors of powers of 2 from relevant | ||
| 1870 | * quantities. | ||
| 1871 | * 5. When converting floating-point integers less than 1e16, | ||
| 1872 | * we use floating-point arithmetic rather than resorting | ||
| 1873 | * to multiple-precision integers. | ||
| 1874 | * 6. When asked to produce fewer than 15 digits, we first try | ||
| 1875 | * to get by with floating-point arithmetic; we resort to | ||
| 1876 | * multiple-precision integer arithmetic only if we cannot | ||
| 1877 | * guarantee that the floating-point calculation has given | ||
| 1878 | * the correctly rounded result. For k requested digits and | ||
| 1879 | * "uniformly" distributed input, the probability is | ||
| 1880 | * something like 10^(k-15) that we must resort to the Long | ||
| 1881 | * calculation. | ||
| 1882 | */ | ||
| 1883 | |||
| 1884 | char * | ||
| 1885 | __dtoa | ||
| 1886 | #ifdef KR_headers | ||
| 1887 | (d, mode, ndigits, decpt, sign, rve) | ||
| 1888 | double d; int mode, ndigits, *decpt, *sign; char **rve; | ||
| 1889 | #else | ||
| 1890 | (double d, int mode, int ndigits, int *decpt, int *sign, char **rve) | ||
| 1891 | #endif | ||
| 1892 | { | ||
| 1893 | /* Arguments ndigits, decpt, sign are similar to those | ||
| 1894 | of ecvt and fcvt; trailing zeros are suppressed from | ||
| 1895 | the returned string. If not null, *rve is set to point | ||
| 1896 | to the end of the return value. If d is +-Infinity or NaN, | ||
| 1897 | then *decpt is set to 9999. | ||
| 1898 | |||
| 1899 | mode: | ||
| 1900 | 0 ==> shortest string that yields d when read in | ||
| 1901 | and rounded to nearest. | ||
| 1902 | 1 ==> like 0, but with Steele & White stopping rule; | ||
| 1903 | e.g. with IEEE P754 arithmetic , mode 0 gives | ||
| 1904 | 1e23 whereas mode 1 gives 9.999999999999999e22. | ||
| 1905 | 2 ==> max(1,ndigits) significant digits. This gives a | ||
| 1906 | return value similar to that of ecvt, except | ||
| 1907 | that trailing zeros are suppressed. | ||
| 1908 | 3 ==> through ndigits past the decimal point. This | ||
| 1909 | gives a return value similar to that from fcvt, | ||
| 1910 | except that trailing zeros are suppressed, and | ||
| 1911 | ndigits can be negative. | ||
| 1912 | 4-9 should give the same return values as 2-3, i.e., | ||
| 1913 | 4 <= mode <= 9 ==> same return as mode | ||
| 1914 | 2 + (mode & 1). These modes are mainly for | ||
| 1915 | debugging; often they run slower but sometimes | ||
| 1916 | faster than modes 2-3. | ||
| 1917 | 4,5,8,9 ==> left-to-right digit generation. | ||
| 1918 | 6-9 ==> don't try fast floating-point estimate | ||
| 1919 | (if applicable). | ||
| 1920 | |||
| 1921 | Values of mode other than 0-9 are treated as mode 0. | ||
| 1922 | |||
| 1923 | Sufficient space is allocated to the return value | ||
| 1924 | to hold the suppressed trailing zeros. | ||
| 1925 | */ | ||
| 1926 | |||
| 1927 | int bbits, b2, b5, be, dig, i, ieps, ilim, ilim0, ilim1, | ||
| 1928 | j, j1, k, k0, k_check, leftright, m2, m5, s2, s5, | ||
| 1929 | spec_case, try_quick; | ||
| 1930 | Long L; | ||
| 1931 | #ifndef Sudden_Underflow | ||
| 1932 | int denorm; | ||
| 1933 | ULong x; | ||
| 1934 | #endif | ||
| 1935 | Bigint *b, *b1, *delta, *mlo, *mhi, *S; | ||
| 1936 | double d2, ds, eps; | ||
| 1937 | char *s, *s0; | ||
| 1938 | static Bigint *result; | ||
| 1939 | static int result_k; | ||
| 1940 | |||
| 1941 | if (result) { | ||
| 1942 | result->k = result_k; | ||
| 1943 | result->maxwds = 1 << result_k; | ||
| 1944 | Bfree(result); | ||
| 1945 | result = 0; | ||
| 1946 | } | ||
| 1947 | |||
| 1948 | if (word0(d) & Sign_bit) { | ||
| 1949 | /* set sign for everything, including 0's and NaNs */ | ||
| 1950 | *sign = 1; | ||
| 1951 | word0(d) &= ~Sign_bit; /* clear sign bit */ | ||
| 1952 | } | ||
| 1953 | else | ||
| 1954 | *sign = 0; | ||
| 1955 | |||
| 1956 | #if defined(IEEE_Arith) + defined(VAX) | ||
| 1957 | #ifdef IEEE_Arith | ||
| 1958 | if ((word0(d) & Exp_mask) == Exp_mask) | ||
| 1959 | #else | ||
| 1960 | if (word0(d) == 0x8000) | ||
| 1961 | #endif | ||
| 1962 | { | ||
| 1963 | /* Infinity or NaN */ | ||
| 1964 | *decpt = 9999; | ||
| 1965 | s = | ||
| 1966 | #ifdef IEEE_Arith | ||
| 1967 | !word1(d) && !(word0(d) & 0xfffff) ? "Infinity" : | ||
| 1968 | #endif | ||
| 1969 | "NaN"; | ||
| 1970 | if (rve) | ||
| 1971 | *rve = | ||
| 1972 | #ifdef IEEE_Arith | ||
| 1973 | s[3] ? s + 8 : | ||
| 1974 | #endif | ||
| 1975 | s + 3; | ||
| 1976 | return s; | ||
| 1977 | } | ||
| 1978 | #endif | ||
| 1979 | #ifdef IBM | ||
| 1980 | d += 0; /* normalize */ | ||
| 1981 | #endif | ||
| 1982 | if (!d) { | ||
| 1983 | *decpt = 1; | ||
| 1984 | s = "0"; | ||
| 1985 | if (rve) | ||
| 1986 | *rve = s + 1; | ||
| 1987 | return s; | ||
| 1988 | } | ||
| 1989 | |||
| 1990 | b = d2b(d, &be, &bbits); | ||
| 1991 | #ifdef Sudden_Underflow | ||
| 1992 | i = (int)(word0(d) >> Exp_shift1 & (Exp_mask>>Exp_shift1)); | ||
| 1993 | #else | ||
| 1994 | if (i = (int)(word0(d) >> Exp_shift1 & (Exp_mask>>Exp_shift1))) { | ||
| 1995 | #endif | ||
| 1996 | d2 = d; | ||
| 1997 | word0(d2) &= Frac_mask1; | ||
| 1998 | word0(d2) |= Exp_11; | ||
| 1999 | #ifdef IBM | ||
| 2000 | if (j = 11 - hi0bits(word0(d2) & Frac_mask)) | ||
| 2001 | d2 /= 1 << j; | ||
| 2002 | #endif | ||
| 2003 | |||
| 2004 | /* log(x) ~=~ log(1.5) + (x-1.5)/1.5 | ||
| 2005 | * log10(x) = log(x) / log(10) | ||
| 2006 | * ~=~ log(1.5)/log(10) + (x-1.5)/(1.5*log(10)) | ||
| 2007 | * log10(d) = (i-Bias)*log(2)/log(10) + log10(d2) | ||
| 2008 | * | ||
| 2009 | * This suggests computing an approximation k to log10(d) by | ||
| 2010 | * | ||
| 2011 | * k = (i - Bias)*0.301029995663981 | ||
| 2012 | * + ( (d2-1.5)*0.289529654602168 + 0.176091259055681 ); | ||
| 2013 | * | ||
| 2014 | * We want k to be too large rather than too small. | ||
| 2015 | * The error in the first-order Taylor series approximation | ||
| 2016 | * is in our favor, so we just round up the constant enough | ||
| 2017 | * to compensate for any error in the multiplication of | ||
| 2018 | * (i - Bias) by 0.301029995663981; since |i - Bias| <= 1077, | ||
| 2019 | * and 1077 * 0.30103 * 2^-52 ~=~ 7.2e-14, | ||
| 2020 | * adding 1e-13 to the constant term more than suffices. | ||
| 2021 | * Hence we adjust the constant term to 0.1760912590558. | ||
| 2022 | * (We could get a more accurate k by invoking log10, | ||
| 2023 | * but this is probably not worthwhile.) | ||
| 2024 | */ | ||
| 2025 | |||
| 2026 | i -= Bias; | ||
| 2027 | #ifdef IBM | ||
| 2028 | i <<= 2; | ||
| 2029 | i += j; | ||
| 2030 | #endif | ||
| 2031 | #ifndef Sudden_Underflow | ||
| 2032 | denorm = 0; | ||
| 2033 | } | ||
| 2034 | else { | ||
| 2035 | /* d is denormalized */ | ||
| 2036 | |||
| 2037 | i = bbits + be + (Bias + (P-1) - 1); | ||
| 2038 | x = i > 32 ? word0(d) << 64 - i | word1(d) >> i - 32 | ||
| 2039 | : word1(d) << 32 - i; | ||
| 2040 | d2 = x; | ||
| 2041 | word0(d2) -= 31*Exp_msk1; /* adjust exponent */ | ||
| 2042 | i -= (Bias + (P-1) - 1) + 1; | ||
| 2043 | denorm = 1; | ||
| 2044 | } | ||
| 2045 | #endif | ||
| 2046 | ds = (d2-1.5)*0.289529654602168 + 0.1760912590558 + i*0.301029995663981; | ||
| 2047 | k = (int)ds; | ||
| 2048 | if (ds < 0. && ds != k) | ||
| 2049 | k--; /* want k = floor(ds) */ | ||
| 2050 | k_check = 1; | ||
| 2051 | if (k >= 0 && k <= Ten_pmax) { | ||
| 2052 | if (d < tens[k]) | ||
| 2053 | k--; | ||
| 2054 | k_check = 0; | ||
| 2055 | } | ||
| 2056 | j = bbits - i - 1; | ||
| 2057 | if (j >= 0) { | ||
| 2058 | b2 = 0; | ||
| 2059 | s2 = j; | ||
| 2060 | } | ||
| 2061 | else { | ||
| 2062 | b2 = -j; | ||
| 2063 | s2 = 0; | ||
| 2064 | } | ||
| 2065 | if (k >= 0) { | ||
| 2066 | b5 = 0; | ||
| 2067 | s5 = k; | ||
| 2068 | s2 += k; | ||
| 2069 | } | ||
| 2070 | else { | ||
| 2071 | b2 -= k; | ||
| 2072 | b5 = -k; | ||
| 2073 | s5 = 0; | ||
| 2074 | } | ||
| 2075 | if (mode < 0 || mode > 9) | ||
| 2076 | mode = 0; | ||
| 2077 | try_quick = 1; | ||
| 2078 | if (mode > 5) { | ||
| 2079 | mode -= 4; | ||
| 2080 | try_quick = 0; | ||
| 2081 | } | ||
| 2082 | leftright = 1; | ||
| 2083 | switch(mode) { | ||
| 2084 | case 0: | ||
| 2085 | case 1: | ||
| 2086 | ilim = ilim1 = -1; | ||
| 2087 | i = 18; | ||
| 2088 | ndigits = 0; | ||
| 2089 | break; | ||
| 2090 | case 2: | ||
| 2091 | leftright = 0; | ||
| 2092 | /* no break */ | ||
| 2093 | case 4: | ||
| 2094 | if (ndigits <= 0) | ||
| 2095 | ndigits = 1; | ||
| 2096 | ilim = ilim1 = i = ndigits; | ||
| 2097 | break; | ||
| 2098 | case 3: | ||
| 2099 | leftright = 0; | ||
| 2100 | /* no break */ | ||
| 2101 | case 5: | ||
| 2102 | i = ndigits + k + 1; | ||
| 2103 | ilim = i; | ||
| 2104 | ilim1 = i - 1; | ||
| 2105 | if (i <= 0) | ||
| 2106 | i = 1; | ||
| 2107 | } | ||
| 2108 | j = sizeof(ULong); | ||
| 2109 | for(result_k = 0; sizeof(Bigint) - sizeof(ULong) + j <= i; | ||
| 2110 | j <<= 1) result_k++; | ||
| 2111 | result = Balloc(result_k); | ||
| 2112 | s = s0 = (char *)result; | ||
| 2113 | |||
| 2114 | if (ilim >= 0 && ilim <= Quick_max && try_quick) { | ||
| 2115 | |||
| 2116 | /* Try to get by with floating-point arithmetic. */ | ||
| 2117 | |||
| 2118 | i = 0; | ||
| 2119 | d2 = d; | ||
| 2120 | k0 = k; | ||
| 2121 | ilim0 = ilim; | ||
| 2122 | ieps = 2; /* conservative */ | ||
| 2123 | if (k > 0) { | ||
| 2124 | ds = tens[k&0xf]; | ||
| 2125 | j = k >> 4; | ||
| 2126 | if (j & Bletch) { | ||
| 2127 | /* prevent overflows */ | ||
| 2128 | j &= Bletch - 1; | ||
| 2129 | d /= bigtens[n_bigtens-1]; | ||
| 2130 | ieps++; | ||
| 2131 | } | ||
| 2132 | for(; j; j >>= 1, i++) | ||
| 2133 | if (j & 1) { | ||
| 2134 | ieps++; | ||
| 2135 | ds *= bigtens[i]; | ||
| 2136 | } | ||
| 2137 | d /= ds; | ||
| 2138 | } | ||
| 2139 | else if (j1 = -k) { | ||
| 2140 | d *= tens[j1 & 0xf]; | ||
| 2141 | for(j = j1 >> 4; j; j >>= 1, i++) | ||
| 2142 | if (j & 1) { | ||
| 2143 | ieps++; | ||
| 2144 | d *= bigtens[i]; | ||
| 2145 | } | ||
| 2146 | } | ||
| 2147 | if (k_check && d < 1. && ilim > 0) { | ||
| 2148 | if (ilim1 <= 0) | ||
| 2149 | goto fast_failed; | ||
| 2150 | ilim = ilim1; | ||
| 2151 | k--; | ||
| 2152 | d *= 10.; | ||
| 2153 | ieps++; | ||
| 2154 | } | ||
| 2155 | eps = ieps*d + 7.; | ||
| 2156 | word0(eps) -= (P-1)*Exp_msk1; | ||
| 2157 | if (ilim == 0) { | ||
| 2158 | S = mhi = 0; | ||
| 2159 | d -= 5.; | ||
| 2160 | if (d > eps) | ||
| 2161 | goto one_digit; | ||
| 2162 | if (d < -eps) | ||
| 2163 | goto no_digits; | ||
| 2164 | goto fast_failed; | ||
| 2165 | } | ||
| 2166 | #ifndef No_leftright | ||
| 2167 | if (leftright) { | ||
| 2168 | /* Use Steele & White method of only | ||
| 2169 | * generating digits needed. | ||
| 2170 | */ | ||
| 2171 | eps = 0.5/tens[ilim-1] - eps; | ||
| 2172 | for(i = 0;;) { | ||
| 2173 | L = d; | ||
| 2174 | d -= L; | ||
| 2175 | *s++ = '0' + (int)L; | ||
| 2176 | if (d < eps) | ||
| 2177 | goto ret1; | ||
| 2178 | if (1. - d < eps) | ||
| 2179 | goto bump_up; | ||
| 2180 | if (++i >= ilim) | ||
| 2181 | break; | ||
| 2182 | eps *= 10.; | ||
| 2183 | d *= 10.; | ||
| 2184 | } | ||
| 2185 | } | ||
| 2186 | else { | ||
| 2187 | #endif | ||
| 2188 | /* Generate ilim digits, then fix them up. */ | ||
| 2189 | eps *= tens[ilim-1]; | ||
| 2190 | for(i = 1;; i++, d *= 10.) { | ||
| 2191 | L = d; | ||
| 2192 | d -= L; | ||
| 2193 | *s++ = '0' + (int)L; | ||
| 2194 | if (i == ilim) { | ||
| 2195 | if (d > 0.5 + eps) | ||
| 2196 | goto bump_up; | ||
| 2197 | else if (d < 0.5 - eps) { | ||
| 2198 | while(*--s == '0'); | ||
| 2199 | s++; | ||
| 2200 | goto ret1; | ||
| 2201 | } | ||
| 2202 | break; | ||
| 2203 | } | ||
| 2204 | } | ||
| 2205 | #ifndef No_leftright | ||
| 2206 | } | ||
| 2207 | #endif | ||
| 2208 | fast_failed: | ||
| 2209 | s = s0; | ||
| 2210 | d = d2; | ||
| 2211 | k = k0; | ||
| 2212 | ilim = ilim0; | ||
| 2213 | } | ||
| 2214 | |||
| 2215 | /* Do we have a "small" integer? */ | ||
| 2216 | |||
| 2217 | if (be >= 0 && k <= Int_max) { | ||
| 2218 | /* Yes. */ | ||
| 2219 | ds = tens[k]; | ||
| 2220 | if (ndigits < 0 && ilim <= 0) { | ||
| 2221 | S = mhi = 0; | ||
| 2222 | if (ilim < 0 || d <= 5*ds) | ||
| 2223 | goto no_digits; | ||
| 2224 | goto one_digit; | ||
| 2225 | } | ||
| 2226 | for(i = 1;; i++) { | ||
| 2227 | L = d / ds; | ||
| 2228 | d -= L*ds; | ||
| 2229 | #ifdef Check_FLT_ROUNDS | ||
| 2230 | /* If FLT_ROUNDS == 2, L will usually be high by 1 */ | ||
| 2231 | if (d < 0) { | ||
| 2232 | L--; | ||
| 2233 | d += ds; | ||
| 2234 | } | ||
| 2235 | #endif | ||
| 2236 | *s++ = '0' + (int)L; | ||
| 2237 | if (i == ilim) { | ||
| 2238 | d += d; | ||
| 2239 | if (d > ds || d == ds && L & 1) { | ||
| 2240 | bump_up: | ||
| 2241 | while(*--s == '9') | ||
| 2242 | if (s == s0) { | ||
| 2243 | k++; | ||
| 2244 | *s = '0'; | ||
| 2245 | break; | ||
| 2246 | } | ||
| 2247 | ++*s++; | ||
| 2248 | } | ||
| 2249 | break; | ||
| 2250 | } | ||
| 2251 | if (!(d *= 10.)) | ||
| 2252 | break; | ||
| 2253 | } | ||
| 2254 | goto ret1; | ||
| 2255 | } | ||
| 2256 | |||
| 2257 | m2 = b2; | ||
| 2258 | m5 = b5; | ||
| 2259 | mhi = mlo = 0; | ||
| 2260 | if (leftright) { | ||
| 2261 | if (mode < 2) { | ||
| 2262 | i = | ||
| 2263 | #ifndef Sudden_Underflow | ||
| 2264 | denorm ? be + (Bias + (P-1) - 1 + 1) : | ||
| 2265 | #endif | ||
| 2266 | #ifdef IBM | ||
| 2267 | 1 + 4*P - 3 - bbits + ((bbits + be - 1) & 3); | ||
| 2268 | #else | ||
| 2269 | 1 + P - bbits; | ||
| 2270 | #endif | ||
| 2271 | } | ||
| 2272 | else { | ||
| 2273 | j = ilim - 1; | ||
| 2274 | if (m5 >= j) | ||
| 2275 | m5 -= j; | ||
| 2276 | else { | ||
| 2277 | s5 += j -= m5; | ||
| 2278 | b5 += j; | ||
| 2279 | m5 = 0; | ||
| 2280 | } | ||
| 2281 | if ((i = ilim) < 0) { | ||
| 2282 | m2 -= i; | ||
| 2283 | i = 0; | ||
| 2284 | } | ||
| 2285 | } | ||
| 2286 | b2 += i; | ||
| 2287 | s2 += i; | ||
| 2288 | mhi = i2b(1); | ||
| 2289 | } | ||
| 2290 | if (m2 > 0 && s2 > 0) { | ||
| 2291 | i = m2 < s2 ? m2 : s2; | ||
| 2292 | b2 -= i; | ||
| 2293 | m2 -= i; | ||
| 2294 | s2 -= i; | ||
| 2295 | } | ||
| 2296 | if (b5 > 0) { | ||
| 2297 | if (leftright) { | ||
| 2298 | if (m5 > 0) { | ||
| 2299 | mhi = pow5mult(mhi, m5); | ||
| 2300 | b1 = mult(mhi, b); | ||
| 2301 | Bfree(b); | ||
| 2302 | b = b1; | ||
| 2303 | } | ||
| 2304 | if (j = b5 - m5) | ||
| 2305 | b = pow5mult(b, j); | ||
| 2306 | } | ||
| 2307 | else | ||
| 2308 | b = pow5mult(b, b5); | ||
| 2309 | } | ||
| 2310 | S = i2b(1); | ||
| 2311 | if (s5 > 0) | ||
| 2312 | S = pow5mult(S, s5); | ||
| 2313 | |||
| 2314 | /* Check for special case that d is a normalized power of 2. */ | ||
| 2315 | |||
| 2316 | if (mode < 2) { | ||
| 2317 | if (!word1(d) && !(word0(d) & Bndry_mask) | ||
| 2318 | #ifndef Sudden_Underflow | ||
| 2319 | && word0(d) & Exp_mask | ||
| 2320 | #endif | ||
| 2321 | ) { | ||
| 2322 | /* The special case */ | ||
| 2323 | b2 += Log2P; | ||
| 2324 | s2 += Log2P; | ||
| 2325 | spec_case = 1; | ||
| 2326 | } | ||
| 2327 | else | ||
| 2328 | spec_case = 0; | ||
| 2329 | } | ||
| 2330 | |||
| 2331 | /* Arrange for convenient computation of quotients: | ||
| 2332 | * shift left if necessary so divisor has 4 leading 0 bits. | ||
| 2333 | * | ||
| 2334 | * Perhaps we should just compute leading 28 bits of S once | ||
| 2335 | * and for all and pass them and a shift to quorem, so it | ||
| 2336 | * can do shifts and ors to compute the numerator for q. | ||
| 2337 | */ | ||
| 2338 | #ifdef Pack_32 | ||
| 2339 | if (i = ((s5 ? 32 - hi0bits(S->x[S->wds-1]) : 1) + s2) & 0x1f) | ||
| 2340 | i = 32 - i; | ||
| 2341 | #else | ||
| 2342 | if (i = ((s5 ? 32 - hi0bits(S->x[S->wds-1]) : 1) + s2) & 0xf) | ||
| 2343 | i = 16 - i; | ||
| 2344 | #endif | ||
| 2345 | if (i > 4) { | ||
| 2346 | i -= 4; | ||
| 2347 | b2 += i; | ||
| 2348 | m2 += i; | ||
| 2349 | s2 += i; | ||
| 2350 | } | ||
| 2351 | else if (i < 4) { | ||
| 2352 | i += 28; | ||
| 2353 | b2 += i; | ||
| 2354 | m2 += i; | ||
| 2355 | s2 += i; | ||
| 2356 | } | ||
| 2357 | if (b2 > 0) | ||
| 2358 | b = lshift(b, b2); | ||
| 2359 | if (s2 > 0) | ||
| 2360 | S = lshift(S, s2); | ||
| 2361 | if (k_check) { | ||
| 2362 | if (cmp(b,S) < 0) { | ||
| 2363 | k--; | ||
| 2364 | b = multadd(b, 10, 0); /* we botched the k estimate */ | ||
| 2365 | if (leftright) | ||
| 2366 | mhi = multadd(mhi, 10, 0); | ||
| 2367 | ilim = ilim1; | ||
| 2368 | } | ||
| 2369 | } | ||
| 2370 | if (ilim <= 0 && mode > 2) { | ||
| 2371 | if (ilim < 0 || cmp(b,S = multadd(S,5,0)) <= 0) { | ||
| 2372 | /* no digits, fcvt style */ | ||
| 2373 | no_digits: | ||
| 2374 | k = -1 - ndigits; | ||
| 2375 | goto ret; | ||
| 2376 | } | ||
| 2377 | one_digit: | ||
| 2378 | *s++ = '1'; | ||
| 2379 | k++; | ||
| 2380 | goto ret; | ||
| 2381 | } | ||
| 2382 | if (leftright) { | ||
| 2383 | if (m2 > 0) | ||
| 2384 | mhi = lshift(mhi, m2); | ||
| 2385 | |||
| 2386 | /* Compute mlo -- check for special case | ||
| 2387 | * that d is a normalized power of 2. | ||
| 2388 | */ | ||
| 2389 | |||
| 2390 | mlo = mhi; | ||
| 2391 | if (spec_case) { | ||
| 2392 | mhi = Balloc(mhi->k); | ||
| 2393 | Bcopy(mhi, mlo); | ||
| 2394 | mhi = lshift(mhi, Log2P); | ||
| 2395 | } | ||
| 2396 | |||
| 2397 | for(i = 1;;i++) { | ||
| 2398 | dig = quorem(b,S) + '0'; | ||
| 2399 | /* Do we yet have the shortest decimal string | ||
| 2400 | * that will round to d? | ||
| 2401 | */ | ||
| 2402 | j = cmp(b, mlo); | ||
| 2403 | delta = diff(S, mhi); | ||
| 2404 | j1 = delta->sign ? 1 : cmp(b, delta); | ||
| 2405 | Bfree(delta); | ||
| 2406 | #ifndef ROUND_BIASED | ||
| 2407 | if (j1 == 0 && !mode && !(word1(d) & 1)) { | ||
| 2408 | if (dig == '9') | ||
| 2409 | goto round_9_up; | ||
| 2410 | if (j > 0) | ||
| 2411 | dig++; | ||
| 2412 | *s++ = dig; | ||
| 2413 | goto ret; | ||
| 2414 | } | ||
| 2415 | #endif | ||
| 2416 | if (j < 0 || j == 0 && !mode | ||
| 2417 | #ifndef ROUND_BIASED | ||
| 2418 | && !(word1(d) & 1) | ||
| 2419 | #endif | ||
| 2420 | ) { | ||
| 2421 | if (j1 > 0) { | ||
| 2422 | b = lshift(b, 1); | ||
| 2423 | j1 = cmp(b, S); | ||
| 2424 | if ((j1 > 0 || j1 == 0 && dig & 1) | ||
| 2425 | && dig++ == '9') | ||
| 2426 | goto round_9_up; | ||
| 2427 | } | ||
| 2428 | *s++ = dig; | ||
| 2429 | goto ret; | ||
| 2430 | } | ||
| 2431 | if (j1 > 0) { | ||
| 2432 | if (dig == '9') { /* possible if i == 1 */ | ||
| 2433 | round_9_up: | ||
| 2434 | *s++ = '9'; | ||
| 2435 | goto roundoff; | ||
| 2436 | } | ||
| 2437 | *s++ = dig + 1; | ||
| 2438 | goto ret; | ||
| 2439 | } | ||
| 2440 | *s++ = dig; | ||
| 2441 | if (i == ilim) | ||
| 2442 | break; | ||
| 2443 | b = multadd(b, 10, 0); | ||
| 2444 | if (mlo == mhi) | ||
| 2445 | mlo = mhi = multadd(mhi, 10, 0); | ||
| 2446 | else { | ||
| 2447 | mlo = multadd(mlo, 10, 0); | ||
| 2448 | mhi = multadd(mhi, 10, 0); | ||
| 2449 | } | ||
| 2450 | } | ||
| 2451 | } | ||
| 2452 | else | ||
| 2453 | for(i = 1;; i++) { | ||
| 2454 | *s++ = dig = quorem(b,S) + '0'; | ||
| 2455 | if (i >= ilim) | ||
| 2456 | break; | ||
| 2457 | b = multadd(b, 10, 0); | ||
| 2458 | } | ||
| 2459 | |||
| 2460 | /* Round off last digit */ | ||
| 2461 | |||
| 2462 | b = lshift(b, 1); | ||
| 2463 | j = cmp(b, S); | ||
| 2464 | if (j > 0 || j == 0 && dig & 1) { | ||
| 2465 | roundoff: | ||
| 2466 | while(*--s == '9') | ||
| 2467 | if (s == s0) { | ||
| 2468 | k++; | ||
| 2469 | *s++ = '1'; | ||
| 2470 | goto ret; | ||
| 2471 | } | ||
| 2472 | ++*s++; | ||
| 2473 | } | ||
| 2474 | else { | ||
| 2475 | while(*--s == '0'); | ||
| 2476 | s++; | ||
| 2477 | } | ||
| 2478 | ret: | ||
| 2479 | Bfree(S); | ||
| 2480 | if (mhi) { | ||
| 2481 | if (mlo && mlo != mhi) | ||
| 2482 | Bfree(mlo); | ||
| 2483 | Bfree(mhi); | ||
| 2484 | } | ||
| 2485 | ret1: | ||
| 2486 | Bfree(b); | ||
| 2487 | if (s == s0) { /* don't return empty string */ | ||
| 2488 | *s++ = '0'; | ||
| 2489 | k = 0; | ||
| 2490 | } | ||
| 2491 | *s = 0; | ||
| 2492 | *decpt = k + 1; | ||
| 2493 | if (rve) | ||
| 2494 | *rve = s; | ||
| 2495 | return s0; | ||
| 2496 | } | ||
| 2497 | #ifdef __cplusplus | ||
| 2498 | } | ||
| 2499 | #endif | ||
diff --git a/src/lib/libc/stdlib/strtol.3 b/src/lib/libc/stdlib/strtol.3 new file mode 100644 index 0000000000..808ba90165 --- /dev/null +++ b/src/lib/libc/stdlib/strtol.3 | |||
| @@ -0,0 +1,169 @@ | |||
| 1 | .\" Copyright (c) 1990, 1991 The Regents of the University of California. | ||
| 2 | .\" All rights reserved. | ||
| 3 | .\" | ||
| 4 | .\" This code is derived from software contributed to Berkeley by | ||
| 5 | .\" Chris Torek and the American National Standards Committee X3, | ||
| 6 | .\" on Information Processing Systems. | ||
| 7 | .\" | ||
| 8 | .\" Redistribution and use in source and binary forms, with or without | ||
| 9 | .\" modification, are permitted provided that the following conditions | ||
| 10 | .\" are met: | ||
| 11 | .\" 1. Redistributions of source code must retain the above copyright | ||
| 12 | .\" notice, this list of conditions and the following disclaimer. | ||
| 13 | .\" 2. Redistributions in binary form must reproduce the above copyright | ||
| 14 | .\" notice, this list of conditions and the following disclaimer in the | ||
| 15 | .\" documentation and/or other materials provided with the distribution. | ||
| 16 | .\" 3. All advertising materials mentioning features or use of this software | ||
| 17 | .\" must display the following acknowledgement: | ||
| 18 | .\" This product includes software developed by the University of | ||
| 19 | .\" California, Berkeley and its contributors. | ||
| 20 | .\" 4. Neither the name of the University nor the names of its contributors | ||
| 21 | .\" may be used to endorse or promote products derived from this software | ||
| 22 | .\" without specific prior written permission. | ||
| 23 | .\" | ||
| 24 | .\" THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND | ||
| 25 | .\" ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE | ||
| 26 | .\" IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE | ||
| 27 | .\" ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE | ||
| 28 | .\" FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL | ||
| 29 | .\" DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS | ||
| 30 | .\" OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) | ||
| 31 | .\" HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT | ||
| 32 | .\" LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY | ||
| 33 | .\" OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF | ||
| 34 | .\" SUCH DAMAGE. | ||
| 35 | .\" | ||
| 36 | .\" from: @(#)strtol.3 5.4 (Berkeley) 6/25/92 | ||
| 37 | .\" $Id: strtol.3,v 1.1.1.1 1995/10/18 08:42:19 deraadt Exp $ | ||
| 38 | .\" | ||
| 39 | .Dd June 25, 1992 | ||
| 40 | .Dt STRTOL 3 | ||
| 41 | .Os | ||
| 42 | .Sh NAME | ||
| 43 | .Nm strtol, strtoq | ||
| 44 | .Nd convert string value to a long or quad_t integer | ||
| 45 | .Sh SYNOPSIS | ||
| 46 | .Fd #include <stdlib.h> | ||
| 47 | .Fd #include <limits.h> | ||
| 48 | .Ft long | ||
| 49 | .Fn strtol "char *nptr" "char **endptr" "int base" | ||
| 50 | |||
| 51 | .Fd #include <sys/types.h> | ||
| 52 | .Fd #include <stdlib.h> | ||
| 53 | .Fd #include <limits.h> | ||
| 54 | .Ft quad_t | ||
| 55 | .Fn strtoq "char *nptr" "char **endptr" "int base" | ||
| 56 | .Sh DESCRIPTION | ||
| 57 | The | ||
| 58 | .Fn strtol | ||
| 59 | function | ||
| 60 | converts the string in | ||
| 61 | .Fa nptr | ||
| 62 | to a | ||
| 63 | .Em long | ||
| 64 | value. | ||
| 65 | The | ||
| 66 | .Fn strtoq | ||
| 67 | function | ||
| 68 | converts the string in | ||
| 69 | .Fa nptr | ||
| 70 | to a | ||
| 71 | .Em quad_t | ||
| 72 | value. | ||
| 73 | The conversion is done according to the given | ||
| 74 | .Fa base , | ||
| 75 | which must be between 2 and 36 inclusive, | ||
| 76 | or be the special value 0. | ||
| 77 | .Pp | ||
| 78 | The string may begin with an arbitrary amount of white space | ||
| 79 | (as determined by | ||
| 80 | .Xr isspace 3 ) | ||
| 81 | followed by a single optional | ||
| 82 | .Ql + | ||
| 83 | or | ||
| 84 | .Ql - | ||
| 85 | sign. | ||
| 86 | If | ||
| 87 | .Fa base | ||
| 88 | is zero or 16, | ||
| 89 | the string may then include a | ||
| 90 | .Ql 0x | ||
| 91 | prefix, | ||
| 92 | and the number will be read in base 16; otherwise, a zero | ||
| 93 | .Fa base | ||
| 94 | is taken as 10 (decimal) unless the next character is | ||
| 95 | .Ql 0 , | ||
| 96 | in which case it is taken as 8 (octal). | ||
| 97 | .Pp | ||
| 98 | The remainder of the string is converted to a | ||
| 99 | .Em long | ||
| 100 | value in the obvious manner, | ||
| 101 | stopping at the first character which is not a valid digit | ||
| 102 | in the given base. | ||
| 103 | (In bases above 10, the letter | ||
| 104 | .Ql A | ||
| 105 | in either upper or lower case | ||
| 106 | represents 10, | ||
| 107 | .Ql B | ||
| 108 | represents 11, and so forth, with | ||
| 109 | .Ql Z | ||
| 110 | representing 35.) | ||
| 111 | .Pp | ||
| 112 | If | ||
| 113 | .Fa endptr | ||
| 114 | is non nil, | ||
| 115 | .Fn strtol | ||
| 116 | stores the address of the first invalid character in | ||
| 117 | .Fa *endptr . | ||
| 118 | If there were no digits at all, however, | ||
| 119 | .Fn strtol | ||
| 120 | stores the original value of | ||
| 121 | .Fa nptr | ||
| 122 | in | ||
| 123 | .Fa *endptr . | ||
| 124 | (Thus, if | ||
| 125 | .Fa *nptr | ||
| 126 | is not | ||
| 127 | .Ql \e0 | ||
| 128 | but | ||
| 129 | .Fa **endptr | ||
| 130 | is | ||
| 131 | .Ql \e0 | ||
| 132 | on return, the entire string was valid.) | ||
| 133 | .Sh RETURN VALUES | ||
| 134 | The | ||
| 135 | .Fn strtol | ||
| 136 | function | ||
| 137 | returns the result of the conversion, | ||
| 138 | unless the value would underflow or overflow. | ||
| 139 | If an underflow occurs, | ||
| 140 | .Fn strtol | ||
| 141 | returns | ||
| 142 | .Dv LONG_MIN . | ||
| 143 | If an overflow occurs, | ||
| 144 | .Fn strtol | ||
| 145 | returns | ||
| 146 | .Dv LONG_MAX . | ||
| 147 | In both cases, | ||
| 148 | .Va errno | ||
| 149 | is set to | ||
| 150 | .Er ERANGE . | ||
| 151 | .Sh ERRORS | ||
| 152 | .Bl -tag -width Er | ||
| 153 | .It Bq Er ERANGE | ||
| 154 | The given string was out of range; the value converted has been clamped. | ||
| 155 | .El | ||
| 156 | .Sh SEE ALSO | ||
| 157 | .Xr atof 3 , | ||
| 158 | .Xr atoi 3 , | ||
| 159 | .Xr atol 3 , | ||
| 160 | .Xr strtod 3 , | ||
| 161 | .Xr strtoul 3 | ||
| 162 | .Sh STANDARDS | ||
| 163 | The | ||
| 164 | .Fn strtol | ||
| 165 | function | ||
| 166 | conforms to | ||
| 167 | .St -ansiC . | ||
| 168 | .Sh BUGS | ||
| 169 | Ignores the current locale. | ||
diff --git a/src/lib/libc/stdlib/strtol.c b/src/lib/libc/stdlib/strtol.c new file mode 100644 index 0000000000..6f374abd5f --- /dev/null +++ b/src/lib/libc/stdlib/strtol.c | |||
| @@ -0,0 +1,130 @@ | |||
| 1 | /*- | ||
| 2 | * Copyright (c) 1990 The Regents of the University of California. | ||
| 3 | * All rights reserved. | ||
| 4 | * | ||
| 5 | * Redistribution and use in source and binary forms, with or without | ||
| 6 | * modification, are permitted provided that the following conditions | ||
| 7 | * are met: | ||
| 8 | * 1. Redistributions of source code must retain the above copyright | ||
| 9 | * notice, this list of conditions and the following disclaimer. | ||
| 10 | * 2. Redistributions in binary form must reproduce the above copyright | ||
| 11 | * notice, this list of conditions and the following disclaimer in the | ||
| 12 | * documentation and/or other materials provided with the distribution. | ||
| 13 | * 3. All advertising materials mentioning features or use of this software | ||
| 14 | * must display the following acknowledgement: | ||
| 15 | * This product includes software developed by the University of | ||
| 16 | * California, Berkeley and its contributors. | ||
| 17 | * 4. Neither the name of the University nor the names of its contributors | ||
| 18 | * may be used to endorse or promote products derived from this software | ||
| 19 | * without specific prior written permission. | ||
| 20 | * | ||
| 21 | * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND | ||
| 22 | * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE | ||
| 23 | * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE | ||
| 24 | * ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE | ||
| 25 | * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL | ||
| 26 | * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS | ||
| 27 | * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) | ||
| 28 | * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT | ||
| 29 | * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY | ||
| 30 | * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF | ||
| 31 | * SUCH DAMAGE. | ||
| 32 | */ | ||
| 33 | |||
| 34 | #if defined(LIBC_SCCS) && !defined(lint) | ||
| 35 | /*static char *sccsid = "from: @(#)strtol.c 5.4 (Berkeley) 2/23/91";*/ | ||
| 36 | static char *rcsid = "$Id: strtol.c,v 1.1.1.1 1995/10/18 08:42:20 deraadt Exp $"; | ||
| 37 | #endif /* LIBC_SCCS and not lint */ | ||
| 38 | |||
| 39 | #include <limits.h> | ||
| 40 | #include <ctype.h> | ||
| 41 | #include <errno.h> | ||
| 42 | #include <stdlib.h> | ||
| 43 | |||
| 44 | |||
| 45 | /* | ||
| 46 | * Convert a string to a long integer. | ||
| 47 | * | ||
| 48 | * Ignores `locale' stuff. Assumes that the upper and lower case | ||
| 49 | * alphabets and digits are each contiguous. | ||
| 50 | */ | ||
| 51 | long | ||
| 52 | strtol(nptr, endptr, base) | ||
| 53 | const char *nptr; | ||
| 54 | char **endptr; | ||
| 55 | register int base; | ||
| 56 | { | ||
| 57 | register const char *s = nptr; | ||
| 58 | register unsigned long acc; | ||
| 59 | register int c; | ||
| 60 | register unsigned long cutoff; | ||
| 61 | register int neg = 0, any, cutlim; | ||
| 62 | |||
| 63 | /* | ||
| 64 | * Skip white space and pick up leading +/- sign if any. | ||
| 65 | * If base is 0, allow 0x for hex and 0 for octal, else | ||
| 66 | * assume decimal; if base is already 16, allow 0x. | ||
| 67 | */ | ||
| 68 | do { | ||
| 69 | c = *s++; | ||
| 70 | } while (isspace(c)); | ||
| 71 | if (c == '-') { | ||
| 72 | neg = 1; | ||
| 73 | c = *s++; | ||
| 74 | } else if (c == '+') | ||
| 75 | c = *s++; | ||
| 76 | if ((base == 0 || base == 16) && | ||
| 77 | c == '0' && (*s == 'x' || *s == 'X')) { | ||
| 78 | c = s[1]; | ||
| 79 | s += 2; | ||
| 80 | base = 16; | ||
| 81 | } | ||
| 82 | if (base == 0) | ||
| 83 | base = c == '0' ? 8 : 10; | ||
| 84 | |||
| 85 | /* | ||
| 86 | * Compute the cutoff value between legal numbers and illegal | ||
| 87 | * numbers. That is the largest legal value, divided by the | ||
| 88 | * base. An input number that is greater than this value, if | ||
| 89 | * followed by a legal input character, is too big. One that | ||
| 90 | * is equal to this value may be valid or not; the limit | ||
| 91 | * between valid and invalid numbers is then based on the last | ||
| 92 | * digit. For instance, if the range for longs is | ||
| 93 | * [-2147483648..2147483647] and the input base is 10, | ||
| 94 | * cutoff will be set to 214748364 and cutlim to either | ||
| 95 | * 7 (neg==0) or 8 (neg==1), meaning that if we have accumulated | ||
| 96 | * a value > 214748364, or equal but the next digit is > 7 (or 8), | ||
| 97 | * the number is too big, and we will return a range error. | ||
| 98 | * | ||
| 99 | * Set any if any `digits' consumed; make it negative to indicate | ||
| 100 | * overflow. | ||
| 101 | */ | ||
| 102 | cutoff = neg ? -(unsigned long)LONG_MIN : LONG_MAX; | ||
| 103 | cutlim = cutoff % (unsigned long)base; | ||
| 104 | cutoff /= (unsigned long)base; | ||
| 105 | for (acc = 0, any = 0;; c = *s++) { | ||
| 106 | if (isdigit(c)) | ||
| 107 | c -= '0'; | ||
| 108 | else if (isalpha(c)) | ||
| 109 | c -= isupper(c) ? 'A' - 10 : 'a' - 10; | ||
| 110 | else | ||
| 111 | break; | ||
| 112 | if (c >= base) | ||
| 113 | break; | ||
| 114 | if (any < 0 || acc > cutoff || acc == cutoff && c > cutlim) | ||
| 115 | any = -1; | ||
| 116 | else { | ||
| 117 | any = 1; | ||
| 118 | acc *= base; | ||
| 119 | acc += c; | ||
| 120 | } | ||
| 121 | } | ||
| 122 | if (any < 0) { | ||
| 123 | acc = neg ? LONG_MIN : LONG_MAX; | ||
| 124 | errno = ERANGE; | ||
| 125 | } else if (neg) | ||
| 126 | acc = -acc; | ||
| 127 | if (endptr != 0) | ||
| 128 | *endptr = (char *) (any ? s - 1 : nptr); | ||
| 129 | return (acc); | ||
| 130 | } | ||
diff --git a/src/lib/libc/stdlib/strtoq.c b/src/lib/libc/stdlib/strtoq.c new file mode 100644 index 0000000000..fc559e9d7f --- /dev/null +++ b/src/lib/libc/stdlib/strtoq.c | |||
| @@ -0,0 +1,136 @@ | |||
| 1 | /*- | ||
| 2 | * Copyright (c) 1992 The Regents of the University of California. | ||
| 3 | * All rights reserved. | ||
| 4 | * | ||
| 5 | * Redistribution and use in source and binary forms, with or without | ||
| 6 | * modification, are permitted provided that the following conditions | ||
| 7 | * are met: | ||
| 8 | * 1. Redistributions of source code must retain the above copyright | ||
| 9 | * notice, this list of conditions and the following disclaimer. | ||
| 10 | * 2. Redistributions in binary form must reproduce the above copyright | ||
| 11 | * notice, this list of conditions and the following disclaimer in the | ||
| 12 | * documentation and/or other materials provided with the distribution. | ||
| 13 | * 3. All advertising materials mentioning features or use of this software | ||
| 14 | * must display the following acknowledgement: | ||
| 15 | * This product includes software developed by the University of | ||
| 16 | * California, Berkeley and its contributors. | ||
| 17 | * 4. Neither the name of the University nor the names of its contributors | ||
| 18 | * may be used to endorse or promote products derived from this software | ||
| 19 | * without specific prior written permission. | ||
| 20 | * | ||
| 21 | * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND | ||
| 22 | * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE | ||
| 23 | * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE | ||
| 24 | * ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE | ||
| 25 | * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL | ||
| 26 | * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS | ||
| 27 | * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) | ||
| 28 | * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT | ||
| 29 | * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY | ||
| 30 | * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF | ||
| 31 | * SUCH DAMAGE. | ||
| 32 | */ | ||
| 33 | |||
| 34 | #if defined(LIBC_SCCS) && !defined(lint) | ||
| 35 | static char sccsid[] = "@(#)strtoq.c 5.1 (Berkeley) 6/26/92"; | ||
| 36 | #endif /* LIBC_SCCS and not lint */ | ||
| 37 | |||
| 38 | #include <sys/types.h> | ||
| 39 | |||
| 40 | #include <limits.h> | ||
| 41 | #include <errno.h> | ||
| 42 | #include <ctype.h> | ||
| 43 | #include <stdlib.h> | ||
| 44 | |||
| 45 | /* | ||
| 46 | * Convert a string to a quad integer. | ||
| 47 | * | ||
| 48 | * Ignores `locale' stuff. Assumes that the upper and lower case | ||
| 49 | * alphabets and digits are each contiguous. | ||
| 50 | */ | ||
| 51 | quad_t | ||
| 52 | strtoq(nptr, endptr, base) | ||
| 53 | const char *nptr; | ||
| 54 | char **endptr; | ||
| 55 | register int base; | ||
| 56 | { | ||
| 57 | register const char *s; | ||
| 58 | register u_quad_t acc; | ||
| 59 | register int c; | ||
| 60 | register u_quad_t qbase, cutoff; | ||
| 61 | register int neg, any, cutlim; | ||
| 62 | |||
| 63 | /* | ||
| 64 | * Skip white space and pick up leading +/- sign if any. | ||
| 65 | * If base is 0, allow 0x for hex and 0 for octal, else | ||
| 66 | * assume decimal; if base is already 16, allow 0x. | ||
| 67 | */ | ||
| 68 | s = nptr; | ||
| 69 | do { | ||
| 70 | c = *s++; | ||
| 71 | } while (isspace(c)); | ||
| 72 | if (c == '-') { | ||
| 73 | neg = 1; | ||
| 74 | c = *s++; | ||
| 75 | } else { | ||
| 76 | neg = 0; | ||
| 77 | if (c == '+') | ||
| 78 | c = *s++; | ||
| 79 | } | ||
| 80 | if ((base == 0 || base == 16) && | ||
| 81 | c == '0' && (*s == 'x' || *s == 'X')) { | ||
| 82 | c = s[1]; | ||
| 83 | s += 2; | ||
| 84 | base = 16; | ||
| 85 | } | ||
| 86 | if (base == 0) | ||
| 87 | base = c == '0' ? 8 : 10; | ||
| 88 | |||
| 89 | /* | ||
| 90 | * Compute the cutoff value between legal numbers and illegal | ||
| 91 | * numbers. That is the largest legal value, divided by the | ||
| 92 | * base. An input number that is greater than this value, if | ||
| 93 | * followed by a legal input character, is too big. One that | ||
| 94 | * is equal to this value may be valid or not; the limit | ||
| 95 | * between valid and invalid numbers is then based on the last | ||
| 96 | * digit. For instance, if the range for quads is | ||
| 97 | * [-9223372036854775808..9223372036854775807] and the input base | ||
| 98 | * is 10, cutoff will be set to 922337203685477580 and cutlim to | ||
| 99 | * either 7 (neg==0) or 8 (neg==1), meaning that if we have | ||
| 100 | * accumulated a value > 922337203685477580, or equal but the | ||
| 101 | * next digit is > 7 (or 8), the number is too big, and we will | ||
| 102 | * return a range error. | ||
| 103 | * | ||
| 104 | * Set any if any `digits' consumed; make it negative to indicate | ||
| 105 | * overflow. | ||
| 106 | */ | ||
| 107 | qbase = (unsigned)base; | ||
| 108 | cutoff = neg ? -(u_quad_t)QUAD_MIN : QUAD_MAX; | ||
| 109 | cutlim = cutoff % qbase; | ||
| 110 | cutoff /= qbase; | ||
| 111 | for (acc = 0, any = 0;; c = *s++) { | ||
| 112 | if (isdigit(c)) | ||
| 113 | c -= '0'; | ||
| 114 | else if (isalpha(c)) | ||
| 115 | c -= isupper(c) ? 'A' - 10 : 'a' - 10; | ||
| 116 | else | ||
| 117 | break; | ||
| 118 | if (c >= base) | ||
| 119 | break; | ||
| 120 | if (any < 0 || acc > cutoff || acc == cutoff && c > cutlim) | ||
| 121 | any = -1; | ||
| 122 | else { | ||
| 123 | any = 1; | ||
| 124 | acc *= qbase; | ||
| 125 | acc += c; | ||
| 126 | } | ||
| 127 | } | ||
| 128 | if (any < 0) { | ||
| 129 | acc = neg ? QUAD_MIN : QUAD_MAX; | ||
| 130 | errno = ERANGE; | ||
| 131 | } else if (neg) | ||
| 132 | acc = -acc; | ||
| 133 | if (endptr != 0) | ||
| 134 | *endptr = (char *) (any ? s - 1 : nptr); | ||
| 135 | return (acc); | ||
| 136 | } | ||
diff --git a/src/lib/libc/stdlib/strtoul.3 b/src/lib/libc/stdlib/strtoul.3 new file mode 100644 index 0000000000..db551b0141 --- /dev/null +++ b/src/lib/libc/stdlib/strtoul.3 | |||
| @@ -0,0 +1,164 @@ | |||
| 1 | .\" Copyright (c) 1990, 1991 The Regents of the University of California. | ||
| 2 | .\" All rights reserved. | ||
| 3 | .\" | ||
| 4 | .\" This code is derived from software contributed to Berkeley by | ||
| 5 | .\" Chris Torek and the American National Standards Committee X3, | ||
| 6 | .\" on Information Processing Systems. | ||
| 7 | .\" | ||
| 8 | .\" Redistribution and use in source and binary forms, with or without | ||
| 9 | .\" modification, are permitted provided that the following conditions | ||
| 10 | .\" are met: | ||
| 11 | .\" 1. Redistributions of source code must retain the above copyright | ||
| 12 | .\" notice, this list of conditions and the following disclaimer. | ||
| 13 | .\" 2. Redistributions in binary form must reproduce the above copyright | ||
| 14 | .\" notice, this list of conditions and the following disclaimer in the | ||
| 15 | .\" documentation and/or other materials provided with the distribution. | ||
| 16 | .\" 3. All advertising materials mentioning features or use of this software | ||
| 17 | .\" must display the following acknowledgement: | ||
| 18 | .\" This product includes software developed by the University of | ||
| 19 | .\" California, Berkeley and its contributors. | ||
| 20 | .\" 4. Neither the name of the University nor the names of its contributors | ||
| 21 | .\" may be used to endorse or promote products derived from this software | ||
| 22 | .\" without specific prior written permission. | ||
| 23 | .\" | ||
| 24 | .\" THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND | ||
| 25 | .\" ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE | ||
| 26 | .\" IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE | ||
| 27 | .\" ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE | ||
| 28 | .\" FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL | ||
| 29 | .\" DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS | ||
| 30 | .\" OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) | ||
| 31 | .\" HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT | ||
| 32 | .\" LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY | ||
| 33 | .\" OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF | ||
| 34 | .\" SUCH DAMAGE. | ||
| 35 | .\" | ||
| 36 | .\" from: @(#)strtoul.3 5.4 (Berkeley) 6/25/92 | ||
| 37 | .\" $Id: strtoul.3,v 1.1.1.1 1995/10/18 08:42:20 deraadt Exp $ | ||
| 38 | .\" | ||
| 39 | .Dd June 25, 1992 | ||
| 40 | .Dt STRTOUL 3 | ||
| 41 | .Os | ||
| 42 | .Sh NAME | ||
| 43 | .Nm strtoul, strtouq | ||
| 44 | .Nd convert a string to an unsigned long or uquad_t integer | ||
| 45 | .Sh SYNOPSIS | ||
| 46 | .Fd #include <stdlib.h> | ||
| 47 | .Fd #include <limits.h> | ||
| 48 | .Ft unsigned long | ||
| 49 | .Fn strtoul "const char *nptr" "char **endptr" "int base" | ||
| 50 | |||
| 51 | .Fd #include <sys/types.h> | ||
| 52 | .Fd #include <stdlib.h> | ||
| 53 | .Fd #include <limits.h> | ||
| 54 | .Ft u_quad_t | ||
| 55 | .Fn strtouq "const char *nptr" "char **endptr" "int base" | ||
| 56 | .Sh DESCRIPTION | ||
| 57 | The | ||
| 58 | .Fn strtoul | ||
| 59 | function | ||
| 60 | converts the string in | ||
| 61 | .Fa nptr | ||
| 62 | to an | ||
| 63 | .Em unsigned long | ||
| 64 | value. | ||
| 65 | The | ||
| 66 | .Fn strtouq | ||
| 67 | function | ||
| 68 | converts the string in | ||
| 69 | .Fa nptr | ||
| 70 | to a | ||
| 71 | .Em u_quad_t | ||
| 72 | value. | ||
| 73 | The conversion is done according to the given | ||
| 74 | .Fa base , | ||
| 75 | which must be between 2 and 36 inclusive, | ||
| 76 | or be the special value 0. | ||
| 77 | .Pp | ||
| 78 | The string may begin with an arbitrary amount of white space | ||
| 79 | (as determined by | ||
| 80 | .Xr isspace 3 ) | ||
| 81 | followed by a single optional | ||
| 82 | .Ql + | ||
| 83 | or | ||
| 84 | .Ql - | ||
| 85 | sign. | ||
| 86 | If | ||
| 87 | .Fa base | ||
| 88 | is zero or 16, | ||
| 89 | the string may then include a | ||
| 90 | .Ql 0x | ||
| 91 | prefix, | ||
| 92 | and the number will be read in base 16; otherwise, a zero | ||
| 93 | .Fa base | ||
| 94 | is taken as 10 (decimal) unless the next character is | ||
| 95 | .Ql 0 , | ||
| 96 | in which case it is taken as 8 (octal). | ||
| 97 | .Pp | ||
| 98 | The remainder of the string is converted to an | ||
| 99 | .Em unsigned long | ||
| 100 | value in the obvious manner, | ||
| 101 | stopping at the end of the string | ||
| 102 | or at the first character that does not produce a valid digit | ||
| 103 | in the given base. | ||
| 104 | (In bases above 10, the letter | ||
| 105 | .Ql A | ||
| 106 | in either upper or lower case | ||
| 107 | represents 10, | ||
| 108 | .Ql B | ||
| 109 | represents 11, and so forth, with | ||
| 110 | .Ql Z | ||
| 111 | representing 35.) | ||
| 112 | .Pp | ||
| 113 | If | ||
| 114 | .Fa endptr | ||
| 115 | is non nil, | ||
| 116 | .Fn strtoul | ||
| 117 | stores the address of the first invalid character in | ||
| 118 | .Fa *endptr . | ||
| 119 | If there were no digits at all, however, | ||
| 120 | .Fn strtoul | ||
| 121 | stores the original value of | ||
| 122 | .Fa nptr | ||
| 123 | in | ||
| 124 | .Fa *endptr . | ||
| 125 | (Thus, if | ||
| 126 | .Fa *nptr | ||
| 127 | is not | ||
| 128 | .Ql \e0 | ||
| 129 | but | ||
| 130 | .Fa **endptr | ||
| 131 | is | ||
| 132 | .Ql \e0 | ||
| 133 | on return, the entire string was valid.) | ||
| 134 | .Sh RETURN VALUES | ||
| 135 | The | ||
| 136 | .Fn strtoul | ||
| 137 | function | ||
| 138 | returns either the result of the conversion | ||
| 139 | or, if there was a leading minus sign, | ||
| 140 | the negation of the result of the conversion, | ||
| 141 | unless the original (non-negated) value would overflow; | ||
| 142 | in the latter case, | ||
| 143 | .Fn strtoul | ||
| 144 | returns | ||
| 145 | .Dv ULONG_MAX | ||
| 146 | and sets the global variable | ||
| 147 | .Va errno | ||
| 148 | to | ||
| 149 | .Er ERANGE . | ||
| 150 | .Sh ERRORS | ||
| 151 | .Bl -tag -width Er | ||
| 152 | .It Bq Er ERANGE | ||
| 153 | The given string was out of range; the value converted has been clamped. | ||
| 154 | .El | ||
| 155 | .Sh SEE ALSO | ||
| 156 | .Xr strtol 3 | ||
| 157 | .Sh STANDARDS | ||
| 158 | The | ||
| 159 | .Fn strtoul | ||
| 160 | function | ||
| 161 | conforms to | ||
| 162 | .St -ansiC . | ||
| 163 | .Sh BUGS | ||
| 164 | Ignores the current locale. | ||
diff --git a/src/lib/libc/stdlib/strtoul.c b/src/lib/libc/stdlib/strtoul.c new file mode 100644 index 0000000000..00f7210fa1 --- /dev/null +++ b/src/lib/libc/stdlib/strtoul.c | |||
| @@ -0,0 +1,108 @@ | |||
| 1 | /* | ||
| 2 | * Copyright (c) 1990 Regents of the University of California. | ||
| 3 | * All rights reserved. | ||
| 4 | * | ||
| 5 | * Redistribution and use in source and binary forms, with or without | ||
| 6 | * modification, are permitted provided that the following conditions | ||
| 7 | * are met: | ||
| 8 | * 1. Redistributions of source code must retain the above copyright | ||
| 9 | * notice, this list of conditions and the following disclaimer. | ||
| 10 | * 2. Redistributions in binary form must reproduce the above copyright | ||
| 11 | * notice, this list of conditions and the following disclaimer in the | ||
| 12 | * documentation and/or other materials provided with the distribution. | ||
| 13 | * 3. All advertising materials mentioning features or use of this software | ||
| 14 | * must display the following acknowledgement: | ||
| 15 | * This product includes software developed by the University of | ||
| 16 | * California, Berkeley and its contributors. | ||
| 17 | * 4. Neither the name of the University nor the names of its contributors | ||
| 18 | * may be used to endorse or promote products derived from this software | ||
| 19 | * without specific prior written permission. | ||
| 20 | * | ||
| 21 | * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND | ||
| 22 | * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE | ||
| 23 | * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE | ||
| 24 | * ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE | ||
| 25 | * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL | ||
| 26 | * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS | ||
| 27 | * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) | ||
| 28 | * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT | ||
| 29 | * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY | ||
| 30 | * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF | ||
| 31 | * SUCH DAMAGE. | ||
| 32 | */ | ||
| 33 | |||
| 34 | #if defined(LIBC_SCCS) && !defined(lint) | ||
| 35 | /*static char *sccsid = "from: @(#)strtoul.c 5.3 (Berkeley) 2/23/91";*/ | ||
| 36 | static char *rcsid = "$Id: strtoul.c,v 1.1.1.1 1995/10/18 08:42:20 deraadt Exp $"; | ||
| 37 | #endif /* LIBC_SCCS and not lint */ | ||
| 38 | |||
| 39 | #include <limits.h> | ||
| 40 | #include <ctype.h> | ||
| 41 | #include <errno.h> | ||
| 42 | #include <stdlib.h> | ||
| 43 | |||
| 44 | /* | ||
| 45 | * Convert a string to an unsigned long integer. | ||
| 46 | * | ||
| 47 | * Ignores `locale' stuff. Assumes that the upper and lower case | ||
| 48 | * alphabets and digits are each contiguous. | ||
| 49 | */ | ||
| 50 | unsigned long | ||
| 51 | strtoul(nptr, endptr, base) | ||
| 52 | const char *nptr; | ||
| 53 | char **endptr; | ||
| 54 | register int base; | ||
| 55 | { | ||
| 56 | register const char *s = nptr; | ||
| 57 | register unsigned long acc; | ||
| 58 | register int c; | ||
| 59 | register unsigned long cutoff; | ||
| 60 | register int neg = 0, any, cutlim; | ||
| 61 | |||
| 62 | /* | ||
| 63 | * See strtol for comments as to the logic used. | ||
| 64 | */ | ||
| 65 | do { | ||
| 66 | c = *s++; | ||
| 67 | } while (isspace(c)); | ||
| 68 | if (c == '-') { | ||
| 69 | neg = 1; | ||
| 70 | c = *s++; | ||
| 71 | } else if (c == '+') | ||
| 72 | c = *s++; | ||
| 73 | if ((base == 0 || base == 16) && | ||
| 74 | c == '0' && (*s == 'x' || *s == 'X')) { | ||
| 75 | c = s[1]; | ||
| 76 | s += 2; | ||
| 77 | base = 16; | ||
| 78 | } | ||
| 79 | if (base == 0) | ||
| 80 | base = c == '0' ? 8 : 10; | ||
| 81 | cutoff = (unsigned long)ULONG_MAX / (unsigned long)base; | ||
| 82 | cutlim = (unsigned long)ULONG_MAX % (unsigned long)base; | ||
| 83 | for (acc = 0, any = 0;; c = *s++) { | ||
| 84 | if (isdigit(c)) | ||
| 85 | c -= '0'; | ||
| 86 | else if (isalpha(c)) | ||
| 87 | c -= isupper(c) ? 'A' - 10 : 'a' - 10; | ||
| 88 | else | ||
| 89 | break; | ||
| 90 | if (c >= base) | ||
| 91 | break; | ||
| 92 | if (any < 0 || acc > cutoff || acc == cutoff && c > cutlim) | ||
| 93 | any = -1; | ||
| 94 | else { | ||
| 95 | any = 1; | ||
| 96 | acc *= base; | ||
| 97 | acc += c; | ||
| 98 | } | ||
| 99 | } | ||
| 100 | if (any < 0) { | ||
| 101 | acc = ULONG_MAX; | ||
| 102 | errno = ERANGE; | ||
| 103 | } else if (neg) | ||
| 104 | acc = -acc; | ||
| 105 | if (endptr != 0) | ||
| 106 | *endptr = (char *) (any ? s - 1 : nptr); | ||
| 107 | return (acc); | ||
| 108 | } | ||
diff --git a/src/lib/libc/stdlib/strtouq.c b/src/lib/libc/stdlib/strtouq.c new file mode 100644 index 0000000000..cc647d8d28 --- /dev/null +++ b/src/lib/libc/stdlib/strtouq.c | |||
| @@ -0,0 +1,114 @@ | |||
| 1 | /*- | ||
| 2 | * Copyright (c) 1992 The Regents of the University of California. | ||
| 3 | * All rights reserved. | ||
| 4 | * | ||
| 5 | * Redistribution and use in source and binary forms, with or without | ||
| 6 | * modification, are permitted provided that the following conditions | ||
| 7 | * are met: | ||
| 8 | * 1. Redistributions of source code must retain the above copyright | ||
| 9 | * notice, this list of conditions and the following disclaimer. | ||
| 10 | * 2. Redistributions in binary form must reproduce the above copyright | ||
| 11 | * notice, this list of conditions and the following disclaimer in the | ||
| 12 | * documentation and/or other materials provided with the distribution. | ||
| 13 | * 3. All advertising materials mentioning features or use of this software | ||
| 14 | * must display the following acknowledgement: | ||
| 15 | * This product includes software developed by the University of | ||
| 16 | * California, Berkeley and its contributors. | ||
| 17 | * 4. Neither the name of the University nor the names of its contributors | ||
| 18 | * may be used to endorse or promote products derived from this software | ||
| 19 | * without specific prior written permission. | ||
| 20 | * | ||
| 21 | * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND | ||
| 22 | * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE | ||
| 23 | * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE | ||
| 24 | * ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE | ||
| 25 | * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL | ||
| 26 | * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS | ||
| 27 | * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) | ||
| 28 | * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT | ||
| 29 | * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY | ||
| 30 | * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF | ||
| 31 | * SUCH DAMAGE. | ||
| 32 | */ | ||
| 33 | |||
| 34 | #if defined(LIBC_SCCS) && !defined(lint) | ||
| 35 | static char sccsid[] = "@(#)strtouq.c 5.1 (Berkeley) 6/26/92"; | ||
| 36 | #endif /* LIBC_SCCS and not lint */ | ||
| 37 | |||
| 38 | #include <sys/types.h> | ||
| 39 | |||
| 40 | #include <limits.h> | ||
| 41 | #include <errno.h> | ||
| 42 | #include <ctype.h> | ||
| 43 | #include <stdlib.h> | ||
| 44 | |||
| 45 | /* | ||
| 46 | * Convert a string to an unsigned quad integer. | ||
| 47 | * | ||
| 48 | * Ignores `locale' stuff. Assumes that the upper and lower case | ||
| 49 | * alphabets and digits are each contiguous. | ||
| 50 | */ | ||
| 51 | u_quad_t | ||
| 52 | strtouq(nptr, endptr, base) | ||
| 53 | const char *nptr; | ||
| 54 | char **endptr; | ||
| 55 | register int base; | ||
| 56 | { | ||
| 57 | register const char *s = nptr; | ||
| 58 | register u_quad_t acc; | ||
| 59 | register int c; | ||
| 60 | register u_quad_t qbase, cutoff; | ||
| 61 | register int neg, any, cutlim; | ||
| 62 | |||
| 63 | /* | ||
| 64 | * See strtoq for comments as to the logic used. | ||
| 65 | */ | ||
| 66 | s = nptr; | ||
| 67 | do { | ||
| 68 | c = *s++; | ||
| 69 | } while (isspace(c)); | ||
| 70 | if (c == '-') { | ||
| 71 | neg = 1; | ||
| 72 | c = *s++; | ||
| 73 | } else { | ||
| 74 | neg = 0; | ||
| 75 | if (c == '+') | ||
| 76 | c = *s++; | ||
| 77 | } | ||
| 78 | if ((base == 0 || base == 16) && | ||
| 79 | c == '0' && (*s == 'x' || *s == 'X')) { | ||
| 80 | c = s[1]; | ||
| 81 | s += 2; | ||
| 82 | base = 16; | ||
| 83 | } | ||
| 84 | if (base == 0) | ||
| 85 | base = c == '0' ? 8 : 10; | ||
| 86 | qbase = (unsigned)base; | ||
| 87 | cutoff = (u_quad_t)UQUAD_MAX / qbase; | ||
| 88 | cutlim = (u_quad_t)UQUAD_MAX % qbase; | ||
| 89 | for (acc = 0, any = 0;; c = *s++) { | ||
| 90 | if (isdigit(c)) | ||
| 91 | c -= '0'; | ||
| 92 | else if (isalpha(c)) | ||
| 93 | c -= isupper(c) ? 'A' - 10 : 'a' - 10; | ||
| 94 | else | ||
| 95 | break; | ||
| 96 | if (c >= base) | ||
| 97 | break; | ||
| 98 | if (any < 0 || acc > cutoff || acc == cutoff && c > cutlim) | ||
| 99 | any = -1; | ||
| 100 | else { | ||
| 101 | any = 1; | ||
| 102 | acc *= qbase; | ||
| 103 | acc += c; | ||
| 104 | } | ||
| 105 | } | ||
| 106 | if (any < 0) { | ||
| 107 | acc = UQUAD_MAX; | ||
| 108 | errno = ERANGE; | ||
| 109 | } else if (neg) | ||
| 110 | acc = -acc; | ||
| 111 | if (endptr != 0) | ||
| 112 | *endptr = (char *) (any ? s - 1 : nptr); | ||
| 113 | return (acc); | ||
| 114 | } | ||
diff --git a/src/lib/libc/stdlib/system.3 b/src/lib/libc/stdlib/system.3 new file mode 100644 index 0000000000..520f51db0a --- /dev/null +++ b/src/lib/libc/stdlib/system.3 | |||
| @@ -0,0 +1,99 @@ | |||
| 1 | .\" Copyright (c) 1990, 1991 The Regents of the University of California. | ||
| 2 | .\" All rights reserved. | ||
| 3 | .\" | ||
| 4 | .\" This code is derived from software contributed to Berkeley by | ||
| 5 | .\" the American National Standards Committee X3, on Information | ||
| 6 | .\" Processing Systems. | ||
| 7 | .\" | ||
| 8 | .\" Redistribution and use in source and binary forms, with or without | ||
| 9 | .\" modification, are permitted provided that the following conditions | ||
| 10 | .\" are met: | ||
| 11 | .\" 1. Redistributions of source code must retain the above copyright | ||
| 12 | .\" notice, this list of conditions and the following disclaimer. | ||
| 13 | .\" 2. Redistributions in binary form must reproduce the above copyright | ||
| 14 | .\" notice, this list of conditions and the following disclaimer in the | ||
| 15 | .\" documentation and/or other materials provided with the distribution. | ||
| 16 | .\" 3. All advertising materials mentioning features or use of this software | ||
| 17 | .\" must display the following acknowledgement: | ||
| 18 | .\" This product includes software developed by the University of | ||
| 19 | .\" California, Berkeley and its contributors. | ||
| 20 | .\" 4. Neither the name of the University nor the names of its contributors | ||
| 21 | .\" may be used to endorse or promote products derived from this software | ||
| 22 | .\" without specific prior written permission. | ||
| 23 | .\" | ||
| 24 | .\" THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND | ||
| 25 | .\" ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE | ||
| 26 | .\" IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE | ||
| 27 | .\" ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE | ||
| 28 | .\" FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL | ||
| 29 | .\" DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS | ||
| 30 | .\" OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) | ||
| 31 | .\" HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT | ||
| 32 | .\" LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY | ||
| 33 | .\" OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF | ||
| 34 | .\" SUCH DAMAGE. | ||
| 35 | .\" | ||
| 36 | .\" from: @(#)system.3 6.5 (Berkeley) 6/29/91 | ||
| 37 | .\" $Id: system.3,v 1.1.1.1 1995/10/18 08:42:20 deraadt Exp $ | ||
| 38 | .\" | ||
| 39 | .Dd June 29, 1991 | ||
| 40 | .Dt SYSTEM 3 | ||
| 41 | .Os | ||
| 42 | .Sh NAME | ||
| 43 | .Nm system | ||
| 44 | .Nd pass a command to the shell | ||
| 45 | .Sh SYNOPSIS | ||
| 46 | .Fd #include <stdlib.h> | ||
| 47 | .Ft int | ||
| 48 | .Fn system "const char *string" | ||
| 49 | .Sh DESCRIPTION | ||
| 50 | The | ||
| 51 | .Fn system | ||
| 52 | function | ||
| 53 | hands the argument | ||
| 54 | .Fa string | ||
| 55 | to the command interpreter | ||
| 56 | .Xr sh 1 . | ||
| 57 | The calling process waits for the shell to finish executing the command, | ||
| 58 | ignoring | ||
| 59 | .Dv SIGINT | ||
| 60 | and | ||
| 61 | .Dv SIGQUIT , | ||
| 62 | and blocking | ||
| 63 | .Dv SIGCHLD . | ||
| 64 | .Pp | ||
| 65 | If | ||
| 66 | .Fa string | ||
| 67 | is a | ||
| 68 | .Dv NULL | ||
| 69 | pointer, | ||
| 70 | .Fn system | ||
| 71 | will return non-zero. | ||
| 72 | Otherwise, | ||
| 73 | .Fn system | ||
| 74 | returns the termination status of the shell in the format specified by | ||
| 75 | .Xr waitpid 3 . | ||
| 76 | .Sh RETURN VALUES | ||
| 77 | If a child process cannot be created, or the termination status of | ||
| 78 | the shell cannot be obtained, | ||
| 79 | .Fn system | ||
| 80 | returns -1 and sets | ||
| 81 | .Va errno | ||
| 82 | to indicate the error. | ||
| 83 | If execution of the shell fails, | ||
| 84 | .Fn system | ||
| 85 | returns the termination status for a program that terminates with a call of | ||
| 86 | .Fn exit 127 . | ||
| 87 | .Sh SEE ALSO | ||
| 88 | .Xr sh 1 , | ||
| 89 | .Xr execve 2 , | ||
| 90 | .Xr popen 3 , | ||
| 91 | .Xr waitpid 3 , | ||
| 92 | .Sh STANDARDS | ||
| 93 | The | ||
| 94 | .Fn system | ||
| 95 | function | ||
| 96 | conforms to | ||
| 97 | .St -ansiC | ||
| 98 | and | ||
| 99 | .St -1003.2-92 . | ||
diff --git a/src/lib/libc/stdlib/system.c b/src/lib/libc/stdlib/system.c new file mode 100644 index 0000000000..c2f39325f6 --- /dev/null +++ b/src/lib/libc/stdlib/system.c | |||
| @@ -0,0 +1,79 @@ | |||
| 1 | /* | ||
| 2 | * Copyright (c) 1988 The Regents of the University of California. | ||
| 3 | * All rights reserved. | ||
| 4 | * | ||
| 5 | * Redistribution and use in source and binary forms, with or without | ||
| 6 | * modification, are permitted provided that the following conditions | ||
| 7 | * are met: | ||
| 8 | * 1. Redistributions of source code must retain the above copyright | ||
| 9 | * notice, this list of conditions and the following disclaimer. | ||
| 10 | * 2. Redistributions in binary form must reproduce the above copyright | ||
| 11 | * notice, this list of conditions and the following disclaimer in the | ||
| 12 | * documentation and/or other materials provided with the distribution. | ||
| 13 | * 3. All advertising materials mentioning features or use of this software | ||
| 14 | * must display the following acknowledgement: | ||
| 15 | * This product includes software developed by the University of | ||
| 16 | * California, Berkeley and its contributors. | ||
| 17 | * 4. Neither the name of the University nor the names of its contributors | ||
| 18 | * may be used to endorse or promote products derived from this software | ||
| 19 | * without specific prior written permission. | ||
| 20 | * | ||
| 21 | * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND | ||
| 22 | * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE | ||
| 23 | * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE | ||
| 24 | * ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE | ||
| 25 | * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL | ||
| 26 | * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS | ||
| 27 | * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) | ||
| 28 | * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT | ||
| 29 | * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY | ||
| 30 | * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF | ||
| 31 | * SUCH DAMAGE. | ||
| 32 | */ | ||
| 33 | |||
| 34 | #if defined(LIBC_SCCS) && !defined(lint) | ||
| 35 | /*static char *sccsid = "from: @(#)system.c 5.10 (Berkeley) 2/23/91";*/ | ||
| 36 | static char *rcsid = "$Id: system.c,v 1.1.1.1 1995/10/18 08:42:20 deraadt Exp $"; | ||
| 37 | #endif /* LIBC_SCCS and not lint */ | ||
| 38 | |||
| 39 | #include <sys/types.h> | ||
| 40 | #include <sys/wait.h> | ||
| 41 | #include <signal.h> | ||
| 42 | #include <stdlib.h> | ||
| 43 | #include <unistd.h> | ||
| 44 | #include <paths.h> | ||
| 45 | |||
| 46 | extern char **environ; | ||
| 47 | |||
| 48 | int | ||
| 49 | system(command) | ||
| 50 | const char *command; | ||
| 51 | { | ||
| 52 | pid_t pid; | ||
| 53 | sig_t intsave, quitsave; | ||
| 54 | int omask; | ||
| 55 | int pstat; | ||
| 56 | char *argp[] = {"sh", "-c", (char *) command, NULL}; | ||
| 57 | |||
| 58 | if (!command) /* just checking... */ | ||
| 59 | return(1); | ||
| 60 | |||
| 61 | omask = sigblock(sigmask(SIGCHLD)); | ||
| 62 | switch(pid = vfork()) { | ||
| 63 | case -1: /* error */ | ||
| 64 | (void)sigsetmask(omask); | ||
| 65 | return(-1); | ||
| 66 | case 0: /* child */ | ||
| 67 | (void)sigsetmask(omask); | ||
| 68 | execve(_PATH_BSHELL, argp, environ); | ||
| 69 | _exit(127); | ||
| 70 | } | ||
| 71 | |||
| 72 | intsave = signal(SIGINT, SIG_IGN); | ||
| 73 | quitsave = signal(SIGQUIT, SIG_IGN); | ||
| 74 | pid = waitpid(pid, (int *)&pstat, 0); | ||
| 75 | (void)sigsetmask(omask); | ||
| 76 | (void)signal(SIGINT, intsave); | ||
| 77 | (void)signal(SIGQUIT, quitsave); | ||
| 78 | return(pid == -1 ? -1 : pstat); | ||
| 79 | } | ||
diff --git a/src/lib/libc/string/Makefile.inc b/src/lib/libc/string/Makefile.inc new file mode 100644 index 0000000000..2b7ce63a63 --- /dev/null +++ b/src/lib/libc/string/Makefile.inc | |||
| @@ -0,0 +1,108 @@ | |||
| 1 | # from: @(#)Makefile.inc 5.6 (Berkeley) 3/5/91 | ||
| 2 | # $Id: Makefile.inc,v 1.1.1.1 1995/10/18 08:42:20 deraadt Exp $ | ||
| 3 | |||
| 4 | # string sources | ||
| 5 | .PATH: ${.CURDIR}/arch/${MACHINE_ARCH}/string ${.CURDIR}/string | ||
| 6 | |||
| 7 | SRCS+= bm.c memccpy.c strcasecmp.c strcoll.c strdup.c strerror.c \ | ||
| 8 | strftime.c strmode.c strsignal.c strtok.c strxfrm.c \ | ||
| 9 | __strerror.c __strsignal.c | ||
| 10 | |||
| 11 | # machine-dependent net sources | ||
| 12 | # m-d Makefile.inc must include sources for: | ||
| 13 | # bcmp() bcopy() bzero() ffs() index() memchr() memcmp() memset() | ||
| 14 | # rindex() strcat() strcmp() strcpy() strcspn() strlen() | ||
| 15 | # strncat() strncmp() strncpy() strpbrk() strsep() | ||
| 16 | # strspn() strstr() swav() | ||
| 17 | # m-d Makefile.inc may include sources for: | ||
| 18 | # memcpy() memmove() strchr() strrchr() | ||
| 19 | |||
| 20 | .include "${.CURDIR}/arch/${MACHINE_ARCH}/string/Makefile.inc" | ||
| 21 | |||
| 22 | # if no machine specific memmove(3), build one out of bcopy(3). | ||
| 23 | .if empty(SRCS:Mmemmove.S) | ||
| 24 | OBJS+= memmove.o | ||
| 25 | memmove.o: bcopy.c | ||
| 26 | ${CC} -DMEMMOVE ${CFLAGS} ${CPPFLAGS} -c ${.ALLSRC} -o ${.TARGET} | ||
| 27 | @${LD} -x -r ${.TARGET} | ||
| 28 | @mv a.out ${.TARGET} | ||
| 29 | |||
| 30 | memmove.po: bcopy.c | ||
| 31 | ${CC} -DMEMMOVE ${CFLAGS} ${CPPFLAGS} -c -p ${.ALLSRC} -o ${.TARGET} | ||
| 32 | @${LD} -X -r ${.TARGET} | ||
| 33 | @mv a.out ${.TARGET} | ||
| 34 | |||
| 35 | memmove.so: bcopy.c | ||
| 36 | ${CC} ${PICFLAG} -DPIC -DMEMMOVE ${CFLAGS} ${CPPFLAGS} -c ${.ALLSRC} \ | ||
| 37 | -o ${.TARGET} | ||
| 38 | .endif | ||
| 39 | |||
| 40 | # if no machine specific memcpy(3), build one out of bcopy(3). | ||
| 41 | # if there is a machine specific memmove(3), we'll assume it aliases | ||
| 42 | # memcpy(3). | ||
| 43 | .if empty(SRCS:Mmemcpy.S) | ||
| 44 | .if empty(SRCS:Mmemmove.S) | ||
| 45 | OBJS+= memcpy.o | ||
| 46 | memcpy.o: bcopy.c | ||
| 47 | ${CC} -DMEMCOPY ${CFLAGS} ${CPPFLAGS} -c ${.ALLSRC} -o ${.TARGET} | ||
| 48 | @${LD} -x -r ${.TARGET} | ||
| 49 | @mv a.out ${.TARGET} | ||
| 50 | |||
| 51 | memcpy.po: bcopy.c | ||
| 52 | ${CC} -DMEMCOPY ${CFLAGS} ${CPPFLAGS} -c -p ${.ALLSRC} -o ${.TARGET} | ||
| 53 | @${LD} -X -r ${.TARGET} | ||
| 54 | @mv a.out ${.TARGET} | ||
| 55 | |||
| 56 | memcpy.so: bcopy.c | ||
| 57 | ${CC} ${PICFLAG} -DPIC -DMEMCOPY ${CFLAGS} ${CPPFLAGS} -c ${.ALLSRC} \ | ||
| 58 | -o ${.TARGET} | ||
| 59 | .endif | ||
| 60 | .endif | ||
| 61 | |||
| 62 | # if no machine specific strchr(3), build one out of index(3). | ||
| 63 | .if empty(SRCS:Mstrchr.S) | ||
| 64 | OBJS+= strchr.o | ||
| 65 | strchr.o: index.c | ||
| 66 | ${CC} -DSTRCHR ${CFLAGS} ${CPPFLAGS} -c ${.ALLSRC} -o ${.TARGET} | ||
| 67 | @${LD} -x -r ${.TARGET} | ||
| 68 | @mv a.out ${.TARGET} | ||
| 69 | |||
| 70 | strchr.po: index.c | ||
| 71 | ${CC} -DSTRCHR ${CFLAGS} ${CPPFLAGS} -c -p ${.ALLSRC} -o ${.TARGET} | ||
| 72 | @${LD} -X -r ${.TARGET} | ||
| 73 | @mv a.out ${.TARGET} | ||
| 74 | |||
| 75 | strchr.so: index.c | ||
| 76 | ${CC} ${PICFLAG} -DPIC -DSTRCHR ${CFLAGS} ${CPPFLAGS} -c ${.ALLSRC} \ | ||
| 77 | -o ${.TARGET} | ||
| 78 | .endif | ||
| 79 | |||
| 80 | # if no machine specific strrchr(3), build one out of rindex(3). | ||
| 81 | .if empty(SRCS:Mstrrchr.S) | ||
| 82 | OBJS+= strrchr.o | ||
| 83 | strrchr.o: rindex.c | ||
| 84 | ${CC} -DSTRRCHR ${CFLAGS} ${CPPFLAGS} -c ${.ALLSRC} -o ${.TARGET} | ||
| 85 | @${LD} -x -r ${.TARGET} | ||
| 86 | @mv a.out ${.TARGET} | ||
| 87 | |||
| 88 | strrchr.po: rindex.c | ||
| 89 | ${CC} -DSTRRCHR ${CFLAGS} ${CPPFLAGS} -c -p ${.ALLSRC} -o ${.TARGET} | ||
| 90 | @${LD} -X -r ${.TARGET} | ||
| 91 | @mv a.out ${.TARGET} | ||
| 92 | |||
| 93 | strrchr.so: rindex.c | ||
| 94 | ${CC} ${PICFLAG} -DPIC -DSTRRCHR ${CFLAGS} ${CPPFLAGS} -c ${.ALLSRC} \ | ||
| 95 | -o ${.TARGET} | ||
| 96 | .endif | ||
| 97 | |||
| 98 | MAN+= bm.3 bcmp.3 bcopy.3 bstring.3 bzero.3 ffs.3 index.3 memccpy.3 memchr.3 \ | ||
| 99 | memcmp.3 memcpy.3 memmove.3 memset.3 rindex.3 strcasecmp.3 strcat.3 \ | ||
| 100 | strchr.3 strcmp.3 strcoll.3 strcpy.3 strcspn.3 strerror.3 strftime.3 \ | ||
| 101 | string.3 strlen.3 strmode.3 strdup.3 strpbrk.3 strrchr.3 strsep.3 \ | ||
| 102 | strsignal.3 strspn.3 strstr.3 strtok.3 strxfrm.3 swab.3 | ||
| 103 | |||
| 104 | MLINKS+=bm.3 bm_comp.3 bm.3 bm_exec.3 bm.3 bm_free.3 | ||
| 105 | MLINKS+=strcasecmp.3 strncasecmp.3 | ||
| 106 | MLINKS+=strcat.3 strncat.3 | ||
| 107 | MLINKS+=strcmp.3 strncmp.3 | ||
| 108 | MLINKS+=strcpy.3 strncpy.3 | ||
diff --git a/src/lib/libc/string/__strerror.c b/src/lib/libc/string/__strerror.c new file mode 100644 index 0000000000..cd604906db --- /dev/null +++ b/src/lib/libc/string/__strerror.c | |||
| @@ -0,0 +1,92 @@ | |||
| 1 | /* | ||
| 2 | * Copyright (c) 1988 Regents of the University of California. | ||
| 3 | * All rights reserved. | ||
| 4 | * | ||
| 5 | * Redistribution and use in source and binary forms, with or without | ||
| 6 | * modification, are permitted provided that the following conditions | ||
| 7 | * are met: | ||
| 8 | * 1. Redistributions of source code must retain the above copyright | ||
| 9 | * notice, this list of conditions and the following disclaimer. | ||
| 10 | * 2. Redistributions in binary form must reproduce the above copyright | ||
| 11 | * notice, this list of conditions and the following disclaimer in the | ||
| 12 | * documentation and/or other materials provided with the distribution. | ||
| 13 | * 3. All advertising materials mentioning features or use of this software | ||
| 14 | * must display the following acknowledgement: | ||
| 15 | * This product includes software developed by the University of | ||
| 16 | * California, Berkeley and its contributors. | ||
| 17 | * 4. Neither the name of the University nor the names of its contributors | ||
| 18 | * may be used to endorse or promote products derived from this software | ||
| 19 | * without specific prior written permission. | ||
| 20 | * | ||
| 21 | * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND | ||
| 22 | * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE | ||
| 23 | * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE | ||
| 24 | * ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE | ||
| 25 | * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL | ||
| 26 | * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS | ||
| 27 | * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) | ||
| 28 | * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT | ||
| 29 | * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY | ||
| 30 | * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF | ||
| 31 | * SUCH DAMAGE. | ||
| 32 | */ | ||
| 33 | |||
| 34 | #if defined(LIBC_SCCS) && !defined(lint) | ||
| 35 | /*static char *sccsid = "from: @(#)strerror.c 5.6 (Berkeley) 5/4/91";*/ | ||
| 36 | static char *rcsid = "$Id: __strerror.c,v 1.1.1.1 1995/10/18 08:42:20 deraadt Exp $"; | ||
| 37 | #endif /* LIBC_SCCS and not lint */ | ||
| 38 | |||
| 39 | #ifdef NLS | ||
| 40 | #define catclose _catclose | ||
| 41 | #define catgets _catgets | ||
| 42 | #define catopen _catopen | ||
| 43 | #include <nl_types.h> | ||
| 44 | #endif | ||
| 45 | |||
| 46 | #define sys_errlist _sys_errlist | ||
| 47 | #define sys_nerr _sys_nerr | ||
| 48 | |||
| 49 | #include <stdio.h> | ||
| 50 | #include <string.h> | ||
| 51 | |||
| 52 | /* | ||
| 53 | * Since perror() is not allowed to change the contents of strerror()'s | ||
| 54 | * static buffer, both functions supply their own buffers to the | ||
| 55 | * internal function __strerror(). | ||
| 56 | */ | ||
| 57 | |||
| 58 | char * | ||
| 59 | __strerror(num, buf) | ||
| 60 | int num; | ||
| 61 | char *buf; | ||
| 62 | { | ||
| 63 | #define UPREFIX "Unknown error: %u" | ||
| 64 | register unsigned int errnum; | ||
| 65 | |||
| 66 | #ifdef NLS | ||
| 67 | nl_catd catd ; | ||
| 68 | catd = catopen("libc", 0); | ||
| 69 | #endif | ||
| 70 | |||
| 71 | errnum = num; /* convert to unsigned */ | ||
| 72 | if (errnum < sys_nerr) { | ||
| 73 | #ifdef NLS | ||
| 74 | strcpy(buf, catgets(catd, 1, errnum, | ||
| 75 | (char *)sys_errlist[errnum])); | ||
| 76 | #else | ||
| 77 | return(sys_errlist[errnum]); | ||
| 78 | #endif | ||
| 79 | } else { | ||
| 80 | #ifdef NLS | ||
| 81 | sprintf(buf, catgets(catd, 1, 0xffff, UPREFIX), errnum); | ||
| 82 | #else | ||
| 83 | sprintf(buf, UPREFIX, errnum); | ||
| 84 | #endif | ||
| 85 | } | ||
| 86 | |||
| 87 | #ifdef NLS | ||
| 88 | catclose(catd); | ||
| 89 | #endif | ||
| 90 | |||
| 91 | return buf; | ||
| 92 | } | ||
diff --git a/src/lib/libc/string/__strsignal.c b/src/lib/libc/string/__strsignal.c new file mode 100644 index 0000000000..1937e2d608 --- /dev/null +++ b/src/lib/libc/string/__strsignal.c | |||
| @@ -0,0 +1,86 @@ | |||
| 1 | /* | ||
| 2 | * Copyright (c) 1988 Regents of the University of California. | ||
| 3 | * All rights reserved. | ||
| 4 | * | ||
| 5 | * Redistribution and use in source and binary forms, with or without | ||
| 6 | * modification, are permitted provided that the following conditions | ||
| 7 | * are met: | ||
| 8 | * 1. Redistributions of source code must retain the above copyright | ||
| 9 | * notice, this list of conditions and the following disclaimer. | ||
| 10 | * 2. Redistributions in binary form must reproduce the above copyright | ||
| 11 | * notice, this list of conditions and the following disclaimer in the | ||
| 12 | * documentation and/or other materials provided with the distribution. | ||
| 13 | * 3. All advertising materials mentioning features or use of this software | ||
| 14 | * must display the following acknowledgement: | ||
| 15 | * This product includes software developed by the University of | ||
| 16 | * California, Berkeley and its contributors. | ||
| 17 | * 4. Neither the name of the University nor the names of its contributors | ||
| 18 | * may be used to endorse or promote products derived from this software | ||
| 19 | * without specific prior written permission. | ||
| 20 | * | ||
| 21 | * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND | ||
| 22 | * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE | ||
| 23 | * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE | ||
| 24 | * ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE | ||
| 25 | * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL | ||
| 26 | * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS | ||
| 27 | * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) | ||
| 28 | * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT | ||
| 29 | * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY | ||
| 30 | * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF | ||
| 31 | * SUCH DAMAGE. | ||
| 32 | */ | ||
| 33 | |||
| 34 | #if defined(LIBC_SCCS) && !defined(lint) | ||
| 35 | /*static char *sccsid = "from: @(#)strerror.c 5.6 (Berkeley) 5/4/91";*/ | ||
| 36 | static char *rcsid = "$Id: __strsignal.c,v 1.1.1.1 1995/10/18 08:42:20 deraadt Exp $"; | ||
| 37 | #endif /* LIBC_SCCS and not lint */ | ||
| 38 | |||
| 39 | #ifdef NLS | ||
| 40 | #define catclose _catclose | ||
| 41 | #define catgets _catgets | ||
| 42 | #define catopen _catopen | ||
| 43 | #include <nl_types.h> | ||
| 44 | #endif | ||
| 45 | |||
| 46 | #define sys_siglist _sys_siglist | ||
| 47 | |||
| 48 | #include <stdio.h> | ||
| 49 | #include <signal.h> | ||
| 50 | #include <string.h> | ||
| 51 | |||
| 52 | char * | ||
| 53 | __strsignal(num, buf) | ||
| 54 | int num; | ||
| 55 | char *buf; | ||
| 56 | { | ||
| 57 | #define UPREFIX "Unknown signal: %u" | ||
| 58 | register unsigned int signum; | ||
| 59 | |||
| 60 | #ifdef NLS | ||
| 61 | nl_catd catd ; | ||
| 62 | catd = catopen("libc", 0); | ||
| 63 | #endif | ||
| 64 | |||
| 65 | signum = num; /* convert to unsigned */ | ||
| 66 | if (signum < NSIG) { | ||
| 67 | #ifdef NLS | ||
| 68 | strcpy(buf, catgets(catd, 2, signum, | ||
| 69 | (char *)sys_siglist[signum])); | ||
| 70 | #else | ||
| 71 | return((char *)sys_siglist[signum]); | ||
| 72 | #endif | ||
| 73 | } else { | ||
| 74 | #ifdef NLS | ||
| 75 | sprintf(buf, catgets(catd, 1, 0xffff, UPREFIX), signum); | ||
| 76 | #else | ||
| 77 | sprintf(buf, UPREFIX, signum); | ||
| 78 | #endif | ||
| 79 | } | ||
| 80 | |||
| 81 | #ifdef NLS | ||
| 82 | catclose(catd); | ||
| 83 | #endif | ||
| 84 | |||
| 85 | return buf; | ||
| 86 | } | ||
diff --git a/src/lib/libc/string/bcmp.3 b/src/lib/libc/string/bcmp.3 new file mode 100644 index 0000000000..118c55c579 --- /dev/null +++ b/src/lib/libc/string/bcmp.3 | |||
| @@ -0,0 +1,72 @@ | |||
| 1 | .\" Copyright (c) 1990, 1991 The Regents of the University of California. | ||
| 2 | .\" All rights reserved. | ||
| 3 | .\" | ||
| 4 | .\" This code is derived from software contributed to Berkeley by | ||
| 5 | .\" Chris Torek. | ||
| 6 | .\" Redistribution and use in source and binary forms, with or without | ||
| 7 | .\" modification, are permitted provided that the following conditions | ||
| 8 | .\" are met: | ||
| 9 | .\" 1. Redistributions of source code must retain the above copyright | ||
| 10 | .\" notice, this list of conditions and the following disclaimer. | ||
| 11 | .\" 2. Redistributions in binary form must reproduce the above copyright | ||
| 12 | .\" notice, this list of conditions and the following disclaimer in the | ||
| 13 | .\" documentation and/or other materials provided with the distribution. | ||
| 14 | .\" 3. All advertising materials mentioning features or use of this software | ||
| 15 | .\" must display the following acknowledgement: | ||
| 16 | .\" This product includes software developed by the University of | ||
| 17 | .\" California, Berkeley and its contributors. | ||
| 18 | .\" 4. Neither the name of the University nor the names of its contributors | ||
| 19 | .\" may be used to endorse or promote products derived from this software | ||
| 20 | .\" without specific prior written permission. | ||
| 21 | .\" | ||
| 22 | .\" THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND | ||
| 23 | .\" ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE | ||
| 24 | .\" IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE | ||
| 25 | .\" ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE | ||
| 26 | .\" FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL | ||
| 27 | .\" DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS | ||
| 28 | .\" OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) | ||
| 29 | .\" HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT | ||
| 30 | .\" LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY | ||
| 31 | .\" OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF | ||
| 32 | .\" SUCH DAMAGE. | ||
| 33 | .\" | ||
| 34 | .\" from: @(#)bcmp.3 5.4 (Berkeley) 4/19/91 | ||
| 35 | .\" $Id: bcmp.3,v 1.1.1.1 1995/10/18 08:42:20 deraadt Exp $ | ||
| 36 | .\" | ||
| 37 | .Dd April 19, 1991 | ||
| 38 | .Dt BCMP 3 | ||
| 39 | .Os BSD 4.2 | ||
| 40 | .Sh NAME | ||
| 41 | .Nm bcmp | ||
| 42 | .Nd compare byte string | ||
| 43 | .Sh SYNOPSIS | ||
| 44 | .Fd #include <string.h> | ||
| 45 | .Ft int | ||
| 46 | .Fn bcmp "const void *b1" "const void *b2" "size_t len" | ||
| 47 | .Sh DESCRIPTION | ||
| 48 | The | ||
| 49 | .Fn bcmp | ||
| 50 | function | ||
| 51 | compares byte string | ||
| 52 | .Fa b1 | ||
| 53 | against byte string | ||
| 54 | .Fa b2 , | ||
| 55 | returning zero if they are identical, non-zero otherwise. | ||
| 56 | Both strings are assumed to be | ||
| 57 | .Fa len | ||
| 58 | bytes long. | ||
| 59 | Zero-length strings are always identical. | ||
| 60 | .Pp | ||
| 61 | The strings may overlap. | ||
| 62 | .Sh SEE ALSO | ||
| 63 | .Xr memcmp 3 , | ||
| 64 | .Xr strcasecmp 3 , | ||
| 65 | .Xr strcmp 3 , | ||
| 66 | .Xr strcoll 3 , | ||
| 67 | .Xr strxfrm 3 | ||
| 68 | .Sh HISTORY | ||
| 69 | A | ||
| 70 | .Fn bcmp | ||
| 71 | function first appeared in | ||
| 72 | .Bx 4.2 . | ||
diff --git a/src/lib/libc/string/bcmp.c b/src/lib/libc/string/bcmp.c new file mode 100644 index 0000000000..2cc38baee3 --- /dev/null +++ b/src/lib/libc/string/bcmp.c | |||
| @@ -0,0 +1,59 @@ | |||
| 1 | /* | ||
| 2 | * Copyright (c) 1987 Regents of the University of California. | ||
| 3 | * All rights reserved. | ||
| 4 | * | ||
| 5 | * Redistribution and use in source and binary forms, with or without | ||
| 6 | * modification, are permitted provided that the following conditions | ||
| 7 | * are met: | ||
| 8 | * 1. Redistributions of source code must retain the above copyright | ||
| 9 | * notice, this list of conditions and the following disclaimer. | ||
| 10 | * 2. Redistributions in binary form must reproduce the above copyright | ||
| 11 | * notice, this list of conditions and the following disclaimer in the | ||
| 12 | * documentation and/or other materials provided with the distribution. | ||
| 13 | * 3. All advertising materials mentioning features or use of this software | ||
| 14 | * must display the following acknowledgement: | ||
| 15 | * This product includes software developed by the University of | ||
| 16 | * California, Berkeley and its contributors. | ||
| 17 | * 4. Neither the name of the University nor the names of its contributors | ||
| 18 | * may be used to endorse or promote products derived from this software | ||
| 19 | * without specific prior written permission. | ||
| 20 | * | ||
| 21 | * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND | ||
| 22 | * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE | ||
| 23 | * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE | ||
| 24 | * ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE | ||
| 25 | * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL | ||
| 26 | * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS | ||
| 27 | * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) | ||
| 28 | * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT | ||
| 29 | * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY | ||
| 30 | * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF | ||
| 31 | * SUCH DAMAGE. | ||
| 32 | */ | ||
| 33 | |||
| 34 | #if defined(LIBC_SCCS) && !defined(lint) | ||
| 35 | /*static char *sccsid = "from: @(#)bcmp.c 5.6 (Berkeley) 2/24/91";*/ | ||
| 36 | static char *rcsid = "$Id: bcmp.c,v 1.1.1.1 1995/10/18 08:42:20 deraadt Exp $"; | ||
| 37 | #endif /* LIBC_SCCS and not lint */ | ||
| 38 | |||
| 39 | #include <string.h> | ||
| 40 | |||
| 41 | /* | ||
| 42 | * bcmp -- vax cmpc3 instruction | ||
| 43 | */ | ||
| 44 | bcmp(b1, b2, length) | ||
| 45 | const void *b1, *b2; | ||
| 46 | register size_t length; | ||
| 47 | { | ||
| 48 | register char *p1, *p2; | ||
| 49 | |||
| 50 | if (length == 0) | ||
| 51 | return(0); | ||
| 52 | p1 = (char *)b1; | ||
| 53 | p2 = (char *)b2; | ||
| 54 | do | ||
| 55 | if (*p1++ != *p2++) | ||
| 56 | break; | ||
| 57 | while (--length); | ||
| 58 | return(length); | ||
| 59 | } | ||
diff --git a/src/lib/libc/string/bcopy.3 b/src/lib/libc/string/bcopy.3 new file mode 100644 index 0000000000..6db3812caf --- /dev/null +++ b/src/lib/libc/string/bcopy.3 | |||
| @@ -0,0 +1,72 @@ | |||
| 1 | .\" Copyright (c) 1990, 1991 The Regents of the University of California. | ||
| 2 | .\" All rights reserved. | ||
| 3 | .\" | ||
| 4 | .\" This code is derived from software contributed to Berkeley by | ||
| 5 | .\" Chris Torek. | ||
| 6 | .\" | ||
| 7 | .\" Redistribution and use in source and binary forms, with or without | ||
| 8 | .\" modification, are permitted provided that the following conditions | ||
| 9 | .\" are met: | ||
| 10 | .\" 1. Redistributions of source code must retain the above copyright | ||
| 11 | .\" notice, this list of conditions and the following disclaimer. | ||
| 12 | .\" 2. Redistributions in binary form must reproduce the above copyright | ||
| 13 | .\" notice, this list of conditions and the following disclaimer in the | ||
| 14 | .\" documentation and/or other materials provided with the distribution. | ||
| 15 | .\" 3. All advertising materials mentioning features or use of this software | ||
| 16 | .\" must display the following acknowledgement: | ||
| 17 | .\" This product includes software developed by the University of | ||
| 18 | .\" California, Berkeley and its contributors. | ||
| 19 | .\" 4. Neither the name of the University nor the names of its contributors | ||
| 20 | .\" may be used to endorse or promote products derived from this software | ||
| 21 | .\" without specific prior written permission. | ||
| 22 | .\" | ||
| 23 | .\" THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND | ||
| 24 | .\" ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE | ||
| 25 | .\" IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE | ||
| 26 | .\" ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE | ||
| 27 | .\" FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL | ||
| 28 | .\" DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS | ||
| 29 | .\" OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) | ||
| 30 | .\" HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT | ||
| 31 | .\" LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY | ||
| 32 | .\" OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF | ||
| 33 | .\" SUCH DAMAGE. | ||
| 34 | .\" | ||
| 35 | .\" from: @(#)bcopy.3 5.3 (Berkeley) 4/19/91 | ||
| 36 | .\" $Id: bcopy.3,v 1.1.1.1 1995/10/18 08:42:20 deraadt Exp $ | ||
| 37 | .\" | ||
| 38 | .Dd April 19, 1991 | ||
| 39 | .Dt BCOPY 3 | ||
| 40 | .Os BSD 4.2 | ||
| 41 | .Sh NAME | ||
| 42 | .Nm bcopy | ||
| 43 | .Nd copy byte string | ||
| 44 | .Sh SYNOPSIS | ||
| 45 | .Fd #include <string.h> | ||
| 46 | .Ft void | ||
| 47 | .Fn bcopy "const void *src" "void *dst" "size_t len" | ||
| 48 | .Sh DESCRIPTION | ||
| 49 | The | ||
| 50 | .Fn bcopy | ||
| 51 | function | ||
| 52 | copies | ||
| 53 | .Fa len | ||
| 54 | bytes from string | ||
| 55 | .Fa src | ||
| 56 | to string | ||
| 57 | .Fa dst . | ||
| 58 | The two strings may overlap. | ||
| 59 | If | ||
| 60 | .Fa len | ||
| 61 | is zero, no bytes are copied. | ||
| 62 | .Sh SEE ALSO | ||
| 63 | .Xr memccpy 3 , | ||
| 64 | .Xr memcpy 3 , | ||
| 65 | .Xr memmove 3 , | ||
| 66 | .Xr strcpy 3 , | ||
| 67 | .Xr strncpy 3 | ||
| 68 | .Sh HISTORY | ||
| 69 | A | ||
| 70 | .Fn bcopy | ||
| 71 | function appeared in | ||
| 72 | .Bx 4.2 . | ||
diff --git a/src/lib/libc/string/bcopy.c b/src/lib/libc/string/bcopy.c new file mode 100644 index 0000000000..92feed66ea --- /dev/null +++ b/src/lib/libc/string/bcopy.c | |||
| @@ -0,0 +1,139 @@ | |||
| 1 | /*- | ||
| 2 | * Copyright (c) 1990 The Regents of the University of California. | ||
| 3 | * All rights reserved. | ||
| 4 | * | ||
| 5 | * This code is derived from software contributed to Berkeley by | ||
| 6 | * Chris Torek. | ||
| 7 | * | ||
| 8 | * Redistribution and use in source and binary forms, with or without | ||
| 9 | * modification, are permitted provided that the following conditions | ||
| 10 | * are met: | ||
| 11 | * 1. Redistributions of source code must retain the above copyright | ||
| 12 | * notice, this list of conditions and the following disclaimer. | ||
| 13 | * 2. Redistributions in binary form must reproduce the above copyright | ||
| 14 | * notice, this list of conditions and the following disclaimer in the | ||
| 15 | * documentation and/or other materials provided with the distribution. | ||
| 16 | * 3. All advertising materials mentioning features or use of this software | ||
| 17 | * must display the following acknowledgement: | ||
| 18 | * This product includes software developed by the University of | ||
| 19 | * California, Berkeley and its contributors. | ||
| 20 | * 4. Neither the name of the University nor the names of its contributors | ||
| 21 | * may be used to endorse or promote products derived from this software | ||
| 22 | * without specific prior written permission. | ||
| 23 | * | ||
| 24 | * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND | ||
| 25 | * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE | ||
| 26 | * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE | ||
| 27 | * ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE | ||
| 28 | * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL | ||
| 29 | * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS | ||
| 30 | * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) | ||
| 31 | * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT | ||
| 32 | * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY | ||
| 33 | * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF | ||
| 34 | * SUCH DAMAGE. | ||
| 35 | */ | ||
| 36 | |||
| 37 | #if defined(LIBC_SCCS) && !defined(lint) | ||
| 38 | /*static char *sccsid = "from: @(#)bcopy.c 5.11 (Berkeley) 6/21/91";*/ | ||
| 39 | static char *rcsid = "$Id: bcopy.c,v 1.1.1.1 1995/10/18 08:42:20 deraadt Exp $"; | ||
| 40 | #endif /* LIBC_SCCS and not lint */ | ||
| 41 | |||
| 42 | #include <string.h> | ||
| 43 | |||
| 44 | /* | ||
| 45 | * sizeof(word) MUST BE A POWER OF TWO | ||
| 46 | * SO THAT wmask BELOW IS ALL ONES | ||
| 47 | */ | ||
| 48 | typedef long word; /* "word" used for optimal copy speed */ | ||
| 49 | |||
| 50 | #define wsize sizeof(word) | ||
| 51 | #define wmask (wsize - 1) | ||
| 52 | |||
| 53 | /* | ||
| 54 | * Copy a block of memory, handling overlap. | ||
| 55 | * This is the routine that actually implements | ||
| 56 | * (the portable versions of) bcopy, memcpy, and memmove. | ||
| 57 | */ | ||
| 58 | #ifdef MEMCOPY | ||
| 59 | void * | ||
| 60 | memcpy(dst0, src0, length) | ||
| 61 | #else | ||
| 62 | #ifdef MEMMOVE | ||
| 63 | void * | ||
| 64 | memmove(dst0, src0, length) | ||
| 65 | #else | ||
| 66 | void | ||
| 67 | bcopy(src0, dst0, length) | ||
| 68 | #endif | ||
| 69 | #endif | ||
| 70 | void *dst0; | ||
| 71 | const void *src0; | ||
| 72 | register size_t length; | ||
| 73 | { | ||
| 74 | register char *dst = dst0; | ||
| 75 | register const char *src = src0; | ||
| 76 | register size_t t; | ||
| 77 | |||
| 78 | if (length == 0 || dst == src) /* nothing to do */ | ||
| 79 | goto done; | ||
| 80 | |||
| 81 | /* | ||
| 82 | * Macros: loop-t-times; and loop-t-times, t>0 | ||
| 83 | */ | ||
| 84 | #define TLOOP(s) if (t) TLOOP1(s) | ||
| 85 | #define TLOOP1(s) do { s; } while (--t) | ||
| 86 | |||
| 87 | if ((unsigned long)dst < (unsigned long)src) { | ||
| 88 | /* | ||
| 89 | * Copy forward. | ||
| 90 | */ | ||
| 91 | t = (long)src; /* only need low bits */ | ||
| 92 | if ((t | (long)dst) & wmask) { | ||
| 93 | /* | ||
| 94 | * Try to align operands. This cannot be done | ||
| 95 | * unless the low bits match. | ||
| 96 | */ | ||
| 97 | if ((t ^ (long)dst) & wmask || length < wsize) | ||
| 98 | t = length; | ||
| 99 | else | ||
| 100 | t = wsize - (t & wmask); | ||
| 101 | length -= t; | ||
| 102 | TLOOP1(*dst++ = *src++); | ||
| 103 | } | ||
| 104 | /* | ||
| 105 | * Copy whole words, then mop up any trailing bytes. | ||
| 106 | */ | ||
| 107 | t = length / wsize; | ||
| 108 | TLOOP(*(word *)dst = *(word *)src; src += wsize; dst += wsize); | ||
| 109 | t = length & wmask; | ||
| 110 | TLOOP(*dst++ = *src++); | ||
| 111 | } else { | ||
| 112 | /* | ||
| 113 | * Copy backwards. Otherwise essentially the same. | ||
| 114 | * Alignment works as before, except that it takes | ||
| 115 | * (t&wmask) bytes to align, not wsize-(t&wmask). | ||
| 116 | */ | ||
| 117 | src += length; | ||
| 118 | dst += length; | ||
| 119 | t = (long)src; | ||
| 120 | if ((t | (long)dst) & wmask) { | ||
| 121 | if ((t ^ (long)dst) & wmask || length <= wsize) | ||
| 122 | t = length; | ||
| 123 | else | ||
| 124 | t &= wmask; | ||
| 125 | length -= t; | ||
| 126 | TLOOP1(*--dst = *--src); | ||
| 127 | } | ||
| 128 | t = length / wsize; | ||
| 129 | TLOOP(src -= wsize; dst -= wsize; *(word *)dst = *(word *)src); | ||
| 130 | t = length & wmask; | ||
| 131 | TLOOP(*--dst = *--src); | ||
| 132 | } | ||
| 133 | done: | ||
| 134 | #if defined(MEMCOPY) || defined(MEMMOVE) | ||
| 135 | return (dst0); | ||
| 136 | #else | ||
| 137 | return; | ||
| 138 | #endif | ||
| 139 | } | ||
diff --git a/src/lib/libc/string/bm.3 b/src/lib/libc/string/bm.3 new file mode 100644 index 0000000000..2264a6a1c4 --- /dev/null +++ b/src/lib/libc/string/bm.3 | |||
| @@ -0,0 +1,114 @@ | |||
| 1 | .\" Copyright (c) 1994 | ||
| 2 | .\" The Regents of the University of California. All rights reserved. | ||
| 3 | .\" | ||
| 4 | .\" This code is derived from software contributed to Berkeley by | ||
| 5 | .\" Andrew Hume of AT&T Bell Laboratories. | ||
| 6 | .\" | ||
| 7 | .\" Redistribution and use in source and binary forms, with or without | ||
| 8 | .\" modification, are permitted provided that the following conditions | ||
| 9 | .\" are met: | ||
| 10 | .\" 1. Redistributions of source code must retain the above copyright | ||
| 11 | .\" notice, this list of conditions and the following disclaimer. | ||
| 12 | .\" 2. Redistributions in binary form must reproduce the above copyright | ||
| 13 | .\" notice, this list of conditions and the following disclaimer in the | ||
| 14 | .\" documentation and/or other materials provided with the distribution. | ||
| 15 | .\" 3. All advertising materials mentioning features or use of this software | ||
| 16 | .\" must display the following acknowledgement: | ||
| 17 | .\" This product includes software developed by the University of | ||
| 18 | .\" California, Berkeley and its contributors. | ||
| 19 | .\" 4. Neither the name of the University nor the names of its contributors | ||
| 20 | .\" may be used to endorse or promote products derived from this software | ||
| 21 | .\" without specific prior written permission. | ||
| 22 | .\" | ||
| 23 | .\" THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND | ||
| 24 | .\" ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE | ||
| 25 | .\" IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE | ||
| 26 | .\" ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE | ||
| 27 | .\" FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL | ||
| 28 | .\" DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS | ||
| 29 | .\" OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) | ||
| 30 | .\" HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT | ||
| 31 | .\" LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY | ||
| 32 | .\" OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF | ||
| 33 | .\" SUCH DAMAGE. | ||
| 34 | .\" | ||
| 35 | .\" from: @(#)bm.3 8.4 (Berkeley) 6/21/94 | ||
| 36 | .\" $Id: bm.3,v 1.1.1.1 1995/10/18 08:42:20 deraadt Exp $ | ||
| 37 | .\" | ||
| 38 | .TH BM 3 | ||
| 39 | .SH NAME | ||
| 40 | bm_comp, bm_exec, bm_free \- Boyer-Moore string search | ||
| 41 | .SH SYNOPSIS | ||
| 42 | .ft B | ||
| 43 | #include <sys/types.h> | ||
| 44 | .br | ||
| 45 | #include <bm.h> | ||
| 46 | .sp | ||
| 47 | bm_pat * | ||
| 48 | .br | ||
| 49 | bm_comp(u_char *pattern, size_t patlen, u_char freq[256]); | ||
| 50 | .sp | ||
| 51 | u_char * | ||
| 52 | .br | ||
| 53 | bm_exec(bm_pat *pdesc, u_char *text, size_t len); | ||
| 54 | .sp | ||
| 55 | void | ||
| 56 | .br | ||
| 57 | bm_free(bm_pat *pdesc); | ||
| 58 | .SH DESCRIPTION | ||
| 59 | These routines implement an efficient mechanism to find an | ||
| 60 | occurrence of a byte string within another byte string. | ||
| 61 | .PP | ||
| 62 | .I Bm_comp | ||
| 63 | evaluates the | ||
| 64 | .I patlen | ||
| 65 | bytes starting at | ||
| 66 | .IR pattern , | ||
| 67 | and returns a pointer to a structure describing them. | ||
| 68 | The bytes referenced by | ||
| 69 | .I pattern | ||
| 70 | may be of any value. | ||
| 71 | .PP | ||
| 72 | The search takes advantage of the frequency distribution of the | ||
| 73 | bytes in the text to be searched. | ||
| 74 | If specified, | ||
| 75 | .I freq | ||
| 76 | should be an array of 256 values, | ||
| 77 | with higher values indicating that the corresponding character occurs | ||
| 78 | more frequently. | ||
| 79 | (A less than optimal frequency distribution can only result in less | ||
| 80 | than optimal performance, not incorrect results.) | ||
| 81 | If | ||
| 82 | .I freq | ||
| 83 | is NULL, | ||
| 84 | a system default table is used. | ||
| 85 | .PP | ||
| 86 | .I Bm_exec | ||
| 87 | returns a pointer to the leftmost occurrence of the string given to | ||
| 88 | .I bm_comp | ||
| 89 | within | ||
| 90 | .IR text , | ||
| 91 | or NULL if none occurs. | ||
| 92 | The number of bytes in | ||
| 93 | .I text | ||
| 94 | must be specified by | ||
| 95 | .IR len . | ||
| 96 | .PP | ||
| 97 | Space allocated for the returned description is discarded | ||
| 98 | by calling | ||
| 99 | .I bm_free | ||
| 100 | with the returned description as an argument. | ||
| 101 | .PP | ||
| 102 | The asymptotic speed of | ||
| 103 | .I bm_exec | ||
| 104 | is | ||
| 105 | .RI O( len / patlen ). | ||
| 106 | .PP | ||
| 107 | .SH "SEE ALSO" | ||
| 108 | .IR regexp (3), | ||
| 109 | .IR strstr (3) | ||
| 110 | .sp | ||
| 111 | .IR "Fast String Searching" , | ||
| 112 | Hume and Sunday, | ||
| 113 | Software Practice and Experience, | ||
| 114 | Vol. 21, 11 (November 1991) pp. 1221-48. | ||
diff --git a/src/lib/libc/string/bm.c b/src/lib/libc/string/bm.c new file mode 100644 index 0000000000..68eac22ecc --- /dev/null +++ b/src/lib/libc/string/bm.c | |||
| @@ -0,0 +1,220 @@ | |||
| 1 | /*- | ||
| 2 | * Copyright (c) 1994 | ||
| 3 | * The Regents of the University of California. All rights reserved. | ||
| 4 | * | ||
| 5 | * This code is derived from software contributed to Berkeley by | ||
| 6 | * Andrew Hume of AT&T Bell Laboratories. | ||
| 7 | * | ||
| 8 | * Redistribution and use in source and binary forms, with or without | ||
| 9 | * modification, are permitted provided that the following conditions | ||
| 10 | * are met: | ||
| 11 | * 1. Redistributions of source code must retain the above copyright | ||
| 12 | * notice, this list of conditions and the following disclaimer. | ||
| 13 | * 2. Redistributions in binary form must reproduce the above copyright | ||
| 14 | * notice, this list of conditions and the following disclaimer in the | ||
| 15 | * documentation and/or other materials provided with the distribution. | ||
| 16 | * 3. All advertising materials mentioning features or use of this software | ||
| 17 | * must display the following acknowledgement: | ||
| 18 | * This product includes software developed by the University of | ||
| 19 | * California, Berkeley and its contributors. | ||
| 20 | * 4. Neither the name of the University nor the names of its contributors | ||
| 21 | * may be used to endorse or promote products derived from this software | ||
| 22 | * without specific prior written permission. | ||
| 23 | * | ||
| 24 | * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND | ||
| 25 | * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE | ||
| 26 | * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE | ||
| 27 | * ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE | ||
| 28 | * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL | ||
| 29 | * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS | ||
| 30 | * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) | ||
| 31 | * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT | ||
| 32 | * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY | ||
| 33 | * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF | ||
| 34 | * SUCH DAMAGE. | ||
| 35 | */ | ||
| 36 | |||
| 37 | #ifndef lint | ||
| 38 | /* from: static char sccsid[] = "@(#)bm.c 8.7 (Berkeley) 6/21/94"; */ | ||
| 39 | static char *rcsid = "$Id: bm.c,v 1.1.1.1 1995/10/18 08:42:20 deraadt Exp $"; | ||
| 40 | #endif /* not lint */ | ||
| 41 | |||
| 42 | #include <sys/types.h> | ||
| 43 | |||
| 44 | #include <bm.h> | ||
| 45 | #include <errno.h> | ||
| 46 | #include <stdlib.h> | ||
| 47 | #include <string.h> | ||
| 48 | |||
| 49 | /* | ||
| 50 | * XXX | ||
| 51 | * The default frequency table starts at 99 and counts down. The default | ||
| 52 | * table should probably be oriented toward text, and will necessarily be | ||
| 53 | * locale specific. This one is for English. It was derived from the | ||
| 54 | * OSF/1 and 4.4BSD formatted and unformatted manual pages, and about 100Mb | ||
| 55 | * of email and random text. Change it if you can find something better. | ||
| 56 | */ | ||
| 57 | static u_char const freq_def[256] = { | ||
| 58 | 0, 0, 0, 0, 0, 0, 0, 0, | ||
| 59 | 0, 77, 90, 0, 0, 0, 0, 0, | ||
| 60 | 0, 0, 0, 0, 0, 0, 0, 0, | ||
| 61 | 0, 0, 0, 0, 0, 0, 0, 0, | ||
| 62 | 99, 28, 42, 27, 16, 14, 20, 51, | ||
| 63 | 66, 65, 59, 24, 75, 76, 84, 56, | ||
| 64 | 72, 74, 64, 55, 54, 47, 41, 37, | ||
| 65 | 44, 61, 70, 43, 23, 53, 49, 22, | ||
| 66 | 33, 58, 40, 46, 45, 57, 60, 26, | ||
| 67 | 30, 63, 21, 12, 32, 50, 38, 39, | ||
| 68 | 34, 11, 48, 67, 62, 35, 15, 29, | ||
| 69 | 71, 18, 9, 17, 25, 13, 10, 52, | ||
| 70 | 36, 95, 78, 86, 87, 98, 82, 80, | ||
| 71 | 88, 94, 19, 68, 89, 83, 93, 96, | ||
| 72 | 81, 7, 91, 92, 97, 85, 69, 73, | ||
| 73 | 31, 79, 8, 5, 4, 6, 3, 0, | ||
| 74 | 0, 0, 0, 0, 0, 0, 0, 0, | ||
| 75 | 0, 0, 0, 0, 0, 0, 0, 0, | ||
| 76 | 0, 0, 0, 0, 0, 0, 0, 0, | ||
| 77 | 0, 0, 0, 0, 0, 0, 0, 0, | ||
| 78 | 0, 0, 0, 0, 0, 0, 0, 0, | ||
| 79 | 0, 0, 0, 0, 0, 0, 0, 0, | ||
| 80 | 0, 0, 0, 0, 0, 0, 0, 0, | ||
| 81 | 0, 0, 0, 0, 0, 0, 0, 0, | ||
| 82 | 0, 0, 0, 0, 0, 0, 0, 0, | ||
| 83 | 0, 0, 0, 0, 0, 0, 0, 0, | ||
| 84 | 0, 0, 0, 0, 0, 0, 0, 0, | ||
| 85 | 0, 0, 0, 0, 0, 0, 0, 0, | ||
| 86 | 0, 0, 0, 0, 0, 0, 0, 0, | ||
| 87 | 0, 0, 0, 0, 0, 0, 0, 0, | ||
| 88 | 0, 0, 0, 0, 0, 0, 0, 0, | ||
| 89 | 0, 0, 0, 0, 0, 0, 0, 0, | ||
| 90 | }; | ||
| 91 | |||
| 92 | bm_pat * | ||
| 93 | bm_comp(pb, len, freq) | ||
| 94 | u_char const *pb; | ||
| 95 | size_t len; | ||
| 96 | u_char const *freq; | ||
| 97 | { | ||
| 98 | register u_char const *pe, *p; | ||
| 99 | register size_t *d, r; | ||
| 100 | register int j; | ||
| 101 | int sv_errno; | ||
| 102 | bm_pat *pat; | ||
| 103 | |||
| 104 | if (len == 0) { | ||
| 105 | errno = EINVAL; | ||
| 106 | return (NULL); | ||
| 107 | } | ||
| 108 | if ((pat = malloc(sizeof(*pat))) == NULL) | ||
| 109 | return (NULL); | ||
| 110 | pat->pat = NULL; | ||
| 111 | pat->delta = NULL; | ||
| 112 | |||
| 113 | pat->patlen = len; /* copy pattern */ | ||
| 114 | if ((pat->pat = malloc(pat->patlen)) == NULL) | ||
| 115 | goto mem; | ||
| 116 | memcpy(pat->pat, pb, pat->patlen); | ||
| 117 | /* get skip delta */ | ||
| 118 | if ((pat->delta = malloc(256 * sizeof(*d))) == NULL) | ||
| 119 | goto mem; | ||
| 120 | for (j = 0, d = pat->delta; j < 256; j++) | ||
| 121 | d[j] = pat->patlen; | ||
| 122 | for (pe = pb + pat->patlen - 1; pb <= pe; pb++) | ||
| 123 | d[*pb] = pe - pb; | ||
| 124 | |||
| 125 | if (freq == NULL) /* default freq table */ | ||
| 126 | freq = freq_def; | ||
| 127 | r = 0; /* get guard */ | ||
| 128 | for (pb = pat->pat, pe = pb + pat->patlen - 1; pb < pe; pb++) | ||
| 129 | if (freq[*pb] < freq[pat->pat[r]]) | ||
| 130 | r = pb - pat->pat; | ||
| 131 | pat->rarec = pat->pat[r]; | ||
| 132 | pat->rareoff = r - (pat->patlen - 1); | ||
| 133 | |||
| 134 | /* get md2 shift */ | ||
| 135 | for (pe = pat->pat + pat->patlen - 1, p = pe - 1; p >= pat->pat; p--) | ||
| 136 | if (*p == *pe) | ||
| 137 | break; | ||
| 138 | |||
| 139 | /* *p is first leftward reoccurrence of *pe */ | ||
| 140 | pat->md2 = pe - p; | ||
| 141 | return (pat); | ||
| 142 | |||
| 143 | mem: sv_errno = errno; | ||
| 144 | bm_free(pat); | ||
| 145 | errno = sv_errno; | ||
| 146 | return (NULL); | ||
| 147 | } | ||
| 148 | |||
| 149 | void | ||
| 150 | bm_free(pat) | ||
| 151 | bm_pat *pat; | ||
| 152 | { | ||
| 153 | if (pat->pat != NULL) | ||
| 154 | free(pat->pat); | ||
| 155 | if (pat->delta != NULL) | ||
| 156 | free(pat->delta); | ||
| 157 | free(pat); | ||
| 158 | } | ||
| 159 | |||
| 160 | u_char * | ||
| 161 | bm_exec(pat, base, n) | ||
| 162 | bm_pat *pat; | ||
| 163 | u_char *base; | ||
| 164 | size_t n; | ||
| 165 | { | ||
| 166 | register u_char *e, *ep, *p, *q, *s; | ||
| 167 | register size_t *d0, k, md2, n1, ro; | ||
| 168 | register int rc; | ||
| 169 | |||
| 170 | if (n == 0) | ||
| 171 | return (NULL); | ||
| 172 | |||
| 173 | d0 = pat->delta; | ||
| 174 | n1 = pat->patlen - 1; | ||
| 175 | md2 = pat->md2; | ||
| 176 | ro = pat->rareoff; | ||
| 177 | rc = pat->rarec; | ||
| 178 | ep = pat->pat + pat->patlen - 1; | ||
| 179 | s = base + (pat->patlen - 1); | ||
| 180 | |||
| 181 | /* fast loop up to n - 3 * patlen */ | ||
| 182 | e = base + n - 3 * pat->patlen; | ||
| 183 | while (s < e) { | ||
| 184 | k = d0[*s]; /* ufast skip loop */ | ||
| 185 | while (k) { | ||
| 186 | k = d0[*(s += k)]; | ||
| 187 | k = d0[*(s += k)]; | ||
| 188 | } | ||
| 189 | if (s >= e) | ||
| 190 | break; | ||
| 191 | if (s[ro] != rc) /* guard test */ | ||
| 192 | goto mismatch1; | ||
| 193 | /* fwd match */ | ||
| 194 | for (p = pat->pat, q = s - n1; p < ep;) | ||
| 195 | if (*q++ != *p++) | ||
| 196 | goto mismatch1; | ||
| 197 | return (s - n1); | ||
| 198 | |||
| 199 | mismatch1: s += md2; /* md2 shift */ | ||
| 200 | } | ||
| 201 | |||
| 202 | /* slow loop up to end */ | ||
| 203 | e = base + n; | ||
| 204 | while (s < e) { | ||
| 205 | s += d0[*s]; /* step */ | ||
| 206 | if (s >= e) | ||
| 207 | break; | ||
| 208 | if (s[ro] != rc) /* guard test */ | ||
| 209 | goto mismatch2; | ||
| 210 | /* fwd match */ | ||
| 211 | for (p = pat->pat, q = s - n1; p <= ep;) | ||
| 212 | if (*q++ != *p++) | ||
| 213 | goto mismatch2; | ||
| 214 | return (s - n1); | ||
| 215 | |||
| 216 | mismatch2: s += md2; /* md2 shift */ | ||
| 217 | } | ||
| 218 | |||
| 219 | return (NULL); | ||
| 220 | } | ||
diff --git a/src/lib/libc/string/bstring.3 b/src/lib/libc/string/bstring.3 new file mode 100644 index 0000000000..12fcfb0cc1 --- /dev/null +++ b/src/lib/libc/string/bstring.3 | |||
| @@ -0,0 +1,110 @@ | |||
| 1 | .\" Copyright (c) 1990, 1991 The Regents of the University of California. | ||
| 2 | .\" All rights reserved. | ||
| 3 | .\" | ||
| 4 | .\" This code is derived from software contributed to Berkeley by | ||
| 5 | .\" Chris Torek. | ||
| 6 | .\" Redistribution and use in source and binary forms, with or without | ||
| 7 | .\" modification, are permitted provided that the following conditions | ||
| 8 | .\" are met: | ||
| 9 | .\" 1. Redistributions of source code must retain the above copyright | ||
| 10 | .\" notice, this list of conditions and the following disclaimer. | ||
| 11 | .\" 2. Redistributions in binary form must reproduce the above copyright | ||
| 12 | .\" notice, this list of conditions and the following disclaimer in the | ||
| 13 | .\" documentation and/or other materials provided with the distribution. | ||
| 14 | .\" 3. All advertising materials mentioning features or use of this software | ||
| 15 | .\" must display the following acknowledgement: | ||
| 16 | .\" This product includes software developed by the University of | ||
| 17 | .\" California, Berkeley and its contributors. | ||
| 18 | .\" 4. Neither the name of the University nor the names of its contributors | ||
| 19 | .\" may be used to endorse or promote products derived from this software | ||
| 20 | .\" without specific prior written permission. | ||
| 21 | .\" | ||
| 22 | .\" THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND | ||
| 23 | .\" ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE | ||
| 24 | .\" IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE | ||
| 25 | .\" ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE | ||
| 26 | .\" FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL | ||
| 27 | .\" DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS | ||
| 28 | .\" OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) | ||
| 29 | .\" HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT | ||
| 30 | .\" LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY | ||
| 31 | .\" OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF | ||
| 32 | .\" SUCH DAMAGE. | ||
| 33 | .\" | ||
| 34 | .\" from: @(#)bstring.3 6.8 (Berkeley) 4/19/91 | ||
| 35 | .\" $Id: bstring.3,v 1.1.1.1 1995/10/18 08:42:21 deraadt Exp $ | ||
| 36 | .\" | ||
| 37 | .Dd April 19, 1991 | ||
| 38 | .Dt BSTRING 3 | ||
| 39 | .Os | ||
| 40 | .Sh NAME | ||
| 41 | .Nm bcmp , | ||
| 42 | .Nm bcopy , | ||
| 43 | .Nm bzero , | ||
| 44 | .Nm memccpy , | ||
| 45 | .Nm memchr , | ||
| 46 | .Nm memcmp , | ||
| 47 | .Nm memcpy , | ||
| 48 | .Nm memmove, | ||
| 49 | .Nm memset | ||
| 50 | .Nd byte string operations | ||
| 51 | .Sh SYNOPSIS | ||
| 52 | .Fd #include <string.h> | ||
| 53 | .Ft int | ||
| 54 | .Fn bcmp "const void *b1" "const void *b2" "size_t len" | ||
| 55 | .Ft void | ||
| 56 | .Fn bcopy "const void *src" "void *dst" "size_t len" | ||
| 57 | .Ft void | ||
| 58 | .Fn bzero "void *b" "size_t len" | ||
| 59 | .Ft void * | ||
| 60 | .Fn memchr "const void *b" "int c" "size_t len" | ||
| 61 | .Ft int | ||
| 62 | .Fn memcmp "const void *b1" "const void *b2" "size_t len" | ||
| 63 | .Ft void * | ||
| 64 | .Fn memccpy "void *dst" "const void *src" "int c" "size_t len" | ||
| 65 | .Ft void * | ||
| 66 | .Fn memcpy "void *dst" "const void *src" "size_t len" | ||
| 67 | .Ft void * | ||
| 68 | .Fn memmove "void *dst" "const void *src" "size_t len" | ||
| 69 | .Ft void * | ||
| 70 | .Fn memset "void *b" "int c" "size_t len" | ||
| 71 | .Sh DESCRIPTION | ||
| 72 | These functions operate on variable length strings of bytes. | ||
| 73 | They do not check for terminating null bytes as the routines | ||
| 74 | listed in | ||
| 75 | .Xr string 3 | ||
| 76 | do. | ||
| 77 | .Pp | ||
| 78 | See the specific manual pages for more information. | ||
| 79 | .Sh SEE ALSO | ||
| 80 | .Xr bcmp 3 , | ||
| 81 | .Xr bcopy 3 , | ||
| 82 | .Xr bzero 3 , | ||
| 83 | .Xr memccpy 3 , | ||
| 84 | .Xr memchr 3 , | ||
| 85 | .Xr memcmp 3 , | ||
| 86 | .Xr memcpy 3 , | ||
| 87 | .Xr memmove 3 , | ||
| 88 | .Xr memset 3 | ||
| 89 | .Sh STANDARDS | ||
| 90 | The functions | ||
| 91 | .Fn memchr , | ||
| 92 | .Fn memcmp , | ||
| 93 | .Fn memcpy , | ||
| 94 | .Fn memmove , | ||
| 95 | and | ||
| 96 | .Fn memset | ||
| 97 | conform to | ||
| 98 | .St -ansiC . | ||
| 99 | .Sh HISTORY | ||
| 100 | The functions | ||
| 101 | .Fn bzero | ||
| 102 | and | ||
| 103 | .Fn memccpy | ||
| 104 | appeared in | ||
| 105 | .Bx 4.3 ; | ||
| 106 | the functions | ||
| 107 | .Fn bcmp , | ||
| 108 | .Fn bcopy , | ||
| 109 | appeared in | ||
| 110 | .Bx 4.2 . | ||
diff --git a/src/lib/libc/string/bzero.3 b/src/lib/libc/string/bzero.3 new file mode 100644 index 0000000000..4f0141e051 --- /dev/null +++ b/src/lib/libc/string/bzero.3 | |||
| @@ -0,0 +1,68 @@ | |||
| 1 | .\" Copyright (c) 1990, 1991 The Regents of the University of California. | ||
| 2 | .\" All rights reserved. | ||
| 3 | .\" | ||
| 4 | .\" This code is derived from software contributed to Berkeley by | ||
| 5 | .\" Chris Torek. | ||
| 6 | .\" Redistribution and use in source and binary forms, with or without | ||
| 7 | .\" modification, are permitted provided that the following conditions | ||
| 8 | .\" are met: | ||
| 9 | .\" 1. Redistributions of source code must retain the above copyright | ||
| 10 | .\" notice, this list of conditions and the following disclaimer. | ||
| 11 | .\" 2. Redistributions in binary form must reproduce the above copyright | ||
| 12 | .\" notice, this list of conditions and the following disclaimer in the | ||
| 13 | .\" documentation and/or other materials provided with the distribution. | ||
| 14 | .\" 3. All advertising materials mentioning features or use of this software | ||
| 15 | .\" must display the following acknowledgement: | ||
| 16 | .\" This product includes software developed by the University of | ||
| 17 | .\" California, Berkeley and its contributors. | ||
| 18 | .\" 4. Neither the name of the University nor the names of its contributors | ||
| 19 | .\" may be used to endorse or promote products derived from this software | ||
| 20 | .\" without specific prior written permission. | ||
| 21 | .\" | ||
| 22 | .\" THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND | ||
| 23 | .\" ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE | ||
| 24 | .\" IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE | ||
| 25 | .\" ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE | ||
| 26 | .\" FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL | ||
| 27 | .\" DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS | ||
| 28 | .\" OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) | ||
| 29 | .\" HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT | ||
| 30 | .\" LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY | ||
| 31 | .\" OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF | ||
| 32 | .\" SUCH DAMAGE. | ||
| 33 | .\" | ||
| 34 | .\" from: @(#)bzero.3 5.3 (Berkeley) 4/19/91 | ||
| 35 | .\" $Id: bzero.3,v 1.1.1.1 1995/10/18 08:42:21 deraadt Exp $ | ||
| 36 | .\" | ||
| 37 | .Dd April 19, 1991 | ||
| 38 | .Dt BZERO 3 | ||
| 39 | .Os BSD 4.3 | ||
| 40 | .Sh NAME | ||
| 41 | .Nm bzero | ||
| 42 | .Nd write zeroes to a byte string | ||
| 43 | .Sh SYNOPSIS | ||
| 44 | .Fd #include <string.h> | ||
| 45 | .Ft void | ||
| 46 | .Fn bzero "void *b" "size_t len" | ||
| 47 | .Sh DESCRIPTION | ||
| 48 | The | ||
| 49 | .Fn bzero | ||
| 50 | function | ||
| 51 | writes | ||
| 52 | .Fa len | ||
| 53 | zero bytes to the string | ||
| 54 | .Fa b . | ||
| 55 | If | ||
| 56 | .Fa len | ||
| 57 | is zero, | ||
| 58 | .Fn bzero | ||
| 59 | does nothing. | ||
| 60 | .Sh SEE ALSO | ||
| 61 | .Xr memset 3 , | ||
| 62 | .Xr swab 3 | ||
| 63 | .Sh HISTORY | ||
| 64 | A | ||
| 65 | .Fn bzero | ||
| 66 | function | ||
| 67 | appeared in | ||
| 68 | .Bx 4.3 . | ||
diff --git a/src/lib/libc/string/bzero.c b/src/lib/libc/string/bzero.c new file mode 100644 index 0000000000..4865e396ef --- /dev/null +++ b/src/lib/libc/string/bzero.c | |||
| @@ -0,0 +1,53 @@ | |||
| 1 | /* | ||
| 2 | * Copyright (c) 1987 Regents of the University of California. | ||
| 3 | * All rights reserved. | ||
| 4 | * | ||
| 5 | * Redistribution and use in source and binary forms, with or without | ||
| 6 | * modification, are permitted provided that the following conditions | ||
| 7 | * are met: | ||
| 8 | * 1. Redistributions of source code must retain the above copyright | ||
| 9 | * notice, this list of conditions and the following disclaimer. | ||
| 10 | * 2. Redistributions in binary form must reproduce the above copyright | ||
| 11 | * notice, this list of conditions and the following disclaimer in the | ||
| 12 | * documentation and/or other materials provided with the distribution. | ||
| 13 | * 3. All advertising materials mentioning features or use of this software | ||
| 14 | * must display the following acknowledgement: | ||
| 15 | * This product includes software developed by the University of | ||
| 16 | * California, Berkeley and its contributors. | ||
| 17 | * 4. Neither the name of the University nor the names of its contributors | ||
| 18 | * may be used to endorse or promote products derived from this software | ||
| 19 | * without specific prior written permission. | ||
| 20 | * | ||
| 21 | * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND | ||
| 22 | * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE | ||
| 23 | * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE | ||
| 24 | * ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE | ||
| 25 | * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL | ||
| 26 | * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS | ||
| 27 | * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) | ||
| 28 | * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT | ||
| 29 | * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY | ||
| 30 | * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF | ||
| 31 | * SUCH DAMAGE. | ||
| 32 | */ | ||
| 33 | |||
| 34 | #if defined(LIBC_SCCS) && !defined(lint) | ||
| 35 | /*static char *sccsid = "from: @(#)bzero.c 5.7 (Berkeley) 2/24/91";*/ | ||
| 36 | static char *rcsid = "$Id: bzero.c,v 1.1.1.1 1995/10/18 08:42:21 deraadt Exp $"; | ||
| 37 | #endif /* LIBC_SCCS and not lint */ | ||
| 38 | |||
| 39 | #include <string.h> | ||
| 40 | |||
| 41 | /* | ||
| 42 | * bzero -- vax movc5 instruction | ||
| 43 | */ | ||
| 44 | void | ||
| 45 | bzero(b, length) | ||
| 46 | void *b; | ||
| 47 | register size_t length; | ||
| 48 | { | ||
| 49 | register char *p; | ||
| 50 | |||
| 51 | for (p = b; length--;) | ||
| 52 | *p++ = '\0'; | ||
| 53 | } | ||
diff --git a/src/lib/libc/string/ffs.3 b/src/lib/libc/string/ffs.3 new file mode 100644 index 0000000000..6464bea2b1 --- /dev/null +++ b/src/lib/libc/string/ffs.3 | |||
| @@ -0,0 +1,63 @@ | |||
| 1 | .\" Copyright (c) 1990, 1991 The Regents of the University of California. | ||
| 2 | .\" All rights reserved. | ||
| 3 | .\" | ||
| 4 | .\" This code is derived from software contributed to Berkeley by | ||
| 5 | .\" Chris Torek. | ||
| 6 | .\" Redistribution and use in source and binary forms, with or without | ||
| 7 | .\" modification, are permitted provided that the following conditions | ||
| 8 | .\" are met: | ||
| 9 | .\" 1. Redistributions of source code must retain the above copyright | ||
| 10 | .\" notice, this list of conditions and the following disclaimer. | ||
| 11 | .\" 2. Redistributions in binary form must reproduce the above copyright | ||
| 12 | .\" notice, this list of conditions and the following disclaimer in the | ||
| 13 | .\" documentation and/or other materials provided with the distribution. | ||
| 14 | .\" 3. All advertising materials mentioning features or use of this software | ||
| 15 | .\" must display the following acknowledgement: | ||
| 16 | .\" This product includes software developed by the University of | ||
| 17 | .\" California, Berkeley and its contributors. | ||
| 18 | .\" 4. Neither the name of the University nor the names of its contributors | ||
| 19 | .\" may be used to endorse or promote products derived from this software | ||
| 20 | .\" without specific prior written permission. | ||
| 21 | .\" | ||
| 22 | .\" THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND | ||
| 23 | .\" ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE | ||
| 24 | .\" IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE | ||
| 25 | .\" ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE | ||
| 26 | .\" FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL | ||
| 27 | .\" DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS | ||
| 28 | .\" OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) | ||
| 29 | .\" HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT | ||
| 30 | .\" LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY | ||
| 31 | .\" OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF | ||
| 32 | .\" SUCH DAMAGE. | ||
| 33 | .\" | ||
| 34 | .\" from: @(#)ffs.3 5.3 (Berkeley) 4/19/91 | ||
| 35 | .\" $Id: ffs.3,v 1.1.1.1 1995/10/18 08:42:21 deraadt Exp $ | ||
| 36 | .\" | ||
| 37 | .Dd April 19, 1991 | ||
| 38 | .Dt FFS 3 | ||
| 39 | .Os | ||
| 40 | .Sh NAME | ||
| 41 | .Nm ffs | ||
| 42 | .Nd find first bit set in a bit string | ||
| 43 | .Sh SYNOPSIS | ||
| 44 | .Fd #include <string.h> | ||
| 45 | .Ft int | ||
| 46 | .Fn ffs "int value" | ||
| 47 | .Sh DESCRIPTION | ||
| 48 | The | ||
| 49 | .Fn ffs | ||
| 50 | function | ||
| 51 | finds the first bit set in | ||
| 52 | .Fa value | ||
| 53 | and returns the index of that bit. | ||
| 54 | Bits are numbered starting from 1, starting at the right-most | ||
| 55 | bit. | ||
| 56 | A return value of 0 means that the argument was zero. | ||
| 57 | .Sh SEE ALSO | ||
| 58 | .Xr bitstring 3 | ||
| 59 | .Sh HISTORY | ||
| 60 | The | ||
| 61 | .Fn ffs | ||
| 62 | function appeared in | ||
| 63 | .Bx 4.3 . | ||
diff --git a/src/lib/libc/string/ffs.c b/src/lib/libc/string/ffs.c new file mode 100644 index 0000000000..42bc87ddea --- /dev/null +++ b/src/lib/libc/string/ffs.c | |||
| @@ -0,0 +1,55 @@ | |||
| 1 | /*- | ||
| 2 | * Copyright (c) 1990 The Regents of the University of California. | ||
| 3 | * All rights reserved. | ||
| 4 | * | ||
| 5 | * Redistribution and use in source and binary forms, with or without | ||
| 6 | * modification, are permitted provided that the following conditions | ||
| 7 | * are met: | ||
| 8 | * 1. Redistributions of source code must retain the above copyright | ||
| 9 | * notice, this list of conditions and the following disclaimer. | ||
| 10 | * 2. Redistributions in binary form must reproduce the above copyright | ||
| 11 | * notice, this list of conditions and the following disclaimer in the | ||
| 12 | * documentation and/or other materials provided with the distribution. | ||
| 13 | * 3. All advertising materials mentioning features or use of this software | ||
| 14 | * must display the following acknowledgement: | ||
| 15 | * This product includes software developed by the University of | ||
| 16 | * California, Berkeley and its contributors. | ||
| 17 | * 4. Neither the name of the University nor the names of its contributors | ||
| 18 | * may be used to endorse or promote products derived from this software | ||
| 19 | * without specific prior written permission. | ||
| 20 | * | ||
| 21 | * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND | ||
| 22 | * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE | ||
| 23 | * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE | ||
| 24 | * ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE | ||
| 25 | * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL | ||
| 26 | * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS | ||
| 27 | * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) | ||
| 28 | * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT | ||
| 29 | * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY | ||
| 30 | * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF | ||
| 31 | * SUCH DAMAGE. | ||
| 32 | */ | ||
| 33 | |||
| 34 | #if defined(LIBC_SCCS) && !defined(lint) | ||
| 35 | /*static char *sccsid = "from: @(#)ffs.c 5.4 (Berkeley) 5/17/90";*/ | ||
| 36 | static char *rcsid = "$Id: ffs.c,v 1.1.1.1 1995/10/18 08:42:21 deraadt Exp $"; | ||
| 37 | #endif /* LIBC_SCCS and not lint */ | ||
| 38 | |||
| 39 | #include <string.h> | ||
| 40 | |||
| 41 | /* | ||
| 42 | * ffs -- vax ffs instruction | ||
| 43 | */ | ||
| 44 | int | ||
| 45 | ffs(mask) | ||
| 46 | register int mask; | ||
| 47 | { | ||
| 48 | register int bit; | ||
| 49 | |||
| 50 | if (mask == 0) | ||
| 51 | return(0); | ||
| 52 | for (bit = 1; !(mask & 1); bit++) | ||
| 53 | mask >>= 1; | ||
| 54 | return(bit); | ||
| 55 | } | ||
diff --git a/src/lib/libc/string/index.3 b/src/lib/libc/string/index.3 new file mode 100644 index 0000000000..847b03628b --- /dev/null +++ b/src/lib/libc/string/index.3 | |||
| @@ -0,0 +1,83 @@ | |||
| 1 | .\" Copyright (c) 1990, 1991 The Regents of the University of California. | ||
| 2 | .\" All rights reserved. | ||
| 3 | .\" | ||
| 4 | .\" This code is derived from software contributed to Berkeley by | ||
| 5 | .\" Chris Torek. | ||
| 6 | .\" Redistribution and use in source and binary forms, with or without | ||
| 7 | .\" modification, are permitted provided that the following conditions | ||
| 8 | .\" are met: | ||
| 9 | .\" 1. Redistributions of source code must retain the above copyright | ||
| 10 | .\" notice, this list of conditions and the following disclaimer. | ||
| 11 | .\" 2. Redistributions in binary form must reproduce the above copyright | ||
| 12 | .\" notice, this list of conditions and the following disclaimer in the | ||
| 13 | .\" documentation and/or other materials provided with the distribution. | ||
| 14 | .\" 3. All advertising materials mentioning features or use of this software | ||
| 15 | .\" must display the following acknowledgement: | ||
| 16 | .\" This product includes software developed by the University of | ||
| 17 | .\" California, Berkeley and its contributors. | ||
| 18 | .\" 4. Neither the name of the University nor the names of its contributors | ||
| 19 | .\" may be used to endorse or promote products derived from this software | ||
| 20 | .\" without specific prior written permission. | ||
| 21 | .\" | ||
| 22 | .\" THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND | ||
| 23 | .\" ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE | ||
| 24 | .\" IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE | ||
| 25 | .\" ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE | ||
| 26 | .\" FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL | ||
| 27 | .\" DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS | ||
| 28 | .\" OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) | ||
| 29 | .\" HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT | ||
| 30 | .\" LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY | ||
| 31 | .\" OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF | ||
| 32 | .\" SUCH DAMAGE. | ||
| 33 | .\" | ||
| 34 | .\" from: @(#)index.3 5.3 (Berkeley) 4/19/91 | ||
| 35 | .\" $Id: index.3,v 1.1.1.1 1995/10/18 08:42:21 deraadt Exp $ | ||
| 36 | .\" | ||
| 37 | .Dd April 19, 1991 | ||
| 38 | .Dt INDEX 3 | ||
| 39 | .Os | ||
| 40 | .Sh NAME | ||
| 41 | .Nm index | ||
| 42 | .Nd locate character in string | ||
| 43 | .Sh SYNOPSIS | ||
| 44 | .Fd #include <string.h> | ||
| 45 | .Ft char * | ||
| 46 | .Fn index "const char *s" "int c" | ||
| 47 | .Sh DESCRIPTION | ||
| 48 | The | ||
| 49 | .Fn index | ||
| 50 | function | ||
| 51 | locates the first character matching | ||
| 52 | .Fa c | ||
| 53 | (converted to a | ||
| 54 | .Em char ) | ||
| 55 | in the null-terminated string | ||
| 56 | .Fa s . | ||
| 57 | .Sh RETURN VALUES | ||
| 58 | The character | ||
| 59 | .Fa c | ||
| 60 | is returned if it is found; otherwise | ||
| 61 | .Dv NULL | ||
| 62 | is returned. | ||
| 63 | If | ||
| 64 | .Fa c | ||
| 65 | is '\e0', | ||
| 66 | .Fn index | ||
| 67 | locates the terminating '\e0'. | ||
| 68 | .Sh SEE ALSO | ||
| 69 | .Xr memchr 3 , | ||
| 70 | .Xr rindex 3 , | ||
| 71 | .Xr strchr 3 , | ||
| 72 | .Xr strcspn 3 , | ||
| 73 | .Xr strpbrk 3 , | ||
| 74 | .Xr strrchr 3 , | ||
| 75 | .Xr strsep 3 , | ||
| 76 | .Xr strspn 3 , | ||
| 77 | .Xr strstr 3 , | ||
| 78 | .Xr strtok 3 | ||
| 79 | .Sh HISTORY | ||
| 80 | A | ||
| 81 | .Fn index | ||
| 82 | function appeared in | ||
| 83 | .At v6 . | ||
diff --git a/src/lib/libc/string/index.c b/src/lib/libc/string/index.c new file mode 100644 index 0000000000..3d9c05f961 --- /dev/null +++ b/src/lib/libc/string/index.c | |||
| @@ -0,0 +1,56 @@ | |||
| 1 | /*- | ||
| 2 | * Copyright (c) 1990 The Regents of the University of California. | ||
| 3 | * All rights reserved. | ||
| 4 | * | ||
| 5 | * Redistribution and use in source and binary forms, with or without | ||
| 6 | * modification, are permitted provided that the following conditions | ||
| 7 | * are met: | ||
| 8 | * 1. Redistributions of source code must retain the above copyright | ||
| 9 | * notice, this list of conditions and the following disclaimer. | ||
| 10 | * 2. Redistributions in binary form must reproduce the above copyright | ||
| 11 | * notice, this list of conditions and the following disclaimer in the | ||
| 12 | * documentation and/or other materials provided with the distribution. | ||
| 13 | * 3. All advertising materials mentioning features or use of this software | ||
| 14 | * must display the following acknowledgement: | ||
| 15 | * This product includes software developed by the University of | ||
| 16 | * California, Berkeley and its contributors. | ||
| 17 | * 4. Neither the name of the University nor the names of its contributors | ||
| 18 | * may be used to endorse or promote products derived from this software | ||
| 19 | * without specific prior written permission. | ||
| 20 | * | ||
| 21 | * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND | ||
| 22 | * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE | ||
| 23 | * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE | ||
| 24 | * ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE | ||
| 25 | * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL | ||
| 26 | * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS | ||
| 27 | * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) | ||
| 28 | * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT | ||
| 29 | * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY | ||
| 30 | * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF | ||
| 31 | * SUCH DAMAGE. | ||
| 32 | */ | ||
| 33 | |||
| 34 | #if defined(LIBC_SCCS) && !defined(lint) | ||
| 35 | /*static char *sccsid = "from: @(#)index.c 5.7 (Berkeley) 2/24/91";*/ | ||
| 36 | static char *rcsid = "$Id: index.c,v 1.1.1.1 1995/10/18 08:42:21 deraadt Exp $"; | ||
| 37 | #endif /* LIBC_SCCS and not lint */ | ||
| 38 | |||
| 39 | #include <string.h> | ||
| 40 | |||
| 41 | char * | ||
| 42 | #ifdef STRCHR | ||
| 43 | strchr(p, ch) | ||
| 44 | #else | ||
| 45 | index(p, ch) | ||
| 46 | #endif | ||
| 47 | register const char *p, ch; | ||
| 48 | { | ||
| 49 | for (;; ++p) { | ||
| 50 | if (*p == ch) | ||
| 51 | return((char *)p); | ||
| 52 | if (!*p) | ||
| 53 | return((char *)NULL); | ||
| 54 | } | ||
| 55 | /* NOTREACHED */ | ||
| 56 | } | ||
diff --git a/src/lib/libc/string/memccpy.3 b/src/lib/libc/string/memccpy.3 new file mode 100644 index 0000000000..61df704028 --- /dev/null +++ b/src/lib/libc/string/memccpy.3 | |||
| @@ -0,0 +1,73 @@ | |||
| 1 | .\" Copyright (c) 1990, 1991 The Regents of the University of California. | ||
| 2 | .\" All rights reserved. | ||
| 3 | .\" | ||
| 4 | .\" Redistribution and use in source and binary forms, with or without | ||
| 5 | .\" modification, are permitted provided that the following conditions | ||
| 6 | .\" are met: | ||
| 7 | .\" 1. Redistributions of source code must retain the above copyright | ||
| 8 | .\" notice, this list of conditions and the following disclaimer. | ||
| 9 | .\" 2. Redistributions in binary form must reproduce the above copyright | ||
| 10 | .\" notice, this list of conditions and the following disclaimer in the | ||
| 11 | .\" documentation and/or other materials provided with the distribution. | ||
| 12 | .\" 3. All advertising materials mentioning features or use of this software | ||
| 13 | .\" must display the following acknowledgement: | ||
| 14 | .\" This product includes software developed by the University of | ||
| 15 | .\" California, Berkeley and its contributors. | ||
| 16 | .\" 4. Neither the name of the University nor the names of its contributors | ||
| 17 | .\" may be used to endorse or promote products derived from this software | ||
| 18 | .\" without specific prior written permission. | ||
| 19 | .\" | ||
| 20 | .\" THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND | ||
| 21 | .\" ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE | ||
| 22 | .\" IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE | ||
| 23 | .\" ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE | ||
| 24 | .\" FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL | ||
| 25 | .\" DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS | ||
| 26 | .\" OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) | ||
| 27 | .\" HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT | ||
| 28 | .\" LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY | ||
| 29 | .\" OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF | ||
| 30 | .\" SUCH DAMAGE. | ||
| 31 | .\" | ||
| 32 | .\" from: @(#)memccpy.3 5.4 (Berkeley) 4/19/91 | ||
| 33 | .\" $Id: memccpy.3,v 1.1.1.1 1995/10/18 08:42:21 deraadt Exp $ | ||
| 34 | .\" | ||
| 35 | .Dd April 19, 1991 | ||
| 36 | .Dt MEMCCPY 3 | ||
| 37 | .Os | ||
| 38 | .Sh NAME | ||
| 39 | .Nm memccpy | ||
| 40 | .Sh SYNOPSIS | ||
| 41 | .Fd #include <string.h> | ||
| 42 | .Ft void * | ||
| 43 | .Fn memccpy "void *dst" "const void *src" "int c" "size_t len" | ||
| 44 | .Sh DESCRIPTION | ||
| 45 | The | ||
| 46 | .Fn memccpy | ||
| 47 | function | ||
| 48 | copies bytes from string | ||
| 49 | .Fa src | ||
| 50 | to string | ||
| 51 | .Fa dst . | ||
| 52 | If the character | ||
| 53 | .Fa c | ||
| 54 | (as converted to an unsigned char) occurs in the string | ||
| 55 | .Fa src , | ||
| 56 | the copy stops and a pointer to the byte after the copy of | ||
| 57 | .Fa c | ||
| 58 | in the string | ||
| 59 | .Fa dst | ||
| 60 | is returned. | ||
| 61 | Otherwise, | ||
| 62 | .Fa len | ||
| 63 | bytes are copied, and a NULL pointer is returned. | ||
| 64 | .Sh SEE ALSO | ||
| 65 | .Xr bcopy 3 , | ||
| 66 | .Xr memcpy 3 , | ||
| 67 | .Xr memmove 3 , | ||
| 68 | .Xr strcpy 3 | ||
| 69 | .Sh HISTORY | ||
| 70 | The | ||
| 71 | .Fn memccpy | ||
| 72 | function is | ||
| 73 | .Ud . | ||
diff --git a/src/lib/libc/string/memccpy.c b/src/lib/libc/string/memccpy.c new file mode 100644 index 0000000000..3a1d7bcca1 --- /dev/null +++ b/src/lib/libc/string/memccpy.c | |||
| @@ -0,0 +1,59 @@ | |||
| 1 | /*- | ||
| 2 | * Copyright (c) 1990 The Regents of the University of California. | ||
| 3 | * All rights reserved. | ||
| 4 | * | ||
| 5 | * Redistribution and use in source and binary forms, with or without | ||
| 6 | * modification, are permitted provided that the following conditions | ||
| 7 | * are met: | ||
| 8 | * 1. Redistributions of source code must retain the above copyright | ||
| 9 | * notice, this list of conditions and the following disclaimer. | ||
| 10 | * 2. Redistributions in binary form must reproduce the above copyright | ||
| 11 | * notice, this list of conditions and the following disclaimer in the | ||
| 12 | * documentation and/or other materials provided with the distribution. | ||
| 13 | * 3. All advertising materials mentioning features or use of this software | ||
| 14 | * must display the following acknowledgement: | ||
| 15 | * This product includes software developed by the University of | ||
| 16 | * California, Berkeley and its contributors. | ||
| 17 | * 4. Neither the name of the University nor the names of its contributors | ||
| 18 | * may be used to endorse or promote products derived from this software | ||
| 19 | * without specific prior written permission. | ||
| 20 | * | ||
| 21 | * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND | ||
| 22 | * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE | ||
| 23 | * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE | ||
| 24 | * ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE | ||
| 25 | * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL | ||
| 26 | * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS | ||
| 27 | * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) | ||
| 28 | * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT | ||
| 29 | * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY | ||
| 30 | * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF | ||
| 31 | * SUCH DAMAGE. | ||
| 32 | */ | ||
| 33 | |||
| 34 | #if defined(LIBC_SCCS) && !defined(lint) | ||
| 35 | /*static char *sccsid = "from: @(#)memccpy.c 5.8 (Berkeley) 5/30/91";*/ | ||
| 36 | static char *rcsid = "$Id: memccpy.c,v 1.1.1.1 1995/10/18 08:42:21 deraadt Exp $"; | ||
| 37 | #endif /* LIBC_SCCS and not lint */ | ||
| 38 | |||
| 39 | #include <string.h> | ||
| 40 | |||
| 41 | void * | ||
| 42 | memccpy(t, f, c, n) | ||
| 43 | void *t; | ||
| 44 | const void *f; | ||
| 45 | int c; | ||
| 46 | register size_t n; | ||
| 47 | { | ||
| 48 | |||
| 49 | if (n) { | ||
| 50 | register unsigned char *tp = t; | ||
| 51 | register const unsigned char *fp = f; | ||
| 52 | register unsigned char uc = c; | ||
| 53 | do { | ||
| 54 | if ((*tp++ = *fp++) == uc) | ||
| 55 | return (tp); | ||
| 56 | } while (--n != 0); | ||
| 57 | } | ||
| 58 | return (0); | ||
| 59 | } | ||
diff --git a/src/lib/libc/string/memchr.3 b/src/lib/libc/string/memchr.3 new file mode 100644 index 0000000000..265711e3b5 --- /dev/null +++ b/src/lib/libc/string/memchr.3 | |||
| @@ -0,0 +1,82 @@ | |||
| 1 | .\" Copyright (c) 1990, 1991 The Regents of the University of California. | ||
| 2 | .\" All rights reserved. | ||
| 3 | .\" | ||
| 4 | .\" This code is derived from software contributed to Berkeley by | ||
| 5 | .\" Chris Torek and the American National Standards Committee X3, | ||
| 6 | .\" on Information Processing Systems. | ||
| 7 | .\" | ||
| 8 | .\" Redistribution and use in source and binary forms, with or without | ||
| 9 | .\" modification, are permitted provided that the following conditions | ||
| 10 | .\" are met: | ||
| 11 | .\" 1. Redistributions of source code must retain the above copyright | ||
| 12 | .\" notice, this list of conditions and the following disclaimer. | ||
| 13 | .\" 2. Redistributions in binary form must reproduce the above copyright | ||
| 14 | .\" notice, this list of conditions and the following disclaimer in the | ||
| 15 | .\" documentation and/or other materials provided with the distribution. | ||
| 16 | .\" 3. All advertising materials mentioning features or use of this software | ||
| 17 | .\" must display the following acknowledgement: | ||
| 18 | .\" This product includes software developed by the University of | ||
| 19 | .\" California, Berkeley and its contributors. | ||
| 20 | .\" 4. Neither the name of the University nor the names of its contributors | ||
| 21 | .\" may be used to endorse or promote products derived from this software | ||
| 22 | .\" without specific prior written permission. | ||
| 23 | .\" | ||
| 24 | .\" THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND | ||
| 25 | .\" ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE | ||
| 26 | .\" IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE | ||
| 27 | .\" ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE | ||
| 28 | .\" FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL | ||
| 29 | .\" DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS | ||
| 30 | .\" OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) | ||
| 31 | .\" HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT | ||
| 32 | .\" LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY | ||
| 33 | .\" OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF | ||
| 34 | .\" SUCH DAMAGE. | ||
| 35 | .\" | ||
| 36 | .\" from: @(#)memchr.3 5.4 (Berkeley) 6/29/91 | ||
| 37 | .\" $Id: memchr.3,v 1.1.1.1 1995/10/18 08:42:21 deraadt Exp $ | ||
| 38 | .\" | ||
| 39 | .Dd June 29, 1991 | ||
| 40 | .Dt MEMCHR 3 | ||
| 41 | .Os | ||
| 42 | .Sh NAME | ||
| 43 | .Nm memchr | ||
| 44 | .Nd locate byte in byte string | ||
| 45 | .Sh SYNOPSIS | ||
| 46 | .Fd #include <string.h> | ||
| 47 | .Ft void * | ||
| 48 | .Fn memchr "const void *b" "int c" "size_t len" | ||
| 49 | .Sh DESCRIPTION | ||
| 50 | The | ||
| 51 | .Fn memchr | ||
| 52 | function | ||
| 53 | locates the first occurrence of | ||
| 54 | .Fa c | ||
| 55 | (converted to an unsigned char) | ||
| 56 | in string | ||
| 57 | .Fa b . | ||
| 58 | .Sh RETURN VALUES | ||
| 59 | The | ||
| 60 | .Fn memchr | ||
| 61 | function | ||
| 62 | returns a pointer to the byte located, | ||
| 63 | or NULL if no such byte exists within | ||
| 64 | .Fa len | ||
| 65 | bytes. | ||
| 66 | .Sh SEE ALSO | ||
| 67 | .Xr index 3 , | ||
| 68 | .Xr rindex 3 , | ||
| 69 | .Xr strchr 3 , | ||
| 70 | .Xr strcspn 3 , | ||
| 71 | .Xr strpbrk 3 , | ||
| 72 | .Xr strrchr 3 , | ||
| 73 | .Xr strsep 3 , | ||
| 74 | .Xr strspn 3 , | ||
| 75 | .Xr strstr 3 , | ||
| 76 | .Xr strtok 3 | ||
| 77 | .Sh STANDARDS | ||
| 78 | The | ||
| 79 | .Fn memchr | ||
| 80 | function | ||
| 81 | conforms to | ||
| 82 | .St -ansiC . | ||
diff --git a/src/lib/libc/string/memchr.c b/src/lib/libc/string/memchr.c new file mode 100644 index 0000000000..61652c6bb1 --- /dev/null +++ b/src/lib/libc/string/memchr.c | |||
| @@ -0,0 +1,59 @@ | |||
| 1 | /*- | ||
| 2 | * Copyright (c) 1990 The Regents of the University of California. | ||
| 3 | * All rights reserved. | ||
| 4 | * | ||
| 5 | * This code is derived from software contributed to Berkeley by | ||
| 6 | * Chris Torek. | ||
| 7 | * | ||
| 8 | * Redistribution and use in source and binary forms, with or without | ||
| 9 | * modification, are permitted provided that the following conditions | ||
| 10 | * are met: | ||
| 11 | * 1. Redistributions of source code must retain the above copyright | ||
| 12 | * notice, this list of conditions and the following disclaimer. | ||
| 13 | * 2. Redistributions in binary form must reproduce the above copyright | ||
| 14 | * notice, this list of conditions and the following disclaimer in the | ||
| 15 | * documentation and/or other materials provided with the distribution. | ||
| 16 | * 3. All advertising materials mentioning features or use of this software | ||
| 17 | * must display the following acknowledgement: | ||
| 18 | * This product includes software developed by the University of | ||
| 19 | * California, Berkeley and its contributors. | ||
| 20 | * 4. Neither the name of the University nor the names of its contributors | ||
| 21 | * may be used to endorse or promote products derived from this software | ||
| 22 | * without specific prior written permission. | ||
| 23 | * | ||
| 24 | * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND | ||
| 25 | * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE | ||
| 26 | * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE | ||
| 27 | * ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE | ||
| 28 | * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL | ||
| 29 | * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS | ||
| 30 | * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) | ||
| 31 | * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT | ||
| 32 | * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY | ||
| 33 | * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF | ||
| 34 | * SUCH DAMAGE. | ||
| 35 | */ | ||
| 36 | |||
| 37 | #if defined(LIBC_SCCS) && !defined(lint) | ||
| 38 | /*static char *sccsid = "from: @(#)memchr.c 5.6 (Berkeley) 1/26/91";*/ | ||
| 39 | static char *rcsid = "$Id: memchr.c,v 1.1.1.1 1995/10/18 08:42:21 deraadt Exp $"; | ||
| 40 | #endif /* LIBC_SCCS and not lint */ | ||
| 41 | |||
| 42 | #include <string.h> | ||
| 43 | |||
| 44 | void * | ||
| 45 | memchr(s, c, n) | ||
| 46 | const void *s; | ||
| 47 | register unsigned char c; | ||
| 48 | register size_t n; | ||
| 49 | { | ||
| 50 | if (n != 0) { | ||
| 51 | register const unsigned char *p = s; | ||
| 52 | |||
| 53 | do { | ||
| 54 | if (*p++ == c) | ||
| 55 | return ((void *)(p - 1)); | ||
| 56 | } while (--n != 0); | ||
| 57 | } | ||
| 58 | return (NULL); | ||
| 59 | } | ||
diff --git a/src/lib/libc/string/memcmp.3 b/src/lib/libc/string/memcmp.3 new file mode 100644 index 0000000000..13901c1009 --- /dev/null +++ b/src/lib/libc/string/memcmp.3 | |||
| @@ -0,0 +1,83 @@ | |||
| 1 | .\" Copyright (c) 1990, 1991 The Regents of the University of California. | ||
| 2 | .\" All rights reserved. | ||
| 3 | .\" | ||
| 4 | .\" This code is derived from software contributed to Berkeley by | ||
| 5 | .\" Chris Torek and the American National Standards Committee X3, | ||
| 6 | .\" on Information Processing Systems. | ||
| 7 | .\" | ||
| 8 | .\" Redistribution and use in source and binary forms, with or without | ||
| 9 | .\" modification, are permitted provided that the following conditions | ||
| 10 | .\" are met: | ||
| 11 | .\" 1. Redistributions of source code must retain the above copyright | ||
| 12 | .\" notice, this list of conditions and the following disclaimer. | ||
| 13 | .\" 2. Redistributions in binary form must reproduce the above copyright | ||
| 14 | .\" notice, this list of conditions and the following disclaimer in the | ||
| 15 | .\" documentation and/or other materials provided with the distribution. | ||
| 16 | .\" 3. All advertising materials mentioning features or use of this software | ||
| 17 | .\" must display the following acknowledgement: | ||
| 18 | .\" This product includes software developed by the University of | ||
| 19 | .\" California, Berkeley and its contributors. | ||
| 20 | .\" 4. Neither the name of the University nor the names of its contributors | ||
| 21 | .\" may be used to endorse or promote products derived from this software | ||
| 22 | .\" without specific prior written permission. | ||
| 23 | .\" | ||
| 24 | .\" THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND | ||
| 25 | .\" ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE | ||
| 26 | .\" IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE | ||
| 27 | .\" ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE | ||
| 28 | .\" FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL | ||
| 29 | .\" DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS | ||
| 30 | .\" OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) | ||
| 31 | .\" HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT | ||
| 32 | .\" LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY | ||
| 33 | .\" OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF | ||
| 34 | .\" SUCH DAMAGE. | ||
| 35 | .\" | ||
| 36 | .\" from: @(#)memcmp.3 5.5 (Berkeley) 6/29/91 | ||
| 37 | .\" $Id: memcmp.3,v 1.1.1.1 1995/10/18 08:42:21 deraadt Exp $ | ||
| 38 | .\" | ||
| 39 | .Dd June 29, 1991 | ||
| 40 | .Dt MEMCMP 3 | ||
| 41 | .Os | ||
| 42 | .Sh NAME | ||
| 43 | .Nm memcmp | ||
| 44 | .Nd compare byte string | ||
| 45 | .Sh SYNOPSIS | ||
| 46 | .Fd #include <string.h> | ||
| 47 | .Ft int | ||
| 48 | .Fn memcmp "const void *b1" "const void *b2" "size_t len" | ||
| 49 | .Sh DESCRIPTION | ||
| 50 | The | ||
| 51 | .Fn memcmp | ||
| 52 | function | ||
| 53 | compares byte string | ||
| 54 | .Fa b1 | ||
| 55 | against byte string | ||
| 56 | .Fa b2 . | ||
| 57 | Both strings are assumed to be | ||
| 58 | .Fa len | ||
| 59 | bytes long. | ||
| 60 | .Sh RETURN VALUES | ||
| 61 | The | ||
| 62 | .Fn memcmp | ||
| 63 | function | ||
| 64 | returns zero if the the two strings are identical, | ||
| 65 | otherwise returns the difference between the first two differing bytes | ||
| 66 | (treated as unsigned char values, so that | ||
| 67 | .Sq Li \e200 | ||
| 68 | is greater than | ||
| 69 | .Sq Li \&\e0 , | ||
| 70 | for example). | ||
| 71 | Zero-length strings are always identical. | ||
| 72 | .Sh SEE ALSO | ||
| 73 | .Xr bcmp 3 , | ||
| 74 | .Xr strcasecmp 3 , | ||
| 75 | .Xr strcmp 3 , | ||
| 76 | .Xr strcoll 3 , | ||
| 77 | .Xr strxfrm 3 | ||
| 78 | .Sh STANDARDS | ||
| 79 | The | ||
| 80 | .Fn memcmp | ||
| 81 | function | ||
| 82 | conforms to | ||
| 83 | .St -ansiC . | ||
diff --git a/src/lib/libc/string/memcmp.c b/src/lib/libc/string/memcmp.c new file mode 100644 index 0000000000..23d2ab2393 --- /dev/null +++ b/src/lib/libc/string/memcmp.c | |||
| @@ -0,0 +1,61 @@ | |||
| 1 | /*- | ||
| 2 | * Copyright (c) 1990 The Regents of the University of California. | ||
| 3 | * All rights reserved. | ||
| 4 | * | ||
| 5 | * This code is derived from software contributed to Berkeley by | ||
| 6 | * Chris Torek. | ||
| 7 | * | ||
| 8 | * Redistribution and use in source and binary forms, with or without | ||
| 9 | * modification, are permitted provided that the following conditions | ||
| 10 | * are met: | ||
| 11 | * 1. Redistributions of source code must retain the above copyright | ||
| 12 | * notice, this list of conditions and the following disclaimer. | ||
| 13 | * 2. Redistributions in binary form must reproduce the above copyright | ||
| 14 | * notice, this list of conditions and the following disclaimer in the | ||
| 15 | * documentation and/or other materials provided with the distribution. | ||
| 16 | * 3. All advertising materials mentioning features or use of this software | ||
| 17 | * must display the following acknowledgement: | ||
| 18 | * This product includes software developed by the University of | ||
| 19 | * California, Berkeley and its contributors. | ||
| 20 | * 4. Neither the name of the University nor the names of its contributors | ||
| 21 | * may be used to endorse or promote products derived from this software | ||
| 22 | * without specific prior written permission. | ||
| 23 | * | ||
| 24 | * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND | ||
| 25 | * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE | ||
| 26 | * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE | ||
| 27 | * ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE | ||
| 28 | * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL | ||
| 29 | * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS | ||
| 30 | * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) | ||
| 31 | * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT | ||
| 32 | * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY | ||
| 33 | * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF | ||
| 34 | * SUCH DAMAGE. | ||
| 35 | */ | ||
| 36 | |||
| 37 | #if defined(LIBC_SCCS) && !defined(lint) | ||
| 38 | /*static char *sccsid = "from: @(#)memcmp.c 5.6 (Berkeley) 1/26/91";*/ | ||
| 39 | static char *rcsid = "$Id: memcmp.c,v 1.1.1.1 1995/10/18 08:42:21 deraadt Exp $"; | ||
| 40 | #endif /* LIBC_SCCS and not lint */ | ||
| 41 | |||
| 42 | #include <string.h> | ||
| 43 | |||
| 44 | /* | ||
| 45 | * Compare memory regions. | ||
| 46 | */ | ||
| 47 | int | ||
| 48 | memcmp(s1, s2, n) | ||
| 49 | const void *s1, *s2; | ||
| 50 | size_t n; | ||
| 51 | { | ||
| 52 | if (n != 0) { | ||
| 53 | register const unsigned char *p1 = s1, *p2 = s2; | ||
| 54 | |||
| 55 | do { | ||
| 56 | if (*p1++ != *p2++) | ||
| 57 | return (*--p1 - *--p2); | ||
| 58 | } while (--n != 0); | ||
| 59 | } | ||
| 60 | return (0); | ||
| 61 | } | ||
diff --git a/src/lib/libc/string/memcpy.3 b/src/lib/libc/string/memcpy.3 new file mode 100644 index 0000000000..3f4bb643c9 --- /dev/null +++ b/src/lib/libc/string/memcpy.3 | |||
| @@ -0,0 +1,84 @@ | |||
| 1 | .\" Copyright (c) 1990, 1991 The Regents of the University of California. | ||
| 2 | .\" All rights reserved. | ||
| 3 | .\" | ||
| 4 | .\" This code is derived from software contributed to Berkeley by | ||
| 5 | .\" Chris Torek and the American National Standards Committee X3, | ||
| 6 | .\" on Information Processing Systems. | ||
| 7 | .\" | ||
| 8 | .\" Redistribution and use in source and binary forms, with or without | ||
| 9 | .\" modification, are permitted provided that the following conditions | ||
| 10 | .\" are met: | ||
| 11 | .\" 1. Redistributions of source code must retain the above copyright | ||
| 12 | .\" notice, this list of conditions and the following disclaimer. | ||
| 13 | .\" 2. Redistributions in binary form must reproduce the above copyright | ||
| 14 | .\" notice, this list of conditions and the following disclaimer in the | ||
| 15 | .\" documentation and/or other materials provided with the distribution. | ||
| 16 | .\" 3. All advertising materials mentioning features or use of this software | ||
| 17 | .\" must display the following acknowledgement: | ||
| 18 | .\" This product includes software developed by the University of | ||
| 19 | .\" California, Berkeley and its contributors. | ||
| 20 | .\" 4. Neither the name of the University nor the names of its contributors | ||
| 21 | .\" may be used to endorse or promote products derived from this software | ||
| 22 | .\" without specific prior written permission. | ||
| 23 | .\" | ||
| 24 | .\" THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND | ||
| 25 | .\" ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE | ||
| 26 | .\" IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE | ||
| 27 | .\" ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE | ||
| 28 | .\" FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL | ||
| 29 | .\" DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS | ||
| 30 | .\" OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) | ||
| 31 | .\" HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT | ||
| 32 | .\" LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY | ||
| 33 | .\" OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF | ||
| 34 | .\" SUCH DAMAGE. | ||
| 35 | .\" | ||
| 36 | .\" from: @(#)memcpy.3 5.5 (Berkeley) 6/29/91 | ||
| 37 | .\" $Id: memcpy.3,v 1.1.1.1 1995/10/18 08:42:21 deraadt Exp $ | ||
| 38 | .\" | ||
| 39 | .Dd June 29, 1991 | ||
| 40 | .Dt MEMCPY 3 | ||
| 41 | .Os | ||
| 42 | .Sh NAME | ||
| 43 | .Nm memcpy | ||
| 44 | .Nd copy byte string | ||
| 45 | .Sh SYNOPSIS | ||
| 46 | .Fd #include <string.h> | ||
| 47 | .Ft void * | ||
| 48 | .Fn memcpy "void *dst" "const void *src" "size_t len" | ||
| 49 | .Sh DESCRIPTION | ||
| 50 | The | ||
| 51 | .Fn memcpy | ||
| 52 | function | ||
| 53 | copies | ||
| 54 | .Fa len | ||
| 55 | bytes from string | ||
| 56 | .Fa src | ||
| 57 | to string | ||
| 58 | .Fa dst . | ||
| 59 | .Sh RETURN VALUES | ||
| 60 | The | ||
| 61 | .Fn memcpy | ||
| 62 | function | ||
| 63 | returns the original value of | ||
| 64 | .Fa dst . | ||
| 65 | .Sh SEE ALSO | ||
| 66 | .Xr bcopy 3 , | ||
| 67 | .Xr memccpy 3 , | ||
| 68 | .Xr memmove 3 , | ||
| 69 | .Xr strcpy 3 | ||
| 70 | .Sh STANDARDS | ||
| 71 | The | ||
| 72 | .Fn memcpy | ||
| 73 | function | ||
| 74 | conforms to | ||
| 75 | .St -ansiC . | ||
| 76 | .Sh BUGS | ||
| 77 | In this implementation | ||
| 78 | .Fn memcpy | ||
| 79 | is implemented using | ||
| 80 | .Xr bcopy 3 , | ||
| 81 | and therefore the strings may overlap. | ||
| 82 | On other systems, copying overlapping strings may produce surprises. | ||
| 83 | A simpler solution is to not use | ||
| 84 | .Fn memcpy . | ||
diff --git a/src/lib/libc/string/memmove.3 b/src/lib/libc/string/memmove.3 new file mode 100644 index 0000000000..24422e7971 --- /dev/null +++ b/src/lib/libc/string/memmove.3 | |||
| @@ -0,0 +1,76 @@ | |||
| 1 | .\" Copyright (c) 1990, 1991 The Regents of the University of California. | ||
| 2 | .\" All rights reserved. | ||
| 3 | .\" | ||
| 4 | .\" This code is derived from software contributed to Berkeley by | ||
| 5 | .\" Chris Torek and the American National Standards Committee X3, | ||
| 6 | .\" on Information Processing Systems. | ||
| 7 | .\" | ||
| 8 | .\" Redistribution and use in source and binary forms, with or without | ||
| 9 | .\" modification, are permitted provided that the following conditions | ||
| 10 | .\" are met: | ||
| 11 | .\" 1. Redistributions of source code must retain the above copyright | ||
| 12 | .\" notice, this list of conditions and the following disclaimer. | ||
| 13 | .\" 2. Redistributions in binary form must reproduce the above copyright | ||
| 14 | .\" notice, this list of conditions and the following disclaimer in the | ||
| 15 | .\" documentation and/or other materials provided with the distribution. | ||
| 16 | .\" 3. All advertising materials mentioning features or use of this software | ||
| 17 | .\" must display the following acknowledgement: | ||
| 18 | .\" This product includes software developed by the University of | ||
| 19 | .\" California, Berkeley and its contributors. | ||
| 20 | .\" 4. Neither the name of the University nor the names of its contributors | ||
| 21 | .\" may be used to endorse or promote products derived from this software | ||
| 22 | .\" without specific prior written permission. | ||
| 23 | .\" | ||
| 24 | .\" THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND | ||
| 25 | .\" ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE | ||
| 26 | .\" IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE | ||
| 27 | .\" ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE | ||
| 28 | .\" FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL | ||
| 29 | .\" DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS | ||
| 30 | .\" OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) | ||
| 31 | .\" HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT | ||
| 32 | .\" LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY | ||
| 33 | .\" OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF | ||
| 34 | .\" SUCH DAMAGE. | ||
| 35 | .\" | ||
| 36 | .\" from: @(#)memmove.3 5.5 (Berkeley) 6/29/91 | ||
| 37 | .\" $Id: memmove.3,v 1.1.1.1 1995/10/18 08:42:21 deraadt Exp $ | ||
| 38 | .\" | ||
| 39 | .Dd June 29, 1991 | ||
| 40 | .Dt MEMMOVE 3 | ||
| 41 | .Os | ||
| 42 | .Sh NAME | ||
| 43 | .Nm memmove | ||
| 44 | .Nd copy byte string | ||
| 45 | .Sh SYNOPSIS | ||
| 46 | .Fd #include <string.h> | ||
| 47 | .Ft void * | ||
| 48 | .Fn memmove "void *dst" "const void *src" "size_t len" | ||
| 49 | .Sh DESCRIPTION | ||
| 50 | The | ||
| 51 | .Fn memmove | ||
| 52 | function | ||
| 53 | copies | ||
| 54 | .Fa len | ||
| 55 | bytes from string | ||
| 56 | .Fa src | ||
| 57 | to string | ||
| 58 | .Fa dst . | ||
| 59 | The two strings may overlap; | ||
| 60 | the copy is always done in a non-destructive manner. | ||
| 61 | .Sh RETURN VALUES | ||
| 62 | The | ||
| 63 | .Fn memmove | ||
| 64 | function returns the original value of | ||
| 65 | .Fa dst . | ||
| 66 | .Sh SEE ALSO | ||
| 67 | .Xr bcopy 3 , | ||
| 68 | .Xr memccpy 3 , | ||
| 69 | .Xr memcpy 3 , | ||
| 70 | .Xr strcpy 3 | ||
| 71 | .Sh STANDARDS | ||
| 72 | The | ||
| 73 | .Fn memmove | ||
| 74 | function | ||
| 75 | conforms to | ||
| 76 | .St -ansiC . | ||
diff --git a/src/lib/libc/string/memset.3 b/src/lib/libc/string/memset.3 new file mode 100644 index 0000000000..1afc052182 --- /dev/null +++ b/src/lib/libc/string/memset.3 | |||
| @@ -0,0 +1,67 @@ | |||
| 1 | .\" Copyright (c) 1990, 1991 The Regents of the University of California. | ||
| 2 | .\" All rights reserved. | ||
| 3 | .\" | ||
| 4 | .\" This code is derived from software contributed to Berkeley by | ||
| 5 | .\" Chris Torek and the American National Standards Committee X3, | ||
| 6 | .\" on Information Processing Systems. | ||
| 7 | .\" | ||
| 8 | .\" Redistribution and use in source and binary forms, with or without | ||
| 9 | .\" modification, are permitted provided that the following conditions | ||
| 10 | .\" are met: | ||
| 11 | .\" 1. Redistributions of source code must retain the above copyright | ||
| 12 | .\" notice, this list of conditions and the following disclaimer. | ||
| 13 | .\" 2. Redistributions in binary form must reproduce the above copyright | ||
| 14 | .\" notice, this list of conditions and the following disclaimer in the | ||
| 15 | .\" documentation and/or other materials provided with the distribution. | ||
| 16 | .\" 3. All advertising materials mentioning features or use of this software | ||
| 17 | .\" must display the following acknowledgement: | ||
| 18 | .\" This product includes software developed by the University of | ||
| 19 | .\" California, Berkeley and its contributors. | ||
| 20 | .\" 4. Neither the name of the University nor the names of its contributors | ||
| 21 | .\" may be used to endorse or promote products derived from this software | ||
| 22 | .\" without specific prior written permission. | ||
| 23 | .\" | ||
| 24 | .\" THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND | ||
| 25 | .\" ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE | ||
| 26 | .\" IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE | ||
| 27 | .\" ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE | ||
| 28 | .\" FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL | ||
| 29 | .\" DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS | ||
| 30 | .\" OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) | ||
| 31 | .\" HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT | ||
| 32 | .\" LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY | ||
| 33 | .\" OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF | ||
| 34 | .\" SUCH DAMAGE. | ||
| 35 | .\" | ||
| 36 | .\" from: @(#)memset.3 5.4 (Berkeley) 6/29/91 | ||
| 37 | .\" $Id: memset.3,v 1.1.1.1 1995/10/18 08:42:21 deraadt Exp $ | ||
| 38 | .\" | ||
| 39 | .Dd June 29, 1991 | ||
| 40 | .Dt MEMSET 3 | ||
| 41 | .Os | ||
| 42 | .Sh NAME | ||
| 43 | .Nm memset | ||
| 44 | .Nd write a byte to byte string | ||
| 45 | .Sh SYNOPSIS | ||
| 46 | .Fd #include <string.h> | ||
| 47 | .Ft void * | ||
| 48 | .Fn memset "void *b" "int c" "size_t len" | ||
| 49 | .Sh DESCRIPTION | ||
| 50 | The | ||
| 51 | .Fn memset | ||
| 52 | function | ||
| 53 | writes | ||
| 54 | .Fa len | ||
| 55 | bytes of value | ||
| 56 | .Fa c | ||
| 57 | (converted to an unsigned char) to the string | ||
| 58 | .Fa b . | ||
| 59 | .Sh SEE ALSO | ||
| 60 | .Xr bzero 3 , | ||
| 61 | .Xr swab 3 | ||
| 62 | .Sh STANDARDS | ||
| 63 | The | ||
| 64 | .Fn memset | ||
| 65 | function | ||
| 66 | conforms to | ||
| 67 | .St -ansiC . | ||
diff --git a/src/lib/libc/string/memset.c b/src/lib/libc/string/memset.c new file mode 100644 index 0000000000..117de2e80b --- /dev/null +++ b/src/lib/libc/string/memset.c | |||
| @@ -0,0 +1,59 @@ | |||
| 1 | /*- | ||
| 2 | * Copyright (c) 1990 The Regents of the University of California. | ||
| 3 | * All rights reserved. | ||
| 4 | * | ||
| 5 | * This code is derived from software contributed to Berkeley by | ||
| 6 | * Chris Torek. | ||
| 7 | * | ||
| 8 | * Redistribution and use in source and binary forms, with or without | ||
| 9 | * modification, are permitted provided that the following conditions | ||
| 10 | * are met: | ||
| 11 | * 1. Redistributions of source code must retain the above copyright | ||
| 12 | * notice, this list of conditions and the following disclaimer. | ||
| 13 | * 2. Redistributions in binary form must reproduce the above copyright | ||
| 14 | * notice, this list of conditions and the following disclaimer in the | ||
| 15 | * documentation and/or other materials provided with the distribution. | ||
| 16 | * 3. All advertising materials mentioning features or use of this software | ||
| 17 | * must display the following acknowledgement: | ||
| 18 | * This product includes software developed by the University of | ||
| 19 | * California, Berkeley and its contributors. | ||
| 20 | * 4. Neither the name of the University nor the names of its contributors | ||
| 21 | * may be used to endorse or promote products derived from this software | ||
| 22 | * without specific prior written permission. | ||
| 23 | * | ||
| 24 | * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND | ||
| 25 | * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE | ||
| 26 | * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE | ||
| 27 | * ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE | ||
| 28 | * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL | ||
| 29 | * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS | ||
| 30 | * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) | ||
| 31 | * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT | ||
| 32 | * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY | ||
| 33 | * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF | ||
| 34 | * SUCH DAMAGE. | ||
| 35 | */ | ||
| 36 | |||
| 37 | #if defined(LIBC_SCCS) && !defined(lint) | ||
| 38 | /*static char *sccsid = "from: @(#)memset.c 5.6 (Berkeley) 1/26/91";*/ | ||
| 39 | static char *rcsid = "$Id: memset.c,v 1.1.1.1 1995/10/18 08:42:21 deraadt Exp $"; | ||
| 40 | #endif /* LIBC_SCCS and not lint */ | ||
| 41 | |||
| 42 | #include <string.h> | ||
| 43 | |||
| 44 | void * | ||
| 45 | memset(dst, c, n) | ||
| 46 | void *dst; | ||
| 47 | register int c; | ||
| 48 | register size_t n; | ||
| 49 | { | ||
| 50 | |||
| 51 | if (n != 0) { | ||
| 52 | register char *d = dst; | ||
| 53 | |||
| 54 | do | ||
| 55 | *d++ = c; | ||
| 56 | while (--n != 0); | ||
| 57 | } | ||
| 58 | return (dst); | ||
| 59 | } | ||
diff --git a/src/lib/libc/string/rindex.3 b/src/lib/libc/string/rindex.3 new file mode 100644 index 0000000000..b13b3513e0 --- /dev/null +++ b/src/lib/libc/string/rindex.3 | |||
| @@ -0,0 +1,81 @@ | |||
| 1 | .\" Copyright (c) 1990, 1991 The Regents of the University of California. | ||
| 2 | .\" All rights reserved. | ||
| 3 | .\" | ||
| 4 | .\" This code is derived from software contributed to Berkeley by | ||
| 5 | .\" Chris Torek. | ||
| 6 | .\" Redistribution and use in source and binary forms, with or without | ||
| 7 | .\" modification, are permitted provided that the following conditions | ||
| 8 | .\" are met: | ||
| 9 | .\" 1. Redistributions of source code must retain the above copyright | ||
| 10 | .\" notice, this list of conditions and the following disclaimer. | ||
| 11 | .\" 2. Redistributions in binary form must reproduce the above copyright | ||
| 12 | .\" notice, this list of conditions and the following disclaimer in the | ||
| 13 | .\" documentation and/or other materials provided with the distribution. | ||
| 14 | .\" 3. All advertising materials mentioning features or use of this software | ||
| 15 | .\" must display the following acknowledgement: | ||
| 16 | .\" This product includes software developed by the University of | ||
| 17 | .\" California, Berkeley and its contributors. | ||
| 18 | .\" 4. Neither the name of the University nor the names of its contributors | ||
| 19 | .\" may be used to endorse or promote products derived from this software | ||
| 20 | .\" without specific prior written permission. | ||
| 21 | .\" | ||
| 22 | .\" THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND | ||
| 23 | .\" ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE | ||
| 24 | .\" IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE | ||
| 25 | .\" ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE | ||
| 26 | .\" FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL | ||
| 27 | .\" DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS | ||
| 28 | .\" OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) | ||
| 29 | .\" HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT | ||
| 30 | .\" LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY | ||
| 31 | .\" OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF | ||
| 32 | .\" SUCH DAMAGE. | ||
| 33 | .\" | ||
| 34 | .\" from: @(#)rindex.3 5.2 (Berkeley) 4/19/91 | ||
| 35 | .\" $Id: rindex.3,v 1.1.1.1 1995/10/18 08:42:21 deraadt Exp $ | ||
| 36 | .\" | ||
| 37 | .Dd April 19, 1991 | ||
| 38 | .Dt RINDEX 3 | ||
| 39 | .Os | ||
| 40 | .Sh NAME | ||
| 41 | .Nm rindex | ||
| 42 | .Nd locate character in string | ||
| 43 | .Sh SYNOPSIS | ||
| 44 | .Fd #include <string.h> | ||
| 45 | .Ft char * | ||
| 46 | .Fn rindex "const char *s" "int c" | ||
| 47 | .Sh DESCRIPTION | ||
| 48 | The | ||
| 49 | .Fn rindex | ||
| 50 | function | ||
| 51 | locates the last character | ||
| 52 | matching | ||
| 53 | .Fa c | ||
| 54 | (converted to a | ||
| 55 | .Em char ) | ||
| 56 | in the null-terminated string | ||
| 57 | .Fa s . | ||
| 58 | The character c is returned if it is found; otherwise NULL is returned. | ||
| 59 | If | ||
| 60 | .Fa c | ||
| 61 | is | ||
| 62 | .Ql \e0 , | ||
| 63 | .Fn rindex | ||
| 64 | locates the terminating | ||
| 65 | .Ql \e0 . | ||
| 66 | .Sh SEE ALSO | ||
| 67 | .Xr index 3 , | ||
| 68 | .Xr memchr 3 , | ||
| 69 | .Xr strchr 3 , | ||
| 70 | .Xr strcspn 3 , | ||
| 71 | .Xr strpbrk 3 , | ||
| 72 | .Xr strrchr 3 , | ||
| 73 | .Xr strsep 3 , | ||
| 74 | .Xr strspn 3 , | ||
| 75 | .Xr strstr 3 , | ||
| 76 | .Xr strtok 3 | ||
| 77 | .Sh HISTORY | ||
| 78 | A | ||
| 79 | .Fn rindex | ||
| 80 | function appeared in | ||
| 81 | .At v6 . | ||
diff --git a/src/lib/libc/string/rindex.c b/src/lib/libc/string/rindex.c new file mode 100644 index 0000000000..1b84c92072 --- /dev/null +++ b/src/lib/libc/string/rindex.c | |||
| @@ -0,0 +1,58 @@ | |||
| 1 | /* | ||
| 2 | * Copyright (c) 1988 Regents of the University of California. | ||
| 3 | * All rights reserved. | ||
| 4 | * | ||
| 5 | * Redistribution and use in source and binary forms, with or without | ||
| 6 | * modification, are permitted provided that the following conditions | ||
| 7 | * are met: | ||
| 8 | * 1. Redistributions of source code must retain the above copyright | ||
| 9 | * notice, this list of conditions and the following disclaimer. | ||
| 10 | * 2. Redistributions in binary form must reproduce the above copyright | ||
| 11 | * notice, this list of conditions and the following disclaimer in the | ||
| 12 | * documentation and/or other materials provided with the distribution. | ||
| 13 | * 3. All advertising materials mentioning features or use of this software | ||
| 14 | * must display the following acknowledgement: | ||
| 15 | * This product includes software developed by the University of | ||
| 16 | * California, Berkeley and its contributors. | ||
| 17 | * 4. Neither the name of the University nor the names of its contributors | ||
| 18 | * may be used to endorse or promote products derived from this software | ||
| 19 | * without specific prior written permission. | ||
| 20 | * | ||
| 21 | * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND | ||
| 22 | * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE | ||
| 23 | * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE | ||
| 24 | * ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE | ||
| 25 | * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL | ||
| 26 | * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS | ||
| 27 | * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) | ||
| 28 | * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT | ||
| 29 | * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY | ||
| 30 | * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF | ||
| 31 | * SUCH DAMAGE. | ||
| 32 | */ | ||
| 33 | |||
| 34 | #if defined(LIBC_SCCS) && !defined(lint) | ||
| 35 | /*static char *sccsid = "from: @(#)rindex.c 5.9 (Berkeley) 2/24/91";*/ | ||
| 36 | static char *rcsid = "$Id: rindex.c,v 1.1.1.1 1995/10/18 08:42:21 deraadt Exp $"; | ||
| 37 | #endif /* LIBC_SCCS and not lint */ | ||
| 38 | |||
| 39 | #include <string.h> | ||
| 40 | |||
| 41 | char * | ||
| 42 | #ifdef STRRCHR | ||
| 43 | strrchr(p, ch) | ||
| 44 | #else | ||
| 45 | rindex(p, ch) | ||
| 46 | #endif | ||
| 47 | register const char *p, ch; | ||
| 48 | { | ||
| 49 | register char *save; | ||
| 50 | |||
| 51 | for (save = NULL;; ++p) { | ||
| 52 | if (*p == ch) | ||
| 53 | save = (char *)p; | ||
| 54 | if (!*p) | ||
| 55 | return(save); | ||
| 56 | } | ||
| 57 | /* NOTREACHED */ | ||
| 58 | } | ||
diff --git a/src/lib/libc/string/strcasecmp.3 b/src/lib/libc/string/strcasecmp.3 new file mode 100644 index 0000000000..46e9010e4f --- /dev/null +++ b/src/lib/libc/string/strcasecmp.3 | |||
| @@ -0,0 +1,88 @@ | |||
| 1 | .\" Copyright (c) 1990, 1991 The Regents of the University of California. | ||
| 2 | .\" All rights reserved. | ||
| 3 | .\" | ||
| 4 | .\" This code is derived from software contributed to Berkeley by | ||
| 5 | .\" Chris Torek. | ||
| 6 | .\" Redistribution and use in source and binary forms, with or without | ||
| 7 | .\" modification, are permitted provided that the following conditions | ||
| 8 | .\" are met: | ||
| 9 | .\" 1. Redistributions of source code must retain the above copyright | ||
| 10 | .\" notice, this list of conditions and the following disclaimer. | ||
| 11 | .\" 2. Redistributions in binary form must reproduce the above copyright | ||
| 12 | .\" notice, this list of conditions and the following disclaimer in the | ||
| 13 | .\" documentation and/or other materials provided with the distribution. | ||
| 14 | .\" 3. All advertising materials mentioning features or use of this software | ||
| 15 | .\" must display the following acknowledgement: | ||
| 16 | .\" This product includes software developed by the University of | ||
| 17 | .\" California, Berkeley and its contributors. | ||
| 18 | .\" 4. Neither the name of the University nor the names of its contributors | ||
| 19 | .\" may be used to endorse or promote products derived from this software | ||
| 20 | .\" without specific prior written permission. | ||
| 21 | .\" | ||
| 22 | .\" THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND | ||
| 23 | .\" ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE | ||
| 24 | .\" IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE | ||
| 25 | .\" ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE | ||
| 26 | .\" FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL | ||
| 27 | .\" DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS | ||
| 28 | .\" OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) | ||
| 29 | .\" HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT | ||
| 30 | .\" LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY | ||
| 31 | .\" OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF | ||
| 32 | .\" SUCH DAMAGE. | ||
| 33 | .\" | ||
| 34 | .\" from: @(#)strcasecmp.3 5.4 (Berkeley) 4/19/91 | ||
| 35 | .\" $Id: strcasecmp.3,v 1.1.1.1 1995/10/18 08:42:21 deraadt Exp $ | ||
| 36 | .\" | ||
| 37 | .Dd April 19, 1991 | ||
| 38 | .Dt STRCASECMP 3 | ||
| 39 | .Os | ||
| 40 | .Sh NAME | ||
| 41 | .Nm strcasecmp | ||
| 42 | .Nd compare strings, ignoring case | ||
| 43 | .Sh SYNOPSIS | ||
| 44 | .Fd #include <string.h> | ||
| 45 | .Ft int | ||
| 46 | .Fn strcasecmp "const char *s1" "const char *s2" | ||
| 47 | .Ft int | ||
| 48 | .Fn strncasecmp "const char *s1" "const char *s2" "size_t len" | ||
| 49 | .Sh DESCRIPTION | ||
| 50 | The | ||
| 51 | .Fn strcasecmp | ||
| 52 | and | ||
| 53 | .Fn strncasecmp | ||
| 54 | functions | ||
| 55 | compare the null-terminated strings | ||
| 56 | .Fa s1 | ||
| 57 | and | ||
| 58 | .Fa s2 | ||
| 59 | and return an integer greater than, equal to, or less than 0, | ||
| 60 | according as | ||
| 61 | .Fa s1 | ||
| 62 | is lexicographically greater than, equal to, or less than | ||
| 63 | .Fa s2 | ||
| 64 | after translation of each corresponding character to lower-case. | ||
| 65 | The strings themselves are not modified. | ||
| 66 | The comparison is done using unsigned characters, so that | ||
| 67 | .Sq Li \e200 | ||
| 68 | is greater than | ||
| 69 | .Ql \e0 . | ||
| 70 | .Pp | ||
| 71 | The | ||
| 72 | .Fn strncasecmp | ||
| 73 | compares at most | ||
| 74 | .Fa len | ||
| 75 | characters. | ||
| 76 | .Sh SEE ALSO | ||
| 77 | .Xr bcmp 3 , | ||
| 78 | .Xr memcmp 3 , | ||
| 79 | .Xr strcmp 3 , | ||
| 80 | .Xr strcoll 3 , | ||
| 81 | .Xr strxfrm 3 | ||
| 82 | .Sh HISTORY | ||
| 83 | The | ||
| 84 | .Fn strcasecmp | ||
| 85 | and | ||
| 86 | .Fn strncasecmp | ||
| 87 | functions are | ||
| 88 | .Ud . | ||
diff --git a/src/lib/libc/string/strcasecmp.c b/src/lib/libc/string/strcasecmp.c new file mode 100644 index 0000000000..79bd0081e3 --- /dev/null +++ b/src/lib/libc/string/strcasecmp.c | |||
| @@ -0,0 +1,115 @@ | |||
| 1 | /* | ||
| 2 | * Copyright (c) 1987 Regents of the University of California. | ||
| 3 | * All rights reserved. | ||
| 4 | * | ||
| 5 | * Redistribution and use in source and binary forms, with or without | ||
| 6 | * modification, are permitted provided that the following conditions | ||
| 7 | * are met: | ||
| 8 | * 1. Redistributions of source code must retain the above copyright | ||
| 9 | * notice, this list of conditions and the following disclaimer. | ||
| 10 | * 2. Redistributions in binary form must reproduce the above copyright | ||
| 11 | * notice, this list of conditions and the following disclaimer in the | ||
| 12 | * documentation and/or other materials provided with the distribution. | ||
| 13 | * 3. All advertising materials mentioning features or use of this software | ||
| 14 | * must display the following acknowledgement: | ||
| 15 | * This product includes software developed by the University of | ||
| 16 | * California, Berkeley and its contributors. | ||
| 17 | * 4. Neither the name of the University nor the names of its contributors | ||
| 18 | * may be used to endorse or promote products derived from this software | ||
| 19 | * without specific prior written permission. | ||
| 20 | * | ||
| 21 | * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND | ||
| 22 | * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE | ||
| 23 | * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE | ||
| 24 | * ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE | ||
| 25 | * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL | ||
| 26 | * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS | ||
| 27 | * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) | ||
| 28 | * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT | ||
| 29 | * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY | ||
| 30 | * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF | ||
| 31 | * SUCH DAMAGE. | ||
| 32 | */ | ||
| 33 | |||
| 34 | #if defined(LIBC_SCCS) && !defined(lint) | ||
| 35 | /*static const char sccsid[] = "from: @(#)strcasecmp.c 5.10 (Berkeley) 1/26/91";*/ | ||
| 36 | static char *rcsid = "$Id: strcasecmp.c,v 1.1.1.1 1995/10/18 08:42:21 deraadt Exp $"; | ||
| 37 | #endif /* LIBC_SCCS and not lint */ | ||
| 38 | |||
| 39 | #include <string.h> | ||
| 40 | |||
| 41 | typedef unsigned char u_char; | ||
| 42 | |||
| 43 | /* | ||
| 44 | * This array is designed for mapping upper and lower case letter | ||
| 45 | * together for a case independent comparison. The mappings are | ||
| 46 | * based upon ascii character sequences. | ||
| 47 | */ | ||
| 48 | static const u_char charmap[] = { | ||
| 49 | '\000', '\001', '\002', '\003', '\004', '\005', '\006', '\007', | ||
| 50 | '\010', '\011', '\012', '\013', '\014', '\015', '\016', '\017', | ||
| 51 | '\020', '\021', '\022', '\023', '\024', '\025', '\026', '\027', | ||
| 52 | '\030', '\031', '\032', '\033', '\034', '\035', '\036', '\037', | ||
| 53 | '\040', '\041', '\042', '\043', '\044', '\045', '\046', '\047', | ||
| 54 | '\050', '\051', '\052', '\053', '\054', '\055', '\056', '\057', | ||
| 55 | '\060', '\061', '\062', '\063', '\064', '\065', '\066', '\067', | ||
| 56 | '\070', '\071', '\072', '\073', '\074', '\075', '\076', '\077', | ||
| 57 | '\100', '\141', '\142', '\143', '\144', '\145', '\146', '\147', | ||
| 58 | '\150', '\151', '\152', '\153', '\154', '\155', '\156', '\157', | ||
| 59 | '\160', '\161', '\162', '\163', '\164', '\165', '\166', '\167', | ||
| 60 | '\170', '\171', '\172', '\133', '\134', '\135', '\136', '\137', | ||
| 61 | '\140', '\141', '\142', '\143', '\144', '\145', '\146', '\147', | ||
| 62 | '\150', '\151', '\152', '\153', '\154', '\155', '\156', '\157', | ||
| 63 | '\160', '\161', '\162', '\163', '\164', '\165', '\166', '\167', | ||
| 64 | '\170', '\171', '\172', '\173', '\174', '\175', '\176', '\177', | ||
| 65 | '\200', '\201', '\202', '\203', '\204', '\205', '\206', '\207', | ||
| 66 | '\210', '\211', '\212', '\213', '\214', '\215', '\216', '\217', | ||
| 67 | '\220', '\221', '\222', '\223', '\224', '\225', '\226', '\227', | ||
| 68 | '\230', '\231', '\232', '\233', '\234', '\235', '\236', '\237', | ||
| 69 | '\240', '\241', '\242', '\243', '\244', '\245', '\246', '\247', | ||
| 70 | '\250', '\251', '\252', '\253', '\254', '\255', '\256', '\257', | ||
| 71 | '\260', '\261', '\262', '\263', '\264', '\265', '\266', '\267', | ||
| 72 | '\270', '\271', '\272', '\273', '\274', '\275', '\276', '\277', | ||
| 73 | '\300', '\301', '\302', '\303', '\304', '\305', '\306', '\307', | ||
| 74 | '\310', '\311', '\312', '\313', '\314', '\315', '\316', '\317', | ||
| 75 | '\320', '\321', '\322', '\323', '\324', '\325', '\326', '\327', | ||
| 76 | '\330', '\331', '\332', '\333', '\334', '\335', '\336', '\337', | ||
| 77 | '\340', '\341', '\342', '\343', '\344', '\345', '\346', '\347', | ||
| 78 | '\350', '\351', '\352', '\353', '\354', '\355', '\356', '\357', | ||
| 79 | '\360', '\361', '\362', '\363', '\364', '\365', '\366', '\367', | ||
| 80 | '\370', '\371', '\372', '\373', '\374', '\375', '\376', '\377', | ||
| 81 | }; | ||
| 82 | |||
| 83 | int | ||
| 84 | strcasecmp(s1, s2) | ||
| 85 | const char *s1, *s2; | ||
| 86 | { | ||
| 87 | register const u_char *cm = charmap, | ||
| 88 | *us1 = (const u_char *)s1, | ||
| 89 | *us2 = (const u_char *)s2; | ||
| 90 | |||
| 91 | while (cm[*us1] == cm[*us2++]) | ||
| 92 | if (*us1++ == '\0') | ||
| 93 | return (0); | ||
| 94 | return (cm[*us1] - cm[*--us2]); | ||
| 95 | } | ||
| 96 | |||
| 97 | int | ||
| 98 | strncasecmp(s1, s2, n) | ||
| 99 | const char *s1, *s2; | ||
| 100 | register size_t n; | ||
| 101 | { | ||
| 102 | if (n != 0) { | ||
| 103 | register const u_char *cm = charmap, | ||
| 104 | *us1 = (const u_char *)s1, | ||
| 105 | *us2 = (const u_char *)s2; | ||
| 106 | |||
| 107 | do { | ||
| 108 | if (cm[*us1] != cm[*us2++]) | ||
| 109 | return (cm[*us1] - cm[*--us2]); | ||
| 110 | if (*us1++ == '\0') | ||
| 111 | break; | ||
| 112 | } while (--n != 0); | ||
| 113 | } | ||
| 114 | return (0); | ||
| 115 | } | ||
diff --git a/src/lib/libc/string/strcat.3 b/src/lib/libc/string/strcat.3 new file mode 100644 index 0000000000..5357d65754 --- /dev/null +++ b/src/lib/libc/string/strcat.3 | |||
| @@ -0,0 +1,94 @@ | |||
| 1 | .\" Copyright (c) 1990, 1991 The Regents of the University of California. | ||
| 2 | .\" All rights reserved. | ||
| 3 | .\" | ||
| 4 | .\" This code is derived from software contributed to Berkeley by | ||
| 5 | .\" Chris Torek and the American National Standards Committee X3, | ||
| 6 | .\" on Information Processing Systems. | ||
| 7 | .\" | ||
| 8 | .\" Redistribution and use in source and binary forms, with or without | ||
| 9 | .\" modification, are permitted provided that the following conditions | ||
| 10 | .\" are met: | ||
| 11 | .\" 1. Redistributions of source code must retain the above copyright | ||
| 12 | .\" notice, this list of conditions and the following disclaimer. | ||
| 13 | .\" 2. Redistributions in binary form must reproduce the above copyright | ||
| 14 | .\" notice, this list of conditions and the following disclaimer in the | ||
| 15 | .\" documentation and/or other materials provided with the distribution. | ||
| 16 | .\" 3. All advertising materials mentioning features or use of this software | ||
| 17 | .\" must display the following acknowledgement: | ||
| 18 | .\" This product includes software developed by the University of | ||
| 19 | .\" California, Berkeley and its contributors. | ||
| 20 | .\" 4. Neither the name of the University nor the names of its contributors | ||
| 21 | .\" may be used to endorse or promote products derived from this software | ||
| 22 | .\" without specific prior written permission. | ||
| 23 | .\" | ||
| 24 | .\" THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND | ||
| 25 | .\" ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE | ||
| 26 | .\" IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE | ||
| 27 | .\" ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE | ||
| 28 | .\" FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL | ||
| 29 | .\" DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS | ||
| 30 | .\" OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) | ||
| 31 | .\" HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT | ||
| 32 | .\" LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY | ||
| 33 | .\" OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF | ||
| 34 | .\" SUCH DAMAGE. | ||
| 35 | .\" | ||
| 36 | .\" from: @(#)strcat.3 5.6 (Berkeley) 6/29/91 | ||
| 37 | .\" $Id: strcat.3,v 1.1.1.1 1995/10/18 08:42:22 deraadt Exp $ | ||
| 38 | .\" | ||
| 39 | .Dd June 29, 1991 | ||
| 40 | .Dt STRCAT 3 | ||
| 41 | .Os | ||
| 42 | .Sh NAME | ||
| 43 | .Nm strcat | ||
| 44 | .Nd concatenate strings | ||
| 45 | .Sh SYNOPSIS | ||
| 46 | .Fd #include <string.h> | ||
| 47 | .Ft char * | ||
| 48 | .Fn strcat "char *s" "const char *append" | ||
| 49 | .Ft char * | ||
| 50 | .Fn strncat "char *s" "const char *append" "size_t count" | ||
| 51 | .Sh DESCRIPTION | ||
| 52 | The | ||
| 53 | .Fn strcat | ||
| 54 | and | ||
| 55 | .Fn strncat | ||
| 56 | functions | ||
| 57 | append a copy of the null-terminated string | ||
| 58 | .Fa append | ||
| 59 | to the end of the null-terminated string | ||
| 60 | .Fa s , | ||
| 61 | then add a terminating | ||
| 62 | .Ql \e0 . | ||
| 63 | The string | ||
| 64 | .Fa s | ||
| 65 | must have sufficient space to hold the result. | ||
| 66 | .Pp | ||
| 67 | The | ||
| 68 | .Fn strncat | ||
| 69 | function | ||
| 70 | appends not more than | ||
| 71 | .Fa count | ||
| 72 | characters. | ||
| 73 | .Sh RETURN VALUES | ||
| 74 | The | ||
| 75 | .Fn strcat | ||
| 76 | and | ||
| 77 | .Fn strncat | ||
| 78 | functions | ||
| 79 | return the pointer | ||
| 80 | .Fa s . | ||
| 81 | .Sh SEE ALSO | ||
| 82 | .Xr bcopy 3 , | ||
| 83 | .Xr memccpy 3 , | ||
| 84 | .Xr memcpy 3 , | ||
| 85 | .Xr memmove 3 , | ||
| 86 | .Xr strcpy 3 | ||
| 87 | .Sh STANDARDS | ||
| 88 | The | ||
| 89 | .Fn strcat | ||
| 90 | and | ||
| 91 | .Fn strncat | ||
| 92 | functions | ||
| 93 | conform to | ||
| 94 | .St -ansiC . | ||
diff --git a/src/lib/libc/string/strcat.c b/src/lib/libc/string/strcat.c new file mode 100644 index 0000000000..e741b84f03 --- /dev/null +++ b/src/lib/libc/string/strcat.c | |||
| @@ -0,0 +1,51 @@ | |||
| 1 | /* | ||
| 2 | * Copyright (c) 1988 Regents of the University of California. | ||
| 3 | * All rights reserved. | ||
| 4 | * | ||
| 5 | * Redistribution and use in source and binary forms, with or without | ||
| 6 | * modification, are permitted provided that the following conditions | ||
| 7 | * are met: | ||
| 8 | * 1. Redistributions of source code must retain the above copyright | ||
| 9 | * notice, this list of conditions and the following disclaimer. | ||
| 10 | * 2. Redistributions in binary form must reproduce the above copyright | ||
| 11 | * notice, this list of conditions and the following disclaimer in the | ||
| 12 | * documentation and/or other materials provided with the distribution. | ||
| 13 | * 3. All advertising materials mentioning features or use of this software | ||
| 14 | * must display the following acknowledgement: | ||
| 15 | * This product includes software developed by the University of | ||
| 16 | * California, Berkeley and its contributors. | ||
| 17 | * 4. Neither the name of the University nor the names of its contributors | ||
| 18 | * may be used to endorse or promote products derived from this software | ||
| 19 | * without specific prior written permission. | ||
| 20 | * | ||
| 21 | * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND | ||
| 22 | * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE | ||
| 23 | * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE | ||
| 24 | * ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE | ||
| 25 | * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL | ||
| 26 | * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS | ||
| 27 | * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) | ||
| 28 | * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT | ||
| 29 | * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY | ||
| 30 | * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF | ||
| 31 | * SUCH DAMAGE. | ||
| 32 | */ | ||
| 33 | |||
| 34 | #if defined(LIBC_SCCS) && !defined(lint) | ||
| 35 | /*static char *sccsid = "from: @(#)strcat.c 5.6 (Berkeley) 2/24/91";*/ | ||
| 36 | static char *rcsid = "$Id: strcat.c,v 1.1.1.1 1995/10/18 08:42:22 deraadt Exp $"; | ||
| 37 | #endif /* LIBC_SCCS and not lint */ | ||
| 38 | |||
| 39 | #include <string.h> | ||
| 40 | |||
| 41 | char * | ||
| 42 | strcat(s, append) | ||
| 43 | register char *s; | ||
| 44 | register const char *append; | ||
| 45 | { | ||
| 46 | char *save = s; | ||
| 47 | |||
| 48 | for (; *s; ++s); | ||
| 49 | while (*s++ = *append++); | ||
| 50 | return(save); | ||
| 51 | } | ||
diff --git a/src/lib/libc/string/strchr.3 b/src/lib/libc/string/strchr.3 new file mode 100644 index 0000000000..18b50301f3 --- /dev/null +++ b/src/lib/libc/string/strchr.3 | |||
| @@ -0,0 +1,88 @@ | |||
| 1 | .\" Copyright (c) 1990, 1991 The Regents of the University of California. | ||
| 2 | .\" All rights reserved. | ||
| 3 | .\" | ||
| 4 | .\" This code is derived from software contributed to Berkeley by | ||
| 5 | .\" Chris Torek and the American National Standards Committee X3, | ||
| 6 | .\" on Information Processing Systems. | ||
| 7 | .\" | ||
| 8 | .\" Redistribution and use in source and binary forms, with or without | ||
| 9 | .\" modification, are permitted provided that the following conditions | ||
| 10 | .\" are met: | ||
| 11 | .\" 1. Redistributions of source code must retain the above copyright | ||
| 12 | .\" notice, this list of conditions and the following disclaimer. | ||
| 13 | .\" 2. Redistributions in binary form must reproduce the above copyright | ||
| 14 | .\" notice, this list of conditions and the following disclaimer in the | ||
| 15 | .\" documentation and/or other materials provided with the distribution. | ||
| 16 | .\" 3. All advertising materials mentioning features or use of this software | ||
| 17 | .\" must display the following acknowledgement: | ||
| 18 | .\" This product includes software developed by the University of | ||
| 19 | .\" California, Berkeley and its contributors. | ||
| 20 | .\" 4. Neither the name of the University nor the names of its contributors | ||
| 21 | .\" may be used to endorse or promote products derived from this software | ||
| 22 | .\" without specific prior written permission. | ||
| 23 | .\" | ||
| 24 | .\" THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND | ||
| 25 | .\" ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE | ||
| 26 | .\" IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE | ||
| 27 | .\" ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE | ||
| 28 | .\" FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL | ||
| 29 | .\" DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS | ||
| 30 | .\" OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) | ||
| 31 | .\" HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT | ||
| 32 | .\" LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY | ||
| 33 | .\" OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF | ||
| 34 | .\" SUCH DAMAGE. | ||
| 35 | .\" | ||
| 36 | .\" from: @(#)strchr.3 5.4 (Berkeley) 6/29/91 | ||
| 37 | .\" $Id: strchr.3,v 1.1.1.1 1995/10/18 08:42:22 deraadt Exp $ | ||
| 38 | .\" | ||
| 39 | .Dd June 29, 1991 | ||
| 40 | .Dt STRCHR 3 | ||
| 41 | .Os | ||
| 42 | .Sh NAME | ||
| 43 | .Nm strchr | ||
| 44 | .Nd locate character in string | ||
| 45 | .Sh SYNOPSIS | ||
| 46 | .Fd #include <string.h> | ||
| 47 | .Ft char * | ||
| 48 | .Fn strchr "const char *s" "int c" | ||
| 49 | .Sh DESCRIPTION | ||
| 50 | The | ||
| 51 | .Fn strchr | ||
| 52 | function locates the first occurrence of | ||
| 53 | .Ar c | ||
| 54 | in the string pointed to by | ||
| 55 | .Ar s . | ||
| 56 | The terminating | ||
| 57 | .Dv NULL | ||
| 58 | character is considered part of the string. | ||
| 59 | If | ||
| 60 | .Fa c | ||
| 61 | is | ||
| 62 | .Ql \e0 , | ||
| 63 | .Fn strchr | ||
| 64 | locates the terminating | ||
| 65 | .Ql \e0 . | ||
| 66 | .Sh RETURN VALUES | ||
| 67 | The function | ||
| 68 | .Fn strchr | ||
| 69 | returns a pointer to the located character, or | ||
| 70 | .Dv NULL | ||
| 71 | if the character does not appear in the string. | ||
| 72 | .Sh SEE ALSO | ||
| 73 | .Xr index 3 , | ||
| 74 | .Xr memchr 3 , | ||
| 75 | .Xr rindex 3 , | ||
| 76 | .Xr strcspn 3 , | ||
| 77 | .Xr strpbrk 3 , | ||
| 78 | .Xr strrchr 3 , | ||
| 79 | .Xr strsep 3 , | ||
| 80 | .Xr strspn 3 , | ||
| 81 | .Xr strstr 3 , | ||
| 82 | .Xr strtok 3 | ||
| 83 | .Sh STANDARDS | ||
| 84 | The | ||
| 85 | .Fn strchr | ||
| 86 | function | ||
| 87 | conforms to | ||
| 88 | .St -ansiC . | ||
diff --git a/src/lib/libc/string/strcmp.3 b/src/lib/libc/string/strcmp.3 new file mode 100644 index 0000000000..fecaa85410 --- /dev/null +++ b/src/lib/libc/string/strcmp.3 | |||
| @@ -0,0 +1,94 @@ | |||
| 1 | .\" Copyright (c) 1990, 1991 The Regents of the University of California. | ||
| 2 | .\" All rights reserved. | ||
| 3 | .\" | ||
| 4 | .\" This code is derived from software contributed to Berkeley by | ||
| 5 | .\" Chris Torek and the American National Standards Committee X3, | ||
| 6 | .\" on Information Processing Systems. | ||
| 7 | .\" | ||
| 8 | .\" Redistribution and use in source and binary forms, with or without | ||
| 9 | .\" modification, are permitted provided that the following conditions | ||
| 10 | .\" are met: | ||
| 11 | .\" 1. Redistributions of source code must retain the above copyright | ||
| 12 | .\" notice, this list of conditions and the following disclaimer. | ||
| 13 | .\" 2. Redistributions in binary form must reproduce the above copyright | ||
| 14 | .\" notice, this list of conditions and the following disclaimer in the | ||
| 15 | .\" documentation and/or other materials provided with the distribution. | ||
| 16 | .\" 3. All advertising materials mentioning features or use of this software | ||
| 17 | .\" must display the following acknowledgement: | ||
| 18 | .\" This product includes software developed by the University of | ||
| 19 | .\" California, Berkeley and its contributors. | ||
| 20 | .\" 4. Neither the name of the University nor the names of its contributors | ||
| 21 | .\" may be used to endorse or promote products derived from this software | ||
| 22 | .\" without specific prior written permission. | ||
| 23 | .\" | ||
| 24 | .\" THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND | ||
| 25 | .\" ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE | ||
| 26 | .\" IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE | ||
| 27 | .\" ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE | ||
| 28 | .\" FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL | ||
| 29 | .\" DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS | ||
| 30 | .\" OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) | ||
| 31 | .\" HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT | ||
| 32 | .\" LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY | ||
| 33 | .\" OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF | ||
| 34 | .\" SUCH DAMAGE. | ||
| 35 | .\" | ||
| 36 | .\" from: @(#)strcmp.3 5.6 (Berkeley) 6/29/91 | ||
| 37 | .\" $Id: strcmp.3,v 1.1.1.1 1995/10/18 08:42:22 deraadt Exp $ | ||
| 38 | .\" | ||
| 39 | .Dd June 29, 1991 | ||
| 40 | .Dt STRCMP 3 | ||
| 41 | .Os | ||
| 42 | .Sh NAME | ||
| 43 | .Nm strcmp | ||
| 44 | .Nd compare strings | ||
| 45 | .Sh SYNOPSIS | ||
| 46 | .Fd #include <string.h> | ||
| 47 | .Ft int | ||
| 48 | .Fn strcmp "const char *s1" "const char *s2" | ||
| 49 | .Ft int | ||
| 50 | .Fn strncmp "const char *s1" "const char *s2" "size_t len" | ||
| 51 | .Sh DESCRIPTION | ||
| 52 | The | ||
| 53 | .Fn strcmp | ||
| 54 | and | ||
| 55 | .Fn strncmp | ||
| 56 | functions | ||
| 57 | lexicographically compare the null-terminated strings | ||
| 58 | .Fa s1 | ||
| 59 | and | ||
| 60 | .Fa s2 . | ||
| 61 | .Sh RETURN VALUES | ||
| 62 | The | ||
| 63 | .Fn strcmp | ||
| 64 | and | ||
| 65 | .Fn strncmp | ||
| 66 | return an integer greater than, equal to, or less than 0, according | ||
| 67 | as the string | ||
| 68 | .Fa s1 | ||
| 69 | is greater than, equal to, or less than the string | ||
| 70 | .Fa s2 . | ||
| 71 | The comparison is done using unsigned characters, so that | ||
| 72 | .Ql \e200 | ||
| 73 | is greater than | ||
| 74 | .Ql \e0 . | ||
| 75 | .Pp | ||
| 76 | The | ||
| 77 | .Fn strncmp | ||
| 78 | compares not more than | ||
| 79 | .Fa len | ||
| 80 | characters. | ||
| 81 | .Sh SEE ALSO | ||
| 82 | .Xr bcmp 3 , | ||
| 83 | .Xr memcmp 3 , | ||
| 84 | .Xr strcasecmp 3 , | ||
| 85 | .Xr strcoll 3 , | ||
| 86 | .Xr strxfrm 3 | ||
| 87 | .Sh STANDARDS | ||
| 88 | The | ||
| 89 | .Fn strcmp | ||
| 90 | and | ||
| 91 | .Fn strncmp | ||
| 92 | functions | ||
| 93 | conform to | ||
| 94 | .St -ansiC . | ||
diff --git a/src/lib/libc/string/strcmp.c b/src/lib/libc/string/strcmp.c new file mode 100644 index 0000000000..ae19e2e26e --- /dev/null +++ b/src/lib/libc/string/strcmp.c | |||
| @@ -0,0 +1,55 @@ | |||
| 1 | /*- | ||
| 2 | * Copyright (c) 1990 The Regents of the University of California. | ||
| 3 | * All rights reserved. | ||
| 4 | * | ||
| 5 | * This code is derived from software contributed to Berkeley by | ||
| 6 | * Chris Torek. | ||
| 7 | * | ||
| 8 | * Redistribution and use in source and binary forms, with or without | ||
| 9 | * modification, are permitted provided that the following conditions | ||
| 10 | * are met: | ||
| 11 | * 1. Redistributions of source code must retain the above copyright | ||
| 12 | * notice, this list of conditions and the following disclaimer. | ||
| 13 | * 2. Redistributions in binary form must reproduce the above copyright | ||
| 14 | * notice, this list of conditions and the following disclaimer in the | ||
| 15 | * documentation and/or other materials provided with the distribution. | ||
| 16 | * 3. All advertising materials mentioning features or use of this software | ||
| 17 | * must display the following acknowledgement: | ||
| 18 | * This product includes software developed by the University of | ||
| 19 | * California, Berkeley and its contributors. | ||
| 20 | * 4. Neither the name of the University nor the names of its contributors | ||
| 21 | * may be used to endorse or promote products derived from this software | ||
| 22 | * without specific prior written permission. | ||
| 23 | * | ||
| 24 | * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND | ||
| 25 | * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE | ||
| 26 | * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE | ||
| 27 | * ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE | ||
| 28 | * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL | ||
| 29 | * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS | ||
| 30 | * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) | ||
| 31 | * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT | ||
| 32 | * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY | ||
| 33 | * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF | ||
| 34 | * SUCH DAMAGE. | ||
| 35 | */ | ||
| 36 | |||
| 37 | #if defined(LIBC_SCCS) && !defined(lint) | ||
| 38 | /*static char *sccsid = "from: @(#)strcmp.c 5.5 (Berkeley) 1/26/91";*/ | ||
| 39 | static char *rcsid = "$Id: strcmp.c,v 1.1.1.1 1995/10/18 08:42:22 deraadt Exp $"; | ||
| 40 | #endif /* LIBC_SCCS and not lint */ | ||
| 41 | |||
| 42 | #include <string.h> | ||
| 43 | |||
| 44 | /* | ||
| 45 | * Compare strings. | ||
| 46 | */ | ||
| 47 | int | ||
| 48 | strcmp(s1, s2) | ||
| 49 | register const char *s1, *s2; | ||
| 50 | { | ||
| 51 | while (*s1 == *s2++) | ||
| 52 | if (*s1++ == 0) | ||
| 53 | return (0); | ||
| 54 | return (*(unsigned char *)s1 - *(unsigned char *)--s2); | ||
| 55 | } | ||
diff --git a/src/lib/libc/string/strcoll.3 b/src/lib/libc/string/strcoll.3 new file mode 100644 index 0000000000..12f73f98f2 --- /dev/null +++ b/src/lib/libc/string/strcoll.3 | |||
| @@ -0,0 +1,75 @@ | |||
| 1 | .\" Copyright (c) 1990, 1991 The Regents of the University of California. | ||
| 2 | .\" All rights reserved. | ||
| 3 | .\" | ||
| 4 | .\" This code is derived from software contributed to Berkeley by | ||
| 5 | .\" Chris Torek and the American National Standards Committee X3, | ||
| 6 | .\" on Information Processing Systems. | ||
| 7 | .\" | ||
| 8 | .\" Redistribution and use in source and binary forms, with or without | ||
| 9 | .\" modification, are permitted provided that the following conditions | ||
| 10 | .\" are met: | ||
| 11 | .\" 1. Redistributions of source code must retain the above copyright | ||
| 12 | .\" notice, this list of conditions and the following disclaimer. | ||
| 13 | .\" 2. Redistributions in binary form must reproduce the above copyright | ||
| 14 | .\" notice, this list of conditions and the following disclaimer in the | ||
| 15 | .\" documentation and/or other materials provided with the distribution. | ||
| 16 | .\" 3. All advertising materials mentioning features or use of this software | ||
| 17 | .\" must display the following acknowledgement: | ||
| 18 | .\" This product includes software developed by the University of | ||
| 19 | .\" California, Berkeley and its contributors. | ||
| 20 | .\" 4. Neither the name of the University nor the names of its contributors | ||
| 21 | .\" may be used to endorse or promote products derived from this software | ||
| 22 | .\" without specific prior written permission. | ||
| 23 | .\" | ||
| 24 | .\" THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND | ||
| 25 | .\" ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE | ||
| 26 | .\" IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE | ||
| 27 | .\" ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE | ||
| 28 | .\" FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL | ||
| 29 | .\" DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS | ||
| 30 | .\" OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) | ||
| 31 | .\" HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT | ||
| 32 | .\" LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY | ||
| 33 | .\" OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF | ||
| 34 | .\" SUCH DAMAGE. | ||
| 35 | .\" | ||
| 36 | .\" from: @(#)strcoll.3 5.6 (Berkeley) 6/29/91 | ||
| 37 | .\" $Id: strcoll.3,v 1.1.1.1 1995/10/18 08:42:22 deraadt Exp $ | ||
| 38 | .\" | ||
| 39 | .Dd June 29, 1991 | ||
| 40 | .Dt STRCOLL 3 | ||
| 41 | .Os | ||
| 42 | .Sh NAME | ||
| 43 | .Nm strcoll | ||
| 44 | .Nd compare strings according to current collation | ||
| 45 | .Sh SYNOPSIS | ||
| 46 | .Fd #include <string.h> | ||
| 47 | .Ft int | ||
| 48 | .Fn strcoll "const char *s1" "const char *s2" | ||
| 49 | .Sh DESCRIPTION | ||
| 50 | The | ||
| 51 | .Fn strcoll | ||
| 52 | function | ||
| 53 | lexicographically compares the null-terminated strings | ||
| 54 | .Fa s1 | ||
| 55 | and | ||
| 56 | .Fa s2 | ||
| 57 | according to the current locale collation | ||
| 58 | and returns an integer greater than, equal to, or less than 0, | ||
| 59 | according as | ||
| 60 | .Fa s1 | ||
| 61 | is greater than, equal to, or less than | ||
| 62 | .Fa s2 . | ||
| 63 | .Sh SEE ALSO | ||
| 64 | .Xr bcmp 3 , | ||
| 65 | .Xr memcmp 3 , | ||
| 66 | .Xr setlocale 3 , | ||
| 67 | .Xr strcasecmp 3 , | ||
| 68 | .Xr strcmp 3 , | ||
| 69 | .Xr strxfrm 3 | ||
| 70 | .Sh STANDARDS | ||
| 71 | The | ||
| 72 | .Fn strcoll | ||
| 73 | function | ||
| 74 | conforms to | ||
| 75 | .St -ansiC . | ||
diff --git a/src/lib/libc/string/strcoll.c b/src/lib/libc/string/strcoll.c new file mode 100644 index 0000000000..86c742cba9 --- /dev/null +++ b/src/lib/libc/string/strcoll.c | |||
| @@ -0,0 +1,53 @@ | |||
| 1 | /*- | ||
| 2 | * Copyright (c) 1990 The Regents of the University of California. | ||
| 3 | * All rights reserved. | ||
| 4 | * | ||
| 5 | * This code is derived from software contributed to Berkeley by | ||
| 6 | * Chris Torek. | ||
| 7 | * | ||
| 8 | * Redistribution and use in source and binary forms, with or without | ||
| 9 | * modification, are permitted provided that the following conditions | ||
| 10 | * are met: | ||
| 11 | * 1. Redistributions of source code must retain the above copyright | ||
| 12 | * notice, this list of conditions and the following disclaimer. | ||
| 13 | * 2. Redistributions in binary form must reproduce the above copyright | ||
| 14 | * notice, this list of conditions and the following disclaimer in the | ||
| 15 | * documentation and/or other materials provided with the distribution. | ||
| 16 | * 3. All advertising materials mentioning features or use of this software | ||
| 17 | * must display the following acknowledgement: | ||
| 18 | * This product includes software developed by the University of | ||
| 19 | * California, Berkeley and its contributors. | ||
| 20 | * 4. Neither the name of the University nor the names of its contributors | ||
| 21 | * may be used to endorse or promote products derived from this software | ||
| 22 | * without specific prior written permission. | ||
| 23 | * | ||
| 24 | * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND | ||
| 25 | * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE | ||
| 26 | * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE | ||
| 27 | * ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE | ||
| 28 | * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL | ||
| 29 | * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS | ||
| 30 | * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) | ||
| 31 | * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT | ||
| 32 | * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY | ||
| 33 | * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF | ||
| 34 | * SUCH DAMAGE. | ||
| 35 | */ | ||
| 36 | |||
| 37 | #if defined(LIBC_SCCS) && !defined(lint) | ||
| 38 | /*static char *sccsid = "from: @(#)strcoll.c 5.2 (Berkeley) 1/26/91";*/ | ||
| 39 | static char *rcsid = "$Id: strcoll.c,v 1.1.1.1 1995/10/18 08:42:22 deraadt Exp $"; | ||
| 40 | #endif /* LIBC_SCCS and not lint */ | ||
| 41 | |||
| 42 | #include <string.h> | ||
| 43 | |||
| 44 | /* | ||
| 45 | * Compare strings according to LC_COLLATE category of current locale. | ||
| 46 | */ | ||
| 47 | int | ||
| 48 | strcoll(s1, s2) | ||
| 49 | const char *s1, *s2; | ||
| 50 | { | ||
| 51 | /* LC_COLLATE is unimplemented, hence always "C" */ | ||
| 52 | return (strcmp(s1, s2)); | ||
| 53 | } | ||
diff --git a/src/lib/libc/string/strcpy.3 b/src/lib/libc/string/strcpy.3 new file mode 100644 index 0000000000..1ca12c2707 --- /dev/null +++ b/src/lib/libc/string/strcpy.3 | |||
| @@ -0,0 +1,122 @@ | |||
| 1 | .\" Copyright (c) 1990, 1991 The Regents of the University of California. | ||
| 2 | .\" All rights reserved. | ||
| 3 | .\" | ||
| 4 | .\" This code is derived from software contributed to Berkeley by | ||
| 5 | .\" Chris Torek and the American National Standards Committee X3, | ||
| 6 | .\" on Information Processing Systems. | ||
| 7 | .\" | ||
| 8 | .\" Redistribution and use in source and binary forms, with or without | ||
| 9 | .\" modification, are permitted provided that the following conditions | ||
| 10 | .\" are met: | ||
| 11 | .\" 1. Redistributions of source code must retain the above copyright | ||
| 12 | .\" notice, this list of conditions and the following disclaimer. | ||
| 13 | .\" 2. Redistributions in binary form must reproduce the above copyright | ||
| 14 | .\" notice, this list of conditions and the following disclaimer in the | ||
| 15 | .\" documentation and/or other materials provided with the distribution. | ||
| 16 | .\" 3. All advertising materials mentioning features or use of this software | ||
| 17 | .\" must display the following acknowledgement: | ||
| 18 | .\" This product includes software developed by the University of | ||
| 19 | .\" California, Berkeley and its contributors. | ||
| 20 | .\" 4. Neither the name of the University nor the names of its contributors | ||
| 21 | .\" may be used to endorse or promote products derived from this software | ||
| 22 | .\" without specific prior written permission. | ||
| 23 | .\" | ||
| 24 | .\" THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND | ||
| 25 | .\" ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE | ||
| 26 | .\" IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE | ||
| 27 | .\" ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE | ||
| 28 | .\" FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL | ||
| 29 | .\" DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS | ||
| 30 | .\" OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) | ||
| 31 | .\" HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT | ||
| 32 | .\" LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY | ||
| 33 | .\" OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF | ||
| 34 | .\" SUCH DAMAGE. | ||
| 35 | .\" | ||
| 36 | .\" from: @(#)strcpy.3 5.4 (Berkeley) 6/29/91 | ||
| 37 | .\" $Id: strcpy.3,v 1.1.1.1 1995/10/18 08:42:22 deraadt Exp $ | ||
| 38 | .\" | ||
| 39 | .Dd June 29, 1991 | ||
| 40 | .Dt STRCPY 3 | ||
| 41 | .Os BSD 4 | ||
| 42 | .Sh NAME | ||
| 43 | .Nm strcpy | ||
| 44 | .Nd copy strings | ||
| 45 | .Sh SYNOPSIS | ||
| 46 | .Fd #include <string.h> | ||
| 47 | .Ft char * | ||
| 48 | .Fn strcpy "char *dst" "const char *src" | ||
| 49 | .Ft char * | ||
| 50 | .Fn strncpy "char *dst" "const char *src" "size_t len" | ||
| 51 | .Sh DESCRIPTION | ||
| 52 | The | ||
| 53 | .Fn strcpy | ||
| 54 | and | ||
| 55 | .Fn strncpy | ||
| 56 | functions | ||
| 57 | copy the string | ||
| 58 | .Fa src | ||
| 59 | to | ||
| 60 | .Fa dst | ||
| 61 | (including the terminating | ||
| 62 | .Ql \e0 | ||
| 63 | character). | ||
| 64 | .Pp | ||
| 65 | The | ||
| 66 | .Fn strncpy | ||
| 67 | copies not more than | ||
| 68 | .Fa len | ||
| 69 | characters into | ||
| 70 | .Fa dst , | ||
| 71 | appending | ||
| 72 | .Ql \e0 | ||
| 73 | characters if | ||
| 74 | .Fa src | ||
| 75 | is less than | ||
| 76 | .Fa len | ||
| 77 | characters long, and | ||
| 78 | .Em not | ||
| 79 | terminating | ||
| 80 | .Fa dst | ||
| 81 | if | ||
| 82 | .Fa src | ||
| 83 | is more than | ||
| 84 | .Fa len | ||
| 85 | characters long. | ||
| 86 | .Sh RETURN VALUES | ||
| 87 | The | ||
| 88 | .Fn strcpy | ||
| 89 | and | ||
| 90 | .Fn strncpy | ||
| 91 | functions | ||
| 92 | return | ||
| 93 | .Fa dst . | ||
| 94 | .Sh EXAMPLES | ||
| 95 | The following sets | ||
| 96 | .Dq Li chararray | ||
| 97 | to | ||
| 98 | .Dq Li abc\e0\e0\e0 : | ||
| 99 | .Bd -literal -offset indent | ||
| 100 | (void)strncpy(chararray, "abc", 6). | ||
| 101 | .Ed | ||
| 102 | .Pp | ||
| 103 | The following sets | ||
| 104 | .Dq Li chararray | ||
| 105 | to | ||
| 106 | .Dq Li abcdef : | ||
| 107 | .Bd -literal -offset indent | ||
| 108 | (void)strncpy(chararray, "abcdefgh", 6); | ||
| 109 | .Ed | ||
| 110 | .Sh SEE ALSO | ||
| 111 | .Xr bcopy 3 , | ||
| 112 | .Xr memccpy 3 , | ||
| 113 | .Xr memcpy 3 , | ||
| 114 | .Xr memmove 3 | ||
| 115 | .Sh STANDARDS | ||
| 116 | The | ||
| 117 | .Fn strcpy | ||
| 118 | and | ||
| 119 | .Fn strncpy | ||
| 120 | functions | ||
| 121 | conform to | ||
| 122 | .St -ansiC . | ||
diff --git a/src/lib/libc/string/strcpy.c b/src/lib/libc/string/strcpy.c new file mode 100644 index 0000000000..669bfde23e --- /dev/null +++ b/src/lib/libc/string/strcpy.c | |||
| @@ -0,0 +1,50 @@ | |||
| 1 | /* | ||
| 2 | * Copyright (c) 1988 Regents of the University of California. | ||
| 3 | * All rights reserved. | ||
| 4 | * | ||
| 5 | * Redistribution and use in source and binary forms, with or without | ||
| 6 | * modification, are permitted provided that the following conditions | ||
| 7 | * are met: | ||
| 8 | * 1. Redistributions of source code must retain the above copyright | ||
| 9 | * notice, this list of conditions and the following disclaimer. | ||
| 10 | * 2. Redistributions in binary form must reproduce the above copyright | ||
| 11 | * notice, this list of conditions and the following disclaimer in the | ||
| 12 | * documentation and/or other materials provided with the distribution. | ||
| 13 | * 3. All advertising materials mentioning features or use of this software | ||
| 14 | * must display the following acknowledgement: | ||
| 15 | * This product includes software developed by the University of | ||
| 16 | * California, Berkeley and its contributors. | ||
| 17 | * 4. Neither the name of the University nor the names of its contributors | ||
| 18 | * may be used to endorse or promote products derived from this software | ||
| 19 | * without specific prior written permission. | ||
| 20 | * | ||
| 21 | * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND | ||
| 22 | * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE | ||
| 23 | * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE | ||
| 24 | * ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE | ||
| 25 | * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL | ||
| 26 | * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS | ||
| 27 | * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) | ||
| 28 | * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT | ||
| 29 | * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY | ||
| 30 | * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF | ||
| 31 | * SUCH DAMAGE. | ||
| 32 | */ | ||
| 33 | |||
| 34 | #if defined(LIBC_SCCS) && !defined(lint) | ||
| 35 | /*static char *sccsid = "from: @(#)strcpy.c 5.7 (Berkeley) 2/24/91";*/ | ||
| 36 | static char *rcsid = "$Id: strcpy.c,v 1.1.1.1 1995/10/18 08:42:22 deraadt Exp $"; | ||
| 37 | #endif /* LIBC_SCCS and not lint */ | ||
| 38 | |||
| 39 | #include <string.h> | ||
| 40 | |||
| 41 | char * | ||
| 42 | strcpy(to, from) | ||
| 43 | register char *to; | ||
| 44 | register const char *from; | ||
| 45 | { | ||
| 46 | char *save = to; | ||
| 47 | |||
| 48 | for (; *to = *from; ++from, ++to); | ||
| 49 | return(save); | ||
| 50 | } | ||
diff --git a/src/lib/libc/string/strcspn.3 b/src/lib/libc/string/strcspn.3 new file mode 100644 index 0000000000..cc9e5c2fe3 --- /dev/null +++ b/src/lib/libc/string/strcspn.3 | |||
| @@ -0,0 +1,85 @@ | |||
| 1 | .\" Copyright (c) 1990, 1991 The Regents of the University of California. | ||
| 2 | .\" All rights reserved. | ||
| 3 | .\" | ||
| 4 | .\" This code is derived from software contributed to Berkeley by | ||
| 5 | .\" Chris Torek and the American National Standards Committee X3, | ||
| 6 | .\" on Information Processing Systems. | ||
| 7 | .\" | ||
| 8 | .\" Redistribution and use in source and binary forms, with or without | ||
| 9 | .\" modification, are permitted provided that the following conditions | ||
| 10 | .\" are met: | ||
| 11 | .\" 1. Redistributions of source code must retain the above copyright | ||
| 12 | .\" notice, this list of conditions and the following disclaimer. | ||
| 13 | .\" 2. Redistributions in binary form must reproduce the above copyright | ||
| 14 | .\" notice, this list of conditions and the following disclaimer in the | ||
| 15 | .\" documentation and/or other materials provided with the distribution. | ||
| 16 | .\" 3. All advertising materials mentioning features or use of this software | ||
| 17 | .\" must display the following acknowledgement: | ||
| 18 | .\" This product includes software developed by the University of | ||
| 19 | .\" California, Berkeley and its contributors. | ||
| 20 | .\" 4. Neither the name of the University nor the names of its contributors | ||
| 21 | .\" may be used to endorse or promote products derived from this software | ||
| 22 | .\" without specific prior written permission. | ||
| 23 | .\" | ||
| 24 | .\" THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND | ||
| 25 | .\" ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE | ||
| 26 | .\" IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE | ||
| 27 | .\" ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE | ||
| 28 | .\" FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL | ||
| 29 | .\" DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS | ||
| 30 | .\" OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) | ||
| 31 | .\" HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT | ||
| 32 | .\" LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY | ||
| 33 | .\" OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF | ||
| 34 | .\" SUCH DAMAGE. | ||
| 35 | .\" | ||
| 36 | .\" from: @(#)strcspn.3 5.3 (Berkeley) 6/29/91 | ||
| 37 | .\" $Id: strcspn.3,v 1.1.1.1 1995/10/18 08:42:22 deraadt Exp $ | ||
| 38 | .\" | ||
| 39 | .Dd June 29, 1991 | ||
| 40 | .Dt STRCSPN 3 | ||
| 41 | .Os | ||
| 42 | .Sh NAME | ||
| 43 | .Nm strcspn | ||
| 44 | .Nd span the complement of a string | ||
| 45 | .Sh SYNOPSIS | ||
| 46 | .Fd #include <string.h> | ||
| 47 | .Ft size_t | ||
| 48 | .Fn strcspn "const char *s" "const char *charset" | ||
| 49 | .Sh DESCRIPTION | ||
| 50 | The | ||
| 51 | .Fn strcspn | ||
| 52 | function | ||
| 53 | spans the initial part of the null-terminated string | ||
| 54 | .Fa s | ||
| 55 | as long as the characters from | ||
| 56 | .Fa s | ||
| 57 | do not occur in string | ||
| 58 | .Fa charset | ||
| 59 | (it | ||
| 60 | spans the | ||
| 61 | .Em complement | ||
| 62 | of | ||
| 63 | .Fa charset ) . | ||
| 64 | .Sh RETURN VALUES | ||
| 65 | The | ||
| 66 | .Fn strcspn | ||
| 67 | function | ||
| 68 | returns the number of characters spanned. | ||
| 69 | .Sh SEE ALSO | ||
| 70 | .Xr index 3 , | ||
| 71 | .Xr memchr 3 , | ||
| 72 | .Xr rindex 3 , | ||
| 73 | .Xr strchr 3 , | ||
| 74 | .Xr strpbrk 3 , | ||
| 75 | .Xr strrchr 3 , | ||
| 76 | .Xr strsep 3 , | ||
| 77 | .Xr strspn 3 , | ||
| 78 | .Xr strstr 3 , | ||
| 79 | .Xr strtok 3 | ||
| 80 | .Sh STANDARDS | ||
| 81 | The | ||
| 82 | .Fn strcspn | ||
| 83 | function | ||
| 84 | conforms to | ||
| 85 | .St -ansiC . | ||
diff --git a/src/lib/libc/string/strcspn.c b/src/lib/libc/string/strcspn.c new file mode 100644 index 0000000000..acb4d2a3af --- /dev/null +++ b/src/lib/libc/string/strcspn.c | |||
| @@ -0,0 +1,68 @@ | |||
| 1 | /*- | ||
| 2 | * Copyright (c) 1990 The Regents of the University of California. | ||
| 3 | * All rights reserved. | ||
| 4 | * | ||
| 5 | * This code is derived from software contributed to Berkeley by | ||
| 6 | * Chris Torek. | ||
| 7 | * | ||
| 8 | * Redistribution and use in source and binary forms, with or without | ||
| 9 | * modification, are permitted provided that the following conditions | ||
| 10 | * are met: | ||
| 11 | * 1. Redistributions of source code must retain the above copyright | ||
| 12 | * notice, this list of conditions and the following disclaimer. | ||
| 13 | * 2. Redistributions in binary form must reproduce the above copyright | ||
| 14 | * notice, this list of conditions and the following disclaimer in the | ||
| 15 | * documentation and/or other materials provided with the distribution. | ||
| 16 | * 3. All advertising materials mentioning features or use of this software | ||
| 17 | * must display the following acknowledgement: | ||
| 18 | * This product includes software developed by the University of | ||
| 19 | * California, Berkeley and its contributors. | ||
| 20 | * 4. Neither the name of the University nor the names of its contributors | ||
| 21 | * may be used to endorse or promote products derived from this software | ||
| 22 | * without specific prior written permission. | ||
| 23 | * | ||
| 24 | * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND | ||
| 25 | * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE | ||
| 26 | * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE | ||
| 27 | * ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE | ||
| 28 | * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL | ||
| 29 | * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS | ||
| 30 | * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) | ||
| 31 | * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT | ||
| 32 | * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY | ||
| 33 | * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF | ||
| 34 | * SUCH DAMAGE. | ||
| 35 | */ | ||
| 36 | |||
| 37 | #if defined(LIBC_SCCS) && !defined(lint) | ||
| 38 | /*static char *sccsid = "from: @(#)strcspn.c 5.6 (Berkeley) 1/26/91";*/ | ||
| 39 | static char *rcsid = "$Id: strcspn.c,v 1.1.1.1 1995/10/18 08:42:22 deraadt Exp $"; | ||
| 40 | #endif /* LIBC_SCCS and not lint */ | ||
| 41 | |||
| 42 | #include <string.h> | ||
| 43 | |||
| 44 | /* | ||
| 45 | * Span the complement of string s2. | ||
| 46 | */ | ||
| 47 | size_t | ||
| 48 | strcspn(s1, s2) | ||
| 49 | const char *s1; | ||
| 50 | register const char *s2; | ||
| 51 | { | ||
| 52 | register const char *p, *spanp; | ||
| 53 | register char c, sc; | ||
| 54 | |||
| 55 | /* | ||
| 56 | * Stop as soon as we find any character from s2. Note that there | ||
| 57 | * must be a NUL in s2; it suffices to stop when we find that, too. | ||
| 58 | */ | ||
| 59 | for (p = s1;;) { | ||
| 60 | c = *p++; | ||
| 61 | spanp = s2; | ||
| 62 | do { | ||
| 63 | if ((sc = *spanp++) == c) | ||
| 64 | return (p - 1 - s1); | ||
| 65 | } while (sc != 0); | ||
| 66 | } | ||
| 67 | /* NOTREACHED */ | ||
| 68 | } | ||
diff --git a/src/lib/libc/string/strdup.3 b/src/lib/libc/string/strdup.3 new file mode 100644 index 0000000000..925cbf3d46 --- /dev/null +++ b/src/lib/libc/string/strdup.3 | |||
| @@ -0,0 +1,65 @@ | |||
| 1 | .\" Copyright (c) 1990, 1991 The Regents of the University of California. | ||
| 2 | .\" All rights reserved. | ||
| 3 | .\" | ||
| 4 | .\" Redistribution and use in source and binary forms, with or without | ||
| 5 | .\" modification, are permitted provided that the following conditions | ||
| 6 | .\" are met: | ||
| 7 | .\" 1. Redistributions of source code must retain the above copyright | ||
| 8 | .\" notice, this list of conditions and the following disclaimer. | ||
| 9 | .\" 2. Redistributions in binary form must reproduce the above copyright | ||
| 10 | .\" notice, this list of conditions and the following disclaimer in the | ||
| 11 | .\" documentation and/or other materials provided with the distribution. | ||
| 12 | .\" 3. All advertising materials mentioning features or use of this software | ||
| 13 | .\" must display the following acknowledgement: | ||
| 14 | .\" This product includes software developed by the University of | ||
| 15 | .\" California, Berkeley and its contributors. | ||
| 16 | .\" 4. Neither the name of the University nor the names of its contributors | ||
| 17 | .\" may be used to endorse or promote products derived from this software | ||
| 18 | .\" without specific prior written permission. | ||
| 19 | .\" | ||
| 20 | .\" THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND | ||
| 21 | .\" ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE | ||
| 22 | .\" IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE | ||
| 23 | .\" ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE | ||
| 24 | .\" FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL | ||
| 25 | .\" DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS | ||
| 26 | .\" OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) | ||
| 27 | .\" HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT | ||
| 28 | .\" LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY | ||
| 29 | .\" OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF | ||
| 30 | .\" SUCH DAMAGE. | ||
| 31 | .\" | ||
| 32 | .\" from: @(#)strdup.3 5.3 (Berkeley) 4/19/91 | ||
| 33 | .\" $Id: strdup.3,v 1.1.1.1 1995/10/18 08:42:22 deraadt Exp $ | ||
| 34 | .\" | ||
| 35 | .Dd April 19, 1991 | ||
| 36 | .Dt STRDUP 3 | ||
| 37 | .Os | ||
| 38 | .Sh NAME | ||
| 39 | .Nm strdup | ||
| 40 | .Nd save a copy of a string | ||
| 41 | .Sh SYNOPSIS | ||
| 42 | .Fd #include <string.h> | ||
| 43 | .Ft char * | ||
| 44 | .Fn strdup "const char *str" | ||
| 45 | .Sh DESCRIPTION | ||
| 46 | The | ||
| 47 | .Fn strdup | ||
| 48 | function | ||
| 49 | allocates sufficient memory for a copy | ||
| 50 | of the string | ||
| 51 | .Fa str , | ||
| 52 | does the copy, and returns a pointer to it. | ||
| 53 | The pointer may subsequently be used as an | ||
| 54 | argument to the function | ||
| 55 | .Xr free 3 . | ||
| 56 | .Sh SEE ALSO | ||
| 57 | .Xr free 3 , | ||
| 58 | .Xr malloc 3 , | ||
| 59 | .Xt strcpy 3 , | ||
| 60 | .Xt strlen 3 | ||
| 61 | .Sh HISTORY | ||
| 62 | The | ||
| 63 | .Fn strdup | ||
| 64 | function | ||
| 65 | .Ud . | ||
diff --git a/src/lib/libc/string/strdup.c b/src/lib/libc/string/strdup.c new file mode 100644 index 0000000000..27ede44110 --- /dev/null +++ b/src/lib/libc/string/strdup.c | |||
| @@ -0,0 +1,54 @@ | |||
| 1 | /* | ||
| 2 | * Copyright (c) 1988 The Regents of the University of California. | ||
| 3 | * All rights reserved. | ||
| 4 | * | ||
| 5 | * Redistribution and use in source and binary forms, with or without | ||
| 6 | * modification, are permitted provided that the following conditions | ||
| 7 | * are met: | ||
| 8 | * 1. Redistributions of source code must retain the above copyright | ||
| 9 | * notice, this list of conditions and the following disclaimer. | ||
| 10 | * 2. Redistributions in binary form must reproduce the above copyright | ||
| 11 | * notice, this list of conditions and the following disclaimer in the | ||
| 12 | * documentation and/or other materials provided with the distribution. | ||
| 13 | * 3. All advertising materials mentioning features or use of this software | ||
| 14 | * must display the following acknowledgement: | ||
| 15 | * This product includes software developed by the University of | ||
| 16 | * California, Berkeley and its contributors. | ||
| 17 | * 4. Neither the name of the University nor the names of its contributors | ||
| 18 | * may be used to endorse or promote products derived from this software | ||
| 19 | * without specific prior written permission. | ||
| 20 | * | ||
| 21 | * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND | ||
| 22 | * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE | ||
| 23 | * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE | ||
| 24 | * ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE | ||
| 25 | * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL | ||
| 26 | * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS | ||
| 27 | * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) | ||
| 28 | * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT | ||
| 29 | * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY | ||
| 30 | * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF | ||
| 31 | * SUCH DAMAGE. | ||
| 32 | */ | ||
| 33 | |||
| 34 | #if defined(LIBC_SCCS) && !defined(lint) | ||
| 35 | /*static char *sccsid = "from: @(#)strdup.c 5.4 (Berkeley) 2/24/91";*/ | ||
| 36 | static char *rcsid = "$Id: strdup.c,v 1.1.1.1 1995/10/18 08:42:22 deraadt Exp $"; | ||
| 37 | #endif /* LIBC_SCCS and not lint */ | ||
| 38 | |||
| 39 | #include <stdlib.h> | ||
| 40 | #include <string.h> | ||
| 41 | |||
| 42 | char * | ||
| 43 | strdup(str) | ||
| 44 | const char *str; | ||
| 45 | { | ||
| 46 | size_t len; | ||
| 47 | char *copy; | ||
| 48 | |||
| 49 | len = strlen(str) + 1; | ||
| 50 | if (!(copy = malloc(len))) | ||
| 51 | return((char *)NULL); | ||
| 52 | memcpy(copy, str, len); | ||
| 53 | return(copy); | ||
| 54 | } | ||
diff --git a/src/lib/libc/string/strerror.3 b/src/lib/libc/string/strerror.3 new file mode 100644 index 0000000000..c9d8504dbb --- /dev/null +++ b/src/lib/libc/string/strerror.3 | |||
| @@ -0,0 +1,66 @@ | |||
| 1 | .\" Copyright (c) 1980, 1991 Regents of the University of California. | ||
| 2 | .\" All rights reserved. | ||
| 3 | .\" | ||
| 4 | .\" This code is derived from software contributed to Berkeley by | ||
| 5 | .\" the American National Standards Committee X3, on Information | ||
| 6 | .\" Processing Systems. | ||
| 7 | .\" | ||
| 8 | .\" Redistribution and use in source and binary forms, with or without | ||
| 9 | .\" modification, are permitted provided that the following conditions | ||
| 10 | .\" are met: | ||
| 11 | .\" 1. Redistributions of source code must retain the above copyright | ||
| 12 | .\" notice, this list of conditions and the following disclaimer. | ||
| 13 | .\" 2. Redistributions in binary form must reproduce the above copyright | ||
| 14 | .\" notice, this list of conditions and the following disclaimer in the | ||
| 15 | .\" documentation and/or other materials provided with the distribution. | ||
| 16 | .\" 3. All advertising materials mentioning features or use of this software | ||
| 17 | .\" must display the following acknowledgement: | ||
| 18 | .\" This product includes software developed by the University of | ||
| 19 | .\" California, Berkeley and its contributors. | ||
| 20 | .\" 4. Neither the name of the University nor the names of its contributors | ||
| 21 | .\" may be used to endorse or promote products derived from this software | ||
| 22 | .\" without specific prior written permission. | ||
| 23 | .\" | ||
| 24 | .\" THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND | ||
| 25 | .\" ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE | ||
| 26 | .\" IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE | ||
| 27 | .\" ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE | ||
| 28 | .\" FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL | ||
| 29 | .\" DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS | ||
| 30 | .\" OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) | ||
| 31 | .\" HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT | ||
| 32 | .\" LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY | ||
| 33 | .\" OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF | ||
| 34 | .\" SUCH DAMAGE. | ||
| 35 | .\" | ||
| 36 | .\" from: @(#)strerror.3 6.9 (Berkeley) 6/29/91 | ||
| 37 | .\" $Id: strerror.3,v 1.1.1.1 1995/10/18 08:42:22 deraadt Exp $ | ||
| 38 | .\" | ||
| 39 | .Dd June 29, 1991 | ||
| 40 | .Dt STRERROR 3 | ||
| 41 | .Os BSD 4 | ||
| 42 | .Sh NAME | ||
| 43 | .Nm strerror | ||
| 44 | .Nd get error message string | ||
| 45 | .Sh SYNOPSIS | ||
| 46 | .Fd #include <string.h> | ||
| 47 | .Ft char * | ||
| 48 | .Fn strerror "int errnum" | ||
| 49 | .Sh DESCRIPTION | ||
| 50 | The | ||
| 51 | .Fn strerror | ||
| 52 | function returns a pointer to the language-dependent error message | ||
| 53 | string affiliated with an error number. | ||
| 54 | .Pp | ||
| 55 | The array pointed to is not to be modified by the program, but may be | ||
| 56 | overwritten by subsequent calls to | ||
| 57 | .Fn strerror . | ||
| 58 | .Sh SEE ALSO | ||
| 59 | .Xr intro 2 , | ||
| 60 | .Xr perror 3 , | ||
| 61 | .Xr setlocale 3 | ||
| 62 | .Sh STANDARDS | ||
| 63 | The | ||
| 64 | .Fn strerror | ||
| 65 | function conforms to | ||
| 66 | .St -ansiC . | ||
diff --git a/src/lib/libc/string/strerror.c b/src/lib/libc/string/strerror.c new file mode 100644 index 0000000000..c3f5ab5d98 --- /dev/null +++ b/src/lib/libc/string/strerror.c | |||
| @@ -0,0 +1,56 @@ | |||
| 1 | /* | ||
| 2 | * Copyright (c) 1988 Regents of the University of California. | ||
| 3 | * All rights reserved. | ||
| 4 | * | ||
| 5 | * Redistribution and use in source and binary forms, with or without | ||
| 6 | * modification, are permitted provided that the following conditions | ||
| 7 | * are met: | ||
| 8 | * 1. Redistributions of source code must retain the above copyright | ||
| 9 | * notice, this list of conditions and the following disclaimer. | ||
| 10 | * 2. Redistributions in binary form must reproduce the above copyright | ||
| 11 | * notice, this list of conditions and the following disclaimer in the | ||
| 12 | * documentation and/or other materials provided with the distribution. | ||
| 13 | * 3. All advertising materials mentioning features or use of this software | ||
| 14 | * must display the following acknowledgement: | ||
| 15 | * This product includes software developed by the University of | ||
| 16 | * California, Berkeley and its contributors. | ||
| 17 | * 4. Neither the name of the University nor the names of its contributors | ||
| 18 | * may be used to endorse or promote products derived from this software | ||
| 19 | * without specific prior written permission. | ||
| 20 | * | ||
| 21 | * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND | ||
| 22 | * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE | ||
| 23 | * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE | ||
| 24 | * ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE | ||
| 25 | * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL | ||
| 26 | * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS | ||
| 27 | * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) | ||
| 28 | * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT | ||
| 29 | * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY | ||
| 30 | * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF | ||
| 31 | * SUCH DAMAGE. | ||
| 32 | */ | ||
| 33 | |||
| 34 | #if defined(LIBC_SCCS) && !defined(lint) | ||
| 35 | /*static char *sccsid = "from: @(#)strerror.c 5.6 (Berkeley) 5/4/91";*/ | ||
| 36 | static char *rcsid = "$Id: strerror.c,v 1.1.1.1 1995/10/18 08:42:22 deraadt Exp $"; | ||
| 37 | #endif /* LIBC_SCCS and not lint */ | ||
| 38 | |||
| 39 | #include <string.h> | ||
| 40 | #include <limits.h> | ||
| 41 | |||
| 42 | /* | ||
| 43 | * Since perror() is not allowed to change the contents of strerror()'s | ||
| 44 | * static buffer, both functions supply their own buffers to the | ||
| 45 | * internal function __strerror(). | ||
| 46 | */ | ||
| 47 | |||
| 48 | extern char *__strerror __P((int, char *)); | ||
| 49 | |||
| 50 | char * | ||
| 51 | strerror(num) | ||
| 52 | int num; | ||
| 53 | { | ||
| 54 | static char buf[NL_TEXTMAX]; | ||
| 55 | return __strerror(num, buf); | ||
| 56 | } | ||
diff --git a/src/lib/libc/string/strftime.3 b/src/lib/libc/string/strftime.3 new file mode 100644 index 0000000000..f14db4bb13 --- /dev/null +++ b/src/lib/libc/string/strftime.3 | |||
| @@ -0,0 +1,202 @@ | |||
| 1 | .\" Copyright (c) 1989, 1991 The Regents of the University of California. | ||
| 2 | .\" All rights reserved. | ||
| 3 | .\" | ||
| 4 | .\" This code is derived from software contributed to Berkeley by | ||
| 5 | .\" the American National Standards Committee X3, on Information | ||
| 6 | .\" Processing Systems. | ||
| 7 | .\" | ||
| 8 | .\" Redistribution and use in source and binary forms, with or without | ||
| 9 | .\" modification, are permitted provided that the following conditions | ||
| 10 | .\" are met: | ||
| 11 | .\" 1. Redistributions of source code must retain the above copyright | ||
| 12 | .\" notice, this list of conditions and the following disclaimer. | ||
| 13 | .\" 2. Redistributions in binary form must reproduce the above copyright | ||
| 14 | .\" notice, this list of conditions and the following disclaimer in the | ||
| 15 | .\" documentation and/or other materials provided with the distribution. | ||
| 16 | .\" 3. All advertising materials mentioning features or use of this software | ||
| 17 | .\" must display the following acknowledgement: | ||
| 18 | .\" This product includes software developed by the University of | ||
| 19 | .\" California, Berkeley and its contributors. | ||
| 20 | .\" 4. Neither the name of the University nor the names of its contributors | ||
| 21 | .\" may be used to endorse or promote products derived from this software | ||
| 22 | .\" without specific prior written permission. | ||
| 23 | .\" | ||
| 24 | .\" THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND | ||
| 25 | .\" ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE | ||
| 26 | .\" IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE | ||
| 27 | .\" ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE | ||
| 28 | .\" FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL | ||
| 29 | .\" DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS | ||
| 30 | .\" OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) | ||
| 31 | .\" HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT | ||
| 32 | .\" LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY | ||
| 33 | .\" OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF | ||
| 34 | .\" SUCH DAMAGE. | ||
| 35 | .\" | ||
| 36 | .\" from: @(#)strftime.3 5.12 (Berkeley) 6/29/91 | ||
| 37 | .\" $Id: strftime.3,v 1.1.1.1 1995/10/18 08:42:22 deraadt Exp $ | ||
| 38 | .\" | ||
| 39 | .Dd June 29, 1991 | ||
| 40 | .Dt STRFTIME 3 | ||
| 41 | .Os | ||
| 42 | .Sh NAME | ||
| 43 | .Nm strftime | ||
| 44 | .Nd format date and time | ||
| 45 | .Sh SYNOPSIS | ||
| 46 | .Fd #include <time.h> | ||
| 47 | .Ft size_t | ||
| 48 | .Fn strftime "char *buf" "size_t maxsize" "const char *format" "const struct tm *timeptr" | ||
| 49 | .Sh DESCRIPTION | ||
| 50 | The | ||
| 51 | .Fn strftime | ||
| 52 | function formats the information from | ||
| 53 | .Fa timeptr | ||
| 54 | into the buffer | ||
| 55 | .Fa buf | ||
| 56 | according to the string pointed to by | ||
| 57 | .Fa format . | ||
| 58 | .Pp | ||
| 59 | The | ||
| 60 | .Fa format | ||
| 61 | string consists of zero or more conversion specifications and | ||
| 62 | ordinary characters. | ||
| 63 | All ordinary characters are copied directly into the buffer. | ||
| 64 | A conversion specification consists of a percent sign | ||
| 65 | .Ql % | ||
| 66 | and one other character. | ||
| 67 | .Pp | ||
| 68 | No more than | ||
| 69 | .Fa maxsize | ||
| 70 | characters will be placed into the array. | ||
| 71 | If the total number of resulting characters, including the terminating | ||
| 72 | null character, is not more than | ||
| 73 | .Fa maxsize , | ||
| 74 | .Fn strftime | ||
| 75 | returns the number of characters in the array, not counting the | ||
| 76 | terminating null. | ||
| 77 | Otherwise, zero is returned. | ||
| 78 | .Pp | ||
| 79 | Each conversion specification is replaced by the characters as | ||
| 80 | follows which are then copied into the buffer. | ||
| 81 | .Bl -tag -width "xxxx" | ||
| 82 | .It Cm \&%A | ||
| 83 | is replaced by the locale's full weekday name. | ||
| 84 | .It Cm %a | ||
| 85 | is replaced by the locale's abbreviated weekday name. | ||
| 86 | .It Cm \&%B | ||
| 87 | is replaced by the locale's full month name. | ||
| 88 | .It Cm \&%b No or Cm \&%h | ||
| 89 | is replaced by the locale's abbreviated month name. | ||
| 90 | .It Cm \&%C | ||
| 91 | is replaced by the century (a year divided by 100 and truncated to an integer) | ||
| 92 | as a decimal number (00-99). | ||
| 93 | .It Cm \&%c | ||
| 94 | is replaced by the locale's appropriate date and time representation. | ||
| 95 | .It Cm \&%D | ||
| 96 | is replaced by the date in the format | ||
| 97 | .Dq Li %m/%d/%y . | ||
| 98 | .It Cm \&%d | ||
| 99 | is replaced by the day of the month as a decimal number (01-31). | ||
| 100 | .It Cm \&%e | ||
| 101 | is replaced by the day of month as a decimal number (1-31); | ||
| 102 | single digits are preceded by a blank. | ||
| 103 | .It Cm \&%H | ||
| 104 | is replaced by the hour (24-hour clock) as a decimal number (00-23). | ||
| 105 | .It Cm \&%I | ||
| 106 | is replaced by the hour (12-hour clock) as a decimal number (01-12). | ||
| 107 | .It Cm \&%j | ||
| 108 | is replaced by the day of the year as a decimal number (001-366). | ||
| 109 | .It Cm \&%k | ||
| 110 | is replaced by the hour (24-hour clock) as a decimal number (0-23); | ||
| 111 | single digits are preceded by a blank. | ||
| 112 | .It Cm \&%l | ||
| 113 | is replaced by the hour (12-hour clock) as a decimal number (1-12); | ||
| 114 | single digits are preceded by a blank. | ||
| 115 | .It Cm \&%M | ||
| 116 | is replaced by the minute as a decimal number (00-59). | ||
| 117 | .It Cm %m | ||
| 118 | is replaced by the month as a decimal number (01-12). | ||
| 119 | .It Cm %n | ||
| 120 | is replaced by a newline. | ||
| 121 | .It Cm %p | ||
| 122 | is replaced by the locale's equivalent of either | ||
| 123 | .Dq Tn AM | ||
| 124 | or | ||
| 125 | .Dq Tn PM . | ||
| 126 | .It Cm \&%R | ||
| 127 | is replaced by the time in the format | ||
| 128 | .Dq Li %H:%M . | ||
| 129 | .It Cm \&%r | ||
| 130 | is replaced by the locale's representation of 12-hour clock time | ||
| 131 | using AM/PM notation. | ||
| 132 | .It Cm \&%T | ||
| 133 | is replaced by the time in the format | ||
| 134 | .Dq Li %H:%M:%S . | ||
| 135 | .It Cm \&%t | ||
| 136 | is replaced by a tab. | ||
| 137 | .It Cm \&%S | ||
| 138 | is replaced by the second as a decimal number (00-60). | ||
| 139 | .It Cm %s | ||
| 140 | is replaced by the number of seconds since the Epoch, UCT (see | ||
| 141 | .Xr mktime 3 ) . | ||
| 142 | .It Cm \&%U | ||
| 143 | is replaced by the week number of the year (Sunday as the first day of | ||
| 144 | the week) as a decimal number (00-53). | ||
| 145 | .It Cm \&%u | ||
| 146 | is replaced by the weekday (Monday as the first day of the week) | ||
| 147 | as a decimal number (1-7). | ||
| 148 | .It Cm \&%V | ||
| 149 | is replaced by the week number of the year (Monday as the first day of | ||
| 150 | the week) as a decimal number (01-53). If the week containing January | ||
| 151 | 1 has four or more days in the new year, then it is week 1; otherwise | ||
| 152 | it is week 53 of the previous year, and the next week is week 1. | ||
| 153 | .It Cm \&%W | ||
| 154 | is replaced by the week number of the year (Monday as the first day of | ||
| 155 | the week) as a decimal number (00-53). | ||
| 156 | .It Cm \&%w | ||
| 157 | is replaced by the weekday (Sunday as the first day of the week) | ||
| 158 | as a decimal number (0-6). | ||
| 159 | .It Cm \&%X | ||
| 160 | is replaced by the locale's appropriate date representation. | ||
| 161 | .It Cm \&%x | ||
| 162 | is replaced by the locale's appropriate time representation. | ||
| 163 | .It Cm \&%Y | ||
| 164 | is replaced by the year with century as a decimal number. | ||
| 165 | .It Cm \&%y | ||
| 166 | is replaced by the year without century as a decimal number (00-99). | ||
| 167 | .It Cm \&%Z | ||
| 168 | is replaced by the time zone name. | ||
| 169 | .It Cm %% | ||
| 170 | is replaced by | ||
| 171 | .Ql % . | ||
| 172 | .El | ||
| 173 | .Sh SEE ALSO | ||
| 174 | .Xr date 1 , | ||
| 175 | .Xr ctime 3 , | ||
| 176 | .Xr printf 1 , | ||
| 177 | .Xr printf 3 | ||
| 178 | .Sh STANDARDS | ||
| 179 | The | ||
| 180 | .Fn strftime | ||
| 181 | function | ||
| 182 | conforms to | ||
| 183 | .St -ansiC . | ||
| 184 | The | ||
| 185 | .Ql \&%C , | ||
| 186 | .Ql \&%D , | ||
| 187 | .Ql \&%e , | ||
| 188 | .Ql \&%h , | ||
| 189 | .Ql \&%k , | ||
| 190 | .Ql \&%l , | ||
| 191 | .Ql \&%n , | ||
| 192 | .Ql \&%r , | ||
| 193 | .Ql \&%R , | ||
| 194 | .Ql \&%s . | ||
| 195 | .Ql \&%t , | ||
| 196 | .Ql \&%T , | ||
| 197 | .Ql \&%u , | ||
| 198 | and | ||
| 199 | .Ql \&%V | ||
| 200 | conversion specifications are extensions. | ||
| 201 | .Sh BUGS | ||
| 202 | There is no conversion specification for the phase of the moon. | ||
diff --git a/src/lib/libc/string/strftime.c b/src/lib/libc/string/strftime.c new file mode 100644 index 0000000000..fffa9ecbb0 --- /dev/null +++ b/src/lib/libc/string/strftime.c | |||
| @@ -0,0 +1,317 @@ | |||
| 1 | /* | ||
| 2 | * Copyright (c) 1989 The Regents of the University of California. | ||
| 3 | * All rights reserved. | ||
| 4 | * | ||
| 5 | * Redistribution and use in source and binary forms, with or without | ||
| 6 | * modification, are permitted provided that the following conditions | ||
| 7 | * are met: | ||
| 8 | * 1. Redistributions of source code must retain the above copyright | ||
| 9 | * notice, this list of conditions and the following disclaimer. | ||
| 10 | * 2. Redistributions in binary form must reproduce the above copyright | ||
| 11 | * notice, this list of conditions and the following disclaimer in the | ||
| 12 | * documentation and/or other materials provided with the distribution. | ||
| 13 | * 3. All advertising materials mentioning features or use of this software | ||
| 14 | * must display the following acknowledgement: | ||
| 15 | * This product includes software developed by the University of | ||
| 16 | * California, Berkeley and its contributors. | ||
| 17 | * 4. Neither the name of the University nor the names of its contributors | ||
| 18 | * may be used to endorse or promote products derived from this software | ||
| 19 | * without specific prior written permission. | ||
| 20 | * | ||
| 21 | * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND | ||
| 22 | * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE | ||
| 23 | * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE | ||
| 24 | * ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE | ||
| 25 | * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL | ||
| 26 | * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS | ||
| 27 | * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) | ||
| 28 | * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT | ||
| 29 | * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY | ||
| 30 | * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF | ||
| 31 | * SUCH DAMAGE. | ||
| 32 | */ | ||
| 33 | |||
| 34 | #if defined(LIBC_SCCS) && !defined(lint) | ||
| 35 | /*static char *sccsid = "from: @(#)strftime.c 5.11 (Berkeley) 2/24/91";*/ | ||
| 36 | static char *rcsid = "$Id: strftime.c,v 1.1.1.1 1995/10/18 08:42:22 deraadt Exp $"; | ||
| 37 | #endif /* LIBC_SCCS and not lint */ | ||
| 38 | |||
| 39 | #include <sys/localedef.h> | ||
| 40 | #include <locale.h> | ||
| 41 | #include <string.h> | ||
| 42 | #include <tzfile.h> | ||
| 43 | #include <time.h> | ||
| 44 | |||
| 45 | static size_t gsize; | ||
| 46 | static char *pt; | ||
| 47 | static int _add(), _conv(), _secs(); | ||
| 48 | static size_t _fmt(); | ||
| 49 | |||
| 50 | size_t | ||
| 51 | strftime(s, maxsize, format, t) | ||
| 52 | char *s; | ||
| 53 | size_t maxsize; | ||
| 54 | const char *format; | ||
| 55 | const struct tm *t; | ||
| 56 | { | ||
| 57 | tzset(); | ||
| 58 | |||
| 59 | pt = s; | ||
| 60 | if ((gsize = maxsize) < 1) | ||
| 61 | return(0); | ||
| 62 | if (_fmt(format, t)) { | ||
| 63 | *pt = '\0'; | ||
| 64 | return(maxsize - gsize); | ||
| 65 | } | ||
| 66 | return(0); | ||
| 67 | } | ||
| 68 | |||
| 69 | #define SUN_WEEK(t) (((t)->tm_yday + 7 - \ | ||
| 70 | ((t)->tm_wday)) / 7) | ||
| 71 | #define MON_WEEK(t) (((t)->tm_yday + 7 - \ | ||
| 72 | ((t)->tm_wday ? (t)->tm_wday - 1 : 6)) / 7) | ||
| 73 | static size_t | ||
| 74 | _fmt(format, t) | ||
| 75 | register char *format; | ||
| 76 | struct tm *t; | ||
| 77 | { | ||
| 78 | for (; *format; ++format) { | ||
| 79 | if (*format == '%') { | ||
| 80 | ++format; | ||
| 81 | if (*format == 'E') { | ||
| 82 | /* Alternate Era */ | ||
| 83 | ++format; | ||
| 84 | } else if (*format == 'O') { | ||
| 85 | /* Alternate numeric symbols */ | ||
| 86 | ++format; | ||
| 87 | } | ||
| 88 | switch(*format) { | ||
| 89 | case '\0': | ||
| 90 | --format; | ||
| 91 | break; | ||
| 92 | case 'A': | ||
| 93 | if (t->tm_wday < 0 || t->tm_wday > 6) | ||
| 94 | return(0); | ||
| 95 | if (!_add(_CurrentTimeLocale->day[t->tm_wday])) | ||
| 96 | return(0); | ||
| 97 | continue; | ||
| 98 | case 'a': | ||
| 99 | if (t->tm_wday < 0 || t->tm_wday > 6) | ||
| 100 | return(0); | ||
| 101 | if (!_add(_CurrentTimeLocale->abday[t->tm_wday])) | ||
| 102 | return(0); | ||
| 103 | continue; | ||
| 104 | case 'B': | ||
| 105 | if (t->tm_mon < 0 || t->tm_mon > 11) | ||
| 106 | return(0); | ||
| 107 | if (!_add(_CurrentTimeLocale->mon[t->tm_mon])) | ||
| 108 | return(0); | ||
| 109 | continue; | ||
| 110 | case 'b': | ||
| 111 | case 'h': | ||
| 112 | if (t->tm_mon < 0 || t->tm_mon > 11) | ||
| 113 | return(0); | ||
| 114 | if (!_add(_CurrentTimeLocale->abmon[t->tm_mon])) | ||
| 115 | return(0); | ||
| 116 | continue; | ||
| 117 | case 'C': | ||
| 118 | if (!_conv((t->tm_year + TM_YEAR_BASE) / 100, | ||
| 119 | 2, '0')) | ||
| 120 | return(0); | ||
| 121 | continue; | ||
| 122 | case 'c': | ||
| 123 | if (!_fmt(_CurrentTimeLocale->d_t_fmt, t)) | ||
| 124 | return(0); | ||
| 125 | continue; | ||
| 126 | case 'D': | ||
| 127 | if (!_fmt("%m/%d/%y", t)) | ||
| 128 | return(0); | ||
| 129 | continue; | ||
| 130 | case 'd': | ||
| 131 | if (!_conv(t->tm_mday, 2, '0')) | ||
| 132 | return(0); | ||
| 133 | continue; | ||
| 134 | case 'e': | ||
| 135 | if (!_conv(t->tm_mday, 2, ' ')) | ||
| 136 | return(0); | ||
| 137 | continue; | ||
| 138 | case 'H': | ||
| 139 | if (!_conv(t->tm_hour, 2, '0')) | ||
| 140 | return(0); | ||
| 141 | continue; | ||
| 142 | case 'I': | ||
| 143 | if (!_conv(t->tm_hour % 12 ? | ||
| 144 | t->tm_hour % 12 : 12, 2, '0')) | ||
| 145 | return(0); | ||
| 146 | continue; | ||
| 147 | case 'j': | ||
| 148 | if (!_conv(t->tm_yday + 1, 3, '0')) | ||
| 149 | return(0); | ||
| 150 | continue; | ||
| 151 | case 'k': | ||
| 152 | if (!_conv(t->tm_hour, 2, ' ')) | ||
| 153 | return(0); | ||
| 154 | continue; | ||
| 155 | case 'l': | ||
| 156 | if (!_conv(t->tm_hour % 12 ? | ||
| 157 | t->tm_hour % 12: 12, 2, ' ')) | ||
| 158 | return(0); | ||
| 159 | continue; | ||
| 160 | case 'M': | ||
| 161 | if (!_conv(t->tm_min, 2, '0')) | ||
| 162 | return(0); | ||
| 163 | continue; | ||
| 164 | case 'm': | ||
| 165 | if (!_conv(t->tm_mon + 1, 2, '0')) | ||
| 166 | return(0); | ||
| 167 | continue; | ||
| 168 | case 'n': | ||
| 169 | if (!_add("\n")) | ||
| 170 | return(0); | ||
| 171 | continue; | ||
| 172 | case 'p': | ||
| 173 | if (!_add(_CurrentTimeLocale->am_pm[t->tm_hour >= 12])) | ||
| 174 | return(0); | ||
| 175 | continue; | ||
| 176 | case 'R': | ||
| 177 | if (!_fmt("%H:%M", t)) | ||
| 178 | return(0); | ||
| 179 | continue; | ||
| 180 | case 'r': | ||
| 181 | if (!_fmt(_CurrentTimeLocale->t_fmt_ampm, t)) | ||
| 182 | return(0); | ||
| 183 | continue; | ||
| 184 | case 'S': | ||
| 185 | if (!_conv(t->tm_sec, 2, '0')) | ||
| 186 | return(0); | ||
| 187 | continue; | ||
| 188 | case 's': | ||
| 189 | if (!_secs(t)) | ||
| 190 | return(0); | ||
| 191 | continue; | ||
| 192 | case 'T': | ||
| 193 | if (!_fmt("%H:%M:%S", t)) | ||
| 194 | return(0); | ||
| 195 | continue; | ||
| 196 | case 't': | ||
| 197 | if (!_add("\t")) | ||
| 198 | return(0); | ||
| 199 | continue; | ||
| 200 | case 'U': | ||
| 201 | if (!_conv(SUN_WEEK(t), 2, '0')) | ||
| 202 | return(0); | ||
| 203 | continue; | ||
| 204 | case 'u': | ||
| 205 | if (!_conv(t->tm_wday ? t->tm_wday : 7, 2, '0')) | ||
| 206 | return(0); | ||
| 207 | continue; | ||
| 208 | case 'V': | ||
| 209 | { | ||
| 210 | /* ISO 8601 Week Of Year: | ||
| 211 | If the week (Monday - Sunday) containing | ||
| 212 | January 1 has four or more days in the new | ||
| 213 | year, then it is week 1; otherwise it is | ||
| 214 | week 53 of the previous year and the next | ||
| 215 | week is week one. */ | ||
| 216 | |||
| 217 | int week = MON_WEEK(t); | ||
| 218 | |||
| 219 | if (((t->tm_yday + 7 - (t->tm_wday + 1)) % 7) >= 4) { | ||
| 220 | week++; | ||
| 221 | } else if (week == 0) { | ||
| 222 | week = 53; | ||
| 223 | } | ||
| 224 | |||
| 225 | if (!_conv(week, 2, '0')) | ||
| 226 | return(0); | ||
| 227 | continue; | ||
| 228 | } | ||
| 229 | case 'W': | ||
| 230 | if (!_conv(MON_WEEK(t), 2, '0')) | ||
| 231 | return(0); | ||
| 232 | continue; | ||
| 233 | case 'w': | ||
| 234 | if (!_conv(t->tm_wday, 1, '0')) | ||
| 235 | return(0); | ||
| 236 | continue; | ||
| 237 | case 'x': | ||
| 238 | if (!_fmt(_CurrentTimeLocale->d_fmt, t)) | ||
| 239 | return(0); | ||
| 240 | continue; | ||
| 241 | case 'X': | ||
| 242 | if (!_fmt(_CurrentTimeLocale->t_fmt, t)) | ||
| 243 | return(0); | ||
| 244 | continue; | ||
| 245 | case 'y': | ||
| 246 | if (!_conv((t->tm_year + TM_YEAR_BASE) % 100, | ||
| 247 | 2, '0')) | ||
| 248 | return(0); | ||
| 249 | continue; | ||
| 250 | case 'Y': | ||
| 251 | if (!_conv((t->tm_year + TM_YEAR_BASE), 4, '0')) | ||
| 252 | return(0); | ||
| 253 | continue; | ||
| 254 | case 'Z': | ||
| 255 | if (t->tm_zone && !_add(t->tm_zone)) | ||
| 256 | return(0); | ||
| 257 | continue; | ||
| 258 | case '%': | ||
| 259 | /* | ||
| 260 | * X311J/88-090 (4.12.3.5): if conversion char is | ||
| 261 | * undefined, behavior is undefined. Print out the | ||
| 262 | * character itself as printf(3) does. | ||
| 263 | */ | ||
| 264 | default: | ||
| 265 | break; | ||
| 266 | } | ||
| 267 | } | ||
| 268 | if (!gsize--) | ||
| 269 | return(0); | ||
| 270 | *pt++ = *format; | ||
| 271 | } | ||
| 272 | return(gsize); | ||
| 273 | } | ||
| 274 | |||
| 275 | static | ||
| 276 | _secs(t) | ||
| 277 | struct tm *t; | ||
| 278 | { | ||
| 279 | static char buf[15]; | ||
| 280 | register time_t s; | ||
| 281 | register char *p; | ||
| 282 | struct tm tmp; | ||
| 283 | |||
| 284 | /* Make a copy, mktime(3) modifies the tm struct. */ | ||
| 285 | tmp = *t; | ||
| 286 | s = mktime(&tmp); | ||
| 287 | for (p = buf + sizeof(buf) - 2; s > 0 && p > buf; s /= 10) | ||
| 288 | *p-- = s % 10 + '0'; | ||
| 289 | return(_add(++p)); | ||
| 290 | } | ||
| 291 | |||
| 292 | static | ||
| 293 | _conv(n, digits, pad) | ||
| 294 | int n, digits; | ||
| 295 | char pad; | ||
| 296 | { | ||
| 297 | static char buf[10]; | ||
| 298 | register char *p; | ||
| 299 | |||
| 300 | for (p = buf + sizeof(buf) - 2; n > 0 && p > buf; n /= 10, --digits) | ||
| 301 | *p-- = n % 10 + '0'; | ||
| 302 | while (p > buf && digits-- > 0) | ||
| 303 | *p-- = pad; | ||
| 304 | return(_add(++p)); | ||
| 305 | } | ||
| 306 | |||
| 307 | static | ||
| 308 | _add(str) | ||
| 309 | register char *str; | ||
| 310 | { | ||
| 311 | for (;; ++pt, --gsize) { | ||
| 312 | if (!gsize) | ||
| 313 | return(0); | ||
| 314 | if (!(*pt = *str++)) | ||
| 315 | return(1); | ||
| 316 | } | ||
| 317 | } | ||
diff --git a/src/lib/libc/string/string.3 b/src/lib/libc/string/string.3 new file mode 100644 index 0000000000..aaf97e0321 --- /dev/null +++ b/src/lib/libc/string/string.3 | |||
| @@ -0,0 +1,156 @@ | |||
| 1 | .\" Copyright (c) 1990, 1991 The Regents of the University of California. | ||
| 2 | .\" All rights reserved. | ||
| 3 | .\" | ||
| 4 | .\" This code is derived from software contributed to Berkeley by | ||
| 5 | .\" Chris Torek. | ||
| 6 | .\" Redistribution and use in source and binary forms, with or without | ||
| 7 | .\" modification, are permitted provided that the following conditions | ||
| 8 | .\" are met: | ||
| 9 | .\" 1. Redistributions of source code must retain the above copyright | ||
| 10 | .\" notice, this list of conditions and the following disclaimer. | ||
| 11 | .\" 2. Redistributions in binary form must reproduce the above copyright | ||
| 12 | .\" notice, this list of conditions and the following disclaimer in the | ||
| 13 | .\" documentation and/or other materials provided with the distribution. | ||
| 14 | .\" 3. All advertising materials mentioning features or use of this software | ||
| 15 | .\" must display the following acknowledgement: | ||
| 16 | .\" This product includes software developed by the University of | ||
| 17 | .\" California, Berkeley and its contributors. | ||
| 18 | .\" 4. Neither the name of the University nor the names of its contributors | ||
| 19 | .\" may be used to endorse or promote products derived from this software | ||
| 20 | .\" without specific prior written permission. | ||
| 21 | .\" | ||
| 22 | .\" THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND | ||
| 23 | .\" ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE | ||
| 24 | .\" IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE | ||
| 25 | .\" ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE | ||
| 26 | .\" FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL | ||
| 27 | .\" DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS | ||
| 28 | .\" OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) | ||
| 29 | .\" HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT | ||
| 30 | .\" LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY | ||
| 31 | .\" OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF | ||
| 32 | .\" SUCH DAMAGE. | ||
| 33 | .\" | ||
| 34 | .\" from: @(#)string.3 6.9 (Berkeley) 4/19/91 | ||
| 35 | .\" $Id: string.3,v 1.1.1.1 1995/10/18 08:42:22 deraadt Exp $ | ||
| 36 | .\" | ||
| 37 | .Dd April 19, 1991 | ||
| 38 | .Dt STRING 3 | ||
| 39 | .Os BSD 4 | ||
| 40 | .Sh NAME | ||
| 41 | .Nm strcat , | ||
| 42 | .Nm strncat , | ||
| 43 | .Nm strchr , | ||
| 44 | .Nm strrchr , | ||
| 45 | .Nm strcmp , | ||
| 46 | .Nm strncmp , | ||
| 47 | .Nm strcasecmp, | ||
| 48 | .Nm strncasecmp , | ||
| 49 | .Nm strcpy , | ||
| 50 | .Nm strncpy , | ||
| 51 | .Nm strerror , | ||
| 52 | .Nm strlen , | ||
| 53 | .Nm strpbrk , | ||
| 54 | .Nm strsep, | ||
| 55 | .Nm strspn , | ||
| 56 | .Nm strcspn , | ||
| 57 | .Nm strstr , | ||
| 58 | .Nm strtok , | ||
| 59 | .Nm index , | ||
| 60 | .Nm rindex | ||
| 61 | .Nd string specific functions | ||
| 62 | .Sh SYNOPSIS | ||
| 63 | .Fd #include <string.h> | ||
| 64 | .Ft char * | ||
| 65 | .Fn strcat "char *s" "const char * append" | ||
| 66 | .Ft char * | ||
| 67 | .Fn strncat "char *s" "const char *append" "size_t count" | ||
| 68 | .Ft char * | ||
| 69 | .Fn strchr "const char *s" "int c" | ||
| 70 | .Ft char * | ||
| 71 | .Fn strrchr "const char *s" "int c" | ||
| 72 | .Ft int | ||
| 73 | .Fn strcmp "const char *s1" "const char *s2" | ||
| 74 | .Ft int | ||
| 75 | .Fn strncmp "const char *s1" "const char *s2" "size_t count" | ||
| 76 | .Ft int | ||
| 77 | .Fn strcasecmp "const char *s1" "const char *s2" | ||
| 78 | .Ft int | ||
| 79 | .Fn strncasecmp "const char *s1" "const char *s2" "size_t count" | ||
| 80 | .Ft char * | ||
| 81 | .Fn strcpy "char *dst" "const char *src" | ||
| 82 | .Ft char * | ||
| 83 | .Fn strncpy "char *dst" "const char *src" "size_t count" | ||
| 84 | .Ft char * | ||
| 85 | .Fn strerror "int errno" | ||
| 86 | .Ft size_t | ||
| 87 | .Fn strlen "const char *s" | ||
| 88 | .Ft char * | ||
| 89 | .Fn strpbrk "const char *s" "const char *charset" | ||
| 90 | .Ft char * | ||
| 91 | .Fn strsep "char **stringp" "const char *delim" | ||
| 92 | .Ft size_t | ||
| 93 | .Fn strspn "const char *s" "const char *charset" | ||
| 94 | .Ft size_t | ||
| 95 | .Fn strcspn "const char *s" "const char *charset" | ||
| 96 | .Ft char * | ||
| 97 | .Fn strstr "const char *big" "const char *little" | ||
| 98 | .Ft char * | ||
| 99 | .Fn strtok "char *s" "const char *delim" | ||
| 100 | .Ft char * | ||
| 101 | .Fn index "const char *s" "int c" | ||
| 102 | .Ft char * | ||
| 103 | .Fn rindex "const char *s" "int c" | ||
| 104 | .Sh DESCRIPTION | ||
| 105 | The string functions | ||
| 106 | functions manipulate strings terminated by a | ||
| 107 | null byte. | ||
| 108 | .Pp | ||
| 109 | See the specific manual pages for more information. | ||
| 110 | For manipulating variable length generic objects as byte | ||
| 111 | strings (without the null byte check), see | ||
| 112 | .Xr bstring 3 . | ||
| 113 | .Pp | ||
| 114 | Except as noted in their specific manual pages, | ||
| 115 | the string functions do not test the destination | ||
| 116 | for size limitations. | ||
| 117 | .Sh SEE ALSO | ||
| 118 | .Xr index 3 , | ||
| 119 | .Xr strcat 3 , | ||
| 120 | .Xr strchr 3 , | ||
| 121 | .Xr strrchr 3 , | ||
| 122 | .Xr strcmp 3 , | ||
| 123 | .Xr strcasecmp 3 , | ||
| 124 | .Xr strcpy 3 , | ||
| 125 | .Xr strerror 3 , | ||
| 126 | .Xr strlen 3 , | ||
| 127 | .Xr strpbrk 3 , | ||
| 128 | .Xr strsep 3 , | ||
| 129 | .Xr strspn 3 , | ||
| 130 | .Xr strcspn 3 , | ||
| 131 | .Xr strstr 3 , | ||
| 132 | .Xr strtok 3 , | ||
| 133 | .Xr rindex 3 | ||
| 134 | .Xr bstring 3 | ||
| 135 | .Sh STANDARDS | ||
| 136 | The | ||
| 137 | .Fn strcat , | ||
| 138 | .Fn strncat , | ||
| 139 | .Fn strchr , | ||
| 140 | .Fn strrchr , | ||
| 141 | .Fn strcmp , | ||
| 142 | .Fn strncmp , | ||
| 143 | .Fn strcpy , | ||
| 144 | .Fn strncpy , | ||
| 145 | .Fn strerror , | ||
| 146 | .Fn strlen , | ||
| 147 | .Fn strpbrk , | ||
| 148 | .Fn strsep , | ||
| 149 | .Fn strspn , | ||
| 150 | .Fn strcspn , | ||
| 151 | .Fn strstr , | ||
| 152 | and | ||
| 153 | .Fn strtok | ||
| 154 | functions | ||
| 155 | conform to | ||
| 156 | .St -ansiC . | ||
diff --git a/src/lib/libc/string/strlen.3 b/src/lib/libc/string/strlen.3 new file mode 100644 index 0000000000..f4aff363ee --- /dev/null +++ b/src/lib/libc/string/strlen.3 | |||
| @@ -0,0 +1,71 @@ | |||
| 1 | .\" Copyright (c) 1990, 1991 The Regents of the University of California. | ||
| 2 | .\" All rights reserved. | ||
| 3 | .\" | ||
| 4 | .\" This code is derived from software contributed to Berkeley by | ||
| 5 | .\" Chris Torek and the American National Standards Committee X3, | ||
| 6 | .\" on Information Processing Systems. | ||
| 7 | .\" | ||
| 8 | .\" Redistribution and use in source and binary forms, with or without | ||
| 9 | .\" modification, are permitted provided that the following conditions | ||
| 10 | .\" are met: | ||
| 11 | .\" 1. Redistributions of source code must retain the above copyright | ||
| 12 | .\" notice, this list of conditions and the following disclaimer. | ||
| 13 | .\" 2. Redistributions in binary form must reproduce the above copyright | ||
| 14 | .\" notice, this list of conditions and the following disclaimer in the | ||
| 15 | .\" documentation and/or other materials provided with the distribution. | ||
| 16 | .\" 3. All advertising materials mentioning features or use of this software | ||
| 17 | .\" must display the following acknowledgement: | ||
| 18 | .\" This product includes software developed by the University of | ||
| 19 | .\" California, Berkeley and its contributors. | ||
| 20 | .\" 4. Neither the name of the University nor the names of its contributors | ||
| 21 | .\" may be used to endorse or promote products derived from this software | ||
| 22 | .\" without specific prior written permission. | ||
| 23 | .\" | ||
| 24 | .\" THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND | ||
| 25 | .\" ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE | ||
| 26 | .\" IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE | ||
| 27 | .\" ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE | ||
| 28 | .\" FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL | ||
| 29 | .\" DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS | ||
| 30 | .\" OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) | ||
| 31 | .\" HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT | ||
| 32 | .\" LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY | ||
| 33 | .\" OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF | ||
| 34 | .\" SUCH DAMAGE. | ||
| 35 | .\" | ||
| 36 | .\" from: @(#)strlen.3 5.4 (Berkeley) 6/29/91 | ||
| 37 | .\" $Id: strlen.3,v 1.1.1.1 1995/10/18 08:42:22 deraadt Exp $ | ||
| 38 | .\" | ||
| 39 | .Dd June 29, 1991 | ||
| 40 | .Dt STRLEN 3 | ||
| 41 | .Os | ||
| 42 | .Sh NAME | ||
| 43 | .Nm strlen | ||
| 44 | .Nd find length of string | ||
| 45 | .Sh SYNOPSIS | ||
| 46 | .Fd #include <string.h> | ||
| 47 | .Ft size_t | ||
| 48 | .Fn strlen "const char *s" | ||
| 49 | .Sh DESCRIPTION | ||
| 50 | The | ||
| 51 | .Fn strlen | ||
| 52 | function | ||
| 53 | computes the length of the string | ||
| 54 | .Fa s . | ||
| 55 | .Sh RETURN VALUES | ||
| 56 | The | ||
| 57 | .Fn strlen | ||
| 58 | function | ||
| 59 | returns | ||
| 60 | the number of characters that precede the | ||
| 61 | terminating | ||
| 62 | .Dv NUL | ||
| 63 | character. | ||
| 64 | .Sh SEE ALSO | ||
| 65 | .Xr string 3 | ||
| 66 | .Sh STANDARDS | ||
| 67 | The | ||
| 68 | .Fn strlen | ||
| 69 | function | ||
| 70 | conforms to | ||
| 71 | .St -ansiC . | ||
diff --git a/src/lib/libc/string/strlen.c b/src/lib/libc/string/strlen.c new file mode 100644 index 0000000000..d23aadafc0 --- /dev/null +++ b/src/lib/libc/string/strlen.c | |||
| @@ -0,0 +1,50 @@ | |||
| 1 | /*- | ||
| 2 | * Copyright (c) 1990 The Regents of the University of California. | ||
| 3 | * All rights reserved. | ||
| 4 | * | ||
| 5 | * Redistribution and use in source and binary forms, with or without | ||
| 6 | * modification, are permitted provided that the following conditions | ||
| 7 | * are met: | ||
| 8 | * 1. Redistributions of source code must retain the above copyright | ||
| 9 | * notice, this list of conditions and the following disclaimer. | ||
| 10 | * 2. Redistributions in binary form must reproduce the above copyright | ||
| 11 | * notice, this list of conditions and the following disclaimer in the | ||
| 12 | * documentation and/or other materials provided with the distribution. | ||
| 13 | * 3. All advertising materials mentioning features or use of this software | ||
| 14 | * must display the following acknowledgement: | ||
| 15 | * This product includes software developed by the University of | ||
| 16 | * California, Berkeley and its contributors. | ||
| 17 | * 4. Neither the name of the University nor the names of its contributors | ||
| 18 | * may be used to endorse or promote products derived from this software | ||
| 19 | * without specific prior written permission. | ||
| 20 | * | ||
| 21 | * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND | ||
| 22 | * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE | ||
| 23 | * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE | ||
| 24 | * ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE | ||
| 25 | * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL | ||
| 26 | * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS | ||
| 27 | * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) | ||
| 28 | * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT | ||
| 29 | * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY | ||
| 30 | * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF | ||
| 31 | * SUCH DAMAGE. | ||
| 32 | */ | ||
| 33 | |||
| 34 | #if defined(LIBC_SCCS) && !defined(lint) | ||
| 35 | /*static char *sccsid = "from: @(#)strlen.c 5.5 (Berkeley) 1/26/91";*/ | ||
| 36 | static char *rcsid = "$Id: strlen.c,v 1.1.1.1 1995/10/18 08:42:22 deraadt Exp $"; | ||
| 37 | #endif /* LIBC_SCCS and not lint */ | ||
| 38 | |||
| 39 | #include <string.h> | ||
| 40 | |||
| 41 | size_t | ||
| 42 | strlen(str) | ||
| 43 | const char *str; | ||
| 44 | { | ||
| 45 | register const char *s; | ||
| 46 | |||
| 47 | for (s = str; *s; ++s); | ||
| 48 | return(s - str); | ||
| 49 | } | ||
| 50 | |||
diff --git a/src/lib/libc/string/strmode.3 b/src/lib/libc/string/strmode.3 new file mode 100644 index 0000000000..1907e7ab03 --- /dev/null +++ b/src/lib/libc/string/strmode.3 | |||
| @@ -0,0 +1,149 @@ | |||
| 1 | .\" Copyright (c) 1990, 1991 The Regents of the University of California. | ||
| 2 | .\" All rights reserved. | ||
| 3 | .\" | ||
| 4 | .\" Redistribution and use in source and binary forms, with or without | ||
| 5 | .\" modification, are permitted provided that the following conditions | ||
| 6 | .\" are met: | ||
| 7 | .\" 1. Redistributions of source code must retain the above copyright | ||
| 8 | .\" notice, this list of conditions and the following disclaimer. | ||
| 9 | .\" 2. Redistributions in binary form must reproduce the above copyright | ||
| 10 | .\" notice, this list of conditions and the following disclaimer in the | ||
| 11 | .\" documentation and/or other materials provided with the distribution. | ||
| 12 | .\" 3. All advertising materials mentioning features or use of this software | ||
| 13 | .\" must display the following acknowledgement: | ||
| 14 | .\" This product includes software developed by the University of | ||
| 15 | .\" California, Berkeley and its contributors. | ||
| 16 | .\" 4. Neither the name of the University nor the names of its contributors | ||
| 17 | .\" may be used to endorse or promote products derived from this software | ||
| 18 | .\" without specific prior written permission. | ||
| 19 | .\" | ||
| 20 | .\" THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND | ||
| 21 | .\" ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE | ||
| 22 | .\" IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE | ||
| 23 | .\" ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE | ||
| 24 | .\" FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL | ||
| 25 | .\" DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS | ||
| 26 | .\" OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) | ||
| 27 | .\" HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT | ||
| 28 | .\" LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY | ||
| 29 | .\" OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF | ||
| 30 | .\" SUCH DAMAGE. | ||
| 31 | .\" | ||
| 32 | .\" from: @(#)strmode.3 5.4 (Berkeley) 7/31/91 | ||
| 33 | .\" $Id: strmode.3,v 1.1.1.1 1995/10/18 08:42:22 deraadt Exp $ | ||
| 34 | .\" | ||
| 35 | .Dd July 31, 1991 | ||
| 36 | .Dt STRMODE 3 | ||
| 37 | .Os | ||
| 38 | .Sh NAME | ||
| 39 | .Nm strmode | ||
| 40 | .Nd convert inode status information into a symbolic string | ||
| 41 | .Sh SYNOPSIS | ||
| 42 | .Fd #include <string.h> | ||
| 43 | .Ft void | ||
| 44 | .Fn strmode "mode_t mode" "char *bp" | ||
| 45 | .Sh DESCRIPTION | ||
| 46 | The | ||
| 47 | .Fn strmode | ||
| 48 | function | ||
| 49 | converts a file | ||
| 50 | .Fa mode | ||
| 51 | (the type and permission information associated with an inode, see | ||
| 52 | .Xr stat 2 ) | ||
| 53 | into a symbolic string which is stored in the location referenced by | ||
| 54 | .Fa bp . | ||
| 55 | This stored string is eleven characters in length plus a trailing | ||
| 56 | .Dv NULL . | ||
| 57 | .Pp | ||
| 58 | The first character is the inode type, and will be one of the following: | ||
| 59 | .Pp | ||
| 60 | .Bl -tag -width flag -offset indent -compact | ||
| 61 | .It \- | ||
| 62 | regular file | ||
| 63 | .It b | ||
| 64 | block special | ||
| 65 | .It c | ||
| 66 | character special | ||
| 67 | .It d | ||
| 68 | directory | ||
| 69 | .It l | ||
| 70 | symbolic link | ||
| 71 | .It p | ||
| 72 | fifo | ||
| 73 | .It s | ||
| 74 | socket | ||
| 75 | .It ? | ||
| 76 | unknown inode type | ||
| 77 | .El | ||
| 78 | .Pp | ||
| 79 | The next nine characters encode three sets of permissions, in three | ||
| 80 | characters each. | ||
| 81 | The first three characters are the permissions for the owner of the | ||
| 82 | file, the second three for the group the file belongs to, and the | ||
| 83 | third for the ``other'', or default, set of users. | ||
| 84 | .Pp | ||
| 85 | Permission checking is done as specifically as possible. | ||
| 86 | If read permission is denied to the owner of a file in the first set | ||
| 87 | of permissions, the owner of the file will not be able to read the file. | ||
| 88 | This is true even if the owner is in the file's group and the group | ||
| 89 | permissions allow reading or the ``other'' permissions allow reading. | ||
| 90 | .Pp | ||
| 91 | If the first character of the three character set is an ``r'', the file is | ||
| 92 | readable for that set of users; if a dash ``\-'', it is not readable. | ||
| 93 | .Pp | ||
| 94 | If the second character of the three character set is a ``w'', the file is | ||
| 95 | writable for that set of users; if a dash ``\-'', it is not writable. | ||
| 96 | .Pp | ||
| 97 | The third character is the first of the following characters that apply: | ||
| 98 | .Bl -tag -width xxxx | ||
| 99 | .It S | ||
| 100 | If the character is part of the owner permissions and the file is not | ||
| 101 | executable or the directory is not searchable, by the owner, and the | ||
| 102 | set-user-id bit is set. | ||
| 103 | .It S | ||
| 104 | If the character is part of the group permissions and the file is not | ||
| 105 | executable or the directory is not searchable, by the group, and the | ||
| 106 | set-group-id bit is set. | ||
| 107 | .It T | ||
| 108 | If the character is part of the other permissions and the file is not | ||
| 109 | executable or the directory is not searchable, by others, and the ``sticky'' | ||
| 110 | .Pq Dv S_ISVTX | ||
| 111 | bit is set. | ||
| 112 | .It s | ||
| 113 | If the character is part of the owner permissions and the file is | ||
| 114 | executable or the directory searchable, by the owner, and the set-user-id | ||
| 115 | bit is set. | ||
| 116 | .It s | ||
| 117 | If the character is part of the group permissions and the file is | ||
| 118 | executable or the directory searchable, by the group, and the set-group-id | ||
| 119 | bit is set. | ||
| 120 | .It t | ||
| 121 | If the character is part of the other permissions and the file is | ||
| 122 | executable or the directory searchable, by others, and the ``sticky'' | ||
| 123 | .Pq Dv S_ISVTX | ||
| 124 | bit is set. | ||
| 125 | .It x | ||
| 126 | The file is executable or the directory is searchable. | ||
| 127 | .It \- | ||
| 128 | None of the above apply. | ||
| 129 | .El | ||
| 130 | .Pp | ||
| 131 | The last character is a plus sign ``+'' if any there are any alternate | ||
| 132 | or additional access control methods associated with the inode, otherwise | ||
| 133 | it will be a space. | ||
| 134 | .Sh RETURN VALUES | ||
| 135 | The | ||
| 136 | .Fn strmode | ||
| 137 | function | ||
| 138 | always returns 0. | ||
| 139 | .Sh SEE ALSO | ||
| 140 | .Xr chmod 1 , | ||
| 141 | .Xr find 1 , | ||
| 142 | .Xr stat 2 , | ||
| 143 | .Xr getmode 3 , | ||
| 144 | .Xr setmode 3 | ||
| 145 | .Sh HISTORY | ||
| 146 | The | ||
| 147 | .Fn strmode | ||
| 148 | function | ||
| 149 | .Ud . | ||
diff --git a/src/lib/libc/string/strmode.c b/src/lib/libc/string/strmode.c new file mode 100644 index 0000000000..441fc76e90 --- /dev/null +++ b/src/lib/libc/string/strmode.c | |||
| @@ -0,0 +1,148 @@ | |||
| 1 | /*- | ||
| 2 | * Copyright (c) 1990 The Regents of the University of California. | ||
| 3 | * All rights reserved. | ||
| 4 | * | ||
| 5 | * Redistribution and use in source and binary forms, with or without | ||
| 6 | * modification, are permitted provided that the following conditions | ||
| 7 | * are met: | ||
| 8 | * 1. Redistributions of source code must retain the above copyright | ||
| 9 | * notice, this list of conditions and the following disclaimer. | ||
| 10 | * 2. Redistributions in binary form must reproduce the above copyright | ||
| 11 | * notice, this list of conditions and the following disclaimer in the | ||
| 12 | * documentation and/or other materials provided with the distribution. | ||
| 13 | * 3. All advertising materials mentioning features or use of this software | ||
| 14 | * must display the following acknowledgement: | ||
| 15 | * This product includes software developed by the University of | ||
| 16 | * California, Berkeley and its contributors. | ||
| 17 | * 4. Neither the name of the University nor the names of its contributors | ||
| 18 | * may be used to endorse or promote products derived from this software | ||
| 19 | * without specific prior written permission. | ||
| 20 | * | ||
| 21 | * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND | ||
| 22 | * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE | ||
| 23 | * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE | ||
| 24 | * ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE | ||
| 25 | * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL | ||
| 26 | * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS | ||
| 27 | * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) | ||
| 28 | * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT | ||
| 29 | * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY | ||
| 30 | * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF | ||
| 31 | * SUCH DAMAGE. | ||
| 32 | */ | ||
| 33 | |||
| 34 | #if defined(LIBC_SCCS) && !defined(lint) | ||
| 35 | /*static char *sccsid = "from: @(#)strmode.c 5.3 (Berkeley) 5/18/90";*/ | ||
| 36 | static char *rcsid = "$Id: strmode.c,v 1.1.1.1 1995/10/18 08:42:22 deraadt Exp $"; | ||
| 37 | #endif /* LIBC_SCCS and not lint */ | ||
| 38 | |||
| 39 | #include <sys/types.h> | ||
| 40 | #include <sys/stat.h> | ||
| 41 | #include <string.h> | ||
| 42 | |||
| 43 | void | ||
| 44 | strmode(mode, p) | ||
| 45 | register mode_t mode; | ||
| 46 | register char *p; | ||
| 47 | { | ||
| 48 | /* print type */ | ||
| 49 | switch (mode & S_IFMT) { | ||
| 50 | case S_IFDIR: /* directory */ | ||
| 51 | *p++ = 'd'; | ||
| 52 | break; | ||
| 53 | case S_IFCHR: /* character special */ | ||
| 54 | *p++ = 'c'; | ||
| 55 | break; | ||
| 56 | case S_IFBLK: /* block special */ | ||
| 57 | *p++ = 'b'; | ||
| 58 | break; | ||
| 59 | case S_IFREG: /* regular */ | ||
| 60 | *p++ = '-'; | ||
| 61 | break; | ||
| 62 | case S_IFLNK: /* symbolic link */ | ||
| 63 | *p++ = 'l'; | ||
| 64 | break; | ||
| 65 | case S_IFSOCK: /* socket */ | ||
| 66 | *p++ = 's'; | ||
| 67 | break; | ||
| 68 | #ifdef S_IFIFO | ||
| 69 | case S_IFIFO: /* fifo */ | ||
| 70 | *p++ = 'p'; | ||
| 71 | break; | ||
| 72 | #endif | ||
| 73 | default: /* unknown */ | ||
| 74 | *p++ = '?'; | ||
| 75 | break; | ||
| 76 | } | ||
| 77 | /* usr */ | ||
| 78 | if (mode & S_IRUSR) | ||
| 79 | *p++ = 'r'; | ||
| 80 | else | ||
| 81 | *p++ = '-'; | ||
| 82 | if (mode & S_IWUSR) | ||
| 83 | *p++ = 'w'; | ||
| 84 | else | ||
| 85 | *p++ = '-'; | ||
| 86 | switch (mode & (S_IXUSR | S_ISUID)) { | ||
| 87 | case 0: | ||
| 88 | *p++ = '-'; | ||
| 89 | break; | ||
| 90 | case S_IXUSR: | ||
| 91 | *p++ = 'x'; | ||
| 92 | break; | ||
| 93 | case S_ISUID: | ||
| 94 | *p++ = 'S'; | ||
| 95 | break; | ||
| 96 | case S_IXUSR | S_ISUID: | ||
| 97 | *p++ = 's'; | ||
| 98 | break; | ||
| 99 | } | ||
| 100 | /* group */ | ||
| 101 | if (mode & S_IRGRP) | ||
| 102 | *p++ = 'r'; | ||
| 103 | else | ||
| 104 | *p++ = '-'; | ||
| 105 | if (mode & S_IWGRP) | ||
| 106 | *p++ = 'w'; | ||
| 107 | else | ||
| 108 | *p++ = '-'; | ||
| 109 | switch (mode & (S_IXGRP | S_ISGID)) { | ||
| 110 | case 0: | ||
| 111 | *p++ = '-'; | ||
| 112 | break; | ||
| 113 | case S_IXGRP: | ||
| 114 | *p++ = 'x'; | ||
| 115 | break; | ||
| 116 | case S_ISGID: | ||
| 117 | *p++ = 'S'; | ||
| 118 | break; | ||
| 119 | case S_IXGRP | S_ISGID: | ||
| 120 | *p++ = 's'; | ||
| 121 | break; | ||
| 122 | } | ||
| 123 | /* other */ | ||
| 124 | if (mode & S_IROTH) | ||
| 125 | *p++ = 'r'; | ||
| 126 | else | ||
| 127 | *p++ = '-'; | ||
| 128 | if (mode & S_IWOTH) | ||
| 129 | *p++ = 'w'; | ||
| 130 | else | ||
| 131 | *p++ = '-'; | ||
| 132 | switch (mode & (S_IXOTH | S_ISVTX)) { | ||
| 133 | case 0: | ||
| 134 | *p++ = '-'; | ||
| 135 | break; | ||
| 136 | case S_IXOTH: | ||
| 137 | *p++ = 'x'; | ||
| 138 | break; | ||
| 139 | case S_ISVTX: | ||
| 140 | *p++ = 'T'; | ||
| 141 | break; | ||
| 142 | case S_IXOTH | S_ISVTX: | ||
| 143 | *p++ = 't'; | ||
| 144 | break; | ||
| 145 | } | ||
| 146 | *p++ = ' '; /* will be a '+' if ACL's implemented */ | ||
| 147 | *p = '\0'; | ||
| 148 | } | ||
diff --git a/src/lib/libc/string/strncat.c b/src/lib/libc/string/strncat.c new file mode 100644 index 0000000000..3d96452af4 --- /dev/null +++ b/src/lib/libc/string/strncat.c | |||
| @@ -0,0 +1,68 @@ | |||
| 1 | /*- | ||
| 2 | * Copyright (c) 1990 The Regents of the University of California. | ||
| 3 | * All rights reserved. | ||
| 4 | * | ||
| 5 | * This code is derived from software contributed to Berkeley by | ||
| 6 | * Chris Torek. | ||
| 7 | * | ||
| 8 | * Redistribution and use in source and binary forms, with or without | ||
| 9 | * modification, are permitted provided that the following conditions | ||
| 10 | * are met: | ||
| 11 | * 1. Redistributions of source code must retain the above copyright | ||
| 12 | * notice, this list of conditions and the following disclaimer. | ||
| 13 | * 2. Redistributions in binary form must reproduce the above copyright | ||
| 14 | * notice, this list of conditions and the following disclaimer in the | ||
| 15 | * documentation and/or other materials provided with the distribution. | ||
| 16 | * 3. All advertising materials mentioning features or use of this software | ||
| 17 | * must display the following acknowledgement: | ||
| 18 | * This product includes software developed by the University of | ||
| 19 | * California, Berkeley and its contributors. | ||
| 20 | * 4. Neither the name of the University nor the names of its contributors | ||
| 21 | * may be used to endorse or promote products derived from this software | ||
| 22 | * without specific prior written permission. | ||
| 23 | * | ||
| 24 | * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND | ||
| 25 | * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE | ||
| 26 | * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE | ||
| 27 | * ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE | ||
| 28 | * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL | ||
| 29 | * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS | ||
| 30 | * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) | ||
| 31 | * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT | ||
| 32 | * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY | ||
| 33 | * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF | ||
| 34 | * SUCH DAMAGE. | ||
| 35 | */ | ||
| 36 | |||
| 37 | #if defined(LIBC_SCCS) && !defined(lint) | ||
| 38 | /*static char *sccsid = "from: @(#)strncat.c 5.6 (Berkeley) 1/26/91";*/ | ||
| 39 | static char *rcsid = "$Id: strncat.c,v 1.1.1.1 1995/10/18 08:42:23 deraadt Exp $"; | ||
| 40 | #endif /* LIBC_SCCS and not lint */ | ||
| 41 | |||
| 42 | #include <string.h> | ||
| 43 | |||
| 44 | /* | ||
| 45 | * Concatenate src on the end of dst. At most strlen(dst)+n+1 bytes | ||
| 46 | * are written at dst (at most n+1 bytes being appended). Return dst. | ||
| 47 | */ | ||
| 48 | char * | ||
| 49 | strncat(dst, src, n) | ||
| 50 | char *dst; | ||
| 51 | const char *src; | ||
| 52 | register size_t n; | ||
| 53 | { | ||
| 54 | if (n != 0) { | ||
| 55 | register char *d = dst; | ||
| 56 | register const char *s = src; | ||
| 57 | |||
| 58 | while (*d != 0) | ||
| 59 | d++; | ||
| 60 | do { | ||
| 61 | if ((*d = *s++) == 0) | ||
| 62 | break; | ||
| 63 | d++; | ||
| 64 | } while (--n != 0); | ||
| 65 | *d = 0; | ||
| 66 | } | ||
| 67 | return (dst); | ||
| 68 | } | ||
diff --git a/src/lib/libc/string/strncmp.c b/src/lib/libc/string/strncmp.c new file mode 100644 index 0000000000..0638d4dcf2 --- /dev/null +++ b/src/lib/libc/string/strncmp.c | |||
| @@ -0,0 +1,56 @@ | |||
| 1 | /* | ||
| 2 | * Copyright (c) 1989 The Regents of the University of California. | ||
| 3 | * All rights reserved. | ||
| 4 | * | ||
| 5 | * Redistribution and use in source and binary forms, with or without | ||
| 6 | * modification, are permitted provided that the following conditions | ||
| 7 | * are met: | ||
| 8 | * 1. Redistributions of source code must retain the above copyright | ||
| 9 | * notice, this list of conditions and the following disclaimer. | ||
| 10 | * 2. Redistributions in binary form must reproduce the above copyright | ||
| 11 | * notice, this list of conditions and the following disclaimer in the | ||
| 12 | * documentation and/or other materials provided with the distribution. | ||
| 13 | * 3. All advertising materials mentioning features or use of this software | ||
| 14 | * must display the following acknowledgement: | ||
| 15 | * This product includes software developed by the University of | ||
| 16 | * California, Berkeley and its contributors. | ||
| 17 | * 4. Neither the name of the University nor the names of its contributors | ||
| 18 | * may be used to endorse or promote products derived from this software | ||
| 19 | * without specific prior written permission. | ||
| 20 | * | ||
| 21 | * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND | ||
| 22 | * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE | ||
| 23 | * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE | ||
| 24 | * ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE | ||
| 25 | * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL | ||
| 26 | * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS | ||
| 27 | * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) | ||
| 28 | * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT | ||
| 29 | * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY | ||
| 30 | * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF | ||
| 31 | * SUCH DAMAGE. | ||
| 32 | */ | ||
| 33 | |||
| 34 | #if defined(LIBC_SCCS) && !defined(lint) | ||
| 35 | /*static char *sccsid = "from: @(#)strncmp.c 5.6 (Berkeley) 1/26/91";*/ | ||
| 36 | static char *rcsid = "$Id: strncmp.c,v 1.1.1.1 1995/10/18 08:42:23 deraadt Exp $"; | ||
| 37 | #endif /* LIBC_SCCS and not lint */ | ||
| 38 | |||
| 39 | #include <string.h> | ||
| 40 | |||
| 41 | int | ||
| 42 | strncmp(s1, s2, n) | ||
| 43 | register const char *s1, *s2; | ||
| 44 | register size_t n; | ||
| 45 | { | ||
| 46 | |||
| 47 | if (n == 0) | ||
| 48 | return (0); | ||
| 49 | do { | ||
| 50 | if (*s1 != *s2++) | ||
| 51 | return (*(unsigned char *)s1 - *(unsigned char *)--s2); | ||
| 52 | if (*s1++ == 0) | ||
| 53 | break; | ||
| 54 | } while (--n != 0); | ||
| 55 | return (0); | ||
| 56 | } | ||
diff --git a/src/lib/libc/string/strncpy.c b/src/lib/libc/string/strncpy.c new file mode 100644 index 0000000000..5215311b75 --- /dev/null +++ b/src/lib/libc/string/strncpy.c | |||
| @@ -0,0 +1,68 @@ | |||
| 1 | /*- | ||
| 2 | * Copyright (c) 1990 The Regents of the University of California. | ||
| 3 | * All rights reserved. | ||
| 4 | * | ||
| 5 | * This code is derived from software contributed to Berkeley by | ||
| 6 | * Chris Torek. | ||
| 7 | * | ||
| 8 | * Redistribution and use in source and binary forms, with or without | ||
| 9 | * modification, are permitted provided that the following conditions | ||
| 10 | * are met: | ||
| 11 | * 1. Redistributions of source code must retain the above copyright | ||
| 12 | * notice, this list of conditions and the following disclaimer. | ||
| 13 | * 2. Redistributions in binary form must reproduce the above copyright | ||
| 14 | * notice, this list of conditions and the following disclaimer in the | ||
| 15 | * documentation and/or other materials provided with the distribution. | ||
| 16 | * 3. All advertising materials mentioning features or use of this software | ||
| 17 | * must display the following acknowledgement: | ||
| 18 | * This product includes software developed by the University of | ||
| 19 | * California, Berkeley and its contributors. | ||
| 20 | * 4. Neither the name of the University nor the names of its contributors | ||
| 21 | * may be used to endorse or promote products derived from this software | ||
| 22 | * without specific prior written permission. | ||
| 23 | * | ||
| 24 | * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND | ||
| 25 | * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE | ||
| 26 | * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE | ||
| 27 | * ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE | ||
| 28 | * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL | ||
| 29 | * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS | ||
| 30 | * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) | ||
| 31 | * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT | ||
| 32 | * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY | ||
| 33 | * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF | ||
| 34 | * SUCH DAMAGE. | ||
| 35 | */ | ||
| 36 | |||
| 37 | #if defined(LIBC_SCCS) && !defined(lint) | ||
| 38 | /*static char *sccsid = "from: @(#)strncpy.c 5.6 (Berkeley) 1/26/91";*/ | ||
| 39 | static char *rcsid = "$Id: strncpy.c,v 1.1.1.1 1995/10/18 08:42:23 deraadt Exp $"; | ||
| 40 | #endif /* LIBC_SCCS and not lint */ | ||
| 41 | |||
| 42 | #include <string.h> | ||
| 43 | |||
| 44 | /* | ||
| 45 | * Copy src to dst, truncating or null-padding to always copy n bytes. | ||
| 46 | * Return dst. | ||
| 47 | */ | ||
| 48 | char * | ||
| 49 | strncpy(dst, src, n) | ||
| 50 | char *dst; | ||
| 51 | const char *src; | ||
| 52 | register size_t n; | ||
| 53 | { | ||
| 54 | if (n != 0) { | ||
| 55 | register char *d = dst; | ||
| 56 | register const char *s = src; | ||
| 57 | |||
| 58 | do { | ||
| 59 | if ((*d++ = *s++) == 0) { | ||
| 60 | /* NUL pad the remaining n-1 bytes */ | ||
| 61 | while (--n != 0) | ||
| 62 | *d++ = 0; | ||
| 63 | break; | ||
| 64 | } | ||
| 65 | } while (--n != 0); | ||
| 66 | } | ||
| 67 | return (dst); | ||
| 68 | } | ||
diff --git a/src/lib/libc/string/strpbrk.3 b/src/lib/libc/string/strpbrk.3 new file mode 100644 index 0000000000..8578546c05 --- /dev/null +++ b/src/lib/libc/string/strpbrk.3 | |||
| @@ -0,0 +1,80 @@ | |||
| 1 | .\" Copyright (c) 1990, 1991 The Regents of the University of California. | ||
| 2 | .\" All rights reserved. | ||
| 3 | .\" | ||
| 4 | .\" This code is derived from software contributed to Berkeley by | ||
| 5 | .\" Chris Torek and the American National Standards Committee X3, | ||
| 6 | .\" on Information Processing Systems. | ||
| 7 | .\" | ||
| 8 | .\" Redistribution and use in source and binary forms, with or without | ||
| 9 | .\" modification, are permitted provided that the following conditions | ||
| 10 | .\" are met: | ||
| 11 | .\" 1. Redistributions of source code must retain the above copyright | ||
| 12 | .\" notice, this list of conditions and the following disclaimer. | ||
| 13 | .\" 2. Redistributions in binary form must reproduce the above copyright | ||
| 14 | .\" notice, this list of conditions and the following disclaimer in the | ||
| 15 | .\" documentation and/or other materials provided with the distribution. | ||
| 16 | .\" 3. All advertising materials mentioning features or use of this software | ||
| 17 | .\" must display the following acknowledgement: | ||
| 18 | .\" This product includes software developed by the University of | ||
| 19 | .\" California, Berkeley and its contributors. | ||
| 20 | .\" 4. Neither the name of the University nor the names of its contributors | ||
| 21 | .\" may be used to endorse or promote products derived from this software | ||
| 22 | .\" without specific prior written permission. | ||
| 23 | .\" | ||
| 24 | .\" THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND | ||
| 25 | .\" ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE | ||
| 26 | .\" IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE | ||
| 27 | .\" ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE | ||
| 28 | .\" FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL | ||
| 29 | .\" DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS | ||
| 30 | .\" OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) | ||
| 31 | .\" HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT | ||
| 32 | .\" LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY | ||
| 33 | .\" OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF | ||
| 34 | .\" SUCH DAMAGE. | ||
| 35 | .\" | ||
| 36 | .\" from: @(#)strpbrk.3 5.4 (Berkeley) 6/29/91 | ||
| 37 | .\" $Id: strpbrk.3,v 1.1.1.1 1995/10/18 08:42:23 deraadt Exp $ | ||
| 38 | .\" | ||
| 39 | .Dd June 29, 1991 | ||
| 40 | .Dt STRPBRK 3 | ||
| 41 | .Os | ||
| 42 | .Sh NAME | ||
| 43 | .Nm strpbrk | ||
| 44 | .Nd locate multiple characters in string | ||
| 45 | .Sh SYNOPSIS | ||
| 46 | .Fd #include <string.h> | ||
| 47 | .Ft char * | ||
| 48 | .Fn strpbrk "const char *s" "const char *charset" | ||
| 49 | .Sh DESCRIPTION | ||
| 50 | The | ||
| 51 | .Fn strpbrk | ||
| 52 | function | ||
| 53 | locates in the null-terminated string | ||
| 54 | .Fa s | ||
| 55 | the first occurrence of any character in the string | ||
| 56 | .Fa charset | ||
| 57 | and returns a pointer to this character. | ||
| 58 | If no characters from | ||
| 59 | .Fa charset | ||
| 60 | occur anywhere in | ||
| 61 | .Fa s | ||
| 62 | .Fn strpbrk | ||
| 63 | returns NULL. | ||
| 64 | .Sh SEE ALSO | ||
| 65 | .Xr index 3 , | ||
| 66 | .Xr memchr 3 , | ||
| 67 | .Xr rindex 3 , | ||
| 68 | .Xr strchr 3 , | ||
| 69 | .Xr strcspn 3 , | ||
| 70 | .Xr strrchr 3 , | ||
| 71 | .Xr strsep 3 , | ||
| 72 | .Xr strspn 3 , | ||
| 73 | .Xr strstr 3 , | ||
| 74 | .Xr strtok 3 | ||
| 75 | .Sh STANDARDS | ||
| 76 | The | ||
| 77 | .Fn strpbrk | ||
| 78 | function | ||
| 79 | conforms to | ||
| 80 | .St -ansiC . | ||
diff --git a/src/lib/libc/string/strpbrk.c b/src/lib/libc/string/strpbrk.c new file mode 100644 index 0000000000..f1d542a525 --- /dev/null +++ b/src/lib/libc/string/strpbrk.c | |||
| @@ -0,0 +1,57 @@ | |||
| 1 | /* | ||
| 2 | * Copyright (c) 1985 Regents of the University of California. | ||
| 3 | * All rights reserved. | ||
| 4 | * | ||
| 5 | * Redistribution and use in source and binary forms, with or without | ||
| 6 | * modification, are permitted provided that the following conditions | ||
| 7 | * are met: | ||
| 8 | * 1. Redistributions of source code must retain the above copyright | ||
| 9 | * notice, this list of conditions and the following disclaimer. | ||
| 10 | * 2. Redistributions in binary form must reproduce the above copyright | ||
| 11 | * notice, this list of conditions and the following disclaimer in the | ||
| 12 | * documentation and/or other materials provided with the distribution. | ||
| 13 | * 3. All advertising materials mentioning features or use of this software | ||
| 14 | * must display the following acknowledgement: | ||
| 15 | * This product includes software developed by the University of | ||
| 16 | * California, Berkeley and its contributors. | ||
| 17 | * 4. Neither the name of the University nor the names of its contributors | ||
| 18 | * may be used to endorse or promote products derived from this software | ||
| 19 | * without specific prior written permission. | ||
| 20 | * | ||
| 21 | * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND | ||
| 22 | * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE | ||
| 23 | * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE | ||
| 24 | * ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE | ||
| 25 | * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL | ||
| 26 | * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS | ||
| 27 | * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) | ||
| 28 | * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT | ||
| 29 | * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY | ||
| 30 | * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF | ||
| 31 | * SUCH DAMAGE. | ||
| 32 | */ | ||
| 33 | |||
| 34 | #if defined(LIBC_SCCS) && !defined(lint) | ||
| 35 | /*static char *sccsid = "from: @(#)strpbrk.c 5.8 (Berkeley) 1/26/91";*/ | ||
| 36 | static char *rcsid = "$Id: strpbrk.c,v 1.1.1.1 1995/10/18 08:42:23 deraadt Exp $"; | ||
| 37 | #endif /* LIBC_SCCS and not lint */ | ||
| 38 | |||
| 39 | #include <string.h> | ||
| 40 | |||
| 41 | /* | ||
| 42 | * Find the first occurrence in s1 of a character in s2 (excluding NUL). | ||
| 43 | */ | ||
| 44 | char * | ||
| 45 | strpbrk(s1, s2) | ||
| 46 | register const char *s1, *s2; | ||
| 47 | { | ||
| 48 | register const char *scanp; | ||
| 49 | register int c, sc; | ||
| 50 | |||
| 51 | while ((c = *s1++) != 0) { | ||
| 52 | for (scanp = s2; (sc = *scanp++) != 0;) | ||
| 53 | if (sc == c) | ||
| 54 | return ((char *)(s1 - 1)); | ||
| 55 | } | ||
| 56 | return (NULL); | ||
| 57 | } | ||
diff --git a/src/lib/libc/string/strrchr.3 b/src/lib/libc/string/strrchr.3 new file mode 100644 index 0000000000..1d98cbff24 --- /dev/null +++ b/src/lib/libc/string/strrchr.3 | |||
| @@ -0,0 +1,91 @@ | |||
| 1 | .\" Copyright (c) 1990, 1991 The Regents of the University of California. | ||
| 2 | .\" All rights reserved. | ||
| 3 | .\" | ||
| 4 | .\" This code is derived from software contributed to Berkeley by | ||
| 5 | .\" Chris Torek and the American National Standards Committee X3, | ||
| 6 | .\" on Information Processing Systems. | ||
| 7 | .\" | ||
| 8 | .\" Redistribution and use in source and binary forms, with or without | ||
| 9 | .\" modification, are permitted provided that the following conditions | ||
| 10 | .\" are met: | ||
| 11 | .\" 1. Redistributions of source code must retain the above copyright | ||
| 12 | .\" notice, this list of conditions and the following disclaimer. | ||
| 13 | .\" 2. Redistributions in binary form must reproduce the above copyright | ||
| 14 | .\" notice, this list of conditions and the following disclaimer in the | ||
| 15 | .\" documentation and/or other materials provided with the distribution. | ||
| 16 | .\" 3. All advertising materials mentioning features or use of this software | ||
| 17 | .\" must display the following acknowledgement: | ||
| 18 | .\" This product includes software developed by the University of | ||
| 19 | .\" California, Berkeley and its contributors. | ||
| 20 | .\" 4. Neither the name of the University nor the names of its contributors | ||
| 21 | .\" may be used to endorse or promote products derived from this software | ||
| 22 | .\" without specific prior written permission. | ||
| 23 | .\" | ||
| 24 | .\" THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND | ||
| 25 | .\" ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE | ||
| 26 | .\" IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE | ||
| 27 | .\" ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE | ||
| 28 | .\" FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL | ||
| 29 | .\" DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS | ||
| 30 | .\" OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) | ||
| 31 | .\" HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT | ||
| 32 | .\" LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY | ||
| 33 | .\" OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF | ||
| 34 | .\" SUCH DAMAGE. | ||
| 35 | .\" | ||
| 36 | .\" from: @(#)strrchr.3 5.3 (Berkeley) 6/29/91 | ||
| 37 | .\" $Id: strrchr.3,v 1.1.1.1 1995/10/18 08:42:23 deraadt Exp $ | ||
| 38 | .\" | ||
| 39 | .Dd June 29, 1991 | ||
| 40 | .Dt STRRCHR 3 | ||
| 41 | .Os | ||
| 42 | .Sh NAME | ||
| 43 | .Nm strrchr | ||
| 44 | .Nd locate character in string | ||
| 45 | .Sh SYNOPSIS | ||
| 46 | .Fd #include <string.h> | ||
| 47 | .Ft char * | ||
| 48 | .Fn strrchr "const char *s" "int c" | ||
| 49 | .Sh DESCRIPTION | ||
| 50 | The | ||
| 51 | .Fn strrchr | ||
| 52 | function | ||
| 53 | locates the last occurrence of | ||
| 54 | .Fa c | ||
| 55 | (converted to a char) | ||
| 56 | in the string | ||
| 57 | .Fa s . | ||
| 58 | If | ||
| 59 | .Fa c | ||
| 60 | is | ||
| 61 | .Ql \e0 , | ||
| 62 | .Fn strrchr | ||
| 63 | locates the terminating | ||
| 64 | .Ql \e0 . | ||
| 65 | .Sh RETURN VALUES | ||
| 66 | The | ||
| 67 | .Fn strrchr | ||
| 68 | function | ||
| 69 | returns a pointer to the character, | ||
| 70 | or a null | ||
| 71 | pointer if | ||
| 72 | .Fa c | ||
| 73 | does not occur anywhere in | ||
| 74 | .Fa s . | ||
| 75 | .Sh SEE ALSO | ||
| 76 | .Xr index 3 , | ||
| 77 | .Xr memchr 3 , | ||
| 78 | .Xr rindex 3 , | ||
| 79 | .Xr strchr 3 , | ||
| 80 | .Xr strcspn 3 , | ||
| 81 | .Xr strpbrk 3 , | ||
| 82 | .Xr strsep 3 , | ||
| 83 | .Xr strspn 3 , | ||
| 84 | .Xr strstr 3 , | ||
| 85 | .Xr strtok 3 | ||
| 86 | .Sh STANDARDS | ||
| 87 | The | ||
| 88 | .Fn strrchr | ||
| 89 | function | ||
| 90 | conforms to | ||
| 91 | .St -ansiC . | ||
diff --git a/src/lib/libc/string/strsep.3 b/src/lib/libc/string/strsep.3 new file mode 100644 index 0000000000..21aa7376f2 --- /dev/null +++ b/src/lib/libc/string/strsep.3 | |||
| @@ -0,0 +1,92 @@ | |||
| 1 | .\" Copyright (c) 1990, 1991 The Regents of the University of California. | ||
| 2 | .\" All rights reserved. | ||
| 3 | .\" | ||
| 4 | .\" This code is derived from software contributed to Berkeley by | ||
| 5 | .\" Chris Torek. | ||
| 6 | .\" Redistribution and use in source and binary forms, with or without | ||
| 7 | .\" modification, are permitted provided that the following conditions | ||
| 8 | .\" are met: | ||
| 9 | .\" 1. Redistributions of source code must retain the above copyright | ||
| 10 | .\" notice, this list of conditions and the following disclaimer. | ||
| 11 | .\" 2. Redistributions in binary form must reproduce the above copyright | ||
| 12 | .\" notice, this list of conditions and the following disclaimer in the | ||
| 13 | .\" documentation and/or other materials provided with the distribution. | ||
| 14 | .\" 3. All advertising materials mentioning features or use of this software | ||
| 15 | .\" must display the following acknowledgement: | ||
| 16 | .\" This product includes software developed by the University of | ||
| 17 | .\" California, Berkeley and its contributors. | ||
| 18 | .\" 4. Neither the name of the University nor the names of its contributors | ||
| 19 | .\" may be used to endorse or promote products derived from this software | ||
| 20 | .\" without specific prior written permission. | ||
| 21 | .\" | ||
| 22 | .\" THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND | ||
| 23 | .\" ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE | ||
| 24 | .\" IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE | ||
| 25 | .\" ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE | ||
| 26 | .\" FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL | ||
| 27 | .\" DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS | ||
| 28 | .\" OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) | ||
| 29 | .\" HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT | ||
| 30 | .\" LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY | ||
| 31 | .\" OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF | ||
| 32 | .\" SUCH DAMAGE. | ||
| 33 | .\" | ||
| 34 | .\" from: @(#)strsep.3 5.3 (Berkeley) 4/19/91 | ||
| 35 | .\" $Id: strsep.3,v 1.1.1.1 1995/10/18 08:42:23 deraadt Exp $ | ||
| 36 | .\" | ||
| 37 | .Dd April 19, 1991 | ||
| 38 | .Dt STRSEP 3 | ||
| 39 | .Os | ||
| 40 | .Sh NAME | ||
| 41 | .Nm strsep | ||
| 42 | .Nd separate strings | ||
| 43 | .Sh SYNOPSIS | ||
| 44 | .Fd #include <string.h> | ||
| 45 | .Ft char * | ||
| 46 | .Fn strsep "char **stringp" "char *delim" | ||
| 47 | .Sh DESCRIPTION | ||
| 48 | The | ||
| 49 | .Fn strsep | ||
| 50 | locates in the null-terminated string at | ||
| 51 | .Fa *stringp | ||
| 52 | the first occurrence of any character in | ||
| 53 | .Fa delim | ||
| 54 | and replaces this with a | ||
| 55 | .Ql \e0 , | ||
| 56 | records the location of the immediate following character in | ||
| 57 | .Fa *stringp , | ||
| 58 | then returns the original value of | ||
| 59 | .Fa *stringp . | ||
| 60 | If no delimiter characters are found, | ||
| 61 | .Fn strsep | ||
| 62 | sets | ||
| 63 | .Fa *stringp | ||
| 64 | to | ||
| 65 | .Dv NULL ; | ||
| 66 | if | ||
| 67 | .Fa *stringp | ||
| 68 | is initially | ||
| 69 | .Dv NULL , | ||
| 70 | .Fn strsep | ||
| 71 | returns | ||
| 72 | .Dv NULL . | ||
| 73 | .Sh EXAMPLES | ||
| 74 | The following uses | ||
| 75 | .Fn strsep | ||
| 76 | to parse strings containing runs of white space, | ||
| 77 | making up an argument vector: | ||
| 78 | .Bd -literal -offset indent | ||
| 79 | char inputstring[100]; | ||
| 80 | char **argv[51], **ap = argv, *p, *val; | ||
| 81 | /* set up inputstring */ | ||
| 82 | for (p = inputstring; p != NULL; ) { | ||
| 83 | while ((val = strsep(&p, " \et")) != NULL && *val == '\e0'); | ||
| 84 | *ap++ = val; | ||
| 85 | } | ||
| 86 | *ap = 0; | ||
| 87 | .Ed | ||
| 88 | .Sh HISTORY | ||
| 89 | The | ||
| 90 | .Fn strsep | ||
| 91 | function is | ||
| 92 | .Ud . | ||
diff --git a/src/lib/libc/string/strsep.c b/src/lib/libc/string/strsep.c new file mode 100644 index 0000000000..69be7fe046 --- /dev/null +++ b/src/lib/libc/string/strsep.c | |||
| @@ -0,0 +1,79 @@ | |||
| 1 | /*- | ||
| 2 | * Copyright (c) 1990 The Regents of the University of California. | ||
| 3 | * All rights reserved. | ||
| 4 | * | ||
| 5 | * Redistribution and use in source and binary forms, with or without | ||
| 6 | * modification, are permitted provided that the following conditions | ||
| 7 | * are met: | ||
| 8 | * 1. Redistributions of source code must retain the above copyright | ||
| 9 | * notice, this list of conditions and the following disclaimer. | ||
| 10 | * 2. Redistributions in binary form must reproduce the above copyright | ||
| 11 | * notice, this list of conditions and the following disclaimer in the | ||
| 12 | * documentation and/or other materials provided with the distribution. | ||
| 13 | * 3. All advertising materials mentioning features or use of this software | ||
| 14 | * must display the following acknowledgement: | ||
| 15 | * This product includes software developed by the University of | ||
| 16 | * California, Berkeley and its contributors. | ||
| 17 | * 4. Neither the name of the University nor the names of its contributors | ||
| 18 | * may be used to endorse or promote products derived from this software | ||
| 19 | * without specific prior written permission. | ||
| 20 | * | ||
| 21 | * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND | ||
| 22 | * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE | ||
| 23 | * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE | ||
| 24 | * ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE | ||
| 25 | * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL | ||
| 26 | * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS | ||
| 27 | * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) | ||
| 28 | * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT | ||
| 29 | * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY | ||
| 30 | * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF | ||
| 31 | * SUCH DAMAGE. | ||
| 32 | */ | ||
| 33 | |||
| 34 | #if defined(LIBC_SCCS) && !defined(lint) | ||
| 35 | /*static const char sccsid[] = "from: @(#)strsep.c 5.4 (Berkeley) 1/26/91";*/ | ||
| 36 | static char *rcsid = "$Id: strsep.c,v 1.1.1.1 1995/10/18 08:42:23 deraadt Exp $"; | ||
| 37 | #endif /* LIBC_SCCS and not lint */ | ||
| 38 | |||
| 39 | #include <string.h> | ||
| 40 | |||
| 41 | /* | ||
| 42 | * Get next token from string *stringp, where tokens are nonempty | ||
| 43 | * strings separated by characters from delim. | ||
| 44 | * | ||
| 45 | * Writes NULs into the string at *stringp to end tokens. | ||
| 46 | * delim need not remain constant from call to call. | ||
| 47 | * On return, *stringp points past the last NUL written (if there might | ||
| 48 | * be further tokens), or is NULL (if there are definitely no more tokens). | ||
| 49 | * | ||
| 50 | * If *stringp is NULL, strtoken returns NULL. | ||
| 51 | */ | ||
| 52 | char * | ||
| 53 | strsep(stringp, delim) | ||
| 54 | register char **stringp; | ||
| 55 | register const char *delim; | ||
| 56 | { | ||
| 57 | register char *s; | ||
| 58 | register const char *spanp; | ||
| 59 | register int c, sc; | ||
| 60 | char *tok; | ||
| 61 | |||
| 62 | if ((s = *stringp) == NULL) | ||
| 63 | return (NULL); | ||
| 64 | for (tok = s;;) { | ||
| 65 | c = *s++; | ||
| 66 | spanp = delim; | ||
| 67 | do { | ||
| 68 | if ((sc = *spanp++) == c) { | ||
| 69 | if (c == 0) | ||
| 70 | s = NULL; | ||
| 71 | else | ||
| 72 | s[-1] = 0; | ||
| 73 | *stringp = s; | ||
| 74 | return (tok); | ||
| 75 | } | ||
| 76 | } while (sc != 0); | ||
| 77 | } | ||
| 78 | /* NOTREACHED */ | ||
| 79 | } | ||
diff --git a/src/lib/libc/string/strsignal.3 b/src/lib/libc/string/strsignal.3 new file mode 100644 index 0000000000..3287fef53e --- /dev/null +++ b/src/lib/libc/string/strsignal.3 | |||
| @@ -0,0 +1,61 @@ | |||
| 1 | .\" Copyright (c) 1980, 1991 Regents of the University of California. | ||
| 2 | .\" All rights reserved. | ||
| 3 | .\" | ||
| 4 | .\" This code is derived from software contributed to Berkeley by | ||
| 5 | .\" the American National Standards Committee X3, on Information | ||
| 6 | .\" Processing Systems. | ||
| 7 | .\" | ||
| 8 | .\" Redistribution and use in source and binary forms, with or without | ||
| 9 | .\" modification, are permitted provided that the following conditions | ||
| 10 | .\" are met: | ||
| 11 | .\" 1. Redistributions of source code must retain the above copyright | ||
| 12 | .\" notice, this list of conditions and the following disclaimer. | ||
| 13 | .\" 2. Redistributions in binary form must reproduce the above copyright | ||
| 14 | .\" notice, this list of conditions and the following disclaimer in the | ||
| 15 | .\" documentation and/or other materials provided with the distribution. | ||
| 16 | .\" 3. All advertising materials mentioning features or use of this software | ||
| 17 | .\" must display the following acknowledgement: | ||
| 18 | .\" This product includes software developed by the University of | ||
| 19 | .\" California, Berkeley and its contributors. | ||
| 20 | .\" 4. Neither the name of the University nor the names of its contributors | ||
| 21 | .\" may be used to endorse or promote products derived from this software | ||
| 22 | .\" without specific prior written permission. | ||
| 23 | .\" | ||
| 24 | .\" THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND | ||
| 25 | .\" ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE | ||
| 26 | .\" IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE | ||
| 27 | .\" ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE | ||
| 28 | .\" FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL | ||
| 29 | .\" DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS | ||
| 30 | .\" OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) | ||
| 31 | .\" HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT | ||
| 32 | .\" LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY | ||
| 33 | .\" OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF | ||
| 34 | .\" SUCH DAMAGE. | ||
| 35 | .\" | ||
| 36 | .\" from: @(#)strerror.3 6.9 (Berkeley) 6/29/91 | ||
| 37 | .\" $Id: strsignal.3,v 1.1.1.1 1995/10/18 08:42:23 deraadt Exp $ | ||
| 38 | .\" | ||
| 39 | .Dd June 29, 1991 | ||
| 40 | .Dt STRSIGNAL 3 | ||
| 41 | .Os BSD 4 | ||
| 42 | .Sh NAME | ||
| 43 | .Nm strsignal | ||
| 44 | .Nd get signal description string | ||
| 45 | .Sh SYNOPSIS | ||
| 46 | .Fd #include <string.h> | ||
| 47 | .Ft char * | ||
| 48 | .Fn strsignal "int sig" | ||
| 49 | .Sh DESCRIPTION | ||
| 50 | The | ||
| 51 | .Fn strsignal | ||
| 52 | function returns a pointer to the language-dependent string describing | ||
| 53 | a signal. | ||
| 54 | .Pp | ||
| 55 | The array pointed to is not to be modified by the program, but may be | ||
| 56 | overwritten by subsequent calls to | ||
| 57 | .Fn strsignal . | ||
| 58 | .Sh SEE ALSO | ||
| 59 | .Xr intro 2 , | ||
| 60 | .Xr psignal 3 , | ||
| 61 | .Xr setlocale 3 | ||
diff --git a/src/lib/libc/string/strsignal.c b/src/lib/libc/string/strsignal.c new file mode 100644 index 0000000000..ec4a267edf --- /dev/null +++ b/src/lib/libc/string/strsignal.c | |||
| @@ -0,0 +1,50 @@ | |||
| 1 | /* | ||
| 2 | * Copyright (c) 1988 Regents of the University of California. | ||
| 3 | * All rights reserved. | ||
| 4 | * | ||
| 5 | * Redistribution and use in source and binary forms, with or without | ||
| 6 | * modification, are permitted provided that the following conditions | ||
| 7 | * are met: | ||
| 8 | * 1. Redistributions of source code must retain the above copyright | ||
| 9 | * notice, this list of conditions and the following disclaimer. | ||
| 10 | * 2. Redistributions in binary form must reproduce the above copyright | ||
| 11 | * notice, this list of conditions and the following disclaimer in the | ||
| 12 | * documentation and/or other materials provided with the distribution. | ||
| 13 | * 3. All advertising materials mentioning features or use of this software | ||
| 14 | * must display the following acknowledgement: | ||
| 15 | * This product includes software developed by the University of | ||
| 16 | * California, Berkeley and its contributors. | ||
| 17 | * 4. Neither the name of the University nor the names of its contributors | ||
| 18 | * may be used to endorse or promote products derived from this software | ||
| 19 | * without specific prior written permission. | ||
| 20 | * | ||
| 21 | * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND | ||
| 22 | * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE | ||
| 23 | * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE | ||
| 24 | * ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE | ||
| 25 | * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL | ||
| 26 | * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS | ||
| 27 | * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) | ||
| 28 | * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT | ||
| 29 | * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY | ||
| 30 | * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF | ||
| 31 | * SUCH DAMAGE. | ||
| 32 | */ | ||
| 33 | |||
| 34 | #if defined(LIBC_SCCS) && !defined(lint) | ||
| 35 | /*static char *sccsid = "from: @(#)strerror.c 5.6 (Berkeley) 5/4/91";*/ | ||
| 36 | static char *rcsid = "$Id: strsignal.c,v 1.1.1.1 1995/10/18 08:42:23 deraadt Exp $"; | ||
| 37 | #endif /* LIBC_SCCS and not lint */ | ||
| 38 | |||
| 39 | #include <string.h> | ||
| 40 | #include <limits.h> | ||
| 41 | |||
| 42 | extern char *__strsignal __P((int, char *)); | ||
| 43 | |||
| 44 | char * | ||
| 45 | strsignal(sig) | ||
| 46 | int sig; | ||
| 47 | { | ||
| 48 | static char buf[NL_TEXTMAX]; | ||
| 49 | return __strsignal(sig, buf); | ||
| 50 | } | ||
diff --git a/src/lib/libc/string/strspn.3 b/src/lib/libc/string/strspn.3 new file mode 100644 index 0000000000..4de03aa58b --- /dev/null +++ b/src/lib/libc/string/strspn.3 | |||
| @@ -0,0 +1,80 @@ | |||
| 1 | .\" Copyright (c) 1990, 1991 The Regents of the University of California. | ||
| 2 | .\" All rights reserved. | ||
| 3 | .\" | ||
| 4 | .\" This code is derived from software contributed to Berkeley by | ||
| 5 | .\" Chris Torek and the American National Standards Committee X3, | ||
| 6 | .\" on Information Processing Systems. | ||
| 7 | .\" | ||
| 8 | .\" Redistribution and use in source and binary forms, with or without | ||
| 9 | .\" modification, are permitted provided that the following conditions | ||
| 10 | .\" are met: | ||
| 11 | .\" 1. Redistributions of source code must retain the above copyright | ||
| 12 | .\" notice, this list of conditions and the following disclaimer. | ||
| 13 | .\" 2. Redistributions in binary form must reproduce the above copyright | ||
| 14 | .\" notice, this list of conditions and the following disclaimer in the | ||
| 15 | .\" documentation and/or other materials provided with the distribution. | ||
| 16 | .\" 3. All advertising materials mentioning features or use of this software | ||
| 17 | .\" must display the following acknowledgement: | ||
| 18 | .\" This product includes software developed by the University of | ||
| 19 | .\" California, Berkeley and its contributors. | ||
| 20 | .\" 4. Neither the name of the University nor the names of its contributors | ||
| 21 | .\" may be used to endorse or promote products derived from this software | ||
| 22 | .\" without specific prior written permission. | ||
| 23 | .\" | ||
| 24 | .\" THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND | ||
| 25 | .\" ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE | ||
| 26 | .\" IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE | ||
| 27 | .\" ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE | ||
| 28 | .\" FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL | ||
| 29 | .\" DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS | ||
| 30 | .\" OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) | ||
| 31 | .\" HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT | ||
| 32 | .\" LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY | ||
| 33 | .\" OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF | ||
| 34 | .\" SUCH DAMAGE. | ||
| 35 | .\" | ||
| 36 | .\" from: @(#)strspn.3 5.3 (Berkeley) 6/29/91 | ||
| 37 | .\" $Id: strspn.3,v 1.1.1.1 1995/10/18 08:42:23 deraadt Exp $ | ||
| 38 | .\" | ||
| 39 | .Dd June 29, 1991 | ||
| 40 | .Dt STRSPN 3 | ||
| 41 | .Os | ||
| 42 | .Sh NAME | ||
| 43 | .Nm strspn | ||
| 44 | .Nd span a string | ||
| 45 | .Sh SYNOPSIS | ||
| 46 | .Fd #include <string.h> | ||
| 47 | .Ft size_t | ||
| 48 | .Fn strspn "const char *s" "const char *charset" | ||
| 49 | .Sh DESCRIPTION | ||
| 50 | The | ||
| 51 | .Xr strcspn | ||
| 52 | function | ||
| 53 | spans the initial part of the null-terminated string | ||
| 54 | .Fa s | ||
| 55 | as long as the characters from | ||
| 56 | .Fa s | ||
| 57 | occur in string | ||
| 58 | .Fa charset . | ||
| 59 | .Sh RETURN VALUES | ||
| 60 | The | ||
| 61 | .Fn strspn | ||
| 62 | function | ||
| 63 | returns the number of characters spanned. | ||
| 64 | .Sh SEE ALSO | ||
| 65 | .Xr index 3 , | ||
| 66 | .Xr memchr 3 , | ||
| 67 | .Xr rindex 3 , | ||
| 68 | .Xr strchr 3 , | ||
| 69 | .Xr strcspn 3 , | ||
| 70 | .Xr strpbrk 3 , | ||
| 71 | .Xr strrchr 3 , | ||
| 72 | .Xr strsep 3 , | ||
| 73 | .Xr strstr 3 , | ||
| 74 | .Xr strtok 3 | ||
| 75 | .Sh STANDARDS | ||
| 76 | The | ||
| 77 | .Fn strspn | ||
| 78 | function | ||
| 79 | conforms to | ||
| 80 | .St -ansiC . | ||
diff --git a/src/lib/libc/string/strspn.c b/src/lib/libc/string/strspn.c new file mode 100644 index 0000000000..6224b25c2a --- /dev/null +++ b/src/lib/libc/string/strspn.c | |||
| @@ -0,0 +1,61 @@ | |||
| 1 | /* | ||
| 2 | * Copyright (c) 1989 The Regents of the University of California. | ||
| 3 | * All rights reserved. | ||
| 4 | * | ||
| 5 | * Redistribution and use in source and binary forms, with or without | ||
| 6 | * modification, are permitted provided that the following conditions | ||
| 7 | * are met: | ||
| 8 | * 1. Redistributions of source code must retain the above copyright | ||
| 9 | * notice, this list of conditions and the following disclaimer. | ||
| 10 | * 2. Redistributions in binary form must reproduce the above copyright | ||
| 11 | * notice, this list of conditions and the following disclaimer in the | ||
| 12 | * documentation and/or other materials provided with the distribution. | ||
| 13 | * 3. All advertising materials mentioning features or use of this software | ||
| 14 | * must display the following acknowledgement: | ||
| 15 | * This product includes software developed by the University of | ||
| 16 | * California, Berkeley and its contributors. | ||
| 17 | * 4. Neither the name of the University nor the names of its contributors | ||
| 18 | * may be used to endorse or promote products derived from this software | ||
| 19 | * without specific prior written permission. | ||
| 20 | * | ||
| 21 | * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND | ||
| 22 | * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE | ||
| 23 | * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE | ||
| 24 | * ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE | ||
| 25 | * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL | ||
| 26 | * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS | ||
| 27 | * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) | ||
| 28 | * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT | ||
| 29 | * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY | ||
| 30 | * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF | ||
| 31 | * SUCH DAMAGE. | ||
| 32 | */ | ||
| 33 | |||
| 34 | #if defined(LIBC_SCCS) && !defined(lint) | ||
| 35 | /*static char *sccsid = "from: @(#)strspn.c 5.8 (Berkeley) 1/26/91";*/ | ||
| 36 | static char *rcsid = "$Id: strspn.c,v 1.1.1.1 1995/10/18 08:42:23 deraadt Exp $"; | ||
| 37 | #endif /* LIBC_SCCS and not lint */ | ||
| 38 | |||
| 39 | #include <string.h> | ||
| 40 | |||
| 41 | /* | ||
| 42 | * Span the string s2 (skip characters that are in s2). | ||
| 43 | */ | ||
| 44 | size_t | ||
| 45 | strspn(s1, s2) | ||
| 46 | const char *s1; | ||
| 47 | register const char *s2; | ||
| 48 | { | ||
| 49 | register const char *p = s1, *spanp; | ||
| 50 | register char c, sc; | ||
| 51 | |||
| 52 | /* | ||
| 53 | * Skip any characters in s2, excluding the terminating \0. | ||
| 54 | */ | ||
| 55 | cont: | ||
| 56 | c = *p++; | ||
| 57 | for (spanp = s2; (sc = *spanp++) != 0;) | ||
| 58 | if (sc == c) | ||
| 59 | goto cont; | ||
| 60 | return (p - 1 - s1); | ||
| 61 | } | ||
diff --git a/src/lib/libc/string/strstr.3 b/src/lib/libc/string/strstr.3 new file mode 100644 index 0000000000..24fdf540ed --- /dev/null +++ b/src/lib/libc/string/strstr.3 | |||
| @@ -0,0 +1,89 @@ | |||
| 1 | .\" Copyright (c) 1990, 1991 The Regents of the University of California. | ||
| 2 | .\" All rights reserved. | ||
| 3 | .\" | ||
| 4 | .\" This code is derived from software contributed to Berkeley by | ||
| 5 | .\" Chris Torek and the American National Standards Committee X3, | ||
| 6 | .\" on Information Processing Systems. | ||
| 7 | .\" | ||
| 8 | .\" Redistribution and use in source and binary forms, with or without | ||
| 9 | .\" modification, are permitted provided that the following conditions | ||
| 10 | .\" are met: | ||
| 11 | .\" 1. Redistributions of source code must retain the above copyright | ||
| 12 | .\" notice, this list of conditions and the following disclaimer. | ||
| 13 | .\" 2. Redistributions in binary form must reproduce the above copyright | ||
| 14 | .\" notice, this list of conditions and the following disclaimer in the | ||
| 15 | .\" documentation and/or other materials provided with the distribution. | ||
| 16 | .\" 3. All advertising materials mentioning features or use of this software | ||
| 17 | .\" must display the following acknowledgement: | ||
| 18 | .\" This product includes software developed by the University of | ||
| 19 | .\" California, Berkeley and its contributors. | ||
| 20 | .\" 4. Neither the name of the University nor the names of its contributors | ||
| 21 | .\" may be used to endorse or promote products derived from this software | ||
| 22 | .\" without specific prior written permission. | ||
| 23 | .\" | ||
| 24 | .\" THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND | ||
| 25 | .\" ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE | ||
| 26 | .\" IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE | ||
| 27 | .\" ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE | ||
| 28 | .\" FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL | ||
| 29 | .\" DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS | ||
| 30 | .\" OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) | ||
| 31 | .\" HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT | ||
| 32 | .\" LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY | ||
| 33 | .\" OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF | ||
| 34 | .\" SUCH DAMAGE. | ||
| 35 | .\" | ||
| 36 | .\" from: @(#)strstr.3 5.3 (Berkeley) 6/29/91 | ||
| 37 | .\" $Id: strstr.3,v 1.1.1.1 1995/10/18 08:42:23 deraadt Exp $ | ||
| 38 | .\" | ||
| 39 | .Dd June 29, 1991 | ||
| 40 | .Dt STRSTR 3 | ||
| 41 | .Os | ||
| 42 | .Sh NAME | ||
| 43 | .Nm strstr | ||
| 44 | .Nd locate a substring in a string | ||
| 45 | .Sh SYNOPSIS | ||
| 46 | .Fd #include <string.h> | ||
| 47 | .Ft char * | ||
| 48 | .Fn strstr "const char *big" "const char *little" | ||
| 49 | .Sh DESCRIPTION | ||
| 50 | The | ||
| 51 | .Fn strstr | ||
| 52 | function | ||
| 53 | locates the first occurrence of the null-terminated string | ||
| 54 | .Fa little | ||
| 55 | in the null-terminated string | ||
| 56 | .Fa big . | ||
| 57 | If | ||
| 58 | .Fa little | ||
| 59 | is the empty string, | ||
| 60 | .Fn strstr | ||
| 61 | returns | ||
| 62 | .Fa big ; | ||
| 63 | if | ||
| 64 | .Fa little | ||
| 65 | occurs nowhere in | ||
| 66 | .Fa big , | ||
| 67 | .Fn strstr | ||
| 68 | returns NULL; | ||
| 69 | otherwise | ||
| 70 | .Fn strstr | ||
| 71 | returns a pointer to the first character of the first occurrence of | ||
| 72 | .Fa little . | ||
| 73 | .Sh SEE ALSO | ||
| 74 | .Xr index 3 , | ||
| 75 | .Xr memchr 3 , | ||
| 76 | .Xr rindex 3 , | ||
| 77 | .Xr strchr 3 , | ||
| 78 | .Xr strcspn 3 , | ||
| 79 | .Xr strpbrk 3 , | ||
| 80 | .Xr strrchr 3 , | ||
| 81 | .Xr strsep 3 , | ||
| 82 | .Xr strspn 3 , | ||
| 83 | .Xr strtok 3 | ||
| 84 | .Sh STANDARDS | ||
| 85 | The | ||
| 86 | .Fn strstr | ||
| 87 | function | ||
| 88 | conforms to | ||
| 89 | .St -ansiC . | ||
diff --git a/src/lib/libc/string/strstr.c b/src/lib/libc/string/strstr.c new file mode 100644 index 0000000000..1ed59e357b --- /dev/null +++ b/src/lib/libc/string/strstr.c | |||
| @@ -0,0 +1,65 @@ | |||
| 1 | /*- | ||
| 2 | * Copyright (c) 1990 The Regents of the University of California. | ||
| 3 | * All rights reserved. | ||
| 4 | * | ||
| 5 | * This code is derived from software contributed to Berkeley by | ||
| 6 | * Chris Torek. | ||
| 7 | * | ||
| 8 | * Redistribution and use in source and binary forms, with or without | ||
| 9 | * modification, are permitted provided that the following conditions | ||
| 10 | * are met: | ||
| 11 | * 1. Redistributions of source code must retain the above copyright | ||
| 12 | * notice, this list of conditions and the following disclaimer. | ||
| 13 | * 2. Redistributions in binary form must reproduce the above copyright | ||
| 14 | * notice, this list of conditions and the following disclaimer in the | ||
| 15 | * documentation and/or other materials provided with the distribution. | ||
| 16 | * 3. All advertising materials mentioning features or use of this software | ||
| 17 | * must display the following acknowledgement: | ||
| 18 | * This product includes software developed by the University of | ||
| 19 | * California, Berkeley and its contributors. | ||
| 20 | * 4. Neither the name of the University nor the names of its contributors | ||
| 21 | * may be used to endorse or promote products derived from this software | ||
| 22 | * without specific prior written permission. | ||
| 23 | * | ||
| 24 | * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND | ||
| 25 | * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE | ||
| 26 | * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE | ||
| 27 | * ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE | ||
| 28 | * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL | ||
| 29 | * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS | ||
| 30 | * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) | ||
| 31 | * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT | ||
| 32 | * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY | ||
| 33 | * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF | ||
| 34 | * SUCH DAMAGE. | ||
| 35 | */ | ||
| 36 | |||
| 37 | #if defined(LIBC_SCCS) && !defined(lint) | ||
| 38 | /*static char *sccsid = "from: @(#)strstr.c 5.2 (Berkeley) 1/26/91";*/ | ||
| 39 | static char *rcsid = "$Id: strstr.c,v 1.1.1.1 1995/10/18 08:42:23 deraadt Exp $"; | ||
| 40 | #endif /* LIBC_SCCS and not lint */ | ||
| 41 | |||
| 42 | #include <string.h> | ||
| 43 | |||
| 44 | /* | ||
| 45 | * Find the first occurrence of find in s. | ||
| 46 | */ | ||
| 47 | char * | ||
| 48 | strstr(s, find) | ||
| 49 | register const char *s, *find; | ||
| 50 | { | ||
| 51 | register char c, sc; | ||
| 52 | register size_t len; | ||
| 53 | |||
| 54 | if ((c = *find++) != 0) { | ||
| 55 | len = strlen(find); | ||
| 56 | do { | ||
| 57 | do { | ||
| 58 | if ((sc = *s++) == 0) | ||
| 59 | return (NULL); | ||
| 60 | } while (sc != c); | ||
| 61 | } while (strncmp(s, find, len) != 0); | ||
| 62 | s--; | ||
| 63 | } | ||
| 64 | return ((char *)s); | ||
| 65 | } | ||
diff --git a/src/lib/libc/string/strtok.3 b/src/lib/libc/string/strtok.3 new file mode 100644 index 0000000000..644bd10aed --- /dev/null +++ b/src/lib/libc/string/strtok.3 | |||
| @@ -0,0 +1,112 @@ | |||
| 1 | .\" Copyright (c) 1988, 1991 The Regents of the University of California. | ||
| 2 | .\" All rights reserved. | ||
| 3 | .\" | ||
| 4 | .\" This code is derived from software contributed to Berkeley by | ||
| 5 | .\" the American National Standards Committee X3, on Information | ||
| 6 | .\" Processing Systems. | ||
| 7 | .\" | ||
| 8 | .\" Redistribution and use in source and binary forms, with or without | ||
| 9 | .\" modification, are permitted provided that the following conditions | ||
| 10 | .\" are met: | ||
| 11 | .\" 1. Redistributions of source code must retain the above copyright | ||
| 12 | .\" notice, this list of conditions and the following disclaimer. | ||
| 13 | .\" 2. Redistributions in binary form must reproduce the above copyright | ||
| 14 | .\" notice, this list of conditions and the following disclaimer in the | ||
| 15 | .\" documentation and/or other materials provided with the distribution. | ||
| 16 | .\" 3. All advertising materials mentioning features or use of this software | ||
| 17 | .\" must display the following acknowledgement: | ||
| 18 | .\" This product includes software developed by the University of | ||
| 19 | .\" California, Berkeley and its contributors. | ||
| 20 | .\" 4. Neither the name of the University nor the names of its contributors | ||
| 21 | .\" may be used to endorse or promote products derived from this software | ||
| 22 | .\" without specific prior written permission. | ||
| 23 | .\" | ||
| 24 | .\" THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND | ||
| 25 | .\" ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE | ||
| 26 | .\" IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE | ||
| 27 | .\" ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE | ||
| 28 | .\" FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL | ||
| 29 | .\" DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS | ||
| 30 | .\" OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) | ||
| 31 | .\" HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT | ||
| 32 | .\" LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY | ||
| 33 | .\" OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF | ||
| 34 | .\" SUCH DAMAGE. | ||
| 35 | .\" | ||
| 36 | .\" from: @(#)strtok.3 5.8 (Berkeley) 6/29/91 | ||
| 37 | .\" $Id: strtok.3,v 1.1.1.1 1995/10/18 08:42:23 deraadt Exp $ | ||
| 38 | .\" | ||
| 39 | .Dd June 29, 1991 | ||
| 40 | .Dt STRTOK 3 | ||
| 41 | .Os BSD 3 | ||
| 42 | .Sh NAME | ||
| 43 | .Nm strtok , | ||
| 44 | .Nm strsep | ||
| 45 | .Nd string token operations | ||
| 46 | .Sh SYNOPSIS | ||
| 47 | .Fd #include <string.h> | ||
| 48 | .Ft char * | ||
| 49 | .Fn strtok "char *str" "const char *sep" | ||
| 50 | .Sh DESCRIPTION | ||
| 51 | .Bf -symbolic | ||
| 52 | This interface is obsoleted by strsep(3). | ||
| 53 | .Ef | ||
| 54 | .Pp | ||
| 55 | The | ||
| 56 | .Fn strtok | ||
| 57 | function | ||
| 58 | is used to isolate sequential tokens in a null-terminated string, | ||
| 59 | .Fa str . | ||
| 60 | These tokens are separated in the string by at least one of the | ||
| 61 | characters in | ||
| 62 | .Fa sep . | ||
| 63 | The first time that | ||
| 64 | .Fn strtok | ||
| 65 | is called, | ||
| 66 | .Fa str | ||
| 67 | should be specified; subsequent calls, wishing to obtain further tokens | ||
| 68 | from the same string, should pass a null pointer instead. | ||
| 69 | The separator string, | ||
| 70 | .Fa sep , | ||
| 71 | must be supplied each time, and may change between calls. | ||
| 72 | .Pp | ||
| 73 | The | ||
| 74 | .Fn strtok | ||
| 75 | function | ||
| 76 | returns a pointer to the beginning of each subsequent token in the string, | ||
| 77 | after replacing the separator character itself with a | ||
| 78 | .Dv NUL | ||
| 79 | character. | ||
| 80 | When no more tokens remain, a null pointer is returned. | ||
| 81 | .Sh SEE ALSO | ||
| 82 | .Xr index 3 , | ||
| 83 | .Xr memchr 3 , | ||
| 84 | .Xr rindex 3 , | ||
| 85 | .Xr strchr 3 , | ||
| 86 | .Xr strcspn 3 , | ||
| 87 | .Xr strpbrk 3 , | ||
| 88 | .Xr strrchr 3 , | ||
| 89 | .Xr strsep 3 , | ||
| 90 | .Xr strspn 3 , | ||
| 91 | .Xr strstr 3 | ||
| 92 | .Sh STANDARDS | ||
| 93 | The | ||
| 94 | .Fn strtok | ||
| 95 | function | ||
| 96 | conforms to | ||
| 97 | .St -ansiC . | ||
| 98 | .Sh BUGS | ||
| 99 | There is no way to get tokens from multiple strings simultaneously. | ||
| 100 | .Pp | ||
| 101 | The System V | ||
| 102 | .Fn strtok , | ||
| 103 | if handed a string containing only delimiter characters, | ||
| 104 | will not alter the next starting point, so that a call to | ||
| 105 | .Fn strtok | ||
| 106 | with a different (or empty) delimiter string | ||
| 107 | may return a | ||
| 108 | .Pf non- Dv NULL | ||
| 109 | value. | ||
| 110 | Since this implementation always alters the next starting point, | ||
| 111 | such a sequence of calls would always return | ||
| 112 | .Dv NULL . | ||
diff --git a/src/lib/libc/string/strtok.c b/src/lib/libc/string/strtok.c new file mode 100644 index 0000000000..9f712579bf --- /dev/null +++ b/src/lib/libc/string/strtok.c | |||
| @@ -0,0 +1,90 @@ | |||
| 1 | /* | ||
| 2 | * Copyright (c) 1988 Regents of the University of California. | ||
| 3 | * All rights reserved. | ||
| 4 | * | ||
| 5 | * Redistribution and use in source and binary forms, with or without | ||
| 6 | * modification, are permitted provided that the following conditions | ||
| 7 | * are met: | ||
| 8 | * 1. Redistributions of source code must retain the above copyright | ||
| 9 | * notice, this list of conditions and the following disclaimer. | ||
| 10 | * 2. Redistributions in binary form must reproduce the above copyright | ||
| 11 | * notice, this list of conditions and the following disclaimer in the | ||
| 12 | * documentation and/or other materials provided with the distribution. | ||
| 13 | * 3. All advertising materials mentioning features or use of this software | ||
| 14 | * must display the following acknowledgement: | ||
| 15 | * This product includes software developed by the University of | ||
| 16 | * California, Berkeley and its contributors. | ||
| 17 | * 4. Neither the name of the University nor the names of its contributors | ||
| 18 | * may be used to endorse or promote products derived from this software | ||
| 19 | * without specific prior written permission. | ||
| 20 | * | ||
| 21 | * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND | ||
| 22 | * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE | ||
| 23 | * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE | ||
| 24 | * ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE | ||
| 25 | * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL | ||
| 26 | * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS | ||
| 27 | * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) | ||
| 28 | * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT | ||
| 29 | * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY | ||
| 30 | * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF | ||
| 31 | * SUCH DAMAGE. | ||
| 32 | */ | ||
| 33 | |||
| 34 | #if defined(LIBC_SCCS) && !defined(lint) | ||
| 35 | /*static char *sccsid = "from: @(#)strtok.c 5.8 (Berkeley) 2/24/91";*/ | ||
| 36 | static char *rcsid = "$Id: strtok.c,v 1.1.1.1 1995/10/18 08:42:23 deraadt Exp $"; | ||
| 37 | #endif /* LIBC_SCCS and not lint */ | ||
| 38 | |||
| 39 | #include <string.h> | ||
| 40 | |||
| 41 | char * | ||
| 42 | strtok(s, delim) | ||
| 43 | register char *s; | ||
| 44 | register const char *delim; | ||
| 45 | { | ||
| 46 | register char *spanp; | ||
| 47 | register int c, sc; | ||
| 48 | char *tok; | ||
| 49 | static char *last; | ||
| 50 | |||
| 51 | |||
| 52 | if (s == NULL && (s = last) == NULL) | ||
| 53 | return (NULL); | ||
| 54 | |||
| 55 | /* | ||
| 56 | * Skip (span) leading delimiters (s += strspn(s, delim), sort of). | ||
| 57 | */ | ||
| 58 | cont: | ||
| 59 | c = *s++; | ||
| 60 | for (spanp = (char *)delim; (sc = *spanp++) != 0;) { | ||
| 61 | if (c == sc) | ||
| 62 | goto cont; | ||
| 63 | } | ||
| 64 | |||
| 65 | if (c == 0) { /* no non-delimiter characters */ | ||
| 66 | last = NULL; | ||
| 67 | return (NULL); | ||
| 68 | } | ||
| 69 | tok = s - 1; | ||
| 70 | |||
| 71 | /* | ||
| 72 | * Scan token (scan for delimiters: s += strcspn(s, delim), sort of). | ||
| 73 | * Note that delim must have one NUL; we stop if we see that, too. | ||
| 74 | */ | ||
| 75 | for (;;) { | ||
| 76 | c = *s++; | ||
| 77 | spanp = (char *)delim; | ||
| 78 | do { | ||
| 79 | if ((sc = *spanp++) == c) { | ||
| 80 | if (c == 0) | ||
| 81 | s = NULL; | ||
| 82 | else | ||
| 83 | s[-1] = 0; | ||
| 84 | last = s; | ||
| 85 | return (tok); | ||
| 86 | } | ||
| 87 | } while (sc != 0); | ||
| 88 | } | ||
| 89 | /* NOTREACHED */ | ||
| 90 | } | ||
diff --git a/src/lib/libc/string/strxfrm.3 b/src/lib/libc/string/strxfrm.3 new file mode 100644 index 0000000000..84fd945472 --- /dev/null +++ b/src/lib/libc/string/strxfrm.3 | |||
| @@ -0,0 +1,69 @@ | |||
| 1 | .\" Copyright (c) 1990, 1991 The Regents of the University of California. | ||
| 2 | .\" All rights reserved. | ||
| 3 | .\" | ||
| 4 | .\" This code is derived from software contributed to Berkeley by | ||
| 5 | .\" Chris Torek and the American National Standards Committee X3, | ||
| 6 | .\" on Information Processing Systems. | ||
| 7 | .\" | ||
| 8 | .\" Redistribution and use in source and binary forms, with or without | ||
| 9 | .\" modification, are permitted provided that the following conditions | ||
| 10 | .\" are met: | ||
| 11 | .\" 1. Redistributions of source code must retain the above copyright | ||
| 12 | .\" notice, this list of conditions and the following disclaimer. | ||
| 13 | .\" 2. Redistributions in binary form must reproduce the above copyright | ||
| 14 | .\" notice, this list of conditions and the following disclaimer in the | ||
| 15 | .\" documentation and/or other materials provided with the distribution. | ||
| 16 | .\" 3. All advertising materials mentioning features or use of this software | ||
| 17 | .\" must display the following acknowledgement: | ||
| 18 | .\" This product includes software developed by the University of | ||
| 19 | .\" California, Berkeley and its contributors. | ||
| 20 | .\" 4. Neither the name of the University nor the names of its contributors | ||
| 21 | .\" may be used to endorse or promote products derived from this software | ||
| 22 | .\" without specific prior written permission. | ||
| 23 | .\" | ||
| 24 | .\" THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND | ||
| 25 | .\" ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE | ||
| 26 | .\" IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE | ||
| 27 | .\" ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE | ||
| 28 | .\" FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL | ||
| 29 | .\" DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS | ||
| 30 | .\" OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) | ||
| 31 | .\" HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT | ||
| 32 | .\" LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY | ||
| 33 | .\" OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF | ||
| 34 | .\" SUCH DAMAGE. | ||
| 35 | .\" | ||
| 36 | .\" from: @(#)strxfrm.3 5.4 (Berkeley) 6/29/91 | ||
| 37 | .\" $Id: strxfrm.3,v 1.1.1.1 1995/10/18 08:42:23 deraadt Exp $ | ||
| 38 | .\" | ||
| 39 | .Dd June 29, 1991 | ||
| 40 | .Dt STRXFRM 3 | ||
| 41 | .Os | ||
| 42 | .Sh NAME | ||
| 43 | .Nm strxfrm | ||
| 44 | .Nd transform a string under locale | ||
| 45 | .Sh SYNOPSIS | ||
| 46 | .Fd #include <string.h> | ||
| 47 | .Ft size_t | ||
| 48 | .Fn strxfrm "char *dst" "const char *src" "size_t n" | ||
| 49 | .Sh DESCRIPTION | ||
| 50 | The | ||
| 51 | .Fn strxfrm | ||
| 52 | function | ||
| 53 | does something horrible (see | ||
| 54 | .Tn ANSI | ||
| 55 | standard). | ||
| 56 | In this implementation it just copies. | ||
| 57 | .Sh SEE ALSO | ||
| 58 | .Xr bcmp 3 , | ||
| 59 | .Xr memcmp 3 , | ||
| 60 | .\" .Xr setlocale 3 , | ||
| 61 | .Xr strcasecmp 3 , | ||
| 62 | .Xr strcmp 3 , | ||
| 63 | .Xr strcoll 3 | ||
| 64 | .Sh STANDARDS | ||
| 65 | The | ||
| 66 | .Fn strxfrm | ||
| 67 | function | ||
| 68 | conforms to | ||
| 69 | .St -ansiC . | ||
diff --git a/src/lib/libc/string/strxfrm.c b/src/lib/libc/string/strxfrm.c new file mode 100644 index 0000000000..d9df77b957 --- /dev/null +++ b/src/lib/libc/string/strxfrm.c | |||
| @@ -0,0 +1,74 @@ | |||
| 1 | /*- | ||
| 2 | * Copyright (c) 1990 The Regents of the University of California. | ||
| 3 | * All rights reserved. | ||
| 4 | * | ||
| 5 | * This code is derived from software contributed to Berkeley by | ||
| 6 | * Chris Torek. | ||
| 7 | * | ||
| 8 | * Redistribution and use in source and binary forms, with or without | ||
| 9 | * modification, are permitted provided that the following conditions | ||
| 10 | * are met: | ||
| 11 | * 1. Redistributions of source code must retain the above copyright | ||
| 12 | * notice, this list of conditions and the following disclaimer. | ||
| 13 | * 2. Redistributions in binary form must reproduce the above copyright | ||
| 14 | * notice, this list of conditions and the following disclaimer in the | ||
| 15 | * documentation and/or other materials provided with the distribution. | ||
| 16 | * 3. All advertising materials mentioning features or use of this software | ||
| 17 | * must display the following acknowledgement: | ||
| 18 | * This product includes software developed by the University of | ||
| 19 | * California, Berkeley and its contributors. | ||
| 20 | * 4. Neither the name of the University nor the names of its contributors | ||
| 21 | * may be used to endorse or promote products derived from this software | ||
| 22 | * without specific prior written permission. | ||
| 23 | * | ||
| 24 | * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND | ||
| 25 | * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE | ||
| 26 | * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE | ||
| 27 | * ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE | ||
| 28 | * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL | ||
| 29 | * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS | ||
| 30 | * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) | ||
| 31 | * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT | ||
| 32 | * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY | ||
| 33 | * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF | ||
| 34 | * SUCH DAMAGE. | ||
| 35 | */ | ||
| 36 | |||
| 37 | #if defined(LIBC_SCCS) && !defined(lint) | ||
| 38 | /*static char *sccsid = "from: @(#)strxfrm.c 5.2 (Berkeley) 1/26/91";*/ | ||
| 39 | static char *rcsid = "$Id: strxfrm.c,v 1.1.1.1 1995/10/18 08:42:23 deraadt Exp $"; | ||
| 40 | #endif /* LIBC_SCCS and not lint */ | ||
| 41 | |||
| 42 | #include <string.h> | ||
| 43 | |||
| 44 | /* | ||
| 45 | * Transform src, storing the result in dst, such that | ||
| 46 | * strcmp() on transformed strings returns what strcoll() | ||
| 47 | * on the original untransformed strings would return. | ||
| 48 | */ | ||
| 49 | size_t | ||
| 50 | strxfrm(dst, src, n) | ||
| 51 | register char *dst; | ||
| 52 | register const char *src; | ||
| 53 | register size_t n; | ||
| 54 | { | ||
| 55 | register size_t r = 0; | ||
| 56 | register int c; | ||
| 57 | |||
| 58 | /* | ||
| 59 | * Since locales are unimplemented, this is just a copy. | ||
| 60 | */ | ||
| 61 | if (n != 0) { | ||
| 62 | while ((c = *src++) != 0) { | ||
| 63 | r++; | ||
| 64 | if (--n == 0) { | ||
| 65 | while (*src++ != 0) | ||
| 66 | r++; | ||
| 67 | break; | ||
| 68 | } | ||
| 69 | *dst++ = c; | ||
| 70 | } | ||
| 71 | *dst = 0; | ||
| 72 | } | ||
| 73 | return (r); | ||
| 74 | } | ||
diff --git a/src/lib/libc/string/swab.3 b/src/lib/libc/string/swab.3 new file mode 100644 index 0000000000..133c487bbd --- /dev/null +++ b/src/lib/libc/string/swab.3 | |||
| @@ -0,0 +1,66 @@ | |||
| 1 | .\" Copyright (c) 1990, 1991 The Regents of the University of California. | ||
| 2 | .\" All rights reserved. | ||
| 3 | .\" | ||
| 4 | .\" Redistribution and use in source and binary forms, with or without | ||
| 5 | .\" modification, are permitted provided that the following conditions | ||
| 6 | .\" are met: | ||
| 7 | .\" 1. Redistributions of source code must retain the above copyright | ||
| 8 | .\" notice, this list of conditions and the following disclaimer. | ||
| 9 | .\" 2. Redistributions in binary form must reproduce the above copyright | ||
| 10 | .\" notice, this list of conditions and the following disclaimer in the | ||
| 11 | .\" documentation and/or other materials provided with the distribution. | ||
| 12 | .\" 3. All advertising materials mentioning features or use of this software | ||
| 13 | .\" must display the following acknowledgement: | ||
| 14 | .\" This product includes software developed by the University of | ||
| 15 | .\" California, Berkeley and its contributors. | ||
| 16 | .\" 4. Neither the name of the University nor the names of its contributors | ||
| 17 | .\" may be used to endorse or promote products derived from this software | ||
| 18 | .\" without specific prior written permission. | ||
| 19 | .\" | ||
| 20 | .\" THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND | ||
| 21 | .\" ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE | ||
| 22 | .\" IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE | ||
| 23 | .\" ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE | ||
| 24 | .\" FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL | ||
| 25 | .\" DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS | ||
| 26 | .\" OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) | ||
| 27 | .\" HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT | ||
| 28 | .\" LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY | ||
| 29 | .\" OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF | ||
| 30 | .\" SUCH DAMAGE. | ||
| 31 | .\" | ||
| 32 | .\" from: @(#)swab.3 6.6 (Berkeley) 5/1/91 | ||
| 33 | .\" $Id: swab.3,v 1.1.1.1 1995/10/18 08:42:23 deraadt Exp $ | ||
| 34 | .\" | ||
| 35 | .Dd May 1, 1991 | ||
| 36 | .Dt SWAB 3 | ||
| 37 | .Os | ||
| 38 | .Sh NAME | ||
| 39 | .Nm swab | ||
| 40 | .Nd swap adjacent bytes | ||
| 41 | .Sh SYNOPSIS | ||
| 42 | .Fd #include <string.h> | ||
| 43 | .Ft void | ||
| 44 | .Fn swab "const void *src" "void *dst" "size_t len" | ||
| 45 | .Sh DESCRIPTION | ||
| 46 | The function | ||
| 47 | .Fn swab | ||
| 48 | copies | ||
| 49 | .Fa len | ||
| 50 | bytes from the location referenced by | ||
| 51 | .Fa src | ||
| 52 | to the location referenced by | ||
| 53 | .Fa dst , | ||
| 54 | swapping adjacent bytes. | ||
| 55 | .Pp | ||
| 56 | The argument | ||
| 57 | .Fa len | ||
| 58 | must be even number. | ||
| 59 | .Sh SEE ALSO | ||
| 60 | .Xr bzero 3 , | ||
| 61 | .Xr memset 3 | ||
| 62 | .Sh HISTORY | ||
| 63 | A | ||
| 64 | .Fn swab | ||
| 65 | function appeared in | ||
| 66 | .At v7 . | ||
diff --git a/src/lib/libc/string/swab.c b/src/lib/libc/string/swab.c new file mode 100644 index 0000000000..f33fc53bd6 --- /dev/null +++ b/src/lib/libc/string/swab.c | |||
| @@ -0,0 +1,66 @@ | |||
| 1 | /* | ||
| 2 | * Copyright (c) 1988 Regents of the University of California. | ||
| 3 | * All rights reserved. | ||
| 4 | * | ||
| 5 | * This code is derived from software contributed to Berkeley by | ||
| 6 | * Jeffrey Mogul. | ||
| 7 | * | ||
| 8 | * Redistribution and use in source and binary forms, with or without | ||
| 9 | * modification, are permitted provided that the following conditions | ||
| 10 | * are met: | ||
| 11 | * 1. Redistributions of source code must retain the above copyright | ||
| 12 | * notice, this list of conditions and the following disclaimer. | ||
| 13 | * 2. Redistributions in binary form must reproduce the above copyright | ||
| 14 | * notice, this list of conditions and the following disclaimer in the | ||
| 15 | * documentation and/or other materials provided with the distribution. | ||
| 16 | * 3. All advertising materials mentioning features or use of this software | ||
| 17 | * must display the following acknowledgement: | ||
| 18 | * This product includes software developed by the University of | ||
| 19 | * California, Berkeley and its contributors. | ||
| 20 | * 4. Neither the name of the University nor the names of its contributors | ||
| 21 | * may be used to endorse or promote products derived from this software | ||
| 22 | * without specific prior written permission. | ||
| 23 | * | ||
| 24 | * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND | ||
| 25 | * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE | ||
| 26 | * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE | ||
| 27 | * ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE | ||
| 28 | * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL | ||
| 29 | * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS | ||
| 30 | * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) | ||
| 31 | * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT | ||
| 32 | * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY | ||
| 33 | * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF | ||
| 34 | * SUCH DAMAGE. | ||
| 35 | */ | ||
| 36 | |||
| 37 | #if defined(LIBC_SCCS) && !defined(lint) | ||
| 38 | /*static char *sccsid = "from: @(#)swab.c 5.10 (Berkeley) 3/6/91";*/ | ||
| 39 | static char *rcsid = "$Id: swab.c,v 1.1.1.1 1995/10/18 08:42:23 deraadt Exp $"; | ||
| 40 | #endif /* LIBC_SCCS and not lint */ | ||
| 41 | |||
| 42 | #include <string.h> | ||
| 43 | |||
| 44 | void | ||
| 45 | swab(from, to, len) | ||
| 46 | const void *from; | ||
| 47 | void *to; | ||
| 48 | size_t len; | ||
| 49 | { | ||
| 50 | register unsigned long temp; | ||
| 51 | register int n; | ||
| 52 | register char *fp, *tp; | ||
| 53 | |||
| 54 | n = (len >> 1) + 1; | ||
| 55 | fp = (char *)from; | ||
| 56 | tp = (char *)to; | ||
| 57 | #define STEP temp = *fp++,*tp++ = *fp++,*tp++ = temp | ||
| 58 | /* round to multiple of 8 */ | ||
| 59 | while ((--n) & 07) | ||
| 60 | STEP; | ||
| 61 | n >>= 3; | ||
| 62 | while (--n >= 0) { | ||
| 63 | STEP; STEP; STEP; STEP; | ||
| 64 | STEP; STEP; STEP; STEP; | ||
| 65 | } | ||
| 66 | } | ||
