-
Notifications
You must be signed in to change notification settings - Fork 3
/
rhstr.c
257 lines (197 loc) · 6.88 KB
/
rhstr.c
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
/*
* rawhide - find files using pretty C expressions
* https://raf.org/rawhide
* https://github.com/raforg/rawhide
* https://codeberg.org/raforg/rawhide
*
* Copyright (C) 1990 Ken Stauffer, 2022-2023 raf <raf@raf.org>
*
* This program is free software; you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by
* the Free Software Foundation; either version 3 of the License, or
* (at your option) any later version.
*
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
*
* You should have received a copy of the GNU General Public License
* along with this program; if not, see <https://www.gnu.org/licenses/>.
*
* 20231013 raf <raf@raf.org>
*/
/*
$OpenBSD: strlcpy.c,v 1.4 1999/05/01 18:56:41 millert Exp $
$OpenBSD: strlcat.c,v 1.5 2001/01/13 16:17:24 millert Exp $
Copyright (c) 1998 Todd C. Miller <Todd.Miller@courtesan.com>
All rights reserved.
Redistribution and use in source and binary forms, with or without
modification, are permitted provided that the following conditions
are met:
1. Redistributions of source code must retain the above copyright
notice, this list of conditions and the following disclaimer.
2. Redistributions in binary form must reproduce the above copyright
notice, this list of conditions and the following disclaimer in the
documentation and/or other materials provided with the distribution.
3. The name of the author may not be used to endorse or promote products
derived from this software without specific prior written permission.
THIS SOFTWARE IS PROVIDED ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES,
INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY
AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL
THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL,
EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO,
PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS;
OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY,
WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR
OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF
ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
*/
#define _FILE_OFFSET_BITS 64 /* For 64-bit off_t on 32-bit systems (Not AIX) */
#include <stdarg.h>
#include <stdio.h>
#include <string.h>
#include <sys/stat.h>
#include "rh.h"
#include "rhstr.h"
/*
size_t strlcpy(char *dst, const char *src, size_t size);
Copy src to dst (which is size bytes). The result will be no longer than
size-1 bytes, and will be nul-terminated (unless size is zero). This is like
strncpy, but it always terminates the string with a nul byte (so it's safer)
and it doesn't fill the remainder of the buffer with nul bytes (so it's
faster). Returns the length of src (If this is >= size, truncation
occurred). Use this rather than strcpy or strncpy.
*/
#ifndef strlcpy /* macOS */
size_t strlcpy(char *dst, const char *src, size_t size)
{
const char *s = src;
char *d = dst;
size_t n = size;
if (n)
while (--n && (*d++ = *s++))
{}
if (n == 0)
{
if (size)
*d = '\0';
while (*s++)
{}
}
return s - src - 1;
}
#endif
/*
size_t strlcat(char *dst, const char *src, size_t size);
Appends src to dst (which is size bytes). The result will be no longer than
size-1 bytes, and will be nul-terminated (unless size is zero). This is like
strncat but the last argument is the size of the buffer not the amount of
space available (so it's more intuitive and hence safer). Returns the sum of
the lengths of src and dst (If this is >= size, truncation occurred). Use
this rather than strcat or strncat.
*/
#ifndef strlcat /* macOS */
size_t strlcat(char *dst, const char *src, size_t size)
{
const char *s = src;
char *d = dst;
size_t n = size;
size_t dlen = 0;
while (n-- && *d)
++d;
dlen = d - dst;
if (++n == 0)
{
while (*s++)
{}
return dlen + s - src - 1;
}
for (; *s; ++s)
if (n - 1)
--n, *d++ = *s;
*d = '\0';
return dlen + s - src;
}
#endif
/*
int ssnprintf(char *str, ssize_t size, const char *format, ...);
A snprintf wrapper whose size parameter is signed so we can detect negative
size remaining and not overwrite the buffer. When that happens, return zero.
*/
int ssnprintf(char *str, ssize_t size, const char *format, ...)
{
va_list args;
int rc;
if (size <= 0)
return 0;
va_start(args, format);
rc = vsnprintf(str, (size_t)size, format, args);
va_end(args);
return rc;
}
/*
int cescape(char *dst, ssize_t dstsz, const char *src, ssize_t srcsz, int opts);
Copy src to dst using C escapes. At most dstsz bytes are written.
If srcsz is -1, src is nul-terminated. Otherwise, the given number
of bytes are processed. The opts bitmask can contain CESCAPE_QUOTES (to quote "),
CESCAPE_HEX (to use hex not octal), CESCAPE_JSON (for \uxxxx ctrls and no \a\v),
and CESCAPE_BIN (to hex all 8bit).
*/
int cescape(char *dst, ssize_t dstsz, const char *src, ssize_t srcsz, int opts)
{
static char *escape = "\013\007\010\011\012\014\015\\";
static char *escaped = "vabtnfr\\";
char *e;
int i, pos = 0;
int j = (opts & CESCAPE_JSON) == CESCAPE_JSON ? 2 : 0;
for (i = 0; (srcsz == -1) ? src[i] : i < srcsz; ++i)
{
if (src[i] == '\0')
pos += ssnprintf(dst + pos, dstsz - pos, "\\0");
else if ((e = strchr(escape + j, src[i])))
pos += ssnprintf(dst + pos, dstsz - pos, "\\%c", (escaped + j)[e - (escape + j)]);
else if (isquotable(src[i]))
pos += ssnprintf(dst + pos, dstsz - pos, (j) ? "\\u00%02x" : (opts & CESCAPE_HEX) ? "\\x%02x" : "\\%03o", (unsigned char)src[i]);
else if ((opts & CESCAPE_QUOTES) && src[i] == '"')
pos += ssnprintf(dst + pos, dstsz - pos, "\\\"");
else if ((opts & CESCAPE_BIN) == CESCAPE_BIN && (src[i] & 0x80))
pos += ssnprintf(dst + pos, dstsz - pos, "\\x%02x", (unsigned char)src[i]);
else
pos += ssnprintf(dst + pos, dstsz - pos, "%c", (unsigned char)src[i]);
}
return pos;
}
/* Sanitise text for errors */
static const char *sanitise(char *buf, size_t sz, const char *str)
{
char *s;
if (!attr.ttyerr)
return str;
for (s = buf; (s - buf) < (sz - 1) && *str; ++str)
*s++ = isquotable(*str) ? '?' : *str;
*s = '\0';
return buf;
}
const char *ok(const char *str)
{
static char buf[8192];
return sanitise(buf, 8192, str);
}
/* The same, to allow two uses in one statement */
const char *ok2(const char *str)
{
static char buf[8192];
return sanitise(buf, 8192, str);
}
/* The same, for when it's not nul-terminated */
const char *oklen(const char *str, size_t len)
{
static char buf[1024];
char *s;
for (s = buf; len > 0; ++str, --len)
*s++ = (attr.ttyerr && isquotable(*str)) ? '?' : *str;
*s = '\0';
return buf;
}
/* vi:set ts=4 sw=4: */