Helper code cleanup

Signed-off-by: Claudio Matsuoka <cmatsuoka@gmail.com>
This commit is contained in:
Claudio Matsuoka committed 2008-02-17 08:42:57 -03:00
1 parent d17c3160bd
commit cdbc2c536d
1 file changed
+21 -25
+21 -25
View File
@@ -43,20 +43,22 @@ void ifconfig(char *name, char *inet, char *mask, int up)
memset(&ifr, 0, sizeof (ifr)); memset(&ifr, 0, sizeof (ifr));
strncpy(ifr.ifr_name, name, IFNAMSIZ); strncpy(ifr.ifr_name, name, IFNAMSIZ);
a->sin_family = AF_INET; a->sin_family = AF_INET;
inet_aton(inet, &a->sin_addr);
if (up) { if (up) {
inet_aton(inet, &a->sin_addr);
ioctl(sock, SIOCSIFADDR, &ifr); ioctl(sock, SIOCSIFADDR, &ifr);
inet_aton(mask, &a->sin_addr); inet_aton(mask, &a->sin_addr);
ioctl(sock, SIOCSIFNETMASK, &ifr); ioctl(sock, SIOCSIFNETMASK, &ifr);
ioctl(sock, SIOCGIFFLAGS, &ifr);
ifr.ifr_flags |= IFF_UP;
ioctl(sock, SIOCSIFFLAGS, &ifr);
} else {
ioctl(sock, SIOCGIFFLAGS, &ifr);
ifr.ifr_flags &= ~IFF_UP;
ioctl(sock, SIOCSIFFLAGS, &ifr);
} }
ioctl(sock, SIOCGIFFLAGS, &ifr);
if (up)
ifr.ifr_flags |= IFF_UP;
else
ifr.ifr_flags &= ~IFF_UP;
ioctl(sock, SIOCSIFFLAGS, &ifr);
close(sock); close(sock);
} }
@@ -68,25 +70,19 @@ void copyfile(char *src, char *dst, int size)
char buffer[BUF_SIZE]; char buffer[BUF_SIZE];
int s, d, n; int s, d, n;
/* Size == 0 means copy entire file */
if (size == 0)
size = INT_MAX;
if ((s = open(src, O_RDONLY)) >= 0) { if ((s = open(src, O_RDONLY)) >= 0) {
if ((d = open(dst, O_WRONLY | O_CREAT, 0644)) >= 0) { if ((d = open(dst, O_WRONLY | O_CREAT, 0644)) >= 0) {
do {
/* Size == 0 means copy entire file */ int csize = size > BUF_SIZE ? BUF_SIZE : size;
if (size == 0) {
do { if ((n = read(s, buffer, csize)) > 0)
if ((n = read(s, buffer, BUF_SIZE)) > 0) write(d, buffer, n);
write(d, buffer, n); size -= csize;
} while (n == BUF_SIZE); } while (size > 0 && n == BUF_SIZE);
} else {
do {
int csize = size > BUF_SIZE ?
BUF_SIZE : size;
if ((n = read(s, buffer, csize)) > 0)
write(d, buffer, n);
size -= csize;
} while (size > 0 && n == BUF_SIZE);
}
close(d); close(d);
} }
close(s); close(s);