sdhcp

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

commit 19e0ab7cc747ad5f6f239e484531a5fec0626e14
parent 8559ac1f08e7592d9f9b142e11125e210d6e7f5b
Author: Hiltjo Posthuma <hiltjo@codemadness.org>
Date:   Fri, 25 Apr 2014 21:50:25 +0200

add eprintf(), weprintf()

use it instead of die()

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

Diffstat:
MMakefile | 5+++--
Msdhcp.c | 20++++++++++++--------
Mutil.h | 4++++
Autil/eprintf.c | 67+++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++
4 files changed, 86 insertions(+), 10 deletions(-)

diff --git a/Makefile b/Makefile @@ -3,9 +3,10 @@ include config.mk .POSIX: .SUFFIXES: .c .o -HDR = util.h +HDR = util.h arg.h LIB = \ - util/strlcpy.o + util/strlcpy.o \ + util/eprintf.o SRC = sdhcp.c diff --git a/sdhcp.c b/sdhcp.c @@ -194,8 +194,10 @@ static void setdns(unsigned char dns[4]) { char buf[128], *bp = buf; - int fd = creat("/etc/resolv.conf", 0644); + int fd; + if((fd = creat("/etca/resolv.conf", 0644)) == -1) + weprintf("can't change /etc/resolv.conf:"); cat(fd, "/etc/resolv.conf.head"); memcpy(buf, "\nnameserver ", 12), bp+=11; *(bp = itoa(bp+1, dns[0])) = '.'; @@ -441,20 +443,22 @@ main(int argc, char *argv[]) signal(SIGTERM, cleanexit); if((sock = socket(AF_INET, SOCK_DGRAM, 0)) == -1) - die("socket"); + eprintf("socket:"); if(setsockopt(sock, SOL_SOCKET, SO_BROADCAST, &bcast, sizeof bcast) == -1) - die("setsockopt"); + eprintf("setsockopt:"); strlcpy(ifreq.ifr_name, ifname, IF_NAMESIZE); ioctl(sock, SIOCGIFINDEX, &ifreq); if(setsockopt(sock, SOL_SOCKET, SO_BINDTODEVICE, &ifreq, sizeof ifreq) == -1) - die("setsockopt"); - addr = iptoaddr(IP(255,255,255,255), 68); - if(bind(sock, (void*)&addr, sizeof addr)!=0) - die("bind"); + eprintf("setsockopt:"); + addr = iptoaddr(IP(255, 255, 255, 255), 68); + if(bind(sock, (void*)&addr, sizeof addr) != 0) + eprintf("bind:"); ioctl(sock, SIOCGIFHWADDR, &ifreq); memcpy(hwaddr, ifreq.ifr_hwaddr.sa_data, sizeof ifreq.ifr_hwaddr.sa_data); - rnd = open("/dev/urandom", O_RDONLY); + + if((rnd = open("/dev/urandom", O_RDONLY) == -1)) + eprintf("can't open /dev/urandom to generate unique transaction identifier:"); read(rnd, xid, sizeof xid); close(rnd); diff --git a/util.h b/util.h @@ -3,3 +3,7 @@ #undef strlcpy size_t strlcpy(char *, const char *, size_t); + +void weprintf(const char *, ...); +void eprintf(const char *, ...); +void enprintf(int, const char *, ...); diff --git a/util/eprintf.c b/util/eprintf.c @@ -0,0 +1,67 @@ +/* See LICENSE file for copyright and license details. */ +#include <stdarg.h> +#include <stdio.h> +#include <stdlib.h> +#include <string.h> + +#include "../util.h" + +char *argv0; + +static void venprintf(int, const char *, va_list); + +void +eprintf(const char *fmt, ...) +{ + va_list ap; + + va_start(ap, fmt); + venprintf(EXIT_FAILURE, fmt, ap); + va_end(ap); +} + +void +enprintf(int status, const char *fmt, ...) +{ + va_list ap; + + va_start(ap, fmt); + venprintf(status, fmt, ap); + va_end(ap); +} + +void +venprintf(int status, const char *fmt, va_list ap) +{ +#ifdef DEBUG + fprintf(stderr, "%s: ", argv0); +#endif + + vfprintf(stderr, fmt, ap); + + if(fmt[0] && fmt[strlen(fmt)-1] == ':') { + fputc(' ', stderr); + perror(NULL); + } + + exit(status); +} + +void +weprintf(const char *fmt, ...) +{ + va_list ap; + +#ifdef DEBUG + fprintf(stderr, "%s: ", argv0); +#endif + + va_start(ap, fmt); + vfprintf(stderr, fmt, ap); + va_end(ap); + + if (fmt[0] && fmt[strlen(fmt)-1] == ':') { + fputc(' ', stderr); + perror(NULL); + } +}