Skip to content

Commit d749ff0

Browse files
committed
Avoid bzero/bcopy/bcmp/strlcpy() to not depend on libbsd
1 parent 5bced87 commit d749ff0

File tree

10 files changed

+40
-41
lines changed

10 files changed

+40
-41
lines changed

GNUmakefile

Lines changed: 0 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -20,8 +20,6 @@ ifeq ($(OS),FreeBSD)
2020
CFLAGS+= -D_ST_FLAGS_PRESENT_
2121
else ifeq ($(OS),Linux)
2222
CFLAGS+= -D_GNU_SOURCE -D_FILE_OFFSET_BITS=64
23-
CFLAGS+= $(shell pkg-config --cflags libbsd-overlay)
24-
LIBS+= $(shell pkg-config --libs libbsd-overlay)
2523
endif
2624

2725
PREFIX?= /usr/local

README.md

Lines changed: 3 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -40,14 +40,13 @@ Installation
4040
* `make` (GNU make)
4141
* `gcc`
4242
* `pkg-config`
43-
* `libbsd-dev` (Required only on Linux)
4443
* `libssl-dev` (OpenSSL >= 1.0.2 or LibreSSL)
4544

46-
Arch Linux: `pacman -S pkgconf libbsd openssl`
45+
Arch Linux: `pacman -S pkgconf openssl`
4746

48-
CentOS: `yum install pkgconfig libbsd-devel openssl-devel`
47+
CentOS: `yum install pkgconfig openssl-devel`
4948

50-
Debian: `apt install pkg-config libbsd-dev libssl-dev`
49+
Debian: `apt install pkg-config libssl-dev`
5150

5251
DragonFly BSD / FreeBSD: `pkg install gmake pkgconf libressl`
5352

linux/PKGBUILD

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,7 @@ pkgdesc="Filesystem mirroring utility from DragonFly BSD"
77
url="https://github.com/DragonFlyBSD/cpdup"
88
license=('BSD')
99
arch=('i686' 'x86_64')
10-
depends=('libbsd' 'openssl')
10+
depends=('openssl')
1111
makedepends=('pkg-config')
1212

1313
build() {

linux/cpdup.spec

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -7,8 +7,8 @@ License: BSD
77
URL: https://github.com/DragonFlyBSD/cpdup
88

99
BuildRequires: make, gcc, binutils
10-
BuildRequires: pkgconfig, libbsd-devel, openssl-devel
11-
Requires: libbsd, openssl
10+
BuildRequires: pkgconfig, openssl-devel
11+
Requires: openssl
1212

1313
%description
1414
The "cpdup" utility makes an exact mirror copy of the source in the

linux/debian/control

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,6 @@ Priority: optional
33
Maintainer: Aaron LI <aly@aaronly.me>
44
Build-Depends:
55
debhelper-compat (= 13),
6-
libbsd-dev,
76
libssl-dev,
87
make,
98
pkgconf | pkg-config

src/cpdup.c

Lines changed: 9 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -363,7 +363,7 @@ main(int ac, char **av)
363363
fprintf(stderr, "Group[%d] == %d\n", i, GroupList[i]);
364364
#endif
365365

366-
bzero(&info, sizeof(info));
366+
memset(&info, 0, sizeof(info));
367367
if (dst) {
368368
DstBaseLen = strlen(dst);
369369
info.spath = src;
@@ -564,7 +564,7 @@ hltadd(struct stat *stp, const char *path)
564564
new->ino = stp->st_ino;
565565
new->dino = (ino_t)-1;
566566
new->refs = 1;
567-
bcopy(path, new->name, plen + 1);
567+
memcpy(new->name, path, plen + 1);
568568
new->nlinked = 1;
569569
new->prev = NULL;
570570
n = stp->st_ino & HLMASK;
@@ -677,7 +677,7 @@ validate_check(const char *spath, const char *dpath)
677677
CountTargetReadBytes += x;
678678
if (x != n)
679679
break;
680-
if (bcmp(iobuf1, iobuf2, n) != 0)
680+
if (memcmp(iobuf1, iobuf2, n) != 0)
681681
break;
682682
}
683683
free(iobuf1);
@@ -1054,7 +1054,7 @@ DoCopy(copy_info_t info, struct stat *stat1, int depth)
10541054
hc_chflags(&DstHost, dpath, stat1->st_flags);
10551055
#endif
10561056
if (ForceOpt || mtimecmp(stat1, &st2) != 0) {
1057-
bzero(tv, sizeof(tv));
1057+
memset(tv, 0, sizeof(tv));
10581058
tv[0].tv_sec = stat1->st_mtime;
10591059
tv[1].tv_sec = stat1->st_mtime;
10601060
#if defined(st_mtime) /* A macro, so very likely on modern POSIX */
@@ -1164,7 +1164,7 @@ DoCopy(copy_info_t info, struct stat *stat1, int depth)
11641164
if (n == 0) {
11651165
struct timeval tv[2];
11661166

1167-
bzero(tv, sizeof(tv));
1167+
memset(tv, 0, sizeof(tv));
11681168
tv[0].tv_sec = stat1->st_mtime;
11691169
tv[1].tv_sec = stat1->st_mtime;
11701170
#if defined(st_mtime)
@@ -1254,12 +1254,12 @@ DoCopy(copy_info_t info, struct stat *stat1, int depth)
12541254
n2 = -1;
12551255
}
12561256
if (n1 >= 0) {
1257-
if (ForceOpt || n1 != n2 || bcmp(link1, link2, n1) != 0 ||
1257+
if (ForceOpt || n1 != n2 || memcmp(link1, link2, n1) != 0 ||
12581258
(st2Valid && symlink_mfo_test(&DstHost, stat1, &st2))
12591259
) {
12601260
struct timeval tv[2];
12611261

1262-
bzero(tv, sizeof(tv));
1262+
memset(tv, 0, sizeof(tv));
12631263
tv[0].tv_sec = stat1->st_mtime;
12641264
tv[1].tv_sec = stat1->st_mtime;
12651265
#if defined(st_mtime)
@@ -1433,7 +1433,7 @@ ScanDir(List *list, struct HostConf *host, const char *path,
14331433
}
14341434
bufused = strlen(next);
14351435
if (bufused)
1436-
bcopy(next, buf, bufused);
1436+
memcpy(buf, next, bufused);
14371437
}
14381438
if (bufused) {
14391439
/* last line has no trailing newline */
@@ -1572,7 +1572,7 @@ RemoveRecur(const char *dpath, dev_t devNo, struct stat *dstat)
15721572
static void
15731573
InitList(List *list)
15741574
{
1575-
bzero(list, sizeof(List));
1575+
memset(list, 0, sizeof(List));
15761576
list->li_Node.no_Next = &list->li_Node;
15771577
}
15781578

src/cpdup.h

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -43,7 +43,6 @@
4343
#include <stddef.h>
4444
#include <stdint.h>
4545
#include <string.h>
46-
#include <strings.h>
4746
#include <errno.h>
4847
#include <unistd.h>
4948
#include <utime.h>

src/hclink.c

Lines changed: 6 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -117,9 +117,9 @@ hcc_slave(int fdin, int fdout, struct HCDesc *descs, int count)
117117
int i;
118118
int r;
119119

120-
bzero(&hcslave, sizeof(hcslave));
121-
bzero(&trans, sizeof(trans));
122-
bzero(dispatch, sizeof(dispatch));
120+
memset(&hcslave, 0, sizeof(hcslave));
121+
memset(&trans, 0, sizeof(trans));
122+
memset(dispatch, 0, sizeof(dispatch));
123123
for (i = 0; i < count; ++i) {
124124
struct HCDesc *desc = &descs[i];
125125
assert(desc->cmd >= 0 && desc->cmd < 256);
@@ -212,7 +212,7 @@ hcc_read_command(struct HostConf *hc, hctransaction_t trans)
212212
assert(tmp.bytes >= (int)sizeof(tmp) && tmp.bytes < HC_BUFSIZE);
213213

214214
trans->swap = need_swap;
215-
bcopy(&tmp, trans->rbuf, n);
215+
memcpy(trans->rbuf, &tmp, n);
216216
aligned_bytes = HCC_ALIGN(tmp.bytes);
217217

218218
while (n < aligned_bytes) {
@@ -358,7 +358,7 @@ hcc_leaf_string(hctransaction_t trans, int16_t leafid, const char *str)
358358
item->leafid = leafid;
359359
item->reserved = 0;
360360
item->bytes = sizeof(*item) + bytes;
361-
bcopy(str, item + 1, bytes);
361+
memcpy(item + 1, str, bytes);
362362
trans->windex = HCC_ALIGN(trans->windex + item->bytes);
363363
}
364364

@@ -372,7 +372,7 @@ hcc_leaf_data(hctransaction_t trans, int16_t leafid, const void *ptr, int bytes)
372372
item->leafid = leafid;
373373
item->reserved = 0;
374374
item->bytes = sizeof(*item) + bytes;
375-
bcopy(ptr, item + 1, bytes);
375+
memcpy(item + 1, ptr, bytes);
376376
trans->windex = HCC_ALIGN(trans->windex + item->bytes);
377377
}
378378

src/hcproto.c

Lines changed: 16 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -171,7 +171,7 @@ hc_hello(struct HostConf *hc)
171171
char hostbuf[256];
172172
int error;
173173

174-
bzero(hostbuf, sizeof(hostbuf));
174+
memset(hostbuf, 0, sizeof(hostbuf));
175175
if (gethostname(hostbuf, sizeof(hostbuf) - 1) < 0)
176176
return(-1);
177177
if (hostbuf[0] == 0)
@@ -232,7 +232,7 @@ rc_hello(hctransaction_t trans, struct HCHead *head)
232232
UseCpFile = strdup(HCC_STRING(item));
233233
}
234234

235-
bzero(hostbuf, sizeof(hostbuf));
235+
memset(hostbuf, 0, sizeof(hostbuf));
236236
if (gethostname(hostbuf, sizeof(hostbuf) - 1) < 0)
237237
return(-1);
238238
if (hostbuf[0] == 0)
@@ -287,7 +287,7 @@ hc_decode_stat(hctransaction_t trans, struct stat *st, struct HCHead *head)
287287
{
288288
struct HCLeaf *item;
289289

290-
bzero(st, sizeof(*st));
290+
memset(st, 0, sizeof(*st));
291291
FOR_EACH_ITEM(item, trans, head)
292292
hc_decode_stat_item(st, item);
293293
return(0);
@@ -511,7 +511,8 @@ hc_readdir(struct HostConf *hc, DIR *dir, struct stat **statpp)
511511

512512
if ((sysden = readdir(dir)) == NULL)
513513
return (NULL);
514-
strlcpy(denbuf.d_name, sysden->d_name, sizeof(denbuf.d_name));
514+
strncpy(denbuf.d_name, sysden->d_name, sizeof(denbuf.d_name) - 1);
515+
denbuf.d_name[sizeof(denbuf.d_name) - 1] = '\0';
515516
return (&denbuf);
516517
}
517518

@@ -530,8 +531,10 @@ hc_readdir(struct HostConf *hc, DIR *dir, struct stat **statpp)
530531
return (NULL); /* XXX errno */
531532
den->d_name[0] = 0;
532533
FOR_EACH_ITEM(item, trans, head) {
533-
if (item->leafid == LC_PATH1)
534-
strlcpy(den->d_name, HCC_STRING(item), sizeof(den->d_name));
534+
if (item->leafid == LC_PATH1) {
535+
strncpy(den->d_name, HCC_STRING(item), sizeof(den->d_name) - 1);
536+
den->d_name[sizeof(den->d_name) - 1] = '\0';
537+
}
535538
}
536539
return (den->d_name[0] ? den : NULL);
537540
}
@@ -540,10 +543,11 @@ hc_readdir(struct HostConf *hc, DIR *dir, struct stat **statpp)
540543
denbuf.d_name[0] = 0;
541544
head = (void *)dir;
542545
*statpp = malloc(sizeof(struct stat));
543-
bzero(*statpp, sizeof(struct stat));
546+
memset(*statpp, 0, sizeof(struct stat));
544547
while ((item = hcc_nextchaineditem(hc, head)) != NULL) {
545548
if (item->leafid == LC_PATH1) { /* this must be the last item */
546-
strlcpy(denbuf.d_name, HCC_STRING(item), sizeof(denbuf.d_name));
549+
strncpy(denbuf.d_name, HCC_STRING(item), sizeof(denbuf.d_name) - 1);
550+
denbuf.d_name[sizeof(denbuf.d_name) - 1] = '\0';
547551
break;
548552
} else {
549553
stat_ok = 1;
@@ -917,7 +921,7 @@ hc_read(struct HostConf *hc, int fd, void *buf, size_t bytes)
917921
}
918922
else
919923
head->magic = 0; /* all bytes used up */
920-
bcopy((char *)HCC_BINARYDATA(item) + offset, buf, x);
924+
memcpy(buf, (char *)HCC_BINARYDATA(item) + offset, x);
921925
buf = (char *)buf + x;
922926
bytes -= (size_t)x;
923927
r += x;
@@ -943,7 +947,7 @@ hc_read(struct HostConf *hc, int fd, void *buf, size_t bytes)
943947
x = item->bytes - sizeof(*item);
944948
if (x > (int)bytes)
945949
x = (int)bytes;
946-
bcopy(HCC_BINARYDATA(item), buf, x);
950+
memcpy(buf, HCC_BINARYDATA(item), x);
947951
buf = (char *)buf + x;
948952
bytes -= (size_t)x;
949953
r += x;
@@ -1695,7 +1699,7 @@ hc_readlink(struct HostConf *hc, const char *path, char *buf, int bufsiz)
16951699
r = 0;
16961700
if (r > bufsiz)
16971701
r = bufsiz;
1698-
bcopy(HCC_BINARYDATA(item), buf, r);
1702+
memcpy(buf, HCC_BINARYDATA(item), r);
16991703
}
17001704
}
17011705
return(r);
@@ -1931,7 +1935,7 @@ rc_utimes(hctransaction_t trans, struct HCHead *head)
19311935
struct timeval times[2];
19321936
const char *path;
19331937

1934-
bzero(times, sizeof(times));
1938+
memset(times, 0, sizeof(times));
19351939
path = NULL;
19361940

19371941
FOR_EACH_ITEM(item, trans, head) {

src/md5.c

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -154,7 +154,7 @@ md5_lookup(const char *spath)
154154
if ((node = malloc(sizeof(MD5Node))) == NULL)
155155
fatal("out of memory");
156156

157-
bzero(node, sizeof(MD5Node));
157+
memset(node, 0, sizeof(MD5Node));
158158
node->md_Name = strdup(sfile);
159159
node->md_Next = MD5Base;
160160
MD5Base = node;
@@ -183,7 +183,7 @@ md5_update(const char *spath)
183183
r = 0;
184184
if (strcmp(scode, node->md_Code) != 0) {
185185
r = 1;
186-
bcopy(scode, node->md_Code, sizeof(scode));
186+
memcpy(node->md_Code, scode, sizeof(scode));
187187
MD5SCacheDirty = 1;
188188
}
189189
} else {
@@ -222,7 +222,7 @@ md5_check(const char *spath, const char *dpath)
222222
*/
223223
if (md5_file(spath, scode, 0 /* is_target */) == 0) {
224224
if (strcmp(node->md_Code, scode) != 0) {
225-
bcopy(scode, node->md_Code, sizeof(scode));
225+
memcpy(node->md_Code, scode, sizeof(scode));
226226
MD5SCacheDirty = 1;
227227
if (strcmp(node->md_Code, dcode) == 0)
228228
r = 0;

0 commit comments

Comments
 (0)