1 /* $OpenBSD: strings.c,v 1.4 2010/01/12 23:22:06 nicm Exp $ */ 2 3 /**************************************************************************** 4 * Copyright (c) 2000-2003,2007 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("$Id: strings.c,v 1.4 2010/01/12 23:22:06 nicm Exp $") 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 *) 98 _nc_str_copy(string_desc * dst, string_desc * src) 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 { 110 if (src != 0) { 111 size_t len = strlen(src); 112 113 if (len < dst->s_size) { 114 if (dst->s_tail != 0) { 115 strlcpy(dst->s_tail, src, dst->s_size); 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 { 131 if (src != 0) { 132 size_t len = strlen(src); 133 134 if (len < dst->s_size) { 135 if (dst->s_head != 0) { 136 strlcpy(dst->s_head, src, dst->s_size); 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 } 145