Perform inconsequential code cleanup

This commit is contained in:
Justine Tunney 2023-08-07 20:22:49 -07:00
parent 929478c524
commit decf216655
No known key found for this signature in database
GPG key ID: BE714B4575D6E328
52 changed files with 326 additions and 442 deletions

View file

@ -46,11 +46,11 @@
* @asyncsignalsafe
*/
void *memccpy(void *dst, const void *src, int c, size_t n) {
char *d;
size_t i;
unsigned char *d;
const unsigned char *s;
const char *s;
for (d = dst, s = src, i = 0; i < n; ++i) {
if ((d[i] = s[i]) == (c & 255)) {
if (((d[i] = s[i]) & 255) == (c & 255)) {
return d + i + 1;
}
}

View file

@ -16,27 +16,30 @@
TORTIOUS ACTION, ARISING OUT OF OR IN CONNECTION WITH THE USE OR
PERFORMANCE OF THIS SOFTWARE.
*/
#include "libc/macros.internal.h"
#include "libc/str/str.h"
/**
* Prepares static search buffer.
*
* 1. If SRC is too long, it's truncated and *not* NUL-terminated.
*
* 2. If SRC is too short, the remainder is zero-filled.
*
* Please note this function isn't designed to prevent untrustworthy
* data from modifying memory without authorization. Consider trying
* memccpy() for that purpose.
*
* @return dest + stride
* @see stncpy(), memccpy()
* @param dst is output buffer
* @param src is a nul-terminated string
* @param dstlen is size of `dst` buffer
* @return pointer to first nul-terminator, otherwise dest + stride
* @asyncsignalsafe
* @see strncpy()
* @see memccpy()
* @see strlcpy()
*/
char *stpncpy(char *dest, const char *src, size_t stride) {
char *p;
if ((p = memccpy(dest, src, '\0', stride))) {
bzero(p, dest + stride - p);
}
return dest + stride;
char *stpncpy(char *dst, const char *src, size_t dstlen) {
size_t srclen, cpylen, zerlen;
srclen = strlen(src);
cpylen = MIN(srclen, dstlen);
if (cpylen) memcpy(dst, src, cpylen);
zerlen = dstlen - cpylen;
if (zerlen) bzero(dst + cpylen, zerlen);
return dst + cpylen;
}

View file

@ -27,12 +27,14 @@
* @param 𝑛 is maximum number of characters from s to copy
* @return 𝑑
* @note 𝑑 and 𝑠 can't overlap
* @asyncsignaslenafe
* @asyncsignalsafe
*/
char *strncat(char *d, const char *s, size_t n) {
size_t o;
if (!memccpy(d + (o = strlen(d)), s, '\0', n)) {
d[o + n] = '\0';
size_t dn, sn;
if ((sn = strnlen(s, n))) {
dn = strlen(d);
memcpy(d + dn, s, sn);
d[dn + sn] = 0;
}
return d;
}

View file

@ -16,6 +16,7 @@
TORTIOUS ACTION, ARISING OUT OF OR IN CONNECTION WITH THE USE OR
PERFORMANCE OF THIS SOFTWARE.
*/
#include "libc/macros.internal.h"
#include "libc/str/str.h"
/**
@ -24,16 +25,21 @@
* 1. If SRC is too long, it's truncated and *not* NUL-terminated.
* 2. If SRC is too short, the remainder is zero-filled.
*
* @return dest
* @see stpncpy(), memccpy()
* @param dst is output buffer
* @param src is a nul-terminated string
* @param dstlen is size of `dst` buffer
* @return dst
* @asyncsignalsafe
* @vforksafe
* @see stpncpy()
* @see strlcpy()
* @see memccpy()
*/
char *strncpy(char *dest, const char *src, size_t stride) {
size_t i;
for (i = 0; i < stride; ++i) {
if (!(dest[i] = src[i])) break;
}
bzero(dest + i, stride - i);
return dest;
char *strncpy(char *dst, const char *src, size_t dstlen) {
size_t srclen, cpylen, zerlen;
srclen = strlen(src);
cpylen = MIN(srclen, dstlen);
if (cpylen) memcpy(dst, src, cpylen);
zerlen = dstlen - cpylen;
if (zerlen) bzero(dst + cpylen, zerlen);
return dst;
}