strndup.c (185690) | strndup.c (203391) |
---|---|
1/* $NetBSD: strndup.c,v 1.3 2007/01/14 23:41:24 cbiere 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 --- 16 unchanged lines hidden (view full) --- 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/cdefs.h> | 1/* $NetBSD: strndup.c,v 1.3 2007/01/14 23:41:24 cbiere 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 --- 16 unchanged lines hidden (view full) --- 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/cdefs.h> |
33__FBSDID("$FreeBSD: head/lib/libc/string/strndup.c 185690 2008-12-06 09:37:54Z kib $"); | 33__FBSDID("$FreeBSD: head/lib/libc/string/strndup.c 203391 2010-02-02 19:02:08Z ed $"); |
34 35#include <stddef.h> 36#include <stdlib.h> 37#include <string.h> 38 39char * 40strndup(const char *str, size_t n) 41{ 42 size_t len; 43 char *copy; 44 | 34 35#include <stddef.h> 36#include <stdlib.h> 37#include <string.h> 38 39char * 40strndup(const char *str, size_t n) 41{ 42 size_t len; 43 char *copy; 44 |
45 for (len = 0; len < n && str[len]; len++) 46 continue; 47 | 45 len = strnlen(str, n); |
48 if ((copy = malloc(len + 1)) == NULL) 49 return (NULL); 50 memcpy(copy, str, len); 51 copy[len] = '\0'; 52 return (copy); 53} | 46 if ((copy = malloc(len + 1)) == NULL) 47 return (NULL); 48 memcpy(copy, str, len); 49 copy[len] = '\0'; 50 return (copy); 51} |