xref: /illumos-gate/usr/src/lib/print/libpapi-common/common/misc.c (revision f808c858fa61e7769218966759510a8b1190dfcf)
1 /*
2  * CDDL HEADER START
3  *
4  * The contents of this file are subject to the terms of the
5  * Common Development and Distribution License (the "License").
6  * You may not use this file except in compliance with the License.
7  *
8  * You can obtain a copy of the license at usr/src/OPENSOLARIS.LICENSE
9  * or http://www.opensolaris.org/os/licensing.
10  * See the License for the specific language governing permissions
11  * and limitations under the License.
12  *
13  * When distributing Covered Code, include this CDDL HEADER in each
14  * file and include the License file at usr/src/OPENSOLARIS.LICENSE.
15  * If applicable, add the following below this CDDL HEADER, with the
16  * fields enclosed by brackets "[]" replaced with your own identifying
17  * information: Portions Copyright [yyyy] [name of copyright owner]
18  *
19  * CDDL HEADER END
20  */
21 
22 /*
23  * Copyright 2006 Sun Microsystems, Inc.  All rights reserved.
24  * Use is subject to license terms.
25  *
26  */
27 
28 /* $Id: misc.c 146 2006-03-24 00:26:54Z njacobs $ */
29 
30 #pragma ident	"%Z%%M%	%I%	%E% SMI"
31 
32 /*LINTLIBRARY*/
33 
34 #include <string.h>
35 #include <papi.h>
36 
37 #include <config-site.h>
38 
39 /*
40  * The implementations of strlcpy() and strlcat() have been taken directly
41  * from OpenSolaris.  The contents of this file originated from
42  *     usr/src/lib/libc/port/gen/strlcpy.c
43  *     usr/src/lib/libc/port/gen/strcat.c
44  */
45 
46 #ifndef HAVE_STRLCPY
47 size_t
48 strlcpy(char *dst, const char *src, size_t len)
49 {
50 	size_t slen = strlen(src);
51 	size_t copied;
52 
53 	if (len == 0)
54 		return (slen);
55 
56 	if (slen >= len)
57 		copied = len - 1;
58 	else
59 		copied = slen;
60 	(void) memcpy(dst, src, copied);
61 	dst[copied] = '\0';
62 	return (slen);
63 }
64 #endif
65 
66 #ifndef HAVE_STRLCAT
67 size_t
68 strlcat(char *dst, const char *src, size_t dstsize)
69 {
70 	char *df = dst;
71 	size_t left = dstsize;
72 	size_t l1;
73 	size_t l2 = strlen(src);
74 	size_t copied;
75 
76 	while (left-- != 0 && *df != '\0')
77 		df++;
78 	l1 = df - dst;
79 	if (dstsize == l1)
80 		return (l1 + l2);
81 
82 	copied = l1 + l2 >= dstsize ? dstsize - l1 - 1 : l2;
83 	(void) memcpy(dst + l1, src, copied);
84 	dst[l1+copied] = '\0';
85 	return (l1 + l2);
86 }
87 #endif
88