WvStreams
strndup.c
00001 /* strndup.c
00002  *
00003  */
00004 
00005 /* Written by Niels Möller <nisse@lysator.liu.se>
00006  *
00007  * This file is hereby placed in the public domain.
00008  */
00009 
00010 #include <stdlib.h>
00011 #include <string.h>
00012 
00013 char *
00014 strndup (const char *s, size_t size)
00015 {
00016   char *r;
00017   char *end = memchr(s, 0, size);
00018   
00019   if (end)
00020     /* Length + 1 */
00021     size = end - s + 1;
00022   
00023   r = malloc(size);
00024 
00025   if (size)
00026     {
00027       memcpy(r, s, size-1);
00028       r[size-1] = '\0';
00029     }
00030   return r;
00031 }