1 /******************************************************************************
2 *
3 * Copyright 2015 Google, Inc.
4 *
5 * Licensed under the Apache License, Version 2.0 (the "License");
6 * you may not use this file except in compliance with the License.
7 * You may obtain a copy of the License at:
8 *
9 * http://www.apache.org/licenses/LICENSE-2.0
10 *
11 * Unless required by applicable law or agreed to in writing, software
12 * distributed under the License is distributed on an "AS IS" BASIS,
13 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
14 * See the License for the specific language governing permissions and
15 * limitations under the License.
16 *
17 ******************************************************************************/
18
19 /************************************************************************************
20 *
21 * Filename: compat.c
22 *
23 * Description: Compatibility functions for non-bionic C libraries
24 *
25 *
26 ***********************************************************************************/
27
28 #include <features.h>
29 #include <string.h>
30 #include <sys/syscall.h>
31 #include <sys/types.h>
32 #include <unistd.h>
33 #include "osi/include/compat.h"
34
35 #if __GLIBC__
36 pid_t
gettid(void)37 gettid(void)
38 {
39 return syscall(SYS_gettid);
40 }
41 #endif
42
43 /* These functions from bionic
44 *
45 * Copyright (c) 1998 Todd C. Miller <Todd.Miller@courtesan.com>
46 *
47 * Permission to use, copy, modify, and distribute this software for any
48 * purpose with or without fee is hereby granted, provided that the above
49 * copyright notice and this permission notice appear in all copies.
50 *
51 * THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES
52 * WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF
53 * MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR
54 * ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES
55 * WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN
56 * ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF
57 * OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE.
58 */
59
60 #if __GLIBC__
61 /*
62 * Copy src to string dst of size siz. At most siz-1 characters
63 * will be copied. Always NUL terminates (unless siz == 0).
64 * Returns strlen(src); if retval >= siz, truncation occurred.
65 */
66 size_t
strlcpy(char * dst,const char * src,size_t siz)67 strlcpy(char *dst, const char *src, size_t siz)
68 {
69 char *d = dst;
70 const char *s = src;
71 size_t n = siz;
72
73 /* Copy as many bytes as will fit */
74 if (n != 0) {
75 while (--n != 0) {
76 if ((*d++ = *s++) == '\0')
77 break;
78 }
79 }
80
81 /* Not enough room in dst, add NUL and traverse rest of src */
82 if (n == 0) {
83 if (siz != 0)
84 *d = '\0'; /* NUL-terminate dst */
85 while (*s++)
86 ;
87 }
88
89 return(s - src - 1); /* count does not include NUL */
90 }
91 #endif
92
93 #if __GLIBC__
94 /*
95 * Appends src to string dst of size siz (unlike strncat, siz is the
96 * full size of dst, not space left). At most siz-1 characters
97 * will be copied. Always NUL terminates (unless siz <= strlen(dst)).
98 * Returns strlen(src) + MIN(siz, strlen(initial dst)).
99 * If retval >= siz, truncation occurred.
100 */
101 size_t
strlcat(char * dst,const char * src,size_t siz)102 strlcat(char *dst, const char *src, size_t siz)
103 {
104 char *d = dst;
105 const char *s = src;
106 size_t n = siz;
107 size_t dlen;
108
109 /* Find the end of dst and adjust bytes left but don't go past end */
110 while (n-- != 0 && *d != '\0')
111 d++;
112 dlen = d - dst;
113 n = siz - dlen;
114
115 if (n == 0)
116 return (dlen + strlen(s));
117
118 while (*s != '\0') {
119 if (n != 1) {
120 *d++ = *s;
121 n--;
122 }
123
124 s++;
125 }
126
127 *d = '\0';
128
129 return (dlen + (s - src)); /* count does not include NUL */
130 }
131 #endif
132