sdhcp

simple dhcp client
git clone git://git.2f30.org/sdhcp.git
Log | Files | Refs | LICENSE

commit ecd7d1ab7d75dae0f468bacd43b985f37e61f01c
parent c1aa3d309eb59561dbc17845ee9fdb73efa51679
Author: Hiltjo Posthuma <hiltjo@codemadness.org>
Date:   Fri Apr 25 20:59:41 +0200

add strlcpy

Signed-off-by: Hiltjo Posthuma <hiltjo@codemadness.org>

Diffstat:
sdhcp.c | 4++--
util.h | 5+++++
util/strlcpy.c | 32++++++++++++++++++++++++++++++++
3 files changed, 39 insertions(+), 2 deletions(-)
diff --git a/sdhcp.c b/sdhcp.c @@ -141,7 +141,7 @@ setip(unsigned char ip[4], unsigned char mask[4], unsigned char gateway[4]) struct rtentry rtreq = {0,}; fd = socket(PF_INET, SOCK_DGRAM, IPPROTO_IP); - strcpy(ifreq.ifr_name, ifname); /*TODO: strlcpy */ + strlcpy(ifreq.ifr_name, ifname, IF_NAMESIZE); ifreq.ifr_addr = iptoaddr(ip, 0); ioctl(fd, SIOCSIFADDR , &ifreq); ifreq.ifr_netmask = iptoaddr(mask, 0); @@ -445,7 +445,7 @@ main(int argc, char *argv[]) if(setsockopt(sock, SOL_SOCKET, SO_BROADCAST, &bcast, sizeof bcast) == -1) die("setsockopt"); - strcpy(ifreq.ifr_name, ifname); /* TODO: strlcpy */ + strlcpy(ifreq.ifr_name, ifname, IF_NAMESIZE); ioctl(sock, SIOCGIFINDEX, &ifreq); if(setsockopt(sock, SOL_SOCKET, SO_BINDTODEVICE, &ifreq, sizeof ifreq) == -1) die("setsockopt"); diff --git a/util.h b/util.h @@ -0,0 +1,5 @@ +#define MIN(a,b) (((a)<(b))?(a):(b)) +#define bpdump(p,n) 1 + +#undef strlcpy +size_t strlcpy(char *, const char *, size_t); diff --git a/util/strlcpy.c b/util/strlcpy.c @@ -0,0 +1,32 @@ +/* Taken from OpenBSD */ +#include <sys/types.h> +#include <string.h> +#include "../util.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. + */ +size_t +strlcpy(char *dst, const char *src, size_t siz) +{ + char *d = dst; + const char *s = src; + size_t n = siz; + /* Copy as many bytes as will fit */ + if (n != 0) { + while (--n != 0) { + if ((*d++ = *s++) == '\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++) + ; + } + return(s - src - 1); /* count does not include NUL */ +}