/src/sudo/lib/util/strlcpy.c
Line  | Count  | Source  | 
1  |  | /*  $OpenBSD: strlcpy.c,v 1.12 2015/01/15 03:54:12 millert Exp $  */  | 
2  |  |  | 
3  |  | /*  | 
4  |  |  * SPDX-License-Identifier: ISC  | 
5  |  |  *  | 
6  |  |  * Copyright (c) 1998, 2003-2005, 2010-2011, 2013-2015  | 
7  |  |  *  Todd C. Miller <Todd.Miller@sudo.ws>  | 
8  |  |  *  | 
9  |  |  * Permission to use, copy, modify, and distribute this software for any  | 
10  |  |  * purpose with or without fee is hereby granted, provided that the above  | 
11  |  |  * copyright notice and this permission notice appear in all copies.  | 
12  |  |  *  | 
13  |  |  * THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES  | 
14  |  |  * WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF  | 
15  |  |  * MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR  | 
16  |  |  * ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES  | 
17  |  |  * WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN  | 
18  |  |  * ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF  | 
19  |  |  * OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE.  | 
20  |  |  */  | 
21  |  |  | 
22  |  | #include <config.h>  | 
23  |  |  | 
24  |  | #ifndef HAVE_STRLCPY  | 
25  |  |  | 
26  |  | #include <string.h>  | 
27  |  |  | 
28  |  | #include <sudo_compat.h>  | 
29  |  |  | 
30  |  | /*  | 
31  |  |  * Copy string src to buffer dst of size dsize.  At most dsize-1  | 
32  |  |  * chars will be copied.  Always NUL terminates (unless dsize == 0).  | 
33  |  |  * Returns strlen(src); if retval >= dsize, truncation occurred.  | 
34  |  |  */  | 
35  |  | size_t  | 
36  |  | sudo_strlcpy(char * restrict dst, const char * restrict src, size_t dsize)  | 
37  | 9.42M  | { | 
38  | 9.42M  |   const char *osrc = src;  | 
39  | 9.42M  |   size_t nleft = dsize;  | 
40  |  |  | 
41  |  |   /* Copy as many bytes as will fit. */  | 
42  | 9.42M  |   if (nleft != 0) { | 
43  | 72.5M  |     while (--nleft != 0) { | 
44  | 72.5M  |       if ((*dst++ = *src++) == '\0')  | 
45  | 9.42M  |         break;  | 
46  | 72.5M  |     }  | 
47  | 9.42M  |   }  | 
48  |  |  | 
49  |  |   /* Not enough room in dst, add NUL and traverse rest of src. */  | 
50  | 9.42M  |   if (nleft == 0) { | 
51  | 1.52k  |     if (dsize != 0)  | 
52  | 1.52k  |       *dst = '\0';   /* NUL-terminate dst */  | 
53  | 1.52k  |     while (*src++)  | 
54  | 0  |       continue;  | 
55  | 1.52k  |   }  | 
56  |  |  | 
57  | 9.42M  |   return((size_t)(src - osrc) - 1); /* count does not include NUL */  | 
58  | 9.42M  | }  | 
59  |  | #endif /* HAVE_STRLCPY */  |