LLVM API Documentation

regstrlcpy.c
Go to the documentation of this file.
00001 /*
00002  * This code is derived from OpenBSD's libc, original license follows:
00003  *
00004  * Copyright (c) 1998 Todd C. Miller <[email protected]>
00005  *
00006  * Permission to use, copy, modify, and distribute this software for any
00007  * purpose with or without fee is hereby granted, provided that the above
00008  * copyright notice and this permission notice appear in all copies.
00009  *
00010  * THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES
00011  * WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF
00012  * MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR
00013  * ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES
00014  * WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN
00015  * ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF
00016  * OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE.
00017  */
00018 
00019 #include <sys/types.h>
00020 #include <string.h>
00021 
00022 #include "regex_impl.h"
00023 /*
00024  * Copy src to string dst of size siz.  At most siz-1 characters
00025  * will be copied.  Always NUL terminates (unless siz == 0).
00026  * Returns strlen(src); if retval >= siz, truncation occurred.
00027  */
00028 size_t
00029 llvm_strlcpy(char *dst, const char *src, size_t siz)
00030 {
00031   char *d = dst;
00032   const char *s = src;
00033   size_t n = siz;
00034 
00035   /* Copy as many bytes as will fit */
00036   if (n != 0) {
00037     while (--n != 0) {
00038       if ((*d++ = *s++) == '\0')
00039         break;
00040     }
00041   }
00042 
00043   /* Not enough room in dst, add NUL and traverse rest of src */
00044   if (n == 0) {
00045     if (siz != 0)
00046       *d = '\0';    /* NUL-terminate dst */
00047     while (*s++)
00048       ;
00049   }
00050 
00051   return(s - src - 1);  /* count does not include NUL */
00052 }