Browse Source

add posix_madvise, posix_memalign, strndup, and strnlen. mostly from

brad and millert, with hints from guenther, jmc, and otto I think.
ok previous.
OPENBSD_4_8
tedu 14 years ago
parent
commit
bbe8fdc923
11 changed files with 278 additions and 19 deletions
  1. +2
    -1
      src/include/stdlib.h
  2. +6
    -1
      src/include/string.h
  3. +4
    -4
      src/lib/libc/stdlib/Makefile.inc
  4. +4
    -3
      src/lib/libc/stdlib/malloc.3
  5. +26
    -1
      src/lib/libc/stdlib/malloc.c
  6. +94
    -0
      src/lib/libc/stdlib/posix_memalign.3
  7. +5
    -3
      src/lib/libc/string/Makefile.inc
  8. +28
    -3
      src/lib/libc/string/strdup.3
  9. +36
    -3
      src/lib/libc/string/strlen.3
  10. +39
    -0
      src/lib/libc/string/strndup.c
  11. +34
    -0
      src/lib/libc/string/strnlen.c

+ 2
- 1
src/include/stdlib.h View File

@ -1,4 +1,4 @@
/* $OpenBSD: stdlib.h,v 1.46 2009/06/03 15:52:16 millert Exp $ */
/* $OpenBSD: stdlib.h,v 1.47 2010/05/18 22:24:55 tedu Exp $ */
/* $NetBSD: stdlib.h,v 1.25 1995/12/27 21:19:08 jtc Exp $ */
/*-
@ -125,6 +125,7 @@ char *getenv(const char *);
long labs(long);
ldiv_t ldiv(long, long);
void *malloc(size_t);
int posix_memalign(void **, size_t, size_t);
void qsort(void *, size_t, size_t, int (*)(const void *, const void *));
int rand(void);
void *realloc(void *, size_t);


+ 6
- 1
src/include/string.h View File

@ -1,4 +1,4 @@
/* $OpenBSD: string.h,v 1.18 2007/09/03 14:36:40 millert Exp $ */
/* $OpenBSD: string.h,v 1.19 2010/05/18 22:24:55 tedu Exp $ */
/* $NetBSD: string.h,v 1.6 1994/10/26 00:56:30 cgd Exp $ */
/*-
@ -110,6 +110,11 @@ int strerror_r(int, char *, size_t)
__attribute__ ((__bounded__(__string__,2,3)));
#endif
#if __POSIX_VISIBLE >= 200809
char *strndup(const char *, size_t);
size_t strnlen(const char *, size_t);
#endif
#if __BSD_VISIBLE
char *strcasestr(const char *, const char *);
size_t strlcat(char *, const char *, size_t)


+ 4
- 4
src/lib/libc/stdlib/Makefile.inc View File

@ -1,4 +1,4 @@
# $OpenBSD: Makefile.inc,v 1.43 2010/02/03 20:49:00 miod Exp $
# $OpenBSD: Makefile.inc,v 1.44 2010/05/18 22:24:55 tedu Exp $
# stdlib sources
.PATH: ${LIBCSRCDIR}/arch/${MACHINE_CPU}/stdlib ${LIBCSRCDIR}/stdlib
@ -42,9 +42,9 @@ SRCS+= insque.c remque.c
MAN+= a64l.3 abort.3 abs.3 alloca.3 atexit.3 atof.3 atoi.3 atol.3 atoll.3 \
bsearch.3 div.3 ecvt.3 exit.3 getenv.3 getopt.3 getopt_long.3 \
getsubopt.3 hcreate.3 imaxabs.3 imaxdiv.3 insque.3 labs.3 ldiv.3 \
lldiv.3 lsearch.3 malloc.3 qabs.3 qdiv.3 qsort.3 radixsort.3 rand48.3 \
rand.3 random.3 realpath.3 strtod.3 strtonum.3 strtol.3 strtoul.3 \
system.3 tsearch.3
lldiv.3 lsearch.3 malloc.3 posix_memalign.3 qabs.3 qdiv.3 qsort.3 \
radixsort.3 rand48.3 rand.3 random.3 realpath.3 strtod.3 strtonum.3 \
strtol.3 strtoul.3 system.3 tsearch.3
MLINKS+=exit.3 _Exit.3
MLINKS+=ecvt.3 fcvt.3 ecvt.3 gcvt.3


+ 4
- 3
src/lib/libc/stdlib/malloc.3 View File

@ -30,9 +30,9 @@
.\" OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
.\" SUCH DAMAGE.
.\"
.\" $OpenBSD: malloc.3,v 1.65 2010/01/25 20:14:11 jmc Exp $
.\" $OpenBSD: malloc.3,v 1.66 2010/05/18 22:24:55 tedu Exp $
.\"
.Dd $Mdocdate: January 25 2010 $
.Dd $Mdocdate: May 18 2010 $
.Dt MALLOC 3
.Os
.Sh NAME
@ -420,7 +420,8 @@ consult sources and/or wizards.
.Xr mmap 2 ,
.Xr munmap 2 ,
.Xr alloca 3 ,
.Xr getpagesize 3
.Xr getpagesize 3 ,
.Xr posix_memalign 3
.Sh STANDARDS
The
.Fn malloc


+ 26
- 1
src/lib/libc/stdlib/malloc.c View File

@ -1,4 +1,4 @@
/* $OpenBSD: malloc.c,v 1.124 2010/01/13 12:40:11 otto Exp $ */
/* $OpenBSD: malloc.c,v 1.125 2010/05/18 22:24:55 tedu Exp $ */
/*
* Copyright (c) 2008 Otto Moerbeek <otto@drijf.net>
*
@ -1488,3 +1488,28 @@ calloc(size_t nmemb, size_t size)
return r;
}
int
posix_memalign(void **memptr, size_t alignment, size_t size)
{
void *result;
/* Make sure that alignment is a large enough power of 2. */
if (((alignment - 1) & alignment) != 0 || alignment < sizeof(void *) ||
alignment > MALLOC_PAGESIZE)
return EINVAL;
/*
* max(size, alignment) is enough to assure the requested alignment,
* since the allocator always allocates power-of-two blocks.
*/
if (size < alignment)
size = alignment;
result = malloc(size);
if (result == NULL)
return ENOMEM;
*memptr = result;
return 0;
}

+ 94
- 0
src/lib/libc/stdlib/posix_memalign.3 View File

@ -0,0 +1,94 @@
.\" $OpenBSD
.\" Copyright (C) 2006 Jason Evans <jasone@FreeBSD.org>.
.\" All rights reserved.
.\"
.\" Redistribution and use in source and binary forms, with or without
.\" modification, are permitted provided that the following conditions
.\" are met:
.\" 1. Redistributions of source code must retain the above copyright
.\" notice(s), this list of conditions and the following disclaimer as
.\" the first lines of this file unmodified other than the possible
.\" addition of one or more copyright notices.
.\" 2. Redistributions in binary form must reproduce the above copyright
.\" notice(s), this list of conditions and the following disclaimer in
.\" the documentation and/or other materials provided with the
.\" distribution.
.\"
.\" THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDER(S) ``AS IS'' AND ANY
.\" EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
.\" IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
.\" PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER(S) BE
.\" LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
.\" CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
.\" SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR
.\" BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY,
.\" WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE
.\" OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE,
.\" EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
.\"
.\" $FreeBSD: src/lib/libc/stdlib/posix_memalign.3,v 1.3 2007/03/28 04:32:51 jasone Exp $
.\"
.Dd $Mdocdate: May 18 2010 $
.Dt POSIX_MEMALIGN 3
.Os
.Sh NAME
.Nm posix_memalign
.Nd aligned memory allocation
.Sh SYNOPSIS
.In stdlib.h
.Ft int
.Fn posix_memalign "void **ptr" "size_t alignment" "size_t size"
.Sh DESCRIPTION
The
.Fn posix_memalign
function allocates
.Fa size
bytes of memory such that the allocation's base address is a multiple of
.Fa alignment ,
and returns the allocation in the value pointed to by
.Fa ptr .
.Pp
The requested
.Fa alignment
must be a power of 2 at least as large as
.Fn sizeof "void *" .
.Pp
Memory that is allocated via
.Fn posix_memalign
can be used as an argument in subsequent calls to
.Xr realloc 3
and
.Xr free 3 .
.Sh RETURN VALUES
The
.Fn posix_memalign
function returns the value 0 if successful; otherwise it returns an error value.
.Sh ERRORS
The
.Fn posix_memalign
function will fail if:
.Bl -tag -width Er
.It Bq Er EINVAL
The
.Fa alignment
parameter is not a power of 2 at least as large as
.Fn sizeof "void *" .
.It Bq Er ENOMEM
Memory allocation error.
.El
.Sh SEE ALSO
.Xr free 3 ,
.Xr malloc 3 ,
.Xr realloc 3
.Sh STANDARDS
The
.Fn posix_memalign
function conforms to
.St -p1003.1-2001 .
.Sh HISTORY
The
.Fn posix_memalign
function first appeared in
.Ox 4.8 .
.Sh BUGS
Only alignments up to the page size can be specified.

+ 5
- 3
src/lib/libc/string/Makefile.inc View File

@ -1,11 +1,11 @@
# $OpenBSD: Makefile.inc,v 1.21 2010/02/03 20:49:00 miod Exp $
# $OpenBSD: Makefile.inc,v 1.22 2010/05/18 22:24:55 tedu Exp $
# string sources
.PATH: ${LIBCSRCDIR}/arch/${MACHINE_CPU}/string ${LIBCSRCDIR}/string
SRCS+= bm.c memccpy.c memrchr.c strcasecmp.c strcasestr.c strcoll.c strdup.c \
strerror.c strerror_r.c strlcat.c strmode.c strsignal.c strtok.c \
strxfrm.c \
strerror.c strerror_r.c strlcat.c strmode.c strndup.c strnlen.c \
strsignal.c strtok.c strxfrm.c \
wcscat.c wcschr.c wcscmp.c wcscpy.c wcscspn.c wcslcat.c wcslcpy.c \
wcslen.c wcsncat.c wcsncmp.c wcsncpy.c wcspbrk.c wcsrchr.c wcsspn.c \
wcsstr.c wcstok.c wcswcs.c wcswidth.c wmemchr.c wmemcmp.c wmemcpy.c \
@ -153,7 +153,9 @@ MLINKS+=strcasecmp.3 strncasecmp.3
MLINKS+=strcat.3 strncat.3
MLINKS+=strcmp.3 strncmp.3
MLINKS+=strcpy.3 strncpy.3
MLINKS+=strdup.3 strndup.3
MLINKS+=strlcpy.3 strlcat.3
MLINKS+=strlen.3 strnlen.3
MLINKS+=strstr.3 strcasestr.3
MLINKS+=strtok.3 strtok_r.3
MLINKS+=strerror.3 strerror_r.3


+ 28
- 3
src/lib/libc/string/strdup.3 View File

@ -1,4 +1,4 @@
.\" $OpenBSD: strdup.3,v 1.15 2010/03/24 14:47:46 kettenis Exp $
.\" $OpenBSD: strdup.3,v 1.16 2010/05/18 22:24:55 tedu Exp $
.\"
.\" Copyright (c) 1990, 1991, 1993
.\" The Regents of the University of California. All rights reserved.
@ -29,16 +29,19 @@
.\"
.\" @(#)strdup.3 8.1 (Berkeley) 6/9/93
.\"
.Dd $Mdocdate: March 24 2010 $
.Dd $Mdocdate: May 18 2010 $
.Dt STRDUP 3
.Os
.Sh NAME
.Nm strdup
.Nm strdup ,
.Nm strndup
.Nd save a copy of a string
.Sh SYNOPSIS
.Fd #include <string.h>
.Ft char *
.Fn strdup "const char *s"
.Ft char *
.Fn strndup "const char *s" "size_t maxlen"
.Sh DESCRIPTION
The
.Fn strdup
@ -48,6 +51,16 @@ does the copy, and returns a pointer to it.
The pointer may subsequently be used as an argument to the function
.Xr free 3 .
.Pp
The
.Fn strndup
function behaves similarly to
.Nm strdup
but only copies up to
.Fa maxlen
characters from
.Fa s .
The resulting string is always NUL-terminated.
.Pp
If insufficient memory is available,
.Dv NULL
is returned.
@ -83,3 +96,15 @@ The
.Fn strdup
function first appeared in
.Bx 4.4 .
.Pp
The
.Fn strndup
function first appeared in
.Ox 4.8 .
.Sh STANDARDS
The
.Fn strdup
and
.Fn strndup
functions conform to
.St -p1003.1-2008 .

+ 36
- 3
src/lib/libc/string/strlen.3 View File

@ -29,29 +29,55 @@
.\" OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
.\" SUCH DAMAGE.
.\"
.\" $OpenBSD: strlen.3,v 1.6 2007/05/31 19:19:32 jmc Exp $
.\" $OpenBSD: strlen.3,v 1.7 2010/05/18 22:24:55 tedu Exp $
.\"
.Dd $Mdocdate: May 31 2007 $
.Dd $Mdocdate: May 18 2010 $
.Dt STRLEN 3
.Os
.Sh NAME
.Nm strlen
.Nm strlen ,
.Nm strnlen
.Nd find length of a string
.Sh SYNOPSIS
.Fd #include <string.h>
.Ft size_t
.Fn strlen "const char *s"
.Ft size_t
.Fn strnlen "const char *s" "size_t maxlen"
.Sh DESCRIPTION
The
.Fn strlen
function computes the length of the string
.Fa s .
.Pp
The
.Fn strnlen
function computes the length of the string
.Fa s ,
up to
.Fa maxlen
characters.
The
.Fn strnlen
function will never attempt to address more than
.Fa maxlen
characters, making it suitable for use with character arrays that are
not guaranteed to be NUL-terminated.
.Pp
.Sh RETURN VALUES
The
.Fn strlen
function returns the number of characters that precede the terminating
.Tn NUL
character.
.Pp
The
.Fn strnlen
function returns the number of characters that precede the terminating
.Tn NUL
or
.Fa maxlen ,
whichever is smaller.
.Sh SEE ALSO
.Xr string 3
.Sh STANDARDS
@ -59,3 +85,10 @@ The
.Fn strlen
function conforms to
.St -ansiC .
.Pp
The
.Fn strlen
and
.Fn strnlen
functions conform to
.St -p1003.1-2008 .

+ 39
- 0
src/lib/libc/string/strndup.c View File

@ -0,0 +1,39 @@
/* $OpenBSD: strndup.c,v 1.1 2010/05/18 22:24:55 tedu Exp $ */
/*
* Copyright (c) 2010 Todd C. Miller <Todd.Miller@courtesan.com>
*
* Permission to use, copy, modify, and distribute this software for any
* purpose with or without fee is hereby granted, provided that the above
* copyright notice and this permission notice appear in all copies.
*
* THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES
* WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF
* MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR
* ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES
* WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN
* ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF
* OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE.
*/
#include <sys/types.h>
#include <stddef.h>
#include <stdlib.h>
#include <string.h>
char *
strndup(const char *str, size_t maxlen)
{
char *copy;
size_t len;
len = strnlen(str, maxlen);
copy = malloc(len + 1);
if (copy != NULL) {
(void)memcpy(copy, str, len);
copy[len] = '\0';
}
return copy;
}

+ 34
- 0
src/lib/libc/string/strnlen.c View File

@ -0,0 +1,34 @@
/* $OpenBSD: strnlen.c,v 1.1 2010/05/18 22:24:55 tedu Exp $ */
/*
* Copyright (c) 2010 Todd C. Miller <Todd.Miller@courtesan.com>
*
* Permission to use, copy, modify, and distribute this software for any
* purpose with or without fee is hereby granted, provided that the above
* copyright notice and this permission notice appear in all copies.
*
* THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES
* WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF
* MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR
* ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES
* WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN
* ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF
* OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE.
*/
#include <sys/types.h>
#include <string.h>
size_t
strnlen(const char *str, size_t maxlen)
{
const char *cp, *ep;
size_t len;
ep = str + maxlen;
for (cp = str; cp < ep && *cp != '\0'; cp++)
;
return (size_t)(cp - str);
}

Loading…
Cancel
Save