rename bb_default_error_retval -> xfunc_error_retval
[oweals/busybox.git] / networking / nc.c
index bda0c407bc497c4bf6ca946b6acb8b874e68b2e7..f8b3fb2dd9575f939e0ca25f45fd865ab62b98d6 100644 (file)
@@ -1,16 +1,14 @@
 /* vi: set sw=4 ts=4: */
 /*  nc: mini-netcat - built from the ground up for LRP
- *  
+ *
  *  Copyright (C) 1998, 1999  Charles P. Wright
  *  Copyright (C) 1998  Dave Cinege
- *  
+ *
  *  Licensed under GPLv2 or later, see file LICENSE in this tarball for details.
  */
 
 #include "busybox.h"
 
-#define xread bb_xread
-
 static void timeout(int signum)
 {
        bb_error_msg_and_die("Timed out");
@@ -27,7 +25,7 @@ int nc_main(int argc, char **argv)
 
        memset(&address, 0, sizeof(address));
 
-       if (ENABLE_NC_SERVER || ENABLE_NC_EXTRA) { 
+       if (ENABLE_NC_SERVER || ENABLE_NC_EXTRA) {
                while ((opt = getopt(argc, argv, "lp:" USE_NC_EXTRA("i:ew:f:"))) > 0) {
                        if (ENABLE_NC_SERVER && opt=='l') do_listen++;
                        else if (ENABLE_NC_SERVER && opt=='p')
@@ -42,7 +40,7 @@ int nc_main(int argc, char **argv)
                }
        }
 
-       
+
        // For listen or file we need zero arguments, dialout is 2.
        // For exec we need at least one more argument at the end, more ok
 
@@ -55,11 +53,11 @@ int nc_main(int argc, char **argv)
                signal(SIGALRM, timeout);
                alarm(wsecs);
        }
-       
-       if (infile) cfd = bb_xopen(infile, O_RDWR);
+
+       if (infile) cfd = xopen(infile, O_RDWR);
        else {
                opt = 1;
-               sfd = bb_xsocket(AF_INET, SOCK_STREAM, 0);
+               sfd = xsocket(AF_INET, SOCK_STREAM, 0);
                fcntl(sfd, F_SETFD, FD_CLOEXEC);
                setsockopt(sfd, SOL_SOCKET, SO_REUSEADDR, &opt, sizeof (opt));
                address.sin_family = AF_INET;
@@ -69,13 +67,13 @@ int nc_main(int argc, char **argv)
                if (lport != 0) {
                        address.sin_port = lport;
 
-                       bb_xbind(sfd, (struct sockaddr *) &address, sizeof(address));
+                       xbind(sfd, (struct sockaddr *) &address, sizeof(address));
                }
 
                if (do_listen) {
                        socklen_t addrlen = sizeof(address);
 
-                       bb_xlisten(sfd, do_listen);
+                       xlisten(sfd, do_listen);
 
                        // If we didn't specify a port number, query and print it to stderr.
 
@@ -134,7 +132,7 @@ repeatyness:
        }
 
        // Select loop copying stdin to cfd, and cfd to stdout.
-       
+
        FD_ZERO(&readfds);
        FD_SET(cfd, &readfds);
        FD_SET(STDIN_FILENO, &readfds);
@@ -151,13 +149,14 @@ repeatyness:
 
                for (fd = 0; fd < FD_SETSIZE; fd++) {
                        if (FD_ISSET(fd, &testfds)) {
-                               nread = xread(fd, bb_common_bufsiz1, sizeof(bb_common_bufsiz1));
+                               nread = safe_read(fd, bb_common_bufsiz1,
+                                                       sizeof(bb_common_bufsiz1));
 
                                if (fd == cfd) {
-                                       if (!nread) exit(0);
+                                       if (nread<1) exit(0);
                                        ofd = STDOUT_FILENO;
                                } else {
-                                       if (!nread) {
+                                       if (nread<1) {
                                                // Close outgoing half-connection so they get EOF, but
                                                // leave incoming alone so we can see response.
                                                shutdown(cfd, 1);
@@ -166,8 +165,7 @@ repeatyness:
                                        ofd = cfd;
                                }
 
-                               if (bb_full_write(ofd, bb_common_bufsiz1, nread) < 0)
-                                       bb_perror_msg_and_die(bb_msg_write_error);
+                               xwrite(ofd, bb_common_bufsiz1, nread);
                                if (delay > 0) sleep(delay);
                        }
                }