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 "osi/include/compat.h"
29
30 #include <features.h>
31 #include <string.h>
32 #include <sys/syscall.h>
33 #include <sys/types.h>
34 #include <unistd.h>
35
36 #include "osi/include/osi.h"
37
38 #if __GLIBC__
gettid(void)39 pid_t gettid(void) throw() { return syscall(SYS_gettid); }
40
41 /* These functions from bionic
42 *
43 * Copyright (c) 1998 Todd C. Miller <[email protected]>
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 #endif /* __GLIBC__ */
58
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 */
osi_strlcpy(char * dst,const char * src,size_t siz)64 size_t osi_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') {
73 break;
74 }
75 }
76 }
77
78 /* Not enough room in dst, add NUL and traverse rest of src */
79 if (n == 0) {
80 if (siz != 0) {
81 *d = '\0'; /* NUL-terminate dst */
82 }
83 while (*s++)
84 ;
85 }
86
87 return s - src - 1; /* count does not include NUL */
88 }
89