diff options
| author | tedu <> | 2010-05-18 22:24:55 +0000 |
|---|---|---|
| committer | tedu <> | 2010-05-18 22:24:55 +0000 |
| commit | 84e56ad2cf3f4b33089396bba6483888d54d99a3 (patch) | |
| tree | a6c0f4a1cf1cb7596fb9279f0a7ff14d13a1317e /src/lib/libc | |
| parent | 5a36fcc1c46490330487900d818ea64d85ac1f1a (diff) | |
| download | openbsd-84e56ad2cf3f4b33089396bba6483888d54d99a3.tar.gz openbsd-84e56ad2cf3f4b33089396bba6483888d54d99a3.tar.bz2 openbsd-84e56ad2cf3f4b33089396bba6483888d54d99a3.zip | |
add posix_madvise, posix_memalign, strndup, and strnlen. mostly from
brad and millert, with hints from guenther, jmc, and otto I think.
ok previous.
Diffstat (limited to '')
| -rw-r--r-- | src/lib/libc/stdlib/Makefile.inc | 8 | ||||
| -rw-r--r-- | src/lib/libc/stdlib/malloc.3 | 7 | ||||
| -rw-r--r-- | src/lib/libc/stdlib/malloc.c | 27 | ||||
| -rw-r--r-- | src/lib/libc/stdlib/posix_memalign.3 | 94 | ||||
| -rw-r--r-- | src/lib/libc/string/Makefile.inc | 8 | ||||
| -rw-r--r-- | src/lib/libc/string/strdup.3 | 31 | ||||
| -rw-r--r-- | src/lib/libc/string/strlen.3 | 39 | ||||
| -rw-r--r-- | src/lib/libc/string/strndup.c | 39 | ||||
| -rw-r--r-- | src/lib/libc/string/strnlen.c | 34 |
9 files changed, 270 insertions, 17 deletions
diff --git a/src/lib/libc/stdlib/Makefile.inc b/src/lib/libc/stdlib/Makefile.inc index b003560291..81d1962026 100644 --- a/src/lib/libc/stdlib/Makefile.inc +++ b/src/lib/libc/stdlib/Makefile.inc | |||
| @@ -1,4 +1,4 @@ | |||
| 1 | # $OpenBSD: Makefile.inc,v 1.43 2010/02/03 20:49:00 miod Exp $ | 1 | # $OpenBSD: Makefile.inc,v 1.44 2010/05/18 22:24:55 tedu Exp $ |
| 2 | 2 | ||
| 3 | # stdlib sources | 3 | # stdlib sources |
| 4 | .PATH: ${LIBCSRCDIR}/arch/${MACHINE_CPU}/stdlib ${LIBCSRCDIR}/stdlib | 4 | .PATH: ${LIBCSRCDIR}/arch/${MACHINE_CPU}/stdlib ${LIBCSRCDIR}/stdlib |
| @@ -42,9 +42,9 @@ SRCS+= insque.c remque.c | |||
| 42 | MAN+= a64l.3 abort.3 abs.3 alloca.3 atexit.3 atof.3 atoi.3 atol.3 atoll.3 \ | 42 | MAN+= a64l.3 abort.3 abs.3 alloca.3 atexit.3 atof.3 atoi.3 atol.3 atoll.3 \ |
| 43 | bsearch.3 div.3 ecvt.3 exit.3 getenv.3 getopt.3 getopt_long.3 \ | 43 | bsearch.3 div.3 ecvt.3 exit.3 getenv.3 getopt.3 getopt_long.3 \ |
| 44 | getsubopt.3 hcreate.3 imaxabs.3 imaxdiv.3 insque.3 labs.3 ldiv.3 \ | 44 | getsubopt.3 hcreate.3 imaxabs.3 imaxdiv.3 insque.3 labs.3 ldiv.3 \ |
| 45 | lldiv.3 lsearch.3 malloc.3 qabs.3 qdiv.3 qsort.3 radixsort.3 rand48.3 \ | 45 | lldiv.3 lsearch.3 malloc.3 posix_memalign.3 qabs.3 qdiv.3 qsort.3 \ |
| 46 | rand.3 random.3 realpath.3 strtod.3 strtonum.3 strtol.3 strtoul.3 \ | 46 | radixsort.3 rand48.3 rand.3 random.3 realpath.3 strtod.3 strtonum.3 \ |
| 47 | system.3 tsearch.3 | 47 | strtol.3 strtoul.3 system.3 tsearch.3 |
| 48 | 48 | ||
| 49 | MLINKS+=exit.3 _Exit.3 | 49 | MLINKS+=exit.3 _Exit.3 |
| 50 | MLINKS+=ecvt.3 fcvt.3 ecvt.3 gcvt.3 | 50 | MLINKS+=ecvt.3 fcvt.3 ecvt.3 gcvt.3 |
diff --git a/src/lib/libc/stdlib/malloc.3 b/src/lib/libc/stdlib/malloc.3 index d30ac8c08c..62006a7eb5 100644 --- a/src/lib/libc/stdlib/malloc.3 +++ b/src/lib/libc/stdlib/malloc.3 | |||
| @@ -30,9 +30,9 @@ | |||
| 30 | .\" OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF | 30 | .\" OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF |
| 31 | .\" SUCH DAMAGE. | 31 | .\" SUCH DAMAGE. |
| 32 | .\" | 32 | .\" |
| 33 | .\" $OpenBSD: malloc.3,v 1.65 2010/01/25 20:14:11 jmc Exp $ | 33 | .\" $OpenBSD: malloc.3,v 1.66 2010/05/18 22:24:55 tedu Exp $ |
| 34 | .\" | 34 | .\" |
| 35 | .Dd $Mdocdate: January 25 2010 $ | 35 | .Dd $Mdocdate: May 18 2010 $ |
| 36 | .Dt MALLOC 3 | 36 | .Dt MALLOC 3 |
| 37 | .Os | 37 | .Os |
| 38 | .Sh NAME | 38 | .Sh NAME |
| @@ -420,7 +420,8 @@ consult sources and/or wizards. | |||
| 420 | .Xr mmap 2 , | 420 | .Xr mmap 2 , |
| 421 | .Xr munmap 2 , | 421 | .Xr munmap 2 , |
| 422 | .Xr alloca 3 , | 422 | .Xr alloca 3 , |
| 423 | .Xr getpagesize 3 | 423 | .Xr getpagesize 3 , |
| 424 | .Xr posix_memalign 3 | ||
| 424 | .Sh STANDARDS | 425 | .Sh STANDARDS |
| 425 | The | 426 | The |
| 426 | .Fn malloc | 427 | .Fn malloc |
diff --git a/src/lib/libc/stdlib/malloc.c b/src/lib/libc/stdlib/malloc.c index 9cee3e5935..902b69c216 100644 --- a/src/lib/libc/stdlib/malloc.c +++ b/src/lib/libc/stdlib/malloc.c | |||
| @@ -1,4 +1,4 @@ | |||
| 1 | /* $OpenBSD: malloc.c,v 1.124 2010/01/13 12:40:11 otto Exp $ */ | 1 | /* $OpenBSD: malloc.c,v 1.125 2010/05/18 22:24:55 tedu Exp $ */ |
| 2 | /* | 2 | /* |
| 3 | * Copyright (c) 2008 Otto Moerbeek <otto@drijf.net> | 3 | * Copyright (c) 2008 Otto Moerbeek <otto@drijf.net> |
| 4 | * | 4 | * |
| @@ -1488,3 +1488,28 @@ calloc(size_t nmemb, size_t size) | |||
| 1488 | return r; | 1488 | return r; |
| 1489 | } | 1489 | } |
| 1490 | 1490 | ||
| 1491 | int | ||
| 1492 | posix_memalign(void **memptr, size_t alignment, size_t size) | ||
| 1493 | { | ||
| 1494 | void *result; | ||
| 1495 | |||
| 1496 | /* Make sure that alignment is a large enough power of 2. */ | ||
| 1497 | if (((alignment - 1) & alignment) != 0 || alignment < sizeof(void *) || | ||
| 1498 | alignment > MALLOC_PAGESIZE) | ||
| 1499 | return EINVAL; | ||
| 1500 | |||
| 1501 | /* | ||
| 1502 | * max(size, alignment) is enough to assure the requested alignment, | ||
| 1503 | * since the allocator always allocates power-of-two blocks. | ||
| 1504 | */ | ||
| 1505 | if (size < alignment) | ||
| 1506 | size = alignment; | ||
| 1507 | result = malloc(size); | ||
| 1508 | |||
| 1509 | if (result == NULL) | ||
| 1510 | return ENOMEM; | ||
| 1511 | |||
| 1512 | *memptr = result; | ||
| 1513 | return 0; | ||
| 1514 | } | ||
| 1515 | |||
diff --git a/src/lib/libc/stdlib/posix_memalign.3 b/src/lib/libc/stdlib/posix_memalign.3 new file mode 100644 index 0000000000..7bd39933dc --- /dev/null +++ b/src/lib/libc/stdlib/posix_memalign.3 | |||
| @@ -0,0 +1,94 @@ | |||
| 1 | .\" $OpenBSD | ||
| 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 18 2010 $ | ||
| 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 | ||
| 42 | The | ||
| 43 | .Fn posix_memalign | ||
| 44 | function allocates | ||
| 45 | .Fa size | ||
| 46 | bytes of memory such that the allocation's base address is a multiple of | ||
| 47 | .Fa alignment , | ||
| 48 | and returns the allocation in the value pointed to by | ||
| 49 | .Fa ptr . | ||
| 50 | .Pp | ||
| 51 | The requested | ||
| 52 | .Fa alignment | ||
| 53 | must be a power of 2 at least as large as | ||
| 54 | .Fn sizeof "void *" . | ||
| 55 | .Pp | ||
| 56 | Memory that is allocated via | ||
| 57 | .Fn posix_memalign | ||
| 58 | can be used as an argument in subsequent calls to | ||
| 59 | .Xr realloc 3 | ||
| 60 | and | ||
| 61 | .Xr free 3 . | ||
| 62 | .Sh RETURN VALUES | ||
| 63 | The | ||
| 64 | .Fn posix_memalign | ||
| 65 | function returns the value 0 if successful; otherwise it returns an error value. | ||
| 66 | .Sh ERRORS | ||
| 67 | The | ||
| 68 | .Fn posix_memalign | ||
| 69 | function will fail if: | ||
| 70 | .Bl -tag -width Er | ||
| 71 | .It Bq Er EINVAL | ||
| 72 | The | ||
| 73 | .Fa alignment | ||
| 74 | parameter is not a power of 2 at least as large as | ||
| 75 | .Fn sizeof "void *" . | ||
| 76 | .It Bq Er ENOMEM | ||
| 77 | Memory allocation error. | ||
| 78 | .El | ||
| 79 | .Sh SEE ALSO | ||
| 80 | .Xr free 3 , | ||
| 81 | .Xr malloc 3 , | ||
| 82 | .Xr realloc 3 | ||
| 83 | .Sh STANDARDS | ||
| 84 | The | ||
| 85 | .Fn posix_memalign | ||
| 86 | function conforms to | ||
| 87 | .St -p1003.1-2001 . | ||
| 88 | .Sh HISTORY | ||
| 89 | The | ||
| 90 | .Fn posix_memalign | ||
| 91 | function first appeared in | ||
| 92 | .Ox 4.8 . | ||
| 93 | .Sh BUGS | ||
| 94 | Only alignments up to the page size can be specified. | ||
diff --git a/src/lib/libc/string/Makefile.inc b/src/lib/libc/string/Makefile.inc index 8aa072a289..3264b799f1 100644 --- a/src/lib/libc/string/Makefile.inc +++ b/src/lib/libc/string/Makefile.inc | |||
| @@ -1,11 +1,11 @@ | |||
| 1 | # $OpenBSD: Makefile.inc,v 1.21 2010/02/03 20:49:00 miod Exp $ | 1 | # $OpenBSD: Makefile.inc,v 1.22 2010/05/18 22:24:55 tedu Exp $ |
| 2 | 2 | ||
| 3 | # string sources | 3 | # string sources |
| 4 | .PATH: ${LIBCSRCDIR}/arch/${MACHINE_CPU}/string ${LIBCSRCDIR}/string | 4 | .PATH: ${LIBCSRCDIR}/arch/${MACHINE_CPU}/string ${LIBCSRCDIR}/string |
| 5 | 5 | ||
| 6 | SRCS+= bm.c memccpy.c memrchr.c strcasecmp.c strcasestr.c strcoll.c strdup.c \ | 6 | SRCS+= bm.c memccpy.c memrchr.c strcasecmp.c strcasestr.c strcoll.c strdup.c \ |
| 7 | strerror.c strerror_r.c strlcat.c strmode.c strsignal.c strtok.c \ | 7 | strerror.c strerror_r.c strlcat.c strmode.c strndup.c strnlen.c \ |
| 8 | strxfrm.c \ | 8 | strsignal.c strtok.c strxfrm.c \ |
| 9 | wcscat.c wcschr.c wcscmp.c wcscpy.c wcscspn.c wcslcat.c wcslcpy.c \ | 9 | wcscat.c wcschr.c wcscmp.c wcscpy.c wcscspn.c wcslcat.c wcslcpy.c \ |
| 10 | wcslen.c wcsncat.c wcsncmp.c wcsncpy.c wcspbrk.c wcsrchr.c wcsspn.c \ | 10 | wcslen.c wcsncat.c wcsncmp.c wcsncpy.c wcspbrk.c wcsrchr.c wcsspn.c \ |
| 11 | wcsstr.c wcstok.c wcswcs.c wcswidth.c wmemchr.c wmemcmp.c wmemcpy.c \ | 11 | wcsstr.c wcstok.c wcswcs.c wcswidth.c wmemchr.c wmemcmp.c wmemcpy.c \ |
| @@ -153,7 +153,9 @@ MLINKS+=strcasecmp.3 strncasecmp.3 | |||
| 153 | MLINKS+=strcat.3 strncat.3 | 153 | MLINKS+=strcat.3 strncat.3 |
| 154 | MLINKS+=strcmp.3 strncmp.3 | 154 | MLINKS+=strcmp.3 strncmp.3 |
| 155 | MLINKS+=strcpy.3 strncpy.3 | 155 | MLINKS+=strcpy.3 strncpy.3 |
| 156 | MLINKS+=strdup.3 strndup.3 | ||
| 156 | MLINKS+=strlcpy.3 strlcat.3 | 157 | MLINKS+=strlcpy.3 strlcat.3 |
| 158 | MLINKS+=strlen.3 strnlen.3 | ||
| 157 | MLINKS+=strstr.3 strcasestr.3 | 159 | MLINKS+=strstr.3 strcasestr.3 |
| 158 | MLINKS+=strtok.3 strtok_r.3 | 160 | MLINKS+=strtok.3 strtok_r.3 |
| 159 | MLINKS+=strerror.3 strerror_r.3 | 161 | MLINKS+=strerror.3 strerror_r.3 |
diff --git a/src/lib/libc/string/strdup.3 b/src/lib/libc/string/strdup.3 index 60a7462777..05dcb794f0 100644 --- a/src/lib/libc/string/strdup.3 +++ b/src/lib/libc/string/strdup.3 | |||
| @@ -1,4 +1,4 @@ | |||
| 1 | .\" $OpenBSD: strdup.3,v 1.15 2010/03/24 14:47:46 kettenis Exp $ | 1 | .\" $OpenBSD: strdup.3,v 1.16 2010/05/18 22:24:55 tedu Exp $ |
| 2 | .\" | 2 | .\" |
| 3 | .\" Copyright (c) 1990, 1991, 1993 | 3 | .\" Copyright (c) 1990, 1991, 1993 |
| 4 | .\" The Regents of the University of California. All rights reserved. | 4 | .\" The Regents of the University of California. All rights reserved. |
| @@ -29,16 +29,19 @@ | |||
| 29 | .\" | 29 | .\" |
| 30 | .\" @(#)strdup.3 8.1 (Berkeley) 6/9/93 | 30 | .\" @(#)strdup.3 8.1 (Berkeley) 6/9/93 |
| 31 | .\" | 31 | .\" |
| 32 | .Dd $Mdocdate: March 24 2010 $ | 32 | .Dd $Mdocdate: May 18 2010 $ |
| 33 | .Dt STRDUP 3 | 33 | .Dt STRDUP 3 |
| 34 | .Os | 34 | .Os |
| 35 | .Sh NAME | 35 | .Sh NAME |
| 36 | .Nm strdup | 36 | .Nm strdup , |
| 37 | .Nm strndup | ||
| 37 | .Nd save a copy of a string | 38 | .Nd save a copy of a string |
| 38 | .Sh SYNOPSIS | 39 | .Sh SYNOPSIS |
| 39 | .Fd #include <string.h> | 40 | .Fd #include <string.h> |
| 40 | .Ft char * | 41 | .Ft char * |
| 41 | .Fn strdup "const char *s" | 42 | .Fn strdup "const char *s" |
| 43 | .Ft char * | ||
| 44 | .Fn strndup "const char *s" "size_t maxlen" | ||
| 42 | .Sh DESCRIPTION | 45 | .Sh DESCRIPTION |
| 43 | The | 46 | The |
| 44 | .Fn strdup | 47 | .Fn strdup |
| @@ -48,6 +51,16 @@ does the copy, and returns a pointer to it. | |||
| 48 | The pointer may subsequently be used as an argument to the function | 51 | The pointer may subsequently be used as an argument to the function |
| 49 | .Xr free 3 . | 52 | .Xr free 3 . |
| 50 | .Pp | 53 | .Pp |
| 54 | The | ||
| 55 | .Fn strndup | ||
| 56 | function behaves similarly to | ||
| 57 | .Nm strdup | ||
| 58 | but only copies up to | ||
| 59 | .Fa maxlen | ||
| 60 | characters from | ||
| 61 | .Fa s . | ||
| 62 | The resulting string is always NUL-terminated. | ||
| 63 | .Pp | ||
| 51 | If insufficient memory is available, | 64 | If insufficient memory is available, |
| 52 | .Dv NULL | 65 | .Dv NULL |
| 53 | is returned. | 66 | is returned. |
| @@ -83,3 +96,15 @@ The | |||
| 83 | .Fn strdup | 96 | .Fn strdup |
| 84 | function first appeared in | 97 | function first appeared in |
| 85 | .Bx 4.4 . | 98 | .Bx 4.4 . |
| 99 | .Pp | ||
| 100 | The | ||
| 101 | .Fn strndup | ||
| 102 | function first appeared in | ||
| 103 | .Ox 4.8 . | ||
| 104 | .Sh STANDARDS | ||
| 105 | The | ||
| 106 | .Fn strdup | ||
| 107 | and | ||
| 108 | .Fn strndup | ||
| 109 | functions conform to | ||
| 110 | .St -p1003.1-2008 . | ||
diff --git a/src/lib/libc/string/strlen.3 b/src/lib/libc/string/strlen.3 index f8a4efe9d2..3b5f9a10c1 100644 --- a/src/lib/libc/string/strlen.3 +++ b/src/lib/libc/string/strlen.3 | |||
| @@ -29,29 +29,55 @@ | |||
| 29 | .\" OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF | 29 | .\" OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF |
| 30 | .\" SUCH DAMAGE. | 30 | .\" SUCH DAMAGE. |
| 31 | .\" | 31 | .\" |
| 32 | .\" $OpenBSD: strlen.3,v 1.6 2007/05/31 19:19:32 jmc Exp $ | 32 | .\" $OpenBSD: strlen.3,v 1.7 2010/05/18 22:24:55 tedu Exp $ |
| 33 | .\" | 33 | .\" |
| 34 | .Dd $Mdocdate: May 31 2007 $ | 34 | .Dd $Mdocdate: May 18 2010 $ |
| 35 | .Dt STRLEN 3 | 35 | .Dt STRLEN 3 |
| 36 | .Os | 36 | .Os |
| 37 | .Sh NAME | 37 | .Sh NAME |
| 38 | .Nm strlen | 38 | .Nm strlen , |
| 39 | .Nm strnlen | ||
| 39 | .Nd find length of a string | 40 | .Nd find length of a string |
| 40 | .Sh SYNOPSIS | 41 | .Sh SYNOPSIS |
| 41 | .Fd #include <string.h> | 42 | .Fd #include <string.h> |
| 42 | .Ft size_t | 43 | .Ft size_t |
| 43 | .Fn strlen "const char *s" | 44 | .Fn strlen "const char *s" |
| 45 | .Ft size_t | ||
| 46 | .Fn strnlen "const char *s" "size_t maxlen" | ||
| 44 | .Sh DESCRIPTION | 47 | .Sh DESCRIPTION |
| 45 | The | 48 | The |
| 46 | .Fn strlen | 49 | .Fn strlen |
| 47 | function computes the length of the string | 50 | function computes the length of the string |
| 48 | .Fa s . | 51 | .Fa s . |
| 52 | .Pp | ||
| 53 | The | ||
| 54 | .Fn strnlen | ||
| 55 | function computes the length of the string | ||
| 56 | .Fa s , | ||
| 57 | up to | ||
| 58 | .Fa maxlen | ||
| 59 | characters. | ||
| 60 | The | ||
| 61 | .Fn strnlen | ||
| 62 | function will never attempt to address more than | ||
| 63 | .Fa maxlen | ||
| 64 | characters, making it suitable for use with character arrays that are | ||
| 65 | not guaranteed to be NUL-terminated. | ||
| 66 | .Pp | ||
| 49 | .Sh RETURN VALUES | 67 | .Sh RETURN VALUES |
| 50 | The | 68 | The |
| 51 | .Fn strlen | 69 | .Fn strlen |
| 52 | function returns the number of characters that precede the terminating | 70 | function returns the number of characters that precede the terminating |
| 53 | .Tn NUL | 71 | .Tn NUL |
| 54 | character. | 72 | character. |
| 73 | .Pp | ||
| 74 | The | ||
| 75 | .Fn strnlen | ||
| 76 | function returns the number of characters that precede the terminating | ||
| 77 | .Tn NUL | ||
| 78 | or | ||
| 79 | .Fa maxlen , | ||
| 80 | whichever is smaller. | ||
| 55 | .Sh SEE ALSO | 81 | .Sh SEE ALSO |
| 56 | .Xr string 3 | 82 | .Xr string 3 |
| 57 | .Sh STANDARDS | 83 | .Sh STANDARDS |
| @@ -59,3 +85,10 @@ The | |||
| 59 | .Fn strlen | 85 | .Fn strlen |
| 60 | function conforms to | 86 | function conforms to |
| 61 | .St -ansiC . | 87 | .St -ansiC . |
| 88 | .Pp | ||
| 89 | The | ||
| 90 | .Fn strlen | ||
| 91 | and | ||
| 92 | .Fn strnlen | ||
| 93 | functions conform to | ||
| 94 | .St -p1003.1-2008 . | ||
diff --git a/src/lib/libc/string/strndup.c b/src/lib/libc/string/strndup.c new file mode 100644 index 0000000000..27701ac555 --- /dev/null +++ b/src/lib/libc/string/strndup.c | |||
| @@ -0,0 +1,39 @@ | |||
| 1 | /* $OpenBSD: strndup.c,v 1.1 2010/05/18 22:24:55 tedu Exp $ */ | ||
| 2 | |||
| 3 | /* | ||
| 4 | * Copyright (c) 2010 Todd C. Miller <Todd.Miller@courtesan.com> | ||
| 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 | |||
| 25 | char * | ||
| 26 | strndup(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 | } | ||
diff --git a/src/lib/libc/string/strnlen.c b/src/lib/libc/string/strnlen.c new file mode 100644 index 0000000000..5c99994744 --- /dev/null +++ b/src/lib/libc/string/strnlen.c | |||
| @@ -0,0 +1,34 @@ | |||
| 1 | /* $OpenBSD: strnlen.c,v 1.1 2010/05/18 22:24:55 tedu Exp $ */ | ||
| 2 | |||
| 3 | /* | ||
| 4 | * Copyright (c) 2010 Todd C. Miller <Todd.Miller@courtesan.com> | ||
| 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 | |||
| 23 | size_t | ||
| 24 | strnlen(const char *str, size_t maxlen) | ||
| 25 | { | ||
| 26 | const char *cp, *ep; | ||
| 27 | size_t len; | ||
| 28 | |||
| 29 | ep = str + maxlen; | ||
| 30 | for (cp = str; cp < ep && *cp != '\0'; cp++) | ||
| 31 | ; | ||
| 32 | |||
| 33 | return (size_t)(cp - str); | ||
| 34 | } | ||
