blob: 44cf127408d02e4969054d2010b92502f169fc79 [file] [log] [blame]
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -08001/*
2 * Copyright (C) 2007 The Android Open Source Project
3 *
4 * Licensed under the Apache License, Version 2.0 (the "License");
5 * you may not use this file except in compliance with the License.
6 * You may obtain a copy of the License at
7 *
8 * http://www.apache.org/licenses/LICENSE-2.0
9 *
10 * Unless required by applicable law or agreed to in writing, software
11 * distributed under the License is distributed on an "AS IS" BASIS,
12 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13 * See the License for the specific language governing permissions and
14 * limitations under the License.
15 */
16
17#include <cutils/memory.h>
18
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -080019#if !HAVE_STRLCPY
20/*
21 * Copyright (c) 1998 Todd C. Miller <Todd.Miller@courtesan.com>
22 *
23 * Permission to use, copy, modify, and distribute this software for any
24 * purpose with or without fee is hereby granted, provided that the above
25 * copyright notice and this permission notice appear in all copies.
26 *
27 * THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES
28 * WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF
29 * MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR
30 * ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES
31 * WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN
32 * ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF
33 * OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE.
34 */
35
36#include <sys/types.h>
37#include <string.h>
38
39/* Implementation of strlcpy() for platforms that don't already have it. */
40
41/*
42 * Copy src to string dst of size siz. At most siz-1 characters
43 * will be copied. Always NUL terminates (unless siz == 0).
44 * Returns strlen(src); if retval >= siz, truncation occurred.
45 */
46size_t
47strlcpy(char *dst, const char *src, size_t siz)
48{
49 char *d = dst;
50 const char *s = src;
51 size_t n = siz;
52
53 /* Copy as many bytes as will fit */
54 if (n != 0) {
55 while (--n != 0) {
56 if ((*d++ = *s++) == '\0')
57 break;
58 }
59 }
60
61 /* Not enough room in dst, add NUL and traverse rest of src */
62 if (n == 0) {
63 if (siz != 0)
64 *d = '\0'; /* NUL-terminate dst */
65 while (*s++)
66 ;
67 }
68
69 return(s - src - 1); /* count does not include NUL */
70}
71#endif