|
| 1 | +/** |
| 2 | + * Marlin 3D Printer Firmware |
| 3 | + * Copyright (c) 2023 MarlinFirmware [https://github.com/MarlinFirmware/Marlin] |
| 4 | + * |
| 5 | + * Based on Sprinter and grbl. |
| 6 | + * Copyright (c) 2011 Camiel Gubbels / Erik van der Zalm |
| 7 | + * |
| 8 | + * This program is free software: you can redistribute it and/or modify |
| 9 | + * it under the terms of the GNU General Public License as published by |
| 10 | + * the Free Software Foundation, either version 3 of the License, or |
| 11 | + * (at your option) any later version. |
| 12 | + * |
| 13 | + * This program is distributed in the hope that it will be useful, |
| 14 | + * but WITHOUT ANY WARRANTY; without even the implied warranty of |
| 15 | + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the |
| 16 | + * GNU General Public License for more details. |
| 17 | + * |
| 18 | + * You should have received a copy of the GNU General Public License |
| 19 | + * along with this program. If not, see <https://www.gnu.org/licenses/>. |
| 20 | + * |
| 21 | + */ |
| 22 | + |
| 23 | +/** |
| 24 | + * Copyright (c) 1998, 2015 Todd C. Miller <Todd.Miller@courtesan.com> |
| 25 | + * |
| 26 | + * Permission to use, copy, modify, and distribute this software for any |
| 27 | + * purpose with or without fee is hereby granted, provided that the above |
| 28 | + * copyright notice and this permission notice appear in all copies. |
| 29 | + * |
| 30 | + * THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES |
| 31 | + * WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF |
| 32 | + * MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR |
| 33 | + * ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES |
| 34 | + * WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN |
| 35 | + * ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF |
| 36 | + * OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE. |
| 37 | + */ |
| 38 | + |
| 39 | +#ifdef __PLAT_NATIVE_SIM__ |
| 40 | + |
| 41 | +#ifndef HAS_LIBBSD |
| 42 | + |
| 43 | + #include "HAL.h" |
| 44 | + |
| 45 | + /** |
| 46 | + * Copy string src to buffer dst of size dsize. At most dsize-1 |
| 47 | + * chars will be copied. Always NUL terminates (unless dsize == 0). |
| 48 | + * Returns strlen(src); if retval >= dsize, truncation occurred. |
| 49 | + */ |
| 50 | + size_t MarlinHAL::_strlcpy(char *dst, const char *src, size_t dsize) { |
| 51 | + const char *osrc = src; |
| 52 | + size_t nleft = dsize; |
| 53 | + |
| 54 | + // Copy as many bytes as will fit. |
| 55 | + if (nleft != 0) while (--nleft != 0) if ((*dst++ = *src++) == '\0') break; |
| 56 | + |
| 57 | + // Not enough room in dst, add NUL and traverse rest of src. |
| 58 | + if (nleft == 0) { |
| 59 | + if (dsize != 0) *dst = '\0'; // NUL-terminate dst |
| 60 | + while (*src++) { /* nada */ } |
| 61 | + } |
| 62 | + |
| 63 | + return (src - osrc - 1); // count does not include NUL |
| 64 | + } |
| 65 | + |
| 66 | +#endif // HAS_LIBBSD |
| 67 | +#endif // __PLAT_NATIVE_SIM__ |
0 commit comments