Skip to content

Commit

Permalink
remove xbps_strl{cat,cpy} and use strl{cat,cpy} directly
Browse files Browse the repository at this point in the history
  • Loading branch information
Duncaen committed Sep 1, 2023
1 parent cf5afa8 commit 51e3bef
Show file tree
Hide file tree
Showing 26 changed files with 168 additions and 178 deletions.
4 changes: 2 additions & 2 deletions bin/xbps-alternatives/main.c
Original file line number Diff line number Diff line change
Expand Up @@ -230,9 +230,9 @@ main(int argc, char **argv)
memset(&xh, 0, sizeof(xh));
xh.state_cb = state_cb;
if (rootdir)
xbps_strlcpy(xh.rootdir, rootdir, sizeof(xh.rootdir));
strlcpy(xh.rootdir, rootdir, sizeof(xh.rootdir));
if (confdir)
xbps_strlcpy(xh.confdir, confdir, sizeof(xh.confdir));
strlcpy(xh.confdir, confdir, sizeof(xh.confdir));

xh.flags = flags;

Expand Down
8 changes: 4 additions & 4 deletions bin/xbps-checkvers/main.c
Original file line number Diff line number Diff line change
Expand Up @@ -113,10 +113,10 @@ rcv_init(rcv_t *rcv, const char *prog)
assert(rcv->cache);

if (rcv->xbps_conf != NULL) {
xbps_strlcpy(rcv->xhp.confdir, rcv->xbps_conf, sizeof(rcv->xhp.confdir));
strlcpy(rcv->xhp.confdir, rcv->xbps_conf, sizeof(rcv->xhp.confdir));
}
if (rcv->rootdir != NULL) {
xbps_strlcpy(rcv->xhp.rootdir, rcv->rootdir, sizeof(rcv->xhp.rootdir));
strlcpy(rcv->xhp.rootdir, rcv->rootdir, sizeof(rcv->xhp.rootdir));
}
if (xbps_init(&rcv->xhp) != 0)
abort();
Expand Down Expand Up @@ -820,8 +820,8 @@ main(int argc, char **argv)
if ((p = strrchr(argv[i], '/')) && (strcmp(p, "/template")) == 0) {
tmpl = argv[i];
} else {
xbps_strlcat(tmp, argv[i], sizeof tmp);
xbps_strlcat(tmp, "/template", sizeof tmp);
strlcat(tmp, argv[i], sizeof tmp);
strlcat(tmp, "/template", sizeof tmp);
tmpl = tmp;
}
rcv_process_file(&rcv, tmpl, rcv_check_version);
Expand Down
4 changes: 2 additions & 2 deletions bin/xbps-dgraph/main.c
Original file line number Diff line number Diff line change
Expand Up @@ -598,9 +598,9 @@ main(int argc, char **argv)
/* Initialize libxbps */
memset(&xh, 0, sizeof(xh));
if (rootdir != NULL)
xbps_strlcpy(xh.rootdir, rootdir, sizeof(xh.rootdir));
strlcpy(xh.rootdir, rootdir, sizeof(xh.rootdir));
if (confdir)
xbps_strlcpy(xh.confdir, confdir, sizeof(xh.confdir));
strlcpy(xh.confdir, confdir, sizeof(xh.confdir));

xh.flags = flags;
if ((rv = xbps_init(&xh)) != 0)
Expand Down
6 changes: 3 additions & 3 deletions bin/xbps-install/main.c
Original file line number Diff line number Diff line change
Expand Up @@ -216,11 +216,11 @@ main(int argc, char **argv)
xh.fetch_cb = fetch_file_progress_cb;
xh.fetch_cb_data = &xfer;
if (rootdir)
xbps_strlcpy(xh.rootdir, rootdir, sizeof(xh.rootdir));
strlcpy(xh.rootdir, rootdir, sizeof(xh.rootdir));
if (cachedir)
xbps_strlcpy(xh.cachedir, cachedir, sizeof(xh.cachedir));
strlcpy(xh.cachedir, cachedir, sizeof(xh.cachedir));
if (confdir)
xbps_strlcpy(xh.confdir, confdir, sizeof(xh.confdir));
strlcpy(xh.confdir, confdir, sizeof(xh.confdir));
xh.flags = flags;
if (flags & XBPS_FLAG_VERBOSE)
xh.unpack_cb = unpack_progress_cb;
Expand Down
4 changes: 2 additions & 2 deletions bin/xbps-pkgdb/main.c
Original file line number Diff line number Diff line change
Expand Up @@ -144,9 +144,9 @@ main(int argc, char **argv)

memset(&xh, 0, sizeof(xh));
if (rootdir)
xbps_strlcpy(xh.rootdir, rootdir, sizeof(xh.rootdir));
strlcpy(xh.rootdir, rootdir, sizeof(xh.rootdir));
if (confdir)
xbps_strlcpy(xh.confdir, confdir, sizeof(xh.confdir));
strlcpy(xh.confdir, confdir, sizeof(xh.confdir));
xh.flags = flags;

if ((rv = xbps_init(&xh)) != 0) {
Expand Down
6 changes: 3 additions & 3 deletions bin/xbps-query/main.c
Original file line number Diff line number Diff line change
Expand Up @@ -241,11 +241,11 @@ main(int argc, char **argv)
* Initialize libxbps.
*/
if (rootdir)
xbps_strlcpy(xh.rootdir, rootdir, sizeof(xh.rootdir));
strlcpy(xh.rootdir, rootdir, sizeof(xh.rootdir));
if (cachedir)
xbps_strlcpy(xh.cachedir, cachedir, sizeof(xh.cachedir));
strlcpy(xh.cachedir, cachedir, sizeof(xh.cachedir));
if (confdir)
xbps_strlcpy(xh.confdir, confdir, sizeof(xh.confdir));
strlcpy(xh.confdir, confdir, sizeof(xh.confdir));

xh.flags = flags;

Expand Down
4 changes: 2 additions & 2 deletions bin/xbps-reconfigure/main.c
Original file line number Diff line number Diff line change
Expand Up @@ -168,9 +168,9 @@ main(int argc, char **argv)
memset(&xh, 0, sizeof(xh));
xh.state_cb = state_cb;
if (rootdir)
xbps_strlcpy(xh.rootdir, rootdir, sizeof(xh.rootdir));
strlcpy(xh.rootdir, rootdir, sizeof(xh.rootdir));
if (confdir)
xbps_strlcpy(xh.confdir, confdir, sizeof(xh.confdir));
strlcpy(xh.confdir, confdir, sizeof(xh.confdir));

xh.flags = flags;

Expand Down
2 changes: 1 addition & 1 deletion bin/xbps-remove/clean-cache.c
Original file line number Diff line number Diff line change
Expand Up @@ -43,7 +43,7 @@ static int
binpkg_parse(char *buf, size_t bufsz, const char *path, const char **pkgver, const char **arch)
{
char *p;
size_t n = xbps_strlcpy(buf, path, bufsz);
size_t n = strlcpy(buf, path, bufsz);
if (n >= bufsz)
return -ENOBUFS;

Expand Down
6 changes: 3 additions & 3 deletions bin/xbps-remove/main.c
Original file line number Diff line number Diff line change
Expand Up @@ -251,11 +251,11 @@ main(int argc, char **argv)
memset(&xh, 0, sizeof(xh));
xh.state_cb = state_cb_rm;
if (rootdir)
xbps_strlcpy(xh.rootdir, rootdir, sizeof(xh.rootdir));
strlcpy(xh.rootdir, rootdir, sizeof(xh.rootdir));
if (cachedir)
xbps_strlcpy(xh.cachedir, cachedir, sizeof(xh.cachedir));
strlcpy(xh.cachedir, cachedir, sizeof(xh.cachedir));
if (confdir)
xbps_strlcpy(xh.confdir, confdir, sizeof(xh.confdir));
strlcpy(xh.confdir, confdir, sizeof(xh.confdir));

xh.flags = flags;

Expand Down
4 changes: 2 additions & 2 deletions bin/xbps-uhelper/main.c
Original file line number Diff line number Diff line change
Expand Up @@ -146,9 +146,9 @@ main(int argc, char **argv)
xh.fetch_cb_data = &xfer;
xh.flags = flags;
if (rootdir)
xbps_strlcpy(xh.rootdir, rootdir, sizeof(xh.rootdir));
strlcpy(xh.rootdir, rootdir, sizeof(xh.rootdir));
if (confdir)
xbps_strlcpy(xh.confdir, confdir, sizeof(xh.confdir));
strlcpy(xh.confdir, confdir, sizeof(xh.confdir));
if ((rv = xbps_init(&xh)) != 0) {
xbps_error_printf("xbps-uhelper: failed to "
"initialize libxbps: %s.\n", strerror(rv));
Expand Down
2 changes: 1 addition & 1 deletion configure
Original file line number Diff line number Diff line change
Expand Up @@ -195,7 +195,7 @@ echo "CC = $CC" >>$CONFIG_MK
echo "CFLAGS = -O2" >>$CONFIG_MK

echo "LDFLAGS = -L\$(TOPDIR)/lib" >>$CONFIG_MK
echo "CPPFLAGS = -I. -I\$(TOPDIR) -I\$(TOPDIR)/include" >>$CONFIG_MK
echo "CPPFLAGS = -I\$(TOPDIR)/lib/compat -I. -I\$(TOPDIR) -I\$(TOPDIR)/include" >>$CONFIG_MK
echo "CPPFLAGS += -DXBPS_SYSCONF_PATH=\\\"${ETCDIR}\\\"" >>$CONFIG_MK
echo "CPPFLAGS += -DXBPS_SYSDEFCONF_PATH=\\\"${SHAREDIR}/xbps.d\\\"" >>$CONFIG_MK
echo "CPPFLAGS += -DXBPS_VERSION=\\\"${VERSION}\\\"" >>$CONFIG_MK
Expand Down
15 changes: 0 additions & 15 deletions include/compat.h
Original file line number Diff line number Diff line change
Expand Up @@ -8,21 +8,6 @@

#include "macro.h"

#ifndef HAVE_STRLCAT
size_t HIDDEN strlcat(char *, const char *, size_t);
#endif

#ifndef HAVE_STRLCPY
size_t HIDDEN strlcpy(char *, const char *, size_t);
#endif

#ifndef HAVE_STRCASESTR
char HIDDEN *strcasestr(const char *, const char *);
#endif

#ifndef HAVE_VASPRINTF
int HIDDEN vasprintf(char **, const char *, va_list);
#endif

#ifndef HAVE_HUMANIZE_NUMBER
#define HN_DECIMAL 0x01
Expand Down
10 changes: 10 additions & 0 deletions lib/compat/stdio.h
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
#ifndef XBPS_COMPAT_STDIO_H
#define XBPS_COMPAT_STDIO_H

#include_next <stdio.h>

#ifndef HAVE_VASPRINTF
int vasprintf(char **, const char *, va_list);
#endif

#endif /*!XBPS_COMPAT_STDIO_H*/
18 changes: 18 additions & 0 deletions lib/compat/string.h
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
#ifndef XBPS_COMPAT_STRING_H
#define XBPS_COMPAT_STRING_H

#include_next <string.h>

#ifndef HAVE_STRLCAT
size_t strlcat(char *, const char *, size_t);
#endif

#ifndef HAVE_STRLCPY
size_t strlcpy(char *, const char *, size_t);
#endif

#ifndef HAVE_STRCASESTR
char *strcasestr(const char *, const char *);
#endif

#endif /*!XBPS_COMPAT_STRING_H*/
52 changes: 25 additions & 27 deletions lib/compat/strlcat.c
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
/* $OpenBSD: strlcat.c,v 1.13 2005/08/08 08:05:37 espie Exp $ */
/* $OpenBSD: strlcat.c,v 1.19 2019/01/25 00:19:25 millert Exp $ */

/*
* Copyright (c) 1998 Todd C. Miller <[email protected]>
* Copyright (c) 1998, 2015 Todd C. Miller <[email protected]>
*
* Permission to use, copy, modify, and distribute this software for any
* purpose with or without fee is hereby granted, provided that the above
Expand All @@ -19,39 +19,37 @@
#include <sys/types.h>
#include <string.h>

#include "compat.h"

/*
* Appends src to string dst of size siz (unlike strncat, siz is the
* full size of dst, not space left). At most siz-1 characters
* will be copied. Always NUL terminates (unless siz <= strlen(dst)).
* Returns strlen(src) + MIN(siz, strlen(initial dst)).
* If retval >= siz, truncation occurred.
* Appends src to string dst of size dsize (unlike strncat, dsize is the
* full size of dst, not space left). At most dsize-1 characters
* will be copied. Always NUL terminates (unless dsize <= strlen(dst)).
* Returns strlen(src) + MIN(dsize, strlen(initial dst)).
* If retval >= dsize, truncation occurred.
*/
size_t HIDDEN
strlcat(char *dst, const char *src, size_t siz)
size_t
strlcat(char *dst, const char *src, size_t dsize)
{
char *d = dst;
const char *s = src;
size_t n = siz;
const char *odst = dst;
const char *osrc = src;
size_t n = dsize;
size_t dlen;

/* Find the end of dst and adjust bytes left but don't go past end */
while (n-- != 0 && *d != '\0')
d++;
dlen = d - dst;
n = siz - dlen;
/* Find the end of dst and adjust bytes left but don't go past end. */
while (n-- != 0 && *dst != '\0')
dst++;
dlen = dst - odst;
n = dsize - dlen;

if (n == 0)
return(dlen + strlen(s));
while (*s != '\0') {
if (n != 1) {
*d++ = *s;
if (n-- == 0)
return(dlen + strlen(src));
while (*src != '\0') {
if (n != 0) {
*dst++ = *src;
n--;
}
s++;
src++;
}
*d = '\0';
*dst = '\0';

return(dlen + (s - src)); /* count does not include NUL */
return(dlen + (src - osrc)); /* count does not include NUL */
}
41 changes: 19 additions & 22 deletions lib/compat/strlcpy.c
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
/* $OpenBSD: strlcpy.c,v 1.11 2006/05/05 15:27:38 millert Exp $ */
/* $OpenBSD: strlcpy.c,v 1.16 2019/01/25 00:19:25 millert Exp $ */

/*
* Copyright (c) 1998 Todd C. Miller <[email protected]>
* Copyright (c) 1998, 2015 Todd C. Miller <[email protected]>
*
* Permission to use, copy, modify, and distribute this software for any
* purpose with or without fee is hereby granted, provided that the above
Expand All @@ -19,35 +19,32 @@
#include <sys/types.h>
#include <string.h>

#include "compat.h"

/*
* Copy src to string dst of size siz. At most siz-1 characters
* will be copied. Always NUL terminates (unless siz == 0).
* Returns strlen(src); if retval >= siz, truncation occurred.
* Copy string src to buffer dst of size dsize. At most dsize-1
* chars will be copied. Always NUL terminates (unless dsize == 0).
* Returns strlen(src); if retval >= dsize, truncation occurred.
*/
size_t HIDDEN
strlcpy(char *dst, const char *src, size_t siz)
size_t
strlcpy(char *dst, const char *src, size_t dsize)
{
char *d = dst;
const char *s = src;
size_t n = siz;
const char *osrc = src;
size_t nleft = dsize;

/* Copy as many bytes as will fit */
if (n != 0) {
while (--n != 0) {
if ((*d++ = *s++) == '\0')
/* Copy as many bytes as will fit. */
if (nleft != 0) {
while (--nleft != 0) {
if ((*dst++ = *src++) == '\0')
break;
}
}

/* Not enough room in dst, add NUL and traverse rest of src */
if (n == 0) {
if (siz != 0)
*d = '\0'; /* NUL-terminate dst */
while (*s++)
/* Not enough room in dst, add NUL and traverse rest of src. */
if (nleft == 0) {
if (dsize != 0)
*dst = '\0'; /* NUL-terminate dst */
while (*src++)
;
}

return(s - src - 1); /* count does not include NUL */
return(src - osrc - 1); /* count does not include NUL */
}
2 changes: 1 addition & 1 deletion lib/conf.c
Original file line number Diff line number Diff line change
Expand Up @@ -109,7 +109,7 @@ store_preserved_file(struct xbps_handle *xhp, const char *file)
len = strlen(globbuf.gl_pathv[i]) - strlen(xhp->rootdir) + 1;
p = malloc(len);
assert(p);
xbps_strlcpy(p, globbuf.gl_pathv[i] + strlen(xhp->rootdir), len);
strlcpy(p, globbuf.gl_pathv[i] + strlen(xhp->rootdir), len);
xbps_array_add_cstring(xhp->preserved_files, p);
xbps_dbg_printf("Added preserved file: %s (expanded from %s)\n", p, file);
free(p);
Expand Down
4 changes: 2 additions & 2 deletions lib/download.c
Original file line number Diff line number Diff line change
Expand Up @@ -122,7 +122,7 @@ xbps_fetch_file_dest_sha256(struct xbps_handle *xhp, const char *uri, const char

memset(&fetch_flags, 0, sizeof(fetch_flags));
if (flags != NULL)
xbps_strlcpy(fetch_flags, flags, 7);
strlcpy(fetch_flags, flags, 7);

tempfile = xbps_xasprintf("%s.part", filename);
/*
Expand All @@ -145,7 +145,7 @@ xbps_fetch_file_dest_sha256(struct xbps_handle *xhp, const char *uri, const char
if (stat(filename, &st) == 0) {
refetch = true;
url->last_modified = st.st_mtime;
xbps_strlcat(fetch_flags, "i", sizeof(fetch_flags));
strlcat(fetch_flags, "i", sizeof(fetch_flags));
} else {
if (errno != ENOENT) {
rv = -1;
Expand Down
2 changes: 1 addition & 1 deletion lib/external/dewey.c
Original file line number Diff line number Diff line change
Expand Up @@ -331,7 +331,7 @@ dewey_match(const char *pattern, const char *pkg)
if (sep2) {
char ver[PKG_PATTERN_MAX];

xbps_strlcpy(ver, sep, MIN((ssize_t)sizeof(ver), sep2-sep+1));
strlcpy(ver, sep, MIN((ssize_t)sizeof(ver), sep2-sep+1));
if (dewey_cmp(version, op, ver))
return 1;
} else {
Expand Down
4 changes: 2 additions & 2 deletions lib/initend.c
Original file line number Diff line number Diff line change
Expand Up @@ -114,7 +114,7 @@ xbps_init(struct xbps_handle *xhp)

/* allow to overwrite uname(3) and conf file with env variable */
if ((native_arch = getenv("XBPS_ARCH")) && *native_arch != '\0') {
if (xbps_strlcpy(xhp->native_arch, native_arch,
if (strlcpy(xhp->native_arch, native_arch,
sizeof xhp->native_arch) >= sizeof xhp->native_arch)
return ENOBUFS;
}
Expand All @@ -123,7 +123,7 @@ xbps_init(struct xbps_handle *xhp)
struct utsname un;
if (uname(&un) == -1)
return ENOTSUP;
if (xbps_strlcpy(xhp->native_arch, un.machine,
if (strlcpy(xhp->native_arch, un.machine,
sizeof xhp->native_arch) >= sizeof xhp->native_arch)
return ENOBUFS;
}
Expand Down
Loading

0 comments on commit 51e3bef

Please sign in to comment.