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.cc
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
34 #include "osi/include/compat.h"
35 #include "osi/include/osi.h"
36
37 #if __GLIBC__
gettid(void)38 pid_t gettid(void) { return syscall(SYS_gettid); }
39 #endif
40
41 /* These functions from bionic
42 *
43 * Copyright (c) 1998 Todd C. Miller <Todd.Miller@courtesan.com>
44 *
45 * Permission to use, copy, modify, and distribute this software for any
46 * purpose with or without fee is hereby granted, provided that the above
47 * copyright notice and this permission notice appear in all copies.
48 *
49 * THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES
50 * WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF
51 * MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR
52 * ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES
53 * WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN
54 * ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF
55 * OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE.
56 */
57
58 #if __GLIBC__
59 /*
60 * Copy src to string dst of size siz. At most siz-1 characters
61 * will be copied. Always NUL terminates (unless siz == 0).
62 * Returns strlen(src); if retval >= siz, truncation occurred.
63 */
strlcpy(char * dst,const char * src,size_t siz)64 size_t strlcpy(char* dst, const char* src, size_t siz) {
65 char* d = dst;
66 const char* s = src;
67 size_t n = siz;
68
69 /* Copy as many bytes as will fit */
70 if (n != 0) {
71 while (--n != 0) {
72 if ((*d++ = *s++) == '\0') break;
73 }
74 }
75
76 /* Not enough room in dst, add NUL and traverse rest of src */
77 if (n == 0) {
78 if (siz != 0) *d = '\0'; /* NUL-terminate dst */
79 while (*s++)
80 ;
81 }
82
83 return (s - src - 1); /* count does not include NUL */
84 }
85 #endif
86
87 #if __GLIBC__
88 /*
89 * Appends src to string dst of size siz (unlike strncat, siz is the
90 * full size of dst, not space left). At most siz-1 characters
91 * will be copied. Always NUL terminates (unless siz <= strlen(dst)).
92 * Returns strlen(src) + MIN(siz, strlen(initial dst)).
93 * If retval >= siz, truncation occurred.
94 */
strlcat(char * dst,const char * src,size_t siz)95 size_t strlcat(char* dst, const char* src, size_t siz) {
96 char* d = dst;
97 const char* s = src;
98 size_t n = siz;
99 size_t dlen;
100
101 /* Find the end of dst and adjust bytes left but don't go past end */
102 while (n-- != 0 && *d != '\0') d++;
103 dlen = d - dst;
104 n = siz - dlen;
105
106 if (n == 0) return (dlen + strlen(s));
107
108 while (*s != '\0') {
109 if (n != 1) {
110 *d++ = *s;
111 n--;
112 }
113
114 s++;
115 }
116
117 *d = '\0';
118
119 return (dlen + (s - src)); /* count does not include NUL */
120 }
121 #endif
122