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