Use errx() instead of perror()/exit() for conciseness.

Suggested by:	ru (some time ago)
This commit is contained in:
Robert Watson 2004-10-23 22:11:35 +00:00
parent 9a5cf32635
commit a09c60ffeb
3 changed files with 51 additions and 93 deletions

View File

@ -33,6 +33,7 @@
#include <arpa/inet.h>
#include <errno.h>
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
@ -69,20 +70,14 @@ tcpconnect_server(int argc, char *argv[])
sin.sin_port = htons(port);
listen_sock = socket(PF_INET, SOCK_STREAM, 0);
if (listen_sock == -1) {
perror("socket");
exit(-1);
}
if (listen_sock == -1)
errx(-1, "socket: %s", strerror(errno));
if (bind(listen_sock, (struct sockaddr *)&sin, sizeof(sin)) == -1) {
perror("bind");
exit(-1);
}
if (bind(listen_sock, (struct sockaddr *)&sin, sizeof(sin)) == -1)
errx(-1, "bind: %s", strerror(errno));
if (listen(listen_sock, -1) == -1) {
perror("listen");
exit(1);
}
if (listen(listen_sock, -1) == -1)
errx(-1, "listen: %s", strerror(errno));
while (1) {
accept_sock = accept(listen_sock, NULL, NULL);
@ -104,10 +99,8 @@ tcpconnect_client(int argc, char *argv[])
bzero(&sin, sizeof(sin));
sin.sin_len = sizeof(sin);
sin.sin_family = AF_INET;
if (inet_aton(argv[0], &sin.sin_addr) == 0) {
perror(argv[0]);
exit(-1);
}
if (inet_aton(argv[0], &sin.sin_addr) == 0)
errx(-1, "listen: %x", strerror(errno));
port = strtoul(argv[1], &dummy, 10);
if (port < 1 || port > 65535 || *dummy != '\0')
@ -120,15 +113,11 @@ tcpconnect_client(int argc, char *argv[])
for (i = 0; i < count; i++) {
sock = socket(PF_INET, SOCK_STREAM, 0);
if (sock == -1) {
perror("socket");
exit(-1);
}
if (sock == -1)
errx(-1, "socket: %s", strerror(errno));
if (connect(sock, (struct sockaddr *)&sin, sizeof(sin)) == -1) {
perror("connect");
exit(-1);
}
if (connect(sock, (struct sockaddr *)&sin, sizeof(sin)) == -1)
errx(-1, "connect: %s", strerror(errno));
close(sock);
}

View File

@ -40,6 +40,8 @@
#include <arpa/inet.h>
#include <err.h>
#include <errno.h>
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
@ -88,15 +90,11 @@ tcpstream_client(struct sockaddr_in sin, long seed)
srandom(seed);
sock = socket(PF_INET, SOCK_STREAM, 0);
if (sock == -1) {
perror("socket");
exit(-1);
}
if (sock == -1)
errx(-1, "socket: %s", strerror(errno));
if (connect(sock, (struct sockaddr *) &sin, sizeof(sin)) == -1) {
perror("connect");
exit(-1);
}
if (connect(sock, (struct sockaddr *) &sin, sizeof(sin)) == -1)
errx(-1, "connect: %s", strerror(errno));
for (j = 0; j < MAX_LOOPS; j++) {
for (i = 0; i < MAX_LONGS; i++) {
@ -128,20 +126,14 @@ tcpstream_server(struct sockaddr_in sin, long seed)
int input_byte_counter;
listen_sock = socket(PF_INET, SOCK_STREAM, 0);
if (listen_sock == -1) {
perror("socket");
exit(-1);
}
if (listen_sock == -1)
errx(-1, "socket: %s", strerror(errno));
if (bind(listen_sock, (struct sockaddr *)&sin, sizeof(sin)) == -1) {
perror("bind");
exit(-1);
}
if (bind(listen_sock, (struct sockaddr *)&sin, sizeof(sin)) == -1)
errx(-1, "bind: %s", strerror(errno));
if (listen(listen_sock, -1) == -1) {
perror("listen");
exit(-1);
}
if (listen(listen_sock, -1) == -1)
errx(-1, "listen: %s", strerror(errno));
while (1) {
bzero(&other_sin, sizeof(other_sin));
@ -201,11 +193,8 @@ main(int argc, char *argv[])
sin.sin_len = sizeof(sin);
sin.sin_family = AF_INET;
if (inet_aton(argv[2], &sin.sin_addr) != 1) {
errno = EINVAL;
perror(argv[2]);
exit(-1);
}
if (inet_aton(argv[2], &sin.sin_addr) != 1)
errx(-1, "%s: %s", argv[2], strerror(EINVAL));
port = strtoul(argv[3], &dummy, 10);
if (port < 1 || port > 65535 || *dummy != '\0')

View File

@ -31,6 +31,7 @@
#include <netinet/in.h>
#include <err.h>
#include <errno.h>
#include <fcntl.h>
#include <stdio.h>
@ -59,16 +60,12 @@ main(int argc, char *argv[])
*/
fd1 = dup(STDIN_FILENO);
fd2 = dup(STDIN_FILENO);
if (fd2 != fd1 + 1) {
fprintf(stderr, "Non-sequential fd allocation!\n");
exit(-1);
}
if (fd2 != fd1 + 1)
errx(-1, "Non-sequential fd allocation\n");
s = socket(PF_INET, SOCK_STREAM, 0);
if (s == -1) {
perror("socket");
exit(-1);
}
if (s == -1)
errx(-1, "socket: %s", strerror(errno));
bzero(&sin, sizeof(sin));
sin.sin_len = sizeof(sin);
@ -76,46 +73,30 @@ main(int argc, char *argv[])
sin.sin_addr.s_addr = htonl(INADDR_LOOPBACK);
sin.sin_port = htons(8080);
if (bind(s, (struct sockaddr *) &sin, sizeof(sin)) != 0) {
perror("bind");
exit(-1);
}
if (bind(s, (struct sockaddr *) &sin, sizeof(sin)) != 0)
errx(-1, "bind: %s", strerror(errno));
if (listen(s, -1) != 0) {
perror("listen");
exit(-1);
}
if (listen(s, -1) != 0)
errx(-1, "listen: %s", strerror(errno));
i = fcntl(s, F_GETFL);
if (i == -1) {
perror("F_GETFL");
exit(-1);
}
if (i == -1)
errx(-1, "ioctl(F_GETFL): %s", strerror(errno));
i |= O_NONBLOCK;
if (fcntl(s, F_SETFL, i) != 0) {
perror("F_SETFL");
exit(-1);
}
if (fcntl(s, F_SETFL, i) != 0)
errx(-1, "ioctl(F_SETFL): %s", strerror(errno));
i = fcntl(s, F_GETFL);
if (i == -1) {
perror("F_GETFL");
exit(-1);
}
if ((i & O_NONBLOCK) != O_NONBLOCK) {
fprintf(stderr, "Failed to set O_NONBLOCK (i=%d)\n", i);
exit(-1);
}
if (i == -1)
errx(-1, "ioctl(F_GETFL): %s", strerror(errno));
if ((i & O_NONBLOCK) != O_NONBLOCK)
errx(-1, "Failed to set O_NONBLOCK (i=0x%x)\n", i);
for (i = 0; i < LOOPS; i++) {
size = sizeof(sin);
if (accept(s, (struct sockaddr *)&sin, &size) != -1) {
fprintf(stderr, "accept succeeded!\n");
exit(-1);
}
if (errno != EAGAIN) {
perror("accept");
exit(-1);
}
if (accept(s, (struct sockaddr *)&sin, &size) != -1)
errx(-1, "accept succeeded\n");
if (errno != EAGAIN)
errx(-1, "accept: %s", strerror(errno));
}
/*
@ -123,10 +104,9 @@ main(int argc, char *argv[])
* we allocate an fd for the socket.
*/
fd3 = dup(STDIN_FILENO);
if (fd3 != fd2 + 2) {
fprintf(stderr, "FAIL (%d, %d, %d)\n", fd1, fd2, fd3);
exit(-1);
} else
if (fd3 != fd2 + 2)
errx(-1, "FAIL (%d, %d, %d)\n", fd1, fd2, fd3);
else
fprintf(stderr, "PASS\n");
return (0);