Amit Daniel Kachhap | e6a01f5 | 2011-07-20 11:45:59 +0530 | [diff] [blame] | 1 | /**************************************************************************** |
micky387 | 9b9f5e7 | 2025-07-08 18:04:53 -0400 | [diff] [blame] | 2 | * Copyright 2020,2023 Thomas E. Dickey * |
| 3 | * Copyright 2000-2012,2017 Free Software Foundation, Inc. * |
Amit Daniel Kachhap | e6a01f5 | 2011-07-20 11:45:59 +0530 | [diff] [blame] | 4 | * * |
| 5 | * Permission is hereby granted, free of charge, to any person obtaining a * |
| 6 | * copy of this software and associated documentation files (the * |
| 7 | * "Software"), to deal in the Software without restriction, including * |
| 8 | * without limitation the rights to use, copy, modify, merge, publish, * |
| 9 | * distribute, distribute with modifications, sublicense, and/or sell * |
| 10 | * copies of the Software, and to permit persons to whom the Software is * |
| 11 | * furnished to do so, subject to the following conditions: * |
| 12 | * * |
| 13 | * The above copyright notice and this permission notice shall be included * |
| 14 | * in all copies or substantial portions of the Software. * |
| 15 | * * |
| 16 | * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS * |
| 17 | * OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF * |
| 18 | * MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. * |
| 19 | * IN NO EVENT SHALL THE ABOVE COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, * |
| 20 | * DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR * |
| 21 | * OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR * |
| 22 | * THE USE OR OTHER DEALINGS IN THE SOFTWARE. * |
| 23 | * * |
| 24 | * Except as contained in this notice, the name(s) of the above copyright * |
| 25 | * holders shall not be used in advertising or otherwise to promote the * |
| 26 | * sale, use or other dealings in this Software without prior written * |
| 27 | * authorization. * |
| 28 | ****************************************************************************/ |
| 29 | |
| 30 | /**************************************************************************** |
| 31 | * Author: Thomas E. Dickey * |
| 32 | ****************************************************************************/ |
| 33 | |
| 34 | /* |
| 35 | ** lib_mvcur.c |
| 36 | **/ |
| 37 | |
| 38 | #include <curses.priv.h> |
micky387 | 9b9f5e7 | 2025-07-08 18:04:53 -0400 | [diff] [blame] | 39 | #include <tic.h> |
Amit Daniel Kachhap | e6a01f5 | 2011-07-20 11:45:59 +0530 | [diff] [blame] | 40 | |
micky387 | 9b9f5e7 | 2025-07-08 18:04:53 -0400 | [diff] [blame] | 41 | MODULE_ID("$Id: strings.c,v 1.11 2023/05/27 20:13:10 tom Exp $") |
Amit Daniel Kachhap | e6a01f5 | 2011-07-20 11:45:59 +0530 | [diff] [blame] | 42 | |
| 43 | /**************************************************************************** |
| 44 | * Useful string functions (especially for mvcur) |
| 45 | ****************************************************************************/ |
| 46 | |
| 47 | #if !HAVE_STRSTR |
| 48 | NCURSES_EXPORT(char *) |
| 49 | _nc_strstr(const char *haystack, const char *needle) |
| 50 | { |
| 51 | size_t len1 = strlen(haystack); |
| 52 | size_t len2 = strlen(needle); |
| 53 | char *result = 0; |
| 54 | |
| 55 | while ((len1 != 0) && (len1-- >= len2)) { |
| 56 | if (!strncmp(haystack, needle, len2)) { |
| 57 | result = (char *) haystack; |
| 58 | break; |
| 59 | } |
| 60 | haystack++; |
| 61 | } |
| 62 | return result; |
| 63 | } |
| 64 | #endif |
| 65 | |
| 66 | /* |
| 67 | * Initialize the descriptor so we can append to it. Note that 'src' may |
| 68 | * be a null pointer (see _nc_str_null), so the corresponding strcat and |
| 69 | * strcpy calls have to allow for this. |
| 70 | */ |
| 71 | NCURSES_EXPORT(string_desc *) |
| 72 | _nc_str_init(string_desc * dst, char *src, size_t len) |
| 73 | { |
| 74 | if (dst != 0) { |
| 75 | dst->s_head = src; |
| 76 | dst->s_tail = src; |
| 77 | dst->s_size = len - 1; |
| 78 | dst->s_init = dst->s_size; |
| 79 | if (src != 0) |
| 80 | *src = 0; |
| 81 | } |
| 82 | return dst; |
| 83 | } |
| 84 | |
| 85 | /* |
| 86 | * Initialize the descriptor for only tracking the amount of memory used. |
| 87 | */ |
| 88 | NCURSES_EXPORT(string_desc *) |
| 89 | _nc_str_null(string_desc * dst, size_t len) |
| 90 | { |
| 91 | return _nc_str_init(dst, 0, len); |
| 92 | } |
| 93 | |
| 94 | /* |
| 95 | * Copy a descriptor |
| 96 | */ |
| 97 | NCURSES_EXPORT(string_desc *) |
micky387 | 9b9f5e7 | 2025-07-08 18:04:53 -0400 | [diff] [blame] | 98 | _nc_str_copy(string_desc * dst, const string_desc * const src) |
Amit Daniel Kachhap | e6a01f5 | 2011-07-20 11:45:59 +0530 | [diff] [blame] | 99 | { |
| 100 | *dst = *src; |
| 101 | return dst; |
| 102 | } |
| 103 | |
| 104 | /* |
| 105 | * Replaces strcat into a fixed buffer, returning false on failure. |
| 106 | */ |
| 107 | NCURSES_EXPORT(bool) |
| 108 | _nc_safe_strcat(string_desc * dst, const char *src) |
| 109 | { |
micky387 | 9b9f5e7 | 2025-07-08 18:04:53 -0400 | [diff] [blame] | 110 | if (PRESENT(src)) { |
Amit Daniel Kachhap | e6a01f5 | 2011-07-20 11:45:59 +0530 | [diff] [blame] | 111 | size_t len = strlen(src); |
| 112 | |
| 113 | if (len < dst->s_size) { |
| 114 | if (dst->s_tail != 0) { |
Steve Kondik | ae271bc | 2015-11-15 02:50:53 +0100 | [diff] [blame] | 115 | _nc_STRCPY(dst->s_tail, src, dst->s_size); |
Amit Daniel Kachhap | e6a01f5 | 2011-07-20 11:45:59 +0530 | [diff] [blame] | 116 | dst->s_tail += len; |
| 117 | } |
| 118 | dst->s_size -= len; |
| 119 | return TRUE; |
| 120 | } |
| 121 | } |
| 122 | return FALSE; |
| 123 | } |
| 124 | |
| 125 | /* |
| 126 | * Replaces strcpy into a fixed buffer, returning false on failure. |
| 127 | */ |
| 128 | NCURSES_EXPORT(bool) |
| 129 | _nc_safe_strcpy(string_desc * dst, const char *src) |
| 130 | { |
micky387 | 9b9f5e7 | 2025-07-08 18:04:53 -0400 | [diff] [blame] | 131 | if (PRESENT(src)) { |
Amit Daniel Kachhap | e6a01f5 | 2011-07-20 11:45:59 +0530 | [diff] [blame] | 132 | size_t len = strlen(src); |
| 133 | |
| 134 | if (len < dst->s_size) { |
| 135 | if (dst->s_head != 0) { |
Steve Kondik | ae271bc | 2015-11-15 02:50:53 +0100 | [diff] [blame] | 136 | _nc_STRCPY(dst->s_head, src, dst->s_size); |
Amit Daniel Kachhap | e6a01f5 | 2011-07-20 11:45:59 +0530 | [diff] [blame] | 137 | dst->s_tail = dst->s_head + len; |
| 138 | } |
| 139 | dst->s_size = dst->s_init - len; |
| 140 | return TRUE; |
| 141 | } |
| 142 | } |
| 143 | return FALSE; |
| 144 | } |