xref: /OK3568_Linux_fs/external/xserver/os/strndup.c (revision 4882a59341e53eb6f0b4789bf948001014eff981)
1*4882a593Smuzhiyun /*
2*4882a593Smuzhiyun  * Copyright (c) 1988, 1993
3*4882a593Smuzhiyun  *	The Regents of the University of California.  All rights reserved.
4*4882a593Smuzhiyun  *
5*4882a593Smuzhiyun  * Redistribution and use in source and binary forms, with or without
6*4882a593Smuzhiyun  * modification, are permitted provided that the following conditions
7*4882a593Smuzhiyun  * are met:
8*4882a593Smuzhiyun  * 1. Redistributions of source code must retain the above copyright
9*4882a593Smuzhiyun  *    notice, this list of conditions and the following disclaimer.
10*4882a593Smuzhiyun  * 2. Redistributions in binary form must reproduce the above copyright
11*4882a593Smuzhiyun  *    notice, this list of conditions and the following disclaimer in the
12*4882a593Smuzhiyun  *    documentation and/or other materials provided with the distribution.
13*4882a593Smuzhiyun  * 4. Neither the name of the University nor the names of its contributors
14*4882a593Smuzhiyun  *    may be used to endorse or promote products derived from this software
15*4882a593Smuzhiyun  *    without specific prior written permission.
16*4882a593Smuzhiyun  *
17*4882a593Smuzhiyun  * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
18*4882a593Smuzhiyun  * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
19*4882a593Smuzhiyun  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
20*4882a593Smuzhiyun  * ARE DISCLAIMED.  IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
21*4882a593Smuzhiyun  * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
22*4882a593Smuzhiyun  * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
23*4882a593Smuzhiyun  * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
24*4882a593Smuzhiyun  * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
25*4882a593Smuzhiyun  * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
26*4882a593Smuzhiyun  * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
27*4882a593Smuzhiyun  * SUCH DAMAGE.
28*4882a593Smuzhiyun  */
29*4882a593Smuzhiyun 
30*4882a593Smuzhiyun #ifdef HAVE_DIX_CONFIG_H
31*4882a593Smuzhiyun #include <dix-config.h>
32*4882a593Smuzhiyun #endif
33*4882a593Smuzhiyun 
34*4882a593Smuzhiyun #include <stddef.h>
35*4882a593Smuzhiyun #include <stdlib.h>
36*4882a593Smuzhiyun #include <string.h>
37*4882a593Smuzhiyun #include "os.h"
38*4882a593Smuzhiyun 
39*4882a593Smuzhiyun char *
strndup(const char * str,size_t n)40*4882a593Smuzhiyun strndup(const char *str, size_t n)
41*4882a593Smuzhiyun {
42*4882a593Smuzhiyun     size_t len;
43*4882a593Smuzhiyun     char *copy;
44*4882a593Smuzhiyun 
45*4882a593Smuzhiyun     for (len = 0; len < n && str[len]; len++)
46*4882a593Smuzhiyun         continue;
47*4882a593Smuzhiyun 
48*4882a593Smuzhiyun     if ((copy = malloc(len + 1)) == NULL)
49*4882a593Smuzhiyun         return (NULL);
50*4882a593Smuzhiyun     memcpy(copy, str, len);
51*4882a593Smuzhiyun     copy[len] = '\0';
52*4882a593Smuzhiyun     return (copy);
53*4882a593Smuzhiyun }
54