Rewrite nc to be simpler, smaller, and to check syscalls for errors.

This commit is contained in:
Matt Kraai 2000-12-15 22:34:34 +00:00
parent 8677d7b6ec
commit bfa7967c4a
5 changed files with 46 additions and 85 deletions

View File

@ -147,6 +147,7 @@ int makeString(int argc, const char **argv, char *buf, int bufLen);
char *getChunk(int size); char *getChunk(int size);
char *chunkstrdup(const char *str); char *chunkstrdup(const char *str);
void freeChunks(void); void freeChunks(void);
ssize_t safe_read(int fd, void *buf, size_t count);
int full_write(int fd, const char *buf, int len); int full_write(int fd, const char *buf, int len);
int full_read(int fd, char *buf, int len); int full_read(int fd, char *buf, int len);
int recursive_action(const char *fileName, int recurse, int followLinks, int depthFirst, int recursive_action(const char *fileName, int recurse, int followLinks, int depthFirst,

View File

@ -147,6 +147,7 @@ int makeString(int argc, const char **argv, char *buf, int bufLen);
char *getChunk(int size); char *getChunk(int size);
char *chunkstrdup(const char *str); char *chunkstrdup(const char *str);
void freeChunks(void); void freeChunks(void);
ssize_t safe_read(int fd, void *buf, size_t count);
int full_write(int fd, const char *buf, int len); int full_write(int fd, const char *buf, int len);
int full_read(int fd, char *buf, int len); int full_read(int fd, char *buf, int len);
int recursive_action(const char *fileName, int recurse, int followLinks, int depthFirst, int recursive_action(const char *fileName, int recurse, int followLinks, int depthFirst,

57
nc.c
View File

@ -38,14 +38,10 @@
#include <sys/time.h> #include <sys/time.h>
#include <sys/ioctl.h> #include <sys/ioctl.h>
#define BUFSIZE 100
int nc_main(int argc, char **argv) int nc_main(int argc, char **argv)
{ {
int sfd; int sfd;
int result; char buf[BUFSIZ];
int len;
char ch[BUFSIZE];
struct sockaddr_in address; struct sockaddr_in address;
struct hostent *hostinfo; struct hostent *hostinfo;
@ -54,34 +50,26 @@ int nc_main(int argc, char **argv)
argc--; argc--;
argv++; argv++;
if (argc < 2 || **(argv + 1) == '-') { if (argc < 2 || **argv == '-') {
usage(nc_usage); usage(nc_usage);
} }
sfd = socket(AF_INET, SOCK_STREAM, 0); if ((sfd = socket(AF_INET, SOCK_STREAM, 0)) < 0)
perror_msg_and_die("socket");
hostinfo = (struct hostent *) gethostbyname(*argv); if ((hostinfo = gethostbyname(*argv)) == NULL)
if (!hostinfo) {
error_msg_and_die("cannot resolve %s\n", *argv); error_msg_and_die("cannot resolve %s\n", *argv);
}
address.sin_family = AF_INET; address.sin_family = AF_INET;
address.sin_addr = *(struct in_addr *) *hostinfo->h_addr_list; address.sin_addr = *(struct in_addr *) *hostinfo->h_addr_list;
address.sin_port = htons(atoi(*(++argv))); address.sin_port = htons(atoi(*(++argv)));
len = sizeof(address); if (connect(sfd, (struct sockaddr *) &address, sizeof(address)) < 0)
perror_msg_and_die("connect");
result = connect(sfd, (struct sockaddr *) &address, len);
if (result < 0) {
perror("nc: connect");
exit(2);
}
FD_ZERO(&readfds); FD_ZERO(&readfds);
FD_SET(sfd, &readfds); FD_SET(sfd, &readfds);
FD_SET(fileno(stdin), &readfds); FD_SET(STDIN_FILENO, &readfds);
while (1) { while (1) {
int fd; int fd;
@ -90,41 +78,26 @@ int nc_main(int argc, char **argv)
testfds = readfds; testfds = readfds;
result = if (select(FD_SETSIZE, &testfds, NULL, NULL, NULL) < 0)
select(FD_SETSIZE, &testfds, (fd_set *) NULL, (fd_set *) NULL, perror_msg_and_die("select");
(struct timeval *) 0);
if (result < 1) {
perror("nc: select");
exit(3);
}
for (fd = 0; fd < FD_SETSIZE; fd++) { for (fd = 0; fd < FD_SETSIZE; fd++) {
if (FD_ISSET(fd, &testfds)) { if (FD_ISSET(fd, &testfds)) {
int trn = 0; if ((nread = safe_read(fd, buf, sizeof(buf))) < 0)
int rn; perror_msg_and_die("read");
ioctl(fd, FIONREAD, &nread);
if (fd == sfd) { if (fd == sfd) {
if (nread == 0) if (nread == 0)
exit(0); exit(0);
ofd = fileno(stdout); ofd = STDOUT_FILENO;
} else { } else {
if (nread == 0) if (nread == 0)
shutdown(sfd, 1); shutdown(sfd, 1);
ofd = sfd; ofd = sfd;
} }
if (full_write(ofd, buf, nread) < 0)
perror_msg_and_die("write");
do {
rn = (BUFSIZE < nread - trn) ? BUFSIZE : nread - trn;
trn += rn;
read(fd, ch, rn);
write(ofd, ch, rn);
}
while (trn < nread);
} }
} }
} }

View File

@ -38,14 +38,10 @@
#include <sys/time.h> #include <sys/time.h>
#include <sys/ioctl.h> #include <sys/ioctl.h>
#define BUFSIZE 100
int nc_main(int argc, char **argv) int nc_main(int argc, char **argv)
{ {
int sfd; int sfd;
int result; char buf[BUFSIZ];
int len;
char ch[BUFSIZE];
struct sockaddr_in address; struct sockaddr_in address;
struct hostent *hostinfo; struct hostent *hostinfo;
@ -54,34 +50,26 @@ int nc_main(int argc, char **argv)
argc--; argc--;
argv++; argv++;
if (argc < 2 || **(argv + 1) == '-') { if (argc < 2 || **argv == '-') {
usage(nc_usage); usage(nc_usage);
} }
sfd = socket(AF_INET, SOCK_STREAM, 0); if ((sfd = socket(AF_INET, SOCK_STREAM, 0)) < 0)
perror_msg_and_die("socket");
hostinfo = (struct hostent *) gethostbyname(*argv); if ((hostinfo = gethostbyname(*argv)) == NULL)
if (!hostinfo) {
error_msg_and_die("cannot resolve %s\n", *argv); error_msg_and_die("cannot resolve %s\n", *argv);
}
address.sin_family = AF_INET; address.sin_family = AF_INET;
address.sin_addr = *(struct in_addr *) *hostinfo->h_addr_list; address.sin_addr = *(struct in_addr *) *hostinfo->h_addr_list;
address.sin_port = htons(atoi(*(++argv))); address.sin_port = htons(atoi(*(++argv)));
len = sizeof(address); if (connect(sfd, (struct sockaddr *) &address, sizeof(address)) < 0)
perror_msg_and_die("connect");
result = connect(sfd, (struct sockaddr *) &address, len);
if (result < 0) {
perror("nc: connect");
exit(2);
}
FD_ZERO(&readfds); FD_ZERO(&readfds);
FD_SET(sfd, &readfds); FD_SET(sfd, &readfds);
FD_SET(fileno(stdin), &readfds); FD_SET(STDIN_FILENO, &readfds);
while (1) { while (1) {
int fd; int fd;
@ -90,41 +78,26 @@ int nc_main(int argc, char **argv)
testfds = readfds; testfds = readfds;
result = if (select(FD_SETSIZE, &testfds, NULL, NULL, NULL) < 0)
select(FD_SETSIZE, &testfds, (fd_set *) NULL, (fd_set *) NULL, perror_msg_and_die("select");
(struct timeval *) 0);
if (result < 1) {
perror("nc: select");
exit(3);
}
for (fd = 0; fd < FD_SETSIZE; fd++) { for (fd = 0; fd < FD_SETSIZE; fd++) {
if (FD_ISSET(fd, &testfds)) { if (FD_ISSET(fd, &testfds)) {
int trn = 0; if ((nread = safe_read(fd, buf, sizeof(buf))) < 0)
int rn; perror_msg_and_die("read");
ioctl(fd, FIONREAD, &nread);
if (fd == sfd) { if (fd == sfd) {
if (nread == 0) if (nread == 0)
exit(0); exit(0);
ofd = fileno(stdout); ofd = STDOUT_FILENO;
} else { } else {
if (nread == 0) if (nread == 0)
shutdown(sfd, 1); shutdown(sfd, 1);
ofd = sfd; ofd = sfd;
} }
if (full_write(ofd, buf, nread) < 0)
perror_msg_and_die("write");
do {
rn = (BUFSIZE < nread - trn) ? BUFSIZE : nread - trn;
trn += rn;
read(fd, ch, rn);
write(ofd, ch, rn);
}
while (trn < nread);
} }
} }
} }

View File

@ -536,7 +536,7 @@ const char *time_string(time_t timeVal)
} }
#endif /* BB_TAR || BB_AR */ #endif /* BB_TAR || BB_AR */
#if defined BB_TAR || defined BB_CP_MV || defined BB_AR || defined BB_DD #if defined BB_AR || defined BB_CP_MV || defined BB_DD || defined BB_NC || defined BB_TAR
/* /*
* Write all of the supplied buffer out to a file. * Write all of the supplied buffer out to a file.
* This does multiple writes as necessary. * This does multiple writes as necessary.
@ -1791,6 +1791,19 @@ int applet_name_compare(const void *x, const void *y)
return strcmp(applet1->name, applet2->name); return strcmp(applet1->name, applet2->name);
} }
#if defined BB_NC
ssize_t safe_read(int fd, void *buf, size_t count)
{
ssize_t n;
do {
n = read(fd, buf, count);
} while (n < 0 && errno == EINTR);
return n;
}
#endif
/* END CODE */ /* END CODE */
/* /*
Local Variables: Local Variables: