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