ifconfig: code shrink
[oweals/busybox.git] / miscutils / rx.c
index 3df4613491ffed1788ba5ceb6477476bb3dcdde8..898703c592a377a80361e54955934048be271f44 100644 (file)
@@ -1,7 +1,6 @@
 /* vi: set sw=4 ts=4: */
 /*-------------------------------------------------------------------------
  * Filename:      xmodem.c
- * Version:       $Id: rx.c,v 1.2 2004/03/15 08:28:46 andersen Exp $
  * Copyright:     Copyright (C) 2001, Hewlett-Packard Company
  * Author:        Christopher Hoover <ch@hpl.hp.com>
  * Description:   xmodem functionality for uploading of kernels
  *
  */
 
-#include <stdlib.h>
-#include <stdarg.h>
-#include <stdio.h>
-#include <unistd.h>
-#include <errno.h>
-#include <termios.h>
-#include <signal.h>
-#include <sys/types.h>
-#include <sys/stat.h>
-#include <fcntl.h>
-#include <string.h>
-#include "busybox.h"
-
+#include "libbb.h"
 
 #define SOH 0x01
 #define STX 0x02
 #define EOT 0x04
 #define ACK 0x06
 #define NAK 0x15
-#define CAN 0x18
 #define BS  0x08
 
 /*
@@ -57,19 +43,8 @@ Cf:
 #define TIMEOUT_LONG 10
 #define MAXERRORS 10
 
-static inline void write_byte(int fd, char cc) {
-       write(fd, &cc, 1);
-}
-
-static inline void write_flush(int fd) {
-       tcdrain(fd);
-}
-
-static inline void read_flush(int fd) {
-       tcflush(fd, TCIFLUSH);
-}
-
-static int read_byte(int fd, unsigned int timeout) {
+static int read_byte(int fd, unsigned int timeout)
+{
        char buf[1];
        int n;
 
@@ -99,11 +74,11 @@ static int receive(char *error_buf, size_t error_buf_size,
 #define note_error(fmt,args...) \
        snprintf(error_buf, error_buf_size, fmt,##args)
 
-       read_flush(ttyfd);
+       /* Flush pending input */
+       tcflush(ttyfd, TCIFLUSH);
 
        /* Ask for CRC; if we get errors, we will go with checksum */
-       write_byte(ttyfd, nak);
-       write_flush(ttyfd);
+       write(ttyfd, &nak, 1);
 
        for (;;) {
                int blockBegin;
@@ -126,8 +101,8 @@ static int receive(char *error_buf, size_t error_buf_size,
                        break;
 
                case EOT:
-                       write_byte(ttyfd, ACK);
-                       write_flush(ttyfd);
+                       nak = ACK;
+                       write(ttyfd, &nak, 1);
                        goto done;
 
                default:
@@ -225,15 +200,15 @@ static int receive(char *error_buf, size_t error_buf_size,
                wantBlockNo++;
                length += blockLength;
 
-               if (bb_full_write(filefd, blockBuf, blockLength) < 0) {
+               if (full_write(filefd, blockBuf, blockLength) < 0) {
                        note_error("write to file failed: %m");
                        goto fatal;
                }
 
        next:
                errors = 0;
-               write_byte(ttyfd, ACK);
-               write_flush(ttyfd);
+               nak = ACK;
+               write(ttyfd, &nak, 1);
                continue;
 
        error:
@@ -241,7 +216,6 @@ static int receive(char *error_buf, size_t error_buf_size,
                errors++;
                if (errors == MAXERRORS) {
                        /* Abort */
-                       int i;
 
                        // if using crc, try again w/o crc
                        if (nak == 'C') {
@@ -254,17 +228,15 @@ static int receive(char *error_buf, size_t error_buf_size,
                        note_error("too many errors; giving up");
 
                fatal:
-                       for (i = 0; i < 5; i ++)
-                               write_byte(ttyfd, CAN);
-                       for (i = 0; i < 5; i ++)
-                               write_byte(ttyfd, BS);
-                       write_flush(ttyfd);
+                       /* 5 CAN followed by 5 BS */
+                       write(ttyfd, "\030\030\030\030\030\010\010\010\010\010", 10);
                        return -1;
                }
 
-               read_flush(ttyfd);
-               write_byte(ttyfd, nak);
-               write_flush(ttyfd);
+               /* Flush pending input */
+               tcflush(ttyfd, TCIFLUSH);
+
+               write(ttyfd, &nak, 1);
        }
 
  done:
@@ -277,6 +249,7 @@ static void sigalrm_handler(int ATTRIBUTE_UNUSED signum)
 {
 }
 
+int rx_main(int argc, char **argv) MAIN_EXTERNALLY_VISIBLE;
 int rx_main(int argc, char **argv)
 {
        char *fn;
@@ -290,11 +263,11 @@ int rx_main(int argc, char **argv)
                        bb_show_usage();
 
        fn = argv[1];
-       ttyfd = bb_xopen3("/dev/tty", O_RDWR, 0);
-       filefd = bb_xopen3(fn, O_RDWR|O_CREAT|O_TRUNC, 0666);
+       ttyfd = xopen(CURRENT_TTY, O_RDWR);
+       filefd = xopen(fn, O_RDWR|O_CREAT|O_TRUNC);
 
        if (tcgetattr(ttyfd, &tty) < 0)
-                       bb_error_msg_and_die("%s: tcgetattr failed: %m\n", argv[0]);
+                       bb_perror_msg_and_die("tcgetattr");
 
        orig_tty = tty;
 
@@ -312,16 +285,7 @@ int rx_main(int argc, char **argv)
        tcsetattr(ttyfd, TCSAFLUSH, &orig_tty);
 
        if (n < 0)
-                       bb_error_msg_and_die("\n%s: receive failed:\n  %s\n",
-                                                          argv[0], error_buf);
+               bb_error_msg_and_die("\nreceive failed:\n  %s", error_buf);
 
-       bb_fflush_stdout_and_exit(EXIT_SUCCESS);
+       fflush_stdout_and_exit(EXIT_SUCCESS);
 }
-
-/*
-Local Variables:
-c-file-style: "linux"
-c-basic-offset: 4
-tab-width: 4
-End:
-*/