- patch from Denis Vlasenko to add and use bb_xchdir()
[oweals/busybox.git] / networking / telnetd.c
index d5de8903c06c3d4907409aa1a115c9f1cd1beea0..1a53c0c0ccf3ed830fb86f6988ced32f8debee36 100644 (file)
@@ -1,10 +1,9 @@
-/* $Id: telnetd.c,v 1.13 2004/09/14 17:24:58 bug1 Exp $
- *
+/* vi: set sw=4 ts=4: */
+/*
  * Simple telnet server
  * Bjorn Wesen, Axis Communications AB (bjornw@axis.com)
  *
- * This file is distributed under the Gnu Public License (GPL),
- * please see the file LICENSE for further information.
+ * Licensed under GPL, see file LICENSE in this tarball for details.
  *
  * ---------------------------------------------------------------------------
  * (C) Copyright 2000, Axis Communications AB, LUND, SWEDEN
@@ -94,7 +93,7 @@ struct tsession {
    +-------+     wridx1++     +------+     rdidx1++     +----------+
    |       | <--------------  | buf1 | <--------------  |          |
    |       |     size1--      +------+     size1++      |          |
-   |  pty  |                                            |  socket  |
+   |  pty  |                                       |  socket  |
    |       |     rdidx2++     +------+     wridx2++     |          |
    |       |  --------------> | buf2 |  --------------> |          |
    +-------+     size2++      +------+     size2--      +----------+
@@ -131,7 +130,7 @@ static struct tsession *sessions;
   */
 static char *
 remove_iacs(struct tsession *ts, int *pnum_totty) {
-       unsigned char *ptr0 = ts->buf1 + ts->wridx1;
+       unsigned char *ptr0 = (unsigned char *)ts->buf1 + ts->wridx1;
        unsigned char *ptr = ptr0;
        unsigned char *totty = ptr;
        unsigned char *end = ptr + MIN(BUFSIZE - ts->wridx1, ts->size1);
@@ -166,7 +165,7 @@ remove_iacs(struct tsession *ts, int *pnum_totty) {
                        else if (ptr[1] == SB && ptr[2] == TELOPT_NAWS) {
                                struct winsize ws;
                                if ((ptr+8) >= end)
-                                       break;  /* incomplete, can't process */
+                                       break;  /* incomplete, can't process */
                                ws.ws_col = (ptr[3] << 8) | ptr[4];
                                ws.ws_row = (ptr[5] << 8) | ptr[6];
                                (void) ioctl(ts->ptyfd, TIOCSWINSZ, (char *)&ws);
@@ -383,7 +382,7 @@ int
 telnetd_main(int argc, char **argv)
 {
 #ifndef CONFIG_FEATURE_TELNETD_INETD
-        sockaddr_type sa;
+       sockaddr_type sa;
        int master_fd;
 #endif /* CONFIG_FEATURE_TELNETD_INETD */
        fd_set rdfdset, wrfdset;
@@ -446,10 +445,7 @@ telnetd_main(int argc, char **argv)
 
        /* Grab a TCP socket.  */
 
-        master_fd = socket(SOCKET_TYPE, SOCK_STREAM, 0);
-       if (master_fd < 0) {
-               bb_perror_msg_and_die("socket");
-       }
+       master_fd = bb_xsocket(SOCKET_TYPE, SOCK_STREAM, 0);
        (void)setsockopt(master_fd, SOL_SOCKET, SO_REUSEADDR, &on, sizeof(on));
 
        /* Set it to listen to specified port.  */
@@ -465,17 +461,9 @@ telnetd_main(int argc, char **argv)
        sa.sin_addr = bind_addr;
 #endif
 
-       if (bind(master_fd, (struct sockaddr *) &sa, sizeof(sa)) < 0) {
-               bb_perror_msg_and_die("bind");
-       }
-
-       if (listen(master_fd, 1) < 0) {
-               bb_perror_msg_and_die("listen");
-       }
-
-       if (daemon(0, 0) < 0)
-               bb_perror_msg_and_die("daemon");
-
+       bb_xbind(master_fd, (struct sockaddr *) &sa, sizeof(sa));
+       bb_xlisten(master_fd, 1);
+       bb_xdaemon(0, 0);
 
        maxfd = master_fd;
 #endif /* CONFIG_FEATURE_TELNETD_INETD */