backport
[AROS.git] / compiler / clib / strndup.c
blobceddbf1f938374a524d69eddbd7b30883b393917
1 /*
2 Copyright © 1995-2001, The AROS Development Team. All rights reserved.
3 $Id$
5 ANSI C function strndup().
6 */
8 #include <stdlib.h>
10 /*****************************************************************************
12 NAME */
13 #include <string.h>
15 char * strndup (
17 /* SYNOPSIS */
18 const char *s, size_t n)
20 /* FUNCTION
21 Create a copy of a string. The copy can be freed with free() or will
22 be freed when then program ends. The copy will be at most n character
23 long, excluding the trailing \000
25 INPUTS
26 s - String to duplicate
27 n - Maximum length
29 RESULT
30 A copy of the string which can be freed with free().
32 NOTES
34 EXAMPLE
36 BUGS
38 SEE ALSO
40 INTERNALS
42 ******************************************************************************/
44 char * copy;
45 int len = strlen(s);
47 if (len > n)
48 len = n;
50 if ((copy = malloc (len+1)))
52 memcpy(copy, s, len);
53 copy[len] = 0;
56 return copy;
57 } /* strndup */