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