summaryrefslogtreecommitdiff
path: root/src/lib/libc
diff options
context:
space:
mode:
authorcvs2svn <admin@example.com>2025-04-14 17:32:06 +0000
committercvs2svn <admin@example.com>2025-04-14 17:32:06 +0000
commiteb8dd9dca1228af0cd132f515509051ecfabf6f6 (patch)
treeedb6da6af7e865d488dc1a29309f1e1ec226e603 /src/lib/libc
parent247f0352e0ed72a4f476db9dc91f4d982bc83eb2 (diff)
downloadopenbsd-eb8dd9dca1228af0cd132f515509051ecfabf6f6.tar.gz
openbsd-eb8dd9dca1228af0cd132f515509051ecfabf6f6.tar.bz2
openbsd-eb8dd9dca1228af0cd132f515509051ecfabf6f6.zip
This commit was manufactured by cvs2git to create tag 'tb_20250414'.tb_20250414
Diffstat (limited to 'src/lib/libc')
-rw-r--r--src/lib/libc/crypt/Makefile.inc8
-rw-r--r--src/lib/libc/crypt/arc4random.3116
-rw-r--r--src/lib/libc/crypt/arc4random.c204
-rw-r--r--src/lib/libc/crypt/arc4random.h61
-rw-r--r--src/lib/libc/crypt/arc4random_uniform.c57
-rw-r--r--src/lib/libc/crypt/bcrypt.c397
-rw-r--r--src/lib/libc/crypt/blowfish.3103
-rw-r--r--src/lib/libc/crypt/blowfish.c688
-rw-r--r--src/lib/libc/crypt/chacha_private.h222
-rw-r--r--src/lib/libc/crypt/crypt.3144
-rw-r--r--src/lib/libc/crypt/crypt.c22
-rw-r--r--src/lib/libc/crypt/crypt_checkpass.3113
-rw-r--r--src/lib/libc/crypt/cryptutil.c97
-rw-r--r--src/lib/libc/include/DETAILS133
-rw-r--r--src/lib/libc/include/README110
-rw-r--r--src/lib/libc/include/atfork.h45
-rw-r--r--src/lib/libc/include/cancel.h78
-rw-r--r--src/lib/libc/include/ctype_private.h9
-rw-r--r--src/lib/libc/include/localedef.h106
-rw-r--r--src/lib/libc/include/mpool.h122
-rw-r--r--src/lib/libc/include/namespace.h98
-rw-r--r--src/lib/libc/include/thread_private.h427
-rw-r--r--src/lib/libc/net/Makefile.inc31
-rw-r--r--src/lib/libc/net/base64.c307
-rw-r--r--src/lib/libc/net/ether_aton.3114
-rw-r--r--src/lib/libc/net/ethers.c183
-rw-r--r--src/lib/libc/net/freeaddrinfo.c50
-rw-r--r--src/lib/libc/net/gai_strerror.395
-rw-r--r--src/lib/libc/net/gai_strerror.c79
-rw-r--r--src/lib/libc/net/getaddrinfo.3477
-rw-r--r--src/lib/libc/net/gethostbyname.3271
-rw-r--r--src/lib/libc/net/getifaddrs.3157
-rw-r--r--src/lib/libc/net/getifaddrs.c298
-rw-r--r--src/lib/libc/net/getnameinfo.3264
-rw-r--r--src/lib/libc/net/getnetent.3147
-rw-r--r--src/lib/libc/net/getnetent.c49
-rw-r--r--src/lib/libc/net/getpeereid.3121
-rw-r--r--src/lib/libc/net/getpeereid.c36
-rw-r--r--src/lib/libc/net/getproto.c60
-rw-r--r--src/lib/libc/net/getprotoent.3213
-rw-r--r--src/lib/libc/net/getprotoent.c169
-rw-r--r--src/lib/libc/net/getprotoname.c69
-rw-r--r--src/lib/libc/net/getrrsetbyname.3164
-rw-r--r--src/lib/libc/net/getservbyname.c72
-rw-r--r--src/lib/libc/net/getservbyport.c65
-rw-r--r--src/lib/libc/net/getservent.3220
-rw-r--r--src/lib/libc/net/getservent.c171
-rw-r--r--src/lib/libc/net/herror.c106
-rw-r--r--src/lib/libc/net/htobe64.3164
-rw-r--r--src/lib/libc/net/htonl.3102
-rw-r--r--src/lib/libc/net/htonl.c15
-rw-r--r--src/lib/libc/net/htons.c15
-rw-r--r--src/lib/libc/net/if_indextoname.3143
-rw-r--r--src/lib/libc/net/if_indextoname.c78
-rw-r--r--src/lib/libc/net/if_nameindex.c145
-rw-r--r--src/lib/libc/net/if_nametoindex.c77
-rw-r--r--src/lib/libc/net/inet6_opt_init.3328
-rw-r--r--src/lib/libc/net/inet6_rth_space.3220
-rw-r--r--src/lib/libc/net/inet_addr.3195
-rw-r--r--src/lib/libc/net/inet_addr.c175
-rw-r--r--src/lib/libc/net/inet_lnaof.389
-rw-r--r--src/lib/libc/net/inet_lnaof.c50
-rw-r--r--src/lib/libc/net/inet_makeaddr.c53
-rw-r--r--src/lib/libc/net/inet_net_ntop.3195
-rw-r--r--src/lib/libc/net/inet_net_ntop.c160
-rw-r--r--src/lib/libc/net/inet_net_pton.c246
-rw-r--r--src/lib/libc/net/inet_neta.c80
-rw-r--r--src/lib/libc/net/inet_netof.c49
-rw-r--r--src/lib/libc/net/inet_network.c85
-rw-r--r--src/lib/libc/net/inet_ntoa.c51
-rw-r--r--src/lib/libc/net/inet_ntop.3213
-rw-r--r--src/lib/libc/net/inet_ntop.c205
-rw-r--r--src/lib/libc/net/inet_pton.c213
-rw-r--r--src/lib/libc/net/ip6opt.c285
-rw-r--r--src/lib/libc/net/link_ntoa.378
-rw-r--r--src/lib/libc/net/linkaddr.c85
-rw-r--r--src/lib/libc/net/ntohl.c15
-rw-r--r--src/lib/libc/net/ntohs.c15
-rw-r--r--src/lib/libc/net/rcmd.3232
-rw-r--r--src/lib/libc/net/rcmd.c296
-rw-r--r--src/lib/libc/net/rcmdsh.395
-rw-r--r--src/lib/libc/net/rcmdsh.c194
-rw-r--r--src/lib/libc/net/recv.c41
-rw-r--r--src/lib/libc/net/res_comp.c466
-rw-r--r--src/lib/libc/net/res_data.c104
-rw-r--r--src/lib/libc/net/res_debug_syms.c192
-rw-r--r--src/lib/libc/net/res_init.3441
-rw-r--r--src/lib/libc/net/res_random.c281
-rw-r--r--src/lib/libc/net/rresvport.c107
-rw-r--r--src/lib/libc/net/rthdr.c198
-rw-r--r--src/lib/libc/net/ruserok.c392
-rw-r--r--src/lib/libc/net/send.c41
-rw-r--r--src/lib/libc/net/sockatmark.3122
-rw-r--r--src/lib/libc/net/sockatmark.c40
-rw-r--r--src/lib/libc/net/vars6.c42
-rw-r--r--src/lib/libc/stdlib/Makefile.inc33
-rw-r--r--src/lib/libc/stdlib/_Exit.c22
-rw-r--r--src/lib/libc/stdlib/__mktemp4.c83
-rw-r--r--src/lib/libc/stdlib/_rand48.c47
-rw-r--r--src/lib/libc/stdlib/a64l.3133
-rw-r--r--src/lib/libc/stdlib/a64l.c42
-rw-r--r--src/lib/libc/stdlib/abort.377
-rw-r--r--src/lib/libc/stdlib/abort.c64
-rw-r--r--src/lib/libc/stdlib/abs.375
-rw-r--r--src/lib/libc/stdlib/abs.c38
-rw-r--r--src/lib/libc/stdlib/alloca.374
-rw-r--r--src/lib/libc/stdlib/atexit.397
-rw-r--r--src/lib/libc/stdlib/atexit.c256
-rw-r--r--src/lib/libc/stdlib/atexit.h61
-rw-r--r--src/lib/libc/stdlib/atof.381
-rw-r--r--src/lib/libc/stdlib/atof.c37
-rw-r--r--src/lib/libc/stdlib/atoi.389
-rw-r--r--src/lib/libc/stdlib/atoi.c38
-rw-r--r--src/lib/libc/stdlib/atol.368
-rw-r--r--src/lib/libc/stdlib/atol.c37
-rw-r--r--src/lib/libc/stdlib/atoll.368
-rw-r--r--src/lib/libc/stdlib/atoll.c37
-rw-r--r--src/lib/libc/stdlib/bsearch.384
-rw-r--r--src/lib/libc/stdlib/bsearch.c69
-rw-r--r--src/lib/libc/stdlib/div.363
-rw-r--r--src/lib/libc/stdlib/div.c72
-rw-r--r--src/lib/libc/stdlib/drand48.c30
-rw-r--r--src/lib/libc/stdlib/ecvt.3166
-rw-r--r--src/lib/libc/stdlib/ecvt.c104
-rw-r--r--src/lib/libc/stdlib/erand48.c26
-rw-r--r--src/lib/libc/stdlib/exit.3102
-rw-r--r--src/lib/libc/stdlib/exit.c57
-rw-r--r--src/lib/libc/stdlib/gcvt.c123
-rw-r--r--src/lib/libc/stdlib/getenv.3191
-rw-r--r--src/lib/libc/stdlib/getenv.c78
-rw-r--r--src/lib/libc/stdlib/getopt.3365
-rw-r--r--src/lib/libc/stdlib/getopt_long.3460
-rw-r--r--src/lib/libc/stdlib/getopt_long.c513
-rw-r--r--src/lib/libc/stdlib/getsubopt.3162
-rw-r--r--src/lib/libc/stdlib/getsubopt.c92
-rw-r--r--src/lib/libc/stdlib/hcreate.3234
-rw-r--r--src/lib/libc/stdlib/hcreate.c189
-rw-r--r--src/lib/libc/stdlib/heapsort.c175
-rw-r--r--src/lib/libc/stdlib/icdb.c391
-rw-r--r--src/lib/libc/stdlib/icdb_new.368
-rw-r--r--src/lib/libc/stdlib/imaxabs.370
-rw-r--r--src/lib/libc/stdlib/imaxabs.c38
-rw-r--r--src/lib/libc/stdlib/imaxdiv.365
-rw-r--r--src/lib/libc/stdlib/imaxdiv.c50
-rw-r--r--src/lib/libc/stdlib/insque.3103
-rw-r--r--src/lib/libc/stdlib/insque.c54
-rw-r--r--src/lib/libc/stdlib/jrand48.c22
-rw-r--r--src/lib/libc/stdlib/l64a.c42
-rw-r--r--src/lib/libc/stdlib/labs.388
-rw-r--r--src/lib/libc/stdlib/labs.c37
-rw-r--r--src/lib/libc/stdlib/lcong48.c36
-rw-r--r--src/lib/libc/stdlib/ldiv.371
-rw-r--r--src/lib/libc/stdlib/ldiv.c50
-rw-r--r--src/lib/libc/stdlib/llabs.c40
-rw-r--r--src/lib/libc/stdlib/lldiv.373
-rw-r--r--src/lib/libc/stdlib/lldiv.c52
-rw-r--r--src/lib/libc/stdlib/lrand48.c24
-rw-r--r--src/lib/libc/stdlib/lsearch.3106
-rw-r--r--src/lib/libc/stdlib/lsearch.c70
-rw-r--r--src/lib/libc/stdlib/malloc.3860
-rw-r--r--src/lib/libc/stdlib/malloc.c2781
-rw-r--r--src/lib/libc/stdlib/merge.c336
-rw-r--r--src/lib/libc/stdlib/mkdtemp.c41
-rw-r--r--src/lib/libc/stdlib/mkstemp.c64
-rw-r--r--src/lib/libc/stdlib/mktemp.3431
-rw-r--r--src/lib/libc/stdlib/mktemp.c48
-rw-r--r--src/lib/libc/stdlib/mrand48.c24
-rw-r--r--src/lib/libc/stdlib/nrand48.c22
-rw-r--r--src/lib/libc/stdlib/posix_memalign.397
-rw-r--r--src/lib/libc/stdlib/posix_openpt.3102
-rw-r--r--src/lib/libc/stdlib/posix_pty.c120
-rw-r--r--src/lib/libc/stdlib/ptsname.3160
-rw-r--r--src/lib/libc/stdlib/qsort.3276
-rw-r--r--src/lib/libc/stdlib/qsort.c238
-rw-r--r--src/lib/libc/stdlib/radixsort.3151
-rw-r--r--src/lib/libc/stdlib/radixsort.c294
-rw-r--r--src/lib/libc/stdlib/rand.3135
-rw-r--r--src/lib/libc/stdlib/rand.c74
-rw-r--r--src/lib/libc/stdlib/rand48.3240
-rw-r--r--src/lib/libc/stdlib/rand48.h38
-rw-r--r--src/lib/libc/stdlib/random.3197
-rw-r--r--src/lib/libc/stdlib/random.c419
-rw-r--r--src/lib/libc/stdlib/reallocarray.c39
-rw-r--r--src/lib/libc/stdlib/realpath.3165
-rw-r--r--src/lib/libc/stdlib/realpath.c42
-rw-r--r--src/lib/libc/stdlib/recallocarray.c81
-rw-r--r--src/lib/libc/stdlib/remque.c48
-rw-r--r--src/lib/libc/stdlib/seed48.c45
-rw-r--r--src/lib/libc/stdlib/setenv.c185
-rw-r--r--src/lib/libc/stdlib/srand48.c38
-rw-r--r--src/lib/libc/stdlib/strtod.3176
-rw-r--r--src/lib/libc/stdlib/strtoimax.c151
-rw-r--r--src/lib/libc/stdlib/strtol.3273
-rw-r--r--src/lib/libc/stdlib/strtol.c151
-rw-r--r--src/lib/libc/stdlib/strtoll.c156
-rw-r--r--src/lib/libc/stdlib/strtonum.3152
-rw-r--r--src/lib/libc/stdlib/strtonum.c66
-rw-r--r--src/lib/libc/stdlib/strtoul.3260
-rw-r--r--src/lib/libc/stdlib/strtoul.c110
-rw-r--r--src/lib/libc/stdlib/strtoull.c114
-rw-r--r--src/lib/libc/stdlib/strtoumax.c109
-rw-r--r--src/lib/libc/stdlib/system.3116
-rw-r--r--src/lib/libc/stdlib/system.c89
-rw-r--r--src/lib/libc/stdlib/tfind.c40
-rw-r--r--src/lib/libc/stdlib/thread_atexit.c44
-rw-r--r--src/lib/libc/stdlib/tsearch.3126
-rw-r--r--src/lib/libc/stdlib/tsearch.c118
-rw-r--r--src/lib/libc/string/Makefile.inc35
-rw-r--r--src/lib/libc/string/bcmp.367
-rw-r--r--src/lib/libc/string/bcmp.c52
-rw-r--r--src/lib/libc/string/bcopy.367
-rw-r--r--src/lib/libc/string/bcopy.c113
-rw-r--r--src/lib/libc/string/bzero.392
-rw-r--r--src/lib/libc/string/bzero.c45
-rw-r--r--src/lib/libc/string/explicit_bzero.c20
-rw-r--r--src/lib/libc/string/ffs.361
-rw-r--r--src/lib/libc/string/ffs.c40
-rw-r--r--src/lib/libc/string/memccpy.381
-rw-r--r--src/lib/libc/string/memccpy.c49
-rw-r--r--src/lib/libc/string/memchr.3102
-rw-r--r--src/lib/libc/string/memchr.c49
-rw-r--r--src/lib/libc/string/memcmp.386
-rw-r--r--src/lib/libc/string/memcmp.c52
-rw-r--r--src/lib/libc/string/memcpy.379
-rw-r--r--src/lib/libc/string/memcpy.c109
-rw-r--r--src/lib/libc/string/memmem.377
-rw-r--r--src/lib/libc/string/memmem.c184
-rw-r--r--src/lib/libc/string/memmove.376
-rw-r--r--src/lib/libc/string/memmove.c113
-rw-r--r--src/lib/libc/string/memrchr.c39
-rw-r--r--src/lib/libc/string/memset.375
-rw-r--r--src/lib/libc/string/memset.c48
-rw-r--r--src/lib/libc/string/stpcpy.3184
-rw-r--r--src/lib/libc/string/stpcpy.c44
-rw-r--r--src/lib/libc/string/stpncpy.c57
-rw-r--r--src/lib/libc/string/strcasecmp.3128
-rw-r--r--src/lib/libc/string/strcasecmp.c107
-rw-r--r--src/lib/libc/string/strcasecmp_l.c21
-rw-r--r--src/lib/libc/string/strcasestr.c61
-rw-r--r--src/lib/libc/string/strcat.376
-rw-r--r--src/lib/libc/string/strcat.c47
-rw-r--r--src/lib/libc/string/strchr.3116
-rw-r--r--src/lib/libc/string/strchr.c46
-rw-r--r--src/lib/libc/string/strcmp.395
-rw-r--r--src/lib/libc/string/strcmp.c48
-rw-r--r--src/lib/libc/string/strcoll.3103
-rw-r--r--src/lib/libc/string/strcoll.c45
-rw-r--r--src/lib/libc/string/strcoll_l.c14
-rw-r--r--src/lib/libc/string/strcpy.382
-rw-r--r--src/lib/libc/string/strcpy.c46
-rw-r--r--src/lib/libc/string/strcspn.3108
-rw-r--r--src/lib/libc/string/strcspn.c59
-rw-r--r--src/lib/libc/string/strdup.3119
-rw-r--r--src/lib/libc/string/strdup.c50
-rw-r--r--src/lib/libc/string/strerror.3154
-rw-r--r--src/lib/libc/string/strerror.c42
-rw-r--r--src/lib/libc/string/strerror_l.c33
-rw-r--r--src/lib/libc/string/strerror_r.c107
-rw-r--r--src/lib/libc/string/strlcat.c56
-rw-r--r--src/lib/libc/string/strlcpy.3195
-rw-r--r--src/lib/libc/string/strlcpy.c51
-rw-r--r--src/lib/libc/string/strlen.3103
-rw-r--r--src/lib/libc/string/strlen.c44
-rw-r--r--src/lib/libc/string/strmode.3153
-rw-r--r--src/lib/libc/string/strmode.c139
-rw-r--r--src/lib/libc/string/strncat.3132
-rw-r--r--src/lib/libc/string/strncat.c58
-rw-r--r--src/lib/libc/string/strncmp.c48
-rw-r--r--src/lib/libc/string/strncpy.3138
-rw-r--r--src/lib/libc/string/strncpy.c59
-rw-r--r--src/lib/libc/string/strndup.c40
-rw-r--r--src/lib/libc/string/strnlen.c33
-rw-r--r--src/lib/libc/string/strpbrk.380
-rw-r--r--src/lib/libc/string/strpbrk.c49
-rw-r--r--src/lib/libc/string/strrchr.3118
-rw-r--r--src/lib/libc/string/strrchr.c48
-rw-r--r--src/lib/libc/string/strsep.3110
-rw-r--r--src/lib/libc/string/strsep.c71
-rw-r--r--src/lib/libc/string/strsignal.378
-rw-r--r--src/lib/libc/string/strsignal.c40
-rw-r--r--src/lib/libc/string/strspn.392
-rw-r--r--src/lib/libc/string/strspn.c52
-rw-r--r--src/lib/libc/string/strstr.3101
-rw-r--r--src/lib/libc/string/strstr.c189
-rw-r--r--src/lib/libc/string/strtok.3169
-rw-r--r--src/lib/libc/string/strtok.c87
-rw-r--r--src/lib/libc/string/strxfrm.3105
-rw-r--r--src/lib/libc/string/strxfrm.c52
-rw-r--r--src/lib/libc/string/strxfrm_l.c14
-rw-r--r--src/lib/libc/string/swab.379
-rw-r--r--src/lib/libc/string/swab.c35
-rw-r--r--src/lib/libc/string/timingsafe_bcmp.387
-rw-r--r--src/lib/libc/string/timingsafe_bcmp.c30
-rw-r--r--src/lib/libc/string/timingsafe_memcmp.c47
-rw-r--r--src/lib/libc/string/wcscasecmp.3134
-rw-r--r--src/lib/libc/string/wcscasecmp.c63
-rw-r--r--src/lib/libc/string/wcscasecmp_l.c63
-rw-r--r--src/lib/libc/string/wcscat.3115
-rw-r--r--src/lib/libc/string/wcscat.c56
-rw-r--r--src/lib/libc/string/wcschr.385
-rw-r--r--src/lib/libc/string/wcschr.c50
-rw-r--r--src/lib/libc/string/wcscmp.392
-rw-r--r--src/lib/libc/string/wcscmp.c52
-rw-r--r--src/lib/libc/string/wcscpy.3129
-rw-r--r--src/lib/libc/string/wcscpy.c52
-rw-r--r--src/lib/libc/string/wcscspn.383
-rw-r--r--src/lib/libc/string/wcscspn.c54
-rw-r--r--src/lib/libc/string/wcsdup.397
-rw-r--r--src/lib/libc/string/wcsdup.c32
-rw-r--r--src/lib/libc/string/wcslcat.c56
-rw-r--r--src/lib/libc/string/wcslcpy.3160
-rw-r--r--src/lib/libc/string/wcslcpy.c51
-rw-r--r--src/lib/libc/string/wcslen.3102
-rw-r--r--src/lib/libc/string/wcslen.c45
-rw-r--r--src/lib/libc/string/wcsncat.c53
-rw-r--r--src/lib/libc/string/wcsncmp.c53
-rw-r--r--src/lib/libc/string/wcsncpy.c51
-rw-r--r--src/lib/libc/string/wcsnlen.c45
-rw-r--r--src/lib/libc/string/wcspbrk.381
-rw-r--r--src/lib/libc/string/wcspbrk.c53
-rw-r--r--src/lib/libc/string/wcsrchr.385
-rw-r--r--src/lib/libc/string/wcsrchr.c50
-rw-r--r--src/lib/libc/string/wcsspn.379
-rw-r--r--src/lib/libc/string/wcsspn.c56
-rw-r--r--src/lib/libc/string/wcsstr.388
-rw-r--r--src/lib/libc/string/wcsstr.c71
-rw-r--r--src/lib/libc/string/wcstok.3151
-rw-r--r--src/lib/libc/string/wcstok.c93
-rw-r--r--src/lib/libc/string/wcswcs.c5
-rw-r--r--src/lib/libc/string/wcswidth.369
-rw-r--r--src/lib/libc/string/wcswidth.c51
-rw-r--r--src/lib/libc/string/wmemchr.381
-rw-r--r--src/lib/libc/string/wmemchr.c47
-rw-r--r--src/lib/libc/string/wmemcmp.378
-rw-r--r--src/lib/libc/string/wmemcmp.c51
-rw-r--r--src/lib/libc/string/wmemcpy.379
-rw-r--r--src/lib/libc/string/wmemcpy.c41
-rw-r--r--src/lib/libc/string/wmemmove.378
-rw-r--r--src/lib/libc/string/wmemmove.c41
-rw-r--r--src/lib/libc/string/wmemset.373
-rw-r--r--src/lib/libc/string/wmemset.c47
341 files changed, 0 insertions, 41066 deletions
diff --git a/src/lib/libc/crypt/Makefile.inc b/src/lib/libc/crypt/Makefile.inc
deleted file mode 100644
index e9263b09fe..0000000000
--- a/src/lib/libc/crypt/Makefile.inc
+++ /dev/null
@@ -1,8 +0,0 @@
1# $OpenBSD: Makefile.inc,v 1.27 2016/03/30 06:38:41 jmc Exp $
2
3.PATH: ${LIBCSRCDIR}/arch/${MACHINE_CPU}/crypt ${LIBCSRCDIR}/crypt
4
5SRCS+= crypt.c cryptutil.c arc4random.c arc4random_uniform.c \
6 blowfish.c bcrypt.c
7
8MAN+= crypt.3 crypt_checkpass.3 blowfish.3 arc4random.3
diff --git a/src/lib/libc/crypt/arc4random.3 b/src/lib/libc/crypt/arc4random.3
deleted file mode 100644
index 411860c28f..0000000000
--- a/src/lib/libc/crypt/arc4random.3
+++ /dev/null
@@ -1,116 +0,0 @@
1.\" $OpenBSD: arc4random.3,v 1.37 2019/09/29 16:30:35 jmc Exp $
2.\"
3.\" Copyright 1997 Niels Provos <provos@physnet.uni-hamburg.de>
4.\" 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 Niels Provos.
17.\" 4. The name of the author may not be used to endorse or promote products
18.\" derived from this software without specific prior written permission.
19.\"
20.\" THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR
21.\" IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
22.\" OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
23.\" IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT,
24.\" INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
25.\" NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
26.\" DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
27.\" THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
28.\" (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF
29.\" THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
30.\"
31.\" Manual page, using -mandoc macros
32.\"
33.Dd $Mdocdate: September 29 2019 $
34.Dt ARC4RANDOM 3
35.Os
36.Sh NAME
37.Nm arc4random ,
38.Nm arc4random_buf ,
39.Nm arc4random_uniform
40.Nd random number generator
41.Sh SYNOPSIS
42.In stdlib.h
43.Ft uint32_t
44.Fn arc4random "void"
45.Ft void
46.Fn arc4random_buf "void *buf" "size_t nbytes"
47.Ft uint32_t
48.Fn arc4random_uniform "uint32_t upper_bound"
49.Sh DESCRIPTION
50This family of functions provides higher quality data than those
51described in
52.Xr rand 3 ,
53.Xr random 3 ,
54and
55.Xr rand48 3 .
56.Pp
57Use of these functions is encouraged for almost all random number
58consumption because the other interfaces are deficient in either
59quality, portability, standardization, or availability.
60These functions can be called in almost all coding environments,
61including
62.Xr pthreads 3
63and
64.Xr chroot 2 .
65.Pp
66High quality 32-bit pseudo-random numbers are generated very quickly.
67On each call, a cryptographic pseudo-random number generator is used
68to generate a new result.
69One data pool is used for all consumers in a process, so that consumption
70under program flow can act as additional stirring.
71The subsystem is re-seeded from the kernel
72.Xr random 4
73subsystem using
74.Xr getentropy 2
75on a regular basis, and also upon
76.Xr fork 2 .
77.Pp
78The
79.Fn arc4random
80function returns a single 32-bit value.
81.Pp
82.Fn arc4random_buf
83fills the region
84.Fa buf
85of length
86.Fa nbytes
87with random data.
88.Pp
89.Fn arc4random_uniform
90will return a single 32-bit value, uniformly distributed but less than
91.Fa upper_bound .
92This is recommended over constructions like
93.Dq Li arc4random() % upper_bound
94as it avoids "modulo bias" when the upper bound is not a power of two.
95In the worst case, this function may consume multiple iterations
96to ensure uniformity; see the source code to understand the problem
97and solution.
98.Sh RETURN VALUES
99These functions are always successful, and no return value is
100reserved to indicate an error.
101.Sh SEE ALSO
102.Xr rand 3 ,
103.Xr rand48 3 ,
104.Xr random 3
105.Sh HISTORY
106These functions first appeared in
107.Ox 2.1 .
108.Pp
109The original version of this random number generator used the
110RC4 (also known as ARC4) algorithm.
111In
112.Ox 5.5
113it was replaced with the ChaCha20 cipher, and it may be replaced
114again in the future as cryptographic techniques advance.
115A good mnemonic is
116.Dq A Replacement Call for Random .
diff --git a/src/lib/libc/crypt/arc4random.c b/src/lib/libc/crypt/arc4random.c
deleted file mode 100644
index 2da8e010ad..0000000000
--- a/src/lib/libc/crypt/arc4random.c
+++ /dev/null
@@ -1,204 +0,0 @@
1/* $OpenBSD: arc4random.c,v 1.58 2022/07/31 13:41:45 tb Exp $ */
2
3/*
4 * Copyright (c) 1996, David Mazieres <dm@uun.org>
5 * Copyright (c) 2008, Damien Miller <djm@openbsd.org>
6 * Copyright (c) 2013, Markus Friedl <markus@openbsd.org>
7 * Copyright (c) 2014, Theo de Raadt <deraadt@openbsd.org>
8 *
9 * Permission to use, copy, modify, and distribute this software for any
10 * purpose with or without fee is hereby granted, provided that the above
11 * copyright notice and this permission notice appear in all copies.
12 *
13 * THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES
14 * WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF
15 * MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR
16 * ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES
17 * WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN
18 * ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF
19 * OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE.
20 */
21
22/*
23 * ChaCha based random number generator for OpenBSD.
24 */
25
26#include <fcntl.h>
27#include <limits.h>
28#include <signal.h>
29#include <stdint.h>
30#include <stdlib.h>
31#include <string.h>
32#include <unistd.h>
33#include <sys/types.h>
34#include <sys/time.h>
35
36#define KEYSTREAM_ONLY
37#include "chacha_private.h"
38
39#define minimum(a, b) ((a) < (b) ? (a) : (b))
40
41#if defined(__GNUC__) || defined(_MSC_VER)
42#define inline __inline
43#else /* __GNUC__ || _MSC_VER */
44#define inline
45#endif /* !__GNUC__ && !_MSC_VER */
46
47#define KEYSZ 32
48#define IVSZ 8
49#define BLOCKSZ 64
50#define RSBUFSZ (16*BLOCKSZ)
51
52#define REKEY_BASE (1024*1024) /* NB. should be a power of 2 */
53
54/* Marked MAP_INHERIT_ZERO, so zero'd out in fork children. */
55static struct _rs {
56 size_t rs_have; /* valid bytes at end of rs_buf */
57 size_t rs_count; /* bytes till reseed */
58} *rs;
59
60/* Maybe be preserved in fork children, if _rs_allocate() decides. */
61static struct _rsx {
62 chacha_ctx rs_chacha; /* chacha context for random keystream */
63 u_char rs_buf[RSBUFSZ]; /* keystream blocks */
64} *rsx;
65
66static inline int _rs_allocate(struct _rs **, struct _rsx **);
67static inline void _rs_forkdetect(void);
68#include "arc4random.h"
69
70static inline void _rs_rekey(u_char *dat, size_t datlen);
71
72static inline void
73_rs_init(u_char *buf, size_t n)
74{
75 if (n < KEYSZ + IVSZ)
76 return;
77
78 if (rs == NULL) {
79 if (_rs_allocate(&rs, &rsx) == -1)
80 _exit(1);
81 }
82
83 chacha_keysetup(&rsx->rs_chacha, buf, KEYSZ * 8);
84 chacha_ivsetup(&rsx->rs_chacha, buf + KEYSZ);
85}
86
87static void
88_rs_stir(void)
89{
90 u_char rnd[KEYSZ + IVSZ];
91 uint32_t rekey_fuzz = 0;
92
93 if (getentropy(rnd, sizeof rnd) == -1)
94 _getentropy_fail();
95
96 if (!rs)
97 _rs_init(rnd, sizeof(rnd));
98 else
99 _rs_rekey(rnd, sizeof(rnd));
100 explicit_bzero(rnd, sizeof(rnd)); /* discard source seed */
101
102 /* invalidate rs_buf */
103 rs->rs_have = 0;
104 memset(rsx->rs_buf, 0, sizeof(rsx->rs_buf));
105
106 /* rekey interval should not be predictable */
107 chacha_encrypt_bytes(&rsx->rs_chacha, (uint8_t *)&rekey_fuzz,
108 (uint8_t *)&rekey_fuzz, sizeof(rekey_fuzz));
109 rs->rs_count = REKEY_BASE + (rekey_fuzz % REKEY_BASE);
110}
111
112static inline void
113_rs_stir_if_needed(size_t len)
114{
115 _rs_forkdetect();
116 if (!rs || rs->rs_count <= len)
117 _rs_stir();
118 if (rs->rs_count <= len)
119 rs->rs_count = 0;
120 else
121 rs->rs_count -= len;
122}
123
124static inline void
125_rs_rekey(u_char *dat, size_t datlen)
126{
127#ifndef KEYSTREAM_ONLY
128 memset(rsx->rs_buf, 0, sizeof(rsx->rs_buf));
129#endif
130 /* fill rs_buf with the keystream */
131 chacha_encrypt_bytes(&rsx->rs_chacha, rsx->rs_buf,
132 rsx->rs_buf, sizeof(rsx->rs_buf));
133 /* mix in optional user provided data */
134 if (dat) {
135 size_t i, m;
136
137 m = minimum(datlen, KEYSZ + IVSZ);
138 for (i = 0; i < m; i++)
139 rsx->rs_buf[i] ^= dat[i];
140 }
141 /* immediately reinit for backtracking resistance */
142 _rs_init(rsx->rs_buf, KEYSZ + IVSZ);
143 memset(rsx->rs_buf, 0, KEYSZ + IVSZ);
144 rs->rs_have = sizeof(rsx->rs_buf) - KEYSZ - IVSZ;
145}
146
147static inline void
148_rs_random_buf(void *_buf, size_t n)
149{
150 u_char *buf = (u_char *)_buf;
151 u_char *keystream;
152 size_t m;
153
154 _rs_stir_if_needed(n);
155 while (n > 0) {
156 if (rs->rs_have > 0) {
157 m = minimum(n, rs->rs_have);
158 keystream = rsx->rs_buf + sizeof(rsx->rs_buf)
159 - rs->rs_have;
160 memcpy(buf, keystream, m);
161 memset(keystream, 0, m);
162 buf += m;
163 n -= m;
164 rs->rs_have -= m;
165 }
166 if (rs->rs_have == 0)
167 _rs_rekey(NULL, 0);
168 }
169}
170
171static inline void
172_rs_random_u32(uint32_t *val)
173{
174 u_char *keystream;
175
176 _rs_stir_if_needed(sizeof(*val));
177 if (rs->rs_have < sizeof(*val))
178 _rs_rekey(NULL, 0);
179 keystream = rsx->rs_buf + sizeof(rsx->rs_buf) - rs->rs_have;
180 memcpy(val, keystream, sizeof(*val));
181 memset(keystream, 0, sizeof(*val));
182 rs->rs_have -= sizeof(*val);
183}
184
185uint32_t
186arc4random(void)
187{
188 uint32_t val;
189
190 _ARC4_LOCK();
191 _rs_random_u32(&val);
192 _ARC4_UNLOCK();
193 return val;
194}
195DEF_WEAK(arc4random);
196
197void
198arc4random_buf(void *buf, size_t n)
199{
200 _ARC4_LOCK();
201 _rs_random_buf(buf, n);
202 _ARC4_UNLOCK();
203}
204DEF_WEAK(arc4random_buf);
diff --git a/src/lib/libc/crypt/arc4random.h b/src/lib/libc/crypt/arc4random.h
deleted file mode 100644
index 4abd15321a..0000000000
--- a/src/lib/libc/crypt/arc4random.h
+++ /dev/null
@@ -1,61 +0,0 @@
1/* $OpenBSD: arc4random.h,v 1.4 2015/01/15 06:57:18 deraadt Exp $ */
2
3/*
4 * Copyright (c) 1996, David Mazieres <dm@uun.org>
5 * Copyright (c) 2008, Damien Miller <djm@openbsd.org>
6 * Copyright (c) 2013, Markus Friedl <markus@openbsd.org>
7 * Copyright (c) 2014, Theo de Raadt <deraadt@openbsd.org>
8 *
9 * Permission to use, copy, modify, and distribute this software for any
10 * purpose with or without fee is hereby granted, provided that the above
11 * copyright notice and this permission notice appear in all copies.
12 *
13 * THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES
14 * WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF
15 * MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR
16 * ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES
17 * WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN
18 * ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF
19 * OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE.
20 */
21
22/*
23 * Stub functions for portability.
24 */
25#include <sys/mman.h>
26
27#include <signal.h>
28
29#include "thread_private.h"
30
31static inline void
32_getentropy_fail(void)
33{
34 raise(SIGKILL);
35}
36
37static inline int
38_rs_allocate(struct _rs **rsp, struct _rsx **rsxp)
39{
40 struct {
41 struct _rs rs;
42 struct _rsx rsx;
43 } *p;
44
45 if ((p = mmap(NULL, sizeof(*p), PROT_READ|PROT_WRITE,
46 MAP_ANON|MAP_PRIVATE, -1, 0)) == MAP_FAILED)
47 return (-1);
48 if (minherit(p, sizeof(*p), MAP_INHERIT_ZERO) == -1) {
49 munmap(p, sizeof(*p));
50 return (-1);
51 }
52
53 *rsp = &p->rs;
54 *rsxp = &p->rsx;
55 return (0);
56}
57
58static inline void
59_rs_forkdetect(void)
60{
61}
diff --git a/src/lib/libc/crypt/arc4random_uniform.c b/src/lib/libc/crypt/arc4random_uniform.c
deleted file mode 100644
index a18b5b1238..0000000000
--- a/src/lib/libc/crypt/arc4random_uniform.c
+++ /dev/null
@@ -1,57 +0,0 @@
1/* $OpenBSD: arc4random_uniform.c,v 1.3 2019/01/20 02:59:07 bcook Exp $ */
2
3/*
4 * Copyright (c) 2008, Damien Miller <djm@openbsd.org>
5 *
6 * Permission to use, copy, modify, and distribute this software for any
7 * purpose with or without fee is hereby granted, provided that the above
8 * copyright notice and this permission notice appear in all copies.
9 *
10 * THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES
11 * WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF
12 * MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR
13 * ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES
14 * WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN
15 * ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF
16 * OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE.
17 */
18
19#include <stdint.h>
20#include <stdlib.h>
21
22/*
23 * Calculate a uniformly distributed random number less than upper_bound
24 * avoiding "modulo bias".
25 *
26 * Uniformity is achieved by generating new random numbers until the one
27 * returned is outside the range [0, 2**32 % upper_bound). This
28 * guarantees the selected random number will be inside
29 * [2**32 % upper_bound, 2**32) which maps back to [0, upper_bound)
30 * after reduction modulo upper_bound.
31 */
32uint32_t
33arc4random_uniform(uint32_t upper_bound)
34{
35 uint32_t r, min;
36
37 if (upper_bound < 2)
38 return 0;
39
40 /* 2**32 % x == (2**32 - x) % x */
41 min = -upper_bound % upper_bound;
42
43 /*
44 * This could theoretically loop forever but each retry has
45 * p > 0.5 (worst case, usually far better) of selecting a
46 * number inside the range we need, so it should rarely need
47 * to re-roll.
48 */
49 for (;;) {
50 r = arc4random();
51 if (r >= min)
52 break;
53 }
54
55 return r % upper_bound;
56}
57DEF_WEAK(arc4random_uniform);
diff --git a/src/lib/libc/crypt/bcrypt.c b/src/lib/libc/crypt/bcrypt.c
deleted file mode 100644
index ba45b104ed..0000000000
--- a/src/lib/libc/crypt/bcrypt.c
+++ /dev/null
@@ -1,397 +0,0 @@
1/* $OpenBSD: bcrypt.c,v 1.58 2020/07/06 13:33:05 pirofti Exp $ */
2
3/*
4 * Copyright (c) 2014 Ted Unangst <tedu@openbsd.org>
5 * Copyright (c) 1997 Niels Provos <provos@umich.edu>
6 *
7 * Permission to use, copy, modify, and distribute this software for any
8 * purpose with or without fee is hereby granted, provided that the above
9 * copyright notice and this permission notice appear in all copies.
10 *
11 * THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES
12 * WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF
13 * MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR
14 * ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES
15 * WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN
16 * ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF
17 * OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE.
18 */
19/* This password hashing algorithm was designed by David Mazieres
20 * <dm@lcs.mit.edu> and works as follows:
21 *
22 * 1. state := InitState ()
23 * 2. state := ExpandKey (state, salt, password)
24 * 3. REPEAT rounds:
25 * state := ExpandKey (state, 0, password)
26 * state := ExpandKey (state, 0, salt)
27 * 4. ctext := "OrpheanBeholderScryDoubt"
28 * 5. REPEAT 64:
29 * ctext := Encrypt_ECB (state, ctext);
30 * 6. RETURN Concatenate (salt, ctext);
31 *
32 */
33
34#include <sys/types.h>
35#include <blf.h>
36#include <ctype.h>
37#include <errno.h>
38#include <pwd.h>
39#include <stdio.h>
40#include <stdlib.h>
41#include <string.h>
42#include <time.h>
43
44/* This implementation is adaptable to current computing power.
45 * You can have up to 2^31 rounds which should be enough for some
46 * time to come.
47 */
48
49#define BCRYPT_VERSION '2'
50#define BCRYPT_MAXSALT 16 /* Precomputation is just so nice */
51#define BCRYPT_WORDS 6 /* Ciphertext words */
52#define BCRYPT_MINLOGROUNDS 4 /* we have log2(rounds) in salt */
53
54#define BCRYPT_SALTSPACE (7 + (BCRYPT_MAXSALT * 4 + 2) / 3 + 1)
55#define BCRYPT_HASHSPACE 61
56
57char *bcrypt_gensalt(u_int8_t);
58
59static int encode_base64(char *, const u_int8_t *, size_t);
60static int decode_base64(u_int8_t *, size_t, const char *);
61
62/*
63 * Generates a salt for this version of crypt.
64 */
65static int
66bcrypt_initsalt(int log_rounds, uint8_t *salt, size_t saltbuflen)
67{
68 uint8_t csalt[BCRYPT_MAXSALT];
69
70 if (saltbuflen < BCRYPT_SALTSPACE) {
71 errno = EINVAL;
72 return -1;
73 }
74
75 arc4random_buf(csalt, sizeof(csalt));
76
77 if (log_rounds < 4)
78 log_rounds = 4;
79 else if (log_rounds > 31)
80 log_rounds = 31;
81
82 snprintf(salt, saltbuflen, "$2b$%2.2u$", log_rounds);
83 encode_base64(salt + 7, csalt, sizeof(csalt));
84
85 return 0;
86}
87
88/*
89 * the core bcrypt function
90 */
91static int
92bcrypt_hashpass(const char *key, const char *salt, char *encrypted,
93 size_t encryptedlen)
94{
95 blf_ctx state;
96 u_int32_t rounds, i, k;
97 u_int16_t j;
98 size_t key_len;
99 u_int8_t salt_len, logr, minor;
100 u_int8_t ciphertext[4 * BCRYPT_WORDS] = "OrpheanBeholderScryDoubt";
101 u_int8_t csalt[BCRYPT_MAXSALT];
102 u_int32_t cdata[BCRYPT_WORDS];
103
104 if (encryptedlen < BCRYPT_HASHSPACE)
105 goto inval;
106
107 /* Check and discard "$" identifier */
108 if (salt[0] != '$')
109 goto inval;
110 salt += 1;
111
112 if (salt[0] != BCRYPT_VERSION)
113 goto inval;
114
115 /* Check for minor versions */
116 switch ((minor = salt[1])) {
117 case 'a':
118 key_len = (u_int8_t)(strlen(key) + 1);
119 break;
120 case 'b':
121 /* strlen() returns a size_t, but the function calls
122 * below result in implicit casts to a narrower integer
123 * type, so cap key_len at the actual maximum supported
124 * length here to avoid integer wraparound */
125 key_len = strlen(key);
126 if (key_len > 72)
127 key_len = 72;
128 key_len++; /* include the NUL */
129 break;
130 default:
131 goto inval;
132 }
133 if (salt[2] != '$')
134 goto inval;
135 /* Discard version + "$" identifier */
136 salt += 3;
137
138 /* Check and parse num rounds */
139 if (!isdigit((unsigned char)salt[0]) ||
140 !isdigit((unsigned char)salt[1]) || salt[2] != '$')
141 goto inval;
142 logr = (salt[1] - '0') + ((salt[0] - '0') * 10);
143 if (logr < BCRYPT_MINLOGROUNDS || logr > 31)
144 goto inval;
145 /* Computer power doesn't increase linearly, 2^x should be fine */
146 rounds = 1U << logr;
147
148 /* Discard num rounds + "$" identifier */
149 salt += 3;
150
151 if (strlen(salt) * 3 / 4 < BCRYPT_MAXSALT)
152 goto inval;
153
154 /* We dont want the base64 salt but the raw data */
155 if (decode_base64(csalt, BCRYPT_MAXSALT, salt))
156 goto inval;
157 salt_len = BCRYPT_MAXSALT;
158
159 /* Setting up S-Boxes and Subkeys */
160 Blowfish_initstate(&state);
161 Blowfish_expandstate(&state, csalt, salt_len,
162 (u_int8_t *) key, key_len);
163 for (k = 0; k < rounds; k++) {
164 Blowfish_expand0state(&state, (u_int8_t *) key, key_len);
165 Blowfish_expand0state(&state, csalt, salt_len);
166 }
167
168 /* This can be precomputed later */
169 j = 0;
170 for (i = 0; i < BCRYPT_WORDS; i++)
171 cdata[i] = Blowfish_stream2word(ciphertext, 4 * BCRYPT_WORDS, &j);
172
173 /* Now do the encryption */
174 for (k = 0; k < 64; k++)
175 blf_enc(&state, cdata, BCRYPT_WORDS / 2);
176
177 for (i = 0; i < BCRYPT_WORDS; i++) {
178 ciphertext[4 * i + 3] = cdata[i] & 0xff;
179 cdata[i] = cdata[i] >> 8;
180 ciphertext[4 * i + 2] = cdata[i] & 0xff;
181 cdata[i] = cdata[i] >> 8;
182 ciphertext[4 * i + 1] = cdata[i] & 0xff;
183 cdata[i] = cdata[i] >> 8;
184 ciphertext[4 * i + 0] = cdata[i] & 0xff;
185 }
186
187
188 snprintf(encrypted, 8, "$2%c$%2.2u$", minor, logr);
189 encode_base64(encrypted + 7, csalt, BCRYPT_MAXSALT);
190 encode_base64(encrypted + 7 + 22, ciphertext, 4 * BCRYPT_WORDS - 1);
191 explicit_bzero(&state, sizeof(state));
192 explicit_bzero(ciphertext, sizeof(ciphertext));
193 explicit_bzero(csalt, sizeof(csalt));
194 explicit_bzero(cdata, sizeof(cdata));
195 return 0;
196
197inval:
198 errno = EINVAL;
199 return -1;
200}
201
202/*
203 * user friendly functions
204 */
205int
206bcrypt_newhash(const char *pass, int log_rounds, char *hash, size_t hashlen)
207{
208 char salt[BCRYPT_SALTSPACE];
209
210 if (bcrypt_initsalt(log_rounds, salt, sizeof(salt)) != 0)
211 return -1;
212
213 if (bcrypt_hashpass(pass, salt, hash, hashlen) != 0)
214 return -1;
215
216 explicit_bzero(salt, sizeof(salt));
217 return 0;
218}
219DEF_WEAK(bcrypt_newhash);
220
221int
222bcrypt_checkpass(const char *pass, const char *goodhash)
223{
224 char hash[BCRYPT_HASHSPACE];
225
226 if (bcrypt_hashpass(pass, goodhash, hash, sizeof(hash)) != 0)
227 return -1;
228 if (strlen(hash) != strlen(goodhash) ||
229 timingsafe_bcmp(hash, goodhash, strlen(goodhash)) != 0) {
230 errno = EACCES;
231 return -1;
232 }
233
234 explicit_bzero(hash, sizeof(hash));
235 return 0;
236}
237DEF_WEAK(bcrypt_checkpass);
238
239/*
240 * Measure this system's performance by measuring the time for 8 rounds.
241 * We are aiming for something that takes around 0.1s, but not too much over.
242 */
243int
244_bcrypt_autorounds(void)
245{
246 struct timespec before, after;
247 int r = 8;
248 char buf[_PASSWORD_LEN];
249 int duration;
250
251 WRAP(clock_gettime)(CLOCK_THREAD_CPUTIME_ID, &before);
252 bcrypt_newhash("testpassword", r, buf, sizeof(buf));
253 WRAP(clock_gettime)(CLOCK_THREAD_CPUTIME_ID, &after);
254
255 duration = after.tv_sec - before.tv_sec;
256 duration *= 1000000;
257 duration += (after.tv_nsec - before.tv_nsec) / 1000;
258
259 /* too quick? slow it down. */
260 while (r < 16 && duration <= 60000) {
261 r += 1;
262 duration *= 2;
263 }
264 /* too slow? speed it up. */
265 while (r > 6 && duration > 120000) {
266 r -= 1;
267 duration /= 2;
268 }
269
270 return r;
271}
272
273/*
274 * internal utilities
275 */
276static const u_int8_t Base64Code[] =
277"./ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789";
278
279static const u_int8_t index_64[128] = {
280 255, 255, 255, 255, 255, 255, 255, 255, 255, 255,
281 255, 255, 255, 255, 255, 255, 255, 255, 255, 255,
282 255, 255, 255, 255, 255, 255, 255, 255, 255, 255,
283 255, 255, 255, 255, 255, 255, 255, 255, 255, 255,
284 255, 255, 255, 255, 255, 255, 0, 1, 54, 55,
285 56, 57, 58, 59, 60, 61, 62, 63, 255, 255,
286 255, 255, 255, 255, 255, 2, 3, 4, 5, 6,
287 7, 8, 9, 10, 11, 12, 13, 14, 15, 16,
288 17, 18, 19, 20, 21, 22, 23, 24, 25, 26, 27,
289 255, 255, 255, 255, 255, 255, 28, 29, 30,
290 31, 32, 33, 34, 35, 36, 37, 38, 39, 40,
291 41, 42, 43, 44, 45, 46, 47, 48, 49, 50,
292 51, 52, 53, 255, 255, 255, 255, 255
293};
294#define CHAR64(c) ( (c) > 127 ? 255 : index_64[(c)])
295
296/*
297 * read buflen (after decoding) bytes of data from b64data
298 */
299static int
300decode_base64(u_int8_t *buffer, size_t len, const char *b64data)
301{
302 u_int8_t *bp = buffer;
303 const u_int8_t *p = b64data;
304 u_int8_t c1, c2, c3, c4;
305
306 while (bp < buffer + len) {
307 c1 = CHAR64(*p);
308 /* Invalid data */
309 if (c1 == 255)
310 return -1;
311
312 c2 = CHAR64(*(p + 1));
313 if (c2 == 255)
314 return -1;
315
316 *bp++ = (c1 << 2) | ((c2 & 0x30) >> 4);
317 if (bp >= buffer + len)
318 break;
319
320 c3 = CHAR64(*(p + 2));
321 if (c3 == 255)
322 return -1;
323
324 *bp++ = ((c2 & 0x0f) << 4) | ((c3 & 0x3c) >> 2);
325 if (bp >= buffer + len)
326 break;
327
328 c4 = CHAR64(*(p + 3));
329 if (c4 == 255)
330 return -1;
331 *bp++ = ((c3 & 0x03) << 6) | c4;
332
333 p += 4;
334 }
335 return 0;
336}
337
338/*
339 * Turn len bytes of data into base64 encoded data.
340 * This works without = padding.
341 */
342static int
343encode_base64(char *b64buffer, const u_int8_t *data, size_t len)
344{
345 u_int8_t *bp = b64buffer;
346 const u_int8_t *p = data;
347 u_int8_t c1, c2;
348
349 while (p < data + len) {
350 c1 = *p++;
351 *bp++ = Base64Code[(c1 >> 2)];
352 c1 = (c1 & 0x03) << 4;
353 if (p >= data + len) {
354 *bp++ = Base64Code[c1];
355 break;
356 }
357 c2 = *p++;
358 c1 |= (c2 >> 4) & 0x0f;
359 *bp++ = Base64Code[c1];
360 c1 = (c2 & 0x0f) << 2;
361 if (p >= data + len) {
362 *bp++ = Base64Code[c1];
363 break;
364 }
365 c2 = *p++;
366 c1 |= (c2 >> 6) & 0x03;
367 *bp++ = Base64Code[c1];
368 *bp++ = Base64Code[c2 & 0x3f];
369 }
370 *bp = '\0';
371 return 0;
372}
373
374/*
375 * classic interface
376 */
377char *
378bcrypt_gensalt(u_int8_t log_rounds)
379{
380 static char gsalt[BCRYPT_SALTSPACE];
381
382 bcrypt_initsalt(log_rounds, gsalt, sizeof(gsalt));
383
384 return gsalt;
385}
386
387char *
388bcrypt(const char *pass, const char *salt)
389{
390 static char gencrypted[BCRYPT_HASHSPACE];
391
392 if (bcrypt_hashpass(pass, salt, gencrypted, sizeof(gencrypted)) != 0)
393 return NULL;
394
395 return gencrypted;
396}
397DEF_WEAK(bcrypt);
diff --git a/src/lib/libc/crypt/blowfish.3 b/src/lib/libc/crypt/blowfish.3
deleted file mode 100644
index c64ccb684b..0000000000
--- a/src/lib/libc/crypt/blowfish.3
+++ /dev/null
@@ -1,103 +0,0 @@
1.\" $OpenBSD: blowfish.3,v 1.24 2021/11/29 01:04:45 djm Exp $
2.\"
3.\" Copyright 1997 Niels Provos <provos@physnet.uni-hamburg.de>
4.\" 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. The name of the author may not be used to endorse or promote products
15.\" derived from this software without specific prior written permission.
16.\"
17.\" THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR
18.\" IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
19.\" OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
20.\" IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT,
21.\" INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
22.\" NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
23.\" DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
24.\" THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
25.\" (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF
26.\" THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
27.\"
28.\" Manual page, using -mandoc macros
29.\"
30.Dd $Mdocdate: November 29 2021 $
31.Dt BLF_KEY 3
32.Os
33.Sh NAME
34.Nm blf_key ,
35.Nm blf_enc ,
36.Nm blf_dec ,
37.Nm blf_ecb_encrypt ,
38.Nm blf_ecb_decrypt ,
39.Nm blf_cbc_encrypt ,
40.Nm blf_cbc_decrypt
41.Nd Blowfish encryption
42.Sh SYNOPSIS
43.In blf.h
44.Ft void
45.Fn blf_key "blf_ctx *state" "const u_int8_t *key" "u_int16_t keylen"
46.Ft void
47.Fn blf_enc "blf_ctx *state" "u_int32_t *data" "u_int16_t blocks"
48.Ft void
49.Fn blf_dec "blf_ctx *state" "u_int32_t *data" "u_int16_t blocks"
50.Ft void
51.Fn blf_ecb_encrypt "blf_ctx *state" "u_int8_t *data" "u_int32_t datalen"
52.Ft void
53.Fn blf_ecb_decrypt "blf_ctx *state" "u_int8_t *data" "u_int32_t datalen"
54.Ft void
55.Fn blf_cbc_encrypt "blf_ctx *state" "u_int8_t *iv" "u_int8_t *data" "u_int32_t datalen"
56.Ft void
57.Fn blf_cbc_decrypt "blf_ctx *state" "u_int8_t *iv" "u_int8_t *data" "u_int32_t datalen"
58.Sh DESCRIPTION
59.Em Blowfish
60is a fast unpatented block cipher designed by Bruce Schneier.
61It basically consists of a 16-round Feistel network.
62The block size is 64 bits and the maximum key size is 448 bits.
63.Pp
64The
65.Fn blf_key
66function initializes the 4 8-bit S-boxes and the 18 Subkeys with
67the hexadecimal digits of Pi.
68The key is used for further randomization.
69The first argument to
70.Fn blf_enc
71is the initialized state derived from
72.Fn blf_key .
73The stream of 32-bit words is encrypted in Electronic Codebook
74Mode (ECB) and
75.Fa blocks
76is the number of 64-bit blocks in the stream.
77.Fn blf_dec
78is used for decrypting Blowfish encrypted blocks.
79.Pp
80The functions
81.Fn blf_ecb_encrypt
82and
83.Fn blf_ecb_decrypt
84are used for encrypting and decrypting octet streams in ECB mode.
85The functions
86.Fn blf_cbc_encrypt
87and
88.Fn blf_cbc_decrypt
89are used for encrypting and decrypting octet streams in
90Cipherblock Chaining Mode (CBC).
91For these functions
92.Fa datalen
93specifies the number of octets of data to encrypt or decrypt.
94It must be a multiple of 8 (64-bit block).
95The initialisation vector
96.Fa iv
97points to an 8-byte buffer.
98.Sh SEE ALSO
99.Xr passwd 1 ,
100.Xr crypt 3 ,
101.Xr passwd 5
102.Sh AUTHORS
103.An Niels Provos Aq Mt provos@physnet.uni-hamburg.de
diff --git a/src/lib/libc/crypt/blowfish.c b/src/lib/libc/crypt/blowfish.c
deleted file mode 100644
index 05ad00d7a1..0000000000
--- a/src/lib/libc/crypt/blowfish.c
+++ /dev/null
@@ -1,688 +0,0 @@
1/* $OpenBSD: blowfish.c,v 1.21 2022/08/28 11:11:25 jsg Exp $ */
2/*
3 * Blowfish block cipher for OpenBSD
4 * Copyright 1997 Niels Provos <provos@physnet.uni-hamburg.de>
5 * All rights reserved.
6 *
7 * Implementation advice by David Mazieres <dm@lcs.mit.edu>.
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. The name of the author may not be used to endorse or promote products
18 * derived from this software without specific prior written permission.
19 *
20 * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR
21 * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
22 * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
23 * IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT,
24 * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
25 * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
26 * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
27 * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
28 * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF
29 * THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
30 */
31
32/*
33 * This code is derived from section 14.3 and the given source
34 * in section V of Applied Cryptography, second edition.
35 * Blowfish is an unpatented fast block cipher designed by
36 * Bruce Schneier.
37 */
38
39#if 0
40#include <stdio.h> /* used for debugging */
41#include <string.h>
42#endif
43
44#include <sys/types.h>
45#include <blf.h>
46
47/* Function for Feistel Networks */
48
49#define F(s, x) ((((s)[ (((x)>>24)&0xFF)] \
50 + (s)[0x100 + (((x)>>16)&0xFF)]) \
51 ^ (s)[0x200 + (((x)>> 8)&0xFF)]) \
52 + (s)[0x300 + ( (x) &0xFF)])
53
54#define BLFRND(s,p,i,j,n) (i ^= F(s,j) ^ (p)[n])
55
56void
57Blowfish_encipher(blf_ctx *c, u_int32_t *xl, u_int32_t *xr)
58{
59 u_int32_t Xl;
60 u_int32_t Xr;
61 u_int32_t *s = c->S[0];
62 u_int32_t *p = c->P;
63
64 Xl = *xl;
65 Xr = *xr;
66
67 Xl ^= p[0];
68 BLFRND(s, p, Xr, Xl, 1); BLFRND(s, p, Xl, Xr, 2);
69 BLFRND(s, p, Xr, Xl, 3); BLFRND(s, p, Xl, Xr, 4);
70 BLFRND(s, p, Xr, Xl, 5); BLFRND(s, p, Xl, Xr, 6);
71 BLFRND(s, p, Xr, Xl, 7); BLFRND(s, p, Xl, Xr, 8);
72 BLFRND(s, p, Xr, Xl, 9); BLFRND(s, p, Xl, Xr, 10);
73 BLFRND(s, p, Xr, Xl, 11); BLFRND(s, p, Xl, Xr, 12);
74 BLFRND(s, p, Xr, Xl, 13); BLFRND(s, p, Xl, Xr, 14);
75 BLFRND(s, p, Xr, Xl, 15); BLFRND(s, p, Xl, Xr, 16);
76
77 *xl = Xr ^ p[17];
78 *xr = Xl;
79}
80DEF_WEAK(Blowfish_encipher);
81
82void
83Blowfish_decipher(blf_ctx *c, u_int32_t *xl, u_int32_t *xr)
84{
85 u_int32_t Xl;
86 u_int32_t Xr;
87 u_int32_t *s = c->S[0];
88 u_int32_t *p = c->P;
89
90 Xl = *xl;
91 Xr = *xr;
92
93 Xl ^= p[17];
94 BLFRND(s, p, Xr, Xl, 16); BLFRND(s, p, Xl, Xr, 15);
95 BLFRND(s, p, Xr, Xl, 14); BLFRND(s, p, Xl, Xr, 13);
96 BLFRND(s, p, Xr, Xl, 12); BLFRND(s, p, Xl, Xr, 11);
97 BLFRND(s, p, Xr, Xl, 10); BLFRND(s, p, Xl, Xr, 9);
98 BLFRND(s, p, Xr, Xl, 8); BLFRND(s, p, Xl, Xr, 7);
99 BLFRND(s, p, Xr, Xl, 6); BLFRND(s, p, Xl, Xr, 5);
100 BLFRND(s, p, Xr, Xl, 4); BLFRND(s, p, Xl, Xr, 3);
101 BLFRND(s, p, Xr, Xl, 2); BLFRND(s, p, Xl, Xr, 1);
102
103 *xl = Xr ^ p[0];
104 *xr = Xl;
105}
106DEF_WEAK(Blowfish_decipher);
107
108void
109Blowfish_initstate(blf_ctx *c)
110{
111 /* P-box and S-box tables initialized with digits of Pi */
112
113 static const blf_ctx initstate =
114 { {
115 {
116 0xd1310ba6, 0x98dfb5ac, 0x2ffd72db, 0xd01adfb7,
117 0xb8e1afed, 0x6a267e96, 0xba7c9045, 0xf12c7f99,
118 0x24a19947, 0xb3916cf7, 0x0801f2e2, 0x858efc16,
119 0x636920d8, 0x71574e69, 0xa458fea3, 0xf4933d7e,
120 0x0d95748f, 0x728eb658, 0x718bcd58, 0x82154aee,
121 0x7b54a41d, 0xc25a59b5, 0x9c30d539, 0x2af26013,
122 0xc5d1b023, 0x286085f0, 0xca417918, 0xb8db38ef,
123 0x8e79dcb0, 0x603a180e, 0x6c9e0e8b, 0xb01e8a3e,
124 0xd71577c1, 0xbd314b27, 0x78af2fda, 0x55605c60,
125 0xe65525f3, 0xaa55ab94, 0x57489862, 0x63e81440,
126 0x55ca396a, 0x2aab10b6, 0xb4cc5c34, 0x1141e8ce,
127 0xa15486af, 0x7c72e993, 0xb3ee1411, 0x636fbc2a,
128 0x2ba9c55d, 0x741831f6, 0xce5c3e16, 0x9b87931e,
129 0xafd6ba33, 0x6c24cf5c, 0x7a325381, 0x28958677,
130 0x3b8f4898, 0x6b4bb9af, 0xc4bfe81b, 0x66282193,
131 0x61d809cc, 0xfb21a991, 0x487cac60, 0x5dec8032,
132 0xef845d5d, 0xe98575b1, 0xdc262302, 0xeb651b88,
133 0x23893e81, 0xd396acc5, 0x0f6d6ff3, 0x83f44239,
134 0x2e0b4482, 0xa4842004, 0x69c8f04a, 0x9e1f9b5e,
135 0x21c66842, 0xf6e96c9a, 0x670c9c61, 0xabd388f0,
136 0x6a51a0d2, 0xd8542f68, 0x960fa728, 0xab5133a3,
137 0x6eef0b6c, 0x137a3be4, 0xba3bf050, 0x7efb2a98,
138 0xa1f1651d, 0x39af0176, 0x66ca593e, 0x82430e88,
139 0x8cee8619, 0x456f9fb4, 0x7d84a5c3, 0x3b8b5ebe,
140 0xe06f75d8, 0x85c12073, 0x401a449f, 0x56c16aa6,
141 0x4ed3aa62, 0x363f7706, 0x1bfedf72, 0x429b023d,
142 0x37d0d724, 0xd00a1248, 0xdb0fead3, 0x49f1c09b,
143 0x075372c9, 0x80991b7b, 0x25d479d8, 0xf6e8def7,
144 0xe3fe501a, 0xb6794c3b, 0x976ce0bd, 0x04c006ba,
145 0xc1a94fb6, 0x409f60c4, 0x5e5c9ec2, 0x196a2463,
146 0x68fb6faf, 0x3e6c53b5, 0x1339b2eb, 0x3b52ec6f,
147 0x6dfc511f, 0x9b30952c, 0xcc814544, 0xaf5ebd09,
148 0xbee3d004, 0xde334afd, 0x660f2807, 0x192e4bb3,
149 0xc0cba857, 0x45c8740f, 0xd20b5f39, 0xb9d3fbdb,
150 0x5579c0bd, 0x1a60320a, 0xd6a100c6, 0x402c7279,
151 0x679f25fe, 0xfb1fa3cc, 0x8ea5e9f8, 0xdb3222f8,
152 0x3c7516df, 0xfd616b15, 0x2f501ec8, 0xad0552ab,
153 0x323db5fa, 0xfd238760, 0x53317b48, 0x3e00df82,
154 0x9e5c57bb, 0xca6f8ca0, 0x1a87562e, 0xdf1769db,
155 0xd542a8f6, 0x287effc3, 0xac6732c6, 0x8c4f5573,
156 0x695b27b0, 0xbbca58c8, 0xe1ffa35d, 0xb8f011a0,
157 0x10fa3d98, 0xfd2183b8, 0x4afcb56c, 0x2dd1d35b,
158 0x9a53e479, 0xb6f84565, 0xd28e49bc, 0x4bfb9790,
159 0xe1ddf2da, 0xa4cb7e33, 0x62fb1341, 0xcee4c6e8,
160 0xef20cada, 0x36774c01, 0xd07e9efe, 0x2bf11fb4,
161 0x95dbda4d, 0xae909198, 0xeaad8e71, 0x6b93d5a0,
162 0xd08ed1d0, 0xafc725e0, 0x8e3c5b2f, 0x8e7594b7,
163 0x8ff6e2fb, 0xf2122b64, 0x8888b812, 0x900df01c,
164 0x4fad5ea0, 0x688fc31c, 0xd1cff191, 0xb3a8c1ad,
165 0x2f2f2218, 0xbe0e1777, 0xea752dfe, 0x8b021fa1,
166 0xe5a0cc0f, 0xb56f74e8, 0x18acf3d6, 0xce89e299,
167 0xb4a84fe0, 0xfd13e0b7, 0x7cc43b81, 0xd2ada8d9,
168 0x165fa266, 0x80957705, 0x93cc7314, 0x211a1477,
169 0xe6ad2065, 0x77b5fa86, 0xc75442f5, 0xfb9d35cf,
170 0xebcdaf0c, 0x7b3e89a0, 0xd6411bd3, 0xae1e7e49,
171 0x00250e2d, 0x2071b35e, 0x226800bb, 0x57b8e0af,
172 0x2464369b, 0xf009b91e, 0x5563911d, 0x59dfa6aa,
173 0x78c14389, 0xd95a537f, 0x207d5ba2, 0x02e5b9c5,
174 0x83260376, 0x6295cfa9, 0x11c81968, 0x4e734a41,
175 0xb3472dca, 0x7b14a94a, 0x1b510052, 0x9a532915,
176 0xd60f573f, 0xbc9bc6e4, 0x2b60a476, 0x81e67400,
177 0x08ba6fb5, 0x571be91f, 0xf296ec6b, 0x2a0dd915,
178 0xb6636521, 0xe7b9f9b6, 0xff34052e, 0xc5855664,
179 0x53b02d5d, 0xa99f8fa1, 0x08ba4799, 0x6e85076a},
180 {
181 0x4b7a70e9, 0xb5b32944, 0xdb75092e, 0xc4192623,
182 0xad6ea6b0, 0x49a7df7d, 0x9cee60b8, 0x8fedb266,
183 0xecaa8c71, 0x699a17ff, 0x5664526c, 0xc2b19ee1,
184 0x193602a5, 0x75094c29, 0xa0591340, 0xe4183a3e,
185 0x3f54989a, 0x5b429d65, 0x6b8fe4d6, 0x99f73fd6,
186 0xa1d29c07, 0xefe830f5, 0x4d2d38e6, 0xf0255dc1,
187 0x4cdd2086, 0x8470eb26, 0x6382e9c6, 0x021ecc5e,
188 0x09686b3f, 0x3ebaefc9, 0x3c971814, 0x6b6a70a1,
189 0x687f3584, 0x52a0e286, 0xb79c5305, 0xaa500737,
190 0x3e07841c, 0x7fdeae5c, 0x8e7d44ec, 0x5716f2b8,
191 0xb03ada37, 0xf0500c0d, 0xf01c1f04, 0x0200b3ff,
192 0xae0cf51a, 0x3cb574b2, 0x25837a58, 0xdc0921bd,
193 0xd19113f9, 0x7ca92ff6, 0x94324773, 0x22f54701,
194 0x3ae5e581, 0x37c2dadc, 0xc8b57634, 0x9af3dda7,
195 0xa9446146, 0x0fd0030e, 0xecc8c73e, 0xa4751e41,
196 0xe238cd99, 0x3bea0e2f, 0x3280bba1, 0x183eb331,
197 0x4e548b38, 0x4f6db908, 0x6f420d03, 0xf60a04bf,
198 0x2cb81290, 0x24977c79, 0x5679b072, 0xbcaf89af,
199 0xde9a771f, 0xd9930810, 0xb38bae12, 0xdccf3f2e,
200 0x5512721f, 0x2e6b7124, 0x501adde6, 0x9f84cd87,
201 0x7a584718, 0x7408da17, 0xbc9f9abc, 0xe94b7d8c,
202 0xec7aec3a, 0xdb851dfa, 0x63094366, 0xc464c3d2,
203 0xef1c1847, 0x3215d908, 0xdd433b37, 0x24c2ba16,
204 0x12a14d43, 0x2a65c451, 0x50940002, 0x133ae4dd,
205 0x71dff89e, 0x10314e55, 0x81ac77d6, 0x5f11199b,
206 0x043556f1, 0xd7a3c76b, 0x3c11183b, 0x5924a509,
207 0xf28fe6ed, 0x97f1fbfa, 0x9ebabf2c, 0x1e153c6e,
208 0x86e34570, 0xeae96fb1, 0x860e5e0a, 0x5a3e2ab3,
209 0x771fe71c, 0x4e3d06fa, 0x2965dcb9, 0x99e71d0f,
210 0x803e89d6, 0x5266c825, 0x2e4cc978, 0x9c10b36a,
211 0xc6150eba, 0x94e2ea78, 0xa5fc3c53, 0x1e0a2df4,
212 0xf2f74ea7, 0x361d2b3d, 0x1939260f, 0x19c27960,
213 0x5223a708, 0xf71312b6, 0xebadfe6e, 0xeac31f66,
214 0xe3bc4595, 0xa67bc883, 0xb17f37d1, 0x018cff28,
215 0xc332ddef, 0xbe6c5aa5, 0x65582185, 0x68ab9802,
216 0xeecea50f, 0xdb2f953b, 0x2aef7dad, 0x5b6e2f84,
217 0x1521b628, 0x29076170, 0xecdd4775, 0x619f1510,
218 0x13cca830, 0xeb61bd96, 0x0334fe1e, 0xaa0363cf,
219 0xb5735c90, 0x4c70a239, 0xd59e9e0b, 0xcbaade14,
220 0xeecc86bc, 0x60622ca7, 0x9cab5cab, 0xb2f3846e,
221 0x648b1eaf, 0x19bdf0ca, 0xa02369b9, 0x655abb50,
222 0x40685a32, 0x3c2ab4b3, 0x319ee9d5, 0xc021b8f7,
223 0x9b540b19, 0x875fa099, 0x95f7997e, 0x623d7da8,
224 0xf837889a, 0x97e32d77, 0x11ed935f, 0x16681281,
225 0x0e358829, 0xc7e61fd6, 0x96dedfa1, 0x7858ba99,
226 0x57f584a5, 0x1b227263, 0x9b83c3ff, 0x1ac24696,
227 0xcdb30aeb, 0x532e3054, 0x8fd948e4, 0x6dbc3128,
228 0x58ebf2ef, 0x34c6ffea, 0xfe28ed61, 0xee7c3c73,
229 0x5d4a14d9, 0xe864b7e3, 0x42105d14, 0x203e13e0,
230 0x45eee2b6, 0xa3aaabea, 0xdb6c4f15, 0xfacb4fd0,
231 0xc742f442, 0xef6abbb5, 0x654f3b1d, 0x41cd2105,
232 0xd81e799e, 0x86854dc7, 0xe44b476a, 0x3d816250,
233 0xcf62a1f2, 0x5b8d2646, 0xfc8883a0, 0xc1c7b6a3,
234 0x7f1524c3, 0x69cb7492, 0x47848a0b, 0x5692b285,
235 0x095bbf00, 0xad19489d, 0x1462b174, 0x23820e00,
236 0x58428d2a, 0x0c55f5ea, 0x1dadf43e, 0x233f7061,
237 0x3372f092, 0x8d937e41, 0xd65fecf1, 0x6c223bdb,
238 0x7cde3759, 0xcbee7460, 0x4085f2a7, 0xce77326e,
239 0xa6078084, 0x19f8509e, 0xe8efd855, 0x61d99735,
240 0xa969a7aa, 0xc50c06c2, 0x5a04abfc, 0x800bcadc,
241 0x9e447a2e, 0xc3453484, 0xfdd56705, 0x0e1e9ec9,
242 0xdb73dbd3, 0x105588cd, 0x675fda79, 0xe3674340,
243 0xc5c43465, 0x713e38d8, 0x3d28f89e, 0xf16dff20,
244 0x153e21e7, 0x8fb03d4a, 0xe6e39f2b, 0xdb83adf7},
245 {
246 0xe93d5a68, 0x948140f7, 0xf64c261c, 0x94692934,
247 0x411520f7, 0x7602d4f7, 0xbcf46b2e, 0xd4a20068,
248 0xd4082471, 0x3320f46a, 0x43b7d4b7, 0x500061af,
249 0x1e39f62e, 0x97244546, 0x14214f74, 0xbf8b8840,
250 0x4d95fc1d, 0x96b591af, 0x70f4ddd3, 0x66a02f45,
251 0xbfbc09ec, 0x03bd9785, 0x7fac6dd0, 0x31cb8504,
252 0x96eb27b3, 0x55fd3941, 0xda2547e6, 0xabca0a9a,
253 0x28507825, 0x530429f4, 0x0a2c86da, 0xe9b66dfb,
254 0x68dc1462, 0xd7486900, 0x680ec0a4, 0x27a18dee,
255 0x4f3ffea2, 0xe887ad8c, 0xb58ce006, 0x7af4d6b6,
256 0xaace1e7c, 0xd3375fec, 0xce78a399, 0x406b2a42,
257 0x20fe9e35, 0xd9f385b9, 0xee39d7ab, 0x3b124e8b,
258 0x1dc9faf7, 0x4b6d1856, 0x26a36631, 0xeae397b2,
259 0x3a6efa74, 0xdd5b4332, 0x6841e7f7, 0xca7820fb,
260 0xfb0af54e, 0xd8feb397, 0x454056ac, 0xba489527,
261 0x55533a3a, 0x20838d87, 0xfe6ba9b7, 0xd096954b,
262 0x55a867bc, 0xa1159a58, 0xcca92963, 0x99e1db33,
263 0xa62a4a56, 0x3f3125f9, 0x5ef47e1c, 0x9029317c,
264 0xfdf8e802, 0x04272f70, 0x80bb155c, 0x05282ce3,
265 0x95c11548, 0xe4c66d22, 0x48c1133f, 0xc70f86dc,
266 0x07f9c9ee, 0x41041f0f, 0x404779a4, 0x5d886e17,
267 0x325f51eb, 0xd59bc0d1, 0xf2bcc18f, 0x41113564,
268 0x257b7834, 0x602a9c60, 0xdff8e8a3, 0x1f636c1b,
269 0x0e12b4c2, 0x02e1329e, 0xaf664fd1, 0xcad18115,
270 0x6b2395e0, 0x333e92e1, 0x3b240b62, 0xeebeb922,
271 0x85b2a20e, 0xe6ba0d99, 0xde720c8c, 0x2da2f728,
272 0xd0127845, 0x95b794fd, 0x647d0862, 0xe7ccf5f0,
273 0x5449a36f, 0x877d48fa, 0xc39dfd27, 0xf33e8d1e,
274 0x0a476341, 0x992eff74, 0x3a6f6eab, 0xf4f8fd37,
275 0xa812dc60, 0xa1ebddf8, 0x991be14c, 0xdb6e6b0d,
276 0xc67b5510, 0x6d672c37, 0x2765d43b, 0xdcd0e804,
277 0xf1290dc7, 0xcc00ffa3, 0xb5390f92, 0x690fed0b,
278 0x667b9ffb, 0xcedb7d9c, 0xa091cf0b, 0xd9155ea3,
279 0xbb132f88, 0x515bad24, 0x7b9479bf, 0x763bd6eb,
280 0x37392eb3, 0xcc115979, 0x8026e297, 0xf42e312d,
281 0x6842ada7, 0xc66a2b3b, 0x12754ccc, 0x782ef11c,
282 0x6a124237, 0xb79251e7, 0x06a1bbe6, 0x4bfb6350,
283 0x1a6b1018, 0x11caedfa, 0x3d25bdd8, 0xe2e1c3c9,
284 0x44421659, 0x0a121386, 0xd90cec6e, 0xd5abea2a,
285 0x64af674e, 0xda86a85f, 0xbebfe988, 0x64e4c3fe,
286 0x9dbc8057, 0xf0f7c086, 0x60787bf8, 0x6003604d,
287 0xd1fd8346, 0xf6381fb0, 0x7745ae04, 0xd736fccc,
288 0x83426b33, 0xf01eab71, 0xb0804187, 0x3c005e5f,
289 0x77a057be, 0xbde8ae24, 0x55464299, 0xbf582e61,
290 0x4e58f48f, 0xf2ddfda2, 0xf474ef38, 0x8789bdc2,
291 0x5366f9c3, 0xc8b38e74, 0xb475f255, 0x46fcd9b9,
292 0x7aeb2661, 0x8b1ddf84, 0x846a0e79, 0x915f95e2,
293 0x466e598e, 0x20b45770, 0x8cd55591, 0xc902de4c,
294 0xb90bace1, 0xbb8205d0, 0x11a86248, 0x7574a99e,
295 0xb77f19b6, 0xe0a9dc09, 0x662d09a1, 0xc4324633,
296 0xe85a1f02, 0x09f0be8c, 0x4a99a025, 0x1d6efe10,
297 0x1ab93d1d, 0x0ba5a4df, 0xa186f20f, 0x2868f169,
298 0xdcb7da83, 0x573906fe, 0xa1e2ce9b, 0x4fcd7f52,
299 0x50115e01, 0xa70683fa, 0xa002b5c4, 0x0de6d027,
300 0x9af88c27, 0x773f8641, 0xc3604c06, 0x61a806b5,
301 0xf0177a28, 0xc0f586e0, 0x006058aa, 0x30dc7d62,
302 0x11e69ed7, 0x2338ea63, 0x53c2dd94, 0xc2c21634,
303 0xbbcbee56, 0x90bcb6de, 0xebfc7da1, 0xce591d76,
304 0x6f05e409, 0x4b7c0188, 0x39720a3d, 0x7c927c24,
305 0x86e3725f, 0x724d9db9, 0x1ac15bb4, 0xd39eb8fc,
306 0xed545578, 0x08fca5b5, 0xd83d7cd3, 0x4dad0fc4,
307 0x1e50ef5e, 0xb161e6f8, 0xa28514d9, 0x6c51133c,
308 0x6fd5c7e7, 0x56e14ec4, 0x362abfce, 0xddc6c837,
309 0xd79a3234, 0x92638212, 0x670efa8e, 0x406000e0},
310 {
311 0x3a39ce37, 0xd3faf5cf, 0xabc27737, 0x5ac52d1b,
312 0x5cb0679e, 0x4fa33742, 0xd3822740, 0x99bc9bbe,
313 0xd5118e9d, 0xbf0f7315, 0xd62d1c7e, 0xc700c47b,
314 0xb78c1b6b, 0x21a19045, 0xb26eb1be, 0x6a366eb4,
315 0x5748ab2f, 0xbc946e79, 0xc6a376d2, 0x6549c2c8,
316 0x530ff8ee, 0x468dde7d, 0xd5730a1d, 0x4cd04dc6,
317 0x2939bbdb, 0xa9ba4650, 0xac9526e8, 0xbe5ee304,
318 0xa1fad5f0, 0x6a2d519a, 0x63ef8ce2, 0x9a86ee22,
319 0xc089c2b8, 0x43242ef6, 0xa51e03aa, 0x9cf2d0a4,
320 0x83c061ba, 0x9be96a4d, 0x8fe51550, 0xba645bd6,
321 0x2826a2f9, 0xa73a3ae1, 0x4ba99586, 0xef5562e9,
322 0xc72fefd3, 0xf752f7da, 0x3f046f69, 0x77fa0a59,
323 0x80e4a915, 0x87b08601, 0x9b09e6ad, 0x3b3ee593,
324 0xe990fd5a, 0x9e34d797, 0x2cf0b7d9, 0x022b8b51,
325 0x96d5ac3a, 0x017da67d, 0xd1cf3ed6, 0x7c7d2d28,
326 0x1f9f25cf, 0xadf2b89b, 0x5ad6b472, 0x5a88f54c,
327 0xe029ac71, 0xe019a5e6, 0x47b0acfd, 0xed93fa9b,
328 0xe8d3c48d, 0x283b57cc, 0xf8d56629, 0x79132e28,
329 0x785f0191, 0xed756055, 0xf7960e44, 0xe3d35e8c,
330 0x15056dd4, 0x88f46dba, 0x03a16125, 0x0564f0bd,
331 0xc3eb9e15, 0x3c9057a2, 0x97271aec, 0xa93a072a,
332 0x1b3f6d9b, 0x1e6321f5, 0xf59c66fb, 0x26dcf319,
333 0x7533d928, 0xb155fdf5, 0x03563482, 0x8aba3cbb,
334 0x28517711, 0xc20ad9f8, 0xabcc5167, 0xccad925f,
335 0x4de81751, 0x3830dc8e, 0x379d5862, 0x9320f991,
336 0xea7a90c2, 0xfb3e7bce, 0x5121ce64, 0x774fbe32,
337 0xa8b6e37e, 0xc3293d46, 0x48de5369, 0x6413e680,
338 0xa2ae0810, 0xdd6db224, 0x69852dfd, 0x09072166,
339 0xb39a460a, 0x6445c0dd, 0x586cdecf, 0x1c20c8ae,
340 0x5bbef7dd, 0x1b588d40, 0xccd2017f, 0x6bb4e3bb,
341 0xdda26a7e, 0x3a59ff45, 0x3e350a44, 0xbcb4cdd5,
342 0x72eacea8, 0xfa6484bb, 0x8d6612ae, 0xbf3c6f47,
343 0xd29be463, 0x542f5d9e, 0xaec2771b, 0xf64e6370,
344 0x740e0d8d, 0xe75b1357, 0xf8721671, 0xaf537d5d,
345 0x4040cb08, 0x4eb4e2cc, 0x34d2466a, 0x0115af84,
346 0xe1b00428, 0x95983a1d, 0x06b89fb4, 0xce6ea048,
347 0x6f3f3b82, 0x3520ab82, 0x011a1d4b, 0x277227f8,
348 0x611560b1, 0xe7933fdc, 0xbb3a792b, 0x344525bd,
349 0xa08839e1, 0x51ce794b, 0x2f32c9b7, 0xa01fbac9,
350 0xe01cc87e, 0xbcc7d1f6, 0xcf0111c3, 0xa1e8aac7,
351 0x1a908749, 0xd44fbd9a, 0xd0dadecb, 0xd50ada38,
352 0x0339c32a, 0xc6913667, 0x8df9317c, 0xe0b12b4f,
353 0xf79e59b7, 0x43f5bb3a, 0xf2d519ff, 0x27d9459c,
354 0xbf97222c, 0x15e6fc2a, 0x0f91fc71, 0x9b941525,
355 0xfae59361, 0xceb69ceb, 0xc2a86459, 0x12baa8d1,
356 0xb6c1075e, 0xe3056a0c, 0x10d25065, 0xcb03a442,
357 0xe0ec6e0e, 0x1698db3b, 0x4c98a0be, 0x3278e964,
358 0x9f1f9532, 0xe0d392df, 0xd3a0342b, 0x8971f21e,
359 0x1b0a7441, 0x4ba3348c, 0xc5be7120, 0xc37632d8,
360 0xdf359f8d, 0x9b992f2e, 0xe60b6f47, 0x0fe3f11d,
361 0xe54cda54, 0x1edad891, 0xce6279cf, 0xcd3e7e6f,
362 0x1618b166, 0xfd2c1d05, 0x848fd2c5, 0xf6fb2299,
363 0xf523f357, 0xa6327623, 0x93a83531, 0x56cccd02,
364 0xacf08162, 0x5a75ebb5, 0x6e163697, 0x88d273cc,
365 0xde966292, 0x81b949d0, 0x4c50901b, 0x71c65614,
366 0xe6c6c7bd, 0x327a140a, 0x45e1d006, 0xc3f27b9a,
367 0xc9aa53fd, 0x62a80f00, 0xbb25bfe2, 0x35bdd2f6,
368 0x71126905, 0xb2040222, 0xb6cbcf7c, 0xcd769c2b,
369 0x53113ec0, 0x1640e3d3, 0x38abbd60, 0x2547adf0,
370 0xba38209c, 0xf746ce76, 0x77afa1c5, 0x20756060,
371 0x85cbfe4e, 0x8ae88dd8, 0x7aaaf9b0, 0x4cf9aa7e,
372 0x1948c25c, 0x02fb8a8c, 0x01c36ae4, 0xd6ebe1f9,
373 0x90d4f869, 0xa65cdea0, 0x3f09252d, 0xc208e69f,
374 0xb74e6132, 0xce77e25b, 0x578fdfe3, 0x3ac372e6}
375 },
376 {
377 0x243f6a88, 0x85a308d3, 0x13198a2e, 0x03707344,
378 0xa4093822, 0x299f31d0, 0x082efa98, 0xec4e6c89,
379 0x452821e6, 0x38d01377, 0xbe5466cf, 0x34e90c6c,
380 0xc0ac29b7, 0xc97c50dd, 0x3f84d5b5, 0xb5470917,
381 0x9216d5d9, 0x8979fb1b
382 } };
383
384 *c = initstate;
385}
386DEF_WEAK(Blowfish_initstate);
387
388u_int32_t
389Blowfish_stream2word(const u_int8_t *data, u_int16_t databytes,
390 u_int16_t *current)
391{
392 u_int8_t i;
393 u_int16_t j;
394 u_int32_t temp;
395
396 temp = 0x00000000;
397 j = *current;
398
399 for (i = 0; i < 4; i++, j++) {
400 if (j >= databytes)
401 j = 0;
402 temp = (temp << 8) | data[j];
403 }
404
405 *current = j;
406 return temp;
407}
408DEF_WEAK(Blowfish_stream2word);
409
410void
411Blowfish_expand0state(blf_ctx *c, const u_int8_t *key, u_int16_t keybytes)
412{
413 u_int16_t i;
414 u_int16_t j;
415 u_int16_t k;
416 u_int32_t temp;
417 u_int32_t datal;
418 u_int32_t datar;
419
420 j = 0;
421 for (i = 0; i < BLF_N + 2; i++) {
422 /* Extract 4 int8 to 1 int32 from keystream */
423 temp = Blowfish_stream2word(key, keybytes, &j);
424 c->P[i] = c->P[i] ^ temp;
425 }
426
427 j = 0;
428 datal = 0x00000000;
429 datar = 0x00000000;
430 for (i = 0; i < BLF_N + 2; i += 2) {
431 Blowfish_encipher(c, &datal, &datar);
432
433 c->P[i] = datal;
434 c->P[i + 1] = datar;
435 }
436
437 for (i = 0; i < 4; i++) {
438 for (k = 0; k < 256; k += 2) {
439 Blowfish_encipher(c, &datal, &datar);
440
441 c->S[i][k] = datal;
442 c->S[i][k + 1] = datar;
443 }
444 }
445}
446DEF_WEAK(Blowfish_expand0state);
447
448
449void
450Blowfish_expandstate(blf_ctx *c, const u_int8_t *data, u_int16_t databytes,
451 const u_int8_t *key, u_int16_t keybytes)
452{
453 u_int16_t i;
454 u_int16_t j;
455 u_int16_t k;
456 u_int32_t temp;
457 u_int32_t datal;
458 u_int32_t datar;
459
460 j = 0;
461 for (i = 0; i < BLF_N + 2; i++) {
462 /* Extract 4 int8 to 1 int32 from keystream */
463 temp = Blowfish_stream2word(key, keybytes, &j);
464 c->P[i] = c->P[i] ^ temp;
465 }
466
467 j = 0;
468 datal = 0x00000000;
469 datar = 0x00000000;
470 for (i = 0; i < BLF_N + 2; i += 2) {
471 datal ^= Blowfish_stream2word(data, databytes, &j);
472 datar ^= Blowfish_stream2word(data, databytes, &j);
473 Blowfish_encipher(c, &datal, &datar);
474
475 c->P[i] = datal;
476 c->P[i + 1] = datar;
477 }
478
479 for (i = 0; i < 4; i++) {
480 for (k = 0; k < 256; k += 2) {
481 datal ^= Blowfish_stream2word(data, databytes, &j);
482 datar ^= Blowfish_stream2word(data, databytes, &j);
483 Blowfish_encipher(c, &datal, &datar);
484
485 c->S[i][k] = datal;
486 c->S[i][k + 1] = datar;
487 }
488 }
489
490}
491DEF_WEAK(Blowfish_expandstate);
492
493void
494blf_key(blf_ctx *c, const u_int8_t *k, u_int16_t len)
495{
496 /* Initialize S-boxes and subkeys with Pi */
497 Blowfish_initstate(c);
498
499 /* Transform S-boxes and subkeys with key */
500 Blowfish_expand0state(c, k, len);
501}
502DEF_WEAK(blf_key);
503
504void
505blf_enc(blf_ctx *c, u_int32_t *data, u_int16_t blocks)
506{
507 u_int32_t *d;
508 u_int16_t i;
509
510 d = data;
511 for (i = 0; i < blocks; i++) {
512 Blowfish_encipher(c, d, d + 1);
513 d += 2;
514 }
515}
516DEF_WEAK(blf_enc);
517
518void
519blf_dec(blf_ctx *c, u_int32_t *data, u_int16_t blocks)
520{
521 u_int32_t *d;
522 u_int16_t i;
523
524 d = data;
525 for (i = 0; i < blocks; i++) {
526 Blowfish_decipher(c, d, d + 1);
527 d += 2;
528 }
529}
530DEF_WEAK(blf_dec);
531
532void
533blf_ecb_encrypt(blf_ctx *c, u_int8_t *data, u_int32_t len)
534{
535 u_int32_t l, r;
536 u_int32_t i;
537
538 for (i = 0; i < len; i += 8) {
539 l = data[0] << 24 | data[1] << 16 | data[2] << 8 | data[3];
540 r = data[4] << 24 | data[5] << 16 | data[6] << 8 | data[7];
541 Blowfish_encipher(c, &l, &r);
542 data[0] = l >> 24 & 0xff;
543 data[1] = l >> 16 & 0xff;
544 data[2] = l >> 8 & 0xff;
545 data[3] = l & 0xff;
546 data[4] = r >> 24 & 0xff;
547 data[5] = r >> 16 & 0xff;
548 data[6] = r >> 8 & 0xff;
549 data[7] = r & 0xff;
550 data += 8;
551 }
552}
553DEF_WEAK(blf_ecb_encrypt);
554
555void
556blf_ecb_decrypt(blf_ctx *c, u_int8_t *data, u_int32_t len)
557{
558 u_int32_t l, r;
559 u_int32_t i;
560
561 for (i = 0; i < len; i += 8) {
562 l = data[0] << 24 | data[1] << 16 | data[2] << 8 | data[3];
563 r = data[4] << 24 | data[5] << 16 | data[6] << 8 | data[7];
564 Blowfish_decipher(c, &l, &r);
565 data[0] = l >> 24 & 0xff;
566 data[1] = l >> 16 & 0xff;
567 data[2] = l >> 8 & 0xff;
568 data[3] = l & 0xff;
569 data[4] = r >> 24 & 0xff;
570 data[5] = r >> 16 & 0xff;
571 data[6] = r >> 8 & 0xff;
572 data[7] = r & 0xff;
573 data += 8;
574 }
575}
576DEF_WEAK(blf_ecb_decrypt);
577
578void
579blf_cbc_encrypt(blf_ctx *c, u_int8_t *iv, u_int8_t *data, u_int32_t len)
580{
581 u_int32_t l, r;
582 u_int32_t i, j;
583
584 for (i = 0; i < len; i += 8) {
585 for (j = 0; j < 8; j++)
586 data[j] ^= iv[j];
587 l = data[0] << 24 | data[1] << 16 | data[2] << 8 | data[3];
588 r = data[4] << 24 | data[5] << 16 | data[6] << 8 | data[7];
589 Blowfish_encipher(c, &l, &r);
590 data[0] = l >> 24 & 0xff;
591 data[1] = l >> 16 & 0xff;
592 data[2] = l >> 8 & 0xff;
593 data[3] = l & 0xff;
594 data[4] = r >> 24 & 0xff;
595 data[5] = r >> 16 & 0xff;
596 data[6] = r >> 8 & 0xff;
597 data[7] = r & 0xff;
598 iv = data;
599 data += 8;
600 }
601}
602DEF_WEAK(blf_cbc_encrypt);
603
604void
605blf_cbc_decrypt(blf_ctx *c, u_int8_t *iva, u_int8_t *data, u_int32_t len)
606{
607 u_int32_t l, r;
608 u_int8_t *iv;
609 u_int32_t i, j;
610
611 iv = data + len - 16;
612 data = data + len - 8;
613 for (i = len - 8; i >= 8; i -= 8) {
614 l = data[0] << 24 | data[1] << 16 | data[2] << 8 | data[3];
615 r = data[4] << 24 | data[5] << 16 | data[6] << 8 | data[7];
616 Blowfish_decipher(c, &l, &r);
617 data[0] = l >> 24 & 0xff;
618 data[1] = l >> 16 & 0xff;
619 data[2] = l >> 8 & 0xff;
620 data[3] = l & 0xff;
621 data[4] = r >> 24 & 0xff;
622 data[5] = r >> 16 & 0xff;
623 data[6] = r >> 8 & 0xff;
624 data[7] = r & 0xff;
625 for (j = 0; j < 8; j++)
626 data[j] ^= iv[j];
627 iv -= 8;
628 data -= 8;
629 }
630 l = data[0] << 24 | data[1] << 16 | data[2] << 8 | data[3];
631 r = data[4] << 24 | data[5] << 16 | data[6] << 8 | data[7];
632 Blowfish_decipher(c, &l, &r);
633 data[0] = l >> 24 & 0xff;
634 data[1] = l >> 16 & 0xff;
635 data[2] = l >> 8 & 0xff;
636 data[3] = l & 0xff;
637 data[4] = r >> 24 & 0xff;
638 data[5] = r >> 16 & 0xff;
639 data[6] = r >> 8 & 0xff;
640 data[7] = r & 0xff;
641 for (j = 0; j < 8; j++)
642 data[j] ^= iva[j];
643}
644DEF_WEAK(blf_cbc_decrypt);
645
646#if 0
647void
648report(u_int32_t data[], u_int16_t len)
649{
650 u_int16_t i;
651 for (i = 0; i < len; i += 2)
652 printf("Block %0hd: %08lx %08lx.\n",
653 i / 2, data[i], data[i + 1]);
654}
655void
656main(void)
657{
658
659 blf_ctx c;
660 char key[] = "AAAAA";
661 char key2[] = "abcdefghijklmnopqrstuvwxyz";
662
663 u_int32_t data[10];
664 u_int32_t data2[] =
665 {0x424c4f57l, 0x46495348l};
666
667 u_int16_t i;
668
669 /* First test */
670 for (i = 0; i < 10; i++)
671 data[i] = i;
672
673 blf_key(&c, (u_int8_t *) key, 5);
674 blf_enc(&c, data, 5);
675 blf_dec(&c, data, 1);
676 blf_dec(&c, data + 2, 4);
677 printf("Should read as 0 - 9.\n");
678 report(data, 10);
679
680 /* Second test */
681 blf_key(&c, (u_int8_t *) key2, strlen(key2));
682 blf_enc(&c, data2, 1);
683 printf("\nShould read as: 0x324ed0fe 0xf413a203.\n");
684 report(data2, 2);
685 blf_dec(&c, data2, 1);
686 report(data2, 2);
687}
688#endif
diff --git a/src/lib/libc/crypt/chacha_private.h b/src/lib/libc/crypt/chacha_private.h
deleted file mode 100644
index b0427b6b3e..0000000000
--- a/src/lib/libc/crypt/chacha_private.h
+++ /dev/null
@@ -1,222 +0,0 @@
1/*
2chacha-merged.c version 20080118
3D. J. Bernstein
4Public domain.
5*/
6
7/* $OpenBSD: chacha_private.h,v 1.3 2022/02/28 21:56:29 dtucker Exp $ */
8
9typedef unsigned char u8;
10typedef unsigned int u32;
11
12typedef struct
13{
14 u32 input[16]; /* could be compressed */
15} chacha_ctx;
16
17#define U8C(v) (v##U)
18#define U32C(v) (v##U)
19
20#define U8V(v) ((u8)(v) & U8C(0xFF))
21#define U32V(v) ((u32)(v) & U32C(0xFFFFFFFF))
22
23#define ROTL32(v, n) \
24 (U32V((v) << (n)) | ((v) >> (32 - (n))))
25
26#define U8TO32_LITTLE(p) \
27 (((u32)((p)[0]) ) | \
28 ((u32)((p)[1]) << 8) | \
29 ((u32)((p)[2]) << 16) | \
30 ((u32)((p)[3]) << 24))
31
32#define U32TO8_LITTLE(p, v) \
33 do { \
34 (p)[0] = U8V((v) ); \
35 (p)[1] = U8V((v) >> 8); \
36 (p)[2] = U8V((v) >> 16); \
37 (p)[3] = U8V((v) >> 24); \
38 } while (0)
39
40#define ROTATE(v,c) (ROTL32(v,c))
41#define XOR(v,w) ((v) ^ (w))
42#define PLUS(v,w) (U32V((v) + (w)))
43#define PLUSONE(v) (PLUS((v),1))
44
45#define QUARTERROUND(a,b,c,d) \
46 a = PLUS(a,b); d = ROTATE(XOR(d,a),16); \
47 c = PLUS(c,d); b = ROTATE(XOR(b,c),12); \
48 a = PLUS(a,b); d = ROTATE(XOR(d,a), 8); \
49 c = PLUS(c,d); b = ROTATE(XOR(b,c), 7);
50
51static const char sigma[16] = "expand 32-byte k";
52static const char tau[16] = "expand 16-byte k";
53
54static void
55chacha_keysetup(chacha_ctx *x,const u8 *k,u32 kbits)
56{
57 const char *constants;
58
59 x->input[4] = U8TO32_LITTLE(k + 0);
60 x->input[5] = U8TO32_LITTLE(k + 4);
61 x->input[6] = U8TO32_LITTLE(k + 8);
62 x->input[7] = U8TO32_LITTLE(k + 12);
63 if (kbits == 256) { /* recommended */
64 k += 16;
65 constants = sigma;
66 } else { /* kbits == 128 */
67 constants = tau;
68 }
69 x->input[8] = U8TO32_LITTLE(k + 0);
70 x->input[9] = U8TO32_LITTLE(k + 4);
71 x->input[10] = U8TO32_LITTLE(k + 8);
72 x->input[11] = U8TO32_LITTLE(k + 12);
73 x->input[0] = U8TO32_LITTLE(constants + 0);
74 x->input[1] = U8TO32_LITTLE(constants + 4);
75 x->input[2] = U8TO32_LITTLE(constants + 8);
76 x->input[3] = U8TO32_LITTLE(constants + 12);
77}
78
79static void
80chacha_ivsetup(chacha_ctx *x,const u8 *iv)
81{
82 x->input[12] = 0;
83 x->input[13] = 0;
84 x->input[14] = U8TO32_LITTLE(iv + 0);
85 x->input[15] = U8TO32_LITTLE(iv + 4);
86}
87
88static void
89chacha_encrypt_bytes(chacha_ctx *x,const u8 *m,u8 *c,u32 bytes)
90{
91 u32 x0, x1, x2, x3, x4, x5, x6, x7, x8, x9, x10, x11, x12, x13, x14, x15;
92 u32 j0, j1, j2, j3, j4, j5, j6, j7, j8, j9, j10, j11, j12, j13, j14, j15;
93 u8 *ctarget = NULL;
94 u8 tmp[64];
95 u_int i;
96
97 if (!bytes) return;
98
99 j0 = x->input[0];
100 j1 = x->input[1];
101 j2 = x->input[2];
102 j3 = x->input[3];
103 j4 = x->input[4];
104 j5 = x->input[5];
105 j6 = x->input[6];
106 j7 = x->input[7];
107 j8 = x->input[8];
108 j9 = x->input[9];
109 j10 = x->input[10];
110 j11 = x->input[11];
111 j12 = x->input[12];
112 j13 = x->input[13];
113 j14 = x->input[14];
114 j15 = x->input[15];
115
116 for (;;) {
117 if (bytes < 64) {
118 for (i = 0;i < bytes;++i) tmp[i] = m[i];
119 m = tmp;
120 ctarget = c;
121 c = tmp;
122 }
123 x0 = j0;
124 x1 = j1;
125 x2 = j2;
126 x3 = j3;
127 x4 = j4;
128 x5 = j5;
129 x6 = j6;
130 x7 = j7;
131 x8 = j8;
132 x9 = j9;
133 x10 = j10;
134 x11 = j11;
135 x12 = j12;
136 x13 = j13;
137 x14 = j14;
138 x15 = j15;
139 for (i = 20;i > 0;i -= 2) {
140 QUARTERROUND( x0, x4, x8,x12)
141 QUARTERROUND( x1, x5, x9,x13)
142 QUARTERROUND( x2, x6,x10,x14)
143 QUARTERROUND( x3, x7,x11,x15)
144 QUARTERROUND( x0, x5,x10,x15)
145 QUARTERROUND( x1, x6,x11,x12)
146 QUARTERROUND( x2, x7, x8,x13)
147 QUARTERROUND( x3, x4, x9,x14)
148 }
149 x0 = PLUS(x0,j0);
150 x1 = PLUS(x1,j1);
151 x2 = PLUS(x2,j2);
152 x3 = PLUS(x3,j3);
153 x4 = PLUS(x4,j4);
154 x5 = PLUS(x5,j5);
155 x6 = PLUS(x6,j6);
156 x7 = PLUS(x7,j7);
157 x8 = PLUS(x8,j8);
158 x9 = PLUS(x9,j9);
159 x10 = PLUS(x10,j10);
160 x11 = PLUS(x11,j11);
161 x12 = PLUS(x12,j12);
162 x13 = PLUS(x13,j13);
163 x14 = PLUS(x14,j14);
164 x15 = PLUS(x15,j15);
165
166#ifndef KEYSTREAM_ONLY
167 x0 = XOR(x0,U8TO32_LITTLE(m + 0));
168 x1 = XOR(x1,U8TO32_LITTLE(m + 4));
169 x2 = XOR(x2,U8TO32_LITTLE(m + 8));
170 x3 = XOR(x3,U8TO32_LITTLE(m + 12));
171 x4 = XOR(x4,U8TO32_LITTLE(m + 16));
172 x5 = XOR(x5,U8TO32_LITTLE(m + 20));
173 x6 = XOR(x6,U8TO32_LITTLE(m + 24));
174 x7 = XOR(x7,U8TO32_LITTLE(m + 28));
175 x8 = XOR(x8,U8TO32_LITTLE(m + 32));
176 x9 = XOR(x9,U8TO32_LITTLE(m + 36));
177 x10 = XOR(x10,U8TO32_LITTLE(m + 40));
178 x11 = XOR(x11,U8TO32_LITTLE(m + 44));
179 x12 = XOR(x12,U8TO32_LITTLE(m + 48));
180 x13 = XOR(x13,U8TO32_LITTLE(m + 52));
181 x14 = XOR(x14,U8TO32_LITTLE(m + 56));
182 x15 = XOR(x15,U8TO32_LITTLE(m + 60));
183#endif
184
185 j12 = PLUSONE(j12);
186 if (!j12) {
187 j13 = PLUSONE(j13);
188 /* stopping at 2^70 bytes per nonce is user's responsibility */
189 }
190
191 U32TO8_LITTLE(c + 0,x0);
192 U32TO8_LITTLE(c + 4,x1);
193 U32TO8_LITTLE(c + 8,x2);
194 U32TO8_LITTLE(c + 12,x3);
195 U32TO8_LITTLE(c + 16,x4);
196 U32TO8_LITTLE(c + 20,x5);
197 U32TO8_LITTLE(c + 24,x6);
198 U32TO8_LITTLE(c + 28,x7);
199 U32TO8_LITTLE(c + 32,x8);
200 U32TO8_LITTLE(c + 36,x9);
201 U32TO8_LITTLE(c + 40,x10);
202 U32TO8_LITTLE(c + 44,x11);
203 U32TO8_LITTLE(c + 48,x12);
204 U32TO8_LITTLE(c + 52,x13);
205 U32TO8_LITTLE(c + 56,x14);
206 U32TO8_LITTLE(c + 60,x15);
207
208 if (bytes <= 64) {
209 if (bytes < 64) {
210 for (i = 0;i < bytes;++i) ctarget[i] = c[i];
211 }
212 x->input[12] = j12;
213 x->input[13] = j13;
214 return;
215 }
216 bytes -= 64;
217 c += 64;
218#ifndef KEYSTREAM_ONLY
219 m += 64;
220#endif
221 }
222}
diff --git a/src/lib/libc/crypt/crypt.3 b/src/lib/libc/crypt/crypt.3
deleted file mode 100644
index 6a21571ddb..0000000000
--- a/src/lib/libc/crypt/crypt.3
+++ /dev/null
@@ -1,144 +0,0 @@
1.\" $OpenBSD: crypt.3,v 1.46 2025/01/09 23:18:08 jsg Exp $
2.\"
3.\" FreeSec: libcrypt
4.\"
5.\" Copyright (c) 1994 David Burren
6.\" All rights reserved.
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.\" 4. Neither the name of the author nor the names of other 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 AUTHOR 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 AUTHOR 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.\" Manual page, using -mandoc macros
33.\"
34.Dd $Mdocdate: January 9 2025 $
35.Dt CRYPT 3
36.Os
37.Sh NAME
38.Nm crypt ,
39.Nm bcrypt_gensalt ,
40.Nm bcrypt
41.Nd password hashing
42.Sh SYNOPSIS
43.In stdlib.h
44.Pp
45.In unistd.h
46.Ft char *
47.Fn crypt "const char *key" "const char *setting"
48.In pwd.h
49.Ft char *
50.Fn bcrypt_gensalt "u_int8_t log_rounds"
51.Ft char *
52.Fn bcrypt "const char *key" "const char *salt"
53.Sh DESCRIPTION
54These functions are deprecated in favor of
55.Xr crypt_checkpass 3
56and
57.Xr crypt_newhash 3 .
58.Pp
59The
60.Fn crypt
61function performs password hashing.
62Additional code has been added to deter key search attempts and to use
63stronger hashing algorithms.
64.Pp
65The first argument to
66.Fn crypt
67is a NUL-terminated
68string
69.Fa key ,
70typically a user's typed password.
71The second,
72.Fa setting ,
73currently supports a single form.
74If it begins
75with a string character
76.Pq Ql $
77and a number then a different algorithm is used depending on the number.
78At the moment
79.Ql $2
80chooses Blowfish hashing; see below for more information.
81.Ss Blowfish crypt
82The Blowfish version of crypt has 128 bits of
83.Fa salt
84in order to make building dictionaries of common passwords space consuming.
85The initial state of the
86Blowfish cipher is expanded using the
87.Fa salt
88and the
89.Fa password
90repeating the process a variable number of rounds, which is encoded in
91the password string.
92The maximum password length is 72.
93The final Blowfish password entry is created by encrypting the string
94.Pp
95.Dq OrpheanBeholderScryDoubt
96.Pp
97with the Blowfish state 64 times.
98.Pp
99The version number, the logarithm of the number of rounds and
100the concatenation of salt and hashed password are separated by the
101.Ql $
102character.
103An encoded
104.Sq 8
105would specify 256 rounds.
106A valid Blowfish password looks like this:
107.Pp
108.Dq $2b$12$FPWWO2RJ3CK4FINTw0Hi8OiPKJcX653gzSS.jqltHFMxyDmmQ0Hqq .
109.Pp
110The whole Blowfish password string is passed as
111.Fa setting
112for interpretation.
113.Sh RETURN VALUES
114The function
115.Fn crypt
116returns a pointer to the encrypted value on success, and
117.Dv NULL
118on failure.
119.Sh SEE ALSO
120.Xr encrypt 1 ,
121.Xr login 1 ,
122.Xr passwd 1 ,
123.Xr blowfish 3 ,
124.Xr crypt_checkpass 3 ,
125.Xr getpass 3 ,
126.Xr passwd 5
127.Sh HISTORY
128An M-209 based
129.Fn crypt
130function appeared in
131.At v3 .
132A DES-based
133.Fn crypt
134first appeared in
135.At v7 .
136.Fn bcrypt
137first appeared in
138.Ox 2.1 .
139.Sh BUGS
140The
141.Fn crypt
142function returns a pointer to static data, and subsequent calls to
143.Fn crypt
144will modify the same object.
diff --git a/src/lib/libc/crypt/crypt.c b/src/lib/libc/crypt/crypt.c
deleted file mode 100644
index 40d5503544..0000000000
--- a/src/lib/libc/crypt/crypt.c
+++ /dev/null
@@ -1,22 +0,0 @@
1/* $OpenBSD: crypt.c,v 1.31 2015/09/12 14:56:50 guenther Exp $ */
2
3#include <errno.h>
4#include <pwd.h>
5#include <unistd.h>
6
7char *
8crypt(const char *key, const char *setting)
9{
10 if (setting[0] == '$') {
11 switch (setting[1]) {
12 case '2':
13 return bcrypt(key, setting);
14 default:
15 errno = EINVAL;
16 return (NULL);
17 }
18 }
19 errno = EINVAL;
20 return (NULL);
21}
22DEF_WEAK(crypt);
diff --git a/src/lib/libc/crypt/crypt_checkpass.3 b/src/lib/libc/crypt/crypt_checkpass.3
deleted file mode 100644
index 07a77ae7c0..0000000000
--- a/src/lib/libc/crypt/crypt_checkpass.3
+++ /dev/null
@@ -1,113 +0,0 @@
1.\" $OpenBSD: crypt_checkpass.3,v 1.13 2021/10/29 10:54:33 deraadt Exp $
2.\"
3.\" Copyright (c) 2014 Ted Unangst <tedu@openbsd.org>
4.\"
5.\" Permission to use, copy, modify, and distribute this software for any
6.\" purpose with or without fee is hereby granted, provided that the above
7.\" copyright notice and this permission notice appear in all copies.
8.\"
9.\" THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES
10.\" WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF
11.\" MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR
12.\" ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES
13.\" WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN
14.\" ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF
15.\" OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE.
16.\"
17.Dd $Mdocdate: October 29 2021 $
18.Dt CRYPT_CHECKPASS 3
19.Os
20.Sh NAME
21.Nm crypt_checkpass ,
22.Nm crypt_newhash
23.Nd password hashing
24.Sh SYNOPSIS
25.In pwd.h
26.In unistd.h
27.Ft int
28.Fn crypt_checkpass "const char *password" "const char *hash"
29.Ft int
30.Fn crypt_newhash "const char *password" "const char *pref" "char *hash" "size_t hashsize"
31.Sh DESCRIPTION
32The
33.Fn crypt_checkpass
34function simplifies checking a user's password.
35If both the
36.Fa hash
37and the
38.Fa password
39are the empty string, authentication
40is a success.
41Otherwise, the
42.Fa password
43is hashed and compared to the provided
44.Fa hash .
45If the
46.Fa hash
47is
48.Dv NULL ,
49authentication will always fail, but a default
50amount of work is performed to simulate the hashing operation.
51A successful match will return 0.
52A failure will return \-1 and set
53.Xr errno 2 .
54.Pp
55The
56.Fn crypt_newhash
57function simplifies the creation of new password hashes.
58The provided
59.Fa password
60is randomly salted and hashed and stored in
61.Fa hash .
62The size of the available space is specified by
63.Fa hashsize ,
64which should be
65.Dv _PASSWORD_LEN .
66The
67.Fa pref
68argument identifies the preferred hashing algorithm and parameters.
69Possible values are:
70.Bl -tag -width Ds
71.It Dq bcrypt,<rounds>
72The bcrypt algorithm, where the value of rounds can be between 4 and 31 and
73specifies the base 2 logarithm of the number of rounds.
74If rounds is omitted or the special value
75.Sq a ,
76an appropriate number of rounds is automatically selected based on system
77performance.
78.El
79.Sh RETURN VALUES
80.Rv -std crypt_checkpass crypt_newhash
81.Sh ERRORS
82The
83.Fn crypt_checkpass
84function sets
85.Va errno
86to
87.Er EACCES
88when authentication fails.
89.Pp
90The
91.Fn crypt_newhash
92function sets
93.Va errno
94to
95.Er EINVAL
96if
97.Fa pref
98is unsupported or insufficient space is provided.
99.Sh SEE ALSO
100.Xr crypt 3 ,
101.Xr login.conf 5 ,
102.Xr passwd 5
103.Sh HISTORY
104The function
105.Fn crypt_checkpass
106first appeared in
107.Ox 5.6 ,
108and
109.Fn crypt_newhash
110in
111.Ox 5.7 .
112.Sh AUTHORS
113.An Ted Unangst Aq Mt tedu@openbsd.org
diff --git a/src/lib/libc/crypt/cryptutil.c b/src/lib/libc/crypt/cryptutil.c
deleted file mode 100644
index f5fdb29589..0000000000
--- a/src/lib/libc/crypt/cryptutil.c
+++ /dev/null
@@ -1,97 +0,0 @@
1/* $OpenBSD: cryptutil.c,v 1.13 2024/01/22 19:26:55 deraadt Exp $ */
2/*
3 * Copyright (c) 2014 Ted Unangst <tedu@openbsd.org>
4 *
5 * Permission to use, copy, modify, and distribute this software for any
6 * purpose with or without fee is hereby granted, provided that the above
7 * copyright notice and this permission notice appear in all copies.
8 *
9 * THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES
10 * WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF
11 * MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR
12 * ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES
13 * WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN
14 * ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF
15 * OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE.
16 */
17#include <stdlib.h>
18#include <unistd.h>
19#include <string.h>
20#include <pwd.h>
21#include <login_cap.h>
22#include <errno.h>
23
24int
25crypt_checkpass(const char *pass, const char *goodhash)
26{
27 char dummy[_PASSWORD_LEN];
28
29 if (goodhash == NULL) {
30 /* fake it */
31 goto fake;
32 }
33
34 /* empty password */
35 if (strlen(goodhash) == 0 && strlen(pass) == 0)
36 return 0;
37
38 if (goodhash[0] == '$' && goodhash[1] == '2') {
39 if (bcrypt_checkpass(pass, goodhash))
40 goto fail;
41 return 0;
42 }
43
44 /* unsupported. fake it. */
45fake:
46 bcrypt_newhash(pass, 8, dummy, sizeof(dummy));
47fail:
48 errno = EACCES;
49 return -1;
50}
51DEF_WEAK(crypt_checkpass);
52
53int
54crypt_newhash(const char *pass, const char *pref, char *hash, size_t hashlen)
55{
56 int rv = -1;
57 const char *defaultpref = "bcrypt,8";
58 const char *errstr;
59 const char *choices[] = { "blowfish", "bcrypt" };
60 size_t maxchoice = sizeof(choices) / sizeof(choices[0]);
61 int i;
62 int rounds;
63
64 if (pref == NULL)
65 pref = defaultpref;
66
67 for (i = 0; i < maxchoice; i++) {
68 const char *choice = choices[i];
69 size_t len = strlen(choice);
70 if (strcmp(pref, choice) == 0) {
71 rounds = _bcrypt_autorounds();
72 break;
73 } else if (strncmp(pref, choice, len) == 0 &&
74 pref[len] == ',') {
75 if (strcmp(pref + len + 1, "a") == 0) {
76 rounds = _bcrypt_autorounds();
77 } else {
78 rounds = strtonum(pref + len + 1, 4, 31, &errstr);
79 if (errstr) {
80 errno = EINVAL;
81 goto err;
82 }
83 }
84 break;
85 }
86 }
87 if (i == maxchoice) {
88 errno = EINVAL;
89 goto err;
90 }
91
92 rv = bcrypt_newhash(pass, rounds, hash, hashlen);
93
94err:
95 return rv;
96}
97DEF_WEAK(crypt_newhash);
diff --git a/src/lib/libc/include/DETAILS b/src/lib/libc/include/DETAILS
deleted file mode 100644
index 6382cc1c0d..0000000000
--- a/src/lib/libc/include/DETAILS
+++ /dev/null
@@ -1,133 +0,0 @@
1The goal: calls from inside libc to other libc functions should
2 * not be overridable (except for the malloc family), and
3 * not have pointless inefficiencies.
4
5To achieve this, we arrange that all these internal call be via
6identifiers that are of hidden visibility and--to avoid confusion
7and work correctly in static executables--are in the reserved
8namespace.
9
10This document describes the details of the naming scheme and how
11it is implemented.
12
13I've chosen a prefix of underbar-libc-underbar ("_libc_") for this.
14These are not declared directly; instead, the gcc "asm labels"
15extension is used to rename the function.
16
17We need many of the symbols to be weak in *static* builds, but they
18can be strong in the dynamic library, as there's a natural precedence
19from the search order there. When the descriptions below say a
20name is "weak", that is only necessary for the static library and
21not for the shared library. Note: use defined(PIC) to recognize
22when compiling the shared objects: some archs define __PIC__ *all*
23the time.
24
25---------
26
27For syscalls which are not cancellation points, such as getpid(),
28the identifiers are just:
29 _libc_getpid hidden alias, for use internal to libc only
30 _thread_sys_getpid global name, for use outside libc only
31 getpid weak alias, for use outside libc only
32
33For syscalls which are cancellation points, such as wait4(), there
34are identifiers that do not provide cancellation:
35 _libc_wait4 hidden alias, for use internal to libc only
36 _thread_sys_wait4 global name, for use outside libc only
37...and identifiers that do provide cancellation:
38 wait4 weak alias, for general use
39 _libc_wait4_cancel hidden name, for use internal to libc only
40Inside libc, the bare name ("wait4") binds to the version *with*
41cancellation. If it's necessary to use the syscall without doing
42cancellation it can be obtained by calling HIDDEN(x) instead of
43just x.
44
45Some other calls need to be wrapped for reasons other than cancellation,
46such as to provide functionality beyond the underlying syscall (e.g.,
47ptrace). For these, there are identifiers for the raw call, without
48the wrapping:
49 _libc_ptrace hidden alias, for use internal to libc only
50 _thread_sys_ptrace global name, for use outside libc only
51...and identifiers that do provide the libc wrapping:
52 ptrace weak alias, for general use
53 _libc_ptrace_wrap hidden name, for use internal to libc only
54Inside libc, the bare name ("ptrace") binds to the wrapper; if
55the raw version is necessary it can be obtained by calling HIDDEN(x)
56instead of just x.
57
58For syscalls implemented in ASM, the aliases of the raw syscall stub
59are provided by the ASM macros. Of the macros used by sys/Makefile.inc:
60RSYSCALL(), PSEUDO(), and PSEUDO_NOERROR() generate all three names
61(ala getpid() above), while RSYSCALL_HIDDEN() generates just the
62_thread_sys_x and _libc_x names.
63
64
65
66By using gcc's "asm label" extension, we can usually avoid having
67to type those prefixes in the .h and .c files. However, for those
68cases where a non-default binding is necessary we can use these macros
69to get the desired identifier:
70
71 CANCEL(x)
72 This expands to the internal, hidden name of a cancellation
73 wrapper: _libc_x_cancel. ex: CANCEL(fsync)(fd)
74
75 WRAP(x)
76 This expands to the internal, hidden name of a non-cancellation
77 wrapper: _libc_x_wrap. ex: WRAP(sigprocmask)(set)
78
79
80In order to actually set up the desired asm labels, we use these in
81the internal .h files:
82 PROTO_NORMAL(x) Symbols used both internally and externally
83 This makes gcc convert use of x to use _libc_x instead
84 ex: PROTO_NORMAL(getpid)
85
86 PROTO_STD_DEPRECATED(x) Standard C symbols that we don't want to use
87 This just marks the symbol as deprecated, with no renaming.
88 ex: PROTO_STD_DEPRECATED(strcpy)
89
90 PROTO_DEPRECATED(x) Symbols not in ISO C that we don't want to use
91 This marks the symbol as both weak and deprecated, with no renaming
92 ex: PROTO_DEPRECATED(creat)
93
94 PROTO_CANCEL(x) Functions that have cancellation wrappers
95 Like PROTO_NORMAL(x), but also declares _libc_x_cancel
96 ex: PROTO_CANCEL(wait4)
97
98 PROTO_WRAP(x) Functions that have wrappers for other reasons
99 Like PROTO_NORMAL(x), but also declares _libc_x_wrap. Internal
100 calls that want the wrapper's processing should invoke WRAP(x)(...)
101 ex: PROTO_WRAP(sigaction)
102
103
104Finally, to create the expected aliases, we use these in the .c files
105where the definitions are:
106 DEF_STRONG(x) Symbols reserved to or specified by ISO C
107 This defines x as a strong alias for _libc_x; this must only
108 be used for symbols that are reserved by the C standard
109 (or reserved in the external identifier namespace).
110 Matches with PROTO_NORMAL()
111 ex: DEF_STRONG(fopen)
112
113 DEF_WEAK(x) Symbols used internally and not in ISO C
114 This defines x as a weak alias for _libc_x
115 Matches with PROTO_NORMAL()
116 ex: DEF_WEAK(lseek)
117
118 DEF_CANCEL(x) Symbols that have a cancellation wrapper
119 This defines x as a weak alias for _libc_x_cancel.
120 Matches with PROTO_CANCEL()
121 ex: DEF_CANCEL(read)
122
123 DEF_WRAP(x)
124 This defines x as a weak alias for _libc_x_wrap.
125 Matches with PROTO_WRAP()
126 ex: DEF_WRAP(ptrace)
127
128 MAKE_CLONE(dst, src) Symbols that are exact clones of other symbols
129 This declares _libc_dst as being the same type as dst, and makes
130 _libc_dst a strong, hidden alias for _libc_src. You still need to
131 DEF_STRONG(dst) or DEF_WEAK(dst) to alias dst itself
132 ex: MAKE_CLONE(SHA224Pad, SHA256Pad)
133
diff --git a/src/lib/libc/include/README b/src/lib/libc/include/README
deleted file mode 100644
index 89acee491e..0000000000
--- a/src/lib/libc/include/README
+++ /dev/null
@@ -1,110 +0,0 @@
1So you want to add an interface to libc...
2
3CASE I: internal symbols
4
5 A) used in a single file
6 Duh: use whatever name you want and make it static
7
8 B) used in multiple files
9 Give it a name in the implementation namespace (leading underbar)
10 and declare it in a __{BEGIN,END}_HIDDEN_DECLS block in a .h
11 file inside libc. If it's used in just a single subdir of
12 libc *and* that subdir has an appropriate .h file in it, then
13 declare it there.
14 Example: stdio/local.h.
15 Otherwise, declare it in one of the hidden/* files.
16 Example: _mktemp() in hidden/stdio.h
17
18CASE II: external symbols
19
20 First of all, add your symbol to Symbols.list. MD symbols go in
21 arch/*/Symbols.list (shock, I know)
22
23 Declare your function in the appropriate header. It almost certainly
24 should be in a public header installed under /usr/include/. Exceptions
25 are symbols that are just shared between libc and libpthread/csu/ld.so
26 which are only declared in libc/include/* or just in each .c file.
27
28 A) objects (variables)
29 That's it, you're done.
30
31
32 B) plain C functions (not syscalls)
33 1) functions that are *not* called from inside libc
34
35 If this function is specified in the ISO C standard or its
36 name begins with an underbar, then in the hidden/* version
37 of the header where you declared the function, add this line:
38 PROTO_STD_DEPRECATED(your_function_name);
39
40 Otherwise, this is *not* a function specified in the ISO C
41 standard and its name begins with a letter. In the hidden/*
42 version of the header where you declared the function, add
43 this line:
44 PROTO_DEPRECATED(your_function_name);
45
46 Note: the "DEPRECATED" suffix is about a detail of
47 how the macros work and has nothing to do with whether the
48 function itself is a Good Thing vs deprecated.
49
50 2) functions that are called from inside libc
51
52 In the hidden/* version of the header where you declared
53 the function, add this line:
54 PROTO_NORMAL(your_function_name);
55
56 Then, in the .c file(s) where the function is defined:
57 - if the function is specified in the ISO C standard or
58 its name begins with an underbar, add
59 DEF_STRONG(your_function_name);
60
61 - otherwise, add:
62 DEF_WEAK(your_function_name);
63
64
65 C) syscalls that don't require any wrapping
66
67 In the hidden/* version of the header where you declared the
68 function, add this line:
69 PROTO_NORMAL(your_function_name);
70
71 Generate the stub by adding it to the ASM variable in
72 libc/sys/Makefile.inc
73
74
75 D) syscalls that require cancellation or similar wrappers that don't
76 change the function signature
77
78 Generate the stub by adding it to the HIDDEN (*not* ASM!)
79 variable in libc/sys/Makefile.inc
80
81 In the hidden/* version of the header where you declared the
82 function, add this line:
83 PROTO_WRAP(your_function_name);
84
85 The wrapper function should be defined in
86 libc/sys/w_your_function_name.c
87 which should define WRAP(your_function_name) and follow it
88 with DEF_WRAP(your_function_name). Look at libc/sys/w_sigaction.c
89 for an example.
90
91 By default, libc code that calls your_function_name() will get
92 the real syscall and *not* the wrapper. libc calls that need
93 to go through the wrapper should invoke WRAP(your_function_name)
94
95
96 E) syscalls that require libc wrappers for other reasons
97 First of all, make sure this really is the Right Thing. Talk
98 with kettenis, deraadt, millert, and guenther.
99
100 If the actual syscall doesn't have the same calling convention
101 as the C interface, then maybe it shouldn't be exported at all
102 and you should just have an ASM stub, like SYS___tfork -->
103 __tfork_thread() or SYS_break --> brk() and sbrk(). If it
104 _could_ be called from C, then give the syscall a name different
105 from the C API. Syscalls that fail this for historical reasons
106 (e.g., exit == _exit(2)) are generated with PSEUDO/PSEUDO_NOERR
107 in libc/sys/Makefile.inc, so the ASM stub has a leading underbar.
108 Go read gen/getlogin.c rev 1.13 for an example of how to do
109 this, but don't pick this option, really.
110
diff --git a/src/lib/libc/include/atfork.h b/src/lib/libc/include/atfork.h
deleted file mode 100644
index f09de4892a..0000000000
--- a/src/lib/libc/include/atfork.h
+++ /dev/null
@@ -1,45 +0,0 @@
1/* $OpenBSD: atfork.h,v 1.2 2015/08/27 04:37:09 guenther Exp $ */
2
3/*
4 * Copyright (c) 2008 Kurt Miller <kurt@openbsd.org>
5 * Copyright (c) 2008 Philip Guenther <guenther@openbsd.org>
6 * Copyright (c) 2003 Daniel Eischen <deischen@freebsd.org>
7 * All rights reserved.
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. Neither the name of the author nor the names of any co-contributors
15 * may be used to endorse or promote products derived from this software
16 * without specific prior written permission.
17 *
18 * THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND
19 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
20 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
21 * ARE DISCLAIMED. IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE
22 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
23 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
24 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
25 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
26 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
27 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
28 * SUCH DAMAGE.
29 *
30 * $FreeBSD: /repoman/r/ncvs/src/lib/libc_r/uthread/uthread_atfork.c,v 1.1 2004/12/10 03:36:45 grog Exp $
31 */
32
33#include <sys/queue.h>
34
35struct atfork_fn {
36 TAILQ_ENTRY(atfork_fn) fn_next;
37 void (*fn_prepare)(void);
38 void (*fn_parent)(void);
39 void (*fn_child)(void);
40 void *fn_dso;
41};
42
43__BEGIN_HIDDEN_DECLS
44extern TAILQ_HEAD(atfork_listhead, atfork_fn) _atfork_list;
45__END_HIDDEN_DECLS
diff --git a/src/lib/libc/include/cancel.h b/src/lib/libc/include/cancel.h
deleted file mode 100644
index c452bf3d4c..0000000000
--- a/src/lib/libc/include/cancel.h
+++ /dev/null
@@ -1,78 +0,0 @@
1/* $OpenBSD: cancel.h,v 1.5 2017/09/05 02:40:54 guenther Exp $ */
2/*
3 * Copyright (c) 2015 Philip Guenther <guenther@openbsd.org>
4 *
5 * Permission to use, copy, modify, and distribute this software for any
6 * purpose with or without fee is hereby granted, provided that the above
7 * copyright notice and this permission notice appear in all copies.
8 *
9 * THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES
10 * WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF
11 * MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR
12 * ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES
13 * WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN
14 * ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF
15 * OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE.
16 */
17
18#ifndef _CANCEL_H_
19#define _CANCEL_H_
20
21#include <tib.h>
22#include "thread_private.h"
23
24/* process a cancel request at a cancel point */
25__dead void _thread_canceled(void);
26
27#ifdef __LIBC__
28PROTO_NORMAL(_thread_canceled);
29#endif
30
31#if defined(__LIBC__) && !defined(TCB_HAVE_MD_GET)
32/*
33 * Override TIB_GET macro to use the caching callback
34 */
35#undef TIB_GET
36#define TIB_GET() TCB_TO_TIB(_thread_cb.tc_tcb())
37#endif
38
39#define PREP_CANCEL_POINT(tib) \
40 int _cantcancel = (tib)->tib_cantcancel
41
42#define ENTER_CANCEL_POINT_INNER(tib, can_cancel, delay) \
43 if (_cantcancel == 0) { \
44 (tib)->tib_cancel_point = (delay) ? \
45 CANCEL_POINT_DELAYED : CANCEL_POINT; \
46 if (can_cancel) { \
47 __asm volatile("":::"memory"); \
48 if (__predict_false((tib)->tib_canceled)) \
49 _thread_canceled(); \
50 } \
51 }
52
53#define LEAVE_CANCEL_POINT_INNER(tib, can_cancel) \
54 if (_cantcancel == 0) { \
55 (tib)->tib_cancel_point = 0; \
56 if (can_cancel) { \
57 __asm volatile("":::"memory"); \
58 if (__predict_false((tib)->tib_canceled)) \
59 _thread_canceled(); \
60 } \
61 }
62
63/*
64 * Enter or leave a cancelation point, optionally processing pending
65 * cancelation requests. Note that ENTER_CANCEL_POINT opens a block
66 * and LEAVE_CANCEL_POINT must close that same block.
67 */
68#define ENTER_CANCEL_POINT(can_cancel) \
69 { \
70 struct tib *_tib = TIB_GET(); \
71 PREP_CANCEL_POINT(_tib); \
72 ENTER_CANCEL_POINT_INNER(_tib, can_cancel, 0)
73
74#define LEAVE_CANCEL_POINT(can_cancel) \
75 LEAVE_CANCEL_POINT_INNER(_tib, can_cancel); \
76 }
77
78#endif /* _CANCEL_H_ */
diff --git a/src/lib/libc/include/ctype_private.h b/src/lib/libc/include/ctype_private.h
deleted file mode 100644
index cbe1b20475..0000000000
--- a/src/lib/libc/include/ctype_private.h
+++ /dev/null
@@ -1,9 +0,0 @@
1/* $OpenBSD: ctype_private.h,v 1.2 2015/08/27 04:37:09 guenther Exp $ */
2/* Written by Marc Espie, public domain */
3#define CTYPE_NUM_CHARS 256
4
5__BEGIN_HIDDEN_DECLS
6extern const char _C_ctype_[];
7extern const short _C_toupper_[];
8extern const short _C_tolower_[];
9__END_HIDDEN_DECLS
diff --git a/src/lib/libc/include/localedef.h b/src/lib/libc/include/localedef.h
deleted file mode 100644
index 12dd47fffa..0000000000
--- a/src/lib/libc/include/localedef.h
+++ /dev/null
@@ -1,106 +0,0 @@
1/* $OpenBSD: localedef.h,v 1.1 2016/05/23 00:05:15 guenther Exp $ */
2/* $NetBSD: localedef.h,v 1.4 1996/04/09 20:55:31 cgd Exp $ */
3
4/*
5 * Copyright (c) 1994 Winning Strategies, Inc.
6 * All rights reserved.
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 Winning Strategies, Inc.
19 * 4. The name of the author may not be used to endorse or promote products
20 * derived from this software without specific prior written permission.
21 *
22 * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR
23 * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
24 * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
25 * IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT,
26 * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
27 * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
28 * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
29 * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
30 * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF
31 * THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
32 */
33
34#ifndef _LOCALEDEF_H_
35#define _LOCALEDEF_H_
36
37#include <sys/types.h>
38
39typedef struct
40{
41 char *yesexpr;
42 char *noexpr;
43 char *yesstr;
44 char *nostr;
45} _MessagesLocale;
46
47
48typedef struct
49{
50 char *int_curr_symbol;
51 char *currency_symbol;
52 char *mon_decimal_point;
53 char *mon_thousands_sep;
54 char *mon_grouping;
55 char *positive_sign;
56 char *negative_sign;
57 char int_frac_digits;
58 char frac_digits;
59 char p_cs_precedes;
60 char p_sep_by_space;
61 char n_cs_precedes;
62 char n_sep_by_space;
63 char p_sign_posn;
64 char n_sign_posn;
65 char int_p_cs_precedes;
66 char int_p_sep_by_space;
67 char int_n_cs_precedes;
68 char int_n_sep_by_space;
69 char int_p_sign_posn;
70 char int_n_sign_posn;
71} _MonetaryLocale;
72
73
74typedef struct
75{
76 const char *decimal_point;
77 const char *thousands_sep;
78 const char *grouping;
79} _NumericLocale;
80
81
82typedef struct {
83 const char *abday[7];
84 const char *day[7];
85 const char *abmon[12];
86 const char *mon[12];
87 const char *am_pm[2];
88 const char *d_t_fmt;
89 const char *d_fmt;
90 const char *t_fmt;
91 const char *t_fmt_ampm;
92} _TimeLocale;
93
94
95__BEGIN_HIDDEN_DECLS
96extern const _MessagesLocale *_CurrentMessagesLocale;
97extern const _MessagesLocale _DefaultMessagesLocale;
98extern const _MonetaryLocale *_CurrentMonetaryLocale;
99extern const _MonetaryLocale _DefaultMonetaryLocale;
100extern const _NumericLocale *_CurrentNumericLocale;
101extern const _NumericLocale _DefaultNumericLocale;
102extern const _TimeLocale *_CurrentTimeLocale;
103extern const _TimeLocale _DefaultTimeLocale;
104__END_HIDDEN_DECLS
105
106#endif /* !_LOCALEDEF_H_ */
diff --git a/src/lib/libc/include/mpool.h b/src/lib/libc/include/mpool.h
deleted file mode 100644
index 005b006d17..0000000000
--- a/src/lib/libc/include/mpool.h
+++ /dev/null
@@ -1,122 +0,0 @@
1/* $OpenBSD: mpool.h,v 1.1 2015/09/09 15:35:24 guenther Exp $ */
2/* $NetBSD: mpool.h,v 1.7 1996/05/03 21:13:41 cgd Exp $ */
3
4/*-
5 * Copyright (c) 1991, 1993, 1994
6 * The Regents of the University of California. All rights reserved.
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. 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 * @(#)mpool.h 8.4 (Berkeley) 11/2/95
33 */
34
35#ifndef _MPOOL_H_
36#define _MPOOL_H_
37
38#include <sys/queue.h>
39
40/*
41 * The memory pool scheme is a simple one. Each in-memory page is referenced
42 * by a bucket which is threaded in up to two of three ways. All active pages
43 * are threaded on a hash chain (hashed by page number) and an lru chain.
44 * Inactive pages are threaded on a free chain. Each reference to a memory
45 * pool is handed an opaque MPOOL cookie which stores all of this information.
46 */
47#define HASHSIZE 128
48#define HASHKEY(pgno) ((pgno - 1 + HASHSIZE) % HASHSIZE)
49
50/* The BKT structures are the elements of the queues. */
51typedef struct _bkt {
52 TAILQ_ENTRY(_bkt) hq; /* hash queue */
53 TAILQ_ENTRY(_bkt) q; /* lru queue */
54 void *page; /* page */
55 pgno_t pgno; /* page number */
56
57#define MPOOL_DIRTY 0x01 /* page needs to be written */
58#define MPOOL_PINNED 0x02 /* page is pinned into memory */
59#define MPOOL_INUSE 0x04 /* page address is valid */
60 u_int8_t flags; /* flags */
61} BKT;
62
63typedef struct MPOOL {
64 TAILQ_HEAD(_lqh, _bkt) lqh; /* lru queue head */
65 /* hash queue array */
66 TAILQ_HEAD(_hqh, _bkt) hqh[HASHSIZE];
67 pgno_t curcache; /* current number of cached pages */
68 pgno_t maxcache; /* max number of cached pages */
69 pgno_t npages; /* number of pages in the file */
70 unsigned long pagesize; /* file page size */
71 int fd; /* file descriptor */
72 /* page in conversion routine */
73 void (*pgin)(void *, pgno_t, void *);
74 /* page out conversion routine */
75 void (*pgout)(void *, pgno_t, void *);
76 void *pgcookie; /* cookie for page in/out routines */
77#ifdef STATISTICS
78 unsigned long cachehit;
79 unsigned long cachemiss;
80 unsigned long pagealloc;
81 unsigned long pageflush;
82 unsigned long pageget;
83 unsigned long pagenew;
84 unsigned long pageput;
85 unsigned long pageread;
86 unsigned long pagewrite;
87#endif
88} MPOOL;
89
90#define MPOOL_IGNOREPIN 0x01 /* Ignore if the page is pinned. */
91#define MPOOL_PAGE_REQUEST 0x01 /* Allocate a new page with a
92 specific page number. */
93#define MPOOL_PAGE_NEXT 0x02 /* Allocate a new page with the next
94 page number. */
95
96__BEGIN_HIDDEN_DECLS
97MPOOL *mpool_open(void *, int, pgno_t, pgno_t);
98void mpool_filter(MPOOL *, void (*)(void *, pgno_t, void *),
99 void (*)(void *, pgno_t, void *), void *);
100void *mpool_new(MPOOL *, pgno_t *, unsigned int);
101void *mpool_get(MPOOL *, pgno_t, unsigned int);
102int mpool_delete(MPOOL *, void *);
103int mpool_put(MPOOL *, void *, unsigned int);
104int mpool_sync(MPOOL *);
105int mpool_close(MPOOL *);
106
107PROTO_NORMAL(mpool_open);
108PROTO_NORMAL(mpool_filter);
109PROTO_NORMAL(mpool_new);
110PROTO_NORMAL(mpool_get);
111PROTO_NORMAL(mpool_delete);
112PROTO_NORMAL(mpool_put);
113PROTO_NORMAL(mpool_sync);
114PROTO_NORMAL(mpool_close);
115
116#ifdef STATISTICS
117void mpool_stat(MPOOL *);
118PROTO_NORMAL(mpool_stat);
119#endif
120__END_HIDDEN_DECLS
121
122#endif
diff --git a/src/lib/libc/include/namespace.h b/src/lib/libc/include/namespace.h
deleted file mode 100644
index 8503de47be..0000000000
--- a/src/lib/libc/include/namespace.h
+++ /dev/null
@@ -1,98 +0,0 @@
1/* $OpenBSD: namespace.h,v 1.16 2023/10/29 14:26:13 millert Exp $ */
2
3#ifndef _LIBC_NAMESPACE_H_
4#define _LIBC_NAMESPACE_H_
5
6/*
7 * Copyright (c) 2015 Philip Guenther <guenther@openbsd.org>
8 *
9 * Permission to use, copy, modify, and distribute this software for any
10 * purpose with or without fee is hereby granted, provided that the above
11 * copyright notice and this permission notice appear in all copies.
12 *
13 * THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES
14 * WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF
15 * MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR
16 * ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES
17 * WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN
18 * ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF
19 * OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE.
20 */
21
22/*
23 * ISO C11 or higher is required to build libc.
24 * This must come _before_ sys/cdefs.h is included.
25 */
26#if !defined(__STDC_VERSION__) || __STDC_VERSION__ < 201112
27#define _ISOC11_SOURCE 1
28#endif
29
30/*
31 * For explanations of how to use these, see README
32 * For explanations of why we use them and how they work, see DETAILS
33 */
34
35#include <sys/cdefs.h> /* for __dso_hidden and __{weak,strong}_alias */
36
37#define __dso_protected __attribute__((__visibility__("protected")))
38
39#define HIDDEN(x) _libc_##x
40#define CANCEL(x) _libc_##x##_cancel
41#define WRAP(x) _libc_##x##_wrap
42#define HIDDEN_STRING(x) "_libc_" __STRING(x)
43#define CANCEL_STRING(x) "_libc_" __STRING(x) "_cancel"
44#define WRAP_STRING(x) "_libc_" __STRING(x) "_wrap"
45
46#define PROTO_NORMAL(x) __dso_hidden typeof(x) x asm(HIDDEN_STRING(x))
47#define PROTO_STD_DEPRECATED(x) typeof(x) x __attribute__((deprecated))
48#define PROTO_DEPRECATED(x) typeof(x) x __attribute__((deprecated, weak))
49#define PROTO_CANCEL(x) __dso_hidden typeof(x) HIDDEN(x), \
50 x asm(CANCEL_STRING(x))
51#define PROTO_WRAP(x) PROTO_NORMAL(x), WRAP(x)
52#define PROTO_PROTECTED(x) __dso_protected typeof(x) x
53
54#define DEF_STRONG(x) __strong_alias(x, HIDDEN(x))
55#define DEF_WEAK(x) __weak_alias(x, HIDDEN(x))
56#define DEF_CANCEL(x) __weak_alias(x, CANCEL(x))
57#define DEF_WRAP(x) __weak_alias(x, WRAP(x))
58#define DEF_SYS(x) __strong_alias(_thread_sys_##x, HIDDEN(x))
59
60#if !defined(__clang__) && __GNUC__ != 3
61/* our gcc 4.2 handles redirecting builtins via PROTO_NORMAL()'s asm() label */
62#define DEF_BUILTIN(x) DEF_STRONG(x)
63#define BUILTIN
64#else
65/*
66 * clang and gcc can't redirect builtins via asm() labels, so mark
67 * them protected instead.
68 */
69#define DEF_BUILTIN(x) __asm("")
70#define BUILTIN __dso_protected
71#endif
72
73#define MAKE_CLONE(dst, src) __dso_hidden typeof(dst) HIDDEN(dst) \
74 __attribute__((alias (HIDDEN_STRING(src))))
75
76#define __relro __attribute__((section(".data.rel.ro")))
77
78
79/*
80 * gcc and clang will generate calls to the functions below.
81 * Declare and redirect them here so we always go
82 * directly to our hidden aliases.
83 */
84#include <sys/_types.h>
85BUILTIN void *memmove(void *, const void *, __size_t);
86BUILTIN void *memcpy(void *__restrict, const void *__restrict, __size_t);
87BUILTIN void *memset(void *, int, __size_t);
88BUILTIN void __stack_smash_handler(const char [], int __unused);
89#if !defined(__clang__) && __GNUC__ != 3
90PROTO_NORMAL(memmove);
91PROTO_NORMAL(memcpy);
92PROTO_NORMAL(memset);
93PROTO_NORMAL(__stack_smash_handler);
94#endif
95#undef BUILTIN
96
97#endif /* _LIBC_NAMESPACE_H_ */
98
diff --git a/src/lib/libc/include/thread_private.h b/src/lib/libc/include/thread_private.h
deleted file mode 100644
index 1ec1071161..0000000000
--- a/src/lib/libc/include/thread_private.h
+++ /dev/null
@@ -1,427 +0,0 @@
1/* $OpenBSD: thread_private.h,v 1.37 2024/08/18 02:25:51 guenther Exp $ */
2
3/* PUBLIC DOMAIN: No Rights Reserved. Marco S Hyman <marc@snafu.org> */
4
5#ifndef _THREAD_PRIVATE_H_
6#define _THREAD_PRIVATE_H_
7
8extern int __isthreaded;
9
10#define _MALLOC_MUTEXES 32
11void _malloc_init(int);
12#ifdef __LIBC__
13PROTO_NORMAL(_malloc_init);
14#endif /* __LIBC__ */
15
16/*
17 * The callbacks needed by libc to handle the threaded case.
18 * NOTE: Bump the version when you change the struct contents!
19 *
20 * tc_canceled:
21 * If not NULL, what to do when canceled (otherwise _exit(0))
22 *
23 * tc_flockfile, tc_ftrylockfile, and tc_funlockfile:
24 * If not NULL, these implement the flockfile() family.
25 * XXX In theory, you should be able to lock a FILE before
26 * XXX loading libpthread and have that be a real lock on it,
27 * XXX but that doesn't work without the libc base version
28 * XXX tracking the recursion count.
29 *
30 * tc_malloc_lock and tc_malloc_unlock:
31 * tc_atexit_lock and tc_atexit_unlock:
32 * tc_atfork_lock and tc_atfork_unlock:
33 * tc_arc4_lock and tc_arc4_unlock:
34 * The locks used by the malloc, atexit, atfork, and arc4 subsystems.
35 * These have to be ordered specially in the fork/vfork wrappers
36 * and may be implemented differently than the general mutexes
37 * in the callbacks below.
38 *
39 * tc_mutex_lock and tc_mutex_unlock:
40 * Lock and unlock the given mutex. If the given mutex is NULL
41 * a mutex is allocated and initialized automatically.
42 *
43 * tc_mutex_destroy:
44 * Destroy/deallocate the given mutex.
45 *
46 * tc_tag_lock and tc_tag_unlock:
47 * Lock and unlock the mutex associated with the given tag.
48 * If the given tag is NULL a tag is allocated and initialized
49 * automatically.
50 *
51 * tc_tag_storage:
52 * Returns a pointer to per-thread instance of data associated
53 * with the given tag. If the given tag is NULL a tag is
54 * allocated and initialized automatically.
55 *
56 * tc_fork, tc_vfork:
57 * If not NULL, they are called instead of the syscall stub, so that
58 * the thread library can do necessary locking and reinitialization.
59 *
60 * tc_thread_release:
61 * Handles the release of a thread's TIB and struct pthread and the
62 * notification of other threads...when there are other threads.
63 *
64 * tc_thread_key_zero:
65 * For each thread, zero out the key data associated with the given key.
66
67 * If <machine/tcb.h> doesn't define TCB_GET(), then locating the TCB in a
68 * threaded process requires a syscall (__get_tcb(2)) which is too much
69 * overhead for single-threaded processes. For those archs, there are two
70 * additional callbacks, though they are placed first in the struct for
71 * convenience in ASM:
72 *
73 * tc_errnoptr:
74 * Returns the address of the thread's errno.
75 *
76 * tc_tcb:
77 * Returns the address of the thread's TCB.
78 */
79
80struct __sFILE;
81struct pthread;
82struct thread_callbacks {
83 int *(*tc_errnoptr)(void); /* MUST BE FIRST */
84 void *(*tc_tcb)(void);
85 __dead void (*tc_canceled)(void);
86 void (*tc_flockfile)(struct __sFILE *);
87 int (*tc_ftrylockfile)(struct __sFILE *);
88 void (*tc_funlockfile)(struct __sFILE *);
89 void (*tc_malloc_lock)(int);
90 void (*tc_malloc_unlock)(int);
91 void (*tc_atexit_lock)(void);
92 void (*tc_atexit_unlock)(void);
93 void (*tc_atfork_lock)(void);
94 void (*tc_atfork_unlock)(void);
95 void (*tc_arc4_lock)(void);
96 void (*tc_arc4_unlock)(void);
97 void (*tc_mutex_lock)(void **);
98 void (*tc_mutex_unlock)(void **);
99 void (*tc_mutex_destroy)(void **);
100 void (*tc_tag_lock)(void **);
101 void (*tc_tag_unlock)(void **);
102 void *(*tc_tag_storage)(void **, void *, size_t, void (*)(void *),
103 void *);
104 __pid_t (*tc_fork)(void);
105 __pid_t (*tc_vfork)(void);
106 void (*tc_thread_release)(struct pthread *);
107 void (*tc_thread_key_zero)(int);
108};
109
110__BEGIN_PUBLIC_DECLS
111/*
112 * Set the callbacks used by libc
113 */
114void _thread_set_callbacks(const struct thread_callbacks *_cb, size_t _len);
115__END_PUBLIC_DECLS
116
117#ifdef __LIBC__
118__BEGIN_HIDDEN_DECLS
119/* the current set */
120extern struct thread_callbacks _thread_cb;
121__END_HIDDEN_DECLS
122#endif /* __LIBC__ */
123
124/*
125 * helper macro to make unique names in the thread namespace
126 */
127#define __THREAD_NAME(name) __CONCAT(_thread_tagname_,name)
128
129/*
130 * Macros used in libc to access thread mutex, keys, and per thread storage.
131 * _THREAD_PRIVATE_KEY and _THREAD_PRIVATE_MUTEX are different macros for
132 * historical reasons. They do the same thing, define a static variable
133 * keyed by 'name' that identifies a mutex and a key to identify per thread
134 * data.
135 */
136#define _THREAD_PRIVATE_KEY(name) \
137 static void *__THREAD_NAME(name)
138#define _THREAD_PRIVATE_MUTEX(name) \
139 static void *__THREAD_NAME(name)
140
141
142#ifndef __LIBC__ /* building some sort of reach around */
143
144#define _THREAD_PRIVATE_MUTEX_LOCK(name) do {} while (0)
145#define _THREAD_PRIVATE_MUTEX_UNLOCK(name) do {} while (0)
146#define _THREAD_PRIVATE(keyname, storage, error) &(storage)
147#define _THREAD_PRIVATE_DT(keyname, storage, dt, error) &(storage)
148#define _MUTEX_LOCK(mutex) do {} while (0)
149#define _MUTEX_UNLOCK(mutex) do {} while (0)
150#define _MUTEX_DESTROY(mutex) do {} while (0)
151#define _MALLOC_LOCK(n) do {} while (0)
152#define _MALLOC_UNLOCK(n) do {} while (0)
153#define _ATEXIT_LOCK() do {} while (0)
154#define _ATEXIT_UNLOCK() do {} while (0)
155#define _ATFORK_LOCK() do {} while (0)
156#define _ATFORK_UNLOCK() do {} while (0)
157#define _ARC4_LOCK() do {} while (0)
158#define _ARC4_UNLOCK() do {} while (0)
159
160#else /* building libc */
161#define _THREAD_PRIVATE_MUTEX_LOCK(name) \
162 do { \
163 if (_thread_cb.tc_tag_lock != NULL) \
164 _thread_cb.tc_tag_lock(&(__THREAD_NAME(name))); \
165 } while (0)
166#define _THREAD_PRIVATE_MUTEX_UNLOCK(name) \
167 do { \
168 if (_thread_cb.tc_tag_unlock != NULL) \
169 _thread_cb.tc_tag_unlock(&(__THREAD_NAME(name))); \
170 } while (0)
171#define _THREAD_PRIVATE(keyname, storage, error) \
172 (_thread_cb.tc_tag_storage == NULL ? &(storage) : \
173 _thread_cb.tc_tag_storage(&(__THREAD_NAME(keyname)), \
174 &(storage), sizeof(storage), NULL, (error)))
175
176#define _THREAD_PRIVATE_DT(keyname, storage, dt, error) \
177 (_thread_cb.tc_tag_storage == NULL ? &(storage) : \
178 _thread_cb.tc_tag_storage(&(__THREAD_NAME(keyname)), \
179 &(storage), sizeof(storage), (dt), (error)))
180
181/*
182 * Macros used in libc to access mutexes.
183 */
184#define _MUTEX_LOCK(mutex) \
185 do { \
186 if (__isthreaded) \
187 _thread_cb.tc_mutex_lock(mutex); \
188 } while (0)
189#define _MUTEX_UNLOCK(mutex) \
190 do { \
191 if (__isthreaded) \
192 _thread_cb.tc_mutex_unlock(mutex); \
193 } while (0)
194#define _MUTEX_DESTROY(mutex) \
195 do { \
196 if (__isthreaded) \
197 _thread_cb.tc_mutex_destroy(mutex); \
198 } while (0)
199
200/*
201 * malloc lock/unlock prototypes and definitions
202 */
203#define _MALLOC_LOCK(n) \
204 do { \
205 if (__isthreaded) \
206 _thread_cb.tc_malloc_lock(n); \
207 } while (0)
208#define _MALLOC_UNLOCK(n) \
209 do { \
210 if (__isthreaded) \
211 _thread_cb.tc_malloc_unlock(n); \
212 } while (0)
213
214#define _ATEXIT_LOCK() \
215 do { \
216 if (__isthreaded) \
217 _thread_cb.tc_atexit_lock(); \
218 } while (0)
219#define _ATEXIT_UNLOCK() \
220 do { \
221 if (__isthreaded) \
222 _thread_cb.tc_atexit_unlock(); \
223 } while (0)
224
225#define _ATFORK_LOCK() \
226 do { \
227 if (__isthreaded) \
228 _thread_cb.tc_atfork_lock(); \
229 } while (0)
230#define _ATFORK_UNLOCK() \
231 do { \
232 if (__isthreaded) \
233 _thread_cb.tc_atfork_unlock(); \
234 } while (0)
235
236#define _ARC4_LOCK() \
237 do { \
238 if (__isthreaded) \
239 _thread_cb.tc_arc4_lock(); \
240 } while (0)
241#define _ARC4_UNLOCK() \
242 do { \
243 if (__isthreaded) \
244 _thread_cb.tc_arc4_unlock(); \
245 } while (0)
246#endif /* __LIBC__ */
247
248
249/*
250 * Copyright (c) 2004,2005 Ted Unangst <tedu@openbsd.org>
251 * All Rights Reserved.
252 *
253 * Permission to use, copy, modify, and distribute this software for any
254 * purpose with or without fee is hereby granted, provided that the above
255 * copyright notice and this permission notice appear in all copies.
256 *
257 * THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES
258 * WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF
259 * MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR
260 * ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES
261 * WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN
262 * ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF
263 * OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE.
264 */
265/*
266 * Private data structures that back up the typedefs in pthread.h.
267 * Since only the thread library cares about their size or arrangement,
268 * it should be possible to switch libraries without relinking.
269 *
270 * Do not reorder _atomic_lock_t and sem_t variables in the structs.
271 * This is due to alignment requirements of certain arches like hppa.
272 * The current requirement is 16 bytes.
273 *
274 * THE MACHINE DEPENDENT CERROR CODE HAS HARD CODED OFFSETS INTO PTHREAD_T!
275 */
276
277#include <sys/queue.h>
278#include <pthread.h>
279#include <semaphore.h>
280#include <machine/spinlock.h>
281
282#define _SPINLOCK_UNLOCKED _ATOMIC_LOCK_UNLOCKED
283
284struct __sem {
285 _atomic_lock_t lock;
286 volatile int waitcount;
287 volatile int value;
288 int shared;
289};
290
291TAILQ_HEAD(pthread_queue, pthread);
292
293#ifdef FUTEX
294
295struct pthread_mutex {
296 volatile unsigned int lock;
297 int type;
298 pthread_t owner;
299 int count;
300 int prioceiling;
301};
302
303struct pthread_cond {
304 volatile unsigned int seq;
305 clockid_t clock;
306 struct pthread_mutex *mutex;
307};
308
309struct pthread_rwlock {
310 volatile unsigned int value;
311};
312
313#else
314
315struct pthread_mutex {
316 _atomic_lock_t lock;
317 struct pthread_queue lockers;
318 int type;
319 pthread_t owner;
320 int count;
321 int prioceiling;
322};
323
324struct pthread_cond {
325 _atomic_lock_t lock;
326 struct pthread_queue waiters;
327 struct pthread_mutex *mutex;
328 clockid_t clock;
329};
330
331struct pthread_rwlock {
332 _atomic_lock_t lock;
333 pthread_t owner;
334 struct pthread_queue writers;
335 int readers;
336};
337#endif /* FUTEX */
338
339struct pthread_mutex_attr {
340 int ma_type;
341 int ma_protocol;
342 int ma_prioceiling;
343};
344
345struct pthread_cond_attr {
346 clockid_t ca_clock;
347};
348
349struct pthread_attr {
350 void *stack_addr;
351 size_t stack_size;
352 size_t guard_size;
353 int detach_state;
354 int contention_scope;
355 int sched_policy;
356 struct sched_param sched_param;
357 int sched_inherit;
358};
359
360struct rthread_storage {
361 int keyid;
362 struct rthread_storage *next;
363 void *data;
364};
365
366struct rthread_cleanup_fn {
367 void (*fn)(void *);
368 void *arg;
369 struct rthread_cleanup_fn *next;
370};
371
372struct tib;
373struct stack;
374struct pthread {
375 struct __sem donesem;
376 unsigned int flags;
377 _atomic_lock_t flags_lock;
378 struct tib *tib;
379 void *retval;
380 void *(*fn)(void *);
381 void *arg;
382 char name[32];
383 struct stack *stack;
384 LIST_ENTRY(pthread) threads;
385 TAILQ_ENTRY(pthread) waiting;
386 pthread_cond_t blocking_cond;
387 struct pthread_attr attr;
388 struct rthread_storage *local_storage;
389 struct rthread_cleanup_fn *cleanup_fns;
390
391 /* cancel received in a delayed cancel block? */
392 int delayed_cancel;
393};
394/* flags in pthread->flags */
395#define THREAD_DONE 0x001
396#define THREAD_DETACHED 0x002
397
398/* flags in tib->tib_thread_flags */
399#define TIB_THREAD_ASYNC_CANCEL 0x001
400#define TIB_THREAD_INITIAL_STACK 0x002 /* has stack from exec */
401
402#define ENTER_DELAYED_CANCEL_POINT(tib, self) \
403 (self)->delayed_cancel = 0; \
404 ENTER_CANCEL_POINT_INNER(tib, 1, 1)
405
406/*
407 * Internal functions exported from libc's thread bits for use by libpthread
408 */
409void _spinlock(volatile _atomic_lock_t *);
410int _spinlocktry(volatile _atomic_lock_t *);
411void _spinunlock(volatile _atomic_lock_t *);
412
413void _rthread_debug(int, const char *, ...)
414 __attribute__((__format__ (printf, 2, 3)));
415pid_t _thread_dofork(pid_t (*_sys_fork)(void));
416void _thread_finalize(void);
417
418/*
419 * Threading syscalls not declared in system headers
420 */
421__dead void __threxit(pid_t *);
422int __thrsleep(const volatile void *, clockid_t,
423 const struct timespec *, volatile void *, const int *);
424int __thrwakeup(const volatile void *, int n);
425int __thrsigdivert(sigset_t, siginfo_t *, const struct timespec *);
426
427#endif /* _THREAD_PRIVATE_H_ */
diff --git a/src/lib/libc/net/Makefile.inc b/src/lib/libc/net/Makefile.inc
deleted file mode 100644
index db2c75dad6..0000000000
--- a/src/lib/libc/net/Makefile.inc
+++ /dev/null
@@ -1,31 +0,0 @@
1# $OpenBSD: Makefile.inc,v 1.61 2024/04/15 14:30:48 naddy Exp $
2
3# net sources
4.PATH: ${LIBCSRCDIR}/net
5
6CFLAGS+=-DRESOLVSORT
7
8SRCS+= base64.c ethers.c freeaddrinfo.c \
9 gai_strerror.c getaddrinfo.c gethostnamadr.c \
10 getifaddrs.c getnameinfo.c getnetent.c \
11 getnetnamadr.c getpeereid.c getproto.c getprotoent.c getprotoname.c \
12 getservbyname.c getservbyport.c getservent.c getrrsetbyname.c \
13 herror.c htonl.c htons.c \
14 if_indextoname.c if_nameindex.c if_nametoindex.c inet_addr.c \
15 inet_lnaof.c inet_makeaddr.c inet_neta.c inet_netof.c inet_network.c \
16 inet_net_ntop.c inet_net_pton.c inet_ntoa.c inet_ntop.c inet_pton.c \
17 linkaddr.c ntohl.c ntohs.c rcmd.c rcmdsh.c ruserok.c \
18 rresvport.c recv.c res_comp.c res_data.c \
19 res_debug.c res_debug_syms.c res_init.c res_mkquery.c res_query.c \
20 res_random.c res_send.c \
21 send.c sethostent.c sockatmark.c
22
23# IPv6
24SRCS+= ip6opt.c rthdr.c vars6.c
25
26MAN+= htobe64.3 ether_aton.3 gai_strerror.3 getaddrinfo.3 gethostbyname.3 \
27 getifaddrs.3 getnameinfo.3 getnetent.3 getpeereid.3 getprotoent.3 \
28 getrrsetbyname.3 getservent.3 htonl.3 if_indextoname.3 \
29 inet_addr.3 inet_lnaof.3 inet_net_ntop.3 inet_ntop.3 \
30 inet6_opt_init.3 inet6_rth_space.3 link_ntoa.3 \
31 rcmd.3 rcmdsh.3 res_init.3 sockatmark.3
diff --git a/src/lib/libc/net/base64.c b/src/lib/libc/net/base64.c
deleted file mode 100644
index f36c11a21c..0000000000
--- a/src/lib/libc/net/base64.c
+++ /dev/null
@@ -1,307 +0,0 @@
1/* $OpenBSD: base64.c,v 1.15 2021/10/25 14:41:09 jca Exp $ */
2
3/*
4 * Copyright (c) 1996 by Internet Software Consortium.
5 *
6 * Permission to use, copy, modify, and distribute this software for any
7 * purpose with or without fee is hereby granted, provided that the above
8 * copyright notice and this permission notice appear in all copies.
9 *
10 * THE SOFTWARE IS PROVIDED "AS IS" AND INTERNET SOFTWARE CONSORTIUM DISCLAIMS
11 * ALL WARRANTIES WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES
12 * OF MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL INTERNET SOFTWARE
13 * CONSORTIUM BE LIABLE FOR ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL
14 * DAMAGES OR ANY DAMAGES WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR
15 * PROFITS, WHETHER IN AN ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS
16 * ACTION, ARISING OUT OF OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS
17 * SOFTWARE.
18 */
19
20/*
21 * Portions Copyright (c) 1995 by International Business Machines, Inc.
22 *
23 * International Business Machines, Inc. (hereinafter called IBM) grants
24 * permission under its copyrights to use, copy, modify, and distribute this
25 * Software with or without fee, provided that the above copyright notice and
26 * all paragraphs of this notice appear in all copies, and that the name of IBM
27 * not be used in connection with the marketing of any product incorporating
28 * the Software or modifications thereof, without specific, written prior
29 * permission.
30 *
31 * To the extent it has a right to do so, IBM grants an immunity from suit
32 * under its patents, if any, for the use, sale or manufacture of products to
33 * the extent that such products are used for performing Domain Name System
34 * dynamic updates in TCP/IP networks by means of the Software. No immunity is
35 * granted for any product per se or for any other function of any product.
36 *
37 * THE SOFTWARE IS PROVIDED "AS IS", AND IBM DISCLAIMS ALL WARRANTIES,
38 * INCLUDING ALL IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A
39 * PARTICULAR PURPOSE. IN NO EVENT SHALL IBM BE LIABLE FOR ANY SPECIAL,
40 * DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES WHATSOEVER ARISING
41 * OUT OF OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE, EVEN
42 * IF IBM IS APPRISED OF THE POSSIBILITY OF SUCH DAMAGES.
43 */
44
45#include <sys/types.h>
46#include <sys/socket.h>
47#include <netinet/in.h>
48#include <arpa/inet.h>
49
50#include <ctype.h>
51#include <resolv.h>
52
53#include <stdlib.h>
54#include <string.h>
55
56static const char Base64[] =
57 "ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789+/";
58static const char Pad64 = '=';
59
60/* (From RFC1521 and draft-ietf-dnssec-secext-03.txt)
61 The following encoding technique is taken from RFC 1521 by Borenstein
62 and Freed. It is reproduced here in a slightly edited form for
63 convenience.
64
65 A 65-character subset of US-ASCII is used, enabling 6 bits to be
66 represented per printable character. (The extra 65th character, "=",
67 is used to signify a special processing function.)
68
69 The encoding process represents 24-bit groups of input bits as output
70 strings of 4 encoded characters. Proceeding from left to right, a
71 24-bit input group is formed by concatenating 3 8-bit input groups.
72 These 24 bits are then treated as 4 concatenated 6-bit groups, each
73 of which is translated into a single digit in the base64 alphabet.
74
75 Each 6-bit group is used as an index into an array of 64 printable
76 characters. The character referenced by the index is placed in the
77 output string.
78
79 Table 1: The Base64 Alphabet
80
81 Value Encoding Value Encoding Value Encoding Value Encoding
82 0 A 17 R 34 i 51 z
83 1 B 18 S 35 j 52 0
84 2 C 19 T 36 k 53 1
85 3 D 20 U 37 l 54 2
86 4 E 21 V 38 m 55 3
87 5 F 22 W 39 n 56 4
88 6 G 23 X 40 o 57 5
89 7 H 24 Y 41 p 58 6
90 8 I 25 Z 42 q 59 7
91 9 J 26 a 43 r 60 8
92 10 K 27 b 44 s 61 9
93 11 L 28 c 45 t 62 +
94 12 M 29 d 46 u 63 /
95 13 N 30 e 47 v
96 14 O 31 f 48 w (pad) =
97 15 P 32 g 49 x
98 16 Q 33 h 50 y
99
100 Special processing is performed if fewer than 24 bits are available
101 at the end of the data being encoded. A full encoding quantum is
102 always completed at the end of a quantity. When fewer than 24 input
103 bits are available in an input group, zero bits are added (on the
104 right) to form an integral number of 6-bit groups. Padding at the
105 end of the data is performed using the '=' character.
106
107 Since all base64 input is an integral number of octets, only the
108 -------------------------------------------------
109 following cases can arise:
110
111 (1) the final quantum of encoding input is an integral
112 multiple of 24 bits; here, the final unit of encoded
113 output will be an integral multiple of 4 characters
114 with no "=" padding,
115 (2) the final quantum of encoding input is exactly 8 bits;
116 here, the final unit of encoded output will be two
117 characters followed by two "=" padding characters, or
118 (3) the final quantum of encoding input is exactly 16 bits;
119 here, the final unit of encoded output will be three
120 characters followed by one "=" padding character.
121 */
122
123int
124b64_ntop(unsigned char const *src, size_t srclength, char *target,
125 size_t targsize)
126{
127 size_t datalength = 0;
128 unsigned char input[3];
129 unsigned char output[4];
130 int i;
131
132 while (2 < srclength) {
133 input[0] = *src++;
134 input[1] = *src++;
135 input[2] = *src++;
136 srclength -= 3;
137
138 output[0] = input[0] >> 2;
139 output[1] = ((input[0] & 0x03) << 4) + (input[1] >> 4);
140 output[2] = ((input[1] & 0x0f) << 2) + (input[2] >> 6);
141 output[3] = input[2] & 0x3f;
142
143 if (datalength + 4 > targsize)
144 return (-1);
145 target[datalength++] = Base64[output[0]];
146 target[datalength++] = Base64[output[1]];
147 target[datalength++] = Base64[output[2]];
148 target[datalength++] = Base64[output[3]];
149 }
150
151 /* Now we worry about padding. */
152 if (0 != srclength) {
153 /* Get what's left. */
154 input[0] = input[1] = input[2] = '\0';
155 for (i = 0; i < srclength; i++)
156 input[i] = *src++;
157
158 output[0] = input[0] >> 2;
159 output[1] = ((input[0] & 0x03) << 4) + (input[1] >> 4);
160 output[2] = ((input[1] & 0x0f) << 2) + (input[2] >> 6);
161
162 if (datalength + 4 > targsize)
163 return (-1);
164 target[datalength++] = Base64[output[0]];
165 target[datalength++] = Base64[output[1]];
166 if (srclength == 1)
167 target[datalength++] = Pad64;
168 else
169 target[datalength++] = Base64[output[2]];
170 target[datalength++] = Pad64;
171 }
172 if (datalength >= targsize)
173 return (-1);
174 target[datalength] = '\0'; /* Returned value doesn't count \0. */
175 return (datalength);
176}
177
178/* skips all whitespace anywhere.
179 converts characters, four at a time, starting at (or after)
180 src from base - 64 numbers into three 8 bit bytes in the target area.
181 it returns the number of data bytes stored at the target, or -1 on error.
182 */
183
184int
185b64_pton(char const *src, unsigned char *target, size_t targsize)
186{
187 int tarindex, state, ch;
188 unsigned char nextbyte;
189 char *pos;
190
191 state = 0;
192 tarindex = 0;
193
194 while ((ch = (unsigned char)*src++) != '\0') {
195 if (isspace(ch)) /* Skip whitespace anywhere. */
196 continue;
197
198 if (ch == Pad64)
199 break;
200
201 pos = strchr(Base64, ch);
202 if (pos == 0) /* A non-base64 character. */
203 return (-1);
204
205 switch (state) {
206 case 0:
207 if (target) {
208 if (tarindex >= targsize)
209 return (-1);
210 target[tarindex] = (pos - Base64) << 2;
211 }
212 state = 1;
213 break;
214 case 1:
215 if (target) {
216 if (tarindex >= targsize)
217 return (-1);
218 target[tarindex] |= (pos - Base64) >> 4;
219 nextbyte = ((pos - Base64) & 0x0f) << 4;
220 if (tarindex + 1 < targsize)
221 target[tarindex+1] = nextbyte;
222 else if (nextbyte)
223 return (-1);
224 }
225 tarindex++;
226 state = 2;
227 break;
228 case 2:
229 if (target) {
230 if (tarindex >= targsize)
231 return (-1);
232 target[tarindex] |= (pos - Base64) >> 2;
233 nextbyte = ((pos - Base64) & 0x03) << 6;
234 if (tarindex + 1 < targsize)
235 target[tarindex+1] = nextbyte;
236 else if (nextbyte)
237 return (-1);
238 }
239 tarindex++;
240 state = 3;
241 break;
242 case 3:
243 if (target) {
244 if (tarindex >= targsize)
245 return (-1);
246 target[tarindex] |= (pos - Base64);
247 }
248 tarindex++;
249 state = 0;
250 break;
251 }
252 }
253
254 /*
255 * We are done decoding Base-64 chars. Let's see if we ended
256 * on a byte boundary, and/or with erroneous trailing characters.
257 */
258
259 if (ch == Pad64) { /* We got a pad char. */
260 ch = (unsigned char)*src++; /* Skip it, get next. */
261 switch (state) {
262 case 0: /* Invalid = in first position */
263 case 1: /* Invalid = in second position */
264 return (-1);
265
266 case 2: /* Valid, means one byte of info */
267 /* Skip any number of spaces. */
268 for (; ch != '\0'; ch = (unsigned char)*src++)
269 if (!isspace(ch))
270 break;
271 /* Make sure there is another trailing = sign. */
272 if (ch != Pad64)
273 return (-1);
274 ch = (unsigned char)*src++; /* Skip the = */
275 /* Fall through to "single trailing =" case. */
276 /* FALLTHROUGH */
277
278 case 3: /* Valid, means two bytes of info */
279 /*
280 * We know this char is an =. Is there anything but
281 * whitespace after it?
282 */
283 for (; ch != '\0'; ch = (unsigned char)*src++)
284 if (!isspace(ch))
285 return (-1);
286
287 /*
288 * Now make sure for cases 2 and 3 that the "extra"
289 * bits that slopped past the last full byte were
290 * zeros. If we don't check them, they become a
291 * subliminal channel.
292 */
293 if (target && tarindex < targsize &&
294 target[tarindex] != 0)
295 return (-1);
296 }
297 } else {
298 /*
299 * We ended by seeing the end of the string. Make sure we
300 * have no partial bytes lying around.
301 */
302 if (state != 0)
303 return (-1);
304 }
305
306 return (tarindex);
307}
diff --git a/src/lib/libc/net/ether_aton.3 b/src/lib/libc/net/ether_aton.3
deleted file mode 100644
index 98562dc44c..0000000000
--- a/src/lib/libc/net/ether_aton.3
+++ /dev/null
@@ -1,114 +0,0 @@
1.\" $OpenBSD: ether_aton.3,v 1.3 2022/09/11 06:38:10 jmc Exp $
2.\"
3.\" Written by roland@frob.com. Public domain.
4.\"
5.Dd $Mdocdate: September 11 2022 $
6.Dt ETHER_ATON 3
7.Os
8.Sh NAME
9.Nm ether_aton ,
10.Nm ether_ntoa ,
11.Nm ether_ntohost ,
12.Nm ether_hostton ,
13.Nm ether_line
14.Nd get ethers entry
15.Sh SYNOPSIS
16.In sys/types.h
17.In sys/socket.h
18.In net/if.h
19.In netinet/in.h
20.In netinet/if_ether.h
21.Ft char *
22.Fn ether_ntoa "struct ether_addr *e"
23.Ft struct ether_addr *
24.Fn ether_aton "const char *s"
25.Ft int
26.Fn ether_ntohost "char *hostname" "struct ether_addr *e"
27.Ft int
28.Fn ether_hostton "const char *hostname" "struct ether_addr *e"
29.Ft int
30.Fn ether_line "const char *l" "struct ether_addr *e" "char *hostname"
31.Sh DESCRIPTION
32Ethernet addresses are represented by the
33following structure:
34.Bd -literal -offset indent
35struct ether_addr {
36 u_int8_t ether_addr_octet[ETHER_ADDR_LEN];
37};
38.Ed
39.Pp
40The
41.Fn ether_ntoa
42function converts this structure into an
43.Tn ASCII
44string of the form
45.Dq xx:xx:xx:xx:xx:xx ,
46consisting of 6 hexadecimal numbers separated
47by colons.
48It returns a pointer to a static buffer that is reused for each call.
49The
50.Fn ether_aton
51function converts an
52.Tn ASCII
53string of the same form and to a structure
54containing the 6 octets of the address.
55It returns a pointer to a static structure that is reused for each call.
56.Fn ether_aton
57will return NULL if the string does not represent a valid address.
58.Pp
59The
60.Fn ether_ntohost
61and
62.Fn ether_hostton
63functions interrogate the database mapping host names to Ethernet
64addresses,
65.Pa /etc/ethers .
66The
67.Fn ether_ntohost
68function looks up the given Ethernet address and writes the associated
69host name into the character buffer passed.
70This buffer should be
71.Dv MAXHOSTNAMELEN
72characters in size.
73The
74.Fn ether_hostton
75function looks up the given host name and writes the associated
76Ethernet address into the structure passed.
77Both functions return
78zero if they find the requested host name or address, and \-1 if not.
79.Pp
80Each call reads
81.Pa /etc/ethers
82from the beginning.
83.Pp
84The
85.Fn ether_line
86function parses a line from the
87.Pa /etc/ethers
88file and fills in the passed
89.Vt struct ether_addr
90and character buffer with the Ethernet address and host name on the line.
91It returns zero if the line was successfully parsed and \-1 if not.
92The character buffer should be
93.Dv MAXHOSTNAMELEN
94characters in size.
95.Sh FILES
96.Bl -tag -width /etc/ethers -compact
97.It Pa /etc/ethers
98.El
99.Sh SEE ALSO
100.Xr ethers 5
101.Sh HISTORY
102The
103.Fn ether_ntoa ,
104.Fn ether_aton ,
105.Fn ether_ntohost ,
106.Fn ether_hostton ,
107and
108.Fn ether_line
109functions were adopted from SunOS and appeared in
110.Nx 0.9b .
111.Sh BUGS
112The data space used by these functions is static; if future use
113requires the data, it should be copied before any subsequent calls to
114these functions overwrite it.
diff --git a/src/lib/libc/net/ethers.c b/src/lib/libc/net/ethers.c
deleted file mode 100644
index d62be1ca71..0000000000
--- a/src/lib/libc/net/ethers.c
+++ /dev/null
@@ -1,183 +0,0 @@
1/* $OpenBSD: ethers.c,v 1.27 2019/01/25 00:19:25 millert Exp $ */
2
3/*
4 * Copyright (c) 1998 Todd C. Miller <millert@openbsd.org>
5 *
6 * Permission to use, copy, modify, and distribute this software for any
7 * purpose with or without fee is hereby granted, provided that the above
8 * copyright notice and this permission notice appear in all copies.
9 *
10 * THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES
11 * WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF
12 * MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR
13 * ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES
14 * WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN
15 * ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF
16 * OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE.
17 */
18
19/*
20 * ethers(3) a la Sun.
21 * Originally Written by Roland McGrath <roland@frob.com> 10/14/93.
22 * Substantially modified by Todd C. Miller <millert@openbsd.org>
23 */
24
25#include <sys/types.h>
26#include <sys/socket.h>
27#include <net/if.h>
28#include <netinet/in.h>
29#include <netinet/if_ether.h>
30#include <paths.h>
31#include <errno.h>
32#include <stdio.h>
33#include <stdlib.h>
34#include <string.h>
35#include <ctype.h>
36#include <limits.h>
37
38#ifndef _PATH_ETHERS
39#define _PATH_ETHERS "/etc/ethers"
40#endif
41
42static char * _ether_aton(const char *, struct ether_addr *);
43
44char *
45ether_ntoa(struct ether_addr *e)
46{
47 static char a[] = "xx:xx:xx:xx:xx:xx";
48
49 (void)snprintf(a, sizeof a, "%02x:%02x:%02x:%02x:%02x:%02x",
50 e->ether_addr_octet[0], e->ether_addr_octet[1],
51 e->ether_addr_octet[2], e->ether_addr_octet[3],
52 e->ether_addr_octet[4], e->ether_addr_octet[5]);
53
54 return (a);
55}
56
57static char *
58_ether_aton(const char *s, struct ether_addr *e)
59{
60 int i;
61 long l;
62 char *pp;
63
64 while (isspace((unsigned char)*s))
65 s++;
66
67 /* expect 6 hex octets separated by ':' or space/NUL if last octet */
68 for (i = 0; i < 6; i++) {
69 l = strtol(s, &pp, 16);
70 if (pp == s || l > 0xFF || l < 0)
71 return (NULL);
72 if (!(*pp == ':' ||
73 (i == 5 && (isspace((unsigned char)*pp) ||
74 *pp == '\0'))))
75 return (NULL);
76 e->ether_addr_octet[i] = (u_char)l;
77 s = pp + 1;
78 }
79
80 /* return character after the octets ala strtol(3) */
81 return (pp);
82}
83
84struct ether_addr *
85ether_aton(const char *s)
86{
87 static struct ether_addr n;
88
89 return (_ether_aton(s, &n) ? &n : NULL);
90}
91
92int
93ether_ntohost(char *hostname, struct ether_addr *e)
94{
95 FILE *f;
96 char buf[BUFSIZ+1], *p;
97 size_t len;
98 struct ether_addr try;
99
100 f = fopen(_PATH_ETHERS, "re");
101 if (f == NULL)
102 return (-1);
103 while ((p = fgetln(f, &len)) != NULL) {
104 if (p[len-1] == '\n')
105 len--;
106 if (len > sizeof(buf) - 2)
107 continue;
108 (void)memcpy(buf, p, len);
109 buf[len] = '\n'; /* code assumes newlines later on */
110 buf[len+1] = '\0';
111 /* A + in the file meant try YP, ignore it. */
112 if (!strncmp(buf, "+\n", sizeof(buf)))
113 continue;
114 if (ether_line(buf, &try, hostname) == 0 &&
115 memcmp(&try, e, sizeof(try)) == 0) {
116 (void)fclose(f);
117 return (0);
118 }
119 }
120 (void)fclose(f);
121 errno = ENOENT;
122 return (-1);
123}
124
125int
126ether_hostton(const char *hostname, struct ether_addr *e)
127{
128 FILE *f;
129 char buf[BUFSIZ+1], *p;
130 char try[HOST_NAME_MAX+1];
131 size_t len;
132
133 f = fopen(_PATH_ETHERS, "re");
134 if (f==NULL)
135 return (-1);
136
137 while ((p = fgetln(f, &len)) != NULL) {
138 if (p[len-1] == '\n')
139 len--;
140 if (len > sizeof(buf) - 2)
141 continue;
142 memcpy(buf, p, len);
143 buf[len] = '\n'; /* code assumes newlines later on */
144 buf[len+1] = '\0';
145 /* A + in the file meant try YP, ignore it. */
146 if (!strncmp(buf, "+\n", sizeof(buf)))
147 continue;
148 if (ether_line(buf, e, try) == 0 && strcmp(hostname, try) == 0) {
149 (void)fclose(f);
150 return (0);
151 }
152 }
153 (void)fclose(f);
154 errno = ENOENT;
155 return (-1);
156}
157
158int
159ether_line(const char *line, struct ether_addr *e, char *hostname)
160{
161 char *p;
162 size_t n;
163
164 /* Parse "xx:xx:xx:xx:xx:xx" */
165 if ((p = _ether_aton(line, e)) == NULL || (*p != ' ' && *p != '\t'))
166 goto bad;
167
168 /* Now get the hostname */
169 while (isspace((unsigned char)*p))
170 p++;
171 if (*p == '\0')
172 goto bad;
173 n = strcspn(p, " \t\n");
174 if (n >= HOST_NAME_MAX+1)
175 goto bad;
176 strlcpy(hostname, p, n + 1);
177 return (0);
178
179bad:
180 errno = EINVAL;
181 return (-1);
182}
183DEF_WEAK(ether_line);
diff --git a/src/lib/libc/net/freeaddrinfo.c b/src/lib/libc/net/freeaddrinfo.c
deleted file mode 100644
index 154f70cd75..0000000000
--- a/src/lib/libc/net/freeaddrinfo.c
+++ /dev/null
@@ -1,50 +0,0 @@
1/* $OpenBSD: freeaddrinfo.c,v 1.9 2016/09/21 04:38:56 guenther Exp $ */
2
3/*
4 * Copyright (c) 1996, 1997, 1998, 1999, Craig Metz, 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 Craig Metz and
17 * by other contributors.
18 * 4. Neither the name of the author nor the names of 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
35#include <stdlib.h>
36#include <netdb.h>
37
38void
39freeaddrinfo(struct addrinfo *ai)
40{
41 struct addrinfo *p;
42
43 do {
44 p = ai;
45 ai = ai->ai_next;
46 free(p->ai_canonname);
47 free(p);
48 } while (ai);
49}
50DEF_WEAK(freeaddrinfo);
diff --git a/src/lib/libc/net/gai_strerror.3 b/src/lib/libc/net/gai_strerror.3
deleted file mode 100644
index d271f492c5..0000000000
--- a/src/lib/libc/net/gai_strerror.3
+++ /dev/null
@@ -1,95 +0,0 @@
1.\" $OpenBSD: gai_strerror.3,v 1.10 2017/05/03 01:58:33 deraadt Exp $
2.\" $KAME: gai_strerror.3,v 1.1 2005/01/05 03:04:47 itojun Exp $
3.\"
4.\" Copyright (C) 2004 Internet Systems Consortium, Inc. ("ISC")
5.\" Copyright (C) 2000, 2001 Internet Software Consortium.
6.\"
7.\" Permission to use, copy, modify, and distribute this software for any
8.\" purpose with or without fee is hereby granted, provided that the above
9.\" copyright notice and this permission notice appear in all copies.
10.\"
11.\" THE SOFTWARE IS PROVIDED "AS IS" AND ISC DISCLAIMS ALL WARRANTIES WITH
12.\" REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF MERCHANTABILITY
13.\" AND FITNESS. IN NO EVENT SHALL ISC BE LIABLE FOR ANY SPECIAL, DIRECT,
14.\" INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES WHATSOEVER RESULTING FROM
15.\" LOSS OF USE, DATA OR PROFITS, WHETHER IN AN ACTION OF CONTRACT, NEGLIGENCE
16.\" OR OTHER TORTIOUS ACTION, ARISING OUT OF OR IN CONNECTION WITH THE USE OR
17.\" PERFORMANCE OF THIS SOFTWARE.
18.\"
19.Dd $Mdocdate: May 3 2017 $
20.Dt GAI_STRERROR 3
21.Os
22.Sh NAME
23.Nm gai_strerror
24.Nd get error message string from EAI_xxx error code
25.Sh SYNOPSIS
26.In sys/types.h
27.In sys/socket.h
28.In netdb.h
29.Ft "const char *"
30.Fn gai_strerror "int ecode"
31.Sh DESCRIPTION
32The
33.Fn gai_strerror
34function returns an error message string corresponding to the error code
35returned by
36.Xr getaddrinfo 3
37or
38.Xr getnameinfo 3 .
39.Pp
40The following error codes and their meaning are defined in
41.In netdb.h :
42.Pp
43.Bl -tag -width "EAI_ADDRFAMILYXX" -offset indent -compact
44.It Dv EAI_ADDRFAMILY
45address family for
46.Fa name
47not supported
48.It Dv EAI_AGAIN
49temporary failure in name resolution
50.It Dv EAI_BADFLAGS
51invalid value for
52.Fa ai_flags
53.It Dv EAI_BADHINTS
54invalid value for
55.Fa hints
56.It Dv EAI_FAIL
57non-recoverable failure in name resolution
58.It Dv EAI_FAMILY
59.Fa ai_family
60not supported
61.It Dv EAI_MEMORY
62memory allocation failure
63.It Dv EAI_NODATA
64no address associated with
65.Fa name
66.It Dv EAI_NONAME
67.Fa name
68or
69.Fa service
70not provided, or not known
71.It Dv EAI_OVERFLOW
72argument buffer overflow
73.It Dv EAI_PROTOCOL
74resolved protocol is unknown
75.It Dv EAI_SERVICE
76.Fa service
77not supported for
78.Fa ai_socktype
79.It Dv EAI_SOCKTYPE
80.Fa ai_socktype
81not supported
82.It Dv EAI_SYSTEM
83system error (returned in
84.Va errno )
85.El
86.Sh RETURN VALUES
87.Fn gai_strerror
88returns a pointer to the error message string corresponding to
89.Fa ecode .
90If
91.Fa ecode
92is out of range, an implementation-specific error message string is returned.
93.Sh SEE ALSO
94.Xr getaddrinfo 3 ,
95.Xr getnameinfo 3
diff --git a/src/lib/libc/net/gai_strerror.c b/src/lib/libc/net/gai_strerror.c
deleted file mode 100644
index 1e9b585029..0000000000
--- a/src/lib/libc/net/gai_strerror.c
+++ /dev/null
@@ -1,79 +0,0 @@
1/* $OpenBSD: gai_strerror.c,v 1.8 2015/09/14 07:38:38 guenther Exp $ */
2
3/*
4 * Copyright (c) 1997-1999, Craig Metz, 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 Craig Metz and
17 * by other contributors.
18 * 4. Neither the name of the author nor the names of 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
35/* gai_strerror() v1.38 */
36
37#include <sys/types.h>
38#include <netdb.h>
39#include <errno.h>
40
41const char *
42gai_strerror(int errnum)
43{
44 switch (errnum) {
45 case 0:
46 return "no error";
47 case EAI_BADFLAGS:
48 return "invalid value for ai_flags";
49 case EAI_NONAME:
50 return "name or service is not known";
51 case EAI_AGAIN:
52 return "temporary failure in name resolution";
53 case EAI_FAIL:
54 return "non-recoverable failure in name resolution";
55 case EAI_NODATA:
56 return "no address associated with name";
57 case EAI_FAMILY:
58 return "ai_family not supported";
59 case EAI_SOCKTYPE:
60 return "ai_socktype not supported";
61 case EAI_SERVICE:
62 return "service not supported for ai_socktype";
63 case EAI_ADDRFAMILY:
64 return "address family for name not supported";
65 case EAI_MEMORY:
66 return "memory allocation failure";
67 case EAI_SYSTEM:
68 return "system error";
69 case EAI_BADHINTS:
70 return "invalid value for hints";
71 case EAI_PROTOCOL:
72 return "resolved protocol is unknown";
73 case EAI_OVERFLOW:
74 return "argument buffer overflow";
75 default:
76 return "unknown/invalid error";
77 }
78}
79DEF_WEAK(gai_strerror);
diff --git a/src/lib/libc/net/getaddrinfo.3 b/src/lib/libc/net/getaddrinfo.3
deleted file mode 100644
index 780c7a409f..0000000000
--- a/src/lib/libc/net/getaddrinfo.3
+++ /dev/null
@@ -1,477 +0,0 @@
1.\" $OpenBSD: getaddrinfo.3,v 1.61 2022/09/11 06:38:10 jmc Exp $
2.\" $KAME: getaddrinfo.3,v 1.36 2005/01/05 03:23:05 itojun Exp $
3.\"
4.\" Copyright (C) 2004 Internet Systems Consortium, Inc. ("ISC")
5.\" Copyright (C) 2000, 2001 Internet Software Consortium.
6.\"
7.\" Permission to use, copy, modify, and distribute this software for any
8.\" purpose with or without fee is hereby granted, provided that the above
9.\" copyright notice and this permission notice appear in all copies.
10.\"
11.\" THE SOFTWARE IS PROVIDED "AS IS" AND ISC DISCLAIMS ALL WARRANTIES WITH
12.\" REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF MERCHANTABILITY
13.\" AND FITNESS. IN NO EVENT SHALL ISC BE LIABLE FOR ANY SPECIAL, DIRECT,
14.\" INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES WHATSOEVER RESULTING FROM
15.\" LOSS OF USE, DATA OR PROFITS, WHETHER IN AN ACTION OF CONTRACT, NEGLIGENCE
16.\" OR OTHER TORTIOUS ACTION, ARISING OUT OF OR IN CONNECTION WITH THE USE OR
17.\" PERFORMANCE OF THIS SOFTWARE.
18.\"
19.Dd $Mdocdate: September 11 2022 $
20.Dt GETADDRINFO 3
21.Os
22.Sh NAME
23.Nm getaddrinfo ,
24.Nm freeaddrinfo
25.Nd host and service name to socket address structure
26.Sh SYNOPSIS
27.In sys/types.h
28.In sys/socket.h
29.In netdb.h
30.Ft int
31.Fn getaddrinfo "const char *hostname" "const char *servname" \
32 "const struct addrinfo *hints" "struct addrinfo **res"
33.Ft void
34.Fn freeaddrinfo "struct addrinfo *ai"
35.Sh DESCRIPTION
36The
37.Fn getaddrinfo
38function is used to get a list of
39.Tn IP
40addresses and port numbers for host
41.Fa hostname
42and service
43.Fa servname .
44It is a replacement for and provides more flexibility than the
45.Xr gethostbyname 3
46and
47.Xr getservbyname 3
48functions.
49.Pp
50The
51.Fa hostname
52and
53.Fa servname
54arguments are either pointers to NUL-terminated strings or the null pointer.
55An acceptable value for
56.Fa hostname
57is either a valid host name or a numeric host address string consisting
58of a dotted decimal IPv4 address or an IPv6 address.
59The
60.Fa servname
61is either a decimal port number or a service name listed in
62.Xr services 5 .
63At least one of
64.Fa hostname
65and
66.Fa servname
67must be non-null.
68.Pp
69.Fa hints
70is an optional pointer to a
71.Vt struct addrinfo ,
72as defined by
73.In netdb.h :
74.Bd -literal
75struct addrinfo {
76 int ai_flags; /* input flags */
77 int ai_family; /* address family for socket */
78 int ai_socktype; /* socket type */
79 int ai_protocol; /* protocol for socket */
80 socklen_t ai_addrlen; /* length of socket-address */
81 struct sockaddr *ai_addr; /* socket-address for socket */
82 char *ai_canonname; /* canonical name for service location */
83 struct addrinfo *ai_next; /* pointer to next in list */
84};
85.Ed
86.Pp
87This structure can be used to provide hints concerning the type of socket
88that the caller supports or wishes to use.
89The caller can supply the following structure elements in
90.Fa hints :
91.Bl -tag -width "ai_socktypeXX"
92.It Fa ai_family
93The address family that should be used.
94When
95.Fa ai_family
96is set to
97.Dv AF_UNSPEC ,
98it means the caller will accept any address family supported by the
99operating system.
100.It Fa ai_socktype
101Denotes the type of socket that is wanted:
102.Dv SOCK_STREAM ,
103.Dv SOCK_DGRAM ,
104or
105.Dv SOCK_RAW .
106When
107.Fa ai_socktype
108is zero, the caller will accept any socket type.
109.It Fa ai_protocol
110Indicates which transport protocol is desired,
111.Dv IPPROTO_UDP
112or
113.Dv IPPROTO_TCP .
114If
115.Fa ai_protocol
116is zero, the caller will accept any protocol.
117.It Fa ai_flags
118.Fa ai_flags
119is formed by
120.Tn OR Ns 'ing
121the following values:
122.Bl -tag -width "AI_CANONNAMEXX"
123.It Dv AI_ADDRCONFIG
124If the
125.Dv AI_ADDRCONFIG
126bit is set, IPv4 addresses will be returned only if an IPv4 address is
127configured on an interface, and IPv6 addresses will be returned only if an IPv6
128address is configured on an interface.
129Addresses on a loopback interface and link-local IPv6 addresses are not
130considered valid as configured addresses.
131This bit is only considered when determining whether a DNS query should
132be performed or not.
133.It Dv AI_CANONNAME
134If the
135.Dv AI_CANONNAME
136bit is set, a successful call to
137.Fn getaddrinfo
138will return a NUL-terminated string containing the canonical name
139of the specified host name in the
140.Fa ai_canonname
141element of the first
142.Vt addrinfo
143structure returned.
144.It Dv AI_FQDN
145If the
146.Dv AI_FQDN
147bit is set, a successful call to
148.Fn getaddrinfo
149will return a NUL-terminated string containing the fully qualified domain name
150of the specified host name in the
151.Fa ai_canonname
152element of the first
153.Vt addrinfo
154structure returned.
155.Pp
156This is different from the
157.Dv AI_CANONNAME
158bit flag that returns the canonical name registered in DNS,
159which may be different from the fully qualified domain name
160that the host name resolved to.
161Only one of the
162.Dv AI_FQDN
163and
164.Dv AI_CANONNAME
165bits can be set.
166.It Dv AI_NUMERICHOST
167If the
168.Dv AI_NUMERICHOST
169bit is set, it indicates that
170.Fa hostname
171should be treated as a numeric string defining an IPv4 or IPv6 address
172and no name resolution should be attempted.
173.It Dv AI_NUMERICSERV
174If the
175.Dv AI_NUMERICSERV
176bit is set, it indicates that
177.Fa servname
178should be treated as a numeric port string
179and no service name resolution should be attempted.
180.It Dv AI_PASSIVE
181If the
182.Dv AI_PASSIVE
183bit is set, it indicates that the returned socket address structure
184is intended for use in a call to
185.Xr bind 2 .
186In this case, if the
187.Fa hostname
188argument is the null pointer, then the IP address portion of the
189socket address structure will be set to
190.Dv INADDR_ANY
191for an IPv4 address or
192.Dv IN6ADDR_ANY_INIT
193for an IPv6 address.
194.Pp
195If the
196.Dv AI_PASSIVE
197bit is not set, the returned socket address structure will be ready
198for use in a call to
199.Xr connect 2
200for a connection-oriented protocol or
201.Xr connect 2 ,
202.Xr sendto 2 ,
203or
204.Xr sendmsg 2
205if a connectionless protocol was chosen.
206The
207.Tn IP
208address portion of the socket address structure will be set to the
209loopback address if
210.Fa hostname
211is the null pointer and
212.Dv AI_PASSIVE
213is not set.
214.El
215.El
216.Pp
217All other elements of the
218.Vt addrinfo
219structure passed via
220.Fa hints
221must be zero or the null pointer.
222.Pp
223If
224.Fa hints
225is the null pointer,
226.Fn getaddrinfo
227behaves as if the caller provided a
228.Vt struct addrinfo
229with
230.Fa ai_family
231set to
232.Dv AF_UNSPEC ,
233.Fa ai_flags
234set to
235.Dv AI_ADDRCONFIG ,
236and all other elements set to zero or
237.Dv NULL .
238.Pp
239After a successful call to
240.Fn getaddrinfo ,
241.Fa *res
242is a pointer to a linked list of one or more
243.Vt addrinfo
244structures.
245The list can be traversed by following the
246.Fa ai_next
247pointer in each
248.Vt addrinfo
249structure until a null pointer is encountered.
250The three members
251.Fa ai_family ,
252.Fa ai_socktype ,
253and
254.Fa ai_protocol
255in each returned
256.Vt addrinfo
257structure are suitable for a call to
258.Xr socket 2 .
259For each
260.Vt addrinfo
261structure in the list, the
262.Fa ai_addr
263member points to a filled-in socket address structure of length
264.Fa ai_addrlen .
265.Pp
266This implementation of
267.Fn getaddrinfo
268allows numeric IPv6 address notation with scope identifier,
269as documented in RFC 4007.
270By appending the percent character and scope identifier to addresses,
271one can fill the
272.Li sin6_scope_id
273field for addresses.
274This would make management of scoped addresses easier
275and allows cut-and-paste input of scoped addresses.
276.Pp
277At this moment the code supports only link-local addresses with the format.
278The scope identifier is hardcoded to the name of the hardware interface
279associated
280with the link
281.Po
282such as
283.Li ne0
284.Pc .
285An example is
286.Dq Li fe80::1%ne0 ,
287which means
288.Do
289.Li fe80::1
290on the link associated with the
291.Li ne0
292interface
293.Dc .
294.Pp
295The current implementation assumes a one-to-one relationship between
296the interface and link, which is not necessarily true from the specification.
297.Pp
298All of the information returned by
299.Fn getaddrinfo
300is dynamically allocated: the
301.Vt addrinfo
302structures themselves as well as the socket address structures and
303the canonical host name strings included in the
304.Vt addrinfo
305structures.
306.Pp
307Memory allocated for the dynamically allocated structures created by
308a successful call to
309.Fn getaddrinfo
310is released by the
311.Fn freeaddrinfo
312function.
313The
314.Fa ai
315pointer should be an
316.Vt addrinfo
317structure created by a call to
318.Fn getaddrinfo .
319.Sh RETURN VALUES
320.Fn getaddrinfo
321returns zero on success or one of the error codes listed in
322.Xr gai_strerror 3
323if an error occurs.
324If an error occurs, no memory is allocated by
325.Fn getaddrinfo ,
326therefore it is not necessary to release the
327.Vt addrinfo
328structure(s).
329.Sh EXAMPLES
330The following code tries to connect to
331.Dq Li www.kame.net
332service
333.Dq Li www
334via a stream socket.
335It loops through all the addresses available, regardless of address family.
336If the destination resolves to an IPv4 address, it will use an
337.Dv AF_INET
338socket.
339Similarly, if it resolves to IPv6, an
340.Dv AF_INET6
341socket is used.
342Observe that there is no hardcoded reference to a particular address family.
343The code works even if
344.Fn getaddrinfo
345returns addresses that are not IPv4/v6.
346.Bd -literal -offset indent
347struct addrinfo hints, *res, *res0;
348int error;
349int save_errno;
350int s;
351const char *cause = NULL;
352
353memset(&hints, 0, sizeof(hints));
354hints.ai_family = AF_UNSPEC;
355hints.ai_socktype = SOCK_STREAM;
356error = getaddrinfo("www.kame.net", "www", &hints, &res0);
357if (error)
358 errx(1, "%s", gai_strerror(error));
359s = -1;
360for (res = res0; res; res = res->ai_next) {
361 s = socket(res->ai_family, res->ai_socktype,
362 res->ai_protocol);
363 if (s == -1) {
364 cause = "socket";
365 continue;
366 }
367
368 if (connect(s, res->ai_addr, res->ai_addrlen) == -1) {
369 cause = "connect";
370 save_errno = errno;
371 close(s);
372 errno = save_errno;
373 s = -1;
374 continue;
375 }
376
377 break; /* okay we got one */
378}
379if (s == -1)
380 err(1, "%s", cause);
381freeaddrinfo(res0);
382.Ed
383.Pp
384The following example tries to open a wildcard listening socket onto service
385.Dq Li www ,
386for all the address families available.
387.Bd -literal -offset indent
388struct addrinfo hints, *res, *res0;
389int error;
390int save_errno;
391int s[MAXSOCK];
392int nsock;
393const char *cause = NULL;
394
395memset(&hints, 0, sizeof(hints));
396hints.ai_family = AF_UNSPEC;
397hints.ai_socktype = SOCK_STREAM;
398hints.ai_flags = AI_PASSIVE;
399error = getaddrinfo(NULL, "www", &hints, &res0);
400if (error)
401 errx(1, "%s", gai_strerror(error));
402nsock = 0;
403for (res = res0; res && nsock < MAXSOCK; res = res->ai_next) {
404 s[nsock] = socket(res->ai_family, res->ai_socktype,
405 res->ai_protocol);
406 if (s[nsock] == -1) {
407 cause = "socket";
408 continue;
409 }
410
411 if (bind(s[nsock], res->ai_addr, res->ai_addrlen) == -1) {
412 cause = "bind";
413 save_errno = errno;
414 close(s[nsock]);
415 errno = save_errno;
416 continue;
417 }
418 (void) listen(s[nsock], 5);
419
420 nsock++;
421}
422if (nsock == 0)
423 err(1, "%s", cause);
424freeaddrinfo(res0);
425.Ed
426.Sh SEE ALSO
427.Xr bind 2 ,
428.Xr connect 2 ,
429.Xr send 2 ,
430.Xr socket 2 ,
431.Xr gai_strerror 3 ,
432.Xr gethostbyname 3 ,
433.Xr getnameinfo 3 ,
434.Xr getservbyname 3 ,
435.Xr res_init 3 ,
436.Xr hosts 5 ,
437.Xr resolv.conf 5 ,
438.Xr services 5 ,
439.Xr hostname 7
440.Rs
441.%A Craig Metz
442.%B Proceedings of the Freenix Track: 2000 USENIX Annual Technical Conference
443.%D June 2000
444.%T Protocol Independence Using the Sockets API
445.Re
446.Sh STANDARDS
447The
448.Fn getaddrinfo
449function is defined by the
450.St -p1003.1g-2000
451draft specification and documented in RFC 3493.
452.Pp
453The
454.Dv AI_FQDN
455flag bit first appeared in Windows 7.
456.Pp
457.Rs
458.%A R. Gilligan
459.%A S. Thomson
460.%A J. Bound
461.%A J. McCann
462.%A W. Stevens
463.%D February 2003
464.%R RFC 3493
465.%T Basic Socket Interface Extensions for IPv6
466.Re
467.Pp
468.Rs
469.%A S. Deering
470.%A B. Haberman
471.%A T. Jinmei
472.%A E. Nordmark
473.%A B. Zill
474.%D March 2005
475.%R RFC 4007
476.%T IPv6 Scoped Address Architecture
477.Re
diff --git a/src/lib/libc/net/gethostbyname.3 b/src/lib/libc/net/gethostbyname.3
deleted file mode 100644
index 06170c3c1a..0000000000
--- a/src/lib/libc/net/gethostbyname.3
+++ /dev/null
@@ -1,271 +0,0 @@
1.\" $OpenBSD: gethostbyname.3,v 1.34 2019/08/30 20:20:50 jmc 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. Neither the name of the University nor the names of its contributors
15.\" may be used to endorse or promote products derived from this software
16.\" without specific prior written permission.
17.\"
18.\" THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
19.\" ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
20.\" IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
21.\" ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
22.\" FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
23.\" DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
24.\" OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
25.\" HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
26.\" LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
27.\" OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
28.\" SUCH DAMAGE.
29.\"
30.Dd $Mdocdate: August 30 2019 $
31.Dt GETHOSTBYNAME 3
32.Os
33.Sh NAME
34.Nm gethostbyname ,
35.Nm gethostbyname2 ,
36.Nm gethostbyaddr ,
37.Nm gethostent ,
38.Nm sethostent ,
39.Nm endhostent ,
40.Nm hstrerror ,
41.Nm herror
42.Nd get network host entry
43.Sh SYNOPSIS
44.In netdb.h
45.Vt extern int h_errno ;
46.Ft struct hostent *
47.Fn gethostbyname "const char *name"
48.Ft struct hostent *
49.Fn gethostent void
50.Ft void
51.Fn sethostent "int stayopen"
52.Ft void
53.Fn endhostent void
54.Ft void
55.Fn herror "const char *string"
56.Ft const char *
57.Fn hstrerror "int err"
58.In sys/socket.h
59.In netdb.h
60.Ft struct hostent *
61.Fn gethostbyname2 "const char *name" "int af"
62.Ft struct hostent *
63.Fn gethostbyaddr "const void *addr" "socklen_t len" "int af"
64.Sh DESCRIPTION
65The
66.Fn gethostbyname ,
67.Fn gethostbyname2 ,
68and
69.Fn gethostbyaddr
70functions each return a pointer to an object with the following structure
71describing an Internet host referenced by
72.Fa name
73or
74.Fa addr ,
75respectively.
76This structure contains either information obtained from a name server,
77broken-out fields from a line in
78.Pa /etc/hosts ,
79or database entries supplied by the
80.Xr yp 8
81system.
82.Xr resolv.conf 5
83describes how the particular database is chosen.
84.Bd -literal -offset indent
85struct hostent {
86 char *h_name; /* official name of host */
87 char **h_aliases; /* alias list */
88 int h_addrtype; /* host address type */
89 int h_length; /* length of address */
90 char **h_addr_list; /* list of returned addresses */
91};
92#define h_addr h_addr_list[0] /* address, for backward compat */
93.Ed
94.Pp
95The members of this structure are:
96.Bl -tag -width h_addr_list
97.It Fa h_name
98Official name of the host.
99.It Fa h_aliases
100A
101.Dv NULL Ns -terminated
102array of alternate names for the host.
103.It Fa h_addrtype
104The type of address being returned.
105.It Fa h_length
106The length, in bytes, of the address.
107.It Fa h_addr_list
108A
109.Dv NULL Ns -terminated
110array of network addresses for the host.
111Host addresses are returned in network byte order.
112.It Fa h_addr
113The first address in
114.Fa h_addr_list ;
115this is for backward compatibility.
116.El
117.Pp
118The function
119.Fn gethostbyname
120will search for the named host in the current domain and its parents
121using the search lookup semantics detailed in
122.Xr resolv.conf 5
123and
124.Xr hostname 7 .
125.Pp
126.Fn gethostbyname2
127is similar to
128.Fn gethostbyname
129except that it supports an
130.Fa af
131of
132.Dv AF_INET6
133in addition to
134.Dv AF_INET .
135.Pp
136The
137.Fn gethostbyaddr
138function will search for the specified address of length
139.Fa len
140in the address family
141.Fa af .
142The only address family supported is
143.Dv AF_INET .
144.Pp
145The
146.Fn sethostent ,
147.Fn gethostent ,
148and
149.Fn endhostent
150functions are deprecated and no longer have any effect.
151They could be used in the past for queries over a persistent TCP
152connection or to iterate entries in the
153.Xr hosts 5
154file.
155.Pp
156The
157.Fn herror
158function prints an error message describing the failure.
159If its argument
160.Fa string
161is not
162.Dv NULL ,
163it is prepended to the message string and separated from it by a colon
164.Pq Ql \&:
165and a space.
166The error message is printed with a trailing newline.
167The contents of the error message is the same as that returned by
168.Fn hstrerror
169with argument
170.Va h_errno .
171.Sh ENVIRONMENT
172.Bl -tag -width RES_OPTIONS
173.It Ev RES_OPTIONS
174A list of options to override the resolver's internal defaults.
175See
176.Xr resolv.conf 5
177for more information.
178.El
179.Sh FILES
180.Bl -tag -width /etc/resolv.conf -compact
181.It Pa /etc/hosts
182.It Pa /etc/resolv.conf
183.El
184.Sh DIAGNOSTICS
185Error return status from
186.Fn gethostbyname ,
187.Fn gethostbyname2 ,
188and
189.Fn gethostbyaddr
190is indicated by return of a
191.Dv NULL
192pointer.
193The external integer
194.Va h_errno
195may then be checked to see whether this is a temporary failure
196or an invalid or unknown host.
197.Pp
198The variable
199.Va h_errno
200can have the following values:
201.Bl -tag -width HOST_NOT_FOUND
202.It Dv HOST_NOT_FOUND
203No such host is known.
204.It Dv TRY_AGAIN
205This is usually a temporary error
206and means that the local server did not receive
207a response from an authoritative server.
208A retry at some later time may succeed.
209.It Dv NO_RECOVERY
210Some unexpected server failure was encountered.
211This is a non-recoverable error.
212.It Dv NO_DATA
213The requested name is valid but does not have an IP address;
214this is not a temporary error.
215This means that the name is known to the name server but there is no address
216associated with this name.
217Another type of request to the name server using this domain name
218will result in an answer;
219for example, a mail-forwarder may be registered for this domain.
220.It Dv NETDB_INTERNAL
221An internal error occurred.
222This may occur when an address family other than
223.Dv AF_INET
224or
225.Dv AF_INET6
226is specified or when a resource is unable to be allocated.
227It is always set by
228.Fn gethostent .
229.It Dv NETDB_SUCCESS
230The function completed successfully.
231.El
232.Sh SEE ALSO
233.Xr getaddrinfo 3 ,
234.Xr getnameinfo 3 ,
235.Xr res_init 3 ,
236.Xr hosts 5 ,
237.Xr resolv.conf 5 ,
238.Xr hostname 7
239.Sh HISTORY
240The
241.Fn endhostent ,
242.Fn gethostbyaddr ,
243.Fn gethostbyname ,
244.Fn gethostent ,
245and
246.Fn sethostent
247functions appeared in
248.Bx 4.1c .
249The function
250.Fn herror
251was added in
252.Bx 4.3 Tahoe ,
253.Fn hstrerror
254in
255.Bx 4.4 ,
256and
257.Fn gethostbyname2
258in
259.Ox 2.1 .
260.Sh BUGS
261These functions use static data storage;
262if the data is needed for future use, it should be
263copied before any subsequent calls overwrite it.
264.Pp
265Only the Internet
266address formats are currently understood.
267.Pp
268YP does not support any address families other than
269.Dv AF_INET
270and uses
271the traditional database format.
diff --git a/src/lib/libc/net/getifaddrs.3 b/src/lib/libc/net/getifaddrs.3
deleted file mode 100644
index 8ea08fa3da..0000000000
--- a/src/lib/libc/net/getifaddrs.3
+++ /dev/null
@@ -1,157 +0,0 @@
1.\" $OpenBSD: getifaddrs.3,v 1.23 2022/03/29 18:15:52 naddy Exp $
2.\" BSDI getifaddrs.3,v 2.5 2000/02/23 14:51:59 dab Exp
3.\"
4.\" Copyright (c) 1995, 1999
5.\" Berkeley Software Design, Inc. 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.\"
13.\" THIS SOFTWARE IS PROVIDED BY Berkeley Software Design, Inc. ``AS IS'' AND
14.\" ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
15.\" IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
16.\" ARE DISCLAIMED. IN NO EVENT SHALL Berkeley Software Design, Inc. BE LIABLE
17.\" FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
18.\" DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
19.\" OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
20.\" HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
21.\" LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
22.\" OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
23.\" SUCH DAMAGE.
24.Dd $Mdocdate: March 29 2022 $
25.Dt GETIFADDRS 3
26.Os
27.Sh NAME
28.Nm getifaddrs ,
29.Nm freeifaddrs
30.Nd get interface addresses
31.Sh SYNOPSIS
32.In sys/types.h
33.In sys/socket.h
34.In ifaddrs.h
35.Ft int
36.Fn getifaddrs "struct ifaddrs **ifap"
37.Ft void
38.Fn freeifaddrs "struct ifaddrs *ifap"
39.Sh DESCRIPTION
40The
41.Fn getifaddrs
42function stores a reference to a linked list of the network interfaces
43on the local machine in the memory referenced by
44.Fa ifap .
45The list consists of
46.Vt ifaddrs
47structures, as defined in the include file
48.In ifaddrs.h .
49The
50.Vt ifaddrs
51structure contains at least the following entries:
52.Bd -literal
53 struct ifaddrs *ifa_next; /* Pointer to next struct */
54 char *ifa_name; /* Interface name */
55 u_int ifa_flags; /* Interface flags */
56 struct sockaddr *ifa_addr; /* Interface address */
57 struct sockaddr *ifa_netmask; /* Interface netmask */
58 struct sockaddr *ifa_broadaddr; /* Interface broadcast address */
59 struct sockaddr *ifa_dstaddr; /* P2P interface destination */
60 void *ifa_data; /* Address specific data */
61.Ed
62.Bl -tag -width ifa_broadaddr
63.It Fa ifa_next
64Contains a pointer to the next structure on the list.
65This field is set to
66.Dv NULL
67in the last structure on the list.
68.It Fa ifa_name
69Contains the interface name.
70.It Fa ifa_flags
71Contains the interface flags, as set by
72.Xr ifconfig 8 .
73.It Fa ifa_addr
74References either the address of the interface or the link level
75address of the interface, if one exists, otherwise it is
76.Dv NULL .
77(The
78.Fa sa_family
79field of the
80.Fa ifa_addr
81field should be consulted to determine the format of the
82.Fa ifa_addr
83address.)
84.It Fa ifa_netmask
85References the netmask associated with
86.Fa ifa_addr ,
87if one is set, otherwise it is
88.Dv NULL .
89.It Fa ifa_broadaddr
90This field, which should only be referenced for non-P2P interfaces,
91references the broadcast address associated with
92.Fa ifa_addr ,
93if one exists, otherwise it is
94.Dv NULL .
95.It Fa ifa_dstaddr
96This field, which should only be referenced for P2P interfaces,
97references the destination address on a P2P interface,
98if one exists, otherwise it is
99.Dv NULL .
100.It Fa ifa_data
101References address family specific data.
102For
103.Dv AF_LINK
104addresses it contains a pointer to the
105.Vt struct if_data
106(as defined in include file
107.In net/if.h )
108which contains various interface attributes and statistics.
109For all other address families,
110.Fa ifa_data
111is
112.Dv NULL .
113.El
114.Pp
115The data returned by
116.Fn getifaddrs
117is dynamically allocated and should be freed using
118.Fn freeifaddrs
119when no longer needed.
120.Sh RETURN VALUES
121.Rv -std
122.Sh ERRORS
123The
124.Fn getifaddrs
125function may fail and set
126.Va errno
127for any of the errors specified for the library routines
128.Xr ioctl 2 ,
129.Xr socket 2 ,
130.Xr malloc 3 ,
131or
132.Xr sysctl 2 .
133.Sh SEE ALSO
134.Xr ioctl 2 ,
135.Xr socket 2 ,
136.Xr sysctl 2 ,
137.Xr netintro 4 ,
138.Xr ifconfig 8
139.Sh HISTORY
140The
141.Fn getifaddrs
142function first appeared in BSDI
143.Bsx .
144The function has been available on
145.Ox
146since
147.Ox 2.7 .
148.Sh BUGS
149If both
150.In net/if.h
151and
152.In ifaddrs.h
153are being included,
154.In net/if.h
155.Em must
156be included before
157.In ifaddrs.h .
diff --git a/src/lib/libc/net/getifaddrs.c b/src/lib/libc/net/getifaddrs.c
deleted file mode 100644
index 069ee9afab..0000000000
--- a/src/lib/libc/net/getifaddrs.c
+++ /dev/null
@@ -1,298 +0,0 @@
1/* $OpenBSD: getifaddrs.c,v 1.14 2021/11/29 03:20:37 deraadt Exp $ */
2
3/*
4 * Copyright (c) 1995, 1999
5 * Berkeley Software Design, Inc. 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 *
13 * THIS SOFTWARE IS PROVIDED BY Berkeley Software Design, Inc. ``AS IS'' AND
14 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
15 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
16 * ARE DISCLAIMED. IN NO EVENT SHALL Berkeley Software Design, Inc. BE LIABLE
17 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
18 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
19 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
20 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
21 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
22 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
23 * SUCH DAMAGE.
24 *
25 * BSDI getifaddrs.c,v 2.12 2000/02/23 14:51:59 dab Exp
26 */
27
28#include <sys/param.h> /* ALIGN ALIGNBYTES */
29#include <sys/types.h>
30#include <sys/ioctl.h>
31#include <sys/socket.h>
32#include <net/if.h>
33#include <net/route.h>
34#include <sys/sysctl.h>
35#include <net/if_dl.h>
36
37#include <errno.h>
38#include <ifaddrs.h>
39#include <stddef.h>
40#include <stdlib.h>
41#include <string.h>
42#include <unistd.h>
43
44#define SALIGN (sizeof(long) - 1)
45#define SA_RLEN(sa) ((sa)->sa_len ? (((sa)->sa_len + SALIGN) & ~SALIGN) : (SALIGN + 1))
46
47int
48getifaddrs(struct ifaddrs **pif)
49{
50 int icnt = 1;
51 int dcnt = 0;
52 int ncnt = 0;
53 int mib[6];
54 size_t needed;
55 char *buf = NULL, *bufp;
56 char *next;
57 struct ifaddrs *cif = 0;
58 char *p, *p0;
59 struct rt_msghdr *rtm;
60 struct if_msghdr *ifm;
61 struct ifa_msghdr *ifam;
62 struct sockaddr_dl *dl;
63 struct sockaddr *sa;
64 u_short index = 0;
65 size_t len, alen, dlen;
66 struct ifaddrs *ifa, *ift;
67 int i;
68 char *data;
69 char *names;
70
71 mib[0] = CTL_NET;
72 mib[1] = PF_ROUTE;
73 mib[2] = 0; /* protocol */
74 mib[3] = 0; /* wildcard address family */
75 mib[4] = NET_RT_IFLIST;
76 mib[5] = 0; /* no flags */
77 while (1) {
78 if (sysctl(mib, 6, NULL, &needed, NULL, 0) == -1) {
79 free(buf);
80 return (-1);
81 }
82 if (needed == 0)
83 break;
84 if ((bufp = realloc(buf, needed)) == NULL) {
85 free(buf);
86 return (-1);
87 }
88 buf = bufp;
89 if (sysctl(mib, 6, buf, &needed, NULL, 0) == -1) {
90 if (errno == ENOMEM)
91 continue;
92 free(buf);
93 return (-1);
94 }
95 break;
96 }
97
98 for (next = buf; next < buf + needed; next += rtm->rtm_msglen) {
99 rtm = (struct rt_msghdr *)next;
100 if (rtm->rtm_version != RTM_VERSION)
101 continue;
102 switch (rtm->rtm_type) {
103 case RTM_IFINFO:
104 ifm = (struct if_msghdr *)rtm;
105 if (ifm->ifm_addrs & RTA_IFP) {
106 index = ifm->ifm_index;
107 ++icnt;
108 dl = (struct sockaddr_dl *)(next +
109 rtm->rtm_hdrlen);
110 dcnt += SA_RLEN((struct sockaddr *)dl) +
111 ALIGNBYTES;
112 dcnt += sizeof(ifm->ifm_data);
113 ncnt += dl->sdl_nlen + 1;
114 } else
115 index = 0;
116 break;
117
118 case RTM_NEWADDR:
119 ifam = (struct ifa_msghdr *)rtm;
120 if (index && ifam->ifam_index != index)
121 abort(); /* XXX abort illegal in library */
122
123#define RTA_MASKS (RTA_NETMASK | RTA_IFA | RTA_BRD)
124 if (index == 0 || (ifam->ifam_addrs & RTA_MASKS) == 0)
125 break;
126 p = next + rtm->rtm_hdrlen;
127 ++icnt;
128 /* Scan to look for length of address */
129 alen = 0;
130 for (p0 = p, i = 0; i < RTAX_MAX; i++) {
131 if ((RTA_MASKS & ifam->ifam_addrs & (1 << i))
132 == 0)
133 continue;
134 sa = (struct sockaddr *)p;
135 len = SA_RLEN(sa);
136 if (i == RTAX_IFA) {
137 alen = len;
138 break;
139 }
140 p += len;
141 }
142 for (p = p0, i = 0; i < RTAX_MAX; i++) {
143 if ((RTA_MASKS & ifam->ifam_addrs & (1 << i))
144 == 0)
145 continue;
146 sa = (struct sockaddr *)p;
147 len = SA_RLEN(sa);
148 if (i == RTAX_NETMASK && sa->sa_len == 0)
149 dcnt += alen;
150 else
151 dcnt += len;
152 p += len;
153 }
154 break;
155 }
156 }
157
158 if (icnt + dcnt + ncnt == 1) {
159 *pif = NULL;
160 free(buf);
161 return (0);
162 }
163 data = malloc(sizeof(struct ifaddrs) * icnt + dcnt + ncnt);
164 if (data == NULL) {
165 free(buf);
166 return(-1);
167 }
168
169 ifa = (struct ifaddrs *)data;
170 data += sizeof(struct ifaddrs) * icnt;
171 names = data + dcnt;
172
173 memset(ifa, 0, sizeof(struct ifaddrs) * icnt);
174 ift = ifa;
175
176 index = 0;
177 for (next = buf; next < buf + needed; next += rtm->rtm_msglen) {
178 rtm = (struct rt_msghdr *)next;
179 if (rtm->rtm_version != RTM_VERSION)
180 continue;
181 switch (rtm->rtm_type) {
182 case RTM_IFINFO:
183 ifm = (struct if_msghdr *)rtm;
184 if (ifm->ifm_addrs & RTA_IFP) {
185 index = ifm->ifm_index;
186 dl = (struct sockaddr_dl *)(next +
187 rtm->rtm_hdrlen);
188
189 cif = ift;
190 ift->ifa_name = names;
191 ift->ifa_flags = (int)ifm->ifm_flags;
192 memcpy(names, dl->sdl_data, dl->sdl_nlen);
193 names[dl->sdl_nlen] = 0;
194 names += dl->sdl_nlen + 1;
195
196 ift->ifa_addr = (struct sockaddr *)data;
197 memcpy(data, dl,
198 ((struct sockaddr *)dl)->sa_len);
199 data += SA_RLEN((struct sockaddr *)dl);
200
201 /* ifm_data needs to be aligned */
202 ift->ifa_data = data = (void *)ALIGN(data);
203 dlen = rtm->rtm_hdrlen -
204 offsetof(struct if_msghdr, ifm_data);
205 if (dlen > sizeof(ifm->ifm_data))
206 dlen = sizeof(ifm->ifm_data);
207 memcpy(data, &ifm->ifm_data, dlen);
208 data += sizeof(ifm->ifm_data);
209
210 ift = (ift->ifa_next = ift + 1);
211 } else
212 index = 0;
213 break;
214
215 case RTM_NEWADDR:
216 ifam = (struct ifa_msghdr *)rtm;
217 if (index && ifam->ifam_index != index)
218 abort(); /* XXX abort illegal in library */
219
220 if (index == 0 || (ifam->ifam_addrs & RTA_MASKS) == 0)
221 break;
222 ift->ifa_name = cif->ifa_name;
223 ift->ifa_flags = cif->ifa_flags;
224 ift->ifa_data = NULL;
225 p = next + rtm->rtm_hdrlen;
226 /* Scan to look for length of address */
227 alen = 0;
228 for (p0 = p, i = 0; i < RTAX_MAX; i++) {
229 if ((RTA_MASKS & ifam->ifam_addrs & (1 << i))
230 == 0)
231 continue;
232 sa = (struct sockaddr *)p;
233 len = SA_RLEN(sa);
234 if (i == RTAX_IFA) {
235 alen = len;
236 break;
237 }
238 p += len;
239 }
240 for (p = p0, i = 0; i < RTAX_MAX; i++) {
241 if ((RTA_MASKS & ifam->ifam_addrs & (1 << i))
242 == 0)
243 continue;
244 sa = (struct sockaddr *)p;
245 len = SA_RLEN(sa);
246 switch (i) {
247 case RTAX_IFA:
248 ift->ifa_addr = (struct sockaddr *)data;
249 memcpy(data, p, len);
250 data += len;
251 break;
252
253 case RTAX_NETMASK:
254 ift->ifa_netmask =
255 (struct sockaddr *)data;
256 if (sa->sa_len == 0) {
257 memset(data, 0, alen);
258 data += alen;
259 break;
260 }
261 memcpy(data, p, len);
262 data += len;
263 break;
264
265 case RTAX_BRD:
266 ift->ifa_broadaddr =
267 (struct sockaddr *)data;
268 memcpy(data, p, len);
269 data += len;
270 break;
271 }
272 p += len;
273 }
274
275
276 ift = (ift->ifa_next = ift + 1);
277 break;
278 }
279 }
280
281 free(buf);
282 if (--ift >= ifa) {
283 ift->ifa_next = NULL;
284 *pif = ifa;
285 } else {
286 *pif = NULL;
287 free(ifa);
288 }
289 return (0);
290}
291DEF_WEAK(getifaddrs);
292
293void
294freeifaddrs(struct ifaddrs *ifp)
295{
296 free(ifp);
297}
298DEF_WEAK(freeifaddrs);
diff --git a/src/lib/libc/net/getnameinfo.3 b/src/lib/libc/net/getnameinfo.3
deleted file mode 100644
index 4e97b5d07b..0000000000
--- a/src/lib/libc/net/getnameinfo.3
+++ /dev/null
@@ -1,264 +0,0 @@
1.\" $OpenBSD: getnameinfo.3,v 1.49 2022/09/11 06:38:10 jmc Exp $
2.\" $KAME: getnameinfo.3,v 1.37 2005/01/05 03:23:05 itojun Exp $
3.\"
4.\" Copyright (C) 2004 Internet Systems Consortium, Inc. ("ISC")
5.\" Copyright (C) 2000, 2001 Internet Software Consortium.
6.\"
7.\" Permission to use, copy, modify, and distribute this software for any
8.\" purpose with or without fee is hereby granted, provided that the above
9.\" copyright notice and this permission notice appear in all copies.
10.\"
11.\" THE SOFTWARE IS PROVIDED "AS IS" AND ISC DISCLAIMS ALL WARRANTIES WITH
12.\" REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF MERCHANTABILITY
13.\" AND FITNESS. IN NO EVENT SHALL ISC BE LIABLE FOR ANY SPECIAL, DIRECT,
14.\" INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES WHATSOEVER RESULTING FROM
15.\" LOSS OF USE, DATA OR PROFITS, WHETHER IN AN ACTION OF CONTRACT, NEGLIGENCE
16.\" OR OTHER TORTIOUS ACTION, ARISING OUT OF OR IN CONNECTION WITH THE USE OR
17.\" PERFORMANCE OF THIS SOFTWARE.
18.\"
19.Dd $Mdocdate: September 11 2022 $
20.Dt GETNAMEINFO 3
21.Os
22.Sh NAME
23.Nm getnameinfo
24.Nd socket address structure to hostname and service name
25.Sh SYNOPSIS
26.In sys/types.h
27.In sys/socket.h
28.In netdb.h
29.Ft int
30.Fn getnameinfo "const struct sockaddr *sa" "socklen_t salen" "char *host" \
31 "size_t hostlen" "char *serv" "size_t servlen" "int flags"
32.Sh DESCRIPTION
33The
34.Fn getnameinfo
35function is used to convert a
36.Vt sockaddr
37structure to a pair of host name and service strings.
38It is a replacement for and provides more flexibility than the
39.Xr gethostbyaddr 3
40and
41.Xr getservbyport 3
42functions and is the converse of the
43.Xr getaddrinfo 3
44function.
45.Pp
46The
47.Vt sockaddr
48structure
49.Fa sa
50should point to either a
51.Vt sockaddr_in
52or
53.Vt sockaddr_in6
54structure (for IPv4 or IPv6 respectively) that is
55.Fa salen
56bytes long.
57.Pp
58The host and service names associated with
59.Fa sa
60are stored in
61.Fa host
62and
63.Fa serv
64which have length parameters
65.Fa hostlen
66and
67.Fa servlen .
68The maximum value for
69.Fa hostlen
70is
71.Dv NI_MAXHOST
72and
73the maximum value for
74.Fa servlen
75is
76.Dv NI_MAXSERV ,
77as defined by
78.In netdb.h .
79If a length parameter is zero, no string will be stored.
80Otherwise, enough space must be provided to store the
81host name or service string plus a byte for the NUL terminator.
82.Pp
83The
84.Fa flags
85argument is formed by
86.Tn OR Ns 'ing
87the following values:
88.Bl -tag -width "NI_NUMERICHOSTXX"
89.It Dv NI_NOFQDN
90A fully qualified domain name is not required for local hosts.
91The local part of the fully qualified domain name is returned instead.
92.It Dv NI_NUMERICHOST
93Return the address in numeric form, as if calling
94.Xr inet_ntop 3 ,
95instead of a host name.
96.It Dv NI_NAMEREQD
97A name is required.
98If the host name cannot be found in DNS and this flag is set,
99a non-zero error code is returned.
100If the host name is not found and the flag is not set, the
101address is returned in numeric form.
102.It NI_NUMERICSERV
103The service name is returned as a digit string representing the port number.
104.It NI_DGRAM
105Specifies that the service being looked up is a datagram
106service, and causes
107.Xr getservbyport 3
108to be called with a second argument of
109.Dq udp
110instead of its default of
111.Dq tcp .
112This is required for the few ports (512\-514) that have different services
113for
114.Tn UDP
115and
116.Tn TCP .
117.El
118.Pp
119This implementation allows numeric IPv6 address notation with scope identifier,
120as documented in RFC 4007.
121IPv6 link-local address will appear as a string like
122.Dq Li fe80::1%ne0 .
123Refer to
124.Xr getaddrinfo 3
125for more information.
126.Sh RETURN VALUES
127.Fn getnameinfo
128returns zero on success or one of the error codes listed in
129.Xr gai_strerror 3
130if an error occurs.
131.Sh EXAMPLES
132The following code tries to get a numeric host name, and service name,
133for a given socket address.
134Observe that there is no hardcoded reference to a particular address family.
135.Bd -literal -offset indent
136struct sockaddr *sa; /* input */
137char hbuf[NI_MAXHOST], sbuf[NI_MAXSERV];
138
139if (getnameinfo(sa, sa->sa_len, hbuf, sizeof(hbuf), sbuf,
140 sizeof(sbuf), NI_NUMERICHOST | NI_NUMERICSERV))
141 errx(1, "could not get numeric hostname");
142printf("host=%s, serv=%s\en", hbuf, sbuf);
143.Ed
144.Pp
145The following version checks if the socket address has a reverse address mapping:
146.Bd -literal -offset indent
147struct sockaddr *sa; /* input */
148char hbuf[NI_MAXHOST];
149
150if (getnameinfo(sa, sa->sa_len, hbuf, sizeof(hbuf), NULL, 0,
151 NI_NAMEREQD))
152 errx(1, "could not resolve hostname");
153printf("host=%s\en", hbuf);
154.Ed
155.Sh SEE ALSO
156.Xr gai_strerror 3 ,
157.Xr getaddrinfo 3 ,
158.Xr gethostbyaddr 3 ,
159.Xr getservbyport 3 ,
160.Xr inet_ntop 3 ,
161.Xr res_init 3 ,
162.Xr hosts 5 ,
163.Xr resolv.conf 5 ,
164.Xr services 5 ,
165.Xr hostname 7
166.Rs
167.%A Craig Metz
168.%T Protocol Independence Using the Sockets API
169.%B Proceedings of the Freenix Track: 2000 USENIX Annual Technical Conference
170.%D June 2000
171.Re
172.Sh STANDARDS
173The
174.Fn getnameinfo
175function is defined by the
176.St -p1003.1g-2000
177draft specification and documented in RFC 3493.
178.Pp
179.Rs
180.%A R. Gilligan
181.%A S. Thomson
182.%A J. Bound
183.%A J. McCann
184.%A W. Stevens
185.%D February 2003
186.%R RFC 3493
187.%T Basic Socket Interface Extensions for IPv6
188.Re
189.Pp
190.Rs
191.%A S. Deering
192.%A B. Haberman
193.%A T. Jinmei
194.%A E. Nordmark
195.%A B. Zill
196.%D March 2005
197.%R RFC 4007
198.%T IPv6 Scoped Address Architecture
199.Re
200.Sh CAVEATS
201.Fn getnameinfo
202can return both numeric and FQDN forms of the address specified in
203.Fa sa .
204There is no return value that indicates whether the string returned in
205.Fa host
206is a result of binary to numeric-text translation (like
207.Xr inet_ntop 3 ) ,
208or is the result of a DNS reverse lookup.
209Because of this, malicious parties could set up a PTR record as follows:
210.Bd -literal -offset indent
2111.0.0.127.in-addr.arpa. IN PTR 10.1.1.1
212.Ed
213.Pp
214and trick the caller of
215.Fn getnameinfo
216into believing that
217.Fa sa
218is
219.Li 10.1.1.1
220when it is actually
221.Li 127.0.0.1 .
222.Pp
223To prevent such attacks, the use of
224.Dv NI_NAMEREQD
225is recommended when the result of
226.Fn getnameinfo
227is used
228for access control purposes:
229.Bd -literal -offset indent
230struct sockaddr *sa;
231char addr[NI_MAXHOST];
232struct addrinfo hints, *res;
233int error;
234
235error = getnameinfo(sa, sa->sa_len, addr, sizeof(addr),
236 NULL, 0, NI_NAMEREQD);
237if (error == 0) {
238 memset(&hints, 0, sizeof(hints));
239 hints.ai_socktype = SOCK_DGRAM; /*dummy*/
240 hints.ai_flags = AI_NUMERICHOST;
241 if (getaddrinfo(addr, "0", &hints, &res) == 0) {
242 /* malicious PTR record */
243 freeaddrinfo(res);
244 printf("bogus PTR record\en");
245 return -1;
246 }
247 /* addr is FQDN as a result of PTR lookup */
248} else {
249 /* addr is numeric string */
250 error = getnameinfo(sa, sa->sa_len, addr, sizeof(addr),
251 NULL, 0, NI_NUMERICHOST);
252}
253.Ed
254.Sh BUGS
255The implementation of
256.Fn getnameinfo
257is not thread-safe.
258.Pp
259.Ox
260intentionally uses a different
261.Dv NI_MAXHOST
262value from what
263.Tn "RFC 2553"
264suggests, to avoid buffer length handling mistakes.
diff --git a/src/lib/libc/net/getnetent.3 b/src/lib/libc/net/getnetent.3
deleted file mode 100644
index da2c4ef4a3..0000000000
--- a/src/lib/libc/net/getnetent.3
+++ /dev/null
@@ -1,147 +0,0 @@
1.\" $OpenBSD: getnetent.3,v 1.19 2019/08/30 20:20:51 jmc 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. Neither the name of the University nor the names of its contributors
15.\" may be used to endorse or promote products derived from this software
16.\" without specific prior written permission.
17.\"
18.\" THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
19.\" ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
20.\" IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
21.\" ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
22.\" FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
23.\" DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
24.\" OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
25.\" HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
26.\" LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
27.\" OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
28.\" SUCH DAMAGE.
29.\"
30.Dd $Mdocdate: August 30 2019 $
31.Dt GETNETENT 3
32.Os
33.Sh NAME
34.Nm getnetent ,
35.Nm getnetbyaddr ,
36.Nm getnetbyname ,
37.Nm setnetent ,
38.Nm endnetent
39.Nd get network entry
40.Sh SYNOPSIS
41.In netdb.h
42.Ft struct netent *
43.Fn getnetent "void"
44.Ft struct netent *
45.Fn getnetbyname "const char *name"
46.Ft struct netent *
47.Fn getnetbyaddr "in_addr_t net" "int type"
48.Ft void
49.Fn setnetent "int stayopen"
50.Ft void
51.Fn endnetent "void"
52.Sh DESCRIPTION
53The
54.Fn getnetbyname
55and
56.Fn getnetbyaddr
57functions return a pointer to an object with the following structure:
58.Bd -literal -offset indent
59struct netent {
60 char *n_name; /* official name of net */
61 char **n_aliases; /* alias list */
62 int n_addrtype; /* net number type */
63 in_addr_t n_net; /* net number */
64};
65.Ed
66.Pp
67The members of this structure are:
68.Bl -tag -width n_addrtype
69.It Fa n_name
70The official name of the network.
71.It Fa n_aliases
72A null-terminated list of alternate names for the network.
73.It Fa n_addrtype
74The type of the network number returned; it is always
75.Dv AF_INET .
76.It Fa n_net
77The network number.
78Network numbers are returned in machine byte order.
79.El
80.Pp
81On
82.Ox ,
83these legacy functions perform a lookup in a similar fashion as
84.Xr gethostbyname 3
85and
86.Xr gethostbyaddr 3 ,
87respectively.
88On other systems, they may use a separate network database file,
89.Pa /etc/networks .
90.Pp
91In contrast to
92.Xr gethostbyaddr 3 ,
93the
94.Fa net
95argument is expected in machine byte order.
96.Pp
97The
98.Fn setnetent ,
99.Fn getnetent ,
100and
101.Fn endnetent
102functions are deprecated and no longer have any effect.
103They could be used in the past to iterate over entries in the former file
104.Pa /etc/networks .
105.Sh RETURN VALUES
106The
107.Fn getnetbyaddr
108and
109.Fn getnetbyname
110functions return
111.Dv NULL
112if the requested entry is not found.
113.Pp
114The
115.Fn getnetent
116function always returns
117.Dv NULL .
118.Sh FILES
119.Bl -tag -width /etc/hosts -compact
120.It Pa /etc/hosts
121The local host and network name database.
122.El
123.Sh SEE ALSO
124.Xr getaddrinfo 3 ,
125.Xr gethostbyname 3 ,
126.Xr getnameinfo 3 ,
127.Xr res_init 3 ,
128.Xr hosts 5
129.Sh STANDARDS
130These functions conform to
131.St -p1003.1-2008 .
132.Sh HISTORY
133The
134.Fn getnetent ,
135.Fn getnetbyaddr ,
136.Fn getnetbyname ,
137.Fn setnetent ,
138and
139.Fn endnetent
140functions appeared in
141.Bx 4.2 .
142.Sh BUGS
143The data space used by these functions is static; if future use
144requires the data, it should be copied before any subsequent calls
145to these functions overwrite it.
146Only Internet network numbers are currently understood.
147Expecting network numbers to fit in no more than 32 bits is naive.
diff --git a/src/lib/libc/net/getnetent.c b/src/lib/libc/net/getnetent.c
deleted file mode 100644
index b93e442fa9..0000000000
--- a/src/lib/libc/net/getnetent.c
+++ /dev/null
@@ -1,49 +0,0 @@
1/* $OpenBSD: getnetent.c,v 1.18 2018/04/28 15:05:40 schwarze Exp $ */
2/*
3 * Copyright (c) 1983, 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. Neither the name of the University nor the names of its contributors
15 * may be used to endorse or promote products derived from this software
16 * without specific prior written permission.
17 *
18 * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
19 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
20 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
21 * ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
22 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
23 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
24 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
25 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
26 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
27 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
28 * SUCH DAMAGE.
29 */
30
31#include <netdb.h>
32#include <stddef.h>
33
34void
35setnetent(int f)
36{
37}
38
39void
40endnetent(void)
41{
42}
43
44struct netent *
45getnetent(void)
46{
47 h_errno = NETDB_INTERNAL;
48 return NULL;
49}
diff --git a/src/lib/libc/net/getpeereid.3 b/src/lib/libc/net/getpeereid.3
deleted file mode 100644
index 4475bbd77d..0000000000
--- a/src/lib/libc/net/getpeereid.3
+++ /dev/null
@@ -1,121 +0,0 @@
1.\" $OpenBSD: getpeereid.3,v 1.4 2022/09/11 06:38:10 jmc 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. Neither the name of the University nor the names of its contributors
15.\" may be used to endorse or promote products derived from this software
16.\" without specific prior written permission.
17.\"
18.\" THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
19.\" ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
20.\" IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
21.\" ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
22.\" FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
23.\" DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
24.\" OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
25.\" HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
26.\" LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
27.\" OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
28.\" SUCH DAMAGE.
29.Dd $Mdocdate: September 11 2022 $
30.Dt GETPEEREID 3
31.Os
32.Sh NAME
33.Nm getpeereid
34.Nd get effective user and group identification of locally-connected peer
35.Sh SYNOPSIS
36.In sys/types.h
37.In sys/socket.h
38.Ft int
39.Fn getpeereid "int s" "uid_t *euid" "gid_t *egid"
40.Sh DESCRIPTION
41.Fn getpeereid
42returns the effective user ID and group ID of the peer connected to
43a
44.Ux Ns -domain
45socket (see
46.Xr unix 4 ) .
47The argument
48.Fa s
49must be of type
50.Dv SOCK_STREAM
51or
52.Dv SOCK_SEQPACKET .
53.Pp
54One common use is for
55.Ux Ns -domain
56servers to determine the credentials of clients that have connected to it.
57.Pp
58.Fn getpeereid
59takes three parameters:
60.Bl -bullet
61.It
62.Fa s
63contains the file descriptor of the socket whose peer credentials
64should be looked up.
65.It
66.Fa euid
67points to a
68.Vt uid_t
69variable into which the effective user ID for the connected peer will
70be stored.
71.It
72.Fa egid
73points to a
74.Vt gid_t
75variable into which the effective group ID for the connected peer will
76be stored.
77.El
78.Sh RETURN VALUES
79If the call succeeds, a 0 is returned and
80.Fa euid
81and
82.Fa egid
83are set to the effective user ID and group ID of the connected peer.
84Otherwise,
85.Va errno
86is set and a value of \-1 is returned.
87.Sh ERRORS
88On failure,
89.Va errno
90is set to one of the following:
91.Bl -tag -width Er
92.It Bq Er EBADF
93The argument
94.Fa s
95is not a valid descriptor.
96.It Bq Er ENOTSOCK
97The argument
98.Fa s
99is a file, not a socket.
100.It Bq Er EOPNOTSUPP
101The socket is not in the
102.Ux Ns -domain .
103.It Bq Er ENOTCONN
104The socket is not connected.
105.It Bq Er ENOBUFS
106Insufficient resources were available in the system
107to perform the operation.
108.El
109.Sh SEE ALSO
110.Xr accept 2 ,
111.Xr bind 2 ,
112.Xr getpeername 2 ,
113.Xr getsockname 2 ,
114.Xr getsockopt 2 ,
115.Xr socket 2 ,
116.Xr unix 4
117.Sh HISTORY
118The
119.Fn getpeereid
120function call appeared in
121.Ox 3.0 .
diff --git a/src/lib/libc/net/getpeereid.c b/src/lib/libc/net/getpeereid.c
deleted file mode 100644
index 208e541f17..0000000000
--- a/src/lib/libc/net/getpeereid.c
+++ /dev/null
@@ -1,36 +0,0 @@
1/* $OpenBSD: getpeereid.c,v 1.1 2010/07/01 19:15:30 deraadt Exp $ */
2
3/*
4 * Copyright (c) 2010 Theo de Raadt <deraadt@openbsd.org>
5 *
6 * Permission to use, copy, modify, and distribute this software for any
7 * purpose with or without fee is hereby granted, provided that the above
8 * copyright notice and this permission notice appear in all copies.
9 *
10 * THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES
11 * WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF
12 * MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR
13 * ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES
14 * WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN
15 * ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF
16 * OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE.
17 */
18
19#include <sys/types.h>
20#include <sys/socket.h>
21
22int
23getpeereid(int s, uid_t *euid, gid_t *egid)
24{
25 struct sockpeercred creds;
26 socklen_t credslen = sizeof(creds);
27 int error;
28
29 error = getsockopt(s, SOL_SOCKET, SO_PEERCRED,
30 &creds, &credslen);
31 if (error)
32 return (error);
33 *euid = creds.uid;
34 *egid = creds.gid;
35 return (0);
36}
diff --git a/src/lib/libc/net/getproto.c b/src/lib/libc/net/getproto.c
deleted file mode 100644
index 8e080679d0..0000000000
--- a/src/lib/libc/net/getproto.c
+++ /dev/null
@@ -1,60 +0,0 @@
1/* $OpenBSD: getproto.c,v 1.8 2015/09/14 07:38:38 guenther Exp $ */
2/*
3 * Copyright (c) 1983, 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. Neither the name of the University nor the names of its contributors
15 * may be used to endorse or promote products derived from this software
16 * without specific prior written permission.
17 *
18 * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
19 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
20 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
21 * ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
22 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
23 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
24 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
25 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
26 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
27 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
28 * SUCH DAMAGE.
29 */
30
31#include <netdb.h>
32#include <stdio.h>
33
34int
35getprotobynumber_r(int num, struct protoent *pe, struct protoent_data *pd)
36{
37 int error;
38
39 setprotoent_r(pd->stayopen, pd);
40 while ((error = getprotoent_r(pe, pd)) == 0)
41 if (pe->p_proto == num)
42 break;
43 if (!pd->stayopen && pd->fp != NULL) {
44 (void)fclose(pd->fp);
45 pd->fp = NULL;
46 }
47 return (error);
48}
49DEF_WEAK(getprotobynumber_r);
50
51struct protoent *
52getprotobynumber(int num)
53{
54 extern struct protoent_data _protoent_data;
55 static struct protoent proto;
56
57 if (getprotobynumber_r(num, &proto, &_protoent_data) != 0)
58 return (NULL);
59 return (&proto);
60}
diff --git a/src/lib/libc/net/getprotoent.3 b/src/lib/libc/net/getprotoent.3
deleted file mode 100644
index cc2c69836a..0000000000
--- a/src/lib/libc/net/getprotoent.3
+++ /dev/null
@@ -1,213 +0,0 @@
1.\" $OpenBSD: getprotoent.3,v 1.18 2013/06/05 03:39:23 tedu 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. Neither the name of the University nor the names of its contributors
15.\" may be used to endorse or promote products derived from this software
16.\" without specific prior written permission.
17.\"
18.\" THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
19.\" ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
20.\" IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
21.\" ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
22.\" FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
23.\" DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
24.\" OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
25.\" HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
26.\" LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
27.\" OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
28.\" SUCH DAMAGE.
29.\"
30.Dd $Mdocdate: June 5 2013 $
31.Dt GETPROTOENT 3
32.Os
33.Sh NAME
34.Nm getprotoent ,
35.Nm getprotoent_r ,
36.Nm getprotobynumber ,
37.Nm getprotobynumber_r ,
38.Nm getprotobyname ,
39.Nm getprotobyname_r ,
40.Nm setprotoent ,
41.Nm setprotoent_r ,
42.Nm endprotoent ,
43.Nm endprotoent_r
44.Nd get protocol entry
45.Sh SYNOPSIS
46.In netdb.h
47.Ft struct protoent *
48.Fn getprotoent "void"
49.Ft int
50.Fn getprotoent_r "struct protoent *protoent" "struct protoent_data *protoent_data"
51.Ft struct protoent *
52.Fn getprotobyname "const char *name"
53.Ft int
54.Fn getprotobyname_r "const char *name" "struct protoent *protoent" "struct protoent_data *protoent_data"
55.Ft struct protoent *
56.Fn getprotobynumber "int proto"
57.Ft int
58.Fn getprotobynumber_r "int proto" "struct protoent *protoent" "struct protoent_data *protoent_data"
59.Ft void
60.Fn setprotoent "int stayopen"
61.Ft void
62.Fn setprotoent_r "int stayopen" "struct protoent_data *protoent_data"
63.Ft void
64.Fn endprotoent "void"
65.Ft void
66.Fn endprotoent_r "struct protoent_data *protoent_data"
67.Sh DESCRIPTION
68The
69.Fn getprotoent ,
70.Fn getprotobyname ,
71and
72.Fn getprotobynumber
73functions each return a pointer to an object with the following structure
74containing the broken-out fields of a line in the network protocol database,
75.Pa /etc/protocols .
76.Bd -literal -offset indent
77.Pp
78struct protoent {
79 char *p_name; /* official name of protocol */
80 char **p_aliases; /* alias list */
81 int p_proto; /* protocol number */
82};
83.Ed
84.Pp
85The members of this structure are:
86.Bl -tag -width p_aliases
87.It Fa p_name
88The official name of the protocol.
89.It Fa p_aliases
90A null-terminated list of alternate names for the protocol.
91.It Fa p_proto
92The protocol number.
93.El
94.Pp
95The
96.Fn getprotoent
97function reads the next line of the file, opening the file if necessary.
98.Pp
99The
100.Fn setprotoent
101function opens and rewinds the file.
102If the
103.Fa stayopen
104flag is non-zero,
105the protocol database will not be closed after each call to
106.Fn getprotobyname
107or
108.Fn getprotobynumber .
109.Pp
110The
111.Fn endprotoent
112function closes the file.
113.Pp
114The
115.Fn getprotobyname
116and
117.Fn getprotobynumber
118functions sequentially search from the beginning of the file until a
119matching protocol name or protocol number is found, or until
120.Dv EOF
121is encountered.
122.Pp
123The
124.Fn getprotoent_r ,
125.Fn getprotobyport_r ,
126.Fn getprotobyname_r ,
127.Fn setprotoent_r ,
128and
129.Fn endprotoent_r
130functions are reentrant versions of the above functions that take a
131pointer to a
132.Vt protoent_data
133structure which is used to store state information.
134The structure must be zero-filled before it is used
135and should be considered opaque for the sake of portability.
136.Pp
137The
138.Fn getprotoent_r ,
139.Fn getprotobyport_r ,
140and
141.Fn getprotobyname_r
142functions
143also take a pointer to a
144.Vt protoent
145structure which is used to store the results of the database lookup.
146.Sh RETURN VALUES
147The
148.Fn getprotoent ,
149.Fn getprotobyport ,
150and
151.Fn getprotobyname
152functions return a pointer to a
153.Vt protoent
154structure on success or a null pointer if end-of-file
155is reached or an error occurs.
156.Pp
157The
158.Fn getprotoent_r ,
159.Fn getprotobyport_r ,
160and
161.Fn getprotobyname_r
162functions return 0 on success or \-1 if end-of-file
163is reached or an error occurs.
164.Sh FILES
165.Bl -tag -width /etc/protocols -compact
166.It Pa /etc/protocols
167.El
168.Sh SEE ALSO
169.Xr protocols 5
170.Sh STANDARDS
171The
172.Fn getprotoent ,
173.Fn getprotobynumber ,
174.Fn getprotobyname ,
175.Fn setprotoent ,
176and
177.Fn endprotoent
178functions conform to
179.St -p1003.1-2004 .
180.Pp
181The
182.Fn getprotoent_r ,
183.Fn getprotobyport_r ,
184.Fn getprotobyname_r ,
185.Fn setprotoent_r ,
186and
187.Fn endprotoent_r
188functions are not currently standardized.
189This implementation follows the API used by HP, IBM, and Digital.
190.Sh HISTORY
191The
192.Fn getprotoent ,
193.Fn getprotobynumber ,
194.Fn getprotobyname ,
195.Fn setprotoent ,
196and
197.Fn endprotoent
198functions appeared in
199.Bx 4.2 .
200.Pp
201The
202.Fn getprotoent_r ,
203.Fn getprotobyport_r ,
204.Fn getprotobyname_r ,
205.Fn setprotoent_r ,
206and
207.Fn endprotoent_r
208functions appeared in
209.Ox 3.7 .
210.Sh BUGS
211The non-reentrant functions use a static data space; if the data is needed
212for future use, it should be copied before any subsequent calls overwrite it.
213Only the Internet protocols are currently understood.
diff --git a/src/lib/libc/net/getprotoent.c b/src/lib/libc/net/getprotoent.c
deleted file mode 100644
index a218863d07..0000000000
--- a/src/lib/libc/net/getprotoent.c
+++ /dev/null
@@ -1,169 +0,0 @@
1/* $OpenBSD: getprotoent.c,v 1.13 2015/09/14 07:38:38 guenther Exp $ */
2/*
3 * Copyright (c) 1983, 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. Neither the name of the University nor the names of its contributors
15 * may be used to endorse or promote products derived from this software
16 * without specific prior written permission.
17 *
18 * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
19 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
20 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
21 * ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
22 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
23 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
24 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
25 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
26 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
27 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
28 * SUCH DAMAGE.
29 */
30
31#include <sys/types.h>
32#include <sys/socket.h>
33
34#include <errno.h>
35#include <limits.h>
36#include <netdb.h>
37#include <stdio.h>
38#include <stdlib.h>
39#include <string.h>
40
41void
42setprotoent_r(int f, struct protoent_data *pd)
43{
44 if (pd->fp == NULL)
45 pd->fp = fopen(_PATH_PROTOCOLS, "re" );
46 else
47 rewind(pd->fp);
48 pd->stayopen |= f;
49}
50DEF_WEAK(setprotoent_r);
51
52void
53endprotoent_r(struct protoent_data *pd)
54{
55 if (pd->fp) {
56 fclose(pd->fp);
57 pd->fp = NULL;
58 }
59 free(pd->aliases);
60 pd->aliases = NULL;
61 pd->maxaliases = 0;
62 free(pd->line);
63 pd->line = NULL;
64 pd->stayopen = 0;
65}
66DEF_WEAK(endprotoent_r);
67
68int
69getprotoent_r(struct protoent *pe, struct protoent_data *pd)
70{
71 char *p, *cp, **q, *endp;
72 size_t len;
73 long l;
74 int serrno;
75
76 if (pd->fp == NULL && (pd->fp = fopen(_PATH_PROTOCOLS, "re" )) == NULL)
77 return (-1);
78again:
79 if ((p = fgetln(pd->fp, &len)) == NULL)
80 return (-1);
81 if (len == 0 || *p == '#' || *p == '\n')
82 goto again;
83 if (p[len-1] == '\n')
84 len--;
85 if ((cp = memchr(p, '#', len)) != NULL)
86 len = cp - p;
87 cp = realloc(pd->line, len + 1);
88 if (cp == NULL)
89 return (-1);
90 pd->line = pe->p_name = memcpy(cp, p, len);
91 cp[len] = '\0';
92 cp = strpbrk(cp, " \t");
93 if (cp == NULL)
94 goto again;
95 *cp++ = '\0';
96 while (*cp == ' ' || *cp == '\t')
97 cp++;
98 p = strpbrk(cp, " \t");
99 if (p != NULL)
100 *p++ = '\0';
101 l = strtol(cp, &endp, 10);
102 if (endp == cp || *endp != '\0' || l < 0 || l >= INT_MAX)
103 goto again;
104 pe->p_proto = l;
105 if (pd->aliases == NULL) {
106 pd->maxaliases = 5;
107 pd->aliases = calloc(pd->maxaliases, sizeof(char *));
108 if (pd->aliases == NULL) {
109 serrno = errno;
110 endprotoent_r(pd);
111 errno = serrno;
112 return (-1);
113 }
114 }
115 q = pe->p_aliases = pd->aliases;
116 if (p != NULL) {
117 cp = p;
118 while (cp && *cp) {
119 if (*cp == ' ' || *cp == '\t') {
120 cp++;
121 continue;
122 }
123 if (q == &pe->p_aliases[pd->maxaliases - 1]) {
124 p = reallocarray(pe->p_aliases,
125 pd->maxaliases, 2 * sizeof(char *));
126 if (p == NULL) {
127 serrno = errno;
128 endprotoent_r(pd);
129 errno = serrno;
130 return (-1);
131 }
132 pd->maxaliases *= 2;
133 q = (char **)p + (q - pe->p_aliases);
134 pe->p_aliases = pd->aliases = (char **)p;
135 }
136 *q++ = cp;
137 cp = strpbrk(cp, " \t");
138 if (cp != NULL)
139 *cp++ = '\0';
140 }
141 }
142 *q = NULL;
143 return (0);
144}
145DEF_WEAK(getprotoent_r);
146
147struct protoent_data _protoent_data; /* shared with getproto{,name}.c */
148
149void
150setprotoent(int f)
151{
152 setprotoent_r(f, &_protoent_data);
153}
154
155void
156endprotoent(void)
157{
158 endprotoent_r(&_protoent_data);
159}
160
161struct protoent *
162getprotoent(void)
163{
164 static struct protoent proto;
165
166 if (getprotoent_r(&proto, &_protoent_data) != 0)
167 return (NULL);
168 return (&proto);
169}
diff --git a/src/lib/libc/net/getprotoname.c b/src/lib/libc/net/getprotoname.c
deleted file mode 100644
index f6ed4c49b1..0000000000
--- a/src/lib/libc/net/getprotoname.c
+++ /dev/null
@@ -1,69 +0,0 @@
1/* $OpenBSD: getprotoname.c,v 1.8 2015/09/14 07:38:38 guenther Exp $ */
2/*
3 * Copyright (c) 1983, 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. Neither the name of the University nor the names of its contributors
15 * may be used to endorse or promote products derived from this software
16 * without specific prior written permission.
17 *
18 * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
19 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
20 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
21 * ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
22 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
23 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
24 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
25 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
26 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
27 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
28 * SUCH DAMAGE.
29 */
30
31#include <netdb.h>
32#include <stdio.h>
33#include <string.h>
34
35int
36getprotobyname_r(const char *name, struct protoent *pe,
37 struct protoent_data *pd)
38{
39 char **cp;
40 int error;
41
42 setprotoent_r(pd->stayopen, pd);
43 while ((error = getprotoent_r(pe, pd)) == 0) {
44 if (strcmp(pe->p_name, name) == 0)
45 break;
46 for (cp = pe->p_aliases; *cp != 0; cp++)
47 if (strcmp(*cp, name) == 0)
48 goto found;
49 }
50found:
51 if (!pd->stayopen && pd->fp != NULL) {
52 fclose(pd->fp);
53 pd->fp = NULL;
54 }
55 return (error);
56}
57DEF_WEAK(getprotobyname_r);
58
59struct protoent *
60getprotobyname(const char *name)
61{
62 extern struct protoent_data _protoent_data;
63 static struct protoent proto;
64
65 if (getprotobyname_r(name, &proto, &_protoent_data) != 0)
66 return (NULL);
67 return (&proto);
68}
69DEF_WEAK(getprotobyname);
diff --git a/src/lib/libc/net/getrrsetbyname.3 b/src/lib/libc/net/getrrsetbyname.3
deleted file mode 100644
index 314f31665c..0000000000
--- a/src/lib/libc/net/getrrsetbyname.3
+++ /dev/null
@@ -1,164 +0,0 @@
1.\" $OpenBSD: getrrsetbyname.3,v 1.22 2022/09/11 06:38:10 jmc Exp $
2.\"
3.\" Copyright (C) 2000, 2001 Internet Software Consortium.
4.\"
5.\" Permission to use, copy, modify, and distribute this software for any
6.\" purpose with or without fee is hereby granted, provided that the above
7.\" copyright notice and this permission notice appear in all copies.
8.\"
9.\" THE SOFTWARE IS PROVIDED "AS IS" AND INTERNET SOFTWARE CONSORTIUM
10.\" DISCLAIMS ALL WARRANTIES WITH REGARD TO THIS SOFTWARE INCLUDING ALL
11.\" IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL
12.\" INTERNET SOFTWARE CONSORTIUM BE LIABLE FOR ANY SPECIAL, DIRECT,
13.\" INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES WHATSOEVER RESULTING
14.\" FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN ACTION OF CONTRACT,
15.\" NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF OR IN CONNECTION
16.\" WITH THE USE OR PERFORMANCE OF THIS SOFTWARE.
17.\"
18.Dd $Mdocdate: September 11 2022 $
19.Dt GETRRSETBYNAME 3
20.Os
21.Sh NAME
22.Nm freerrset ,
23.Nm getrrsetbyname
24.Nd retrieve DNS records
25.Sh SYNOPSIS
26.In netdb.h
27.Ft int
28.Fn getrrsetbyname "const char *hostname" "unsigned int rdclass" \
29"unsigned int rdtype" "unsigned int flags" "struct rrsetinfo **res"
30.Ft void
31.Fn freerrset "struct rrsetinfo *rrset"
32.Sh DESCRIPTION
33.Fn getrrsetbyname
34gets a set of resource records associated with a
35.Fa hostname ,
36.Fa rdclass ,
37and
38.Fa rdtype .
39.Fa hostname
40is a pointer to a NUL-terminated string.
41The
42.Fa flags
43field is currently unused and must be zero.
44.Pp
45After a successful call to
46.Fn getrrsetbyname ,
47.Fa *res
48is a pointer to an
49.Vt rrsetinfo
50structure, containing a list of one or more
51.Vt rdatainfo
52structures containing resource records and potentially another list of
53.Vt rdatainfo
54structures containing SIG resource records associated with those records.
55The members
56.Li rri_rdclass
57and
58.Li rri_rdtype
59are copied from the parameters.
60.Li rri_ttl
61and
62.Li rri_name
63are properties of the obtained rrset.
64The resource records contained in
65.Li rri_rdatas
66and
67.Li rri_sigs
68are in uncompressed DNS wire format.
69Properties of the rdataset are represented in the
70.Li rri_flags
71bitfield.
72If the
73.Dv RRSET_VALIDATED
74bit is set, the data has been DNSSEC
75validated and the signatures verified.
76.Pp
77The following structures are used:
78.Bd -literal -offset indent
79struct rdatainfo {
80 unsigned int rdi_length; /* length of data */
81 unsigned char *rdi_data; /* record data */
82};
83
84struct rrsetinfo {
85 unsigned int rri_flags; /* RRSET_VALIDATED ... */
86 unsigned int rri_rdclass; /* class number */
87 unsigned int rri_rdtype; /* RR type number */
88 unsigned int rri_ttl; /* time to live */
89 unsigned int rri_nrdatas; /* size of rdatas array */
90 unsigned int rri_nsigs; /* size of sigs array */
91 char *rri_name; /* canonical name */
92 struct rdatainfo *rri_rdatas; /* individual records */
93 struct rdatainfo *rri_sigs; /* individual signatures */
94};
95.Ed
96.Pp
97All of the information returned by
98.Fn getrrsetbyname
99is dynamically allocated: the
100.Vt rrsetinfo
101and
102.Vt rdatainfo
103structures,
104and the canonical host name strings pointed to by the
105.Vt rrsetinfo
106structure.
107Memory allocated for the dynamically allocated structures created by
108a successful call to
109.Fn getrrsetbyname
110is released by
111.Fn freerrset .
112.Li rrset
113is a pointer to a
114.Vt struct rrsetinfo
115created by a call to
116.Fn getrrsetbyname .
117.\" .Pp
118.\" If the EDNS0 option is activated in
119.\" .Xr resolv.conf 5 ,
120.\" .Fn getrrsetbyname
121.\" will request DNSSEC authentication using the EDNS0 DNSSEC OK (DO) bit.
122.Sh RETURN VALUES
123.Fn getrrsetbyname
124returns zero on success, and one of the following error
125codes if an error occurred:
126.Bl -tag -width ERRSET_NOMEMORY
127.It Bq Er ERRSET_NONAME
128The name does not exist.
129.It Bq Er ERRSET_NODATA
130The name exists, but does not have data of the desired type.
131.It Bq Er ERRSET_NOMEMORY
132Memory could not be allocated.
133.It Bq Er ERRSET_INVAL
134A parameter is invalid.
135.It Bq Er ERRSET_FAIL
136Other failure.
137.El
138.Sh SEE ALSO
139.Xr res_init 3 ,
140.Xr resolv.conf 5
141.Sh HISTORY
142.Fn getrrsetbyname
143first appeared in
144.Ox 3.0 .
145The API first appeared in ISC BIND version 9.
146.Sh AUTHORS
147.An Jakob Schlyter Aq Mt jakob@openbsd.org
148.Sh CAVEATS
149The
150.Dv RRSET_VALIDATED
151flag in
152.Li rri_flags
153is set if the AD (authenticated data) bit in the DNS answer is
154set.
155This flag
156.Em should not
157be trusted unless the transport between the nameserver and the resolver
158is secure (e.g. IPsec, trusted network, loopback communication).
159.Sh BUGS
160The data in
161.Li *rdi_data
162should be returned in uncompressed wire format.
163Currently, the data is in compressed format and the caller can't
164uncompress since it doesn't have the full message.
diff --git a/src/lib/libc/net/getservbyname.c b/src/lib/libc/net/getservbyname.c
deleted file mode 100644
index 80c7e62a41..0000000000
--- a/src/lib/libc/net/getservbyname.c
+++ /dev/null
@@ -1,72 +0,0 @@
1/* $OpenBSD: getservbyname.c,v 1.11 2015/09/14 07:38:38 guenther Exp $ */
2/*
3 * Copyright (c) 1983, 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. Neither the name of the University nor the names of its contributors
15 * may be used to endorse or promote products derived from this software
16 * without specific prior written permission.
17 *
18 * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
19 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
20 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
21 * ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
22 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
23 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
24 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
25 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
26 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
27 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
28 * SUCH DAMAGE.
29 */
30
31#include <netdb.h>
32#include <stdio.h>
33#include <string.h>
34
35int
36getservbyname_r(const char *name, const char *proto, struct servent *se,
37 struct servent_data *sd)
38{
39 char **cp;
40 int error;
41
42 setservent_r(sd->stayopen, sd);
43 while ((error = getservent_r(se, sd)) == 0) {
44 if (strcmp(name, se->s_name) == 0)
45 goto gotname;
46 for (cp = se->s_aliases; *cp; cp++)
47 if (strcmp(name, *cp) == 0)
48 goto gotname;
49 continue;
50gotname:
51 if (proto == 0 || strcmp(se->s_proto, proto) == 0)
52 break;
53 }
54 if (!sd->stayopen && sd->fp != NULL) {
55 fclose(sd->fp);
56 sd->fp = NULL;
57 }
58 return (error);
59}
60DEF_WEAK(getservbyname_r);
61
62struct servent *
63getservbyname(const char *name, const char *proto)
64{
65 extern struct servent_data _servent_data;
66 static struct servent serv;
67
68 if (getservbyname_r(name, proto, &serv, &_servent_data) != 0)
69 return (NULL);
70 return (&serv);
71}
72DEF_WEAK(getservbyname);
diff --git a/src/lib/libc/net/getservbyport.c b/src/lib/libc/net/getservbyport.c
deleted file mode 100644
index 3584fbea3a..0000000000
--- a/src/lib/libc/net/getservbyport.c
+++ /dev/null
@@ -1,65 +0,0 @@
1/* $OpenBSD: getservbyport.c,v 1.8 2015/09/14 07:38:38 guenther Exp $ */
2/*
3 * Copyright (c) 1983, 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. Neither the name of the University nor the names of its contributors
15 * may be used to endorse or promote products derived from this software
16 * without specific prior written permission.
17 *
18 * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
19 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
20 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
21 * ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
22 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
23 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
24 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
25 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
26 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
27 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
28 * SUCH DAMAGE.
29 */
30
31#include <netdb.h>
32#include <stdio.h>
33#include <string.h>
34
35int
36getservbyport_r(int port, const char *proto, struct servent *se,
37 struct servent_data *sd)
38{
39 int error;
40
41 setservent_r(sd->stayopen, sd);
42 while ((error = getservent_r(se, sd)) == 0) {
43 if (se->s_port != port)
44 continue;
45 if (proto == 0 || strcmp(se->s_proto, proto) == 0)
46 break;
47 }
48 if (!sd->stayopen && sd->fp != NULL) {
49 fclose(sd->fp);
50 sd->fp = NULL;
51 }
52 return (error);
53}
54DEF_WEAK(getservbyport_r);
55
56struct servent *
57getservbyport(int port, const char *proto)
58{
59 extern struct servent_data _servent_data;
60 static struct servent serv;
61
62 if (getservbyport_r(port, proto, &serv, &_servent_data) != 0)
63 return (NULL);
64 return (&serv);
65}
diff --git a/src/lib/libc/net/getservent.3 b/src/lib/libc/net/getservent.3
deleted file mode 100644
index 29dd3eb5f4..0000000000
--- a/src/lib/libc/net/getservent.3
+++ /dev/null
@@ -1,220 +0,0 @@
1.\" $OpenBSD: getservent.3,v 1.21 2013/06/05 03:39:23 tedu 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. Neither the name of the University nor the names of its contributors
15.\" may be used to endorse or promote products derived from this software
16.\" without specific prior written permission.
17.\"
18.\" THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
19.\" ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
20.\" IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
21.\" ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
22.\" FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
23.\" DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
24.\" OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
25.\" HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
26.\" LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
27.\" OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
28.\" SUCH DAMAGE.
29.\"
30.Dd $Mdocdate: June 5 2013 $
31.Dt GETSERVENT 3
32.Os
33.Sh NAME
34.Nm getservent ,
35.Nm getservent_r ,
36.Nm getservbyport ,
37.Nm getservbyport_r ,
38.Nm getservbyname ,
39.Nm getservbyname_r ,
40.Nm setservent ,
41.Nm setservent_r ,
42.Nm endservent ,
43.Nm endservent_r
44.Nd get service entry
45.Sh SYNOPSIS
46.In netdb.h
47.Ft struct servent *
48.Fn getservent "void"
49.Ft int
50.Fn getservent_r "struct servent *servent" "struct servent_data *servent_data"
51.Ft struct servent *
52.Fn getservbyname "const char *name" "const char *proto"
53.Ft int
54.Fn getservbyname_r "const char *name" "const char *proto" "struct servent *servent" "struct servent_data *servent_data"
55.Ft struct servent *
56.Fn getservbyport "int port" "const char *proto"
57.Ft int
58.Fn getservbyport_r "int port" "const char *proto" "struct servent *servent" "struct servent_data *servent_data"
59.Ft void
60.Fn setservent "int stayopen"
61.Ft void
62.Fn setservent_r "int stayopen" "struct servent_data *servent_data"
63.Ft void
64.Fn endservent "void"
65.Ft void
66.Fn endservent_r "struct servent_data *servent_data"
67.Sh DESCRIPTION
68The
69.Fn getservent ,
70.Fn getservbyname ,
71and
72.Fn getservbyport
73functions each return a pointer to an object with the following structure
74containing the broken-out fields of a line in the network services database,
75.Pa /etc/services .
76.Bd -literal -offset indent
77struct servent {
78 char *s_name; /* official name of service */
79 char **s_aliases; /* alias list */
80 int s_port; /* port service resides at */
81 char *s_proto; /* protocol to use */
82};
83.Ed
84.Pp
85The members of this structure are:
86.Bl -tag -width s_aliases
87.It Fa s_name
88The official name of the service.
89.It Fa s_aliases
90A null-terminated list of alternate names for the service.
91.It Fa s_port
92The port number at which the service resides.
93Port numbers are returned in network byte order.
94.It Fa s_proto
95The name of the protocol to use when contacting the service.
96.El
97.Pp
98The
99.Fn getservent
100function reads the next line of the file, opening the file if necessary.
101.Pp
102The
103.Fn setservent
104function opens and rewinds the file.
105If the
106.Fa stayopen
107flag is non-zero,
108the services database will not be closed after each call to
109.Fn getservbyname
110or
111.Fn getservbyport .
112.Pp
113The
114.Fn endservent
115function closes the file.
116.Pp
117The
118.Fn getservbyname
119and
120.Fn getservbyport
121functions sequentially search from the beginning of the file until a
122matching protocol name or port number (specified in network byte order)
123is found, or until
124.Dv EOF
125is encountered.
126If a protocol name is also supplied (non-null),
127searches must also match the protocol.
128.Pp
129The
130.Fn getservent_r ,
131.Fn getservbyport_r ,
132.Fn getservbyname_r ,
133.Fn setservent_r ,
134and
135.Fn endservent_r
136functions are reentrant versions of the above functions that take a
137pointer to a
138.Fa servent_data
139structure which is used to store state information.
140The structure must be zero-filled before it is used
141and should be considered opaque for the sake of portability.
142.Pp
143The
144.Fn getservent_r ,
145.Fn getservbyport_r ,
146and
147.Fn getservbyname_r
148functions
149also take a pointer to a
150.Fa servent
151structure which is used to store the results of the database lookup.
152.Sh RETURN VALUES
153The
154.Fn getservent ,
155.Fn getservbyport ,
156and
157.Fn getservbyname
158functions return a pointer to a
159.Fa servent
160structure on success or a null pointer if end-of-file
161is reached or an error occurs.
162.Pp
163The
164.Fn getservent_r ,
165.Fn getservbyport_r ,
166and
167.Fn getservbyname_r
168functions return 0 on success or \-1 if end-of-file
169is reached or an error occurs.
170.Sh FILES
171.Bl -tag -width /etc/services -compact
172.It Pa /etc/services
173.El
174.Sh SEE ALSO
175.Xr getprotoent 3 ,
176.Xr services 5
177.Sh STANDARDS
178The
179.Fn getservent ,
180.Fn getservbynumber ,
181.Fn getservbyname ,
182.Fn setservent ,
183and
184.Fn endservent
185functions conform to
186.St -p1003.1-2004 .
187.Pp
188The
189.Fn getservent_r ,
190.Fn getservbyport_r ,
191.Fn getservbyname_r ,
192.Fn setservent_r ,
193and
194.Fn endservent_r
195functions are not currently standardized.
196This implementation follows the API used by HP, IBM, and Digital.
197.Sh HISTORY
198The
199.Fn getservent ,
200.Fn getservbyport ,
201.Fn getservbyname ,
202.Fn setservent ,
203and
204.Fn endservent
205functions appeared in
206.Bx 4.2 .
207.Pp
208The
209.Fn getservent_r ,
210.Fn getservbyport_r ,
211.Fn getservbyname_r ,
212.Fn setservent_r ,
213and
214.Fn endservent_r
215functions appeared in
216.Ox 3.7 .
217.Sh BUGS
218The non-reentrant functions use static data storage; if the data is needed
219for future use, it should be copied before any subsequent calls overwrite it.
220Expecting port numbers to fit in a 32-bit quantity is probably naive.
diff --git a/src/lib/libc/net/getservent.c b/src/lib/libc/net/getservent.c
deleted file mode 100644
index 220a5851ce..0000000000
--- a/src/lib/libc/net/getservent.c
+++ /dev/null
@@ -1,171 +0,0 @@
1/* $OpenBSD: getservent.c,v 1.15 2015/09/14 07:38:38 guenther Exp $ */
2/*
3 * Copyright (c) 1983, 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. Neither the name of the University nor the names of its contributors
15 * may be used to endorse or promote products derived from this software
16 * without specific prior written permission.
17 *
18 * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
19 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
20 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
21 * ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
22 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
23 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
24 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
25 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
26 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
27 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
28 * SUCH DAMAGE.
29 */
30
31#include <sys/types.h>
32#include <sys/socket.h>
33
34#include <errno.h>
35#include <limits.h>
36#include <netdb.h>
37#include <stdio.h>
38#include <string.h>
39#include <stdlib.h>
40
41void
42setservent_r(int f, struct servent_data *sd)
43{
44 if (sd->fp == NULL)
45 sd->fp = fopen(_PATH_SERVICES, "re" );
46 else
47 rewind(sd->fp);
48 sd->stayopen |= f;
49}
50DEF_WEAK(setservent_r);
51
52void
53endservent_r(struct servent_data *sd)
54{
55 if (sd->fp) {
56 fclose(sd->fp);
57 sd->fp = NULL;
58 }
59 free(sd->aliases);
60 sd->aliases = NULL;
61 sd->maxaliases = 0;
62 free(sd->line);
63 sd->line = NULL;
64 sd->stayopen = 0;
65}
66DEF_WEAK(endservent_r);
67
68int
69getservent_r(struct servent *se, struct servent_data *sd)
70{
71 char *p, *cp, **q, *endp;
72 size_t len;
73 long l;
74 int serrno;
75
76 if (sd->fp == NULL && (sd->fp = fopen(_PATH_SERVICES, "re" )) == NULL)
77 return (-1);
78again:
79 if ((p = fgetln(sd->fp, &len)) == NULL)
80 return (-1);
81 if (len == 0 || *p == '#' || *p == '\n')
82 goto again;
83 if (p[len-1] == '\n')
84 len--;
85 if ((cp = memchr(p, '#', len)) != NULL)
86 len = cp - p;
87 cp = realloc(sd->line, len + 1);
88 if (cp == NULL)
89 return (-1);
90 sd->line = se->s_name = memcpy(cp, p, len);
91 cp[len] = '\0';
92 p = strpbrk(cp, " \t");
93 if (p == NULL)
94 goto again;
95 *p++ = '\0';
96 while (*p == ' ' || *p == '\t')
97 p++;
98 cp = strpbrk(p, ",/");
99 if (cp == NULL)
100 goto again;
101 *cp++ = '\0';
102 l = strtol(p, &endp, 10);
103 if (endp == p || *endp != '\0' || l < 0 || l > USHRT_MAX)
104 goto again;
105 se->s_port = htons((in_port_t)l);
106 se->s_proto = cp;
107 if (sd->aliases == NULL) {
108 sd->maxaliases = 10;
109 sd->aliases = calloc(sd->maxaliases, sizeof(char *));
110 if (sd->aliases == NULL) {
111 serrno = errno;
112 endservent_r(sd);
113 errno = serrno;
114 return (-1);
115 }
116 }
117 q = se->s_aliases = sd->aliases;
118 cp = strpbrk(cp, " \t");
119 if (cp != NULL)
120 *cp++ = '\0';
121 while (cp && *cp) {
122 if (*cp == ' ' || *cp == '\t') {
123 cp++;
124 continue;
125 }
126 if (q == &se->s_aliases[sd->maxaliases - 1]) {
127 p = reallocarray(se->s_aliases, sd->maxaliases,
128 2 * sizeof(char *));
129 if (p == NULL) {
130 serrno = errno;
131 endservent_r(sd);
132 errno = serrno;
133 return (-1);
134 }
135 sd->maxaliases *= 2;
136 q = (char **)p + (q - se->s_aliases);
137 se->s_aliases = sd->aliases = (char **)p;
138 }
139 *q++ = cp;
140 cp = strpbrk(cp, " \t");
141 if (cp != NULL)
142 *cp++ = '\0';
143 }
144 *q = NULL;
145 return (0);
146}
147DEF_WEAK(getservent_r);
148
149struct servent_data _servent_data; /* shared with getservby{name,port}.c */
150
151void
152setservent(int f)
153{
154 setservent_r(f, &_servent_data);
155}
156
157void
158endservent(void)
159{
160 endservent_r(&_servent_data);
161}
162
163struct servent *
164getservent(void)
165{
166 static struct servent serv;
167
168 if (getservent_r(&serv, &_servent_data) != 0)
169 return (NULL);
170 return (&serv);
171}
diff --git a/src/lib/libc/net/herror.c b/src/lib/libc/net/herror.c
deleted file mode 100644
index 356c85fa1d..0000000000
--- a/src/lib/libc/net/herror.c
+++ /dev/null
@@ -1,106 +0,0 @@
1/* $OpenBSD: herror.c,v 1.10 2015/09/14 07:38:38 guenther Exp $ */
2
3/*
4 * ++Copyright++ 1987, 1993
5 * -
6 * Copyright (c) 1987, 1993
7 * The Regents of the University of California. All rights reserved.
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. 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 * Portions Copyright (c) 1993 by Digital Equipment Corporation.
34 *
35 * Permission to use, copy, modify, and distribute this software for any
36 * purpose with or without fee is hereby granted, provided that the above
37 * copyright notice and this permission notice appear in all copies, and that
38 * the name of Digital Equipment Corporation not be used in advertising or
39 * publicity pertaining to distribution of the document or software without
40 * specific, written prior permission.
41 *
42 * THE SOFTWARE IS PROVIDED "AS IS" AND DIGITAL EQUIPMENT CORP. DISCLAIMS ALL
43 * WARRANTIES WITH REGARD TO THIS SOFTWARE, INCLUDING ALL IMPLIED WARRANTIES
44 * OF MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL DIGITAL EQUIPMENT
45 * CORPORATION BE LIABLE FOR ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL
46 * DAMAGES OR ANY DAMAGES WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR
47 * PROFITS, WHETHER IN AN ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS
48 * ACTION, ARISING OUT OF OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS
49 * SOFTWARE.
50 * -
51 * --Copyright--
52 */
53
54#include <sys/types.h>
55#include <sys/uio.h>
56#include <netdb.h>
57#include <unistd.h>
58#include <string.h>
59
60const char * const h_errlist[] = {
61 "Resolver Error 0 (no error)",
62 "Unknown host", /* 1 HOST_NOT_FOUND */
63 "Host name lookup failure", /* 2 TRY_AGAIN */
64 "Unknown server error", /* 3 NO_RECOVERY */
65 "No address associated with name", /* 4 NO_ADDRESS */
66};
67const int h_nerr = { sizeof h_errlist / sizeof h_errlist[0] };
68
69extern int h_errno;
70
71/*
72 * herror --
73 * print the error indicated by the h_errno value.
74 */
75void
76herror(const char *s)
77{
78 struct iovec iov[4];
79 struct iovec *v = iov;
80
81 if (s && *s) {
82 v->iov_base = (char *)s;
83 v->iov_len = strlen(s);
84 v++;
85 v->iov_base = ": ";
86 v->iov_len = 2;
87 v++;
88 }
89 v->iov_base = (char *)hstrerror(h_errno);
90 v->iov_len = strlen(v->iov_base);
91 v++;
92 v->iov_base = "\n";
93 v->iov_len = 1;
94 writev(STDERR_FILENO, iov, (v - iov) + 1);
95}
96
97const char *
98hstrerror(int err)
99{
100 if (err < 0)
101 return ("Resolver internal error");
102 else if (err < h_nerr)
103 return (h_errlist[err]);
104 return ("Unknown resolver error");
105}
106DEF_WEAK(hstrerror);
diff --git a/src/lib/libc/net/htobe64.3 b/src/lib/libc/net/htobe64.3
deleted file mode 100644
index e41c9124c4..0000000000
--- a/src/lib/libc/net/htobe64.3
+++ /dev/null
@@ -1,164 +0,0 @@
1.\" $OpenBSD: htobe64.3,v 1.2 2024/08/03 23:06:56 guenther 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. Neither the name of the University nor the names of its contributors
15.\" may be used to endorse or promote products derived from this software
16.\" without specific prior written permission.
17.\"
18.\" THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
19.\" ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
20.\" IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
21.\" ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
22.\" FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
23.\" DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
24.\" OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
25.\" HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
26.\" LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
27.\" OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
28.\" SUCH DAMAGE.
29.\"
30.Dd $Mdocdate: August 3 2024 $
31.Dt HTOBE64 3
32.Os
33.Sh NAME
34.Nm htobe64 ,
35.Nm htobe32 ,
36.Nm htobe16 ,
37.Nm be64toh ,
38.Nm be32toh ,
39.Nm be16toh ,
40.Nm betoh64 ,
41.Nm betoh32 ,
42.Nm betoh16 ,
43.Nm htole64 ,
44.Nm htole32 ,
45.Nm htole16 ,
46.Nm le64toh ,
47.Nm le32toh ,
48.Nm le16toh ,
49.Nm letoh64 ,
50.Nm letoh32 ,
51.Nm letoh16 ,
52.Nm swap64 ,
53.Nm swap32 ,
54.Nm swap16
55.Nd convert values between different byte orderings
56.Sh SYNOPSIS
57.In endian.h
58.Ft uint64_t
59.Fn htobe64 "uint64_t host64"
60.Ft uint32_t
61.Fn htobe32 "uint32_t host32"
62.Ft uint16_t
63.Fn htobe16 "uint16_t host16"
64.Ft uint64_t
65.Fn be64toh "uint64_t big64"
66.Ft uint32_t
67.Fn be32toh "uint32_t big32"
68.Ft uint16_t
69.Fn be16toh "uint16_t big16"
70.Ft uint64_t
71.Fn betoh64 "uint64_t big64"
72.Ft uint32_t
73.Fn betoh32 "uint32_t big32"
74.Ft uint16_t
75.Fn betoh16 "uint16_t big16"
76.Ft uint64_t
77.Fn htole64 "uint64_t host64"
78.Ft uint32_t
79.Fn htole32 "uint32_t host32"
80.Ft uint16_t
81.Fn htole16 "uint16_t host16"
82.Ft uint64_t
83.Fn letoh64 "uint64_t little64"
84.Ft uint64_t
85.Fn le64toh "uint64_t little64"
86.Ft uint32_t
87.Fn le32toh "uint32_t little32"
88.Ft uint16_t
89.Fn le16toh "uint16_t little16"
90.Ft uint32_t
91.Fn letoh32 "uint32_t little32"
92.Ft uint16_t
93.Fn letoh16 "uint16_t little16"
94.Ft uint64_t
95.Fn swap64 "uint64_t val64"
96.Ft uint32_t
97.Fn swap32 "uint32_t val32"
98.Ft uint16_t
99.Fn swap16 "uint16_t val16"
100.Sh DESCRIPTION
101These routines convert 16, 32 and 64-bit quantities between different
102byte orderings.
103The
104.Dq swap
105functions reverse the byte ordering of
106the given quantity; the others convert either from/to the native
107byte order used by the host to/from either little- or big-endian (a.k.a
108network) order.
109.Pp
110Apart from the swap functions,
111the names containing
112.Dq be
113convert between host and big-endian (most significant byte first) order
114of the given quantity, while the names containing
115.Dq le
116convert between host and little-endian (least significant byte first) order
117of the given quantity.
118.Pp
119All these functions use the numbers
12016, 32, or 64 for specifying the bitwidth of the quantities they operate on.
121Currently all supported architectures are either big- or little-endian
122so either the
123.Dq be
124or
125.Dq le
126variants are implemented as null macros.
127.Sh SEE ALSO
128.Xr htonl 3
129.Sh STANDARDS
130The
131.Fn htobe64 ,
132.Fn htobe32 ,
133.Fn htobe16 ,
134.Fn be64toh ,
135.Fn be32toh ,
136.Fn be16toh ,
137.Fn htole64 ,
138.Fn htole32 ,
139.Fn htole16 ,
140.Fn le64toh ,
141.Fn le32toh ,
142and
143.Fn le16toh
144functions conform to
145.St -p1003.1-2024 .
146The other functions are extensions that should not be used
147when portability is required.
148.Sh HISTORY
149The
150.Nm swap{size}
151and
152.Nm {src-order}to{dst-order}{size}
153functions appeared in
154.Bx 4.2 .
155The
156.Nm {src-order}{size}to{dst-order}
157functions appeared in
158.Ox 5.6 .
159.Sh BUGS
160The perceived antagonism between
161.Sq host
162and
163.Sq network
164byte order does not allow PDP-11 users to sleep soundly at night.
diff --git a/src/lib/libc/net/htonl.3 b/src/lib/libc/net/htonl.3
deleted file mode 100644
index 65fa9a53b5..0000000000
--- a/src/lib/libc/net/htonl.3
+++ /dev/null
@@ -1,102 +0,0 @@
1.\" $OpenBSD: htonl.3,v 1.6 2022/09/11 06:38:10 jmc 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. Neither the name of the University nor the names of its contributors
15.\" may be used to endorse or promote products derived from this software
16.\" without specific prior written permission.
17.\"
18.\" THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
19.\" ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
20.\" IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
21.\" ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
22.\" FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
23.\" DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
24.\" OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
25.\" HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
26.\" LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
27.\" OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
28.\" SUCH DAMAGE.
29.\"
30.Dd $Mdocdate: September 11 2022 $
31.Dt HTONL 3
32.Os
33.Sh NAME
34.Nm htonl ,
35.Nm htons ,
36.Nm ntohl ,
37.Nm ntohs
38.Nd convert values between host and network byte orderings
39.Sh SYNOPSIS
40.In arpa/inet.h
41.Ft uint32_t
42.Fn htonl "uint32_t host32"
43.Ft uint16_t
44.Fn htons "uint16_t host16"
45.Ft uint32_t
46.Fn ntohl "uint32_t net32"
47.Ft uint16_t
48.Fn ntohs "uint16_t net16"
49.Sh DESCRIPTION
50These routines convert 16 and 32-bit quantities between different
51byte orderings.
52.Pp
53The
54.Fn htonl
55and
56.Fn htons
57functions convert quantities from host to network byte order while the
58.Fn ntohl
59and
60.Fn ntohs
61functions convert in the other direction.
62.Pp
63The last letter
64.Pf ( Sq s
65or
66.Sq l )
67is a mnemonic
68for the traditional names for such quantities,
69short and long, respectively.
70Today, the C concept of
71.Vt short
72and
73.Vt long
74integers need not coincide with this traditional misunderstanding.
75On machines which have a byte order which is the same as the network
76order, routines are defined as null macros.
77.Pp
78These routines are most often used in conjunction with Internet
79addresses and ports as returned by
80.Xr gethostbyname 3
81and
82.Xr getservent 3 .
83.Sh SEE ALSO
84.Xr gethostbyname 3 ,
85.Xr getservent 3 ,
86.Xr htobe64 3
87.Sh STANDARDS
88The
89.Fn htonl ,
90.Fn htons ,
91.Fn ntohl ,
92and
93.Fn ntohs
94functions conform to
95.St -p1003.1 .
96.Sh HISTORY
97These functions appeared in
98.Bx 4.2 .
99.Sh BUGS
100On the alpha, amd64, i386, and some mips and arm architectures,
101bytes are handled backwards from most everyone else in the world.
102This is not expected to be fixed in the near future.
diff --git a/src/lib/libc/net/htonl.c b/src/lib/libc/net/htonl.c
deleted file mode 100644
index 58bfb4699a..0000000000
--- a/src/lib/libc/net/htonl.c
+++ /dev/null
@@ -1,15 +0,0 @@
1/* $OpenBSD: htonl.c,v 1.8 2024/04/15 14:30:48 naddy Exp $ */
2/*
3 * Public domain.
4 */
5
6#include <sys/types.h>
7#include <endian.h>
8
9#undef htonl
10
11uint32_t
12htonl(uint32_t x)
13{
14 return htobe32(x);
15}
diff --git a/src/lib/libc/net/htons.c b/src/lib/libc/net/htons.c
deleted file mode 100644
index 28b13cef98..0000000000
--- a/src/lib/libc/net/htons.c
+++ /dev/null
@@ -1,15 +0,0 @@
1/* $OpenBSD: htons.c,v 1.10 2024/04/15 14:30:48 naddy Exp $ */
2/*
3 * Public domain.
4 */
5
6#include <sys/types.h>
7#include <endian.h>
8
9#undef htons
10
11uint16_t
12htons(uint16_t x)
13{
14 return htobe16(x);
15}
diff --git a/src/lib/libc/net/if_indextoname.3 b/src/lib/libc/net/if_indextoname.3
deleted file mode 100644
index 25d2a2722f..0000000000
--- a/src/lib/libc/net/if_indextoname.3
+++ /dev/null
@@ -1,143 +0,0 @@
1.\" $OpenBSD: if_indextoname.3,v 1.16 2015/11/21 07:48:10 jmc Exp $
2.\" Copyright (c) 1983, 1991, 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. Neither the name of the University nor the names of its contributors
14.\" may be used to endorse or promote products derived from this software
15.\" without specific prior written permission.
16.\"
17.\" THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
18.\" ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
19.\" IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
20.\" ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
21.\" FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
22.\" DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
23.\" OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
24.\" HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
25.\" LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
26.\" OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
27.\" SUCH DAMAGE.
28.\"
29.\" From: @(#)rcmd.3 8.1 (Berkeley) 6/4/93
30.\"
31.Dd $Mdocdate: November 21 2015 $
32.Dt IF_NAMETOINDEX 3
33.Os
34.Sh NAME
35.Nm if_nametoindex ,
36.Nm if_indextoname ,
37.Nm if_nameindex ,
38.Nm if_freenameindex
39.Nd convert interface index to name, and vice versa
40.Sh SYNOPSIS
41.In sys/types.h
42.In sys/socket.h
43.In net/if.h
44.Ft "unsigned int"
45.Fn if_nametoindex "const char *ifname"
46.Ft "char *"
47.Fn if_indextoname "unsigned int ifindex" "char *ifname"
48.Ft "struct if_nameindex *"
49.Fn if_nameindex "void"
50.Ft "void"
51.Fn if_freenameindex "struct if_nameindex *ptr"
52.Sh DESCRIPTION
53These functions map interface indexes to interface names (such as
54.Dq lo0 ) ,
55and vice versa.
56.Pp
57The
58.Fn if_nametoindex
59function converts an interface name specified by the
60.Fa ifname
61argument to an interface index (positive integer value).
62If the specified interface does not exist, 0 will be returned.
63.Pp
64.Fn if_indextoname
65converts an interface index specified by the
66.Fa ifindex
67argument to an interface name.
68The
69.Fa ifname
70argument must point to a buffer of at least
71.Dv IF_NAMESIZE
72bytes into which the interface name corresponding to the specified index is
73returned.
74.Pf ( Dv IF_NAMESIZE
75is also defined in
76.In net/if.h
77and its value includes a terminating NUL byte at the end of the
78interface name.)
79This pointer is also the return value of the function.
80If there is no interface corresponding to the specified index,
81.Dv NULL
82is returned.
83.Pp
84.Fn if_nameindex
85returns an array of
86.Vt if_nameindex
87structures.
88.Vt if_nameindex
89is also defined in
90.In net/if.h ,
91and is as follows:
92.Bd -literal -offset indent
93struct if_nameindex {
94 unsigned int if_index; /* 1, 2, ... */
95 char *if_name; /* NUL-terminated name */
96};
97.Ed
98.Pp
99The end of the array of structures is indicated by a structure with
100an
101.Fa if_index
102of 0 and an
103.Fa if_name
104of
105.Dv NULL .
106The function returns a null pointer on error.
107The memory used for this array of structures along with the interface
108names pointed to by the
109.Fa if_name
110members is obtained dynamically.
111This memory is freed by the
112.Fn if_freenameindex
113function.
114.Pp
115.Fn if_freenameindex
116takes a pointer that was returned by
117.Fn if_nameindex
118as argument
119.Pq Fa ptr ,
120and it reclaims the region allocated.
121.Sh DIAGNOSTICS
122.Fn if_nametoindex
123returns 0 on error, positive integer on success.
124.Fn if_indextoname
125and
126.Fn if_nameindex
127return
128.Dv NULL
129on errors.
130.Sh SEE ALSO
131.Xr getifaddrs 3 ,
132.Xr netintro 4
133.Sh STANDARDS
134.Rs
135.%A R. Gilligan
136.%A S. Thomson
137.%A J. Bound
138.%A J. McCann
139.%A W. Stevens
140.%D February 2003
141.%R RFC 3493
142.%T Basic Socket Interface Extensions for IPv6
143.Re
diff --git a/src/lib/libc/net/if_indextoname.c b/src/lib/libc/net/if_indextoname.c
deleted file mode 100644
index 3c6e8681fb..0000000000
--- a/src/lib/libc/net/if_indextoname.c
+++ /dev/null
@@ -1,78 +0,0 @@
1/* $OpenBSD: if_indextoname.c,v 1.12 2018/05/10 13:44:43 tb Exp $ */
2/* $KAME: if_indextoname.c,v 1.6 2000/11/07 22:33:25 jinmei Exp $ */
3
4/*-
5 * Copyright (c) 2015 Claudio Jeker <claudio@openbsd.org>
6 * Copyright (c) 1997, 2000
7 * Berkeley Software Design, Inc. All rights reserved.
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 *
15 * THIS SOFTWARE IS PROVIDED BY Berkeley Software Design, Inc. ``AS IS'' AND
16 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
17 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
18 * ARE DISCLAIMED. IN NO EVENT SHALL Berkeley Software Design, Inc. BE LIABLE
19 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
20 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
21 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
22 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
23 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
24 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
25 * SUCH DAMAGE.
26 *
27 * BSDI Id: if_indextoname.c,v 2.3 2000/04/17 22:38:05 dab Exp
28 */
29
30#include <sys/types.h>
31#include <sys/socket.h>
32#include <net/if.h>
33#include <stdlib.h>
34#include <string.h>
35#include <errno.h>
36
37/*
38 * From RFC 2553:
39 *
40 * The second function maps an interface index into its corresponding
41 * name.
42 *
43 * #include <net/if.h>
44 *
45 * char *if_indextoname(unsigned int ifindex, char *ifname);
46 *
47 * The ifname argument must point to a buffer of at least IF_NAMESIZE
48 * bytes into which the interface name corresponding to the specified
49 * index is returned. (IF_NAMESIZE is also defined in <net/if.h> and
50 * its value includes a terminating null byte at the end of the
51 * interface name.) This pointer is also the return value of the
52 * function. If there is no interface corresponding to the specified
53 * index, NULL is returned, and errno is set to ENXIO, if there was a
54 * system error (such as running out of memory), if_indextoname returns
55 * NULL and errno would be set to the proper value (e.g., ENOMEM).
56 */
57
58char *
59if_indextoname(unsigned int ifindex, char *ifname)
60{
61 struct if_nameindex *ifni, *ifni2;
62
63 if ((ifni = if_nameindex()) == NULL)
64 return NULL;
65
66 for (ifni2 = ifni; ifni2->if_index != 0; ifni2++) {
67 if (ifni2->if_index == ifindex) {
68 strlcpy(ifname, ifni2->if_name, IFNAMSIZ);
69 if_freenameindex(ifni);
70 return ifname;
71 }
72 }
73
74 if_freenameindex(ifni);
75 errno = ENXIO;
76 return NULL;
77}
78DEF_WEAK(if_indextoname);
diff --git a/src/lib/libc/net/if_nameindex.c b/src/lib/libc/net/if_nameindex.c
deleted file mode 100644
index 6e5bc0809a..0000000000
--- a/src/lib/libc/net/if_nameindex.c
+++ /dev/null
@@ -1,145 +0,0 @@
1/* $OpenBSD: if_nameindex.c,v 1.13 2016/12/16 17:44:59 krw Exp $ */
2/* $KAME: if_nameindex.c,v 1.7 2000/11/24 08:17:20 itojun Exp $ */
3
4/*-
5 * Copyright (c) 2015 Claudio Jeker <claudio@openbsd.org>
6 * Copyright (c) 1997, 2000
7 * Berkeley Software Design, Inc. All rights reserved.
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 *
15 * THIS SOFTWARE IS PROVIDED BY Berkeley Software Design, Inc. ``AS IS'' AND
16 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
17 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
18 * ARE DISCLAIMED. IN NO EVENT SHALL Berkeley Software Design, Inc. BE LIABLE
19 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
20 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
21 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
22 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
23 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
24 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
25 * SUCH DAMAGE.
26 *
27 * BSDI Id: if_nameindex.c,v 2.3 2000/04/17 22:38:05 dab Exp
28 */
29
30#include <sys/types.h>
31#include <sys/socket.h>
32#include <sys/sysctl.h>
33#include <net/if.h>
34#include <stdlib.h>
35#include <string.h>
36#include <errno.h>
37
38/*
39 * From RFC 2553:
40 *
41 * 4.3 Return All Interface Names and Indexes
42 *
43 * The if_nameindex structure holds the information about a single
44 * interface and is defined as a result of including the <net/if.h>
45 * header.
46 *
47 * struct if_nameindex {
48 * unsigned int if_index;
49 * char *if_name;
50 * };
51 *
52 * The final function returns an array of if_nameindex structures, one
53 * structure per interface.
54 *
55 * struct if_nameindex *if_nameindex(void);
56 *
57 * The end of the array of structures is indicated by a structure with
58 * an if_index of 0 and an if_name of NULL. The function returns a NULL
59 * pointer upon an error, and would set errno to the appropriate value.
60 *
61 * The memory used for this array of structures along with the interface
62 * names pointed to by the if_name members is obtained dynamically.
63 * This memory is freed by the next function.
64 *
65 * 4.4. Free Memory
66 *
67 * The following function frees the dynamic memory that was allocated by
68 * if_nameindex().
69 *
70 * #include <net/if.h>
71 *
72 * void if_freenameindex(struct if_nameindex *ptr);
73 *
74 * The argument to this function must be a pointer that was returned by
75 * if_nameindex().
76 */
77
78struct if_nameindex *
79if_nameindex(void)
80{
81 struct if_nameindex_msg *ifnm = NULL;
82 struct if_nameindex *ifni = NULL, *ifni2;
83 char *cp;
84 size_t needed;
85 unsigned int ni, i;
86 int mib[6];
87
88 mib[0] = CTL_NET;
89 mib[1] = PF_ROUTE;
90 mib[2] = 0; /* protocol */
91 mib[3] = 0; /* not used */
92 mib[4] = NET_RT_IFNAMES;
93 mib[5] = 0; /* no flags */
94 while (1) {
95 struct if_nameindex_msg *buf = NULL;
96
97 if (sysctl(mib, 6, NULL, &needed, NULL, 0) == -1)
98 goto out;
99 if (needed == 0)
100 break;
101 if ((buf = realloc(ifnm, needed)) == NULL)
102 goto out;
103 ifnm = buf;
104 if (sysctl(mib, 6, ifnm, &needed, NULL, 0) == -1) {
105 if (errno == ENOMEM)
106 continue;
107 goto out;
108 }
109 break;
110 }
111
112 /*
113 * Allocate a chunk of memory, use the first part for the array of
114 * structures, and the last part for the strings.
115 */
116 ni = needed / sizeof(*ifnm);
117 ifni = calloc(ni + 1, sizeof(struct if_nameindex) + IF_NAMESIZE);
118 if (ifni == NULL)
119 goto out;
120 cp = (char *)(ifni + (ni + 1));
121
122 ifni2 = ifni;
123 for (i = 0; i < ni; i++) {
124 ifni2->if_index = ifnm[i].if_index;
125 /* don't care about truncation */
126 strlcpy(cp, ifnm[i].if_name, IF_NAMESIZE);
127 ifni2->if_name = cp;
128 ifni2++;
129 cp += IF_NAMESIZE;
130 }
131 /* Finally, terminate the array. */
132 ifni2->if_index = 0;
133 ifni2->if_name = NULL;
134out:
135 free(ifnm);
136 return ifni;
137}
138
139void
140if_freenameindex(struct if_nameindex *ptr)
141{
142 free(ptr);
143}
144DEF_WEAK(if_nameindex);
145DEF_WEAK(if_freenameindex);
diff --git a/src/lib/libc/net/if_nametoindex.c b/src/lib/libc/net/if_nametoindex.c
deleted file mode 100644
index 7a4492e37d..0000000000
--- a/src/lib/libc/net/if_nametoindex.c
+++ /dev/null
@@ -1,77 +0,0 @@
1/* $OpenBSD: if_nametoindex.c,v 1.10 2015/10/23 13:09:19 claudio Exp $ */
2/* $KAME: if_nametoindex.c,v 1.5 2000/11/24 08:04:40 itojun Exp $ */
3
4/*-
5 * Copyright (c) 2015 Claudio Jeker <claudio@openbsd.org>
6 * Copyright (c) 1997, 2000
7 * Berkeley Software Design, Inc. All rights reserved.
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 *
15 * THIS SOFTWARE IS PROVIDED BY Berkeley Software Design, Inc. ``AS IS'' AND
16 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
17 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
18 * ARE DISCLAIMED. IN NO EVENT SHALL Berkeley Software Design, Inc. BE LIABLE
19 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
20 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
21 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
22 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
23 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
24 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
25 * SUCH DAMAGE.
26 *
27 * BSDI Id: if_nametoindex.c,v 2.3 2000/04/17 22:38:05 dab Exp
28 */
29
30#include <sys/types.h>
31#include <sys/socket.h>
32#include <net/if.h>
33#include <stdlib.h>
34#include <string.h>
35#include <errno.h>
36
37/*
38 * From RFC 2553:
39 *
40 * 4.1 Name-to-Index
41 *
42 *
43 * The first function maps an interface name into its corresponding
44 * index.
45 *
46 * #include <net/if.h>
47 *
48 * unsigned int if_nametoindex(const char *ifname);
49 *
50 * If the specified interface name does not exist, the return value is
51 * 0, and errno is set to ENXIO. If there was a system error (such as
52 * running out of memory), the return value is 0 and errno is set to the
53 * proper value (e.g., ENOMEM).
54 */
55
56unsigned int
57if_nametoindex(const char *ifname)
58{
59 struct if_nameindex *ifni, *ifni2;
60 unsigned int index;
61
62 if ((ifni = if_nameindex()) == NULL)
63 return(0);
64
65 for (ifni2 = ifni; ifni2->if_index != 0; ifni2++) {
66 if (strcmp(ifni2->if_name, ifname) == 0) {
67 index = ifni2->if_index;
68 if_freenameindex(ifni);
69 return index;
70 }
71 }
72
73 if_freenameindex(ifni);
74 errno = ENXIO;
75 return 0;
76}
77DEF_WEAK(if_nametoindex);
diff --git a/src/lib/libc/net/inet6_opt_init.3 b/src/lib/libc/net/inet6_opt_init.3
deleted file mode 100644
index 41ba842166..0000000000
--- a/src/lib/libc/net/inet6_opt_init.3
+++ /dev/null
@@ -1,328 +0,0 @@
1.\" $OpenBSD: inet6_opt_init.3,v 1.8 2022/03/31 17:27:16 naddy Exp $
2.\" $KAME: inet6_opt_init.3,v 1.7 2004/12/27 05:08:23 itojun Exp $
3.\"
4.\" Copyright (C) 2004 WIDE Project.
5.\" 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. Neither the name of the project nor the names of its contributors
16.\" may be used to endorse or promote products derived from this software
17.\" without specific prior written permission.
18.\"
19.\" THIS SOFTWARE IS PROVIDED BY THE PROJECT AND CONTRIBUTORS ``AS IS'' AND
20.\" ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
21.\" IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
22.\" ARE DISCLAIMED. IN NO EVENT SHALL THE PROJECT OR CONTRIBUTORS BE LIABLE
23.\" FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
24.\" DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
25.\" OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
26.\" HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
27.\" LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
28.\" OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
29.\" SUCH DAMAGE.
30.\"
31.Dd $Mdocdate: March 31 2022 $
32.Dt INET6_OPT_INIT 3
33.Os
34.\"
35.Sh NAME
36.Nm inet6_opt_init ,
37.Nm inet6_opt_append ,
38.Nm inet6_opt_finish ,
39.Nm inet6_opt_set_val ,
40.Nm inet6_opt_next ,
41.Nm inet6_opt_find ,
42.Nm inet6_opt_get_val
43.Nd IPv6 Hop-by-Hop and Destination Options manipulation
44.\"
45.Sh SYNOPSIS
46.In netinet/in.h
47.Ft "int"
48.Fn inet6_opt_init "void *extbuf" "socklen_t extlen"
49.Ft "int"
50.Fn inet6_opt_append "void *extbuf" "socklen_t extlen" "int offset" "u_int8_t type" "socklen_t len" "u_int8_t align" "void **databufp"
51.Ft "int"
52.Fn inet6_opt_finish "void *extbuf" "socklen_t extlen" "int offset"
53.Ft "int"
54.Fn inet6_opt_set_val "void *databuf" "int offset" "void *val" "socklen_t vallen"
55.Ft "int"
56.Fn inet6_opt_next "void *extbuf" "socklen_t extlen" "int offset" "u_int8_t *typep" "socklen_t *lenp" "void **databufp"
57.Ft "int"
58.Fn inet6_opt_find "void *extbuf" "socklen_t extlen" "int offset" "u_int8_t type" "socklen_t *lenp" "void **databufp"
59.Ft "int"
60.Fn inet6_opt_get_val "void *databuf" "socklen_t offset" "void *val" "socklen_t vallen"
61.\"
62.Sh DESCRIPTION
63Building and parsing the Hop-by-Hop and Destination options is
64complicated.
65The advanced sockets API defines a set of functions to
66help applications create and manipulate Hop-by-Hop and Destination
67options.
68These functions use the
69formatting rules specified in Appendix B in RFC 2460, i.e. that the
70largest field is placed last in the option.
71The function prototypes
72for these functions are all contained in the header file
73.In netinet/in.h .
74.\"
75.Ss inet6_opt_init
76The
77.Fn inet6_opt_init
78function
79returns the number of bytes needed for an empty
80extension header, one without any options.
81If the
82.Va extbuf
83argument points to a valid section of memory
84then the
85.Fn inet6_opt_init
86function also initializes the extension header's length field.
87When attempting to initialize an extension buffer passed in the
88.Va extbuf
89argument,
90.Fa extlen
91must be a positive multiple of 8 or else the function fails and
92returns \-1 to the caller.
93.\"
94.Ss inet6_opt_append
95The
96.Fn inet6_opt_append
97function can perform different jobs.
98When a valid
99.Fa extbuf
100argument is supplied, it appends an option to the extension buffer and
101returns the updated total length as well as a pointer to the newly
102created option in
103.Fa databufp .
104If the value
105of
106.Fa extbuf
107is
108.Dv NULL
109then the
110.Fn inet6_opt_append
111function only reports what the total length would
112be if the option were actually appended.
113The
114.Fa len
115and
116.Fa align
117arguments specify the length of the option and the required data
118alignment which must be used when appending the option.
119The
120.Fa offset
121argument should be the length returned by the
122.Fn inet6_opt_init
123function or a previous call to
124.Fn inet6_opt_append .
125.Pp
126The
127.Fa type
128argument is the 8-bit option type.
129.Pp
130After
131.Fn inet6_opt_append
132has been called, the application can use the buffer pointed to by
133.Fa databufp
134directly, or use
135.Fn inet6_opt_set_val
136to specify the data to be contained in the option.
137.Pp
138Option types of
139.Li 0
140and
141.Li 1
142are reserved for the
143.Li Pad1
144and
145.Li PadN
146options.
147All other values from 2 through 255 may be used by applications.
148.Pp
149The length of the option data is contained in an 8-bit value and so
150may contain any value from 0 through 255.
151.Pp
152The
153.Fa align
154parameter must have a value of 1, 2, 4, or 8 and cannot exceed the
155value of
156.Fa len .
157The alignment values represent no alignment, 16-bit, 32-bit and 64-bit
158alignments respectively.
159.\"
160.Ss inet6_opt_finish
161The
162.Fn inet6_opt_finish
163function calculates the final padding necessary to make the extension header a
164multiple of 8 bytes, as required by the IPv6 extension header
165specification, and returns the extension header's updated total
166length.
167The
168.Fa offset
169argument should be the length returned by
170.Fn inet6_opt_init
171or
172.Fn inet6_opt_append .
173When
174.Fa extbuf
175is not
176.Dv NULL ,
177the function also sets up the appropriate padding bytes by inserting a
178Pad1 or PadN option of the proper length.
179.Pp
180If the extension header is too small to contain the proper padding
181then an error of \-1 is returned to the caller.
182.\"
183.Ss inet6_opt_set_val
184The
185.Fn inet6_opt_set_val
186function inserts data items of various sizes into the data portion of
187the option.
188The
189.Fa databuf
190argument is a pointer to memory that was returned by the
191.Fn inet6_opt_append
192call and the
193.Fa offset
194argument specifies where the option should be placed in the
195data buffer.
196The
197.Fa val
198argument points to an area of memory containing the data to be
199inserted into the extension header, and the
200.Fa vallen
201argument indicates how much data to copy.
202.Pp
203The caller should ensure that each field is aligned on its natural
204boundaries as described in Appendix B of RFC 2460.
205.Pp
206The function returns the offset for the next field which is calculated as
207.Fa offset
208+
209.Fa vallen
210and is used when composing options with multiple fields.
211.\"
212.Ss inet6_opt_next
213The
214.Fn inet6_opt_next
215function parses received extension headers.
216The
217.Fa extbuf
218and
219.Fa extlen
220arguments specify the location and length of the extension header
221being parsed.
222The
223.Fa offset
224argument should either be zero, for the first option, or the length value
225returned by a previous call to
226.Fn inet6_opt_next
227or
228.Fn inet6_opt_find .
229The return value specifies the position where to continue scanning the
230extension buffer.
231The option is returned in the arguments
232.Fa typep , lenp ,
233and
234.Fa databufp .
235.Fa typep , lenp ,
236and
237.Fa databufp
238point to the 8-bit option type, the 8-bit option length and the option
239data respectively.
240This function does not return any PAD1 or PADN options.
241When an error occurs or there are no more options, the return
242value is \-1.
243.\"
244.Ss inet6_opt_find
245The
246.Fn inet6_opt_find
247function searches the extension buffer for a particular option type,
248passed in through the
249.Fa type
250argument.
251If the option is found then the
252.Fa lenp
253and
254.Fa databufp
255arguments are updated to point to the option's length and data
256respectively.
257.Fa extbuf
258and
259.Fa extlen
260must point to a valid extension buffer and give its length.
261The
262.Fa offset
263argument can be used to search from a location anywhere in the
264extension header.
265.Ss inet6_opt_get_val
266The
267.Fn inet6_opt_get_val
268function extracts data items of various sizes in the data portion of
269the option.
270The
271.Fa databuf
272is a pointer returned by the
273.Fn inet6_opt_next
274or
275.Fn inet6_opt_find
276functions.
277The
278.Fa val
279argument points to where the data will be extracted.
280The
281.Fa offset
282argument specifies from where in the data portion of the option the
283value should be extracted; the first byte of option data is specified
284by an offset of zero.
285.Pp
286It is expected that each field is aligned on its natural boundaries as
287described in Appendix B of RFC 2460.
288.Pp
289The function returns the offset for the next field
290by calculating
291.Fa offset
292+
293.Fa vallen
294which can be used when extracting option content with multiple fields.
295Robust receivers must verify alignment before calling this function.
296.\"
297.Sh EXAMPLES
298RFC 3542 gives comprehensive examples in Section 23.
299KAME also provides examples in the
300.Pa advapitest
301directory of its kit.
302.\"
303.Sh DIAGNOSTICS
304All the functions return
305\-1
306on an error.
307.\"
308.Sh STANDARDS
309.Rs
310.%A S. Deering
311.%A R. Hinden
312.%D December 1998
313.%R RFC 2460
314.%T Internet Protocol, Version 6 (IPv6) Specification
315.Re
316.Pp
317.Rs
318.%A W. Stevens
319.%A M. Thomas
320.%A E. Nordmark
321.%A T. Jinmei
322.%D May 2003
323.%R RFC 3542
324.%T Advanced Sockets Application Program Interface (API) for IPv6
325.Re
326.Sh HISTORY
327The implementation first appeared in KAME advanced networking kit.
328.\"
diff --git a/src/lib/libc/net/inet6_rth_space.3 b/src/lib/libc/net/inet6_rth_space.3
deleted file mode 100644
index c40b45057e..0000000000
--- a/src/lib/libc/net/inet6_rth_space.3
+++ /dev/null
@@ -1,220 +0,0 @@
1.\" $OpenBSD: inet6_rth_space.3,v 1.8 2022/03/31 17:27:16 naddy Exp $
2.\" $KAME: inet6_rth_space.3,v 1.7 2005/01/05 03:00:44 itojun Exp $
3.\"
4.\" Copyright (C) 2004 WIDE Project.
5.\" 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. Neither the name of the project nor the names of its contributors
16.\" may be used to endorse or promote products derived from this software
17.\" without specific prior written permission.
18.\"
19.\" THIS SOFTWARE IS PROVIDED BY THE PROJECT AND CONTRIBUTORS ``AS IS'' AND
20.\" ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
21.\" IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
22.\" ARE DISCLAIMED. IN NO EVENT SHALL THE PROJECT OR CONTRIBUTORS BE LIABLE
23.\" FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
24.\" DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
25.\" OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
26.\" HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
27.\" LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
28.\" OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
29.\" SUCH DAMAGE.
30.\"
31.Dd $Mdocdate: March 31 2022 $
32.Dt INET6_RTH_SPACE 3
33.Os
34.\"
35.Sh NAME
36.Nm inet6_rth_space ,
37.Nm inet6_rth_init ,
38.Nm inet6_rth_add ,
39.Nm inet6_rth_reverse ,
40.Nm inet6_rth_segments ,
41.Nm inet6_rth_getaddr
42.Nd IPv6 Routing Header Options manipulation
43.\"
44.Sh SYNOPSIS
45.In netinet/in.h
46.Ft socklen_t
47.Fn inet6_rth_space "int" "int"
48.Ft "void *"
49.Fn inet6_rth_init "void *" "socklen_t" "int" "int"
50.Ft int
51.Fn inet6_rth_add "void *" "const struct in6_addr *"
52.Ft int
53.Fn inet6_rth_reverse "const void *" "void *"
54.Ft int
55.Fn inet6_rth_segments "const void *"
56.Ft "struct in6_addr *"
57.Fn inet6_rth_getaddr "const void *" "int"
58.\"
59.Sh DESCRIPTION
60The IPv6 Advanced API, RFC 3542, defines the functions that an
61application calls to build and examine IPv6 Routing headers.
62Routing headers are used to perform source routing in IPv6 networks.
63The RFC uses the word
64.Dq segments
65to describe addresses and that is the term used here as well.
66All of the functions are defined in the header file
67.In netinet/in.h .
68The functions described in this manual page all operate
69on routing header structures which are defined in
70.In netinet/ip6.h
71but which should not need to be modified outside the use of this API.
72The size and shape of the route header structures may change, so using
73the APIs is a more portable, long term, solution.
74.Pp
75The functions in the API are split into two groups, those that build a
76routing header and those that parse a received routing header.
77The builder functions are described first, followed by the parser functions.
78.Ss inet6_rth_space
79The
80.Fn inet6_rth_space
81function returns the number of bytes required to hold a Routing Header
82of the type, specified in the
83.Fa type
84argument and containing the number of addresses specified in the
85.Fa segments
86argument.
87When the type is
88.Dv IPV6_RTHDR_TYPE_0 ,
89the number of segments must be from 0 through 127.
90The return value from this function is the number of bytes required to
91store the routing header.
92If the value 0 is returned then either the
93route header type was not recognized or another error occurred.
94.Ss inet6_rth_init
95The
96.Fn inet6_rth_init
97function initializes the pre-allocated buffer pointed to by
98.Fa bp
99to contain a routing header of the specified type.
100The
101.Fa bp_len
102argument is used to verify that the buffer is large enough.
103The caller must allocate the buffer pointed to by bp.
104The necessary buffer size should be determined by calling
105.Fn inet6_rth_space
106described in the previous sections.
107.Pp
108The
109.Fn inet6_rth_init
110function returns a pointer to
111.Fa bp
112on success and
113.Dv NULL
114when there is an error.
115.Ss inet6_rth_add
116The
117.Fn inet6_rth_add
118function adds the IPv6 address pointed to by
119.Fa addr
120to the end of the routing header being constructed.
121.Pp
122A successful addition results in the function returning 0, otherwise
123\-1 is returned.
124.Ss inet6_rth_reverse
125The
126.Fn inet6_rth_reverse
127function takes a routing header, pointed to by the
128argument
129.Fa in ,
130and writes a new routing header into the argument pointed to by
131.Fa out .
132The routing header at that sends datagrams along the reverse of that
133route.
134Both arguments are allowed to point to the same buffer meaning
135that the reversal can occur in place.
136.Pp
137The return value of the function is 0 on success, or \-1 when
138there is an error.
139.\"
140.Pp
141The next set of functions operate on a routing header that the
142application wants to parse.
143In the usual case such a routing header
144is received from the network, although these functions can also be
145used with routing headers that the application itself created.
146.Ss inet6_rth_segments
147The
148.Fn inet6_rth_segments
149function returns the number of segments contained in the
150routing header pointed to by
151.Fa bp .
152The return value is the number of segments contained in the routing
153header, or \-1 if an error occurred.
154It is not an error for 0 to be
155returned as a routing header may contain 0 segments.
156.\"
157.Ss inet6_rth_getaddr
158The
159.Fn inet6_rth_getaddr
160function is used to retrieve a single address from a routing header.
161The
162.Fa index
163is the location in the routing header from which the application wants
164to retrieve an address.
165The
166.Fa index
167parameter must have a value between 0 and one less than the number of
168segments present in the routing header.
169The
170.Fn inet6_rth_segments
171function, described in the last section, should be used to determine
172the total number of segments in the routing header.
173The
174.Fn inet6_rth_getaddr
175function returns a pointer to an IPv6 address on success or
176.Dv NULL
177when an error has occurred.
178.\"
179.Sh EXAMPLES
180RFC 3542 gives extensive examples in Section 21, Appendix B.
181KAME also provides examples in the advapitest directory of its kit.
182.\"
183.Sh DIAGNOSTICS
184The
185.Fn inet6_rth_space
186and
187.Fn inet6_rth_getaddr
188functions return 0 on errors.
189.Pp
190The
191.Fn inet6_rth_init
192function returns
193.Dv NULL
194on error.
195The
196.Fn inet6_rth_add
197and
198.Fn inet6_rth_reverse
199functions return 0 on success, or \-1 upon an error.
200.\"
201.Sh STANDARDS
202.Rs
203.%A S. Deering
204.%A R. Hinden
205.%D December 1998
206.%R RFC 2460
207.%T Internet Protocol, Version 6 (IPv6) Specification
208.Re
209.Pp
210.Rs
211.%A W. Stevens
212.%A M. Thomas
213.%A E. Nordmark
214.%A T. Jinmei
215.%D May 2003
216.%R RFC 3542
217.%T Advanced Sockets Application Programming Interface (API) for IPv6
218.Re
219.Sh HISTORY
220The implementation first appeared in KAME advanced networking kit.
diff --git a/src/lib/libc/net/inet_addr.3 b/src/lib/libc/net/inet_addr.3
deleted file mode 100644
index cbb9e14f6d..0000000000
--- a/src/lib/libc/net/inet_addr.3
+++ /dev/null
@@ -1,195 +0,0 @@
1.\" $OpenBSD: inet_addr.3,v 1.7 2024/03/06 07:29:37 bentley Exp $
2.\" $NetBSD: inet.3,v 1.7 1997/06/18 02:25:24 lukem Exp $
3.\"
4.\" Copyright (c) 1983, 1990, 1991, 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. Neither the name of the University nor the names of its contributors
16.\" may be used to endorse or promote products derived from this software
17.\" without specific prior written permission.
18.\"
19.\" THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
20.\" ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
21.\" IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
22.\" ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
23.\" FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
24.\" DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
25.\" OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
26.\" HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
27.\" LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
28.\" OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
29.\" SUCH DAMAGE.
30.\"
31.\" @(#)inet.3 8.1 (Berkeley) 6/4/93
32.\"
33.Dd $Mdocdate: March 6 2024 $
34.Dt INET_ADDR 3
35.Os
36.Sh NAME
37.Nm inet_aton ,
38.Nm inet_addr ,
39.Nm inet_network ,
40.Nm inet_ntoa
41.Nd Internet Protocol version 4 (IPv4) address manipulation routines
42.Sh SYNOPSIS
43.In arpa/inet.h
44.Ft int
45.Fn inet_aton "const char *cp" "struct in_addr *addr"
46.Ft in_addr_t
47.Fn inet_addr "const char *cp"
48.Ft in_addr_t
49.Fn inet_network "const char *cp"
50.Ft char *
51.Fn inet_ntoa "struct in_addr in"
52.Sh DESCRIPTION
53The functions presented here only support IPv4 addresses.
54In order to support IPv6 addresses as well,
55.Xr inet_ntop 3
56and
57.Xr inet_pton 3
58should be used rather than the functions presented here.
59Scoped IPv6 addresses are supported via
60.Xr getaddrinfo 3
61and
62.Xr getnameinfo 3 .
63.Pp
64The routines
65.Fn inet_aton ,
66.Fn inet_addr ,
67and
68.Fn inet_network
69interpret character strings representing
70numbers expressed in the Internet standard
71.Dq dot
72notation.
73.Pp
74The
75.Fn inet_aton
76routine interprets the specified character string as an Internet address,
77placing the address into the structure provided.
78It returns 1 if the string was successfully interpreted,
79or 0 if the string was invalid.
80.Pp
81The
82.Fn inet_addr
83and
84.Fn inet_network
85functions return numbers suitable for use
86as Internet addresses and Internet network
87numbers, respectively.
88Both functions return the constant
89.Dv INADDR_NONE
90if the specified character string is malformed.
91.Pp
92The routine
93.Fn inet_ntoa
94takes an Internet address and returns an
95ASCII string representing the address in dot notation.
96.Pp
97All Internet addresses are returned in network
98order (bytes ordered from left to right).
99All network numbers and local address parts are
100returned as machine format integer values.
101.Sh INTERNET ADDRESSES (IP VERSION 4)
102Values specified using dot notation take one of the following forms:
103.Bd -literal -offset indent
104a.b.c.d
105a.b.c
106a.b
107a
108.Ed
109.Pp
110When four parts are specified, each is interpreted
111as a byte of data and assigned, from left to right,
112to the four bytes of an Internet address.
113Note that when an Internet address is viewed as a 32-bit
114integer quantity on a system that uses little-endian
115byte order
116(such as AMD64 or ARM processors)
117the bytes referred to above appear as
118.Dq Li d.c.b.a .
119That is, little-endian bytes are ordered from right to left.
120.Pp
121When a three part address is specified, the last
122part is interpreted as a 16-bit quantity and placed
123in the rightmost two bytes of the network address.
124This makes the three part address format convenient
125for specifying Class B network addresses as
126.Dq Li 128.net.host .
127.Pp
128When a two part address is supplied, the last part
129is interpreted as a 24-bit quantity and placed in
130the rightmost three bytes of the network address.
131This makes the two part address format convenient
132for specifying Class A network addresses as
133.Dq Li net.host .
134.Pp
135When only one part is given, the value is stored
136directly in the network address without any byte
137rearrangement.
138.Pp
139All numbers supplied as
140.Dq parts
141in a dot notation
142may be decimal, octal, or hexadecimal, as specified
143in the C language (i.e., a leading 0x or 0X implies
144hexadecimal; a leading 0 implies octal;
145otherwise, the number is interpreted as decimal).
146.Sh SEE ALSO
147.Xr gethostbyname 3 ,
148.Xr htonl 3 ,
149.Xr inet_lnaof 3 ,
150.Xr inet_net_ntop 3 ,
151.Xr inet_ntop 3 ,
152.Xr hosts 5
153.Sh STANDARDS
154The
155.Nm inet_addr
156and
157.Nm inet_ntoa
158functions conform to
159.St -p1003.1-2008 .
160.Sh HISTORY
161The
162.Nm inet_addr
163and
164.Nm inet_network
165functions appeared in
166.Bx 4.2 .
167The
168.Nm inet_aton
169and
170.Nm inet_ntoa
171functions appeared in
172.Bx 4.3 .
173.Sh BUGS
174The value
175.Dv INADDR_NONE
176(0xffffffff) is a valid broadcast address, but
177.Fn inet_addr
178cannot return that value without indicating failure.
179Also,
180.Fn inet_addr
181should have been designed to return a
182.Vt struct in_addr .
183The newer
184.Fn inet_aton
185function does not share these problems, and almost all existing code
186should be modified to use
187.Fn inet_aton
188instead.
189.Pp
190The problem of host byte ordering versus network byte ordering is
191confusing.
192.Pp
193The string returned by
194.Fn inet_ntoa
195resides in a static memory area.
diff --git a/src/lib/libc/net/inet_addr.c b/src/lib/libc/net/inet_addr.c
deleted file mode 100644
index 62d46ad664..0000000000
--- a/src/lib/libc/net/inet_addr.c
+++ /dev/null
@@ -1,175 +0,0 @@
1/* $OpenBSD: inet_addr.c,v 1.12 2015/09/13 21:36:08 guenther Exp $ */
2
3/*
4 * ++Copyright++ 1983, 1990, 1993
5 * -
6 * Copyright (c) 1983, 1990, 1993
7 * The Regents of the University of California. All rights reserved.
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. 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 * Portions Copyright (c) 1993 by Digital Equipment Corporation.
34 *
35 * Permission to use, copy, modify, and distribute this software for any
36 * purpose with or without fee is hereby granted, provided that the above
37 * copyright notice and this permission notice appear in all copies, and that
38 * the name of Digital Equipment Corporation not be used in advertising or
39 * publicity pertaining to distribution of the document or software without
40 * specific, written prior permission.
41 *
42 * THE SOFTWARE IS PROVIDED "AS IS" AND DIGITAL EQUIPMENT CORP. DISCLAIMS ALL
43 * WARRANTIES WITH REGARD TO THIS SOFTWARE, INCLUDING ALL IMPLIED WARRANTIES
44 * OF MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL DIGITAL EQUIPMENT
45 * CORPORATION BE LIABLE FOR ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL
46 * DAMAGES OR ANY DAMAGES WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR
47 * PROFITS, WHETHER IN AN ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS
48 * ACTION, ARISING OUT OF OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS
49 * SOFTWARE.
50 * -
51 * --Copyright--
52 */
53
54#include <sys/types.h>
55#include <netinet/in.h>
56#include <arpa/inet.h>
57#include <ctype.h>
58
59/*
60 * Ascii internet address interpretation routine.
61 * The value returned is in network order.
62 */
63in_addr_t
64inet_addr(const char *cp)
65{
66 struct in_addr val;
67
68 if (inet_aton(cp, &val))
69 return (val.s_addr);
70 return (INADDR_NONE);
71}
72
73/*
74 * Check whether "cp" is a valid ascii representation
75 * of an Internet address and convert to a binary address.
76 * Returns 1 if the address is valid, 0 if not.
77 * This replaces inet_addr, the return value from which
78 * cannot distinguish between failure and a local broadcast address.
79 */
80int
81inet_aton(const char *cp, struct in_addr *addr)
82{
83 in_addr_t val;
84 int base, n;
85 char c;
86 u_int parts[4];
87 u_int *pp = parts;
88
89 c = *cp;
90 for (;;) {
91 /*
92 * Collect number up to ``.''.
93 * Values are specified as for C:
94 * 0x=hex, 0=octal, isdigit=decimal.
95 */
96 if (!isdigit((unsigned char)c))
97 return (0);
98 val = 0; base = 10;
99 if (c == '0') {
100 c = *++cp;
101 if (c == 'x' || c == 'X')
102 base = 16, c = *++cp;
103 else
104 base = 8;
105 }
106 for (;;) {
107 if (isascii((unsigned char)c) &&
108 isdigit((unsigned char)c)) {
109 val = (val * base) + (c - '0');
110 c = *++cp;
111 } else if (base == 16 &&
112 isascii((unsigned char)c) &&
113 isxdigit((unsigned char)c)) {
114 val = (val << 4) |
115 (c + 10 - (islower((unsigned char)c) ? 'a' : 'A'));
116 c = *++cp;
117 } else
118 break;
119 }
120 if (c == '.') {
121 /*
122 * Internet format:
123 * a.b.c.d
124 * a.b.c (with c treated as 16 bits)
125 * a.b (with b treated as 24 bits)
126 */
127 if (pp >= parts + 3)
128 return (0);
129 *pp++ = val;
130 c = *++cp;
131 } else
132 break;
133 }
134 /*
135 * Check for trailing characters.
136 */
137 if (c != '\0' &&
138 (!isascii((unsigned char)c) || !isspace((unsigned char)c)))
139 return (0);
140 /*
141 * Concoct the address according to
142 * the number of parts specified.
143 */
144 n = pp - parts + 1;
145 switch (n) {
146
147 case 0:
148 return (0); /* initial nondigit */
149
150 case 1: /* a -- 32 bits */
151 break;
152
153 case 2: /* a.b -- 8.24 bits */
154 if ((val > 0xffffff) || (parts[0] > 0xff))
155 return (0);
156 val |= parts[0] << 24;
157 break;
158
159 case 3: /* a.b.c -- 8.8.16 bits */
160 if ((val > 0xffff) || (parts[0] > 0xff) || (parts[1] > 0xff))
161 return (0);
162 val |= (parts[0] << 24) | (parts[1] << 16);
163 break;
164
165 case 4: /* a.b.c.d -- 8.8.8.8 bits */
166 if ((val > 0xff) || (parts[0] > 0xff) || (parts[1] > 0xff) || (parts[2] > 0xff))
167 return (0);
168 val |= (parts[0] << 24) | (parts[1] << 16) | (parts[2] << 8);
169 break;
170 }
171 if (addr)
172 addr->s_addr = htonl(val);
173 return (1);
174}
175DEF_WEAK(inet_aton);
diff --git a/src/lib/libc/net/inet_lnaof.3 b/src/lib/libc/net/inet_lnaof.3
deleted file mode 100644
index 6151f7fe34..0000000000
--- a/src/lib/libc/net/inet_lnaof.3
+++ /dev/null
@@ -1,89 +0,0 @@
1.\" $OpenBSD: inet_lnaof.3,v 1.4 2019/08/30 20:06:07 jmc Exp $
2.\" $NetBSD: inet.3,v 1.7 1997/06/18 02:25:24 lukem Exp $
3.\"
4.\" Copyright (c) 1983, 1990, 1991, 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. Neither the name of the University nor the names of its contributors
16.\" may be used to endorse or promote products derived from this software
17.\" without specific prior written permission.
18.\"
19.\" THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
20.\" ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
21.\" IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
22.\" ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
23.\" FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
24.\" DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
25.\" OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
26.\" HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
27.\" LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
28.\" OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
29.\" SUCH DAMAGE.
30.\"
31.\" @(#)inet.3 8.1 (Berkeley) 6/4/93
32.\"
33.Dd $Mdocdate: August 30 2019 $
34.Dt INET_LNAOF 3
35.Os
36.Sh NAME
37.Nm inet_makeaddr ,
38.Nm inet_netof ,
39.Nm inet_lnaof
40.Nd routines for manipulating classful Internet Protocol version 4 (IPv4) addresses
41.Sh SYNOPSIS
42.In arpa/inet.h
43.Ft struct in_addr
44.Fn inet_makeaddr "in_addr_t net" "in_addr_t lna"
45.Ft in_addr_t
46.Fn inet_netof "struct in_addr in"
47.Ft in_addr_t
48.Fn inet_lnaof "struct in_addr in"
49.Sh DESCRIPTION
50As originally designed,
51IP version 4 split each address into a network part and local network
52address part, encoding that split into the address itself.
53It is frequency-encoded;
54the most-significant bit is clear in Class A addresses,
55in which the high-order 8 bits are the network number.
56Class B addresses use the high-order 16 bits as the network field,
57and Class C addresses have a 24-bit network part.
58.Pp
59The routine
60.Fn inet_makeaddr
61takes an Internet network number and a local
62network address and constructs an Internet address
63from it.
64.Pp
65The routines
66.Fn inet_netof
67and
68.Fn inet_lnaof
69break apart Internet host addresses, returning
70the network number and local network address part,
71respectively.
72.Pp
73All Internet addresses are returned in network
74order (bytes ordered from left to right).
75All network numbers and local address parts are
76returned as machine format integer values.
77.Sh SEE ALSO
78.Xr gethostbyname 3 ,
79.Xr inet_addr 3 ,
80.Xr inet_net_ntop 3 ,
81.Xr hosts 5
82.Sh HISTORY
83The
84.Nm inet_makeaddr ,
85.Nm inet_lnaof ,
86and
87.Nm inet_netof
88functions appeared in
89.Bx 4.2 .
diff --git a/src/lib/libc/net/inet_lnaof.c b/src/lib/libc/net/inet_lnaof.c
deleted file mode 100644
index 92845387d3..0000000000
--- a/src/lib/libc/net/inet_lnaof.c
+++ /dev/null
@@ -1,50 +0,0 @@
1/* $OpenBSD: inet_lnaof.c,v 1.7 2015/01/16 16:48:51 deraadt Exp $ */
2/*
3 * Copyright (c) 1983, 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. Neither the name of the University nor the names of its contributors
15 * may be used to endorse or promote products derived from this software
16 * without specific prior written permission.
17 *
18 * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
19 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
20 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
21 * ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
22 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
23 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
24 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
25 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
26 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
27 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
28 * SUCH DAMAGE.
29 */
30
31#include <netinet/in.h>
32#include <arpa/inet.h>
33
34/*
35 * Return the local network address portion of an
36 * internet address; handles class a/b/c network
37 * number formats.
38 */
39in_addr_t
40inet_lnaof(struct in_addr in)
41{
42 in_addr_t i = ntohl(in.s_addr);
43
44 if (IN_CLASSA(i))
45 return ((i)&IN_CLASSA_HOST);
46 else if (IN_CLASSB(i))
47 return ((i)&IN_CLASSB_HOST);
48 else
49 return ((i)&IN_CLASSC_HOST);
50}
diff --git a/src/lib/libc/net/inet_makeaddr.c b/src/lib/libc/net/inet_makeaddr.c
deleted file mode 100644
index 88ddd2850e..0000000000
--- a/src/lib/libc/net/inet_makeaddr.c
+++ /dev/null
@@ -1,53 +0,0 @@
1/* $OpenBSD: inet_makeaddr.c,v 1.7 2015/01/16 16:48:51 deraadt Exp $ */
2/*
3 * Copyright (c) 1983, 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. Neither the name of the University nor the names of its contributors
15 * may be used to endorse or promote products derived from this software
16 * without specific prior written permission.
17 *
18 * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
19 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
20 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
21 * ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
22 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
23 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
24 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
25 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
26 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
27 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
28 * SUCH DAMAGE.
29 */
30
31#include <netinet/in.h>
32#include <arpa/inet.h>
33
34/*
35 * Formulate an Internet address from network + host. Used in
36 * building addresses stored in the ifnet structure.
37 */
38struct in_addr
39inet_makeaddr(in_addr_t net, in_addr_t host)
40{
41 in_addr_t addr;
42
43 if (net < 128)
44 addr = (net << IN_CLASSA_NSHIFT) | (host & IN_CLASSA_HOST);
45 else if (net < 65536)
46 addr = (net << IN_CLASSB_NSHIFT) | (host & IN_CLASSB_HOST);
47 else if (net < 16777216L)
48 addr = (net << IN_CLASSC_NSHIFT) | (host & IN_CLASSC_HOST);
49 else
50 addr = net | host;
51 addr = htonl(addr);
52 return (*(struct in_addr *)&addr);
53}
diff --git a/src/lib/libc/net/inet_net_ntop.3 b/src/lib/libc/net/inet_net_ntop.3
deleted file mode 100644
index 4212af3bb6..0000000000
--- a/src/lib/libc/net/inet_net_ntop.3
+++ /dev/null
@@ -1,195 +0,0 @@
1.\" $OpenBSD: inet_net_ntop.3,v 1.4 2022/09/11 06:38:10 jmc Exp $
2.\" $NetBSD: inet_net.3,v 1.1 1997/06/18 02:25:27 lukem Exp $
3.\"
4.\" Copyright (c) 1997 The NetBSD Foundation, Inc.
5.\" All rights reserved.
6.\"
7.\" This code is derived from software contributed to The NetBSD Foundation
8.\" by Luke Mewburn.
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.\"
19.\" THIS SOFTWARE IS PROVIDED BY THE NETBSD FOUNDATION, INC. AND CONTRIBUTORS
20.\" ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED
21.\" TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
22.\" PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE
23.\" LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
24.\" CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
25.\" SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
26.\" INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
27.\" CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
28.\" ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
29.\" POSSIBILITY OF SUCH DAMAGE.
30.\"
31.Dd $Mdocdate: September 11 2022 $
32.Dt INET_NET_NTOP 3
33.Os
34.Sh NAME
35.Nm inet_net_ntop ,
36.Nm inet_net_pton
37.Nd Internet network number manipulation routines
38.Sh SYNOPSIS
39.In sys/socket.h
40.In arpa/inet.h
41.Ft char *
42.Fn inet_net_ntop "int af" "const void *src" "int bits" "char *dst" "size_t size"
43.Ft int
44.Fn inet_net_pton "int af" "const char *src" "void *dst" "size_t size"
45.Sh DESCRIPTION
46The
47.Fn inet_net_ntop
48function converts an Internet network number from network format (usually a
49.Vt struct in_addr
50or some other binary form, in network byte order) to CIDR presentation format
51(suitable for external display purposes).
52.Fa bits
53is the number of bits in
54.Fa src
55that are the network number.
56It returns
57.Dv NULL
58if a system error occurs (in which case,
59.Va errno
60will have been set), or it returns a pointer to the destination string.
61.Pp
62The
63.Fn inet_net_pton
64function converts a presentation format Internet network number (that is,
65printable form as held in a character string) to network format (usually a
66.Vt struct in_addr
67or some other internal binary representation, in network byte order).
68It returns the number of bits (either computed based on the class, or
69specified with /CIDR), or \-1 if a failure occurred
70(in which case
71.Va errno
72will have been set.
73It will be set to
74.Er ENOENT
75if the Internet network number was not valid).
76.Pp
77Caution:
78The
79.Fa dst
80field should be zeroed before calling
81.Fn inet_net_pton
82as the function will only fill the number of bytes necessary to
83encode the network number in network byte order.
84.Pp
85The only values for
86.Fa af
87currently supported are
88.Dv AF_INET
89and
90.Dv AF_INET6 .
91.Fa size
92is the size of the result buffer
93.Fa dst .
94.Sh NETWORK NUMBERS (IP VERSION 4)
95The external representation of Internet network numbers may be specified in
96one of the following forms:
97.Bd -literal -offset indent
98a
99a.b
100a.b.c
101a.b.c.d
102.Ed
103.Pp
104Any of the above four forms may have
105.Dq Li /bits
106appended where
107.Dq Li bits
108is in the range
109.Li 0-32
110and is used to explicitly specify the number of bits in the network address.
111When
112.Dq Li /bits
113is not specified, the number of bits in the network address is calculated
114as the larger of the number of bits in the class to which the address
115belongs and the number of bits provided rounded up modulo 8.
116Examples:
117.Pp
118.Bl -tag -width 10.1.2.3/24 -offset indent -compact
119.It Li 10
120an 8-bit network number (class A), value
121.Li 10.0.0.0 .
122.It Li 192
123a 24-bit network number (class C), value
124.Li 192.0.0.0 .
125.It Li 10.10
126a 16-bit network number, value
127.Li 10.10.0.0 .
128.It Li 10.1.2
129a 24-bit network number, value
130.Li 10.1.2.0 .
131.It Li 10.1.2.3
132a 32-bit network number, value
133.Li 10.1.2.3 .
134.It Li 10.1.2.3/24
135a 24-bit network number (explicit), value
136.Li 10.1.2.3 .
137.El
138.Pp
139Note that when the number of bits is specified using
140.Dq Li /bits
141notation, the value of the address still includes all bits supplied
142in the external representation, even those bits which are the host
143part of an Internet address.
144Also, unlike
145.Xr inet_pton 3
146where the external representation is assumed to be a host address, the
147external representation for
148.Fn inet_net_pton
149is assumed to be a network address.
150Thus
151.Dq Li 10.1
152is assumed to be
153.Dq Li 10.1.0.0
154not
155.Dq Li 10.0.0.1
156.Pp
157All numbers supplied as
158.Dq parts
159in a
160.Ql \&.
161notation
162may be decimal, octal, or hexadecimal, as specified
163in the C language (i.e., a leading 0x or 0X implies
164hexadecimal; otherwise, a leading 0 implies octal;
165otherwise, the number is interpreted as decimal).
166.Sh NETWORK NUMBERS (IP VERSION 6)
167See
168.Xr inet_pton 3
169for valid external representations of IP version 6 addresses.
170A valid external representation may have
171.Dq Li /bits
172appended where
173.Dq Li bits
174is in the range
175.Li 0-128
176and is used to explicitly specify the number of bits in the network address.
177When
178.Dq Li /bits
179is not specified, 128 is used.
180Note that when the number of bits is specified using
181.Dq Li /bits
182notation, the value of the address still includes all bits supplied
183in the external representation, even those bits which are the host
184part of an Internet address.
185.Sh SEE ALSO
186.Xr htonl 3 ,
187.Xr inet_pton 3 ,
188.Xr inet 4 ,
189.Xr hosts 5
190.Sh HISTORY
191The
192.Nm inet_net_ntop
193and
194.Nm inet_net_pton
195functions first appeared in BIND 4.9.4.
diff --git a/src/lib/libc/net/inet_net_ntop.c b/src/lib/libc/net/inet_net_ntop.c
deleted file mode 100644
index 1f1c4bed12..0000000000
--- a/src/lib/libc/net/inet_net_ntop.c
+++ /dev/null
@@ -1,160 +0,0 @@
1/* $OpenBSD: inet_net_ntop.c,v 1.9 2019/07/03 03:24:04 deraadt Exp $ */
2
3/*
4 * Copyright (c) 2012 by Gilles Chehade <gilles@openbsd.org>
5 * Copyright (c) 1996 by Internet Software Consortium.
6 *
7 * Permission to use, copy, modify, and distribute this software for any
8 * purpose with or without fee is hereby granted, provided that the above
9 * copyright notice and this permission notice appear in all copies.
10 *
11 * THE SOFTWARE IS PROVIDED "AS IS" AND INTERNET SOFTWARE CONSORTIUM DISCLAIMS
12 * ALL WARRANTIES WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES
13 * OF MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL INTERNET SOFTWARE
14 * CONSORTIUM BE LIABLE FOR ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL
15 * DAMAGES OR ANY DAMAGES WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR
16 * PROFITS, WHETHER IN AN ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS
17 * ACTION, ARISING OUT OF OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS
18 * SOFTWARE.
19 */
20
21#include <sys/types.h>
22#include <sys/socket.h>
23#include <netinet/in.h>
24#include <arpa/inet.h>
25
26#include <errno.h>
27#include <stdio.h>
28#include <string.h>
29#include <stdlib.h>
30
31static char *inet_net_ntop_ipv4(const u_char *, int, char *, size_t);
32static char *inet_net_ntop_ipv6(const u_char *, int, char *, size_t);
33
34/*
35 * char *
36 * inet_net_ntop(af, src, bits, dst, size)
37 * convert network number from network to presentation format.
38 * generates CIDR style result always.
39 * return:
40 * pointer to dst, or NULL if an error occurred (check errno).
41 * author:
42 * Paul Vixie (ISC), July 1996
43 */
44char *
45inet_net_ntop(int af, const void *src, int bits, char *dst, size_t size)
46{
47 switch (af) {
48 case AF_INET:
49 return (inet_net_ntop_ipv4(src, bits, dst, size));
50 case AF_INET6:
51 return (inet_net_ntop_ipv6(src, bits, dst, size));
52 default:
53 errno = EAFNOSUPPORT;
54 return (NULL);
55 }
56}
57
58/*
59 * static char *
60 * inet_net_ntop_ipv4(src, bits, dst, size)
61 * convert IPv4 network number from network to presentation format.
62 * generates CIDR style result always.
63 * return:
64 * pointer to dst, or NULL if an error occurred (check errno).
65 * note:
66 * network byte order assumed. this means 192.5.5.240/28 has
67 * 0x11110000 in its fourth octet.
68 * author:
69 * Paul Vixie (ISC), July 1996
70 */
71static char *
72inet_net_ntop_ipv4(const u_char *src, int bits, char *dst, size_t size)
73{
74 char *odst = dst;
75 u_int m;
76 int b;
77 char *ep;
78 int advance;
79
80 ep = dst + size;
81 if (ep <= dst)
82 goto emsgsize;
83
84 if (bits < 0 || bits > 32) {
85 errno = EINVAL;
86 return (NULL);
87 }
88 if (bits == 0) {
89 if (ep - dst < sizeof "0")
90 goto emsgsize;
91 *dst++ = '0';
92 *dst = '\0';
93 }
94
95 /* Format whole octets. */
96 for (b = bits / 8; b > 0; b--) {
97 if (ep - dst < sizeof "255.")
98 goto emsgsize;
99 advance = snprintf(dst, ep - dst, "%u", *src++);
100 if (advance <= 0 || advance >= ep - dst)
101 goto emsgsize;
102 dst += advance;
103 if (b > 1) {
104 if (dst + 1 >= ep)
105 goto emsgsize;
106 *dst++ = '.';
107 *dst = '\0';
108 }
109 }
110
111 /* Format partial octet. */
112 b = bits % 8;
113 if (b > 0) {
114 if (ep - dst < sizeof ".255")
115 goto emsgsize;
116 if (dst != odst)
117 *dst++ = '.';
118 m = ((1 << b) - 1) << (8 - b);
119 advance = snprintf(dst, ep - dst, "%u", *src & m);
120 if (advance <= 0 || advance >= ep - dst)
121 goto emsgsize;
122 dst += advance;
123 }
124
125 /* Format CIDR /width. */
126 if (ep - dst < sizeof "/32")
127 goto emsgsize;
128 advance = snprintf(dst, ep - dst, "/%u", bits);
129 if (advance <= 0 || advance >= ep - dst)
130 goto emsgsize;
131 dst += advance;
132 return (odst);
133
134 emsgsize:
135 errno = EMSGSIZE;
136 return (NULL);
137}
138
139static char *
140inet_net_ntop_ipv6(const u_char *src, int bits, char *dst, size_t size)
141{
142 int ret;
143 char buf[sizeof("xxxx:xxxx:xxxx:xxxx:xxxx:xxxx:255:255:255:255/128")];
144
145 if (bits < 0 || bits > 128) {
146 errno = EINVAL;
147 return (NULL);
148 }
149
150 if (inet_ntop(AF_INET6, src, buf, size) == NULL)
151 return (NULL);
152
153 ret = snprintf(dst, size, "%s/%d", buf, bits);
154 if (ret < 0 || ret >= size) {
155 errno = EMSGSIZE;
156 return (NULL);
157 }
158
159 return (dst);
160}
diff --git a/src/lib/libc/net/inet_net_pton.c b/src/lib/libc/net/inet_net_pton.c
deleted file mode 100644
index c5baa4d6f4..0000000000
--- a/src/lib/libc/net/inet_net_pton.c
+++ /dev/null
@@ -1,246 +0,0 @@
1/* $OpenBSD: inet_net_pton.c,v 1.14 2022/12/27 17:10:06 jmc Exp $ */
2
3/*
4 * Copyright (c) 2012 by Gilles Chehade <gilles@openbsd.org>
5 * Copyright (c) 1996,1999 by Internet Software Consortium.
6 *
7 * Permission to use, copy, modify, and distribute this software for any
8 * purpose with or without fee is hereby granted, provided that the above
9 * copyright notice and this permission notice appear in all copies.
10 *
11 * THE SOFTWARE IS PROVIDED "AS IS" AND INTERNET SOFTWARE CONSORTIUM DISCLAIMS
12 * ALL WARRANTIES WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES
13 * OF MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL INTERNET SOFTWARE
14 * CONSORTIUM BE LIABLE FOR ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL
15 * DAMAGES OR ANY DAMAGES WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR
16 * PROFITS, WHETHER IN AN ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS
17 * ACTION, ARISING OUT OF OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS
18 * SOFTWARE.
19 */
20
21#include <sys/types.h>
22#include <sys/socket.h>
23#include <netinet/in.h>
24#include <arpa/inet.h>
25
26#include <assert.h>
27#include <ctype.h>
28#include <errno.h>
29#include <stdio.h>
30#include <string.h>
31#include <stdlib.h>
32
33static int inet_net_pton_ipv4(const char *, u_char *, size_t);
34static int inet_net_pton_ipv6(const char *, u_char *, size_t);
35
36/*
37 * static int
38 * inet_net_pton(af, src, dst, size)
39 * convert network number from presentation to network format.
40 * accepts hex octets, hex strings, decimal octets, and /CIDR.
41 * "size" is in bytes and describes "dst".
42 * return:
43 * number of bits, either imputed classfully or specified with /CIDR,
44 * or -1 if some failure occurred (check errno). ENOENT means it was
45 * not a valid network specification.
46 * author:
47 * Paul Vixie (ISC), June 1996
48 */
49int
50inet_net_pton(int af, const char *src, void *dst, size_t size)
51{
52 switch (af) {
53 case AF_INET:
54 return (inet_net_pton_ipv4(src, dst, size));
55 case AF_INET6:
56 return (inet_net_pton_ipv6(src, dst, size));
57 default:
58 errno = EAFNOSUPPORT;
59 return (-1);
60 }
61}
62
63/*
64 * static int
65 * inet_net_pton_ipv4(src, dst, size)
66 * convert IPv4 network number from presentation to network format.
67 * accepts hex octets, hex strings, decimal octets, and /CIDR.
68 * "size" is in bytes and describes "dst".
69 * return:
70 * number of bits, either imputed classfully or specified with /CIDR,
71 * or -1 if some failure occurred (check errno). ENOENT means it was
72 * not an IPv4 network specification.
73 * note:
74 * network byte order assumed. this means 192.5.5.240/28 has
75 * 0x11110000 in its fourth octet.
76 * author:
77 * Paul Vixie (ISC), June 1996
78 */
79static int
80inet_net_pton_ipv4(const char *src, u_char *dst, size_t size)
81{
82 static const char
83 xdigits[] = "0123456789abcdef",
84 digits[] = "0123456789";
85 int n, ch, tmp, dirty, bits;
86 const u_char *odst = dst;
87
88 ch = (unsigned char)*src++;
89 if (ch == '0' && (src[0] == 'x' || src[0] == 'X')
90 && isascii((unsigned char)src[1]) && isxdigit((unsigned char)src[1])) {
91 /* Hexadecimal: Eat nybble string. */
92 if (size == 0)
93 goto emsgsize;
94 tmp = 0, dirty = 0;
95 src++; /* skip x or X. */
96 while ((ch = (unsigned char)*src++) != '\0' &&
97 isascii(ch) && isxdigit(ch)) {
98 if (isupper(ch))
99 ch = tolower(ch);
100 n = strchr(xdigits, ch) - xdigits;
101 assert(n >= 0 && n <= 15);
102 if (dirty == 0)
103 tmp = n;
104 else
105 tmp = (tmp << 4) | n;
106 if (++dirty == 2) {
107 if (size-- == 0)
108 goto emsgsize;
109 *dst++ = (u_char) tmp;
110 dirty = 0;
111 }
112 }
113 if (dirty) { /* Odd trailing nybble? */
114 if (size-- == 0)
115 goto emsgsize;
116 *dst++ = (u_char) (tmp << 4);
117 }
118 } else if (isascii(ch) && isdigit(ch)) {
119 /* Decimal: eat dotted digit string. */
120 for (;;) {
121 tmp = 0;
122 do {
123 n = strchr(digits, ch) - digits;
124 assert(n >= 0 && n <= 9);
125 tmp *= 10;
126 tmp += n;
127 if (tmp > 255)
128 goto enoent;
129 } while ((ch = (unsigned char)*src++) != '\0' &&
130 isascii(ch) && isdigit(ch));
131 if (size-- == 0)
132 goto emsgsize;
133 *dst++ = (u_char) tmp;
134 if (ch == '\0' || ch == '/')
135 break;
136 if (ch != '.')
137 goto enoent;
138 ch = (unsigned char)*src++;
139 if (!isascii(ch) || !isdigit(ch))
140 goto enoent;
141 }
142 } else
143 goto enoent;
144
145 bits = -1;
146 if (ch == '/' && isascii((unsigned char)src[0]) &&
147 isdigit((unsigned char)src[0]) && dst > odst) {
148 /* CIDR width specifier. Nothing can follow it. */
149 ch = (unsigned char)*src++; /* Skip over the /. */
150 bits = 0;
151 do {
152 n = strchr(digits, ch) - digits;
153 assert(n >= 0 && n <= 9);
154 bits *= 10;
155 bits += n;
156 if (bits > 32)
157 goto emsgsize;
158 } while ((ch = (unsigned char)*src++) != '\0' &&
159 isascii(ch) && isdigit(ch));
160 if (ch != '\0')
161 goto enoent;
162 }
163
164 /* Fiery death and destruction unless we prefetched EOS. */
165 if (ch != '\0')
166 goto enoent;
167
168 /* If nothing was written to the destination, we found no address. */
169 if (dst == odst)
170 goto enoent;
171 /* If no CIDR spec was given, infer width from net class. */
172 if (bits == -1) {
173 if (*odst >= 240) /* Class E */
174 bits = 32;
175 else if (*odst >= 224) /* Class D */
176 bits = 4;
177 else if (*odst >= 192) /* Class C */
178 bits = 24;
179 else if (*odst >= 128) /* Class B */
180 bits = 16;
181 else /* Class A */
182 bits = 8;
183 /* If imputed mask is narrower than specified octets, widen. */
184 if (bits < ((dst - odst) * 8))
185 bits = (dst - odst) * 8;
186 }
187 /* Extend network to cover the actual mask. */
188 while (bits > ((dst - odst) * 8)) {
189 if (size-- == 0)
190 goto emsgsize;
191 *dst++ = '\0';
192 }
193 return (bits);
194
195 enoent:
196 errno = ENOENT;
197 return (-1);
198
199 emsgsize:
200 errno = EMSGSIZE;
201 return (-1);
202}
203
204
205static int
206inet_net_pton_ipv6(const char *src, u_char *dst, size_t size)
207{
208 struct in6_addr in6;
209 int ret;
210 int bits;
211 size_t bytes;
212 char buf[INET6_ADDRSTRLEN + sizeof("/128")];
213 char *sep;
214 const char *errstr;
215
216 if (strlcpy(buf, src, sizeof buf) >= sizeof buf) {
217 errno = EMSGSIZE;
218 return (-1);
219 }
220
221 sep = strchr(buf, '/');
222 if (sep != NULL)
223 *sep++ = '\0';
224
225 ret = inet_pton(AF_INET6, buf, &in6);
226 if (ret != 1)
227 return (-1);
228
229 if (sep == NULL)
230 bits = 128;
231 else {
232 bits = strtonum(sep, 0, 128, &errstr);
233 if (errstr) {
234 errno = EINVAL;
235 return (-1);
236 }
237 }
238
239 bytes = (bits + 7) / 8;
240 if (bytes > size) {
241 errno = EMSGSIZE;
242 return (-1);
243 }
244 memcpy(dst, &in6.s6_addr, bytes);
245 return (bits);
246}
diff --git a/src/lib/libc/net/inet_neta.c b/src/lib/libc/net/inet_neta.c
deleted file mode 100644
index e3e7d0eb71..0000000000
--- a/src/lib/libc/net/inet_neta.c
+++ /dev/null
@@ -1,80 +0,0 @@
1/* $OpenBSD: inet_neta.c,v 1.7 2005/08/06 20:30:03 espie Exp $ */
2
3/*
4 * Copyright (c) 1996 by Internet Software Consortium.
5 *
6 * Permission to use, copy, modify, and distribute this software for any
7 * purpose with or without fee is hereby granted, provided that the above
8 * copyright notice and this permission notice appear in all copies.
9 *
10 * THE SOFTWARE IS PROVIDED "AS IS" AND INTERNET SOFTWARE CONSORTIUM DISCLAIMS
11 * ALL WARRANTIES WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES
12 * OF MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL INTERNET SOFTWARE
13 * CONSORTIUM BE LIABLE FOR ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL
14 * DAMAGES OR ANY DAMAGES WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR
15 * PROFITS, WHETHER IN AN ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS
16 * ACTION, ARISING OUT OF OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS
17 * SOFTWARE.
18 */
19
20#include <sys/types.h>
21#include <sys/socket.h>
22#include <netinet/in.h>
23#include <arpa/inet.h>
24
25#include <errno.h>
26#include <stdio.h>
27#include <string.h>
28
29/*
30 * char *
31 * inet_neta(src, dst, size)
32 * format an in_addr_t network number into presentation format.
33 * return:
34 * pointer to dst, or NULL if an error occurred (check errno).
35 * note:
36 * format of ``src'' is as for inet_network().
37 * author:
38 * Paul Vixie (ISC), July 1996
39 */
40char *
41inet_neta(in_addr_t src, char *dst, size_t size)
42{
43 char *odst = dst;
44 char *ep;
45 int advance;
46
47 if (src == 0x00000000) {
48 if (size < sizeof "0.0.0.0")
49 goto emsgsize;
50 strlcpy(dst, "0.0.0.0", size);
51 return dst;
52 }
53 ep = dst + size;
54 if (ep <= dst)
55 goto emsgsize;
56 while (src & 0xffffffff) {
57 u_char b = (src & 0xff000000) >> 24;
58
59 src <<= 8;
60 if (b || src) {
61 if (ep - dst < sizeof "255.")
62 goto emsgsize;
63 advance = snprintf(dst, ep - dst, "%u", b);
64 if (advance <= 0 || advance >= ep - dst)
65 goto emsgsize;
66 dst += advance;
67 if (src != 0L) {
68 if (dst + 1 >= ep)
69 goto emsgsize;
70 *dst++ = '.';
71 *dst = '\0';
72 }
73 }
74 }
75 return (odst);
76
77 emsgsize:
78 errno = EMSGSIZE;
79 return (NULL);
80}
diff --git a/src/lib/libc/net/inet_netof.c b/src/lib/libc/net/inet_netof.c
deleted file mode 100644
index 4efceede13..0000000000
--- a/src/lib/libc/net/inet_netof.c
+++ /dev/null
@@ -1,49 +0,0 @@
1/* $OpenBSD: inet_netof.c,v 1.7 2015/01/16 16:48:51 deraadt Exp $ */
2/*
3 * Copyright (c) 1983, 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. Neither the name of the University nor the names of its contributors
15 * may be used to endorse or promote products derived from this software
16 * without specific prior written permission.
17 *
18 * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
19 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
20 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
21 * ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
22 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
23 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
24 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
25 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
26 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
27 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
28 * SUCH DAMAGE.
29 */
30
31#include <netinet/in.h>
32#include <arpa/inet.h>
33
34/*
35 * Return the network number from an internet
36 * address; handles class a/b/c network #'s.
37 */
38in_addr_t
39inet_netof(struct in_addr in)
40{
41 in_addr_t i = ntohl(in.s_addr);
42
43 if (IN_CLASSA(i))
44 return (((i)&IN_CLASSA_NET) >> IN_CLASSA_NSHIFT);
45 else if (IN_CLASSB(i))
46 return (((i)&IN_CLASSB_NET) >> IN_CLASSB_NSHIFT);
47 else
48 return (((i)&IN_CLASSC_NET) >> IN_CLASSC_NSHIFT);
49}
diff --git a/src/lib/libc/net/inet_network.c b/src/lib/libc/net/inet_network.c
deleted file mode 100644
index 7b0cf50e79..0000000000
--- a/src/lib/libc/net/inet_network.c
+++ /dev/null
@@ -1,85 +0,0 @@
1/* $OpenBSD: inet_network.c,v 1.13 2015/10/22 23:55:51 mmcc Exp $ */
2/*
3 * Copyright (c) 1983, 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. Neither the name of the University nor the names of its contributors
15 * may be used to endorse or promote products derived from this software
16 * without specific prior written permission.
17 *
18 * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
19 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
20 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
21 * ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
22 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
23 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
24 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
25 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
26 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
27 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
28 * SUCH DAMAGE.
29 */
30
31#include <sys/types.h>
32#include <netinet/in.h>
33#include <arpa/inet.h>
34#include <ctype.h>
35
36/*
37 * Internet network address interpretation routine.
38 * The library routines call this routine to interpret
39 * network numbers.
40 */
41in_addr_t
42inet_network(const char *cp)
43{
44 in_addr_t val, base, n;
45 u_char c;
46 in_addr_t parts[4], *pp = parts;
47 int i;
48
49again:
50 val = 0; base = 10;
51 if (*cp == '0')
52 base = 8, cp++;
53 if (*cp == 'x' || *cp == 'X')
54 base = 16, cp++;
55 while ((c = *cp)) {
56 if (isdigit(c)) {
57 val = (val * base) + (c - '0');
58 cp++;
59 continue;
60 }
61 if (base == 16 && isxdigit(c)) {
62 val = (val << 4) + (c + 10 - (islower(c) ? 'a' : 'A'));
63 cp++;
64 continue;
65 }
66 break;
67 }
68 if (*cp == '.') {
69 if (pp >= parts + 3)
70 return (INADDR_NONE);
71 *pp++ = val, cp++;
72 goto again;
73 }
74 if (*cp && !isspace((unsigned char)*cp))
75 return (INADDR_NONE);
76 *pp++ = val;
77 n = pp - parts;
78 for (val = 0, i = 0; i < 4; i++) {
79 val <<= 8;
80 if (i < n)
81 val |= parts[i] & 0xff;
82 }
83 return (val);
84}
85DEF_WEAK(inet_network);
diff --git a/src/lib/libc/net/inet_ntoa.c b/src/lib/libc/net/inet_ntoa.c
deleted file mode 100644
index ff5d93ded2..0000000000
--- a/src/lib/libc/net/inet_ntoa.c
+++ /dev/null
@@ -1,51 +0,0 @@
1/* $OpenBSD: inet_ntoa.c,v 1.6 2005/08/06 20:30:03 espie Exp $ */
2/*
3 * Copyright (c) 1983, 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. Neither the name of the University nor the names of its contributors
15 * may be used to endorse or promote products derived from this software
16 * without specific prior written permission.
17 *
18 * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
19 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
20 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
21 * ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
22 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
23 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
24 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
25 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
26 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
27 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
28 * SUCH DAMAGE.
29 */
30
31/*
32 * Convert network-format internet address
33 * to base 256 d.d.d.d representation.
34 */
35#include <sys/types.h>
36#include <netinet/in.h>
37#include <arpa/inet.h>
38#include <stdio.h>
39
40char *
41inet_ntoa(struct in_addr in)
42{
43 static char b[18];
44 char *p;
45
46 p = (char *)&in;
47#define UC(b) (((int)b)&0xff)
48 (void)snprintf(b, sizeof(b),
49 "%u.%u.%u.%u", UC(p[0]), UC(p[1]), UC(p[2]), UC(p[3]));
50 return (b);
51}
diff --git a/src/lib/libc/net/inet_ntop.3 b/src/lib/libc/net/inet_ntop.3
deleted file mode 100644
index e5c1c574f8..0000000000
--- a/src/lib/libc/net/inet_ntop.3
+++ /dev/null
@@ -1,213 +0,0 @@
1.\" $OpenBSD: inet_ntop.3,v 1.6 2022/09/11 06:38:10 jmc Exp $
2.\" $NetBSD: inet.3,v 1.7 1997/06/18 02:25:24 lukem Exp $
3.\"
4.\" Copyright (c) 1983, 1990, 1991, 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. Neither the name of the University nor the names of its contributors
16.\" may be used to endorse or promote products derived from this software
17.\" without specific prior written permission.
18.\"
19.\" THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
20.\" ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
21.\" IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
22.\" ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
23.\" FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
24.\" DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
25.\" OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
26.\" HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
27.\" LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
28.\" OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
29.\" SUCH DAMAGE.
30.\"
31.\" @(#)inet.3 8.1 (Berkeley) 6/4/93
32.\"
33.Dd $Mdocdate: September 11 2022 $
34.Dt INET_NTOP 3
35.Os
36.Sh NAME
37.Nm inet_ntop ,
38.Nm inet_pton
39.Nd convert Internet addresses between presentation and network formats
40.Sh SYNOPSIS
41.In sys/socket.h
42.In arpa/inet.h
43.Ft const char *
44.Fn inet_ntop "int af" "const void * restrict src" "char * restrict dst" "socklen_t size"
45.Ft int
46.Fn inet_pton "int af" "const char * restrict src" "void * restrict dst"
47.Sh DESCRIPTION
48The
49.Fn inet_pton
50function converts a presentation format address (that is, printable form
51as held in a character string) to network format (usually a
52.Vt struct in_addr
53or some other internal binary representation, in network byte order).
54It returns 1 if the address was valid for the specified address family;
550 if the address wasn't parseable in the specified address family; or \-1
56if some system error occurred (in which case
57.Va errno
58will have been set).
59This function is presently valid for
60.Dv AF_INET
61and
62.Dv AF_INET6 .
63.Pp
64The function
65.Fn inet_ntop
66converts an address from network format to presentation format.
67It returns
68.Dv NULL
69if a system
70error occurs (in which case,
71.Va errno
72will have been set), or it returns a pointer to the destination string.
73.Pp
74All Internet addresses are returned in network
75order (bytes ordered from left to right).
76.Sh INTERNET ADDRESSES (IP VERSION 4)
77Values must be specified using the standard dot notation:
78.Bd -literal -offset indent
79a.b.c.d
80.Ed
81.Pp
82All four parts must be decimal numbers between 0 and 255, inclusive,
83and are assigned, from left to right,
84to the four bytes of an Internet address.
85Note that when an Internet address is viewed as a 32-bit integer
86quantity on a system that uses little-endian byte order
87(such as AMD64 or ARM processors)
88the bytes referred to above appear as
89.Dq Li d.c.b.a .
90That is, little-endian bytes are ordered from right to left.
91.Sh INTERNET ADDRESSES (IP VERSION 6)
92In order to support scoped IPv6 addresses,
93.Xr getaddrinfo 3
94and
95.Xr getnameinfo 3
96are recommended rather than the functions presented here.
97.Pp
98The presentation format of an IPv6 address is given in RFC 4291:
99.Pp
100There are three conventional forms for representing IPv6 addresses as
101text strings:
102.Bl -enum
103.It
104The preferred form is x:x:x:x:x:x:x:x, where the 'x's are the
105hexadecimal values of the eight 16-bit pieces of the address.
106Examples:
107.Bd -literal -offset indent
108FEDC:BA98:7654:3210:FEDC:BA98:7654:3210
1091080:0:0:0:8:800:200C:417A
110.Ed
111.Pp
112Note that it is not necessary to write the leading zeros in an
113individual field, but there must be at least one numeral in
114every field (except for the case described in 2.).
115.It
116Due to the method of allocating certain styles of IPv6
117addresses, it will be common for addresses to contain long
118strings of zero bits.
119In order to make writing addresses
120containing zero bits easier, a special syntax is available to
121compress the zeros.
122The use of
123.Dq \&:\&:
124indicates multiple groups
125of 16 bits of zeros.
126The
127.Dq \&:\&:
128can only appear once in an
129address.
130The
131.Dq \&:\&:
132can also be used to compress the leading and/or trailing zeros in an address.
133.Pp
134For example the following addresses:
135.Bd -literal -offset indent
1361080:0:0:0:8:800:200C:417A a unicast address
137FF01:0:0:0:0:0:0:43 a multicast address
1380:0:0:0:0:0:0:1 the loopback address
1390:0:0:0:0:0:0:0 the unspecified addresses
140.Ed
141.Pp
142may be represented as:
143.Bd -literal -offset indent
1441080::8:800:200C:417A a unicast address
145FF01::43 a multicast address
146::1 the loopback address
147:: the unspecified addresses
148.Ed
149.It
150An alternative form that is sometimes more convenient when
151dealing with a mixed environment of IPv4 and IPv6 nodes is
152x:x:x:x:x:x:d.d.d.d, where the 'x's are the hexadecimal values
153of the six high-order 16-bit pieces of the address, and the 'd's
154are the decimal values of the four low-order 8-bit pieces of the
155address (standard IPv4 representation).
156Examples:
157.Bd -literal -offset indent
1580:0:0:0:0:0:13.1.68.3
1590:0:0:0:0:FFFF:129.144.52.38
160.Ed
161.Pp
162or in compressed form:
163.Bd -literal -offset indent
164::13.1.68.3
165::FFFF:129.144.52.38
166.Ed
167.El
168.Sh SEE ALSO
169.Xr gethostbyname 3 ,
170.Xr inet_addr 3 ,
171.Xr inet_net_ntop 3 ,
172.Xr hosts 5
173.Sh STANDARDS
174The
175.Nm inet_ntop
176and
177.Nm inet_pton
178functions conform to the IETF IPv6 BSD API and address formatting
179specifications, as well as
180.St -p1003.1-2008 .
181.Sh HISTORY
182The
183.Nm inet_pton
184and
185.Nm inet_ntop
186functions appeared in BIND 4.9.4.
187.Sh CAVEATS
188Note that
189.Nm inet_pton
190does not accept 1-, 2-, or 3-part dotted addresses;
191all four parts must be specified and must be in decimal
192(and not octal or hexadecimal).
193This is a narrower input set than that accepted by
194.Nm inet_aton .
195.Pp
196.Rs
197.%A R. Gilligan
198.%A S. Thomson
199.%A J. Bound
200.%A J. McCann
201.%A W. Stevens
202.%D February 2003
203.%R RFC 3493
204.%T Basic Socket Interface Extensions for IPv6
205.Re
206.Pp
207.Rs
208.%A R. Hinden
209.%A S. Deering
210.%D February 2006
211.%R RFC 4291
212.%T IP Version 6 Addressing Architecture
213.Re
diff --git a/src/lib/libc/net/inet_ntop.c b/src/lib/libc/net/inet_ntop.c
deleted file mode 100644
index 2bb11c2ab2..0000000000
--- a/src/lib/libc/net/inet_ntop.c
+++ /dev/null
@@ -1,205 +0,0 @@
1/* $OpenBSD: inet_ntop.c,v 1.13 2016/09/21 04:38:56 guenther Exp $ */
2
3/* Copyright (c) 1996 by Internet Software Consortium.
4 *
5 * Permission to use, copy, modify, and distribute this software for any
6 * purpose with or without fee is hereby granted, provided that the above
7 * copyright notice and this permission notice appear in all copies.
8 *
9 * THE SOFTWARE IS PROVIDED "AS IS" AND INTERNET SOFTWARE CONSORTIUM DISCLAIMS
10 * ALL WARRANTIES WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES
11 * OF MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL INTERNET SOFTWARE
12 * CONSORTIUM BE LIABLE FOR ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL
13 * DAMAGES OR ANY DAMAGES WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR
14 * PROFITS, WHETHER IN AN ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS
15 * ACTION, ARISING OUT OF OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS
16 * SOFTWARE.
17 */
18
19#include <sys/types.h>
20#include <sys/socket.h>
21#include <netinet/in.h>
22#include <arpa/inet.h>
23#include <arpa/nameser.h>
24#include <string.h>
25#include <errno.h>
26#include <stdio.h>
27
28/*
29 * WARNING: Don't even consider trying to compile this on a system where
30 * sizeof(int) < 4. sizeof(int) > 4 is fine; all the world's not a VAX.
31 */
32
33static const char *inet_ntop4(const u_char *src, char *dst, size_t size);
34static const char *inet_ntop6(const u_char *src, char *dst, size_t size);
35
36/* const char *
37 * inet_ntop(af, src, dst, size)
38 * convert a network format address to presentation format.
39 * return:
40 * pointer to presentation format address (`dst'), or NULL (see errno).
41 * author:
42 * Paul Vixie, 1996.
43 */
44const char *
45inet_ntop(int af, const void *src, char *dst, socklen_t size)
46{
47 switch (af) {
48 case AF_INET:
49 return (inet_ntop4(src, dst, size));
50 case AF_INET6:
51 return (inet_ntop6(src, dst, size));
52 default:
53 errno = EAFNOSUPPORT;
54 return (NULL);
55 }
56 /* NOTREACHED */
57}
58DEF_WEAK(inet_ntop);
59
60/* const char *
61 * inet_ntop4(src, dst, size)
62 * format an IPv4 address, more or less like inet_ntoa()
63 * return:
64 * `dst' (as a const)
65 * notes:
66 * (1) uses no statics
67 * (2) takes a u_char* not an in_addr as input
68 * author:
69 * Paul Vixie, 1996.
70 */
71static const char *
72inet_ntop4(const u_char *src, char *dst, size_t size)
73{
74 char tmp[sizeof "255.255.255.255"];
75 int l;
76
77 l = snprintf(tmp, sizeof(tmp), "%u.%u.%u.%u",
78 src[0], src[1], src[2], src[3]);
79 if (l <= 0 || l >= size) {
80 errno = ENOSPC;
81 return (NULL);
82 }
83 strlcpy(dst, tmp, size);
84 return (dst);
85}
86
87/* const char *
88 * inet_ntop6(src, dst, size)
89 * convert IPv6 binary address into presentation (printable) format
90 * author:
91 * Paul Vixie, 1996.
92 */
93static const char *
94inet_ntop6(const u_char *src, char *dst, size_t size)
95{
96 /*
97 * Note that int32_t and int16_t need only be "at least" large enough
98 * to contain a value of the specified size. On some systems, like
99 * Crays, there is no such thing as an integer variable with 16 bits.
100 * Keep this in mind if you think this function should have been coded
101 * to use pointer overlays. All the world's not a VAX.
102 */
103 char tmp[sizeof "ffff:ffff:ffff:ffff:ffff:ffff:255.255.255.255"];
104 char *tp, *ep;
105 struct { int base, len; } best, cur;
106 u_int words[IN6ADDRSZ / INT16SZ];
107 int i;
108 int advance;
109
110 /*
111 * Preprocess:
112 * Copy the input (bytewise) array into a wordwise array.
113 * Find the longest run of 0x00's in src[] for :: shorthanding.
114 */
115 memset(words, '\0', sizeof words);
116 for (i = 0; i < IN6ADDRSZ; i++)
117 words[i / 2] |= (src[i] << ((1 - (i % 2)) << 3));
118 best.base = -1;
119 cur.base = -1;
120 for (i = 0; i < (IN6ADDRSZ / INT16SZ); i++) {
121 if (words[i] == 0) {
122 if (cur.base == -1)
123 cur.base = i, cur.len = 1;
124 else
125 cur.len++;
126 } else {
127 if (cur.base != -1) {
128 if (best.base == -1 || cur.len > best.len)
129 best = cur;
130 cur.base = -1;
131 }
132 }
133 }
134 if (cur.base != -1) {
135 if (best.base == -1 || cur.len > best.len)
136 best = cur;
137 }
138 if (best.base != -1 && best.len < 2)
139 best.base = -1;
140
141 /*
142 * Format the result.
143 */
144 tp = tmp;
145 ep = tmp + sizeof(tmp);
146 for (i = 0; i < (IN6ADDRSZ / INT16SZ) && tp < ep; i++) {
147 /* Are we inside the best run of 0x00's? */
148 if (best.base != -1 && i >= best.base &&
149 i < (best.base + best.len)) {
150 if (i == best.base) {
151 if (tp + 1 >= ep) {
152 errno = ENOSPC;
153 return (NULL);
154 }
155 *tp++ = ':';
156 }
157 continue;
158 }
159 /* Are we following an initial run of 0x00s or any real hex? */
160 if (i != 0) {
161 if (tp + 1 >= ep) {
162 errno = ENOSPC;
163 return (NULL);
164 }
165 *tp++ = ':';
166 }
167 /* Is this address an encapsulated IPv4? */
168 if (i == 6 && best.base == 0 &&
169 (best.len == 6 || (best.len == 5 && words[5] == 0xffff))) {
170 if (!inet_ntop4(src+12, tp, ep - tp))
171 return (NULL);
172 tp += strlen(tp);
173 break;
174 }
175 advance = snprintf(tp, ep - tp, "%x", words[i]);
176 if (advance <= 0 || advance >= ep - tp) {
177 errno = ENOSPC;
178 return (NULL);
179 }
180 tp += advance;
181 }
182 /* Was it a trailing run of 0x00's? */
183 if (best.base != -1 && (best.base + best.len) == (IN6ADDRSZ / INT16SZ)) {
184 if (tp + 1 >= ep) {
185 errno = ENOSPC;
186 return (NULL);
187 }
188 *tp++ = ':';
189 }
190 if (tp + 1 >= ep) {
191 errno = ENOSPC;
192 return (NULL);
193 }
194 *tp++ = '\0';
195
196 /*
197 * Check for overflow, copy, and we're done.
198 */
199 if ((size_t)(tp - tmp) > size) {
200 errno = ENOSPC;
201 return (NULL);
202 }
203 strlcpy(dst, tmp, size);
204 return (dst);
205}
diff --git a/src/lib/libc/net/inet_pton.c b/src/lib/libc/net/inet_pton.c
deleted file mode 100644
index 45bafd2ffc..0000000000
--- a/src/lib/libc/net/inet_pton.c
+++ /dev/null
@@ -1,213 +0,0 @@
1/* $OpenBSD: inet_pton.c,v 1.11 2024/09/03 17:05:59 deraadt Exp $ */
2
3/* Copyright (c) 1996 by Internet Software Consortium.
4 *
5 * Permission to use, copy, modify, and distribute this software for any
6 * purpose with or without fee is hereby granted, provided that the above
7 * copyright notice and this permission notice appear in all copies.
8 *
9 * THE SOFTWARE IS PROVIDED "AS IS" AND INTERNET SOFTWARE CONSORTIUM DISCLAIMS
10 * ALL WARRANTIES WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES
11 * OF MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL INTERNET SOFTWARE
12 * CONSORTIUM BE LIABLE FOR ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL
13 * DAMAGES OR ANY DAMAGES WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR
14 * PROFITS, WHETHER IN AN ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS
15 * ACTION, ARISING OUT OF OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS
16 * SOFTWARE.
17 */
18
19#include <sys/types.h>
20#include <sys/socket.h>
21#include <netinet/in.h>
22#include <arpa/inet.h>
23#include <arpa/nameser.h>
24#include <string.h>
25#include <errno.h>
26
27/*
28 * WARNING: Don't even consider trying to compile this on a system where
29 * sizeof(int) < 4. sizeof(int) > 4 is fine; all the world's not a VAX.
30 */
31
32static int inet_pton4(const char *src, u_char *dst);
33static int inet_pton6(const char *src, u_char *dst);
34
35/* int
36 * inet_pton(af, src, dst)
37 * convert from presentation format (which usually means ASCII printable)
38 * to network format (which is usually some kind of binary format).
39 * return:
40 * 1 if the address was valid for the specified address family
41 * 0 if the address wasn't valid (`dst' is untouched in this case)
42 * -1 if some other error occurred (`dst' is untouched in this case, too)
43 * author:
44 * Paul Vixie, 1996.
45 */
46int
47inet_pton(int af, const char *src, void *dst)
48{
49 switch (af) {
50 case AF_INET:
51 return (inet_pton4(src, dst));
52 case AF_INET6:
53 return (inet_pton6(src, dst));
54 default:
55 errno = EAFNOSUPPORT;
56 return (-1);
57 }
58 /* NOTREACHED */
59}
60DEF_WEAK(inet_pton);
61
62/* int
63 * inet_pton4(src, dst)
64 * like inet_aton() but without all the hexadecimal and shorthand.
65 * return:
66 * 1 if `src' is a valid dotted quad, else 0.
67 * notice:
68 * does not touch `dst' unless it's returning 1.
69 * author:
70 * Paul Vixie, 1996.
71 */
72static int
73inet_pton4(const char *src, u_char *dst)
74{
75 static const char digits[] = "0123456789";
76 int saw_digit, octets, ch;
77 u_char tmp[INADDRSZ], *tp;
78
79 saw_digit = 0;
80 octets = 0;
81 *(tp = tmp) = 0;
82 while ((ch = *src++) != '\0') {
83 const char *pch;
84
85 if ((pch = strchr(digits, ch)) != NULL) {
86 u_int new = *tp * 10 + (pch - digits);
87
88 if (new > 255)
89 return (0);
90 if (!saw_digit) {
91 if (++octets > 4)
92 return (0);
93 saw_digit = 1;
94 }
95 *tp = new;
96 } else if (ch == '.' && saw_digit) {
97 if (octets == 4)
98 return (0);
99 *++tp = 0;
100 saw_digit = 0;
101 } else
102 return (0);
103 }
104 if (octets < 4)
105 return (0);
106
107 memcpy(dst, tmp, INADDRSZ);
108 return (1);
109}
110
111/* int
112 * inet_pton6(src, dst)
113 * convert presentation level address to network order binary form.
114 * return:
115 * 1 if `src' is a valid [RFC1884 2.2] address, else 0.
116 * notice:
117 * does not touch `dst' unless it's returning 1.
118 * credit:
119 * inspired by Mark Andrews.
120 * author:
121 * Paul Vixie, 1996.
122 */
123static int
124inet_pton6(const char *src, u_char *dst)
125{
126 static const char xdigits_l[] = "0123456789abcdef",
127 xdigits_u[] = "0123456789ABCDEF";
128 u_char tmp[IN6ADDRSZ], *tp, *endp, *colonp;
129 const char *xdigits, *curtok;
130 int ch, saw_xdigit, count_xdigit;
131 u_int val;
132
133 memset((tp = tmp), '\0', IN6ADDRSZ);
134 endp = tp + IN6ADDRSZ;
135 colonp = NULL;
136 /* Leading :: requires some special handling. */
137 if (*src == ':')
138 if (*++src != ':')
139 return (0);
140 curtok = src;
141 saw_xdigit = count_xdigit = 0;
142 val = 0;
143 while ((ch = *src++) != '\0') {
144 const char *pch;
145
146 if ((pch = strchr((xdigits = xdigits_l), ch)) == NULL)
147 pch = strchr((xdigits = xdigits_u), ch);
148 if (pch != NULL) {
149 if (count_xdigit >= 4)
150 return (0);
151 val <<= 4;
152 val |= (pch - xdigits);
153 if (val > 0xffff)
154 return (0);
155 saw_xdigit = 1;
156 count_xdigit++;
157 continue;
158 }
159 if (ch == ':') {
160 curtok = src;
161 if (!saw_xdigit) {
162 if (colonp)
163 return (0);
164 colonp = tp;
165 continue;
166 } else if (*src == '\0') {
167 return (0);
168 }
169 if (tp + INT16SZ > endp)
170 return (0);
171 *tp++ = (u_char) (val >> 8) & 0xff;
172 *tp++ = (u_char) val & 0xff;
173 saw_xdigit = 0;
174 count_xdigit = 0;
175 val = 0;
176 continue;
177 }
178 if (ch == '.' && ((tp + INADDRSZ) <= endp) &&
179 inet_pton4(curtok, tp) > 0) {
180 tp += INADDRSZ;
181 saw_xdigit = 0;
182 count_xdigit = 0;
183 break; /* '\0' was seen by inet_pton4(). */
184 }
185 return (0);
186 }
187 if (saw_xdigit) {
188 if (tp + INT16SZ > endp)
189 return (0);
190 *tp++ = (u_char) (val >> 8) & 0xff;
191 *tp++ = (u_char) val & 0xff;
192 }
193 if (colonp != NULL) {
194 /*
195 * Since some memmove()'s erroneously fail to handle
196 * overlapping regions, we'll do the shift by hand.
197 */
198 const int n = tp - colonp;
199 int i;
200
201 if (tp == endp)
202 return (0);
203 for (i = 1; i <= n; i++) {
204 endp[- i] = colonp[n - i];
205 colonp[n - i] = 0;
206 }
207 tp = endp;
208 }
209 if (tp != endp)
210 return (0);
211 memcpy(dst, tmp, IN6ADDRSZ);
212 return (1);
213}
diff --git a/src/lib/libc/net/ip6opt.c b/src/lib/libc/net/ip6opt.c
deleted file mode 100644
index 3837489448..0000000000
--- a/src/lib/libc/net/ip6opt.c
+++ /dev/null
@@ -1,285 +0,0 @@
1/* $OpenBSD: ip6opt.c,v 1.10 2020/01/22 07:52:37 deraadt Exp $ */
2/* $KAME: ip6opt.c,v 1.18 2005/06/15 07:11:35 keiichi Exp $ */
3
4/*
5 * Copyright (C) 1995, 1996, 1997, and 1998 WIDE Project.
6 * All rights reserved.
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. Neither the name of the project 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 PROJECT 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 PROJECT 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
33#include <sys/types.h>
34#include <sys/socket.h>
35
36#include <netinet/in.h>
37#include <netinet/ip6.h>
38
39#include <string.h>
40#include <stdio.h>
41
42static int ip6optlen(u_int8_t *opt, u_int8_t *lim);
43
44/*
45 * Calculate the length of a given IPv6 option. Also checks
46 * if the option is safely stored in user's buffer according to the
47 * calculated length and the limitation of the buffer.
48 */
49static int
50ip6optlen(u_int8_t *opt, u_int8_t *lim)
51{
52 int optlen;
53
54 if (*opt == IP6OPT_PAD1)
55 optlen = 1;
56 else {
57 /* is there enough space to store type and len? */
58 if (opt + 2 > lim)
59 return (0);
60 optlen = *(opt + 1) + 2;
61 }
62 if (opt + optlen <= lim)
63 return (optlen);
64
65 return (0);
66}
67
68/*
69 * The following functions are defined in RFC3542, which is a successor
70 * of RFC2292.
71 */
72
73int
74inet6_opt_init(void *extbuf, socklen_t extlen)
75{
76 struct ip6_ext *ext = (struct ip6_ext *)extbuf;
77
78 if (ext) {
79 if (extlen <= 0 || (extlen % 8))
80 return (-1);
81 ext->ip6e_len = (extlen >> 3) - 1;
82 }
83
84 return (2); /* sizeof the next and the length fields */
85}
86
87int
88inet6_opt_append(void *extbuf, socklen_t extlen, int offset, u_int8_t type,
89 socklen_t len, u_int8_t align, void **databufp)
90{
91 int currentlen = offset, padlen = 0;
92
93 /*
94 * The option type must have a value from 2 to 255, inclusive.
95 * (0 and 1 are reserved for the Pad1 and PadN options, respectively.)
96 */
97#if 0 /* always false */
98 if (type < 2 || type > 255)
99#else
100 if (type < 2)
101#endif
102 return (-1);
103
104 /*
105 * The option data length must have a value between 0 and 255,
106 * inclusive, and is the length of the option data that follows.
107 */
108 if (len > 255)
109 return (-1);
110
111 /*
112 * The align parameter must have a value of 1, 2, 4, or 8.
113 * The align value can not exceed the value of len.
114 */
115 if (align != 1 && align != 2 && align != 4 && align != 8)
116 return (-1);
117 if (align > len)
118 return (-1);
119
120 /* Calculate the padding length. */
121 currentlen += 2 + len; /* 2 means "type + len" */
122 if (currentlen % align)
123 padlen = align - (currentlen % align);
124
125 /* The option must fit in the extension header buffer. */
126 currentlen += padlen;
127 if (extlen && /* XXX: right? */
128 currentlen > extlen)
129 return (-1);
130
131 if (extbuf) {
132 u_int8_t *optp = (u_int8_t *)extbuf + offset;
133
134 if (padlen == 1) {
135 /* insert a Pad1 option */
136 *optp = IP6OPT_PAD1;
137 optp++;
138 } else if (padlen > 0) {
139 /* insert a PadN option for alignment */
140 *optp++ = IP6OPT_PADN;
141 *optp++ = padlen - 2;
142 memset(optp, 0, padlen - 2);
143 optp += (padlen - 2);
144 }
145
146 *optp++ = type;
147 *optp++ = len;
148
149 *databufp = optp;
150 }
151
152 return (currentlen);
153}
154
155int
156inet6_opt_finish(void *extbuf, socklen_t extlen, int offset)
157{
158 int updatelen = offset > 0 ? (1 + ((offset - 1) | 7)) : 0;
159
160 if (extbuf) {
161 u_int8_t *padp;
162 int padlen = updatelen - offset;
163
164 if (updatelen > extlen)
165 return (-1);
166
167 padp = (u_int8_t *)extbuf + offset;
168 if (padlen == 1)
169 *padp = IP6OPT_PAD1;
170 else if (padlen > 0) {
171 *padp++ = IP6OPT_PADN;
172 *padp++ = (padlen - 2);
173 memset(padp, 0, padlen - 2);
174 }
175 }
176
177 return (updatelen);
178}
179
180int
181inet6_opt_set_val(void *databuf, int offset, void *val, socklen_t vallen)
182{
183
184 memcpy((u_int8_t *)databuf + offset, val, vallen);
185 return (offset + vallen);
186}
187
188int
189inet6_opt_next(void *extbuf, socklen_t extlen, int offset, u_int8_t *typep,
190 socklen_t *lenp, void **databufp)
191{
192 u_int8_t *optp, *lim;
193 int optlen;
194
195 /* Validate extlen. XXX: is the variable really necessary?? */
196 if (extlen == 0 || (extlen % 8))
197 return (-1);
198 lim = (u_int8_t *)extbuf + extlen;
199
200 /*
201 * If this is the first time this function called for this options
202 * header, simply return the 1st option.
203 * Otherwise, search the option list for the next option.
204 */
205 if (offset == 0)
206 optp = (u_int8_t *)((struct ip6_hbh *)extbuf + 1);
207 else
208 optp = (u_int8_t *)extbuf + offset;
209
210 /* Find the next option skipping any padding options. */
211 while (optp < lim) {
212 switch(*optp) {
213 case IP6OPT_PAD1:
214 optp++;
215 break;
216 case IP6OPT_PADN:
217 if ((optlen = ip6optlen(optp, lim)) == 0)
218 goto optend;
219 optp += optlen;
220 break;
221 default: /* found */
222 if ((optlen = ip6optlen(optp, lim)) == 0)
223 goto optend;
224 *typep = *optp;
225 *lenp = optlen - 2;
226 *databufp = optp + 2;
227 return (optp + optlen - (u_int8_t *)extbuf);
228 }
229 }
230
231 optend:
232 *databufp = NULL; /* for safety */
233 return (-1);
234}
235
236int
237inet6_opt_find(void *extbuf, socklen_t extlen, int offset, u_int8_t type,
238 socklen_t *lenp, void **databufp)
239{
240 u_int8_t *optp, *lim;
241 int optlen;
242
243 /* Validate extlen. XXX: is the variable really necessary?? */
244 if (extlen == 0 || (extlen % 8))
245 return (-1);
246 lim = (u_int8_t *)extbuf + extlen;
247
248 /*
249 * If this is the first time this function called for this options
250 * header, simply return the 1st option.
251 * Otherwise, search the option list for the next option.
252 */
253 if (offset == 0)
254 optp = (u_int8_t *)((struct ip6_hbh *)extbuf + 1);
255 else
256 optp = (u_int8_t *)extbuf + offset;
257
258 /* Find the specified option */
259 while (optp < lim) {
260 if ((optlen = ip6optlen(optp, lim)) == 0)
261 goto optend;
262
263 if (*optp == type) { /* found */
264 *lenp = optlen - 2;
265 *databufp = optp + 2;
266 return (optp + optlen - (u_int8_t *)extbuf);
267 }
268
269 optp += optlen;
270 }
271
272 optend:
273 *databufp = NULL; /* for safety */
274 return (-1);
275}
276
277int
278inet6_opt_get_val(void *databuf, int offset, void *val, socklen_t vallen)
279{
280
281 /* we can't assume alignment here */
282 memcpy(val, (u_int8_t *)databuf + offset, vallen);
283
284 return (offset + vallen);
285}
diff --git a/src/lib/libc/net/link_ntoa.3 b/src/lib/libc/net/link_ntoa.3
deleted file mode 100644
index 8953677441..0000000000
--- a/src/lib/libc/net/link_ntoa.3
+++ /dev/null
@@ -1,78 +0,0 @@
1.\" $OpenBSD: link_ntoa.3,v 1.2 2015/09/10 10:14:20 jmc 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. 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.Dd $Mdocdate: September 10 2015 $
34.Dt LINK_NTOA 3
35.Os
36.Sh NAME
37.Nm link_ntoa
38.Nd elementary address specification routine for link level access
39.Sh SYNOPSIS
40.In sys/types.h
41.In sys/socket.h
42.In net/if_dl.h
43.Ft char *
44.Fn link_ntoa "const struct sockaddr_dl *sdl"
45.Sh DESCRIPTION
46The
47.Fn link_ntoa
48function takes
49a link-level
50address and returns an
51.Tn ASCII
52string representing some of the information present,
53including the link level address itself, and the interface name
54or number, if present.
55This facility is experimental and is
56still subject to change.
57.Sh RETURN VALUES
58.Fn link_ntoa
59always returns a NUL-terminated string.
60.Sh SEE ALSO
61.Xr ifconfig 8
62.Sh HISTORY
63The
64.Fn link_ntoa
65function appeared in
66.Bx 4.3 Reno .
67.Sh BUGS
68The returned values for
69.Fn link_ntoa
70reside in a static memory area.
71.Pp
72If the
73.Fa sdl_len
74field of the link socket address
75.Fa sdl
76is 0,
77.Fn link_ntoa
78will not insert a colon before the interface address bytes.
diff --git a/src/lib/libc/net/linkaddr.c b/src/lib/libc/net/linkaddr.c
deleted file mode 100644
index 9101e23a86..0000000000
--- a/src/lib/libc/net/linkaddr.c
+++ /dev/null
@@ -1,85 +0,0 @@
1/* $OpenBSD: linkaddr.c,v 1.9 2016/12/08 03:20:50 millert Exp $ */
2/*-
3 * Copyright (c) 1990, 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. Neither the name of the University nor the names of its contributors
15 * may be used to endorse or promote products derived from this software
16 * without specific prior written permission.
17 *
18 * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
19 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
20 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
21 * ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
22 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
23 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
24 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
25 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
26 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
27 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
28 * SUCH DAMAGE.
29 */
30
31#include <sys/types.h>
32#include <sys/socket.h>
33#include <net/if.h>
34#include <net/if_dl.h>
35#include <string.h>
36
37static const char hexlist[] = "0123456789abcdef";
38
39char *
40link_ntoa(const struct sockaddr_dl *sdl)
41{
42 static char obuf[64];
43 char *out;
44 const u_char *in, *inlim;
45 int namelen, i, rem;
46
47 namelen = (sdl->sdl_nlen <= IFNAMSIZ) ? sdl->sdl_nlen : IFNAMSIZ;
48
49 out = obuf;
50 rem = sizeof(obuf);
51 if (namelen > 0) {
52 memcpy(out, sdl->sdl_data, namelen);
53 out += namelen;
54 rem -= namelen;
55 if (sdl->sdl_alen > 0) {
56 *out++ = ':';
57 rem--;
58 }
59 }
60
61 in = (const u_char *)sdl->sdl_data + sdl->sdl_nlen;
62 inlim = in + sdl->sdl_alen;
63
64 while (in < inlim && rem > 1) {
65 if (in != (const u_char *)sdl->sdl_data + sdl->sdl_nlen) {
66 *out++ = '.';
67 rem--;
68 }
69 i = *in++;
70 if (i > 0xf) {
71 if (rem < 3)
72 break;
73 *out++ = hexlist[i >> 4];
74 *out++ = hexlist[i & 0xf];
75 rem -= 2;
76 } else {
77 if (rem < 2)
78 break;
79 *out++ = hexlist[i];
80 rem--;
81 }
82 }
83 *out = 0;
84 return (obuf);
85}
diff --git a/src/lib/libc/net/ntohl.c b/src/lib/libc/net/ntohl.c
deleted file mode 100644
index 7592398e8c..0000000000
--- a/src/lib/libc/net/ntohl.c
+++ /dev/null
@@ -1,15 +0,0 @@
1/* $OpenBSD: ntohl.c,v 1.8 2024/04/15 14:30:48 naddy Exp $ */
2/*
3 * Public domain.
4 */
5
6#include <sys/types.h>
7#include <endian.h>
8
9#undef ntohl
10
11uint32_t
12ntohl(uint32_t x)
13{
14 return be32toh(x);
15}
diff --git a/src/lib/libc/net/ntohs.c b/src/lib/libc/net/ntohs.c
deleted file mode 100644
index ef22ea3068..0000000000
--- a/src/lib/libc/net/ntohs.c
+++ /dev/null
@@ -1,15 +0,0 @@
1/* $OpenBSD: ntohs.c,v 1.10 2024/04/15 14:30:48 naddy Exp $ */
2/*
3 * Public domain.
4 */
5
6#include <sys/types.h>
7#include <endian.h>
8
9#undef ntohs
10
11uint16_t
12ntohs(uint16_t x)
13{
14 return be16toh(x);
15}
diff --git a/src/lib/libc/net/rcmd.3 b/src/lib/libc/net/rcmd.3
deleted file mode 100644
index b2a5cdea9b..0000000000
--- a/src/lib/libc/net/rcmd.3
+++ /dev/null
@@ -1,232 +0,0 @@
1.\" $OpenBSD: rcmd.3,v 1.34 2016/05/28 15:48:30 millert 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. Neither the name of the University nor the names of its contributors
15.\" may be used to endorse or promote products derived from this software
16.\" without specific prior written permission.
17.\"
18.\" THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
19.\" ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
20.\" IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
21.\" ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
22.\" FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
23.\" DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
24.\" OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
25.\" HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
26.\" LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
27.\" OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
28.\" SUCH DAMAGE.
29.\"
30.Dd $Mdocdate: May 28 2016 $
31.Dt RCMD 3
32.Os
33.Sh NAME
34.Nm rcmd ,
35.Nm rcmd_af ,
36.Nm rresvport ,
37.Nm rresvport_af ,
38.Nm ruserok
39.Nd routines for returning a stream to a remote command
40.Sh SYNOPSIS
41.In unistd.h
42.Ft int
43.Fn rcmd "char **ahost" "int inport" "const char *locuser" "const char *remuser" "const char *cmd" "int *fd2p"
44.Ft int
45.Fn rcmd_af "char **ahost" "int inport" "const char *locuser" "const char *remuser" "const char *cmd" "int *fd2p" "int af"
46.Ft int
47.Fn rresvport "int *port"
48.Ft int
49.Fn rresvport_af "int *port" "int af"
50.Ft int
51.Fn ruserok "const char *rhost" "int superuser" "const char *ruser" "const char *luser"
52.Sh DESCRIPTION
53The
54.Fn rcmd
55function is used by the superuser to execute a command on a remote
56machine using an authentication scheme based on reserved
57port numbers.
58If the calling process is not setuid, the
59.Ev RSH
60environment variable is set, and
61.Fa inport
62is
63.Dq shell/tcp ,
64.Xr rcmdsh 3
65is called instead with the value of
66.Ev RSH .
67Alternately, if the user is not the superuser,
68.Fn rcmd
69will invoke
70.Xr rcmdsh 3
71to run the command via
72.Xr ssh 1 .
73While
74.Fn rcmd
75can handle IPv4 cases only,
76the
77.Fn rcmd_af
78function can handle other cases as well.
79.Pp
80The
81.Fn rresvport
82and
83.Fn rresvport_af
84functions return a descriptor to a socket
85with an address in the privileged port space.
86The
87.Fn ruserok
88function is used by servers
89to authenticate clients requesting service with
90.Fn rcmd .
91.Pp
92The
93.Fn rcmd
94function looks up the host
95.Fa *ahost
96using
97.Xr getaddrinfo 3
98and, if the host exists,
99.Fa *ahost
100is set to the canonical name of the host.
101A connection is then established to a server
102residing at the well-known Internet port
103.Fa inport .
104If the user is not the superuser, the only valid port is
105.Dq shell/tcp
106(usually port 514).
107.Pp
108If the connection succeeds,
109a socket in the Internet domain of type
110.Dv SOCK_STREAM
111is returned to the caller, and given to the remote
112command as stdin and stdout.
113If
114.Fa fd2p
115is non-zero, then an auxiliary channel to a control
116process will be set up, and a descriptor for it will be placed
117in
118.Fa *fd2p .
119The control process will return diagnostic
120output from the command (unit 2) on this channel, and will also
121accept bytes on this channel as being
122.Tn UNIX
123signal numbers, to be
124forwarded to the process group of the command.
125If
126.Fa fd2p
127is
128.Va NULL ,
129then the standard error (unit 2 of the remote command) will be made
130the same as the standard output and no provision is made for sending
131arbitrary signals to the remote process, although you may be able to
132get its attention by using out-of-band data.
133Note that if the user is not the superuser,
134.Fa fd2p
135must be
136.Va NULL .
137.Pp
138.Fn rcmd_af
139takes address family in the last argument.
140If the last argument is
141.Dv AF_UNSPEC ,
142interpretation of
143.Fa *ahost
144will obey the underlying address resolution like DNS.
145.Pp
146The
147.Fn rresvport
148and
149.Fn rresvport_af
150functions are used to obtain a socket with a privileged
151address bound to it.
152This socket is suitable for use by
153.Fn rcmd
154and several other functions.
155Privileged Internet ports are those in the range 0 to
156.Va IPPORT_RESERVED - 1 ,
157which happens to be 1023.
158Only the superuser is allowed to bind an address of this sort to a socket.
159.Fn rresvport
160and
161.Fn rresvport_af
162need to be seeded with a port number; if that port
163is not available these functions will find another.
164.Pp
165The
166.Fn ruserok
167function takes a remote host's name, two user names,
168and a flag indicating whether the local user's
169name is that of the superuser.
170Then, if the user is
171.Em not
172the superuser, it checks the
173.Pa /etc/hosts.equiv
174file.
175If that lookup is not done, or is unsuccessful, the
176.Pa .rhosts
177in the local user's home directory is checked to see if the request for
178service is allowed.
179.Pp
180If this file does not exist, is not a regular file, is owned by anyone
181other than the user or the superuser, or is writeable by anyone other
182than the owner, the check automatically fails.
183Zero is returned if the machine name is listed in the
184.Pa hosts.equiv
185file, or the host and remote user name are found in the
186.Pa .rhosts
187file; otherwise
188.Fn ruserok
189returns \-1.
190If the local domain (as obtained from
191.Xr getaddrinfo 3 )
192is the same as the remote domain, only the machine name need be specified.
193.Pp
194.Fn ruserok
195implicitly requires trusting the DNS server for the remote host's domain.
196.Sh DIAGNOSTICS
197The
198.Fn rcmd
199function returns a valid socket descriptor on success.
200It returns \-1 on error and prints a diagnostic message on the standard error.
201.Pp
202The
203.Fn rresvport
204and
205.Fn rresvport_af
206functions return a valid, bound socket descriptor on success.
207It returns \-1 on error with the global value
208.Va errno
209set according to the reason for failure.
210The error code
211.Er EAGAIN
212is overloaded to mean
213.Dq all network ports in use .
214.Sh SEE ALSO
215.Xr ssh 1 ,
216.Xr intro 2 ,
217.Xr bindresvport 3 ,
218.Xr bindresvport_sa 3 ,
219.Xr rcmdsh 3
220.Sh HISTORY
221These
222functions appeared in
223.Bx 4.2 .
224.Pp
225The
226.Fn iruserok
227and
228.Fn iruserok_sa
229functions, IP address based versions of
230.Fn ruserok ,
231were removed in
232.Ox 6.0 .
diff --git a/src/lib/libc/net/rcmd.c b/src/lib/libc/net/rcmd.c
deleted file mode 100644
index bf68603649..0000000000
--- a/src/lib/libc/net/rcmd.c
+++ /dev/null
@@ -1,296 +0,0 @@
1/*
2 * Copyright (c) 1995, 1996, 1998 Theo de Raadt. All rights reserved.
3 * Copyright (c) 1983, 1993, 1994
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. Neither the name of the University nor the names of its contributors
15 * may be used to endorse or promote products derived from this software
16 * without specific prior written permission.
17 *
18 * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
19 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
20 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
21 * ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
22 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
23 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
24 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
25 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
26 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
27 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
28 * SUCH DAMAGE.
29 */
30
31#include <sys/socket.h>
32#include <sys/stat.h>
33
34#include <netinet/in.h>
35#include <arpa/inet.h>
36
37#include <signal.h>
38#include <fcntl.h>
39#include <netdb.h>
40#include <unistd.h>
41#include <limits.h>
42#include <pwd.h>
43#include <errno.h>
44#include <stdio.h>
45#include <ctype.h>
46#include <string.h>
47#include <syslog.h>
48#include <time.h>
49#include <stdlib.h>
50#include <poll.h>
51
52int
53rcmd(char **ahost, int rport, const char *locuser, const char *remuser,
54 const char *cmd, int *fd2p)
55{
56 return rcmd_af(ahost, rport, locuser, remuser, cmd, fd2p, AF_INET);
57}
58
59int
60rcmd_af(char **ahost, int porta, const char *locuser, const char *remuser,
61 const char *cmd, int *fd2p, int af)
62{
63 static char hbuf[HOST_NAME_MAX+1];
64 char pbuf[NI_MAXSERV];
65 struct addrinfo hints, *res, *r;
66 int error;
67 struct sockaddr_storage from;
68 sigset_t oldmask, mask;
69 pid_t pid;
70 int s, lport;
71 struct timespec timo;
72 char c, *p;
73 int refused;
74 in_port_t rport = porta;
75 int numread;
76
77 /* call rcmdsh() with specified remote shell if appropriate. */
78 if (!issetugid() && (p = getenv("RSH")) && *p) {
79 struct servent *sp = getservbyname("shell", "tcp");
80
81 if (sp && sp->s_port == rport)
82 return (rcmdsh(ahost, rport, locuser, remuser,
83 cmd, p));
84 }
85
86 /* use rsh(1) if non-root and remote port is shell. */
87 if (geteuid()) {
88 struct servent *sp = getservbyname("shell", "tcp");
89
90 if (sp && sp->s_port == rport)
91 return (rcmdsh(ahost, rport, locuser, remuser,
92 cmd, NULL));
93 }
94
95 pid = getpid();
96 snprintf(pbuf, sizeof(pbuf), "%u", ntohs(rport));
97 memset(&hints, 0, sizeof(hints));
98 hints.ai_family = af;
99 hints.ai_socktype = SOCK_STREAM;
100 hints.ai_flags = AI_CANONNAME;
101 error = getaddrinfo(*ahost, pbuf, &hints, &res);
102 if (error) {
103 (void)fprintf(stderr, "rcmd: %s: %s\n", *ahost,
104 gai_strerror(error));
105 return (-1);
106 }
107 if (res->ai_canonname) {
108 strlcpy(hbuf, res->ai_canonname, sizeof(hbuf));
109 *ahost = hbuf;
110 } else
111 ; /*XXX*/
112
113 r = res;
114 refused = 0;
115 timespecclear(&timo);
116 sigemptyset(&mask);
117 sigaddset(&mask, SIGURG);
118 sigprocmask(SIG_BLOCK, &mask, &oldmask);
119 for (timo.tv_sec = 1, lport = IPPORT_RESERVED - 1;;) {
120 s = rresvport_af(&lport, r->ai_family);
121 if (s < 0) {
122 if (errno == EAGAIN)
123 (void)fprintf(stderr,
124 "rcmd: socket: All ports in use\n");
125 else
126 (void)fprintf(stderr, "rcmd: socket: %s\n",
127 strerror(errno));
128 if (r->ai_next) {
129 r = r->ai_next;
130 continue;
131 } else {
132 sigprocmask(SIG_SETMASK, &oldmask, NULL);
133 freeaddrinfo(res);
134 return (-1);
135 }
136 }
137 fcntl(s, F_SETOWN, pid);
138 if (connect(s, r->ai_addr, r->ai_addrlen) >= 0)
139 break;
140 (void)close(s);
141 if (errno == EADDRINUSE) {
142 lport--;
143 continue;
144 }
145 if (errno == ECONNREFUSED)
146 refused++;
147 if (r->ai_next) {
148 int oerrno = errno;
149 char hbuf[NI_MAXHOST];
150 const int niflags = NI_NUMERICHOST;
151
152 hbuf[0] = '\0';
153 if (getnameinfo(r->ai_addr, r->ai_addrlen,
154 hbuf, sizeof(hbuf), NULL, 0, niflags) != 0)
155 strlcpy(hbuf, "(invalid)", sizeof hbuf);
156 (void)fprintf(stderr, "connect to address %s: ", hbuf);
157 errno = oerrno;
158 perror(0);
159 r = r->ai_next;
160 hbuf[0] = '\0';
161 if (getnameinfo(r->ai_addr, r->ai_addrlen,
162 hbuf, sizeof(hbuf), NULL, 0, niflags) != 0)
163 strlcpy(hbuf, "(invalid)", sizeof hbuf);
164 (void)fprintf(stderr, "Trying %s...\n", hbuf);
165 continue;
166 }
167 if (refused && timo.tv_sec <= 16) {
168 (void)nanosleep(&timo, NULL);
169 timo.tv_sec *= 2;
170 r = res;
171 refused = 0;
172 continue;
173 }
174 (void)fprintf(stderr, "%s: %s\n", res->ai_canonname,
175 strerror(errno));
176 sigprocmask(SIG_SETMASK, &oldmask, NULL);
177 freeaddrinfo(res);
178 return (-1);
179 }
180 /* given "af" can be PF_UNSPEC, we need the real af for "s" */
181 af = r->ai_family;
182 freeaddrinfo(res);
183 if (fd2p == 0) {
184 write(s, "", 1);
185 lport = 0;
186 } else {
187 struct pollfd pfd[2];
188 char num[8];
189 int s2 = rresvport_af(&lport, af), s3;
190 socklen_t len = sizeof(from);
191
192 if (s2 < 0)
193 goto bad;
194
195 listen(s2, 1);
196 (void)snprintf(num, sizeof(num), "%d", lport);
197 if (write(s, num, strlen(num)+1) != strlen(num)+1) {
198 (void)fprintf(stderr,
199 "rcmd: write (setting up stderr): %s\n",
200 strerror(errno));
201 (void)close(s2);
202 goto bad;
203 }
204again:
205 pfd[0].fd = s;
206 pfd[0].events = POLLIN;
207 pfd[1].fd = s2;
208 pfd[1].events = POLLIN;
209
210 errno = 0;
211 if (poll(pfd, 2, INFTIM) < 1 ||
212 (pfd[1].revents & (POLLIN|POLLHUP)) == 0) {
213 if (errno != 0)
214 (void)fprintf(stderr,
215 "rcmd: poll (setting up stderr): %s\n",
216 strerror(errno));
217 else
218 (void)fprintf(stderr,
219 "poll: protocol failure in circuit setup\n");
220 (void)close(s2);
221 goto bad;
222 }
223 s3 = accept(s2, (struct sockaddr *)&from, &len);
224 if (s3 < 0) {
225 (void)fprintf(stderr,
226 "rcmd: accept: %s\n", strerror(errno));
227 lport = 0;
228 close(s2);
229 goto bad;
230 }
231
232 /*
233 * XXX careful for ftp bounce attacks. If discovered, shut them
234 * down and check for the real auxiliary channel to connect.
235 */
236 switch (from.ss_family) {
237 case AF_INET:
238 case AF_INET6:
239 if (getnameinfo((struct sockaddr *)&from, len,
240 NULL, 0, num, sizeof(num), NI_NUMERICSERV) == 0 &&
241 atoi(num) != 20) {
242 break;
243 }
244 close(s3);
245 goto again;
246 default:
247 break;
248 }
249 (void)close(s2);
250
251 *fd2p = s3;
252 switch (from.ss_family) {
253 case AF_INET:
254 case AF_INET6:
255 if (getnameinfo((struct sockaddr *)&from, len,
256 NULL, 0, num, sizeof(num), NI_NUMERICSERV) != 0 ||
257 (atoi(num) >= IPPORT_RESERVED ||
258 atoi(num) < IPPORT_RESERVED / 2)) {
259 (void)fprintf(stderr,
260 "socket: protocol failure in circuit setup.\n");
261 goto bad2;
262 }
263 break;
264 default:
265 break;
266 }
267 }
268 (void)write(s, locuser, strlen(locuser)+1);
269 (void)write(s, remuser, strlen(remuser)+1);
270 (void)write(s, cmd, strlen(cmd)+1);
271 if ((numread = read(s, &c, 1)) != 1) {
272 (void)fprintf(stderr,
273 "rcmd: %s: %s\n", *ahost,
274 numread == -1 ? strerror(errno) : "Short read");
275 goto bad2;
276 }
277 if (c != 0) {
278 while (read(s, &c, 1) == 1) {
279 (void)write(STDERR_FILENO, &c, 1);
280 if (c == '\n')
281 break;
282 }
283 goto bad2;
284 }
285 sigprocmask(SIG_SETMASK, &oldmask, NULL);
286 return (s);
287bad2:
288 if (lport)
289 (void)close(*fd2p);
290bad:
291 (void)close(s);
292 sigprocmask(SIG_SETMASK, &oldmask, NULL);
293 return (-1);
294}
295DEF_WEAK(rcmd_af);
296
diff --git a/src/lib/libc/net/rcmdsh.3 b/src/lib/libc/net/rcmdsh.3
deleted file mode 100644
index 9092d19f81..0000000000
--- a/src/lib/libc/net/rcmdsh.3
+++ /dev/null
@@ -1,95 +0,0 @@
1.\" $OpenBSD: rcmdsh.3,v 1.18 2016/05/28 15:48:30 millert 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. Neither the name of the University nor the names of its contributors
15.\" may be used to endorse or promote products derived from this software
16.\" without specific prior written permission.
17.\"
18.\" THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
19.\" ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
20.\" IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
21.\" ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
22.\" FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
23.\" DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
24.\" OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
25.\" HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
26.\" LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
27.\" OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
28.\" SUCH DAMAGE.
29.\"
30.Dd $Mdocdate: May 28 2016 $
31.Dt RCMDSH 3
32.Os
33.Sh NAME
34.Nm rcmdsh
35.Nd return a stream to a remote command without superuser
36.Sh SYNOPSIS
37.In unistd.h
38.Ft int
39.Fn rcmdsh "char **ahost" "int inport" "const char *locuser" "const char *remuser" "const char *cmd" "char *rshprog"
40.Sh DESCRIPTION
41The
42.Fn rcmdsh
43function is used by normal users to execute a command on a remote machine
44using an authentication scheme based on reserved port numbers using
45.Xr ssh 1
46or the value of
47.Fa rshprog
48(if non-null).
49.Fa rshprog
50may be a fully-qualified path, a non-qualified command, or a command containing
51space-separated command line arguments.
52.Pp
53The
54.Fn rcmdsh
55function looks up the host
56.Fa *ahost
57using
58.Xr getaddrinfo 3
59and, if the host exists,
60.Fa *ahost
61is set to the canonical name of the host.
62A connection is then established to
63a server residing at the well-known Internet port
64.Li shell/tcp
65(or whatever port is used by
66.Fa rshprog ) .
67The parameter
68.Fa inport
69is ignored; it is only included to provide an interface similar to
70.Xr rcmd 3 .
71.Pp
72If the connection succeeds, a socket in the
73.Ux Ns -domain
74of type
75.Dv SOCK_STREAM
76is returned to the caller, and given to the remote
77command as stdin and stdout, and stderr.
78.Sh DIAGNOSTICS
79The
80.Fn rcmdsh
81function returns a valid socket descriptor on success.
82It returns \-1 on error and prints a diagnostic message on the standard error.
83.Sh SEE ALSO
84.Xr ssh 1 ,
85.Xr socketpair 2 ,
86.Xr rcmd 3
87.Sh HISTORY
88The
89.Fn rcmdsh
90function first appeared in
91.Ox 2.0 .
92.Sh BUGS
93If
94.Xr ssh 1
95encounters an error, a file descriptor is still returned instead of \-1.
diff --git a/src/lib/libc/net/rcmdsh.c b/src/lib/libc/net/rcmdsh.c
deleted file mode 100644
index 66caac3f3d..0000000000
--- a/src/lib/libc/net/rcmdsh.c
+++ /dev/null
@@ -1,194 +0,0 @@
1/* $OpenBSD: rcmdsh.c,v 1.20 2019/06/28 13:32:42 deraadt Exp $ */
2
3/*
4 * Copyright (c) 2001, MagniComp
5 * 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
14 * the documentation and/or other materials provided with the distribution.
15 * 3. Neither the name of the MagniComp nor the names of its contributors may
16 * be used to endorse or promote products derived from this software
17 * without specific prior written permission.
18 *
19 * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
20 * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
21 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
22 * ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE FOR
23 * ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
24 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR
25 * SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER
26 * CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY,
27 * OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE
28 * USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
29 */
30
31/*
32 * This is an rcmd() replacement originally by
33 * Chris Siebenmann <cks@utcc.utoronto.ca>.
34 */
35
36#include <sys/types.h>
37#include <sys/socket.h>
38#include <sys/wait.h>
39#include <signal.h>
40#include <errno.h>
41#include <limits.h>
42#include <netdb.h>
43#include <stdio.h>
44#include <stdlib.h>
45#include <string.h>
46#include <pwd.h>
47#include <paths.h>
48#include <unistd.h>
49
50/*
51 * This is a replacement rcmd() function that uses the ssh(1)
52 * program in place of a direct rcmd(3) function call so as to
53 * avoid having to be root. Note that rport is ignored.
54 */
55int
56rcmdsh(char **ahost, int rport, const char *locuser, const char *remuser,
57 const char *cmd, char *rshprog)
58{
59 static char hbuf[HOST_NAME_MAX+1];
60 struct addrinfo hint, *res;
61 int sp[2];
62 pid_t cpid;
63 char *p, pwbuf[_PW_BUF_LEN];
64 struct passwd pwstore, *pw = NULL;
65
66 /* What rsh/shell to use. */
67 if (rshprog == NULL)
68 rshprog = _PATH_RSH;
69
70 /* locuser must exist on this host. */
71 getpwnam_r(locuser, &pwstore, pwbuf, sizeof(pwbuf), &pw);
72 if (pw == NULL) {
73 (void) fprintf(stderr, "rcmdsh: unknown user: %s\n", locuser);
74 return(-1);
75 }
76
77 /* Validate remote hostname. */
78 if (strcmp(*ahost, "localhost") != 0) {
79 memset(&hint, 0, sizeof(hint));
80 hint.ai_family = PF_UNSPEC;
81 hint.ai_flags = AI_CANONNAME;
82 if (getaddrinfo(*ahost, NULL, &hint, &res) == 0) {
83 if (res->ai_canonname) {
84 strlcpy(hbuf, res->ai_canonname, sizeof(hbuf));
85 *ahost = hbuf;
86 }
87 freeaddrinfo(res);
88 }
89 }
90
91 /* Get a socketpair we'll use for stdin and stdout. */
92 if (socketpair(AF_UNIX, SOCK_STREAM, PF_UNSPEC, sp) == -1) {
93 perror("rcmdsh: socketpair");
94 return(-1);
95 }
96
97 cpid = fork();
98 if (cpid == -1) {
99 perror("rcmdsh: fork failed");
100 return(-1);
101 } else if (cpid == 0) {
102 /*
103 * Child. We use sp[1] to be stdin/stdout, and close sp[0].
104 */
105 (void) close(sp[0]);
106 if (dup2(sp[1], 0) == -1 || dup2(0, 1) == -1) {
107 perror("rcmdsh: dup2 failed");
108 _exit(255);
109 }
110 /* Fork again to lose parent. */
111 cpid = fork();
112 if (cpid == -1) {
113 perror("rcmdsh: fork to lose parent failed");
114 _exit(255);
115 }
116 if (cpid > 0)
117 _exit(0);
118
119 /* In grandchild here. Become local user for rshprog. */
120 if (setuid(pw->pw_uid)) {
121 (void) fprintf(stderr, "rcmdsh: setuid(%u): %s\n",
122 pw->pw_uid, strerror(errno));
123 _exit(255);
124 }
125
126 /*
127 * If remote host is "localhost" and local and remote user
128 * are the same, avoid running remote shell for efficiency.
129 */
130 if (!strcmp(*ahost, "localhost") && !strcmp(locuser, remuser)) {
131 char *argv[4];
132 if (pw->pw_shell[0] == '\0')
133 rshprog = _PATH_BSHELL;
134 else
135 rshprog = pw->pw_shell;
136 p = strrchr(rshprog, '/');
137 argv[0] = p ? p + 1 : rshprog;
138 argv[1] = "-c";
139 argv[2] = (char *)cmd;
140 argv[3] = NULL;
141 execvp(rshprog, argv);
142 } else if ((p = strchr(rshprog, ' ')) == NULL) {
143 /* simple case */
144 char *argv[6];
145 p = strrchr(rshprog, '/');
146 argv[0] = p ? p + 1 : rshprog;
147 argv[1] = "-l";
148 argv[2] = (char *)remuser;
149 argv[3] = *ahost;
150 argv[4] = (char *)cmd;
151 argv[5] = NULL;
152 execvp(rshprog, argv);
153 } else {
154 /* must pull args out of rshprog and dyn alloc argv */
155 char **argv, **ap;
156 int n;
157 for (n = 7; (p = strchr(++p, ' ')) != NULL; n++)
158 continue;
159 rshprog = strdup(rshprog);
160 ap = argv = calloc(sizeof(char *), n);
161 if (rshprog == NULL || argv == NULL) {
162 perror("rcmdsh");
163 _exit(255);
164 }
165 while ((p = strsep(&rshprog, " ")) != NULL) {
166 if (*p == '\0')
167 continue;
168 *ap++ = p;
169 }
170 if (ap != argv) /* all spaces?!? */
171 rshprog = argv[0];
172 if ((p = strrchr(argv[0], '/')) != NULL)
173 argv[0] = p + 1;
174 *ap++ = "-l";
175 *ap++ = (char *)remuser;
176 *ap++ = *ahost;
177 *ap++ = (char *)cmd;
178 *ap++ = NULL;
179 execvp(rshprog, argv);
180 }
181 (void) fprintf(stderr, "rcmdsh: execvp %s failed: %s\n",
182 rshprog, strerror(errno));
183 _exit(255);
184 } else {
185 /* Parent. close sp[1], return sp[0]. */
186 (void) close(sp[1]);
187 /* Reap child. */
188 while (waitpid(cpid, NULL, 0) == -1 && errno == EINTR)
189 ;
190 return(sp[0]);
191 }
192 /* NOTREACHED */
193}
194DEF_WEAK(rcmdsh);
diff --git a/src/lib/libc/net/recv.c b/src/lib/libc/net/recv.c
deleted file mode 100644
index 1488ca26bd..0000000000
--- a/src/lib/libc/net/recv.c
+++ /dev/null
@@ -1,41 +0,0 @@
1/* $OpenBSD: recv.c,v 1.7 2023/03/25 19:16:34 otto Exp $ */
2/*
3 * Copyright (c) 1988, 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. Neither the name of the University nor the names of its contributors
15 * may be used to endorse or promote products derived from this software
16 * without specific prior written permission.
17 *
18 * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
19 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
20 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
21 * ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
22 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
23 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
24 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
25 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
26 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
27 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
28 * SUCH DAMAGE.
29 */
30
31#include <sys/types.h>
32#include <sys/socket.h>
33
34#include <stddef.h>
35
36ssize_t
37recv(int s, void *buf, size_t len, int flags)
38{
39 return (recvfrom(s, buf, len, flags, NULL, NULL));
40}
41DEF_WEAK(recv);
diff --git a/src/lib/libc/net/res_comp.c b/src/lib/libc/net/res_comp.c
deleted file mode 100644
index ce9f92ae77..0000000000
--- a/src/lib/libc/net/res_comp.c
+++ /dev/null
@@ -1,466 +0,0 @@
1/* $OpenBSD: res_comp.c,v 1.23 2023/03/15 22:12:00 millert Exp $ */
2
3/*
4 * ++Copyright++ 1985, 1993
5 * -
6 * Copyright (c) 1985, 1993
7 * The Regents of the University of California. All rights reserved.
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. 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 * Portions Copyright (c) 1993 by Digital Equipment Corporation.
34 *
35 * Permission to use, copy, modify, and distribute this software for any
36 * purpose with or without fee is hereby granted, provided that the above
37 * copyright notice and this permission notice appear in all copies, and that
38 * the name of Digital Equipment Corporation not be used in advertising or
39 * publicity pertaining to distribution of the document or software without
40 * specific, written prior permission.
41 *
42 * THE SOFTWARE IS PROVIDED "AS IS" AND DIGITAL EQUIPMENT CORP. DISCLAIMS ALL
43 * WARRANTIES WITH REGARD TO THIS SOFTWARE, INCLUDING ALL IMPLIED WARRANTIES
44 * OF MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL DIGITAL EQUIPMENT
45 * CORPORATION BE LIABLE FOR ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL
46 * DAMAGES OR ANY DAMAGES WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR
47 * PROFITS, WHETHER IN AN ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS
48 * ACTION, ARISING OUT OF OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS
49 * SOFTWARE.
50 * -
51 * --Copyright--
52 */
53
54#include <sys/types.h>
55#include <netinet/in.h>
56#include <arpa/nameser.h>
57
58#include <stdio.h>
59#include <resolv.h>
60#include <ctype.h>
61
62#include <unistd.h>
63#include <limits.h>
64#include <string.h>
65
66static int dn_find(u_char *, u_char *, u_char **, u_char **);
67
68/*
69 * Expand compressed domain name 'comp_dn' to full domain name.
70 * 'msg' is a pointer to the beginning of the message,
71 * 'eomorig' points to the first location after the message,
72 * 'exp_dn' is a pointer to a buffer of size 'length' for the result.
73 * Return size of compressed name or -1 if there was an error.
74 */
75int
76dn_expand(const u_char *msg, const u_char *eomorig, const u_char *comp_dn,
77 char *exp_dn, int length)
78{
79 const u_char *cp;
80 char *dn;
81 int n, c;
82 char *eom;
83 int len = -1, checked = 0;
84
85 if (comp_dn < msg || comp_dn >= eomorig)
86 return (-1);
87
88 dn = exp_dn;
89 cp = comp_dn;
90 if (length > HOST_NAME_MAX)
91 length = HOST_NAME_MAX;
92 eom = exp_dn + length;
93 /*
94 * fetch next label in domain name
95 */
96 while ((n = *cp++)) {
97 if (cp >= eomorig) /* out of range */
98 return (-1);
99
100 /*
101 * Check for indirection
102 */
103 switch (n & INDIR_MASK) {
104 case 0:
105 if (dn != exp_dn) {
106 if (dn >= eom)
107 return (-1);
108 *dn++ = '.';
109 }
110 if (dn+n >= eom)
111 return (-1);
112 checked += n + 1;
113 while (--n >= 0) {
114 if (((c = *cp++) == '.') || (c == '\\')) {
115 if (dn + n + 2 >= eom)
116 return (-1);
117 *dn++ = '\\';
118 }
119 *dn++ = c;
120 if (cp >= eomorig) /* out of range */
121 return (-1);
122 }
123 break;
124
125 case INDIR_MASK:
126 if (len < 0)
127 len = cp - comp_dn + 1;
128 cp = msg + (((n & 0x3f) << 8) | (*cp & 0xff));
129 if (cp < msg || cp >= eomorig) /* out of range */
130 return (-1);
131 checked += 2;
132 /*
133 * Check for loops in the compressed name;
134 * if we've looked at the whole message,
135 * there must be a loop.
136 */
137 if (checked >= eomorig - msg)
138 return (-1);
139 break;
140
141 default:
142 return (-1); /* flag error */
143 }
144 }
145 *dn = '\0';
146 if (len < 0)
147 len = cp - comp_dn;
148 return (len);
149}
150DEF_WEAK(dn_expand);
151
152/*
153 * Compress domain name 'exp_dn' into 'comp_dn'.
154 * Return the size of the compressed name or -1.
155 * 'length' is the size of the array pointed to by 'comp_dn'.
156 * 'dnptrs' is a list of pointers to previous compressed names. dnptrs[0]
157 * is a pointer to the beginning of the message. The list ends with NULL.
158 * 'lastdnptr' is a pointer to the end of the array pointed to
159 * by 'dnptrs'. Side effect is to update the list of pointers for
160 * labels inserted into the message as we compress the name.
161 * If 'dnptr' is NULL, we don't try to compress names. If 'lastdnptr'
162 * is NULL, we don't update the list.
163 */
164int
165dn_comp(const char *exp_dn, u_char *comp_dn, int length, u_char **dnptrs,
166 u_char **lastdnptr)
167{
168 u_char *cp, *dn;
169 int c, l;
170 u_char **cpp, **lpp, *sp, *eob;
171 u_char *msg;
172
173 dn = (u_char *)exp_dn;
174 cp = comp_dn;
175 eob = cp + length;
176 lpp = cpp = NULL;
177 if (dnptrs != NULL) {
178 if ((msg = *dnptrs++) != NULL) {
179 for (cpp = dnptrs; *cpp != NULL; cpp++)
180 ;
181 lpp = cpp; /* end of list to search */
182 }
183 } else
184 msg = NULL;
185 for (c = *dn++; c != '\0'; ) {
186 /* look to see if we can use pointers */
187 if (msg != NULL) {
188 if ((l = dn_find(dn-1, msg, dnptrs, lpp)) >= 0) {
189 if (cp+1 >= eob)
190 return (-1);
191 *cp++ = (l >> 8) | INDIR_MASK;
192 *cp++ = l % 256;
193 return (cp - comp_dn);
194 }
195 /* not found, save it */
196 if (lastdnptr != NULL && cpp < lastdnptr-1) {
197 *cpp++ = cp;
198 *cpp = NULL;
199 }
200 }
201 sp = cp++; /* save ptr to length byte */
202 do {
203 if (c == '.') {
204 c = *dn++;
205 break;
206 }
207 if (c == '\\') {
208 if ((c = *dn++) == '\0')
209 break;
210 }
211 if (cp >= eob) {
212 if (msg != NULL)
213 *lpp = NULL;
214 return (-1);
215 }
216 *cp++ = c;
217 } while ((c = *dn++) != '\0');
218 /* catch trailing '.'s but not '..' */
219 if ((l = cp - sp - 1) == 0 && c == '\0') {
220 cp--;
221 break;
222 }
223 if (l <= 0 || l > MAXLABEL) {
224 if (msg != NULL)
225 *lpp = NULL;
226 return (-1);
227 }
228 *sp = l;
229 }
230 if (cp >= eob) {
231 if (msg != NULL)
232 *lpp = NULL;
233 return (-1);
234 }
235 *cp++ = '\0';
236 return (cp - comp_dn);
237}
238
239/*
240 * Skip over a compressed domain name. Return the size or -1.
241 */
242int
243__dn_skipname(const u_char *comp_dn, const u_char *eom)
244{
245 const u_char *cp;
246 int n;
247
248 cp = comp_dn;
249 while (cp < eom && (n = *cp++)) {
250 /*
251 * check for indirection
252 */
253 switch (n & INDIR_MASK) {
254 case 0: /* normal case, n == len */
255 cp += n;
256 continue;
257 case INDIR_MASK: /* indirection */
258 cp++;
259 break;
260 default: /* illegal type */
261 return (-1);
262 }
263 break;
264 }
265 if (cp > eom)
266 return (-1);
267 return (cp - comp_dn);
268}
269
270/*
271 * Search for expanded name from a list of previously compressed names.
272 * Return the offset from msg if found or -1.
273 * dnptrs is the pointer to the first name on the list,
274 * not the pointer to the start of the message.
275 */
276static int
277dn_find(u_char *exp_dn, u_char *msg, u_char **dnptrs, u_char **lastdnptr)
278{
279 u_char *dn, *cp, **cpp;
280 int n;
281 u_char *sp;
282
283 for (cpp = dnptrs; cpp < lastdnptr; cpp++) {
284 dn = exp_dn;
285 sp = cp = *cpp;
286 while ((n = *cp++)) {
287 /*
288 * check for indirection
289 */
290 switch (n & INDIR_MASK) {
291 case 0: /* normal case, n == len */
292 while (--n >= 0) {
293 if (*dn == '.')
294 goto next;
295 if (*dn == '\\')
296 dn++;
297 if (tolower((unsigned char)*dn++) !=
298 tolower((unsigned char)*cp++))
299 goto next;
300 }
301 if ((n = *dn++) == '\0' && *cp == '\0')
302 return (sp - msg);
303 if (n == '.')
304 continue;
305 goto next;
306
307 case INDIR_MASK: /* indirection */
308 cp = msg + (((n & 0x3f) << 8) | *cp);
309 break;
310
311 default: /* illegal type */
312 return (-1);
313 }
314 }
315 if (*dn == '\0')
316 return (sp - msg);
317 next: ;
318 }
319 return (-1);
320}
321
322/*
323 * Verify that a domain name uses an acceptable character set.
324 */
325
326/*
327 * Note the conspicuous absence of ctype macros in these definitions. On
328 * non-ASCII hosts, we can't depend on string literals or ctype macros to
329 * tell us anything about network-format data. The rest of the BIND system
330 * is not careful about this, but for some reason, we're doing it right here.
331 */
332#define PERIOD 0x2e
333#define hyphenchar(c) ((c) == 0x2d)
334#define bslashchar(c) ((c) == 0x5c)
335#define underscorechar(c) ((c) == 0x5f)
336#define periodchar(c) ((c) == PERIOD)
337#define asterchar(c) ((c) == 0x2a)
338#define alphachar(c) (((c) >= 0x41 && (c) <= 0x5a) \
339 || ((c) >= 0x61 && (c) <= 0x7a))
340#define digitchar(c) ((c) >= 0x30 && (c) <= 0x39)
341
342#define borderchar(c) (alphachar(c) || digitchar(c))
343#define middlechar(c) (borderchar(c) || hyphenchar(c) || underscorechar(c))
344#define domainchar(c) ((c) > 0x20 && (c) < 0x7f)
345
346int
347__res_hnok(const char *dn)
348{
349 int pch = PERIOD, ch = *dn++;
350
351 while (ch != '\0') {
352 int nch = *dn++;
353
354 if (periodchar(ch)) {
355 ;
356 } else if (periodchar(pch)) {
357 if (!borderchar(ch))
358 return (0);
359 } else if (periodchar(nch) || nch == '\0') {
360 if (!borderchar(ch))
361 return (0);
362 } else {
363 if (!middlechar(ch))
364 return (0);
365 }
366 pch = ch, ch = nch;
367 }
368 return (1);
369}
370DEF_STRONG(__res_hnok);
371
372/*
373 * hostname-like (A, MX, WKS) owners can have "*" as their first label
374 * but must otherwise be as a host name.
375 */
376int
377res_ownok(const char *dn)
378{
379 if (asterchar(dn[0])) {
380 if (periodchar(dn[1]))
381 return (res_hnok(dn+2));
382 if (dn[1] == '\0')
383 return (1);
384 }
385 return (res_hnok(dn));
386}
387
388/*
389 * SOA RNAMEs and RP RNAMEs can have any printable character in their first
390 * label, but the rest of the name has to look like a host name.
391 */
392int
393res_mailok(const char *dn)
394{
395 int ch, escaped = 0;
396
397 /* "." is a valid missing representation */
398 if (*dn == '\0')
399 return(1);
400
401 /* otherwise <label>.<hostname> */
402 while ((ch = *dn++) != '\0') {
403 if (!domainchar(ch))
404 return (0);
405 if (!escaped && periodchar(ch))
406 break;
407 if (escaped)
408 escaped = 0;
409 else if (bslashchar(ch))
410 escaped = 1;
411 }
412 if (periodchar(ch))
413 return (res_hnok(dn));
414 return(0);
415}
416
417/*
418 * This function is quite liberal, since RFC 1034's character sets are only
419 * recommendations.
420 */
421int
422res_dnok(const char *dn)
423{
424 int ch;
425
426 while ((ch = *dn++) != '\0')
427 if (!domainchar(ch))
428 return (0);
429 return (1);
430}
431
432/*
433 * Routines to insert/extract short/long's.
434 */
435
436u_int16_t
437_getshort(const u_char *msgp)
438{
439 u_int16_t u;
440
441 GETSHORT(u, msgp);
442 return (u);
443}
444DEF_STRONG(_getshort);
445
446u_int32_t
447_getlong(const u_char *msgp)
448{
449 u_int32_t u;
450
451 GETLONG(u, msgp);
452 return (u);
453}
454DEF_STRONG(_getlong);
455
456void
457__putshort(u_int16_t s, u_char *msgp)
458{
459 PUTSHORT(s, msgp);
460}
461
462void
463__putlong(u_int32_t l, u_char *msgp)
464{
465 PUTLONG(l, msgp);
466}
diff --git a/src/lib/libc/net/res_data.c b/src/lib/libc/net/res_data.c
deleted file mode 100644
index 6e81b584a9..0000000000
--- a/src/lib/libc/net/res_data.c
+++ /dev/null
@@ -1,104 +0,0 @@
1/* $OpenBSD: res_data.c,v 1.4 2015/01/16 16:48:51 deraadt Exp $ */
2
3/*
4 * ++Copyright++ 1995
5 * -
6 * Copyright (c) 1995
7 * The Regents of the University of California. All rights reserved.
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. 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 * Portions Copyright (c) 1993 by Digital Equipment Corporation.
34 *
35 * Permission to use, copy, modify, and distribute this software for any
36 * purpose with or without fee is hereby granted, provided that the above
37 * copyright notice and this permission notice appear in all copies, and that
38 * the name of Digital Equipment Corporation not be used in advertising or
39 * publicity pertaining to distribution of the document or software without
40 * specific, written prior permission.
41 *
42 * THE SOFTWARE IS PROVIDED "AS IS" AND DIGITAL EQUIPMENT CORP. DISCLAIMS ALL
43 * WARRANTIES WITH REGARD TO THIS SOFTWARE, INCLUDING ALL IMPLIED WARRANTIES
44 * OF MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL DIGITAL EQUIPMENT
45 * CORPORATION BE LIABLE FOR ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL
46 * DAMAGES OR ANY DAMAGES WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR
47 * PROFITS, WHETHER IN AN ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS
48 * ACTION, ARISING OUT OF OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS
49 * SOFTWARE.
50 * -
51 * --Copyright--
52 */
53
54#include <sys/types.h>
55#include <sys/socket.h>
56#include <sys/time.h>
57#include <netinet/in.h>
58#include <arpa/inet.h>
59#include <arpa/nameser.h>
60
61#include <stdio.h>
62#include <ctype.h>
63#include <resolv.h>
64#include <unistd.h>
65#include <stdlib.h>
66#include <string.h>
67
68const char *_res_opcodes[] = {
69 "QUERY",
70 "IQUERY",
71 "CQUERYM",
72 "CQUERYU", /* experimental */
73 "NOTIFY", /* experimental */
74 "5",
75 "6",
76 "7",
77 "8",
78 "UPDATEA",
79 "UPDATED",
80 "UPDATEDA",
81 "UPDATEM",
82 "UPDATEMA",
83 "ZONEINIT",
84 "ZONEREF",
85};
86
87const char *_res_resultcodes[] = {
88 "NOERROR",
89 "FORMERR",
90 "SERVFAIL",
91 "NXDOMAIN",
92 "NOTIMP",
93 "REFUSED",
94 "6",
95 "7",
96 "8",
97 "9",
98 "10",
99 "11",
100 "12",
101 "13",
102 "14",
103 "NOCHANGE",
104};
diff --git a/src/lib/libc/net/res_debug_syms.c b/src/lib/libc/net/res_debug_syms.c
deleted file mode 100644
index fc26f03e74..0000000000
--- a/src/lib/libc/net/res_debug_syms.c
+++ /dev/null
@@ -1,192 +0,0 @@
1/* $OpenBSD: res_debug_syms.c,v 1.2 2015/10/05 02:57:16 guenther Exp $ */
2
3/*
4 * ++Copyright++ 1985, 1990, 1993
5 * -
6 * Copyright (c) 1985, 1990, 1993
7 * The Regents of the University of California. All rights reserved.
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. 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 * Portions Copyright (c) 1993 by Digital Equipment Corporation.
34 *
35 * Permission to use, copy, modify, and distribute this software for any
36 * purpose with or without fee is hereby granted, provided that the above
37 * copyright notice and this permission notice appear in all copies, and that
38 * the name of Digital Equipment Corporation not be used in advertising or
39 * publicity pertaining to distribution of the document or software without
40 * specific, written prior permission.
41 *
42 * THE SOFTWARE IS PROVIDED "AS IS" AND DIGITAL EQUIPMENT CORP. DISCLAIMS ALL
43 * WARRANTIES WITH REGARD TO THIS SOFTWARE, INCLUDING ALL IMPLIED WARRANTIES
44 * OF MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL DIGITAL EQUIPMENT
45 * CORPORATION BE LIABLE FOR ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL
46 * DAMAGES OR ANY DAMAGES WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR
47 * PROFITS, WHETHER IN AN ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS
48 * ACTION, ARISING OUT OF OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS
49 * SOFTWARE.
50 * -
51 * Portions Copyright (c) 1995 by International Business Machines, Inc.
52 *
53 * International Business Machines, Inc. (hereinafter called IBM) grants
54 * permission under its copyrights to use, copy, modify, and distribute this
55 * Software with or without fee, provided that the above copyright notice and
56 * all paragraphs of this notice appear in all copies, and that the name of IBM
57 * not be used in connection with the marketing of any product incorporating
58 * the Software or modifications thereof, without specific, written prior
59 * permission.
60 *
61 * To the extent it has a right to do so, IBM grants an immunity from suit
62 * under its patents, if any, for the use, sale or manufacture of products to
63 * the extent that such products are used for performing Domain Name System
64 * dynamic updates in TCP/IP networks by means of the Software. No immunity is
65 * granted for any product per se or for any other function of any product.
66 *
67 * THE SOFTWARE IS PROVIDED "AS IS", AND IBM DISCLAIMS ALL WARRANTIES,
68 * INCLUDING ALL IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A
69 * PARTICULAR PURPOSE. IN NO EVENT SHALL IBM BE LIABLE FOR ANY SPECIAL,
70 * DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES WHATSOEVER ARISING
71 * OUT OF OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE, EVEN
72 * IF IBM IS APPRISED OF THE POSSIBILITY OF SUCH DAMAGES.
73 * --Copyright--
74 */
75
76
77#include <sys/types.h>
78#include <netinet/in.h>
79#include <arpa/nameser.h>
80
81#include <resolv.h>
82#include <stdio.h>
83
84/*
85 * Names of RR classes and qclasses. Classes and qclasses are the same, except
86 * that C_ANY is a qclass but not a class. (You can ask for records of class
87 * C_ANY, but you can't have any records of that class in the database.)
88 */
89const struct res_sym __p_class_syms[] = {
90 {C_IN, "IN"},
91 {C_CHAOS, "CHAOS"},
92 {C_HS, "HS"},
93 {C_HS, "HESIOD"},
94 {C_ANY, "ANY"},
95 {C_IN, (char *)0}
96};
97
98/*
99 * Names of RR types and qtypes. Types and qtypes are the same, except
100 * that T_ANY is a qtype but not a type. (You can ask for records of type
101 * T_ANY, but you can't have any records of that type in the database.)
102 */
103const struct res_sym __p_type_syms[] = {
104 {T_A, "A", "address"},
105 {T_NS, "NS", "name server"},
106 {T_MD, "MD", "mail destination (deprecated)"},
107 {T_MF, "MF", "mail forwarder (deprecated)"},
108 {T_CNAME, "CNAME", "canonical name"},
109 {T_SOA, "SOA", "start of authority"},
110 {T_MB, "MB", "mailbox"},
111 {T_MG, "MG", "mail group member"},
112 {T_MR, "MR", "mail rename"},
113 {T_NULL, "NULL", "null"},
114 {T_WKS, "WKS", "well-known service (deprecated)"},
115 {T_PTR, "PTR", "domain name pointer"},
116 {T_HINFO, "HINFO", "host information"},
117 {T_MINFO, "MINFO", "mailbox information"},
118 {T_MX, "MX", "mail exchanger"},
119 {T_TXT, "TXT", "text"},
120 {T_RP, "RP", "responsible person"},
121 {T_AFSDB, "AFSDB", "DCE or AFS server"},
122 {T_X25, "X25", "X25 address"},
123 {T_ISDN, "ISDN", "ISDN address"},
124 {T_RT, "RT", "router"},
125 {T_NSAP, "NSAP", "nsap address"},
126 {T_NSAP_PTR, "NSAP_PTR", "domain name pointer"},
127 {T_SIG, "SIG", "signature"},
128 {T_KEY, "KEY", "key"},
129 {T_PX, "PX", "mapping information"},
130 {T_GPOS, "GPOS", "geographical position (withdrawn)"},
131 {T_AAAA, "AAAA", "IPv6 address"},
132 {T_LOC, "LOC", "location"},
133 {T_NXT, "NXT", "next valid name (unimplemented)"},
134 {T_EID, "EID", "endpoint identifier (unimplemented)"},
135 {T_NIMLOC, "NIMLOC", "NIMROD locator (unimplemented)"},
136 {T_SRV, "SRV", "server selection"},
137 {T_ATMA, "ATMA", "ATM address (unimplemented)"},
138 {T_IXFR, "IXFR", "incremental zone transfer"},
139 {T_AXFR, "AXFR", "zone transfer"},
140 {T_MAILB, "MAILB", "mailbox-related data (deprecated)"},
141 {T_MAILA, "MAILA", "mail agent (deprecated)"},
142 {T_UINFO, "UINFO", "user information (nonstandard)"},
143 {T_UID, "UID", "user ID (nonstandard)"},
144 {T_GID, "GID", "group ID (nonstandard)"},
145 {T_NAPTR, "NAPTR", "URN Naming Authority"},
146#ifdef ALLOW_T_UNSPEC
147 {T_UNSPEC, "UNSPEC", "unspecified data (nonstandard)"},
148#endif /* ALLOW_T_UNSPEC */
149 {T_ANY, "ANY", "\"any\""},
150 {0, NULL, NULL}
151};
152
153const char *
154__sym_ntos(const struct res_sym *syms, int number, int *success)
155{
156 static char unname[20];
157
158 for (; syms->name != 0; syms++) {
159 if (number == syms->number) {
160 if (success)
161 *success = 1;
162 return (syms->name);
163 }
164 }
165
166 snprintf(unname, sizeof unname, "%d", number);
167 if (success)
168 *success = 0;
169 return (unname);
170}
171DEF_STRONG(__sym_ntos);
172
173/*
174 * Return a string for the type
175 */
176const char *
177__p_type(int type)
178{
179 return (__sym_ntos (__p_type_syms, type, (int *)0));
180}
181DEF_STRONG(__p_type);
182
183/*
184 * Return a mnemonic for class
185 */
186const char *
187__p_class(int class)
188{
189 return (__sym_ntos (__p_class_syms, class, (int *)0));
190}
191DEF_STRONG(__p_class);
192
diff --git a/src/lib/libc/net/res_init.3 b/src/lib/libc/net/res_init.3
deleted file mode 100644
index 3e0cabc358..0000000000
--- a/src/lib/libc/net/res_init.3
+++ /dev/null
@@ -1,441 +0,0 @@
1.\" $OpenBSD: res_init.3,v 1.6 2021/11/24 20:06:32 jca 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. Neither the name of the University nor the names of its contributors
15.\" may be used to endorse or promote products derived from this software
16.\" without specific prior written permission.
17.\"
18.\" THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
19.\" ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
20.\" IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
21.\" ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
22.\" FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
23.\" DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
24.\" OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
25.\" HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
26.\" LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
27.\" OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
28.\" SUCH DAMAGE.
29.\"
30.Dd $Mdocdate: November 24 2021 $
31.Dt RES_INIT 3
32.Os
33.Sh NAME
34.Nm res_query ,
35.Nm res_search ,
36.Nm res_mkquery ,
37.Nm res_send ,
38.Nm res_init ,
39.Nm dn_comp ,
40.Nm dn_expand
41.Nd resolver routines
42.Sh SYNOPSIS
43.In sys/types.h
44.In netinet/in.h
45.In arpa/nameser.h
46.In resolv.h
47.Ft int
48.Fo res_query
49.Fa "const char *dname"
50.Fa "int class"
51.Fa "int type"
52.Fa "unsigned char *answer"
53.Fa "int anslen"
54.Fc
55.Ft int
56.Fo res_search
57.Fa "const char *dname"
58.Fa "int class"
59.Fa "int type"
60.Fa "unsigned char *answer"
61.Fa "int anslen"
62.Fc
63.Ft int
64.Fo res_mkquery
65.Fa "int op"
66.Fa "const char *dname"
67.Fa "int class"
68.Fa "int type"
69.Fa "const unsigned char *data"
70.Fa "int datalen"
71.Fa "const unsigned char *newrr"
72.Fa "unsigned char *buf"
73.Fa "int buflen"
74.Fc
75.Ft int
76.Fo res_send
77.Fa "const unsigned char *msg"
78.Fa "int msglen"
79.Fa "unsigned char *answer"
80.Fa "int anslen"
81.Fc
82.Ft int
83.Fn res_init "void"
84.Ft int
85.Fo dn_comp
86.Fa "const char *exp_dn"
87.Fa "unsigned char *comp_dn"
88.Fa "int length"
89.Fa "unsigned char **dnptrs"
90.Fa "unsigned char **lastdnptr"
91.Fc
92.Ft int
93.Fo dn_expand
94.Fa "const unsigned char *msg"
95.Fa "const unsigned char *eomorig"
96.Fa "const unsigned char *comp_dn"
97.Fa "char *exp_dn"
98.Fa "int length"
99.Fc
100.Sh DESCRIPTION
101These routines are used for making, sending, and interpreting
102query and reply messages with Internet domain name servers.
103.Pp
104Global configuration and state information that is used by the
105resolver routines is kept in the structure
106.Va _res .
107Most of the values have reasonable defaults and can be ignored.
108Options stored in
109.Va _res.options
110are defined in
111.In resolv.h
112and are as follows.
113Options are stored as a simple bit mask containing the bitwise OR
114of the options enabled.
115.Bl -tag -width RES_USE_DNSSEC
116.It Dv RES_INIT
117True if the initial name server address and default domain name are
118initialized (i.e.\&
119.Fn res_init
120has been called).
121.It Dv RES_DEBUG
122Print debugging messages,
123if libc is compiled with
124.Dv DEBUG .
125By default on
126.Ox
127this option does nothing.
128.It Dv RES_AAONLY
129Accept authoritative answers only.
130With this option,
131.Fn res_send
132should continue until it finds an authoritative answer or finds an error.
133On
134.Ox
135this option does nothing.
136.It Dv RES_USEVC
137Use TCP connections for queries instead of UDP datagrams.
138.It Dv RES_PRIMARY
139Query the primary name server only.
140On
141.Ox
142this option does nothing.
143.It Dv RES_IGNTC
144Ignore truncation errors, i.e. don't retry with TCP.
145.It Dv RES_RECURSE
146Set the recursion-desired bit in queries.
147.Pf ( Fn res_send
148does not do iterative queries and expects the name server
149to handle recursion.)
150This option is enabled by default.
151.It Dv RES_DEFNAMES
152If set,
153.Fn res_search
154will append the default domain name to single-component names
155(those that do not contain a dot).
156This option is enabled by default.
157.It Dv RES_STAYOPEN
158Used with
159.Dv RES_USEVC
160to keep the TCP connection open between queries.
161This is useful only in programs that regularly do many queries.
162UDP should be the normal mode used.
163.It Dv RES_DNSRCH
164If this option is set,
165.Fn res_search
166will search for host names in the current domain and in parent domains; see
167.Xr hostname 7 .
168This is used by the standard host lookup routine
169.Xr gethostbyname 3 .
170This option is enabled by default.
171.It Dv RES_INSECURE_1
172Do not require the IP source address on the reply packet
173to be equal to the server's address.
174.It Dv RES_INSECURE_2
175Do not check if the query section of the reply packet
176is equal to that of the query packet.
177.It Dv RES_NOALIASES
178This option has no effect.
179In the past, it turned off the legacy
180.Ev HOSTALIASES
181feature.
182.It Dv RES_TRUSTAD
183If set, the resolver routines will set the AD flag in DNS queries and
184preserve the value of the AD flag in DNS replies.
185If not set, the resolver routines will clear the AD flag in responses.
186Direct use of this option to enable AD bit processing is discouraged.
187Instead the use of trusted name servers should be annotated with
188.Dq options trust-ad
189in
190.Xr resolv.conf 5 .
191This option is automatically enabled if
192.Xr resolv.conf 5
193only lists name servers on localhost.
194.It Dv RES_USE_INET6
195With this option
196.Xr gethostbyname 3
197will return IPv6 addresses if available.
198This option is deprecated; software should use the
199.Xr getaddrinfo 3
200interface instead of modifying the behavior of
201.Xr gethostbyname 3 .
202On some operating systems this option also causes IPv4 addresses to be
203returned as IPv4-mapped IPv6 addresses.
204For example, 10.1.1.1 will be returned as ::ffff:10.1.1.1.
205IPv4-mapped IPv6 addresses are not supported on
206.Ox .
207.It Dv RES_USE_EDNS0
208Attach an OPT pseudo-RR for the EDNS0 extension,
209as specified in RFC 2671.
210This informs DNS servers of a client's receive buffer size,
211allowing them to take advantage of a non-default receive buffer size,
212and thus to send larger replies.
213DNS query packets with the EDNS0 extension are not compatible with
214non-EDNS0 DNS servers.
215.Ox
216uses 4096 bytes as input buffer size.
217.It Dv RES_USE_DNSSEC
218Request that the resolver uses
219Domain Name System Security Extensions (DNSSEC),
220as defined in RFCs 4033, 4034, and 4035.
221The resolver routines will use the EDNS0 extension and set the DNSSEC DO
222flag in queries, asking the name server to signal validated records by
223setting the AD flag in the reply and to attach additional DNSSEC
224records.
225The resolver routines will clear the AD flag in replies unless the name
226servers are considered trusted.
227Also, client applications are often only interested in the value of the
228AD flag, making the additional DNSSEC records a waste of network
229bandwidth.
230See the description for
231.Dq options trust-ad
232in
233.Xr resolv.conf 5 .
234.It Dv RES_USE_CD
235Set the Checking Disabled flag on queries.
236.El
237.Pp
238The
239.Fn res_init
240routine reads the configuration file (if any; see
241.Xr resolv.conf 5 )
242to get the default domain name, search list, and the Internet address
243of the local name server(s).
244If no server is configured, the host running
245the resolver is tried.
246The current domain name is defined by the hostname
247if not specified in the configuration file;
248it can be overridden by the environment variable
249.Ev LOCALDOMAIN .
250This environment variable may contain several blank-separated
251tokens if you wish to override the search list on a per-process basis.
252This is similar to the
253.Ic search
254command in the configuration file.
255Another environment variable
256.Ev RES_OPTIONS
257can be set to override certain internal resolver options which
258are otherwise set by changing fields in the
259.Va _res
260structure or are inherited from the configuration file's
261.Ic options
262command.
263The syntax of the
264.Ev RES_OPTIONS
265environment variable is explained in
266.Xr resolv.conf 5 .
267Initialization normally occurs on the first call
268to one of the following routines.
269.Pp
270The
271.Fn res_query
272function provides an interface to the server query mechanism.
273It constructs a query, sends it to the local server,
274awaits a response, and makes preliminary checks on the reply.
275The query requests information of the specified
276.Fa type
277and
278.Fa class
279for the specified fully qualified domain name
280.Fa dname .
281The reply message is left in the
282.Fa answer
283buffer with length
284.Fa anslen
285supplied by the caller.
286Values for the
287.Fa class
288and
289.Fa type
290fields
291are defined in
292.In arpa/nameser.h .
293.Pp
294The
295.Fn res_search
296routine makes a query and awaits a response like
297.Fn res_query ,
298but in addition, it implements the default and search rules controlled by the
299.Dv RES_DEFNAMES
300and
301.Dv RES_DNSRCH
302options.
303It returns the first successful reply.
304.Pp
305The remaining routines are lower-level routines used by
306.Fn res_query .
307The
308.Fn res_mkquery
309function constructs a standard query message and places it in
310.Fa buf .
311It returns the size of the query, or \-1 if the query is larger than
312.Fa buflen .
313The query type
314.Fa op
315is usually
316.Dv QUERY ,
317but can be any of the query types defined in
318.In arpa/nameser.h .
319The domain name for the query is given by
320.Fa dname .
321.Fa newrr
322is currently unused but is intended for making update messages.
323.Pp
324The
325.Fn res_send
326routine sends a pre-formatted query and returns an answer.
327It will call
328.Fn res_init
329if
330.Dv RES_INIT
331is not set, send the query to the local name server, and
332handle timeouts and retries.
333The length of the reply message is returned, or \-1 if there were errors.
334.Pp
335The
336.Fn dn_comp
337function compresses the domain name
338.Fa exp_dn
339and stores it in
340.Fa comp_dn .
341The size of the compressed name is returned or \-1 if there were errors.
342The size of the array pointed to by
343.Fa comp_dn
344is given by
345.Fa length .
346The compression uses an array of pointers
347.Fa dnptrs
348to previously compressed names in the current message.
349The first pointer points
350to the beginning of the message and the list ends with
351.Dv NULL .
352The limit to the array is specified by
353.Fa lastdnptr .
354A side effect of
355.Fn dn_comp
356is to update the list of pointers for labels inserted into the message
357as the name is compressed.
358If
359.Fa dnptrs
360is
361.Dv NULL ,
362names are not compressed.
363If
364.Fa lastdnptr
365is
366.Dv NULL ,
367the list of labels is not updated.
368.Pp
369The
370.Fn dn_expand
371entry expands the compressed domain name
372.Fa comp_dn
373to a full domain name.
374The compressed name is contained in a query or reply message;
375.Fa msg
376is a pointer to the beginning of the message.
377The uncompressed name is placed in the buffer indicated by
378.Fa exp_dn
379which is of size
380.Fa length .
381The size of compressed name is returned or \-1 if there was an error.
382.Sh FILES
383.Bl -tag -width "/etc/resolv.confXX"
384.It Pa /etc/resolv.conf
385The configuration file.
386.El
387.Sh SEE ALSO
388.Xr gethostbyname 3 ,
389.Xr resolv.conf 5 ,
390.Xr hostname 7
391.Sh STANDARDS
392.Rs
393.%A M. Stahl
394.%D November 1987
395.%R RFC 1032
396.%T Domain Administrators Guide
397.Re
398.Pp
399.Rs
400.%A M. Lottor
401.%D November 1987
402.%R RFC 1033
403.%T Domain Administrators Operations Guide
404.Re
405.Pp
406.Rs
407.%A P. Mockapetris
408.%D November 1987
409.%R RFC 1034
410.%T Domain Names \(en Concepts and Facilities
411.Re
412.Pp
413.Rs
414.%A P. Mockapetris
415.%D November 1987
416.%R RFC 1035
417.%T Domain Names \(en Implementation and Specification
418.Re
419.Pp
420.Rs
421.%A J. Klensin
422.%D October 2008
423.%R RFC 5321
424.%T Simple Mail Transfer Protocol
425.Re
426.Sh HISTORY
427The functions
428.Fn res_mkquery ,
429.Fn res_send ,
430.Fn res_init ,
431.Fn dn_comp ,
432and
433.Fn dn_expand
434appeared in
435.Bx 4.3 .
436The functions
437.Fn res_query
438and
439.Fn res_search
440appeared in
441.Bx 4.3 Tahoe .
diff --git a/src/lib/libc/net/res_random.c b/src/lib/libc/net/res_random.c
deleted file mode 100644
index 447a2409bd..0000000000
--- a/src/lib/libc/net/res_random.c
+++ /dev/null
@@ -1,281 +0,0 @@
1/* $OpenBSD: res_random.c,v 1.27 2024/09/03 18:21:55 op Exp $ */
2
3/*
4 * Copyright 1997 Niels Provos <provos@physnet.uni-hamburg.de>
5 * Copyright 2008 Damien Miller <djm@openbsd.org>
6 * All rights reserved.
7 *
8 * Theo de Raadt <deraadt@openbsd.org> came up with the idea of using
9 * such a mathematical system to generate more random (yet non-repeating)
10 * ids to solve the resolver/named problem. But Niels designed the
11 * actual system based on the constraints.
12 *
13 * Later modified by Damien Miller to wrap the LCG output in a 15-bit
14 * permutation generator based on a Luby-Rackoff block cipher. This
15 * ensures the output is non-repeating and preserves the MSB twiddle
16 * trick, but makes it more resistant to LCG prediction.
17 *
18 * Redistribution and use in source and binary forms, with or without
19 * modification, are permitted provided that the following conditions
20 * are met:
21 * 1. Redistributions of source code must retain the above copyright
22 * notice, this list of conditions and the following disclaimer.
23 * 2. Redistributions in binary form must reproduce the above copyright
24 * notice, this list of conditions and the following disclaimer in the
25 * documentation and/or other materials provided with the distribution.
26 *
27 * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR
28 * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
29 * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
30 * IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT,
31 * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
32 * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
33 * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
34 * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
35 * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF
36 * THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
37 */
38
39/*
40 * seed = random 15bit
41 * n = prime, g0 = generator to n,
42 * j = random so that gcd(j,n-1) == 1
43 * g = g0^j mod n will be a generator again.
44 *
45 * X[0] = random seed.
46 * X[n] = a*X[n-1]+b mod m is a Linear Congruential Generator
47 * with a = 7^(even random) mod m,
48 * b = random with gcd(b,m) == 1
49 * m = 31104 and a maximal period of m-1.
50 *
51 * The transaction id is determined by:
52 * id[n] = seed xor (g^X[n] mod n)
53 *
54 * Effectively the id is restricted to the lower 15 bits, thus
55 * yielding two different cycles by toggling the msb on and off.
56 * This avoids reuse issues caused by reseeding.
57 *
58 * The output of this generator is then randomly permuted though a
59 * custom 15 bit Luby-Rackoff block cipher.
60 */
61
62#include <sys/types.h>
63#include <netinet/in.h>
64#include <sys/time.h>
65#include <resolv.h>
66
67#include <unistd.h>
68#include <stdlib.h>
69#include <string.h>
70
71#include "thread_private.h"
72
73#define RU_OUT 180 /* Time after which will be reseeded */
74#define RU_MAX 30000 /* Uniq cycle, avoid blackjack prediction */
75#define RU_GEN 2 /* Starting generator */
76#define RU_N 32749 /* RU_N-1 = 2*2*3*2729 */
77#define RU_AGEN 7 /* determine ru_a as RU_AGEN^(2*rand) */
78#define RU_M 31104 /* RU_M = 2^7*3^5 - don't change */
79#define RU_ROUNDS 11 /* Number of rounds for permute (odd) */
80
81struct prf_ctx {
82 /* PRF lookup table for odd rounds (7 bits input to 8 bits output) */
83 u_char prf7[(RU_ROUNDS / 2) * (1 << 7)];
84
85 /* PRF lookup table for even rounds (8 bits input to 7 bits output) */
86 u_char prf8[((RU_ROUNDS + 1) / 2) * (1 << 8)];
87};
88
89#define PFAC_N 3
90static const u_int16_t pfacts[PFAC_N] = {
91 2,
92 3,
93 2729
94};
95
96static u_int16_t ru_x;
97static u_int16_t ru_seed, ru_seed2;
98static u_int16_t ru_a, ru_b;
99static u_int16_t ru_g;
100static u_int16_t ru_counter = 0;
101static u_int16_t ru_msb = 0;
102static struct prf_ctx *ru_prf = NULL;
103static time_t ru_reseed;
104static pid_t ru_pid;
105
106static u_int16_t pmod(u_int16_t, u_int16_t, u_int16_t);
107static void res_initid(void);
108
109/*
110 * Do a fast modular exponation, returned value will be in the range
111 * of 0 - (mod-1)
112 */
113static u_int16_t
114pmod(u_int16_t gen, u_int16_t exp, u_int16_t mod)
115{
116 u_int16_t s, t, u;
117
118 s = 1;
119 t = gen;
120 u = exp;
121
122 while (u) {
123 if (u & 1)
124 s = (s * t) % mod;
125 u >>= 1;
126 t = (t * t) % mod;
127 }
128 return (s);
129}
130
131/*
132 * 15-bit permutation based on Luby-Rackoff block cipher
133 */
134static u_int
135permute15(u_int in)
136{
137 int i;
138 u_int left, right, tmp;
139
140 if (ru_prf == NULL)
141 return in;
142
143 left = (in >> 8) & 0x7f;
144 right = in & 0xff;
145
146 /*
147 * Each round swaps the width of left and right. Even rounds have
148 * a 7-bit left, odd rounds have an 8-bit left. Since this uses an
149 * odd number of rounds, left is always 8 bits wide at the end.
150 */
151 for (i = 0; i < RU_ROUNDS; i++) {
152 if ((i & 1) == 0)
153 tmp = ru_prf->prf8[(i << (8 - 1)) | right] & 0x7f;
154 else
155 tmp = ru_prf->prf7[((i - 1) << (7 - 1)) | right];
156 tmp ^= left;
157 left = right;
158 right = tmp;
159 }
160
161 return (right << 8) | left;
162}
163
164/*
165 * Initializes the seed and chooses a suitable generator. Also toggles
166 * the msb flag. The msb flag is used to generate two distinct
167 * cycles of random numbers and thus avoiding reuse of ids.
168 *
169 * This function is called from res_randomid() when needed, an
170 * application does not have to worry about it.
171 */
172static void
173res_initid(void)
174{
175 u_int16_t j, i;
176 u_int32_t tmp;
177 int noprime = 1;
178 struct timespec ts;
179
180 ru_x = arc4random_uniform(RU_M);
181
182 /* 15 bits of random seed */
183 tmp = arc4random();
184 ru_seed = (tmp >> 16) & 0x7FFF;
185 ru_seed2 = tmp & 0x7FFF;
186
187 /* Determine the LCG we use */
188 tmp = arc4random();
189 ru_b = (tmp & 0xfffe) | 1;
190 ru_a = pmod(RU_AGEN, (tmp >> 16) & 0xfffe, RU_M);
191 while (ru_b % 3 == 0)
192 ru_b += 2;
193
194 j = arc4random_uniform(RU_N);
195
196 /*
197 * Do a fast gcd(j,RU_N-1), so we can find a j with
198 * gcd(j, RU_N-1) == 1, giving a new generator for
199 * RU_GEN^j mod RU_N
200 */
201
202 while (noprime) {
203 for (i = 0; i < PFAC_N; i++)
204 if (j % pfacts[i] == 0)
205 break;
206
207 if (i >= PFAC_N)
208 noprime = 0;
209 else
210 j = (j + 1) % RU_N;
211 }
212
213 ru_g = pmod(RU_GEN, j, RU_N);
214 ru_counter = 0;
215
216 /* Initialise PRF for Luby-Rackoff permutation */
217 if (ru_prf == NULL)
218 ru_prf = malloc(sizeof(*ru_prf));
219 if (ru_prf != NULL)
220 arc4random_buf(ru_prf, sizeof(*ru_prf));
221
222 WRAP(clock_gettime)(CLOCK_MONOTONIC, &ts);
223 ru_reseed = ts.tv_sec + RU_OUT;
224 ru_msb = ru_msb == 0x8000 ? 0 : 0x8000;
225}
226
227u_int
228__res_randomid(void)
229{
230 struct timespec ts;
231 pid_t pid;
232 u_int r;
233 static void *randomid_mutex;
234
235 WRAP(clock_gettime)(CLOCK_MONOTONIC, &ts);
236 pid = getpid();
237
238 _MUTEX_LOCK(&randomid_mutex);
239
240 if (ru_counter >= RU_MAX || ts.tv_sec > ru_reseed || pid != ru_pid) {
241 res_initid();
242 ru_pid = pid;
243 }
244
245 /* Linear Congruential Generator */
246 ru_x = (ru_a * ru_x + ru_b) % RU_M;
247 ru_counter++;
248
249 r = permute15(ru_seed ^ pmod(ru_g, ru_seed2 + ru_x, RU_N)) | ru_msb;
250
251 _MUTEX_UNLOCK(&randomid_mutex);
252
253 return (r);
254}
255DEF_STRONG(__res_randomid);
256
257#if 0
258int
259main(int argc, char **argv)
260{
261 int i, n;
262 u_int16_t wert;
263
264 res_initid();
265
266 printf("Generator: %u\n", ru_g);
267 printf("Seed: %u\n", ru_seed);
268 printf("Reseed at %ld\n", ru_reseed);
269 printf("Ru_X: %u\n", ru_x);
270 printf("Ru_A: %u\n", ru_a);
271 printf("Ru_B: %u\n", ru_b);
272
273 n = argc > 1 ? atoi(argv[1]) : 60001;
274 for (i=0;i<n;i++) {
275 wert = res_randomid();
276 printf("%u\n", wert);
277 }
278 return 0;
279}
280#endif
281
diff --git a/src/lib/libc/net/rresvport.c b/src/lib/libc/net/rresvport.c
deleted file mode 100644
index 72c27c3a3f..0000000000
--- a/src/lib/libc/net/rresvport.c
+++ /dev/null
@@ -1,107 +0,0 @@
1/* $OpenBSD: rresvport.c,v 1.12 2019/06/28 13:32:42 deraadt Exp $ */
2/*
3 * Copyright (c) 1995, 1996, 1998 Theo de Raadt. All rights reserved.
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. Neither the name of the University nor the names of its contributors
16 * may be used to endorse or promote products derived from this software
17 * without specific prior written permission.
18 *
19 * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
20 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
21 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
22 * ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
23 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
24 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
25 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
26 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
27 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
28 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
29 * SUCH DAMAGE.
30 */
31
32#include <sys/socket.h>
33#include <sys/stat.h>
34
35#include <netinet/in.h>
36#include <arpa/inet.h>
37
38#include <signal.h>
39#include <fcntl.h>
40#include <netdb.h>
41#include <unistd.h>
42#include <pwd.h>
43#include <errno.h>
44#include <stdio.h>
45#include <ctype.h>
46#include <string.h>
47#include <syslog.h>
48#include <stdlib.h>
49#include <netgroup.h>
50
51int
52rresvport(int *alport)
53{
54 return rresvport_af(alport, AF_INET);
55}
56
57
58int
59rresvport_af(int *alport, int af)
60{
61 struct sockaddr_storage ss;
62 struct sockaddr *sa;
63 u_int16_t *portp;
64 int s;
65
66 bzero(&ss, sizeof ss);
67 sa = (struct sockaddr *)&ss;
68
69 switch (af) {
70 case AF_INET:
71 sa->sa_len = sizeof(struct sockaddr_in);
72 portp = &((struct sockaddr_in *)sa)->sin_port;
73 break;
74 case AF_INET6:
75 sa->sa_len = sizeof(struct sockaddr_in6);
76 portp = &((struct sockaddr_in6 *)sa)->sin6_port;
77 break;
78 default:
79 errno = EPFNOSUPPORT;
80 return (-1);
81 }
82 sa->sa_family = af;
83
84 s = socket(af, SOCK_STREAM, 0);
85 if (s == -1)
86 return (-1);
87
88 *portp = htons(*alport);
89 if (*alport < IPPORT_RESERVED - 1) {
90 if (bind(s, sa, sa->sa_len) != -1)
91 return (s);
92 if (errno != EADDRINUSE) {
93 (void)close(s);
94 return (-1);
95 }
96 }
97
98 *portp = 0;
99 sa->sa_family = af;
100 if (bindresvport_sa(s, sa) == -1) {
101 (void)close(s);
102 return (-1);
103 }
104 *alport = ntohs(*portp);
105 return (s);
106}
107DEF_WEAK(rresvport_af);
diff --git a/src/lib/libc/net/rthdr.c b/src/lib/libc/net/rthdr.c
deleted file mode 100644
index 5ffb13fc87..0000000000
--- a/src/lib/libc/net/rthdr.c
+++ /dev/null
@@ -1,198 +0,0 @@
1/* $OpenBSD: rthdr.c,v 1.13 2022/12/27 17:10:06 jmc Exp $ */
2/* $KAME: rthdr.c,v 1.22 2006/02/09 08:18:58 keiichi Exp $ */
3
4/*
5 * Copyright (C) 1995, 1996, 1997, and 1998 WIDE Project.
6 * All rights reserved.
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. Neither the name of the project 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 PROJECT 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 PROJECT 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
33#include <sys/types.h>
34#include <sys/socket.h>
35
36#include <netinet/in.h>
37#include <netinet/ip6.h>
38
39#include <string.h>
40#include <stdio.h>
41
42/*
43 * RFC3542 (2292bis) API
44 */
45
46socklen_t
47inet6_rth_space(int type, int segments)
48{
49 switch (type) {
50 case IPV6_RTHDR_TYPE_0:
51 return (((segments * 2) + 1) << 3);
52 default:
53 return (0); /* type not supported */
54 }
55}
56DEF_WEAK(inet6_rth_space);
57
58void *
59inet6_rth_init(void *bp, socklen_t bp_len, int type, int segments)
60{
61 struct ip6_rthdr *rth = (struct ip6_rthdr *)bp;
62 struct ip6_rthdr0 *rth0;
63
64 switch (type) {
65 case IPV6_RTHDR_TYPE_0:
66 /* length validation */
67 if (bp_len < inet6_rth_space(IPV6_RTHDR_TYPE_0, segments))
68 return (NULL);
69
70 memset(bp, 0, bp_len);
71 rth0 = (struct ip6_rthdr0 *)rth;
72 rth0->ip6r0_len = segments * 2;
73 rth0->ip6r0_type = IPV6_RTHDR_TYPE_0;
74 rth0->ip6r0_segleft = 0;
75 rth0->ip6r0_reserved = 0;
76 break;
77 default:
78 return (NULL); /* type not supported */
79 }
80
81 return (bp);
82}
83
84int
85inet6_rth_add(void *bp, const struct in6_addr *addr)
86{
87 struct ip6_rthdr *rth = (struct ip6_rthdr *)bp;
88 struct ip6_rthdr0 *rth0;
89 struct in6_addr *nextaddr;
90
91 switch (rth->ip6r_type) {
92 case IPV6_RTHDR_TYPE_0:
93 rth0 = (struct ip6_rthdr0 *)rth;
94 nextaddr = (struct in6_addr *)(rth0 + 1) + rth0->ip6r0_segleft;
95 *nextaddr = *addr;
96 rth0->ip6r0_segleft++;
97 break;
98 default:
99 return (-1); /* type not supported */
100 }
101
102 return (0);
103}
104
105int
106inet6_rth_reverse(const void *in, void *out)
107{
108 struct ip6_rthdr *rth_in = (struct ip6_rthdr *)in;
109 struct ip6_rthdr0 *rth0_in, *rth0_out;
110 int i, segments;
111
112 switch (rth_in->ip6r_type) {
113 case IPV6_RTHDR_TYPE_0:
114 rth0_in = (struct ip6_rthdr0 *)in;
115 rth0_out = (struct ip6_rthdr0 *)out;
116
117 /* parameter validation XXX too paranoid? */
118 if (rth0_in->ip6r0_len % 2)
119 return (-1);
120 segments = rth0_in->ip6r0_len / 2;
121
122 /* we can't use memcpy here, since in and out may overlap */
123 memmove(rth0_out, rth0_in, ((rth0_in->ip6r0_len) + 1) << 3);
124 rth0_out->ip6r0_segleft = segments;
125
126 /* reverse the addresses */
127 for (i = 0; i < segments / 2; i++) {
128 struct in6_addr addr_tmp, *addr1, *addr2;
129
130 addr1 = (struct in6_addr *)(rth0_out + 1) + i;
131 addr2 = (struct in6_addr *)(rth0_out + 1) +
132 (segments - i - 1);
133 addr_tmp = *addr1;
134 *addr1 = *addr2;
135 *addr2 = addr_tmp;
136 }
137
138 break;
139 default:
140 return (-1); /* type not supported */
141 }
142
143 return (0);
144}
145
146int
147inet6_rth_segments(const void *bp)
148{
149 struct ip6_rthdr *rh = (struct ip6_rthdr *)bp;
150 struct ip6_rthdr0 *rh0;
151 int addrs;
152
153 switch (rh->ip6r_type) {
154 case IPV6_RTHDR_TYPE_0:
155 rh0 = (struct ip6_rthdr0 *)bp;
156
157 /*
158 * Validation for a type-0 routing header.
159 * Is this too strict?
160 */
161 if ((rh0->ip6r0_len % 2) != 0 ||
162 (addrs = (rh0->ip6r0_len >> 1)) < rh0->ip6r0_segleft)
163 return (-1);
164
165 return (addrs);
166 default:
167 return (-1); /* unknown type */
168 }
169}
170
171struct in6_addr *
172inet6_rth_getaddr(const void *bp, int idx)
173{
174 struct ip6_rthdr *rh = (struct ip6_rthdr *)bp;
175 struct ip6_rthdr0 *rh0;
176 int addrs;
177
178 switch (rh->ip6r_type) {
179 case IPV6_RTHDR_TYPE_0:
180 rh0 = (struct ip6_rthdr0 *)bp;
181
182 /*
183 * Validation for a type-0 routing header.
184 * Is this too strict?
185 */
186 if ((rh0->ip6r0_len % 2) != 0 ||
187 (addrs = (rh0->ip6r0_len >> 1)) < rh0->ip6r0_segleft)
188 return (NULL);
189
190 if (idx < 0 || addrs <= idx)
191 return (NULL);
192
193 return (((struct in6_addr *)(rh0 + 1)) + idx);
194 default:
195 return (NULL); /* unknown type */
196 break;
197 }
198}
diff --git a/src/lib/libc/net/ruserok.c b/src/lib/libc/net/ruserok.c
deleted file mode 100644
index 291c702060..0000000000
--- a/src/lib/libc/net/ruserok.c
+++ /dev/null
@@ -1,392 +0,0 @@
1/*
2 * Copyright (c) 1995, 1996, 1998 Theo de Raadt. All rights reserved.
3 * Copyright (c) 1983, 1993, 1994
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. Neither the name of the University nor the names of its contributors
15 * may be used to endorse or promote products derived from this software
16 * without specific prior written permission.
17 *
18 * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
19 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
20 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
21 * ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
22 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
23 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
24 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
25 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
26 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
27 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
28 * SUCH DAMAGE.
29 */
30
31#include <sys/socket.h>
32#include <sys/stat.h>
33
34#include <netinet/in.h>
35#include <arpa/inet.h>
36
37#include <ctype.h>
38#include <errno.h>
39#include <fcntl.h>
40#include <limits.h>
41#include <netdb.h>
42#include <netgroup.h>
43#include <pwd.h>
44#include <signal.h>
45#include <stdio.h>
46#include <stdlib.h>
47#include <string.h>
48#include <syslog.h>
49#include <unistd.h>
50
51static int __ivaliduser_sa(FILE *, struct sockaddr *, socklen_t,
52 const char *, const char *);
53static int __icheckhost(struct sockaddr *, socklen_t, const char *);
54static char *__gethostloop(struct sockaddr *, socklen_t);
55static int iruserok_sa(const void *, int, int, const char *, const char *);
56
57int
58ruserok(const char *rhost, int superuser, const char *ruser, const char *luser)
59{
60 struct addrinfo hints, *res, *r;
61 int error;
62
63 memset(&hints, 0, sizeof(hints));
64 hints.ai_family = PF_UNSPEC;
65 hints.ai_socktype = SOCK_DGRAM; /*dummy*/
66 error = getaddrinfo(rhost, "0", &hints, &res);
67 if (error)
68 return (-1);
69
70 for (r = res; r; r = r->ai_next) {
71 if (iruserok_sa(r->ai_addr, r->ai_addrlen, superuser, ruser,
72 luser) == 0) {
73 freeaddrinfo(res);
74 return (0);
75 }
76 }
77 freeaddrinfo(res);
78 return (-1);
79}
80
81int
82iruserok_sa(const void *raddr, int rlen, int superuser, const char *ruser,
83 const char *luser)
84{
85 struct sockaddr *sa;
86 char *cp;
87 struct stat sbuf;
88 struct passwd pwstore, *pwd;
89 FILE *hostf;
90 uid_t uid;
91 int first;
92 char pbuf[PATH_MAX], pwbuf[_PW_BUF_LEN];
93
94 sa = (struct sockaddr *)raddr;
95 first = 1;
96 hostf = superuser ? NULL : fopen(_PATH_HEQUIV, "re");
97again:
98 if (hostf) {
99 if (__ivaliduser_sa(hostf, sa, rlen, luser, ruser) == 0) {
100 (void)fclose(hostf);
101 return (0);
102 }
103 (void)fclose(hostf);
104 }
105 if (first == 1) {
106 int len;
107
108 first = 0;
109 pwd = NULL;
110 getpwnam_r(luser, &pwstore, pwbuf, sizeof(pwbuf), &pwd);
111 if (pwd == NULL)
112 return (-1);
113 len = snprintf(pbuf, sizeof pbuf, "%s/.rhosts", pwd->pw_dir);
114 if (len < 0 || len >= sizeof pbuf)
115 return (-1);
116
117 /*
118 * Change effective uid while opening .rhosts. If root and
119 * reading an NFS mounted file system, can't read files that
120 * are protected read/write owner only.
121 */
122 uid = geteuid();
123 (void)seteuid(pwd->pw_uid);
124 hostf = fopen(pbuf, "re");
125 (void)seteuid(uid);
126
127 if (hostf == NULL)
128 return (-1);
129 /*
130 * If not a regular file, or is owned by someone other than
131 * user or root or if writeable by anyone but the owner, quit.
132 */
133 cp = NULL;
134 if (lstat(pbuf, &sbuf) == -1)
135 cp = ".rhosts lstat failed";
136 else if (!S_ISREG(sbuf.st_mode))
137 cp = ".rhosts not regular file";
138 else if (fstat(fileno(hostf), &sbuf) == -1)
139 cp = ".rhosts fstat failed";
140 else if (sbuf.st_uid && sbuf.st_uid != pwd->pw_uid)
141 cp = "bad .rhosts owner";
142 else if (sbuf.st_mode & (S_IWGRP|S_IWOTH))
143 cp = ".rhosts writable by other than owner";
144 /* If there were any problems, quit. */
145 if (cp) {
146 (void)fclose(hostf);
147 return (-1);
148 }
149 goto again;
150 }
151 return (-1);
152}
153
154int
155__ivaliduser_sa(FILE *hostf, struct sockaddr *raddr, socklen_t salen,
156 const char *luser, const char *ruser)
157{
158 char *user, *p;
159 char *buf;
160 const char *auser, *ahost;
161 int hostok, userok;
162 char *rhost = (char *)-1;
163 char domain[HOST_NAME_MAX+1];
164 size_t buflen;
165
166 getdomainname(domain, sizeof(domain));
167
168 while ((buf = fgetln(hostf, &buflen))) {
169 p = buf;
170 if (*p == '#')
171 continue;
172 while (p < buf + buflen && *p != '\n' && *p != ' ' && *p != '\t') {
173 if (!isprint((unsigned char)*p))
174 goto bail;
175 *p = isupper((unsigned char)*p) ?
176 tolower((unsigned char)*p) : *p;
177 p++;
178 }
179 if (p >= buf + buflen)
180 continue;
181 if (*p == ' ' || *p == '\t') {
182 *p++ = '\0';
183 while (p < buf + buflen && (*p == ' ' || *p == '\t'))
184 p++;
185 if (p >= buf + buflen)
186 continue;
187 user = p;
188 while (p < buf + buflen && *p != '\n' && *p != ' ' &&
189 *p != '\t') {
190 if (!isprint((unsigned char)*p))
191 goto bail;
192 p++;
193 }
194 } else
195 user = p;
196 *p = '\0';
197
198 if (p == buf)
199 continue;
200
201 auser = *user ? user : luser;
202 ahost = buf;
203
204 if (strlen(ahost) > HOST_NAME_MAX)
205 continue;
206
207 /*
208 * innetgr() must lookup a hostname (we do not attempt
209 * to change the semantics so that netgroups may have
210 * #.#.#.# addresses in the list.)
211 */
212 if (ahost[0] == '+')
213 switch (ahost[1]) {
214 case '\0':
215 hostok = 1;
216 break;
217 case '@':
218 if (rhost == (char *)-1)
219 rhost = __gethostloop(raddr, salen);
220 hostok = 0;
221 if (rhost)
222 hostok = innetgr(&ahost[2], rhost,
223 NULL, domain);
224 break;
225 default:
226 hostok = __icheckhost(raddr, salen, &ahost[1]);
227 break;
228 }
229 else if (ahost[0] == '-')
230 switch (ahost[1]) {
231 case '\0':
232 hostok = -1;
233 break;
234 case '@':
235 if (rhost == (char *)-1)
236 rhost = __gethostloop(raddr, salen);
237 hostok = 0;
238 if (rhost)
239 hostok = -innetgr(&ahost[2], rhost,
240 NULL, domain);
241 break;
242 default:
243 hostok = -__icheckhost(raddr, salen, &ahost[1]);
244 break;
245 }
246 else
247 hostok = __icheckhost(raddr, salen, ahost);
248
249
250 if (auser[0] == '+')
251 switch (auser[1]) {
252 case '\0':
253 userok = 1;
254 break;
255 case '@':
256 userok = innetgr(&auser[2], NULL, ruser,
257 domain);
258 break;
259 default:
260 userok = strcmp(ruser, &auser[1]) ? 0 : 1;
261 break;
262 }
263 else if (auser[0] == '-')
264 switch (auser[1]) {
265 case '\0':
266 userok = -1;
267 break;
268 case '@':
269 userok = -innetgr(&auser[2], NULL, ruser,
270 domain);
271 break;
272 default:
273 userok = strcmp(ruser, &auser[1]) ? 0 : -1;
274 break;
275 }
276 else
277 userok = strcmp(ruser, auser) ? 0 : 1;
278
279 /* Check if one component did not match */
280 if (hostok == 0 || userok == 0)
281 continue;
282
283 /* Check if we got a forbidden pair */
284 if (userok <= -1 || hostok <= -1)
285 return (-1);
286
287 /* Check if we got a valid pair */
288 if (hostok >= 1 && userok >= 1)
289 return (0);
290 }
291bail:
292 return (-1);
293}
294
295/*
296 * Returns "true" if match, 0 if no match. If we do not find any
297 * semblance of an A->PTR->A loop, allow a simple #.#.#.# match to work.
298 */
299static int
300__icheckhost(struct sockaddr *raddr, socklen_t salen, const char *lhost)
301{
302 struct addrinfo hints, *res, *r;
303 char h1[NI_MAXHOST], h2[NI_MAXHOST];
304 int error;
305 const int niflags = NI_NUMERICHOST;
306
307 h1[0] = '\0';
308 if (getnameinfo(raddr, salen, h1, sizeof(h1), NULL, 0,
309 niflags) != 0)
310 return (0);
311
312 /* Resolve laddr into sockaddr */
313 memset(&hints, 0, sizeof(hints));
314 hints.ai_family = raddr->sa_family;
315 hints.ai_socktype = SOCK_DGRAM; /*dummy*/
316 res = NULL;
317 error = getaddrinfo(lhost, "0", &hints, &res);
318 if (error)
319 return (0);
320
321 /*
322 * Try string comparisons between raddr and laddr.
323 */
324 for (r = res; r; r = r->ai_next) {
325 h2[0] = '\0';
326 if (getnameinfo(r->ai_addr, r->ai_addrlen, h2, sizeof(h2),
327 NULL, 0, niflags) != 0)
328 continue;
329 if (strcmp(h1, h2) == 0) {
330 freeaddrinfo(res);
331 return (1);
332 }
333 }
334
335 /* No match. */
336 freeaddrinfo(res);
337 return (0);
338}
339
340/*
341 * Return the hostname associated with the supplied address.
342 * Do a reverse lookup as well for security. If a loop cannot
343 * be found, pack the result of inet_ntoa() into the string.
344 */
345static char *
346__gethostloop(struct sockaddr *raddr, socklen_t salen)
347{
348 static char remotehost[NI_MAXHOST];
349 char h1[NI_MAXHOST], h2[NI_MAXHOST];
350 struct addrinfo hints, *res, *r;
351 int error;
352 const int niflags = NI_NUMERICHOST;
353
354 h1[0] = remotehost[0] = '\0';
355 if (getnameinfo(raddr, salen, remotehost, sizeof(remotehost),
356 NULL, 0, NI_NAMEREQD) != 0)
357 return (NULL);
358 if (getnameinfo(raddr, salen, h1, sizeof(h1), NULL, 0,
359 niflags) != 0)
360 return (NULL);
361
362 /*
363 * Look up the name and check that the supplied
364 * address is in the list
365 */
366 memset(&hints, 0, sizeof(hints));
367 hints.ai_family = raddr->sa_family;
368 hints.ai_socktype = SOCK_DGRAM; /*dummy*/
369 hints.ai_flags = AI_CANONNAME;
370 res = NULL;
371 error = getaddrinfo(remotehost, "0", &hints, &res);
372 if (error)
373 return (NULL);
374
375 for (r = res; r; r = r->ai_next) {
376 h2[0] = '\0';
377 if (getnameinfo(r->ai_addr, r->ai_addrlen, h2, sizeof(h2),
378 NULL, 0, niflags) != 0)
379 continue;
380 if (strcmp(h1, h2) == 0) {
381 freeaddrinfo(res);
382 return (remotehost);
383 }
384 }
385
386 /*
387 * either the DNS administrator has made a configuration
388 * mistake, or someone has attempted to spoof us
389 */
390 freeaddrinfo(res);
391 return (NULL);
392}
diff --git a/src/lib/libc/net/send.c b/src/lib/libc/net/send.c
deleted file mode 100644
index 1fad3bd171..0000000000
--- a/src/lib/libc/net/send.c
+++ /dev/null
@@ -1,41 +0,0 @@
1/* $OpenBSD: send.c,v 1.6 2015/10/04 07:17:27 guenther Exp $ */
2/*
3 * Copyright (c) 1988, 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. Neither the name of the University nor the names of its contributors
15 * may be used to endorse or promote products derived from this software
16 * without specific prior written permission.
17 *
18 * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
19 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
20 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
21 * ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
22 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
23 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
24 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
25 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
26 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
27 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
28 * SUCH DAMAGE.
29 */
30
31#include <sys/types.h>
32#include <sys/socket.h>
33
34#include <stddef.h>
35
36ssize_t
37send(int s, const void *msg, size_t len, int flags)
38{
39 return (sendto(s, msg, len, flags, NULL, 0));
40}
41DEF_WEAK(send);
diff --git a/src/lib/libc/net/sockatmark.3 b/src/lib/libc/net/sockatmark.3
deleted file mode 100644
index 524536c12b..0000000000
--- a/src/lib/libc/net/sockatmark.3
+++ /dev/null
@@ -1,122 +0,0 @@
1.\" Copyright (c) 2002 William C. Fenner. All rights reserved.
2.\"
3.\" Redistribution and use in source and binary forms, with or without
4.\" modification, are permitted provided that the following conditions
5.\" are met:
6.\" 1. Redistributions of source code must retain the above copyright
7.\" notice, this list of conditions and the following disclaimer.
8.\" 2. Redistributions in binary form must reproduce the above copyright
9.\" notice, this list of conditions and the following disclaimer in the
10.\" documentation and/or other materials provided with the distribution.
11.\"
12.\" THIS SOFTWARE IS PROVIDED BY THE CONTRIBUTORS ``AS IS'' AND
13.\" ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
14.\" IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
15.\" ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
16.\" FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
17.\" DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
18.\" OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
19.\" HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
20.\" LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
21.\" OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
22.\" SUCH DAMAGE.
23.\"
24.\" From FreeBSD: r108087 2002-12-19 01:40:28 -0800
25.\" $OpenBSD: sockatmark.3,v 1.2 2019/06/20 14:19:25 deraadt Exp $
26.\"
27.Dd $Mdocdate: June 20 2019 $
28.Dt SOCKATMARK 3
29.Os
30.Sh NAME
31.Nm sockatmark
32.Nd determine whether the read pointer is at the out-of-band data mark
33.Sh SYNOPSIS
34.In sys/socket.h
35.Ft int
36.Fn sockatmark "int s"
37.Sh DESCRIPTION
38The
39.Fn sockatmark
40function returns 1 if the read pointer for the socket
41.Fa s
42is currently at the out-of-band data mark.
43Otherwise, it returns 0 if the socket doesn't have an out-of-band
44data mark or if there is normal data to be received before the mark.
45.Sh RETURN VALUES
46Upon successful completion, the
47.Fn sockatmark
48function returns the value 1 if the read pointer is pointing at
49the out-of-band data mark, 0 if it is not.
50Otherwise the value \-1 is returned
51and the global variable
52.Va errno
53is set to
54indicate the error.
55.Sh EXAMPLES
56The routine used in the historical remote login process to flush
57output on receipt of an interrupt or quit signal is shown below.
58It reads the normal data up to the mark (to discard it),
59then reads the out-of-band byte.
60.Bd -literal -offset indent
61#include <sys/socket.h>
62\&...
63oob()
64{
65 int mark;
66 char waste[BUFSIZ];
67
68 for (;;) {
69 if ((mark = sockatmark(rem)) == -1) {
70 perror("sockatmark");
71 break;
72 }
73 if (mark)
74 break;
75 (void) read(rem, waste, sizeof (waste));
76 }
77 if (recv(rem, &mark, 1, MSG_OOB) == -1) {
78 perror("recv");
79 ...
80 }
81 ...
82}
83.Ed
84.Sh ERRORS
85The
86.Fn sockatmark
87call fails if:
88.Bl -tag -width Er
89.It Bq Er EBADF
90.Fa s
91is not a valid descriptor.
92.It Bq Er ENOTTY
93.Fa s
94is valid but does not refer to a socket.
95.El
96.Sh SEE ALSO
97.Xr recv 2 ,
98.Xr send 2
99.Sh STANDARDS
100The
101.Fn sockatmark
102function conforms to
103.St -p1003.1-2008 .
104.Sh HISTORY
105The
106.Fn sockatmark
107function was introduced by
108.St -p1003.1-2001
109to standardize the historical
110.Dv SIOCATMARK
111.Xr ioctl 2 .
112The
113.Fn sockatmark
114function appeared in
115.Ox 5.7 .
116.Pp
117The
118.Er ENOTTY
119error is returned instead of the usual
120.Er ENOTSOCK
121error to match the historical behavior of
122.Dv SIOCATMARK .
diff --git a/src/lib/libc/net/sockatmark.c b/src/lib/libc/net/sockatmark.c
deleted file mode 100644
index a43e7af3aa..0000000000
--- a/src/lib/libc/net/sockatmark.c
+++ /dev/null
@@ -1,40 +0,0 @@
1/*
2 * Copyright (c) 2002 William C. Fenner. 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 *
13 * THIS SOFTWARE IS PROVIDED BY THE CONTRIBUTORS ``AS IS'' AND
14 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
15 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
16 * ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
17 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
18 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
19 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
20 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
21 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
22 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
23 * SUCH DAMAGE.
24 *
25 * $FreeBSD$
26 */
27
28#include <sys/socket.h>
29#include <sys/sockio.h>
30#include <sys/ioctl.h>
31
32int
33sockatmark(int s)
34{
35 int atmark;
36
37 if (ioctl(s, SIOCATMARK, &atmark) == -1)
38 return (-1);
39 return (atmark);
40}
diff --git a/src/lib/libc/net/vars6.c b/src/lib/libc/net/vars6.c
deleted file mode 100644
index a12932a26e..0000000000
--- a/src/lib/libc/net/vars6.c
+++ /dev/null
@@ -1,42 +0,0 @@
1/* $OpenBSD: vars6.c,v 1.3 2014/08/31 19:20:44 bluhm Exp $ */
2
3/*
4 * Copyright (C) 1995, 1996, 1997, and 1998 WIDE Project.
5 * 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. Neither the name of the project nor the names of its contributors
16 * may be used to endorse or promote products derived from this software
17 * without specific prior written permission.
18 *
19 * THIS SOFTWARE IS PROVIDED BY THE PROJECT AND CONTRIBUTORS ``AS IS'' AND
20 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
21 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
22 * ARE DISCLAIMED. IN NO EVENT SHALL THE PROJECT OR CONTRIBUTORS BE LIABLE
23 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
24 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
25 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
26 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
27 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
28 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
29 * SUCH DAMAGE.
30 */
31
32#include <sys/types.h>
33#include <netinet/in.h>
34
35/*
36 * Definitions of some constant IPv6 addresses.
37 */
38const struct in6_addr in6addr_any = IN6ADDR_ANY_INIT;
39const struct in6_addr in6addr_loopback = IN6ADDR_LOOPBACK_INIT;
40const struct in6_addr in6addr_intfacelocal_allnodes = IN6ADDR_INTFACELOCAL_ALLNODES_INIT;
41const struct in6_addr in6addr_linklocal_allnodes = IN6ADDR_LINKLOCAL_ALLNODES_INIT;
42const struct in6_addr in6addr_linklocal_allrouters = IN6ADDR_LINKLOCAL_ALLROUTERS_INIT;
diff --git a/src/lib/libc/stdlib/Makefile.inc b/src/lib/libc/stdlib/Makefile.inc
deleted file mode 100644
index f5e9a9fe4e..0000000000
--- a/src/lib/libc/stdlib/Makefile.inc
+++ /dev/null
@@ -1,33 +0,0 @@
1# $OpenBSD: Makefile.inc,v 1.66 2024/01/19 19:45:02 millert Exp $
2
3# stdlib sources
4.PATH: ${LIBCSRCDIR}/arch/${MACHINE_CPU}/stdlib ${LIBCSRCDIR}/stdlib
5
6SRCS+= a64l.c abort.c atexit.c atoi.c atof.c atol.c atoll.c bsearch.c \
7 exit.c ecvt.c gcvt.c getenv.c getopt_long.c \
8 getsubopt.c hcreate.c heapsort.c imaxabs.c imaxdiv.c insque.c \
9 l64a.c llabs.c lldiv.c lsearch.c malloc.c __mktemp4.c mkdtemp.c \
10 mkstemp.c mktemp.c reallocarray.c merge.c posix_pty.c qsort.c \
11 radixsort.c rand.c random.c realpath.c remque.c setenv.c strtoimax.c \
12 strtol.c strtoll.c strtonum.c strtoul.c strtoull.c strtoumax.c \
13 system.c \
14 tfind.c thread_atexit.c tsearch.c \
15 _rand48.c drand48.c erand48.c jrand48.c \
16 lcong48.c lrand48.c mrand48.c nrand48.c seed48.c srand48.c \
17 _Exit.c icdb.c
18
19.if (${MACHINE_CPU} == "i386")
20SRCS+= abs.S div.S labs.S ldiv.S
21.elif (${MACHINE_CPU} == "alpha")
22# XXX should be .S's
23SRCS+= abs.c div.c labs.c ldiv.c
24.else
25SRCS+= abs.c div.c labs.c ldiv.c
26.endif
27
28MAN+= a64l.3 abort.3 abs.3 alloca.3 atexit.3 atof.3 atoi.3 atol.3 atoll.3 \
29 bsearch.3 div.3 ecvt.3 exit.3 getenv.3 getopt.3 getopt_long.3 \
30 getsubopt.3 hcreate.3 imaxabs.3 imaxdiv.3 insque.3 labs.3 ldiv.3 \
31 lldiv.3 lsearch.3 malloc.3 mktemp.3 posix_memalign.3 posix_openpt.3 \
32 ptsname.3 qsort.3 radixsort.3 rand48.3 rand.3 random.3 realpath.3 \
33 strtod.3 strtonum.3 strtol.3 strtoul.3 system.3 tsearch.3
diff --git a/src/lib/libc/stdlib/_Exit.c b/src/lib/libc/stdlib/_Exit.c
deleted file mode 100644
index ccf64c2e87..0000000000
--- a/src/lib/libc/stdlib/_Exit.c
+++ /dev/null
@@ -1,22 +0,0 @@
1/* $OpenBSD: _Exit.c,v 1.3 2013/04/03 03:39:29 guenther Exp $ */
2
3/*
4 * Placed in the public domain by Todd C. Miller on January 21, 2004.
5 */
6
7#include <stdlib.h>
8#include <unistd.h>
9
10/*
11 * _Exit() is the ISO/ANSI C99 equivalent of the POSIX _exit() function.
12 * No atexit() handlers are called and no signal handlers are run.
13 * Whether or not stdio buffers are flushed or temporary files are removed
14 * is implementation-dependent in C99. Indeed, POSIX specifies that
15 * _Exit() must *not* flush stdio buffers or remove temporary files, but
16 * rather must behave exactly like _exit()
17 */
18void
19_Exit(int status)
20{
21 _exit(status);
22}
diff --git a/src/lib/libc/stdlib/__mktemp4.c b/src/lib/libc/stdlib/__mktemp4.c
deleted file mode 100644
index 4b4500018b..0000000000
--- a/src/lib/libc/stdlib/__mktemp4.c
+++ /dev/null
@@ -1,83 +0,0 @@
1/* $OpenBSD: __mktemp4.c,v 1.1 2024/01/19 19:45:02 millert Exp $ */
2/*
3 * Copyright (c) 1996-1998, 2008 Theo de Raadt
4 * Copyright (c) 1997, 2008-2009, 2024 Todd C. Miller
5 *
6 * Permission to use, copy, modify, and distribute this software for any
7 * purpose with or without fee is hereby granted, provided that the above
8 * copyright notice and this permission notice appear in all copies.
9 *
10 * THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES
11 * WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF
12 * MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR
13 * ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES
14 * WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN
15 * ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF
16 * OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE.
17 */
18
19#include <errno.h>
20#include <limits.h>
21#include <stdlib.h>
22#include <string.h>
23
24#define TEMPCHARS "ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789"
25#define NUM_CHARS (sizeof(TEMPCHARS) - 1)
26#define MIN_X 6
27
28#ifndef nitems
29#define nitems(_a) (sizeof((_a)) / sizeof((_a)[0]))
30#endif
31
32/*
33 * Internal driver for the mktemp(3) family of functions.
34 * The supplied callback does the actual work of testing or
35 * creating the file/directory.
36 */
37int
38__mktemp4(char *path, int slen, int flags, int (*cb)(const char *, int))
39{
40 char *start, *cp, *ep;
41 const char tempchars[] = TEMPCHARS;
42 unsigned int tries;
43 size_t len;
44 int ret;
45
46 len = strlen(path);
47 if (len < MIN_X || slen < 0 || (size_t)slen > len - MIN_X) {
48 errno = EINVAL;
49 return -1;
50 }
51 ep = path + len - slen;
52
53 for (start = ep; start > path && start[-1] == 'X'; start--)
54 ;
55 if (ep - start < MIN_X) {
56 errno = EINVAL;
57 return -1;
58 }
59
60 tries = INT_MAX;
61 do {
62 cp = start;
63 do {
64 unsigned short rbuf[16];
65 unsigned int i;
66
67 /*
68 * Avoid lots of arc4random() calls by using
69 * a buffer sized for up to 16 Xs at a time.
70 */
71 arc4random_buf(rbuf, sizeof(rbuf));
72 for (i = 0; i < nitems(rbuf) && cp != ep; i++)
73 *cp++ = tempchars[rbuf[i] % NUM_CHARS];
74 } while (cp != ep);
75
76 ret = cb(path, flags);
77 if (ret != -1 || errno != EEXIST)
78 return ret;
79 } while (--tries);
80
81 errno = EEXIST;
82 return -1;
83}
diff --git a/src/lib/libc/stdlib/_rand48.c b/src/lib/libc/stdlib/_rand48.c
deleted file mode 100644
index 7c950f7cee..0000000000
--- a/src/lib/libc/stdlib/_rand48.c
+++ /dev/null
@@ -1,47 +0,0 @@
1/* $OpenBSD: _rand48.c,v 1.3 2005/08/08 08:05:36 espie Exp $ */
2/*
3 * Copyright (c) 1993 Martin Birgmeier
4 * All rights reserved.
5 *
6 * You may redistribute unmodified or modified versions of this source
7 * code provided that the above copyright notice and this and the
8 * following conditions are retained.
9 *
10 * This software is provided ``as is'', and comes with no warranties
11 * of any kind. I shall in no event be liable for anything that happens
12 * to anyone/anything when using this software.
13 */
14
15#include "rand48.h"
16
17unsigned short __rand48_seed[3] = {
18 RAND48_SEED_0,
19 RAND48_SEED_1,
20 RAND48_SEED_2
21};
22unsigned short __rand48_mult[3] = {
23 RAND48_MULT_0,
24 RAND48_MULT_1,
25 RAND48_MULT_2
26};
27unsigned short __rand48_add = RAND48_ADD;
28
29void
30__dorand48(unsigned short xseed[3])
31{
32 unsigned long accu;
33 unsigned short temp[2];
34
35 accu = (unsigned long) __rand48_mult[0] * (unsigned long) xseed[0] +
36 (unsigned long) __rand48_add;
37 temp[0] = (unsigned short) accu; /* lower 16 bits */
38 accu >>= sizeof(unsigned short) * 8;
39 accu += (unsigned long) __rand48_mult[0] * (unsigned long) xseed[1] +
40 (unsigned long) __rand48_mult[1] * (unsigned long) xseed[0];
41 temp[1] = (unsigned short) accu; /* middle 16 bits */
42 accu >>= sizeof(unsigned short) * 8;
43 accu += __rand48_mult[0] * xseed[2] + __rand48_mult[1] * xseed[1] + __rand48_mult[2] * xseed[0];
44 xseed[0] = temp[0];
45 xseed[1] = temp[1];
46 xseed[2] = (unsigned short) accu;
47}
diff --git a/src/lib/libc/stdlib/a64l.3 b/src/lib/libc/stdlib/a64l.3
deleted file mode 100644
index c34af99c88..0000000000
--- a/src/lib/libc/stdlib/a64l.3
+++ /dev/null
@@ -1,133 +0,0 @@
1.\" $OpenBSD: a64l.3,v 1.13 2019/01/25 00:19:25 millert Exp $
2.\"
3.\" Copyright (c) 1997 Todd C. Miller <millert@openbsd.org>
4.\"
5.\" Permission to use, copy, modify, and distribute this software for any
6.\" purpose with or without fee is hereby granted, provided that the above
7.\" copyright notice and this permission notice appear in all copies.
8.\"
9.\" THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES
10.\" WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF
11.\" MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR
12.\" ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES
13.\" WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN
14.\" ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF
15.\" OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE.
16.\"
17.Dd $Mdocdate: January 25 2019 $
18.Dt A64L 3
19.Os
20.Sh NAME
21.Nm a64l ,
22.Nm l64a
23.Nd convert between 32-bit integer and radix-64 ASCII string
24.Sh SYNOPSIS
25.In stdlib.h
26.Ft long
27.Fn a64l "const char *s"
28.Ft char *
29.Fn l64a "long l"
30.Sh DESCRIPTION
31The
32.Fn a64l
33and
34.Fn l64a
35functions are used to maintain numbers stored in radix-64
36.Tn ASCII
37characters.
38This is a notation by which 32-bit integers
39can be represented by up to six characters; each character represents a
40.Dq digit
41in a radix-64 notation.
42.Pp
43The characters used to represent digits are
44.Ql \&.
45for 0,
46.Ql /
47for 1,
48.Ql 0
49through
50.Ql 9
51for 2-11,
52.Ql A
53through
54.Ql Z
55for 12-37, and
56.Ql a
57through
58.Ql z
59for 38-63.
60.Pp
61The
62.Fn a64l
63function takes a pointer to a NUL-terminated radix-64 representation
64and returns a corresponding 32-bit value.
65If the string pointed to by
66.Fa s
67contains more than six characters,
68.Fn a64l
69will use the first six.
70.Fn a64l
71scans the character string from left to right, decoding
72each character as a 6-bit radix-64 number.
73If a long integer is
74larger than 32 bits, the return value will be sign-extended.
75.Pp
76.Fn l64a
77takes a long integer argument
78.Fa l
79and returns a pointer to the corresponding radix-64 representation.
80.Sh RETURN VALUES
81On success,
82.Fn a64l
83returns a 32-bit representation of
84.Fa s .
85If
86.Fa s
87is a null pointer or if it contains digits other than those described above,
88.Fn a64l
89returns \-1 and sets the global variable
90.Va errno
91to
92.Er EINVAL .
93.Pp
94On success,
95.Fn l64a
96returns a pointer to a string containing the radix-64 representation of
97.Fa l .
98If
99.Fa l
100is 0,
101.Fn l64a
102returns a pointer to the empty string.
103If
104.Fa l
105is negative,
106.Fn l64a
107returns a null pointer and sets the global variable
108.Va errno
109to
110.Er EINVAL .
111.Sh STANDARDS
112The
113.Fn a64l
114and
115.Fn l64a
116functions conform to
117.St -xpg4.2 .
118.Sh CAVEATS
119The value returned by
120.Fn l64a
121is a pointer into a static buffer, the contents of which
122will be overwritten by subsequent calls.
123.Pp
124The value returned by
125.Fn a64l
126may be incorrect if the value is too large; for that reason, only strings
127that resulted from a call to
128.Fn l64a
129should be used to call
130.Fn a64l .
131.Pp
132If a long integer is larger than 32 bits, only the low-order
13332 bits are used.
diff --git a/src/lib/libc/stdlib/a64l.c b/src/lib/libc/stdlib/a64l.c
deleted file mode 100644
index 5312929c6f..0000000000
--- a/src/lib/libc/stdlib/a64l.c
+++ /dev/null
@@ -1,42 +0,0 @@
1/* $OpenBSD: a64l.c,v 1.5 2005/08/08 08:05:36 espie Exp $ */
2/*
3 * Written by J.T. Conklin <jtc@netbsd.org>.
4 * Public domain.
5 */
6
7#include <errno.h>
8#include <stdlib.h>
9
10long
11a64l(const char *s)
12{
13 long value, digit, shift;
14 int i;
15
16 if (s == NULL) {
17 errno = EINVAL;
18 return(-1L);
19 }
20
21 value = 0;
22 shift = 0;
23 for (i = 0; *s && i < 6; i++, s++) {
24 if (*s >= '.' && *s <= '/')
25 digit = *s - '.';
26 else if (*s >= '0' && *s <= '9')
27 digit = *s - '0' + 2;
28 else if (*s >= 'A' && *s <= 'Z')
29 digit = *s - 'A' + 12;
30 else if (*s >= 'a' && *s <= 'z')
31 digit = *s - 'a' + 38;
32 else {
33 errno = EINVAL;
34 return(-1L);
35 }
36
37 value |= digit << shift;
38 shift += 6;
39 }
40
41 return(value);
42}
diff --git a/src/lib/libc/stdlib/abort.3 b/src/lib/libc/stdlib/abort.3
deleted file mode 100644
index 2f15cd827c..0000000000
--- a/src/lib/libc/stdlib/abort.3
+++ /dev/null
@@ -1,77 +0,0 @@
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. 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.\" $OpenBSD: abort.3,v 1.11 2014/05/14 21:54:20 tedu Exp $
33.\"
34.Dd $Mdocdate: May 14 2014 $
35.Dt ABORT 3
36.Os
37.Sh NAME
38.Nm abort
39.Nd cause abnormal program termination
40.Sh SYNOPSIS
41.In stdlib.h
42.Ft void
43.Fn abort void
44.Sh DESCRIPTION
45The
46.Fn abort
47function causes abnormal program termination to occur, unless the signal
48.Dv SIGABRT
49is being caught and the signal handler does not return.
50.Pp
51Some implementations may flush output streams before terminating.
52This implementation does not.
53.Sh RETURN VALUES
54The
55.Fn abort
56function never returns.
57.Sh SEE ALSO
58.Xr sigaction 2 ,
59.Xr exit 3
60.Sh STANDARDS
61The
62.Fn abort
63function conforms to
64.St -p1003.1-90 .
65.Sh HISTORY
66The
67.Fn abort
68function first appeared in
69.At v5 .
70.Pp
71Historically, previous standards required
72.Fn abort
73to flush and close output streams, but this conflicted with the requirement
74that
75.Fn abort
76be async signal safe.
77As a result, the flushing requirement was dropped.
diff --git a/src/lib/libc/stdlib/abort.c b/src/lib/libc/stdlib/abort.c
deleted file mode 100644
index 6c22ecd2b0..0000000000
--- a/src/lib/libc/stdlib/abort.c
+++ /dev/null
@@ -1,64 +0,0 @@
1/* $OpenBSD: abort.c,v 1.21 2017/08/12 22:59:52 guenther Exp $ */
2/*
3 * Copyright (c) 1985 Regents of the University of California.
4 * 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. Neither the name of the University nor the names of its contributors
15 * may be used to endorse or promote products derived from this software
16 * without specific prior written permission.
17 *
18 * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
19 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
20 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
21 * ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
22 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
23 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
24 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
25 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
26 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
27 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
28 * SUCH DAMAGE.
29 */
30
31#include <signal.h>
32#include <stdlib.h>
33#include <string.h>
34#include <unistd.h>
35
36
37void
38abort(void)
39{
40 sigset_t mask;
41 struct sigaction sa;
42
43 sigfillset(&mask);
44 /*
45 * don't block SIGABRT to give any handler a chance; we ignore
46 * any errors -- X311J doesn't allow abort to return anyway.
47 */
48 sigdelset(&mask, SIGABRT);
49 (void)sigprocmask(SIG_SETMASK, &mask, NULL);
50
51 (void)raise(SIGABRT);
52
53 /*
54 * if SIGABRT ignored, or caught and the handler returns, do
55 * it again, only harder.
56 */
57 memset(&sa, 0, sizeof(sa));
58 sa.sa_handler = SIG_DFL;
59 (void)sigaction(SIGABRT, &sa, NULL);
60 (void)sigprocmask(SIG_SETMASK, &mask, NULL);
61 (void)raise(SIGABRT);
62 _exit(1);
63}
64DEF_STRONG(abort);
diff --git a/src/lib/libc/stdlib/abs.3 b/src/lib/libc/stdlib/abs.3
deleted file mode 100644
index afacc985df..0000000000
--- a/src/lib/libc/stdlib/abs.3
+++ /dev/null
@@ -1,75 +0,0 @@
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. 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.\" $OpenBSD: abs.3,v 1.12 2019/01/18 07:32:17 schwarze Exp $
33.\"
34.Dd $Mdocdate: January 18 2019 $
35.Dt ABS 3
36.Os
37.Sh NAME
38.Nm abs
39.Nd integer absolute value function
40.Sh SYNOPSIS
41.In limits.h
42.In stdlib.h
43.Ft int
44.Fn abs "int j"
45.Sh DESCRIPTION
46The
47.Fn abs
48function computes the absolute value of the integer
49.Fa j .
50.Sh RETURN VALUES
51The
52.Fn abs
53function returns the absolute value.
54.Sh SEE ALSO
55.Xr cabs 3 ,
56.Xr floor 3 ,
57.Xr hypot 3 ,
58.Xr imaxabs 3 ,
59.Xr labs 3
60.Sh STANDARDS
61The
62.Fn abs
63function conforms to
64.St -ansiC .
65.Sh HISTORY
66The
67.Fn abs
68function first appeared in
69.At v6 .
70.Sh CAVEATS
71The result of applying
72.Fn abs
73to
74.Dv INT_MIN
75is undefined.
diff --git a/src/lib/libc/stdlib/abs.c b/src/lib/libc/stdlib/abs.c
deleted file mode 100644
index 0e39cc5536..0000000000
--- a/src/lib/libc/stdlib/abs.c
+++ /dev/null
@@ -1,38 +0,0 @@
1/* $OpenBSD: abs.c,v 1.6 2015/09/13 08:31:47 guenther Exp $ */
2/*-
3 * Copyright (c) 1990 The Regents of the University of California.
4 * 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. Neither the name of the University nor the names of its contributors
15 * may be used to endorse or promote products derived from this software
16 * without specific prior written permission.
17 *
18 * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
19 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
20 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
21 * ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
22 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
23 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
24 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
25 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
26 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
27 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
28 * SUCH DAMAGE.
29 */
30
31#include <stdlib.h>
32
33int
34abs(int j)
35{
36 return(j < 0 ? -j : j);
37}
38DEF_STRONG(abs);
diff --git a/src/lib/libc/stdlib/alloca.3 b/src/lib/libc/stdlib/alloca.3
deleted file mode 100644
index 5252ba586f..0000000000
--- a/src/lib/libc/stdlib/alloca.3
+++ /dev/null
@@ -1,74 +0,0 @@
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. Neither the name of the University nor the names of its contributors
13.\" may be used to endorse or promote products derived from this software
14.\" without specific prior written permission.
15.\"
16.\" THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
17.\" ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
18.\" IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
19.\" ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
20.\" FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
21.\" DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
22.\" OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
23.\" HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
24.\" LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
25.\" OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
26.\" SUCH DAMAGE.
27.\"
28.\" $OpenBSD: alloca.3,v 1.14 2015/01/17 18:01:43 tedu Exp $
29.\"
30.Dd $Mdocdate: January 17 2015 $
31.Dt ALLOCA 3
32.Os
33.Sh NAME
34.Nm alloca
35.Nd memory allocator
36.Sh SYNOPSIS
37.In stdlib.h
38.Ft void *
39.Fn alloca "size_t size"
40.Sh DESCRIPTION
41The
42.Fn alloca
43function allocates
44.Fa size
45bytes of space in the stack frame of the caller.
46This temporary space is automatically freed on return.
47.Sh RETURN VALUES
48The
49.Fn alloca
50function returns a pointer to the beginning of the allocated space.
51.Sh SEE ALSO
52.Xr pagesize 1 ,
53.Xr brk 2 ,
54.Xr malloc 3
55.\" .Sh HISTORY
56.\" The
57.\" .Fn alloca
58.\" function appeared in
59.\" .Bx ?? .
60.\" The function appeared in 32v, pwb and pwb.2 and in 3bsd 4bsd
61.\" The first man page (or link to a man page that I can find at the
62.\" moment is 4.3...
63.Sh CAVEATS
64The
65.Fn alloca
66function is unsafe because it cannot ensure that the pointer
67returned points to a valid and usable block of memory.
68The allocation made may exceed the bounds of the stack, or even go
69further into other objects in memory, and
70.Fn alloca
71cannot determine such an error.
72Avoid
73.Fn alloca
74with large unbounded allocations.
diff --git a/src/lib/libc/stdlib/atexit.3 b/src/lib/libc/stdlib/atexit.3
deleted file mode 100644
index 3a7e0d978a..0000000000
--- a/src/lib/libc/stdlib/atexit.3
+++ /dev/null
@@ -1,97 +0,0 @@
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. 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.\" $OpenBSD: atexit.3,v 1.13 2022/02/06 00:29:02 jsg Exp $
33.\"
34.Dd $Mdocdate: February 6 2022 $
35.Dt ATEXIT 3
36.Os
37.Sh NAME
38.Nm atexit
39.Nd register a function to be called on exit
40.Sh SYNOPSIS
41.In stdlib.h
42.Ft int
43.Fn atexit "void (*function)(void)"
44.Sh DESCRIPTION
45The
46.Fn atexit
47function registers the given
48.Fa function
49to be called at program exit, whether via
50.Xr exit 3
51or via return from the program's
52.Fn main .
53Functions so registered are called in reverse order;
54no arguments are passed.
55At least 32 functions can always be registered,
56and more are allowed as long as sufficient memory can be allocated.
57.Pp
58If a shared object is unloaded from process memory using
59.Xr dlclose 3 ,
60then any functions registered by calling
61.Fn atexit
62from that shared object will be called in reverse order and unregistered.
63Note that it is the source of the call to
64.Fn atexit
65that matters, not the source of the function that was registered.
66.Pp
67.Fn atexit
68is very difficult to use correctly without creating
69.Xr exit 3 Ns -time
70races.
71Unless absolutely necessary, avoid using it.
72.Sh RETURN VALUES
73The
74.Nm
75function returns the value 0 if successful; otherwise a non-zero
76value is returned and the global variable
77.Va errno
78is set to indicate the error.
79.Sh ERRORS
80.Bl -tag -width Er
81.It Bq Er ENOMEM
82No memory was available to add the function to the list.
83The existing list of functions is unmodified.
84.El
85.Sh SEE ALSO
86.Xr dlclose 3 ,
87.Xr exit 3
88.Sh STANDARDS
89The
90.Fn atexit
91function conforms to
92.St -ansiC .
93.Pp
94Setting
95.Va errno
96on error and the behavior when a shared object is unloaded
97are extensions to that standard.
diff --git a/src/lib/libc/stdlib/atexit.c b/src/lib/libc/stdlib/atexit.c
deleted file mode 100644
index 3334617b65..0000000000
--- a/src/lib/libc/stdlib/atexit.c
+++ /dev/null
@@ -1,256 +0,0 @@
1/* $OpenBSD: atexit.c,v 1.29 2022/12/27 17:10:06 jmc Exp $ */
2/*
3 * Copyright (c) 2002 Daniel Hartmeier
4 * 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 *
10 * - Redistributions of source code must retain the above copyright
11 * notice, this list of conditions and the following disclaimer.
12 * - Redistributions in binary form must reproduce the above
13 * copyright notice, this list of conditions and the following
14 * disclaimer in the documentation and/or other materials provided
15 * with the distribution.
16 *
17 * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
18 * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
19 * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS
20 * FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE
21 * COPYRIGHT HOLDERS OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT,
22 * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING,
23 * BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
24 * LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER
25 * CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
26 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN
27 * ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
28 * POSSIBILITY OF SUCH DAMAGE.
29 *
30 */
31
32#include <sys/types.h>
33#include <sys/mman.h>
34#include <stdlib.h>
35#include <string.h>
36#include <tib.h>
37#include <unistd.h>
38
39#include "atexit.h"
40#include "atfork.h"
41#include "thread_private.h"
42
43struct atexit *__atexit;
44static int restartloop;
45
46/* define and initialize the list */
47struct atfork_listhead _atfork_list = TAILQ_HEAD_INITIALIZER(_atfork_list);
48
49
50/*
51 * Function pointers are stored in a linked list of pages. The list
52 * is initially empty, and pages are allocated on demand. The first
53 * function pointer in the first allocated page (the last one in
54 * the linked list) is reserved for the cleanup function.
55 *
56 * Outside the following functions, all pages are mprotect()'ed
57 * to prevent unintentional/malicious corruption.
58 */
59
60/*
61 * Register a function to be performed at exit or when a shared object
62 * with the given dso handle is unloaded dynamically. Also used as
63 * the backend for atexit(). For more info on this API, see:
64 *
65 * http://www.codesourcery.com/cxx-abi/abi.html#dso-dtor
66 */
67int
68__cxa_atexit(void (*func)(void *), void *arg, void *dso)
69{
70 struct atexit *p;
71 struct atexit_fn *fnp;
72 int pgsize = getpagesize();
73 int ret = -1;
74
75 if (pgsize < sizeof(*p))
76 return (-1);
77 _ATEXIT_LOCK();
78 p = __atexit;
79 if (p != NULL) {
80 if (p->ind + 1 >= p->max)
81 p = NULL;
82 else if (mprotect(p, pgsize, PROT_READ | PROT_WRITE))
83 goto unlock;
84 }
85 if (p == NULL) {
86 p = mmap(NULL, pgsize, PROT_READ | PROT_WRITE,
87 MAP_ANON | MAP_PRIVATE, -1, 0);
88 if (p == MAP_FAILED)
89 goto unlock;
90 if (__atexit == NULL) {
91 memset(&p->fns[0], 0, sizeof(p->fns[0]));
92 p->ind = 1;
93 } else
94 p->ind = 0;
95 p->max = (pgsize - ((char *)&p->fns[0] - (char *)p)) /
96 sizeof(p->fns[0]);
97 p->next = __atexit;
98 __atexit = p;
99 }
100 fnp = &p->fns[p->ind++];
101 fnp->fn_ptr = func;
102 fnp->fn_arg = arg;
103 fnp->fn_dso = dso;
104 if (mprotect(p, pgsize, PROT_READ))
105 goto unlock;
106 restartloop = 1;
107 ret = 0;
108unlock:
109 _ATEXIT_UNLOCK();
110 return (ret);
111}
112DEF_STRONG(__cxa_atexit);
113
114/*
115 * Copy of atexit() used by libc and anything statically linked into the
116 * executable. This passes NULL for the dso, so the callbacks are only
117 * invoked by exit() and not dlclose()
118 */
119int
120atexit(void (*fn)(void))
121{
122 return (__cxa_atexit((void (*)(void *))fn, NULL, NULL));
123}
124DEF_STRONG(atexit);
125
126void
127_thread_finalize(void)
128{
129 struct tib *tib = TIB_GET();
130
131 while (tib->tib_atexit) {
132 struct thread_atexit_fn *fnp = tib->tib_atexit;
133 tib->tib_atexit = fnp->next;
134 fnp->func(fnp->arg);
135 free(fnp);
136 }
137}
138
139/*
140 * Call all handlers registered with __cxa_atexit() for the shared
141 * object owning 'dso'.
142 * Note: if 'dso' is NULL, then all remaining handlers are called.
143 */
144void
145__cxa_finalize(void *dso)
146{
147 struct atexit *p, *q;
148 struct atexit_fn fn;
149 int n, pgsize = getpagesize();
150 static int call_depth;
151
152 if (dso == NULL)
153 _thread_finalize();
154
155 _ATEXIT_LOCK();
156 call_depth++;
157
158restart:
159 restartloop = 0;
160 for (p = __atexit; p != NULL; p = p->next) {
161 for (n = p->ind; --n >= 0;) {
162 if (p->fns[n].fn_ptr == NULL)
163 continue; /* already called */
164 if (dso != NULL && dso != p->fns[n].fn_dso)
165 continue; /* wrong DSO */
166
167 /*
168 * Mark handler as having been already called to avoid
169 * dupes and loops, then call the appropriate function.
170 */
171 fn = p->fns[n];
172 if (mprotect(p, pgsize, PROT_READ | PROT_WRITE) == 0) {
173 p->fns[n].fn_ptr = NULL;
174 mprotect(p, pgsize, PROT_READ);
175 }
176 _ATEXIT_UNLOCK();
177 (*fn.fn_ptr)(fn.fn_arg);
178 _ATEXIT_LOCK();
179 if (restartloop)
180 goto restart;
181 }
182 }
183
184 call_depth--;
185
186 /*
187 * If called via exit(), unmap the pages since we have now run
188 * all the handlers. We defer this until calldepth == 0 so that
189 * we don't unmap things prematurely if called recursively.
190 */
191 if (dso == NULL && call_depth == 0) {
192 for (p = __atexit; p != NULL; ) {
193 q = p;
194 p = p->next;
195 munmap(q, pgsize);
196 }
197 __atexit = NULL;
198 }
199 _ATEXIT_UNLOCK();
200
201 /*
202 * If unloading a DSO, unregister any atfork handlers registered
203 * by it. Skip the locking if the list is currently empty.
204 */
205 if (dso != NULL && TAILQ_FIRST(&_atfork_list) != NULL) {
206 struct atfork_fn *af, *afnext;
207
208 _ATFORK_LOCK();
209 TAILQ_FOREACH_SAFE(af, &_atfork_list, fn_next, afnext)
210 if (af->fn_dso == dso) {
211 TAILQ_REMOVE(&_atfork_list, af, fn_next);
212 free(af);
213 }
214 _ATFORK_UNLOCK();
215
216 }
217}
218DEF_STRONG(__cxa_finalize);
219
220/*
221 * Register the cleanup function
222 */
223void
224__atexit_register_cleanup(void (*func)(void))
225{
226 struct atexit *p;
227 int pgsize = getpagesize();
228
229 if (pgsize < sizeof(*p))
230 return;
231 _ATEXIT_LOCK();
232 p = __atexit;
233 while (p != NULL && p->next != NULL)
234 p = p->next;
235 if (p == NULL) {
236 p = mmap(NULL, pgsize, PROT_READ | PROT_WRITE,
237 MAP_ANON | MAP_PRIVATE, -1, 0);
238 if (p == MAP_FAILED)
239 goto unlock;
240 p->ind = 1;
241 p->max = (pgsize - ((char *)&p->fns[0] - (char *)p)) /
242 sizeof(p->fns[0]);
243 p->next = NULL;
244 __atexit = p;
245 } else {
246 if (mprotect(p, pgsize, PROT_READ | PROT_WRITE))
247 goto unlock;
248 }
249 p->fns[0].fn_ptr = (void (*)(void *))func;
250 p->fns[0].fn_arg = NULL;
251 p->fns[0].fn_dso = NULL;
252 mprotect(p, pgsize, PROT_READ);
253 restartloop = 1;
254unlock:
255 _ATEXIT_UNLOCK();
256}
diff --git a/src/lib/libc/stdlib/atexit.h b/src/lib/libc/stdlib/atexit.h
deleted file mode 100644
index f2fa7bd83f..0000000000
--- a/src/lib/libc/stdlib/atexit.h
+++ /dev/null
@@ -1,61 +0,0 @@
1/* $OpenBSD: atexit.h,v 1.12 2017/12/16 20:06:56 guenther Exp $ */
2
3/*
4 * Copyright (c) 2002 Daniel Hartmeier
5 * 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 *
11 * - Redistributions of source code must retain the above copyright
12 * notice, this list of conditions and the following disclaimer.
13 * - Redistributions in binary form must reproduce the above
14 * copyright notice, this list of conditions and the following
15 * disclaimer in the documentation and/or other materials provided
16 * with the distribution.
17 *
18 * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
19 * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
20 * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS
21 * FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE
22 * COPYRIGHT HOLDERS OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT,
23 * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING,
24 * BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
25 * LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER
26 * CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
27 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN
28 * ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
29 * POSSIBILITY OF SUCH DAMAGE.
30 *
31 */
32
33struct atexit {
34 struct atexit *next; /* next in list */
35 int ind; /* next index in this table */
36 int max; /* max entries >= ATEXIT_SIZE */
37 struct atexit_fn {
38 void (*fn_ptr)(void *);
39 void *fn_arg; /* argument for CXA callback */
40 void *fn_dso; /* shared module handle */
41 } fns[1]; /* the table itself */
42};
43
44/* a chain of these are hung off each thread's TIB's tib_atexit member */
45struct thread_atexit_fn {
46 void (*func)(void *);
47 void *arg;
48 struct thread_atexit_fn *next;
49};
50
51__BEGIN_HIDDEN_DECLS
52extern struct atexit *__atexit; /* points to head of LIFO stack */
53__END_HIDDEN_DECLS
54
55int __cxa_atexit(void (*)(void *), void *, void *);
56int __cxa_thread_atexit(void (*)(void *), void *, void *);
57void __cxa_finalize(void *);
58
59PROTO_NORMAL(__cxa_atexit);
60PROTO_STD_DEPRECATED(__cxa_thread_atexit);
61PROTO_NORMAL(__cxa_finalize);
diff --git a/src/lib/libc/stdlib/atof.3 b/src/lib/libc/stdlib/atof.3
deleted file mode 100644
index 7d1f09d1a4..0000000000
--- a/src/lib/libc/stdlib/atof.3
+++ /dev/null
@@ -1,81 +0,0 @@
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. 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.\" $OpenBSD: atof.3,v 1.10 2022/09/11 06:38:11 jmc Exp $
33.\"
34.Dd $Mdocdate: September 11 2022 $
35.Dt ATOF 3
36.Os
37.Sh NAME
38.Nm atof
39.Nd convert ASCII string to double
40.Sh SYNOPSIS
41.In stdlib.h
42.Ft double
43.Fn atof "const char *nptr"
44.Sh DESCRIPTION
45The
46.Fn atof
47function converts the initial portion of the string pointed to by
48.Fa nptr
49to
50.Vt double
51representation.
52.Pp
53It is equivalent to:
54.Bd -literal -offset indent
55strtod(nptr, (char **)NULL);
56.Ed
57.Sh SEE ALSO
58.Xr atoi 3 ,
59.Xr atol 3 ,
60.Xr strtod 3 ,
61.Xr strtol 3 ,
62.Xr strtoul 3
63.Sh STANDARDS
64The
65.Fn atof
66function conforms to
67.St -ansiC .
68.Sh HISTORY
69An
70.Fn atof
71function first appeared in
72.At v1 .
73.Sh CAVEATS
74On systems other than
75.Ox ,
76the
77.Dv LC_NUMERIC
78.Xr locale 1
79category can cause parsing failures; see CAVEATS in
80.Xr setlocale 3
81for details.
diff --git a/src/lib/libc/stdlib/atof.c b/src/lib/libc/stdlib/atof.c
deleted file mode 100644
index d14b58b070..0000000000
--- a/src/lib/libc/stdlib/atof.c
+++ /dev/null
@@ -1,37 +0,0 @@
1/* $OpenBSD: atof.c,v 1.5 2005/08/08 08:05:36 espie Exp $ */
2/*
3 * Copyright (c) 1988 The Regents of the University of California.
4 * 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. Neither the name of the University nor the names of its contributors
15 * may be used to endorse or promote products derived from this software
16 * without specific prior written permission.
17 *
18 * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
19 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
20 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
21 * ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
22 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
23 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
24 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
25 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
26 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
27 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
28 * SUCH DAMAGE.
29 */
30
31#include <stdlib.h>
32
33double
34atof(const char *ascii)
35{
36 return(strtod(ascii, (char **)NULL));
37}
diff --git a/src/lib/libc/stdlib/atoi.3 b/src/lib/libc/stdlib/atoi.3
deleted file mode 100644
index 92d8de93a6..0000000000
--- a/src/lib/libc/stdlib/atoi.3
+++ /dev/null
@@ -1,89 +0,0 @@
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. 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.\" $OpenBSD: atoi.3,v 1.13 2022/09/11 06:38:11 jmc Exp $
33.\"
34.Dd $Mdocdate: September 11 2022 $
35.Dt ATOI 3
36.Os
37.Sh NAME
38.Nm atoi
39.Nd convert ASCII string to integer
40.Sh SYNOPSIS
41.In stdlib.h
42.Ft int
43.Fn atoi "const char *nptr"
44.Sh DESCRIPTION
45The
46.Fn atoi
47function converts the initial portion of the string pointed to by
48.Fa nptr
49to
50.Vt integer
51representation.
52.Pp
53It is equivalent to:
54.Bd -literal -offset indent
55(int)strtol(nptr, (char **)NULL, 10);
56.Ed
57.Sh SEE ALSO
58.Xr atof 3 ,
59.Xr atol 3 ,
60.Xr strtod 3 ,
61.Xr strtol 3 ,
62.Xr strtonum 3 ,
63.Xr strtoul 3
64.Sh STANDARDS
65The
66.Fn atoi
67function conforms to
68.St -ansiC .
69.Sh HISTORY
70An
71.Fn atoi
72function first appeared in
73.At v1 .
74.Sh CAVEATS
75.Nm
76does no overflow checking, handles unsigned numbers poorly,
77and handles strings containing trailing extra characters
78(like
79.Dq "123abc" )
80poorly.
81Careful use of
82.Xr strtol 3
83and
84.Xr strtoul 3
85can alleviate these problems,
86but
87.Xr strtonum 3
88can be used to convert numbers from strings much more safely
89and easily.
diff --git a/src/lib/libc/stdlib/atoi.c b/src/lib/libc/stdlib/atoi.c
deleted file mode 100644
index 7c9eb1331b..0000000000
--- a/src/lib/libc/stdlib/atoi.c
+++ /dev/null
@@ -1,38 +0,0 @@
1/* $OpenBSD: atoi.c,v 1.6 2015/09/13 08:31:47 guenther Exp $ */
2/*
3 * Copyright (c) 1988 Regents of the University of California.
4 * 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. Neither the name of the University nor the names of its contributors
15 * may be used to endorse or promote products derived from this software
16 * without specific prior written permission.
17 *
18 * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
19 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
20 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
21 * ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
22 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
23 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
24 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
25 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
26 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
27 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
28 * SUCH DAMAGE.
29 */
30
31#include <stdlib.h>
32
33int
34atoi(const char *str)
35{
36 return((int)strtol(str, (char **)NULL, 10));
37}
38DEF_STRONG(atoi);
diff --git a/src/lib/libc/stdlib/atol.3 b/src/lib/libc/stdlib/atol.3
deleted file mode 100644
index f67ca7da6c..0000000000
--- a/src/lib/libc/stdlib/atol.3
+++ /dev/null
@@ -1,68 +0,0 @@
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. 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.\" $OpenBSD: atol.3,v 1.11 2022/09/11 06:38:11 jmc Exp $
33.\"
34.Dd $Mdocdate: September 11 2022 $
35.Dt ATOL 3
36.Os
37.Sh NAME
38.Nm atol
39.Nd convert ASCII string to long integer
40.Sh SYNOPSIS
41.In stdlib.h
42.Ft long
43.Fn atol "const char *nptr"
44.Sh DESCRIPTION
45The
46.Fn atol
47function converts the initial portion of the string pointed to by
48.Fa nptr
49to
50.Vt long integer
51representation.
52.Pp
53It is equivalent to:
54.Bd -literal -offset indent
55strtol(nptr, (char **)NULL, 10);
56.Ed
57.Sh SEE ALSO
58.Xr atof 3 ,
59.Xr atoi 3 ,
60.Xr atoll 3 ,
61.Xr strtod 3 ,
62.Xr strtol 3 ,
63.Xr strtoul 3
64.Sh STANDARDS
65The
66.Fn atol
67function conforms to
68.St -isoC-99 .
diff --git a/src/lib/libc/stdlib/atol.c b/src/lib/libc/stdlib/atol.c
deleted file mode 100644
index 1970804401..0000000000
--- a/src/lib/libc/stdlib/atol.c
+++ /dev/null
@@ -1,37 +0,0 @@
1/* $OpenBSD: atol.c,v 1.5 2005/08/08 08:05:36 espie Exp $ */
2/*
3 * Copyright (c) 1988 Regents of the University of California.
4 * 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. Neither the name of the University nor the names of its contributors
15 * may be used to endorse or promote products derived from this software
16 * without specific prior written permission.
17 *
18 * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
19 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
20 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
21 * ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
22 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
23 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
24 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
25 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
26 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
27 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
28 * SUCH DAMAGE.
29 */
30
31#include <stdlib.h>
32
33long
34atol(const char *str)
35{
36 return(strtol(str, (char **)NULL, 10));
37}
diff --git a/src/lib/libc/stdlib/atoll.3 b/src/lib/libc/stdlib/atoll.3
deleted file mode 100644
index c2b606dda9..0000000000
--- a/src/lib/libc/stdlib/atoll.3
+++ /dev/null
@@ -1,68 +0,0 @@
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. 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.\" $OpenBSD: atoll.3,v 1.9 2022/09/11 06:38:11 jmc Exp $
33.\"
34.Dd $Mdocdate: September 11 2022 $
35.Dt ATOLL 3
36.Os
37.Sh NAME
38.Nm atoll
39.Nd convert ASCII string to long long integer
40.Sh SYNOPSIS
41.In stdlib.h
42.Ft long long
43.Fn atoll "const char *nptr"
44.Sh DESCRIPTION
45The
46.Fn atoll
47function converts the initial portion of the string pointed to by
48.Fa nptr
49to
50.Vt long long integer
51representation.
52.Pp
53It is equivalent to:
54.Bd -literal -offset indent
55strtoll(nptr, (char **)NULL, 10);
56.Ed
57.Sh SEE ALSO
58.Xr atof 3 ,
59.Xr atoi 3 ,
60.Xr atol 3 ,
61.Xr strtod 3 ,
62.Xr strtol 3 ,
63.Xr strtoul 3
64.Sh STANDARDS
65The
66.Fn atoll
67function conforms to
68.St -isoC-99 .
diff --git a/src/lib/libc/stdlib/atoll.c b/src/lib/libc/stdlib/atoll.c
deleted file mode 100644
index 32caf299e4..0000000000
--- a/src/lib/libc/stdlib/atoll.c
+++ /dev/null
@@ -1,37 +0,0 @@
1/* $OpenBSD: atoll.c,v 1.4 2021/10/24 10:05:23 jsg Exp $ */
2/*
3 * Copyright (c) 1988 Regents of the University of California.
4 * 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. Neither the name of the University nor the names of its contributors
15 * may be used to endorse or promote products derived from this software
16 * without specific prior written permission.
17 *
18 * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
19 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
20 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
21 * ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
22 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
23 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
24 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
25 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
26 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
27 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
28 * SUCH DAMAGE.
29 */
30
31#include <stdlib.h>
32
33long long
34atoll(const char *str)
35{
36 return(strtoll(str, (char **)NULL, 10));
37}
diff --git a/src/lib/libc/stdlib/bsearch.3 b/src/lib/libc/stdlib/bsearch.3
deleted file mode 100644
index 9c66eac0fe..0000000000
--- a/src/lib/libc/stdlib/bsearch.3
+++ /dev/null
@@ -1,84 +0,0 @@
1.\" Copyright (c) 1990, 1991, 1993, 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.\" 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. 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.\" $OpenBSD: bsearch.3,v 1.10 2015/11/30 17:03:05 jmc Exp $
33.\"
34.Dd $Mdocdate: November 30 2015 $
35.Dt BSEARCH 3
36.Os
37.Sh NAME
38.Nm bsearch
39.Nd binary search of a sorted table
40.Sh SYNOPSIS
41.In stdlib.h
42.Ft void *
43.Fn bsearch "const void *key" "const void *base" "size_t nmemb" "size_t size" "int (*compar)(const void *, const void *)"
44.Sh DESCRIPTION
45The
46.Fn bsearch
47function searches an array of
48.Fa nmemb
49objects, the initial member of which is
50pointed to by
51.Fa base ,
52for a member that matches the object pointed to by
53.Fa key .
54The size of each member of the array is specified by
55.Fa size .
56.Pp
57The contents of the array should be in ascending sorted order according
58to the comparison function referenced by
59.Fa compar .
60The
61.Fa compar
62routine is expected to have two arguments which point to the
63.Fa key
64object and to an array member, in that order, and should return an integer
65less than, equal to, or greater than zero if the
66.Fa key
67object is found, respectively, to be less than, to match, or be
68greater than the array member.
69.Sh RETURN VALUES
70The
71.Fn bsearch
72function returns a pointer to a matching member of the array, or a null
73pointer if no match is found.
74If two members compare as equal, which member is matched is unspecified.
75.Sh SEE ALSO
76.Xr dbopen 3 ,
77.Xr lsearch 3 ,
78.Xr qsort 3 ,
79.Xr tsearch 3
80.Sh STANDARDS
81The
82.Fn bsearch
83function conforms to
84.St -ansiC .
diff --git a/src/lib/libc/stdlib/bsearch.c b/src/lib/libc/stdlib/bsearch.c
deleted file mode 100644
index 6df44d6b4f..0000000000
--- a/src/lib/libc/stdlib/bsearch.c
+++ /dev/null
@@ -1,69 +0,0 @@
1/* $OpenBSD: bsearch.c,v 1.9 2021/12/02 20:58:01 cheloha Exp $ */
2/*
3 * Copyright (c) 1990 Regents of the University of California.
4 * 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. Neither the name of the University nor the names of its contributors
15 * may be used to endorse or promote products derived from this software
16 * without specific prior written permission.
17 *
18 * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
19 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
20 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
21 * ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
22 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
23 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
24 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
25 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
26 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
27 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
28 * SUCH DAMAGE.
29 */
30
31#include <stdlib.h>
32
33/*
34 * Perform a binary search.
35 *
36 * The code below is a bit sneaky. After a comparison fails, we
37 * divide the work in half by moving either left or right. If lim
38 * is odd, moving left simply involves halving lim: e.g., when lim
39 * is 5 we look at item 2, so we change lim to 2 so that we will
40 * look at items 0 & 1. If lim is even, the same applies. If lim
41 * is odd, moving right again involves halving lim, this time moving
42 * the base up one item past p: e.g., when lim is 5 we change base
43 * to item 3 and make lim 2 so that we will look at items 3 and 4.
44 * If lim is even, however, we have to shrink it by one before
45 * halving: e.g., when lim is 4, we still looked at item 2, so we
46 * have to make lim 3, then halve, obtaining 1, so that we will only
47 * look at item 3.
48 */
49void *
50bsearch(const void *key, const void *base0, size_t nmemb, size_t size,
51 int (*compar)(const void *, const void *))
52{
53 const char *base = base0;
54 const void *p;
55 size_t lim;
56 int cmp;
57
58 for (lim = nmemb; lim != 0; lim >>= 1) {
59 p = base + (lim >> 1) * size;
60 cmp = (*compar)(key, p);
61 if (cmp == 0)
62 return ((void *)p);
63 if (cmp > 0) { /* key > p: move right */
64 base = (char *)p + size;
65 lim--;
66 } /* else move left */
67 }
68 return (NULL);
69}
diff --git a/src/lib/libc/stdlib/div.3 b/src/lib/libc/stdlib/div.3
deleted file mode 100644
index b4b42ba8ec..0000000000
--- a/src/lib/libc/stdlib/div.3
+++ /dev/null
@@ -1,63 +0,0 @@
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. Neither the name of the University nor the names of its contributors
15.\" may be used to endorse or promote products derived from this software
16.\" without specific prior written permission.
17.\"
18.\" THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
19.\" ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
20.\" IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
21.\" ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
22.\" FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
23.\" DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
24.\" OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
25.\" HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
26.\" LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
27.\" OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
28.\" SUCH DAMAGE.
29.\"
30.\" $OpenBSD: div.3,v 1.13 2022/09/11 06:38:11 jmc Exp $
31.\"
32.Dd $Mdocdate: September 11 2022 $
33.Dt DIV 3
34.Os
35.Sh NAME
36.Nm div
37.Nd return quotient and remainder from division
38.Sh SYNOPSIS
39.In stdlib.h
40.Ft div_t
41.Fn div "int num" "int denom"
42.Sh DESCRIPTION
43The
44.Fn div
45function computes the value
46.Fa num Ns / Ns Fa denom
47and returns the quotient and remainder in a structure named
48.Fa div_t
49that contains two
50.Vt int
51members named
52.Fa quot
53and
54.Fa rem .
55.Sh SEE ALSO
56.Xr imaxdiv 3 ,
57.Xr ldiv 3 ,
58.Xr lldiv 3
59.Sh STANDARDS
60The
61.Fn div
62function conforms to
63.St -ansiC .
diff --git a/src/lib/libc/stdlib/div.c b/src/lib/libc/stdlib/div.c
deleted file mode 100644
index 5e6164f0bb..0000000000
--- a/src/lib/libc/stdlib/div.c
+++ /dev/null
@@ -1,72 +0,0 @@
1/* $OpenBSD: div.c,v 1.7 2022/12/27 17:10:06 jmc Exp $ */
2/*
3 * Copyright (c) 1990 Regents of the University of California.
4 * All rights reserved.
5 *
6 * This code is derived from software contributed to Berkeley by
7 * Chris Torek.
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. 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#include <stdlib.h> /* div_t */
35
36div_t
37div(int num, int denom)
38{
39 div_t r;
40
41 r.quot = num / denom;
42 r.rem = num % denom;
43 /*
44 * The ANSI standard says that |r.quot| <= |n/d|, where
45 * n/d is to be computed in infinite precision. In other
46 * words, we should always truncate the quotient towards
47 * 0, never -infinity.
48 *
49 * Machine division and remainder may work either way when
50 * one or both of n or d is negative. If only one is
51 * negative and r.quot has been truncated towards -inf,
52 * r.rem will have the same sign as denom and the opposite
53 * sign of num; if both are negative and r.quot has been
54 * truncated towards -inf, r.rem will be positive (will
55 * have the opposite sign of num). These are considered
56 * `wrong'.
57 *
58 * If both are num and denom are positive, r will always
59 * be positive.
60 *
61 * This all boils down to:
62 * if num >= 0, but r.rem < 0, we got the wrong answer.
63 * In that case, to get the right answer, add 1 to r.quot and
64 * subtract denom from r.rem.
65 */
66 if (num >= 0 && r.rem < 0) {
67 r.quot++;
68 r.rem -= denom;
69 }
70 return (r);
71}
72DEF_STRONG(div);
diff --git a/src/lib/libc/stdlib/drand48.c b/src/lib/libc/stdlib/drand48.c
deleted file mode 100644
index 429e4cf378..0000000000
--- a/src/lib/libc/stdlib/drand48.c
+++ /dev/null
@@ -1,30 +0,0 @@
1/* $OpenBSD: drand48.c,v 1.7 2015/09/14 13:30:17 guenther Exp $ */
2/*
3 * Copyright (c) 1993 Martin Birgmeier
4 * All rights reserved.
5 *
6 * You may redistribute unmodified or modified versions of this source
7 * code provided that the above copyright notice and this and the
8 * following conditions are retained.
9 *
10 * This software is provided ``as is'', and comes with no warranties
11 * of any kind. I shall in no event be liable for anything that happens
12 * to anyone/anything when using this software.
13 */
14
15#include <math.h>
16#include "rand48.h"
17
18double
19drand48(void)
20{
21 if (__rand48_deterministic == 0) {
22 unsigned short rseed[3];
23
24 arc4random_buf(rseed, sizeof rseed);
25 return ldexp((double) rseed[0], -48) +
26 ldexp((double) rseed[1], -32) +
27 ldexp((double) rseed[2], -16);
28 }
29 return erand48(__rand48_seed);
30}
diff --git a/src/lib/libc/stdlib/ecvt.3 b/src/lib/libc/stdlib/ecvt.3
deleted file mode 100644
index f478f8e4b0..0000000000
--- a/src/lib/libc/stdlib/ecvt.3
+++ /dev/null
@@ -1,166 +0,0 @@
1.\" $OpenBSD: ecvt.3,v 1.13 2019/01/25 00:19:25 millert Exp $
2.\"
3.\" Copyright (c) 2002 Todd C. Miller <millert@openbsd.org>
4.\"
5.\" Permission to use, copy, modify, and distribute this software for any
6.\" purpose with or without fee is hereby granted, provided that the above
7.\" copyright notice and this permission notice appear in all copies.
8.\"
9.\" THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES
10.\" WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF
11.\" MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR
12.\" ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES
13.\" WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN
14.\" ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF
15.\" OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE.
16.\"
17.\" Sponsored in part by the Defense Advanced Research Projects
18.\" Agency (DARPA) and Air Force Research Laboratory, Air Force
19.\" Materiel Command, USAF, under agreement number F39502-99-1-0512.
20.\"
21.Dd $Mdocdate: January 25 2019 $
22.Dt ECVT 3
23.Os
24.Sh NAME
25.Nm ecvt ,
26.Nm fcvt ,
27.Nm gcvt
28.Nd convert double to ASCII string
29.Sh SYNOPSIS
30.In stdlib.h
31.Ft char *
32.Fn ecvt "double value" "int ndigit" "int *decpt" "int *sign"
33.Ft char *
34.Fn fcvt "double value" "int ndigit" "int *decpt" "int *sign"
35.Ft char *
36.Fn gcvt "double value" "int ndigit" "char *buf"
37.Sh DESCRIPTION
38.Bf -symbolic
39These functions are provided for compatibility with legacy code.
40New code should use the
41.Xr snprintf 3
42function for improved safety and portability.
43.Ef
44.Pp
45The
46.Fn ecvt ,
47.Fn fcvt
48and
49.Fn gcvt
50functions convert the double precision floating-point number
51.Fa value
52to a NUL-terminated
53.Tn ASCII
54string.
55.Pp
56The
57.Fn ecvt
58function converts
59.Fa value
60to a NUL-terminated string of exactly
61.Fa ndigit
62digits and returns a pointer to that string.
63The result is padded with zeroes from left to right as needed.
64There are no leading zeroes unless
65.Fa value
66itself is 0.
67The least significant digit is rounded in an implementation-dependent manner.
68The position of the decimal point relative to the beginning of the string
69is stored in
70.Fa decpt .
71A negative value indicates that the decimal point is located
72to the left of the returned digits (this occurs when there is no
73whole number component to
74.Fa value ) .
75If
76.Fa value
77is zero, it is unspecified whether the integer pointed to by
78.Fa decpt
79will be 0 or 1.
80The decimal point itself is not included in the returned string.
81If the sign of the result is negative, the integer pointed to by
82.Fa sign
83is non-zero; otherwise, it is 0.
84.Pp
85If the converted value is out of range or is not representable,
86the contents of the returned string are unspecified.
87.Pp
88The
89.Fn fcvt
90function is identical to
91.Fn ecvt
92with the exception that
93.Fa ndigit
94specifies the number of digits after the decimal point (zero-padded as
95needed).
96.Pp
97The
98.Fn gcvt
99function converts
100.Fa value
101to a NUL-terminated string similar to the %g
102.Xr printf 3
103format specifier and stores the result in
104.Fa buf .
105It produces
106.Fa ndigit
107significant digits similar to the %f
108.Xr printf 3
109format specifier where possible.
110If
111.Fa ndigit
112does allow sufficient precision, the result is stored in
113exponential notation similar to the %e
114.Xr printf 3
115format specifier.
116If
117.Fa value
118is less than zero,
119.Fa buf
120will be prefixed with a minus sign.
121A decimal point is included in the returned string if
122.Fa value
123is not a whole number.
124Unlike the
125.Fn ecvt
126and
127.Fn fcvt
128functions,
129.Fa buf
130is not zero-padded.
131.Sh RETURN VALUES
132The
133.Fn ecvt ,
134.Fn fcvt
135and
136.Fn gcvt
137functions return a NUL-terminated string representation of
138.Fa value .
139.Sh SEE ALSO
140.Xr printf 3 ,
141.Xr strtod 3
142.Sh STANDARDS
143The
144.Fn ecvt ,
145.Fn fcvt
146and
147.Fn gcvt
148functions conform to
149.St -p1003.1-2001 ;
150as of
151.St -p1003.1-2008
152they are no longer a part of the standard.
153.Sh CAVEATS
154The
155.Fn ecvt
156and
157.Fn fcvt
158functions return a pointer to internal storage space that will be
159overwritten by subsequent calls to either function.
160.Pp
161The maximum possible precision of the return value is limited by the
162precision of a double and may not be the same on all architectures.
163.Pp
164The
165.Xr snprintf 3
166function is preferred over these functions for new code.
diff --git a/src/lib/libc/stdlib/ecvt.c b/src/lib/libc/stdlib/ecvt.c
deleted file mode 100644
index a6b1d748fe..0000000000
--- a/src/lib/libc/stdlib/ecvt.c
+++ /dev/null
@@ -1,104 +0,0 @@
1/* $OpenBSD: ecvt.c,v 1.11 2019/01/25 00:19:25 millert Exp $ */
2
3/*
4 * Copyright (c) 2002, 2006 Todd C. Miller <millert@openbsd.org>
5 *
6 * Permission to use, copy, modify, and distribute this software for any
7 * purpose with or without fee is hereby granted, provided that the above
8 * copyright notice and this permission notice appear in all copies.
9 *
10 * THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES
11 * WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF
12 * MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR
13 * ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES
14 * WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN
15 * ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF
16 * OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE.
17 *
18 * Sponsored in part by the Defense Advanced Research Projects
19 * Agency (DARPA) and Air Force Research Laboratory, Air Force
20 * Materiel Command, USAF, under agreement number F39502-99-1-0512.
21 */
22
23#include <stdio.h>
24#include <stdlib.h>
25#include <string.h>
26#include "gdtoa.h"
27
28static char *__cvt(double, int, int *, int *, int, int);
29
30static char *
31__cvt(double value, int ndigit, int *decpt, int *sign, int fmode, int pad)
32{
33 static char *s;
34 char *p, *rve, c;
35 size_t siz;
36
37 if (ndigit == 0) {
38 *sign = value < 0.0;
39 *decpt = 0;
40 return ("");
41 }
42
43 free(s);
44 s = NULL;
45
46 if (ndigit < 0)
47 siz = -ndigit + 1;
48 else
49 siz = ndigit + 1;
50
51
52 /* __dtoa() doesn't allocate space for 0 so we do it by hand */
53 if (value == 0.0) {
54 *decpt = 1 - fmode; /* 1 for 'e', 0 for 'f' */
55 *sign = 0;
56 if ((rve = s = malloc(siz)) == NULL)
57 return(NULL);
58 *rve++ = '0';
59 *rve = '\0';
60 } else {
61 p = __dtoa(value, fmode + 2, ndigit, decpt, sign, &rve);
62 if (p == NULL)
63 return (NULL);
64 if (*decpt == 9999) {
65 /* Infinity or Nan, convert to inf or nan like printf */
66 *decpt = 0;
67 c = *p;
68 __freedtoa(p);
69 return(c == 'I' ? "inf" : "nan");
70 }
71 /* Make a local copy and adjust rve to be in terms of s */
72 if (pad && fmode)
73 siz += *decpt;
74 if ((s = malloc(siz)) == NULL) {
75 __freedtoa(p);
76 return(NULL);
77 }
78 (void) strlcpy(s, p, siz);
79 rve = s + (rve - p);
80 __freedtoa(p);
81 }
82
83 /* Add trailing zeros */
84 if (pad) {
85 siz -= rve - s;
86 while (--siz)
87 *rve++ = '0';
88 *rve = '\0';
89 }
90
91 return(s);
92}
93
94char *
95ecvt(double value, int ndigit, int *decpt, int *sign)
96{
97 return(__cvt(value, ndigit, decpt, sign, 0, 1));
98}
99
100char *
101fcvt(double value, int ndigit, int *decpt, int *sign)
102{
103 return(__cvt(value, ndigit, decpt, sign, 1, 1));
104}
diff --git a/src/lib/libc/stdlib/erand48.c b/src/lib/libc/stdlib/erand48.c
deleted file mode 100644
index db0529f4a0..0000000000
--- a/src/lib/libc/stdlib/erand48.c
+++ /dev/null
@@ -1,26 +0,0 @@
1/* $OpenBSD: erand48.c,v 1.5 2015/09/14 13:30:17 guenther Exp $ */
2/*
3 * Copyright (c) 1993 Martin Birgmeier
4 * All rights reserved.
5 *
6 * You may redistribute unmodified or modified versions of this source
7 * code provided that the above copyright notice and this and the
8 * following conditions are retained.
9 *
10 * This software is provided ``as is'', and comes with no warranties
11 * of any kind. I shall in no event be liable for anything that happens
12 * to anyone/anything when using this software.
13 */
14
15#include <math.h>
16#include "rand48.h"
17
18double
19erand48(unsigned short xseed[3])
20{
21 __dorand48(xseed);
22 return ldexp((double) xseed[0], -48) +
23 ldexp((double) xseed[1], -32) +
24 ldexp((double) xseed[2], -16);
25}
26DEF_WEAK(erand48);
diff --git a/src/lib/libc/stdlib/exit.3 b/src/lib/libc/stdlib/exit.3
deleted file mode 100644
index 22acade86c..0000000000
--- a/src/lib/libc/stdlib/exit.3
+++ /dev/null
@@ -1,102 +0,0 @@
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. 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.\" $OpenBSD: exit.3,v 1.18 2024/08/30 03:44:48 guenther Exp $
33.\"
34.Dd $Mdocdate: August 30 2024 $
35.Dt EXIT 3
36.Os
37.Sh NAME
38.Nm exit
39.Nd perform normal program termination
40.Sh SYNOPSIS
41.In stdlib.h
42.Ft void
43.Fn exit "int status"
44.Sh DESCRIPTION
45The
46.Fn exit
47function terminates a process.
48.Pp
49Before termination it performs the following functions in the
50order listed:
51.Bl -enum -offset indent
52.It
53Call the functions registered with the
54.Xr atexit 3
55function, in the reverse order of their registration.
56.It
57Flush all open output streams.
58.It
59Close all open streams.
60.It
61Unlink all files created with the
62.Xr tmpfile 3
63function.
64.El
65.Pp
66Following this,
67.Fn exit
68calls
69.Xr _exit 2 .
70Note that typically
71.Xr _exit 2
72only passes the lower 8 bits of
73.Fa status
74on to the parent, thus negative values have less meaning.
75.Sh RETURN VALUES
76The
77.Fn exit
78function never returns.
79.Sh SEE ALSO
80.Xr _exit 2 ,
81.Xr atexit 3 ,
82.Xr intro 3 ,
83.Xr sysexits 3 ,
84.Xr tmpfile 3
85.Sh STANDARDS
86The
87.Fn exit
88function conforms to
89.St -isoC-99 .
90.Sh HISTORY
91An
92.Fn exit
93function first appeared as a system call in
94.At v1 .
95It has accepted the
96.Fa status
97argument since
98.At v2 .
99In
100.At v7 ,
101the bare system call was renamed to
102.Xr _exit 2 .
diff --git a/src/lib/libc/stdlib/exit.c b/src/lib/libc/stdlib/exit.c
deleted file mode 100644
index e93d4313e3..0000000000
--- a/src/lib/libc/stdlib/exit.c
+++ /dev/null
@@ -1,57 +0,0 @@
1/* $OpenBSD: exit.c,v 1.14 2017/08/12 22:59:52 guenther Exp $ */
2/*-
3 * Copyright (c) 1990 The Regents of the University of California.
4 * 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. Neither the name of the University nor the names of its contributors
15 * may be used to endorse or promote products derived from this software
16 * without specific prior written permission.
17 *
18 * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
19 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
20 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
21 * ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
22 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
23 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
24 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
25 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
26 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
27 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
28 * SUCH DAMAGE.
29 */
30
31#include <stdlib.h>
32#include <unistd.h>
33#include "atexit.h"
34
35/*
36 * This variable is zero until a process has created a thread.
37 * It is used to avoid calling locking functions in libc when they
38 * are not required. By default, libc is intended to be(come)
39 * thread-safe, but without a (significant) penalty to non-threaded
40 * processes.
41 */
42int __isthreaded = 0;
43
44/*
45 * Exit, flushing stdio buffers if necessary.
46 */
47void
48exit(int status)
49{
50 /*
51 * Call functions registered by atexit() or _cxa_atexit()
52 * (including the stdio cleanup routine) and then _exit().
53 */
54 __cxa_finalize(NULL);
55 _exit(status);
56}
57DEF_STRONG(exit);
diff --git a/src/lib/libc/stdlib/gcvt.c b/src/lib/libc/stdlib/gcvt.c
deleted file mode 100644
index d3ab1b0415..0000000000
--- a/src/lib/libc/stdlib/gcvt.c
+++ /dev/null
@@ -1,123 +0,0 @@
1/* $OpenBSD: gcvt.c,v 1.15 2022/12/27 17:10:06 jmc Exp $ */
2
3/*
4 * Copyright (c) 2002, 2003, 2006, 2010
5 * Todd C. Miller <millert@openbsd.org>
6 *
7 * Permission to use, copy, modify, and distribute this software for any
8 * purpose with or without fee is hereby granted, provided that the above
9 * copyright notice and this permission notice appear in all copies.
10 *
11 * THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES
12 * WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF
13 * MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR
14 * ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES
15 * WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN
16 * ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF
17 * OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE.
18 *
19 * Sponsored in part by the Defense Advanced Research Projects
20 * Agency (DARPA) and Air Force Research Laboratory, Air Force
21 * Materiel Command, USAF, under agreement number F39502-99-1-0512.
22 */
23
24#include <locale.h>
25#include <stdio.h>
26#include <stdlib.h>
27#include <string.h>
28#include "gdtoa.h"
29
30#define DEFPREC 6
31
32char *
33gcvt(double value, int ndigit, char *buf)
34{
35 char *digits, *dst, *src;
36 int i, decpt, sign;
37 struct lconv *lconv;
38
39 lconv = localeconv();
40 if (ndigit <= 0) {
41 /* Match printf(3) behavior. */
42 ndigit = ndigit ? DEFPREC : 1;
43 }
44
45 digits = __dtoa(value, 2, ndigit, &decpt, &sign, NULL);
46 if (digits == NULL)
47 return (NULL);
48 if (decpt == 9999) {
49 /*
50 * Infinity or NaN, convert to inf or nan with sign.
51 * We can't infer buffer size based on ndigit.
52 * We have to assume it is at least 5 chars.
53 */
54 snprintf(buf, 5, "%s%s", sign ? "-" : "",
55 *digits == 'I' ? "inf" : "nan");
56 __freedtoa(digits);
57 return (buf);
58 }
59
60 dst = buf;
61 if (sign)
62 *dst++ = '-';
63
64 /* Match printf(3) behavior for exponential vs. regular formatting. */
65 if (decpt <= -4 || decpt > ndigit) {
66 /* exponential format (e.g. 1.2345e+13) */
67 if (--decpt < 0) {
68 sign = 1;
69 decpt = -decpt;
70 } else
71 sign = 0;
72 src = digits;
73 *dst++ = *src++;
74 if (*src != '\0') {
75 *dst++ = *lconv->decimal_point;
76 do {
77 *dst++ = *src++;
78 } while (*src != '\0');
79 }
80 *dst++ = 'e';
81 if (sign)
82 *dst++ = '-';
83 else
84 *dst++ = '+';
85 if (decpt < 10) {
86 *dst++ = '0';
87 *dst++ = '0' + decpt;
88 *dst = '\0';
89 } else {
90 /* XXX - optimize */
91 for (sign = decpt, i = 0; (sign /= 10) != 0; i++)
92 continue;
93 dst[i + 1] = '\0';
94 while (decpt != 0) {
95 dst[i--] = '0' + decpt % 10;
96 decpt /= 10;
97 }
98 }
99 } else {
100 /* standard format */
101 for (i = 0, src = digits; i < decpt; i++) {
102 if (*src != '\0')
103 *dst++ = *src++;
104 else
105 *dst++ = '0';
106 }
107 if (*src != '\0') {
108 if (src == digits)
109 *dst++ = '0'; /* zero before decimal point */
110 *dst++ = *lconv->decimal_point;
111 while (decpt < 0) {
112 *dst++ = '0';
113 decpt++;
114 }
115 for (i = decpt; digits[i] != '\0'; i++) {
116 *dst++ = digits[i];
117 }
118 }
119 *dst = '\0';
120 }
121 __freedtoa(digits);
122 return (buf);
123}
diff --git a/src/lib/libc/stdlib/getenv.3 b/src/lib/libc/stdlib/getenv.3
deleted file mode 100644
index 5a219a5c03..0000000000
--- a/src/lib/libc/stdlib/getenv.3
+++ /dev/null
@@ -1,191 +0,0 @@
1.\" Copyright (c) 1988, 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. 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.\" $OpenBSD: getenv.3,v 1.23 2022/08/08 22:40:03 millert Exp $
33.\"
34.Dd $Mdocdate: August 8 2022 $
35.Dt GETENV 3
36.Os
37.Sh NAME
38.Nm getenv ,
39.Nm putenv ,
40.Nm setenv ,
41.Nm unsetenv
42.Nd environment variable functions
43.Sh SYNOPSIS
44.In stdlib.h
45.Ft char *
46.Fn getenv "const char *name"
47.Ft int
48.Fn setenv "const char *name" "const char *value" "int overwrite"
49.Ft int
50.Fn putenv "char *string"
51.Ft int
52.Fn unsetenv "const char *name"
53.Sh DESCRIPTION
54These functions set, unset, and fetch environment variables from the host
55.Em environment list .
56.Pp
57The
58.Fn getenv
59function obtains the current value of the environment variable
60.Fa name .
61If the variable
62.Fa name
63is not in the current environment, a null pointer is returned.
64.Pp
65The
66.Fn setenv
67function inserts or resets the environment variable
68.Fa name
69in the current environment list.
70If the variable
71.Fa name
72does not exist in the list, it is inserted with the given
73.Fa value .
74If the variable does exist, the argument
75.Fa overwrite
76is tested; if
77.Fa overwrite
78is zero, the variable is not reset, otherwise it is reset to the given
79.Fa value .
80.Pp
81The
82.Fn putenv
83function takes an argument of the form
84.Ar name Ns = Ns Ar value .
85The memory pointed to by
86.Ar string
87becomes part of the environment and must not be deallocated by the caller.
88If the variable already exists, it will be overwritten.
89A common source of bugs is to pass a
90.Ar string
91argument that is a locally scoped string buffer.
92This will result in corruption of the environment after leaving
93the scope in which the variable is defined.
94For this reason, the
95.Fn setenv
96function is preferred over
97.Fn putenv .
98.Pp
99The
100.Fn unsetenv
101function deletes all instances of the variable name pointed to by
102.Fa name
103from the list.
104.Sh RETURN VALUES
105.Rv -std putenv setenv unsetenv
106.Pp
107The
108.Fn getenv
109function returns a pointer to the requested value, or
110.Dv NULL
111if it could not be found.
112If
113.Fn getenv
114is successful, the string returned should be considered read-only.
115.Sh ERRORS
116.Bl -tag -width Er
117.It Bq Er EINVAL
118The
119.Fn setenv
120or
121.Fn unsetenv
122function was passed an empty
123.Ar name
124or a NULL pointer, or was passed a
125.Ar name
126containing an
127.Sq =
128character.
129.Pp
130The
131.Fn putenv
132function was passed a
133.Ar string
134that did not contain an
135.Sq =
136character, or was passed a
137.Ar string
138that started with the
139.Sq =
140character.
141.It Bq Er ENOMEM
142The
143.Fn setenv
144or
145.Fn putenv
146function failed because it was unable to allocate memory for the environment.
147.El
148.Sh SEE ALSO
149.Xr csh 1 ,
150.Xr sh 1 ,
151.Xr execve 2 ,
152.Xr issetugid 2 ,
153.Xr environ 7
154.Sh STANDARDS
155The
156.Fn getenv
157function conforms to
158.St -ansiC .
159The
160.Fn putenv ,
161.Fn setenv ,
162and
163.Fn unsetenv
164functions conform to
165.St -p1003.1-2008 .
166.Sh HISTORY
167The function
168.Fn getenv
169appeared in
170.At v7
171and
172.Bx 3 .
173The functions
174.Fn setenv
175and
176.Fn unsetenv
177appeared in
178.Bx 4.3 Tahoe .
179The
180.Fn putenv
181function first appeared in
182.At V.2
183and was reimplemented for
184.Bx 4.3 Reno .
185.Sh CAVEATS
186Library code must be careful about using
187.Fn getenv
188to read untrusted environment variables in setuid programs.
189The
190.Fn issetugid
191function is provided for this purpose.
diff --git a/src/lib/libc/stdlib/getenv.c b/src/lib/libc/stdlib/getenv.c
deleted file mode 100644
index 068d70a999..0000000000
--- a/src/lib/libc/stdlib/getenv.c
+++ /dev/null
@@ -1,78 +0,0 @@
1/* $OpenBSD: getenv.c,v 1.13 2024/07/10 14:17:58 jca Exp $ */
2/*
3 * Copyright (c) 1987, 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. Neither the name of the University nor the names of its contributors
15 * may be used to endorse or promote products derived from this software
16 * without specific prior written permission.
17 *
18 * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
19 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
20 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
21 * ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
22 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
23 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
24 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
25 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
26 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
27 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
28 * SUCH DAMAGE.
29 */
30
31#include <stdlib.h>
32#include <string.h>
33
34
35/*
36 * __findenv --
37 * Returns pointer to value associated with name, if any, else NULL.
38 * Starts searching within the environmental array at offset.
39 * Sets offset to be the offset of the name/value combination in the
40 * environmental array, for use by putenv(3), setenv(3) and unsetenv(3).
41 * Explicitly removes '=' in argument name.
42 */
43char *
44__findenv(const char *name, int len, int *offset)
45{
46 int i;
47 const char *np;
48 char **p, *cp;
49
50 if (name == NULL || environ == NULL)
51 return (NULL);
52 for (p = environ + *offset; (cp = *p) != NULL; ++p) {
53 for (np = name, i = len; i && *cp; i--)
54 if (*cp++ != *np++)
55 break;
56 if (i == 0 && *cp++ == '=') {
57 *offset = p - environ;
58 return (cp);
59 }
60 }
61 return (NULL);
62}
63
64/*
65 * getenv --
66 * Returns ptr to value associated with name, if any, else NULL.
67 */
68char *
69getenv(const char *name)
70{
71 int offset = 0;
72 const char *np;
73
74 for (np = name; *np && *np != '='; ++np)
75 ;
76 return (__findenv(name, (int)(np - name), &offset));
77}
78DEF_STRONG(getenv);
diff --git a/src/lib/libc/stdlib/getopt.3 b/src/lib/libc/stdlib/getopt.3
deleted file mode 100644
index c7e28ff91f..0000000000
--- a/src/lib/libc/stdlib/getopt.3
+++ /dev/null
@@ -1,365 +0,0 @@
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. Neither the name of the University nor the names of its contributors
13.\" may be used to endorse or promote products derived from this software
14.\" without specific prior written permission.
15.\"
16.\" THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
17.\" ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
18.\" IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
19.\" ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
20.\" FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
21.\" DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
22.\" OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
23.\" HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
24.\" LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
25.\" OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
26.\" SUCH DAMAGE.
27.\"
28.\" $OpenBSD: getopt.3,v 1.48 2022/07/25 02:25:55 jsg Exp $
29.\"
30.Dd $Mdocdate: July 25 2022 $
31.Dt GETOPT 3
32.Os
33.Sh NAME
34.Nm getopt
35.Nd get option character from command line argument list
36.Sh SYNOPSIS
37.In unistd.h
38.Vt extern char *optarg;
39.Vt extern int opterr;
40.Vt extern int optind;
41.Vt extern int optopt;
42.Vt extern int optreset;
43.Ft int
44.Fn getopt "int argc" "char * const *argv" "const char *optstring"
45.Sh DESCRIPTION
46The
47.Fn getopt
48function incrementally parses a command line argument list
49.Fa argv
50and returns the next
51.Em known
52option character.
53An option character is
54.Em known
55if it has been specified in the string of accepted option characters,
56.Fa optstring .
57.Pp
58The option string
59.Fa optstring
60may contain the following elements: individual characters,
61characters followed by a colon, and characters followed by two colons.
62A character followed by a single colon indicates that an argument
63is to follow the option on the command line.
64Two colons indicates that the argument is optional \- this is an
65extension not covered by POSIX.
66For example, an option string
67.Qq x
68recognizes an option
69.Fl x ,
70and an option string
71.Qq Li x:
72recognizes an option and argument
73.Fl x Ar argument .
74It does not matter to
75.Fn getopt
76if a following argument has leading whitespace; except in the case where
77the argument is optional, denoted with two colons, no leading whitespace
78is permitted.
79.Pp
80On return from
81.Fn getopt ,
82.Va optarg
83points to an option argument, if it is anticipated,
84and the variable
85.Va optind
86contains the index to the next
87.Fa argv
88argument for a subsequent call
89to
90.Fn getopt .
91.Pp
92The variables
93.Va opterr
94and
95.Va optind
96are both initialized to 1.
97The
98.Va optind
99variable may be set to another value larger than 0 before a set of calls to
100.Fn getopt
101in order to skip over more or less
102.Fa argv
103entries.
104An
105.Va optind
106value of 0 is reserved for compatibility with GNU
107.Fn getopt .
108.Pp
109In order to use
110.Fn getopt
111to evaluate multiple sets of arguments, or to evaluate a single set of
112arguments multiple times,
113the variable
114.Va optreset
115must be set to 1 before the second and each additional set of calls to
116.Fn getopt ,
117and the variable
118.Va optind
119must be reinitialized.
120.Pp
121The
122.Fn getopt
123function returns \-1 when the argument list is exhausted.
124The interpretation of options in the argument list may be cancelled
125by the option
126.Ql --
127(double dash) which causes
128.Fn getopt
129to signal the end of argument processing and return \-1.
130When all options have been processed (i.e., up to the first non-option
131argument),
132.Fn getopt
133returns \-1.
134.Sh RETURN VALUES
135The
136.Fn getopt
137function returns the next known option character in
138.Fa optstring .
139If
140.Fn getopt
141encounters a character not found in
142.Fa optstring
143or if it detects a missing option argument,
144it returns
145.Sq \&?
146(question mark).
147If
148.Fa optstring
149has a leading
150.Sq \&:
151then a missing option argument causes
152.Sq \&:
153to be returned instead of
154.Sq \&? .
155In either case, the variable
156.Va optopt
157is set to the character that caused the error.
158The
159.Fn getopt
160function returns \-1 when the argument list is exhausted.
161.Sh EXAMPLES
162The following code accepts the options
163.Fl b
164and
165.Fl f Ar argument
166and adjusts
167.Va argc
168and
169.Va argv
170after option argument processing has completed.
171.Bd -literal -offset indent
172int bflag, ch, fd;
173
174bflag = 0;
175while ((ch = getopt(argc, argv, "bf:")) != -1) {
176 switch (ch) {
177 case 'b':
178 bflag = 1;
179 break;
180 case 'f':
181 if ((fd = open(optarg, O_RDONLY)) == -1)
182 err(1, "%s", optarg);
183 break;
184 default:
185 usage();
186 }
187}
188argc -= optind;
189argv += optind;
190.Ed
191.Sh DIAGNOSTICS
192If the
193.Fn getopt
194function encounters a character not found in the string
195.Fa optstring
196or detects
197a missing option argument, it writes an error message to
198.Em stderr
199and returns
200.Ql \&? .
201Setting
202.Va opterr
203to a zero will disable these error messages.
204If
205.Fa optstring
206has a leading
207.Ql \&:
208then a missing option argument causes a
209.Ql \&:
210to be returned in addition to suppressing any error messages.
211.Pp
212Option arguments are allowed to begin with
213.Ql - ;
214this is reasonable but reduces the amount of error checking possible.
215.Sh SEE ALSO
216.Xr getopt 1 ,
217.Xr getopt_long 3 ,
218.Xr getsubopt 3
219.Sh STANDARDS
220The
221.Fn getopt
222function implements a superset of the functionality specified by
223.St -p1003.1 .
224.Pp
225The following extensions are supported:
226.Bl -bullet
227.It
228The
229.Va optreset
230variable was added to make it possible to call the
231.Fn getopt
232function multiple times.
233.It
234If the
235.Va optind
236variable is set to 0,
237.Fn getopt
238will behave as if the
239.Va optreset
240variable has been set.
241This is for compatibility with
242.Tn GNU
243.Fn getopt .
244New code should use
245.Va optreset
246instead.
247.It
248If the first character of
249.Fa optstring
250is a plus sign
251.Pq Ql + ,
252it will be ignored.
253This is for compatibility with
254.Tn GNU
255.Fn getopt .
256.It
257If the first character of
258.Fa optstring
259is a dash
260.Pq Ql - ,
261non-options will be returned as arguments to the option character
262.Ql \e1 .
263This is for compatibility with
264.Tn GNU
265.Fn getopt .
266.It
267A single dash
268.Pq Ql -
269may be specified as a character in
270.Fa optstring ,
271however it should
272.Em never
273have an argument associated with it.
274This allows
275.Fn getopt
276to be used with programs that expect
277.Ql -
278as an option flag.
279This practice is wrong, and should not be used in any current development.
280It is provided for backward compatibility
281.Em only .
282Care should be taken not to use
283.Ql -
284as the first character in
285.Fa optstring
286to avoid a semantic conflict with
287.Tn GNU
288.Fn getopt
289semantics (see above).
290By default, a single dash causes
291.Fn getopt
292to return \-1.
293.El
294.Pp
295Historic
296.Bx
297versions of
298.Fn getopt
299set
300.Fa optopt
301to the last option character processed.
302However, this conflicts with
303.St -p1003.1
304which stipulates that
305.Fa optopt
306be set to the last character that caused an error.
307.Sh HISTORY
308The
309.Fn getopt
310function first appeared in
311.At III
312and was reimplemented for
313.Bx 4.3 .
314.Sh BUGS
315The
316.Fn getopt
317function was once specified to return
318.Dv EOF
319instead of \-1.
320This was changed by
321.St -p1003.2-92
322to decouple
323.Fn getopt
324from
325.In stdio.h .
326.Pp
327It is possible to handle digits as option letters.
328This allows
329.Fn getopt
330to be used with programs that expect a number
331.Pq Dq Li \-3
332as an option.
333This practice is wrong, and should not be used in any current development.
334It is provided for backward compatibility
335.Em only .
336The following code fragment works in most cases and can handle mixed
337number and letter arguments.
338.Bd -literal -offset indent
339int aflag = 0, bflag = 0, ch, lastch = '\e0';
340int length = -1, newarg = 1, prevoptind = 1;
341
342while ((ch = getopt(argc, argv, "0123456789ab")) != -1) {
343 switch (ch) {
344 case '0': case '1': case '2': case '3': case '4':
345 case '5': case '6': case '7': case '8': case '9':
346 if (newarg || !isdigit(lastch))
347 length = 0;
348 else if (length > INT_MAX / 10)
349 usage();
350 length = (length * 10) + (ch - '0');
351 break;
352 case 'a':
353 aflag = 1;
354 break;
355 case 'b':
356 bflag = 1;
357 break;
358 default:
359 usage();
360 }
361 lastch = ch;
362 newarg = optind != prevoptind;
363 prevoptind = optind;
364}
365.Ed
diff --git a/src/lib/libc/stdlib/getopt_long.3 b/src/lib/libc/stdlib/getopt_long.3
deleted file mode 100644
index 88594cbf9c..0000000000
--- a/src/lib/libc/stdlib/getopt_long.3
+++ /dev/null
@@ -1,460 +0,0 @@
1.\" $OpenBSD: getopt_long.3,v 1.25 2022/09/11 06:38:11 jmc Exp $
2.\" $NetBSD: getopt_long.3,v 1.11 2002/10/02 10:54:19 wiz Exp $
3.\"
4.\" Copyright (c) 1988, 1991, 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. Neither the name of the University nor the names of its contributors
16.\" may be used to endorse or promote products derived from this software
17.\" without specific prior written permission.
18.\"
19.\" THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
20.\" ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
21.\" IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
22.\" ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
23.\" FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
24.\" DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
25.\" OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
26.\" HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
27.\" LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
28.\" OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
29.\" SUCH DAMAGE.
30.\"
31.\" @(#)getopt.3 8.5 (Berkeley) 4/27/95
32.\"
33.Dd $Mdocdate: September 11 2022 $
34.Dt GETOPT_LONG 3
35.Os
36.Sh NAME
37.Nm getopt_long ,
38.Nm getopt_long_only
39.Nd get long options from command line argument list
40.Sh SYNOPSIS
41.In getopt.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_long "int argc" "char * const *argv" "const char *optstring" "const struct option *longopts" "int *longindex"
49.Ft int
50.Fn getopt_long_only "int argc" "char * const *argv" "const char *optstring" "const struct option *longopts" "int *longindex"
51.Sh DESCRIPTION
52The
53.Fn getopt_long
54function is similar to
55.Xr getopt 3
56but it accepts options in two forms: words and characters.
57The
58.Fn getopt_long
59function provides a superset of the functionality of
60.Xr getopt 3 .
61.Fn getopt_long
62can be used in two ways.
63In the first way, every long option understood by the program has a
64corresponding short option, and the option structure is only used to
65translate from long options to short options.
66When used in this fashion,
67.Fn getopt_long
68behaves identically to
69.Xr getopt 3 .
70This is a good way to add long option processing to an existing program
71with the minimum of rewriting.
72.Pp
73In the second mechanism, a long option sets a flag in the
74.Fa option
75structure passed, or will store a pointer to the command line argument
76in the
77.Fa option
78structure passed to it for options that take arguments.
79Additionally, the long option's argument may be specified as a single
80argument with an equal sign, e.g.
81.Bd -literal -offset indent
82$ myprogram --myoption=somevalue
83.Ed
84.Pp
85When a long option is processed, the call to
86.Fn getopt_long
87will return 0.
88For this reason, long option processing without
89shortcuts is not backwards compatible with
90.Xr getopt 3 .
91.Pp
92It is possible to combine these methods, providing for long options
93processing with short option equivalents for some options.
94Less frequently used options would be processed as long options only.
95.Pp
96Abbreviated long option names are accepted when
97.Fn getopt_long
98processes long options if the abbreviation is unique.
99An exact match is always preferred for a defined long option.
100.Pp
101By default,
102.Fn getopt_long
103permutes
104.Ar argv
105such that all option arguments are evaluated before any non-options arguments.
106If the first character of
107.Fa optstring
108is a plus sign
109.Pq Ql +
110or if the environment variable
111.Ev POSIXLY_CORRECT
112is set, then
113.Ar argv
114is processed in order and option processing stops as soon as the first
115non-option argument is encountered.
116.Pp
117The
118.Fn getopt_long
119call requires an array to be initialized describing the long
120options.
121Each element of the array is a structure:
122.Bd -literal -offset indent
123struct option {
124 char *name;
125 int has_arg;
126 int *flag;
127 int val;
128};
129.Ed
130.Pp
131The
132.Fa name
133field should contain the option name without the leading double dash.
134.Pp
135The
136.Fa has_arg
137field should be one of:
138.Pp
139.Bl -tag -width "optional_argument" -compact -offset indent
140.It Dv no_argument
141no argument to the option is expected.
142.It Dv required_argument
143an argument to the option is required.
144.It Dv optional_argument
145an argument to the option may be presented.
146.El
147.Pp
148If
149.Fa flag
150is not
151.Dv NULL ,
152then the integer pointed to by it will be set to the value in the
153.Fa val
154field.
155If the
156.Fa flag
157field is
158.Dv NULL ,
159then the
160.Fa val
161field will be returned.
162Setting
163.Fa flag
164to
165.Dv NULL
166and setting
167.Fa val
168to the corresponding short option will make this function act just
169like
170.Xr getopt 3 .
171.Pp
172If the
173.Fa longindex
174argument is not
175.Dv NULL ,
176then the integer pointed to by it will be set to the index of the long
177option relative to
178.Fa longopts .
179.Pp
180The last element of the
181.Fa longopts
182array has to be filled with zeroes.
183.Pp
184The
185.Fn getopt_long_only
186function behaves identically to
187.Fn getopt_long
188with the exception that long options may start with
189.Sq -
190in addition to
191.Sq -- .
192If an option starting with
193.Sq -
194does not match a long option but does match a single-character option,
195the single-character option is returned.
196.Sh RETURN VALUES
197If the
198.Fa flag
199field in
200.Vt struct option
201is
202.Dv NULL ,
203.Fn getopt_long
204and
205.Fn getopt_long_only
206return the value specified in the
207.Fa val
208field, which is usually just the corresponding short option.
209If
210.Fa flag
211is not
212.Dv NULL ,
213these functions return 0 and store
214.Fa val
215in the location pointed to by
216.Fa flag .
217These functions return
218.Sq \&:
219if there was a missing option argument,
220.Sq \&?
221if the user specified an unknown or ambiguous option, and
222\-1 when the argument list has been exhausted.
223.Sh IMPLEMENTATION DIFFERENCES
224This section describes differences to the GNU implementation
225found in glibc-2.1.3:
226.Bl -bullet
227.It
228handling of
229.Ql -
230within the option string (not the first character):
231.Bl -tag -width "OpenBSD"
232.It GNU
233treats a
234.Ql -
235on the command line as a non-argument.
236.It OpenBSD
237a
238.Ql -
239within the option string matches a
240.Ql -
241(single dash) on the command line.
242This functionality is provided for backward compatibility with
243programs, such as
244.Xr su 1 ,
245that use
246.Ql -
247as an option flag.
248This practice is wrong, and should not be used in any current development.
249.El
250.It
251handling of
252.Ql ::
253in the option string in the presence of
254.Ev POSIXLY_CORRECT :
255.Bl -tag -width "OpenBSD"
256.It Both
257GNU and
258.Ox
259ignore
260.Ev POSIXLY_CORRECT
261here and take
262.Ql ::
263to mean the preceding option takes an optional argument.
264.El
265.It
266return value in case of missing argument if first character
267(after
268.Ql +
269or
270.Ql - )
271in the option string is not
272.Ql \&: :
273.Bl -tag -width "OpenBSD"
274.It GNU
275returns
276.Ql \&?
277.It OpenBSD
278returns
279.Ql \&:
280(since
281.Ox Ns 's
282.Xr getopt 3
283does).
284.El
285.It
286handling of
287.Ql --a
288in
289.Xr getopt 3 :
290.Bl -tag -width "OpenBSD"
291.It GNU
292parses this as option
293.Ql - ,
294option
295.Ql a .
296.It OpenBSD
297parses this as
298.Ql -- ,
299and returns \-1 (ignoring the
300.Ql a )
301(because the original
302.Fn getopt
303did.)
304.El
305.It
306setting of
307.Va optopt
308for long options with
309.Va flag
310.No non- Ns Dv NULL :
311.Bl -tag -width "OpenBSD"
312.It GNU
313sets
314.Va optopt
315to
316.Va val .
317.It OpenBSD
318sets
319.Va optopt
320to 0 (since
321.Va val
322would never be returned).
323.El
324.It
325handling of
326.Ql -W
327with
328.Ql W;
329in the option string in
330.Xr getopt 3
331(not
332.Fn getopt_long ) :
333.Bl -tag -width "OpenBSD"
334.It GNU
335causes a segmentation fault.
336.It OpenBSD
337no special handling is done;
338.Ql W;
339is interpreted as two separate options, neither of which take an argument.
340.El
341.It
342setting of
343.Va optarg
344for long options without an argument that are invoked via
345.Ql -W
346(with
347.Ql W;
348in the option string):
349.Bl -tag -width "OpenBSD"
350.It GNU
351sets
352.Va optarg
353to the option name (the argument of
354.Ql -W ) .
355.It OpenBSD
356sets
357.Va optarg
358to
359.Dv NULL
360(the argument of the long option).
361.El
362.It
363handling of
364.Ql -W
365with an argument that is not (a prefix to) a known long option
366(with
367.Ql W;
368in the option string):
369.Bl -tag -width "OpenBSD"
370.It GNU
371returns
372.Ql -W
373with
374.Va optarg
375set to the unknown option.
376.It OpenBSD
377treats this as an error (unknown option) and returns
378.Ql \&?
379with
380.Va optopt
381set to 0 and
382.Va optarg
383set to
384.Dv NULL
385(as GNU's man page documents).
386.El
387.It
388The error messages are different.
389.It
390.Ox
391does not permute the argument vector at the same points in
392the calling sequence as GNU does.
393The aspects normally used by the caller
394(ordering after \-1 is returned, value of
395.Va optind
396relative to current positions) are the same, though.
397(We do fewer variable swaps.)
398.El
399.Sh ENVIRONMENT
400.Bl -tag -width Ev
401.It Ev POSIXLY_CORRECT
402If set, option processing stops when the first non-option is found and
403a leading
404.Sq +
405in the
406.Ar optstring
407is ignored.
408.El
409.Sh EXAMPLES
410.Bd -literal
411int bflag, ch, fd;
412int daggerset;
413
414/* options descriptor */
415static struct option longopts[] = {
416 { "buffy", no_argument, NULL, 'b' },
417 { "fluoride", required_argument, NULL, 'f' },
418 { "daggerset", no_argument, &daggerset, 1 },
419 { NULL, 0, NULL, 0 }
420};
421
422bflag = 0;
423while ((ch = getopt_long(argc, argv, "bf:", longopts, NULL)) != -1)
424 switch (ch) {
425 case 'b':
426 bflag = 1;
427 break;
428 case 'f':
429 if ((fd = open(optarg, O_RDONLY)) == -1)
430 err(1, "unable to open %s", optarg);
431 break;
432 case 0:
433 if (daggerset)
434 fprintf(stderr, "Buffy will use her dagger to "
435 "apply fluoride to dracula's teeth\en");
436 break;
437 default:
438 usage();
439 }
440argc -= optind;
441argv += optind;
442.Ed
443.Sh SEE ALSO
444.Xr getopt 3
445.Sh HISTORY
446The
447.Fn getopt_long
448and
449.Fn getopt_long_only
450functions first appeared in GNU libiberty.
451This implementation first appeared in
452.Ox 3.3 .
453.Sh BUGS
454The
455.Ar argv
456argument is not really
457.Dv const
458as its elements may be permuted (unless
459.Ev POSIXLY_CORRECT
460is set).
diff --git a/src/lib/libc/stdlib/getopt_long.c b/src/lib/libc/stdlib/getopt_long.c
deleted file mode 100644
index 0de50457c2..0000000000
--- a/src/lib/libc/stdlib/getopt_long.c
+++ /dev/null
@@ -1,513 +0,0 @@
1/* $OpenBSD: getopt_long.c,v 1.32 2020/05/27 22:25:09 schwarze Exp $ */
2/* $NetBSD: getopt_long.c,v 1.15 2002/01/31 22:43:40 tv Exp $ */
3
4/*
5 * Copyright (c) 2002 Todd C. Miller <millert@openbsd.org>
6 *
7 * Permission to use, copy, modify, and distribute this software for any
8 * purpose with or without fee is hereby granted, provided that the above
9 * copyright notice and this permission notice appear in all copies.
10 *
11 * THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES
12 * WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF
13 * MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR
14 * ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES
15 * WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN
16 * ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF
17 * OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE.
18 *
19 * Sponsored in part by the Defense Advanced Research Projects
20 * Agency (DARPA) and Air Force Research Laboratory, Air Force
21 * Materiel Command, USAF, under agreement number F39502-99-1-0512.
22 */
23/*-
24 * Copyright (c) 2000 The NetBSD Foundation, Inc.
25 * All rights reserved.
26 *
27 * This code is derived from software contributed to The NetBSD Foundation
28 * by Dieter Baron and Thomas Klausner.
29 *
30 * Redistribution and use in source and binary forms, with or without
31 * modification, are permitted provided that the following conditions
32 * are met:
33 * 1. Redistributions of source code must retain the above copyright
34 * notice, this list of conditions and the following disclaimer.
35 * 2. Redistributions in binary form must reproduce the above copyright
36 * notice, this list of conditions and the following disclaimer in the
37 * documentation and/or other materials provided with the distribution.
38 *
39 * THIS SOFTWARE IS PROVIDED BY THE NETBSD FOUNDATION, INC. AND CONTRIBUTORS
40 * ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED
41 * TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
42 * PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE FOUNDATION OR CONTRIBUTORS
43 * BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
44 * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
45 * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
46 * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
47 * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
48 * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
49 * POSSIBILITY OF SUCH DAMAGE.
50 */
51
52#include <err.h>
53#include <errno.h>
54#include <getopt.h>
55#include <stdlib.h>
56#include <string.h>
57
58int opterr = 1; /* if error message should be printed */
59int optind = 1; /* index into parent argv vector */
60int optopt = '?'; /* character checked for validity */
61int optreset; /* reset getopt */
62char *optarg; /* argument associated with option */
63
64#if 0
65/* DEF_* only work on initialized (non-COMMON) variables */
66DEF_WEAK(opterr);
67DEF_WEAK(optind);
68DEF_WEAK(optopt);
69#endif
70
71#define PRINT_ERROR ((opterr) && (*options != ':'))
72
73#define FLAG_PERMUTE 0x01 /* permute non-options to the end of argv */
74#define FLAG_ALLARGS 0x02 /* treat non-options as args to option "-1" */
75#define FLAG_LONGONLY 0x04 /* operate as getopt_long_only */
76
77/* return values */
78#define BADCH (int)'?'
79#define BADARG ((*options == ':') ? (int)':' : (int)'?')
80#define INORDER (int)1
81
82#define EMSG ""
83
84static int getopt_internal(int, char * const *, const char *,
85 const struct option *, int *, int);
86static int parse_long_options(char * const *, const char *,
87 const struct option *, int *, int, int);
88static int gcd(int, int);
89static void permute_args(int, int, int, char * const *);
90
91static char *place = EMSG; /* option letter processing */
92
93/* XXX: set optreset to 1 rather than these two */
94static int nonopt_start = -1; /* first non option argument (for permute) */
95static int nonopt_end = -1; /* first option after non options (for permute) */
96
97/* Error messages */
98static const char recargchar[] = "option requires an argument -- %c";
99static const char recargstring[] = "option requires an argument -- %s";
100static const char ambig[] = "ambiguous option -- %.*s";
101static const char noarg[] = "option doesn't take an argument -- %.*s";
102static const char illoptchar[] = "unknown option -- %c";
103static const char illoptstring[] = "unknown option -- %s";
104
105/*
106 * Compute the greatest common divisor of a and b.
107 */
108static int
109gcd(int a, int b)
110{
111 int c;
112
113 c = a % b;
114 while (c != 0) {
115 a = b;
116 b = c;
117 c = a % b;
118 }
119
120 return (b);
121}
122
123/*
124 * Exchange the block from nonopt_start to nonopt_end with the block
125 * from nonopt_end to opt_end (keeping the same order of arguments
126 * in each block).
127 */
128static void
129permute_args(int panonopt_start, int panonopt_end, int opt_end,
130 char * const *nargv)
131{
132 int cstart, cyclelen, i, j, ncycle, nnonopts, nopts, pos;
133 char *swap;
134
135 /*
136 * compute lengths of blocks and number and size of cycles
137 */
138 nnonopts = panonopt_end - panonopt_start;
139 nopts = opt_end - panonopt_end;
140 ncycle = gcd(nnonopts, nopts);
141 cyclelen = (opt_end - panonopt_start) / ncycle;
142
143 for (i = 0; i < ncycle; i++) {
144 cstart = panonopt_end+i;
145 pos = cstart;
146 for (j = 0; j < cyclelen; j++) {
147 if (pos >= panonopt_end)
148 pos -= nnonopts;
149 else
150 pos += nopts;
151 swap = nargv[pos];
152 ((char **)nargv)[pos] = nargv[cstart];
153 ((char **)nargv)[cstart] = swap;
154 }
155 }
156}
157
158/*
159 * parse_long_options --
160 * Parse long options in argc/argv argument vector.
161 * Returns -1 if short_too is set and the option does not match long_options.
162 */
163static int
164parse_long_options(char * const *nargv, const char *options,
165 const struct option *long_options, int *idx, int short_too, int flags)
166{
167 char *current_argv, *has_equal;
168 size_t current_argv_len;
169 int i, match, exact_match, second_partial_match;
170
171 current_argv = place;
172 match = -1;
173 exact_match = 0;
174 second_partial_match = 0;
175
176 optind++;
177
178 if ((has_equal = strchr(current_argv, '=')) != NULL) {
179 /* argument found (--option=arg) */
180 current_argv_len = has_equal - current_argv;
181 has_equal++;
182 } else
183 current_argv_len = strlen(current_argv);
184
185 for (i = 0; long_options[i].name; i++) {
186 /* find matching long option */
187 if (strncmp(current_argv, long_options[i].name,
188 current_argv_len))
189 continue;
190
191 if (strlen(long_options[i].name) == current_argv_len) {
192 /* exact match */
193 match = i;
194 exact_match = 1;
195 break;
196 }
197 /*
198 * If this is a known short option, don't allow
199 * a partial match of a single character.
200 */
201 if (short_too && current_argv_len == 1)
202 continue;
203
204 if (match == -1) /* first partial match */
205 match = i;
206 else if ((flags & FLAG_LONGONLY) ||
207 long_options[i].has_arg != long_options[match].has_arg ||
208 long_options[i].flag != long_options[match].flag ||
209 long_options[i].val != long_options[match].val)
210 second_partial_match = 1;
211 }
212 if (!exact_match && second_partial_match) {
213 /* ambiguous abbreviation */
214 if (PRINT_ERROR)
215 warnx(ambig, (int)current_argv_len, current_argv);
216 optopt = 0;
217 return (BADCH);
218 }
219 if (match != -1) { /* option found */
220 if (long_options[match].has_arg == no_argument
221 && has_equal) {
222 if (PRINT_ERROR)
223 warnx(noarg, (int)current_argv_len,
224 current_argv);
225 /*
226 * XXX: GNU sets optopt to val regardless of flag
227 */
228 if (long_options[match].flag == NULL)
229 optopt = long_options[match].val;
230 else
231 optopt = 0;
232 return (BADARG);
233 }
234 if (long_options[match].has_arg == required_argument ||
235 long_options[match].has_arg == optional_argument) {
236 if (has_equal)
237 optarg = has_equal;
238 else if (long_options[match].has_arg ==
239 required_argument) {
240 /*
241 * optional argument doesn't use next nargv
242 */
243 optarg = nargv[optind++];
244 }
245 }
246 if ((long_options[match].has_arg == required_argument)
247 && (optarg == NULL)) {
248 /*
249 * Missing argument; leading ':' indicates no error
250 * should be generated.
251 */
252 if (PRINT_ERROR)
253 warnx(recargstring,
254 current_argv);
255 /*
256 * XXX: GNU sets optopt to val regardless of flag
257 */
258 if (long_options[match].flag == NULL)
259 optopt = long_options[match].val;
260 else
261 optopt = 0;
262 --optind;
263 return (BADARG);
264 }
265 } else { /* unknown option */
266 if (short_too) {
267 --optind;
268 return (-1);
269 }
270 if (PRINT_ERROR)
271 warnx(illoptstring, current_argv);
272 optopt = 0;
273 return (BADCH);
274 }
275 if (idx)
276 *idx = match;
277 if (long_options[match].flag) {
278 *long_options[match].flag = long_options[match].val;
279 return (0);
280 } else
281 return (long_options[match].val);
282}
283
284/*
285 * getopt_internal --
286 * Parse argc/argv argument vector. Called by user level routines.
287 */
288static int
289getopt_internal(int nargc, char * const *nargv, const char *options,
290 const struct option *long_options, int *idx, int flags)
291{
292 char *oli; /* option letter list index */
293 int optchar, short_too;
294 static int posixly_correct = -1;
295
296 if (options == NULL)
297 return (-1);
298
299 /*
300 * XXX Some GNU programs (like cvs) set optind to 0 instead of
301 * XXX using optreset. Work around this braindamage.
302 */
303 if (optind == 0)
304 optind = optreset = 1;
305
306 /*
307 * Disable GNU extensions if POSIXLY_CORRECT is set or options
308 * string begins with a '+'.
309 */
310 if (posixly_correct == -1 || optreset)
311 posixly_correct = (getenv("POSIXLY_CORRECT") != NULL);
312 if (*options == '-')
313 flags |= FLAG_ALLARGS;
314 else if (posixly_correct || *options == '+')
315 flags &= ~FLAG_PERMUTE;
316 if (*options == '+' || *options == '-')
317 options++;
318
319 optarg = NULL;
320 if (optreset)
321 nonopt_start = nonopt_end = -1;
322start:
323 if (optreset || !*place) { /* update scanning pointer */
324 optreset = 0;
325 if (optind >= nargc) { /* end of argument vector */
326 place = EMSG;
327 if (nonopt_end != -1) {
328 /* do permutation, if we have to */
329 permute_args(nonopt_start, nonopt_end,
330 optind, nargv);
331 optind -= nonopt_end - nonopt_start;
332 }
333 else if (nonopt_start != -1) {
334 /*
335 * If we skipped non-options, set optind
336 * to the first of them.
337 */
338 optind = nonopt_start;
339 }
340 nonopt_start = nonopt_end = -1;
341 return (-1);
342 }
343 if (*(place = nargv[optind]) != '-' ||
344 (place[1] == '\0' && strchr(options, '-') == NULL)) {
345 place = EMSG; /* found non-option */
346 if (flags & FLAG_ALLARGS) {
347 /*
348 * GNU extension:
349 * return non-option as argument to option 1
350 */
351 optarg = nargv[optind++];
352 return (INORDER);
353 }
354 if (!(flags & FLAG_PERMUTE)) {
355 /*
356 * If no permutation wanted, stop parsing
357 * at first non-option.
358 */
359 return (-1);
360 }
361 /* do permutation */
362 if (nonopt_start == -1)
363 nonopt_start = optind;
364 else if (nonopt_end != -1) {
365 permute_args(nonopt_start, nonopt_end,
366 optind, nargv);
367 nonopt_start = optind -
368 (nonopt_end - nonopt_start);
369 nonopt_end = -1;
370 }
371 optind++;
372 /* process next argument */
373 goto start;
374 }
375 if (nonopt_start != -1 && nonopt_end == -1)
376 nonopt_end = optind;
377
378 /*
379 * If we have "-" do nothing, if "--" we are done.
380 */
381 if (place[1] != '\0' && *++place == '-' && place[1] == '\0') {
382 optind++;
383 place = EMSG;
384 /*
385 * We found an option (--), so if we skipped
386 * non-options, we have to permute.
387 */
388 if (nonopt_end != -1) {
389 permute_args(nonopt_start, nonopt_end,
390 optind, nargv);
391 optind -= nonopt_end - nonopt_start;
392 }
393 nonopt_start = nonopt_end = -1;
394 return (-1);
395 }
396 }
397
398 /*
399 * Check long options if:
400 * 1) we were passed some
401 * 2) the arg is not just "-"
402 * 3) either the arg starts with -- we are getopt_long_only()
403 */
404 if (long_options != NULL && place != nargv[optind] &&
405 (*place == '-' || (flags & FLAG_LONGONLY))) {
406 short_too = 0;
407 if (*place == '-')
408 place++; /* --foo long option */
409 else if (*place != ':' && strchr(options, *place) != NULL)
410 short_too = 1; /* could be short option too */
411
412 optchar = parse_long_options(nargv, options, long_options,
413 idx, short_too, flags);
414 if (optchar != -1) {
415 place = EMSG;
416 return (optchar);
417 }
418 }
419
420 if ((optchar = (int)*place++) == (int)':' ||
421 (oli = strchr(options, optchar)) == NULL) {
422 if (!*place)
423 ++optind;
424 if (PRINT_ERROR)
425 warnx(illoptchar, optchar);
426 optopt = optchar;
427 return (BADCH);
428 }
429 if (long_options != NULL && optchar == 'W' && oli[1] == ';') {
430 /* -W long-option */
431 if (*place) /* no space */
432 /* NOTHING */;
433 else if (++optind >= nargc) { /* no arg */
434 place = EMSG;
435 if (PRINT_ERROR)
436 warnx(recargchar, optchar);
437 optopt = optchar;
438 return (BADARG);
439 } else /* white space */
440 place = nargv[optind];
441 optchar = parse_long_options(nargv, options, long_options,
442 idx, 0, flags);
443 place = EMSG;
444 return (optchar);
445 }
446 if (*++oli != ':') { /* doesn't take argument */
447 if (!*place)
448 ++optind;
449 } else { /* takes (optional) argument */
450 optarg = NULL;
451 if (*place) /* no white space */
452 optarg = place;
453 else if (oli[1] != ':') { /* arg not optional */
454 if (++optind >= nargc) { /* no arg */
455 place = EMSG;
456 if (PRINT_ERROR)
457 warnx(recargchar, optchar);
458 optopt = optchar;
459 return (BADARG);
460 } else
461 optarg = nargv[optind];
462 }
463 place = EMSG;
464 ++optind;
465 }
466 /* dump back option letter */
467 return (optchar);
468}
469
470/*
471 * getopt --
472 * Parse argc/argv argument vector.
473 */
474int
475getopt(int nargc, char * const *nargv, const char *options)
476{
477
478 /*
479 * We don't pass FLAG_PERMUTE to getopt_internal() since
480 * the BSD getopt(3) (unlike GNU) has never done this.
481 *
482 * Furthermore, since many privileged programs call getopt()
483 * before dropping privileges it makes sense to keep things
484 * as simple (and bug-free) as possible.
485 */
486 return (getopt_internal(nargc, nargv, options, NULL, NULL, 0));
487}
488
489/*
490 * getopt_long --
491 * Parse argc/argv argument vector.
492 */
493int
494getopt_long(int nargc, char * const *nargv, const char *options,
495 const struct option *long_options, int *idx)
496{
497
498 return (getopt_internal(nargc, nargv, options, long_options, idx,
499 FLAG_PERMUTE));
500}
501
502/*
503 * getopt_long_only --
504 * Parse argc/argv argument vector.
505 */
506int
507getopt_long_only(int nargc, char * const *nargv, const char *options,
508 const struct option *long_options, int *idx)
509{
510
511 return (getopt_internal(nargc, nargv, options, long_options, idx,
512 FLAG_PERMUTE|FLAG_LONGONLY));
513}
diff --git a/src/lib/libc/stdlib/getsubopt.3 b/src/lib/libc/stdlib/getsubopt.3
deleted file mode 100644
index fee3840bef..0000000000
--- a/src/lib/libc/stdlib/getsubopt.3
+++ /dev/null
@@ -1,162 +0,0 @@
1.\" $OpenBSD: getsubopt.3,v 1.16 2022/08/04 06:20:24 jsg Exp $
2.\"
3.\" Copyright (c) 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. Neither the name of the University nor the names of its contributors
15.\" may be used to endorse or promote products derived from this software
16.\" without specific prior written permission.
17.\"
18.\" THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
19.\" ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
20.\" IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
21.\" ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
22.\" FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
23.\" DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
24.\" OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
25.\" HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
26.\" LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
27.\" OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
28.\" SUCH DAMAGE.
29.\"
30.\" @(#)getsubopt.3 8.1 (Berkeley) 6/9/93
31.\"
32.Dd $Mdocdate: August 4 2022 $
33.Dt GETSUBOPT 3
34.Os
35.Sh NAME
36.Nm getsubopt
37.Nd get sub options from an argument
38.Sh SYNOPSIS
39.In stdlib.h
40.Vt extern char *suboptarg;
41.Ft int
42.Fn getsubopt "char **optionp" "char * const *tokens" "char **valuep"
43.Sh DESCRIPTION
44The
45.Fn getsubopt
46function parses a string containing tokens delimited by one or more
47tab, space, or comma
48.Pq Ql \&,
49characters.
50It is intended for use in parsing groups of option arguments provided
51as part of a utility command line.
52.Pp
53The argument
54.Fa optionp
55is a pointer to a pointer to the string.
56The argument
57.Fa tokens
58is a pointer to a
59.Dv NULL Ns -terminated
60array of pointers to strings.
61.Pp
62The
63.Fn getsubopt
64function returns the zero-based offset of the pointer in the
65.Fa tokens
66array referencing a string which matches the first token
67in the string, or \-1 if the string contains no tokens or
68.Fa tokens
69does not contain a matching string.
70.Pp
71If the token is of the form
72.Ar name Ns = Ns Ar value ,
73the location referenced by
74.Fa valuep
75will be set to point to the start of the
76.Dq value
77portion of the token.
78.Pp
79On return from
80.Fn getsubopt ,
81.Fa optionp
82will be set to point to the start of the next token in the string,
83or the NUL at the end of the string if no more tokens are present.
84The comma, space, or tab character ending the token just parsed,
85and the equal sign separating name and value if any, are replaced
86with NUL bytes in the original
87.Pf * Fa optionp
88input string.
89The external variable
90.Fa suboptarg
91will be set to point to the start of the current token, or
92.Dv NULL
93if no tokens were present.
94The argument
95.Fa valuep
96will be set to point to the value portion of the token, or
97.Dv NULL
98if no value portion was present.
99.Sh EXAMPLES
100.Bd -literal
101char *tokens[] = {
102 #define ONE 0
103 "one",
104 #define TWO 1
105 "two",
106 NULL
107};
108
109\&...
110
111extern char *optarg, *suboptarg;
112char *options, *value;
113
114while ((ch = getopt(argc, argv, "ab:")) != -1) {
115 switch (ch) {
116 case 'a':
117 /* process "a" option */
118 break;
119 case 'b':
120 options = optarg;
121 while (*options) {
122 switch (getsubopt(&options, tokens, &value)) {
123 case ONE:
124 /* process "one" sub option */
125 break;
126 case TWO:
127 /* process "two" sub option */
128 if (!value)
129 error("no value for two");
130 i = atoi(value);
131 break;
132 case -1:
133 if (suboptarg)
134 error("illegal sub option %s",
135 suboptarg);
136 else
137 error("missing sub option");
138 break;
139 }
140 }
141 break;
142 }
143}
144.Ed
145.Sh SEE ALSO
146.Xr getopt 3 ,
147.Xr strsep 3
148.Sh STANDARDS
149The
150.Fn getsubopt
151function conforms to
152.St -p1003.1-2008 .
153.Pp
154Allowing space and tab characters to separate tokens
155and the external variable
156.Va suboptarg
157are extensions to that standard.
158.Sh HISTORY
159The
160.Fn getsubopt
161function first appeared in
162.Bx 4.3 Net/2 .
diff --git a/src/lib/libc/stdlib/getsubopt.c b/src/lib/libc/stdlib/getsubopt.c
deleted file mode 100644
index 735c85ba8a..0000000000
--- a/src/lib/libc/stdlib/getsubopt.c
+++ /dev/null
@@ -1,92 +0,0 @@
1/* $OpenBSD: getsubopt.c,v 1.4 2005/08/08 08:05:36 espie 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. Neither the name of the University nor the names of its contributors
16 * may be used to endorse or promote products derived from this software
17 * without specific prior written permission.
18 *
19 * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
20 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
21 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
22 * ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
23 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
24 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
25 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
26 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
27 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
28 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
29 * SUCH DAMAGE.
30 */
31
32#include <unistd.h>
33#include <stdlib.h>
34#include <string.h>
35
36/*
37 * The SVID interface to getsubopt provides no way of figuring out which
38 * part of the suboptions list wasn't matched. This makes error messages
39 * tricky... The extern variable suboptarg is a pointer to the token
40 * which didn't match.
41 */
42char *suboptarg;
43
44int
45getsubopt(char **optionp, char * const *tokens, char **valuep)
46{
47 int cnt;
48 char *p;
49
50 suboptarg = *valuep = NULL;
51
52 if (!optionp || !*optionp)
53 return(-1);
54
55 /* skip leading white-space, commas */
56 for (p = *optionp; *p && (*p == ',' || *p == ' ' || *p == '\t'); ++p);
57
58 if (!*p) {
59 *optionp = p;
60 return(-1);
61 }
62
63 /* save the start of the token, and skip the rest of the token. */
64 for (suboptarg = p;
65 *++p && *p != ',' && *p != '=' && *p != ' ' && *p != '\t';);
66
67 if (*p) {
68 /*
69 * If there's an equals sign, set the value pointer, and
70 * skip over the value part of the token. Terminate the
71 * token.
72 */
73 if (*p == '=') {
74 *p = '\0';
75 for (*valuep = ++p;
76 *p && *p != ',' && *p != ' ' && *p != '\t'; ++p);
77 if (*p)
78 *p++ = '\0';
79 } else
80 *p++ = '\0';
81 /* Skip any whitespace or commas after this token. */
82 for (; *p && (*p == ',' || *p == ' ' || *p == '\t'); ++p);
83 }
84
85 /* set optionp for next round. */
86 *optionp = p;
87
88 for (cnt = 0; *tokens; ++tokens, ++cnt)
89 if (!strcmp(suboptarg, *tokens))
90 return(cnt);
91 return(-1);
92}
diff --git a/src/lib/libc/stdlib/hcreate.3 b/src/lib/libc/stdlib/hcreate.3
deleted file mode 100644
index 90bde1995f..0000000000
--- a/src/lib/libc/stdlib/hcreate.3
+++ /dev/null
@@ -1,234 +0,0 @@
1.\" $OpenBSD: hcreate.3,v 1.8 2018/01/30 11:37:58 jmc Exp $
2.\" $NetBSD: hcreate.3,v 1.8 2010/05/01 06:18:03 jruoho Exp $
3.\"
4.\" Copyright (c) 1999 The NetBSD Foundation, Inc.
5.\" All rights reserved.
6.\"
7.\" This code is derived from software contributed to The NetBSD Foundation
8.\" by Klaus Klein.
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.\"
19.\" THIS SOFTWARE IS PROVIDED BY THE NETBSD FOUNDATION, INC. AND CONTRIBUTORS
20.\" ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED
21.\" TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
22.\" PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE FOUNDATION OR CONTRIBUTORS
23.\" BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
24.\" CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
25.\" SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
26.\" INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
27.\" CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
28.\" ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
29.\" POSSIBILITY OF SUCH DAMAGE.
30.\"
31.Dd $Mdocdate: January 30 2018 $
32.Dt HCREATE 3
33.Os
34.Sh NAME
35.Nm hcreate ,
36.Nm hdestroy ,
37.Nm hsearch
38.Nd manage hash search table
39.Sh SYNOPSIS
40.In search.h
41.Ft int
42.Fn hcreate "size_t nel"
43.Ft void
44.Fn hdestroy "void"
45.Ft ENTRY *
46.Fn hsearch "ENTRY item" "ACTION action"
47.Sh DESCRIPTION
48The
49.Fn hcreate ,
50.Fn hdestroy ,
51and
52.Fn hsearch
53functions manage hash search tables.
54.Pp
55The
56.Fn hcreate
57function allocates and initializes the table.
58The
59.Fa nel
60argument specifies an estimate of the maximum number of entries to be held
61by the table.
62Unless further memory allocation fails, supplying an insufficient
63.Fa nel
64value will not result in functional harm, although a performance degradation
65may occur.
66Initialization using the
67.Fn hcreate
68function is mandatory prior to any access operations using
69.Fn hsearch .
70.Pp
71The
72.Fn hdestroy
73function destroys a table previously created using
74.Fn hcreate .
75After a call to
76.Fn hdestroy ,
77the data can no longer be accessed.
78.Pp
79The
80.Fn hsearch
81function is used to search the hash table.
82It returns a pointer into the
83hash table indicating the address of an item.
84The
85.Fa item
86argument is of type
87.Vt ENTRY ,
88defined in the
89.In search.h
90header.
91This is a structure type that contains two pointers:
92.Pp
93.Bl -tag -compact -offset indent -width "void *data "
94.It Fa char *key
95comparison key
96.It Fa void *data
97pointer to data associated with
98.Fa key
99.El
100.Pp
101The key comparison function used by
102.Fn hsearch
103is
104.Xr strcmp 3 .
105.Pp
106The
107.Fa action
108argument is of type
109.Vt ACTION ,
110an enumeration type which defines the following values:
111.Bl -tag -offset indent -width ENTERXX
112.It Dv ENTER
113Insert
114.Fa item
115into the hash table.
116If an existing item with the same key is found, it is not replaced.
117Note that the
118.Fa key
119and
120.Fa data
121elements of
122.Fa item
123are used directly by the new table entry.
124The storage for the
125key must not be modified during the lifetime of the hash table.
126.It Dv FIND
127Search the hash table without inserting
128.Fa item .
129.El
130.Pp
131Note that the comparison
132.Fa key
133must be allocated using
134.Xr malloc 3
135or
136.Xr calloc 3
137if action is
138.Dv ENTER
139and
140.Fn hdestroy
141will be called.
142This is because
143.Fn hdestroy
144will call
145.Xr free 3
146for each comparison
147.Fa key
148(but not
149.Fa data ) .
150Typically the comparison
151.Fa key
152is allocated by using
153.Xr strdup 3 .
154.Sh RETURN VALUES
155If successful, the
156.Fn hcreate
157function returns a non-zero value.
158Otherwise, a value of 0 is returned and
159.Va errno
160is set to indicate the error.
161.Pp
162If successful, the
163.Fn hsearch
164function returns a pointer to a hash table entry matching
165the provided key.
166If the action is
167.Dv FIND
168and the item was not found, or if the action is
169.Dv ENTER
170and the insertion failed,
171.Dv NULL
172is returned and
173.Va errno
174is set to indicate the error.
175If the action is
176.Dv ENTER
177and an entry already existed in the table matching the given
178key, the existing entry is returned and is not replaced.
179.Sh ERRORS
180The
181.Fn hcreate
182and
183.Fn hsearch
184functions will fail if:
185.Bl -tag -width Er
186.It Bq Er ENOMEM
187Insufficient memory is available.
188.El
189.Sh SEE ALSO
190.Xr bsearch 3 ,
191.Xr lsearch 3 ,
192.Xr malloc 3 ,
193.Xr strcmp 3
194.Sh STANDARDS
195The
196.Fn hcreate ,
197.Fn hdestroy
198and
199.Fn hsearch
200functions conform to
201.St -xpg4.2 .
202.Sh HISTORY
203The
204.Fn hcreate ,
205.Fn hdestroy
206and
207.Fn hsearch
208functions first appeared in
209.At V .
210.Sh CAVEATS
211At least the following limitations can be mentioned:
212.Bl -bullet
213.It
214The interface permits the use of only one hash table at a time.
215.It
216Individual hash table entries can be added, but not deleted.
217.It
218The standard is indecipherable about the
219internal memory usage of the functions,
220mentioning only that
221.Do
222.Fn hcreate
223and
224.Fn hsearch
225functions may use
226.Fn malloc
227to allocate space
228.Dc .
229This limits the portability of the functions,
230given that other implementations may not
231.Xr free 3
232the buffer pointed by
233.Fa key .
234.El
diff --git a/src/lib/libc/stdlib/hcreate.c b/src/lib/libc/stdlib/hcreate.c
deleted file mode 100644
index b31108a90e..0000000000
--- a/src/lib/libc/stdlib/hcreate.c
+++ /dev/null
@@ -1,189 +0,0 @@
1/* $OpenBSD: hcreate.c,v 1.7 2016/05/29 20:47:49 guenther Exp $ */
2/* $NetBSD: hcreate.c,v 1.5 2004/04/23 02:48:12 simonb Exp $ */
3
4/*
5 * Copyright (c) 2001 Christopher G. Demetriou
6 * All rights reserved.
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 for the
19 * NetBSD Project. See http://www.NetBSD.org/ for
20 * information about NetBSD.
21 * 4. The name of the author may not be used to endorse or promote products
22 * derived from this software without specific prior written permission.
23 *
24 * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR
25 * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
26 * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
27 * IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT,
28 * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
29 * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
30 * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
31 * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
32 * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF
33 * THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
34 *
35 * <<Id: LICENSE,v 1.2 2000/06/14 15:57:33 cgd Exp>>
36 */
37
38/*
39 * hcreate() / hsearch() / hdestroy()
40 *
41 * SysV/XPG4 hash table functions.
42 *
43 * Implementation done based on NetBSD manual page and Solaris manual page,
44 * plus my own personal experience about how they're supposed to work.
45 *
46 * I tried to look at Knuth (as cited by the Solaris manual page), but
47 * nobody had a copy in the office, so...
48 */
49
50#include <assert.h>
51#include <errno.h>
52#include <stdint.h>
53#include <search.h>
54#include <stdlib.h>
55#include <string.h>
56#include <sys/queue.h>
57
58#include <db.h> /* for __default_hash */
59
60#ifndef _DIAGASSERT
61#define _DIAGASSERT(x)
62#endif
63
64/*
65 * DO NOT MAKE THIS STRUCTURE LARGER THAN 32 BYTES (4 ptrs on 64-bit
66 * ptr machine) without adjusting MAX_BUCKETS_LG2 below.
67 */
68struct internal_entry {
69 SLIST_ENTRY(internal_entry) link;
70 ENTRY ent;
71};
72SLIST_HEAD(internal_head, internal_entry);
73
74#define MIN_BUCKETS_LG2 4
75#define MIN_BUCKETS (1 << MIN_BUCKETS_LG2)
76
77/*
78 * max * sizeof internal_entry must fit into size_t.
79 * assumes internal_entry is <= 32 (2^5) bytes.
80 */
81#define MAX_BUCKETS_LG2 (sizeof (size_t) * 8 - 1 - 5)
82#define MAX_BUCKETS ((size_t)1 << MAX_BUCKETS_LG2)
83
84static struct internal_head *htable;
85static size_t htablesize;
86
87int
88hcreate(size_t nel)
89{
90 size_t idx;
91 unsigned int p2;
92
93 /* Make sure this isn't called when a table already exists. */
94 _DIAGASSERT(htable == NULL);
95 if (htable != NULL) {
96 errno = EINVAL;
97 return 0;
98 }
99
100 /* If nel is too small, make it min sized. */
101 if (nel < MIN_BUCKETS)
102 nel = MIN_BUCKETS;
103
104 /* If it's too large, cap it. */
105 if (nel > MAX_BUCKETS)
106 nel = MAX_BUCKETS;
107
108 /* If it's is not a power of two in size, round up. */
109 if ((nel & (nel - 1)) != 0) {
110 for (p2 = 0; nel != 0; p2++)
111 nel >>= 1;
112 _DIAGASSERT(p2 <= MAX_BUCKETS_LG2);
113 nel = 1 << p2;
114 }
115
116 /* Allocate the table. */
117 htablesize = nel;
118 htable = calloc(htablesize, sizeof htable[0]);
119 if (htable == NULL) {
120 errno = ENOMEM;
121 return 0;
122 }
123
124 /* Initialize it. */
125 for (idx = 0; idx < htablesize; idx++)
126 SLIST_INIT(&htable[idx]);
127
128 return 1;
129}
130
131void
132hdestroy(void)
133{
134 struct internal_entry *ie;
135 size_t idx;
136
137 _DIAGASSERT(htable != NULL);
138 if (htable == NULL)
139 return;
140
141 for (idx = 0; idx < htablesize; idx++) {
142 while (!SLIST_EMPTY(&htable[idx])) {
143 ie = SLIST_FIRST(&htable[idx]);
144 SLIST_REMOVE_HEAD(&htable[idx], link);
145 free(ie->ent.key);
146 free(ie);
147 }
148 }
149 free(htable);
150 htable = NULL;
151}
152
153ENTRY *
154hsearch(ENTRY item, ACTION action)
155{
156 struct internal_head *head;
157 struct internal_entry *ie;
158 uint32_t hashval;
159 size_t len;
160
161 _DIAGASSERT(htable != NULL);
162 _DIAGASSERT(item.key != NULL);
163 _DIAGASSERT(action == ENTER || action == FIND);
164
165 len = strlen(item.key);
166 hashval = __default_hash(item.key, len);
167
168 head = &htable[hashval & (htablesize - 1)];
169 ie = SLIST_FIRST(head);
170 while (ie != NULL) {
171 if (strcmp(ie->ent.key, item.key) == 0)
172 break;
173 ie = SLIST_NEXT(ie, link);
174 }
175
176 if (ie != NULL)
177 return &ie->ent;
178 else if (action == FIND)
179 return NULL;
180
181 ie = malloc(sizeof *ie);
182 if (ie == NULL)
183 return NULL;
184 ie->ent.key = item.key;
185 ie->ent.data = item.data;
186
187 SLIST_INSERT_HEAD(head, ie, link);
188 return &ie->ent;
189}
diff --git a/src/lib/libc/stdlib/heapsort.c b/src/lib/libc/stdlib/heapsort.c
deleted file mode 100644
index f1db2205b0..0000000000
--- a/src/lib/libc/stdlib/heapsort.c
+++ /dev/null
@@ -1,175 +0,0 @@
1/* $OpenBSD: heapsort.c,v 1.11 2017/05/20 12:48:56 millert Exp $ */
2/*-
3 * Copyright (c) 1991, 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 * Ronnie Kon at Mindcraft Inc., Kevin Lew and Elmer Yglesias.
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. 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#include <sys/types.h>
35#include <errno.h>
36#include <stdlib.h>
37
38/*
39 * Swap two areas of size number of bytes. Although qsort(3) permits random
40 * blocks of memory to be sorted, sorting pointers is almost certainly the
41 * common case (and, were it not, could easily be made so). Regardless, it
42 * isn't worth optimizing; the SWAP's get sped up by the cache, and pointer
43 * arithmetic gets lost in the time required for comparison function calls.
44 */
45#define SWAP(a, b, count, size, tmp) { \
46 count = size; \
47 do { \
48 tmp = *a; \
49 *a++ = *b; \
50 *b++ = tmp; \
51 } while (--count); \
52}
53
54/* Copy one block of size size to another. */
55#define COPY(a, b, count, size, tmp1, tmp2) { \
56 count = size; \
57 tmp1 = a; \
58 tmp2 = b; \
59 do { \
60 *tmp1++ = *tmp2++; \
61 } while (--count); \
62}
63
64/*
65 * Build the list into a heap, where a heap is defined such that for
66 * the records K1 ... KN, Kj/2 >= Kj for 1 <= j/2 <= j <= N.
67 *
68 * There are two cases. If j == nmemb, select largest of Ki and Kj. If
69 * j < nmemb, select largest of Ki, Kj and Kj+1.
70 */
71#define CREATE(initval, nmemb, par_i, child_i, par, child, size, count, tmp) { \
72 for (par_i = initval; (child_i = par_i * 2) <= nmemb; \
73 par_i = child_i) { \
74 child = base + child_i * size; \
75 if (child_i < nmemb && compar(child, child + size) < 0) { \
76 child += size; \
77 ++child_i; \
78 } \
79 par = base + par_i * size; \
80 if (compar(child, par) <= 0) \
81 break; \
82 SWAP(par, child, count, size, tmp); \
83 } \
84}
85
86/*
87 * Select the top of the heap and 'heapify'. Since by far the most expensive
88 * action is the call to the compar function, a considerable optimization
89 * in the average case can be achieved due to the fact that k, the displaced
90 * element, is usually quite small, so it would be preferable to first
91 * heapify, always maintaining the invariant that the larger child is copied
92 * over its parent's record.
93 *
94 * Then, starting from the *bottom* of the heap, finding k's correct place,
95 * again maintaining the invariant. As a result of the invariant no element
96 * is 'lost' when k is assigned its correct place in the heap.
97 *
98 * The time savings from this optimization are on the order of 15-20% for the
99 * average case. See Knuth, Vol. 3, page 158, problem 18.
100 *
101 * XXX Don't break the #define SELECT line, below. Reiser cpp gets upset.
102 */
103#define SELECT(par_i, child_i, nmemb, par, child, size, k, count, tmp1, tmp2) { \
104 for (par_i = 1; (child_i = par_i * 2) <= nmemb; par_i = child_i) { \
105 child = base + child_i * size; \
106 if (child_i < nmemb && compar(child, child + size) < 0) { \
107 child += size; \
108 ++child_i; \
109 } \
110 par = base + par_i * size; \
111 COPY(par, child, count, size, tmp1, tmp2); \
112 } \
113 for (;;) { \
114 child_i = par_i; \
115 par_i = child_i / 2; \
116 child = base + child_i * size; \
117 par = base + par_i * size; \
118 if (child_i == 1 || compar(k, par) < 0) { \
119 COPY(child, k, count, size, tmp1, tmp2); \
120 break; \
121 } \
122 COPY(child, par, count, size, tmp1, tmp2); \
123 } \
124}
125
126/*
127 * Heapsort -- Knuth, Vol. 3, page 145. Runs in O (N lg N), both average
128 * and worst. While heapsort is faster than the worst case of quicksort,
129 * the BSD quicksort does median selection so that the chance of finding
130 * a data set that will trigger the worst case is nonexistent. Heapsort's
131 * only advantage over quicksort is that it requires little additional memory.
132 */
133int
134heapsort(void *vbase, size_t nmemb, size_t size,
135 int (*compar)(const void *, const void *))
136{
137 size_t cnt, i, j, l;
138 char tmp, *tmp1, *tmp2;
139 char *base, *k, *p, *t;
140
141 if (nmemb <= 1)
142 return (0);
143
144 if (!size) {
145 errno = EINVAL;
146 return (-1);
147 }
148
149 if ((k = malloc(size)) == NULL)
150 return (-1);
151
152 /*
153 * Items are numbered from 1 to nmemb, so offset from size bytes
154 * below the starting address.
155 */
156 base = (char *)vbase - size;
157
158 for (l = nmemb / 2 + 1; --l;)
159 CREATE(l, nmemb, i, j, t, p, size, cnt, tmp);
160
161 /*
162 * For each element of the heap, save the largest element into its
163 * final slot, save the displaced element (k), then recreate the
164 * heap.
165 */
166 while (nmemb > 1) {
167 COPY(k, base + nmemb * size, cnt, size, tmp1, tmp2);
168 COPY(base + nmemb * size, base + size, cnt, size, tmp1, tmp2);
169 --nmemb;
170 SELECT(i, j, nmemb, t, p, size, k, cnt, tmp1, tmp2);
171 }
172 free(k);
173 return (0);
174}
175DEF_WEAK(heapsort);
diff --git a/src/lib/libc/stdlib/icdb.c b/src/lib/libc/stdlib/icdb.c
deleted file mode 100644
index 2ddd9db48c..0000000000
--- a/src/lib/libc/stdlib/icdb.c
+++ /dev/null
@@ -1,391 +0,0 @@
1/* $OpenBSD: icdb.c,v 1.8 2016/09/04 16:56:02 nicm Exp $ */
2/*
3 * Copyright (c) 2015 Ted Unangst <tedu@openbsd.org>
4 *
5 * Permission to use, copy, modify, and distribute this software for any
6 * purpose with or without fee is hereby granted, provided that the above
7 * copyright notice and this permission notice appear in all copies.
8 *
9 * THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES
10 * WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF
11 * MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR
12 * ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES
13 * WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN
14 * ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF
15 * OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE.
16 */
17
18#include <errno.h>
19#include <fcntl.h>
20#include <icdb.h>
21#include <stddef.h>
22#include <stdint.h>
23#include <stdio.h>
24#include <stdlib.h>
25#include <string.h>
26#include <unistd.h>
27
28#include <sys/mman.h>
29#include <sys/stat.h>
30
31#include <siphash.h>
32
33/*
34 * Creating a new icdb: icdb_new
35 * Opening existing icdb: icdb_open
36 *
37 * Adding new entries: icdb_add
38 * Adding entries does not update the disk or indices.
39 *
40 * Save to disk: icdb_save
41 * Update indices: icdb_rehash
42 * icdb_save will call rehash.
43 *
44 * Change an existing entry: icdb_update
45 * Changing entries does write to disk.
46 *
47 * Find an entry: icdb_lookup
48 * Looking up an entry is only defined when the indices are synced.
49 *
50 * Close and free resources: icdb_close
51 */
52
53/*
54 * There are two major modes of operation.
55 *
56 * Existing databases use the mmap codepath. The entire database is mapped
57 * into the address space for quick access. Individual entries may be updated,
58 * but no new entries added.
59 *
60 * New databases use malloc backed memory instead. The database may be saved
61 * with icdb_save. It should be saved to a new file to avoid corrupting any
62 * open databases in other processes.
63 */
64
65/*
66 * An icdb has the following format:
67 * struct icbinfo header
68 * indexes [ uint32_t * indexsize * nkeys ]
69 * entries [ entrysize * nentries ]
70 *
71 * To find an entry in the file, the user specifies which key to use.
72 * The key is hashed and looked up in the index. The index contains the
73 * position of the entry in the entries array. -1 identifies not found.
74 * Chaining is done by rehashing the hash. All keys are fixed size byte arrays.
75 */
76
77/*
78 * Header info for icdb. This struct is stored on disk.
79 */
80struct icdbinfo {
81 uint32_t magic; /* magic */
82 uint32_t version; /* user specified version */
83 uint32_t nentries; /* number of entries stored */
84 uint32_t entrysize; /* size of each entry */
85 uint32_t indexsize; /* number of entries in hash index */
86 uint32_t nkeys; /* number of keys defined */
87 uint32_t keysize[8]; /* size of each key */
88 uint32_t keyoffset[8]; /* offset of each key in entry */
89 SIPHASH_KEY siphashkey; /* random hash key */
90};
91
92/*
93 * In memory representation with auxiliary data.
94 * idxdata and entries will be written to disk after info.
95 */
96struct icdb {
97 struct icdbinfo *info;
98 void *idxdata[8];
99 void *entries;
100 size_t maplen;
101 uint32_t allocated;
102 int fd;
103};
104
105static const uint32_t magic = 0x1ca9d0b7;
106
107static uint32_t
108roundup(uint32_t num)
109{
110 uint32_t r = 2;
111
112 while (r < num * 3 / 2)
113 r *= 2;
114 return r;
115}
116
117struct icdb *
118icdb_new(uint32_t version, uint32_t nentries, uint32_t entrysize,
119 uint32_t nkeys, const uint32_t *keysizes, const uint32_t *keyoffsets)
120{
121 struct icdb *db;
122 struct icdbinfo *info;
123 int i;
124
125 if (entrysize == 0 || entrysize > 1048576 || nkeys > 8) {
126 errno = EINVAL;
127 return NULL;
128 }
129
130 if (!(db = calloc(1, sizeof(*db))))
131 return NULL;
132 if (!(info = calloc(1, sizeof(*info)))) {
133 free(db);
134 return NULL;
135 }
136 db->info = info;
137 db->fd = -1;
138 info->magic = magic;
139 info->version = version;
140 if (nentries)
141 if ((db->entries = reallocarray(NULL, nentries, entrysize)))
142 db->allocated = nentries;
143 info->entrysize = entrysize;
144 info->nkeys = nkeys;
145 for (i = 0; i < nkeys; i++) {
146 info->keysize[i] = keysizes[i];
147 info->keyoffset[i] = keyoffsets[i];
148 }
149 return db;
150}
151DEF_WEAK(icdb_new);
152
153struct icdb *
154icdb_open(const char *name, int flags, uint32_t version)
155{
156 struct icdb *db = NULL;
157 struct icdbinfo *info;
158 struct stat sb;
159 uint8_t *ptr = MAP_FAILED;
160 uint32_t baseoff, indexsize, idxmask, idxlen;
161 int fd, i, saved_errno;
162
163 if ((fd = open(name, flags | O_CLOEXEC)) == -1)
164 return NULL;
165 if (fstat(fd, &sb) != 0)
166 goto fail;
167 if (sb.st_size < sizeof(struct icdbinfo))
168 goto fail;
169 ptr = mmap(NULL, sb.st_size, PROT_READ |
170 ((flags & O_RDWR) ? PROT_WRITE : 0), MAP_SHARED, fd, 0);
171 if (ptr == MAP_FAILED)
172 goto fail;
173 info = (struct icdbinfo *)ptr;
174 if (info->magic != magic || info->version != version) {
175 errno = ENOENT;
176 goto fail;
177 }
178
179 if (!(db = calloc(1, sizeof(*db))))
180 goto fail;
181 db->info = info;
182
183 indexsize = info->indexsize;
184 idxmask = indexsize - 1;
185 idxlen = indexsize * sizeof(uint32_t);
186 baseoff = sizeof(*info) + idxlen * info->nkeys;
187
188 for (i = 0; i < info->nkeys; i++)
189 db->idxdata[i] = ptr + sizeof(*info) + i * idxlen;
190 db->entries = ptr + baseoff;
191 db->maplen = sb.st_size;
192 db->fd = fd;
193 return db;
194
195fail:
196 saved_errno = errno;
197 if (ptr != MAP_FAILED)
198 munmap(ptr, sb.st_size);
199 if (fd != -1)
200 close(fd);
201 free(db);
202 errno = saved_errno;
203 return NULL;
204}
205DEF_WEAK(icdb_open);
206
207int
208icdb_get(struct icdb *db, void *entry, uint32_t idx)
209{
210 uint32_t entrysize = db->info->entrysize;
211
212 memcpy(entry, (uint8_t *)db->entries + idx * entrysize, entrysize);
213 return 0;
214}
215DEF_WEAK(icdb_get);
216
217int
218icdb_lookup(struct icdb *db, int keynum, const void *key, void *entry,
219 uint32_t *idxp)
220{
221 struct icdbinfo *info = db->info;
222 uint32_t offset;
223 uint64_t hash;
224 uint32_t indexsize, idxmask, idxlen;
225 uint32_t *idxdata;
226
227 indexsize = info->indexsize;
228 idxmask = indexsize - 1;
229 idxlen = indexsize * sizeof(uint32_t);
230
231 idxdata = db->idxdata[keynum];
232
233 hash = SipHash24(&info->siphashkey, key, info->keysize[keynum]);
234 while ((offset = idxdata[hash & idxmask]) != -1) {
235 if (icdb_get(db, entry, offset) != 0) {
236 errno = ENOENT;
237 return -1;
238 }
239 if (memcmp((uint8_t *)entry + info->keyoffset[keynum], key,
240 info->keysize[keynum]) == 0) {
241 if (idxp)
242 *idxp = offset;
243 return 0;
244 }
245 hash = SipHash24(&info->siphashkey, &hash, sizeof(hash));
246 }
247 return 1;
248}
249DEF_WEAK(icdb_lookup);
250
251int
252icdb_nentries(struct icdb *db)
253{
254 return db->info->nentries;
255}
256DEF_WEAK(icdb_nentries);
257
258const void *
259icdb_entries(struct icdb *db)
260{
261 return db->entries;
262}
263DEF_WEAK(icdb_entries);
264
265int
266icdb_update(struct icdb *db, const void *entry, int offset)
267{
268 struct icdbinfo *info = db->info;
269 uint32_t entrysize = info->entrysize;
270 uint32_t baseoff;
271 uint32_t indexsize, idxmask, idxlen;
272
273 indexsize = info->indexsize;
274 idxmask = indexsize - 1;
275 idxlen = indexsize * sizeof(uint32_t);
276 baseoff = sizeof(*info) + idxlen * info->nkeys;
277
278 memcpy((uint8_t *)db->entries + offset * entrysize, entry, entrysize);
279 if (db->fd != -1) {
280 msync((uint8_t *)db->entries + offset * entrysize, entrysize,
281 MS_SYNC);
282 }
283 return 0;
284}
285DEF_WEAK(icdb_update);
286
287int
288icdb_add(struct icdb *db, const void *entry)
289{
290 struct icdbinfo *info = db->info;
291 size_t entrysize = info->entrysize;
292
293 if (db->allocated == info->nentries) {
294 void *p;
295 size_t amt = db->allocated ? db->allocated * 2 : 63;
296 if (!(p = reallocarray(db->entries, amt, entrysize)))
297 return -1;
298 db->allocated = amt;
299 db->entries = p;
300 }
301 memcpy((uint8_t *)db->entries + info->nentries * entrysize,
302 entry, entrysize);
303 info->nentries++;
304 return 0;
305}
306DEF_WEAK(icdb_add);
307
308int
309icdb_rehash(struct icdb *db)
310{
311 struct icdbinfo *info = db->info;
312 uint32_t entrysize = info->entrysize;
313 uint32_t indexsize, idxmask, idxlen;
314 int i, j;
315
316 indexsize = info->indexsize = roundup(info->nentries);
317 idxmask = indexsize - 1;
318 idxlen = sizeof(uint32_t) * indexsize;
319
320 arc4random_buf(&info->siphashkey, sizeof(info->siphashkey));
321
322 for (i = 0; i < info->nkeys; i++) {
323 uint32_t *idxdata = reallocarray(db->idxdata[i],
324 indexsize, sizeof(uint32_t));
325 if (!idxdata)
326 return -1;
327 memset(idxdata, 0xff, idxlen);
328 db->idxdata[i] = idxdata;
329 }
330 for (j = 0; j < info->nentries; j++) {
331 for (i = 0; i < info->nkeys; i++) {
332 uint32_t *idxdata = db->idxdata[i];
333 uint64_t hash = SipHash24(&info->siphashkey,
334 (uint8_t *)db->entries + j * entrysize +
335 info->keyoffset[i], info->keysize[i]);
336 while (idxdata[hash & idxmask] != -1)
337 hash = SipHash24(&info->siphashkey, &hash, sizeof(hash));
338 idxdata[hash & idxmask] = j;
339 }
340 }
341 return 0;
342}
343DEF_WEAK(icdb_rehash);
344
345int
346icdb_save(struct icdb *db, int fd)
347{
348 struct icdbinfo *info = db->info;
349 uint32_t entrysize = info->entrysize;
350 uint32_t indexsize, idxlen;
351 int i;
352
353 if (icdb_rehash(db) != 0)
354 return -1;
355
356 indexsize = info->indexsize;
357 idxlen = sizeof(uint32_t) * indexsize;
358
359 if (ftruncate(fd, 0) != 0)
360 return -1;
361 if (write(fd, info, sizeof(*info)) != sizeof(*info))
362 return -1;
363 for (i = 0; i < info->nkeys; i++) {
364 if (write(fd, db->idxdata[i], idxlen) != idxlen)
365 return -1;
366 }
367 if (write(fd, db->entries, info->nentries * entrysize) !=
368 info->nentries * entrysize)
369 return -1;
370 return 0;
371}
372DEF_WEAK(icdb_save);
373
374int
375icdb_close(struct icdb *db)
376{
377 int i;
378
379 if (db->fd == -1) {
380 for (i = 0; i < db->info->nkeys; i++)
381 free(db->idxdata[i]);
382 free(db->entries);
383 free(db->info);
384 } else {
385 munmap(db->info, db->maplen);
386 close(db->fd);
387 }
388 free(db);
389 return 0;
390}
391DEF_WEAK(icdb_close);
diff --git a/src/lib/libc/stdlib/icdb_new.3 b/src/lib/libc/stdlib/icdb_new.3
deleted file mode 100644
index 9fc07bda80..0000000000
--- a/src/lib/libc/stdlib/icdb_new.3
+++ /dev/null
@@ -1,68 +0,0 @@
1.\" $OpenBSD: icdb_new.3,v 1.2 2016/09/04 19:05:09 jmc Exp $
2.\"
3.\" Copyright (c) Ted Unangst
4.\"
5.\" Permission to use, copy, modify, and distribute this software for any
6.\" purpose with or without fee is hereby granted, provided that the above
7.\" copyright notice and this permission notice appear in all copies.
8.\"
9.\" THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES
10.\" WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF
11.\" MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR
12.\" ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES
13.\" WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN
14.\" ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF
15.\" OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE.
16.\"
17.\"
18.Dd $Mdocdate: September 4 2016 $
19.Dt ICBB_NEW 3
20.Os
21.Sh NAME
22.Nm icdb_new ,
23.Nm icdb_open ,
24.Nm icdb_get ,
25.Nm icdb_lookup ,
26.Nm icdb_nentries ,
27.Nm icdb_entries ,
28.Nm icdb_update ,
29.Nm icdb_add ,
30.Nm icdb_rehash ,
31.Nm icdb_save ,
32.Nm icdb_close
33.Nd simple database
34.Sh SYNOPSIS
35.In icbd.h
36.Ft struct icdb *
37.Fn icdb_new "uint32_t version" "uint32_t nentries" "uint32_t entrysize"
38.Ft struct icdb *
39.Fn icdb_open "const char *name" "int flags" "uint32_t version"
40.Ft int
41.Fn icdb_get "struct icdb *db" "void *entry" "uint32_t idx"
42.Ft int
43.Fn icdb_lookup "struct icdb *db" "int keynum" "const void *key" "void *entry" "uint32_t *idxp"
44.Ft int
45.Fn icdb_nentries "struct icdb *db"
46.Ft const void *
47.Fn icdb_entries "struct icdb *db"
48.Ft int
49.Fn icdb_update "struct icdb *db" "const void *entry" "int offset"
50.Ft int
51.Fn icdb_add "struct icdb *db" "const void *entry"
52.Ft int
53.Fn icdb_rehash "struct icdb *db"
54.Ft int
55.Fn icdb_save "struct icdb *db" "int fd"
56.Ft int
57.Fn icdb_close "struct icdb *db"
58.Sh DESCRIPTION
59These functions provide access to a simple memory mapped database format.
60.Sh EXAMPLES
61Look how easy it is to use.
62.Sh STANDARDS
63These functions are not standardized.
64.Sh HISTORY
65The icdb functions were introduced in
66.Ox 6.0 .
67.Sh AUTHORS
68.An Ted Unangst Aq Mt tedu@openbsd.org
diff --git a/src/lib/libc/stdlib/imaxabs.3 b/src/lib/libc/stdlib/imaxabs.3
deleted file mode 100644
index 340be61b6e..0000000000
--- a/src/lib/libc/stdlib/imaxabs.3
+++ /dev/null
@@ -1,70 +0,0 @@
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. 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.\" $OpenBSD: imaxabs.3,v 1.8 2019/01/18 07:32:17 schwarze Exp $
33.\"
34.Dd $Mdocdate: January 18 2019 $
35.Dt IMAXABS 3
36.Os
37.Sh NAME
38.Nm imaxabs
39.Nd integer absolute value function
40.Sh SYNOPSIS
41.In inttypes.h
42.In stdint.h
43.Ft intmax_t
44.Fn imaxabs "intmax_t j"
45.Sh DESCRIPTION
46The
47.Fn imaxabs
48function computes the absolute value of the intmax_t variable
49.Fa j .
50.Sh RETURN VALUES
51The
52.Fn imaxabs
53function returns the absolute value.
54.Sh SEE ALSO
55.Xr abs 3 ,
56.Xr cabs 3 ,
57.Xr floor 3 ,
58.Xr hypot 3 ,
59.Xr labs 3
60.Sh STANDARDS
61The
62.Fn imaxabs
63function conforms to
64.St -isoC-99 .
65.Sh CAVEATS
66The result of applying
67.Fn imaxabs
68to
69.Dv INTMAX_MIN
70is undefined.
diff --git a/src/lib/libc/stdlib/imaxabs.c b/src/lib/libc/stdlib/imaxabs.c
deleted file mode 100644
index b7e910eefd..0000000000
--- a/src/lib/libc/stdlib/imaxabs.c
+++ /dev/null
@@ -1,38 +0,0 @@
1/* $OpenBSD: imaxabs.c,v 1.1 2006/01/13 17:58:09 millert Exp $ */
2
3/*-
4 * Copyright (c) 1990 The Regents of the University of California.
5 * 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. Neither the name of the University nor the names of its contributors
16 * may be used to endorse or promote products derived from this software
17 * without specific prior written permission.
18 *
19 * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
20 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
21 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
22 * ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
23 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
24 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
25 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
26 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
27 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
28 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
29 * SUCH DAMAGE.
30 */
31
32#include <inttypes.h>
33
34intmax_t
35imaxabs(intmax_t j)
36{
37 return (j < 0 ? -j : j);
38}
diff --git a/src/lib/libc/stdlib/imaxdiv.3 b/src/lib/libc/stdlib/imaxdiv.3
deleted file mode 100644
index 70168acb30..0000000000
--- a/src/lib/libc/stdlib/imaxdiv.3
+++ /dev/null
@@ -1,65 +0,0 @@
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. 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.\" $OpenBSD: imaxdiv.3,v 1.8 2022/09/11 06:38:11 jmc Exp $
33.\"
34.Dd $Mdocdate: September 11 2022 $
35.Dt IMAXDIV 3
36.Os
37.Sh NAME
38.Nm imaxdiv
39.Nd return quotient and remainder from division
40.Sh SYNOPSIS
41.In inttypes.h
42.Ft imaxdiv_t
43.Fn imaxdiv "intmax_t num" "intmax_t denom"
44.Sh DESCRIPTION
45The
46.Fn imaxdiv
47function computes the value
48.Fa num Ns / Ns Fa denom
49and returns the quotient and remainder in a structure named
50.Vt imaxdiv_t
51that contains two
52.Vt intmax_t
53members named
54.Fa quot
55and
56.Fa rem .
57.Sh SEE ALSO
58.Xr div 3 ,
59.Xr ldiv 3 ,
60.Xr lldiv 3
61.Sh STANDARDS
62The
63.Fn imaxdiv
64function conforms to
65.St -isoC-99 .
diff --git a/src/lib/libc/stdlib/imaxdiv.c b/src/lib/libc/stdlib/imaxdiv.c
deleted file mode 100644
index 0515a94b96..0000000000
--- a/src/lib/libc/stdlib/imaxdiv.c
+++ /dev/null
@@ -1,50 +0,0 @@
1/* $OpenBSD: imaxdiv.c,v 1.1 2006/01/13 17:58:09 millert Exp $ */
2/*
3 * Copyright (c) 1990 Regents of the University of California.
4 * All rights reserved.
5 *
6 * This code is derived from software contributed to Berkeley by
7 * Chris Torek.
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. 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#include <inttypes.h> /* imaxdiv_t */
35
36imaxdiv_t
37imaxdiv(intmax_t num, intmax_t denom)
38{
39 imaxdiv_t r;
40
41 /* see div.c for comments */
42
43 r.quot = num / denom;
44 r.rem = num % denom;
45 if (num >= 0 && r.rem < 0) {
46 r.quot++;
47 r.rem -= denom;
48 }
49 return (r);
50}
diff --git a/src/lib/libc/stdlib/insque.3 b/src/lib/libc/stdlib/insque.3
deleted file mode 100644
index 136c08bad8..0000000000
--- a/src/lib/libc/stdlib/insque.3
+++ /dev/null
@@ -1,103 +0,0 @@
1.\" $OpenBSD: insque.3,v 1.12 2020/04/26 16:36:14 schwarze Exp $
2.\" Copyright (c) 1993 John Brezak
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. The name of the author may be used to endorse or promote products
14.\" derived from this software without specific prior written permission.
15.\"
16.\" THIS SOFTWARE IS PROVIDED BY THE AUTHOR `AS IS'' AND
17.\" ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
18.\" IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
19.\" ARE DISCLAIMED. IN NO EVENT SHALL THE AUTHOR BE LIABLE
20.\" FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
21.\" DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
22.\" OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
23.\" HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
24.\" LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
25.\" OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
26.\" SUCH DAMAGE.
27.\"
28.\"
29.Dd $Mdocdate: April 26 2020 $
30.Dt INSQUE 3
31.Os
32.Sh NAME
33.Nm insque ,
34.Nm remque
35.Nd legacy doubly linked lists
36.Sh SYNOPSIS
37.In search.h
38.Ft void
39.Fn insque "void *elem" "void *pred"
40.Ft void
41.Fn remque "void *elem"
42.Sh DESCRIPTION
43.Bf -symbolic
44These interfaces have been superseded by the
45.Xr queue 3
46macros and are provided for compatibility with legacy code.
47.Ef
48.Pp
49.Fn insque
50and
51.Fn remque
52manipulate a legacy variety of intrusive doubly linked lists.
53A list can be either circular or linear.
54Each element in the list must be of the following form:
55.Bd -literal -offset indent
56struct qelem {
57 struct qelem *q_forw;
58 struct qelem *q_back;
59 char q_data[];
60};
61.Ed
62.Pp
63The first two members of the struct must be pointers of the
64same type that point to the next and previous elements in
65the list, respectively.
66Any subsequent data in the struct is application-dependent.
67.Pp
68The
69.Fn insque
70function inserts
71.Fa elem
72into a list immediately after
73.Fa pred .
74.Pp
75The
76.Fn remque
77function removes
78.Fa elem
79from the list.
80.Pp
81These functions are not atomic.
82.Sh SEE ALSO
83.Xr queue 3
84.Sh STANDARDS
85The
86.Fn insque
87and
88.Fn remque
89functions conform to the X/Open System Interfaces option of the
90.St -p1003.1-2008
91specification.
92.Sh HISTORY
93The
94.Fn insque
95and
96.Fn remque
97functions are derived from the
98.Li insque
99and
100.Li remque
101instructions on the VAX.
102They first appeared in
103.Bx 4.2 .
diff --git a/src/lib/libc/stdlib/insque.c b/src/lib/libc/stdlib/insque.c
deleted file mode 100644
index 590ff837b8..0000000000
--- a/src/lib/libc/stdlib/insque.c
+++ /dev/null
@@ -1,54 +0,0 @@
1/* $OpenBSD: insque.c,v 1.3 2014/08/15 04:14:36 guenther Exp $ */
2
3/*
4 * Copyright (c) 1993 John Brezak
5 * 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. The name of the author may be used to endorse or promote products
16 * derived from this software without specific prior written permission.
17 *
18 * THIS SOFTWARE IS PROVIDED BY THE AUTHOR `AS IS'' AND ANY EXPRESS OR
19 * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED
20 * WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
21 * DISCLAIMED. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT,
22 * INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES
23 * (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR
24 * SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
25 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT,
26 * STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN
27 * ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
28 * POSSIBILITY OF SUCH DAMAGE.
29 */
30
31#include <stdlib.h>
32#include <search.h>
33
34struct qelem {
35 struct qelem *q_forw;
36 struct qelem *q_back;
37};
38
39void
40insque(void *entry, void *pred)
41{
42 struct qelem *e = entry;
43 struct qelem *p = pred;
44
45 if (p == NULL)
46 e->q_forw = e->q_back = NULL;
47 else {
48 e->q_forw = p->q_forw;
49 e->q_back = p;
50 if (p->q_forw != NULL)
51 p->q_forw->q_back = e;
52 p->q_forw = e;
53 }
54}
diff --git a/src/lib/libc/stdlib/jrand48.c b/src/lib/libc/stdlib/jrand48.c
deleted file mode 100644
index cb8c592750..0000000000
--- a/src/lib/libc/stdlib/jrand48.c
+++ /dev/null
@@ -1,22 +0,0 @@
1/* $OpenBSD: jrand48.c,v 1.3 2005/08/08 08:05:36 espie Exp $ */
2/*
3 * Copyright (c) 1993 Martin Birgmeier
4 * All rights reserved.
5 *
6 * You may redistribute unmodified or modified versions of this source
7 * code provided that the above copyright notice and this and the
8 * following conditions are retained.
9 *
10 * This software is provided ``as is'', and comes with no warranties
11 * of any kind. I shall in no event be liable for anything that happens
12 * to anyone/anything when using this software.
13 */
14
15#include "rand48.h"
16
17long
18jrand48(unsigned short xseed[3])
19{
20 __dorand48(xseed);
21 return ((long) xseed[2] << 16) + (long) xseed[1];
22}
diff --git a/src/lib/libc/stdlib/l64a.c b/src/lib/libc/stdlib/l64a.c
deleted file mode 100644
index 4f33df37b2..0000000000
--- a/src/lib/libc/stdlib/l64a.c
+++ /dev/null
@@ -1,42 +0,0 @@
1/* $OpenBSD: l64a.c,v 1.5 2005/08/08 08:05:36 espie Exp $ */
2/*
3 * Written by J.T. Conklin <jtc@netbsd.org>.
4 * Public domain.
5 */
6
7#include <errno.h>
8#include <stdlib.h>
9
10char *
11l64a(long value)
12{
13 static char buf[8];
14 char *s = buf;
15 int digit;
16 int i;
17
18 if (value < 0) {
19 errno = EINVAL;
20 return(NULL);
21 }
22
23 for (i = 0; value != 0 && i < 6; i++) {
24 digit = value & 0x3f;
25
26 if (digit < 2)
27 *s = digit + '.';
28 else if (digit < 12)
29 *s = digit + '0' - 2;
30 else if (digit < 38)
31 *s = digit + 'A' - 12;
32 else
33 *s = digit + 'a' - 38;
34
35 value >>= 6;
36 s++;
37 }
38
39 *s = '\0';
40
41 return(buf);
42}
diff --git a/src/lib/libc/stdlib/labs.3 b/src/lib/libc/stdlib/labs.3
deleted file mode 100644
index f3fd6fd528..0000000000
--- a/src/lib/libc/stdlib/labs.3
+++ /dev/null
@@ -1,88 +0,0 @@
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. 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.\" $OpenBSD: labs.3,v 1.17 2019/01/18 07:32:17 schwarze Exp $
33.\"
34.Dd $Mdocdate: January 18 2019 $
35.Dt LABS 3
36.Os
37.Sh NAME
38.Nm labs ,
39.Nm llabs ,
40.Nm qabs
41.Nd return the absolute value of a long integer
42.Sh SYNOPSIS
43.In limits.h
44.In stdlib.h
45.Ft long
46.Fn labs "long i"
47.Ft long long
48.Fn llabs "long long j"
49.Ft quad_t
50.Fn qabs "quad_t j"
51.Sh DESCRIPTION
52The
53.Fn labs
54function returns the absolute value of the long integer
55.Fa i .
56The
57.Fn llabs
58function returns the absolute value of the long long integer
59.Fa j .
60.Pp
61The
62.Fn qabs
63function is a deprecated equivalent of
64.Fn llabs .
65.Sh SEE ALSO
66.Xr abs 3 ,
67.Xr cabs 3 ,
68.Xr floor 3 ,
69.Xr imaxabs 3
70.Sh STANDARDS
71The
72.Fn labs
73and
74.Fn llabs
75functions conform to
76.St -isoC-99 .
77.Sh CAVEATS
78The results of applying
79.Fn labs
80to
81.Dv LONG_MIN
82and
83.Fn llabs
84to
85.Dv LLONG_MIN
86are undefined, and
87.Fn qabs
88is not portable in the first place.
diff --git a/src/lib/libc/stdlib/labs.c b/src/lib/libc/stdlib/labs.c
deleted file mode 100644
index ca60b9aba2..0000000000
--- a/src/lib/libc/stdlib/labs.c
+++ /dev/null
@@ -1,37 +0,0 @@
1/* $OpenBSD: labs.c,v 1.5 2005/08/08 08:05:36 espie Exp $ */
2/*-
3 * Copyright (c) 1990 The Regents of the University of California.
4 * 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. Neither the name of the University nor the names of its contributors
15 * may be used to endorse or promote products derived from this software
16 * without specific prior written permission.
17 *
18 * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
19 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
20 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
21 * ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
22 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
23 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
24 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
25 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
26 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
27 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
28 * SUCH DAMAGE.
29 */
30
31#include <stdlib.h>
32
33long
34labs(long j)
35{
36 return(j < 0 ? -j : j);
37}
diff --git a/src/lib/libc/stdlib/lcong48.c b/src/lib/libc/stdlib/lcong48.c
deleted file mode 100644
index f03083e3c8..0000000000
--- a/src/lib/libc/stdlib/lcong48.c
+++ /dev/null
@@ -1,36 +0,0 @@
1/* $OpenBSD: lcong48.c,v 1.6 2015/09/13 08:31:47 guenther Exp $ */
2/*
3 * Copyright (c) 1993 Martin Birgmeier
4 * All rights reserved.
5 *
6 * You may redistribute unmodified or modified versions of this source
7 * code provided that the above copyright notice and this and the
8 * following conditions are retained.
9 *
10 * This software is provided ``as is'', and comes with no warranties
11 * of any kind. I shall in no event be liable for anything that happens
12 * to anyone/anything when using this software.
13 */
14
15#include "rand48.h"
16
17void
18lcong48(unsigned short p[7])
19{
20 lcong48_deterministic(p);
21 __rand48_deterministic = 0;
22}
23
24void
25lcong48_deterministic(unsigned short p[7])
26{
27 __rand48_deterministic = 1;
28 __rand48_seed[0] = p[0];
29 __rand48_seed[1] = p[1];
30 __rand48_seed[2] = p[2];
31 __rand48_mult[0] = p[3];
32 __rand48_mult[1] = p[4];
33 __rand48_mult[2] = p[5];
34 __rand48_add = p[6];
35}
36DEF_WEAK(lcong48_deterministic);
diff --git a/src/lib/libc/stdlib/ldiv.3 b/src/lib/libc/stdlib/ldiv.3
deleted file mode 100644
index c7ddc85ac5..0000000000
--- a/src/lib/libc/stdlib/ldiv.3
+++ /dev/null
@@ -1,71 +0,0 @@
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. 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.\" $OpenBSD: ldiv.3,v 1.14 2022/09/11 06:38:11 jmc Exp $
33.\"
34.Dd $Mdocdate: September 11 2022 $
35.Dt LDIV 3
36.Os
37.Sh NAME
38.Nm ldiv
39.Nd return quotient and remainder from division
40.Sh SYNOPSIS
41.In stdlib.h
42.Ft ldiv_t
43.Fn ldiv "long num" "long denom"
44.Sh DESCRIPTION
45The
46.Fn ldiv
47function computes the value
48.Fa num Ns / Ns Fa denom
49and returns the quotient and remainder in a structure named
50.Vt ldiv_t
51that contains two
52.Vt long integer
53members named
54.Fa quot
55and
56.Fa rem .
57.Sh SEE ALSO
58.Xr div 3 ,
59.Xr imaxdiv 3 ,
60.Xr lldiv 3
61.Sh STANDARDS
62The
63.Fn ldiv
64function conforms to
65.St -ansiC .
66.Sh HISTORY
67An
68.Fn ldiv
69function with similar functionality, but a different calling convention,
70first appeared in
71.At v4 .
diff --git a/src/lib/libc/stdlib/ldiv.c b/src/lib/libc/stdlib/ldiv.c
deleted file mode 100644
index 775065f525..0000000000
--- a/src/lib/libc/stdlib/ldiv.c
+++ /dev/null
@@ -1,50 +0,0 @@
1/* $OpenBSD: ldiv.c,v 1.5 2005/08/08 08:05:36 espie Exp $ */
2/*
3 * Copyright (c) 1990 Regents of the University of California.
4 * All rights reserved.
5 *
6 * This code is derived from software contributed to Berkeley by
7 * Chris Torek.
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. 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#include <stdlib.h> /* ldiv_t */
35
36ldiv_t
37ldiv(long num, long denom)
38{
39 ldiv_t r;
40
41 /* see div.c for comments */
42
43 r.quot = num / denom;
44 r.rem = num % denom;
45 if (num >= 0 && r.rem < 0) {
46 r.quot++;
47 r.rem -= denom;
48 }
49 return (r);
50}
diff --git a/src/lib/libc/stdlib/llabs.c b/src/lib/libc/stdlib/llabs.c
deleted file mode 100644
index f4a260f4a8..0000000000
--- a/src/lib/libc/stdlib/llabs.c
+++ /dev/null
@@ -1,40 +0,0 @@
1/* $OpenBSD: llabs.c,v 1.4 2016/08/14 23:18:03 guenther Exp $ */
2
3/*-
4 * Copyright (c) 1990 The Regents of the University of California.
5 * 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. Neither the name of the University nor the names of its contributors
16 * may be used to endorse or promote products derived from this software
17 * without specific prior written permission.
18 *
19 * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
20 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
21 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
22 * ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
23 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
24 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
25 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
26 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
27 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
28 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
29 * SUCH DAMAGE.
30 */
31
32#include <stdlib.h>
33
34long long
35llabs(long long j)
36{
37 return (j < 0 ? -j : j);
38}
39
40__weak_alias(qabs, llabs);
diff --git a/src/lib/libc/stdlib/lldiv.3 b/src/lib/libc/stdlib/lldiv.3
deleted file mode 100644
index 539d7b5179..0000000000
--- a/src/lib/libc/stdlib/lldiv.3
+++ /dev/null
@@ -1,73 +0,0 @@
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. 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.\" $OpenBSD: lldiv.3,v 1.9 2022/09/11 06:38:11 jmc Exp $
33.\"
34.Dd $Mdocdate: September 11 2022 $
35.Dt LLDIV 3
36.Os
37.Sh NAME
38.Nm lldiv ,
39.Nm qdiv
40.Nd return quotient and remainder from division
41.Sh SYNOPSIS
42.In stdlib.h
43.Ft lldiv_t
44.Fn lldiv "long long num" "long long denom"
45.Ft qdiv_t
46.Fn qdiv "quad_t num" "quad_t denom"
47.Sh DESCRIPTION
48The
49.Fn lldiv
50function computes the value
51.Fa num Ns / Ns Fa denom
52and returns the quotient and remainder in a structure named
53.Vt lldiv_t
54that contains two
55.Vt long long integer
56members named
57.Fa quot
58and
59.Fa rem .
60.Pp
61The
62.Fn qdiv
63function is a deprecated equivalent of
64.Fn lldiv .
65.Sh SEE ALSO
66.Xr div 3 ,
67.Xr imaxdiv 3 ,
68.Xr ldiv 3
69.Sh STANDARDS
70The
71.Fn lldiv
72function conforms to
73.St -isoC-99 .
diff --git a/src/lib/libc/stdlib/lldiv.c b/src/lib/libc/stdlib/lldiv.c
deleted file mode 100644
index 59c37b878c..0000000000
--- a/src/lib/libc/stdlib/lldiv.c
+++ /dev/null
@@ -1,52 +0,0 @@
1/* $OpenBSD: lldiv.c,v 1.2 2016/08/14 23:18:03 guenther Exp $ */
2/*
3 * Copyright (c) 1990 Regents of the University of California.
4 * All rights reserved.
5 *
6 * This code is derived from software contributed to Berkeley by
7 * Chris Torek.
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. 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#include <stdlib.h> /* lldiv_t */
35
36lldiv_t
37lldiv(long long num, long long denom)
38{
39 lldiv_t r;
40
41 /* see div.c for comments */
42
43 r.quot = num / denom;
44 r.rem = num % denom;
45 if (num >= 0 && r.rem < 0) {
46 r.quot++;
47 r.rem -= denom;
48 }
49 return (r);
50}
51
52__weak_alias(qdiv, lldiv);
diff --git a/src/lib/libc/stdlib/lrand48.c b/src/lib/libc/stdlib/lrand48.c
deleted file mode 100644
index 142cca7f78..0000000000
--- a/src/lib/libc/stdlib/lrand48.c
+++ /dev/null
@@ -1,24 +0,0 @@
1/* $OpenBSD: lrand48.c,v 1.5 2015/08/27 04:33:31 guenther Exp $ */
2/*
3 * Copyright (c) 1993 Martin Birgmeier
4 * All rights reserved.
5 *
6 * You may redistribute unmodified or modified versions of this source
7 * code provided that the above copyright notice and this and the
8 * following conditions are retained.
9 *
10 * This software is provided ``as is'', and comes with no warranties
11 * of any kind. I shall in no event be liable for anything that happens
12 * to anyone/anything when using this software.
13 */
14
15#include "rand48.h"
16
17long
18lrand48(void)
19{
20 if (__rand48_deterministic == 0)
21 return arc4random() & 0x7fffffff;
22 __dorand48(__rand48_seed);
23 return ((long) __rand48_seed[2] << 15) + ((long) __rand48_seed[1] >> 1);
24}
diff --git a/src/lib/libc/stdlib/lsearch.3 b/src/lib/libc/stdlib/lsearch.3
deleted file mode 100644
index d82c55c859..0000000000
--- a/src/lib/libc/stdlib/lsearch.3
+++ /dev/null
@@ -1,106 +0,0 @@
1.\" $OpenBSD: lsearch.3,v 1.14 2015/11/30 17:03:05 jmc Exp $
2.\"
3.\" Copyright (c) 1989, 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. Neither the name of the University nor the names of its contributors
15.\" may be used to endorse or promote products derived from this software
16.\" without specific prior written permission.
17.\"
18.\" THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
19.\" ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
20.\" IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
21.\" ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
22.\" FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
23.\" DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
24.\" OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
25.\" HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
26.\" LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
27.\" OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
28.\" SUCH DAMAGE.
29.\"
30.\" @(#)lsearch.3 8.1 (Berkeley) 6/4/93
31.\"
32.Dd $Mdocdate: November 30 2015 $
33.Dt LSEARCH 3
34.Os
35.Sh NAME
36.Nm lsearch ,
37.Nm lfind
38.Nd linear searching routines
39.Sh SYNOPSIS
40.In search.h
41.Ft void *
42.Fn lsearch "const void *key" "void *base" "size_t *nelp" \
43 "size_t width" "int (*compar)(const void *, const void *)"
44.Ft void *
45.Fn lfind "const void *key" "const void *base" "size_t *nelp" \
46 "size_t width" "int (*compar)(const void *, const void *)"
47.Sh DESCRIPTION
48The functions
49.Fn lsearch
50and
51.Fn lfind
52provide basic linear searching functionality.
53.Pp
54.Fa base
55is the pointer to the beginning of an array.
56The argument
57.Fa nelp
58is the current number of elements in the array, where each element
59is
60.Fa width
61bytes long.
62The
63.Fa compar
64function
65is a comparison routine which is used to compare two elements.
66It takes two arguments which point to the
67.Fa key
68object and to an array member, in that order, and must return an integer
69less than, equivalent to, or greater than zero if the
70.Fa key
71object is considered, respectively, to be less than, equal to, or greater
72than the array member.
73.Pp
74The
75.Fn lsearch
76and
77.Fn lfind
78functions
79return a pointer into the array referenced by
80.Fa base
81where
82.Fa key
83is located.
84If
85.Fa key
86does not exist,
87.Fn lfind
88will return a null pointer and
89.Fn lsearch
90will add it to the array.
91When an element is added to the array by
92.Fn lsearch ,
93the location referenced by the argument
94.Fa nelp
95is incremented by one.
96.Sh SEE ALSO
97.Xr bsearch 3 ,
98.Xr dbopen 3
99.Sh STANDARDS
100The
101.Fn lsearch
102and
103.Fn lfind
104functions conform to the X/Open System Interfaces option of the
105.St -p1003.1-2008
106specification.
diff --git a/src/lib/libc/stdlib/lsearch.c b/src/lib/libc/stdlib/lsearch.c
deleted file mode 100644
index 95ebf49b81..0000000000
--- a/src/lib/libc/stdlib/lsearch.c
+++ /dev/null
@@ -1,70 +0,0 @@
1/* $OpenBSD: lsearch.c,v 1.7 2021/12/08 22:06:28 cheloha Exp $ */
2
3/*
4 * Copyright (c) 1989, 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 * Roger L. Snyder.
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. 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
35#include <sys/types.h>
36#include <string.h>
37#include <search.h>
38
39typedef int (*cmp_fn_t)(const void *, const void *);
40
41void *
42lsearch(const void *key, void *base, size_t *nelp, size_t width,
43 cmp_fn_t compar)
44{
45 void *element = lfind(key, base, nelp, width, compar);
46
47 /*
48 * Use memmove(3) to ensure the key is copied cleanly into the
49 * array, even if the key overlaps with the end of the array.
50 */
51 if (element == NULL) {
52 element = memmove((char *)base + *nelp * width, key, width);
53 *nelp += 1;
54 }
55 return element;
56}
57
58void *
59lfind(const void *key, const void *base, size_t *nelp, size_t width,
60 cmp_fn_t compar)
61{
62 const char *element, *end;
63
64 end = (const char *)base + *nelp * width;
65 for (element = base; element < end; element += width)
66 if (!compar(key, element)) /* key found */
67 return((void *)element);
68 return NULL;
69}
70DEF_WEAK(lfind);
diff --git a/src/lib/libc/stdlib/malloc.3 b/src/lib/libc/stdlib/malloc.3
deleted file mode 100644
index bea5575bf8..0000000000
--- a/src/lib/libc/stdlib/malloc.3
+++ /dev/null
@@ -1,860 +0,0 @@
1.\"
2.\" Copyright (c) 1980, 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.\" the American National Standards Committee X3, on Information
7.\" Processing Systems.
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. 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.\" $OpenBSD: malloc.3,v 1.142 2024/08/03 20:09:24 guenther Exp $
34.\"
35.Dd $Mdocdate: August 3 2024 $
36.Dt MALLOC 3
37.Os
38.Sh NAME
39.Nm malloc ,
40.Nm calloc ,
41.Nm realloc ,
42.Nm free ,
43.Nm reallocarray ,
44.Nm recallocarray ,
45.Nm freezero ,
46.Nm aligned_alloc ,
47.Nm malloc_conceal ,
48.Nm calloc_conceal
49.Nd memory allocation and deallocation
50.Sh SYNOPSIS
51.In stdlib.h
52.Ft void *
53.Fn malloc "size_t size"
54.Ft void *
55.Fn calloc "size_t nmemb" "size_t size"
56.Ft void *
57.Fn realloc "void *ptr" "size_t size"
58.Ft void
59.Fn free "void *ptr"
60.Ft void *
61.Fn reallocarray "void *ptr" "size_t nmemb" "size_t size"
62.Ft void *
63.Fn recallocarray "void *ptr" "size_t oldnmemb" "size_t nmemb" "size_t size"
64.Ft void
65.Fn freezero "void *ptr" "size_t size"
66.Ft void *
67.Fn aligned_alloc "size_t alignment" "size_t size"
68.Ft void *
69.Fn malloc_conceal "size_t size"
70.Ft void *
71.Fn calloc_conceal "size_t nmemb" "size_t size"
72.Vt char *malloc_options ;
73.Sh DESCRIPTION
74The standard functions
75.Fn malloc ,
76.Fn calloc ,
77and
78.Fn realloc
79allocate
80.Em objects ,
81regions of memory to store values.
82The
83.Fn malloc
84function allocates uninitialized space for an object of
85the specified
86.Fa size .
87.Fn malloc
88maintains multiple lists of free objects according to size, allocating
89from the appropriate list or requesting memory from the kernel.
90The allocated space is suitably aligned (after possible pointer coercion) for
91storage of any type of object.
92.Pp
93The
94.Fn calloc
95function allocates space for an array of
96.Fa nmemb
97objects, each of the specified
98.Fa size .
99The space is initialized to zero.
100.Pp
101The
102.Fn realloc
103function changes the size of the object pointed to by
104.Fa ptr
105to
106.Fa size
107bytes and returns a pointer to the (possibly moved) object.
108If
109.Fa ptr
110is not
111.Dv NULL ,
112it must be a pointer returned by an earlier call to an allocation or
113reallocation function that was not freed in between.
114The contents of the object are unchanged up to the lesser
115of the new and old sizes.
116If the new size is larger, the value of the newly allocated portion
117of the object is indeterminate and uninitialized.
118If the space cannot be allocated, the object
119pointed to by
120.Fa ptr
121is unchanged.
122If
123.Fa ptr
124is
125.Dv NULL ,
126.Fn realloc
127behaves like
128.Fn malloc
129and allocates a new object.
130.Pp
131The
132.Fn free
133function causes the space pointed to by
134.Fa ptr
135to be either placed on a list of free blocks to make it available for future
136allocation or, when appropriate, to be returned to the kernel using
137.Xr munmap 2 .
138If
139.Fa ptr
140is
141.Dv NULL ,
142no action occurs.
143If
144.Fa ptr
145was previously freed by
146.Fn free
147or a reallocation function,
148the behavior is undefined and the double free is a security concern.
149.Pp
150Designed for safe allocation of arrays,
151the
152.Fn reallocarray
153function is similar to
154.Fn realloc
155except it operates on
156.Fa nmemb
157members of size
158.Fa size
159and checks for integer overflow in the calculation
160.Fa nmemb
161*
162.Fa size .
163.Pp
164Used for the allocation of memory holding sensitive data,
165the
166.Fn recallocarray
167and
168.Fn freezero
169functions guarantee that memory becoming unallocated is explicitly
170.Em discarded ,
171meaning pages of memory are disposed via
172.Xr munmap 2
173and cached free objects are cleared with
174.Xr explicit_bzero 3 .
175.Pp
176The
177.Fn recallocarray
178function is similar to
179.Fn reallocarray
180except it ensures newly allocated memory is cleared similar to
181.Fn calloc .
182If
183.Fa ptr
184is
185.Dv NULL ,
186.Fa oldnmemb
187is ignored and the call is equivalent to
188.Fn calloc .
189If
190.Fa ptr
191is not
192.Dv NULL ,
193.Fa oldnmemb
194must be a value such that
195.Fa oldnmemb
196*
197.Fa size
198is the size of the earlier allocation that returned
199.Fa ptr ,
200otherwise the behavior is undefined.
201.Pp
202The
203.Fn freezero
204function is similar to the
205.Fn free
206function except it ensures memory is explicitly discarded.
207If
208.Fa ptr
209is
210.Dv NULL ,
211no action occurs.
212If
213.Fa ptr
214is not
215.Dv NULL ,
216the
217.Fa size
218argument must be equal to or smaller than the size of the earlier allocation
219that returned
220.Fa ptr .
221.Fn freezero
222guarantees the memory range starting at
223.Fa ptr
224with length
225.Fa size
226is discarded while deallocating the whole object originally allocated.
227.Pp
228The
229.Fn aligned_alloc
230function allocates
231.Fa size
232bytes of memory such that the allocation's base address is a multiple of
233.Fa alignment .
234The requested
235.Fa alignment
236must be a power of 2.
237If
238.Fa size
239is not a multiple of
240.Fa alignment ,
241behavior is undefined.
242.Pp
243The
244.Fn malloc_conceal
245and
246.Fn calloc_conceal
247functions behave the same as
248.Fn malloc
249and
250.Fn calloc
251respectively,
252with the exception that the allocation returned is marked with the
253.Dv MAP_CONCEAL
254.Xr mmap 2
255flag and calling
256.Fn free
257on the allocation will discard the contents explicitly.
258A reallocation of a concealed allocation will leave these properties intact.
259.Sh MALLOC OPTIONS
260Upon the first call to the
261.Fn malloc
262family of functions, an initialization sequence inspects the
263value of the
264.Va vm.malloc_conf
265.Xr sysctl 2 ,
266next checks the environment for a variable called
267.Ev MALLOC_OPTIONS ,
268and finally looks at the global variable
269.Va malloc_options
270in the program.
271Each is scanned for the flags documented below.
272Unless otherwise noted uppercase means on, lowercase means off.
273During initialization, flags occurring later modify the behaviour
274that was requested by flags processed earlier.
275.Bl -tag -width indent
276.It Cm C
277.Dq Canaries .
278Add canaries at the end of allocations in order to detect
279heap overflows.
280The canary's content is checked when
281.Nm free
282is called.
283If it has been corrupted, the process is aborted.
284.It Cm D
285.Dq Dump .
286.Fn malloc
287will dump a leak report using
288.Xr utrace 2
289at exit.
290To record the dump:
291.Pp
292.Dl $ MALLOC_OPTIONS=D ktrace -tu program ...
293.Pp
294To view the leak report:
295.Pp
296.Dl $ kdump -u malloc ...
297.Pp
298By default, the immediate caller of a
299.Nm
300function will be recorded.
301Use malloc option
302.Cm 2 ,
303.Cm 3
304or
305.Cm 4
306to record deeper call stacks.
307These malloc options imply
308.Cm D .
309.It Cm F
310.Dq Freecheck .
311Enable more extensive double free and write after free detection.
312All chunks in the delayed free list will be checked for double frees and
313write after frees.
314Unused pages on the freelist are read and write protected to
315cause a segmentation fault upon access.
316.It Cm G
317.Dq Guard .
318Enable guard pages.
319Each page size or larger allocation is followed by a guard page that will
320cause a segmentation fault upon any access.
321.It Cm J
322.Dq More junking .
323Increase the junk level by one if it is smaller than 2.
324.It Cm j
325.Dq Less junking .
326Decrease the junk level by one if it is larger than 0.
327Junking writes some junk bytes into the area allocated.
328Junk is bytes of 0xdb when allocating;
329small allocations are initially junked with 0xdf as are freed allocations.
330By default the junk level is 1: after free,
331small chunks are completely junked;
332for pages the first part is junked.
333After a delay,
334the filling pattern is validated and the process is aborted if the pattern
335was modified.
336For junk level 2, junking is done on allocation as well and without size
337restrictions.
338If the junk level is zero, no junking is performed.
339.It Cm R
340.Dq realloc .
341Always reallocate when
342.Fn realloc
343is called, even if the initial allocation was big enough.
344.\".Pp
345.\".It Cm U
346.\".Dq utrace .
347.\"Generate entries for
348.\".Xr ktrace 1
349.\"for all operations.
350.\"Consult the source for this one.
351.It Cm S
352.\" Malloc option S is vaguely documented on purpose.
353Enable all options suitable for security auditing.
354.It Cm U
355.Dq Free unmap .
356Enable use after free protection for larger allocations.
357Unused pages on the freelist are read and write protected to
358cause a segmentation fault upon access.
359.It Cm V
360.Dq Verbose .
361Use with
362.Cm D
363to get a verbose dump of malloc's internal state.
364.It Cm X
365.Dq xmalloc .
366Rather than return failure,
367.Xr abort 3
368the program with a diagnostic message on stderr.
369It is the intention that this option be set at compile time by
370including in the source:
371.Bd -literal -offset indent
372extern char *malloc_options;
373malloc_options = "X";
374.Ed
375.Pp
376Note that this will cause code that is supposed to handle
377out-of-memory conditions gracefully to abort instead.
378.It Cm <
379.Dq Halve the cache size .
380Decrease the size of the free page cache by a factor of two.
381.It Cm >
382.Dq Double the cache size .
383Increase the size of the free page cache by a factor of two.
384.El
385.Pp
386If a program changes behavior if any of these options (except
387.Cm X )
388are used,
389it is buggy.
390.Pp
391The default size of the cache is 64 single page allocations.
392It also caches a number of larger regions.
393Multi-threaded programs use multiple pools.
394.Sh RETURN VALUES
395Upon successful completion, the allocation functions
396return a pointer to the allocated space; otherwise,
397.Dv NULL
398is returned and
399.Va errno
400is set to
401.Er ENOMEM .
402The function
403.Fn aligned_alloc
404returns
405.Dv NULL
406and sets
407.Va errno
408to
409.Er EINVAL
410if
411.Fa alignment
412is not a power of 2.
413.Pp
414If
415.Fa nmemb
416or
417.Fa size
418is equal to 0, a unique pointer to an access protected,
419zero sized object is returned.
420Access via this pointer will generate a
421.Dv SIGSEGV
422exception.
423.Pp
424If multiplying
425.Fa nmemb
426and
427.Fa size
428results in integer overflow,
429.Fn calloc ,
430.Fn reallocarray
431and
432.Fn recallocarray
433return
434.Dv NULL
435and set
436.Va errno
437to
438.Er ENOMEM .
439.Pp
440If
441.Fa ptr
442is not
443.Dv NULL
444and multiplying
445.Fa oldnmemb
446and
447.Fa size
448results in integer overflow,
449.Fn recallocarray
450returns
451.Dv NULL
452and sets
453.Va errno
454to
455.Er EINVAL .
456.Sh IDIOMS
457Consider
458.Fn calloc
459or the extensions
460.Fn reallocarray
461and
462.Fn recallocarray
463when there is multiplication in the
464.Fa size
465argument of
466.Fn malloc
467or
468.Fn realloc .
469For example, avoid this common idiom as it may lead to integer overflow:
470.Bd -literal -offset indent
471if ((p = malloc(num * size)) == NULL)
472 err(1, NULL);
473.Ed
474.Pp
475A drop-in replacement is the
476.Ox
477extension
478.Fn reallocarray :
479.Bd -literal -offset indent
480if ((p = reallocarray(NULL, num, size)) == NULL)
481 err(1, NULL);
482.Ed
483.Pp
484Alternatively,
485.Fn calloc
486may be used at the cost of initialization overhead.
487.Pp
488When using
489.Fn realloc ,
490be careful to avoid the following idiom:
491.Bd -literal -offset indent
492size += 50;
493if ((p = realloc(p, size)) == NULL)
494 return (NULL);
495.Ed
496.Pp
497Do not adjust the variable describing how much memory has been allocated
498until the allocation has been successful.
499This can cause aberrant program behavior if the incorrect size value is used.
500In most cases, the above sample will also result in a leak of memory.
501As stated earlier, a return value of
502.Dv NULL
503indicates that the old object still remains allocated.
504Better code looks like this:
505.Bd -literal -offset indent
506newsize = size + 50;
507if ((newp = realloc(p, newsize)) == NULL) {
508 free(p);
509 p = NULL;
510 size = 0;
511 return (NULL);
512}
513p = newp;
514size = newsize;
515.Ed
516.Pp
517As with
518.Fn malloc ,
519it is important to ensure the new size value will not overflow;
520i.e. avoid allocations like the following:
521.Bd -literal -offset indent
522if ((newp = realloc(p, num * size)) == NULL) {
523 ...
524.Ed
525.Pp
526Instead, use
527.Fn reallocarray :
528.Bd -literal -offset indent
529if ((newp = reallocarray(p, num, size)) == NULL) {
530 ...
531.Ed
532.Pp
533Calling
534.Fn realloc
535with a
536.Dv NULL
537.Fa ptr
538is equivalent to calling
539.Fn malloc .
540Instead of this idiom:
541.Bd -literal -offset indent
542if (p == NULL)
543 newp = malloc(newsize);
544else
545 newp = realloc(p, newsize);
546.Ed
547.Pp
548Use the following:
549.Bd -literal -offset indent
550newp = realloc(p, newsize);
551.Ed
552.Pp
553The
554.Fn recallocarray
555function should be used for resizing objects containing sensitive data like
556keys.
557To avoid leaking information,
558it guarantees memory is cleared before placing it on the internal free list.
559Deallocation of such an object should be done by calling
560.Fn freezero .
561.Sh ENVIRONMENT
562.Bl -tag -width "MALLOC_OPTIONS"
563.It Ev MALLOC_OPTIONS
564String of option flags.
565.El
566.Sh EXAMPLES
567If
568.Fn malloc
569must be used with multiplication, be sure to test for overflow:
570.Bd -literal -offset indent
571size_t num, size;
572\&...
573
574/* Check for size_t overflow */
575if (size && num > SIZE_MAX / size)
576 errc(1, EOVERFLOW, "overflow");
577
578if ((p = malloc(num * size)) == NULL)
579 err(1, NULL);
580.Ed
581.Pp
582The above test is not sufficient in all cases.
583For example, multiplying ints requires a different set of checks:
584.Bd -literal -offset indent
585int num, size;
586\&...
587
588/* Avoid invalid requests */
589if (size < 0 || num < 0)
590 errc(1, EOVERFLOW, "overflow");
591
592/* Check for signed int overflow */
593if (size && num > INT_MAX / size)
594 errc(1, EOVERFLOW, "overflow");
595
596if ((p = malloc(num * size)) == NULL)
597 err(1, NULL);
598.Ed
599.Pp
600Assuming the implementation checks for integer overflow as
601.Ox
602does, it is much easier to use
603.Fn calloc ,
604.Fn reallocarray ,
605or
606.Fn recallocarray .
607.Pp
608The above examples could be simplified to:
609.Bd -literal -offset indent
610if ((p = reallocarray(NULL, num, size)) == NULL)
611 err(1, NULL);
612.Ed
613.Pp
614or at the cost of initialization:
615.Bd -literal -offset indent
616if ((p = calloc(num, size)) == NULL)
617 err(1, NULL);
618.Ed
619.Pp
620Set a systemwide reduction of the cache to a quarter of the
621default size and use guard pages:
622.Pp
623.Dl # sysctl vm.malloc_conf='G<<'
624.Sh DIAGNOSTICS
625If any of the functions detect an error condition,
626a message will be printed to file descriptor
6272 (not using stdio).
628Errors will result in the process being aborted.
629.Pp
630Here is a brief description of the error messages and what they mean:
631.Bl -tag -width Ds
632.It Dq out of memory
633If the
634.Cm X
635option is specified, it is an error for the allocation functions
636to return
637.Dv NULL .
638.It Dq bogus pointer (double free?)
639An attempt to
640.Fn free
641or
642reallocate an unallocated pointer was made.
643.It Dq double free
644There was an attempt to free an allocation that had already been freed.
645.It Dq write to free mem Va address Ns [ Va start Ns .. Ns Va end Ns ]@ Ns Va size
646An allocation has been modified after it was freed,
647or a chunk that was never allocated was written to.
648The
649.Va range
650at which corruption was detected is printed between [ and ].
651.Pp
652Enabling option
653.Cm D
654allows malloc to print information about where the allocation
655was done.
656.It Dq modified chunk-pointer
657The pointer passed to
658.Fn free
659or a reallocation function has been modified.
660.It Dq canary corrupted Va address Ns [ Va offset Ns ]@ Ns Va length Ns / Ns Va size
661A byte after the requested
662.Va length
663has been overwritten,
664indicating a heap overflow.
665The
666.Va offset
667at which corruption was detected is printed between [ and ],
668the requested
669.Va length
670of the allocation is printed before the / and the
671.Va size
672of the allocation after the /.
673.It Dq recorded size Va oldsize No inconsistent with Va size
674.Fn recallocarray
675or
676.Fn freezero
677has detected that the given old size does not match the recorded size in its
678meta data.
679Enabling option
680.Cm C
681allows
682.Fn recallocarray
683to catch more of these cases.
684.It Dq recursive call
685An attempt was made to call recursively into these functions, i.e., from a
686signal handler.
687This behavior is not supported.
688In particular, signal handlers should
689.Em not
690use any of the
691.Fn malloc
692functions nor utilize any other functions which may call
693.Fn malloc
694(e.g.,
695.Xr stdio 3
696routines).
697.It Dq unknown char in Ev MALLOC_OPTIONS
698We found something we didn't understand.
699.It any other error
700.Fn malloc
701detected an internal error;
702consult sources and/or wizards.
703.El
704.Sh SEE ALSO
705.Xr brk 2 ,
706.Xr mmap 2 ,
707.Xr munmap 2 ,
708.Xr sysctl 2 ,
709.Xr alloca 3 ,
710.Xr getpagesize 3 ,
711.Xr posix_memalign 3
712.Sh STANDARDS
713The
714.Fn malloc ,
715.Fn calloc ,
716.Fn realloc ,
717and
718.Fn free
719functions conform to
720.St -ansiC .
721The
722.Fn aligned_alloc
723function conforms to
724.St -isoC-2011 .
725The
726.Fn reallocarray
727function conforms to
728.St -p1003.1-2024 .
729.Pp
730If
731.Fa nmemb
732or
733.Fa size
734are 0, the return value is implementation defined;
735other conforming implementations may return
736.Dv NULL
737in this case.
738.Pp
739The
740.Ev MALLOC_OPTIONS
741environment variable, the
742.Va vm.malloc_conf
743sysctl and the
744.Sx DIAGNOSTICS
745output are extensions to the standard.
746.Sh HISTORY
747A
748.Fn free
749internal kernel function and a predecessor to
750.Fn malloc ,
751.Fn alloc ,
752first appeared in
753.At v1 .
754C library functions
755.Fn alloc
756and
757.Fn free
758appeared in
759.At v6 .
760The functions
761.Fn malloc ,
762.Fn calloc ,
763and
764.Fn realloc
765first appeared in
766.At v7 .
767.Pp
768A new implementation by Chris Kingsley was introduced in
769.Bx 4.2 ,
770followed by a complete rewrite by Poul-Henning Kamp which appeared in
771.Fx 2.2
772and was included in
773.Ox 2.0 .
774These implementations were all
775.Xr sbrk 2
776based.
777In
778.Ox 3.8 ,
779Thierry Deval rewrote
780.Nm
781to use the
782.Xr mmap 2
783system call,
784making the page addresses returned by
785.Nm
786random.
787A rewrite by Otto Moerbeek introducing a new central data structure and more
788randomization appeared in
789.Ox 4.4 .
790.Pp
791The
792.Fn reallocarray
793function appeared in
794.Ox 5.6 .
795The
796.Fn recallocarray
797function appeared in
798.Ox 6.1 .
799The
800.Fn freezero
801function appeared in
802.Ox 6.2 .
803The
804.Fn aligned_alloc
805function appeared in
806.Ox 6.5 .
807The
808.Fn malloc_conceal
809and
810.Fn calloc_conceal
811functions appeared in
812.Ox 6.6 .
813.Sh CAVEATS
814When using
815.Fn malloc ,
816be wary of signed integer and
817.Vt size_t
818overflow especially when there is multiplication in the
819.Fa size
820argument.
821.Pp
822Signed integer overflow will cause undefined behavior which compilers
823typically handle by wrapping back around to negative numbers.
824Depending on the input, this can result in allocating more or less
825memory than intended.
826.Pp
827An unsigned overflow has defined behavior which will wrap back around and
828return less memory than intended.
829.Pp
830A signed or unsigned integer overflow is a
831.Em security
832risk if less memory is returned than intended.
833Subsequent code may corrupt the heap by writing beyond the memory that was
834allocated.
835An attacker may be able to leverage this heap corruption to execute arbitrary
836code.
837.Pp
838Consider using
839.Fn calloc ,
840.Fn reallocarray
841or
842.Fn recallocarray
843instead of using multiplication in
844.Fn malloc
845and
846.Fn realloc
847to avoid these problems on
848.Ox .
849.Pp
850The mechanism to record caller functions when using malloc options
851.Cm 2 ,
852.Cm 3 ,
853or
854.Cm 4
855is not guaranteed to work for all platforms, compilers or compilation
856options,
857and might even crash your program.
858Use
859.Em only
860for debugging purposes.
diff --git a/src/lib/libc/stdlib/malloc.c b/src/lib/libc/stdlib/malloc.c
deleted file mode 100644
index cad8e5d6a1..0000000000
--- a/src/lib/libc/stdlib/malloc.c
+++ /dev/null
@@ -1,2781 +0,0 @@
1/* $OpenBSD: malloc.c,v 1.297 2024/09/20 02:00:46 jsg Exp $ */
2/*
3 * Copyright (c) 2008, 2010, 2011, 2016, 2023 Otto Moerbeek <otto@drijf.net>
4 * Copyright (c) 2012 Matthew Dempsky <matthew@openbsd.org>
5 * Copyright (c) 2008 Damien Miller <djm@openbsd.org>
6 * Copyright (c) 2000 Poul-Henning Kamp <phk@FreeBSD.org>
7 *
8 * Permission to use, copy, modify, and distribute this software for any
9 * purpose with or without fee is hereby granted, provided that the above
10 * copyright notice and this permission notice appear in all copies.
11 *
12 * THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES
13 * WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF
14 * MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR
15 * ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES
16 * WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN
17 * ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF
18 * OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE.
19 */
20
21/*
22 * If we meet some day, and you think this stuff is worth it, you
23 * can buy me a beer in return. Poul-Henning Kamp
24 */
25
26#ifndef MALLOC_SMALL
27#define MALLOC_STATS
28#endif
29
30#include <sys/types.h>
31#include <sys/queue.h>
32#include <sys/mman.h>
33#include <sys/sysctl.h>
34#include <uvm/uvmexp.h>
35#include <errno.h>
36#include <stdarg.h>
37#include <stdint.h>
38#include <stdio.h>
39#include <stdlib.h>
40#include <string.h>
41#include <unistd.h>
42
43#ifdef MALLOC_STATS
44#include <sys/tree.h>
45#include <sys/ktrace.h>
46#include <dlfcn.h>
47#endif
48
49#include "thread_private.h"
50#include <tib.h>
51
52#define MALLOC_PAGESHIFT _MAX_PAGE_SHIFT
53
54#define MALLOC_MINSHIFT 4
55#define MALLOC_MAXSHIFT (MALLOC_PAGESHIFT - 1)
56#define MALLOC_PAGESIZE (1UL << MALLOC_PAGESHIFT)
57#define MALLOC_MINSIZE (1UL << MALLOC_MINSHIFT)
58#define MALLOC_PAGEMASK (MALLOC_PAGESIZE - 1)
59#define MASK_POINTER(p) ((void *)(((uintptr_t)(p)) & ~MALLOC_PAGEMASK))
60
61#define MALLOC_MAXCHUNK (1 << MALLOC_MAXSHIFT)
62#define MALLOC_MAXCACHE 256
63#define MALLOC_DELAYED_CHUNK_MASK 15
64#ifdef MALLOC_STATS
65#define MALLOC_INITIAL_REGIONS 512
66#else
67#define MALLOC_INITIAL_REGIONS (MALLOC_PAGESIZE / sizeof(struct region_info))
68#endif
69#define MALLOC_DEFAULT_CACHE 64
70#define MALLOC_CHUNK_LISTS 4
71#define CHUNK_CHECK_LENGTH 32
72
73#define B2SIZE(b) ((b) * MALLOC_MINSIZE)
74#define B2ALLOC(b) ((b) == 0 ? MALLOC_MINSIZE : \
75 (b) * MALLOC_MINSIZE)
76#define BUCKETS (MALLOC_MAXCHUNK / MALLOC_MINSIZE)
77
78/*
79 * We move allocations between half a page and a whole page towards the end,
80 * subject to alignment constraints. This is the extra headroom we allow.
81 * Set to zero to be the most strict.
82 */
83#define MALLOC_LEEWAY 0
84#define MALLOC_MOVE_COND(sz) ((sz) - mopts.malloc_guard < \
85 MALLOC_PAGESIZE - MALLOC_LEEWAY)
86#define MALLOC_MOVE(p, sz) (((char *)(p)) + \
87 ((MALLOC_PAGESIZE - MALLOC_LEEWAY - \
88 ((sz) - mopts.malloc_guard)) & \
89 ~(MALLOC_MINSIZE - 1)))
90
91#define PAGEROUND(x) (((x) + (MALLOC_PAGEMASK)) & ~MALLOC_PAGEMASK)
92
93/*
94 * What to use for Junk. This is the byte value we use to fill with
95 * when the 'J' option is enabled. Use SOME_JUNK right after alloc,
96 * and SOME_FREEJUNK right before free.
97 */
98#define SOME_JUNK 0xdb /* deadbeef */
99#define SOME_FREEJUNK 0xdf /* dead, free */
100#define SOME_FREEJUNK_ULL 0xdfdfdfdfdfdfdfdfULL
101
102#define MMAP(sz,f) mmap(NULL, (sz), PROT_READ | PROT_WRITE, \
103 MAP_ANON | MAP_PRIVATE | (f), -1, 0)
104
105#define MMAPNONE(sz,f) mmap(NULL, (sz), PROT_NONE, \
106 MAP_ANON | MAP_PRIVATE | (f), -1, 0)
107
108#define MMAPA(a,sz,f) mmap((a), (sz), PROT_READ | PROT_WRITE, \
109 MAP_ANON | MAP_PRIVATE | (f), -1, 0)
110
111struct region_info {
112 void *p; /* page; low bits used to mark chunks */
113 uintptr_t size; /* size for pages, or chunk_info pointer */
114#ifdef MALLOC_STATS
115 void **f; /* where allocated from */
116#endif
117};
118
119LIST_HEAD(chunk_head, chunk_info);
120
121/*
122 * Two caches, one for "small" regions, one for "big".
123 * Small cache is an array per size, big cache is one array with different
124 * sized regions
125 */
126#define MAX_SMALLCACHEABLE_SIZE 32
127#define MAX_BIGCACHEABLE_SIZE 512
128/* If the total # of pages is larger than this, evict before inserting */
129#define BIGCACHE_FILL(sz) (MAX_BIGCACHEABLE_SIZE * (sz) / 4)
130
131struct smallcache {
132 void **pages;
133 ushort length;
134 ushort max;
135};
136
137struct bigcache {
138 void *page;
139 size_t psize;
140};
141
142#ifdef MALLOC_STATS
143#define NUM_FRAMES 4
144struct btnode {
145 RBT_ENTRY(btnode) entry;
146 void *caller[NUM_FRAMES];
147};
148RBT_HEAD(btshead, btnode);
149RBT_PROTOTYPE(btshead, btnode, entry, btcmp);
150#endif /* MALLOC_STATS */
151
152struct dir_info {
153 u_int32_t canary1;
154 int active; /* status of malloc */
155 struct region_info *r; /* region slots */
156 size_t regions_total; /* number of region slots */
157 size_t regions_free; /* number of free slots */
158 size_t rbytesused; /* random bytes used */
159 const char *func; /* current function */
160 int malloc_junk; /* junk fill? */
161 int mmap_flag; /* extra flag for mmap */
162 int mutex;
163 int malloc_mt; /* multi-threaded mode? */
164 /* lists of free chunk info structs */
165 struct chunk_head chunk_info_list[BUCKETS + 1];
166 /* lists of chunks with free slots */
167 struct chunk_head chunk_dir[BUCKETS + 1][MALLOC_CHUNK_LISTS];
168 /* delayed free chunk slots */
169 void *delayed_chunks[MALLOC_DELAYED_CHUNK_MASK + 1];
170 u_char rbytes[32]; /* random bytes */
171 /* free pages cache */
172 struct smallcache smallcache[MAX_SMALLCACHEABLE_SIZE];
173 size_t bigcache_used;
174 size_t bigcache_size;
175 struct bigcache *bigcache;
176 void *chunk_pages;
177 size_t chunk_pages_used;
178#ifdef MALLOC_STATS
179 void *caller;
180 size_t inserts;
181 size_t insert_collisions;
182 size_t deletes;
183 size_t delete_moves;
184 size_t cheap_realloc_tries;
185 size_t cheap_reallocs;
186 size_t malloc_used; /* bytes allocated */
187 size_t malloc_guarded; /* bytes used for guards */
188 struct btshead btraces; /* backtraces seen */
189 struct btnode *btnodes; /* store of backtrace nodes */
190 size_t btnodesused;
191#define STATS_ADD(x,y) ((x) += (y))
192#define STATS_SUB(x,y) ((x) -= (y))
193#define STATS_INC(x) ((x)++)
194#define STATS_ZERO(x) ((x) = 0)
195#define STATS_SETF(x,y) ((x)->f = (y))
196#define STATS_SETFN(x,k,y) ((x)->f[k] = (y))
197#define SET_CALLER(x,y) if (DO_STATS) ((x)->caller = (y))
198#else
199#define STATS_ADD(x,y) /* nothing */
200#define STATS_SUB(x,y) /* nothing */
201#define STATS_INC(x) /* nothing */
202#define STATS_ZERO(x) /* nothing */
203#define STATS_SETF(x,y) /* nothing */
204#define STATS_SETFN(x,k,y) /* nothing */
205#define SET_CALLER(x,y) /* nothing */
206#endif /* MALLOC_STATS */
207 u_int32_t canary2;
208};
209
210static void unmap(struct dir_info *d, void *p, size_t sz, size_t clear);
211
212/*
213 * This structure describes a page worth of chunks.
214 *
215 * How many bits per u_short in the bitmap
216 */
217#define MALLOC_BITS (NBBY * sizeof(u_short))
218struct chunk_info {
219 LIST_ENTRY(chunk_info) entries;
220 void *page; /* pointer to the page */
221 /* number of shorts should add up to 8, check alloc_chunk_info() */
222 u_short canary;
223 u_short bucket;
224 u_short free; /* how many free chunks */
225 u_short total; /* how many chunks */
226 u_short offset; /* requested size table offset */
227#define CHUNK_INFO_TAIL 3
228 u_short bits[CHUNK_INFO_TAIL]; /* which chunks are free */
229};
230
231#define CHUNK_FREE(i, n) ((i)->bits[(n) / MALLOC_BITS] & \
232 (1U << ((n) % MALLOC_BITS)))
233
234struct malloc_readonly {
235 /* Main bookkeeping information */
236 struct dir_info *malloc_pool[_MALLOC_MUTEXES];
237 u_int malloc_mutexes; /* how much in actual use? */
238 int malloc_freecheck; /* Extensive double free check */
239 int malloc_freeunmap; /* mprotect free pages PROT_NONE? */
240 int def_malloc_junk; /* junk fill? */
241 int malloc_realloc; /* always realloc? */
242 int malloc_xmalloc; /* xmalloc behaviour? */
243 u_int chunk_canaries; /* use canaries after chunks? */
244 int internal_funcs; /* use better recallocarray/freezero? */
245 u_int def_maxcache; /* free pages we cache */
246 u_int junk_loc; /* variation in location of junk */
247 size_t malloc_guard; /* use guard pages after allocations? */
248#ifdef MALLOC_STATS
249 int malloc_stats; /* save callers, dump leak report */
250 int malloc_verbose; /* dump verbose statistics at end */
251#define DO_STATS mopts.malloc_stats
252#else
253#define DO_STATS 0
254#endif
255 u_int32_t malloc_canary; /* Matched against ones in pool */
256};
257
258
259/* This object is mapped PROT_READ after initialisation to prevent tampering */
260static union {
261 struct malloc_readonly mopts;
262 u_char _pad[MALLOC_PAGESIZE];
263} malloc_readonly __attribute__((aligned(MALLOC_PAGESIZE)))
264 __attribute__((section(".openbsd.mutable")));
265#define mopts malloc_readonly.mopts
266
267char *malloc_options; /* compile-time options */
268
269static __dead void wrterror(struct dir_info *d, char *msg, ...)
270 __attribute__((__format__ (printf, 2, 3)));
271
272#ifdef MALLOC_STATS
273void malloc_dump(void);
274PROTO_NORMAL(malloc_dump);
275static void malloc_exit(void);
276static void print_chunk_details(struct dir_info *, void *, size_t, size_t);
277static void* store_caller(struct dir_info *, struct btnode *);
278
279/* below are the arches for which deeper caller info has been tested */
280#if defined(__aarch64__) || \
281 defined(__amd64__) || \
282 defined(__arm__) || \
283 defined(__i386__) || \
284 defined(__powerpc__)
285__attribute__((always_inline))
286static inline void*
287caller(struct dir_info *d)
288{
289 struct btnode p;
290 int level = DO_STATS;
291
292 if (level == 0)
293 return NULL;
294
295 memset(&p.caller, 0, sizeof(p.caller));
296 if (level >= 1)
297 p.caller[0] = __builtin_extract_return_addr(
298 __builtin_return_address(0));
299 if (p.caller[0] != NULL && level >= 2)
300 p.caller[1] = __builtin_extract_return_addr(
301 __builtin_return_address(1));
302 if (p.caller[1] != NULL && level >= 3)
303 p.caller[2] = __builtin_extract_return_addr(
304 __builtin_return_address(2));
305 if (p.caller[2] != NULL && level >= 4)
306 p.caller[3] = __builtin_extract_return_addr(
307 __builtin_return_address(3));
308 return store_caller(d, &p);
309}
310#else
311__attribute__((always_inline))
312static inline void* caller(struct dir_info *d)
313{
314 struct btnode p;
315
316 if (DO_STATS == 0)
317 return NULL;
318 memset(&p.caller, 0, sizeof(p.caller));
319 p.caller[0] = __builtin_extract_return_addr(__builtin_return_address(0));
320 return store_caller(d, &p);
321}
322#endif
323#endif /* MALLOC_STATS */
324
325/* low bits of r->p determine size: 0 means >= page size and r->size holding
326 * real size, otherwise low bits is the bucket + 1
327 */
328#define REALSIZE(sz, r) \
329 (sz) = (uintptr_t)(r)->p & MALLOC_PAGEMASK, \
330 (sz) = ((sz) == 0 ? (r)->size : B2SIZE((sz) - 1))
331
332static inline size_t
333hash(void *p)
334{
335 size_t sum;
336 uintptr_t u;
337
338 u = (uintptr_t)p >> MALLOC_PAGESHIFT;
339 sum = u;
340 sum = (sum << 7) - sum + (u >> 16);
341#ifdef __LP64__
342 sum = (sum << 7) - sum + (u >> 32);
343 sum = (sum << 7) - sum + (u >> 48);
344#endif
345 return sum;
346}
347
348static inline struct dir_info *
349getpool(void)
350{
351 if (mopts.malloc_pool[1] == NULL || !mopts.malloc_pool[1]->malloc_mt)
352 return mopts.malloc_pool[1];
353 else /* first one reserved for special pool */
354 return mopts.malloc_pool[1 + TIB_GET()->tib_tid %
355 (mopts.malloc_mutexes - 1)];
356}
357
358static __dead void
359wrterror(struct dir_info *d, char *msg, ...)
360{
361 int saved_errno = errno;
362 va_list ap;
363
364 dprintf(STDERR_FILENO, "%s(%d) in %s(): ", __progname,
365 getpid(), (d != NULL && d->func) ? d->func : "unknown");
366 va_start(ap, msg);
367 vdprintf(STDERR_FILENO, msg, ap);
368 va_end(ap);
369 dprintf(STDERR_FILENO, "\n");
370
371#ifdef MALLOC_STATS
372 if (DO_STATS && mopts.malloc_verbose)
373 malloc_dump();
374#endif
375
376 errno = saved_errno;
377
378 abort();
379}
380
381static void
382rbytes_init(struct dir_info *d)
383{
384 arc4random_buf(d->rbytes, sizeof(d->rbytes));
385 /* add 1 to account for using d->rbytes[0] */
386 d->rbytesused = 1 + d->rbytes[0] % (sizeof(d->rbytes) / 2);
387}
388
389static inline u_char
390getrbyte(struct dir_info *d)
391{
392 u_char x;
393
394 if (d->rbytesused >= sizeof(d->rbytes))
395 rbytes_init(d);
396 x = d->rbytes[d->rbytesused++];
397 return x;
398}
399
400static void
401omalloc_parseopt(char opt)
402{
403 switch (opt) {
404 case '+':
405 mopts.malloc_mutexes <<= 1;
406 if (mopts.malloc_mutexes > _MALLOC_MUTEXES)
407 mopts.malloc_mutexes = _MALLOC_MUTEXES;
408 break;
409 case '-':
410 mopts.malloc_mutexes >>= 1;
411 if (mopts.malloc_mutexes < 2)
412 mopts.malloc_mutexes = 2;
413 break;
414 case '>':
415 mopts.def_maxcache <<= 1;
416 if (mopts.def_maxcache > MALLOC_MAXCACHE)
417 mopts.def_maxcache = MALLOC_MAXCACHE;
418 break;
419 case '<':
420 mopts.def_maxcache >>= 1;
421 break;
422 case 'c':
423 mopts.chunk_canaries = 0;
424 break;
425 case 'C':
426 mopts.chunk_canaries = 1;
427 break;
428#ifdef MALLOC_STATS
429 case 'd':
430 mopts.malloc_stats = 0;
431 break;
432 case 'D':
433 case '1':
434 mopts.malloc_stats = 1;
435 break;
436 case '2':
437 mopts.malloc_stats = 2;
438 break;
439 case '3':
440 mopts.malloc_stats = 3;
441 break;
442 case '4':
443 mopts.malloc_stats = 4;
444 break;
445#endif /* MALLOC_STATS */
446 case 'f':
447 mopts.malloc_freecheck = 0;
448 mopts.malloc_freeunmap = 0;
449 break;
450 case 'F':
451 mopts.malloc_freecheck = 1;
452 mopts.malloc_freeunmap = 1;
453 break;
454 case 'g':
455 mopts.malloc_guard = 0;
456 break;
457 case 'G':
458 mopts.malloc_guard = MALLOC_PAGESIZE;
459 break;
460 case 'j':
461 if (mopts.def_malloc_junk > 0)
462 mopts.def_malloc_junk--;
463 break;
464 case 'J':
465 if (mopts.def_malloc_junk < 2)
466 mopts.def_malloc_junk++;
467 break;
468 case 'r':
469 mopts.malloc_realloc = 0;
470 break;
471 case 'R':
472 mopts.malloc_realloc = 1;
473 break;
474 case 'u':
475 mopts.malloc_freeunmap = 0;
476 break;
477 case 'U':
478 mopts.malloc_freeunmap = 1;
479 break;
480#ifdef MALLOC_STATS
481 case 'v':
482 mopts.malloc_verbose = 0;
483 break;
484 case 'V':
485 mopts.malloc_verbose = 1;
486 break;
487#endif /* MALLOC_STATS */
488 case 'x':
489 mopts.malloc_xmalloc = 0;
490 break;
491 case 'X':
492 mopts.malloc_xmalloc = 1;
493 break;
494 default:
495 dprintf(STDERR_FILENO, "malloc() warning: "
496 "unknown char in MALLOC_OPTIONS\n");
497 break;
498 }
499}
500
501static void
502omalloc_init(void)
503{
504 char *p, *q, b[16];
505 int i, j;
506 const int mib[2] = { CTL_VM, VM_MALLOC_CONF };
507 size_t sb;
508
509 /*
510 * Default options
511 */
512 mopts.malloc_mutexes = 8;
513 mopts.def_malloc_junk = 1;
514 mopts.def_maxcache = MALLOC_DEFAULT_CACHE;
515
516 for (i = 0; i < 3; i++) {
517 switch (i) {
518 case 0:
519 sb = sizeof(b);
520 j = sysctl(mib, 2, b, &sb, NULL, 0);
521 if (j != 0)
522 continue;
523 p = b;
524 break;
525 case 1:
526 if (issetugid() == 0)
527 p = getenv("MALLOC_OPTIONS");
528 else
529 continue;
530 break;
531 case 2:
532 p = malloc_options;
533 break;
534 default:
535 p = NULL;
536 }
537
538 for (; p != NULL && *p != '\0'; p++) {
539 switch (*p) {
540 case 'S':
541 for (q = "CFGJ"; *q != '\0'; q++)
542 omalloc_parseopt(*q);
543 mopts.def_maxcache = 0;
544 break;
545 case 's':
546 for (q = "cfgj"; *q != '\0'; q++)
547 omalloc_parseopt(*q);
548 mopts.def_maxcache = MALLOC_DEFAULT_CACHE;
549 break;
550 default:
551 omalloc_parseopt(*p);
552 break;
553 }
554 }
555 }
556
557#ifdef MALLOC_STATS
558 if (DO_STATS && (atexit(malloc_exit) == -1)) {
559 dprintf(STDERR_FILENO, "malloc() warning: atexit(3) failed."
560 " Will not be able to dump stats on exit\n");
561 }
562#endif
563
564 while ((mopts.malloc_canary = arc4random()) == 0)
565 ;
566 mopts.junk_loc = arc4random();
567 if (mopts.chunk_canaries)
568 do {
569 mopts.chunk_canaries = arc4random();
570 } while ((u_char)mopts.chunk_canaries == 0 ||
571 (u_char)mopts.chunk_canaries == SOME_FREEJUNK);
572}
573
574static void
575omalloc_poolinit(struct dir_info *d, int mmap_flag)
576{
577 u_int i, j;
578
579 d->r = NULL;
580 d->rbytesused = sizeof(d->rbytes);
581 d->regions_free = d->regions_total = 0;
582 for (i = 0; i <= BUCKETS; i++) {
583 LIST_INIT(&d->chunk_info_list[i]);
584 for (j = 0; j < MALLOC_CHUNK_LISTS; j++)
585 LIST_INIT(&d->chunk_dir[i][j]);
586 }
587 d->mmap_flag = mmap_flag;
588 d->malloc_junk = mopts.def_malloc_junk;
589#ifdef MALLOC_STATS
590 RBT_INIT(btshead, &d->btraces);
591#endif
592 d->canary1 = mopts.malloc_canary ^ (u_int32_t)(uintptr_t)d;
593 d->canary2 = ~d->canary1;
594}
595
596static int
597omalloc_grow(struct dir_info *d)
598{
599 size_t newtotal;
600 size_t newsize;
601 size_t mask;
602 size_t i, oldpsz;
603 struct region_info *p;
604
605 if (d->regions_total > SIZE_MAX / sizeof(struct region_info) / 2)
606 return 1;
607
608 newtotal = d->regions_total == 0 ? MALLOC_INITIAL_REGIONS :
609 d->regions_total * 2;
610 newsize = PAGEROUND(newtotal * sizeof(struct region_info));
611 mask = newtotal - 1;
612
613 /* Don't use cache here, we don't want user uaf touch this */
614 p = MMAP(newsize, d->mmap_flag);
615 if (p == MAP_FAILED)
616 return 1;
617
618 STATS_ADD(d->malloc_used, newsize);
619 STATS_ZERO(d->inserts);
620 STATS_ZERO(d->insert_collisions);
621 for (i = 0; i < d->regions_total; i++) {
622 void *q = d->r[i].p;
623 if (q != NULL) {
624 size_t index = hash(q) & mask;
625 STATS_INC(d->inserts);
626 while (p[index].p != NULL) {
627 index = (index - 1) & mask;
628 STATS_INC(d->insert_collisions);
629 }
630 p[index] = d->r[i];
631 }
632 }
633
634 if (d->regions_total > 0) {
635 oldpsz = PAGEROUND(d->regions_total *
636 sizeof(struct region_info));
637 /* clear to avoid meta info ending up in the cache */
638 unmap(d, d->r, oldpsz, oldpsz);
639 }
640 d->regions_free += newtotal - d->regions_total;
641 d->regions_total = newtotal;
642 d->r = p;
643 return 0;
644}
645
646/*
647 * The hashtable uses the assumption that p is never NULL. This holds since
648 * non-MAP_FIXED mappings with hint 0 start at BRKSIZ.
649 */
650static int
651insert(struct dir_info *d, void *p, size_t sz, void *f)
652{
653 size_t index;
654 size_t mask;
655 void *q;
656
657 if (d->regions_free * 4 < d->regions_total || d->regions_total == 0) {
658 if (omalloc_grow(d))
659 return 1;
660 }
661 mask = d->regions_total - 1;
662 index = hash(p) & mask;
663 q = d->r[index].p;
664 STATS_INC(d->inserts);
665 while (q != NULL) {
666 index = (index - 1) & mask;
667 q = d->r[index].p;
668 STATS_INC(d->insert_collisions);
669 }
670 d->r[index].p = p;
671 d->r[index].size = sz;
672 STATS_SETF(&d->r[index], f);
673 d->regions_free--;
674 return 0;
675}
676
677static struct region_info *
678find(struct dir_info *d, void *p)
679{
680 size_t index;
681 size_t mask = d->regions_total - 1;
682 void *q, *r;
683
684 if (mopts.malloc_canary != (d->canary1 ^ (u_int32_t)(uintptr_t)d) ||
685 d->canary1 != ~d->canary2)
686 wrterror(d, "internal struct corrupt");
687 if (d->r == NULL)
688 return NULL;
689 p = MASK_POINTER(p);
690 index = hash(p) & mask;
691 r = d->r[index].p;
692 q = MASK_POINTER(r);
693 while (q != p && r != NULL) {
694 index = (index - 1) & mask;
695 r = d->r[index].p;
696 q = MASK_POINTER(r);
697 }
698 return (q == p && r != NULL) ? &d->r[index] : NULL;
699}
700
701static void
702delete(struct dir_info *d, struct region_info *ri)
703{
704 /* algorithm R, Knuth Vol III section 6.4 */
705 size_t mask = d->regions_total - 1;
706 size_t i, j, r;
707
708 if (d->regions_total & (d->regions_total - 1))
709 wrterror(d, "regions_total not 2^x");
710 d->regions_free++;
711 STATS_INC(d->deletes);
712
713 i = ri - d->r;
714 for (;;) {
715 d->r[i].p = NULL;
716 d->r[i].size = 0;
717 j = i;
718 for (;;) {
719 i = (i - 1) & mask;
720 if (d->r[i].p == NULL)
721 return;
722 r = hash(d->r[i].p) & mask;
723 if ((i <= r && r < j) || (r < j && j < i) ||
724 (j < i && i <= r))
725 continue;
726 d->r[j] = d->r[i];
727 STATS_INC(d->delete_moves);
728 break;
729 }
730
731 }
732}
733
734static inline void
735junk_free(int junk, void *p, size_t sz)
736{
737 size_t i, step = 1;
738 uint64_t *lp = p;
739
740 if (junk == 0 || sz == 0)
741 return;
742 sz /= sizeof(uint64_t);
743 if (junk == 1) {
744 if (sz > MALLOC_PAGESIZE / sizeof(uint64_t))
745 sz = MALLOC_PAGESIZE / sizeof(uint64_t);
746 step = sz / 4;
747 if (step == 0)
748 step = 1;
749 }
750 /* Do not always put the free junk bytes in the same spot.
751 There is modulo bias here, but we ignore that. */
752 for (i = mopts.junk_loc % step; i < sz; i += step)
753 lp[i] = SOME_FREEJUNK_ULL;
754}
755
756static inline void
757validate_junk(struct dir_info *pool, void *p, size_t argsz)
758{
759 size_t i, sz, step = 1;
760 uint64_t *lp = p;
761
762 if (pool->malloc_junk == 0 || argsz == 0)
763 return;
764 sz = argsz / sizeof(uint64_t);
765 if (pool->malloc_junk == 1) {
766 if (sz > MALLOC_PAGESIZE / sizeof(uint64_t))
767 sz = MALLOC_PAGESIZE / sizeof(uint64_t);
768 step = sz / 4;
769 if (step == 0)
770 step = 1;
771 }
772 /* see junk_free */
773 for (i = mopts.junk_loc % step; i < sz; i += step) {
774 if (lp[i] != SOME_FREEJUNK_ULL) {
775#ifdef MALLOC_STATS
776 if (DO_STATS && argsz <= MALLOC_MAXCHUNK)
777 print_chunk_details(pool, lp, argsz, i);
778 else
779#endif
780 wrterror(pool,
781 "write to free mem %p[%zu..%zu]@%zu",
782 lp, i * sizeof(uint64_t),
783 (i + 1) * sizeof(uint64_t) - 1, argsz);
784 }
785 }
786}
787
788
789/*
790 * Cache maintenance.
791 * Opposed to the regular region data structure, the sizes in the
792 * cache are in MALLOC_PAGESIZE units.
793 */
794static void
795unmap(struct dir_info *d, void *p, size_t sz, size_t clear)
796{
797 size_t psz = sz >> MALLOC_PAGESHIFT;
798 void *r;
799 u_short i;
800 struct smallcache *cache;
801
802 if (sz != PAGEROUND(sz) || psz == 0)
803 wrterror(d, "munmap round");
804
805 if (d->bigcache_size > 0 && psz > MAX_SMALLCACHEABLE_SIZE &&
806 psz <= MAX_BIGCACHEABLE_SIZE) {
807 u_short base = getrbyte(d);
808 u_short j;
809
810 /* don't look through all slots */
811 for (j = 0; j < d->bigcache_size / 4; j++) {
812 i = (base + j) & (d->bigcache_size - 1);
813 if (d->bigcache_used <
814 BIGCACHE_FILL(d->bigcache_size)) {
815 if (d->bigcache[i].psize == 0)
816 break;
817 } else {
818 if (d->bigcache[i].psize != 0)
819 break;
820 }
821 }
822 /* if we didn't find a preferred slot, use random one */
823 if (d->bigcache[i].psize != 0) {
824 size_t tmp;
825
826 r = d->bigcache[i].page;
827 d->bigcache_used -= d->bigcache[i].psize;
828 tmp = d->bigcache[i].psize << MALLOC_PAGESHIFT;
829 if (!mopts.malloc_freeunmap)
830 validate_junk(d, r, tmp);
831 if (munmap(r, tmp))
832 wrterror(d, "munmap %p", r);
833 STATS_SUB(d->malloc_used, tmp);
834 }
835
836 if (clear > 0)
837 explicit_bzero(p, clear);
838 if (mopts.malloc_freeunmap) {
839 if (mprotect(p, sz, PROT_NONE))
840 wrterror(d, "mprotect %p", r);
841 } else
842 junk_free(d->malloc_junk, p, sz);
843 d->bigcache[i].page = p;
844 d->bigcache[i].psize = psz;
845 d->bigcache_used += psz;
846 return;
847 }
848 if (psz > MAX_SMALLCACHEABLE_SIZE || d->smallcache[psz - 1].max == 0) {
849 if (munmap(p, sz))
850 wrterror(d, "munmap %p", p);
851 STATS_SUB(d->malloc_used, sz);
852 return;
853 }
854 cache = &d->smallcache[psz - 1];
855 if (cache->length == cache->max) {
856 int fresh;
857 /* use a random slot */
858 i = getrbyte(d) & (cache->max - 1);
859 r = cache->pages[i];
860 fresh = (uintptr_t)r & 1;
861 *(uintptr_t*)&r &= ~1UL;
862 if (!fresh && !mopts.malloc_freeunmap)
863 validate_junk(d, r, sz);
864 if (munmap(r, sz))
865 wrterror(d, "munmap %p", r);
866 STATS_SUB(d->malloc_used, sz);
867 cache->length--;
868 } else
869 i = cache->length;
870
871 /* fill slot */
872 if (clear > 0)
873 explicit_bzero(p, clear);
874 if (mopts.malloc_freeunmap)
875 mprotect(p, sz, PROT_NONE);
876 else
877 junk_free(d->malloc_junk, p, sz);
878 cache->pages[i] = p;
879 cache->length++;
880}
881
882static void *
883map(struct dir_info *d, size_t sz, int zero_fill)
884{
885 size_t psz = sz >> MALLOC_PAGESHIFT;
886 u_short i;
887 void *p;
888 struct smallcache *cache;
889
890 if (mopts.malloc_canary != (d->canary1 ^ (u_int32_t)(uintptr_t)d) ||
891 d->canary1 != ~d->canary2)
892 wrterror(d, "internal struct corrupt");
893 if (sz != PAGEROUND(sz) || psz == 0)
894 wrterror(d, "map round");
895
896
897 if (d->bigcache_size > 0 && psz > MAX_SMALLCACHEABLE_SIZE &&
898 psz <= MAX_BIGCACHEABLE_SIZE) {
899 size_t base = getrbyte(d);
900 size_t cached = d->bigcache_used;
901 ushort j;
902
903 for (j = 0; j < d->bigcache_size && cached >= psz; j++) {
904 i = (j + base) & (d->bigcache_size - 1);
905 if (d->bigcache[i].psize == psz) {
906 p = d->bigcache[i].page;
907 d->bigcache_used -= psz;
908 d->bigcache[i].page = NULL;
909 d->bigcache[i].psize = 0;
910
911 if (!mopts.malloc_freeunmap)
912 validate_junk(d, p, sz);
913 if (mopts.malloc_freeunmap)
914 mprotect(p, sz, PROT_READ | PROT_WRITE);
915 if (zero_fill)
916 memset(p, 0, sz);
917 else if (mopts.malloc_freeunmap)
918 junk_free(d->malloc_junk, p, sz);
919 return p;
920 }
921 cached -= d->bigcache[i].psize;
922 }
923 }
924 if (psz <= MAX_SMALLCACHEABLE_SIZE && d->smallcache[psz - 1].max > 0) {
925 cache = &d->smallcache[psz - 1];
926 if (cache->length > 0) {
927 int fresh;
928 if (cache->length == 1)
929 p = cache->pages[--cache->length];
930 else {
931 i = getrbyte(d) % cache->length;
932 p = cache->pages[i];
933 cache->pages[i] = cache->pages[--cache->length];
934 }
935 /* check if page was not junked, i.e. "fresh
936 we use the lsb of the pointer for that */
937 fresh = (uintptr_t)p & 1UL;
938 *(uintptr_t*)&p &= ~1UL;
939 if (!fresh && !mopts.malloc_freeunmap)
940 validate_junk(d, p, sz);
941 if (mopts.malloc_freeunmap)
942 mprotect(p, sz, PROT_READ | PROT_WRITE);
943 if (zero_fill)
944 memset(p, 0, sz);
945 else if (mopts.malloc_freeunmap)
946 junk_free(d->malloc_junk, p, sz);
947 return p;
948 }
949 if (psz <= 1) {
950 p = MMAP(cache->max * sz, d->mmap_flag);
951 if (p != MAP_FAILED) {
952 STATS_ADD(d->malloc_used, cache->max * sz);
953 cache->length = cache->max - 1;
954 for (i = 0; i < cache->max - 1; i++) {
955 void *q = (char*)p + i * sz;
956 cache->pages[i] = q;
957 /* mark pointer in slot as not junked */
958 *(uintptr_t*)&cache->pages[i] |= 1UL;
959 }
960 if (mopts.malloc_freeunmap)
961 mprotect(p, (cache->max - 1) * sz,
962 PROT_NONE);
963 p = (char*)p + (cache->max - 1) * sz;
964 /* zero fill not needed, freshly mmapped */
965 return p;
966 }
967 }
968
969 }
970 p = MMAP(sz, d->mmap_flag);
971 if (p != MAP_FAILED)
972 STATS_ADD(d->malloc_used, sz);
973 /* zero fill not needed */
974 return p;
975}
976
977static void
978init_chunk_info(struct dir_info *d, struct chunk_info *p, u_int bucket)
979{
980 u_int i;
981
982 p->bucket = bucket;
983 p->total = p->free = MALLOC_PAGESIZE / B2ALLOC(bucket);
984 p->offset = howmany(p->total, MALLOC_BITS);
985 p->canary = (u_short)d->canary1;
986
987 /* set all valid bits in the bitmap */
988 i = p->total - 1;
989 memset(p->bits, 0xff, sizeof(p->bits[0]) * (i / MALLOC_BITS));
990 p->bits[i / MALLOC_BITS] = (2U << (i % MALLOC_BITS)) - 1;
991}
992
993static struct chunk_info *
994alloc_chunk_info(struct dir_info *d, u_int bucket)
995{
996 struct chunk_info *p;
997
998 if (LIST_EMPTY(&d->chunk_info_list[bucket])) {
999 const size_t chunk_pages = 64;
1000 size_t size, count, i;
1001 char *q;
1002
1003 count = MALLOC_PAGESIZE / B2ALLOC(bucket);
1004
1005 size = howmany(count, MALLOC_BITS);
1006 /* see declaration of struct chunk_info */
1007 if (size <= CHUNK_INFO_TAIL)
1008 size = 0;
1009 else
1010 size -= CHUNK_INFO_TAIL;
1011 size = sizeof(struct chunk_info) + size * sizeof(u_short);
1012 if (mopts.chunk_canaries && bucket > 0)
1013 size += count * sizeof(u_short);
1014 size = _ALIGN(size);
1015 count = MALLOC_PAGESIZE / size;
1016
1017 /* Don't use cache here, we don't want user uaf touch this */
1018 if (d->chunk_pages_used == chunk_pages ||
1019 d->chunk_pages == NULL) {
1020 q = MMAP(MALLOC_PAGESIZE * chunk_pages, d->mmap_flag);
1021 if (q == MAP_FAILED)
1022 return NULL;
1023 d->chunk_pages = q;
1024 d->chunk_pages_used = 0;
1025 STATS_ADD(d->malloc_used, MALLOC_PAGESIZE *
1026 chunk_pages);
1027 }
1028 q = (char *)d->chunk_pages + d->chunk_pages_used *
1029 MALLOC_PAGESIZE;
1030 d->chunk_pages_used++;
1031
1032 for (i = 0; i < count; i++, q += size) {
1033 p = (struct chunk_info *)q;
1034 LIST_INSERT_HEAD(&d->chunk_info_list[bucket], p,
1035 entries);
1036 }
1037 }
1038 p = LIST_FIRST(&d->chunk_info_list[bucket]);
1039 LIST_REMOVE(p, entries);
1040 if (p->total == 0)
1041 init_chunk_info(d, p, bucket);
1042 return p;
1043}
1044
1045/*
1046 * Allocate a page of chunks
1047 */
1048static struct chunk_info *
1049omalloc_make_chunks(struct dir_info *d, u_int bucket, u_int listnum)
1050{
1051 struct chunk_info *bp;
1052 void *pp;
1053 void *ff = NULL;
1054
1055 /* Allocate a new bucket */
1056 pp = map(d, MALLOC_PAGESIZE, 0);
1057 if (pp == MAP_FAILED)
1058 return NULL;
1059 if (DO_STATS) {
1060 ff = map(d, MALLOC_PAGESIZE, 0);
1061 if (ff == MAP_FAILED)
1062 goto err;
1063 memset(ff, 0, sizeof(void *) * MALLOC_PAGESIZE /
1064 B2ALLOC(bucket));
1065 }
1066
1067 /* memory protect the page allocated in the malloc(0) case */
1068 if (bucket == 0 && mprotect(pp, MALLOC_PAGESIZE, PROT_NONE) == -1)
1069 goto err;
1070
1071 bp = alloc_chunk_info(d, bucket);
1072 if (bp == NULL)
1073 goto err;
1074 bp->page = pp;
1075
1076 if (insert(d, (void *)((uintptr_t)pp | (bucket + 1)), (uintptr_t)bp,
1077 ff))
1078 goto err;
1079 LIST_INSERT_HEAD(&d->chunk_dir[bucket][listnum], bp, entries);
1080
1081 if (bucket > 0 && d->malloc_junk != 0)
1082 memset(pp, SOME_FREEJUNK, MALLOC_PAGESIZE);
1083
1084 return bp;
1085
1086err:
1087 unmap(d, pp, MALLOC_PAGESIZE, 0);
1088 if (ff != NULL && ff != MAP_FAILED)
1089 unmap(d, ff, MALLOC_PAGESIZE, 0);
1090 return NULL;
1091}
1092
1093#if defined(__GNUC__) && __GNUC__ < 4
1094static inline unsigned int
1095lb(u_int x)
1096{
1097#if defined(__m88k__)
1098 __asm__ __volatile__ ("ff1 %0, %0" : "=r" (x) : "0" (x));
1099 return x;
1100#else
1101 /* portable version */
1102 unsigned int count = 0;
1103 while ((x & (1U << (sizeof(int) * CHAR_BIT - 1))) == 0) {
1104 count++;
1105 x <<= 1;
1106 }
1107 return (sizeof(int) * CHAR_BIT - 1) - count;
1108#endif
1109}
1110#else
1111/* using built-in function version */
1112static inline unsigned int
1113lb(u_int x)
1114{
1115 /* I need an extension just for integer-length (: */
1116 return (sizeof(int) * CHAR_BIT - 1) - __builtin_clz(x);
1117}
1118#endif
1119
1120/* https://pvk.ca/Blog/2015/06/27/linear-log-bucketing-fast-versatile-simple/
1121 via Tony Finch */
1122static inline unsigned int
1123bin_of(unsigned int size)
1124{
1125 const unsigned int linear = 6;
1126 const unsigned int subbin = 2;
1127
1128 unsigned int mask, rounded, rounded_size;
1129 unsigned int n_bits, shift;
1130
1131 n_bits = lb(size | (1U << linear));
1132 shift = n_bits - subbin;
1133 mask = (1ULL << shift) - 1;
1134 rounded = size + mask; /* XXX: overflow. */
1135
1136 rounded_size = rounded & ~mask;
1137 return rounded_size;
1138}
1139
1140static inline u_short
1141find_bucket(u_short size)
1142{
1143 /* malloc(0) is special */
1144 if (size == 0)
1145 return 0;
1146 if (size < MALLOC_MINSIZE)
1147 size = MALLOC_MINSIZE;
1148 if (mopts.def_maxcache != 0)
1149 size = bin_of(size);
1150 return howmany(size, MALLOC_MINSIZE);
1151}
1152
1153static void
1154fill_canary(char *ptr, size_t sz, size_t allocated)
1155{
1156 size_t check_sz = allocated - sz;
1157
1158 if (check_sz > CHUNK_CHECK_LENGTH)
1159 check_sz = CHUNK_CHECK_LENGTH;
1160 memset(ptr + sz, mopts.chunk_canaries, check_sz);
1161}
1162
1163/*
1164 * Allocate a chunk
1165 */
1166static void *
1167malloc_bytes(struct dir_info *d, size_t size)
1168{
1169 u_int i, j, k, r, bucket, listnum;
1170 u_short *lp;
1171 struct chunk_info *bp;
1172 void *p;
1173
1174 if (mopts.malloc_canary != (d->canary1 ^ (u_int32_t)(uintptr_t)d) ||
1175 d->canary1 != ~d->canary2)
1176 wrterror(d, "internal struct corrupt");
1177
1178 bucket = find_bucket(size);
1179
1180 r = getrbyte(d);
1181 listnum = r % MALLOC_CHUNK_LISTS;
1182
1183 /* If it's empty, make a page more of that size chunks */
1184 if ((bp = LIST_FIRST(&d->chunk_dir[bucket][listnum])) == NULL) {
1185 bp = omalloc_make_chunks(d, bucket, listnum);
1186 if (bp == NULL)
1187 return NULL;
1188 }
1189
1190 if (bp->canary != (u_short)d->canary1 || bucket != bp->bucket)
1191 wrterror(d, "chunk info corrupted");
1192
1193 r /= MALLOC_CHUNK_LISTS;
1194 /* do we need more random bits? */
1195 if (bp->total > 256 / MALLOC_CHUNK_LISTS)
1196 r = r << 8 | getrbyte(d);
1197 /* bias, as bp->total is not a power of 2 */
1198 i = r % bp->total;
1199
1200 j = i % MALLOC_BITS;
1201 i /= MALLOC_BITS;
1202 lp = &bp->bits[i];
1203 /* potentially start somewhere in a short */
1204 if (j > 0 && *lp >> j)
1205 k = ffs(*lp >> j) + j;
1206 else {
1207 /* no bit halfway, go to next full short */
1208 for (;;) {
1209 if (*lp) {
1210 k = ffs(*lp);
1211 break;
1212 }
1213 if (++i >= bp->offset)
1214 i = 0;
1215 lp = &bp->bits[i];
1216 }
1217 }
1218 *lp ^= 1 << --k;
1219
1220 /* If there are no more free, remove from free-list */
1221 if (--bp->free == 0)
1222 LIST_REMOVE(bp, entries);
1223
1224 /* Adjust to the real offset of that chunk */
1225 k += i * MALLOC_BITS;
1226
1227 if (mopts.chunk_canaries && size > 0)
1228 bp->bits[bp->offset + k] = size;
1229
1230 if (DO_STATS) {
1231 struct region_info *r = find(d, bp->page);
1232 STATS_SETFN(r, k, d->caller);
1233 }
1234
1235 p = (char *)bp->page + k * B2ALLOC(bucket);
1236 if (bucket > 0) {
1237 validate_junk(d, p, B2SIZE(bucket));
1238 if (mopts.chunk_canaries)
1239 fill_canary(p, size, B2SIZE(bucket));
1240 }
1241 return p;
1242}
1243
1244static void
1245validate_canary(struct dir_info *d, u_char *ptr, size_t sz, size_t allocated)
1246{
1247 size_t check_sz = allocated - sz;
1248 u_char *p, *q;
1249
1250 if (check_sz > CHUNK_CHECK_LENGTH)
1251 check_sz = CHUNK_CHECK_LENGTH;
1252 p = ptr + sz;
1253 q = p + check_sz;
1254
1255 while (p < q) {
1256 if (*p != (u_char)mopts.chunk_canaries && *p != SOME_JUNK) {
1257 wrterror(d, "canary corrupted %p[%tu]@%zu/%zu%s",
1258 ptr, p - ptr, sz, allocated,
1259 *p == SOME_FREEJUNK ? " (double free?)" : "");
1260 }
1261 p++;
1262 }
1263}
1264
1265static inline uint32_t
1266find_chunknum(struct dir_info *d, struct chunk_info *info, void *ptr, int check)
1267{
1268 uint32_t chunknum;
1269
1270 if (info->canary != (u_short)d->canary1)
1271 wrterror(d, "chunk info corrupted");
1272
1273 /* Find the chunk number on the page */
1274 chunknum = ((uintptr_t)ptr & MALLOC_PAGEMASK) / B2ALLOC(info->bucket);
1275
1276 if ((uintptr_t)ptr & (MALLOC_MINSIZE - 1))
1277 wrterror(d, "modified chunk-pointer %p", ptr);
1278 if (CHUNK_FREE(info, chunknum))
1279 wrterror(d, "double free %p", ptr);
1280 if (check && info->bucket > 0) {
1281 validate_canary(d, ptr, info->bits[info->offset + chunknum],
1282 B2SIZE(info->bucket));
1283 }
1284 return chunknum;
1285}
1286
1287/*
1288 * Free a chunk, and possibly the page it's on, if the page becomes empty.
1289 */
1290static void
1291free_bytes(struct dir_info *d, struct region_info *r, void *ptr)
1292{
1293 struct chunk_head *mp;
1294 struct chunk_info *info;
1295 uint32_t chunknum;
1296 uint32_t listnum;
1297
1298 info = (struct chunk_info *)r->size;
1299 chunknum = find_chunknum(d, info, ptr, 0);
1300
1301 info->bits[chunknum / MALLOC_BITS] |= 1U << (chunknum % MALLOC_BITS);
1302 info->free++;
1303
1304 if (info->free == 1) {
1305 /* Page became non-full */
1306 listnum = getrbyte(d) % MALLOC_CHUNK_LISTS;
1307 mp = &d->chunk_dir[info->bucket][listnum];
1308 LIST_INSERT_HEAD(mp, info, entries);
1309 return;
1310 }
1311
1312 if (info->free != info->total)
1313 return;
1314
1315 LIST_REMOVE(info, entries);
1316
1317 if (info->bucket == 0 && !mopts.malloc_freeunmap)
1318 mprotect(info->page, MALLOC_PAGESIZE, PROT_READ | PROT_WRITE);
1319 unmap(d, info->page, MALLOC_PAGESIZE, 0);
1320#ifdef MALLOC_STATS
1321 if (r->f != NULL) {
1322 unmap(d, r->f, MALLOC_PAGESIZE, MALLOC_PAGESIZE);
1323 r->f = NULL;
1324 }
1325#endif
1326
1327 delete(d, r);
1328 mp = &d->chunk_info_list[info->bucket];
1329 LIST_INSERT_HEAD(mp, info, entries);
1330}
1331
1332static void *
1333omalloc(struct dir_info *pool, size_t sz, int zero_fill)
1334{
1335 void *p, *caller = NULL;
1336 size_t psz;
1337
1338 if (sz > MALLOC_MAXCHUNK) {
1339 if (sz >= SIZE_MAX - mopts.malloc_guard - MALLOC_PAGESIZE) {
1340 errno = ENOMEM;
1341 return NULL;
1342 }
1343 sz += mopts.malloc_guard;
1344 psz = PAGEROUND(sz);
1345 p = map(pool, psz, zero_fill);
1346 if (p == MAP_FAILED) {
1347 errno = ENOMEM;
1348 return NULL;
1349 }
1350#ifdef MALLOC_STATS
1351 if (DO_STATS)
1352 caller = pool->caller;
1353#endif
1354 if (insert(pool, p, sz, caller)) {
1355 unmap(pool, p, psz, 0);
1356 errno = ENOMEM;
1357 return NULL;
1358 }
1359 if (mopts.malloc_guard) {
1360 if (mprotect((char *)p + psz - mopts.malloc_guard,
1361 mopts.malloc_guard, PROT_NONE))
1362 wrterror(pool, "mprotect");
1363 STATS_ADD(pool->malloc_guarded, mopts.malloc_guard);
1364 }
1365
1366 if (MALLOC_MOVE_COND(sz)) {
1367 /* fill whole allocation */
1368 if (pool->malloc_junk == 2)
1369 memset(p, SOME_JUNK, psz - mopts.malloc_guard);
1370 /* shift towards the end */
1371 p = MALLOC_MOVE(p, sz);
1372 /* fill zeros if needed and overwritten above */
1373 if (zero_fill && pool->malloc_junk == 2)
1374 memset(p, 0, sz - mopts.malloc_guard);
1375 } else {
1376 if (pool->malloc_junk == 2) {
1377 if (zero_fill)
1378 memset((char *)p + sz -
1379 mopts.malloc_guard, SOME_JUNK,
1380 psz - sz);
1381 else
1382 memset(p, SOME_JUNK,
1383 psz - mopts.malloc_guard);
1384 } else if (mopts.chunk_canaries)
1385 fill_canary(p, sz - mopts.malloc_guard,
1386 psz - mopts.malloc_guard);
1387 }
1388
1389 } else {
1390 /* takes care of SOME_JUNK */
1391 p = malloc_bytes(pool, sz);
1392 if (zero_fill && p != NULL && sz > 0)
1393 memset(p, 0, sz);
1394 }
1395
1396 return p;
1397}
1398
1399/*
1400 * Common function for handling recursion. Only
1401 * print the error message once, to avoid making the problem
1402 * potentially worse.
1403 */
1404static void
1405malloc_recurse(struct dir_info *d)
1406{
1407 static int noprint;
1408
1409 if (noprint == 0) {
1410 noprint = 1;
1411 wrterror(d, "recursive call");
1412 }
1413 d->active--;
1414 _MALLOC_UNLOCK(d->mutex);
1415 errno = EDEADLK;
1416}
1417
1418void
1419_malloc_init(int from_rthreads)
1420{
1421 u_int i, j, nmutexes;
1422 struct dir_info *d;
1423
1424 _MALLOC_LOCK(1);
1425 if (!from_rthreads && mopts.malloc_pool[1]) {
1426 _MALLOC_UNLOCK(1);
1427 return;
1428 }
1429 if (!mopts.malloc_canary) {
1430 char *p;
1431 size_t sz, roundup_sz, d_avail;
1432
1433 omalloc_init();
1434 /*
1435 * Allocate dir_infos with a guard page on either side. Also
1436 * randomise offset inside the page at which the dir_infos
1437 * lay (subject to alignment by 1 << MALLOC_MINSHIFT)
1438 */
1439 sz = mopts.malloc_mutexes * sizeof(*d);
1440 roundup_sz = (sz + MALLOC_PAGEMASK) & ~MALLOC_PAGEMASK;
1441 if ((p = MMAPNONE(roundup_sz + 2 * MALLOC_PAGESIZE, 0)) ==
1442 MAP_FAILED)
1443 wrterror(NULL, "malloc_init mmap1 failed");
1444 if (mprotect(p + MALLOC_PAGESIZE, roundup_sz,
1445 PROT_READ | PROT_WRITE))
1446 wrterror(NULL, "malloc_init mprotect1 failed");
1447 if (mimmutable(p, roundup_sz + 2 * MALLOC_PAGESIZE))
1448 wrterror(NULL, "malloc_init mimmutable1 failed");
1449 d_avail = (roundup_sz - sz) >> MALLOC_MINSHIFT;
1450 d = (struct dir_info *)(p + MALLOC_PAGESIZE +
1451 (arc4random_uniform(d_avail) << MALLOC_MINSHIFT));
1452 STATS_ADD(d[1].malloc_used, roundup_sz + 2 * MALLOC_PAGESIZE);
1453 for (i = 0; i < mopts.malloc_mutexes; i++)
1454 mopts.malloc_pool[i] = &d[i];
1455 mopts.internal_funcs = 1;
1456 if (((uintptr_t)&malloc_readonly & MALLOC_PAGEMASK) == 0) {
1457 if (mprotect(&malloc_readonly, sizeof(malloc_readonly),
1458 PROT_READ))
1459 wrterror(NULL,
1460 "malloc_init mprotect r/o failed");
1461 if (mimmutable(&malloc_readonly,
1462 sizeof(malloc_readonly)))
1463 wrterror(NULL,
1464 "malloc_init mimmutable r/o failed");
1465 }
1466 }
1467
1468 nmutexes = from_rthreads ? mopts.malloc_mutexes : 2;
1469 for (i = 0; i < nmutexes; i++) {
1470 d = mopts.malloc_pool[i];
1471 d->malloc_mt = from_rthreads;
1472 if (d->canary1 == ~d->canary2)
1473 continue;
1474 if (i == 0) {
1475 omalloc_poolinit(d, MAP_CONCEAL);
1476 d->malloc_junk = 2;
1477 d->bigcache_size = 0;
1478 for (j = 0; j < MAX_SMALLCACHEABLE_SIZE; j++)
1479 d->smallcache[j].max = 0;
1480 } else {
1481 size_t sz = 0;
1482
1483 omalloc_poolinit(d, 0);
1484 d->malloc_junk = mopts.def_malloc_junk;
1485 d->bigcache_size = mopts.def_maxcache;
1486 for (j = 0; j < MAX_SMALLCACHEABLE_SIZE; j++) {
1487 d->smallcache[j].max =
1488 mopts.def_maxcache >> (j / 8);
1489 sz += d->smallcache[j].max * sizeof(void *);
1490 }
1491 sz += d->bigcache_size * sizeof(struct bigcache);
1492 if (sz > 0) {
1493 void *p = MMAP(sz, 0);
1494 if (p == MAP_FAILED)
1495 wrterror(NULL,
1496 "malloc_init mmap2 failed");
1497 if (mimmutable(p, sz))
1498 wrterror(NULL,
1499 "malloc_init mimmutable2 failed");
1500 for (j = 0; j < MAX_SMALLCACHEABLE_SIZE; j++) {
1501 d->smallcache[j].pages = p;
1502 p = (char *)p + d->smallcache[j].max *
1503 sizeof(void *);
1504 }
1505 d->bigcache = p;
1506 }
1507 }
1508 d->mutex = i;
1509 }
1510
1511 _MALLOC_UNLOCK(1);
1512}
1513DEF_STRONG(_malloc_init);
1514
1515#define PROLOGUE(p, fn) \
1516 d = (p); \
1517 if (d == NULL) { \
1518 _malloc_init(0); \
1519 d = (p); \
1520 } \
1521 _MALLOC_LOCK(d->mutex); \
1522 d->func = fn; \
1523 if (d->active++) { \
1524 malloc_recurse(d); \
1525 return NULL; \
1526 } \
1527
1528#define EPILOGUE() \
1529 d->active--; \
1530 _MALLOC_UNLOCK(d->mutex); \
1531 if (r == NULL && mopts.malloc_xmalloc) \
1532 wrterror(d, "out of memory"); \
1533 if (r != NULL) \
1534 errno = saved_errno; \
1535
1536void *
1537malloc(size_t size)
1538{
1539 void *r;
1540 struct dir_info *d;
1541 int saved_errno = errno;
1542
1543 PROLOGUE(getpool(), "malloc")
1544 SET_CALLER(d, caller(d));
1545 r = omalloc(d, size, 0);
1546 EPILOGUE()
1547 return r;
1548}
1549DEF_STRONG(malloc);
1550
1551void *
1552malloc_conceal(size_t size)
1553{
1554 void *r;
1555 struct dir_info *d;
1556 int saved_errno = errno;
1557
1558 PROLOGUE(mopts.malloc_pool[0], "malloc_conceal")
1559 SET_CALLER(d, caller(d));
1560 r = omalloc(d, size, 0);
1561 EPILOGUE()
1562 return r;
1563}
1564DEF_WEAK(malloc_conceal);
1565
1566static struct region_info *
1567findpool(void *p, struct dir_info *argpool, struct dir_info **foundpool,
1568 const char ** saved_function)
1569{
1570 struct dir_info *pool = argpool;
1571 struct region_info *r = find(pool, p);
1572
1573 if (r == NULL) {
1574 u_int i, nmutexes;
1575
1576 nmutexes = mopts.malloc_pool[1]->malloc_mt ?
1577 mopts.malloc_mutexes : 2;
1578 for (i = 1; i < nmutexes; i++) {
1579 u_int j = (argpool->mutex + i) & (nmutexes - 1);
1580
1581 pool->active--;
1582 _MALLOC_UNLOCK(pool->mutex);
1583 pool = mopts.malloc_pool[j];
1584 _MALLOC_LOCK(pool->mutex);
1585 pool->active++;
1586 r = find(pool, p);
1587 if (r != NULL) {
1588 *saved_function = pool->func;
1589 pool->func = argpool->func;
1590 break;
1591 }
1592 }
1593 if (r == NULL)
1594 wrterror(argpool, "bogus pointer (double free?) %p", p);
1595 }
1596 *foundpool = pool;
1597 return r;
1598}
1599
1600static void
1601ofree(struct dir_info **argpool, void *p, int clear, int check, size_t argsz)
1602{
1603 struct region_info *r;
1604 struct dir_info *pool;
1605 const char *saved_function;
1606 size_t sz;
1607
1608 r = findpool(p, *argpool, &pool, &saved_function);
1609
1610 REALSIZE(sz, r);
1611 if (pool->mmap_flag) {
1612 clear = 1;
1613 if (!check) {
1614 argsz = sz;
1615 if (sz > MALLOC_MAXCHUNK)
1616 argsz -= mopts.malloc_guard;
1617 }
1618 }
1619 if (check) {
1620 if (sz <= MALLOC_MAXCHUNK) {
1621 if (mopts.chunk_canaries && sz > 0) {
1622 struct chunk_info *info =
1623 (struct chunk_info *)r->size;
1624 uint32_t chunknum =
1625 find_chunknum(pool, info, p, 0);
1626
1627 if (info->bits[info->offset + chunknum] < argsz)
1628 wrterror(pool, "recorded size %hu"
1629 " < %zu",
1630 info->bits[info->offset + chunknum],
1631 argsz);
1632 } else {
1633 if (sz < argsz)
1634 wrterror(pool, "chunk size %zu < %zu",
1635 sz, argsz);
1636 }
1637 } else if (sz - mopts.malloc_guard < argsz) {
1638 wrterror(pool, "recorded size %zu < %zu",
1639 sz - mopts.malloc_guard, argsz);
1640 }
1641 }
1642 if (sz > MALLOC_MAXCHUNK) {
1643 if (!MALLOC_MOVE_COND(sz)) {
1644 if (r->p != p)
1645 wrterror(pool, "bogus pointer %p", p);
1646 if (mopts.chunk_canaries)
1647 validate_canary(pool, p,
1648 sz - mopts.malloc_guard,
1649 PAGEROUND(sz - mopts.malloc_guard));
1650 } else {
1651 /* shifted towards the end */
1652 if (p != MALLOC_MOVE(r->p, sz))
1653 wrterror(pool, "bogus moved pointer %p", p);
1654 p = r->p;
1655 }
1656 if (mopts.malloc_guard) {
1657 if (sz < mopts.malloc_guard)
1658 wrterror(pool, "guard size");
1659 if (!mopts.malloc_freeunmap) {
1660 if (mprotect((char *)p + PAGEROUND(sz) -
1661 mopts.malloc_guard, mopts.malloc_guard,
1662 PROT_READ | PROT_WRITE))
1663 wrterror(pool, "mprotect");
1664 }
1665 STATS_SUB(pool->malloc_guarded, mopts.malloc_guard);
1666 }
1667 unmap(pool, p, PAGEROUND(sz), clear ? argsz : 0);
1668 delete(pool, r);
1669 } else {
1670 void *tmp;
1671 u_int i;
1672
1673 /* Validate and optionally canary check */
1674 struct chunk_info *info = (struct chunk_info *)r->size;
1675 if (B2SIZE(info->bucket) != sz)
1676 wrterror(pool, "internal struct corrupt");
1677 find_chunknum(pool, info, p, mopts.chunk_canaries);
1678
1679 if (mopts.malloc_freecheck) {
1680 for (i = 0; i <= MALLOC_DELAYED_CHUNK_MASK; i++) {
1681 tmp = pool->delayed_chunks[i];
1682 if (tmp == p)
1683 wrterror(pool,
1684 "double free %p", p);
1685 if (tmp != NULL) {
1686 size_t tmpsz;
1687
1688 r = find(pool, tmp);
1689 if (r == NULL)
1690 wrterror(pool,
1691 "bogus pointer ("
1692 "double free?) %p", tmp);
1693 REALSIZE(tmpsz, r);
1694 validate_junk(pool, tmp, tmpsz);
1695 }
1696 }
1697 }
1698
1699 if (clear && argsz > 0)
1700 explicit_bzero(p, argsz);
1701 junk_free(pool->malloc_junk, p, sz);
1702
1703 i = getrbyte(pool) & MALLOC_DELAYED_CHUNK_MASK;
1704 tmp = p;
1705 p = pool->delayed_chunks[i];
1706 if (tmp == p)
1707 wrterror(pool, "double free %p", p);
1708 pool->delayed_chunks[i] = tmp;
1709 if (p != NULL) {
1710 r = find(pool, p);
1711 if (r == NULL)
1712 wrterror(pool,
1713 "bogus pointer (double free?) %p", p);
1714 if (!mopts.malloc_freecheck) {
1715 REALSIZE(sz, r);
1716 validate_junk(pool, p, sz);
1717 }
1718 free_bytes(pool, r, p);
1719 }
1720 }
1721
1722 if (*argpool != pool) {
1723 pool->func = saved_function;
1724 *argpool = pool;
1725 }
1726}
1727
1728void
1729free(void *ptr)
1730{
1731 struct dir_info *d;
1732 int saved_errno = errno;
1733
1734 /* This is legal. */
1735 if (ptr == NULL)
1736 return;
1737
1738 d = getpool();
1739 if (d == NULL)
1740 wrterror(d, "free() called before allocation");
1741 _MALLOC_LOCK(d->mutex);
1742 d->func = "free";
1743 if (d->active++) {
1744 malloc_recurse(d);
1745 return;
1746 }
1747 ofree(&d, ptr, 0, 0, 0);
1748 d->active--;
1749 _MALLOC_UNLOCK(d->mutex);
1750 errno = saved_errno;
1751}
1752DEF_STRONG(free);
1753
1754static void
1755freezero_p(void *ptr, size_t sz)
1756{
1757 explicit_bzero(ptr, sz);
1758 free(ptr);
1759}
1760
1761void
1762freezero(void *ptr, size_t sz)
1763{
1764 struct dir_info *d;
1765 int saved_errno = errno;
1766
1767 /* This is legal. */
1768 if (ptr == NULL)
1769 return;
1770
1771 if (!mopts.internal_funcs) {
1772 freezero_p(ptr, sz);
1773 return;
1774 }
1775
1776 d = getpool();
1777 if (d == NULL)
1778 wrterror(d, "freezero() called before allocation");
1779 _MALLOC_LOCK(d->mutex);
1780 d->func = "freezero";
1781 if (d->active++) {
1782 malloc_recurse(d);
1783 return;
1784 }
1785 ofree(&d, ptr, 1, 1, sz);
1786 d->active--;
1787 _MALLOC_UNLOCK(d->mutex);
1788 errno = saved_errno;
1789}
1790DEF_WEAK(freezero);
1791
1792static void *
1793orealloc(struct dir_info **argpool, void *p, size_t newsz)
1794{
1795 struct region_info *r;
1796 struct dir_info *pool;
1797 const char *saved_function;
1798 struct chunk_info *info;
1799 size_t oldsz, goldsz, gnewsz;
1800 void *q, *ret;
1801 uint32_t chunknum;
1802 int forced;
1803
1804 if (p == NULL)
1805 return omalloc(*argpool, newsz, 0);
1806
1807 if (newsz >= SIZE_MAX - mopts.malloc_guard - MALLOC_PAGESIZE) {
1808 errno = ENOMEM;
1809 return NULL;
1810 }
1811
1812 r = findpool(p, *argpool, &pool, &saved_function);
1813
1814 REALSIZE(oldsz, r);
1815 if (oldsz <= MALLOC_MAXCHUNK) {
1816 if (DO_STATS || mopts.chunk_canaries) {
1817 info = (struct chunk_info *)r->size;
1818 chunknum = find_chunknum(pool, info, p, 0);
1819 }
1820 }
1821
1822 goldsz = oldsz;
1823 if (oldsz > MALLOC_MAXCHUNK) {
1824 if (oldsz < mopts.malloc_guard)
1825 wrterror(pool, "guard size");
1826 oldsz -= mopts.malloc_guard;
1827 }
1828
1829 gnewsz = newsz;
1830 if (gnewsz > MALLOC_MAXCHUNK)
1831 gnewsz += mopts.malloc_guard;
1832
1833 forced = mopts.malloc_realloc || pool->mmap_flag;
1834 if (newsz > MALLOC_MAXCHUNK && oldsz > MALLOC_MAXCHUNK && !forced) {
1835 /* First case: from n pages sized allocation to m pages sized
1836 allocation, m > n */
1837 size_t roldsz = PAGEROUND(goldsz);
1838 size_t rnewsz = PAGEROUND(gnewsz);
1839
1840 if (rnewsz < roldsz && rnewsz > roldsz / 2 &&
1841 roldsz - rnewsz < mopts.def_maxcache * MALLOC_PAGESIZE &&
1842 !mopts.malloc_guard) {
1843
1844 ret = p;
1845 goto done;
1846 }
1847
1848 if (rnewsz > roldsz) {
1849 /* try to extend existing region */
1850 if (!mopts.malloc_guard) {
1851 void *hint = (char *)r->p + roldsz;
1852 size_t needed = rnewsz - roldsz;
1853
1854 STATS_INC(pool->cheap_realloc_tries);
1855 q = MMAPA(hint, needed, MAP_FIXED |
1856 __MAP_NOREPLACE | pool->mmap_flag);
1857 if (q == hint) {
1858 STATS_ADD(pool->malloc_used, needed);
1859 if (pool->malloc_junk == 2)
1860 memset(q, SOME_JUNK, needed);
1861 r->size = gnewsz;
1862 if (r->p != p) {
1863 /* old pointer is moved */
1864 memmove(r->p, p, oldsz);
1865 p = r->p;
1866 }
1867 if (mopts.chunk_canaries)
1868 fill_canary(p, newsz,
1869 PAGEROUND(newsz));
1870 STATS_SETF(r, (*argpool)->caller);
1871 STATS_INC(pool->cheap_reallocs);
1872 ret = p;
1873 goto done;
1874 }
1875 }
1876 } else if (rnewsz < roldsz) {
1877 /* shrink number of pages */
1878 if (mopts.malloc_guard) {
1879 if (mprotect((char *)r->p + rnewsz -
1880 mopts.malloc_guard, mopts.malloc_guard,
1881 PROT_NONE))
1882 wrterror(pool, "mprotect");
1883 }
1884 if (munmap((char *)r->p + rnewsz, roldsz - rnewsz))
1885 wrterror(pool, "munmap %p", (char *)r->p +
1886 rnewsz);
1887 STATS_SUB(pool->malloc_used, roldsz - rnewsz);
1888 r->size = gnewsz;
1889 if (MALLOC_MOVE_COND(gnewsz)) {
1890 void *pp = MALLOC_MOVE(r->p, gnewsz);
1891 memmove(pp, p, newsz);
1892 p = pp;
1893 } else if (mopts.chunk_canaries)
1894 fill_canary(p, newsz, PAGEROUND(newsz));
1895 STATS_SETF(r, (*argpool)->caller);
1896 ret = p;
1897 goto done;
1898 } else {
1899 /* number of pages remains the same */
1900 void *pp = r->p;
1901
1902 r->size = gnewsz;
1903 if (MALLOC_MOVE_COND(gnewsz))
1904 pp = MALLOC_MOVE(r->p, gnewsz);
1905 if (p != pp) {
1906 memmove(pp, p, oldsz < newsz ? oldsz : newsz);
1907 p = pp;
1908 }
1909 if (p == r->p) {
1910 if (newsz > oldsz && pool->malloc_junk == 2)
1911 memset((char *)p + newsz, SOME_JUNK,
1912 rnewsz - mopts.malloc_guard -
1913 newsz);
1914 if (mopts.chunk_canaries)
1915 fill_canary(p, newsz, PAGEROUND(newsz));
1916 }
1917 STATS_SETF(r, (*argpool)->caller);
1918 ret = p;
1919 goto done;
1920 }
1921 }
1922 if (oldsz <= MALLOC_MAXCHUNK && oldsz > 0 &&
1923 newsz <= MALLOC_MAXCHUNK && newsz > 0 &&
1924 !forced && find_bucket(newsz) == find_bucket(oldsz)) {
1925 /* do not reallocate if new size fits good in existing chunk */
1926 if (pool->malloc_junk == 2)
1927 memset((char *)p + newsz, SOME_JUNK, oldsz - newsz);
1928 if (mopts.chunk_canaries) {
1929 info->bits[info->offset + chunknum] = newsz;
1930 fill_canary(p, newsz, B2SIZE(info->bucket));
1931 }
1932 if (DO_STATS)
1933 STATS_SETFN(r, chunknum, (*argpool)->caller);
1934 ret = p;
1935 } else if (newsz != oldsz || forced) {
1936 /* create new allocation */
1937 q = omalloc(pool, newsz, 0);
1938 if (q == NULL) {
1939 ret = NULL;
1940 goto done;
1941 }
1942 if (newsz != 0 && oldsz != 0)
1943 memcpy(q, p, oldsz < newsz ? oldsz : newsz);
1944 ofree(&pool, p, 0, 0, 0);
1945 ret = q;
1946 } else {
1947 /* oldsz == newsz */
1948 if (newsz != 0)
1949 wrterror(pool, "realloc internal inconsistency");
1950 if (DO_STATS)
1951 STATS_SETFN(r, chunknum, (*argpool)->caller);
1952 ret = p;
1953 }
1954done:
1955 if (*argpool != pool) {
1956 pool->func = saved_function;
1957 *argpool = pool;
1958 }
1959 return ret;
1960}
1961
1962void *
1963realloc(void *ptr, size_t size)
1964{
1965 struct dir_info *d;
1966 void *r;
1967 int saved_errno = errno;
1968
1969 PROLOGUE(getpool(), "realloc")
1970 SET_CALLER(d, caller(d));
1971 r = orealloc(&d, ptr, size);
1972 EPILOGUE()
1973 return r;
1974}
1975DEF_STRONG(realloc);
1976
1977/*
1978 * This is sqrt(SIZE_MAX+1), as s1*s2 <= SIZE_MAX
1979 * if both s1 < MUL_NO_OVERFLOW and s2 < MUL_NO_OVERFLOW
1980 */
1981#define MUL_NO_OVERFLOW (1UL << (sizeof(size_t) * 4))
1982
1983void *
1984calloc(size_t nmemb, size_t size)
1985{
1986 struct dir_info *d;
1987 void *r;
1988 int saved_errno = errno;
1989
1990 PROLOGUE(getpool(), "calloc")
1991 SET_CALLER(d, caller(d));
1992 if ((nmemb >= MUL_NO_OVERFLOW || size >= MUL_NO_OVERFLOW) &&
1993 nmemb > 0 && SIZE_MAX / nmemb < size) {
1994 d->active--;
1995 _MALLOC_UNLOCK(d->mutex);
1996 if (mopts.malloc_xmalloc)
1997 wrterror(d, "out of memory");
1998 errno = ENOMEM;
1999 return NULL;
2000 }
2001
2002 size *= nmemb;
2003 r = omalloc(d, size, 1);
2004 EPILOGUE()
2005 return r;
2006}
2007DEF_STRONG(calloc);
2008
2009void *
2010calloc_conceal(size_t nmemb, size_t size)
2011{
2012 struct dir_info *d;
2013 void *r;
2014 int saved_errno = errno;
2015
2016 PROLOGUE(mopts.malloc_pool[0], "calloc_conceal")
2017 SET_CALLER(d, caller(d));
2018 if ((nmemb >= MUL_NO_OVERFLOW || size >= MUL_NO_OVERFLOW) &&
2019 nmemb > 0 && SIZE_MAX / nmemb < size) {
2020 d->active--;
2021 _MALLOC_UNLOCK(d->mutex);
2022 if (mopts.malloc_xmalloc)
2023 wrterror(d, "out of memory");
2024 errno = ENOMEM;
2025 return NULL;
2026 }
2027
2028 size *= nmemb;
2029 r = omalloc(d, size, 1);
2030 EPILOGUE()
2031 return r;
2032}
2033DEF_WEAK(calloc_conceal);
2034
2035static void *
2036orecallocarray(struct dir_info **argpool, void *p, size_t oldsize,
2037 size_t newsize)
2038{
2039 struct region_info *r;
2040 struct dir_info *pool;
2041 const char *saved_function;
2042 void *newptr;
2043 size_t sz;
2044
2045 if (p == NULL)
2046 return omalloc(*argpool, newsize, 1);
2047
2048 if (oldsize == newsize)
2049 return p;
2050
2051 r = findpool(p, *argpool, &pool, &saved_function);
2052
2053 REALSIZE(sz, r);
2054 if (sz <= MALLOC_MAXCHUNK) {
2055 if (mopts.chunk_canaries && sz > 0) {
2056 struct chunk_info *info = (struct chunk_info *)r->size;
2057 uint32_t chunknum = find_chunknum(pool, info, p, 0);
2058
2059 if (info->bits[info->offset + chunknum] != oldsize)
2060 wrterror(pool, "recorded size %hu != %zu",
2061 info->bits[info->offset + chunknum],
2062 oldsize);
2063 } else {
2064 if (sz < oldsize)
2065 wrterror(pool, "chunk size %zu < %zu",
2066 sz, oldsize);
2067 }
2068 } else {
2069 if (sz - mopts.malloc_guard < oldsize)
2070 wrterror(pool, "recorded size %zu < %zu",
2071 sz - mopts.malloc_guard, oldsize);
2072 if (oldsize < (sz - mopts.malloc_guard) / 2)
2073 wrterror(pool,
2074 "recorded size %zu inconsistent with %zu",
2075 sz - mopts.malloc_guard, oldsize);
2076 }
2077
2078 newptr = omalloc(pool, newsize, 0);
2079 if (newptr == NULL)
2080 goto done;
2081
2082 if (newsize > oldsize) {
2083 memcpy(newptr, p, oldsize);
2084 memset((char *)newptr + oldsize, 0, newsize - oldsize);
2085 } else
2086 memcpy(newptr, p, newsize);
2087
2088 ofree(&pool, p, 1, 0, oldsize);
2089
2090done:
2091 if (*argpool != pool) {
2092 pool->func = saved_function;
2093 *argpool = pool;
2094 }
2095
2096 return newptr;
2097}
2098
2099static void *
2100recallocarray_p(void *ptr, size_t oldnmemb, size_t newnmemb, size_t size)
2101{
2102 size_t oldsize, newsize;
2103 void *newptr;
2104
2105 if (ptr == NULL)
2106 return calloc(newnmemb, size);
2107
2108 if ((newnmemb >= MUL_NO_OVERFLOW || size >= MUL_NO_OVERFLOW) &&
2109 newnmemb > 0 && SIZE_MAX / newnmemb < size) {
2110 errno = ENOMEM;
2111 return NULL;
2112 }
2113 newsize = newnmemb * size;
2114
2115 if ((oldnmemb >= MUL_NO_OVERFLOW || size >= MUL_NO_OVERFLOW) &&
2116 oldnmemb > 0 && SIZE_MAX / oldnmemb < size) {
2117 errno = EINVAL;
2118 return NULL;
2119 }
2120 oldsize = oldnmemb * size;
2121
2122 /*
2123 * Don't bother too much if we're shrinking just a bit,
2124 * we do not shrink for series of small steps, oh well.
2125 */
2126 if (newsize <= oldsize) {
2127 size_t d = oldsize - newsize;
2128
2129 if (d < oldsize / 2 && d < MALLOC_PAGESIZE) {
2130 memset((char *)ptr + newsize, 0, d);
2131 return ptr;
2132 }
2133 }
2134
2135 newptr = malloc(newsize);
2136 if (newptr == NULL)
2137 return NULL;
2138
2139 if (newsize > oldsize) {
2140 memcpy(newptr, ptr, oldsize);
2141 memset((char *)newptr + oldsize, 0, newsize - oldsize);
2142 } else
2143 memcpy(newptr, ptr, newsize);
2144
2145 explicit_bzero(ptr, oldsize);
2146 free(ptr);
2147
2148 return newptr;
2149}
2150
2151void *
2152recallocarray(void *ptr, size_t oldnmemb, size_t newnmemb, size_t size)
2153{
2154 struct dir_info *d;
2155 size_t oldsize = 0, newsize;
2156 void *r;
2157 int saved_errno = errno;
2158
2159 if (!mopts.internal_funcs)
2160 return recallocarray_p(ptr, oldnmemb, newnmemb, size);
2161
2162 PROLOGUE(getpool(), "recallocarray")
2163 SET_CALLER(d, caller(d));
2164
2165 if ((newnmemb >= MUL_NO_OVERFLOW || size >= MUL_NO_OVERFLOW) &&
2166 newnmemb > 0 && SIZE_MAX / newnmemb < size) {
2167 d->active--;
2168 _MALLOC_UNLOCK(d->mutex);
2169 if (mopts.malloc_xmalloc)
2170 wrterror(d, "out of memory");
2171 errno = ENOMEM;
2172 return NULL;
2173 }
2174 newsize = newnmemb * size;
2175
2176 if (ptr != NULL) {
2177 if ((oldnmemb >= MUL_NO_OVERFLOW || size >= MUL_NO_OVERFLOW) &&
2178 oldnmemb > 0 && SIZE_MAX / oldnmemb < size) {
2179 d->active--;
2180 _MALLOC_UNLOCK(d->mutex);
2181 errno = EINVAL;
2182 return NULL;
2183 }
2184 oldsize = oldnmemb * size;
2185 }
2186
2187 r = orecallocarray(&d, ptr, oldsize, newsize);
2188 EPILOGUE()
2189 return r;
2190}
2191DEF_WEAK(recallocarray);
2192
2193static void *
2194mapalign(struct dir_info *d, size_t alignment, size_t sz, int zero_fill)
2195{
2196 char *p, *q;
2197
2198 if (alignment < MALLOC_PAGESIZE || ((alignment - 1) & alignment) != 0)
2199 wrterror(d, "mapalign bad alignment");
2200 if (sz != PAGEROUND(sz))
2201 wrterror(d, "mapalign round");
2202
2203 /* Allocate sz + alignment bytes of memory, which must include a
2204 * subrange of size bytes that is properly aligned. Unmap the
2205 * other bytes, and then return that subrange.
2206 */
2207
2208 /* We need sz + alignment to fit into a size_t. */
2209 if (alignment > SIZE_MAX - sz)
2210 return MAP_FAILED;
2211
2212 p = map(d, sz + alignment, zero_fill);
2213 if (p == MAP_FAILED)
2214 return MAP_FAILED;
2215 q = (char *)(((uintptr_t)p + alignment - 1) & ~(alignment - 1));
2216 if (q != p) {
2217 if (munmap(p, q - p))
2218 wrterror(d, "munmap %p", p);
2219 }
2220 if (munmap(q + sz, alignment - (q - p)))
2221 wrterror(d, "munmap %p", q + sz);
2222 STATS_SUB(d->malloc_used, alignment);
2223
2224 return q;
2225}
2226
2227static void *
2228omemalign(struct dir_info *pool, size_t alignment, size_t sz, int zero_fill)
2229{
2230 size_t psz;
2231 void *p, *caller = NULL;
2232
2233 /* If between half a page and a page, avoid MALLOC_MOVE. */
2234 if (sz > MALLOC_MAXCHUNK && sz < MALLOC_PAGESIZE)
2235 sz = MALLOC_PAGESIZE;
2236 if (alignment <= MALLOC_PAGESIZE) {
2237 size_t pof2;
2238 /*
2239 * max(size, alignment) rounded up to power of 2 is enough
2240 * to assure the requested alignment. Large regions are
2241 * always page aligned.
2242 */
2243 if (sz < alignment)
2244 sz = alignment;
2245 if (sz < MALLOC_PAGESIZE) {
2246 pof2 = MALLOC_MINSIZE;
2247 while (pof2 < sz)
2248 pof2 <<= 1;
2249 } else
2250 pof2 = sz;
2251 return omalloc(pool, pof2, zero_fill);
2252 }
2253
2254 if (sz >= SIZE_MAX - mopts.malloc_guard - MALLOC_PAGESIZE) {
2255 errno = ENOMEM;
2256 return NULL;
2257 }
2258
2259 if (sz < MALLOC_PAGESIZE)
2260 sz = MALLOC_PAGESIZE;
2261 sz += mopts.malloc_guard;
2262 psz = PAGEROUND(sz);
2263
2264 p = mapalign(pool, alignment, psz, zero_fill);
2265 if (p == MAP_FAILED) {
2266 errno = ENOMEM;
2267 return NULL;
2268 }
2269
2270#ifdef MALLOC_STATS
2271 if (DO_STATS)
2272 caller = pool->caller;
2273#endif
2274 if (insert(pool, p, sz, caller)) {
2275 unmap(pool, p, psz, 0);
2276 errno = ENOMEM;
2277 return NULL;
2278 }
2279
2280 if (mopts.malloc_guard) {
2281 if (mprotect((char *)p + psz - mopts.malloc_guard,
2282 mopts.malloc_guard, PROT_NONE))
2283 wrterror(pool, "mprotect");
2284 STATS_ADD(pool->malloc_guarded, mopts.malloc_guard);
2285 }
2286
2287 if (pool->malloc_junk == 2) {
2288 if (zero_fill)
2289 memset((char *)p + sz - mopts.malloc_guard,
2290 SOME_JUNK, psz - sz);
2291 else
2292 memset(p, SOME_JUNK, psz - mopts.malloc_guard);
2293 } else if (mopts.chunk_canaries)
2294 fill_canary(p, sz - mopts.malloc_guard,
2295 psz - mopts.malloc_guard);
2296
2297 return p;
2298}
2299
2300int
2301posix_memalign(void **memptr, size_t alignment, size_t size)
2302{
2303 struct dir_info *d;
2304 int res, saved_errno = errno;
2305 void *r;
2306
2307 /* Make sure that alignment is a large enough power of 2. */
2308 if (((alignment - 1) & alignment) != 0 || alignment < sizeof(void *))
2309 return EINVAL;
2310
2311 d = getpool();
2312 if (d == NULL) {
2313 _malloc_init(0);
2314 d = getpool();
2315 }
2316 _MALLOC_LOCK(d->mutex);
2317 d->func = "posix_memalign";
2318 if (d->active++) {
2319 malloc_recurse(d);
2320 goto err;
2321 }
2322 SET_CALLER(d, caller(d));
2323 r = omemalign(d, alignment, size, 0);
2324 d->active--;
2325 _MALLOC_UNLOCK(d->mutex);
2326 if (r == NULL) {
2327 if (mopts.malloc_xmalloc)
2328 wrterror(d, "out of memory");
2329 goto err;
2330 }
2331 errno = saved_errno;
2332 *memptr = r;
2333 return 0;
2334
2335err:
2336 res = errno;
2337 errno = saved_errno;
2338 return res;
2339}
2340DEF_STRONG(posix_memalign);
2341
2342void *
2343aligned_alloc(size_t alignment, size_t size)
2344{
2345 struct dir_info *d;
2346 int saved_errno = errno;
2347 void *r;
2348
2349 /* Make sure that alignment is a positive power of 2. */
2350 if (((alignment - 1) & alignment) != 0 || alignment == 0) {
2351 errno = EINVAL;
2352 return NULL;
2353 }
2354 /* Per spec, size should be a multiple of alignment */
2355 if ((size & (alignment - 1)) != 0) {
2356 errno = EINVAL;
2357 return NULL;
2358 }
2359
2360 PROLOGUE(getpool(), "aligned_alloc")
2361 SET_CALLER(d, caller(d));
2362 r = omemalign(d, alignment, size, 0);
2363 EPILOGUE()
2364 return r;
2365}
2366DEF_STRONG(aligned_alloc);
2367
2368#ifdef MALLOC_STATS
2369
2370static int
2371btcmp(const struct btnode *e1, const struct btnode *e2)
2372{
2373 return memcmp(e1->caller, e2->caller, sizeof(e1->caller));
2374}
2375
2376RBT_GENERATE(btshead, btnode, entry, btcmp);
2377
2378static void*
2379store_caller(struct dir_info *d, struct btnode *f)
2380{
2381 struct btnode *p;
2382
2383 if (DO_STATS == 0 || d->btnodes == MAP_FAILED)
2384 return NULL;
2385
2386 p = RBT_FIND(btshead, &d->btraces, f);
2387 if (p != NULL)
2388 return p;
2389 if (d->btnodes == NULL ||
2390 d->btnodesused >= MALLOC_PAGESIZE / sizeof(struct btnode)) {
2391 d->btnodes = map(d, MALLOC_PAGESIZE, 0);
2392 if (d->btnodes == MAP_FAILED)
2393 return NULL;
2394 d->btnodesused = 0;
2395 }
2396 p = &d->btnodes[d->btnodesused++];
2397 memcpy(p->caller, f->caller, sizeof(p->caller[0]) * DO_STATS);
2398 RBT_INSERT(btshead, &d->btraces, p);
2399 return p;
2400}
2401
2402static void fabstorel(const void *, char *, size_t);
2403
2404static void
2405print_chunk_details(struct dir_info *pool, void *p, size_t sz, size_t index)
2406{
2407 struct region_info *r;
2408 struct chunk_info *chunkinfo;
2409 struct btnode* btnode;
2410 uint32_t chunknum;
2411 int frame;
2412 char buf1[128];
2413 char buf2[128];
2414 const char *msg = "";
2415
2416 r = find(pool, p);
2417 chunkinfo = (struct chunk_info *)r->size;
2418 chunknum = find_chunknum(pool, chunkinfo, p, 0);
2419 btnode = (struct btnode *)r->f[chunknum];
2420 frame = DO_STATS - 1;
2421 if (btnode != NULL)
2422 fabstorel(btnode->caller[frame], buf1, sizeof(buf1));
2423 strlcpy(buf2, ". 0x0", sizeof(buf2));
2424 if (chunknum > 0) {
2425 chunknum--;
2426 btnode = (struct btnode *)r->f[chunknum];
2427 if (btnode != NULL)
2428 fabstorel(btnode->caller[frame], buf2, sizeof(buf2));
2429 if (CHUNK_FREE(chunkinfo, chunknum))
2430 msg = " (now free)";
2431 }
2432
2433 wrterror(pool,
2434 "write to free chunk %p[%zu..%zu]@%zu allocated at %s "
2435 "(preceding chunk %p allocated at %s%s)",
2436 p, index * sizeof(uint64_t), (index + 1) * sizeof(uint64_t) - 1,
2437 sz, buf1, p - sz, buf2, msg);
2438}
2439
2440static void
2441ulog(const char *format, ...)
2442{
2443 va_list ap;
2444 static char* buf;
2445 static size_t filled;
2446 int len;
2447
2448 if (buf == NULL)
2449 buf = MMAP(KTR_USER_MAXLEN, 0);
2450 if (buf == MAP_FAILED)
2451 return;
2452
2453 va_start(ap, format);
2454 len = vsnprintf(buf + filled, KTR_USER_MAXLEN - filled, format, ap);
2455 va_end(ap);
2456 if (len < 0)
2457 return;
2458 if ((size_t)len > KTR_USER_MAXLEN - filled)
2459 len = KTR_USER_MAXLEN - filled;
2460 filled += len;
2461 if (filled > 0) {
2462 if (filled == KTR_USER_MAXLEN || buf[filled - 1] == '\n') {
2463 utrace("malloc", buf, filled);
2464 filled = 0;
2465 }
2466 }
2467}
2468
2469struct malloc_leak {
2470 void *f;
2471 size_t total_size;
2472 int count;
2473};
2474
2475struct leaknode {
2476 RBT_ENTRY(leaknode) entry;
2477 struct malloc_leak d;
2478};
2479
2480static inline int
2481leakcmp(const struct leaknode *e1, const struct leaknode *e2)
2482{
2483 return e1->d.f < e2->d.f ? -1 : e1->d.f > e2->d.f;
2484}
2485
2486RBT_HEAD(leaktree, leaknode);
2487RBT_PROTOTYPE(leaktree, leaknode, entry, leakcmp);
2488RBT_GENERATE(leaktree, leaknode, entry, leakcmp);
2489
2490static void
2491wrtwarning(const char *func, char *msg, ...)
2492{
2493 int saved_errno = errno;
2494 va_list ap;
2495
2496 dprintf(STDERR_FILENO, "%s(%d) in %s(): ", __progname,
2497 getpid(), func != NULL ? func : "unknown");
2498 va_start(ap, msg);
2499 vdprintf(STDERR_FILENO, msg, ap);
2500 va_end(ap);
2501 dprintf(STDERR_FILENO, "\n");
2502
2503 errno = saved_errno;
2504}
2505
2506static void
2507putleakinfo(struct leaktree *leaks, void *f, size_t sz, int cnt)
2508{
2509 struct leaknode key, *p;
2510 static struct leaknode *page;
2511 static unsigned int used;
2512
2513 if (cnt == 0 || page == MAP_FAILED)
2514 return;
2515
2516 key.d.f = f;
2517 p = RBT_FIND(leaktree, leaks, &key);
2518 if (p == NULL) {
2519 if (page == NULL ||
2520 used >= MALLOC_PAGESIZE / sizeof(struct leaknode)) {
2521 page = MMAP(MALLOC_PAGESIZE, 0);
2522 if (page == MAP_FAILED) {
2523 wrtwarning(__func__, strerror(errno));
2524 return;
2525 }
2526 used = 0;
2527 }
2528 p = &page[used++];
2529 p->d.f = f;
2530 p->d.total_size = sz * cnt;
2531 p->d.count = cnt;
2532 RBT_INSERT(leaktree, leaks, p);
2533 } else {
2534 p->d.total_size += sz * cnt;
2535 p->d.count += cnt;
2536 }
2537}
2538
2539static void
2540fabstorel(const void *f, char *buf, size_t size)
2541{
2542 Dl_info info;
2543 const char *object = ".";
2544 const char *caller;
2545
2546 caller = f;
2547 if (caller != NULL && dladdr(f, &info) != 0) {
2548 caller -= (uintptr_t)info.dli_fbase;
2549 object = info.dli_fname;
2550 }
2551 snprintf(buf, size, "%s %p", object, caller);
2552}
2553
2554static void
2555dump_leak(struct leaknode *p)
2556{
2557 int i;
2558 char buf[128];
2559
2560 if (p->d.f == NULL) {
2561 fabstorel(NULL, buf, sizeof(buf));
2562 ulog("%18p %7zu %6u %6zu addr2line -e %s\n",
2563 p->d.f, p->d.total_size, p->d.count,
2564 p->d.total_size / p->d.count, buf);
2565 return;
2566 }
2567
2568 for (i = 0; i < DO_STATS; i++) {
2569 const char *abscaller;
2570
2571 abscaller = ((struct btnode*)p->d.f)->caller[i];
2572 if (abscaller == NULL)
2573 break;
2574 fabstorel(abscaller, buf, sizeof(buf));
2575 if (i == 0)
2576 ulog("%18p %7zu %6u %6zu addr2line -e %s\n",
2577 abscaller, p->d.total_size, p->d.count,
2578 p->d.total_size / p->d.count, buf);
2579 else
2580 ulog("%*p %*s %6s %6s addr2line -e %s\n",
2581 i + 18, abscaller, 7 - i, "-", "-", "-", buf);
2582 }
2583}
2584
2585static void
2586dump_leaks(struct leaktree *leaks)
2587{
2588 struct leaknode *p;
2589
2590 ulog("Leak report:\n");
2591 ulog(" f sum # avg\n");
2592
2593 RBT_FOREACH(p, leaktree, leaks)
2594 dump_leak(p);
2595}
2596
2597static void
2598dump_chunk(struct leaktree* leaks, struct chunk_info *p, void **f,
2599 int fromfreelist)
2600{
2601 while (p != NULL) {
2602 if (mopts.malloc_verbose)
2603 ulog("chunk %18p %18p %4zu %d/%d\n",
2604 p->page, NULL,
2605 B2SIZE(p->bucket), p->free, p->total);
2606 if (!fromfreelist) {
2607 size_t i, sz = B2SIZE(p->bucket);
2608 for (i = 0; i < p->total; i++) {
2609 if (!CHUNK_FREE(p, i))
2610 putleakinfo(leaks, f[i], sz, 1);
2611 }
2612 break;
2613 }
2614 p = LIST_NEXT(p, entries);
2615 if (mopts.malloc_verbose && p != NULL)
2616 ulog(" ->");
2617 }
2618}
2619
2620static void
2621dump_free_chunk_info(struct dir_info *d, struct leaktree *leaks)
2622{
2623 u_int i, j, count;
2624 struct chunk_info *p;
2625
2626 ulog("Free chunk structs:\n");
2627 ulog("Bkt) #CI page"
2628 " f size free/n\n");
2629 for (i = 0; i <= BUCKETS; i++) {
2630 count = 0;
2631 LIST_FOREACH(p, &d->chunk_info_list[i], entries)
2632 count++;
2633 for (j = 0; j < MALLOC_CHUNK_LISTS; j++) {
2634 p = LIST_FIRST(&d->chunk_dir[i][j]);
2635 if (p == NULL && count == 0)
2636 continue;
2637 if (j == 0)
2638 ulog("%3d) %3d ", i, count);
2639 else
2640 ulog(" ");
2641 if (p != NULL)
2642 dump_chunk(leaks, p, NULL, 1);
2643 else
2644 ulog(".\n");
2645 }
2646 }
2647
2648}
2649
2650static void
2651dump_free_page_info(struct dir_info *d)
2652{
2653 struct smallcache *cache;
2654 size_t i, total = 0;
2655
2656 ulog("Cached in small cache:\n");
2657 for (i = 0; i < MAX_SMALLCACHEABLE_SIZE; i++) {
2658 cache = &d->smallcache[i];
2659 if (cache->length != 0)
2660 ulog("%zu(%u): %u = %zu\n", i + 1, cache->max,
2661 cache->length, cache->length * (i + 1));
2662 total += cache->length * (i + 1);
2663 }
2664
2665 ulog("Cached in big cache: %zu/%zu\n", d->bigcache_used,
2666 d->bigcache_size);
2667 for (i = 0; i < d->bigcache_size; i++) {
2668 if (d->bigcache[i].psize != 0)
2669 ulog("%zu: %zu\n", i, d->bigcache[i].psize);
2670 total += d->bigcache[i].psize;
2671 }
2672 ulog("Free pages cached: %zu\n", total);
2673}
2674
2675static void
2676malloc_dump1(int poolno, struct dir_info *d, struct leaktree *leaks)
2677{
2678 size_t i, realsize;
2679
2680 if (mopts.malloc_verbose) {
2681 ulog("Malloc dir of %s pool %d at %p\n", __progname, poolno, d);
2682 ulog("MT=%d J=%d Fl=%#x\n", d->malloc_mt, d->malloc_junk,
2683 d->mmap_flag);
2684 ulog("Region slots free %zu/%zu\n",
2685 d->regions_free, d->regions_total);
2686 ulog("Inserts %zu/%zu\n", d->inserts, d->insert_collisions);
2687 ulog("Deletes %zu/%zu\n", d->deletes, d->delete_moves);
2688 ulog("Cheap reallocs %zu/%zu\n",
2689 d->cheap_reallocs, d->cheap_realloc_tries);
2690 ulog("In use %zu\n", d->malloc_used);
2691 ulog("Guarded %zu\n", d->malloc_guarded);
2692 dump_free_chunk_info(d, leaks);
2693 dump_free_page_info(d);
2694 ulog("Hash table:\n");
2695 ulog("slot) hash d type page "
2696 "f size [free/n]\n");
2697 }
2698 for (i = 0; i < d->regions_total; i++) {
2699 if (d->r[i].p != NULL) {
2700 size_t h = hash(d->r[i].p) &
2701 (d->regions_total - 1);
2702 if (mopts.malloc_verbose)
2703 ulog("%4zx) #%4zx %zd ",
2704 i, h, h - i);
2705 REALSIZE(realsize, &d->r[i]);
2706 if (realsize > MALLOC_MAXCHUNK) {
2707 putleakinfo(leaks, d->r[i].f, realsize, 1);
2708 if (mopts.malloc_verbose)
2709 ulog("pages %18p %18p %zu\n", d->r[i].p,
2710 d->r[i].f, realsize);
2711 } else
2712 dump_chunk(leaks,
2713 (struct chunk_info *)d->r[i].size,
2714 d->r[i].f, 0);
2715 }
2716 }
2717 if (mopts.malloc_verbose)
2718 ulog("\n");
2719}
2720
2721static void
2722malloc_dump0(int poolno, struct dir_info *pool, struct leaktree *leaks)
2723{
2724 int i;
2725 void *p;
2726 struct region_info *r;
2727
2728 if (pool == NULL || pool->r == NULL)
2729 return;
2730 for (i = 0; i < MALLOC_DELAYED_CHUNK_MASK + 1; i++) {
2731 p = pool->delayed_chunks[i];
2732 if (p == NULL)
2733 continue;
2734 r = find(pool, p);
2735 if (r == NULL)
2736 wrterror(pool, "bogus pointer in malloc_dump %p", p);
2737 free_bytes(pool, r, p);
2738 pool->delayed_chunks[i] = NULL;
2739 }
2740 malloc_dump1(poolno, pool, leaks);
2741}
2742
2743void
2744malloc_dump(void)
2745{
2746 u_int i;
2747 int saved_errno = errno;
2748
2749 /* XXX leak when run multiple times */
2750 struct leaktree leaks = RBT_INITIALIZER(&leaks);
2751
2752 for (i = 0; i < mopts.malloc_mutexes; i++)
2753 malloc_dump0(i, mopts.malloc_pool[i], &leaks);
2754
2755 dump_leaks(&leaks);
2756 ulog("\n");
2757 errno = saved_errno;
2758}
2759DEF_WEAK(malloc_dump);
2760
2761static void
2762malloc_exit(void)
2763{
2764 int save_errno = errno;
2765
2766 ulog("******** Start dump %s *******\n", __progname);
2767 ulog("M=%u I=%d F=%d U=%d J=%d R=%d X=%d C=%#x cache=%u "
2768 "G=%zu\n",
2769 mopts.malloc_mutexes,
2770 mopts.internal_funcs, mopts.malloc_freecheck,
2771 mopts.malloc_freeunmap, mopts.def_malloc_junk,
2772 mopts.malloc_realloc, mopts.malloc_xmalloc,
2773 mopts.chunk_canaries, mopts.def_maxcache,
2774 mopts.malloc_guard);
2775
2776 malloc_dump();
2777 ulog("******** End dump %s *******\n", __progname);
2778 errno = save_errno;
2779}
2780
2781#endif /* MALLOC_STATS */
diff --git a/src/lib/libc/stdlib/merge.c b/src/lib/libc/stdlib/merge.c
deleted file mode 100644
index d60317ce08..0000000000
--- a/src/lib/libc/stdlib/merge.c
+++ /dev/null
@@ -1,336 +0,0 @@
1/* $OpenBSD: merge.c,v 1.10 2015/06/21 03:20:56 millert Exp $ */
2/*-
3 * Copyright (c) 1992, 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 * Peter McIlroy.
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. 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/*
35 * Hybrid exponential search/linear search merge sort with hybrid
36 * natural/pairwise first pass. Requires about .3% more comparisons
37 * for random data than LSMS with pairwise first pass alone.
38 * It works for objects as small as two bytes.
39 */
40
41#define NATURAL
42#define THRESHOLD 16 /* Best choice for natural merge cut-off. */
43
44/* #define NATURAL to get hybrid natural merge.
45 * (The default is pairwise merging.)
46 */
47
48#include <sys/types.h>
49
50#include <errno.h>
51#include <stdlib.h>
52#include <string.h>
53
54static void setup(u_char *, u_char *, size_t, size_t, int (*)());
55static void insertionsort(u_char *, size_t, size_t, int (*)());
56
57#define ISIZE sizeof(int)
58#define PSIZE sizeof(u_char *)
59#define ICOPY_LIST(src, dst, last) \
60 do \
61 *(int*)dst = *(int*)src, src += ISIZE, dst += ISIZE; \
62 while(src < last)
63#define ICOPY_ELT(src, dst, i) \
64 do \
65 *(int*) dst = *(int*) src, src += ISIZE, dst += ISIZE; \
66 while (i -= ISIZE)
67
68#define CCOPY_LIST(src, dst, last) \
69 do \
70 *dst++ = *src++; \
71 while (src < last)
72#define CCOPY_ELT(src, dst, i) \
73 do \
74 *dst++ = *src++; \
75 while (i -= 1)
76
77/*
78 * Find the next possible pointer head. (Trickery for forcing an array
79 * to do double duty as a linked list when objects do not align with word
80 * boundaries.
81 */
82/* Assumption: PSIZE is a power of 2. */
83#define EVAL(p) (u_char **) \
84 ((u_char *)0 + \
85 (((u_char *)p + PSIZE - 1 - (u_char *) 0) & ~(PSIZE - 1)))
86
87/*
88 * Arguments are as for qsort.
89 */
90int
91mergesort(void *base, size_t nmemb, size_t size,
92 int (*cmp)(const void *, const void *))
93{
94 int i, sense;
95 int big, iflag;
96 u_char *f1, *f2, *t, *b, *tp2, *q, *l1, *l2;
97 u_char *list2, *list1, *p2, *p, *last, **p1;
98
99 if (size < PSIZE / 2) { /* Pointers must fit into 2 * size. */
100 errno = EINVAL;
101 return (-1);
102 }
103
104 if (nmemb == 0)
105 return (0);
106
107 /*
108 * XXX
109 * Stupid subtraction for the Cray.
110 */
111 iflag = 0;
112 if (!(size % ISIZE) && !(((char *)base - (char *)0) % ISIZE))
113 iflag = 1;
114
115 if ((list2 = malloc(nmemb * size + PSIZE)) == NULL)
116 return (-1);
117
118 list1 = base;
119 setup(list1, list2, nmemb, size, cmp);
120 last = list2 + nmemb * size;
121 i = big = 0;
122 while (*EVAL(list2) != last) {
123 l2 = list1;
124 p1 = EVAL(list1);
125 for (tp2 = p2 = list2; p2 != last; p1 = EVAL(l2)) {
126 p2 = *EVAL(p2);
127 f1 = l2;
128 f2 = l1 = list1 + (p2 - list2);
129 if (p2 != last)
130 p2 = *EVAL(p2);
131 l2 = list1 + (p2 - list2);
132 while (f1 < l1 && f2 < l2) {
133 if ((*cmp)(f1, f2) <= 0) {
134 q = f2;
135 b = f1, t = l1;
136 sense = -1;
137 } else {
138 q = f1;
139 b = f2, t = l2;
140 sense = 0;
141 }
142 if (!big) { /* here i = 0 */
143 while ((b += size) < t && cmp(q, b) >sense)
144 if (++i == 6) {
145 big = 1;
146 goto EXPONENTIAL;
147 }
148 } else {
149EXPONENTIAL: for (i = size; ; i <<= 1)
150 if ((p = (b + i)) >= t) {
151 if ((p = t - size) > b &&
152 (*cmp)(q, p) <= sense)
153 t = p;
154 else
155 b = p;
156 break;
157 } else if ((*cmp)(q, p) <= sense) {
158 t = p;
159 if (i == size)
160 big = 0;
161 goto FASTCASE;
162 } else
163 b = p;
164 while (t > b+size) {
165 i = (((t - b) / size) >> 1) * size;
166 if ((*cmp)(q, p = b + i) <= sense)
167 t = p;
168 else
169 b = p;
170 }
171 goto COPY;
172FASTCASE: while (i > size)
173 if ((*cmp)(q,
174 p = b + (i >>= 1)) <= sense)
175 t = p;
176 else
177 b = p;
178COPY: b = t;
179 }
180 i = size;
181 if (q == f1) {
182 if (iflag) {
183 ICOPY_LIST(f2, tp2, b);
184 ICOPY_ELT(f1, tp2, i);
185 } else {
186 CCOPY_LIST(f2, tp2, b);
187 CCOPY_ELT(f1, tp2, i);
188 }
189 } else {
190 if (iflag) {
191 ICOPY_LIST(f1, tp2, b);
192 ICOPY_ELT(f2, tp2, i);
193 } else {
194 CCOPY_LIST(f1, tp2, b);
195 CCOPY_ELT(f2, tp2, i);
196 }
197 }
198 }
199 if (f2 < l2) {
200 if (iflag)
201 ICOPY_LIST(f2, tp2, l2);
202 else
203 CCOPY_LIST(f2, tp2, l2);
204 } else if (f1 < l1) {
205 if (iflag)
206 ICOPY_LIST(f1, tp2, l1);
207 else
208 CCOPY_LIST(f1, tp2, l1);
209 }
210 *p1 = l2;
211 }
212 tp2 = list1; /* swap list1, list2 */
213 list1 = list2;
214 list2 = tp2;
215 last = list2 + nmemb*size;
216 }
217 if (base == list2) {
218 memmove(list2, list1, nmemb*size);
219 list2 = list1;
220 }
221 free(list2);
222 return (0);
223}
224
225#define swap(a, b) { \
226 s = b; \
227 i = size; \
228 do { \
229 tmp = *a; *a++ = *s; *s++ = tmp; \
230 } while (--i); \
231 a -= size; \
232 }
233#define reverse(bot, top) { \
234 s = top; \
235 do { \
236 i = size; \
237 do { \
238 tmp = *bot; *bot++ = *s; *s++ = tmp; \
239 } while (--i); \
240 s -= size2; \
241 } while(bot < s); \
242}
243
244/*
245 * Optional hybrid natural/pairwise first pass. Eats up list1 in runs of
246 * increasing order, list2 in a corresponding linked list. Checks for runs
247 * when THRESHOLD/2 pairs compare with same sense. (Only used when NATURAL
248 * is defined. Otherwise simple pairwise merging is used.)
249 */
250void
251setup(u_char *list1, u_char *list2, size_t n, size_t size,
252 int (*cmp)(const void *, const void *))
253{
254 int i, length, size2, sense;
255 u_char tmp, *f1, *f2, *s, *l2, *last, *p2;
256
257 size2 = size*2;
258 if (n <= 5) {
259 insertionsort(list1, n, size, cmp);
260 *EVAL(list2) = (u_char*) list2 + n*size;
261 return;
262 }
263 /*
264 * Avoid running pointers out of bounds; limit n to evens
265 * for simplicity.
266 */
267 i = 4 + (n & 1);
268 insertionsort(list1 + (n - i) * size, i, size, cmp);
269 last = list1 + size * (n - i);
270 *EVAL(list2 + (last - list1)) = list2 + n * size;
271
272#ifdef NATURAL
273 p2 = list2;
274 f1 = list1;
275 sense = (cmp(f1, f1 + size) > 0);
276 for (; f1 < last; sense = !sense) {
277 length = 2;
278 /* Find pairs with same sense. */
279 for (f2 = f1 + size2; f2 < last; f2 += size2) {
280 if ((cmp(f2, f2+ size) > 0) != sense)
281 break;
282 length += 2;
283 }
284 if (length < THRESHOLD) { /* Pairwise merge */
285 do {
286 p2 = *EVAL(p2) = f1 + size2 - list1 + list2;
287 if (sense > 0)
288 swap (f1, f1 + size);
289 } while ((f1 += size2) < f2);
290 } else { /* Natural merge */
291 l2 = f2;
292 for (f2 = f1 + size2; f2 < l2; f2 += size2) {
293 if ((cmp(f2-size, f2) > 0) != sense) {
294 p2 = *EVAL(p2) = f2 - list1 + list2;
295 if (sense > 0)
296 reverse(f1, f2-size);
297 f1 = f2;
298 }
299 }
300 if (sense > 0)
301 reverse (f1, f2-size);
302 f1 = f2;
303 if (f2 < last || cmp(f2 - size, f2) > 0)
304 p2 = *EVAL(p2) = f2 - list1 + list2;
305 else
306 p2 = *EVAL(p2) = list2 + n*size;
307 }
308 }
309#else /* pairwise merge only. */
310 for (f1 = list1, p2 = list2; f1 < last; f1 += size2) {
311 p2 = *EVAL(p2) = p2 + size2;
312 if (cmp (f1, f1 + size) > 0)
313 swap(f1, f1 + size);
314 }
315#endif /* NATURAL */
316}
317
318/*
319 * This is to avoid out-of-bounds addresses in sorting the
320 * last 4 elements.
321 */
322static void
323insertionsort(u_char *a, size_t n, size_t size,
324 int (*cmp)(const void *, const void *))
325{
326 u_char *ai, *s, *t, *u, tmp;
327 int i;
328
329 for (ai = a+size; --n >= 1; ai += size)
330 for (t = ai; t > a; t -= size) {
331 u = t - size;
332 if (cmp(u, t) <= 0)
333 break;
334 swap(u, t);
335 }
336}
diff --git a/src/lib/libc/stdlib/mkdtemp.c b/src/lib/libc/stdlib/mkdtemp.c
deleted file mode 100644
index c11501f893..0000000000
--- a/src/lib/libc/stdlib/mkdtemp.c
+++ /dev/null
@@ -1,41 +0,0 @@
1/* $OpenBSD: mkdtemp.c,v 1.2 2024/03/01 21:30:40 millert Exp $ */
2/*
3 * Copyright (c) 2024 Todd C. Miller
4 *
5 * Permission to use, copy, modify, and distribute this software for any
6 * purpose with or without fee is hereby granted, provided that the above
7 * copyright notice and this permission notice appear in all copies.
8 *
9 * THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES
10 * WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF
11 * MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR
12 * ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES
13 * WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN
14 * ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF
15 * OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE.
16 */
17
18#include <sys/stat.h>
19#include <stdlib.h>
20
21static int
22mkdtemp_cb(const char *path, int flags)
23{
24 return mkdir(path, S_IRUSR|S_IWUSR|S_IXUSR);
25}
26
27char *
28mkdtemp(char *path)
29{
30 if (__mktemp4(path, 0, 0, mkdtemp_cb) == 0)
31 return path;
32 return NULL;
33}
34
35char *
36mkdtemps(char *path, int slen)
37{
38 if (__mktemp4(path, slen, 0, mkdtemp_cb) == 0)
39 return path;
40 return NULL;
41}
diff --git a/src/lib/libc/stdlib/mkstemp.c b/src/lib/libc/stdlib/mkstemp.c
deleted file mode 100644
index 75a9d27d1a..0000000000
--- a/src/lib/libc/stdlib/mkstemp.c
+++ /dev/null
@@ -1,64 +0,0 @@
1/* $OpenBSD: mkstemp.c,v 1.1 2024/01/19 19:45:02 millert Exp $ */
2/*
3 * Copyright (c) 2024 Todd C. Miller
4 *
5 * Permission to use, copy, modify, and distribute this software for any
6 * purpose with or without fee is hereby granted, provided that the above
7 * copyright notice and this permission notice appear in all copies.
8 *
9 * THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES
10 * WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF
11 * MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR
12 * ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES
13 * WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN
14 * ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF
15 * OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE.
16 */
17
18#include <sys/stat.h>
19#include <errno.h>
20#include <fcntl.h>
21#include <stdlib.h>
22
23#define MKOSTEMP_FLAGS (O_APPEND | O_CLOEXEC | O_DSYNC | O_RSYNC | O_SYNC)
24
25static int
26mkstemp_cb(const char *path, int flags)
27{
28 flags |= O_CREAT | O_EXCL | O_RDWR;
29 return open(path, flags, S_IRUSR|S_IWUSR);
30}
31
32int
33mkostemps(char *path, int slen, int flags)
34{
35 if (flags & ~MKOSTEMP_FLAGS) {
36 errno = EINVAL;
37 return -1;
38 }
39 return __mktemp4(path, slen, flags, mkstemp_cb);
40}
41
42int
43mkostemp(char *path, int flags)
44{
45 if (flags & ~MKOSTEMP_FLAGS) {
46 errno = EINVAL;
47 return -1;
48 }
49 return __mktemp4(path, 0, flags, mkstemp_cb);
50}
51DEF_WEAK(mkostemp);
52
53int
54mkstemp(char *path)
55{
56 return __mktemp4(path, 0, 0, mkstemp_cb);
57}
58DEF_WEAK(mkstemp);
59
60int
61mkstemps(char *path, int slen)
62{
63 return __mktemp4(path, slen, 0, mkstemp_cb);
64}
diff --git a/src/lib/libc/stdlib/mktemp.3 b/src/lib/libc/stdlib/mktemp.3
deleted file mode 100644
index 83b7c9eb30..0000000000
--- a/src/lib/libc/stdlib/mktemp.3
+++ /dev/null
@@ -1,431 +0,0 @@
1.\" $OpenBSD: mktemp.3,v 1.2 2024/03/01 21:30:40 millert Exp $
2.\"
3.\" Copyright (c) 1989, 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. Neither the name of the University nor the names of its contributors
15.\" may be used to endorse or promote products derived from this software
16.\" without specific prior written permission.
17.\"
18.\" THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
19.\" ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
20.\" IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
21.\" ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
22.\" FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
23.\" DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
24.\" OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
25.\" HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
26.\" LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
27.\" OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
28.\" SUCH DAMAGE.
29.\"
30.Dd $Mdocdate: March 1 2024 $
31.Dt MKTEMP 3
32.Os
33.Sh NAME
34.Nm mktemp ,
35.Nm mkstemp ,
36.Nm mkostemp ,
37.Nm mkstemps ,
38.Nm mkostemps ,
39.Nm mkdtemp ,
40.Nm mkdtemps
41.Nd make temporary file name (unique)
42.Sh SYNOPSIS
43.In stdlib.h
44.Ft char *
45.Fn mktemp "char *template"
46.Ft int
47.Fn mkstemp "char *template"
48.Ft int
49.Fn mkstemps "char *template" "int suffixlen"
50.Ft char *
51.Fn mkdtemp "char *template"
52.Ft char *
53.Fn mkdtemps "char *template" "int suffixlen"
54.In stdlib.h
55.In fcntl.h
56.Ft int
57.Fn mkostemp "char *template" "int flags"
58.Ft int
59.Fn mkostemps "char *template" "int suffixlen" "int flags"
60.Sh DESCRIPTION
61The
62.Fn mktemp
63family of functions take the given file name template and overwrite
64a portion of it to create a new file name.
65This file name is unique and suitable for use by the application.
66The template may be any file name with at least six trailing
67.Em X Ns s ,
68for example
69.Pa /tmp/temp.XXXXXXXX .
70The trailing
71.Em X Ns s
72are replaced with a unique digit and letter combination.
73The number of unique file names that can be returned
74depends on the number of
75.Em X Ns s
76provided;
77.Fn mktemp
78will try at least 2 ** 31 combinations before giving up.
79At least six
80.Em X Ns s
81must be used, though 10 is much better.
82.Pp
83The
84.Fn mktemp
85function generates a temporary file name based on a template as
86described above.
87Because
88.Fn mktemp
89does not actually create the temporary file, there is a window of
90opportunity during which another process can open the file instead.
91Because of this race condition,
92.Fn mktemp
93should not be used where
94.Fn mkstemp
95can be used instead.
96.Fn mktemp
97was marked as a legacy interface in
98.St -p1003.1-2001 .
99.Pp
100The
101.Fn mkstemp
102function makes the same replacement to the template and creates the template
103file, mode 0600, returning a file descriptor opened for reading and writing.
104This avoids the race between testing for a file's existence and opening it
105for use.
106.Pp
107The
108.Fn mkostemp
109function acts the same as
110.Fn mkstemp ,
111except that the
112.Fa flags
113argument may contain zero or more of the following flags for the underlying
114.Xr open 2
115system call:
116.Pp
117.Bl -tag -width "O_CLOEXECXX" -offset indent -compact
118.It Dv O_APPEND
119Append on each write.
120.It Dv O_CLOEXEC
121Set the close-on-exec flag on the new file descriptor.
122.It Dv O_SYNC
123Perform synchronous I/O operations.
124.El
125.Pp
126The
127.Fn mkstemps
128and
129.Fn mkostemps
130functions act the same as
131.Fn mkstemp
132and
133.Fn mkostemp ,
134except they permit a suffix to exist in the template.
135The template should be of the form
136.Pa /tmp/tmpXXXXXXXXXXsuffix .
137.Fn mkstemps
138and
139.Fn mkostemps
140are told the length of the suffix string, i.e.,
141.Li strlen("suffix") .
142.Pp
143The
144.Fn mkdtemp
145function makes the same replacement to the template as in
146.Fn mktemp
147and creates the template directory, mode 0700.
148The
149.Fn mkdtemps
150function acts the same as
151.Fn mkdtemp ,
152except that it permits a suffix to exist in the template,
153similar to
154.Fn mkstemps .
155.Sh RETURN VALUES
156The
157.Fn mktemp ,
158.Fn mkdtemp ,
159and
160.Fn mkdtemps
161functions return a pointer to the template on success and
162.Dv NULL
163on failure.
164The
165.Fn mkstemp ,
166.Fn mkostemp ,
167.Fn mkstemps ,
168and
169.Fn mkostemps
170functions return \-1 if no suitable file could be created.
171If any call fails, an error code is placed in the global variable
172.Va errno .
173.Sh EXAMPLES
174Quite often a programmer will want to replace a use of
175.Fn mktemp
176with
177.Fn mkstemp ,
178usually to avoid the problems described above.
179Doing this correctly requires a good understanding of the code in question.
180.Pp
181For instance, code of this form:
182.Bd -literal -offset indent
183char sfn[19];
184FILE *sfp;
185
186strlcpy(sfn, "/tmp/ed.XXXXXXXXXX", sizeof(sfn));
187if (mktemp(sfn) == NULL || (sfp = fopen(sfn, "w+")) == NULL) {
188 warn("%s", sfn);
189 return (NULL);
190}
191return (sfp);
192.Ed
193.Pp
194should be rewritten like this:
195.Bd -literal -offset indent
196char sfn[19];
197FILE *sfp;
198int fd;
199
200strlcpy(sfn, "/tmp/ed.XXXXXXXXXX", sizeof(sfn));
201if ((fd = mkstemp(sfn)) == -1 ||
202 (sfp = fdopen(fd, "w+")) == NULL) {
203 if (fd != -1) {
204 unlink(sfn);
205 close(fd);
206 }
207 warn("%s", sfn);
208 return (NULL);
209}
210return (sfp);
211.Ed
212.Pp
213Often one will find code which uses
214.Fn mktemp
215very early on, perhaps to globally initialize the template nicely, but the
216code which calls
217.Xr open 2
218or
219.Xr fopen 3
220on that file name will occur much later.
221(In almost all cases, the use of
222.Xr fopen 3
223will mean that the flags
224.Dv O_CREAT
225|
226.Dv O_EXCL
227are not given to
228.Xr open 2 ,
229and thus a symbolic link race becomes possible, hence making
230necessary the use of
231.Xr fdopen 3
232as seen above.)
233Furthermore, one must be careful about code which opens, closes, and then
234re-opens the file in question.
235Finally, one must ensure that upon error the temporary file is
236removed correctly.
237.Pp
238There are also cases where modifying the code to use
239.Fn mktemp ,
240in concert with
241.Xr open 2
242using the flags
243.Dv O_CREAT
244|
245.Dv O_EXCL ,
246is better, as long as the code retries a new template if
247.Xr open 2
248fails with an
249.Va errno
250of
251.Er EEXIST .
252.Sh ERRORS
253The
254.Fn mktemp ,
255.Fn mkstemp ,
256.Fn mkostemp ,
257and
258.Fn mkdtemp
259functions may set
260.Va errno
261to one of the following values:
262.Bl -tag -width Er
263.It Bq Er EINVAL
264The
265.Ar template
266argument has fewer than six trailing
267.Em X Ns s .
268.It Bq Er EEXIST
269All file names tried are already in use.
270Consider appending more
271.Em X Ns s to the
272.Ar template .
273.El
274.Pp
275The
276.Fn mkstemps
277and
278.Fn mkostemps
279functions may set
280.Va errno
281to
282.Bl -tag -width Er
283.It Bq Er EINVAL
284The
285.Ar template
286argument length is less than
287.Ar suffixlen
288or it has fewer than six
289.Em X Ns s
290before the suffix.
291.It Bq Er EEXIST
292All file names tried are already in use.
293Consider appending more
294.Em X Ns s to the
295.Ar template .
296.El
297.Pp
298In addition, the
299.Fn mkostemp
300and
301.Fn mkostemps
302functions may also set
303.Va errno
304to
305.Bl -tag -width Er
306.It Bq Er EINVAL
307.Fa flags
308is invalid.
309.El
310.Pp
311The
312.Fn mktemp
313function may also set
314.Va errno
315to any value specified by the
316.Xr lstat 2
317function.
318.Pp
319The
320.Fn mkstemp ,
321.Fn mkostemp ,
322.Fn mkstemps ,
323and
324.Fn mkostemps
325functions may also set
326.Va errno
327to any value specified by the
328.Xr open 2
329function.
330.Pp
331The
332.Fn mkdtemp
333function may also set
334.Va errno
335to any value specified by the
336.Xr mkdir 2
337function.
338.Sh SEE ALSO
339.Xr chmod 2 ,
340.Xr lstat 2 ,
341.Xr mkdir 2 ,
342.Xr open 2 ,
343.Xr tempnam 3 ,
344.Xr tmpfile 3 ,
345.Xr tmpnam 3
346.Sh STANDARDS
347The
348.Fn mkdtemp
349and
350.Fn mkstemp
351functions conform to the
352.St -p1003.1-2008
353specification.
354The ability to specify more than six
355.Em X Ns s
356is an extension to that standard.
357The
358.Fn mkostemp
359function is expected to conform to a future revision of that standard.
360.Pp
361The
362.Fn mktemp
363function conforms to
364.St -p1003.1-2001 ;
365as of
366.St -p1003.1-2008
367it is no longer a part of the standard.
368.Pp
369The
370.Fn mkstemps ,
371.Fn mkostemps ,
372and
373.Fn mkdtemps
374functions are non-standard and should not be used if portability is required.
375.Sh HISTORY
376A
377.Fn mktemp
378function appeared in
379.At v7 .
380The
381.Fn mkdtemp
382function appeared in
383.Ox 2.2 .
384The
385.Fn mkstemp
386function appeared in
387.Bx 4.3 .
388The
389.Fn mkstemps
390function appeared in
391.Ox 2.3 .
392The
393.Fn mkostemp
394and
395.Fn mkostemps
396functions appeared in
397.Ox 5.7 .
398The
399.Fn mkdtemps
400function appeared in
401.Ox 7.5 .
402.Sh BUGS
403For
404.Fn mktemp
405there is an obvious race between file name selection and file
406creation and deletion: the program is typically written to call
407.Xr tmpnam 3 ,
408.Xr tempnam 3 ,
409or
410.Fn mktemp .
411Subsequently, the program calls
412.Xr open 2
413or
414.Xr fopen 3
415and erroneously opens a file (or symbolic link, FIFO or other
416device) that the attacker has created in the expected file location.
417Hence
418.Fn mkstemp
419is recommended, since it atomically creates the file.
420An attacker can guess the file names produced by
421.Fn mktemp .
422Whenever it is possible,
423.Fn mkstemp
424or
425.Fn mkdtemp
426should be used instead.
427.Pp
428For this reason,
429.Xr ld 1
430will output a warning message whenever it links code that uses
431.Fn mktemp .
diff --git a/src/lib/libc/stdlib/mktemp.c b/src/lib/libc/stdlib/mktemp.c
deleted file mode 100644
index 4dc06f74d1..0000000000
--- a/src/lib/libc/stdlib/mktemp.c
+++ /dev/null
@@ -1,48 +0,0 @@
1/* $OpenBSD: mktemp.c,v 1.2 2024/01/19 19:45:02 millert Exp $ */
2/*
3 * Copyright (c) 2024 Todd C. Miller
4 *
5 * Permission to use, copy, modify, and distribute this software for any
6 * purpose with or without fee is hereby granted, provided that the above
7 * copyright notice and this permission notice appear in all copies.
8 *
9 * THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES
10 * WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF
11 * MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR
12 * ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES
13 * WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN
14 * ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF
15 * OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE.
16 */
17
18#include <sys/stat.h>
19#include <errno.h>
20#include <stdlib.h>
21
22static int
23mktemp_cb(const char *path, int flags)
24{
25 struct stat sb;
26
27 if (lstat(path, &sb) == 0)
28 errno = EEXIST;
29 return (errno == ENOENT ? 0 : -1);
30}
31
32/* Also called via tmpnam(3) and tempnam(3). */
33char *
34_mktemp(char *path)
35{
36 if (__mktemp4(path, 0, 0, mktemp_cb) == 0)
37 return path;
38 return NULL;
39}
40
41__warn_references(mktemp,
42 "mktemp() possibly used unsafely; consider using mkstemp()");
43
44char *
45mktemp(char *path)
46{
47 return _mktemp(path);
48}
diff --git a/src/lib/libc/stdlib/mrand48.c b/src/lib/libc/stdlib/mrand48.c
deleted file mode 100644
index 3334e2ad2b..0000000000
--- a/src/lib/libc/stdlib/mrand48.c
+++ /dev/null
@@ -1,24 +0,0 @@
1/* $OpenBSD: mrand48.c,v 1.6 2015/08/27 04:33:31 guenther Exp $ */
2/*
3 * Copyright (c) 1993 Martin Birgmeier
4 * All rights reserved.
5 *
6 * You may redistribute unmodified or modified versions of this source
7 * code provided that the above copyright notice and this and the
8 * following conditions are retained.
9 *
10 * This software is provided ``as is'', and comes with no warranties
11 * of any kind. I shall in no event be liable for anything that happens
12 * to anyone/anything when using this software.
13 */
14
15#include "rand48.h"
16
17long
18mrand48(void)
19{
20 if (__rand48_deterministic == 0)
21 return (int)arc4random();
22 __dorand48(__rand48_seed);
23 return ((long) __rand48_seed[2] << 16) + (long) __rand48_seed[1];
24}
diff --git a/src/lib/libc/stdlib/nrand48.c b/src/lib/libc/stdlib/nrand48.c
deleted file mode 100644
index f1f548c3af..0000000000
--- a/src/lib/libc/stdlib/nrand48.c
+++ /dev/null
@@ -1,22 +0,0 @@
1/* $OpenBSD: nrand48.c,v 1.3 2005/08/08 08:05:37 espie Exp $ */
2/*
3 * Copyright (c) 1993 Martin Birgmeier
4 * All rights reserved.
5 *
6 * You may redistribute unmodified or modified versions of this source
7 * code provided that the above copyright notice and this and the
8 * following conditions are retained.
9 *
10 * This software is provided ``as is'', and comes with no warranties
11 * of any kind. I shall in no event be liable for anything that happens
12 * to anyone/anything when using this software.
13 */
14
15#include "rand48.h"
16
17long
18nrand48(unsigned short xseed[3])
19{
20 __dorand48(xseed);
21 return ((long) xseed[2] << 15) + ((long) xseed[1] >> 1);
22}
diff --git a/src/lib/libc/stdlib/posix_memalign.3 b/src/lib/libc/stdlib/posix_memalign.3
deleted file mode 100644
index fcd0a4cbdb..0000000000
--- a/src/lib/libc/stdlib/posix_memalign.3
+++ /dev/null
@@ -1,97 +0,0 @@
1.\" $OpenBSD: posix_memalign.3,v 1.4 2017/05/13 07:11:29 otto Exp $
2.\" Copyright (C) 2006 Jason Evans <jasone@FreeBSD.org>.
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(s), this list of conditions and the following disclaimer as
10.\" the first lines of this file unmodified other than the possible
11.\" addition of one or more copyright notices.
12.\" 2. Redistributions in binary form must reproduce the above copyright
13.\" notice(s), this list of conditions and the following disclaimer in
14.\" the documentation and/or other materials provided with the
15.\" distribution.
16.\"
17.\" THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDER(S) ``AS IS'' AND ANY
18.\" EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
19.\" IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
20.\" PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER(S) BE
21.\" LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
22.\" CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
23.\" SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR
24.\" BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY,
25.\" WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE
26.\" OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE,
27.\" EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
28.\"
29.\" $FreeBSD: src/lib/libc/stdlib/posix_memalign.3,v 1.3 2007/03/28 04:32:51 jasone Exp $
30.\"
31.Dd $Mdocdate: May 13 2017 $
32.Dt POSIX_MEMALIGN 3
33.Os
34.Sh NAME
35.Nm posix_memalign
36.Nd aligned memory allocation
37.Sh SYNOPSIS
38.In stdlib.h
39.Ft int
40.Fn posix_memalign "void **ptr" "size_t alignment" "size_t size"
41.Sh DESCRIPTION
42The
43.Fn posix_memalign
44function allocates
45.Fa size
46bytes of memory such that the allocation's base address is a multiple of
47.Fa alignment ,
48and returns the allocation in the value pointed to by
49.Fa ptr .
50.Pp
51The requested
52.Fa alignment
53must be a power of 2 at least as large as
54.Fn sizeof "void *" .
55.Pp
56Memory that is allocated via
57.Fn posix_memalign
58can be used as an argument in subsequent calls to
59.Xr realloc 3 ,
60.Xr reallocarray 3
61and
62.Xr free 3 ,
63but not
64.Xr recallocarray 3
65and
66.Xr freezero 3 .
67.Sh RETURN VALUES
68The
69.Fn posix_memalign
70function returns the value 0 if successful; otherwise it returns an error value.
71.Sh ERRORS
72The
73.Fn posix_memalign
74function will fail if:
75.Bl -tag -width Er
76.It Bq Er EINVAL
77The
78.Fa alignment
79parameter is not a power of 2 at least as large as
80.Fn sizeof "void *" .
81.It Bq Er ENOMEM
82Memory allocation error.
83.El
84.Sh SEE ALSO
85.Xr free 3 ,
86.Xr malloc 3 ,
87.Xr realloc 3
88.Sh STANDARDS
89The
90.Fn posix_memalign
91function conforms to
92.St -p1003.1-2001 .
93.Sh HISTORY
94The
95.Fn posix_memalign
96function first appeared in
97.Ox 4.8 .
diff --git a/src/lib/libc/stdlib/posix_openpt.3 b/src/lib/libc/stdlib/posix_openpt.3
deleted file mode 100644
index b55e1be750..0000000000
--- a/src/lib/libc/stdlib/posix_openpt.3
+++ /dev/null
@@ -1,102 +0,0 @@
1.\" $OpenBSD: posix_openpt.3,v 1.4 2019/01/25 00:19:25 millert Exp $
2.\"
3.\" Copyright (c) 2012 Todd C. Miller <millert@openbsd.org>
4.\"
5.\" Permission to use, copy, modify, and distribute this software for any
6.\" purpose with or without fee is hereby granted, provided that the above
7.\" copyright notice and this permission notice appear in all copies.
8.\"
9.\" THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES
10.\" WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF
11.\" MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR
12.\" ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES
13.\" WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN
14.\" ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF
15.\" OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE.
16.\"
17.Dd $Mdocdate: January 25 2019 $
18.Dt POSIX_OPENPT 3
19.Os
20.Sh NAME
21.Nm posix_openpt
22.Nd open a pseudo-terminal device
23.Sh SYNOPSIS
24.In stdlib.h
25.In fcntl.h
26.Ft int
27.Fn posix_openpt "int oflag"
28.Sh DESCRIPTION
29The
30.Fn posix_openpt
31function finds the next available pseudo-terminal and returns an open
32file descriptor for its master device.
33The path name of the slave device may be determined via the
34.Fn ptsname
35function.
36Note that the
37.Fn unlockpt
38and
39.Fn grantpt
40functions should be called before opening the slave device.
41.Pp
42The
43.Ar oflag
44argument is formed by bitwise-inclusive
45.Tn OR Ns 'ing
46the following values defined in
47.In fcntl.h :
48.Bl -tag -width O_NOCTTY -offset indent
49.It Dv O_RDWR
50Open for reading and writing.
51.It Dv O_NOCTTY
52Prevent the device from being made the controlling terminal for the session.
53This flag has no effect on
54.Ox
55and is included for compatibility with other systems.
56.El
57.Pp
58The
59.Dv O_RDWR
60flag must be specified in
61.Fa oflag .
62If
63.Fa oflag
64contains values other than those listed above,
65.Fn posix_openpt
66will return an error.
67.Sh RETURN VALUES
68If successful,
69.Fn posix_openpt
70returns a non-negative integer, the file descriptor for the
71pseudo-terminal master device.
72Otherwise, a value of \-1 is returned and
73.Va errno
74is set to indicate the error.
75.Sh ERRORS
76The
77.Fn posix_openpt
78function will fail if:
79.Bl -tag -width Er
80.It Bq Er EMFILE
81The per-process descriptor table is full.
82.It Bq Er ENFILE
83The system file table is full.
84.It Bq Er EINVAL
85The value of
86.Fa oflag
87is not valid.
88.El
89.Sh SEE ALSO
90.Xr ptsname 3 ,
91.Xr pty 4 ,
92.Xr tty 4
93.Sh STANDARDS
94The
95.Fn posix_openpt
96function conforms to
97.St -p1003.1-2001 .
98.Sh HISTORY
99The
100.Fn posix_openpt
101function appeared in
102.Ox 5.3 .
diff --git a/src/lib/libc/stdlib/posix_pty.c b/src/lib/libc/stdlib/posix_pty.c
deleted file mode 100644
index e45ab6ebd0..0000000000
--- a/src/lib/libc/stdlib/posix_pty.c
+++ /dev/null
@@ -1,120 +0,0 @@
1/* $OpenBSD: posix_pty.c,v 1.3 2019/01/25 00:19:25 millert Exp $ */
2
3/*
4 * Copyright (c) 2012 Todd C. Miller <millert@openbsd.org>
5 *
6 * Permission to use, copy, modify, and distribute this software for any
7 * purpose with or without fee is hereby granted, provided that the above
8 * copyright notice and this permission notice appear in all copies.
9 *
10 * THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES
11 * WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF
12 * MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR
13 * ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES
14 * WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN
15 * ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF
16 * OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE.
17 */
18
19#include <sys/types.h>
20#include <sys/ioctl.h>
21#include <sys/stat.h>
22#include <sys/tty.h>
23#include <errno.h>
24#include <fcntl.h>
25#include <paths.h>
26#include <stddef.h>
27#include <stdlib.h>
28#include <string.h>
29#include <unistd.h>
30
31int
32posix_openpt(int oflag)
33{
34 struct ptmget ptm;
35 int fd, mfd = -1;
36
37 /* User must specify O_RDWR in oflag. */
38 if ((oflag & O_ACCMODE) != O_RDWR ||
39 (oflag & ~(O_ACCMODE | O_NOCTTY)) != 0) {
40 errno = EINVAL;
41 return -1;
42 }
43
44 /* Get pty master and slave (this API only uses the master). */
45 fd = open(PATH_PTMDEV, O_RDWR);
46 if (fd != -1) {
47 if (ioctl(fd, PTMGET, &ptm) != -1) {
48 close(ptm.sfd);
49 mfd = ptm.cfd;
50 }
51 close(fd);
52 }
53
54 return mfd;
55}
56
57/*
58 * Look up the name of the specified pty master fd.
59 * Note that the name returned does *not* include the /dev/ prefix.
60 * Returns the name on success and NULL on error, setting errno.
61 */
62static const char *
63ptmname(int mfd)
64{
65 struct stat sb;
66 const char *name;
67
68 /* Make sure it is a pty master. */
69 if (fstat(mfd, &sb) != 0)
70 return NULL;
71 if (!S_ISCHR(sb.st_mode)) {
72 errno = EINVAL;
73 return NULL;
74 }
75 name = devname(sb.st_rdev, S_IFCHR);
76 if (strncmp(name, "pty", 3) != 0) {
77 errno = EINVAL;
78 return NULL;
79 }
80 return name;
81}
82
83/*
84 * The PTMGET ioctl handles the mode and owner for us.
85 */
86int
87grantpt(int mfd)
88{
89 return ptmname(mfd) ? 0 : -1;
90}
91
92/*
93 * The PTMGET ioctl unlocks the pty master and slave for us.
94 */
95int
96unlockpt(int mfd)
97{
98 return ptmname(mfd) ? 0 : -1;
99}
100
101/*
102 * Look up the path of the slave pty that corresponds to the master fd.
103 * Returns the path if successful or NULL on error.
104 */
105char *
106ptsname(int mfd)
107{
108 const char *master;
109 static char slave[sizeof(((struct ptmget *)NULL)->sn)];
110
111 if ((master = ptmname(mfd)) == NULL)
112 return NULL;
113
114 /* Add /dev/ prefix and convert "pty" to "tty". */
115 strlcpy(slave, _PATH_DEV, sizeof(slave));
116 strlcat(slave, master, sizeof(slave));
117 slave[sizeof(_PATH_DEV) - 1] = 't';
118
119 return slave;
120}
diff --git a/src/lib/libc/stdlib/ptsname.3 b/src/lib/libc/stdlib/ptsname.3
deleted file mode 100644
index 98705528f5..0000000000
--- a/src/lib/libc/stdlib/ptsname.3
+++ /dev/null
@@ -1,160 +0,0 @@
1.\" $OpenBSD: ptsname.3,v 1.2 2012/12/04 18:42:16 millert Exp $
2.\"
3.\" Copyright (c) 2002 The FreeBSD Project, Inc.
4.\" All rights reserved.
5.\"
6.\" This software includes code contributed to the FreeBSD Project
7.\" by Ryan Younce of North Carolina State University.
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. Neither the name of the FreeBSD Project nor the names of its
18.\" contributors may be used to endorse or promote products derived from
19.\" this software without specific prior written permission.
20.\"
21.\" THIS SOFTWARE IS PROVIDED BY THE FREEBSD PROJECT AND CONTRIBUTORS
22.\" ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
23.\" LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A
24.\" PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE FREEBSD PROJECT
25.\" OR ITS CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
26.\" SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED
27.\" TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR
28.\" PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF
29.\" LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING
30.\" NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
31.\" SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
32.\"
33.\" $FreeBSD: head/lib/libc/stdlib/ptsname.3 240412 2012-09-12 17:54:09Z emaste $
34.\"
35.Dd $Mdocdate: December 4 2012 $
36.Dt PTSNAME 3
37.Os
38.Sh NAME
39.Nm grantpt ,
40.Nm ptsname ,
41.Nm unlockpt
42.Nd pseudo-terminal access functions
43.Sh SYNOPSIS
44.In stdlib.h
45.Ft int
46.Fn grantpt "int fildes"
47.Ft "char *"
48.Fn ptsname "int fildes"
49.Ft int
50.Fn unlockpt "int fildes"
51.Sh DESCRIPTION
52The
53.Fn grantpt ,
54.Fn ptsname ,
55and
56.Fn unlockpt
57functions allow access to pseudo-terminal devices.
58These three functions accept a file descriptor that references the
59master half of a pseudo-terminal pair.
60This file descriptor is created with
61.Xr posix_openpt 3 .
62.Pp
63The
64.Fn grantpt
65function is used to establish ownership and permissions
66of the slave device counterpart to the master device
67specified with
68.Fa fildes .
69The slave device's ownership is set to the real user ID
70of the calling process, and the permissions are set to
71user readable-writable and group writable.
72The group owner of the slave device is also set to the
73group
74.Dq Li tty .
75.Pp
76The
77.Fn ptsname
78function returns the full path name of the slave device
79counterpart to the master device specified with
80.Fa fildes .
81This value can be used
82to subsequently open the appropriate slave after
83.Xr posix_openpt 3
84and
85.Fn grantpt
86have been called.
87.Pp
88The
89.Fn unlockpt
90function clears the lock held on the pseudo-terminal pair
91for the master device specified with
92.Fa fildes .
93.Sh RETURN VALUES
94.Rv -std grantpt unlockpt
95.Pp
96The
97.Fn ptsname
98function returns a pointer to the name
99of the slave device on success; otherwise a
100.Dv NULL
101pointer is returned.
102.Sh ERRORS
103The
104.Fn grantpt ,
105.Fn ptsname
106and
107.Fn unlockpt
108functions may fail and set
109.Va errno
110to:
111.Bl -tag -width Er
112.It Bq Er EBADF
113.Fa fildes
114is not a valid open file descriptor.
115.It Bq Er EINVAL
116.Fa fildes
117is not a master pseudo-terminal device.
118.El
119.Pp
120In addition, the
121.Fn grantpt
122function may set
123.Va errno
124to:
125.Bl -tag -width Er
126.It Bq Er EACCES
127The slave pseudo-terminal device could not be accessed.
128.El
129.Sh SEE ALSO
130.Xr posix_openpt 3 ,
131.Xr pty 4 ,
132.Xr tty 4
133.Sh STANDARDS
134The
135.Fn ptsname
136function conforms to
137.St -p1003.1-2008 .
138.Pp
139This implementation of
140.Fn grantpt
141and
142.Fn unlockpt
143does not conform to
144.St -p1003.1-2008 ,
145because it depends on
146.Xr posix_openpt 3
147to create the pseudo-terminal device with proper permissions in place.
148It only validates whether
149.Fa fildes
150is a valid pseudo-terminal master device.
151Future revisions of the specification will likely allow this behaviour,
152as stated by the Austin Group.
153.Sh HISTORY
154The
155.Fn grantpt ,
156.Fn ptsname
157and
158.Fn unlockpt
159functions appeared in
160.Ox 5.3 .
diff --git a/src/lib/libc/stdlib/qsort.3 b/src/lib/libc/stdlib/qsort.3
deleted file mode 100644
index 4c0cddaccb..0000000000
--- a/src/lib/libc/stdlib/qsort.3
+++ /dev/null
@@ -1,276 +0,0 @@
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. 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.\" $OpenBSD: qsort.3,v 1.27 2020/02/08 01:09:57 jsg Exp $
33.\"
34.Dd $Mdocdate: February 8 2020 $
35.Dt QSORT 3
36.Os
37.Sh NAME
38.Nm qsort ,
39.Nm heapsort ,
40.Nm mergesort
41.Nd sort functions
42.Sh SYNOPSIS
43.In stdlib.h
44.Ft void
45.Fn qsort "void *base" "size_t nmemb" "size_t size" "int (*compar)(const void *, const void *)"
46.Ft int
47.Fn heapsort "void *base" "size_t nmemb" "size_t size" "int (*compar)(const void *, const void *)"
48.Ft int
49.Fn mergesort "void *base" "size_t nmemb" "size_t size" "int (*compar)(const void *, const void *)"
50.Sh DESCRIPTION
51The
52.Fn qsort
53function is a modified partition-exchange sort, or quicksort.
54The
55.Fn heapsort
56function is a modified selection sort.
57The
58.Fn mergesort
59function is a modified merge sort with exponential search
60intended for sorting data with pre-existing order.
61.Pp
62The
63.Fn qsort
64and
65.Fn heapsort
66functions sort an array of
67.Fa nmemb
68objects, the initial member of which is pointed to by
69.Fa base .
70The size of each object is specified by
71.Fa size .
72.Fn mergesort
73behaves similarly, but
74.Em requires
75that
76.Fa size
77be greater than
78.Dq "sizeof(void *) / 2" .
79.Pp
80The contents of the array
81.Fa base
82are sorted in ascending order according to
83a comparison function pointed to by
84.Fa compar ,
85which requires two arguments pointing to the objects being
86compared.
87.Pp
88The comparison function must return an int less than, equal to, or
89greater than zero if the first argument is considered to be respectively
90less than, equal to, or greater than the second.
91.Pp
92The functions
93.Fn qsort
94and
95.Fn heapsort
96are
97.Em not
98stable, that is, if two members compare as equal, their order in
99the sorted array is undefined.
100The function
101.Fn mergesort
102is stable.
103.Pp
104The
105.Fn qsort
106function is an implementation of C.A.R. Hoare's
107.Dq quicksort
108algorithm,
109a variant of partition-exchange sorting; in particular, see D.E. Knuth's
110Algorithm Q.
111.Fn qsort
112takes O N lg N average time.
113This implementation uses median selection to avoid its
114O N**2 worst-case behavior and will fall back to
115.Fn heapsort
116if the recursion depth exceeds 2 lg N.
117.Pp
118The
119.Fn heapsort
120function is an implementation of J.W.J. William's
121.Dq heapsort
122algorithm,
123a variant of selection sorting; in particular, see D.E. Knuth's Algorithm H.
124.Fn heapsort
125takes O N lg N worst-case time.
126This implementation of
127.Fn heapsort
128is implemented without recursive function calls.
129.Pp
130The function
131.Fn mergesort
132requires additional memory of size
133.Fa nmemb *
134.Fa size
135bytes; it should be used only when space is not at a premium.
136.Fn mergesort
137is optimized for data with pre-existing order; its worst case
138time is O N lg N; its best case is O N.
139.Pp
140Normally,
141.Fn qsort
142is faster than
143.Fn mergesort ,
144which is faster than
145.Fn heapsort .
146Memory availability and pre-existing order in the data can make this untrue.
147.Sh RETURN VALUES
148.Rv -std heapsort mergesort
149.Sh EXAMPLES
150.Bd -literal
151#include <stdio.h>
152#include <stdlib.h>
153#include <string.h>
154
155char *array[] = { "XX", "YYY", "Z" };
156#define N (sizeof(array) / sizeof(array[0]))
157
158int
159cmp(const void *a, const void *b)
160{
161 /*
162 * a and b point to elements of the array.
163 * Cast and dereference to obtain the actual elements,
164 * which are also pointers in this case.
165 */
166 size_t lena = strlen(*(const char **)a);
167 size_t lenb = strlen(*(const char **)b);
168 /*
169 * Do not subtract the lengths. The difference between values
170 * cannot be represented by an int.
171 */
172 return lena < lenb ? -1 : lena > lenb;
173}
174
175int
176main()
177{
178 size_t i;
179
180 qsort(array, N, sizeof(array[0]), cmp);
181 for (i = 0; i < N; i++)
182 printf("%s\en", array[i]);
183}
184.Ed
185.Pp
186It is almost always an error to use subtraction to compute the return value
187of the comparison function.
188.Sh ERRORS
189The
190.Fn heapsort
191and
192.Fn mergesort
193functions succeed unless:
194.Bl -tag -width Er
195.It Bq Er EINVAL
196The
197.Fa size
198argument is zero, or the
199.Fa size
200argument to
201.Fn mergesort
202is less than
203.Dq "sizeof(void *) / 2" .
204.It Bq Er ENOMEM
205.Fn heapsort
206or
207.Fn mergesort
208were unable to allocate memory.
209.El
210.Sh SEE ALSO
211.Xr sort 1 ,
212.Xr radixsort 3
213.Rs
214.%A Hoare, C.A.R.
215.%D 1962
216.%T "Quicksort"
217.%J "The Computer Journal"
218.%V 5:1
219.%P pp. 10-15
220.Re
221.Rs
222.%A Williams, J.W.J
223.%D 1964
224.%T "Heapsort"
225.%J "Communications of the ACM"
226.%V 7:1
227.%P pp. 347\-348
228.Re
229.Rs
230.%A Knuth, D.E.
231.%D 1968
232.%B "The Art of Computer Programming"
233.%V Vol. 3
234.%T "Sorting and Searching"
235.%P pp. 114\-123, 145\-149
236.Re
237.Rs
238.%A McIlroy, P.M.
239.%T "Optimistic Sorting and Information Theoretic Complexity"
240.%J "Fourth Annual ACM-SIAM Symposium on Discrete Algorithms"
241.%P pp. 467\-464
242.%D January 1993
243.Re
244.Rs
245.%A Bentley, J.L.
246.%A McIlroy, M.D.
247.%T "Engineering a Sort Function"
248.%J "Software \- Practice and Experience"
249.%V Vol. 23(11)
250.%P pp. 1249\-1265
251.%D November 1993
252.Re
253.Rs
254.%A Musser, D.
255.%T "Introspective Sorting and Selection Algorithms"
256.%J "Software \- Practice and Experience"
257.%V Vol. 27(8)
258.%P pp. 983\-993
259.%D August 1997
260.Re
261.Sh STANDARDS
262Previous versions of
263.Fn qsort
264did not permit the comparison routine itself to call
265.Fn qsort .
266This is no longer true.
267.Pp
268The
269.Fn qsort
270function conforms to
271.St -ansiC .
272.Sh HISTORY
273A
274.Fn qsort
275function first appeared in
276.At v2 .
diff --git a/src/lib/libc/stdlib/qsort.c b/src/lib/libc/stdlib/qsort.c
deleted file mode 100644
index ca73e67f29..0000000000
--- a/src/lib/libc/stdlib/qsort.c
+++ /dev/null
@@ -1,238 +0,0 @@
1/* $OpenBSD: qsort.c,v 1.18 2017/05/30 14:54:09 millert Exp $ */
2/*-
3 * Copyright (c) 1992, 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. Neither the name of the University nor the names of its contributors
15 * may be used to endorse or promote products derived from this software
16 * without specific prior written permission.
17 *
18 * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
19 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
20 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
21 * ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
22 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
23 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
24 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
25 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
26 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
27 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
28 * SUCH DAMAGE.
29 */
30
31#include <sys/types.h>
32#include <stdlib.h>
33
34static __inline char *med3(char *, char *, char *, int (*)(const void *, const void *));
35static __inline void swapfunc(char *, char *, size_t, int);
36
37#define min(a, b) (a) < (b) ? a : b
38
39/*
40 * Qsort routine from Bentley & McIlroy's "Engineering a Sort Function".
41 *
42 * This version differs from Bentley & McIlroy in the following ways:
43 * 1. The partition value is swapped into a[0] instead of being
44 * stored out of line.
45 *
46 * 2. The swap function can swap 32-bit aligned elements on 64-bit
47 * platforms instead of swapping them as byte-aligned.
48 *
49 * 3. It uses David Musser's introsort algorithm to fall back to
50 * heapsort(3) when the recursion depth reaches 2*lg(n + 1).
51 * This avoids quicksort's quadratic behavior for pathological
52 * input without appreciably changing the average run time.
53 *
54 * 4. Tail recursion is eliminated when sorting the larger of two
55 * subpartitions to save stack space.
56 */
57#define SWAPTYPE_BYTEV 1
58#define SWAPTYPE_INTV 2
59#define SWAPTYPE_LONGV 3
60#define SWAPTYPE_INT 4
61#define SWAPTYPE_LONG 5
62
63#define TYPE_ALIGNED(TYPE, a, es) \
64 (((char *)a - (char *)0) % sizeof(TYPE) == 0 && es % sizeof(TYPE) == 0)
65
66#define swapcode(TYPE, parmi, parmj, n) { \
67 size_t i = (n) / sizeof (TYPE); \
68 TYPE *pi = (TYPE *) (parmi); \
69 TYPE *pj = (TYPE *) (parmj); \
70 do { \
71 TYPE t = *pi; \
72 *pi++ = *pj; \
73 *pj++ = t; \
74 } while (--i > 0); \
75}
76
77static __inline void
78swapfunc(char *a, char *b, size_t n, int swaptype)
79{
80 switch (swaptype) {
81 case SWAPTYPE_INT:
82 case SWAPTYPE_INTV:
83 swapcode(int, a, b, n);
84 break;
85 case SWAPTYPE_LONG:
86 case SWAPTYPE_LONGV:
87 swapcode(long, a, b, n);
88 break;
89 default:
90 swapcode(char, a, b, n);
91 break;
92 }
93}
94
95#define swap(a, b) do { \
96 switch (swaptype) { \
97 case SWAPTYPE_INT: { \
98 int t = *(int *)(a); \
99 *(int *)(a) = *(int *)(b); \
100 *(int *)(b) = t; \
101 break; \
102 } \
103 case SWAPTYPE_LONG: { \
104 long t = *(long *)(a); \
105 *(long *)(a) = *(long *)(b); \
106 *(long *)(b) = t; \
107 break; \
108 } \
109 default: \
110 swapfunc(a, b, es, swaptype); \
111 } \
112} while (0)
113
114#define vecswap(a, b, n) if ((n) > 0) swapfunc(a, b, n, swaptype)
115
116static __inline char *
117med3(char *a, char *b, char *c, int (*cmp)(const void *, const void *))
118{
119 return cmp(a, b) < 0 ?
120 (cmp(b, c) < 0 ? b : (cmp(a, c) < 0 ? c : a ))
121 :(cmp(b, c) > 0 ? b : (cmp(a, c) < 0 ? a : c ));
122}
123
124static void
125introsort(char *a, size_t n, size_t es, size_t maxdepth, int swaptype,
126 int (*cmp)(const void *, const void *))
127{
128 char *pa, *pb, *pc, *pd, *pl, *pm, *pn;
129 int cmp_result;
130 size_t r, s;
131
132loop: if (n < 7) {
133 for (pm = a + es; pm < a + n * es; pm += es)
134 for (pl = pm; pl > a && cmp(pl - es, pl) > 0;
135 pl -= es)
136 swap(pl, pl - es);
137 return;
138 }
139 if (maxdepth == 0) {
140 if (heapsort(a, n, es, cmp) == 0)
141 return;
142 }
143 maxdepth--;
144 pm = a + (n / 2) * es;
145 if (n > 7) {
146 pl = a;
147 pn = a + (n - 1) * es;
148 if (n > 40) {
149 s = (n / 8) * es;
150 pl = med3(pl, pl + s, pl + 2 * s, cmp);
151 pm = med3(pm - s, pm, pm + s, cmp);
152 pn = med3(pn - 2 * s, pn - s, pn, cmp);
153 }
154 pm = med3(pl, pm, pn, cmp);
155 }
156 swap(a, pm);
157 pa = pb = a + es;
158 pc = pd = a + (n - 1) * es;
159 for (;;) {
160 while (pb <= pc && (cmp_result = cmp(pb, a)) <= 0) {
161 if (cmp_result == 0) {
162 swap(pa, pb);
163 pa += es;
164 }
165 pb += es;
166 }
167 while (pb <= pc && (cmp_result = cmp(pc, a)) >= 0) {
168 if (cmp_result == 0) {
169 swap(pc, pd);
170 pd -= es;
171 }
172 pc -= es;
173 }
174 if (pb > pc)
175 break;
176 swap(pb, pc);
177 pb += es;
178 pc -= es;
179 }
180
181 pn = a + n * es;
182 r = min(pa - a, pb - pa);
183 vecswap(a, pb - r, r);
184 r = min(pd - pc, pn - pd - es);
185 vecswap(pb, pn - r, r);
186 /*
187 * To save stack space we sort the smaller side of the partition first
188 * using recursion and eliminate tail recursion for the larger side.
189 */
190 r = pb - pa;
191 s = pd - pc;
192 if (r < s) {
193 /* Recurse for 1st side, iterate for 2nd side. */
194 if (s > es) {
195 if (r > es) {
196 introsort(a, r / es, es, maxdepth,
197 swaptype, cmp);
198 }
199 a = pn - s;
200 n = s / es;
201 goto loop;
202 }
203 } else {
204 /* Recurse for 2nd side, iterate for 1st side. */
205 if (r > es) {
206 if (s > es) {
207 introsort(pn - s, s / es, es, maxdepth,
208 swaptype, cmp);
209 }
210 n = r / es;
211 goto loop;
212 }
213 }
214}
215
216void
217qsort(void *a, size_t n, size_t es, int (*cmp)(const void *, const void *))
218{
219 size_t i, maxdepth = 0;
220 int swaptype;
221
222 /* Approximate 2*ceil(lg(n + 1)) */
223 for (i = n; i > 0; i >>= 1)
224 maxdepth++;
225 maxdepth *= 2;
226
227 if (TYPE_ALIGNED(long, a, es))
228 swaptype = es == sizeof(long) ? SWAPTYPE_LONG : SWAPTYPE_LONGV;
229 else if (sizeof(int) != sizeof(long) && TYPE_ALIGNED(int, a, es))
230 swaptype = es == sizeof(int) ? SWAPTYPE_INT : SWAPTYPE_INTV;
231 else
232 swaptype = SWAPTYPE_BYTEV;
233
234 introsort(a, n, es, maxdepth, swaptype, cmp);
235
236}
237
238DEF_STRONG(qsort);
diff --git a/src/lib/libc/stdlib/radixsort.3 b/src/lib/libc/stdlib/radixsort.3
deleted file mode 100644
index 7290142a9c..0000000000
--- a/src/lib/libc/stdlib/radixsort.3
+++ /dev/null
@@ -1,151 +0,0 @@
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. Neither the name of the University nor the names of its contributors
13.\" may be used to endorse or promote products derived from this software
14.\" without specific prior written permission.
15.\"
16.\" THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
17.\" ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
18.\" IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
19.\" ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
20.\" FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
21.\" DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
22.\" OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
23.\" HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
24.\" LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
25.\" OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
26.\" SUCH DAMAGE.
27.\"
28.\" $OpenBSD: radixsort.3,v 1.14 2022/08/04 06:20:24 jsg Exp $
29.\"
30.Dd $Mdocdate: August 4 2022 $
31.Dt RADIXSORT 3
32.Os
33.Sh NAME
34.Nm radixsort ,
35.Nm sradixsort
36.Nd radix sort
37.Sh SYNOPSIS
38.In limits.h
39.In stdlib.h
40.Ft int
41.Fn radixsort "const u_char **base" "int nmemb" "const u_char *table" "u_int endbyte"
42.Ft int
43.Fn sradixsort "const u_char **base" "int nmemb" "const u_char *table" "u_int endbyte"
44.Sh DESCRIPTION
45The
46.Fn radixsort
47and
48.Fn sradixsort
49functions are implementations of radix sort.
50.Pp
51These functions sort an array of
52.Fa nmemb
53pointers to byte strings.
54The initial member is referenced by
55.Fa base .
56The byte strings may contain any values; the end of each string
57is denoted by the user-specified value
58.Fa endbyte .
59.Pp
60Applications may specify a sort order by providing the
61.Fa table
62argument.
63If non-null,
64.Fa table
65must reference an array of
66.Dv UCHAR_MAX
67+ 1 bytes which contains the sort weight of each possible byte value.
68The end-of-string byte must have a sort weight of 0 or 255
69(for sorting in reverse order).
70More than one byte may have the same sort weight.
71The
72.Fa table
73argument is useful for applications which wish to sort different characters
74equally; for example, providing a table with the same weights
75for A\-Z as for a\-z will result in a case-insensitive sort.
76If
77.Fa table
78is
79.Dv NULL ,
80the contents of the array are sorted in ascending order according to the
81ASCII order of the byte strings they reference and
82.Fa endbyte
83has a sorting weight of 0.
84.Pp
85The
86.Fn sradixsort
87function is stable; that is, if two elements compare as equal, their
88order in the sorted array is unchanged.
89The
90.Fn sradixsort
91function uses additional memory sufficient to hold
92.Fa nmemb
93pointers.
94.Pp
95The
96.Fn radixsort
97function is not stable, but uses no additional memory.
98.Pp
99These functions are variants of most-significant-byte radix sorting; in
100particular, see D.E. Knuth's Algorithm R and section 5.2.5, exercise 10.
101They take linear time relative to the number of bytes in the strings.
102.Sh RETURN VALUES
103.Rv -std
104.Sh ERRORS
105.Bl -tag -width Er
106.It Bq Er EINVAL
107The value of the
108.Fa endbyte
109element of
110.Fa table
111is not 0 or 255.
112.El
113.Pp
114Additionally, the
115.Fn sradixsort
116function may fail and set
117.Va errno
118for any of the errors specified for the library routine
119.Xr malloc 3 .
120.Sh SEE ALSO
121.Xr sort 1 ,
122.Xr qsort 3
123.Rs
124.%A Knuth, D.E.
125.%D 1968
126.%B "The Art of Computer Programming"
127.%T "Sorting and Searching"
128.%V Vol. 3
129.%P pp. 170-178
130.Re
131.Rs
132.%A Paige, R.
133.%D 1987
134.%T "Three Partition Refinement Algorithms"
135.%J "SIAM J. Comput."
136.%V Vol. 16
137.%N No. 6
138.Re
139.Rs
140.%A McIlroy, P.
141.%D 1993
142.%B "Engineering Radix Sort"
143.%T "Computing Systems"
144.%V Vol. 6:1
145.%P pp. 5-27
146.Re
147.Sh HISTORY
148The
149.Fn radixsort
150function first appeared in
151.Bx 4.3 Net/2 .
diff --git a/src/lib/libc/stdlib/radixsort.c b/src/lib/libc/stdlib/radixsort.c
deleted file mode 100644
index 49d03b52d5..0000000000
--- a/src/lib/libc/stdlib/radixsort.c
+++ /dev/null
@@ -1,294 +0,0 @@
1/* $OpenBSD: radixsort.c,v 1.9 2007/09/02 15:19:17 deraadt Exp $ */
2/*-
3 * Copyright (c) 1990, 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 * Peter McIlroy and by Dan Bernstein at New York University,
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. 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/*
35 * Radixsort routines.
36 *
37 * Program r_sort_a() is unstable but uses O(logN) extra memory for a stack.
38 * Use radixsort(a, n, trace, endchar) for this case.
39 *
40 * For stable sorting (using N extra pointers) use sradixsort(), which calls
41 * r_sort_b().
42 *
43 * For a description of this code, see D. McIlroy, P. McIlroy, K. Bostic,
44 * "Engineering Radix Sort".
45 */
46
47#include <sys/types.h>
48#include <stdlib.h>
49#include <errno.h>
50
51typedef struct {
52 const u_char **sa;
53 int sn, si;
54} stack;
55
56static __inline void simplesort
57(const u_char **, int, int, const u_char *, u_int);
58static void r_sort_a(const u_char **, int, int, const u_char *, u_int);
59static void r_sort_b(const u_char **,
60 const u_char **, int, int, const u_char *, u_int);
61
62#define THRESHOLD 20 /* Divert to simplesort(). */
63#define SIZE 512 /* Default stack size. */
64
65#define SETUP { \
66 if (tab == NULL) { \
67 tr = tr0; \
68 for (c = 0; c < endch; c++) \
69 tr0[c] = c + 1; \
70 tr0[c] = 0; \
71 for (c++; c < 256; c++) \
72 tr0[c] = c; \
73 endch = 0; \
74 } else { \
75 endch = tab[endch]; \
76 tr = tab; \
77 if (endch != 0 && endch != 255) { \
78 errno = EINVAL; \
79 return (-1); \
80 } \
81 } \
82}
83
84int
85radixsort(const u_char **a, int n, const u_char *tab, u_int endch)
86{
87 const u_char *tr;
88 int c;
89 u_char tr0[256];
90
91 SETUP;
92 r_sort_a(a, n, 0, tr, endch);
93 return (0);
94}
95
96int
97sradixsort(const u_char **a, int n, const u_char *tab, u_int endch)
98{
99 const u_char *tr, **ta;
100 int c;
101 u_char tr0[256];
102
103 SETUP;
104 if (n < THRESHOLD)
105 simplesort(a, n, 0, tr, endch);
106 else {
107 if ((ta = calloc(n, sizeof(a))) == NULL)
108 return (-1);
109 r_sort_b(a, ta, n, 0, tr, endch);
110 free(ta);
111 }
112 return (0);
113}
114
115#define empty(s) (s >= sp)
116#define pop(a, n, i) a = (--sp)->sa, n = sp->sn, i = sp->si
117#define push(a, n, i) sp->sa = a, sp->sn = n, (sp++)->si = i
118#define swap(a, b, t) t = a, a = b, b = t
119
120/* Unstable, in-place sort. */
121void
122r_sort_a(const u_char **a, int n, int i, const u_char *tr, u_int endch)
123{
124 static int count[256], nc, bmin;
125 int c;
126 const u_char **ak, *r;
127 stack s[SIZE], *sp, *sp0, *sp1, temp;
128 int *cp, bigc;
129 const u_char **an, *t, **aj, **top[256];
130
131 /* Set up stack. */
132 sp = s;
133 push(a, n, i);
134 while (!empty(s)) {
135 pop(a, n, i);
136 if (n < THRESHOLD) {
137 simplesort(a, n, i, tr, endch);
138 continue;
139 }
140 an = a + n;
141
142 /* Make character histogram. */
143 if (nc == 0) {
144 bmin = 255; /* First occupied bin, excluding eos. */
145 for (ak = a; ak < an;) {
146 c = tr[(*ak++)[i]];
147 if (++count[c] == 1 && c != endch) {
148 if (c < bmin)
149 bmin = c;
150 nc++;
151 }
152 }
153 if (sp + nc > s + SIZE) { /* Get more stack. */
154 r_sort_a(a, n, i, tr, endch);
155 continue;
156 }
157 }
158
159 /*
160 * Set top[]; push incompletely sorted bins onto stack.
161 * top[] = pointers to last out-of-place element in bins.
162 * count[] = counts of elements in bins.
163 * Before permuting: top[c-1] + count[c] = top[c];
164 * during deal: top[c] counts down to top[c-1].
165 */
166 sp0 = sp1 = sp; /* Stack position of biggest bin. */
167 bigc = 2; /* Size of biggest bin. */
168 if (endch == 0) /* Special case: set top[eos]. */
169 top[0] = ak = a + count[0];
170 else {
171 ak = a;
172 top[255] = an;
173 }
174 for (cp = count + bmin; nc > 0; cp++) {
175 while (*cp == 0) /* Find next non-empty pile. */
176 cp++;
177 if (*cp > 1) {
178 if (*cp > bigc) {
179 bigc = *cp;
180 sp1 = sp;
181 }
182 push(ak, *cp, i+1);
183 }
184 top[cp-count] = ak += *cp;
185 nc--;
186 }
187 swap(*sp0, *sp1, temp); /* Play it safe -- biggest bin last. */
188
189 /*
190 * Permute misplacements home. Already home: everything
191 * before aj, and in bin[c], items from top[c] on.
192 * Inner loop:
193 * r = next element to put in place;
194 * ak = top[r[i]] = location to put the next element.
195 * aj = bottom of 1st disordered bin.
196 * Outer loop:
197 * Once the 1st disordered bin is done, ie. aj >= ak,
198 * aj<-aj + count[c] connects the bins in a linked list;
199 * reset count[c].
200 */
201 for (aj = a; aj < an; *aj = r, aj += count[c], count[c] = 0)
202 for (r = *aj; aj < (ak = --top[c = tr[r[i]]]);)
203 swap(*ak, r, t);
204 }
205}
206
207/* Stable sort, requiring additional memory. */
208void
209r_sort_b(const u_char **a, const u_char **ta, int n, int i, const u_char *tr,
210 u_int endch)
211{
212 static int count[256], nc, bmin;
213 int c;
214 const u_char **ak, **ai;
215 stack s[512], *sp, *sp0, *sp1, temp;
216 const u_char **top[256];
217 int *cp, bigc;
218
219 sp = s;
220 push(a, n, i);
221 while (!empty(s)) {
222 pop(a, n, i);
223 if (n < THRESHOLD) {
224 simplesort(a, n, i, tr, endch);
225 continue;
226 }
227
228 if (nc == 0) {
229 bmin = 255;
230 for (ak = a + n; --ak >= a;) {
231 c = tr[(*ak)[i]];
232 if (++count[c] == 1 && c != endch) {
233 if (c < bmin)
234 bmin = c;
235 nc++;
236 }
237 }
238 if (sp + nc > s + SIZE) {
239 r_sort_b(a, ta, n, i, tr, endch);
240 continue;
241 }
242 }
243
244 sp0 = sp1 = sp;
245 bigc = 2;
246 if (endch == 0) {
247 top[0] = ak = a + count[0];
248 count[0] = 0;
249 } else {
250 ak = a;
251 top[255] = a + n;
252 count[255] = 0;
253 }
254 for (cp = count + bmin; nc > 0; cp++) {
255 while (*cp == 0)
256 cp++;
257 if ((c = *cp) > 1) {
258 if (c > bigc) {
259 bigc = c;
260 sp1 = sp;
261 }
262 push(ak, c, i+1);
263 }
264 top[cp-count] = ak += c;
265 *cp = 0; /* Reset count[]. */
266 nc--;
267 }
268 swap(*sp0, *sp1, temp);
269
270 for (ak = ta + n, ai = a+n; ak > ta;) /* Copy to temp. */
271 *--ak = *--ai;
272 for (ak = ta+n; --ak >= ta;) /* Deal to piles. */
273 *--top[tr[(*ak)[i]]] = *ak;
274 }
275}
276
277static __inline void
278simplesort(const u_char **a, int n, int b, const u_char *tr, u_int endch)
279 /* insertion sort */
280{
281 u_char ch;
282 const u_char **ak, **ai, *s, *t;
283
284 for (ak = a+1; --n >= 1; ak++)
285 for (ai = ak; ai > a; ai--) {
286 for (s = ai[0] + b, t = ai[-1] + b;
287 (ch = tr[*s]) != endch; s++, t++)
288 if (ch != tr[*t])
289 break;
290 if (ch >= tr[*t])
291 break;
292 swap(ai[0], ai[-1], s);
293 }
294}
diff --git a/src/lib/libc/stdlib/rand.3 b/src/lib/libc/stdlib/rand.3
deleted file mode 100644
index cf7dde8536..0000000000
--- a/src/lib/libc/stdlib/rand.3
+++ /dev/null
@@ -1,135 +0,0 @@
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. 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.\" $OpenBSD: rand.3,v 1.21 2022/03/29 18:15:52 naddy Exp $
33.\"
34.Dd $Mdocdate: March 29 2022 $
35.Dt RAND 3
36.Os
37.Sh NAME
38.Nm rand ,
39.Nm rand_r ,
40.Nm srand ,
41.Nm srand_deterministic
42.Nd bad pseudo-random number generator
43.Sh SYNOPSIS
44.In stdlib.h
45.Ft void
46.Fn srand "unsigned int seed"
47.Ft void
48.Fn srand_deterministic "unsigned int seed"
49.Ft int
50.Fn rand void
51.Ft int
52.Fn rand_r "unsigned int *seed"
53.Sh DESCRIPTION
54.Bf -symbolic
55Standards insist that this interface return deterministic results.
56Unsafe usage is very common, so
57.Ox
58changed the subsystem to return non-deterministic results by default.
59.Ef
60.Pp
61To satisfy portable code,
62.Fn srand
63may be called to initialize the subsystem.
64In
65.Ox
66the
67.Ar seed
68variable is ignored, and strong random number results will be provided from
69.Xr arc4random 3 .
70In other systems, the
71.Ar seed
72variable primes a simplistic deterministic algorithm.
73.Pp
74If the standardized behavior is required
75.Fn srand_deterministic
76can be substituted for
77.Fn srand ,
78then subsequent
79.Fn rand
80calls will return results using the deterministic algorithm.
81The deterministic sequence algorithm changed a number of times since
82original development, is underspecified, and should not be relied upon to
83remain consistent between platforms and over time.
84.Pp
85The
86.Fn rand
87function returns a result in the range of 0 to
88.Dv RAND_MAX .
89By default, this result comes from
90.Xr arc4random 3 .
91If
92.Fn srand_deterministic
93was called, the result will be computed using the deterministic algorithm.
94.Pp
95The
96.Fn rand_r
97function is a thread-safe version of
98.Fn rand .
99Storage for the seed must be provided through the
100.Fa seed
101argument, and needs to have been initialized by the caller.
102It always operates using the deterministic algorithm.
103.Sh SEE ALSO
104.Xr arc4random 3 ,
105.Xr rand48 3 ,
106.Xr random 3
107.Sh STANDARDS
108The
109.Fn rand
110function conforms to
111.St -ansiC .
112.Pp
113The
114.Fn rand_r
115function conforms to
116.St -p1003.1-2008 .
117.Pp
118The
119.Fn srand
120function does not conform to
121.St -ansiC ,
122intentionally.
123.Pp
124The
125.Fn srand_deterministic
126function is an
127.Ox
128extension.
129.Sh HISTORY
130The functions
131.Fn rand
132and
133.Fn srand
134first appeared in
135.At v3 .
diff --git a/src/lib/libc/stdlib/rand.c b/src/lib/libc/stdlib/rand.c
deleted file mode 100644
index 97964f6ef9..0000000000
--- a/src/lib/libc/stdlib/rand.c
+++ /dev/null
@@ -1,74 +0,0 @@
1/* $OpenBSD: rand.c,v 1.18 2017/11/28 06:55:49 tb Exp $ */
2/*-
3 * Copyright (c) 1990 The Regents of the University of California.
4 * 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. Neither the name of the University nor the names of its contributors
15 * may be used to endorse or promote products derived from this software
16 * without specific prior written permission.
17 *
18 * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
19 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
20 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
21 * ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
22 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
23 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
24 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
25 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
26 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
27 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
28 * SUCH DAMAGE.
29 */
30
31#include <sys/types.h>
32#include <stdlib.h>
33
34static int rand_deterministic;
35static u_int next = 1;
36
37int
38rand_r(u_int *seed)
39{
40 *seed = *seed * 1103515245 + 12345;
41 return (*seed & RAND_MAX);
42}
43DEF_WEAK(rand_r);
44
45#if defined(APIWARN)
46__warn_references(rand_r,
47 "rand_r() is not random, it is deterministic.");
48#endif
49
50int
51rand(void)
52{
53 if (rand_deterministic == 0)
54 return (arc4random() & RAND_MAX);
55 return (rand_r(&next));
56}
57
58#if defined(APIWARN)
59__warn_references(rand,
60 "rand() may return deterministic values, is that what you want?");
61#endif
62
63void
64srand(u_int seed)
65{
66 rand_deterministic = 0;
67}
68
69void
70srand_deterministic(u_int seed)
71{
72 rand_deterministic = 1;
73 next = seed;
74}
diff --git a/src/lib/libc/stdlib/rand48.3 b/src/lib/libc/stdlib/rand48.3
deleted file mode 100644
index fa7a7179bc..0000000000
--- a/src/lib/libc/stdlib/rand48.3
+++ /dev/null
@@ -1,240 +0,0 @@
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.\" $OpenBSD: rand48.3,v 1.21 2019/12/20 19:16:40 tb Exp $
13.\"
14.Dd $Mdocdate: December 20 2019 $
15.Dt DRAND48 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 srand48_deterministic ,
26.Nm seed48 ,
27.Nm seed48_deterministic ,
28.Nm lcong48 ,
29.Nm lcong48_deterministic
30.Nd pseudo-random number generators and initialization routines
31.Sh SYNOPSIS
32.In stdlib.h
33.Ft double
34.Fn drand48 void
35.Ft double
36.Fn erand48 "unsigned short xseed[3]"
37.Ft long
38.Fn lrand48 void
39.Ft long
40.Fn nrand48 "unsigned short xseed[3]"
41.Ft long
42.Fn mrand48 void
43.Ft long
44.Fn jrand48 "unsigned short xseed[3]"
45.Ft void
46.Fn srand48 "long seed"
47.Ft void
48.Fn srand48_deterministic "long seed"
49.Ft "unsigned short *"
50.Fn seed48 "unsigned short xseed[3]"
51.Ft "unsigned short *"
52.Fn seed48_deterministic "unsigned short xseed[3]"
53.Ft void
54.Fn lcong48 "unsigned short p[7]"
55.Ft void
56.Fn lcong48_deterministic "unsigned short p[7]"
57.Sh DESCRIPTION
58.Bf -symbolic
59Standards insist that this interface return deterministic results.
60Unsafe usage is very common, so
61.Ox
62changed the subsystem to return non-deterministic results by default.
63.Ef
64.Pp
65To satisfy portable code,
66.Fn srand48 ,
67.Fn seed48 ,
68or
69.Fn lcong48
70should be called to initialize the subsystem.
71In
72.Ox
73the
74seeding parameters are ignored, and strong random number results will be
75provided from
76.Xr arc4random 3 .
77In other systems, the
78parameters prime a simplistic deterministic algorithm.
79.Pp
80If the standardized behavior is required then
81.Fn srand48_deterministic ,
82.Fn seed48_deterministic ,
83and
84.Fn lcong48_deterministic
85can be substituted for
86.Fn srand48 ,
87.Fn seed48 ,
88and
89.Fn lcong48 .
90That will cause subsequent
91calls to
92.Fn drand48 ,
93.Fn lrand48 ,
94and
95.Fn jrand48
96to return results using the deterministic algorithm.
97.Pp
98.Fn drand48
99and
100.Fn erand48
101return values of type double.
102The full 48 bits of r(n+1) are
103loaded into the mantissa of the returned value, with the exponent set
104such that the values produced lie in the interval [0.0, 1.0).
105.Pp
106.Fn lrand48
107and
108.Fn nrand48
109return values of type long in the range
110[0, 2**31-1].
111The high-order (31) bits of
112r(n+1) are loaded into the lower bits of the returned value, with
113the topmost (sign) bit set to zero.
114.Pp
115.Fn mrand48
116and
117.Fn jrand48
118return values of type long in the range
119[-2**31, 2**31-1].
120The high-order (32) bits of r(n+1) are loaded into the returned value.
121.Pp
122In the deterministic mode, the
123.Fn rand48
124family of functions generates numbers using a linear congruential
125algorithm working on integers 48 bits in size.
126The particular formula employed is
127r(n+1) = (a * r(n) + c) mod m
128where the default values are
129for the multiplicand a = 0xfdeece66d = 25214903917 and
130the addend c = 0xb = 11.
131The modulus is always fixed at m = 2 ** 48.
132r(n) is called the seed of the random number generator.
133.Pp
134For all the six generator routines described next, the first
135computational step is to perform a single iteration of the algorithm.
136.Pp
137.Fn drand48 ,
138.Fn lrand48 ,
139and
140.Fn mrand48
141use an internal buffer to store r(n).
142For these functions
143the initial value of r(0) = 0x1234abcd330e = 20017429951246.
144.Pp
145On the other hand,
146.Fn erand48 ,
147.Fn nrand48 ,
148and
149.Fn jrand48
150use a user-supplied buffer to store the seed r(n),
151which consists of an array of 3 shorts, where the zeroth member
152holds the least significant bits.
153.Pp
154All functions share the same multiplicand and addend.
155.Pp
156.Fn srand48_deterministic
157is used to initialize the internal buffer r(n) of
158.Fn drand48 ,
159.Fn lrand48 ,
160and
161.Fn mrand48
162such that the 32 bits of the seed value are copied into the upper 32 bits
163of r(n), with the lower 16 bits of r(n) arbitrarily being set to 0x330e.
164Additionally, the constant multiplicand and addend of the algorithm are
165reset to the default values given above.
166.Pp
167.Fn seed48_deterministic
168also initializes the internal buffer r(n) of
169.Fn drand48 ,
170.Fn lrand48 ,
171and
172.Fn mrand48 ,
173but here all 48 bits of the seed can be specified in an array of 3 shorts,
174where the zeroth member specifies the lowest bits.
175Again, the constant multiplicand and addend of the algorithm are
176reset to the default values given above.
177.Fn seed48_deterministic
178returns a pointer to an array of 3 shorts which contains the old seed.
179This array is statically allocated, so its contents are lost after
180each new call to
181.Fn seed48_deterministic .
182.Pp
183Finally,
184.Fn lcong48_deterministic
185allows full control over the multiplicand and addend used in
186.Fn drand48 ,
187.Fn erand48 ,
188.Fn lrand48 ,
189.Fn nrand48 ,
190.Fn mrand48 ,
191and
192.Fn jrand48 ,
193and the seed used in
194.Fn drand48 ,
195.Fn lrand48 ,
196and
197.Fn mrand48 .
198An array of 7 shorts is passed as parameter; the first three shorts are
199used to initialize the seed; the second three are used to initialize the
200multiplicand; and the last short is used to initialize the addend.
201It is thus not possible to use values greater than 0xffff as the addend.
202.Pp
203Note that all three methods of seeding the random number generator
204always also set the multiplicand and addend for any of the six
205generator calls.
206.Sh SEE ALSO
207.Xr arc4random 3 ,
208.Xr rand 3 ,
209.Xr random 3
210.Sh STANDARDS
211The
212.Fn drand48 ,
213.Fn erand48 ,
214.Fn jrand48 ,
215.Fn lrand48 ,
216.Fn mrand48 ,
217and
218.Fn nrand48 ,
219functions conform to
220.St -p1003.1-2008 .
221.Pp
222The
223.Fn seed48 ,
224.Fn srand48 ,
225and
226.Fn lcong48
227function do not conform to
228.St -ansiC ,
229intentionally.
230.Pp
231The
232.Fn seed48_deterministic ,
233.Fn srand48_deterministic ,
234and
235.Fn lcong48_deterministic
236functions are
237.Ox
238extensions.
239.Sh AUTHORS
240.An Martin Birgmeier
diff --git a/src/lib/libc/stdlib/rand48.h b/src/lib/libc/stdlib/rand48.h
deleted file mode 100644
index 7a719beba6..0000000000
--- a/src/lib/libc/stdlib/rand48.h
+++ /dev/null
@@ -1,38 +0,0 @@
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 * $OpenBSD: rand48.h,v 1.6 2015/09/14 13:30:17 guenther Exp $
14 */
15
16#ifndef _RAND48_H_
17#define _RAND48_H_
18
19#include <stdlib.h>
20
21__BEGIN_HIDDEN_DECLS
22extern unsigned short __rand48_seed[3];
23extern unsigned short __rand48_mult[3];
24extern unsigned short __rand48_add;
25
26void __dorand48(unsigned short[3]);
27extern int __rand48_deterministic;
28__END_HIDDEN_DECLS
29
30#define RAND48_SEED_0 (0x330e)
31#define RAND48_SEED_1 (0xabcd)
32#define RAND48_SEED_2 (0x1234)
33#define RAND48_MULT_0 (0xe66d)
34#define RAND48_MULT_1 (0xdeec)
35#define RAND48_MULT_2 (0x0005)
36#define RAND48_ADD (0x000b)
37
38#endif /* _RAND48_H_ */
diff --git a/src/lib/libc/stdlib/random.3 b/src/lib/libc/stdlib/random.3
deleted file mode 100644
index 0770d20f09..0000000000
--- a/src/lib/libc/stdlib/random.3
+++ /dev/null
@@ -1,197 +0,0 @@
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. Neither the name of the University nor the names of its contributors
13.\" may be used to endorse or promote products derived from this software
14.\" without specific prior written permission.
15.\"
16.\" THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
17.\" ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
18.\" IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
19.\" ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
20.\" FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
21.\" DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
22.\" OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
23.\" HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
24.\" LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
25.\" OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
26.\" SUCH DAMAGE.
27.\"
28.\" $OpenBSD: random.3,v 1.29 2021/02/12 17:03:51 deraadt Exp $
29.\"
30.Dd $Mdocdate: February 12 2021 $
31.Dt RANDOM 3
32.Os
33.Sh NAME
34.Nm random ,
35.Nm srandom ,
36.Nm srandom_deterministic ,
37.Nm srandomdev ,
38.Nm initstate ,
39.Nm setstate
40.Nd pseudo-random number generator; routines for changing generators
41.Sh SYNOPSIS
42.In stdlib.h
43.Ft long
44.Fn random void
45.Ft void
46.Fn srandom "unsigned int seed"
47.Ft void
48.Fn srandom_deterministic "unsigned int seed"
49.Ft void
50.Fn srandomdev void
51.Ft char *
52.Fn initstate "unsigned int seed" "char *state" "size_t n"
53.Ft char *
54.Fn setstate "char *state"
55.Sh DESCRIPTION
56.Bf -symbolic
57Standards insist that this interface return deterministic results.
58Unsafe usage is very common, so
59.Ox
60changed the subsystem to return non-deterministic results by default.
61.Ef
62.Pp
63To satisfy portable code,
64.Fn srandom
65or
66.Fn srandomdev
67may be called to initialize the subsystem.
68In
69.Ox
70the
71.Ar seed
72variable is ignored, and strong random number results will be provided from
73.Xr arc4random 3 .
74In other systems, the
75.Ar seed
76variable primes a simplistic deterministic algorithm.
77.Pp
78If the standardized behavior is required
79.Fn srandom_deterministic
80can be substituted for
81.Fn srandom ,
82then subsequent
83.Fn random
84calls will return results using the deterministic algorithm.
85.Pp
86In non-deterministic (default) mode, the
87.Fn random
88function returns results from
89.Xr arc4random 3
90in the range from 0 to (2**31)\-1.
91.Pp
92In deterministic mode, the
93.Fn random
94function uses a non-linear additive feedback random number generator employing
95a default table of size 31 long integers to return successive pseudo-random
96numbers in the range from 0 to (2**31)\-1.
97The period of this random number generator is very large, approximately
9816*((2**31)\-1), but the results are a deterministic sequence from the seed.
99The deterministic sequence algorithm changed a number of times since
100original development, is underspecified, and should not be relied upon to
101remain consistent between platforms and over time.
102.Pp
103The
104.Fn initstate
105routine allows a state array, passed in as an argument, to be initialized
106for future use.
107The size of the state array (in bytes) is used by
108.Fn initstate
109to decide how sophisticated a random number generator it should use \(em the
110more state, the better the random numbers will be.
111(Current "optimal" values for the amount of state information are
1128, 32, 64, 128, and 256 bytes; other amounts will be rounded down to
113the nearest known amount.
114Using less than 8 bytes will cause an error.)
115The seed for the initialization (which specifies a starting point for
116the random number sequence, and provides for restarting at the same
117point) is also an argument.
118The
119.Fn initstate
120function returns a pointer to the previous state information array.
121.Pp
122Once a state has been initialized, the
123.Fn setstate
124routine provides for rapid switching between states.
125The
126.Fn setstate
127function returns a pointer to the previous state array; its
128argument state array is used for further random number generation
129until the next call to
130.Fn initstate
131or
132.Fn setstate .
133.Pp
134Once a state array has been initialized, it may be restarted at a
135different point either by calling
136.Fn initstate
137(with the desired seed, the state array, and its size) or by calling
138both
139.Fn setstate
140(with the state array) and
141.Fn srandom
142(with the desired seed).
143The advantage of calling both
144.Fn setstate
145and
146.Fn srandom
147is that the size of the state array does not have to be remembered after
148it is initialized.
149.Pp
150Use of
151.Fn srandom_deterministic ,
152.Fn initstate ,
153or
154.Fn setstate
155forces the subsystem into deterministic mode.
156.Sh DIAGNOSTICS
157If
158.Fn initstate
159is called with less than 8 bytes of state information, or if
160.Fn setstate
161detects that the state information has been garbled, error
162messages are printed on the standard error output.
163.Sh SEE ALSO
164.Xr arc4random 3 ,
165.Xr drand48 3 ,
166.Xr rand 3 ,
167.Xr random 4
168.Sh STANDARDS
169The
170.Fn random ,
171.Fn initstate ,
172and
173.Fn setstate
174functions conform to
175.St -xpg4.2 .
176.Pp
177The
178.Fn srandom
179function does not conform to
180.St -xpg4.2 ,
181intentionally.
182.Pp
183The
184.Fn srandomdev
185function is an extension.
186.Pp
187The
188.Fn srandom_deterministic
189function is an
190.Ox
191extension.
192.Sh HISTORY
193These
194functions appeared in
195.Bx 4.2 .
196.Sh AUTHORS
197.An Earl T. Cohen
diff --git a/src/lib/libc/stdlib/random.c b/src/lib/libc/stdlib/random.c
deleted file mode 100644
index 62a0c24bdb..0000000000
--- a/src/lib/libc/stdlib/random.c
+++ /dev/null
@@ -1,419 +0,0 @@
1/* $OpenBSD: random.c,v 1.31 2017/11/28 06:55:49 tb Exp $ */
2/*
3 * Copyright (c) 1983 Regents of the University of California.
4 * 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. Neither the name of the University nor the names of its contributors
15 * may be used to endorse or promote products derived from this software
16 * without specific prior written permission.
17 *
18 * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
19 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
20 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
21 * ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
22 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
23 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
24 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
25 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
26 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
27 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
28 * SUCH DAMAGE.
29 */
30
31#include <fcntl.h>
32#include <stdio.h>
33#include <stdlib.h>
34#include <unistd.h>
35
36#include "thread_private.h"
37
38/*
39 * random.c:
40 *
41 * An improved random number generation package. In addition to the standard
42 * rand()/srand() like interface, this package also has a special state info
43 * interface. The initstate() routine is called with a seed, an array of
44 * bytes, and a count of how many bytes are being passed in; this array is
45 * then initialized to contain information for random number generation with
46 * that much state information. Good sizes for the amount of state
47 * information are 32, 64, 128, and 256 bytes. The state can be switched by
48 * calling the setstate() routine with the same array as was initiallized
49 * with initstate(). By default, the package runs with 128 bytes of state
50 * information and generates far better random numbers than a linear
51 * congruential generator. If the amount of state information is less than
52 * 32 bytes, a simple linear congruential R.N.G. is used.
53 *
54 * Internally, the state information is treated as an array of int32_t; the
55 * zeroeth element of the array is the type of R.N.G. being used (small
56 * integer); the remainder of the array is the state information for the
57 * R.N.G. Thus, 32 bytes of state information will give 7 int32_ts worth of
58 * state information, which will allow a degree seven polynomial. (Note:
59 * the zeroeth word of state information also has some other information
60 * stored in it -- see setstate() for details).
61 *
62 * The random number generation technique is a linear feedback shift register
63 * approach, employing trinomials (since there are fewer terms to sum up that
64 * way). In this approach, the least significant bit of all the numbers in
65 * the state table will act as a linear feedback shift register, and will
66 * have period 2^deg - 1 (where deg is the degree of the polynomial being
67 * used, assuming that the polynomial is irreducible and primitive). The
68 * higher order bits will have longer periods, since their values are also
69 * influenced by pseudo-random carries out of the lower bits. The total
70 * period of the generator is approximately deg*(2**deg - 1); thus doubling
71 * the amount of state information has a vast influence on the period of the
72 * generator. Note: the deg*(2**deg - 1) is an approximation only good for
73 * large deg, when the period of the shift register is the dominant factor.
74 * With deg equal to seven, the period is actually much longer than the
75 * 7*(2**7 - 1) predicted by this formula.
76 */
77
78/*
79 * For each of the currently supported random number generators, we have a
80 * break value on the amount of state information (you need at least this
81 * many bytes of state info to support this random number generator), a degree
82 * for the polynomial (actually a trinomial) that the R.N.G. is based on, and
83 * the separation between the two lower order coefficients of the trinomial.
84 */
85#define TYPE_0 0 /* linear congruential */
86#define BREAK_0 8
87#define DEG_0 0
88#define SEP_0 0
89
90#define TYPE_1 1 /* x**7 + x**3 + 1 */
91#define BREAK_1 32
92#define DEG_1 7
93#define SEP_1 3
94
95#define TYPE_2 2 /* x**15 + x + 1 */
96#define BREAK_2 64
97#define DEG_2 15
98#define SEP_2 1
99
100#define TYPE_3 3 /* x**31 + x**3 + 1 */
101#define BREAK_3 128
102#define DEG_3 31
103#define SEP_3 3
104
105#define TYPE_4 4 /* x**63 + x + 1 */
106#define BREAK_4 256
107#define DEG_4 63
108#define SEP_4 1
109
110/*
111 * Array versions of the above information to make code run faster --
112 * relies on fact that TYPE_i == i.
113 */
114#define MAX_TYPES 5 /* max number of types above */
115
116static int degrees[MAX_TYPES] = { DEG_0, DEG_1, DEG_2, DEG_3, DEG_4 };
117static int seps [MAX_TYPES] = { SEP_0, SEP_1, SEP_2, SEP_3, SEP_4 };
118
119/*
120 * Initially, everything is set up as if from:
121 *
122 * initstate(1, &randtbl, 128);
123 *
124 * Note that this initialization takes advantage of the fact that srandom()
125 * advances the front and rear pointers 10*rand_deg times, and hence the
126 * rear pointer which starts at 0 will also end up at zero; thus the zeroeth
127 * element of the state information, which contains info about the current
128 * position of the rear pointer is just
129 *
130 * MAX_TYPES * (rptr - state) + TYPE_3 == TYPE_3.
131 */
132
133static int32_t randtbl[DEG_3 + 1] = {
134 TYPE_3,
135 0x991539b1, 0x16a5bce3, 0x6774a4cd, 0x3e01511e, 0x4e508aaa, 0x61048c05,
136 0xf5500617, 0x846b7115, 0x6a19892c, 0x896a97af, 0xdb48f936, 0x14898454,
137 0x37ffd106, 0xb58bff9c, 0x59e17104, 0xcf918a49, 0x09378c83, 0x52c7a471,
138 0x8d293ea9, 0x1f4fc301, 0xc3db71be, 0x39b44e1c, 0xf8a44ef9, 0x4c8b80b1,
139 0x19edc328, 0x87bf4bdd, 0xc9b240e5, 0xe9ee4b1b, 0x4382aee7, 0x535b6b41,
140 0xf3bec5da,
141};
142
143/*
144 * fptr and rptr are two pointers into the state info, a front and a rear
145 * pointer. These two pointers are always rand_sep places aparts, as they
146 * cycle cyclically through the state information. (Yes, this does mean we
147 * could get away with just one pointer, but the code for random() is more
148 * efficient this way). The pointers are left positioned as they would be
149 * from the call
150 *
151 * initstate(1, randtbl, 128);
152 *
153 * (The position of the rear pointer, rptr, is really 0 (as explained above
154 * in the initialization of randtbl) because the state table pointer is set
155 * to point to randtbl[1] (as explained below).
156 */
157static int32_t *fptr = &randtbl[SEP_3 + 1];
158static int32_t *rptr = &randtbl[1];
159
160/*
161 * The following things are the pointer to the state information table, the
162 * type of the current generator, the degree of the current polynomial being
163 * used, and the separation between the two pointers. Note that for efficiency
164 * of random(), we remember the first location of the state information, not
165 * the zeroeth. Hence it is valid to access state[-1], which is used to
166 * store the type of the R.N.G. Also, we remember the last location, since
167 * this is more efficient than indexing every time to find the address of
168 * the last element to see if the front and rear pointers have wrapped.
169 */
170static int32_t *state = &randtbl[1];
171static int32_t *end_ptr = &randtbl[DEG_3 + 1];
172static int rand_type = TYPE_3;
173static int rand_deg = DEG_3;
174static int rand_sep = SEP_3;
175
176static int random_deterministic;
177
178static void *random_mutex;
179static long random_l(void);
180
181#define LOCK() _MUTEX_LOCK(&random_mutex)
182#define UNLOCK() _MUTEX_UNLOCK(&random_mutex)
183
184/*
185 * srandom:
186 *
187 * Initialize the random number generator based on the given seed. If the
188 * type is the trivial no-state-information type, just remember the seed.
189 * Otherwise, initializes state[] based on the given "seed" via a linear
190 * congruential generator. Then, the pointers are set to known locations
191 * that are exactly rand_sep places apart. Lastly, it cycles the state
192 * information a given number of times to get rid of any initial dependencies
193 * introduced by the L.C.R.N.G. Note that the initialization of randtbl[]
194 * for default usage relies on values produced by this routine.
195 */
196static void
197srandom_l(unsigned int x)
198{
199 int i;
200 int32_t test;
201 div_t val;
202
203 random_deterministic = 1;
204 if (rand_type == TYPE_0)
205 state[0] = x;
206 else {
207 /* A seed of 0 would result in state[] always being zero. */
208 state[0] = x ? x : 1;
209 for (i = 1; i < rand_deg; i++) {
210 /*
211 * Implement the following, without overflowing 31 bits:
212 *
213 * state[i] = (16807 * state[i - 1]) % 2147483647;
214 *
215 * 2^31-1 (prime) = 2147483647 = 127773*16807+2836
216 */
217 val = div(state[i-1], 127773);
218 test = 16807 * val.rem - 2836 * val.quot;
219 state[i] = test + (test < 0 ? 2147483647 : 0);
220 }
221 fptr = &state[rand_sep];
222 rptr = &state[0];
223 for (i = 0; i < 10 * rand_deg; i++)
224 (void)random_l();
225 }
226}
227
228void
229srandom(unsigned int x)
230{
231 random_deterministic = 0;
232}
233
234void
235srandomdev(void)
236{
237 random_deterministic = 0; /* back to the default */
238}
239
240void
241srandom_deterministic(unsigned int x)
242{
243 LOCK();
244 srandom_l(x);
245 UNLOCK();
246}
247
248/*
249 * initstate:
250 *
251 * Initialize the state information in the given array of n bytes for future
252 * random number generation. Based on the number of bytes we are given, and
253 * the break values for the different R.N.G.'s, we choose the best (largest)
254 * one we can and set things up for it. srandom() is then called to
255 * initialize the state information.
256 *
257 * Note that on return from srandom(), we set state[-1] to be the type
258 * multiplexed with the current value of the rear pointer; this is so
259 * successive calls to initstate() won't lose this information and will be
260 * able to restart with setstate().
261 *
262 * Note: the first thing we do is save the current state, if any, just like
263 * setstate() so that it doesn't matter when initstate is called.
264 *
265 * Returns a pointer to the old state.
266 */
267char *
268initstate(u_int seed, char *arg_state, size_t n)
269{
270 char *ostate = (char *)(&state[-1]);
271
272 LOCK();
273 random_deterministic = 1;
274 if (rand_type == TYPE_0)
275 state[-1] = rand_type;
276 else
277 state[-1] = MAX_TYPES * (rptr - state) + rand_type;
278 if (n < BREAK_0) {
279 UNLOCK();
280 return(NULL);
281 }
282 if (n < BREAK_1) {
283 rand_type = TYPE_0;
284 rand_deg = DEG_0;
285 rand_sep = SEP_0;
286 } else if (n < BREAK_2) {
287 rand_type = TYPE_1;
288 rand_deg = DEG_1;
289 rand_sep = SEP_1;
290 } else if (n < BREAK_3) {
291 rand_type = TYPE_2;
292 rand_deg = DEG_2;
293 rand_sep = SEP_2;
294 } else if (n < BREAK_4) {
295 rand_type = TYPE_3;
296 rand_deg = DEG_3;
297 rand_sep = SEP_3;
298 } else {
299 rand_type = TYPE_4;
300 rand_deg = DEG_4;
301 rand_sep = SEP_4;
302 }
303 state = &(((int32_t *)arg_state)[1]); /* first location */
304 end_ptr = &state[rand_deg]; /* must set end_ptr before srandom */
305 srandom_l(seed);
306 if (rand_type == TYPE_0)
307 state[-1] = rand_type;
308 else
309 state[-1] = MAX_TYPES*(rptr - state) + rand_type;
310 UNLOCK();
311 return(ostate);
312}
313
314/*
315 * setstate:
316 *
317 * Restore the state from the given state array.
318 *
319 * Note: it is important that we also remember the locations of the pointers
320 * in the current state information, and restore the locations of the pointers
321 * from the old state information. This is done by multiplexing the pointer
322 * location into the zeroeth word of the state information.
323 *
324 * Note that due to the order in which things are done, it is OK to call
325 * setstate() with the same state as the current state.
326 *
327 * Returns a pointer to the old state information.
328 */
329char *
330setstate(char *arg_state)
331{
332 int32_t *new_state = (int32_t *)arg_state;
333 int32_t type = new_state[0] % MAX_TYPES;
334 int32_t rear = new_state[0] / MAX_TYPES;
335 char *ostate = (char *)(&state[-1]);
336
337 LOCK();
338 random_deterministic = 1;
339 if (rand_type == TYPE_0)
340 state[-1] = rand_type;
341 else
342 state[-1] = MAX_TYPES * (rptr - state) + rand_type;
343 switch(type) {
344 case TYPE_0:
345 case TYPE_1:
346 case TYPE_2:
347 case TYPE_3:
348 case TYPE_4:
349 rand_type = type;
350 rand_deg = degrees[type];
351 rand_sep = seps[type];
352 break;
353 default:
354 UNLOCK();
355 return(NULL);
356 }
357 state = &new_state[1];
358 if (rand_type != TYPE_0) {
359 rptr = &state[rear];
360 fptr = &state[(rear + rand_sep) % rand_deg];
361 }
362 end_ptr = &state[rand_deg]; /* set end_ptr too */
363 UNLOCK();
364 return(ostate);
365}
366
367/*
368 * random:
369 *
370 * If we are using the trivial TYPE_0 R.N.G., just do the old linear
371 * congruential bit. Otherwise, we do our fancy trinomial stuff, which is
372 * the same in all the other cases due to all the global variables that have
373 * been set up. The basic operation is to add the number at the rear pointer
374 * into the one at the front pointer. Then both pointers are advanced to
375 * the next location cyclically in the table. The value returned is the sum
376 * generated, reduced to 31 bits by throwing away the "least random" low bit.
377 *
378 * Note: the code takes advantage of the fact that both the front and
379 * rear pointers can't wrap on the same call by not testing the rear
380 * pointer if the front one has wrapped.
381 *
382 * Returns a 31-bit random number.
383 */
384static long
385random_l(void)
386{
387 int32_t i;
388
389 if (random_deterministic == 0)
390 return arc4random() & 0x7fffffff;
391
392 if (rand_type == TYPE_0)
393 i = state[0] = (state[0] * 1103515245 + 12345) & 0x7fffffff;
394 else {
395 *fptr += *rptr;
396 i = (*fptr >> 1) & 0x7fffffff; /* chucking least random bit */
397 if (++fptr >= end_ptr) {
398 fptr = state;
399 ++rptr;
400 } else if (++rptr >= end_ptr)
401 rptr = state;
402 }
403 return((long)i);
404}
405
406long
407random(void)
408{
409 long r;
410 LOCK();
411 r = random_l();
412 UNLOCK();
413 return r;
414}
415
416#if defined(APIWARN)
417__warn_references(random,
418 "random() may return deterministic values, is that what you want?");
419#endif
diff --git a/src/lib/libc/stdlib/reallocarray.c b/src/lib/libc/stdlib/reallocarray.c
deleted file mode 100644
index baea252a87..0000000000
--- a/src/lib/libc/stdlib/reallocarray.c
+++ /dev/null
@@ -1,39 +0,0 @@
1/* $OpenBSD: reallocarray.c,v 1.3 2015/09/13 08:31:47 guenther Exp $ */
2/*
3 * Copyright (c) 2008 Otto Moerbeek <otto@drijf.net>
4 *
5 * Permission to use, copy, modify, and distribute this software for any
6 * purpose with or without fee is hereby granted, provided that the above
7 * copyright notice and this permission notice appear in all copies.
8 *
9 * THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES
10 * WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF
11 * MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR
12 * ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES
13 * WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN
14 * ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF
15 * OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE.
16 */
17
18#include <sys/types.h>
19#include <errno.h>
20#include <stdint.h>
21#include <stdlib.h>
22
23/*
24 * This is sqrt(SIZE_MAX+1), as s1*s2 <= SIZE_MAX
25 * if both s1 < MUL_NO_OVERFLOW and s2 < MUL_NO_OVERFLOW
26 */
27#define MUL_NO_OVERFLOW ((size_t)1 << (sizeof(size_t) * 4))
28
29void *
30reallocarray(void *optr, size_t nmemb, size_t size)
31{
32 if ((nmemb >= MUL_NO_OVERFLOW || size >= MUL_NO_OVERFLOW) &&
33 nmemb > 0 && SIZE_MAX / nmemb < size) {
34 errno = ENOMEM;
35 return NULL;
36 }
37 return realloc(optr, size * nmemb);
38}
39DEF_WEAK(reallocarray);
diff --git a/src/lib/libc/stdlib/realpath.3 b/src/lib/libc/stdlib/realpath.3
deleted file mode 100644
index 1dec10fef4..0000000000
--- a/src/lib/libc/stdlib/realpath.3
+++ /dev/null
@@ -1,165 +0,0 @@
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. Neither the name of the University nor the names of its contributors
16.\" may be used to endorse or promote products derived from this software
17.\" without specific prior written permission.
18.\"
19.\" THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
20.\" ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
21.\" IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
22.\" ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
23.\" FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
24.\" DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
25.\" OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
26.\" HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
27.\" LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
28.\" OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
29.\" SUCH DAMAGE.
30.\"
31.\" $OpenBSD: realpath.3,v 1.26 2021/10/13 15:04:53 kn Exp $
32.\"
33.Dd $Mdocdate: October 13 2021 $
34.Dt REALPATH 3
35.Os
36.Sh NAME
37.Nm realpath
38.Nd returns the canonicalized absolute pathname
39.Sh SYNOPSIS
40.In limits.h
41.In stdlib.h
42.Ft "char *"
43.Fn realpath "const char *pathname" "char *resolved"
44.Sh DESCRIPTION
45The
46.Fn realpath
47function resolves all symbolic links, extra
48.Dq /
49characters and references to
50.Pa /./
51and
52.Pa /../
53in
54.Fa pathname ,
55and copies the resulting absolute pathname into the memory referenced by
56.Fa resolved .
57The
58.Fa resolved
59argument
60.Em must
61refer to a buffer capable of storing at least
62.Dv PATH_MAX
63characters, or be
64.Dv NULL .
65.Pp
66The
67.Fn realpath
68function will resolve both absolute and relative paths
69and return the absolute pathname corresponding to
70.Fa pathname .
71All components of
72.Fa pathname
73must exist when
74.Fn realpath
75is called.
76.Sh RETURN VALUES
77The
78.Fn realpath
79function returns
80.Fa resolved
81on success.
82If
83.Fa resolved
84is
85.Dv NULL
86and no error occurred, then
87.Fn realpath
88returns a NUL-terminated string in a newly allocated buffer.
89If an error occurs,
90.Fn realpath
91returns
92.Dv NULL
93and the contents of
94.Fa resolved
95are undefined.
96.Sh ERRORS
97The function
98.Fn realpath
99will fail if:
100.Bl -tag -width Er
101.It Bq Er EACCES
102Read or search permission was denied for a component of
103.Ar pathname .
104.It Bq Er EINVAL
105The
106.Ar pathname
107argument is a null pointer.
108.It Bq Er EIO
109An error occurred while reading from the file system.
110.It Bq Er ELOOP
111Too many symbolic links were encountered in translating
112.Ar pathname .
113.It Bq Er ENAMETOOLONG
114A component of
115.Ar pathname
116exceeded
117.Dv NAME_MAX
118characters, or the entire
119.Ar pathname
120(including the terminating NUL) exceeded
121.Dv PATH_MAX .
122.It Bq Er ENAMETOOLONG
123Pathname resolution of a symbolic link produced an intermediate
124result whose length exceeds
125.Dv PATH_MAX .
126.It Bq Er ENOENT
127A component of
128.Ar pathname
129does not name an existing file or
130.Ar pathname
131points to an empty string.
132.It Bq Er ENOTDIR
133A component of the path prefix is not a directory.
134.It Bq Er ENOMEM
135Sufficient storage space is unavailable for allocation.
136.El
137.Sh SEE ALSO
138.Xr readlink 1 ,
139.Xr realpath 1 ,
140.Xr getcwd 3
141.Sh STANDARDS
142The
143.Fn realpath
144function conforms to
145.St -p1003.1-2008 .
146.Sh HISTORY
147The
148.Fn realpath
149function call first appeared in
150.Bx 4.4 .
151.Pp
152In
153.Ox 6.6 ,
154it was reimplemented on top of the
155.Fn __realpath
156system call.
157Its calling convention differs from the standard
158function by requiring
159.Ar resolved
160to not be
161.Dv NULL
162and by returning an integer,
163zero on success, and -1 with corresponding errno on failure.
164This is visible in the output of
165.Xr kdump 1 .
diff --git a/src/lib/libc/stdlib/realpath.c b/src/lib/libc/stdlib/realpath.c
deleted file mode 100644
index 6562e1311b..0000000000
--- a/src/lib/libc/stdlib/realpath.c
+++ /dev/null
@@ -1,42 +0,0 @@
1/* $OpenBSD: realpath.c,v 1.28 2023/05/18 16:11:10 guenther Exp $ */
2/*
3 * Copyright (c) 2019 Bob Beck <beck@openbsd.org>
4 * Copyright (c) 2019 Theo de Raadt <deraadt@openbsd.org>
5 *
6 * Permission to use, copy, modify, and/or distribute this software for any
7 * purpose with or without fee is hereby granted, provided that the above
8 * copyright notice and this permission notice appear in all copies.
9 *
10 * THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES
11 * WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF
12 * MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY
13 * SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES
14 * WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN ACTION
15 * OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF OR IN
16 * CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE.
17 */
18
19#include <errno.h>
20#include <stdlib.h>
21#include <string.h>
22#include <unistd.h>
23#include <limits.h>
24#include <syslog.h>
25#include <stdarg.h>
26
27/*
28 * wrapper for kernel __realpath
29 */
30
31char *
32realpath(const char *path, char *resolved)
33{
34 char rbuf[PATH_MAX];
35
36 if (__realpath(path, rbuf) == -1)
37 return NULL;
38 if (resolved == NULL)
39 return (strdup(rbuf));
40 strlcpy(resolved, rbuf, PATH_MAX);
41 return (resolved);
42}
diff --git a/src/lib/libc/stdlib/recallocarray.c b/src/lib/libc/stdlib/recallocarray.c
deleted file mode 100644
index 81059e6ae1..0000000000
--- a/src/lib/libc/stdlib/recallocarray.c
+++ /dev/null
@@ -1,81 +0,0 @@
1/* $OpenBSD: recallocarray.c,v 1.2 2021/03/18 11:16:58 claudio Exp $ */
2/*
3 * Copyright (c) 2008, 2017 Otto Moerbeek <otto@drijf.net>
4 *
5 * Permission to use, copy, modify, and distribute this software for any
6 * purpose with or without fee is hereby granted, provided that the above
7 * copyright notice and this permission notice appear in all copies.
8 *
9 * THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES
10 * WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF
11 * MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR
12 * ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES
13 * WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN
14 * ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF
15 * OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE.
16 */
17
18#include <errno.h>
19#include <stdlib.h>
20#include <stdint.h>
21#include <string.h>
22#include <unistd.h>
23
24/*
25 * This is sqrt(SIZE_MAX+1), as s1*s2 <= SIZE_MAX
26 * if both s1 < MUL_NO_OVERFLOW and s2 < MUL_NO_OVERFLOW
27 */
28#define MUL_NO_OVERFLOW ((size_t)1 << (sizeof(size_t) * 4))
29
30void *
31recallocarray(void *ptr, size_t oldnmemb, size_t newnmemb, size_t size)
32{
33 size_t oldsize, newsize;
34 void *newptr;
35
36 if (ptr == NULL)
37 return calloc(newnmemb, size);
38
39 if ((newnmemb >= MUL_NO_OVERFLOW || size >= MUL_NO_OVERFLOW) &&
40 newnmemb > 0 && SIZE_MAX / newnmemb < size) {
41 errno = ENOMEM;
42 return NULL;
43 }
44 newsize = newnmemb * size;
45
46 if ((oldnmemb >= MUL_NO_OVERFLOW || size >= MUL_NO_OVERFLOW) &&
47 oldnmemb > 0 && SIZE_MAX / oldnmemb < size) {
48 errno = EINVAL;
49 return NULL;
50 }
51 oldsize = oldnmemb * size;
52
53 /*
54 * Don't bother too much if we're shrinking just a bit,
55 * we do not shrink for series of small steps, oh well.
56 */
57 if (newsize <= oldsize) {
58 size_t d = oldsize - newsize;
59
60 if (d < oldsize / 2 && d < (size_t)getpagesize()) {
61 memset((char *)ptr + newsize, 0, d);
62 return ptr;
63 }
64 }
65
66 newptr = malloc(newsize);
67 if (newptr == NULL)
68 return NULL;
69
70 if (newsize > oldsize) {
71 memcpy(newptr, ptr, oldsize);
72 memset((char *)newptr + oldsize, 0, newsize - oldsize);
73 } else
74 memcpy(newptr, ptr, newsize);
75
76 explicit_bzero(ptr, oldsize);
77 free(ptr);
78
79 return newptr;
80}
81DEF_WEAK(recallocarray);
diff --git a/src/lib/libc/stdlib/remque.c b/src/lib/libc/stdlib/remque.c
deleted file mode 100644
index 71b74b2dce..0000000000
--- a/src/lib/libc/stdlib/remque.c
+++ /dev/null
@@ -1,48 +0,0 @@
1/* $OpenBSD: remque.c,v 1.3 2014/08/15 04:14:36 guenther Exp $ */
2
3/*
4 * Copyright (c) 1993 John Brezak
5 * 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. The name of the author may be used to endorse or promote products
16 * derived from this software without specific prior written permission.
17 *
18 * THIS SOFTWARE IS PROVIDED BY THE AUTHOR `AS IS'' AND ANY EXPRESS OR
19 * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED
20 * WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
21 * DISCLAIMED. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT,
22 * INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES
23 * (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR
24 * SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
25 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT,
26 * STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN
27 * ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
28 * POSSIBILITY OF SUCH DAMAGE.
29 */
30
31#include <stdlib.h>
32#include <search.h>
33
34struct qelem {
35 struct qelem *q_forw;
36 struct qelem *q_back;
37};
38
39void
40remque(void *element)
41{
42 struct qelem *e = element;
43
44 if (e->q_forw != NULL)
45 e->q_forw->q_back = e->q_back;
46 if (e->q_back != NULL)
47 e->q_back->q_forw = e->q_forw;
48}
diff --git a/src/lib/libc/stdlib/seed48.c b/src/lib/libc/stdlib/seed48.c
deleted file mode 100644
index b4b4424c73..0000000000
--- a/src/lib/libc/stdlib/seed48.c
+++ /dev/null
@@ -1,45 +0,0 @@
1/* $OpenBSD: seed48.c,v 1.6 2015/09/13 15:20:40 guenther Exp $ */
2/*
3 * Copyright (c) 1993 Martin Birgmeier
4 * All rights reserved.
5 *
6 * You may redistribute unmodified or modified versions of this source
7 * code provided that the above copyright notice and this and the
8 * following conditions are retained.
9 *
10 * This software is provided ``as is'', and comes with no warranties
11 * of any kind. I shall in no event be liable for anything that happens
12 * to anyone/anything when using this software.
13 */
14
15#include "rand48.h"
16
17unsigned short *
18seed48(unsigned short xseed[3])
19{
20 unsigned short *res;
21
22 res = seed48_deterministic(xseed);
23 __rand48_deterministic = 0;
24 return res;
25}
26
27unsigned short *
28seed48_deterministic(unsigned short xseed[3])
29{
30 static unsigned short sseed[3];
31
32 __rand48_deterministic = 1;
33 sseed[0] = __rand48_seed[0];
34 sseed[1] = __rand48_seed[1];
35 sseed[2] = __rand48_seed[2];
36 __rand48_seed[0] = xseed[0];
37 __rand48_seed[1] = xseed[1];
38 __rand48_seed[2] = xseed[2];
39 __rand48_mult[0] = RAND48_MULT_0;
40 __rand48_mult[1] = RAND48_MULT_1;
41 __rand48_mult[2] = RAND48_MULT_2;
42 __rand48_add = RAND48_ADD;
43 return sseed;
44}
45DEF_WEAK(seed48_deterministic);
diff --git a/src/lib/libc/stdlib/setenv.c b/src/lib/libc/stdlib/setenv.c
deleted file mode 100644
index fc8e5b677f..0000000000
--- a/src/lib/libc/stdlib/setenv.c
+++ /dev/null
@@ -1,185 +0,0 @@
1/* $OpenBSD: setenv.c,v 1.20 2022/08/08 22:40:03 millert Exp $ */
2/*
3 * Copyright (c) 1987 Regents of the University of California.
4 * 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. Neither the name of the University nor the names of its contributors
15 * may be used to endorse or promote products derived from this software
16 * without specific prior written permission.
17 *
18 * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
19 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
20 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
21 * ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
22 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
23 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
24 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
25 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
26 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
27 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
28 * SUCH DAMAGE.
29 */
30
31#include <errno.h>
32#include <stdlib.h>
33#include <string.h>
34
35static char **lastenv; /* last value of environ */
36
37/*
38 * putenv --
39 * Add a name=value string directly to the environmental, replacing
40 * any current value.
41 */
42int
43putenv(char *str)
44{
45 char **P, *cp;
46 size_t cnt = 0;
47 int offset = 0;
48
49 for (cp = str; *cp && *cp != '='; ++cp)
50 ;
51 if (cp == str || *cp != '=') {
52 /* '=' is the first character of string or is missing. */
53 errno = EINVAL;
54 return (-1);
55 }
56
57 if (__findenv(str, (int)(cp - str), &offset) != NULL) {
58 environ[offset++] = str;
59 /* could be set multiple times */
60 while (__findenv(str, (int)(cp - str), &offset)) {
61 for (P = &environ[offset];; ++P)
62 if (!(*P = *(P + 1)))
63 break;
64 }
65 return (0);
66 }
67
68 /* create new slot for string */
69 if (environ != NULL) {
70 for (P = environ; *P != NULL; P++)
71 ;
72 cnt = P - environ;
73 }
74 P = reallocarray(lastenv, cnt + 2, sizeof(char *));
75 if (!P)
76 return (-1);
77 if (lastenv != environ && environ != NULL)
78 memcpy(P, environ, cnt * sizeof(char *));
79 lastenv = environ = P;
80 environ[cnt] = str;
81 environ[cnt + 1] = NULL;
82 return (0);
83}
84DEF_WEAK(putenv);
85
86/*
87 * setenv --
88 * Set the value of the environmental variable "name" to be
89 * "value". If rewrite is set, replace any current value.
90 */
91int
92setenv(const char *name, const char *value, int rewrite)
93{
94 char *C, **P;
95 const char *np;
96 int l_value, offset = 0;
97
98 if (!name || !*name) {
99 errno = EINVAL;
100 return (-1);
101 }
102 for (np = name; *np && *np != '='; ++np)
103 ;
104 if (*np) {
105 errno = EINVAL;
106 return (-1); /* has `=' in name */
107 }
108
109 l_value = strlen(value);
110 if ((C = __findenv(name, (int)(np - name), &offset)) != NULL) {
111 int tmpoff = offset + 1;
112 if (!rewrite)
113 return (0);
114#if 0 /* XXX - existing entry may not be writable */
115 if (strlen(C) >= l_value) { /* old larger; copy over */
116 while ((*C++ = *value++))
117 ;
118 return (0);
119 }
120#endif
121 /* could be set multiple times */
122 while (__findenv(name, (int)(np - name), &tmpoff)) {
123 for (P = &environ[tmpoff];; ++P)
124 if (!(*P = *(P + 1)))
125 break;
126 }
127 } else { /* create new slot */
128 size_t cnt = 0;
129
130 if (environ != NULL) {
131 for (P = environ; *P != NULL; P++)
132 ;
133 cnt = P - environ;
134 }
135 P = reallocarray(lastenv, cnt + 2, sizeof(char *));
136 if (!P)
137 return (-1);
138 if (lastenv != environ && environ != NULL)
139 memcpy(P, environ, cnt * sizeof(char *));
140 lastenv = environ = P;
141 offset = cnt;
142 environ[cnt + 1] = NULL;
143 }
144 if (!(environ[offset] = /* name + `=' + value */
145 malloc((int)(np - name) + l_value + 2)))
146 return (-1);
147 for (C = environ[offset]; (*C = *name++) && *C != '='; ++C)
148 ;
149 for (*C++ = '='; (*C++ = *value++); )
150 ;
151 return (0);
152}
153DEF_WEAK(setenv);
154
155/*
156 * unsetenv(name) --
157 * Delete environmental variable "name".
158 */
159int
160unsetenv(const char *name)
161{
162 char **P;
163 const char *np;
164 int offset = 0;
165
166 if (!name || !*name) {
167 errno = EINVAL;
168 return (-1);
169 }
170 for (np = name; *np && *np != '='; ++np)
171 ;
172 if (*np) {
173 errno = EINVAL;
174 return (-1); /* has `=' in name */
175 }
176
177 /* could be set multiple times */
178 while (__findenv(name, (int)(np - name), &offset)) {
179 for (P = &environ[offset];; ++P)
180 if (!(*P = *(P + 1)))
181 break;
182 }
183 return (0);
184}
185DEF_WEAK(unsetenv);
diff --git a/src/lib/libc/stdlib/srand48.c b/src/lib/libc/stdlib/srand48.c
deleted file mode 100644
index d41391d445..0000000000
--- a/src/lib/libc/stdlib/srand48.c
+++ /dev/null
@@ -1,38 +0,0 @@
1/* $OpenBSD: srand48.c,v 1.6 2015/09/13 08:31:48 guenther Exp $ */
2/*
3 * Copyright (c) 1993 Martin Birgmeier
4 * All rights reserved.
5 *
6 * You may redistribute unmodified or modified versions of this source
7 * code provided that the above copyright notice and this and the
8 * following conditions are retained.
9 *
10 * This software is provided ``as is'', and comes with no warranties
11 * of any kind. I shall in no event be liable for anything that happens
12 * to anyone/anything when using this software.
13 */
14
15#include "rand48.h"
16
17int __rand48_deterministic;
18
19void
20srand48(long seed)
21{
22 srand48_deterministic(seed);
23 __rand48_deterministic = 0;
24}
25
26void
27srand48_deterministic(long seed)
28{
29 __rand48_deterministic = 1;
30 __rand48_seed[0] = RAND48_SEED_0;
31 __rand48_seed[1] = (unsigned short) seed;
32 __rand48_seed[2] = (unsigned short) (seed >> 16);
33 __rand48_mult[0] = RAND48_MULT_0;
34 __rand48_mult[1] = RAND48_MULT_1;
35 __rand48_mult[2] = RAND48_MULT_2;
36 __rand48_add = RAND48_ADD;
37}
38DEF_WEAK(srand48_deterministic);
diff --git a/src/lib/libc/stdlib/strtod.3 b/src/lib/libc/stdlib/strtod.3
deleted file mode 100644
index ad8f28a02f..0000000000
--- a/src/lib/libc/stdlib/strtod.3
+++ /dev/null
@@ -1,176 +0,0 @@
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. 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.\" $OpenBSD: strtod.3,v 1.23 2022/09/11 06:38:11 jmc Exp $
33.\"
34.Dd $Mdocdate: September 11 2022 $
35.Dt STRTOD 3
36.Os
37.Sh NAME
38.Nm strtod ,
39.Nm strtof ,
40.Nm strtold
41.Nd convert ASCII string to double, float or long double
42.Sh SYNOPSIS
43.In stdlib.h
44.Ft double
45.Fn strtod "const char *nptr" "char **endptr"
46.Pp
47.Ft float
48.Fn strtof "const char *nptr" "char **endptr"
49.Pp
50.Ft long double
51.Fn strtold "const char *nptr" "char **endptr"
52.Sh DESCRIPTION
53The
54.Fn strtod
55function converts the initial portion of the string pointed to by
56.Fa nptr
57to
58.Vt double
59representation.
60The
61.Fn strtof
62function converts the initial portion of the string pointed to by
63.Fa nptr
64to
65.Vt float
66representation.
67The
68.Fn strtold
69function converts the initial portion of the string pointed to by
70.Fa nptr
71to
72.Vt long double
73representation.
74.Pp
75The expected form of the string is an optional plus
76.Pq Ql +
77or minus sign
78.Pq Ql -
79followed by a sequence of digits optionally containing
80a decimal-point character, optionally followed by an exponent.
81An exponent consists of an
82.Sq E
83or
84.Sq e ,
85followed by an optional plus or minus sign, followed by a sequence of digits.
86.Pp
87Alternatively, if the portion of the string following the optional
88plus or minus sign begins with
89.Dq INF
90or
91.Dq NAN ,
92ignoring case, it is interpreted as an infinity or a quiet \*(Na,
93respectively.
94The syntax
95.Dq NAN Ns Pq Ar s ,
96where
97.Ar s
98is an alphanumeric string, produces the same value as the call
99.Fo nan
100.Qq Ar s Ns
101.Fc
102(respectively,
103.Fo nanf
104.Qq Ar s Ns
105.Fc
106and
107.Fo nanl
108.Qq Ar s Ns
109.Fc ) .
110.Pp
111In any of the above cases, leading whitespace characters in the
112string (as defined by the
113.Xr isspace 3
114function) are skipped.
115.Sh RETURN VALUES
116The
117.Fn strtod ,
118.Fn strtof
119and
120.Fn strtold
121functions return the converted value, if any.
122.Pp
123If
124.Fa endptr
125is not
126.Dv NULL ,
127a pointer to the character after the last character used
128in the conversion is stored in the location referenced by
129.Fa endptr .
130.Pp
131If no conversion is performed, zero is returned and the value of
132.Fa nptr
133is stored in the location referenced by
134.Fa endptr .
135.Pp
136If the correct value would cause overflow, plus or minus
137.Dv HUGE_VAL
138is returned (according to the sign of the value), and
139.Er ERANGE
140is stored in
141.Va errno .
142If the correct value would cause underflow, zero is returned and
143.Er ERANGE
144is stored in
145.Va errno .
146.Sh ERRORS
147.Bl -tag -width Er
148.It Bq Er ERANGE
149Overflow or underflow occurred.
150.El
151.Sh SEE ALSO
152.Xr atof 3 ,
153.Xr atoi 3 ,
154.Xr atol 3 ,
155.Xr strtol 3 ,
156.Xr strtoul 3
157.Sh STANDARDS
158The
159.Fn strtod
160function conforms to
161.St -ansiC-89 .
162The
163.Fn strtof
164and
165.Fn strtold
166functions conform to
167.St -isoC-99 .
168.Sh CAVEATS
169On systems other than
170.Ox ,
171the
172.Dv LC_NUMERIC
173.Xr locale 1
174category can cause parsing failures; see CAVEATS in
175.Xr setlocale 3
176for details.
diff --git a/src/lib/libc/stdlib/strtoimax.c b/src/lib/libc/stdlib/strtoimax.c
deleted file mode 100644
index 74e355626a..0000000000
--- a/src/lib/libc/stdlib/strtoimax.c
+++ /dev/null
@@ -1,151 +0,0 @@
1/* $OpenBSD: strtoimax.c,v 1.4 2017/07/06 16:23:11 millert Exp $ */
2/*
3 * Copyright (c) 1992 The Regents of the University of California.
4 * 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. Neither the name of the University nor the names of its contributors
15 * may be used to endorse or promote products derived from this software
16 * without specific prior written permission.
17 *
18 * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
19 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
20 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
21 * ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
22 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
23 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
24 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
25 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
26 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
27 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
28 * SUCH DAMAGE.
29 */
30
31#include <ctype.h>
32#include <errno.h>
33#include <inttypes.h>
34
35/*
36 * Convert a string to an intmax_t
37 *
38 * Ignores `locale' stuff. Assumes that the upper and lower case
39 * alphabets and digits are each contiguous.
40 */
41intmax_t
42strtoimax(const char *nptr, char **endptr, int base)
43{
44 const char *s;
45 intmax_t acc, cutoff;
46 int c;
47 int neg, any, cutlim;
48
49 /*
50 * Ensure that base is between 2 and 36 inclusive, or the special
51 * value of 0.
52 */
53 if (base < 0 || base == 1 || base > 36) {
54 if (endptr != 0)
55 *endptr = (char *)nptr;
56 errno = EINVAL;
57 return 0;
58 }
59
60 /*
61 * Skip white space and pick up leading +/- sign if any.
62 * If base is 0, allow 0x for hex and 0 for octal, else
63 * assume decimal; if base is already 16, allow 0x.
64 */
65 s = nptr;
66 do {
67 c = (unsigned char) *s++;
68 } while (isspace(c));
69 if (c == '-') {
70 neg = 1;
71 c = *s++;
72 } else {
73 neg = 0;
74 if (c == '+')
75 c = *s++;
76 }
77 if ((base == 0 || base == 16) && c == '0' &&
78 (*s == 'x' || *s == 'X') && isxdigit((unsigned char)s[1])) {
79 c = s[1];
80 s += 2;
81 base = 16;
82 }
83 if (base == 0)
84 base = c == '0' ? 8 : 10;
85
86 /*
87 * Compute the cutoff value between legal numbers and illegal
88 * numbers. That is the largest legal value, divided by the
89 * base. An input number that is greater than this value, if
90 * followed by a legal input character, is too big. One that
91 * is equal to this value may be valid or not; the limit
92 * between valid and invalid numbers is then based on the last
93 * digit. For instance, if the range for intmax_t is
94 * [-9223372036854775808..9223372036854775807] and the input base
95 * is 10, cutoff will be set to 922337203685477580 and cutlim to
96 * either 7 (neg==0) or 8 (neg==1), meaning that if we have
97 * accumulated a value > 922337203685477580, or equal but the
98 * next digit is > 7 (or 8), the number is too big, and we will
99 * return a range error.
100 *
101 * Set any if any `digits' consumed; make it negative to indicate
102 * overflow.
103 */
104 cutoff = neg ? INTMAX_MIN : INTMAX_MAX;
105 cutlim = cutoff % base;
106 cutoff /= base;
107 if (neg) {
108 if (cutlim > 0) {
109 cutlim -= base;
110 cutoff += 1;
111 }
112 cutlim = -cutlim;
113 }
114 for (acc = 0, any = 0;; c = (unsigned char) *s++) {
115 if (isdigit(c))
116 c -= '0';
117 else if (isalpha(c))
118 c -= isupper(c) ? 'A' - 10 : 'a' - 10;
119 else
120 break;
121 if (c >= base)
122 break;
123 if (any < 0)
124 continue;
125 if (neg) {
126 if (acc < cutoff || (acc == cutoff && c > cutlim)) {
127 any = -1;
128 acc = INTMAX_MIN;
129 errno = ERANGE;
130 } else {
131 any = 1;
132 acc *= base;
133 acc -= c;
134 }
135 } else {
136 if (acc > cutoff || (acc == cutoff && c > cutlim)) {
137 any = -1;
138 acc = INTMAX_MAX;
139 errno = ERANGE;
140 } else {
141 any = 1;
142 acc *= base;
143 acc += c;
144 }
145 }
146 }
147 if (endptr != 0)
148 *endptr = (char *) (any ? s - 1 : nptr);
149 return (acc);
150}
151DEF_STRONG(strtoimax);
diff --git a/src/lib/libc/stdlib/strtol.3 b/src/lib/libc/stdlib/strtol.3
deleted file mode 100644
index 92774d082c..0000000000
--- a/src/lib/libc/stdlib/strtol.3
+++ /dev/null
@@ -1,273 +0,0 @@
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. 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.\" $OpenBSD: strtol.3,v 1.27 2015/04/14 22:16:03 nicm Exp $
33.\"
34.Dd $Mdocdate: April 14 2015 $
35.Dt STRTOL 3
36.Os
37.Sh NAME
38.Nm strtol ,
39.Nm strtoll ,
40.Nm strtoimax ,
41.Nm strtoq
42.Nd convert string value to a long, long long or intmax_t integer
43.Sh SYNOPSIS
44.In limits.h
45.In stdlib.h
46.Ft long
47.Fn strtol "const char *nptr" "char **endptr" "int base"
48.Ft long long
49.Fn strtoll "const char *nptr" "char **endptr" "int base"
50.In inttypes.h
51.Ft intmax_t
52.Fn strtoimax "const char *nptr" "char **endptr" "int base"
53.In sys/types.h
54.In limits.h
55.In stdlib.h
56.Ft quad_t
57.Fn strtoq "const char *nptr" "char **endptr" "int base"
58.Sh DESCRIPTION
59The
60.Fn strtol
61function converts the string in
62.Fa nptr
63to a
64.Vt long
65value.
66The
67.Fn strtoll
68function converts the string in
69.Fa nptr
70to a
71.Vt long long
72value.
73The
74.Fn strtoimax
75function converts the string in
76.Fa nptr
77to an
78.Vt intmax_t
79value.
80The
81.Fn strtoq
82function is a deprecated equivalent of
83.Fn strtoll
84and is provided for backwards compatibility with legacy programs.
85The conversion is done according to the given
86.Fa base ,
87which must be a number between 2 and 36 inclusive or the special value 0.
88.Pp
89The string may begin with an arbitrary amount of whitespace
90(as determined by
91.Xr isspace 3 )
92followed by a single optional
93.Ql +
94or
95.Ql -
96sign.
97If
98.Fa base
99is zero or 16, the string may then include a
100.Ql 0x
101prefix, and the number will be read in base 16; otherwise, a zero
102.Fa base
103is taken as 10 (decimal) unless the next character is
104.Ql 0 ,
105in which case it is taken as 8 (octal).
106.Pp
107The remainder of the string is converted to a
108.Vt long ,
109.Vt long long ,
110or
111.Vt intmax_t
112value in the obvious manner,
113stopping at the first character which is not a valid digit
114in the given base.
115(In bases above 10, the letter
116.Ql A
117in either upper or lower case represents 10,
118.Ql B
119represents 11, and so forth, with
120.Ql Z
121representing 35.)
122.Pp
123If
124.Fa endptr
125is non-null,
126.Fn strtol
127stores the address of the first invalid character in
128.Fa *endptr .
129If there were no digits at all, however,
130.Fn strtol
131stores the original value of
132.Fa nptr
133in
134.Fa *endptr .
135(Thus, if
136.Fa *nptr
137is not
138.Ql \e0
139but
140.Fa **endptr
141is
142.Ql \e0
143on return, the entire string was valid.)
144.Sh RETURN VALUES
145The
146.Fn strtol ,
147.Fn strtoll ,
148.Fn strtoimax ,
149and
150.Fn strtoq
151functions return the result of the conversion.
152If overflow or underflow occurs,
153.Va errno
154is set to
155.Er ERANGE
156and the function return value is as follows:
157.Bl -column "strtoimaxXX" "INTMAX_MIN" "INTMAX_MAX" -offset indent
158.It Sy Function Ta Sy underflow Ta Sy overflow
159.It Fn strtol Ta Dv LONG_MIN Ta Dv LONG_MAX
160.It Fn strtoll Ta Dv LLONG_MIN Ta Dv LLONG_MAX
161.It Fn strtoimax Ta Dv INTMAX_MIN Ta Dv INTMAX_MAX
162.It Fn strtoq Ta Dv LLONG_MIN Ta Dv LLONG_MAX
163.El
164.Pp
165If there is no valid digit, 0 is returned.
166If
167.Ar base
168is invalid, 0 is returned and the global variable
169.Va errno
170is set to
171.Er EINVAL .
172.Sh EXAMPLES
173Ensuring that a string is a valid number (i.e., in range and containing no
174trailing characters) requires clearing
175.Va errno
176beforehand explicitly since
177.Va errno
178is not changed on a successful call to
179.Fn strtol ,
180and the return value of
181.Fn strtol
182cannot be used unambiguously to signal an error:
183.Bd -literal -offset indent
184char *ep;
185long lval;
186
187\&...
188
189errno = 0;
190lval = strtol(buf, &ep, 10);
191if (buf[0] == '\e0' || *ep != '\e0')
192 goto not_a_number;
193if (errno == ERANGE && (lval == LONG_MAX || lval == LONG_MIN))
194 goto out_of_range;
195.Ed
196.Pp
197This example will accept
198.Dq 12
199but not
200.Dq 12foo
201or
202.Dq 12\en .
203If trailing whitespace is acceptable, further checks must be done on
204.Va *ep ;
205alternately, use
206.Xr sscanf 3 .
207.Pp
208If
209.Fn strtol
210is being used instead of
211.Xr atoi 3 ,
212error checking is further complicated because the desired return value is an
213.Vt int
214rather than a
215.Vt long ;
216however, on some architectures integers and long integers are the same size.
217Thus the following is necessary:
218.Bd -literal -offset indent
219char *ep;
220int ival;
221long lval;
222
223\&...
224
225errno = 0;
226lval = strtol(buf, &ep, 10);
227if (buf[0] == '\e0' || *ep != '\e0')
228 goto not_a_number;
229if ((errno == ERANGE && (lval == LONG_MAX || lval == LONG_MIN)) ||
230 (lval > INT_MAX || lval < INT_MIN))
231 goto out_of_range;
232ival = lval;
233.Ed
234.Sh ERRORS
235.Bl -tag -width Er
236.It Bq Er EINVAL
237The value of
238.Ar base
239was neither between 2 and 36 inclusive nor the special value 0.
240.It Bq Er ERANGE
241The given string was out of range; the value converted has been clamped.
242.El
243.Sh SEE ALSO
244.Xr atof 3 ,
245.Xr atoi 3 ,
246.Xr atol 3 ,
247.Xr atoll 3 ,
248.Xr sscanf 3 ,
249.Xr strtod 3 ,
250.Xr strtonum 3 ,
251.Xr strtoul 3
252.Sh STANDARDS
253The
254.Fn strtol ,
255.Fn strtoll ,
256and
257.Fn strtoimax
258functions conform to
259.St -isoC-99 .
260Setting
261.Va errno
262to
263.Dv EINVAL
264is an extension to that standard required by
265.St -p1003.1-2008 .
266.Pp
267The
268.Fn strtoq
269function is a
270.Bx
271extension and is provided for backwards compatibility with legacy programs.
272.Sh BUGS
273Ignores the current locale.
diff --git a/src/lib/libc/stdlib/strtol.c b/src/lib/libc/stdlib/strtol.c
deleted file mode 100644
index 599d2355a8..0000000000
--- a/src/lib/libc/stdlib/strtol.c
+++ /dev/null
@@ -1,151 +0,0 @@
1/* $OpenBSD: strtol.c,v 1.12 2017/07/06 16:23:11 millert Exp $ */
2/*
3 * Copyright (c) 1990 The Regents of the University of California.
4 * 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. Neither the name of the University nor the names of its contributors
15 * may be used to endorse or promote products derived from this software
16 * without specific prior written permission.
17 *
18 * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
19 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
20 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
21 * ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
22 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
23 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
24 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
25 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
26 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
27 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
28 * SUCH DAMAGE.
29 */
30
31#include <ctype.h>
32#include <errno.h>
33#include <limits.h>
34#include <stdlib.h>
35
36/*
37 * Convert a string to a long integer.
38 *
39 * Ignores `locale' stuff. Assumes that the upper and lower case
40 * alphabets and digits are each contiguous.
41 */
42long
43strtol(const char *nptr, char **endptr, int base)
44{
45 const char *s;
46 long acc, cutoff;
47 int c;
48 int neg, any, cutlim;
49
50 /*
51 * Ensure that base is between 2 and 36 inclusive, or the special
52 * value of 0.
53 */
54 if (base < 0 || base == 1 || base > 36) {
55 if (endptr != 0)
56 *endptr = (char *)nptr;
57 errno = EINVAL;
58 return 0;
59 }
60
61 /*
62 * Skip white space and pick up leading +/- sign if any.
63 * If base is 0, allow 0x for hex and 0 for octal, else
64 * assume decimal; if base is already 16, allow 0x.
65 */
66 s = nptr;
67 do {
68 c = (unsigned char) *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) && c == '0' &&
79 (*s == 'x' || *s == 'X') && isxdigit((unsigned char)s[1])) {
80 c = s[1];
81 s += 2;
82 base = 16;
83 }
84 if (base == 0)
85 base = c == '0' ? 8 : 10;
86
87 /*
88 * Compute the cutoff value between legal numbers and illegal
89 * numbers. That is the largest legal value, divided by the
90 * base. An input number that is greater than this value, if
91 * followed by a legal input character, is too big. One that
92 * is equal to this value may be valid or not; the limit
93 * between valid and invalid numbers is then based on the last
94 * digit. For instance, if the range for longs is
95 * [-2147483648..2147483647] and the input base is 10,
96 * cutoff will be set to 214748364 and cutlim to either
97 * 7 (neg==0) or 8 (neg==1), meaning that if we have accumulated
98 * a value > 214748364, or equal but the next digit is > 7 (or 8),
99 * the number is too big, and we will return a range error.
100 *
101 * Set any if any `digits' consumed; make it negative to indicate
102 * overflow.
103 */
104 cutoff = neg ? LONG_MIN : LONG_MAX;
105 cutlim = cutoff % base;
106 cutoff /= base;
107 if (neg) {
108 if (cutlim > 0) {
109 cutlim -= base;
110 cutoff += 1;
111 }
112 cutlim = -cutlim;
113 }
114 for (acc = 0, any = 0;; c = (unsigned char) *s++) {
115 if (isdigit(c))
116 c -= '0';
117 else if (isalpha(c))
118 c -= isupper(c) ? 'A' - 10 : 'a' - 10;
119 else
120 break;
121 if (c >= base)
122 break;
123 if (any < 0)
124 continue;
125 if (neg) {
126 if (acc < cutoff || (acc == cutoff && c > cutlim)) {
127 any = -1;
128 acc = LONG_MIN;
129 errno = ERANGE;
130 } else {
131 any = 1;
132 acc *= base;
133 acc -= c;
134 }
135 } else {
136 if (acc > cutoff || (acc == cutoff && c > cutlim)) {
137 any = -1;
138 acc = LONG_MAX;
139 errno = ERANGE;
140 } else {
141 any = 1;
142 acc *= base;
143 acc += c;
144 }
145 }
146 }
147 if (endptr != 0)
148 *endptr = (char *) (any ? s - 1 : nptr);
149 return (acc);
150}
151DEF_STRONG(strtol);
diff --git a/src/lib/libc/stdlib/strtoll.c b/src/lib/libc/stdlib/strtoll.c
deleted file mode 100644
index d21a249a0b..0000000000
--- a/src/lib/libc/stdlib/strtoll.c
+++ /dev/null
@@ -1,156 +0,0 @@
1/* $OpenBSD: strtoll.c,v 1.10 2017/07/06 16:23:11 millert Exp $ */
2/*
3 * Copyright (c) 1992 The Regents of the University of California.
4 * 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. Neither the name of the University nor the names of its contributors
15 * may be used to endorse or promote products derived from this software
16 * without specific prior written permission.
17 *
18 * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
19 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
20 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
21 * ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
22 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
23 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
24 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
25 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
26 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
27 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
28 * SUCH DAMAGE.
29 */
30
31#include <sys/types.h>
32
33#include <ctype.h>
34#include <errno.h>
35#include <limits.h>
36#include <stdlib.h>
37
38/*
39 * Convert a string to a long long.
40 *
41 * Ignores `locale' stuff. Assumes that the upper and lower case
42 * alphabets and digits are each contiguous.
43 */
44long long
45strtoll(const char *nptr, char **endptr, int base)
46{
47 const char *s;
48 long long acc, cutoff;
49 int c;
50 int neg, any, cutlim;
51
52 /*
53 * Ensure that base is between 2 and 36 inclusive, or the special
54 * value of 0.
55 */
56 if (base < 0 || base == 1 || base > 36) {
57 if (endptr != 0)
58 *endptr = (char *)nptr;
59 errno = EINVAL;
60 return 0;
61 }
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 = (unsigned char) *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) && c == '0' &&
81 (*s == 'x' || *s == 'X') && isxdigit((unsigned char)s[1])) {
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 long longs 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 cutoff = neg ? LLONG_MIN : LLONG_MAX;
108 cutlim = cutoff % base;
109 cutoff /= base;
110 if (neg) {
111 if (cutlim > 0) {
112 cutlim -= base;
113 cutoff += 1;
114 }
115 cutlim = -cutlim;
116 }
117 for (acc = 0, any = 0;; c = (unsigned char) *s++) {
118 if (isdigit(c))
119 c -= '0';
120 else if (isalpha(c))
121 c -= isupper(c) ? 'A' - 10 : 'a' - 10;
122 else
123 break;
124 if (c >= base)
125 break;
126 if (any < 0)
127 continue;
128 if (neg) {
129 if (acc < cutoff || (acc == cutoff && c > cutlim)) {
130 any = -1;
131 acc = LLONG_MIN;
132 errno = ERANGE;
133 } else {
134 any = 1;
135 acc *= base;
136 acc -= c;
137 }
138 } else {
139 if (acc > cutoff || (acc == cutoff && c > cutlim)) {
140 any = -1;
141 acc = LLONG_MAX;
142 errno = ERANGE;
143 } else {
144 any = 1;
145 acc *= base;
146 acc += c;
147 }
148 }
149 }
150 if (endptr != 0)
151 *endptr = (char *) (any ? s - 1 : nptr);
152 return (acc);
153}
154DEF_STRONG(strtoll);
155
156__weak_alias(strtoq, strtoll);
diff --git a/src/lib/libc/stdlib/strtonum.3 b/src/lib/libc/stdlib/strtonum.3
deleted file mode 100644
index 87a3ccd588..0000000000
--- a/src/lib/libc/stdlib/strtonum.3
+++ /dev/null
@@ -1,152 +0,0 @@
1.\" $OpenBSD: strtonum.3,v 1.19 2022/09/11 06:38:11 jmc Exp $
2.\"
3.\" Copyright (c) 2004 Ted Unangst
4.\"
5.\" Permission to use, copy, modify, and distribute this software for any
6.\" purpose with or without fee is hereby granted, provided that the above
7.\" copyright notice and this permission notice appear in all copies.
8.\"
9.\" THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES
10.\" WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF
11.\" MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR
12.\" ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES
13.\" WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN
14.\" ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF
15.\" OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE.
16.\"
17.Dd $Mdocdate: September 11 2022 $
18.Dt STRTONUM 3
19.Os
20.Sh NAME
21.Nm strtonum
22.Nd reliably convert string value to an integer
23.Sh SYNOPSIS
24.In stdlib.h
25.Ft long long
26.Fo strtonum
27.Fa "const char *nptr"
28.Fa "long long minval"
29.Fa "long long maxval"
30.Fa "const char **errstr"
31.Fc
32.Sh DESCRIPTION
33The
34.Fn strtonum
35function converts the string in
36.Fa nptr
37to a
38.Vt long long
39value.
40The
41.Fn strtonum
42function was designed to facilitate safe, robust programming
43and overcome the shortcomings of the
44.Xr atoi 3
45and
46.Xr strtol 3
47family of interfaces.
48.Pp
49The string may begin with an arbitrary amount of whitespace
50(as determined by
51.Xr isspace 3 )
52followed by a single optional
53.Ql +
54or
55.Ql -
56sign.
57.Pp
58The remainder of the string is converted to a
59.Vt long long
60value according to base 10.
61.Pp
62The value obtained is then checked against the provided
63.Fa minval
64and
65.Fa maxval
66bounds.
67If
68.Fa errstr
69is non-null,
70.Fn strtonum
71stores an error string in
72.Fa *errstr
73indicating the failure.
74.Sh RETURN VALUES
75The
76.Fn strtonum
77function returns the result of the conversion,
78unless the value would exceed the provided bounds or is invalid.
79On error, 0 is returned,
80.Va errno
81is set, and
82.Fa errstr
83will point to an error message.
84.Fa *errstr
85will be set to
86.Dv NULL
87on success;
88this fact can be used to differentiate
89a successful return of 0 from an error.
90.Sh EXAMPLES
91Using
92.Fn strtonum
93correctly is meant to be simpler than the alternative functions.
94.Bd -literal -offset indent
95int iterations;
96const char *errstr;
97
98iterations = strtonum(optarg, 1, 64, &errstr);
99if (errstr != NULL)
100 errx(1, "number of iterations is %s: %s", errstr, optarg);
101.Ed
102.Pp
103The above example will guarantee that the value of iterations is between
1041 and 64 (inclusive).
105.Sh ERRORS
106.Bl -tag -width Er
107.It Bq Er ERANGE
108The given string was out of range.
109.It Bq Er EINVAL
110The given string did not consist solely of digit characters.
111.It Bq Er EINVAL
112.Ar minval
113was larger than
114.Ar maxval .
115.El
116.Pp
117If an error occurs,
118.Fa errstr
119will be set to one of the following strings:
120.Pp
121.Bl -tag -width "too largeXX" -compact
122.It Qq too large
123The result was larger than the provided maximum value.
124.It Qq too small
125The result was smaller than the provided minimum value.
126.It Qq invalid
127The string did not consist solely of digit characters.
128.El
129.Sh SEE ALSO
130.Xr atof 3 ,
131.Xr atoi 3 ,
132.Xr atol 3 ,
133.Xr atoll 3 ,
134.Xr sscanf 3 ,
135.Xr strtod 3 ,
136.Xr strtol 3 ,
137.Xr strtoul 3
138.Sh STANDARDS
139.Fn strtonum
140is an
141.Ox
142extension.
143The existing alternatives, such as
144.Xr atoi 3
145and
146.Xr strtol 3 ,
147are either impossible or difficult to use safely.
148.Sh HISTORY
149The
150.Fn strtonum
151function first appeared in
152.Ox 3.6 .
diff --git a/src/lib/libc/stdlib/strtonum.c b/src/lib/libc/stdlib/strtonum.c
deleted file mode 100644
index ad22d1c30c..0000000000
--- a/src/lib/libc/stdlib/strtonum.c
+++ /dev/null
@@ -1,66 +0,0 @@
1/* $OpenBSD: strtonum.c,v 1.8 2015/09/13 08:31:48 guenther Exp $ */
2
3/*
4 * Copyright (c) 2004 Ted Unangst and Todd Miller
5 * All rights reserved.
6 *
7 * Permission to use, copy, modify, and distribute this software for any
8 * purpose with or without fee is hereby granted, provided that the above
9 * copyright notice and this permission notice appear in all copies.
10 *
11 * THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES
12 * WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF
13 * MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR
14 * ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES
15 * WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN
16 * ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF
17 * OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE.
18 */
19
20#include <errno.h>
21#include <limits.h>
22#include <stdlib.h>
23
24#define INVALID 1
25#define TOOSMALL 2
26#define TOOLARGE 3
27
28long long
29strtonum(const char *numstr, long long minval, long long maxval,
30 const char **errstrp)
31{
32 long long ll = 0;
33 int error = 0;
34 char *ep;
35 struct errval {
36 const char *errstr;
37 int err;
38 } ev[4] = {
39 { NULL, 0 },
40 { "invalid", EINVAL },
41 { "too small", ERANGE },
42 { "too large", ERANGE },
43 };
44
45 ev[0].err = errno;
46 errno = 0;
47 if (minval > maxval) {
48 error = INVALID;
49 } else {
50 ll = strtoll(numstr, &ep, 10);
51 if (numstr == ep || *ep != '\0')
52 error = INVALID;
53 else if ((ll == LLONG_MIN && errno == ERANGE) || ll < minval)
54 error = TOOSMALL;
55 else if ((ll == LLONG_MAX && errno == ERANGE) || ll > maxval)
56 error = TOOLARGE;
57 }
58 if (errstrp != NULL)
59 *errstrp = ev[error].errstr;
60 errno = ev[error].err;
61 if (error)
62 ll = 0;
63
64 return (ll);
65}
66DEF_WEAK(strtonum);
diff --git a/src/lib/libc/stdlib/strtoul.3 b/src/lib/libc/stdlib/strtoul.3
deleted file mode 100644
index dd5668d1d6..0000000000
--- a/src/lib/libc/stdlib/strtoul.3
+++ /dev/null
@@ -1,260 +0,0 @@
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. 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.\" $OpenBSD: strtoul.3,v 1.24 2014/11/30 21:21:59 schwarze Exp $
33.\"
34.Dd $Mdocdate: November 30 2014 $
35.Dt STRTOUL 3
36.Os
37.Sh NAME
38.Nm strtoul ,
39.Nm strtoull ,
40.Nm strtoumax ,
41.Nm strtouq
42.Nd convert a string to an unsigned long, unsigned long long or uintmax_t integer
43.Sh SYNOPSIS
44.In limits.h
45.In stdlib.h
46.Ft unsigned long
47.Fn strtoul "const char *nptr" "char **endptr" "int base"
48.Ft unsigned long long
49.Fn strtoull "const char *nptr" "char **endptr" "int base"
50.In inttypes.h
51.Ft uintmax_t
52.Fn strtoumax "const char *nptr" "char **endptr" "int base"
53.In sys/types.h
54.In limits.h
55.In stdlib.h
56.Ft u_quad_t
57.Fn strtouq "const char *nptr" "char **endptr" "int base"
58.Sh DESCRIPTION
59The
60.Fn strtoul
61function converts the string in
62.Fa nptr
63to an
64.Vt unsigned long
65value.
66The
67.Fn strtoull
68function converts the string in
69.Fa nptr
70to an
71.Vt unsigned long long
72value.
73The
74.Fn strtoumax
75function converts the string in
76.Fa nptr
77to a
78.Vt umaxint_t
79value.
80The
81.Fn strtouq
82function is a deprecated equivalent of
83.Fn strtoull
84and is provided for backwards compatibility with legacy programs.
85The conversion is done according to the given
86.Fa base ,
87which must be a number between 2 and 36 inclusive or the special value 0.
88If the string in
89.Fa nptr
90represents a negative number, it will be converted to its unsigned equivalent.
91This behavior is consistent with what happens when a signed integer type is
92cast to its unsigned counterpart.
93.Pp
94The string may begin with an arbitrary amount of whitespace
95(as determined by
96.Xr isspace 3 )
97followed by a single optional
98.Ql +
99or
100.Ql -
101sign.
102If
103.Fa base
104is zero or 16, the string may then include a
105.Ql 0x
106prefix, and the number will be read in base 16; otherwise, a zero
107.Fa base
108is taken as 10 (decimal) unless the next character is
109.Ql 0 ,
110in which case it is taken as 8 (octal).
111.Pp
112The remainder of the string is converted to an
113.Vt unsigned long ,
114.Vt unsigned long long ,
115or
116.Vt uintmax_t
117value in the obvious manner,
118stopping at the first character which is not a valid digit
119in the given base.
120(In bases above 10, the letter
121.Ql A
122in either upper or lower case represents 10,
123.Ql B
124represents 11, and so forth, with
125.Ql Z
126representing 35.)
127.Pp
128If
129.Fa endptr
130is non-null,
131.Fn strtoul
132stores the address of the first invalid character in
133.Fa *endptr .
134If there were no digits at all, however,
135.Fn strtoul
136stores the original value of
137.Fa nptr
138in
139.Fa *endptr .
140(Thus, if
141.Fa *nptr
142is not
143.Ql \e0
144but
145.Fa **endptr
146is
147.Ql \e0
148on return, the entire string was valid.)
149.Sh RETURN VALUES
150The
151.Fn strtoul ,
152.Fn strtoull ,
153.Fn strtoumax
154and
155.Fn strtouq
156functions return either the result of the conversion or,
157if there was a leading minus sign,
158the negation of the result of the conversion,
159unless the original (non-negated) value would overflow.
160If overflow occurs,
161.Fn strtoul
162returns
163.Dv ULONG_MAX ,
164.Fn strtoull
165returns
166.Dv ULLONG_MAX ,
167.Fn strtoumax
168returns
169.Dv UINTMAX_MAX ,
170.Fn strtouq
171returns
172.Dv ULLONG_MAX
173and the global variable
174.Va errno
175is set to
176.Er ERANGE .
177.Pp
178There is no way to determine if
179.Fn strtoul
180has processed a negative number (and returned an unsigned value) short of
181examining the string in
182.Fa nptr
183directly.
184.Pp
185If there is no valid digit, 0 is returned.
186If
187.Ar base
188is invalid, 0 is returned and the global variable
189.Va errno
190is set to
191.Er EINVAL .
192.Sh EXAMPLES
193Ensuring that a string is a valid number (i.e., in range and containing no
194trailing characters) requires clearing
195.Va errno
196beforehand explicitly since
197.Va errno
198is not changed on a successful call to
199.Fn strtoul ,
200and the return value of
201.Fn strtoul
202cannot be used unambiguously to signal an error:
203.Bd -literal -offset indent
204char *ep;
205unsigned long ulval;
206
207\&...
208
209errno = 0;
210ulval = strtoul(buf, &ep, 10);
211if (buf[0] == '\e0' || *ep != '\e0')
212 goto not_a_number;
213if (errno == ERANGE && ulval == ULONG_MAX)
214 goto out_of_range;
215.Ed
216.Pp
217This example will accept
218.Dq 12
219but not
220.Dq 12foo
221or
222.Dq 12\en .
223If trailing whitespace is acceptable, further checks must be done on
224.Va *ep ;
225alternately, use
226.Xr sscanf 3 .
227.Sh ERRORS
228.Bl -tag -width Er
229.It Bq Er EINVAL
230The value of
231.Ar base
232was neither between 2 and 36 inclusive nor the special value 0.
233.It Bq Er ERANGE
234The given string was out of range; the value converted has been clamped.
235.El
236.Sh SEE ALSO
237.Xr sscanf 3 ,
238.Xr strtol 3
239.Sh STANDARDS
240The
241.Fn strtoul ,
242.Fn strtoull ,
243and
244.Fn strtoumax
245functions conform to
246.St -isoC-99 .
247Setting
248.Va errno
249to
250.Dv EINVAL
251is an extension to that standard required by
252.St -p1003.1-2008 .
253.Pp
254The
255.Fn strtouq
256function is a
257.Bx
258extension and is provided for backwards compatibility with legacy programs.
259.Sh BUGS
260Ignores the current locale.
diff --git a/src/lib/libc/stdlib/strtoul.c b/src/lib/libc/stdlib/strtoul.c
deleted file mode 100644
index 6667bea8fa..0000000000
--- a/src/lib/libc/stdlib/strtoul.c
+++ /dev/null
@@ -1,110 +0,0 @@
1/* $OpenBSD: strtoul.c,v 1.11 2017/07/06 16:23:11 millert Exp $ */
2/*
3 * Copyright (c) 1990 The Regents of the University of California.
4 * 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. Neither the name of the University nor the names of its contributors
15 * may be used to endorse or promote products derived from this software
16 * without specific prior written permission.
17 *
18 * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
19 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
20 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
21 * ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
22 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
23 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
24 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
25 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
26 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
27 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
28 * SUCH DAMAGE.
29 */
30
31#include <ctype.h>
32#include <errno.h>
33#include <limits.h>
34#include <stdlib.h>
35
36/*
37 * Convert a string to an unsigned long integer.
38 *
39 * Ignores `locale' stuff. Assumes that the upper and lower case
40 * alphabets and digits are each contiguous.
41 */
42unsigned long
43strtoul(const char *nptr, char **endptr, int base)
44{
45 const char *s;
46 unsigned long acc, cutoff;
47 int c;
48 int neg, any, cutlim;
49
50 /*
51 * See strtol for comments as to the logic used.
52 */
53 if (base < 0 || base == 1 || base > 36) {
54 if (endptr != 0)
55 *endptr = (char *)nptr;
56 errno = EINVAL;
57 return 0;
58 }
59
60 s = nptr;
61 do {
62 c = (unsigned char) *s++;
63 } while (isspace(c));
64 if (c == '-') {
65 neg = 1;
66 c = *s++;
67 } else {
68 neg = 0;
69 if (c == '+')
70 c = *s++;
71 }
72 if ((base == 0 || base == 16) && c == '0' &&
73 (*s == 'x' || *s == 'X') && isxdigit((unsigned char)s[1])) {
74 c = s[1];
75 s += 2;
76 base = 16;
77 }
78 if (base == 0)
79 base = c == '0' ? 8 : 10;
80
81 cutoff = ULONG_MAX / (unsigned long)base;
82 cutlim = ULONG_MAX % (unsigned long)base;
83 for (acc = 0, any = 0;; c = (unsigned char) *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)
93 continue;
94 if (acc > cutoff || (acc == cutoff && c > cutlim)) {
95 any = -1;
96 acc = ULONG_MAX;
97 errno = ERANGE;
98 } else {
99 any = 1;
100 acc *= (unsigned long)base;
101 acc += c;
102 }
103 }
104 if (neg && any > 0)
105 acc = -acc;
106 if (endptr != 0)
107 *endptr = (char *) (any ? s - 1 : nptr);
108 return (acc);
109}
110DEF_STRONG(strtoul);
diff --git a/src/lib/libc/stdlib/strtoull.c b/src/lib/libc/stdlib/strtoull.c
deleted file mode 100644
index d7733e4003..0000000000
--- a/src/lib/libc/stdlib/strtoull.c
+++ /dev/null
@@ -1,114 +0,0 @@
1/* $OpenBSD: strtoull.c,v 1.9 2017/07/06 16:23:11 millert Exp $ */
2/*
3 * Copyright (c) 1992 The Regents of the University of California.
4 * 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. Neither the name of the University nor the names of its contributors
15 * may be used to endorse or promote products derived from this software
16 * without specific prior written permission.
17 *
18 * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
19 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
20 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
21 * ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
22 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
23 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
24 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
25 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
26 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
27 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
28 * SUCH DAMAGE.
29 */
30
31#include <sys/types.h>
32
33#include <ctype.h>
34#include <errno.h>
35#include <limits.h>
36#include <stdlib.h>
37
38/*
39 * Convert a string to an unsigned long long.
40 *
41 * Ignores `locale' stuff. Assumes that the upper and lower case
42 * alphabets and digits are each contiguous.
43 */
44unsigned long long
45strtoull(const char *nptr, char **endptr, int base)
46{
47 const char *s;
48 unsigned long long acc, cutoff;
49 int c;
50 int neg, any, cutlim;
51
52 /*
53 * See strtoll for comments as to the logic used.
54 */
55 if (base < 0 || base == 1 || base > 36) {
56 if (endptr != 0)
57 *endptr = (char *)nptr;
58 errno = EINVAL;
59 return 0;
60 }
61
62 s = nptr;
63 do {
64 c = (unsigned char) *s++;
65 } while (isspace(c));
66 if (c == '-') {
67 neg = 1;
68 c = *s++;
69 } else {
70 neg = 0;
71 if (c == '+')
72 c = *s++;
73 }
74 if ((base == 0 || base == 16) && c == '0' &&
75 (*s == 'x' || *s == 'X') && isxdigit((unsigned char)s[1])) {
76 c = s[1];
77 s += 2;
78 base = 16;
79 }
80 if (base == 0)
81 base = c == '0' ? 8 : 10;
82
83 cutoff = ULLONG_MAX / (unsigned long long)base;
84 cutlim = ULLONG_MAX % (unsigned long long)base;
85 for (acc = 0, any = 0;; c = (unsigned char) *s++) {
86 if (isdigit(c))
87 c -= '0';
88 else if (isalpha(c))
89 c -= isupper(c) ? 'A' - 10 : 'a' - 10;
90 else
91 break;
92 if (c >= base)
93 break;
94 if (any < 0)
95 continue;
96 if (acc > cutoff || (acc == cutoff && c > cutlim)) {
97 any = -1;
98 acc = ULLONG_MAX;
99 errno = ERANGE;
100 } else {
101 any = 1;
102 acc *= (unsigned long long)base;
103 acc += c;
104 }
105 }
106 if (neg && any > 0)
107 acc = -acc;
108 if (endptr != 0)
109 *endptr = (char *) (any ? s - 1 : nptr);
110 return (acc);
111}
112DEF_STRONG(strtoull);
113
114__weak_alias(strtouq, strtoull);
diff --git a/src/lib/libc/stdlib/strtoumax.c b/src/lib/libc/stdlib/strtoumax.c
deleted file mode 100644
index 348184c1ac..0000000000
--- a/src/lib/libc/stdlib/strtoumax.c
+++ /dev/null
@@ -1,109 +0,0 @@
1/* $OpenBSD: strtoumax.c,v 1.4 2017/07/06 16:23:11 millert Exp $ */
2/*
3 * Copyright (c) 1992 The Regents of the University of California.
4 * 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. Neither the name of the University nor the names of its contributors
15 * may be used to endorse or promote products derived from this software
16 * without specific prior written permission.
17 *
18 * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
19 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
20 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
21 * ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
22 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
23 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
24 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
25 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
26 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
27 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
28 * SUCH DAMAGE.
29 */
30
31#include <ctype.h>
32#include <errno.h>
33#include <inttypes.h>
34
35/*
36 * Convert a string to a uintmax_t.
37 *
38 * Ignores `locale' stuff. Assumes that the upper and lower case
39 * alphabets and digits are each contiguous.
40 */
41uintmax_t
42strtoumax(const char *nptr, char **endptr, int base)
43{
44 const char *s;
45 uintmax_t acc, cutoff;
46 int c;
47 int neg, any, cutlim;
48
49 /*
50 * See strtoimax for comments as to the logic used.
51 */
52 if (base < 0 || base == 1 || base > 36) {
53 if (endptr != 0)
54 *endptr = (char *)nptr;
55 errno = EINVAL;
56 return 0;
57 }
58
59 s = nptr;
60 do {
61 c = (unsigned char) *s++;
62 } while (isspace(c));
63 if (c == '-') {
64 neg = 1;
65 c = *s++;
66 } else {
67 neg = 0;
68 if (c == '+')
69 c = *s++;
70 }
71 if ((base == 0 || base == 16) && c == '0' &&
72 (*s == 'x' || *s == 'X') && isxdigit((unsigned char)s[1])) {
73 c = s[1];
74 s += 2;
75 base = 16;
76 }
77 if (base == 0)
78 base = c == '0' ? 8 : 10;
79
80 cutoff = UINTMAX_MAX / (uintmax_t)base;
81 cutlim = UINTMAX_MAX % (uintmax_t)base;
82 for (acc = 0, any = 0;; c = (unsigned char) *s++) {
83 if (isdigit(c))
84 c -= '0';
85 else if (isalpha(c))
86 c -= isupper(c) ? 'A' - 10 : 'a' - 10;
87 else
88 break;
89 if (c >= base)
90 break;
91 if (any < 0)
92 continue;
93 if (acc > cutoff || (acc == cutoff && c > cutlim)) {
94 any = -1;
95 acc = UINTMAX_MAX;
96 errno = ERANGE;
97 } else {
98 any = 1;
99 acc *= (uintmax_t)base;
100 acc += c;
101 }
102 }
103 if (neg && any > 0)
104 acc = -acc;
105 if (endptr != 0)
106 *endptr = (char *) (any ? s - 1 : nptr);
107 return (acc);
108}
109DEF_STRONG(strtoumax);
diff --git a/src/lib/libc/stdlib/system.3 b/src/lib/libc/stdlib/system.3
deleted file mode 100644
index bdd94c0115..0000000000
--- a/src/lib/libc/stdlib/system.3
+++ /dev/null
@@ -1,116 +0,0 @@
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. 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.\" $OpenBSD: system.3,v 1.14 2016/02/05 18:09:19 espie Exp $
33.\"
34.Dd $Mdocdate: February 5 2016 $
35.Dt SYSTEM 3
36.Os
37.Sh NAME
38.Nm system
39.Nd pass a command to the shell
40.Sh SYNOPSIS
41.In stdlib.h
42.Ft int
43.Fn system "const char *string"
44.Sh DESCRIPTION
45The
46.Fn system
47function hands the argument
48.Fa string
49to the command interpreter
50.Xr sh 1 .
51The calling process waits for the shell to finish executing the command,
52ignoring
53.Dv SIGINT
54and
55.Dv SIGQUIT ,
56and blocking
57.Dv SIGCHLD .
58.Pp
59If
60.Fa string
61is
62.Dv NULL ,
63.Fn system
64will return non-zero.
65Otherwise,
66.Fn system
67returns the termination status of the shell in the format specified by
68.Xr waitpid 2 .
69.Pp
70Note that fork handlers established using
71.Xr pthread_atfork 3
72are not called when a multithreaded program calls
73.Fn system .
74.Sh RETURN VALUES
75If a child process cannot be created, or the termination status of
76the shell cannot be obtained,
77.Fn system
78returns \-1 and sets
79.Va errno
80to indicate the error.
81If execution of the shell fails,
82.Fn system
83returns the termination status for a program that terminates with a call of
84.Fn exit 127 .
85.Sh SEE ALSO
86.Xr sh 1 ,
87.Xr execve 2 ,
88.Xr waitpid 2 ,
89.Xr popen 3
90.Sh STANDARDS
91The
92.Fn system
93function conforms to
94.St -ansiC
95and
96.St -p1003.2-92 .
97.Sh HISTORY
98The
99.Fn system
100function first appeared in
101.At v6 .
102.Sh CAVEATS
103Never supply the
104.Fn system
105function with a command containing any part of an unsanitized user-supplied
106string.
107Shell meta-characters present will be honored by the
108.Xr sh 1
109command interpreter.
110.Pp
111It is often simpler to bypass the shell and run an external command using
112.Xr fork 2 ,
113.Xr execlp 3 ,
114and
115.Xr waitpid 2
116directly instead of having to sanitize a string for shell consumption.
diff --git a/src/lib/libc/stdlib/system.c b/src/lib/libc/stdlib/system.c
deleted file mode 100644
index 28f01a9c5d..0000000000
--- a/src/lib/libc/stdlib/system.c
+++ /dev/null
@@ -1,89 +0,0 @@
1/* $OpenBSD: system.c,v 1.13 2022/05/21 00:53:53 millert Exp $ */
2/*
3 * Copyright (c) 1988 The Regents of the University of California.
4 * 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. Neither the name of the University nor the names of its contributors
15 * may be used to endorse or promote products derived from this software
16 * without specific prior written permission.
17 *
18 * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
19 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
20 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
21 * ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
22 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
23 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
24 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
25 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
26 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
27 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
28 * SUCH DAMAGE.
29 */
30
31#include <sys/types.h>
32#include <sys/wait.h>
33#include <errno.h>
34#include <signal.h>
35#include <stdlib.h>
36#include <string.h>
37#include <unistd.h>
38#include <paths.h>
39
40int
41system(const char *command)
42{
43 pid_t pid, cpid;
44 struct sigaction intsave, quitsave, sa;
45 sigset_t mask, omask;
46 int pstat;
47 char *argp[] = {"sh", "-c", NULL, NULL};
48
49 if (!command) /* just checking... */
50 return(1);
51
52 argp[2] = (char *)command;
53
54 sigemptyset(&mask);
55 sigaddset(&mask, SIGCHLD);
56 sigaddset(&mask, SIGINT);
57 sigaddset(&mask, SIGQUIT);
58 sigprocmask(SIG_BLOCK, &mask, &omask);
59 switch (cpid = vfork()) {
60 case -1: /* error */
61 sigprocmask(SIG_SETMASK, &omask, NULL);
62 return(-1);
63 case 0: /* child */
64 sigprocmask(SIG_SETMASK, &omask, NULL);
65 execve(_PATH_BSHELL, argp, environ);
66 _exit(127);
67 }
68
69 /* Ignore SIGINT and SIGQUIT while waiting for command to complete. */
70 memset(&sa, 0, sizeof(sa));
71 sigemptyset(&sa.sa_mask);
72 sa.sa_handler = SIG_IGN;
73 sigaction(SIGINT, &sa, &intsave);
74 sigaction(SIGQUIT, &sa, &quitsave);
75 sigemptyset(&mask);
76 sigaddset(&mask, SIGINT);
77 sigaddset(&mask, SIGQUIT);
78 sigprocmask(SIG_UNBLOCK, &mask, NULL);
79
80 do {
81 pid = waitpid(cpid, &pstat, 0);
82 } while (pid == -1 && errno == EINTR);
83
84 sigprocmask(SIG_SETMASK, &omask, NULL);
85 sigaction(SIGINT, &intsave, NULL);
86 sigaction(SIGQUIT, &quitsave, NULL);
87 return (pid == -1 ? -1 : pstat);
88}
89DEF_STRONG(system);
diff --git a/src/lib/libc/stdlib/tfind.c b/src/lib/libc/stdlib/tfind.c
deleted file mode 100644
index 49f9dbc17a..0000000000
--- a/src/lib/libc/stdlib/tfind.c
+++ /dev/null
@@ -1,40 +0,0 @@
1/* $OpenBSD: tfind.c,v 1.7 2015/09/26 16:03:48 guenther Exp $ */
2
3/*
4 * Tree search generalized from Knuth (6.2.2) Algorithm T just like
5 * the AT&T man page says.
6 *
7 * The node_t structure is for internal use only
8 *
9 * Written by reading the System V Interface Definition, not the code.
10 *
11 * Totally public domain.
12 */
13#include <search.h>
14
15typedef struct node_t
16{
17 char *key;
18 struct node_t *llink, *rlink;
19} node;
20
21/* find a node, or return 0 */
22void *
23tfind(const void *vkey, void * const *vrootp,
24 int (*compar)(const void *, const void *))
25{
26 char *key = (char *)vkey;
27 node **rootp = (node **)vrootp;
28
29 if (rootp == (struct node_t **)0)
30 return ((struct node_t *)0);
31 while (*rootp != (struct node_t *)0) { /* T1: */
32 int r;
33 if ((r = (*compar)(key, (*rootp)->key)) == 0) /* T2: */
34 return (*rootp); /* key found */
35 rootp = (r < 0) ?
36 &(*rootp)->llink : /* T3: follow left branch */
37 &(*rootp)->rlink; /* T4: follow right branch */
38 }
39 return (node *)0;
40}
diff --git a/src/lib/libc/stdlib/thread_atexit.c b/src/lib/libc/stdlib/thread_atexit.c
deleted file mode 100644
index ef0423c428..0000000000
--- a/src/lib/libc/stdlib/thread_atexit.c
+++ /dev/null
@@ -1,44 +0,0 @@
1/* $OpenBSD: thread_atexit.c,v 1.2 2019/06/02 01:03:01 guenther Exp $ */
2/*
3 * Copyright (c) 2017 Mark Kettenis <kettenis@openbsd.org>
4 *
5 * Permission to use, copy, modify, and distribute this software for any
6 * purpose with or without fee is hereby granted, provided that the above
7 * copyright notice and this permission notice appear in all copies.
8 *
9 * THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES
10 * WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF
11 * MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR
12 * ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES
13 * WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN
14 * ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF
15 * OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE.
16 */
17
18#include <dlfcn.h>
19#include <stdlib.h>
20#include <tib.h>
21
22#include "atexit.h"
23
24__weak_alias(__cxa_thread_atexit, __cxa_thread_atexit_impl);
25
26int
27__cxa_thread_atexit_impl(void (*func)(void *), void *arg, void *dso)
28{
29 struct thread_atexit_fn *fnp;
30 struct tib *tib = TIB_GET();
31
32 fnp = calloc(1, sizeof(struct thread_atexit_fn));
33 if (fnp == NULL)
34 return -1;
35
36 dlctl(NULL, DL_REFERENCE, dso);
37
38 fnp->func = func;
39 fnp->arg = arg;
40 fnp->next = tib->tib_atexit;
41 tib->tib_atexit = fnp;
42
43 return 0;
44}
diff --git a/src/lib/libc/stdlib/tsearch.3 b/src/lib/libc/stdlib/tsearch.3
deleted file mode 100644
index a7ab985013..0000000000
--- a/src/lib/libc/stdlib/tsearch.3
+++ /dev/null
@@ -1,126 +0,0 @@
1.\" $OpenBSD: tsearch.3,v 1.22 2022/03/31 17:27:16 naddy Exp $
2.\"
3.\" Copyright (c) 1997 Todd C. Miller <millert@openbsd.org>
4.\"
5.\" Permission to use, copy, modify, and distribute this software for any
6.\" purpose with or without fee is hereby granted, provided that the above
7.\" copyright notice and this permission notice appear in all copies.
8.\"
9.\" THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES
10.\" WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF
11.\" MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR
12.\" ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES
13.\" WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN
14.\" ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF
15.\" OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE.
16.\"
17.Dd $Mdocdate: March 31 2022 $
18.Dt TSEARCH 3
19.Os
20.Sh NAME
21.Nm tsearch ,
22.Nm tfind ,
23.Nm tdelete ,
24.Nm twalk
25.Nd manipulate binary search trees
26.Sh SYNOPSIS
27.In search.h
28.Ft void *
29.Fn tdelete "const void *key" "void **rootp" "int (*compar)(const void *, const void *)"
30.Ft void *
31.Fn tfind "const void *key" "void * const *rootp" "int (*compar)(const void *, const void *)"
32.Ft void *
33.Fn tsearch "const void *key" "void **rootp" "int (*compar)(const void *, const void *)"
34.Ft void
35.Fn twalk "const void *root" "void (*action)(const void *, VISIT, int)"
36.Sh DESCRIPTION
37The
38.Fn tdelete ,
39.Fn tfind ,
40.Fn tsearch ,
41and
42.Fn twalk
43functions manage binary search trees based on algorithms T and D
44from Knuth (6.2.2).
45The comparison function passed in by
46the user has the same style of return values as
47.Xr strcmp 3 .
48.Pp
49.Fn tfind
50searches for the datum matched by the argument
51.Fa key
52in the binary tree rooted at
53.Fa rootp ,
54returning a pointer to the datum if it is found and
55.Dv NULL
56if it is not.
57.Pp
58.Fn tsearch
59is identical to
60.Fn tfind
61except that if no match is found,
62.Fa key
63is inserted into the tree and a pointer to it is returned.
64If
65.Fa rootp
66points to a null value, a new binary search tree is created.
67.Pp
68.Fn tdelete
69deletes a node from the specified binary search tree and returns
70a pointer to the parent of the node to be deleted.
71If the node to be deleted is the root of the binary search tree,
72.Fa rootp
73will be adjusted and an unspecified non-null pointer will be returned.
74It takes the same arguments as
75.Fn tfind
76and
77.Fn tsearch .
78.Pp
79.Fn twalk
80walks the binary search tree rooted in
81.Fa root
82and calls the function
83.Fa action
84on each node.
85.Fa action
86is called with three arguments: a pointer to the current node,
87a value from the enum
88.Sy "typedef enum { preorder, postorder, endorder, leaf } VISIT;"
89specifying the traversal type, and a node level (where level
90zero is the root of the tree).
91.Sh RETURN VALUES
92The
93.Fn tsearch
94function returns
95.Dv NULL
96if allocation of a new node fails (usually
97due to a lack of free memory).
98.Pp
99.Fn tdelete
100returns a pointer to the parent of the deleted node or an unspecified
101non-null pointer if the root node is deleted.
102.Pp
103.Fn tfind ,
104.Fn tsearch ,
105and
106.Fn tdelete
107return
108.Dv NULL
109if
110.Fa rootp
111is
112.Dv NULL
113or the datum cannot be found.
114.Sh SEE ALSO
115.Xr bsearch 3 ,
116.Xr lsearch 3
117.Sh STANDARDS
118These functions conform to
119.St -p1003.1-2008 .
120.Sh CAVEATS
121The value returned when deleting the root node was unspecified before
122the
123.St -p1003.1-2008
124standard, so users of the
125.Fn tdelete
126function should be wary of relying on a specific behaviour.
diff --git a/src/lib/libc/stdlib/tsearch.c b/src/lib/libc/stdlib/tsearch.c
deleted file mode 100644
index 1dd31454eb..0000000000
--- a/src/lib/libc/stdlib/tsearch.c
+++ /dev/null
@@ -1,118 +0,0 @@
1/* $OpenBSD: tsearch.c,v 1.10 2015/09/26 16:03:48 guenther Exp $ */
2
3/*
4 * Tree search generalized from Knuth (6.2.2) Algorithm T just like
5 * the AT&T man page says.
6 *
7 * The node_t structure is for internal use only
8 *
9 * Written by reading the System V Interface Definition, not the code.
10 *
11 * Totally public domain.
12 */
13
14#include <search.h>
15#include <stdlib.h>
16
17typedef struct node_t {
18 char *key;
19 struct node_t *left, *right;
20} node;
21
22/* find or insert datum into search tree */
23void *
24tsearch(const void *vkey, void **vrootp,
25 int (*compar)(const void *, const void *))
26{
27 node *q;
28 char *key = (char *)vkey;
29 node **rootp = (node **)vrootp;
30
31 if (rootp == (struct node_t **)0)
32 return ((void *)0);
33 while (*rootp != (struct node_t *)0) { /* Knuth's T1: */
34 int r;
35
36 if ((r = (*compar)(key, (*rootp)->key)) == 0) /* T2: */
37 return ((void *)*rootp); /* we found it! */
38 rootp = (r < 0) ?
39 &(*rootp)->left : /* T3: follow left branch */
40 &(*rootp)->right; /* T4: follow right branch */
41 }
42 q = malloc(sizeof(node)); /* T5: key not found */
43 if (q != (struct node_t *)0) { /* make new node */
44 *rootp = q; /* link new node to old */
45 q->key = key; /* initialize new node */
46 q->left = q->right = (struct node_t *)0;
47 }
48 return ((void *)q);
49}
50
51/* delete node with given key */
52void *
53tdelete(const void *vkey, void **vrootp,
54 int (*compar)(const void *, const void *))
55{
56 node **rootp = (node **)vrootp;
57 char *key = (char *)vkey;
58 node *p = (node *)1;
59 node *q;
60 node *r;
61 int cmp;
62
63 if (rootp == (struct node_t **)0 || *rootp == (struct node_t *)0)
64 return ((struct node_t *)0);
65 while ((cmp = (*compar)(key, (*rootp)->key)) != 0) {
66 p = *rootp;
67 rootp = (cmp < 0) ?
68 &(*rootp)->left : /* follow left branch */
69 &(*rootp)->right; /* follow right branch */
70 if (*rootp == (struct node_t *)0)
71 return ((void *)0); /* key not found */
72 }
73 r = (*rootp)->right; /* D1: */
74 if ((q = (*rootp)->left) == (struct node_t *)0) /* Left (struct node_t *)0? */
75 q = r;
76 else if (r != (struct node_t *)0) { /* Right link is null? */
77 if (r->left == (struct node_t *)0) { /* D2: Find successor */
78 r->left = q;
79 q = r;
80 } else { /* D3: Find (struct node_t *)0 link */
81 for (q = r->left; q->left != (struct node_t *)0; q = r->left)
82 r = q;
83 r->left = q->right;
84 q->left = (*rootp)->left;
85 q->right = (*rootp)->right;
86 }
87 }
88 free((struct node_t *) *rootp); /* D4: Free node */
89 *rootp = q; /* link parent to new node */
90 return(p);
91}
92
93/* Walk the nodes of a tree */
94static void
95trecurse(node *root, void (*action)(const void *, VISIT, int), int level)
96{
97 if (root->left == (struct node_t *)0 && root->right == (struct node_t *)0)
98 (*action)(root, leaf, level);
99 else {
100 (*action)(root, preorder, level);
101 if (root->left != (struct node_t *)0)
102 trecurse(root->left, action, level + 1);
103 (*action)(root, postorder, level);
104 if (root->right != (struct node_t *)0)
105 trecurse(root->right, action, level + 1);
106 (*action)(root, endorder, level);
107 }
108}
109
110/* Walk the nodes of a tree */
111void
112twalk(const void *vroot, void (*action)(const void *, VISIT, int))
113{
114 node *root = (node *)vroot;
115
116 if (root != (node *)0 && action != (void (*)(const void *, VISIT, int))0)
117 trecurse(root, action, 0);
118}
diff --git a/src/lib/libc/string/Makefile.inc b/src/lib/libc/string/Makefile.inc
deleted file mode 100644
index 204ca1b266..0000000000
--- a/src/lib/libc/string/Makefile.inc
+++ /dev/null
@@ -1,35 +0,0 @@
1# $OpenBSD: Makefile.inc,v 1.40 2024/07/14 09:51:18 jca Exp $
2
3# string sources
4.PATH: ${LIBCSRCDIR}/arch/${MACHINE_CPU}/string ${LIBCSRCDIR}/string
5
6SRCS+= explicit_bzero.c memccpy.c memmem.c memrchr.c stpcpy.c stpncpy.c \
7 strcasecmp.c strcasecmp_l.c strcasestr.c strcoll.c strcoll_l.c \
8 strdup.c strerror.c strerror_l.c strerror_r.c strmode.c \
9 strndup.c strnlen.c strsignal.c strtok.c strxfrm.c strxfrm_l.c \
10 timingsafe_bcmp.c timingsafe_memcmp.c \
11 wcscat.c wcschr.c wcscmp.c wcscpy.c wcscspn.c wcslcat.c wcslcpy.c \
12 wcslen.c wcsncat.c wcsncmp.c wcsncpy.c wcsnlen.c wcspbrk.c wcsrchr.c \
13 wcsspn.c wcsstr.c wcstok.c wcswcs.c wcswidth.c wmemchr.c wmemcmp.c \
14 wmemcpy.c wmemmove.c wmemset.c wcsdup.c wcscasecmp.c wcscasecmp_l.c
15
16# machine-dependent net sources
17# ../arch/ARCH/Makefile.inc must include sources for:
18# bcmp() bcopy() bzero() ffs() index() memchr() memcmp() memset()
19# memcpy() memmove() memset() rindex() strcat() strchr()
20# strcmp() strcpy() strcspn() strlen() strlcat() strlcpy()
21# strncat() strncmp() strncpy() strpbrk() strrchr() strsep()
22# strspn() strstr() swab()
23
24.include "${LIBCSRCDIR}/arch/${MACHINE_CPU}/string/Makefile.inc"
25
26MAN+= bcmp.3 bcopy.3 bzero.3 ffs.3 memccpy.3 memchr.3 \
27 memcmp.3 memcpy.3 memmem.3 memmove.3 memset.3 stpcpy.3 strcasecmp.3 \
28 strcat.3 strchr.3 strcmp.3 strcoll.3 strcpy.3 strcspn.3 strdup.3 \
29 strerror.3 strlen.3 strmode.3 strncat.3 strncpy.3 strpbrk.3 \
30 strrchr.3 strsep.3 strsignal.3 strspn.3 strstr.3 strtok.3 strxfrm.3 \
31 swab.3 strlcpy.3 timingsafe_bcmp.3 \
32 wcscasecmp.3 wcscat.3 wcschr.3 wcscmp.3 wcscpy.3 \
33 wcscspn.3 wcsdup.3 wcslcpy.3 wcslen.3 wcspbrk.3 wcsrchr.3 wcsspn.3 \
34 wcsstr.3 wcstok.3 wcswidth.3 wmemchr.3 wmemcmp.3 wmemcpy.3 wmemmove.3 \
35 wmemset.3
diff --git a/src/lib/libc/string/bcmp.3 b/src/lib/libc/string/bcmp.3
deleted file mode 100644
index a3cb1dfb74..0000000000
--- a/src/lib/libc/string/bcmp.3
+++ /dev/null
@@ -1,67 +0,0 @@
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. Neither the name of the University nor the names of its contributors
15.\" may be used to endorse or promote products derived from this software
16.\" without specific prior written permission.
17.\"
18.\" THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
19.\" ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
20.\" IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
21.\" ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
22.\" FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
23.\" DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
24.\" OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
25.\" HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
26.\" LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
27.\" OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
28.\" SUCH DAMAGE.
29.\"
30.\" $OpenBSD: bcmp.3,v 1.12 2015/11/24 09:14:35 daniel Exp $
31.\"
32.Dd $Mdocdate: November 24 2015 $
33.Dt BCMP 3
34.Os
35.Sh NAME
36.Nm bcmp
37.Nd compare byte string
38.Sh SYNOPSIS
39.In strings.h
40.Ft int
41.Fn bcmp "const void *b1" "const void *b2" "size_t len"
42.Sh DESCRIPTION
43The
44.Fn bcmp
45function compares byte string
46.Fa b1
47against byte string
48.Fa b2 ,
49returning zero if they are identical, non-zero otherwise.
50Both strings are assumed to be
51.Fa len
52bytes long.
53Zero-length strings are always identical.
54.Pp
55The strings may overlap.
56.Sh SEE ALSO
57.Xr memcmp 3 ,
58.Xr strcasecmp 3 ,
59.Xr strcmp 3 ,
60.Xr strcoll 3 ,
61.Xr strxfrm 3 ,
62.Xr timingsafe_bcmp 3
63.Sh HISTORY
64The
65.Fn bcmp
66function first appeared in
67.Bx 4.2 .
diff --git a/src/lib/libc/string/bcmp.c b/src/lib/libc/string/bcmp.c
deleted file mode 100644
index 5d446bf096..0000000000
--- a/src/lib/libc/string/bcmp.c
+++ /dev/null
@@ -1,52 +0,0 @@
1/* $OpenBSD: bcmp.c,v 1.11 2015/08/31 02:53:57 guenther Exp $ */
2
3/*
4 * Copyright (c) 1987 Regents of the University of California.
5 * 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. Neither the name of the University nor the names of its contributors
16 * may be used to endorse or promote products derived from this software
17 * without specific prior written permission.
18 *
19 * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
20 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
21 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
22 * ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
23 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
24 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
25 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
26 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
27 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
28 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
29 * SUCH DAMAGE.
30 */
31
32#include <string.h>
33
34/*
35 * bcmp -- vax cmpc3 instruction
36 */
37int
38bcmp(const void *b1, const void *b2, size_t length)
39{
40 char *p1, *p2;
41
42 if (length == 0)
43 return (0);
44 p1 = (char *)b1;
45 p2 = (char *)b2;
46 do
47 if (*p1++ != *p2++)
48 return (1);
49 while (--length);
50 return (0);
51}
52DEF_WEAK(bcmp);
diff --git a/src/lib/libc/string/bcopy.3 b/src/lib/libc/string/bcopy.3
deleted file mode 100644
index 68c63f92ec..0000000000
--- a/src/lib/libc/string/bcopy.3
+++ /dev/null
@@ -1,67 +0,0 @@
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. Neither the name of the University nor the names of its contributors
16.\" may be used to endorse or promote products derived from this software
17.\" without specific prior written permission.
18.\"
19.\" THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
20.\" ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
21.\" IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
22.\" ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
23.\" FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
24.\" DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
25.\" OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
26.\" HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
27.\" LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
28.\" OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
29.\" SUCH DAMAGE.
30.\"
31.\" $OpenBSD: bcopy.3,v 1.12 2015/11/24 09:14:35 daniel Exp $
32.\"
33.Dd $Mdocdate: November 24 2015 $
34.Dt BCOPY 3
35.Os
36.Sh NAME
37.Nm bcopy
38.Nd copy bytes
39.Sh SYNOPSIS
40.In strings.h
41.Ft void
42.Fn bcopy "const void *src" "void *dst" "size_t len"
43.Sh DESCRIPTION
44The
45.Fn bcopy
46function copies
47.Fa len
48bytes from buffer
49.Fa src
50to buffer
51.Fa dst .
52The two buffers may overlap.
53If
54.Fa len
55is zero, no bytes are copied.
56.Sh SEE ALSO
57.Xr memccpy 3 ,
58.Xr memcpy 3 ,
59.Xr memmove 3 ,
60.Xr strcpy 3 ,
61.Xr strlcpy 3 ,
62.Xr strncpy 3
63.Sh HISTORY
64The
65.Fn bcopy
66function first appeared in
67.Bx 4.2 .
diff --git a/src/lib/libc/string/bcopy.c b/src/lib/libc/string/bcopy.c
deleted file mode 100644
index ef0d368053..0000000000
--- a/src/lib/libc/string/bcopy.c
+++ /dev/null
@@ -1,113 +0,0 @@
1/* $OpenBSD: bcopy.c,v 1.7 2015/08/31 02:53:57 guenther Exp $ */
2/*-
3 * Copyright (c) 1990 The Regents of the University of California.
4 * All rights reserved.
5 *
6 * This code is derived from software contributed to Berkeley by
7 * Chris Torek.
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. 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#include <string.h>
35
36/*
37 * sizeof(word) MUST BE A POWER OF TWO
38 * SO THAT wmask BELOW IS ALL ONES
39 */
40typedef long word; /* "word" used for optimal copy speed */
41
42#define wsize sizeof(word)
43#define wmask (wsize - 1)
44
45/*
46 * Copy a block of memory, handling overlap.
47 */
48void
49bcopy(const void *src0, void *dst0, size_t length)
50{
51 char *dst = dst0;
52 const char *src = src0;
53 size_t t;
54
55 if (length == 0 || dst == src) /* nothing to do */
56 goto done;
57
58 /*
59 * Macros: loop-t-times; and loop-t-times, t>0
60 */
61#define TLOOP(s) if (t) TLOOP1(s)
62#define TLOOP1(s) do { s; } while (--t)
63
64 if ((unsigned long)dst < (unsigned long)src) {
65 /*
66 * Copy forward.
67 */
68 t = (long)src; /* only need low bits */
69 if ((t | (long)dst) & wmask) {
70 /*
71 * Try to align operands. This cannot be done
72 * unless the low bits match.
73 */
74 if ((t ^ (long)dst) & wmask || length < wsize)
75 t = length;
76 else
77 t = wsize - (t & wmask);
78 length -= t;
79 TLOOP1(*dst++ = *src++);
80 }
81 /*
82 * Copy whole words, then mop up any trailing bytes.
83 */
84 t = length / wsize;
85 TLOOP(*(word *)dst = *(word *)src; src += wsize; dst += wsize);
86 t = length & wmask;
87 TLOOP(*dst++ = *src++);
88 } else {
89 /*
90 * Copy backwards. Otherwise essentially the same.
91 * Alignment works as before, except that it takes
92 * (t&wmask) bytes to align, not wsize-(t&wmask).
93 */
94 src += length;
95 dst += length;
96 t = (long)src;
97 if ((t | (long)dst) & wmask) {
98 if ((t ^ (long)dst) & wmask || length <= wsize)
99 t = length;
100 else
101 t &= wmask;
102 length -= t;
103 TLOOP1(*--dst = *--src);
104 }
105 t = length / wsize;
106 TLOOP(src -= wsize; dst -= wsize; *(word *)dst = *(word *)src);
107 t = length & wmask;
108 TLOOP(*--dst = *--src);
109 }
110done:
111 return;
112}
113DEF_WEAK(bcopy);
diff --git a/src/lib/libc/string/bzero.3 b/src/lib/libc/string/bzero.3
deleted file mode 100644
index 87a23e20bb..0000000000
--- a/src/lib/libc/string/bzero.3
+++ /dev/null
@@ -1,92 +0,0 @@
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. Neither the name of the University nor the names of its contributors
15.\" may be used to endorse or promote products derived from this software
16.\" without specific prior written permission.
17.\"
18.\" THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
19.\" ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
20.\" IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
21.\" ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
22.\" FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
23.\" DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
24.\" OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
25.\" HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
26.\" LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
27.\" OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
28.\" SUCH DAMAGE.
29.\"
30.\" $OpenBSD: bzero.3,v 1.13 2017/10/12 15:22:32 schwarze Exp $
31.\"
32.Dd $Mdocdate: October 12 2017 $
33.Dt BZERO 3
34.Os
35.Sh NAME
36.Nm bzero ,
37.Nm explicit_bzero
38.Nd write zeroes to a byte string
39.Sh SYNOPSIS
40.In strings.h
41.Ft void
42.Fn bzero "void *b" "size_t len"
43.In string.h
44.Ft void
45.Fn explicit_bzero "void *b" "size_t len"
46.Sh DESCRIPTION
47The
48.Fn bzero
49function writes
50.Fa len
51zero bytes to the string
52.Fa b .
53If
54.Fa len
55is zero,
56.Fn bzero
57does nothing.
58.Pp
59The
60.Fn explicit_bzero
61variant behaves the same, but will not be removed by a compiler's dead store
62optimization pass, making it useful for clearing sensitive memory such as a
63password.
64.Sh SEE ALSO
65.Xr memset 3 ,
66.Xr swab 3
67.Sh STANDARDS
68The
69.Fn bzero
70function conforms to the X/Open System Interfaces option of the
71.St -p1003.1-2004
72specification.
73It was removed from the standard in
74.St -p1003.1-2008 ,
75which recommends using
76.Xr memset 3
77instead.
78.Pp
79The
80.Fn explicit_bzero
81function is an
82.Ox
83extension.
84.Sh HISTORY
85The
86.Fn bzero
87function first appeared in
88.Bx 4.2 .
89The
90.Fn explicit_bzero
91function first appeared in
92.Ox 5.5 .
diff --git a/src/lib/libc/string/bzero.c b/src/lib/libc/string/bzero.c
deleted file mode 100644
index 5173de26dd..0000000000
--- a/src/lib/libc/string/bzero.c
+++ /dev/null
@@ -1,45 +0,0 @@
1/* $OpenBSD: bzero.c,v 1.9 2015/08/31 02:53:57 guenther Exp $ */
2
3/*
4 * Copyright (c) 1987 Regents of the University of California.
5 * 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. Neither the name of the University nor the names of its contributors
16 * may be used to endorse or promote products derived from this software
17 * without specific prior written permission.
18 *
19 * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
20 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
21 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
22 * ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
23 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
24 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
25 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
26 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
27 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
28 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
29 * SUCH DAMAGE.
30 */
31
32#include <string.h>
33
34/*
35 * bzero -- vax movc5 instruction
36 */
37void
38bzero(void *b, size_t length)
39{
40 char *p;
41
42 for (p = b; length--;)
43 *p++ = '\0';
44}
45DEF_WEAK(bzero);
diff --git a/src/lib/libc/string/explicit_bzero.c b/src/lib/libc/string/explicit_bzero.c
deleted file mode 100644
index 003ea7cc4c..0000000000
--- a/src/lib/libc/string/explicit_bzero.c
+++ /dev/null
@@ -1,20 +0,0 @@
1/* $OpenBSD: explicit_bzero.c,v 1.4 2015/08/31 02:53:57 guenther Exp $ */
2/*
3 * Public domain.
4 * Written by Matthew Dempsky.
5 */
6
7#include <string.h>
8
9__attribute__((weak)) void
10__explicit_bzero_hook(void *buf, size_t len)
11{
12}
13
14void
15explicit_bzero(void *buf, size_t len)
16{
17 memset(buf, 0, len);
18 __explicit_bzero_hook(buf, len);
19}
20DEF_WEAK(explicit_bzero);
diff --git a/src/lib/libc/string/ffs.3 b/src/lib/libc/string/ffs.3
deleted file mode 100644
index e78ab99e8f..0000000000
--- a/src/lib/libc/string/ffs.3
+++ /dev/null
@@ -1,61 +0,0 @@
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. Neither the name of the University nor the names of its contributors
15.\" may be used to endorse or promote products derived from this software
16.\" without specific prior written permission.
17.\"
18.\" THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
19.\" ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
20.\" IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
21.\" ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
22.\" FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
23.\" DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
24.\" OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
25.\" HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
26.\" LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
27.\" OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
28.\" SUCH DAMAGE.
29.\"
30.\" $OpenBSD: ffs.3,v 1.11 2019/08/30 18:35:03 deraadt Exp $
31.\"
32.Dd $Mdocdate: August 30 2019 $
33.Dt FFS 3
34.Os
35.Sh NAME
36.Nm ffs
37.Nd find first bit set in a bit string
38.Sh SYNOPSIS
39.In strings.h
40.Ft int
41.Fn ffs "int value"
42.Sh DESCRIPTION
43The
44.Fn ffs
45function finds the first bit set in
46.Fa value
47and returns the index of that bit.
48Bits are numbered starting from 1, starting at the rightmost bit.
49A return value of 0 means that the argument was zero.
50.Sh SEE ALSO
51.Xr bit_ffs 3
52.Sh STANDARDS
53The
54.Fn ffs
55function conforms to
56.St -p1003.1-2008 .
57.Sh HISTORY
58The
59.Fn ffs
60function first appeared in
61.Bx 4.2 .
diff --git a/src/lib/libc/string/ffs.c b/src/lib/libc/string/ffs.c
deleted file mode 100644
index 09d6e35eca..0000000000
--- a/src/lib/libc/string/ffs.c
+++ /dev/null
@@ -1,40 +0,0 @@
1/* $OpenBSD: ffs.c,v 1.10 2018/01/18 08:23:44 guenther Exp $ */
2
3/*
4 * Public domain.
5 * Written by Dale Rahn.
6 */
7
8#include <string.h>
9
10/*
11 * ffs -- vax ffs instruction
12 */
13int
14ffs(int mask)
15{
16 int bit;
17 unsigned int r = mask;
18 static const signed char t[16] = {
19 -28, 1, 2, 1,
20 3, 1, 2, 1,
21 4, 1, 2, 1,
22 3, 1, 2, 1
23 };
24
25 bit = 0;
26 if (!(r & 0xffff)) {
27 bit += 16;
28 r >>= 16;
29 }
30 if (!(r & 0xff)) {
31 bit += 8;
32 r >>= 8;
33 }
34 if (!(r & 0xf)) {
35 bit += 4;
36 r >>= 4;
37 }
38
39 return (bit + t[ r & 0xf ]);
40}
diff --git a/src/lib/libc/string/memccpy.3 b/src/lib/libc/string/memccpy.3
deleted file mode 100644
index 49b8ab5d26..0000000000
--- a/src/lib/libc/string/memccpy.3
+++ /dev/null
@@ -1,81 +0,0 @@
1.\" $OpenBSD: memccpy.3,v 1.14 2024/12/11 23:28:20 jsg Exp $
2.\"
3.\" Copyright (c) 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. Neither the name of the University nor the names of its contributors
15.\" may be used to endorse or promote products derived from this software
16.\" without specific prior written permission.
17.\"
18.\" THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
19.\" ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
20.\" IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
21.\" ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
22.\" FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
23.\" DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
24.\" OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
25.\" HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
26.\" LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
27.\" OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
28.\" SUCH DAMAGE.
29.\"
30.\" @(#)memccpy.3 8.1 (Berkeley) 6/9/93
31.\"
32.Dd $Mdocdate: December 11 2024 $
33.Dt MEMCCPY 3
34.Os
35.Sh NAME
36.Nm memccpy
37.Nd copy string until character found
38.Sh SYNOPSIS
39.In string.h
40.Ft void *
41.Fn memccpy "void *dst" "const void *src" "int c" "size_t len"
42.Sh DESCRIPTION
43The
44.Fn memccpy
45function copies bytes from string
46.Fa src
47to string
48.Fa dst .
49If the character
50.Fa c
51(as converted to an
52.Vt unsigned char )
53occurs in the string
54.Fa src ,
55the copy stops and a pointer to the byte after the copy of
56.Fa c
57in the string
58.Fa dst
59is returned.
60Otherwise,
61.Fa len
62bytes are copied, and a null pointer is returned.
63.Pp
64If the
65.Fa src
66and
67.Fa dst
68strings overlap, the behavior is undefined.
69.Sh SEE ALSO
70.Xr bcopy 3 ,
71.Xr memcpy 3 ,
72.Xr memmove 3 ,
73.Xr strcpy 3 ,
74.Xr strlcpy 3
75.Sh HISTORY
76The
77.Fn memccpy
78function first appeared in
79.At V
80and was reimplemented for
81.Bx 4.3 .
diff --git a/src/lib/libc/string/memccpy.c b/src/lib/libc/string/memccpy.c
deleted file mode 100644
index 635061b8cb..0000000000
--- a/src/lib/libc/string/memccpy.c
+++ /dev/null
@@ -1,49 +0,0 @@
1/* $OpenBSD: memccpy.c,v 1.7 2015/08/31 02:53:57 guenther 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. Neither the name of the University nor the names of its contributors
16 * may be used to endorse or promote products derived from this software
17 * without specific prior written permission.
18 *
19 * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
20 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
21 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
22 * ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
23 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
24 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
25 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
26 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
27 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
28 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
29 * SUCH DAMAGE.
30 */
31
32#include <string.h>
33
34void *
35memccpy(void *t, const void *f, int c, size_t n)
36{
37
38 if (n) {
39 unsigned char *tp = t;
40 const unsigned char *fp = f;
41 unsigned char uc = c;
42 do {
43 if ((*tp++ = *fp++) == uc)
44 return (tp);
45 } while (--n != 0);
46 }
47 return (0);
48}
49DEF_WEAK(memccpy);
diff --git a/src/lib/libc/string/memchr.3 b/src/lib/libc/string/memchr.3
deleted file mode 100644
index fa5809fa98..0000000000
--- a/src/lib/libc/string/memchr.3
+++ /dev/null
@@ -1,102 +0,0 @@
1.\" $OpenBSD: memchr.3,v 1.14 2024/12/11 23:28:20 jsg Exp $
2.\"
3.\" Copyright (c) 1990, 1991 The Regents of the University of California.
4.\" All rights reserved.
5.\"
6.\" This code is derived from software contributed to Berkeley by
7.\" Chris Torek and the American National Standards Committee X3,
8.\" on Information Processing Systems.
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. 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.Dd $Mdocdate: December 11 2024 $
35.Dt MEMCHR 3
36.Os
37.Sh NAME
38.Nm memchr ,
39.Nm memrchr
40.Nd locate byte in byte string
41.Sh SYNOPSIS
42.In string.h
43.Ft void *
44.Fn memchr "const void *b" "int c" "size_t len"
45.Ft void *
46.Fn memrchr "const void *b" "int c" "size_t len"
47.Sh DESCRIPTION
48The
49.Fn memchr
50function locates the first occurrence of
51.Fa c
52(converted to an
53.Vt unsigned char )
54in string
55.Fa b .
56.Pp
57The
58.Fn memrchr
59function behaves like
60.Fn memchr ,
61except that it locates the last occurrence of
62.Fa c
63in string
64.Fa b .
65.Sh RETURN VALUES
66The
67.Fn memchr
68and
69.Fn memrchr
70functions return a pointer to the byte located, or
71.Dv NULL
72if no such byte exists within
73.Fa len
74bytes.
75.Sh SEE ALSO
76.Xr strchr 3 ,
77.Xr strcspn 3 ,
78.Xr strpbrk 3 ,
79.Xr strrchr 3 ,
80.Xr strsep 3 ,
81.Xr strspn 3 ,
82.Xr strstr 3 ,
83.Xr strtok 3 ,
84.Xr wmemchr 3
85.Sh STANDARDS
86The
87.Fn memchr
88function conforms to
89.St -ansiC .
90.Pp
91The
92.Fn memrchr
93function is an
94.Ox
95extension.
96.Sh HISTORY
97The
98.Fn memchr
99function first appeared in
100.At V
101and was reimplemented for
102.Bx 4.3 .
diff --git a/src/lib/libc/string/memchr.c b/src/lib/libc/string/memchr.c
deleted file mode 100644
index a6a4bd60d0..0000000000
--- a/src/lib/libc/string/memchr.c
+++ /dev/null
@@ -1,49 +0,0 @@
1/* $OpenBSD: memchr.c,v 1.8 2015/08/31 02:53:57 guenther Exp $ */
2/*-
3 * Copyright (c) 1990 The Regents of the University of California.
4 * All rights reserved.
5 *
6 * This code is derived from software contributed to Berkeley by
7 * Chris Torek.
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. 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#include <string.h>
35
36void *
37memchr(const void *s, int c, size_t n)
38{
39 if (n != 0) {
40 const unsigned char *p = s;
41
42 do {
43 if (*p++ == (unsigned char)c)
44 return ((void *)(p - 1));
45 } while (--n != 0);
46 }
47 return (NULL);
48}
49DEF_STRONG(memchr);
diff --git a/src/lib/libc/string/memcmp.3 b/src/lib/libc/string/memcmp.3
deleted file mode 100644
index ace8f79255..0000000000
--- a/src/lib/libc/string/memcmp.3
+++ /dev/null
@@ -1,86 +0,0 @@
1.\" $OpenBSD: memcmp.3,v 1.11 2024/12/11 23:28:20 jsg Exp $
2.\"
3.\" Copyright (c) 1990, 1991 The Regents of the University of California.
4.\" All rights reserved.
5.\"
6.\" This code is derived from software contributed to Berkeley by
7.\" Chris Torek and the American National Standards Committee X3,
8.\" on Information Processing Systems.
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. 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.Dd $Mdocdate: December 11 2024 $
35.Dt MEMCMP 3
36.Os
37.Sh NAME
38.Nm memcmp
39.Nd compare byte string
40.Sh SYNOPSIS
41.In string.h
42.Ft int
43.Fn memcmp "const void *b1" "const void *b2" "size_t len"
44.Sh DESCRIPTION
45The
46.Fn memcmp
47function compares byte string
48.Fa b1
49against byte string
50.Fa b2 .
51Both strings are assumed to be
52.Fa len
53bytes long.
54.Sh RETURN VALUES
55The
56.Fn memcmp
57function returns zero if the two strings are identical,
58otherwise returns the difference between the first two differing bytes
59(treated as
60.Vt unsigned char
61values, so that
62.Sq Li \e200
63is greater than
64.Sq Li \&\e0 ,
65for example).
66Zero-length strings are always identical.
67.Sh SEE ALSO
68.Xr bcmp 3 ,
69.Xr strcasecmp 3 ,
70.Xr strcmp 3 ,
71.Xr strcoll 3 ,
72.Xr strxfrm 3 ,
73.Xr timingsafe_memcmp 3 ,
74.Xr wmemcmp 3
75.Sh STANDARDS
76The
77.Fn memcmp
78function conforms to
79.St -ansiC .
80.Sh HISTORY
81The
82.Fn memcmp
83function first appeared in
84.At V
85and was reimplemented for
86.Bx 4.3 .
diff --git a/src/lib/libc/string/memcmp.c b/src/lib/libc/string/memcmp.c
deleted file mode 100644
index 0df2c54d2a..0000000000
--- a/src/lib/libc/string/memcmp.c
+++ /dev/null
@@ -1,52 +0,0 @@
1/* $OpenBSD: memcmp.c,v 1.6 2015/08/31 02:53:57 guenther Exp $ */
2/*-
3 * Copyright (c) 1990 The Regents of the University of California.
4 * All rights reserved.
5 *
6 * This code is derived from software contributed to Berkeley by
7 * Chris Torek.
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. 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#include <string.h>
35
36/*
37 * Compare memory regions.
38 */
39int
40memcmp(const void *s1, const void *s2, size_t n)
41{
42 if (n != 0) {
43 const unsigned char *p1 = s1, *p2 = s2;
44
45 do {
46 if (*p1++ != *p2++)
47 return (*--p1 - *--p2);
48 } while (--n != 0);
49 }
50 return (0);
51}
52DEF_STRONG(memcmp);
diff --git a/src/lib/libc/string/memcpy.3 b/src/lib/libc/string/memcpy.3
deleted file mode 100644
index b093afc919..0000000000
--- a/src/lib/libc/string/memcpy.3
+++ /dev/null
@@ -1,79 +0,0 @@
1.\" $OpenBSD: memcpy.3,v 1.11 2024/12/11 23:28:20 jsg Exp $
2.\"
3.\" Copyright (c) 1990, 1991 The Regents of the University of California.
4.\" All rights reserved.
5.\"
6.\" This code is derived from software contributed to Berkeley by
7.\" Chris Torek and the American National Standards Committee X3,
8.\" on Information Processing Systems.
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. 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.Dd $Mdocdate: December 11 2024 $
35.Dt MEMCPY 3
36.Os
37.Sh NAME
38.Nm memcpy
39.Nd copy bytes
40.Sh SYNOPSIS
41.In string.h
42.Ft void *
43.Fn memcpy "void *dst" "const void *src" "size_t len"
44.Sh DESCRIPTION
45The
46.Fn memcpy
47function copies
48.Fa len
49bytes from buffer
50.Fa src
51to buffer
52.Fa dst .
53If the two buffers may overlap,
54.Xr memmove 3
55must be used instead.
56.Sh RETURN VALUES
57The
58.Fn memcpy
59function returns the original value of
60.Fa dst .
61.Sh SEE ALSO
62.Xr bcopy 3 ,
63.Xr memccpy 3 ,
64.Xr memmove 3 ,
65.Xr strcpy 3 ,
66.Xr strlcpy 3 ,
67.Xr wmemcpy 3
68.Sh STANDARDS
69The
70.Fn memcpy
71function conforms to
72.St -ansiC .
73.Sh HISTORY
74The
75.Fn memcpy
76function first appeared in
77.At V
78and was reimplemented for
79.Bx 4.3 .
diff --git a/src/lib/libc/string/memcpy.c b/src/lib/libc/string/memcpy.c
deleted file mode 100644
index 19fddc0ab5..0000000000
--- a/src/lib/libc/string/memcpy.c
+++ /dev/null
@@ -1,109 +0,0 @@
1/* $OpenBSD: memcpy.c,v 1.4 2017/11/29 05:13:57 guenther Exp $ */
2/*-
3 * Copyright (c) 1990 The Regents of the University of California.
4 * All rights reserved.
5 *
6 * This code is derived from software contributed to Berkeley by
7 * Chris Torek.
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. 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#include <string.h>
35#include <stdlib.h>
36#include <syslog.h>
37
38/*
39 * sizeof(word) MUST BE A POWER OF TWO
40 * SO THAT wmask BELOW IS ALL ONES
41 */
42typedef long word; /* "word" used for optimal copy speed */
43
44#define wsize sizeof(word)
45#define wmask (wsize - 1)
46
47static const char backwards_msg[] = ": backwards memcpy";
48
49/*
50 * Copy a block of memory, not handling overlap.
51 */
52void *
53memcpy(void *dst0, const void *src0, size_t length)
54{
55 char *dst = dst0;
56 const char *src = src0;
57 size_t t;
58
59 if (length == 0 || dst == src) /* nothing to do */
60 goto done;
61
62 if ((dst < src && dst + length > src) ||
63 (src < dst && src + length > dst)) {
64 char buf[1024];
65
66 /* <10> is LOG_CRIT */
67 strlcpy(buf, "<10>", sizeof buf);
68
69 /* Make sure progname does not fill the whole buffer */
70 strlcat(buf, __progname, sizeof(buf) - sizeof backwards_msg);
71 strlcat(buf, backwards_msg, sizeof buf);
72
73 sendsyslog(buf, strlen(buf), LOG_CONS);
74 abort();
75 }
76
77 /*
78 * Macros: loop-t-times; and loop-t-times, t>0
79 */
80#define TLOOP(s) if (t) TLOOP1(s)
81#define TLOOP1(s) do { s; } while (--t)
82
83 /*
84 * Copy forward.
85 */
86 t = (long)src; /* only need low bits */
87 if ((t | (long)dst) & wmask) {
88 /*
89 * Try to align operands. This cannot be done
90 * unless the low bits match.
91 */
92 if ((t ^ (long)dst) & wmask || length < wsize)
93 t = length;
94 else
95 t = wsize - (t & wmask);
96 length -= t;
97 TLOOP1(*dst++ = *src++);
98 }
99 /*
100 * Copy whole words, then mop up any trailing bytes.
101 */
102 t = length / wsize;
103 TLOOP(*(word *)dst = *(word *)src; src += wsize; dst += wsize);
104 t = length & wmask;
105 TLOOP(*dst++ = *src++);
106done:
107 return (dst0);
108}
109DEF_BUILTIN(memcpy);
diff --git a/src/lib/libc/string/memmem.3 b/src/lib/libc/string/memmem.3
deleted file mode 100644
index de62d738de..0000000000
--- a/src/lib/libc/string/memmem.3
+++ /dev/null
@@ -1,77 +0,0 @@
1.\" $OpenBSD: memmem.3,v 1.4 2024/08/03 20:13:23 guenther Exp $
2.\"
3.\" Copyright (c) 2005 Pascal Gloor <pascal.gloor@spale.com>
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. The name of the author may not be used to endorse or promote
14.\" products derived from this software without specific prior written
15.\" permission.
16.\"
17.\" THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND
18.\" ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
19.\" IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
20.\" ARE DISCLAIMED. IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE
21.\" FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
22.\" DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
23.\" OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
24.\" HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
25.\" LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
26.\" OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
27.\" SUCH DAMAGE.
28.\"
29.Dd $Mdocdate: August 3 2024 $
30.Dt MEMMEM 3
31.Os
32.Sh NAME
33.Nm memmem
34.Nd locate a byte substring in a byte string
35.Sh SYNOPSIS
36.In string.h
37.Ft "void *"
38.Fo memmem
39.Fa "const void *big" "size_t big_len"
40.Fa "const void *little" "size_t little_len"
41.Fc
42.Sh DESCRIPTION
43The
44.Fn memmem
45function
46locates the first occurrence of the byte string
47.Fa little
48in the byte string
49.Fa big .
50.Sh RETURN VALUES
51If
52.Fa little
53is zero length,
54.Fa big
55is returned; if
56.Fa little
57occurs nowhere in
58.Fa big ,
59.Dv NULL
60is returned;
61otherwise a pointer to the first character of the first occurrence of
62.Fa little
63is returned.
64.Sh SEE ALSO
65.Xr memchr 3 ,
66.Xr strchr 3 ,
67.Xr strstr 3
68.Sh STANDARDS
69The
70.Fn memmem
71function conforms to
72.St -p1003.1-2024 .
73.Sh HISTORY
74The
75.Fn memmem
76function first appeared in
77.Ox 5.4 .
diff --git a/src/lib/libc/string/memmem.c b/src/lib/libc/string/memmem.c
deleted file mode 100644
index 3b180b4a62..0000000000
--- a/src/lib/libc/string/memmem.c
+++ /dev/null
@@ -1,184 +0,0 @@
1/* $OpenBSD: memmem.c,v 1.5 2020/04/16 12:39:28 claudio Exp $ */
2
3/*
4 * Copyright (c) 2005-2020 Rich Felker, et al.
5 *
6 * Permission is hereby granted, free of charge, to any person obtaining
7 * a copy of this software and associated documentation files (the
8 * "Software"), to deal in the Software without restriction, including
9 * without limitation the rights to use, copy, modify, merge, publish,
10 * distribute, sublicense, and/or sell copies of the Software, and to
11 * permit persons to whom the Software is furnished to do so, subject to
12 * the following conditions:
13 *
14 * The above copyright notice and this permission notice shall be
15 * included in all copies or substantial portions of the Software.
16 *
17 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
18 * EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
19 * MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT.
20 * IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY
21 * CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT,
22 * TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE
23 * SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
24 */
25
26#include <string.h>
27#include <stdint.h>
28
29static char *
30twobyte_memmem(const unsigned char *h, size_t k, const unsigned char *n)
31{
32 uint16_t nw = n[0]<<8 | n[1], hw = h[0]<<8 | h[1];
33 for (h+=2, k-=2; k; k--, hw = hw<<8 | *h++)
34 if (hw == nw) return (char *)h-2;
35 return hw == nw ? (char *)h-2 : 0;
36}
37
38static char *
39threebyte_memmem(const unsigned char *h, size_t k, const unsigned char *n)
40{
41 uint32_t nw = n[0]<<24 | n[1]<<16 | n[2]<<8;
42 uint32_t hw = h[0]<<24 | h[1]<<16 | h[2]<<8;
43 for (h+=3, k-=3; k; k--, hw = (hw|*h++)<<8)
44 if (hw == nw) return (char *)h-3;
45 return hw == nw ? (char *)h-3 : 0;
46}
47
48static char *
49fourbyte_memmem(const unsigned char *h, size_t k, const unsigned char *n)
50{
51 uint32_t nw = n[0]<<24 | n[1]<<16 | n[2]<<8 | n[3];
52 uint32_t hw = h[0]<<24 | h[1]<<16 | h[2]<<8 | h[3];
53 for (h+=4, k-=4; k; k--, hw = hw<<8 | *h++)
54 if (hw == nw) return (char *)h-4;
55 return hw == nw ? (char *)h-4 : 0;
56}
57
58#define MAX(a,b) ((a)>(b)?(a):(b))
59#define MIN(a,b) ((a)<(b)?(a):(b))
60
61#define BITOP(a,b,op) \
62 ((a)[(size_t)(b)/(8*sizeof *(a))] op (size_t)1<<((size_t)(b)%(8*sizeof *(a))))
63
64/*
65 * Maxime Crochemore and Dominique Perrin, Two-way string-matching,
66 * Journal of the ACM, 38(3):651-675, July 1991.
67 */
68static char *
69twoway_memmem(const unsigned char *h, const unsigned char *z,
70 const unsigned char *n, size_t l)
71{
72 size_t i, ip, jp, k, p, ms, p0, mem, mem0;
73 size_t byteset[32 / sizeof(size_t)] = { 0 };
74 size_t shift[256];
75
76 /* Computing length of needle and fill shift table */
77 for (i=0; i<l; i++)
78 BITOP(byteset, n[i], |=), shift[n[i]] = i+1;
79
80 /* Compute maximal suffix */
81 ip = -1; jp = 0; k = p = 1;
82 while (jp+k<l) {
83 if (n[ip+k] == n[jp+k]) {
84 if (k == p) {
85 jp += p;
86 k = 1;
87 } else k++;
88 } else if (n[ip+k] > n[jp+k]) {
89 jp += k;
90 k = 1;
91 p = jp - ip;
92 } else {
93 ip = jp++;
94 k = p = 1;
95 }
96 }
97 ms = ip;
98 p0 = p;
99
100 /* And with the opposite comparison */
101 ip = -1; jp = 0; k = p = 1;
102 while (jp+k<l) {
103 if (n[ip+k] == n[jp+k]) {
104 if (k == p) {
105 jp += p;
106 k = 1;
107 } else k++;
108 } else if (n[ip+k] < n[jp+k]) {
109 jp += k;
110 k = 1;
111 p = jp - ip;
112 } else {
113 ip = jp++;
114 k = p = 1;
115 }
116 }
117 if (ip+1 > ms+1) ms = ip;
118 else p = p0;
119
120 /* Periodic needle? */
121 if (memcmp(n, n+p, ms+1)) {
122 mem0 = 0;
123 p = MAX(ms, l-ms-1) + 1;
124 } else mem0 = l-p;
125 mem = 0;
126
127 /* Search loop */
128 for (;;) {
129 /* If remainder of haystack is shorter than needle, done */
130 if (z-h < l) return 0;
131
132 /* Check last byte first; advance by shift on mismatch */
133 if (BITOP(byteset, h[l-1], &)) {
134 k = l-shift[h[l-1]];
135 if (k) {
136 if (k < mem) k = mem;
137 h += k;
138 mem = 0;
139 continue;
140 }
141 } else {
142 h += l;
143 mem = 0;
144 continue;
145 }
146
147 /* Compare right half */
148 for (k=MAX(ms+1,mem); k<l && n[k] == h[k]; k++);
149 if (k < l) {
150 h += k-ms;
151 mem = 0;
152 continue;
153 }
154 /* Compare left half */
155 for (k=ms+1; k>mem && n[k-1] == h[k-1]; k--);
156 if (k <= mem) return (char *)h;
157 h += p;
158 mem = mem0;
159 }
160}
161
162void *
163memmem(const void *h0, size_t k, const void *n0, size_t l)
164{
165 const unsigned char *h = h0, *n = n0;
166
167 /* Return immediately on empty needle */
168 if (!l) return (void *)h;
169
170 /* Return immediately when needle is longer than haystack */
171 if (k<l) return 0;
172
173 /* Use faster algorithms for short needles */
174 h = memchr(h0, *n, k);
175 if (!h || l==1) return (void *)h;
176 k -= h - (const unsigned char *)h0;
177 if (k<l) return 0;
178 if (l==2) return twobyte_memmem(h, k, n);
179 if (l==3) return threebyte_memmem(h, k, n);
180 if (l==4) return fourbyte_memmem(h, k, n);
181
182 return twoway_memmem(h, h+k, n, l);
183}
184DEF_WEAK(memmem);
diff --git a/src/lib/libc/string/memmove.3 b/src/lib/libc/string/memmove.3
deleted file mode 100644
index 8665e4abcf..0000000000
--- a/src/lib/libc/string/memmove.3
+++ /dev/null
@@ -1,76 +0,0 @@
1.\" $OpenBSD: memmove.3,v 1.9 2013/06/05 03:39:23 tedu Exp $
2.\"
3.\" Copyright (c) 1990, 1991 The Regents of the University of California.
4.\" All rights reserved.
5.\"
6.\" This code is derived from software contributed to Berkeley by
7.\" Chris Torek and the American National Standards Committee X3,
8.\" on Information Processing Systems.
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. 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.Dd $Mdocdate: June 5 2013 $
35.Dt MEMMOVE 3
36.Os
37.Sh NAME
38.Nm memmove
39.Nd copy bytes
40.Sh SYNOPSIS
41.In string.h
42.Ft void *
43.Fn memmove "void *dst" "const void *src" "size_t len"
44.Sh DESCRIPTION
45The
46.Fn memmove
47function copies
48.Fa len
49bytes from buffer
50.Fa src
51to buffer
52.Fa dst .
53The two buffers may overlap;
54the copy is always done in a non-destructive manner.
55.Sh RETURN VALUES
56The
57.Fn memmove
58function returns the original value of
59.Fa dst .
60.Sh SEE ALSO
61.Xr bcopy 3 ,
62.Xr memccpy 3 ,
63.Xr memcpy 3 ,
64.Xr strcpy 3 ,
65.Xr strlcpy 3 ,
66.Xr wmemmove 3
67.Sh STANDARDS
68The
69.Fn memmove
70function conforms to
71.St -ansiC .
72.Sh HISTORY
73The
74.Fn memmove
75function first appeared in
76.Bx 4.3 Reno .
diff --git a/src/lib/libc/string/memmove.c b/src/lib/libc/string/memmove.c
deleted file mode 100644
index 1cd1086ab9..0000000000
--- a/src/lib/libc/string/memmove.c
+++ /dev/null
@@ -1,113 +0,0 @@
1/* $OpenBSD: memmove.c,v 1.3 2017/11/29 05:13:57 guenther Exp $ */
2/*-
3 * Copyright (c) 1990 The Regents of the University of California.
4 * All rights reserved.
5 *
6 * This code is derived from software contributed to Berkeley by
7 * Chris Torek.
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. 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#include <string.h>
35
36/*
37 * sizeof(word) MUST BE A POWER OF TWO
38 * SO THAT wmask BELOW IS ALL ONES
39 */
40typedef long word; /* "word" used for optimal copy speed */
41
42#define wsize sizeof(word)
43#define wmask (wsize - 1)
44
45/*
46 * Copy a block of memory, handling overlap.
47 */
48void *
49memmove(void *dst0, const void *src0, size_t length)
50{
51 char *dst = dst0;
52 const char *src = src0;
53 size_t t;
54
55 if (length == 0 || dst == src) /* nothing to do */
56 goto done;
57
58 /*
59 * Macros: loop-t-times; and loop-t-times, t>0
60 */
61#define TLOOP(s) if (t) TLOOP1(s)
62#define TLOOP1(s) do { s; } while (--t)
63
64 if ((unsigned long)dst < (unsigned long)src) {
65 /*
66 * Copy forward.
67 */
68 t = (long)src; /* only need low bits */
69 if ((t | (long)dst) & wmask) {
70 /*
71 * Try to align operands. This cannot be done
72 * unless the low bits match.
73 */
74 if ((t ^ (long)dst) & wmask || length < wsize)
75 t = length;
76 else
77 t = wsize - (t & wmask);
78 length -= t;
79 TLOOP1(*dst++ = *src++);
80 }
81 /*
82 * Copy whole words, then mop up any trailing bytes.
83 */
84 t = length / wsize;
85 TLOOP(*(word *)dst = *(word *)src; src += wsize; dst += wsize);
86 t = length & wmask;
87 TLOOP(*dst++ = *src++);
88 } else {
89 /*
90 * Copy backwards. Otherwise essentially the same.
91 * Alignment works as before, except that it takes
92 * (t&wmask) bytes to align, not wsize-(t&wmask).
93 */
94 src += length;
95 dst += length;
96 t = (long)src;
97 if ((t | (long)dst) & wmask) {
98 if ((t ^ (long)dst) & wmask || length <= wsize)
99 t = length;
100 else
101 t &= wmask;
102 length -= t;
103 TLOOP1(*--dst = *--src);
104 }
105 t = length / wsize;
106 TLOOP(src -= wsize; dst -= wsize; *(word *)dst = *(word *)src);
107 t = length & wmask;
108 TLOOP(*--dst = *--src);
109 }
110done:
111 return (dst0);
112}
113DEF_BUILTIN(memmove);
diff --git a/src/lib/libc/string/memrchr.c b/src/lib/libc/string/memrchr.c
deleted file mode 100644
index e123bc1737..0000000000
--- a/src/lib/libc/string/memrchr.c
+++ /dev/null
@@ -1,39 +0,0 @@
1/* $OpenBSD: memrchr.c,v 1.4 2019/01/25 00:19:25 millert Exp $ */
2
3/*
4 * Copyright (c) 2007 Todd C. Miller <millert@openbsd.org>
5 *
6 * Permission to use, copy, modify, and distribute this software for any
7 * purpose with or without fee is hereby granted, provided that the above
8 * copyright notice and this permission notice appear in all copies.
9 *
10 * THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES
11 * WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF
12 * MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR
13 * ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES
14 * WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN
15 * ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF
16 * OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE.
17 */
18
19#include <string.h>
20
21/*
22 * Reverse memchr()
23 * Find the last occurrence of 'c' in the buffer 's' of size 'n'.
24 */
25void *
26memrchr(const void *s, int c, size_t n)
27{
28 const unsigned char *cp;
29
30 if (n != 0) {
31 cp = (unsigned char *)s + n;
32 do {
33 if (*(--cp) == (unsigned char)c)
34 return((void *)cp);
35 } while (--n != 0);
36 }
37 return(NULL);
38}
39DEF_WEAK(memrchr);
diff --git a/src/lib/libc/string/memset.3 b/src/lib/libc/string/memset.3
deleted file mode 100644
index 34f2ca7888..0000000000
--- a/src/lib/libc/string/memset.3
+++ /dev/null
@@ -1,75 +0,0 @@
1.\" $OpenBSD: memset.3,v 1.10 2024/12/11 23:28:20 jsg Exp $
2.\"
3.\" Copyright (c) 1990, 1991 The Regents of the University of California.
4.\" All rights reserved.
5.\"
6.\" This code is derived from software contributed to Berkeley by
7.\" Chris Torek and the American National Standards Committee X3,
8.\" on Information Processing Systems.
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. 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.Dd $Mdocdate: December 11 2024 $
35.Dt MEMSET 3
36.Os
37.Sh NAME
38.Nm memset
39.Nd write a byte to byte string
40.Sh SYNOPSIS
41.In string.h
42.Ft void *
43.Fn memset "void *b" "int c" "size_t len"
44.Sh DESCRIPTION
45The
46.Fn memset
47function writes
48.Fa len
49bytes of value
50.Fa c
51(converted to an
52.Vt unsigned char )
53to the string
54.Fa b .
55.Sh RETURN VALUES
56The
57.Fn memset
58function returns the original value of
59.Fa b .
60.Sh SEE ALSO
61.Xr bzero 3 ,
62.Xr swab 3 ,
63.Xr wmemset 3
64.Sh STANDARDS
65The
66.Fn memset
67function conforms to
68.St -ansiC .
69.Sh HISTORY
70The
71.Fn memset
72function first appeared in
73.At V
74and was reimplemented for
75.Bx 4.3 .
diff --git a/src/lib/libc/string/memset.c b/src/lib/libc/string/memset.c
deleted file mode 100644
index 0c261f0965..0000000000
--- a/src/lib/libc/string/memset.c
+++ /dev/null
@@ -1,48 +0,0 @@
1/* $OpenBSD: memset.c,v 1.8 2017/11/29 05:13:57 guenther Exp $ */
2/*-
3 * Copyright (c) 1990 The Regents of the University of California.
4 * All rights reserved.
5 *
6 * This code is derived from software contributed to Berkeley by
7 * Chris Torek.
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. 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#include <string.h>
35
36void *
37memset(void *dst, int c, size_t n)
38{
39 if (n != 0) {
40 unsigned char *d = dst;
41
42 do
43 *d++ = (unsigned char)c;
44 while (--n != 0);
45 }
46 return (dst);
47}
48DEF_BUILTIN(memset);
diff --git a/src/lib/libc/string/stpcpy.3 b/src/lib/libc/string/stpcpy.3
deleted file mode 100644
index 973eebecdd..0000000000
--- a/src/lib/libc/string/stpcpy.3
+++ /dev/null
@@ -1,184 +0,0 @@
1.\" $OpenBSD: stpcpy.3,v 1.6 2014/02/23 23:09:34 schwarze Exp $
2.\"
3.\" Copyright (c) 1990, 1991 The Regents of the University of California.
4.\" All rights reserved.
5.\"
6.\" This code is derived from software contributed to Berkeley by
7.\" Chris Torek and the American National Standards Committee X3,
8.\" on Information Processing Systems.
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. 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.Dd $Mdocdate: February 23 2014 $
35.Dt STPCPY 3
36.Os
37.Sh NAME
38.Nm stpcpy ,
39.Nm stpncpy
40.Nd copy strings
41.Sh SYNOPSIS
42.In string.h
43.Ft char *
44.Fn stpcpy "char *dst" "const char *src"
45.Ft char *
46.Fn stpncpy "char *dst" "const char *src" "size_t len"
47.Sh DESCRIPTION
48The
49.Fn stpcpy
50and
51.Fn stpncpy
52functions copy the string
53.Fa src
54to
55.Fa dst
56(including the terminating
57.Ql \e0
58character).
59.Pp
60The
61.Fn stpncpy
62function copies not more than
63.Fa len
64characters into
65.Fa dst ,
66appending
67.Ql \e0
68characters if
69.Fa src
70is less than
71.Fa len
72characters long, and
73.Em not
74terminating
75.Fa dst
76if the length of
77.Fa src
78is greater than or equal to
79.Fa len .
80.Pp
81If the
82.Fa src
83and
84.Fa dst
85strings overlap, the behavior is undefined.
86.Sh RETURN VALUES
87The
88.Fn stpcpy
89function returns a pointer to the terminating
90.Ql \e0
91character written into
92.Fa dst .
93.Pp
94The
95.Fn stpncpy
96function returns a pointer to the first
97.Ql \e0
98character written into
99.Fa dst ,
100or to
101.Fa &dst[len]
102if the length of
103.Fa src
104is greater than or equal to
105.Fa len .
106.Sh EXAMPLES
107The most common use of
108.Fn stpcpy
109is to build up a string from multiple elements.
110The following example builds up a pathname from
111directory and file components using
112.Fn stpcpy :
113.Bd -literal -offset indent
114char *dir, *file, pname[PATH_MAX];
115
116\&...
117
118if (strlen(dir) + strlen("/") + strlen(file) >= sizeof(pname))
119 goto toolong;
120stpcpy(stpcpy(stpcpy(pname, dir), "/"), file);
121.Ed
122.Pp
123However, the size check required to avoid a buffer overflow is error
124prone since the check can become out of sync with the code that
125performs the copy.
126.Pp
127One might expect that
128.Fn stpncpy
129could be safely used instead, but it suffers from the same defects as
130.Fn strncpy .
131The example below using
132.Fn stpncpy
133is even more prone to error and will not detect when truncation occurs:
134.Bd -literal -offset indent
135char *dir, *file, pname[PATH_MAX];
136char *p1, *p2;
137
138\&...
139
140p1 = stpncpy(pname, dir, sizeof(pname) - 1);
141p2 = stpncpy(p1, "/", sizeof(pname) - 1 - (p1 - pname));
142stpncpy(p2, file, sizeof(pname) - 1 - (p2 - pname));
143pname[sizeof(pname) - 1] = '\e0';
144.Ed
145.Pp
146A safer (and simpler) approach is to use
147.Fn snprintf :
148.Bd -literal -offset indent
149char *dir, *file, pname[PATH_MAX];
150int len;
151
152\&...
153
154len = snprintf(pname, sizeof(pname), "%s/%s", dir, file);
155if (len >= sizeof(pname))
156 goto toolong;
157.Ed
158.Pp
159In most cases, it is better to use
160.Fn snprintf ,
161.Fn strlcpy ,
162or
163.Fn strlcat .
164.Sh SEE ALSO
165.Xr snprintf 3 ,
166.Xr strcpy 3 ,
167.Xr strlcpy 3 ,
168.Xr strncpy 3
169.Sh STANDARDS
170The
171.Fn stpcpy
172and
173.Fn stpncpy
174functions conform to
175.St -p1003.1-2008 .
176.Sh HISTORY
177The function
178.Fn stpcpy
179first appeared in the Lattice C AmigaDOS compiler (1986 or earlier).
180The function
181.Fn stpncpy
182first appeared in the GNU C library version 1.07 (1993).
183Both functions have been available since
184.Ox 5.1 .
diff --git a/src/lib/libc/string/stpcpy.c b/src/lib/libc/string/stpcpy.c
deleted file mode 100644
index 5a86541f08..0000000000
--- a/src/lib/libc/string/stpcpy.c
+++ /dev/null
@@ -1,44 +0,0 @@
1/* $OpenBSD: stpcpy.c,v 1.3 2017/11/28 06:55:49 tb Exp $ */
2
3/*
4 * Copyright (c) 1988 Regents of the University of California.
5 * 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. Neither the name of the University nor the names of its contributors
16 * may be used to endorse or promote products derived from this software
17 * without specific prior written permission.
18 *
19 * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
20 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
21 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
22 * ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
23 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
24 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
25 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
26 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
27 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
28 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
29 * SUCH DAMAGE.
30 */
31
32#include <string.h>
33
34#if defined(APIWARN)
35__warn_references(stpcpy,
36 "stpcpy() is dangerous; do not use it");
37#endif
38
39char *
40stpcpy(char *to, const char *from)
41{
42 for (; (*to = *from) != '\0'; ++from, ++to);
43 return(to);
44}
diff --git a/src/lib/libc/string/stpncpy.c b/src/lib/libc/string/stpncpy.c
deleted file mode 100644
index 6bdee5de16..0000000000
--- a/src/lib/libc/string/stpncpy.c
+++ /dev/null
@@ -1,57 +0,0 @@
1/* $OpenBSD: stpncpy.c,v 1.3 2015/08/31 02:53:57 guenther Exp $ */
2
3/*-
4 * Copyright (c) 1990 The Regents of the University of California.
5 * All rights reserved.
6 *
7 * This code is derived from software contributed to Berkeley by
8 * Chris Torek.
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. 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
35#include <string.h>
36
37char *
38stpncpy(char *dst, const char *src, size_t n)
39{
40 if (n != 0) {
41 char *d = dst;
42 const char *s = src;
43
44 dst = &dst[n];
45 do {
46 if ((*d++ = *s++) == 0) {
47 dst = d - 1;
48 /* NUL pad the remaining n-1 bytes */
49 while (--n != 0)
50 *d++ = 0;
51 break;
52 }
53 } while (--n != 0);
54 }
55 return (dst);
56}
57DEF_WEAK(stpncpy);
diff --git a/src/lib/libc/string/strcasecmp.3 b/src/lib/libc/string/strcasecmp.3
deleted file mode 100644
index bb39df895a..0000000000
--- a/src/lib/libc/string/strcasecmp.3
+++ /dev/null
@@ -1,128 +0,0 @@
1.\" $OpenBSD: strcasecmp.3,v 1.14 2017/09/05 03:16:13 schwarze Exp $
2.\"
3.\" Copyright (c) 1990, 1991, 1993
4.\" The Regents of the University of California. All rights reserved.
5.\" Copyright (c) 2017 Ingo Schwarze <schwarze@openbsd.org>
6.\"
7.\" This code is derived from software contributed to Berkeley by
8.\" Chris Torek.
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. 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.\" @(#)strcasecmp.3 8.1 (Berkeley) 6/9/93
34.\"
35.Dd $Mdocdate: September 5 2017 $
36.Dt STRCASECMP 3
37.Os
38.Sh NAME
39.Nm strcasecmp ,
40.Nm strcasecmp_l ,
41.Nm strncasecmp ,
42.Nm strncasecmp_l
43.Nd compare strings, ignoring case
44.Sh SYNOPSIS
45.In strings.h
46.Ft int
47.Fo strcasecmp
48.Fa "const char *s1"
49.Fa "const char *s2"
50.Fc
51.Ft int
52.Fo strcasecmp_l
53.Fa "const char *s1"
54.Fa "const char *s2"
55.Fa "locale_t locale"
56.Fc
57.Ft int
58.Fo strncasecmp
59.Fa "const char *s1"
60.Fa "const char *s2"
61.Fa "size_t len"
62.Fc
63.Ft int
64.Fo strncasecmp_l
65.Fa "const char *s1"
66.Fa "const char *s2"
67.Fa "size_t len"
68.Fa "locale_t locale"
69.Fc
70.Sh DESCRIPTION
71These functions compare the NUL-terminated strings
72.Fa s1
73and
74.Fa s2
75and return an integer greater than, equal to, or less than 0,
76according to whether
77.Fa s1
78is lexicographically greater than, equal to, or less than
79.Fa s2
80after translation of each corresponding character to lower-case.
81The strings themselves are not modified.
82The comparison is done using unsigned characters, so that
83.Sq Li \e200
84is greater than
85.Ql \e0 .
86.Pp
87.Fn strncasecmp
88and
89.Fn strncasecmp_l
90compare at most
91.Fa len
92characters.
93.Pp
94On
95.Ox ,
96these functions always use the C locale and ignore
97the global locale, the thread-specific locale, and the
98.Fa locale
99argument.
100.Sh ENVIRONMENT
101On other operating systems, the behaviour of
102.Fn strcasecmp
103and
104.Fn strncasecmp
105may depend on the
106.Dv LC_CTYPE
107.Xr locale 1 .
108.Sh SEE ALSO
109.Xr strcmp 3 ,
110.Xr strcoll 3 ,
111.Xr strxfrm 3 ,
112.Xr wcscasecmp 3
113.Sh STANDARDS
114These functions conform to
115.St -p1003.1-2008 .
116.Sh HISTORY
117The
118.Fn strcasecmp
119and
120.Fn strncasecmp
121functions have been available since
122.Bx 4.3 Tahoe ,
123and
124.Fn strcasecmp_l
125and
126.Fn strncasecmp_l
127since
128.Ox 6.2 .
diff --git a/src/lib/libc/string/strcasecmp.c b/src/lib/libc/string/strcasecmp.c
deleted file mode 100644
index edbd638722..0000000000
--- a/src/lib/libc/string/strcasecmp.c
+++ /dev/null
@@ -1,107 +0,0 @@
1/* $OpenBSD: strcasecmp.c,v 1.7 2015/08/31 02:53:57 guenther 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. Neither the name of the University nor the names of its contributors
16 * may be used to endorse or promote products derived from this software
17 * without specific prior written permission.
18 *
19 * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
20 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
21 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
22 * ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
23 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
24 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
25 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
26 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
27 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
28 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
29 * SUCH DAMAGE.
30 */
31
32#include <string.h>
33
34typedef unsigned char u_char;
35
36/*
37 * This array is designed for mapping upper and lower case letter
38 * together for a case independent comparison. The mappings are
39 * based upon ascii character sequences.
40 */
41static const u_char charmap[] = {
42 '\000', '\001', '\002', '\003', '\004', '\005', '\006', '\007',
43 '\010', '\011', '\012', '\013', '\014', '\015', '\016', '\017',
44 '\020', '\021', '\022', '\023', '\024', '\025', '\026', '\027',
45 '\030', '\031', '\032', '\033', '\034', '\035', '\036', '\037',
46 '\040', '\041', '\042', '\043', '\044', '\045', '\046', '\047',
47 '\050', '\051', '\052', '\053', '\054', '\055', '\056', '\057',
48 '\060', '\061', '\062', '\063', '\064', '\065', '\066', '\067',
49 '\070', '\071', '\072', '\073', '\074', '\075', '\076', '\077',
50 '\100', '\141', '\142', '\143', '\144', '\145', '\146', '\147',
51 '\150', '\151', '\152', '\153', '\154', '\155', '\156', '\157',
52 '\160', '\161', '\162', '\163', '\164', '\165', '\166', '\167',
53 '\170', '\171', '\172', '\133', '\134', '\135', '\136', '\137',
54 '\140', '\141', '\142', '\143', '\144', '\145', '\146', '\147',
55 '\150', '\151', '\152', '\153', '\154', '\155', '\156', '\157',
56 '\160', '\161', '\162', '\163', '\164', '\165', '\166', '\167',
57 '\170', '\171', '\172', '\173', '\174', '\175', '\176', '\177',
58 '\200', '\201', '\202', '\203', '\204', '\205', '\206', '\207',
59 '\210', '\211', '\212', '\213', '\214', '\215', '\216', '\217',
60 '\220', '\221', '\222', '\223', '\224', '\225', '\226', '\227',
61 '\230', '\231', '\232', '\233', '\234', '\235', '\236', '\237',
62 '\240', '\241', '\242', '\243', '\244', '\245', '\246', '\247',
63 '\250', '\251', '\252', '\253', '\254', '\255', '\256', '\257',
64 '\260', '\261', '\262', '\263', '\264', '\265', '\266', '\267',
65 '\270', '\271', '\272', '\273', '\274', '\275', '\276', '\277',
66 '\300', '\301', '\302', '\303', '\304', '\305', '\306', '\307',
67 '\310', '\311', '\312', '\313', '\314', '\315', '\316', '\317',
68 '\320', '\321', '\322', '\323', '\324', '\325', '\326', '\327',
69 '\330', '\331', '\332', '\333', '\334', '\335', '\336', '\337',
70 '\340', '\341', '\342', '\343', '\344', '\345', '\346', '\347',
71 '\350', '\351', '\352', '\353', '\354', '\355', '\356', '\357',
72 '\360', '\361', '\362', '\363', '\364', '\365', '\366', '\367',
73 '\370', '\371', '\372', '\373', '\374', '\375', '\376', '\377',
74};
75
76int
77strcasecmp(const char *s1, const char *s2)
78{
79 const u_char *cm = charmap;
80 const u_char *us1 = (const u_char *)s1;
81 const u_char *us2 = (const u_char *)s2;
82
83 while (cm[*us1] == cm[*us2++])
84 if (*us1++ == '\0')
85 return (0);
86 return (cm[*us1] - cm[*--us2]);
87}
88DEF_WEAK(strcasecmp);
89
90int
91strncasecmp(const char *s1, const char *s2, size_t n)
92{
93 if (n != 0) {
94 const u_char *cm = charmap;
95 const u_char *us1 = (const u_char *)s1;
96 const u_char *us2 = (const u_char *)s2;
97
98 do {
99 if (cm[*us1] != cm[*us2++])
100 return (cm[*us1] - cm[*--us2]);
101 if (*us1++ == '\0')
102 break;
103 } while (--n != 0);
104 }
105 return (0);
106}
107DEF_WEAK(strncasecmp);
diff --git a/src/lib/libc/string/strcasecmp_l.c b/src/lib/libc/string/strcasecmp_l.c
deleted file mode 100644
index a9543dda11..0000000000
--- a/src/lib/libc/string/strcasecmp_l.c
+++ /dev/null
@@ -1,21 +0,0 @@
1/* $OpenBSD: strcasecmp_l.c,v 1.1 2017/09/05 03:16:13 schwarze Exp $ */
2/*
3 * Written in 2017 by Ingo Schwarze <schwarze@openbsd.org>.
4 * Released into the public domain.
5 */
6
7#include <string.h>
8
9int
10strcasecmp_l(const char *s1, const char *s2,
11 locale_t locale __attribute__((__unused__)))
12{
13 return strcasecmp(s1, s2);
14}
15
16int
17strncasecmp_l(const char *s1, const char *s2, size_t n,
18 locale_t locale __attribute__((__unused__)))
19{
20 return strncasecmp(s1, s2, n);
21}
diff --git a/src/lib/libc/string/strcasestr.c b/src/lib/libc/string/strcasestr.c
deleted file mode 100644
index abb3e15549..0000000000
--- a/src/lib/libc/string/strcasestr.c
+++ /dev/null
@@ -1,61 +0,0 @@
1/* $OpenBSD: strcasestr.c,v 1.4 2015/08/31 02:53:57 guenther Exp $ */
2/* $NetBSD: strcasestr.c,v 1.2 2005/02/09 21:35:47 kleink Exp $ */
3
4/*-
5 * Copyright (c) 1990, 1993
6 * The Regents of the University of California. All rights reserved.
7 *
8 * This code is derived from software contributed to Berkeley by
9 * Chris Torek.
10 *
11 * Redistribution and use in source and binary forms, with or without
12 * modification, are permitted provided that the following conditions
13 * are met:
14 * 1. Redistributions of source code must retain the above copyright
15 * notice, this list of conditions and the following disclaimer.
16 * 2. Redistributions in binary form must reproduce the above copyright
17 * notice, this list of conditions and the following disclaimer in the
18 * documentation and/or other materials provided with the distribution.
19 * 3. 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#include <ctype.h>
37#include <string.h>
38
39/*
40 * Find the first occurrence of find in s, ignore case.
41 */
42char *
43strcasestr(const char *s, const char *find)
44{
45 char c, sc;
46 size_t len;
47
48 if ((c = *find++) != 0) {
49 c = (char)tolower((unsigned char)c);
50 len = strlen(find);
51 do {
52 do {
53 if ((sc = *s++) == 0)
54 return (NULL);
55 } while ((char)tolower((unsigned char)sc) != c);
56 } while (strncasecmp(s, find, len) != 0);
57 s--;
58 }
59 return ((char *)s);
60}
61DEF_WEAK(strcasestr);
diff --git a/src/lib/libc/string/strcat.3 b/src/lib/libc/string/strcat.3
deleted file mode 100644
index 68da4ed4a5..0000000000
--- a/src/lib/libc/string/strcat.3
+++ /dev/null
@@ -1,76 +0,0 @@
1.\" $OpenBSD: strcat.3,v 1.19 2022/08/01 00:04:46 jsg Exp $
2.\"
3.\" Copyright (c) 1990, 1991 The Regents of the University of California.
4.\" All rights reserved.
5.\"
6.\" This code is derived from software contributed to Berkeley by
7.\" Chris Torek and the American National Standards Committee X3,
8.\" on Information Processing Systems.
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. 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.Dd $Mdocdate: August 1 2022 $
35.Dt STRCAT 3
36.Os
37.Sh NAME
38.Nm strcat
39.Nd concatenate two strings without bounds checking
40.Sh SYNOPSIS
41.In string.h
42.Ft char *
43.Fn strcat "char *dst" "const char *append"
44.Sh DESCRIPTION
45The
46.Fn strcat
47function appends a copy of the NUL-terminated string
48.Fa append
49to the end of the NUL-terminated string
50.Fa dst ,
51then adds a terminating
52.Ql \e0 .
53.Pp
54No bounds checking is performed.
55If the buffer
56.Fa dst
57is not large enough to hold the result,
58subsequent memory will be damaged.
59.Sh RETURN VALUES
60The
61.Fn strcat
62function return the pointer
63.Fa dst .
64.Sh SEE ALSO
65.Xr strlcpy 3 ,
66.Xr wcscat 3 ,
67.Xr wcslcpy 3
68.Sh STANDARDS
69The
70.Fn strcat
71function conforms to
72.St -ansiC .
73.Sh HISTORY
74The
75.Fn strcat
76function first appeared outside of Bell Labs in PWB/UNIX 1.0.
diff --git a/src/lib/libc/string/strcat.c b/src/lib/libc/string/strcat.c
deleted file mode 100644
index 73da22f75d..0000000000
--- a/src/lib/libc/string/strcat.c
+++ /dev/null
@@ -1,47 +0,0 @@
1/* $OpenBSD: strcat.c,v 1.10 2017/11/28 06:55:49 tb Exp $ */
2
3/*
4 * Copyright (c) 1988 Regents of the University of California.
5 * 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. Neither the name of the University nor the names of its contributors
16 * may be used to endorse or promote products derived from this software
17 * without specific prior written permission.
18 *
19 * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
20 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
21 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
22 * ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
23 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
24 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
25 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
26 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
27 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
28 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
29 * SUCH DAMAGE.
30 */
31
32#include <string.h>
33
34#if defined(APIWARN)
35__warn_references(strcat,
36 "strcat() is almost always misused, please use strlcat()");
37#endif
38
39char *
40strcat(char *s, const char *append)
41{
42 char *save = s;
43
44 for (; *s; ++s);
45 while ((*s++ = *append++) != '\0');
46 return(save);
47}
diff --git a/src/lib/libc/string/strchr.3 b/src/lib/libc/string/strchr.3
deleted file mode 100644
index d1dc67e5ec..0000000000
--- a/src/lib/libc/string/strchr.3
+++ /dev/null
@@ -1,116 +0,0 @@
1.\" $OpenBSD: strchr.3,v 1.14 2024/12/11 23:28:20 jsg Exp $
2.\"
3.\" Copyright (c) 1990, 1991 The Regents of the University of California.
4.\" All rights reserved.
5.\"
6.\" This code is derived from software contributed to Berkeley by
7.\" Chris Torek and the American National Standards Committee X3,
8.\" on Information Processing Systems.
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. 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.Dd $Mdocdate: December 11 2024 $
35.Dt STRCHR 3
36.Os
37.Sh NAME
38.Nm strchr ,
39.Nm index
40.Nd locate first occurrence of a character in a string
41.Sh SYNOPSIS
42.In string.h
43.Ft char *
44.Fn strchr "const char *s" "int c"
45.In strings.h
46.Ft char *
47.Fn index "const char *s" "int c"
48.Sh DESCRIPTION
49The
50.Fn strchr
51function locates the first occurrence of the character
52.Fa c
53.Pq converted to a char
54in the string
55.Fa s .
56The terminating NUL character is considered part of the string.
57If
58.Fa c
59is
60.Ql \e0 ,
61.Fn strchr
62locates the terminating
63.Ql \e0 .
64.Pp
65The
66.Fn index
67function is an old synonym for
68.Fn strchr .
69.Sh RETURN VALUES
70The
71.Fn strchr
72function returns a pointer to the located character or
73.Dv NULL
74if the character does not appear in the string.
75.Sh EXAMPLES
76After the following call to
77.Fn strchr ,
78.Va p
79will point to the string
80.Qq oobar :
81.Bd -literal -offset indent
82char *p;
83char *s = "foobar";
84
85p = strchr(s, 'o');
86.Ed
87.Sh SEE ALSO
88.Xr memchr 3 ,
89.Xr strcspn 3 ,
90.Xr strpbrk 3 ,
91.Xr strrchr 3 ,
92.Xr strsep 3 ,
93.Xr strspn 3 ,
94.Xr strstr 3 ,
95.Xr strtok 3 ,
96.Xr wcschr 3
97.Sh STANDARDS
98The
99.Fn strchr
100function conforms to
101.St -ansiC .
102.Pp
103The
104.Fn index
105function is deprecated and shouldn't be used in new code.
106.Sh HISTORY
107The
108.Fn index
109function first appeared in
110.At v7 .
111The
112.Fn strchr
113function first appeared in
114.At III
115and was reimplemented for
116.Bx 4.3 .
diff --git a/src/lib/libc/string/strchr.c b/src/lib/libc/string/strchr.c
deleted file mode 100644
index 8bfa7ac3aa..0000000000
--- a/src/lib/libc/string/strchr.c
+++ /dev/null
@@ -1,46 +0,0 @@
1/* $OpenBSD: strchr.c,v 1.4 2018/10/01 06:37:37 martijn Exp $ */
2/*-
3 * Copyright (c) 1990 The Regents of the University of California.
4 * 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. Neither the name of the University nor the names of its contributors
15 * may be used to endorse or promote products derived from this software
16 * without specific prior written permission.
17 *
18 * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
19 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
20 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
21 * ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
22 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
23 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
24 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
25 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
26 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
27 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
28 * SUCH DAMAGE.
29 */
30
31#include <string.h>
32
33__weak_alias(index, strchr);
34
35char *
36strchr(const char *p, int ch)
37{
38 for (;; ++p) {
39 if (*p == (char) ch)
40 return((char *)p);
41 if (!*p)
42 return((char *)NULL);
43 }
44 /* NOTREACHED */
45}
46DEF_STRONG(strchr);
diff --git a/src/lib/libc/string/strcmp.3 b/src/lib/libc/string/strcmp.3
deleted file mode 100644
index 4ef341e53e..0000000000
--- a/src/lib/libc/string/strcmp.3
+++ /dev/null
@@ -1,95 +0,0 @@
1.\" $OpenBSD: strcmp.3,v 1.15 2022/08/01 00:04:46 jsg Exp $
2.\"
3.\" Copyright (c) 1990, 1991 The Regents of the University of California.
4.\" All rights reserved.
5.\"
6.\" This code is derived from software contributed to Berkeley by
7.\" Chris Torek and the American National Standards Committee X3,
8.\" on Information Processing Systems.
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. 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.Dd $Mdocdate: August 1 2022 $
35.Dt STRCMP 3
36.Os
37.Sh NAME
38.Nm strcmp ,
39.Nm strncmp
40.Nd compare strings
41.Sh SYNOPSIS
42.In string.h
43.Ft int
44.Fn strcmp "const char *s1" "const char *s2"
45.Ft int
46.Fn strncmp "const char *s1" "const char *s2" "size_t len"
47.Sh DESCRIPTION
48The
49.Fn strcmp
50and
51.Fn strncmp
52functions lexicographically compare the NUL-terminated strings
53.Fa s1
54and
55.Fa s2 .
56The
57.Fn strncmp
58function compares at most
59.Fa len
60characters.
61.Sh RETURN VALUES
62The
63.Fn strcmp
64and
65.Fn strncmp
66functions return an integer greater than, equal to, or less than 0, according
67to whether the string
68.Fa s1
69is greater than, equal to, or less than the string
70.Fa s2 .
71The comparison is done using unsigned characters, so that
72.Ql \e200
73is greater than
74.Ql \e0 .
75.Sh SEE ALSO
76.Xr bcmp 3 ,
77.Xr memcmp 3 ,
78.Xr strcasecmp 3 ,
79.Xr strcoll 3 ,
80.Xr strxfrm 3 ,
81.Xr wcscmp 3
82.Sh STANDARDS
83The
84.Fn strcmp
85and
86.Fn strncmp
87functions conform to
88.St -ansiC .
89.Sh HISTORY
90The
91.Fn strcmp
92function first appeared outside of Bell Labs in PWB/UNIX 1.0.
93.Fn strncmp
94first appeared in
95.At v7 .
diff --git a/src/lib/libc/string/strcmp.c b/src/lib/libc/string/strcmp.c
deleted file mode 100644
index be175563d4..0000000000
--- a/src/lib/libc/string/strcmp.c
+++ /dev/null
@@ -1,48 +0,0 @@
1/* $OpenBSD: strcmp.c,v 1.9 2015/08/31 02:53:57 guenther Exp $ */
2
3/*-
4 * Copyright (c) 1990 The Regents of the University of California.
5 * All rights reserved.
6 *
7 * This code is derived from software contributed to Berkeley by
8 * Chris Torek.
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. 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
35#include <string.h>
36
37/*
38 * Compare strings.
39 */
40int
41strcmp(const char *s1, const char *s2)
42{
43 while (*s1 == *s2++)
44 if (*s1++ == 0)
45 return (0);
46 return (*(unsigned char *)s1 - *(unsigned char *)--s2);
47}
48DEF_STRONG(strcmp);
diff --git a/src/lib/libc/string/strcoll.3 b/src/lib/libc/string/strcoll.3
deleted file mode 100644
index 9daa6cd93f..0000000000
--- a/src/lib/libc/string/strcoll.3
+++ /dev/null
@@ -1,103 +0,0 @@
1.\" $OpenBSD: strcoll.3,v 1.11 2019/01/18 07:43:36 schwarze Exp $
2.\"
3.\" Copyright (c) 1990, 1991 The Regents of the University of California.
4.\" Copyright (c) 2017 Ingo Schwarze <schwarze@openbsd.org>
5.\" All rights reserved.
6.\"
7.\" This code is derived from software contributed to Berkeley by
8.\" Chris Torek and the American National Standards Committee X3,
9.\" on Information Processing Systems.
10.\"
11.\" Redistribution and use in source and binary forms, with or without
12.\" modification, are permitted provided that the following conditions
13.\" are met:
14.\" 1. Redistributions of source code must retain the above copyright
15.\" notice, this list of conditions and the following disclaimer.
16.\" 2. Redistributions in binary form must reproduce the above copyright
17.\" notice, this list of conditions and the following disclaimer in the
18.\" documentation and/or other materials provided with the distribution.
19.\" 3. 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.Dd $Mdocdate: January 18 2019 $
36.Dt STRCOLL 3
37.Os
38.Sh NAME
39.Nm strcoll ,
40.Nm strcoll_l
41.Nd compare strings according to current collation
42.Sh SYNOPSIS
43.In string.h
44.Ft int
45.Fn strcoll "const char *s1" "const char *s2"
46.Ft int
47.Fn strcoll_l "const char *s1" "const char *s2" "locale_t locale"
48.Sh DESCRIPTION
49The
50.Fn strcoll
51and
52.Fn strcoll_l
53functions lexicographically compare the NUL-terminated strings
54.Fa s1
55and
56.Fa s2
57according to the current locale collation
58and return an integer greater than, equal to, or less than 0,
59according to whether
60.Fa s1
61is greater than, equal to, or less than
62.Fa s2 .
63.Pp
64On
65.Ox ,
66they have the same effect as
67.Xr strcmp 3 ,
68and the global locale, the thread-specific locale, and the
69.Fa locale
70argument are ignored.
71On other operating systems, results may depend on the
72.Dv LC_CTYPE
73and
74.Dv LC_COLLATE
75locale categories set with
76.Xr setlocale 3 ,
77.Xr uselocale 3 ,
78or
79.Xr newlocale 3 .
80.Sh SEE ALSO
81.Xr newlocale 3 ,
82.Xr setlocale 3 ,
83.Xr strcmp 3 ,
84.Xr strxfrm 3 ,
85.Xr wcscoll 3
86.Sh STANDARDS
87The
88.Fn strcoll
89function conforms to
90.St -ansiC ,
91and
92.Fn strcoll_l
93to
94.St -p1003.1-2008 .
95.Sh HISTORY
96The
97.Fn strcoll
98function has been available since
99.Bx 4.3 Reno ,
100and
101.Fn strcoll_l
102since
103.Ox 6.2 .
diff --git a/src/lib/libc/string/strcoll.c b/src/lib/libc/string/strcoll.c
deleted file mode 100644
index 47a6ea4f24..0000000000
--- a/src/lib/libc/string/strcoll.c
+++ /dev/null
@@ -1,45 +0,0 @@
1/* $OpenBSD: strcoll.c,v 1.6 2015/08/31 02:53:57 guenther Exp $ */
2/*-
3 * Copyright (c) 1990 The Regents of the University of California.
4 * All rights reserved.
5 *
6 * This code is derived from software contributed to Berkeley by
7 * Chris Torek.
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. 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#include <string.h>
35
36/*
37 * Compare strings according to LC_COLLATE category of current locale.
38 */
39int
40strcoll(const char *s1, const char *s2)
41{
42 /* LC_COLLATE is unimplemented, hence always "C" */
43 return (strcmp(s1, s2));
44}
45DEF_STRONG(strcoll);
diff --git a/src/lib/libc/string/strcoll_l.c b/src/lib/libc/string/strcoll_l.c
deleted file mode 100644
index bcd5b0f417..0000000000
--- a/src/lib/libc/string/strcoll_l.c
+++ /dev/null
@@ -1,14 +0,0 @@
1/* $OpenBSD: strcoll_l.c,v 1.1 2017/09/05 03:16:13 schwarze Exp $ */
2/*
3 * Written in 2017 by Ingo Schwarze <schwarze@openbsd.org>.
4 * Released into the public domain.
5 */
6
7#include <string.h>
8
9int
10strcoll_l(const char *s1, const char *s2,
11 locale_t locale __attribute__((__unused__)))
12{
13 return strcmp(s1, s2);
14}
diff --git a/src/lib/libc/string/strcpy.3 b/src/lib/libc/string/strcpy.3
deleted file mode 100644
index 4bf690ba76..0000000000
--- a/src/lib/libc/string/strcpy.3
+++ /dev/null
@@ -1,82 +0,0 @@
1.\" $OpenBSD: strcpy.3,v 1.22 2022/08/01 00:04:46 jsg Exp $
2.\"
3.\" Copyright (c) 1990, 1991 The Regents of the University of California.
4.\" All rights reserved.
5.\"
6.\" This code is derived from software contributed to Berkeley by
7.\" Chris Torek and the American National Standards Committee X3,
8.\" on Information Processing Systems.
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. 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.Dd $Mdocdate: August 1 2022 $
35.Dt STRCPY 3
36.Os
37.Sh NAME
38.Nm strcpy
39.Nd copy a string without bounds checking
40.Sh SYNOPSIS
41.In string.h
42.Ft char *
43.Fn strcpy "char *dst" "const char *src"
44.Sh DESCRIPTION
45The
46.Fn strcpy
47function copies the string
48.Fa src
49(including the terminating
50.Ql \e0
51character) to the buffer
52.Fa dst .
53.Pp
54No bounds checking is performed.
55If the buffer
56.Fa dst
57is not large enough to hold the result,
58subsequent memory will be damaged.
59.Pp
60If the
61.Fa src
62string is inside the
63.Fa dst
64buffer, the behavior is undefined.
65.Sh RETURN VALUES
66The
67.Fn strcpy
68function returns
69.Fa dst .
70.Sh SEE ALSO
71.Xr strlcpy 3 ,
72.Xr wcscpy 3 ,
73.Xr wcslcpy 3
74.Sh STANDARDS
75The
76.Fn strcpy
77function conforms to
78.St -ansiC .
79.Sh HISTORY
80The
81.Fn strcpy
82function first appeared outside of Bell Labs in PWB/UNIX 1.0.
diff --git a/src/lib/libc/string/strcpy.c b/src/lib/libc/string/strcpy.c
deleted file mode 100644
index 290eefeabf..0000000000
--- a/src/lib/libc/string/strcpy.c
+++ /dev/null
@@ -1,46 +0,0 @@
1/* $OpenBSD: strcpy.c,v 1.10 2017/11/28 06:55:49 tb Exp $ */
2
3/*
4 * Copyright (c) 1988 Regents of the University of California.
5 * 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. Neither the name of the University nor the names of its contributors
16 * may be used to endorse or promote products derived from this software
17 * without specific prior written permission.
18 *
19 * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
20 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
21 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
22 * ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
23 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
24 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
25 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
26 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
27 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
28 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
29 * SUCH DAMAGE.
30 */
31
32#include <string.h>
33
34#if defined(APIWARN)
35__warn_references(strcpy,
36 "strcpy() is almost always misused, please use strlcpy()");
37#endif
38
39char *
40strcpy(char *to, const char *from)
41{
42 char *save = to;
43
44 for (; (*to = *from) != '\0'; ++from, ++to);
45 return(save);
46}
diff --git a/src/lib/libc/string/strcspn.3 b/src/lib/libc/string/strcspn.3
deleted file mode 100644
index 91e5c3f1cc..0000000000
--- a/src/lib/libc/string/strcspn.3
+++ /dev/null
@@ -1,108 +0,0 @@
1.\" $OpenBSD: strcspn.3,v 1.12 2024/12/11 23:28:20 jsg Exp $
2.\"
3.\" Copyright (c) 1990, 1991 The Regents of the University of California.
4.\" All rights reserved.
5.\"
6.\" This code is derived from software contributed to Berkeley by
7.\" Chris Torek and the American National Standards Committee X3,
8.\" on Information Processing Systems.
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. 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.Dd $Mdocdate: December 11 2024 $
35.Dt STRCSPN 3
36.Os
37.Sh NAME
38.Nm strcspn
39.Nd span the complement of a string
40.Sh SYNOPSIS
41.In string.h
42.Ft size_t
43.Fn strcspn "const char *s" "const char *charset"
44.Sh DESCRIPTION
45The
46.Fn strcspn
47function spans the initial part of the NUL-terminated string
48.Fa s
49as long as the characters from
50.Fa s
51do not occur in string
52.Fa charset
53(it spans the
54.Em complement
55of
56.Fa charset ) .
57.Sh RETURN VALUES
58The
59.Fn strcspn
60function returns the number of characters spanned.
61.Sh EXAMPLES
62The following call to
63.Fn strcspn
64will return 3, since the first three characters of string
65.Fa s
66do not occur in string
67.Fa charset :
68.Bd -literal -offset indent
69char *s = "foobar";
70char *charset = "bar";
71size_t span;
72
73span = strcspn(s, charset);
74.Ed
75.Pp
76The following removes the first (if any) newline character from string
77.Fa line .
78This is useful for trimming the newline after a
79.Xr fgets 3
80call.
81.Bd -literal -offset indent
82char line[BUFSIZ];
83
84if (fgets(line, sizeof(line), fp) != NULL)
85 line[strcspn(line, "\en")] = '\e0';
86.Ed
87.Sh SEE ALSO
88.Xr memchr 3 ,
89.Xr strchr 3 ,
90.Xr strpbrk 3 ,
91.Xr strrchr 3 ,
92.Xr strsep 3 ,
93.Xr strspn 3 ,
94.Xr strstr 3 ,
95.Xr strtok 3 ,
96.Xr wcscspn 3
97.Sh STANDARDS
98The
99.Fn strcspn
100function conforms to
101.St -ansiC .
102.Sh HISTORY
103The
104.Fn strcspn
105function first appeared in
106.At III
107and was reimplemented for
108.Bx 4.3 .
diff --git a/src/lib/libc/string/strcspn.c b/src/lib/libc/string/strcspn.c
deleted file mode 100644
index 3c1f5a4cec..0000000000
--- a/src/lib/libc/string/strcspn.c
+++ /dev/null
@@ -1,59 +0,0 @@
1/* $OpenBSD: strcspn.c,v 1.6 2015/08/31 02:53:57 guenther Exp $ */
2/*-
3 * Copyright (c) 1990 The Regents of the University of California.
4 * All rights reserved.
5 *
6 * This code is derived from software contributed to Berkeley by
7 * Chris Torek.
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. 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#include <string.h>
35
36/*
37 * Span the complement of string s2.
38 */
39size_t
40strcspn(const char *s1, const char *s2)
41{
42 const char *p, *spanp;
43 char c, sc;
44
45 /*
46 * Stop as soon as we find any character from s2. Note that there
47 * must be a NUL in s2; it suffices to stop when we find that, too.
48 */
49 for (p = s1;;) {
50 c = *p++;
51 spanp = s2;
52 do {
53 if ((sc = *spanp++) == c)
54 return (p - 1 - s1);
55 } while (sc != 0);
56 }
57 /* NOTREACHED */
58}
59DEF_STRONG(strcspn);
diff --git a/src/lib/libc/string/strdup.3 b/src/lib/libc/string/strdup.3
deleted file mode 100644
index 283f3bc0ea..0000000000
--- a/src/lib/libc/string/strdup.3
+++ /dev/null
@@ -1,119 +0,0 @@
1.\" $OpenBSD: strdup.3,v 1.22 2015/12/01 01:32:48 mmcc Exp $
2.\"
3.\" Copyright (c) 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. Neither the name of the University nor the names of its contributors
15.\" may be used to endorse or promote products derived from this software
16.\" without specific prior written permission.
17.\"
18.\" THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
19.\" ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
20.\" IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
21.\" ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
22.\" FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
23.\" DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
24.\" OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
25.\" HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
26.\" LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
27.\" OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
28.\" SUCH DAMAGE.
29.\"
30.\" @(#)strdup.3 8.1 (Berkeley) 6/9/93
31.\"
32.Dd $Mdocdate: December 1 2015 $
33.Dt STRDUP 3
34.Os
35.Sh NAME
36.Nm strdup ,
37.Nm strndup
38.Nd save a copy of a string
39.Sh SYNOPSIS
40.In string.h
41.Ft char *
42.Fn strdup "const char *s"
43.Ft char *
44.Fn strndup "const char *s" "size_t maxlen"
45.Sh DESCRIPTION
46The
47.Fn strdup
48function allocates sufficient memory for a copy of the string
49.Fa s ,
50does the copy, and returns a pointer to it.
51The pointer may subsequently be used as an argument to the function
52.Xr free 3 .
53.Pp
54The
55.Fn strndup
56function behaves similarly to
57.Nm strdup
58but only copies up to
59.Fa maxlen
60characters from
61.Fa s .
62The resulting string is always NUL-terminated.
63.Pp
64If the memory allocation fails,
65.Dv NULL
66is returned.
67.Sh EXAMPLES
68The following will point
69.Va p
70to an allocated area of memory containing the NUL-terminated string
71.Qq foobar :
72.Bd -literal -offset indent
73char *p;
74
75p = strdup("foobar");
76if (p == NULL)
77 err(1, NULL);
78.Ed
79.Sh ERRORS
80The
81.Fn strdup
82and
83.Fn strndup
84functions may fail and set the external variable
85.Va errno
86for any of the errors specified for the library function
87.Xr malloc 3 .
88.Sh SEE ALSO
89.Xr free 3 ,
90.Xr malloc 3 ,
91.Xr strcpy 3 ,
92.Xr strlcpy 3 ,
93.Xr strlen 3 ,
94.Xr wcsdup 3
95.Sh STANDARDS
96The
97.Fn strdup
98and
99.Fn strndup
100functions conform to
101.St -p1003.1-2008 .
102.Sh HISTORY
103A
104.Fn strdup
105macro was first used in the
106.Bx 4.1c
107debugger,
108.Sy dbx .
109It was rewritten as a C function for the
110.Bx 4.3
111.Xr inetd 8
112and first appeared in the C library of
113.Bx 4.3 Reno .
114The
115.Fn strndup
116function appeared in glibc 2.0, was reimplemented for
117.Nx 4.0 ,
118and ported to
119.Ox 4.8 .
diff --git a/src/lib/libc/string/strdup.c b/src/lib/libc/string/strdup.c
deleted file mode 100644
index 9aebf399c1..0000000000
--- a/src/lib/libc/string/strdup.c
+++ /dev/null
@@ -1,50 +0,0 @@
1/* $OpenBSD: strdup.c,v 1.7 2015/08/31 02:53:57 guenther 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. Neither the name of the University nor the names of its contributors
16 * may be used to endorse or promote products derived from this software
17 * without specific prior written permission.
18 *
19 * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
20 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
21 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
22 * ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
23 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
24 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
25 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
26 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
27 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
28 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
29 * SUCH DAMAGE.
30 */
31
32#include <sys/types.h>
33
34#include <stddef.h>
35#include <stdlib.h>
36#include <string.h>
37
38char *
39strdup(const char *str)
40{
41 size_t siz;
42 char *copy;
43
44 siz = strlen(str) + 1;
45 if ((copy = malloc(siz)) == NULL)
46 return(NULL);
47 (void)memcpy(copy, str, siz);
48 return(copy);
49}
50DEF_WEAK(strdup);
diff --git a/src/lib/libc/string/strerror.3 b/src/lib/libc/string/strerror.3
deleted file mode 100644
index 6c5b890fa4..0000000000
--- a/src/lib/libc/string/strerror.3
+++ /dev/null
@@ -1,154 +0,0 @@
1.\" $OpenBSD: strerror.3,v 1.16 2019/05/16 13:35:16 schwarze Exp $
2.\"
3.\" Copyright (c) 1980, 1991 Regents of the University of California.
4.\" Copyright (c) 2017 Ingo Schwarze <schwarze@openbsd.org>
5.\" All rights reserved.
6.\"
7.\" This code is derived from software contributed to Berkeley by
8.\" the American National Standards Committee X3, on Information
9.\" Processing Systems.
10.\"
11.\" Redistribution and use in source and binary forms, with or without
12.\" modification, are permitted provided that the following conditions
13.\" are met:
14.\" 1. Redistributions of source code must retain the above copyright
15.\" notice, this list of conditions and the following disclaimer.
16.\" 2. Redistributions in binary form must reproduce the above copyright
17.\" notice, this list of conditions and the following disclaimer in the
18.\" documentation and/or other materials provided with the distribution.
19.\" 3. 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.Dd $Mdocdate: May 16 2019 $
36.Dt STRERROR 3
37.Os
38.Sh NAME
39.Nm strerror ,
40.Nm strerror_l ,
41.Nm strerror_r
42.Nd get error message string
43.Sh SYNOPSIS
44.In string.h
45.Ft char *
46.Fn strerror "int errnum"
47.Ft char *
48.Fn strerror_l "int errnum" "locale_t locale"
49.Ft int
50.Fn strerror_r "int errnum" "char *strerrbuf" "size_t buflen"
51.Sh DESCRIPTION
52These functions map the error number
53.Fa errnum
54to an error message string.
55.Pp
56.Fn strerror
57and
58.Fn strerror_l
59return a string containing a maximum of
60.Dv NL_TEXTMAX
61characters, including the trailing NUL.
62This string is not to be modified by the calling program.
63The string returned by
64.Fn strerror
65may be overwritten by subsequent calls to
66.Fn strerror
67in any thread.
68The string returned by
69.Fn strerror_l
70may be overwritten by subsequent calls to
71.Fn strerror_l
72in the same thread.
73.Pp
74.Fn strerror_r
75is a thread safe version of
76.Fn strerror
77that places the error message in the specified buffer
78.Fa strerrbuf .
79.Pp
80On
81.Ox ,
82the global locale, the thread-specific locale, and the
83.Fa locale
84argument are ignored.
85.Sh RETURN VALUES
86.Fn strerror
87and
88.Fn strerror_l
89return a pointer to the error message string.
90If an error occurs, the error code is stored in
91.Va errno .
92.Pp
93.Fn strerror_r
94returns zero upon successful completion.
95If an error occurs, the error code is stored in
96.Va errno
97and the error code is returned.
98.Sh ERRORS
99All these functions may fail if:
100.Bl -tag -width Er
101.It Bq Er EINVAL
102.Fa errnum
103is not a valid error number.
104The returned error string will consist of an error message that includes
105.Fa errnum .
106.El
107.Pp
108.Fn strerror_r
109may also fail if:
110.Bl -tag -width Er
111.It Bq Er ERANGE
112The error message is larger than
113.Fa buflen
114characters.
115The message will be truncated to fit.
116.El
117.Sh SEE ALSO
118.Xr intro 2 ,
119.Xr newlocale 3 ,
120.Xr perror 3 ,
121.Xr setlocale 3
122.Sh STANDARDS
123The
124.Fn strerror
125function conforms to
126.St -isoC-99 .
127The
128.Fn strerror_l
129and
130.Fn strerror_r
131functions conform to
132.St -p1003.1-2008 .
133.Sh HISTORY
134The
135.Fn strerror
136function has been available since
137.Bx 4.3 Reno ,
138.Fn strerror_r
139since
140.Ox 3.3 ,
141and
142.Fn strerror_l
143since
144.Ox 6.2 .
145.Sh CAVEATS
146On systems other than
147.Ox ,
148the
149.Dv LC_MESSAGES
150.Xr locale 1
151category can cause different strings to be returned instead of the
152normal error messages; see CAVEATS in
153.Xr setlocale 3
154for details.
diff --git a/src/lib/libc/string/strerror.c b/src/lib/libc/string/strerror.c
deleted file mode 100644
index c6f05446fd..0000000000
--- a/src/lib/libc/string/strerror.c
+++ /dev/null
@@ -1,42 +0,0 @@
1/* $OpenBSD: strerror.c,v 1.8 2015/08/31 02:53:57 guenther Exp $ */
2/*
3 * Copyright (c) 1988 Regents of the University of California.
4 * 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. Neither the name of the University nor the names of its contributors
15 * may be used to endorse or promote products derived from this software
16 * without specific prior written permission.
17 *
18 * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
19 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
20 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
21 * ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
22 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
23 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
24 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
25 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
26 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
27 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
28 * SUCH DAMAGE.
29 */
30
31#include <string.h>
32#include <limits.h>
33
34char *
35strerror(int num)
36{
37 static char buf[NL_TEXTMAX];
38
39 (void)strerror_r(num, buf, sizeof(buf));
40 return (buf);
41}
42DEF_STRONG(strerror);
diff --git a/src/lib/libc/string/strerror_l.c b/src/lib/libc/string/strerror_l.c
deleted file mode 100644
index c16be7a0cc..0000000000
--- a/src/lib/libc/string/strerror_l.c
+++ /dev/null
@@ -1,33 +0,0 @@
1/* $OpenBSD: strerror_l.c,v 1.1 2017/09/05 03:16:13 schwarze Exp $ */
2/*
3 * Copyright (c) 2017 Ingo Schwarze <schwarze@openbsd.org>
4 *
5 * Permission to use, copy, modify, and distribute this software for any
6 * purpose with or without fee is hereby granted, provided that the above
7 * copyright notice and this permission notice appear in all copies.
8 *
9 * THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES
10 * WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF
11 * MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR
12 * ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES
13 * WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN
14 * ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF
15 * OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE.
16 */
17
18#include <limits.h>
19#include <string.h>
20
21#include "thread_private.h"
22
23char *
24strerror_l(int errnum, locale_t locale)
25{
26 static char sel_buf[NL_TEXTMAX];
27 _THREAD_PRIVATE_KEY(strerror_l);
28 char *p = _THREAD_PRIVATE(strerror_l, sel_buf, NULL);
29
30 return p == NULL ? "no buffer available in strerror_l" :
31 strerror_r(errnum, p, sizeof(sel_buf)) ?
32 "strerror_r failure" : p;
33}
diff --git a/src/lib/libc/string/strerror_r.c b/src/lib/libc/string/strerror_r.c
deleted file mode 100644
index b45dee12d8..0000000000
--- a/src/lib/libc/string/strerror_r.c
+++ /dev/null
@@ -1,107 +0,0 @@
1/* $OpenBSD: strerror_r.c,v 1.13 2016/10/19 16:26:16 bluhm Exp $ */
2/* Public Domain <marc@snafu.org> */
3
4#include <errno.h>
5#include <limits.h>
6#include <signal.h>
7#include <string.h>
8
9static size_t
10__digits10(unsigned int num)
11{
12 size_t i = 0;
13
14 do {
15 num /= 10;
16 i++;
17 } while (num != 0);
18
19 return i;
20}
21
22static int
23__itoa(int num, int sign, char *buffer, size_t start, size_t end)
24{
25 size_t pos;
26 unsigned int a;
27 int neg;
28
29 if (sign && num < 0) {
30 a = -num;
31 neg = 1;
32 }
33 else {
34 a = num;
35 neg = 0;
36 }
37
38 pos = start + __digits10(a);
39 if (neg)
40 pos++;
41
42 if (pos < end)
43 buffer[pos] = '\0';
44 else
45 return ERANGE;
46 pos--;
47 do {
48 buffer[pos] = (a % 10) + '0';
49 pos--;
50 a /= 10;
51 } while (a != 0);
52 if (neg)
53 buffer[pos] = '-';
54 return 0;
55}
56
57
58static int
59__num2string(int num, int sign, int setid, char *buf, size_t buflen,
60 const char * const list[], size_t max, const char *def)
61{
62 int ret = 0;
63 size_t len;
64
65 if (0 <= num && num < max) {
66 len = strlcpy(buf, list[num], buflen);
67 if (len >= buflen)
68 ret = ERANGE;
69 } else {
70 len = strlcpy(buf, def, buflen);
71 if (len >= buflen)
72 ret = ERANGE;
73 else {
74 ret = __itoa(num, sign, buf, len, buflen);
75 if (ret == 0)
76 ret = EINVAL;
77 }
78 }
79
80 return ret;
81}
82
83#define UPREFIX "Unknown error: "
84
85int
86strerror_r(int errnum, char *strerrbuf, size_t buflen)
87{
88 int ret_errno;
89
90 ret_errno = __num2string(errnum, 1, 1, strerrbuf, buflen,
91 sys_errlist, sys_nerr, UPREFIX);
92
93 if (ret_errno)
94 errno = ret_errno;
95 return (ret_errno);
96}
97DEF_WEAK(strerror_r);
98
99#define USIGPREFIX "Unknown signal: "
100
101char *
102__strsignal(int num, char *buf)
103{
104 __num2string(num, 0, 2, buf, NL_TEXTMAX, sys_siglist, NSIG,
105 USIGPREFIX);
106 return buf;
107}
diff --git a/src/lib/libc/string/strlcat.c b/src/lib/libc/string/strlcat.c
deleted file mode 100644
index aa3db7ab37..0000000000
--- a/src/lib/libc/string/strlcat.c
+++ /dev/null
@@ -1,56 +0,0 @@
1/* $OpenBSD: strlcat.c,v 1.19 2019/01/25 00:19:25 millert Exp $ */
2
3/*
4 * Copyright (c) 1998, 2015 Todd C. Miller <millert@openbsd.org>
5 *
6 * Permission to use, copy, modify, and distribute this software for any
7 * purpose with or without fee is hereby granted, provided that the above
8 * copyright notice and this permission notice appear in all copies.
9 *
10 * THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES
11 * WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF
12 * MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR
13 * ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES
14 * WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN
15 * ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF
16 * OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE.
17 */
18
19#include <sys/types.h>
20#include <string.h>
21
22/*
23 * Appends src to string dst of size dsize (unlike strncat, dsize is the
24 * full size of dst, not space left). At most dsize-1 characters
25 * will be copied. Always NUL terminates (unless dsize <= strlen(dst)).
26 * Returns strlen(src) + MIN(dsize, strlen(initial dst)).
27 * If retval >= dsize, truncation occurred.
28 */
29size_t
30strlcat(char *dst, const char *src, size_t dsize)
31{
32 const char *odst = dst;
33 const char *osrc = src;
34 size_t n = dsize;
35 size_t dlen;
36
37 /* Find the end of dst and adjust bytes left but don't go past end. */
38 while (n-- != 0 && *dst != '\0')
39 dst++;
40 dlen = dst - odst;
41 n = dsize - dlen;
42
43 if (n-- == 0)
44 return(dlen + strlen(src));
45 while (*src != '\0') {
46 if (n != 0) {
47 *dst++ = *src;
48 n--;
49 }
50 src++;
51 }
52 *dst = '\0';
53
54 return(dlen + (src - osrc)); /* count does not include NUL */
55}
56DEF_WEAK(strlcat);
diff --git a/src/lib/libc/string/strlcpy.3 b/src/lib/libc/string/strlcpy.3
deleted file mode 100644
index 4607289883..0000000000
--- a/src/lib/libc/string/strlcpy.3
+++ /dev/null
@@ -1,195 +0,0 @@
1.\" $OpenBSD: strlcpy.3,v 1.28 2024/08/03 20:13:23 guenther Exp $
2.\"
3.\" Copyright (c) 1998, 2000 Todd C. Miller <millert@openbsd.org>
4.\"
5.\" Permission to use, copy, modify, and distribute this software for any
6.\" purpose with or without fee is hereby granted, provided that the above
7.\" copyright notice and this permission notice appear in all copies.
8.\"
9.\" THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES
10.\" WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF
11.\" MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR
12.\" ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES
13.\" WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN
14.\" ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF
15.\" OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE.
16.\"
17.Dd $Mdocdate: August 3 2024 $
18.Dt STRLCPY 3
19.Os
20.Sh NAME
21.Nm strlcpy ,
22.Nm strlcat
23.Nd size-bounded string copying and concatenation
24.Sh SYNOPSIS
25.In string.h
26.Ft size_t
27.Fn strlcpy "char * restrict dst" "const char * restrict src" "size_t dstsize"
28.Ft size_t
29.Fn strlcat "char * restrict dst" "const char * restrict src" "size_t dstsize"
30.Sh DESCRIPTION
31The
32.Fn strlcpy
33and
34.Fn strlcat
35functions copy and concatenate strings with the
36same input parameters and output result as
37.Xr snprintf 3 .
38They are designed to be safer, more consistent, and less error
39prone replacements for the easily misused functions
40.Xr strncpy 3
41and
42.Xr strncat 3 .
43.Pp
44.Fn strlcpy
45and
46.Fn strlcat
47take the full size of the destination buffer and guarantee
48NUL-termination if there is room.
49Note that room for the NUL should be included in
50.Fa dstsize .
51.Pp
52.Fn strlcpy
53copies up to
54.Fa dstsize
55\- 1 characters from the string
56.Fa src
57to
58.Fa dst ,
59NUL-terminating the result if
60.Fa dstsize
61is not 0.
62.Pp
63.Fn strlcat
64appends string
65.Fa src
66to the end of
67.Fa dst .
68It will append at most
69.Fa dstsize
70\- strlen(dst) \- 1 characters.
71It will then NUL-terminate, unless
72.Fa dstsize
73is 0 or the original
74.Fa dst
75string was longer than
76.Fa dstsize
77(in practice this should not happen
78as it means that either
79.Fa dstsize
80is incorrect or that
81.Fa dst
82is not a proper string).
83.Pp
84If the
85.Fa src
86and
87.Fa dst
88strings overlap, the behavior is undefined.
89.Sh RETURN VALUES
90Besides quibbles over the return type
91.Pf ( Va size_t
92versus
93.Va int )
94and signal handler safety
95.Pf ( Xr snprintf 3
96is not entirely safe on some systems), the
97following two are equivalent:
98.Bd -literal -offset indent
99n = strlcpy(dst, src, len);
100n = snprintf(dst, len, "%s", src);
101.Ed
102.Pp
103Like
104.Xr snprintf 3 ,
105the
106.Fn strlcpy
107and
108.Fn strlcat
109functions return the total length of the string they tried to create.
110For
111.Fn strlcpy
112that means the length of
113.Fa src .
114For
115.Fn strlcat
116that means the initial length of
117.Fa dst
118plus
119the length of
120.Fa src .
121.Pp
122If the return value is
123.Cm >=
124.Va dstsize ,
125the output string has been truncated.
126It is the caller's responsibility to handle this.
127.Sh EXAMPLES
128The following code fragment illustrates the simple case:
129.Bd -literal -offset indent
130char *s, *p, buf[BUFSIZ];
131
132\&...
133
134(void)strlcpy(buf, s, sizeof(buf));
135(void)strlcat(buf, p, sizeof(buf));
136.Ed
137.Pp
138To detect truncation, perhaps while building a pathname, something
139like the following might be used:
140.Bd -literal -offset indent
141char *dir, *file, pname[PATH_MAX];
142
143\&...
144
145if (strlcpy(pname, dir, sizeof(pname)) >= sizeof(pname))
146 goto toolong;
147if (strlcat(pname, file, sizeof(pname)) >= sizeof(pname))
148 goto toolong;
149.Ed
150.Pp
151Since it is known how many characters were copied the first time, things
152can be sped up a bit by using a copy instead of an append:
153.Bd -literal -offset indent
154char *dir, *file, pname[PATH_MAX];
155size_t n;
156
157\&...
158
159n = strlcpy(pname, dir, sizeof(pname));
160if (n >= sizeof(pname))
161 goto toolong;
162if (strlcpy(pname + n, file, sizeof(pname) - n) >= sizeof(pname) - n)
163 goto toolong;
164.Ed
165.Pp
166However, one may question the validity of such optimizations, as they
167defeat the whole purpose of
168.Fn strlcpy
169and
170.Fn strlcat .
171As a matter of fact, the first version of this manual page got it wrong.
172.Sh SEE ALSO
173.Xr snprintf 3 ,
174.Xr strncat 3 ,
175.Xr strncpy 3 ,
176.Xr wcslcpy 3
177.Sh STANDARDS
178The
179.Fn strlcat
180and
181.Fn strlcpy
182functions conform to
183.St -p1003.1-2024 .
184.Sh HISTORY
185.Fn strlcpy
186and
187.Fn strlcat
188first appeared in
189.Ox 2.4 .
190.Sh AUTHORS
191.Fn strlcpy
192and
193.Fn strlcat
194were created by
195.An Todd C. Miller Aq Mt millert@openbsd.org .
diff --git a/src/lib/libc/string/strlcpy.c b/src/lib/libc/string/strlcpy.c
deleted file mode 100644
index 7e3b9aef6f..0000000000
--- a/src/lib/libc/string/strlcpy.c
+++ /dev/null
@@ -1,51 +0,0 @@
1/* $OpenBSD: strlcpy.c,v 1.16 2019/01/25 00:19:25 millert Exp $ */
2
3/*
4 * Copyright (c) 1998, 2015 Todd C. Miller <millert@openbsd.org>
5 *
6 * Permission to use, copy, modify, and distribute this software for any
7 * purpose with or without fee is hereby granted, provided that the above
8 * copyright notice and this permission notice appear in all copies.
9 *
10 * THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES
11 * WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF
12 * MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR
13 * ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES
14 * WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN
15 * ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF
16 * OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE.
17 */
18
19#include <sys/types.h>
20#include <string.h>
21
22/*
23 * Copy string src to buffer dst of size dsize. At most dsize-1
24 * chars will be copied. Always NUL terminates (unless dsize == 0).
25 * Returns strlen(src); if retval >= dsize, truncation occurred.
26 */
27size_t
28strlcpy(char *dst, const char *src, size_t dsize)
29{
30 const char *osrc = src;
31 size_t nleft = dsize;
32
33 /* Copy as many bytes as will fit. */
34 if (nleft != 0) {
35 while (--nleft != 0) {
36 if ((*dst++ = *src++) == '\0')
37 break;
38 }
39 }
40
41 /* Not enough room in dst, add NUL and traverse rest of src. */
42 if (nleft == 0) {
43 if (dsize != 0)
44 *dst = '\0'; /* NUL-terminate dst */
45 while (*src++)
46 ;
47 }
48
49 return(src - osrc - 1); /* count does not include NUL */
50}
51DEF_WEAK(strlcpy);
diff --git a/src/lib/libc/string/strlen.3 b/src/lib/libc/string/strlen.3
deleted file mode 100644
index 18569d58b9..0000000000
--- a/src/lib/libc/string/strlen.3
+++ /dev/null
@@ -1,103 +0,0 @@
1.\" $OpenBSD: strlen.3,v 1.14 2022/07/31 14:50:32 jsg Exp $
2.\"
3.\" Copyright (c) 1990, 1991 The Regents of the University of California.
4.\" All rights reserved.
5.\"
6.\" This code is derived from software contributed to Berkeley by
7.\" Chris Torek and the American National Standards Committee X3,
8.\" on Information Processing Systems.
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. 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.Dd $Mdocdate: July 31 2022 $
35.Dt STRLEN 3
36.Os
37.Sh NAME
38.Nm strlen ,
39.Nm strnlen
40.Nd find length of a string
41.Sh SYNOPSIS
42.In string.h
43.Ft size_t
44.Fn strlen "const char *s"
45.Ft size_t
46.Fn strnlen "const char *s" "size_t maxlen"
47.Sh DESCRIPTION
48The
49.Fn strlen
50function computes the length of the string
51.Fa s .
52.Pp
53The
54.Fn strnlen
55function computes the length of the string
56.Fa s ,
57up to
58.Fa maxlen
59characters.
60The
61.Fn strnlen
62function will never attempt to address more than
63.Fa maxlen
64characters, making it suitable for use with character arrays that are
65not guaranteed to be NUL-terminated.
66.Sh RETURN VALUES
67The
68.Fn strlen
69function returns the number of characters that precede the terminating
70.Tn NUL
71character.
72.Pp
73The
74.Fn strnlen
75function returns the number of characters that precede the terminating
76.Tn NUL
77or
78.Fa maxlen ,
79whichever is smaller.
80.Sh SEE ALSO
81.Xr wcslen 3
82.Sh STANDARDS
83The
84.Fn strlen
85function conforms to
86.St -ansiC .
87.Pp
88The
89.Fn strlen
90and
91.Fn strnlen
92functions conform to
93.St -p1003.1-2008 .
94.Sh HISTORY
95The
96.Fn strlen
97function first appeared in
98.At v6 .
99The
100.Fn strnlen
101function appeared in glibc 2.0
102and was reimplemented for
103.Ox 4.8 .
diff --git a/src/lib/libc/string/strlen.c b/src/lib/libc/string/strlen.c
deleted file mode 100644
index a5721d3e7f..0000000000
--- a/src/lib/libc/string/strlen.c
+++ /dev/null
@@ -1,44 +0,0 @@
1/* $OpenBSD: strlen.c,v 1.9 2015/08/31 02:53:57 guenther 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. Neither the name of the University nor the names of its contributors
16 * may be used to endorse or promote products derived from this software
17 * without specific prior written permission.
18 *
19 * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
20 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
21 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
22 * ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
23 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
24 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
25 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
26 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
27 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
28 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
29 * SUCH DAMAGE.
30 */
31
32#include <string.h>
33
34size_t
35strlen(const char *str)
36{
37 const char *s;
38
39 for (s = str; *s; ++s)
40 ;
41 return (s - str);
42}
43
44DEF_STRONG(strlen);
diff --git a/src/lib/libc/string/strmode.3 b/src/lib/libc/string/strmode.3
deleted file mode 100644
index 8135d30ba1..0000000000
--- a/src/lib/libc/string/strmode.3
+++ /dev/null
@@ -1,153 +0,0 @@
1.\" $OpenBSD: strmode.3,v 1.17 2017/07/05 11:44:35 tb Exp $
2.\"
3.\" Copyright (c) 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. Neither the name of the University nor the names of its contributors
15.\" may be used to endorse or promote products derived from this software
16.\" without specific prior written permission.
17.\"
18.\" THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
19.\" ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
20.\" IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
21.\" ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
22.\" FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
23.\" DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
24.\" OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
25.\" HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
26.\" LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
27.\" OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
28.\" SUCH DAMAGE.
29.\"
30.\" @(#)strmode.3 8.3 (Berkeley) 7/28/94
31.\"
32.Dd $Mdocdate: July 5 2017 $
33.Dt STRMODE 3
34.Os
35.Sh NAME
36.Nm strmode
37.Nd convert inode status information into a symbolic string
38.Sh SYNOPSIS
39.In string.h
40.Ft void
41.Fn strmode "mode_t mode" "char *bp"
42.Sh DESCRIPTION
43The
44.Fn strmode
45function converts a file
46.Fa mode
47(the type and permission information associated with an inode, see
48.Xr stat 2 )
49into a symbolic string which is stored in the location referenced by
50.Fa bp .
51This stored string is eleven characters in length plus a trailing NUL byte.
52.Pp
53The first character is the inode type, and will be one of the following:
54.Pp
55.Bl -tag -width flag -offset indent -compact
56.It \-
57regular file
58.It b
59block special
60.It c
61character special
62.It d
63directory
64.It l
65symbolic link
66.It p
67FIFO
68.It s
69socket
70.It \&?
71unknown inode type
72.El
73.Pp
74The next nine characters encode three sets of permissions, in three
75characters each.
76The first three characters are the permissions for the owner of the
77file, the second three for the group the file belongs to, and the
78third for the
79.Dq other ,
80or default, set of users.
81.Pp
82Permission checking is done as specifically as possible.
83If read permission is denied to the owner of a file in the first set
84of permissions, the owner of the file will not be able to read the file.
85This is true even if the owner is in the file's group and the group
86permissions allow reading or the
87.Dq other
88permissions allow reading.
89.Pp
90If the first character of the three character set is an
91.Sq r ,
92the file is readable for that set of users; if a dash
93.Pq Ql - ,
94it is not readable.
95.Pp
96If the second character of the three character set is a
97.Sq w ,
98the file is writable for that set of users; if a dash
99.Pq Ql - ,
100it is not writable.
101.Pp
102The third character is the first of the following characters that apply:
103.Bl -tag -width xxxx
104.It S
105If the character is part of the owner permissions and the file is not
106executable or the directory is not searchable by the owner, and the
107set-user-ID bit is set.
108.It S
109If the character is part of the group permissions and the file is not
110executable or the directory is not searchable by the group, and the
111set-group-ID bit is set.
112.It T
113If the character is part of the other permissions and the file is not
114executable or the directory is not searchable by others, and the
115.Dq sticky
116.Pq Dv S_ISVTX
117bit is set.
118.It s
119If the character is part of the owner permissions and the file is
120executable or the directory searchable by the owner, and the set-user-ID
121bit is set.
122.It s
123If the character is part of the group permissions and the file is
124executable or the directory searchable by the group, and the set-group-ID
125bit is set.
126.It t
127If the character is part of the other permissions and the file is
128executable or the directory searchable by others, and the
129.Dq sticky
130.Pq Dv S_ISVTX
131bit is set.
132.It x
133The file is executable or the directory is searchable.
134.It \-
135None of the above apply.
136.El
137.Pp
138The last character is a plus sign
139.Pq Ql +
140if there are any alternate
141or additional access control methods associated with the inode, otherwise
142it will be a space.
143.Sh SEE ALSO
144.Xr chmod 1 ,
145.Xr find 1 ,
146.Xr stat 2 ,
147.Xr getmode 3 ,
148.Xr setmode 3
149.Sh HISTORY
150The
151.Fn strmode
152function first appeared in
153.Bx 4.3 Reno .
diff --git a/src/lib/libc/string/strmode.c b/src/lib/libc/string/strmode.c
deleted file mode 100644
index c57fe74b23..0000000000
--- a/src/lib/libc/string/strmode.c
+++ /dev/null
@@ -1,139 +0,0 @@
1/* $OpenBSD: strmode.c,v 1.9 2024/06/23 07:08:26 otto Exp $ */
2/*-
3 * Copyright (c) 1990 The Regents of the University of California.
4 * 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. Neither the name of the University nor the names of its contributors
15 * may be used to endorse or promote products derived from this software
16 * without specific prior written permission.
17 *
18 * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
19 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
20 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
21 * ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
22 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
23 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
24 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
25 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
26 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
27 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
28 * SUCH DAMAGE.
29 */
30
31#include <sys/types.h>
32#include <sys/stat.h>
33#include <string.h>
34
35void
36strmode(mode_t mode, char *p)
37{
38 /* print type */
39 switch (mode & S_IFMT) {
40 case S_IFDIR: /* directory */
41 *p++ = 'd';
42 break;
43 case S_IFCHR: /* character special */
44 *p++ = 'c';
45 break;
46 case S_IFBLK: /* block special */
47 *p++ = 'b';
48 break;
49 case S_IFREG: /* regular */
50 *p++ = '-';
51 break;
52 case S_IFLNK: /* symbolic link */
53 *p++ = 'l';
54 break;
55 case S_IFSOCK: /* socket */
56 *p++ = 's';
57 break;
58#ifdef S_IFIFO
59 case S_IFIFO: /* fifo */
60 *p++ = 'p';
61 break;
62#endif
63 default: /* unknown */
64 *p++ = '?';
65 break;
66 }
67 /* usr */
68 if (mode & S_IRUSR)
69 *p++ = 'r';
70 else
71 *p++ = '-';
72 if (mode & S_IWUSR)
73 *p++ = 'w';
74 else
75 *p++ = '-';
76 switch (mode & (S_IXUSR | S_ISUID)) {
77 case 0:
78 *p++ = '-';
79 break;
80 case S_IXUSR:
81 *p++ = 'x';
82 break;
83 case S_ISUID:
84 *p++ = 'S';
85 break;
86 case S_IXUSR | S_ISUID:
87 *p++ = 's';
88 break;
89 }
90 /* group */
91 if (mode & S_IRGRP)
92 *p++ = 'r';
93 else
94 *p++ = '-';
95 if (mode & S_IWGRP)
96 *p++ = 'w';
97 else
98 *p++ = '-';
99 switch (mode & (S_IXGRP | S_ISGID)) {
100 case 0:
101 *p++ = '-';
102 break;
103 case S_IXGRP:
104 *p++ = 'x';
105 break;
106 case S_ISGID:
107 *p++ = 'S';
108 break;
109 case S_IXGRP | S_ISGID:
110 *p++ = 's';
111 break;
112 }
113 /* other */
114 if (mode & S_IROTH)
115 *p++ = 'r';
116 else
117 *p++ = '-';
118 if (mode & S_IWOTH)
119 *p++ = 'w';
120 else
121 *p++ = '-';
122 switch (mode & (S_IXOTH | S_ISVTX)) {
123 case 0:
124 *p++ = '-';
125 break;
126 case S_IXOTH:
127 *p++ = 'x';
128 break;
129 case S_ISVTX:
130 *p++ = 'T';
131 break;
132 case S_IXOTH | S_ISVTX:
133 *p++ = 't';
134 break;
135 }
136 *p++ = ' '; /* will be a '+' if ACL's implemented */
137 *p = '\0';
138}
139DEF_WEAK(strmode);
diff --git a/src/lib/libc/string/strncat.3 b/src/lib/libc/string/strncat.3
deleted file mode 100644
index d314a9999a..0000000000
--- a/src/lib/libc/string/strncat.3
+++ /dev/null
@@ -1,132 +0,0 @@
1.\" $OpenBSD: strncat.3,v 1.4 2014/04/19 16:50:46 jmc Exp $
2.\"
3.\" Copyright (c) 1990, 1991 The Regents of the University of California.
4.\" All rights reserved.
5.\"
6.\" This code is derived from software contributed to Berkeley by
7.\" Chris Torek and the American National Standards Committee X3,
8.\" on Information Processing Systems.
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. 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.Dd $Mdocdate: April 19 2014 $
35.Dt STRNCAT 3
36.Os
37.Sh NAME
38.Nm strncat
39.Nd concatenate a string with part of another
40.Sh SYNOPSIS
41.In string.h
42.Ft char *
43.Fn strncat "char *dst" "const char *append" "size_t count"
44.Sh DESCRIPTION
45The
46.Fn strncat
47function appends not more than
48.Fa count
49characters of the string
50.Fa append
51to the end of the string found in the buffer
52.Fa dst .
53Space for the terminating
54.Ql \e0
55should not be included in
56.Fa count .
57.Pp
58Bounds checking must be performed manually with great care.
59If the buffer
60.Fa dst
61is not large enough to hold the result,
62subsequent memory will be damaged.
63.Sh RETURN VALUES
64The
65.Fn strncat
66function returns the pointer
67.Fa dst .
68.Sh EXAMPLES
69The following example shows how to use
70.Fn strncat
71in conjunction with
72.Xr strncpy 3 :
73.Bd -literal -offset indent
74char buf[BUFSIZ];
75char *base, *suffix;
76
77(void)strncpy(buf, base, sizeof(buf) - 1);
78buf[sizeof(buf) - 1] = '\e0';
79(void)strncat(buf, suffix, sizeof(buf) - 1 - strlen(buf));
80.Ed
81.Pp
82The above will copy as many characters from
83.Va base
84to
85.Va buf
86as will fit.
87It then appends as many characters from
88.Va suffix
89as will fit.
90If either
91.Va base
92or
93.Va suffix
94are too large, truncation will occur without detection.
95.Pp
96The above example shows dangerous coding patterns, including an
97inability to detect truncation.
98.Fn strncat
99and
100.Fn strncpy
101are dangerously easy to misuse.
102The
103.Xr strlcpy 3
104and
105.Xr strlcat 3
106functions are safer for this kind of operation:
107.Bd -literal -offset indent
108if (strlcpy(buf, base, sizeof(buf)) >= sizeof(buf) ||
109 strlcat(buf, suffix, sizeof(buf)) >= sizeof(buf))
110 goto toolong;
111
112.Ed
113or for greatest portability,
114.Bd -literal -offset indent
115if (snprintf(buf, sizeof(buf), "%s%s",
116 base, suffix) >= sizeof(buf))
117 goto toolong;
118.Ed
119.Sh SEE ALSO
120.Xr strlcpy 3 ,
121.Xr wcscat 3 ,
122.Xr wcslcpy 3
123.Sh STANDARDS
124The
125.Fn strncat
126function conforms to
127.St -ansiC .
128.Sh HISTORY
129The
130.Fn strncat
131function first appeared in
132.At v7 .
diff --git a/src/lib/libc/string/strncat.c b/src/lib/libc/string/strncat.c
deleted file mode 100644
index b3388accf3..0000000000
--- a/src/lib/libc/string/strncat.c
+++ /dev/null
@@ -1,58 +0,0 @@
1/* $OpenBSD: strncat.c,v 1.6 2015/08/31 02:53:57 guenther Exp $ */
2/*-
3 * Copyright (c) 1990 The Regents of the University of California.
4 * All rights reserved.
5 *
6 * This code is derived from software contributed to Berkeley by
7 * Chris Torek.
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. 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#include <string.h>
35
36/*
37 * Concatenate src on the end of dst. At most strlen(dst)+n+1 bytes
38 * are written at dst (at most n+1 bytes being appended). Return dst.
39 */
40char *
41strncat(char *dst, const char *src, size_t n)
42{
43 if (n != 0) {
44 char *d = dst;
45 const char *s = src;
46
47 while (*d != 0)
48 d++;
49 do {
50 if ((*d = *s++) == 0)
51 break;
52 d++;
53 } while (--n != 0);
54 *d = 0;
55 }
56 return (dst);
57}
58DEF_STRONG(strncat);
diff --git a/src/lib/libc/string/strncmp.c b/src/lib/libc/string/strncmp.c
deleted file mode 100644
index 535d2a60fd..0000000000
--- a/src/lib/libc/string/strncmp.c
+++ /dev/null
@@ -1,48 +0,0 @@
1/* $OpenBSD: strncmp.c,v 1.9 2015/08/31 02:53:57 guenther Exp $ */
2
3/*
4 * Copyright (c) 1989 The Regents of the University of California.
5 * 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. Neither the name of the University nor the names of its contributors
16 * may be used to endorse or promote products derived from this software
17 * without specific prior written permission.
18 *
19 * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
20 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
21 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
22 * ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
23 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
24 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
25 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
26 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
27 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
28 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
29 * SUCH DAMAGE.
30 */
31
32#include <string.h>
33
34int
35strncmp(const char *s1, const char *s2, size_t n)
36{
37
38 if (n == 0)
39 return (0);
40 do {
41 if (*s1 != *s2++)
42 return (*(unsigned char *)s1 - *(unsigned char *)--s2);
43 if (*s1++ == 0)
44 break;
45 } while (--n != 0);
46 return (0);
47}
48DEF_STRONG(strncmp);
diff --git a/src/lib/libc/string/strncpy.3 b/src/lib/libc/string/strncpy.3
deleted file mode 100644
index 3a68a0bd5b..0000000000
--- a/src/lib/libc/string/strncpy.3
+++ /dev/null
@@ -1,138 +0,0 @@
1.\" $OpenBSD: strncpy.3,v 1.2 2014/04/19 11:30:40 deraadt Exp $
2.\"
3.\" Copyright (c) 1990, 1991 The Regents of the University of California.
4.\" All rights reserved.
5.\"
6.\" This code is derived from software contributed to Berkeley by
7.\" Chris Torek and the American National Standards Committee X3,
8.\" on Information Processing Systems.
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. 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.Dd $Mdocdate: April 19 2014 $
35.Dt STRNCPY 3
36.Os
37.Sh NAME
38.Nm strncpy
39.Nd copy part of a string to another
40.Sh SYNOPSIS
41.In string.h
42.Ft char *
43.Fn strncpy "char *dst" "const char *src" "size_t len"
44.Sh DESCRIPTION
45The
46.Fn strncpy
47function copies not more than
48.Fa len
49characters from the string
50.Fa src
51to the buffer
52.Fa dst .
53If
54.Fa src
55is less than
56.Fa len
57characters long,
58it fills the remaining buffer with
59.Ql \e0
60characters.
61If the length of
62.Fa src
63is greater than or equal to
64.Fa len ,
65.Fa dst
66will
67.Em not
68be NUL-terminated.
69.Pp
70.Fn strncpy
71.Em only
72NUL terminates the destination string when the length of the source
73string is less than the length parameter.
74.Pp
75If the
76.Fa src
77and
78.Fa dst
79strings overlap, the behavior is undefined.
80.Sh RETURN VALUES
81The
82.Fn strncpy
83function returns
84.Fa dst .
85.Sh EXAMPLES
86The following sets
87.Va chararray
88to
89.Dq abc\e0\e0\e0 :
90.Bd -literal -offset indent
91(void)strncpy(chararray, "abc", 6);
92.Ed
93.Pp
94The following sets
95.Va chararray
96to
97.Dq abcdef ,
98without a NUL-terminator:
99.Bd -literal -offset indent
100(void)strncpy(chararray, "abcdefgh", 6);
101.Ed
102.Pp
103The following sequence copies as many characters from
104.Va input
105to
106.Va buf
107as will fit, and then NUL terminates the result by hand:
108.Bd -literal -offset indent
109char buf[BUFSIZ];
110
111(void)strncpy(buf, input, sizeof(buf) - 1);
112buf[sizeof(buf) - 1] = '\e0';
113.Ed
114.Pp
115By now it is clear that
116.Nm strncpy
117is dangerously easy to misuse.
118The
119.Xr strlcpy 3
120function is safer for this kind of operation:
121.Bd -literal -offset indent
122if (strlcpy(buf, input, sizeof(buf)) >= sizeof(buf))
123 goto toolong;
124.Ed
125.Sh SEE ALSO
126.Xr strlcpy 3 ,
127.Xr wcscpy 3 ,
128.Xr wcslcpy 3
129.Sh STANDARDS
130The
131.Fn strncpy
132function conforms to
133.St -ansiC .
134.Sh HISTORY
135The
136.Fn strncpy
137function first appeared in
138.At v7 .
diff --git a/src/lib/libc/string/strncpy.c b/src/lib/libc/string/strncpy.c
deleted file mode 100644
index d6d8647fc7..0000000000
--- a/src/lib/libc/string/strncpy.c
+++ /dev/null
@@ -1,59 +0,0 @@
1/* $OpenBSD: strncpy.c,v 1.8 2015/08/31 02:53:57 guenther Exp $ */
2
3/*-
4 * Copyright (c) 1990 The Regents of the University of California.
5 * All rights reserved.
6 *
7 * This code is derived from software contributed to Berkeley by
8 * Chris Torek.
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. 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
35#include <string.h>
36
37/*
38 * Copy src to dst, truncating or null-padding to always copy n bytes.
39 * Return dst.
40 */
41char *
42strncpy(char *dst, const char *src, size_t n)
43{
44 if (n != 0) {
45 char *d = dst;
46 const char *s = src;
47
48 do {
49 if ((*d++ = *s++) == 0) {
50 /* NUL pad the remaining n-1 bytes */
51 while (--n != 0)
52 *d++ = 0;
53 break;
54 }
55 } while (--n != 0);
56 }
57 return (dst);
58}
59DEF_STRONG(strncpy);
diff --git a/src/lib/libc/string/strndup.c b/src/lib/libc/string/strndup.c
deleted file mode 100644
index 499f9a028a..0000000000
--- a/src/lib/libc/string/strndup.c
+++ /dev/null
@@ -1,40 +0,0 @@
1/* $OpenBSD: strndup.c,v 1.3 2019/01/25 00:19:25 millert Exp $ */
2
3/*
4 * Copyright (c) 2010 Todd C. Miller <millert@openbsd.org>
5 *
6 * Permission to use, copy, modify, and distribute this software for any
7 * purpose with or without fee is hereby granted, provided that the above
8 * copyright notice and this permission notice appear in all copies.
9 *
10 * THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES
11 * WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF
12 * MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR
13 * ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES
14 * WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN
15 * ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF
16 * OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE.
17 */
18
19#include <sys/types.h>
20
21#include <stddef.h>
22#include <stdlib.h>
23#include <string.h>
24
25char *
26strndup(const char *str, size_t maxlen)
27{
28 char *copy;
29 size_t len;
30
31 len = strnlen(str, maxlen);
32 copy = malloc(len + 1);
33 if (copy != NULL) {
34 (void)memcpy(copy, str, len);
35 copy[len] = '\0';
36 }
37
38 return copy;
39}
40DEF_WEAK(strndup);
diff --git a/src/lib/libc/string/strnlen.c b/src/lib/libc/string/strnlen.c
deleted file mode 100644
index d50a927f28..0000000000
--- a/src/lib/libc/string/strnlen.c
+++ /dev/null
@@ -1,33 +0,0 @@
1/* $OpenBSD: strnlen.c,v 1.9 2019/01/25 00:19:25 millert Exp $ */
2
3/*
4 * Copyright (c) 2010 Todd C. Miller <millert@openbsd.org>
5 *
6 * Permission to use, copy, modify, and distribute this software for any
7 * purpose with or without fee is hereby granted, provided that the above
8 * copyright notice and this permission notice appear in all copies.
9 *
10 * THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES
11 * WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF
12 * MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR
13 * ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES
14 * WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN
15 * ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF
16 * OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE.
17 */
18
19#include <sys/types.h>
20
21#include <string.h>
22
23size_t
24strnlen(const char *str, size_t maxlen)
25{
26 const char *cp;
27
28 for (cp = str; maxlen != 0 && *cp != '\0'; cp++, maxlen--)
29 ;
30
31 return (size_t)(cp - str);
32}
33DEF_WEAK(strnlen);
diff --git a/src/lib/libc/string/strpbrk.3 b/src/lib/libc/string/strpbrk.3
deleted file mode 100644
index 51d2edf5b8..0000000000
--- a/src/lib/libc/string/strpbrk.3
+++ /dev/null
@@ -1,80 +0,0 @@
1.\" $OpenBSD: strpbrk.3,v 1.11 2024/12/11 23:28:20 jsg Exp $
2.\"
3.\" Copyright (c) 1990, 1991 The Regents of the University of California.
4.\" All rights reserved.
5.\"
6.\" This code is derived from software contributed to Berkeley by
7.\" Chris Torek and the American National Standards Committee X3,
8.\" on Information Processing Systems.
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. 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.Dd $Mdocdate: December 11 2024 $
35.Dt STRPBRK 3
36.Os
37.Sh NAME
38.Nm strpbrk
39.Nd locate multiple characters in string
40.Sh SYNOPSIS
41.In string.h
42.Ft char *
43.Fn strpbrk "const char *s" "const char *charset"
44.Sh DESCRIPTION
45The
46.Fn strpbrk
47function locates in the NUL-terminated string
48.Fa s
49the first occurrence of any character in the string
50.Fa charset
51and returns a pointer to this character.
52If no characters from
53.Fa charset
54occur anywhere in
55.Fa s ,
56.Fn strpbrk
57returns
58.Dv NULL .
59.Sh SEE ALSO
60.Xr memchr 3 ,
61.Xr strchr 3 ,
62.Xr strcspn 3 ,
63.Xr strrchr 3 ,
64.Xr strsep 3 ,
65.Xr strspn 3 ,
66.Xr strstr 3 ,
67.Xr strtok 3 ,
68.Xr wcspbrk 3
69.Sh STANDARDS
70The
71.Fn strpbrk
72function conforms to
73.St -ansiC .
74.Sh HISTORY
75The
76.Fn strpbrk
77function first appeared in
78.At III
79and was reimplemented for
80.Bx 4.3 .
diff --git a/src/lib/libc/string/strpbrk.c b/src/lib/libc/string/strpbrk.c
deleted file mode 100644
index 336c22dedd..0000000000
--- a/src/lib/libc/string/strpbrk.c
+++ /dev/null
@@ -1,49 +0,0 @@
1/* $OpenBSD: strpbrk.c,v 1.6 2015/08/31 02:53:57 guenther Exp $ */
2/*
3 * Copyright (c) 1985 Regents of the University of California.
4 * 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. Neither the name of the University nor the names of its contributors
15 * may be used to endorse or promote products derived from this software
16 * without specific prior written permission.
17 *
18 * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
19 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
20 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
21 * ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
22 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
23 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
24 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
25 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
26 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
27 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
28 * SUCH DAMAGE.
29 */
30
31#include <string.h>
32
33/*
34 * Find the first occurrence in s1 of a character in s2 (excluding NUL).
35 */
36char *
37strpbrk(const char *s1, const char *s2)
38{
39 const char *scanp;
40 int c, sc;
41
42 while ((c = *s1++) != 0) {
43 for (scanp = s2; (sc = *scanp++) != 0;)
44 if (sc == c)
45 return ((char *)(s1 - 1));
46 }
47 return (NULL);
48}
49DEF_STRONG(strpbrk);
diff --git a/src/lib/libc/string/strrchr.3 b/src/lib/libc/string/strrchr.3
deleted file mode 100644
index 231260f7f3..0000000000
--- a/src/lib/libc/string/strrchr.3
+++ /dev/null
@@ -1,118 +0,0 @@
1.\" $OpenBSD: strrchr.3,v 1.13 2024/12/11 23:28:20 jsg Exp $
2.\"
3.\" Copyright (c) 1990, 1991 The Regents of the University of California.
4.\" All rights reserved.
5.\"
6.\" This code is derived from software contributed to Berkeley by
7.\" Chris Torek and the American National Standards Committee X3,
8.\" on Information Processing Systems.
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. 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.Dd $Mdocdate: December 11 2024 $
35.Dt STRRCHR 3
36.Os
37.Sh NAME
38.Nm strrchr ,
39.Nm rindex
40.Nd locate last occurrence of a character in a string
41.Sh SYNOPSIS
42.In string.h
43.Ft char *
44.Fn strrchr "const char *s" "int c"
45.In strings.h
46.Ft char *
47.Fn rindex "const char *s" "int c"
48.Sh DESCRIPTION
49The
50.Fn strrchr
51function locates the last occurrence of the character
52.Fa c
53.Pq converted to a char
54in the string
55.Fa s .
56The terminating
57.Tn NUL
58character is considered part of the string.
59If
60.Fa c
61is
62.Ql \e0 ,
63.Fn strrchr
64locates the terminating
65.Ql \e0 .
66.Pp
67The
68.Fn rindex
69function is an old synonym for
70.Fn strrchr .
71.Sh RETURN VALUES
72The
73.Fn strrchr
74function returns a pointer to the located character or
75.Dv NULL
76if the character does not appear in the string.
77.Sh EXAMPLES
78After the following call to
79.Fn strrchr ,
80.Va p
81will point to the string
82.Qq obar :
83.Bd -literal -offset indent
84char *p;
85char *s = "foobar";
86
87p = strrchr(s, 'o');
88.Ed
89.Sh SEE ALSO
90.Xr memchr 3 ,
91.Xr strchr 3 ,
92.Xr strcspn 3 ,
93.Xr strpbrk 3 ,
94.Xr strsep 3 ,
95.Xr strspn 3 ,
96.Xr strstr 3 ,
97.Xr strtok 3 ,
98.Xr wcsrchr 3
99.Sh STANDARDS
100The
101.Fn strrchr
102function conforms to
103.St -ansiC .
104.Pp
105The
106.Fn rindex
107function is deprecated and shouldn't be used in new code.
108.Sh HISTORY
109The
110.Fn rindex
111function first appeared in
112.At v7 .
113The
114.Fn strrchr
115function first appeared in
116.At III
117and was reimplemented for
118.Bx 4.3 .
diff --git a/src/lib/libc/string/strrchr.c b/src/lib/libc/string/strrchr.c
deleted file mode 100644
index 848d1ad22e..0000000000
--- a/src/lib/libc/string/strrchr.c
+++ /dev/null
@@ -1,48 +0,0 @@
1/* $OpenBSD: strrchr.c,v 1.4 2018/10/01 06:37:37 martijn Exp $ */
2/*
3 * Copyright (c) 1988 Regents of the University of California.
4 * 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. Neither the name of the University nor the names of its contributors
15 * may be used to endorse or promote products derived from this software
16 * without specific prior written permission.
17 *
18 * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
19 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
20 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
21 * ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
22 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
23 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
24 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
25 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
26 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
27 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
28 * SUCH DAMAGE.
29 */
30
31#include <string.h>
32
33__weak_alias(rindex, strrchr);
34
35char *
36strrchr(const char *p, int ch)
37{
38 char *save;
39
40 for (save = NULL;; ++p) {
41 if (*p == (char) ch)
42 save = (char *)p;
43 if (!*p)
44 return(save);
45 }
46 /* NOTREACHED */
47}
48DEF_STRONG(strrchr);
diff --git a/src/lib/libc/string/strsep.3 b/src/lib/libc/string/strsep.3
deleted file mode 100644
index 77053f66d7..0000000000
--- a/src/lib/libc/string/strsep.3
+++ /dev/null
@@ -1,110 +0,0 @@
1.\" $OpenBSD: strsep.3,v 1.14 2013/06/05 03:39:23 tedu Exp $
2.\"
3.\" Copyright (c) 1990, 1991, 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.\" Chris Torek.
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. 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.\" @(#)strsep.3 8.1 (Berkeley) 6/9/93
34.\"
35.Dd $Mdocdate: June 5 2013 $
36.Dt STRSEP 3
37.Os
38.Sh NAME
39.Nm strsep
40.Nd separate strings
41.Sh SYNOPSIS
42.In string.h
43.Ft char *
44.Fn strsep "char **stringp" "const char *delim"
45.Sh DESCRIPTION
46The
47.Fn strsep
48function locates, in the string referenced by
49.Fa *stringp ,
50the first occurrence of any character in the string
51.Fa delim
52(or the terminating
53.Ql \e0
54character) and replaces it with a
55.Ql \e0 .
56The location of the next character after the delimiter character
57(or
58.Dv NULL ,
59if the end of the string was reached) is stored in
60.Fa *stringp .
61The original value of
62.Fa *stringp
63is returned.
64.Pp
65An
66.Dq empty
67field, i.e., one caused by two adjacent delimiter characters,
68can be detected by comparing the location referenced by the pointer returned
69by
70.Fn strsep
71to
72.Ql \e0 .
73.Pp
74If
75.Fa *stringp
76is initially
77.Dv NULL ,
78.Fn strsep
79returns
80.Dv NULL .
81.Sh EXAMPLES
82The following uses
83.Fn strsep
84to parse a string, containing tokens delimited by whitespace, into an
85argument vector:
86.Bd -literal -offset indent
87char **ap, *argv[10], *inputstring;
88
89for (ap = argv; ap < &argv[9] &&
90 (*ap = strsep(&inputstring, " \et")) != NULL;) {
91 if (**ap != '\e0')
92 ap++;
93}
94*ap = NULL;
95.Ed
96.Sh HISTORY
97The
98.Fn strsep
99function first appeared in
100.Bx 4.3 Reno .
101It is intended as a replacement for the
102.Xr strtok 3
103function.
104While the
105.Xr strtok 3
106function should be preferred for portability reasons (it conforms to
107.St -ansiC )
108it is unable to handle empty fields, i.e., detect fields delimited by
109two adjacent delimiter characters, or to be used for more than a single
110string at a time.
diff --git a/src/lib/libc/string/strsep.c b/src/lib/libc/string/strsep.c
deleted file mode 100644
index 97c3cbf1f5..0000000000
--- a/src/lib/libc/string/strsep.c
+++ /dev/null
@@ -1,71 +0,0 @@
1/* $OpenBSD: strsep.c,v 1.8 2015/08/31 02:53:57 guenther 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. Neither the name of the University nor the names of its contributors
16 * may be used to endorse or promote products derived from this software
17 * without specific prior written permission.
18 *
19 * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
20 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
21 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
22 * ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
23 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
24 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
25 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
26 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
27 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
28 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
29 * SUCH DAMAGE.
30 */
31
32#include <string.h>
33
34/*
35 * Get next token from string *stringp, where tokens are possibly-empty
36 * strings separated by characters from delim.
37 *
38 * Writes NULs into the string at *stringp to end tokens.
39 * delim need not remain constant from call to call.
40 * On return, *stringp points past the last NUL written (if there might
41 * be further tokens), or is NULL (if there are definitely no more tokens).
42 *
43 * If *stringp is NULL, strsep returns NULL.
44 */
45char *
46strsep(char **stringp, const char *delim)
47{
48 char *s;
49 const char *spanp;
50 int c, sc;
51 char *tok;
52
53 if ((s = *stringp) == NULL)
54 return (NULL);
55 for (tok = s;;) {
56 c = *s++;
57 spanp = delim;
58 do {
59 if ((sc = *spanp++) == c) {
60 if (c == 0)
61 s = NULL;
62 else
63 s[-1] = 0;
64 *stringp = s;
65 return (tok);
66 }
67 } while (sc != 0);
68 }
69 /* NOTREACHED */
70}
71DEF_WEAK(strsep);
diff --git a/src/lib/libc/string/strsignal.3 b/src/lib/libc/string/strsignal.3
deleted file mode 100644
index b46be893b5..0000000000
--- a/src/lib/libc/string/strsignal.3
+++ /dev/null
@@ -1,78 +0,0 @@
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. 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.\" $OpenBSD: strsignal.3,v 1.9 2019/05/16 13:35:16 schwarze Exp $
33.\"
34.Dd $Mdocdate: May 16 2019 $
35.Dt STRSIGNAL 3
36.Os
37.Sh NAME
38.Nm strsignal
39.Nd get signal description string
40.Sh SYNOPSIS
41.In string.h
42.Ft char *
43.Fn strsignal "int sig"
44.Sh DESCRIPTION
45The
46.Fn strsignal
47function returns a pointer to the string describing the signal
48.Fa sig .
49.Pp
50The array pointed to is not to be modified by the program, but may be
51overwritten by subsequent calls to
52.Fn strsignal .
53.Sh SEE ALSO
54.Xr intro 2 ,
55.Xr psignal 3 ,
56.Xr setlocale 3
57.Sh STANDARDS
58The
59.Fn strsignal
60function conforms to
61.St -p1003.1-2008 .
62.Sh HISTORY
63The
64.Fn strsignal
65function first appeared in
66.At V.4
67and was reimplemented for
68.Nx 1.0 .
69.Sh CAVEATS
70On systems other than
71.Ox ,
72the
73.Dv LC_MESSAGES
74.Xr locale 1
75category can cause different strings to be returned instead of the
76normal signal descriptions; see CAVEATS in
77.Xr setlocale 3
78for details.
diff --git a/src/lib/libc/string/strsignal.c b/src/lib/libc/string/strsignal.c
deleted file mode 100644
index fe2a747943..0000000000
--- a/src/lib/libc/string/strsignal.c
+++ /dev/null
@@ -1,40 +0,0 @@
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. Neither the name of the University nor the names of its contributors
14 * may be used to endorse or promote products derived from this software
15 * without specific prior written permission.
16 *
17 * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
18 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
19 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
20 * ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
21 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
22 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
23 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
24 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
25 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
26 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
27 * SUCH DAMAGE.
28 */
29
30#include <string.h>
31#include <limits.h>
32
33char *
34strsignal(int sig)
35{
36 static char buf[NL_TEXTMAX];
37
38 return __strsignal(sig, buf);
39}
40DEF_WEAK(strsignal);
diff --git a/src/lib/libc/string/strspn.3 b/src/lib/libc/string/strspn.3
deleted file mode 100644
index e677ba15a1..0000000000
--- a/src/lib/libc/string/strspn.3
+++ /dev/null
@@ -1,92 +0,0 @@
1.\" $OpenBSD: strspn.3,v 1.12 2024/12/11 23:28:20 jsg Exp $
2.\"
3.\" Copyright (c) 1990, 1991 The Regents of the University of California.
4.\" All rights reserved.
5.\"
6.\" This code is derived from software contributed to Berkeley by
7.\" Chris Torek and the American National Standards Committee X3,
8.\" on Information Processing Systems.
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. 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.Dd $Mdocdate: December 11 2024 $
35.Dt STRSPN 3
36.Os
37.Sh NAME
38.Nm strspn
39.Nd span a string
40.Sh SYNOPSIS
41.In string.h
42.Ft size_t
43.Fn strspn "const char *s" "const char *charset"
44.Sh DESCRIPTION
45The
46.Fn strspn
47function spans the initial part of the NUL-terminated string
48.Fa s
49as long as the characters from
50.Fa s
51occur in string
52.Fa charset .
53.Sh RETURN VALUES
54The
55.Fn strspn
56function returns the number of characters spanned.
57.Sh EXAMPLES
58The following call to
59.Fn strspn
60will return 3, since the first three characters of string
61.Fa s
62are part of string
63.Fa charset :
64.Bd -literal -offset indent
65char *s = "foobar";
66char *charset = "of";
67size_t span;
68
69span = strspn(s, charset);
70.Ed
71.Sh SEE ALSO
72.Xr memchr 3 ,
73.Xr strchr 3 ,
74.Xr strcspn 3 ,
75.Xr strpbrk 3 ,
76.Xr strrchr 3 ,
77.Xr strsep 3 ,
78.Xr strstr 3 ,
79.Xr strtok 3 ,
80.Xr wcsspn 3
81.Sh STANDARDS
82The
83.Fn strspn
84function conforms to
85.St -ansiC .
86.Sh HISTORY
87The
88.Fn strspn
89function first appeared in
90.At III
91and was reimplemented for
92.Bx 4.3 .
diff --git a/src/lib/libc/string/strspn.c b/src/lib/libc/string/strspn.c
deleted file mode 100644
index 0ce41cbb49..0000000000
--- a/src/lib/libc/string/strspn.c
+++ /dev/null
@@ -1,52 +0,0 @@
1/* $OpenBSD: strspn.c,v 1.6 2015/08/31 02:53:57 guenther Exp $ */
2/*
3 * Copyright (c) 1989 The Regents of the University of California.
4 * 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. Neither the name of the University nor the names of its contributors
15 * may be used to endorse or promote products derived from this software
16 * without specific prior written permission.
17 *
18 * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
19 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
20 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
21 * ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
22 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
23 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
24 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
25 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
26 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
27 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
28 * SUCH DAMAGE.
29 */
30
31#include <string.h>
32
33/*
34 * Span the string s2 (skip characters that are in s2).
35 */
36size_t
37strspn(const char *s1, const char *s2)
38{
39 const char *p = s1, *spanp;
40 char c, sc;
41
42 /*
43 * Skip any characters in s2, excluding the terminating \0.
44 */
45cont:
46 c = *p++;
47 for (spanp = s2; (sc = *spanp++) != 0;)
48 if (sc == c)
49 goto cont;
50 return (p - 1 - s1);
51}
52DEF_STRONG(strspn);
diff --git a/src/lib/libc/string/strstr.3 b/src/lib/libc/string/strstr.3
deleted file mode 100644
index 60d2a721a7..0000000000
--- a/src/lib/libc/string/strstr.3
+++ /dev/null
@@ -1,101 +0,0 @@
1.\" $OpenBSD: strstr.3,v 1.13 2016/05/11 17:51:50 schwarze Exp $
2.\"
3.\" Copyright (c) 1990, 1991 The Regents of the University of California.
4.\" All rights reserved.
5.\"
6.\" This code is derived from software contributed to Berkeley by
7.\" Chris Torek and the American National Standards Committee X3,
8.\" on Information Processing Systems.
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. 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.Dd $Mdocdate: May 11 2016 $
35.Dt STRSTR 3
36.Os
37.Sh NAME
38.Nm strstr , strcasestr
39.Nd locate a substring in a string
40.Sh SYNOPSIS
41.In string.h
42.Ft char *
43.Fn strstr "const char *big" "const char *little"
44.Ft char *
45.Fn strcasestr "const char *big" "const char *little"
46.Sh DESCRIPTION
47The
48.Fn strstr
49function locates the first occurrence of the NUL-terminated string
50.Fa little
51in the NUL-terminated string
52.Fa big .
53.Pp
54The
55.Fn strcasestr
56function is similar to
57.Fn strstr
58but ignores the case of both strings.
59.Sh RETURN VALUES
60If
61.Fa little
62is an empty string,
63.Fa big
64is returned;
65if
66.Fa little
67occurs nowhere in
68.Fa big ,
69.Dv NULL
70is returned;
71otherwise a pointer to the first character of the first occurrence of
72.Fa little
73is returned.
74.Sh SEE ALSO
75.Xr memchr 3 ,
76.Xr memmem 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.Xr wcsstr 3
85.Sh STANDARDS
86The
87.Fn strstr
88function conforms to
89.St -ansiC .
90.Sh HISTORY
91The
92.Fn strstr
93function first appeared in
94.Bx 4.3 Reno .
95The
96.Fn strcasestr
97function appeared in glibc 2.1,
98was reimplemented for
99.Fx 4.5
100and ported to
101.Ox 3.8 .
diff --git a/src/lib/libc/string/strstr.c b/src/lib/libc/string/strstr.c
deleted file mode 100644
index 241a080e7a..0000000000
--- a/src/lib/libc/string/strstr.c
+++ /dev/null
@@ -1,189 +0,0 @@
1/* $OpenBSD: strstr.c,v 1.9 2020/04/16 12:37:52 claudio Exp $ */
2
3/*
4 * Copyright (c) 2005-2018 Rich Felker
5 *
6 * Permission is hereby granted, free of charge, to any person obtaining
7 * a copy of this software and associated documentation files (the
8 * "Software"), to deal in the Software without restriction, including
9 * without limitation the rights to use, copy, modify, merge, publish,
10 * distribute, sublicense, and/or sell copies of the Software, and to
11 * permit persons to whom the Software is furnished to do so, subject to
12 * the following conditions:
13 *
14 * The above copyright notice and this permission notice shall be
15 * included in all copies or substantial portions of the Software.
16 *
17 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
18 * EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
19 * MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT.
20 * IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY
21 * CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT,
22 * TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE
23 * SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
24 */
25
26#include <string.h>
27#include <stdint.h>
28
29static char *
30twobyte_strstr(const unsigned char *h, const unsigned char *n)
31{
32 uint16_t nw = n[0]<<8 | n[1], hw = h[0]<<8 | h[1];
33 for (h++; *h && hw != nw; hw = hw<<8 | *++h);
34 return *h ? (char *)h-1 : 0;
35}
36
37static char *
38threebyte_strstr(const unsigned char *h, const unsigned char *n)
39{
40 uint32_t nw = n[0]<<24 | n[1]<<16 | n[2]<<8;
41 uint32_t hw = h[0]<<24 | h[1]<<16 | h[2]<<8;
42 for (h+=2; *h && hw != nw; hw = (hw|*++h)<<8);
43 return *h ? (char *)h-2 : 0;
44}
45
46static char *
47fourbyte_strstr(const unsigned char *h, const unsigned char *n)
48{
49 uint32_t nw = n[0]<<24 | n[1]<<16 | n[2]<<8 | n[3];
50 uint32_t hw = h[0]<<24 | h[1]<<16 | h[2]<<8 | h[3];
51 for (h+=3; *h && hw != nw; hw = hw<<8 | *++h);
52 return *h ? (char *)h-3 : 0;
53}
54
55#define MAX(a,b) ((a)>(b)?(a):(b))
56#define MIN(a,b) ((a)<(b)?(a):(b))
57
58#define BITOP(a,b,op) \
59 ((a)[(size_t)(b)/(8*sizeof *(a))] op (size_t)1<<((size_t)(b)%(8*sizeof *(a))))
60
61/*
62 * Maxime Crochemore and Dominique Perrin, Two-way string-matching,
63 * Journal of the ACM, 38(3):651-675, July 1991.
64 */
65static char *
66twoway_strstr(const unsigned char *h, const unsigned char *n)
67{
68 const unsigned char *z;
69 size_t l, ip, jp, k, p, ms, p0, mem, mem0;
70 size_t byteset[32 / sizeof(size_t)] = { 0 };
71 size_t shift[256];
72
73 /* Computing length of needle and fill shift table */
74 for (l=0; n[l] && h[l]; l++)
75 BITOP(byteset, n[l], |=), shift[n[l]] = l+1;
76 if (n[l]) return 0; /* hit the end of h */
77
78 /* Compute maximal suffix */
79 ip = -1; jp = 0; k = p = 1;
80 while (jp+k<l) {
81 if (n[ip+k] == n[jp+k]) {
82 if (k == p) {
83 jp += p;
84 k = 1;
85 } else k++;
86 } else if (n[ip+k] > n[jp+k]) {
87 jp += k;
88 k = 1;
89 p = jp - ip;
90 } else {
91 ip = jp++;
92 k = p = 1;
93 }
94 }
95 ms = ip;
96 p0 = p;
97
98 /* And with the opposite comparison */
99 ip = -1; jp = 0; k = p = 1;
100 while (jp+k<l) {
101 if (n[ip+k] == n[jp+k]) {
102 if (k == p) {
103 jp += p;
104 k = 1;
105 } else k++;
106 } else if (n[ip+k] < n[jp+k]) {
107 jp += k;
108 k = 1;
109 p = jp - ip;
110 } else {
111 ip = jp++;
112 k = p = 1;
113 }
114 }
115 if (ip+1 > ms+1) ms = ip;
116 else p = p0;
117
118 /* Periodic needle? */
119 if (memcmp(n, n+p, ms+1)) {
120 mem0 = 0;
121 p = MAX(ms, l-ms-1) + 1;
122 } else mem0 = l-p;
123 mem = 0;
124
125 /* Initialize incremental end-of-haystack pointer */
126 z = h;
127
128 /* Search loop */
129 for (;;) {
130 /* Update incremental end-of-haystack pointer */
131 if (z-h < l) {
132 /* Fast estimate for MIN(l,63) */
133 size_t grow = l | 63;
134 const unsigned char *z2 = memchr(z, 0, grow);
135 if (z2) {
136 z = z2;
137 if (z-h < l) return 0;
138 } else z += grow;
139 }
140
141 /* Check last byte first; advance by shift on mismatch */
142 if (BITOP(byteset, h[l-1], &)) {
143 k = l-shift[h[l-1]];
144 if (k) {
145 if (k < mem) k = mem;
146 h += k;
147 mem = 0;
148 continue;
149 }
150 } else {
151 h += l;
152 mem = 0;
153 continue;
154 }
155
156 /* Compare right half */
157 for (k=MAX(ms+1,mem); n[k] && n[k] == h[k]; k++);
158 if (n[k]) {
159 h += k-ms;
160 mem = 0;
161 continue;
162 }
163 /* Compare left half */
164 for (k=ms+1; k>mem && n[k-1] == h[k-1]; k--);
165 if (k <= mem) return (char *)h;
166 h += p;
167 mem = mem0;
168 }
169}
170
171char *
172strstr(const char *h, const char *n)
173{
174 /* Return immediately on empty needle */
175 if (!n[0]) return (char *)h;
176
177 /* Use faster algorithms for short needles */
178 h = strchr(h, *n);
179 if (!h || !n[1]) return (char *)h;
180 if (!h[1]) return 0;
181 if (!n[2]) return twobyte_strstr((void *)h, (void *)n);
182 if (!h[2]) return 0;
183 if (!n[3]) return threebyte_strstr((void *)h, (void *)n);
184 if (!h[3]) return 0;
185 if (!n[4]) return fourbyte_strstr((void *)h, (void *)n);
186
187 return twoway_strstr((void *)h, (void *)n);
188}
189DEF_STRONG(strstr);
diff --git a/src/lib/libc/string/strtok.3 b/src/lib/libc/string/strtok.3
deleted file mode 100644
index a28f72d6f2..0000000000
--- a/src/lib/libc/string/strtok.3
+++ /dev/null
@@ -1,169 +0,0 @@
1.\" $OpenBSD: strtok.3,v 1.24 2024/12/11 23:28:20 jsg Exp $
2.\"
3.\" Copyright (c) 1988, 1991 The Regents of the University of California.
4.\" All rights reserved.
5.\"
6.\" This code is derived from software contributed to Berkeley by
7.\" the American National Standards Committee X3, on Information
8.\" Processing Systems.
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. 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.Dd $Mdocdate: December 11 2024 $
35.Dt STRTOK 3
36.Os
37.Sh NAME
38.Nm strtok ,
39.Nm strtok_r
40.Nd string token operations
41.Sh SYNOPSIS
42.In string.h
43.Ft char *
44.Fn strtok "char *str" "const char *sep"
45.Ft char *
46.Fn strtok_r "char *str" "const char *sep" "char **last"
47.Sh DESCRIPTION
48.Bf -symbolic
49This interface is obsoleted by
50.Xr strsep 3 .
51.Ef
52.Pp
53The
54.Fn strtok
55function is used to isolate sequential tokens in a NUL-terminated string,
56.Fa str .
57These tokens are separated in the string by at least one of the
58characters in
59.Fa sep .
60The first time that
61.Fn strtok
62is called,
63.Fa str
64should be specified; subsequent calls, wishing to obtain further tokens
65from the same string, should pass a null pointer instead.
66The separator string,
67.Fa sep ,
68must be supplied each time, and may change between calls.
69.Pp
70The
71.Fn strtok_r
72function is a version of
73.Fn strtok
74that takes an explicit context argument and is reentrant.
75.Pp
76Since
77.Fn strtok
78and
79.Fn strtok_r
80modify the string,
81.Fa str
82should not point to an area in the initialized data segment.
83.Sh RETURN VALUES
84The
85.Fn strtok
86and
87.Fn strtok_r
88functions return a pointer to the beginning of each subsequent token
89in the string, after replacing the separator character itself with
90a NUL character.
91When no more tokens remain, a null pointer is returned.
92.Sh EXAMPLES
93The following will construct an array of pointers to each individual word in
94the string
95.Va s :
96.Bd -literal -offset indent
97#define MAXTOKENS 128
98
99char s[512], *p, *tokens[MAXTOKENS];
100char *last;
101int i = 0;
102
103snprintf(s, sizeof(s), "cat dog horse cow");
104
105for ((p = strtok_r(s, " ", &last)); p;
106 (p = strtok_r(NULL, " ", &last))) {
107 if (i < MAXTOKENS - 1)
108 tokens[i++] = p;
109}
110tokens[i] = NULL;
111.Ed
112.Pp
113That is,
114.Li tokens[0]
115will point to
116.Qq cat ,
117.Li tokens[1]
118will point to
119.Qq dog ,
120.Li tokens[2]
121will point to
122.Qq horse ,
123and
124.Li tokens[3]
125will point to
126.Qq cow .
127.Sh SEE ALSO
128.Xr memchr 3 ,
129.Xr strchr 3 ,
130.Xr strcspn 3 ,
131.Xr strpbrk 3 ,
132.Xr strrchr 3 ,
133.Xr strsep 3 ,
134.Xr strspn 3 ,
135.Xr strstr 3 ,
136.Xr wcstok 3
137.Sh STANDARDS
138The
139.Fn strtok
140function conforms to
141.St -ansiC .
142The
143.Fn strtok_r
144function conforms to
145.St -p1003.1-2001 .
146.Sh HISTORY
147The
148.Fn strtok
149function first appeared in
150.At III
151and was reimplemented for
152.Bx 4.3 .
153The
154.Fn strtok_r
155function first appeared in
156.Nx 1.3
157and was reimplemented for
158.Ox 2.7 .
159.Sh BUGS
160The System V
161.Fn strtok ,
162if handed a string containing only delimiter characters,
163will not alter the next starting point, so that a call to
164.Fn strtok
165with a different (or empty) delimiter string
166may return a non-null value.
167Since this implementation always alters the next starting point,
168such a sequence of calls would always return
169.Dv NULL .
diff --git a/src/lib/libc/string/strtok.c b/src/lib/libc/string/strtok.c
deleted file mode 100644
index c5765756fc..0000000000
--- a/src/lib/libc/string/strtok.c
+++ /dev/null
@@ -1,87 +0,0 @@
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. Neither the name of the University nor the names of its contributors
14 * may be used to endorse or promote products derived from this software
15 * without specific prior written permission.
16 *
17 * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
18 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
19 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
20 * ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
21 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
22 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
23 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
24 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
25 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
26 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
27 * SUCH DAMAGE.
28 */
29
30#include <string.h>
31
32char *
33strtok(char *s, const char *delim)
34{
35 static char *last;
36
37 return strtok_r(s, delim, &last);
38}
39DEF_STRONG(strtok);
40
41char *
42strtok_r(char *s, const char *delim, char **last)
43{
44 const char *spanp;
45 int c, sc;
46 char *tok;
47
48 if (s == NULL && (s = *last) == NULL)
49 return (NULL);
50
51 /*
52 * Skip (span) leading delimiters (s += strspn(s, delim), sort of).
53 */
54cont:
55 c = *s++;
56 for (spanp = delim; (sc = *spanp++) != 0;) {
57 if (c == sc)
58 goto cont;
59 }
60
61 if (c == 0) { /* no non-delimiter characters */
62 *last = NULL;
63 return (NULL);
64 }
65 tok = s - 1;
66
67 /*
68 * Scan token (scan for delimiters: s += strcspn(s, delim), sort of).
69 * Note that delim must have one NUL; we stop if we see that, too.
70 */
71 for (;;) {
72 c = *s++;
73 spanp = delim;
74 do {
75 if ((sc = *spanp++) == c) {
76 if (c == 0)
77 s = NULL;
78 else
79 s[-1] = '\0';
80 *last = s;
81 return (tok);
82 }
83 } while (sc != 0);
84 }
85 /* NOTREACHED */
86}
87DEF_WEAK(strtok_r);
diff --git a/src/lib/libc/string/strxfrm.3 b/src/lib/libc/string/strxfrm.3
deleted file mode 100644
index dab3673f38..0000000000
--- a/src/lib/libc/string/strxfrm.3
+++ /dev/null
@@ -1,105 +0,0 @@
1.\" $OpenBSD: strxfrm.3,v 1.12 2019/01/18 07:43:36 schwarze Exp $
2.\"
3.\" Copyright (c) 1990, 1991 The Regents of the University of California.
4.\" Copyright (c) 2017 Ingo Schwarze <schwarze@openbsd.org>
5.\" All rights reserved.
6.\"
7.\" This code is derived from software contributed to Berkeley by
8.\" Chris Torek and the American National Standards Committee X3,
9.\" on Information Processing Systems.
10.\"
11.\" Redistribution and use in source and binary forms, with or without
12.\" modification, are permitted provided that the following conditions
13.\" are met:
14.\" 1. Redistributions of source code must retain the above copyright
15.\" notice, this list of conditions and the following disclaimer.
16.\" 2. Redistributions in binary form must reproduce the above copyright
17.\" notice, this list of conditions and the following disclaimer in the
18.\" documentation and/or other materials provided with the distribution.
19.\" 3. 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.Dd $Mdocdate: January 18 2019 $
36.Dt STRXFRM 3
37.Os
38.Sh NAME
39.Nm strxfrm ,
40.Nm strxfrm_l
41.Nd transform a string under locale
42.Sh SYNOPSIS
43.In string.h
44.Ft size_t
45.Fn strxfrm "char *dst" "const char *src" "size_t n"
46.Ft size_t
47.Fn strxfrm_l "char *dst" "const char *src" "size_t n" "locale_t locale"
48.Sh DESCRIPTION
49The idea of
50.Fn strxfrm
51and
52.Fn strxfrm_l
53is to
54.Dq un-localize
55a string: the functions transform
56.Ar src ,
57storing the result in
58.Ar dst ,
59such that
60.Xr strcmp 3
61on transformed strings returns what
62.Xr strcoll 3
63on the original untransformed strings would return.
64.Pp
65On
66.Ox ,
67both have the same effect as
68.Xr strlcpy 3 ,
69and the global locale, the thread-specific locale, and the
70.Fa locale
71argument are ignored.
72On other operating systems, the behaviour may depend on the
73.Dv LC_CTYPE
74and
75.Dv LC_COLLATE
76locale categories set with
77.Xr setlocale 3 ,
78.Xr uselocale 3 ,
79or
80.Xr newlocale 3 .
81.Sh SEE ALSO
82.Xr newlocale 3 ,
83.Xr setlocale 3 ,
84.Xr strcmp 3 ,
85.Xr strcoll 3 ,
86.Xr strlcpy 3 ,
87.Xr wcsxfrm 3
88.Sh STANDARDS
89The
90.Fn strxfrm
91function conforms to
92.St -ansiC ,
93and
94.Fn strxfrm_l
95to
96.St -p1003.1-2008 .
97.Sh HISTORY
98The
99.Fn strxfrm
100function has been available since
101.Bx 4.3 Reno ,
102and
103.Fn strxfrm_l
104since
105.Ox 6.2 .
diff --git a/src/lib/libc/string/strxfrm.c b/src/lib/libc/string/strxfrm.c
deleted file mode 100644
index 97df097b29..0000000000
--- a/src/lib/libc/string/strxfrm.c
+++ /dev/null
@@ -1,52 +0,0 @@
1/* $OpenBSD: strxfrm.c,v 1.7 2015/08/31 02:53:57 guenther Exp $ */
2/*-
3 * Copyright (c) 1990 The Regents of the University of California.
4 * All rights reserved.
5 *
6 * This code is derived from software contributed to Berkeley by
7 * Chris Torek.
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. 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#include <string.h>
35
36/*
37 * Transform src, storing the result in dst, such that
38 * strcmp() on transformed strings returns what strcoll()
39 * on the original untransformed strings would return.
40 */
41size_t
42strxfrm(char *dst, const char *src, size_t n)
43{
44
45 /*
46 * Since locales are unimplemented, this is just a copy.
47 */
48 if (n == 0)
49 return (strlen(src));
50 return (strlcpy(dst, src, n));
51}
52DEF_STRONG(strxfrm);
diff --git a/src/lib/libc/string/strxfrm_l.c b/src/lib/libc/string/strxfrm_l.c
deleted file mode 100644
index ff77947953..0000000000
--- a/src/lib/libc/string/strxfrm_l.c
+++ /dev/null
@@ -1,14 +0,0 @@
1/* $OpenBSD: strxfrm_l.c,v 1.1 2017/09/05 03:16:14 schwarze Exp $ */
2/*
3 * Written in 2017 by Ingo Schwarze <schwarze@openbsd.org>.
4 * Released into the public domain.
5 */
6
7#include <string.h>
8
9size_t
10strxfrm_l(char *dst, const char *src, size_t n,
11 locale_t locale __attribute__((__unused__)))
12{
13 return strxfrm(dst, src, n);
14}
diff --git a/src/lib/libc/string/swab.3 b/src/lib/libc/string/swab.3
deleted file mode 100644
index 777f2833b6..0000000000
--- a/src/lib/libc/string/swab.3
+++ /dev/null
@@ -1,79 +0,0 @@
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. Neither the name of the University nor the names of its contributors
13.\" may be used to endorse or promote products derived from this software
14.\" without specific prior written permission.
15.\"
16.\" THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
17.\" ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
18.\" IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
19.\" ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
20.\" FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
21.\" DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
22.\" OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
23.\" HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
24.\" LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
25.\" OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
26.\" SUCH DAMAGE.
27.\"
28.\" $OpenBSD: swab.3,v 1.10 2022/09/28 20:27:12 jmc Exp $
29.\"
30.Dd $Mdocdate: September 28 2022 $
31.Dt SWAB 3
32.Os
33.Sh NAME
34.Nm swab
35.Nd swap adjacent bytes
36.Sh SYNOPSIS
37.In unistd.h
38.Ft void
39.Fo swab
40.Fa "const void *restrict src"
41.Fa "void *restrict dst"
42.Fa "ssize_t len"
43.Fc
44.Sh DESCRIPTION
45The
46.Fn swab
47function copies
48.Fa len
49bytes from the location referenced by
50.Fa src
51to the location referenced by
52.Fa dst ,
53swapping adjacent bytes.
54.Pp
55If
56.Fa len
57is zero or less,
58.Fn swab
59does nothing.
60If it is odd, what happens to the last byte is unspecified.
61If
62.Fa src
63and
64.Fa dst
65overlap, behaviour is undefined.
66.Sh SEE ALSO
67.Xr bzero 3 ,
68.Xr memset 3
69.Sh STANDARDS
70The
71.Fn swab
72function is compliant with the X/Open System Interfaces option of the
73.St -p1003.1-2008
74specification.
75.Sh HISTORY
76The
77.Fn swab
78function first appeared in
79.At v7 .
diff --git a/src/lib/libc/string/swab.c b/src/lib/libc/string/swab.c
deleted file mode 100644
index c7d7d72ce0..0000000000
--- a/src/lib/libc/string/swab.c
+++ /dev/null
@@ -1,35 +0,0 @@
1/* $OpenBSD: swab.c,v 1.9 2014/12/11 23:05:38 tedu Exp $ */
2/*
3 * Copyright (c) 2014 Ted Unangst <tedu@openbsd.org>
4 *
5 * Permission to use, copy, modify, and distribute this software for any
6 * purpose with or without fee is hereby granted, provided that the above
7 * copyright notice and this permission notice appear in all copies.
8 *
9 * THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES
10 * WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF
11 * MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR
12 * ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES
13 * WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN
14 * ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF
15 * OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE.
16 */
17#include <unistd.h>
18
19void
20swab(const void *__restrict from, void *__restrict to, ssize_t len)
21{
22 const unsigned char *src = from;
23 unsigned char *dst = to;
24 unsigned char t0, t1;
25
26 while (len > 1) {
27 t0 = src[0];
28 t1 = src[1];
29 dst[0] = t1;
30 dst[1] = t0;
31 src += 2;
32 dst += 2;
33 len -= 2;
34 }
35}
diff --git a/src/lib/libc/string/timingsafe_bcmp.3 b/src/lib/libc/string/timingsafe_bcmp.3
deleted file mode 100644
index 00da769157..0000000000
--- a/src/lib/libc/string/timingsafe_bcmp.3
+++ /dev/null
@@ -1,87 +0,0 @@
1.\" $OpenBSD: timingsafe_bcmp.3,v 1.2 2014/06/21 20:22:15 tedu Exp $
2.\"
3.\" Copyright (c) 2014 Google Inc.
4.\"
5.\" Permission to use, copy, modify, and distribute this software for any
6.\" purpose with or without fee is hereby granted, provided that the above
7.\" copyright notice and this permission notice appear in all copies.
8.\"
9.\" THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES
10.\" WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF
11.\" MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR
12.\" ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES
13.\" WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN
14.\" ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF
15.\" OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE.
16.Dd $Mdocdate: June 21 2014 $
17.Dt TIMINGSAFE_BCMP 3
18.Os
19.Sh NAME
20.Nm timingsafe_bcmp ,
21.Nm timingsafe_memcmp
22.Nd timing-safe byte sequence comparisons
23.Sh SYNOPSIS
24.In string.h
25.Ft int
26.Fn timingsafe_bcmp "const void *b1" "const void *b2" "size_t len"
27.Ft int
28.Fn timingsafe_memcmp "const void *b1" "const void *b2" "size_t len"
29.Sh DESCRIPTION
30The
31.Fn timingsafe_bcmp
32and
33.Fn timingsafe_memcmp
34functions lexicographically compare the first
35.Fa len
36bytes (each interpreted as an
37.Vt unsigned char )
38pointed to by
39.Fa b1
40and
41.Fa b2 .
42.Pp
43Additionally, their running times are independent of the byte sequences compared,
44making them safe to use for comparing secret values such as cryptographic MACs.
45In contrast,
46.Xr bcmp 3
47and
48.Xr memcmp 3
49may short-circuit after finding the first differing byte.
50.Sh RETURN VALUES
51The
52.Fn timingsafe_bcmp
53function returns 0 or not zero if the byte sequence pointed to by
54.Fa b1
55compares equal to or not equal to (respectively)
56the byte sequence pointed to by
57.Fa b2 .
58.Pp
59The
60.Fn timingsafe_memcmp
61function returns a negative value, 0, or positive value if the byte sequence
62pointed to by
63.Fa b1
64compares less than, equal to, or greater than (respectively)
65the byte sequence pointed to by
66.Fa b2 .
67.Sh SEE ALSO
68.Xr bcmp 3 ,
69.Xr memcmp 3
70.Sh STANDARDS
71The
72.Fn timingsafe_bcmp
73and
74.Fn timingsafe_memcmp
75functions are
76.Ox
77extensions.
78.Sh HISTORY
79The
80.Fn timingsafe_bcmp
81function first appeared in
82.Ox 4.9 .
83.Pp
84The
85.Fn timingsafe_memcmp
86function first appeared in
87.Ox 5.6 .
diff --git a/src/lib/libc/string/timingsafe_bcmp.c b/src/lib/libc/string/timingsafe_bcmp.c
deleted file mode 100644
index 0409ec3244..0000000000
--- a/src/lib/libc/string/timingsafe_bcmp.c
+++ /dev/null
@@ -1,30 +0,0 @@
1/* $OpenBSD: timingsafe_bcmp.c,v 1.3 2015/08/31 02:53:57 guenther Exp $ */
2/*
3 * Copyright (c) 2010 Damien Miller. All rights reserved.
4 *
5 * Permission to use, copy, modify, and distribute this software for any
6 * purpose with or without fee is hereby granted, provided that the above
7 * copyright notice and this permission notice appear in all copies.
8 *
9 * THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES
10 * WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF
11 * MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR
12 * ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES
13 * WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN
14 * ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF
15 * OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE.
16 */
17
18#include <string.h>
19
20int
21timingsafe_bcmp(const void *b1, const void *b2, size_t n)
22{
23 const unsigned char *p1 = b1, *p2 = b2;
24 int ret = 0;
25
26 for (; n > 0; n--)
27 ret |= *p1++ ^ *p2++;
28 return (ret != 0);
29}
30DEF_WEAK(timingsafe_bcmp);
diff --git a/src/lib/libc/string/timingsafe_memcmp.c b/src/lib/libc/string/timingsafe_memcmp.c
deleted file mode 100644
index 373f8cb197..0000000000
--- a/src/lib/libc/string/timingsafe_memcmp.c
+++ /dev/null
@@ -1,47 +0,0 @@
1/* $OpenBSD: timingsafe_memcmp.c,v 1.2 2015/08/31 02:53:57 guenther Exp $ */
2/*
3 * Copyright (c) 2014 Google Inc.
4 *
5 * Permission to use, copy, modify, and distribute this software for any
6 * purpose with or without fee is hereby granted, provided that the above
7 * copyright notice and this permission notice appear in all copies.
8 *
9 * THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES
10 * WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF
11 * MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR
12 * ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES
13 * WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN
14 * ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF
15 * OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE.
16 */
17
18#include <limits.h>
19#include <string.h>
20
21int
22timingsafe_memcmp(const void *b1, const void *b2, size_t len)
23{
24 const unsigned char *p1 = b1, *p2 = b2;
25 size_t i;
26 int res = 0, done = 0;
27
28 for (i = 0; i < len; i++) {
29 /* lt is -1 if p1[i] < p2[i]; else 0. */
30 int lt = (p1[i] - p2[i]) >> CHAR_BIT;
31
32 /* gt is -1 if p1[i] > p2[i]; else 0. */
33 int gt = (p2[i] - p1[i]) >> CHAR_BIT;
34
35 /* cmp is 1 if p1[i] > p2[i]; -1 if p1[i] < p2[i]; else 0. */
36 int cmp = lt - gt;
37
38 /* set res = cmp if !done. */
39 res |= cmp & ~done;
40
41 /* set done if p1[i] != p2[i]. */
42 done |= lt | gt;
43 }
44
45 return (res);
46}
47DEF_WEAK(timingsafe_memcmp);
diff --git a/src/lib/libc/string/wcscasecmp.3 b/src/lib/libc/string/wcscasecmp.3
deleted file mode 100644
index 9db4a829b9..0000000000
--- a/src/lib/libc/string/wcscasecmp.3
+++ /dev/null
@@ -1,134 +0,0 @@
1.\" $OpenBSD: wcscasecmp.3,v 1.5 2017/09/05 03:16:14 schwarze Exp $
2.\"
3.\" Copyright (c) 1990, 1991, 1993
4.\" The Regents of the University of California. All rights reserved.
5.\" Copyright (c) 2017 Ingo Schwarze <schwarze@openbsd.org>
6.\"
7.\" This code is derived from software contributed to Berkeley by
8.\" Chris Torek.
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. 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.\" @(#)strcasecmp.3 8.1 (Berkeley) 6/9/93
34.\"
35.Dd $Mdocdate: September 5 2017 $
36.Dt WCSCASECMP 3
37.Os
38.Sh NAME
39.Nm wcscasecmp ,
40.Nm wcscasecmp_l ,
41.Nm wcsncasecmp ,
42.Nm wcsncasecmp_l
43.Nd compare wide strings, ignoring case
44.Sh SYNOPSIS
45.In wchar.h
46.Ft int
47.Fo wcscasecmp
48.Fa "const wchar_t *s1"
49.Fa "const wchar_t *s2"
50.Fc
51.Ft int
52.Fo wcscasecmp_l
53.Fa "const wchar_t *s1"
54.Fa "const wchar_t *s2"
55.Fa "locale_t locale"
56.Fc
57.Ft int
58.Fo wcsncasecmp
59.Fa "const wchar_t *s1"
60.Fa "const wchar_t *s2"
61.Fa "size_t len"
62.Fc
63.Ft int
64.Fo wcsncasecmp_l
65.Fa "const wchar_t *s1"
66.Fa "const wchar_t *s2"
67.Fa "size_t len"
68.Fa "locale_t locale"
69.Fc
70.Sh DESCRIPTION
71These functions compare the wide strings
72.Fa s1
73and
74.Fa s2
75and return an integer greater than, equal to, or less than 0,
76according to whether
77.Fa s1
78is lexicographically greater than, equal to, or less than
79.Fa s2
80after translation of each corresponding wide character to lower case.
81The wide strings themselves are not modified.
82.Pp
83For the translation to lower case,
84.Fn wcscasecmp
85and
86.Fn wcsncasecmp
87use the thread-specific locale as defined with
88.Xr uselocale 3 ,
89falling back to the global locale defined with
90.Xr setlocale 3 .
91.Fn wcscasecmp_l
92and
93.Fn wcsncasecmp_l
94use the
95.Fa locale
96argument instead.
97.Pp
98.Fn wcsncasecmp
99and
100.Fn wcsncasecmp_l
101compare at most
102.Fa len
103wide characters.
104.Sh SEE ALSO
105.Xr newlocale 3 ,
106.Xr setlocale 3 ,
107.Xr strcasecmp 3 ,
108.Xr wcscmp 3 ,
109.Xr wmemcmp 3
110.Sh STANDARDS
111These functions conform to
112.St -p1003.1-2008 .
113.Sh HISTORY
114The
115.Fn wcscasecmp
116and
117.Fn wcsncasecmp
118functions have been available since
119.Ox 5.0 ,
120and
121.Fn wcscasecmp_l
122and
123.Fn wcsncasecmp_l
124since
125.Ox 6.2 .
126.Sh AUTHORS
127The
128.Ox
129versions of
130.Fn wcscasecmp
131and
132.Fn wcsncasecmp
133were implemented by
134.An Marc Espie Aq Mt espie@openbsd.org .
diff --git a/src/lib/libc/string/wcscasecmp.c b/src/lib/libc/string/wcscasecmp.c
deleted file mode 100644
index b122e720fe..0000000000
--- a/src/lib/libc/string/wcscasecmp.c
+++ /dev/null
@@ -1,63 +0,0 @@
1/* $OpenBSD: wcscasecmp.c,v 1.3 2015/09/12 16:23:14 guenther Exp $ */
2
3/*
4 * Copyright (c) 2011 Marc Espie
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 *
15 * THIS SOFTWARE IS PROVIDED BY THE OPENBSD PROJECT AND CONTRIBUTORS
16 * ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
17 * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
18 * A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE OPENBSD
19 * PROJECT OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
20 * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
21 * LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
22 * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
23 * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
24 * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
25 * OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
26 */
27
28#include <wchar.h>
29#include <wctype.h>
30#include "locale/runetype.h"
31
32int
33wcscasecmp(const wchar_t *s1, const wchar_t *s2)
34{
35 wchar_t l1, l2;
36
37 while ((l1 = towlower(*s1++)) == (l2 = towlower(*s2++))) {
38 if (l1 == 0)
39 return (0);
40 }
41 /* XXX assumes wchar_t = int */
42 return ((rune_t)l1 - (rune_t)l2);
43}
44DEF_WEAK(wcscasecmp);
45
46int
47wcsncasecmp(const wchar_t *s1, const wchar_t *s2, size_t n)
48{
49 wchar_t l1, l2;
50
51 if (n == 0)
52 return (0);
53 do {
54 if (((l1 = towlower(*s1++))) != (l2 = towlower(*s2++))) {
55 /* XXX assumes wchar_t = int */
56 return ((rune_t)l1 - (rune_t)l2);
57 }
58 if (l1 == 0)
59 break;
60 } while (--n != 0);
61 return (0);
62}
63DEF_WEAK(wcsncasecmp);
diff --git a/src/lib/libc/string/wcscasecmp_l.c b/src/lib/libc/string/wcscasecmp_l.c
deleted file mode 100644
index 35b99225c1..0000000000
--- a/src/lib/libc/string/wcscasecmp_l.c
+++ /dev/null
@@ -1,63 +0,0 @@
1/* $OpenBSD: wcscasecmp_l.c,v 1.1 2017/09/05 03:16:14 schwarze Exp $ */
2
3/*
4 * Copyright (c) 2011 Marc Espie
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 *
15 * THIS SOFTWARE IS PROVIDED BY THE OPENBSD PROJECT AND CONTRIBUTORS
16 * ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
17 * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
18 * A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE OPENBSD
19 * PROJECT OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
20 * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
21 * LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
22 * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
23 * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
24 * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
25 * OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
26 */
27
28#include <wchar.h>
29#include <wctype.h>
30#include "locale/runetype.h"
31
32int
33wcscasecmp_l(const wchar_t *s1, const wchar_t *s2, locale_t locale)
34{
35 wchar_t l1, l2;
36
37 while ((l1 = towlower_l(*s1++, locale)) ==
38 (l2 = towlower_l(*s2++, locale))) {
39 if (l1 == 0)
40 return (0);
41 }
42 /* XXX assumes wchar_t = int */
43 return ((rune_t)l1 - (rune_t)l2);
44}
45
46int
47wcsncasecmp_l(const wchar_t *s1, const wchar_t *s2, size_t n, locale_t locale)
48{
49 wchar_t l1, l2;
50
51 if (n == 0)
52 return (0);
53 do {
54 if (((l1 = towlower_l(*s1++, locale))) !=
55 (l2 = towlower_l(*s2++, locale))) {
56 /* XXX assumes wchar_t = int */
57 return ((rune_t)l1 - (rune_t)l2);
58 }
59 if (l1 == 0)
60 break;
61 } while (--n != 0);
62 return (0);
63}
diff --git a/src/lib/libc/string/wcscat.3 b/src/lib/libc/string/wcscat.3
deleted file mode 100644
index 9b7588cf9e..0000000000
--- a/src/lib/libc/string/wcscat.3
+++ /dev/null
@@ -1,115 +0,0 @@
1.\" $OpenBSD: wcscat.3,v 1.3 2013/06/05 03:39:23 tedu Exp $
2.\"
3.\" Copyright (c) 1990, 1991 The Regents of the University of California.
4.\" All rights reserved.
5.\"
6.\" This code is derived from software contributed to Berkeley by
7.\" Chris Torek and the American National Standards Committee X3,
8.\" on Information Processing Systems.
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. 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.Dd $Mdocdate: June 5 2013 $
35.Dt WCSCAT 3
36.Os
37.Sh NAME
38.Nm wcscat ,
39.Nm wcsncat
40.Nd concatenate wide strings
41.Sh SYNOPSIS
42.In wchar.h
43.Ft wchar_t *
44.Fn wcscat "wchar_t * restrict s" "const wchar_t * restrict append"
45.Ft wchar_t *
46.Fo wcsncat
47.Fa "wchar_t * restrict s"
48.Fa "const wchar_t * restrict append"
49.Fa "size_t count"
50.Fc
51.Sh DESCRIPTION
52The
53.Fn wcscat
54and
55.Fn wcsncat
56functions append a copy of the wide string
57.Fa append
58to the end of the wide string
59.Fa s ,
60then add a terminating null wide character (L'\e0').
61The wide string
62.Fa s
63must have sufficient space to hold the result.
64.Pp
65The
66.Fn wcsncat
67function appends not more than
68.Fa count
69wide characters where space for the terminating null wide character
70should not be included in
71.Fa count .
72.Sh RETURN VALUES
73The
74.Fn wcscat
75and
76.Fn wcsncat
77functions return the pointer
78.Fa s .
79.Sh SEE ALSO
80.Xr strcat 3 ,
81.Xr strlcpy 3 ,
82.Xr wcscpy 3 ,
83.Xr wcslcpy 3 ,
84.Xr wmemcpy 3 ,
85.Xr wmemmove 3
86.Sh STANDARDS
87The
88.Fn wcscat
89and
90.Fn wcsncat
91functions conform to
92.St -isoC-99
93and were first introduced in
94.St -isoC-amd1 .
95.Sh HISTORY
96The
97.Fn wcscat
98and
99.Fn wcsncat
100functions were ported from
101.Nx
102and first appeared in
103.Ox 3.8 .
104.Sh CAVEATS
105Using the functions
106.Fn wcscat
107and
108.Fn wcsncat
109is very error-prone with respect to buffer overflows;
110see the EXAMPLES section in
111.Xr strcat 3
112for correct usage.
113Using
114.Xr wcslcat 3
115is a better choice in most cases.
diff --git a/src/lib/libc/string/wcscat.c b/src/lib/libc/string/wcscat.c
deleted file mode 100644
index 0525c3cb14..0000000000
--- a/src/lib/libc/string/wcscat.c
+++ /dev/null
@@ -1,56 +0,0 @@
1/* $OpenBSD: wcscat.c,v 1.5 2017/11/28 06:55:49 tb Exp $ */
2/* $NetBSD: wcscat.c,v 1.2 2001/01/03 14:29:36 lukem Exp $ */
3
4/*-
5 * Copyright (c)1999 Citrus Project,
6 * All rights reserved.
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 *
17 * THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND
18 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
19 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
20 * ARE DISCLAIMED. IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE
21 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
22 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
23 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
24 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
25 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
26 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
27 * SUCH DAMAGE.
28 *
29 * citrus Id: wcscat.c,v 1.1 1999/12/29 21:47:45 tshiozak Exp
30 */
31
32#include <wchar.h>
33
34#if defined(APIWARN)
35__warn_references(wcscat,
36 "wcscat() is almost always misused, please use wcslcat()");
37#endif
38
39wchar_t *
40wcscat(wchar_t *s1, const wchar_t *s2)
41{
42 wchar_t *p;
43 wchar_t *q;
44 const wchar_t *r;
45
46 p = s1;
47 while (*p)
48 p++;
49 q = p;
50 r = s2;
51 while (*r)
52 *q++ = *r++;
53 *q = '\0';
54 return s1;
55}
56DEF_STRONG(wcscat);
diff --git a/src/lib/libc/string/wcschr.3 b/src/lib/libc/string/wcschr.3
deleted file mode 100644
index bb714b2099..0000000000
--- a/src/lib/libc/string/wcschr.3
+++ /dev/null
@@ -1,85 +0,0 @@
1.\" $OpenBSD: wcschr.3,v 1.4 2013/06/05 03:39:23 tedu Exp $
2.\"
3.\" Copyright (c) 1990, 1991 The Regents of the University of California.
4.\" All rights reserved.
5.\"
6.\" This code is derived from software contributed to Berkeley by
7.\" Chris Torek and the American National Standards Committee X3,
8.\" on Information Processing Systems.
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. 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.Dd $Mdocdate: June 5 2013 $
35.Dt WCSCHR 3
36.Os
37.Sh NAME
38.Nm wcschr
39.Nd locate first occurrence of a wide character in a wide string
40.Sh SYNOPSIS
41.In wchar.h
42.Ft wchar_t *
43.Fn wcschr "const wchar_t *s" "wchar_t c"
44.Sh DESCRIPTION
45The
46.Fn wcschr
47function locates the first occurrence of the wide character
48.Fa c
49in the wide string
50.Fa s .
51The terminating null wide character is considered part of the wide string.
52If
53.Fa c
54is the null wide character (L'\e0'),
55.Fn wcschr
56locates the terminating null wide character.
57.Sh RETURN VALUES
58The
59.Fn wcschr
60function returns a pointer to the located wide character or
61.Dv NULL
62if the wide character does not appear in the wide string.
63.Sh SEE ALSO
64.Xr strchr 3 ,
65.Xr wcscspn 3 ,
66.Xr wcspbrk 3 ,
67.Xr wcsrchr 3 ,
68.Xr wcsspn 3 ,
69.Xr wcsstr 3 ,
70.Xr wcstok 3 ,
71.Xr wmemchr 3
72.Sh STANDARDS
73The
74.Fn wcschr
75function conforms to
76.St -isoC-99
77and was first introduced in
78.St -isoC-amd1 .
79.Sh HISTORY
80The
81.Fn wcschr
82function was ported from
83.Nx
84and first appeared in
85.Ox 3.8 .
diff --git a/src/lib/libc/string/wcschr.c b/src/lib/libc/string/wcschr.c
deleted file mode 100644
index 709d4797bb..0000000000
--- a/src/lib/libc/string/wcschr.c
+++ /dev/null
@@ -1,50 +0,0 @@
1/* $OpenBSD: wcschr.c,v 1.6 2015/10/01 02:32:07 guenther Exp $ */
2/* $NetBSD: wcschr.c,v 1.2 2001/01/03 14:29:36 lukem Exp $ */
3
4/*-
5 * Copyright (c)1999 Citrus Project,
6 * All rights reserved.
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 *
17 * THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND
18 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
19 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
20 * ARE DISCLAIMED. IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE
21 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
22 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
23 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
24 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
25 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
26 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
27 * SUCH DAMAGE.
28 *
29 * citrus Id: wcschr.c,v 1.2 2000/12/21 05:07:25 itojun Exp
30 */
31
32#include <wchar.h>
33
34wchar_t *
35wcschr(const wchar_t *s, wchar_t c)
36{
37 const wchar_t *p;
38
39 p = s;
40 for (;;) {
41 if (*p == c) {
42 return (wchar_t *)p;
43 }
44 if (!*p)
45 return NULL;
46 p++;
47 }
48 /* NOTREACHED */
49}
50DEF_STRONG(wcschr);
diff --git a/src/lib/libc/string/wcscmp.3 b/src/lib/libc/string/wcscmp.3
deleted file mode 100644
index 53cd15a1f0..0000000000
--- a/src/lib/libc/string/wcscmp.3
+++ /dev/null
@@ -1,92 +0,0 @@
1.\" $OpenBSD: wcscmp.3,v 1.3 2013/06/05 03:39:23 tedu Exp $
2.\"
3.\" Copyright (c) 1990, 1991 The Regents of the University of California.
4.\" All rights reserved.
5.\"
6.\" This code is derived from software contributed to Berkeley by
7.\" Chris Torek and the American National Standards Committee X3,
8.\" on Information Processing Systems.
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. 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.Dd $Mdocdate: June 5 2013 $
35.Dt WCSCMP 3
36.Os
37.Sh NAME
38.Nm wcscmp ,
39.Nm wcsncmp
40.Nd compare wide strings
41.Sh SYNOPSIS
42.In wchar.h
43.Ft int
44.Fn wcscmp "const wchar_t *s1" "const wchar_t *s2"
45.Ft int
46.Fn wcsncmp "const wchar_t *s1" "const wchar_t *s2" "size_t len"
47.Sh DESCRIPTION
48The
49.Fn wcscmp
50and
51.Fn wcsncmp
52functions lexicographically compare the wide strings
53.Fa s1
54and
55.Fa s2 .
56The
57.Fn wcsncmp
58function compares at most
59.Fa len
60wide characters.
61.Sh RETURN VALUES
62The
63.Fn wcscmp
64and
65.Fn wcsncmp
66functions return an integer greater than, equal to, or less than 0, according
67to whether the wide string
68.Fa s1
69is greater than, equal to, or less than the wide string
70.Fa s2 .
71.Sh SEE ALSO
72.Xr strcmp 3 ,
73.Xr wcscasecmp 3 ,
74.Xr wmemcmp 3
75.Sh STANDARDS
76The
77.Fn wcscmp
78and
79.Fn wcsncmp
80functions conform to
81.St -isoC-99
82and were first introduced in
83.St -isoC-amd1 .
84.Sh HISTORY
85The
86.Fn wcscmp
87and
88.Fn wcsncmp
89functions were ported from
90.Nx
91and first appeared in
92.Ox 3.8 .
diff --git a/src/lib/libc/string/wcscmp.c b/src/lib/libc/string/wcscmp.c
deleted file mode 100644
index 42d6bca3cc..0000000000
--- a/src/lib/libc/string/wcscmp.c
+++ /dev/null
@@ -1,52 +0,0 @@
1/* $OpenBSD: wcscmp.c,v 1.5 2015/09/12 16:23:14 guenther Exp $ */
2/* $NetBSD: wcscmp.c,v 1.5 2003/08/07 16:43:54 agc Exp $ */
3
4/*-
5 * Copyright (c) 1990, 1993
6 * The Regents of the University of California. All rights reserved.
7 *
8 * This code is derived from software contributed to Berkeley by
9 * Chris Torek.
10 *
11 * Redistribution and use in source and binary forms, with or without
12 * modification, are permitted provided that the following conditions
13 * are met:
14 * 1. Redistributions of source code must retain the above copyright
15 * notice, this list of conditions and the following disclaimer.
16 * 2. Redistributions in binary form must reproduce the above copyright
17 * notice, this list of conditions and the following disclaimer in the
18 * documentation and/or other materials provided with the distribution.
19 * 3. 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#include <wchar.h>
37#include "locale/runetype.h"
38
39/*
40 * Compare strings.
41 */
42int
43wcscmp(const wchar_t *s1, const wchar_t *s2)
44{
45
46 while (*s1 == *s2++)
47 if (*s1++ == 0)
48 return (0);
49 /* XXX assumes wchar_t = int */
50 return (*(const rune_t *)s1 - *(const rune_t *)--s2);
51}
52DEF_STRONG(wcscmp);
diff --git a/src/lib/libc/string/wcscpy.3 b/src/lib/libc/string/wcscpy.3
deleted file mode 100644
index fcf01b5c57..0000000000
--- a/src/lib/libc/string/wcscpy.3
+++ /dev/null
@@ -1,129 +0,0 @@
1.\" $OpenBSD: wcscpy.3,v 1.5 2016/11/12 08:58:43 jmc Exp $
2.\"
3.\" Copyright (c) 1990, 1991 The Regents of the University of California.
4.\" All rights reserved.
5.\"
6.\" This code is derived from software contributed to Berkeley by
7.\" Chris Torek and the American National Standards Committee X3,
8.\" on Information Processing Systems.
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. 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.Dd $Mdocdate: November 12 2016 $
35.Dt WCSCPY 3
36.Os
37.Sh NAME
38.Nm wcscpy ,
39.Nm wcsncpy
40.Nd copy wide strings
41.Sh SYNOPSIS
42.In wchar.h
43.Ft wchar_t *
44.Fn wcscpy "wchar_t * restrict dst" "const wchar_t * restrict src"
45.Ft wchar_t *
46.Fo wcsncpy
47.Fa "wchar_t * restrict dst"
48.Fa "const wchar_t * restrict src"
49.Fa "size_t len"
50.Fc
51.Sh DESCRIPTION
52The
53.Fn wcscpy
54and
55.Fn wcsncpy
56functions copy the wide string
57.Fa src
58to
59.Fa dst
60(including the terminating null wide character).
61.Pp
62The
63.Fn wcsncpy
64function copies not more than
65.Fa len
66wide characters to
67.Fa dst ,
68appending null wide characters if the length of
69.Fa src
70is less than
71.Fa len ,
72and
73.Em not
74terminating
75.Fa dst
76if the length of
77.Fa src
78is greater than or equal to
79.Fa len .
80.Pp
81If the
82.Fa src
83and
84.Fa dst
85strings overlap, the behavior is undefined.
86.Sh RETURN VALUES
87The
88.Fn wcscpy
89and
90.Fn wcsncpy
91functions return
92.Fa dst .
93.Sh SEE ALSO
94.Xr strcpy 3 ,
95.Xr strlcpy 3 ,
96.Xr wcscat 3 ,
97.Xr wcslcpy 3 ,
98.Xr wmemcpy 3 ,
99.Xr wmemmove 3
100.Sh STANDARDS
101The
102.Fn wcscpy
103and
104.Fn wcsncpy
105functions conform to
106.St -isoC-99
107and were first introduced in
108.St -isoC-amd1 .
109.Sh HISTORY
110The
111.Fn wcscpy
112and
113.Fn wcsncpy
114functions were ported from
115.Nx
116and first appeared in
117.Ox 3.8 .
118.Sh CAVEATS
119Using the functions
120.Fn wcscpy
121and
122.Fn wcsncpy
123is very error-prone with respect to buffer overflows;
124see the EXAMPLES section in
125.Xr strncpy 3
126for correct usage.
127Using
128.Xr wcslcpy 3
129is a better choice in most cases.
diff --git a/src/lib/libc/string/wcscpy.c b/src/lib/libc/string/wcscpy.c
deleted file mode 100644
index f772752413..0000000000
--- a/src/lib/libc/string/wcscpy.c
+++ /dev/null
@@ -1,52 +0,0 @@
1/* $OpenBSD: wcscpy.c,v 1.5 2017/11/28 06:55:49 tb Exp $ */
2/* $NetBSD: wcscpy.c,v 1.2 2001/01/03 14:29:36 lukem Exp $ */
3
4/*-
5 * Copyright (c)1999 Citrus Project,
6 * All rights reserved.
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 *
17 * THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND
18 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
19 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
20 * ARE DISCLAIMED. IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE
21 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
22 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
23 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
24 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
25 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
26 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
27 * SUCH DAMAGE.
28 *
29 * citrus Id: wcscpy.c,v 1.2 2000/12/21 04:51:09 itojun Exp
30 */
31
32#include <wchar.h>
33
34#if defined(APIWARN)
35__warn_references(wcscpy,
36 "wcscpy() is almost always misused, please use wcslcpy()");
37#endif
38
39wchar_t *
40wcscpy(wchar_t *s1, const wchar_t *s2)
41{
42 wchar_t *p;
43 const wchar_t *q;
44
45 p = s1;
46 q = s2;
47 while (*q)
48 *p++ = *q++;
49 *p = '\0';
50
51 return s1;
52}
diff --git a/src/lib/libc/string/wcscspn.3 b/src/lib/libc/string/wcscspn.3
deleted file mode 100644
index 520a739202..0000000000
--- a/src/lib/libc/string/wcscspn.3
+++ /dev/null
@@ -1,83 +0,0 @@
1.\" $OpenBSD: wcscspn.3,v 1.3 2013/06/05 03:39:23 tedu Exp $
2.\"
3.\" Copyright (c) 1990, 1991 The Regents of the University of California.
4.\" All rights reserved.
5.\"
6.\" This code is derived from software contributed to Berkeley by
7.\" Chris Torek and the American National Standards Committee X3,
8.\" on Information Processing Systems.
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. 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.Dd $Mdocdate: June 5 2013 $
35.Dt WCSCSPN 3
36.Os
37.Sh NAME
38.Nm wcscspn
39.Nd span the complement of a wide string
40.Sh SYNOPSIS
41.In wchar.h
42.Ft size_t
43.Fn wcscspn "const wchar_t *s" "const wchar_t *charset"
44.Sh DESCRIPTION
45The
46.Fn wcscspn
47function spans the initial part of the wide string
48.Fa s
49as long as the wide characters from
50.Fa s
51do not occur in string
52.Fa charset
53(it spans the
54.Em complement
55of
56.Fa charset ) .
57.Sh RETURN VALUES
58The
59.Fn wcscspn
60function returns the number of wide characters spanned.
61.Sh SEE ALSO
62.Xr strcspn 3 ,
63.Xr wcschr 3 ,
64.Xr wcspbrk 3 ,
65.Xr wcsrchr 3 ,
66.Xr wcsspn 3 ,
67.Xr wcsstr 3 ,
68.Xr wcstok 3 ,
69.Xr wmemchr 3
70.Sh STANDARDS
71The
72.Fn wcscspn
73function conforms to
74.St -isoC-99
75and was first introduced in
76.St -isoC-amd1 .
77.Sh HISTORY
78The
79.Fn wcscspn
80function was ported from
81.Nx
82and first appeared in
83.Ox 3.8 .
diff --git a/src/lib/libc/string/wcscspn.c b/src/lib/libc/string/wcscspn.c
deleted file mode 100644
index 23e78ab22f..0000000000
--- a/src/lib/libc/string/wcscspn.c
+++ /dev/null
@@ -1,54 +0,0 @@
1/* $OpenBSD: wcscspn.c,v 1.4 2015/09/12 16:23:14 guenther Exp $ */
2/* $NetBSD: wcscspn.c,v 1.2 2001/01/03 14:29:36 lukem Exp $ */
3
4/*-
5 * Copyright (c)1999 Citrus Project,
6 * All rights reserved.
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 *
17 * THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND
18 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
19 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
20 * ARE DISCLAIMED. IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE
21 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
22 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
23 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
24 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
25 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
26 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
27 * SUCH DAMAGE.
28 *
29 * citrus Id: wcscspn.c,v 1.1 1999/12/29 21:47:45 tshiozak Exp
30 */
31
32#include <wchar.h>
33
34size_t
35wcscspn(const wchar_t *s, const wchar_t *set)
36{
37 const wchar_t *p;
38 const wchar_t *q;
39
40 p = s;
41 while (*p) {
42 q = set;
43 while (*q) {
44 if (*p == *q)
45 goto done;
46 q++;
47 }
48 p++;
49 }
50
51done:
52 return (p - s);
53}
54DEF_STRONG(wcscspn);
diff --git a/src/lib/libc/string/wcsdup.3 b/src/lib/libc/string/wcsdup.3
deleted file mode 100644
index 588a2571db..0000000000
--- a/src/lib/libc/string/wcsdup.3
+++ /dev/null
@@ -1,97 +0,0 @@
1.\" $OpenBSD: wcsdup.3,v 1.4 2011/07/25 00:38:53 schwarze Exp $
2.\" $NetBSD: wcsdup.3,v 1.3 2010/12/16 17:42:28 wiz Exp $
3.\"
4.\" Copyright (c) 1990, 1991, 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. Neither the name of the University nor the names of its contributors
16.\" may be used to endorse or promote products derived from this software
17.\" without specific prior written permission.
18.\"
19.\" THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
20.\" ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
21.\" IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
22.\" ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
23.\" FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
24.\" DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
25.\" OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
26.\" HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
27.\" LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
28.\" OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
29.\" SUCH DAMAGE.
30.\"
31.\" @(#)strdup.3 8.1 (Berkeley) 6/9/93
32.\"
33.Dd $Mdocdate: July 25 2011 $
34.Dt WCSDUP 3
35.Os
36.Sh NAME
37.Nm wcsdup
38.Nd save a copy of a wide string
39.Sh SYNOPSIS
40.In wchar.h
41.Ft wchar_t *
42.Fn wcsdup "const wchar_t *str"
43.Sh DESCRIPTION
44The
45.Fn wcsdup
46function
47allocates sufficient memory for a copy
48of the wide-character string
49.Fa str ,
50does the copy, and returns a pointer to it.
51The pointer may subsequently be used as an
52argument to the function
53.Xr free 3 .
54.Pp
55If insufficient memory is available,
56.Dv NULL
57is returned.
58.Sh EXAMPLES
59The following will point
60.Va p
61to an allocated area of memory containing the nul-terminated string
62.Qq foobar :
63.Bd -literal -offset indent
64const char *o = "foobar";
65wchar_t *p, b[32];
66size_t blen;
67
68blen = sizeof(b) / sizeof(b[0]);
69if (mbstowcs(b, o, blen) == (size_t)-1)
70 err(1, NULL);
71b[blen - 1] = 0;
72if ((p = wcsdup(b)) == NULL)
73 err(1, NULL);
74.Ed
75.Sh ERRORS
76The
77.Fn wcsdup
78function may fail and set the external variable
79.Va errno
80for any of the errors specified for the library function
81.Xr malloc 3 .
82.Sh SEE ALSO
83.Xr free 3 ,
84.Xr malloc 3 ,
85.Xr strdup 3
86.Sh STANDARDS
87The
88.Fn wcsdup
89function conforms to
90.St -p1003.1-2008 .
91.Sh HISTORY
92The
93.Fn wcsdup
94function was ported from
95.Nx
96and first appeared in
97.Ox 5.0 .
diff --git a/src/lib/libc/string/wcsdup.c b/src/lib/libc/string/wcsdup.c
deleted file mode 100644
index 36f19186ab..0000000000
--- a/src/lib/libc/string/wcsdup.c
+++ /dev/null
@@ -1,32 +0,0 @@
1/* $OpenBSD: wcsdup.c,v 1.3 2015/09/12 16:23:14 guenther Exp $ */
2/* $NetBSD: wcsdup.c,v 1.3 2008/05/26 13:17:48 haad Exp $ */
3
4/*
5 * Copyright (C) 2006 Aleksey Cheusov
6 *
7 * This material is provided "as is", with absolutely no warranty expressed
8 * or implied. Any use is at your own risk.
9 *
10 * Permission to use or copy this software for any purpose is hereby granted
11 * without fee. Permission to modify the code and to distribute modified
12 * code is also granted without any restrictions.
13 */
14
15#include <stdlib.h>
16#include <wchar.h>
17
18wchar_t *
19wcsdup(const wchar_t *str)
20{
21 wchar_t *copy;
22 size_t len;
23
24 len = wcslen(str) + 1;
25 copy = reallocarray(NULL, len, sizeof(wchar_t));
26
27 if (!copy)
28 return (NULL);
29
30 return (wmemcpy(copy, str, len));
31}
32DEF_WEAK(wcsdup);
diff --git a/src/lib/libc/string/wcslcat.c b/src/lib/libc/string/wcslcat.c
deleted file mode 100644
index 9949057df4..0000000000
--- a/src/lib/libc/string/wcslcat.c
+++ /dev/null
@@ -1,56 +0,0 @@
1/* $OpenBSD: wcslcat.c,v 1.7 2019/01/25 00:19:25 millert Exp $ */
2
3/*
4 * Copyright (c) 1998, 2015 Todd C. Miller <millert@openbsd.org>
5 *
6 * Permission to use, copy, modify, and distribute this software for any
7 * purpose with or without fee is hereby granted, provided that the above
8 * copyright notice and this permission notice appear in all copies.
9 *
10 * THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES
11 * WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF
12 * MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR
13 * ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES
14 * WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN
15 * ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF
16 * OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE.
17 */
18
19#include <sys/types.h>
20#include <wchar.h>
21
22/*
23 * Appends src to string dst of size dsize (unlike strncat, dsize is the
24 * full size of dst, not space left). At most dsize-1 characters
25 * will be copied. Always NUL terminates (unless dsize <= wcslen(dst)).
26 * Returns wcslen(src) + MIN(dsize, wcslen(initial dst)).
27 * If retval >= siz, truncation occurred.
28 */
29size_t
30wcslcat(wchar_t *dst, const wchar_t *src, size_t dsize)
31{
32 const wchar_t *odst = dst;
33 const wchar_t *osrc = src;
34 size_t n = dsize;
35 size_t dlen;
36
37 /* Find the end of dst and adjust bytes left but don't go past end. */
38 while (n-- != 0 && *dst != L'\0')
39 dst++;
40 dlen = dst - odst;
41 n = dsize - dlen;
42
43 if (n-- == 0)
44 return(dlen + wcslen(src));
45 while (*src != L'\0') {
46 if (n != 0) {
47 *dst++ = *src;
48 n--;
49 }
50 src++;
51 }
52 *dst = L'\0';
53
54 return(dlen + (src - osrc)); /* count does not include NUL */
55}
56DEF_WEAK(wcslcat);
diff --git a/src/lib/libc/string/wcslcpy.3 b/src/lib/libc/string/wcslcpy.3
deleted file mode 100644
index d279038079..0000000000
--- a/src/lib/libc/string/wcslcpy.3
+++ /dev/null
@@ -1,160 +0,0 @@
1.\" $OpenBSD: wcslcpy.3,v 1.8 2024/08/07 04:59:45 guenther Exp $
2.\"
3.\" Copyright (c) 1998, 2000 Todd C. Miller <millert@openbsd.org>
4.\"
5.\" Permission to use, copy, modify, and distribute this software for any
6.\" purpose with or without fee is hereby granted, provided that the above
7.\" copyright notice and this permission notice appear in all copies.
8.\"
9.\" THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES
10.\" WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF
11.\" MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR
12.\" ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES
13.\" WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN
14.\" ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF
15.\" OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE.
16.\"
17.Dd $Mdocdate: August 7 2024 $
18.Dt WCSLCPY 3
19.Os
20.Sh NAME
21.Nm wcslcpy ,
22.Nm wcslcat
23.Nd size-bounded wide string copying and concatenation
24.Sh SYNOPSIS
25.In wchar.h
26.Ft size_t
27.Fn wcslcpy "wchar_t * restrict dst" "const wchar_t * restrict src" "size_t size"
28.Ft size_t
29.Fn wcslcat "wchar_t * restrict dst" "const wchar_t * restrict src" "size_t size"
30.Sh DESCRIPTION
31The
32.Fn wcslcpy
33and
34.Fn wcslcat
35functions copy and concatenate wide strings respectively.
36They are designed to be safer, more consistent, and less error prone
37replacements for
38.Xr wcsncpy 3
39and
40.Xr wcsncat 3 .
41Unlike those functions,
42.Fn wcslcpy
43and
44.Fn wcslcat
45take the full size of the buffer (not just the length) and guarantee to
46terminate the result with a null wide character (as long as
47.Fa size
48is larger than 0 or, in the case of
49.Fn wcslcat ,
50as long as there is at least one wide character free in
51.Fa dst ) .
52Note that a wide character for the null wide character should be included in
53.Fa size .
54Also note that
55.Fn wcslcpy
56and
57.Fn wcslcat
58only operate on wide strings that are terminated with a null wide character
59(L'\e0').
60This means that for
61.Fn wcslcpy
62.Fa src
63must be terminated with a null wide character and for
64.Fn wcslcat
65both
66.Fa src
67and
68.Fa dst
69must be terminated with a null wide character.
70.Pp
71The
72.Fn wcslcpy
73function copies up to
74.Fa size
75\(mi 1 wide characters from the wide string
76.Fa src
77to
78.Fa dst ,
79terminating the result with a null wide character.
80.Pp
81The
82.Fn wcslcat
83function appends the wide string
84.Fa src
85to the end of
86.Fa dst .
87It will append at most
88.Fa size
89\(mi wcslen(dst) \(mi 1 wide characters, terminating the result with a null
90wide character.
91.Pp
92If the
93.Fa src
94and
95.Fa dst
96strings overlap, the behavior is undefined.
97.Sh RETURN VALUES
98The
99.Fn wcslcpy
100and
101.Fn wcslcat
102functions return the total length of the wide string they tried to create.
103For
104.Fn wcslcpy
105that means the length of
106.Fa src .
107For
108.Fn wcslcat
109that means the initial length of
110.Fa dst
111plus
112the length of
113.Fa src .
114While this may seem somewhat confusing, it was done to make
115truncation detection simple.
116.Pp
117Note, however, that if
118.Fn wcslcat
119traverses
120.Fa size
121wide characters without finding a null wide character, the length of the
122string is considered to be
123.Fa size
124and the destination wide string will not be terminated with a null wide
125character (since there was no space for it).
126This keeps
127.Fn wcslcat
128from running off the end of a wide string.
129In practice this should not happen (as it means that either
130.Fa size
131is incorrect or that
132.Fa dst
133is not terminated with a null wide character).
134The check exists to prevent potential security problems in incorrect code.
135.Sh SEE ALSO
136.Xr strlcpy 3 ,
137.Xr swprintf 3 ,
138.Xr wcsncat 3 ,
139.Xr wcsncpy 3
140.Sh STANDARDS
141The
142.Fn wcslcpy
143and
144.Fn wcslcat
145functions conform to
146.St -p1003.1-2024 .
147.Sh HISTORY
148The
149.Fn wcslcpy
150and
151.Fn wcslcat
152functions first appeared in
153.Ox 3.8 .
154.Sh AUTHORS
155The
156.Fn wcslcpy
157and
158.Fn wcslcat
159functions are based on code by
160.An Todd C. Miller Aq Mt millert@openbsd.org .
diff --git a/src/lib/libc/string/wcslcpy.c b/src/lib/libc/string/wcslcpy.c
deleted file mode 100644
index 9c433c83dc..0000000000
--- a/src/lib/libc/string/wcslcpy.c
+++ /dev/null
@@ -1,51 +0,0 @@
1/* $OpenBSD: wcslcpy.c,v 1.8 2019/01/25 00:19:25 millert Exp $ */
2
3/*
4 * Copyright (c) 1998, 2015 Todd C. Miller <millert@openbsd.org>
5 *
6 * Permission to use, copy, modify, and distribute this software for any
7 * purpose with or without fee is hereby granted, provided that the above
8 * copyright notice and this permission notice appear in all copies.
9 *
10 * THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES
11 * WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF
12 * MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR
13 * ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES
14 * WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN
15 * ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF
16 * OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE.
17 */
18
19#include <sys/types.h>
20#include <wchar.h>
21
22/*
23 * Copy string src to buffer dst of size dsize. At most dsize-1
24 * chars will be copied. Always NUL terminates (unless dsize == 0).
25 * Returns wcslen(src); if retval >= dsize, truncation occurred.
26 */
27size_t
28wcslcpy(wchar_t *dst, const wchar_t *src, size_t dsize)
29{
30 const wchar_t *osrc = src;
31 size_t nleft = dsize;
32
33 /* Copy as many bytes as will fit. */
34 if (nleft != 0) {
35 while (--nleft != 0) {
36 if ((*dst++ = *src++) == L'\0')
37 break;
38 }
39 }
40
41 /* Not enough room in dst, add NUL and traverse rest of src. */
42 if (nleft == 0) {
43 if (dsize != 0)
44 *dst = L'\0'; /* NUL-terminate dst */
45 while (*src++)
46 ;
47 }
48
49 return(src - osrc - 1); /* count does not include NUL */
50}
51DEF_WEAK(wcslcpy);
diff --git a/src/lib/libc/string/wcslen.3 b/src/lib/libc/string/wcslen.3
deleted file mode 100644
index b6d9aafc41..0000000000
--- a/src/lib/libc/string/wcslen.3
+++ /dev/null
@@ -1,102 +0,0 @@
1.\" $OpenBSD: wcslen.3,v 1.4 2024/07/14 09:51:18 jca Exp $
2.\"
3.\" Copyright (c) 1990, 1991 The Regents of the University of California.
4.\" All rights reserved.
5.\"
6.\" This code is derived from software contributed to Berkeley by
7.\" Chris Torek and the American National Standards Committee X3,
8.\" on Information Processing Systems.
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. 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.Dd $Mdocdate: July 14 2024 $
35.Dt WCSLEN 3
36.Os
37.Sh NAME
38.Nm wcslen ,
39.Nm wcsnlen
40.Nd find length of a wide string
41.Sh SYNOPSIS
42.In wchar.h
43.Ft size_t
44.Fn wcslen "const wchar_t *s"
45.Ft size_t
46.Fn wcsnlen "const wchar_t *s" "size_t maxlen"
47.Sh DESCRIPTION
48The
49.Fn wcslen
50function computes the length of the wide string
51.Fa s .
52The
53.Fn wcsnlen
54function computes the length of the wide string
55.Fa s ,
56up to
57.Fa maxlen
58wide characters.
59The
60.Fn wcsnlen
61function will never attempt to address more than
62.Fa maxlen
63wide characters, making it suitable for use with wide character arrays
64that are not guaranteed to be NUL-terminated.
65.Sh RETURN VALUES
66The
67.Fn wcslen
68function returns the number of wide characters that precede the terminating
69null wide character.
70.Pp
71The
72.Fn wcsnlen
73function returns the number of wide characters that precede the terminating
74null wide character
75or
76.Fa maxlen ,
77whichever is smaller.
78.Sh SEE ALSO
79.Xr strlen 3 ,
80.Xr wcswidth 3
81.Sh STANDARDS
82The
83.Fn wcslen
84function conforms to
85.St -isoC-99
86and was first introduced in
87.St -isoC-amd1 .
88The
89.Fn wcsnlen
90function conforms to
91.St -p1003.1-2008 .
92.Sh HISTORY
93The
94.Fn wcslen
95function was ported from
96.Nx
97and first appeared in
98.Ox 3.8 .
99The
100.Fn wcsnlen
101function first appeared in
102.Ox 7.6 .
diff --git a/src/lib/libc/string/wcslen.c b/src/lib/libc/string/wcslen.c
deleted file mode 100644
index 16d4bba128..0000000000
--- a/src/lib/libc/string/wcslen.c
+++ /dev/null
@@ -1,45 +0,0 @@
1/* $OpenBSD: wcslen.c,v 1.4 2015/09/12 16:23:14 guenther Exp $ */
2/* $NetBSD: wcslen.c,v 1.2 2001/01/03 14:29:36 lukem Exp $ */
3
4/*-
5 * Copyright (c)1999 Citrus Project,
6 * All rights reserved.
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 *
17 * THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND
18 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
19 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
20 * ARE DISCLAIMED. IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE
21 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
22 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
23 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
24 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
25 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
26 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
27 * SUCH DAMAGE.
28 *
29 * citrus Id: wcslen.c,v 1.1 1999/12/29 21:47:45 tshiozak Exp
30 */
31
32#include <wchar.h>
33
34size_t
35wcslen(const wchar_t *s)
36{
37 const wchar_t *p;
38
39 p = s;
40 while (*p)
41 p++;
42
43 return p - s;
44}
45DEF_STRONG(wcslen);
diff --git a/src/lib/libc/string/wcsncat.c b/src/lib/libc/string/wcsncat.c
deleted file mode 100644
index 2b4b9f0d49..0000000000
--- a/src/lib/libc/string/wcsncat.c
+++ /dev/null
@@ -1,53 +0,0 @@
1/* $OpenBSD: wcsncat.c,v 1.4 2015/09/12 16:23:14 guenther Exp $ */
2/* $NetBSD: wcsncat.c,v 1.2 2001/01/03 14:29:36 lukem Exp $ */
3
4/*-
5 * Copyright (c)1999 Citrus Project,
6 * All rights reserved.
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 *
17 * THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND
18 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
19 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
20 * ARE DISCLAIMED. IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE
21 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
22 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
23 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
24 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
25 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
26 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
27 * SUCH DAMAGE.
28 *
29 * citrus Id: wcsncat.c,v 1.1 1999/12/29 21:47:45 tshiozak Exp
30 */
31
32#include <wchar.h>
33
34wchar_t *
35wcsncat(wchar_t *s1, const wchar_t *s2, size_t n)
36{
37 wchar_t *p;
38 wchar_t *q;
39 const wchar_t *r;
40
41 p = s1;
42 while (*p)
43 p++;
44 q = p;
45 r = s2;
46 while (*r && n) {
47 *q++ = *r++;
48 n--;
49 }
50 *q = '\0';
51 return s1;
52}
53DEF_STRONG(wcsncat);
diff --git a/src/lib/libc/string/wcsncmp.c b/src/lib/libc/string/wcsncmp.c
deleted file mode 100644
index bdaab33e4c..0000000000
--- a/src/lib/libc/string/wcsncmp.c
+++ /dev/null
@@ -1,53 +0,0 @@
1/* $OpenBSD: wcsncmp.c,v 1.5 2015/09/12 16:23:14 guenther Exp $ */
2/* $NetBSD: wcsncmp.c,v 1.5 2003/08/07 16:43:54 agc Exp $ */
3
4/*
5 * Copyright (c) 1989, 1993
6 * The Regents of the University of California. All rights reserved.
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. 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
33#include <wchar.h>
34#include "locale/runetype.h"
35
36int
37wcsncmp(const wchar_t *s1, const wchar_t *s2, size_t n)
38{
39
40 if (n == 0)
41 return (0);
42 do {
43 if (*s1 != *s2++) {
44 /* XXX assumes wchar_t = int */
45 return (*(const rune_t *)s1 -
46 *(const rune_t *)--s2);
47 }
48 if (*s1++ == 0)
49 break;
50 } while (--n != 0);
51 return (0);
52}
53DEF_STRONG(wcsncmp);
diff --git a/src/lib/libc/string/wcsncpy.c b/src/lib/libc/string/wcsncpy.c
deleted file mode 100644
index 1be482b114..0000000000
--- a/src/lib/libc/string/wcsncpy.c
+++ /dev/null
@@ -1,51 +0,0 @@
1/* $OpenBSD: wcsncpy.c,v 1.5 2015/09/12 16:23:14 guenther Exp $ */
2/* $NetBSD: wcsncpy.c,v 1.2 2001/01/03 14:29:37 lukem Exp $ */
3
4/*-
5 * Copyright (c)1999 Citrus Project,
6 * All rights reserved.
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 *
17 * THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND
18 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
19 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
20 * ARE DISCLAIMED. IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE
21 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
22 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
23 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
24 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
25 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
26 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
27 * SUCH DAMAGE.
28 *
29 * citrus Id: wcsncpy.c,v 1.1 1999/12/29 21:47:45 tshiozak Exp
30 */
31
32#include <wchar.h>
33
34wchar_t *
35wcsncpy(wchar_t *s1, const wchar_t *s2, size_t n)
36{
37 wchar_t *p;
38
39 p = s1;
40 while (n && *s2) {
41 *p++ = *s2++;
42 n--;
43 }
44 while (n) {
45 *p++ = L'\0';
46 n--;
47 }
48
49 return s1;
50}
51DEF_STRONG(wcsncpy);
diff --git a/src/lib/libc/string/wcsnlen.c b/src/lib/libc/string/wcsnlen.c
deleted file mode 100644
index eb1d08e85b..0000000000
--- a/src/lib/libc/string/wcsnlen.c
+++ /dev/null
@@ -1,45 +0,0 @@
1/* $OpenBSD: wcsnlen.c,v 1.1 2024/07/14 09:51:18 jca Exp $ */
2/* $NetBSD: wcslen.c,v 1.2 2001/01/03 14:29:36 lukem Exp $ */
3
4/*-
5 * Copyright (c)1999 Citrus Project,
6 * All rights reserved.
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 *
17 * THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND
18 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
19 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
20 * ARE DISCLAIMED. IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE
21 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
22 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
23 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
24 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
25 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
26 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
27 * SUCH DAMAGE.
28 *
29 * citrus Id: wcslen.c,v 1.1 1999/12/29 21:47:45 tshiozak Exp
30 */
31
32#include <wchar.h>
33
34size_t
35wcsnlen(const wchar_t *s, size_t maxlen)
36{
37 const wchar_t *p;
38
39 p = s;
40 while (maxlen-- && *p)
41 p++;
42
43 return p - s;
44}
45
diff --git a/src/lib/libc/string/wcspbrk.3 b/src/lib/libc/string/wcspbrk.3
deleted file mode 100644
index 602bfdc3d6..0000000000
--- a/src/lib/libc/string/wcspbrk.3
+++ /dev/null
@@ -1,81 +0,0 @@
1.\" $OpenBSD: wcspbrk.3,v 1.3 2013/06/05 03:39:23 tedu Exp $
2.\"
3.\" Copyright (c) 1990, 1991 The Regents of the University of California.
4.\" All rights reserved.
5.\"
6.\" This code is derived from software contributed to Berkeley by
7.\" Chris Torek and the American National Standards Committee X3,
8.\" on Information Processing Systems.
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. 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.Dd $Mdocdate: June 5 2013 $
35.Dt WCSPBRK 3
36.Os
37.Sh NAME
38.Nm wcspbrk
39.Nd locate multiple wide characters in a wide string
40.Sh SYNOPSIS
41.In wchar.h
42.Ft wchar_t *
43.Fn wcspbrk "const wchar_t *s" "const wchar_t *charset"
44.Sh DESCRIPTION
45The
46.Fn wcspbrk
47function locates in the wide string
48.Fa s
49the first occurrence of any wide character in the wide string
50.Fa charset
51and returns a pointer to this wide character.
52If no wide characters from
53.Fa charset
54occur anywhere in
55.Fa s ,
56.Fn wcspbrk
57returns
58.Dv NULL .
59.Sh SEE ALSO
60.Xr strpbrk 3 ,
61.Xr wcschr 3 ,
62.Xr wcscspn 3 ,
63.Xr wcsrchr 3 ,
64.Xr wcsspn 3 ,
65.Xr wcsstr 3 ,
66.Xr wcstok 3 ,
67.Xr wmemchr 3
68.Sh STANDARDS
69The
70.Fn wcspbrk
71function conforms to
72.St -isoC-99
73and was first introduced in
74.St -isoC-amd1 .
75.Sh HISTORY
76The
77.Fn wcspbrk
78function was ported from
79.Nx
80and first appeared in
81.Ox 3.8 .
diff --git a/src/lib/libc/string/wcspbrk.c b/src/lib/libc/string/wcspbrk.c
deleted file mode 100644
index f398fd92ab..0000000000
--- a/src/lib/libc/string/wcspbrk.c
+++ /dev/null
@@ -1,53 +0,0 @@
1/* $OpenBSD: wcspbrk.c,v 1.5 2015/10/01 02:32:07 guenther Exp $ */
2/* $NetBSD: wcspbrk.c,v 1.2 2001/01/03 14:29:37 lukem Exp $ */
3
4/*-
5 * Copyright (c)1999 Citrus Project,
6 * All rights reserved.
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 *
17 * THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND
18 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
19 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
20 * ARE DISCLAIMED. IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE
21 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
22 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
23 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
24 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
25 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
26 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
27 * SUCH DAMAGE.
28 *
29 * citrus Id: wcspbrk.c,v 1.2 2000/12/21 05:07:25 itojun Exp
30 */
31
32#include <wchar.h>
33
34wchar_t *
35wcspbrk(const wchar_t *s, const wchar_t *set)
36{
37 const wchar_t *p;
38 const wchar_t *q;
39
40 p = s;
41 while (*p) {
42 q = set;
43 while (*q) {
44 if (*p == *q) {
45 return (wchar_t *)p;
46 }
47 q++;
48 }
49 p++;
50 }
51 return NULL;
52}
53DEF_STRONG(wcspbrk);
diff --git a/src/lib/libc/string/wcsrchr.3 b/src/lib/libc/string/wcsrchr.3
deleted file mode 100644
index d4a2e7cf78..0000000000
--- a/src/lib/libc/string/wcsrchr.3
+++ /dev/null
@@ -1,85 +0,0 @@
1.\" $OpenBSD: wcsrchr.3,v 1.3 2013/06/05 03:39:23 tedu Exp $
2.\"
3.\" Copyright (c) 1990, 1991 The Regents of the University of California.
4.\" All rights reserved.
5.\"
6.\" This code is derived from software contributed to Berkeley by
7.\" Chris Torek and the American National Standards Committee X3,
8.\" on Information Processing Systems.
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. 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.Dd $Mdocdate: June 5 2013 $
35.Dt WCSRCHR 3
36.Os
37.Sh NAME
38.Nm wcsrchr
39.Nd locate last occurrence of a wide character in a wide string
40.Sh SYNOPSIS
41.In wchar.h
42.Ft wchar_t *
43.Fn wcsrchr "const wchar_t *s" "wchar_t c"
44.Sh DESCRIPTION
45The
46.Fn wcsrchr
47function locates the last occurrence of the wide character
48.Fa c
49in the wide string
50.Fa s .
51The terminating null wide character is considered part of the wide string.
52If
53.Fa c
54is the null wide character (L'\e0'),
55.Fn wcsrchr
56locates the terminating null wide character.
57.Sh RETURN VALUES
58The
59.Fn wcsrchr
60function returns a pointer to the located wide character or
61.Dv NULL
62if the wide character does not appear in the wide string.
63.Sh SEE ALSO
64.Xr strrchr 3 ,
65.Xr wcschr 3 ,
66.Xr wcscspn 3 ,
67.Xr wcspbrk 3 ,
68.Xr wcsspn 3 ,
69.Xr wcsstr 3 ,
70.Xr wcstok 3 ,
71.Xr wmemchr 3
72.Sh STANDARDS
73The
74.Fn wcsrchr
75function conforms to
76.St -isoC-99
77and was first introduced in
78.St -isoC-amd1 .
79.Sh HISTORY
80The
81.Fn wcsrchr
82function was ported from
83.Nx
84and first appeared in
85.Ox 3.8 .
diff --git a/src/lib/libc/string/wcsrchr.c b/src/lib/libc/string/wcsrchr.c
deleted file mode 100644
index aa936de508..0000000000
--- a/src/lib/libc/string/wcsrchr.c
+++ /dev/null
@@ -1,50 +0,0 @@
1/* $OpenBSD: wcsrchr.c,v 1.5 2015/10/01 02:32:07 guenther Exp $ */
2/* $NetBSD: wcsrchr.c,v 1.2 2001/01/03 14:29:37 lukem Exp $ */
3
4/*-
5 * Copyright (c)1999 Citrus Project,
6 * All rights reserved.
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 *
17 * THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND
18 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
19 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
20 * ARE DISCLAIMED. IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE
21 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
22 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
23 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
24 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
25 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
26 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
27 * SUCH DAMAGE.
28 *
29 * citrus Id: wcsrchr.c,v 1.2 2000/12/21 05:07:25 itojun Exp
30 */
31
32#include <wchar.h>
33
34wchar_t *
35wcsrchr(const wchar_t *s, wchar_t c)
36{
37 const wchar_t *p;
38
39 p = s;
40 while (*p)
41 p++;
42 while (s <= p) {
43 if (*p == c) {
44 return (wchar_t *)p;
45 }
46 p--;
47 }
48 return NULL;
49}
50DEF_STRONG(wcsrchr);
diff --git a/src/lib/libc/string/wcsspn.3 b/src/lib/libc/string/wcsspn.3
deleted file mode 100644
index 3be82344dd..0000000000
--- a/src/lib/libc/string/wcsspn.3
+++ /dev/null
@@ -1,79 +0,0 @@
1.\" $OpenBSD: wcsspn.3,v 1.3 2013/06/05 03:39:23 tedu Exp $
2.\"
3.\" Copyright (c) 1990, 1991 The Regents of the University of California.
4.\" All rights reserved.
5.\"
6.\" This code is derived from software contributed to Berkeley by
7.\" Chris Torek and the American National Standards Committee X3,
8.\" on Information Processing Systems.
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. 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.Dd $Mdocdate: June 5 2013 $
35.Dt WCSSPN 3
36.Os
37.Sh NAME
38.Nm wcsspn
39.Nd span a wide string
40.Sh SYNOPSIS
41.In wchar.h
42.Ft size_t
43.Fn wcsspn "const wchar_t *s" "const wchar_t *charset"
44.Sh DESCRIPTION
45The
46.Fn wcsspn
47function spans the initial part of the wide string
48.Fa s
49as long as the wide characters from
50.Fa s
51occur in the wide string
52.Fa charset .
53.Sh RETURN VALUES
54The
55.Fn wcsspn
56function returns the number of wide characters spanned.
57.Sh SEE ALSO
58.Xr strspn 3 ,
59.Xr wcschr 3 ,
60.Xr wcscspn 3 ,
61.Xr wcspbrk 3 ,
62.Xr wcsrchr 3 ,
63.Xr wcsstr 3 ,
64.Xr wcstok 3 ,
65.Xr wmemchr 3
66.Sh STANDARDS
67The
68.Fn wcsspn
69function conforms to
70.St -isoC-99
71and was first introduced in
72.St -isoC-amd1 .
73.Sh HISTORY
74The
75.Fn wcsspn
76function was ported from
77.Nx
78and first appeared in
79.Ox 3.8 .
diff --git a/src/lib/libc/string/wcsspn.c b/src/lib/libc/string/wcsspn.c
deleted file mode 100644
index a3f28e3a49..0000000000
--- a/src/lib/libc/string/wcsspn.c
+++ /dev/null
@@ -1,56 +0,0 @@
1/* $OpenBSD: wcsspn.c,v 1.4 2015/09/12 16:23:14 guenther Exp $ */
2/* $NetBSD: wcsspn.c,v 1.3 2001/09/21 16:09:15 yamt Exp $ */
3
4/*-
5 * Copyright (c)1999,2001 Citrus Project,
6 * All rights reserved.
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 *
17 * THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND
18 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
19 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
20 * ARE DISCLAIMED. IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE
21 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
22 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
23 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
24 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
25 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
26 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
27 * SUCH DAMAGE.
28 *
29 * $Citrus: xpg4dl/FreeBSD/lib/libc/string/wcsspn.c,v 1.3 2001/09/21 16:06:43 yamt Exp $
30 */
31
32#include <wchar.h>
33
34size_t
35wcsspn(const wchar_t *s, const wchar_t *set)
36{
37 const wchar_t *p;
38 const wchar_t *q;
39
40 p = s;
41 while (*p) {
42 q = set;
43 while (*q) {
44 if (*p == *q)
45 break;
46 q++;
47 }
48 if (!*q)
49 goto done;
50 p++;
51 }
52
53done:
54 return (p - s);
55}
56DEF_STRONG(wcsspn);
diff --git a/src/lib/libc/string/wcsstr.3 b/src/lib/libc/string/wcsstr.3
deleted file mode 100644
index 203a76ded3..0000000000
--- a/src/lib/libc/string/wcsstr.3
+++ /dev/null
@@ -1,88 +0,0 @@
1.\" $OpenBSD: wcsstr.3,v 1.3 2013/06/05 03:39:23 tedu Exp $
2.\"
3.\" Copyright (c) 1990, 1991 The Regents of the University of California.
4.\" All rights reserved.
5.\"
6.\" This code is derived from software contributed to Berkeley by
7.\" Chris Torek and the American National Standards Committee X3,
8.\" on Information Processing Systems.
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. 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.Dd $Mdocdate: June 5 2013 $
35.Dt WCSSTR 3
36.Os
37.Sh NAME
38.Nm wcsstr
39.Nd locate a wide substring in a wide string
40.Sh SYNOPSIS
41.In wchar.h
42.Ft wchar_t *
43.Fn wcsstr "const wchar_t *big" "const wchar_t *little"
44.Sh DESCRIPTION
45The
46.Fn wcsstr
47function locates the first occurrence of the wide string
48.Fa little
49in the wide string
50.Fa big .
51.Pp
52If
53.Fa little
54is an empty wide string,
55.Fa big
56is returned;
57if
58.Fa little
59occurs nowhere in
60.Fa big ,
61.Dv NULL
62is returned;
63otherwise a pointer to the first wide character of the first occurrence of
64.Fa little
65is returned.
66.Sh SEE ALSO
67.Xr strstr 3 ,
68.Xr wcschr 3 ,
69.Xr wcscspn 3 ,
70.Xr wcspbrk 3 ,
71.Xr wcsrchr 3 ,
72.Xr wcsspn 3 ,
73.Xr wcstok 3 ,
74.Xr wmemchr 3
75.Sh STANDARDS
76The
77.Fn wcsstr
78function conforms to
79.St -isoC-99
80and was first introduced in
81.St -isoC-amd1 .
82.Sh HISTORY
83The
84.Fn wcsstr
85function was ported from
86.Nx
87and first appeared in
88.Ox 3.8 .
diff --git a/src/lib/libc/string/wcsstr.c b/src/lib/libc/string/wcsstr.c
deleted file mode 100644
index 6a7b0dac4a..0000000000
--- a/src/lib/libc/string/wcsstr.c
+++ /dev/null
@@ -1,71 +0,0 @@
1/* $OpenBSD: wcsstr.c,v 1.5 2015/10/01 02:32:07 guenther Exp $ */
2/* $NetBSD: wcsstr.c,v 1.3 2003/03/05 20:18:17 tshiozak Exp $ */
3
4/*-
5 * Copyright (c)1999 Citrus Project,
6 * All rights reserved.
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 *
17 * THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND
18 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
19 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
20 * ARE DISCLAIMED. IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE
21 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
22 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
23 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
24 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
25 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
26 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
27 * SUCH DAMAGE.
28 *
29 * citrus Id: wcsstr.c,v 1.2 2000/12/21 05:07:25 itojun Exp
30 */
31
32#include <wchar.h>
33
34wchar_t *
35#ifdef WCSWCS
36wcswcs(const wchar_t *big, const wchar_t *little)
37#else
38wcsstr(const wchar_t *big, const wchar_t *little)
39#endif
40{
41 const wchar_t *p;
42 const wchar_t *q;
43 const wchar_t *r;
44
45 if (!*little) {
46 return (wchar_t *)big;
47 }
48 if (wcslen(big) < wcslen(little))
49 return NULL;
50
51 p = big;
52 q = little;
53 while (*p) {
54 q = little;
55 r = p;
56 while (*q) {
57 if (*r != *q)
58 break;
59 q++;
60 r++;
61 }
62 if (!*q) {
63 return (wchar_t *)p;
64 }
65 p++;
66 }
67 return NULL;
68}
69#ifndef WCSWCS
70DEF_STRONG(wcsstr);
71#endif
diff --git a/src/lib/libc/string/wcstok.3 b/src/lib/libc/string/wcstok.3
deleted file mode 100644
index 33ba1318a1..0000000000
--- a/src/lib/libc/string/wcstok.3
+++ /dev/null
@@ -1,151 +0,0 @@
1.\" $OpenBSD: wcstok.3,v 1.7 2011/07/25 00:38:53 schwarze Exp $
2.\"
3.\" $NetBSD: wcstok.3,v 1.3 2003/09/08 17:54:33 wiz Exp $
4.\"
5.\" Copyright (c) 1998 Softweyr LLC. All rights reserved.
6.\"
7.\" strtok_r, from Berkeley strtok
8.\" Oct 13, 1998 by Wes Peters <wes@softweyr.com>
9.\"
10.\" Copyright (c) 1988, 1991, 1993
11.\" The Regents of the University of California. All rights reserved.
12.\"
13.\" This code is derived from software contributed to Berkeley by
14.\" the American National Standards Committee X3, on Information
15.\" Processing Systems.
16.\"
17.\" Redistribution and use in source and binary forms, with or without
18.\" modification, are permitted provided that the following conditions
19.\" are met:
20.\"
21.\" 1. Redistributions of source code must retain the above copyright
22.\" notices, this list of conditions and the following disclaimer.
23.\"
24.\" 2. Redistributions in binary form must reproduce the above
25.\" copyright notices, this list of conditions and the following
26.\" disclaimer in the documentation and/or other materials provided
27.\" with the distribution.
28.\"
29.\" 3. All advertising materials mentioning features or use of this
30.\" software must display the following acknowledgement:
31.\"
32.\" This product includes software developed by Softweyr LLC, the
33.\" University of California, Berkeley, and its contributors.
34.\"
35.\" 4. Neither the name of Softweyr LLC, the University nor the names
36.\" of its contributors may be used to endorse or promote products
37.\" derived from this software without specific prior written
38.\" permission.
39.\"
40.\" THIS SOFTWARE IS PROVIDED BY SOFTWEYR LLC, THE REGENTS AND
41.\" CONTRIBUTORS ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES,
42.\" INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF
43.\" MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
44.\" DISCLAIMED. IN NO EVENT SHALL SOFTWEYR LLC, THE REGENTS, OR
45.\" CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
46.\" SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
47.\" LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF
48.\" USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND
49.\" ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY,
50.\" OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT
51.\" OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
52.\" SUCH DAMAGE.
53.\"
54.\" Original version ID:
55.\" FreeBSD: src/lib/libc/string/wcstok.3,v 1.4 2002/10/15 09:49:54 tjr Exp
56.\"
57.Dd $Mdocdate: July 25 2011 $
58.Dt WCSTOK 3
59.Os
60.Sh NAME
61.Nm wcstok
62.Nd split wide-character string into tokens
63.Sh SYNOPSIS
64.In wchar.h
65.Ft wchar_t *
66.Fn wcstok "wchar_t * restrict str" "const wchar_t * restrict sep" "wchar_t ** restrict last"
67.Sh DESCRIPTION
68The
69.Fn wcstok
70function
71is used to isolate sequential tokens in a NUL-terminated wide-character
72string,
73.Fa str .
74These tokens are separated in the string by at least one of the
75characters in
76.Fa sep .
77The first time that
78.Fn wcstok
79is called,
80.Fa str
81should be specified; subsequent calls, wishing to obtain further tokens
82from the same string, should pass a null pointer instead.
83The separator string,
84.Fa sep ,
85must be supplied each time, and may change between calls.
86The context pointer
87.Fa last
88must be provided on each call.
89.Pp
90The
91.Fn wcstok
92function is the wide-character counterpart of the
93.Xr strtok_r 3
94function.
95.Pp
96Since
97.Fn wcstok
98modifies the string,
99.Fa str
100should not point to an area in the initialized data segment.
101.Sh RETURN VALUES
102The
103.Fn wcstok
104function
105returns a pointer to the beginning of each subsequent token in the string,
106after replacing the token itself with a NUL wide character (L'\e0').
107When no more tokens remain, a null pointer is returned.
108.Sh EXAMPLES
109The following code fragment splits a wide-character string on
110.Tn ASCII
111space, tab, and newline characters and writes the tokens to
112standard output:
113.Bd -literal -offset indent
114const wchar_t *seps = L" \et\en";
115wchar_t *last, *tok, text[] = L" \enone\ettwo\et\etthree \en";
116
117for (tok = wcstok(text, seps, &last); tok != NULL;
118 tok = wcstok(NULL, seps, &last))
119 wprintf(L"%ls\en", tok);
120.Ed
121.Sh SEE ALSO
122.Xr strtok 3 ,
123.Xr wcschr 3 ,
124.Xr wcscspn 3 ,
125.Xr wcspbrk 3 ,
126.Xr wcsrchr 3 ,
127.Xr wcsspn 3 ,
128.Xr wcsstr 3 ,
129.Xr wmemchr 3
130.Sh STANDARDS
131The
132.Fn wcstok
133function
134conforms to
135.St -isoC-99 .
136.Sh HISTORY
137The
138.Fn wcstok
139function was ported from
140.Nx
141and first appeared in
142.Ox 3.8 .
143.Pp
144Some early implementations of
145.Fn wcstok
146omit the
147context pointer argument,
148.Fa last ,
149and maintain state across calls in a static variable like
150.Xr strtok 3
151does.
diff --git a/src/lib/libc/string/wcstok.c b/src/lib/libc/string/wcstok.c
deleted file mode 100644
index bc1ac3e8cb..0000000000
--- a/src/lib/libc/string/wcstok.c
+++ /dev/null
@@ -1,93 +0,0 @@
1/* $OpenBSD: wcstok.c,v 1.3 2005/08/08 08:05:37 espie Exp $ */
2/* $NetBSD: wcstok.c,v 1.3 2003/07/10 08:50:48 tshiozak Exp $ */
3
4/*-
5 * Copyright (c) 1998 Softweyr LLC. All rights reserved.
6 *
7 * strtok_r, from Berkeley strtok
8 * Oct 13, 1998 by Wes Peters <wes@softweyr.com>
9 *
10 * Copyright (c) 1988, 1993
11 * The Regents of the University of California. All rights reserved.
12 *
13 * Redistribution and use in source and binary forms, with or without
14 * modification, are permitted provided that the following conditions
15 * are met:
16 * 1. Redistributions of source code must retain the above copyright
17 * notices, this list of conditions and the following disclaimer.
18 * 2. Redistributions in binary form must reproduce the above copyright
19 * notices, this list of conditions and the following disclaimer in the
20 * documentation and/or other materials provided with the distribution.
21 * 3. All advertising materials mentioning features or use of this software
22 * must display the following acknowledgement:
23 * This product includes software developed by Softweyr LLC, the
24 * University of California, Berkeley, and its contributors.
25 * 4. Neither the name of the University nor the names of its contributors
26 * may be used to endorse or promote products derived from this software
27 * without specific prior written permission.
28 *
29 * THIS SOFTWARE IS PROVIDED BY SOFTWEYR LLC, THE REGENTS AND CONTRIBUTORS
30 * ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
31 * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A
32 * PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL SOFTWEYR LLC, THE
33 * REGENTS, OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
34 * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED
35 * TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR
36 * PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF
37 * LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING
38 * NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
39 * SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
40 *
41 * Original version ID:
42 * FreeBSD: src/lib/libc/string/wcstok.c,v 1.1 2002/09/07 08:16:57 tjr Exp
43 */
44
45#include <wchar.h>
46
47wchar_t *
48wcstok(wchar_t * __restrict s, const wchar_t * __restrict delim,
49 wchar_t ** __restrict last)
50{
51 const wchar_t *spanp;
52 wchar_t c, sc;
53 wchar_t *tok;
54
55 if (s == NULL && (s = *last) == NULL)
56 return (NULL);
57
58 /*
59 * Skip (span) leading delimiters (s += wcsspn(s, delim), sort of).
60 */
61cont:
62 c = *s++;
63 for (spanp = delim; (sc = *spanp++) != L'\0';) {
64 if (c == sc)
65 goto cont;
66 }
67
68 if (c == L'\0') { /* no non-delimiter characters */
69 *last = NULL;
70 return (NULL);
71 }
72 tok = s - 1;
73
74 /*
75 * Scan token (scan for delimiters: s += wcscspn(s, delim), sort of).
76 * Note that delim must have one NUL; we stop if we see that, too.
77 */
78 for (;;) {
79 c = *s++;
80 spanp = delim;
81 do {
82 if ((sc = *spanp++) == c) {
83 if (c == L'\0')
84 s = NULL;
85 else
86 s[-1] = L'\0';
87 *last = s;
88 return (tok);
89 }
90 } while (sc != L'\0');
91 }
92 /* NOTREACHED */
93}
diff --git a/src/lib/libc/string/wcswcs.c b/src/lib/libc/string/wcswcs.c
deleted file mode 100644
index bd35605547..0000000000
--- a/src/lib/libc/string/wcswcs.c
+++ /dev/null
@@ -1,5 +0,0 @@
1/* $OpenBSD: wcswcs.c,v 1.1 2005/04/13 16:35:58 espie Exp $ */
2/* $NetBSD: wcswcs.c,v 1.1 2003/03/05 20:18:17 tshiozak Exp $ */
3
4#define WCSWCS
5#include "wcsstr.c"
diff --git a/src/lib/libc/string/wcswidth.3 b/src/lib/libc/string/wcswidth.3
deleted file mode 100644
index a2a5b2df86..0000000000
--- a/src/lib/libc/string/wcswidth.3
+++ /dev/null
@@ -1,69 +0,0 @@
1.\" $OpenBSD: wcswidth.3,v 1.2 2011/07/25 00:38:53 schwarze Exp $
2.\"
3.\" Copyright (c) 2002 Tim J. Robbins
4.\" 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.\"
15.\" THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND
16.\" ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
17.\" IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
18.\" ARE DISCLAIMED. IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE
19.\" FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
20.\" DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
21.\" OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
22.\" HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
23.\" LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
24.\" OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
25.\" SUCH DAMAGE.
26.\"
27.Dd $Mdocdate: July 25 2011 $
28.Dt WCSWIDTH 3
29.Os
30.Sh NAME
31.Nm wcswidth
32.Nd number of column positions in wide-character string
33.Sh SYNOPSIS
34.In wchar.h
35.Ft int
36.Fn wcswidth "const wchar_t *pwcs" "size_t n"
37.Sh DESCRIPTION
38The
39.Fn wcswidth
40function determines the number of column positions required for the first
41.Fa n
42characters of
43.Fa pwcs ,
44or until a null wide character (L'\e0') is encountered.
45.Sh RETURN VALUES
46The
47.Fn wcswidth
48function returns 0 if
49.Fa pwcs
50is an empty string (L""),
51\-1 if a non-printing wide character is encountered,
52otherwise it returns the number of column positions occupied.
53.Sh SEE ALSO
54.Xr iswprint 3 ,
55.Xr strlen 3 ,
56.Xr wcslen 3 ,
57.Xr wcwidth 3
58.Sh STANDARDS
59The
60.Fn wcswidth
61function conforms to
62.St -p1003.1-2001 .
63.Sh HISTORY
64The
65.Fn wcswidth
66function was ported from
67.Nx
68and first appeared in
69.Ox 3.8 .
diff --git a/src/lib/libc/string/wcswidth.c b/src/lib/libc/string/wcswidth.c
deleted file mode 100644
index 9f003f96c7..0000000000
--- a/src/lib/libc/string/wcswidth.c
+++ /dev/null
@@ -1,51 +0,0 @@
1/* $OpenBSD: wcswidth.c,v 1.5 2015/09/12 16:23:14 guenther Exp $ */
2/* $NetBSD: wcswidth.c,v 1.2 2001/01/03 14:29:37 lukem Exp $ */
3
4/*-
5 * Copyright (c)1999 Citrus Project,
6 * All rights reserved.
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 *
17 * THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND
18 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
19 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
20 * ARE DISCLAIMED. IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE
21 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
22 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
23 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
24 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
25 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
26 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
27 * SUCH DAMAGE.
28 *
29 * citrus Id: wcswidth.c,v 1.1 1999/12/29 21:47:45 tshiozak Exp
30 */
31
32#include <wchar.h>
33
34int
35wcswidth(const wchar_t *s, size_t n)
36{
37 int w, q;
38
39 w = 0;
40 while (n && *s) {
41 q = wcwidth(*s);
42 if (q == -1)
43 return (-1);
44 w += q;
45 s++;
46 n--;
47 }
48
49 return w;
50}
51DEF_WEAK(wcswidth);
diff --git a/src/lib/libc/string/wmemchr.3 b/src/lib/libc/string/wmemchr.3
deleted file mode 100644
index 17fbc9dbc5..0000000000
--- a/src/lib/libc/string/wmemchr.3
+++ /dev/null
@@ -1,81 +0,0 @@
1.\" $OpenBSD: wmemchr.3,v 1.10 2013/06/05 03:39:23 tedu Exp $
2.\"
3.\" Copyright (c) 1990, 1991 The Regents of the University of California.
4.\" All rights reserved.
5.\"
6.\" This code is derived from software contributed to Berkeley by
7.\" Chris Torek and the American National Standards Committee X3,
8.\" on Information Processing Systems.
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. 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.Dd $Mdocdate: June 5 2013 $
35.Dt WMEMCHR 3
36.Os
37.Sh NAME
38.Nm wmemchr
39.Nd locate wide character in wide string
40.Sh SYNOPSIS
41.In wchar.h
42.Ft wchar_t *
43.Fn wmemchr "const wchar_t *b" "wchar_t c" "size_t len"
44.Sh DESCRIPTION
45The
46.Fn wmemchr
47function locates the first occurrence of
48.Fa c
49in wide string
50.Fa b .
51.Sh RETURN VALUES
52The
53.Fn wmemchr
54function returns a pointer to the wide character located, or
55.Dv NULL
56if no such wide character exists within
57.Fa len
58wide characters.
59.Sh SEE ALSO
60.Xr memchr 3 ,
61.Xr wcschr 3 ,
62.Xr wcscspn 3 ,
63.Xr wcspbrk 3 ,
64.Xr wcsrchr 3 ,
65.Xr wcsspn 3 ,
66.Xr wcsstr 3 ,
67.Xr wcstok 3
68.Sh STANDARDS
69The
70.Fn wmemchr
71function conforms to
72.St -isoC-99
73and was first introduced in
74.St -isoC-amd1 .
75.Sh HISTORY
76The
77.Fn wmemchr
78function was ported from
79.Nx
80and first appeared in
81.Ox 3.8 .
diff --git a/src/lib/libc/string/wmemchr.c b/src/lib/libc/string/wmemchr.c
deleted file mode 100644
index 556a0b35cc..0000000000
--- a/src/lib/libc/string/wmemchr.c
+++ /dev/null
@@ -1,47 +0,0 @@
1/* $OpenBSD: wmemchr.c,v 1.5 2015/10/01 02:32:07 guenther Exp $ */
2/* $NetBSD: wmemchr.c,v 1.2 2001/01/03 14:29:37 lukem Exp $ */
3
4/*-
5 * Copyright (c)1999 Citrus Project,
6 * All rights reserved.
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 *
17 * THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND
18 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
19 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
20 * ARE DISCLAIMED. IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE
21 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
22 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
23 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
24 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
25 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
26 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
27 * SUCH DAMAGE.
28 *
29 * citrus Id: wmemchr.c,v 1.2 2000/12/20 14:08:31 itojun Exp
30 */
31
32#include <wchar.h>
33
34wchar_t *
35wmemchr(const wchar_t *s, wchar_t c, size_t n)
36{
37 size_t i;
38
39 for (i = 0; i < n; i++) {
40 if (*s == c) {
41 return (wchar_t *)s;
42 }
43 s++;
44 }
45 return NULL;
46}
47DEF_STRONG(wmemchr);
diff --git a/src/lib/libc/string/wmemcmp.3 b/src/lib/libc/string/wmemcmp.3
deleted file mode 100644
index bd8ddfd120..0000000000
--- a/src/lib/libc/string/wmemcmp.3
+++ /dev/null
@@ -1,78 +0,0 @@
1.\" $OpenBSD: wmemcmp.3,v 1.3 2013/06/05 03:39:23 tedu Exp $
2.\"
3.\" Copyright (c) 1990, 1991 The Regents of the University of California.
4.\" All rights reserved.
5.\"
6.\" This code is derived from software contributed to Berkeley by
7.\" Chris Torek and the American National Standards Committee X3,
8.\" on Information Processing Systems.
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. 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.Dd $Mdocdate: June 5 2013 $
35.Dt WMEMCMP 3
36.Os
37.Sh NAME
38.Nm wmemcmp
39.Nd compare wide strings
40.Sh SYNOPSIS
41.In wchar.h
42.Ft int
43.Fn wmemcmp "const wchar_t *s1" "const wchar_t *s2" "size_t len"
44.Sh DESCRIPTION
45The
46.Fn wmemcmp
47function compares the wide string
48.Fa s1
49against the wide string
50.Fa s2 .
51Both wide strings are assumed to be
52.Fa len
53wide characters long.
54.Sh RETURN VALUES
55The
56.Fn wmemcmp
57function returns zero if the two wide strings are identical,
58otherwise the difference between the first two differing wide characters is
59returned.
60Zero-length wide strings are always identical.
61.Sh SEE ALSO
62.Xr memcmp 3 ,
63.Xr wcscasecmp 3 ,
64.Xr wcscmp 3
65.Sh STANDARDS
66The
67.Fn wmemcmp
68function conforms to
69.St -isoC-99
70and was first introduced in
71.St -isoC-amd1 .
72.Sh HISTORY
73The
74.Fn wmemcmp
75function was ported from
76.Nx
77and first appeared in
78.Ox 3.8 .
diff --git a/src/lib/libc/string/wmemcmp.c b/src/lib/libc/string/wmemcmp.c
deleted file mode 100644
index 46617e29cf..0000000000
--- a/src/lib/libc/string/wmemcmp.c
+++ /dev/null
@@ -1,51 +0,0 @@
1/* $OpenBSD: wmemcmp.c,v 1.5 2015/09/12 16:23:14 guenther Exp $ */
2/* $NetBSD: wmemcmp.c,v 1.3 2003/04/06 18:33:23 tshiozak Exp $ */
3
4/*-
5 * Copyright (c)1999 Citrus Project,
6 * All rights reserved.
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 *
17 * THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND
18 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
19 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
20 * ARE DISCLAIMED. IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE
21 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
22 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
23 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
24 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
25 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
26 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
27 * SUCH DAMAGE.
28 *
29 * citrus Id: wmemcmp.c,v 1.2 2000/12/20 14:08:31 itojun Exp
30 */
31
32#include <wchar.h>
33#include "locale/runetype.h"
34
35int
36wmemcmp(const wchar_t *s1, const wchar_t *s2, size_t n)
37{
38 size_t i;
39
40 for (i = 0; i < n; i++) {
41 if (*s1 != *s2) {
42 /* wchar might be unsigned */
43 return *(const rune_t *)s1 >
44 *(const rune_t *)s2 ? 1 : -1;
45 }
46 s1++;
47 s2++;
48 }
49 return 0;
50}
51DEF_STRONG(wmemcmp);
diff --git a/src/lib/libc/string/wmemcpy.3 b/src/lib/libc/string/wmemcpy.3
deleted file mode 100644
index 2a6d528a1a..0000000000
--- a/src/lib/libc/string/wmemcpy.3
+++ /dev/null
@@ -1,79 +0,0 @@
1.\" $OpenBSD: wmemcpy.3,v 1.5 2013/06/05 03:39:23 tedu Exp $
2.\"
3.\" Copyright (c) 1990, 1991 The Regents of the University of California.
4.\" All rights reserved.
5.\"
6.\" This code is derived from software contributed to Berkeley by
7.\" Chris Torek and the American National Standards Committee X3,
8.\" on Information Processing Systems.
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. 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.Dd $Mdocdate: June 5 2013 $
35.Dt WMEMCPY 3
36.Os
37.Sh NAME
38.Nm wmemcpy
39.Nd copy wide characters
40.Sh SYNOPSIS
41.In wchar.h
42.Ft wchar_t *
43.Fn wmemcpy "wchar_t * restrict dst" "const wchar_t * restrict src" "size_t len"
44.Sh DESCRIPTION
45The
46.Fn wmemcpy
47function copies
48.Fa len
49wide characters from buffer
50.Fa src
51to buffer
52.Fa dst .
53If the two buffers may overlap,
54.Xr wmemmove 3
55must be used instead.
56.Sh RETURN VALUES
57The
58.Fn wmemcpy
59function returns the original value of
60.Fa dst .
61.Sh SEE ALSO
62.Xr memcpy 3 ,
63.Xr wcscpy 3 ,
64.Xr wcslcpy 3 ,
65.Xr wmemmove 3
66.Sh STANDARDS
67The
68.Fn wmemcpy
69function conforms to
70.St -isoC-99
71and was first introduced in
72.St -isoC-amd1 .
73.Sh HISTORY
74The
75.Fn wmemcpy
76function was ported from
77.Nx
78and first appeared in
79.Ox 3.8 .
diff --git a/src/lib/libc/string/wmemcpy.c b/src/lib/libc/string/wmemcpy.c
deleted file mode 100644
index cf02ab9d56..0000000000
--- a/src/lib/libc/string/wmemcpy.c
+++ /dev/null
@@ -1,41 +0,0 @@
1/* $OpenBSD: wmemcpy.c,v 1.4 2015/09/12 16:23:14 guenther Exp $ */
2/* $NetBSD: wmemcpy.c,v 1.2 2001/01/03 14:29:37 lukem Exp $ */
3
4/*-
5 * Copyright (c)1999 Citrus Project,
6 * All rights reserved.
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 *
17 * THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND
18 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
19 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
20 * ARE DISCLAIMED. IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE
21 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
22 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
23 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
24 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
25 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
26 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
27 * SUCH DAMAGE.
28 *
29 * citrus Id: wmemcpy.c,v 1.2 2000/12/20 14:08:31 itojun Exp
30 */
31
32#include <string.h>
33#include <wchar.h>
34
35wchar_t *
36wmemcpy(wchar_t *d, const wchar_t *s, size_t n)
37{
38
39 return (wchar_t *)memcpy(d, s, n * sizeof(wchar_t));
40}
41DEF_STRONG(wmemcpy);
diff --git a/src/lib/libc/string/wmemmove.3 b/src/lib/libc/string/wmemmove.3
deleted file mode 100644
index 46942e7928..0000000000
--- a/src/lib/libc/string/wmemmove.3
+++ /dev/null
@@ -1,78 +0,0 @@
1.\" $OpenBSD: wmemmove.3,v 1.3 2013/06/05 03:39:23 tedu Exp $
2.\"
3.\" Copyright (c) 1990, 1991 The Regents of the University of California.
4.\" All rights reserved.
5.\"
6.\" This code is derived from software contributed to Berkeley by
7.\" Chris Torek and the American National Standards Committee X3,
8.\" on Information Processing Systems.
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. 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.Dd $Mdocdate: June 5 2013 $
35.Dt WMEMMOVE 3
36.Os
37.Sh NAME
38.Nm wmemmove
39.Nd copy wide characters
40.Sh SYNOPSIS
41.In wchar.h
42.Ft wchar_t *
43.Fn wmemmove "wchar_t *dst" "const wchar_t *src" "size_t len"
44.Sh DESCRIPTION
45The
46.Fn wmemmove
47function copies
48.Fa len
49wide characters from buffer
50.Fa src
51to buffer
52.Fa dst .
53The two buffers may overlap;
54the copy is always done in a non-destructive manner.
55.Sh RETURN VALUES
56The
57.Fn wmemmove
58function returns the original value of
59.Fa dst .
60.Sh SEE ALSO
61.Xr memmove 3 ,
62.Xr wcscpy 3 ,
63.Xr wcslcpy 3 ,
64.Xr wmemcpy 3
65.Sh STANDARDS
66The
67.Fn wmemmove
68function conforms to
69.St -isoC-99
70and was first introduced in
71.St -isoC-amd1 .
72.Sh HISTORY
73The
74.Fn wmemmove
75function was ported from
76.Nx
77and first appeared in
78.Ox 3.8 .
diff --git a/src/lib/libc/string/wmemmove.c b/src/lib/libc/string/wmemmove.c
deleted file mode 100644
index 8bf8011064..0000000000
--- a/src/lib/libc/string/wmemmove.c
+++ /dev/null
@@ -1,41 +0,0 @@
1/* $OpenBSD: wmemmove.c,v 1.4 2015/09/12 16:23:14 guenther Exp $ */
2/* $NetBSD: wmemmove.c,v 1.2 2001/01/03 14:29:37 lukem Exp $ */
3
4/*-
5 * Copyright (c)1999 Citrus Project,
6 * All rights reserved.
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 *
17 * THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND
18 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
19 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
20 * ARE DISCLAIMED. IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE
21 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
22 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
23 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
24 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
25 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
26 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
27 * SUCH DAMAGE.
28 *
29 * citrus Id: wmemmove.c,v 1.2 2000/12/20 14:08:31 itojun Exp
30 */
31
32#include <string.h>
33#include <wchar.h>
34
35wchar_t *
36wmemmove(wchar_t *d, const wchar_t *s, size_t n)
37{
38
39 return (wchar_t *)memmove(d, s, n * sizeof(wchar_t));
40}
41DEF_STRONG(wmemmove);
diff --git a/src/lib/libc/string/wmemset.3 b/src/lib/libc/string/wmemset.3
deleted file mode 100644
index 2655d68bcb..0000000000
--- a/src/lib/libc/string/wmemset.3
+++ /dev/null
@@ -1,73 +0,0 @@
1.\" $OpenBSD: wmemset.3,v 1.4 2013/06/05 03:39:23 tedu Exp $
2.\"
3.\" Copyright (c) 1990, 1991 The Regents of the University of California.
4.\" All rights reserved.
5.\"
6.\" This code is derived from software contributed to Berkeley by
7.\" Chris Torek and the American National Standards Committee X3,
8.\" on Information Processing Systems.
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. 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.Dd $Mdocdate: June 5 2013 $
35.Dt WMEMSET 3
36.Os
37.Sh NAME
38.Nm wmemset
39.Nd write a wide string
40.Sh SYNOPSIS
41.In wchar.h
42.Ft wchar_t *
43.Fn wmemset "wchar_t *s" "wchar_t c" "size_t len"
44.Sh DESCRIPTION
45The
46.Fn wmemset
47function writes
48.Fa len
49wide characters of value
50.Fa c
51to the wide string
52.Fa s .
53.Sh RETURN VALUES
54The
55.Fn wmemset
56function returns the original value of
57.Fa s .
58.Sh SEE ALSO
59.Xr memset 3
60.Sh STANDARDS
61The
62.Fn wmemset
63function conforms to
64.St -isoC-99
65and was first introduced in
66.St -isoC-amd1 .
67.Sh HISTORY
68The
69.Fn wmemset
70function was ported from
71.Nx
72and first appeared in
73.Ox 3.8 .
diff --git a/src/lib/libc/string/wmemset.c b/src/lib/libc/string/wmemset.c
deleted file mode 100644
index a2d3295170..0000000000
--- a/src/lib/libc/string/wmemset.c
+++ /dev/null
@@ -1,47 +0,0 @@
1/* $OpenBSD: wmemset.c,v 1.4 2015/09/12 16:23:14 guenther Exp $ */
2/* $NetBSD: wmemset.c,v 1.2 2001/01/03 14:29:37 lukem Exp $ */
3
4/*-
5 * Copyright (c)1999 Citrus Project,
6 * All rights reserved.
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 *
17 * THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND
18 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
19 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
20 * ARE DISCLAIMED. IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE
21 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
22 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
23 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
24 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
25 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
26 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
27 * SUCH DAMAGE.
28 *
29 * citrus Id: wmemset.c,v 1.2 2000/12/20 14:08:31 itojun Exp
30 */
31
32#include <wchar.h>
33
34wchar_t *
35wmemset(wchar_t *s, wchar_t c, size_t n)
36{
37 size_t i;
38 wchar_t *p;
39
40 p = s;
41 for (i = 0; i < n; i++) {
42 *p = c;
43 p++;
44 }
45 return s;
46}
47DEF_STRONG(wmemset);