Usermode/Telnet - Use _SysSelect instead of select (removes libposix requirement)
[tpg/acess2.git] / Usermode / Applications / telnet_src / main.c
index 519978d..56cffb4 100644 (file)
@@ -3,12 +3,11 @@
 */\r
 #include <acess/sys.h>\r
 #include <stdio.h>\r
+#include <stdlib.h>\r
 #include <net.h>\r
 #include <readline.h>\r
 #include <string.h>\r
 \r
-#define BUFSIZ 128\r
-\r
 // === PROTOTYPES ===\r
  int   main(int argc, char *argv[], char *envp[]);\r
  int   OpenTCP(const char *AddressString, short PortNumber);\r
@@ -20,9 +19,20 @@ int main(int argc, char *argv[], char *envp[])
         int    client_running = 1;\r
         int    bUseReadline = !!argv[3];       // HACK: If third argument is present, use ReadLine\r
        tReadline       *readline_info;\r
+        int    port;\r
+       \r
+       if( argc < 2 || argc > 3 ) {\r
+               fprintf(stderr, "Usage: telnet <host> [<port>]\n Port defaults to 23\n");\r
+               return 0;\r
+       }\r
+       \r
+       if(argc == 3)\r
+               port = atoi(argv[2]);\r
+       else\r
+               port = 23;\r
        \r
        // Connect to the remove server\r
-       server_fd = OpenTCP( argv[1], atoi(argv[2]) );\r
+       server_fd = OpenTCP( argv[1], port );\r
        if( server_fd == -1 ) {\r
                fprintf(stderr, "Unable to create socket\n");\r
                return -1;\r
@@ -44,7 +54,7 @@ int main(int argc, char *argv[], char *envp[])
                FD_SET(server_fd, &fds);        FD_SET(server_fd, &err_fds);\r
                \r
                // Wait for data (no timeout)\r
-               rv = select(server_fd+1, &fds, NULL, &err_fds, NULL);\r
+               rv = _SysSelect(server_fd+1, &fds, NULL, &err_fds, NULL, 0);\r
                if( rv < 0 )    break;\r
                \r
                // Check for remote data avaliable\r
@@ -53,8 +63,8 @@ int main(int argc, char *argv[], char *envp[])
                        // Read from server, and write to stdout\r
                        do\r
                        {\r
-                               len = read(server_fd, BUFSIZ, buffer);\r
-                               write(1, len, buffer);\r
+                               len = _SysRead(server_fd, buffer, BUFSIZ);\r
+                               _SysWrite(1, buffer, len);\r
                        } while( len == BUFSIZ );\r
                }\r
                \r
@@ -67,17 +77,17 @@ int main(int argc, char *argv[], char *envp[])
                                char    *line = Readline_NonBlock(readline_info);\r
                                if( line )\r
                                {\r
-                                       write(server_fd, strlen(line), line);\r
-                                       write(server_fd, 1, "\n");\r
+                                       _SysWrite(server_fd, line, strlen(line));\r
+                                       _SysWrite(server_fd, "\n", 1);\r
                                }\r
                        }\r
                        else\r
                        {\r
                                do\r
                                {\r
-                                       len = read(0, BUFSIZ, buffer);\r
-                                       write(server_fd, len, buffer);\r
-                                       write(1, len, buffer);\r
+                                       len = _SysRead(0, buffer, BUFSIZ);\r
+                                       _SysWrite(server_fd, buffer, len);\r
+                                       _SysWrite(1, buffer, len);\r
                                } while( len == BUFSIZ );\r
                        }\r
                }\r
@@ -90,7 +100,7 @@ int main(int argc, char *argv[], char *envp[])
                }\r
        }\r
        \r
-       close(server_fd);\r
+       _SysClose(server_fd);\r
        return 0;\r
 }\r
 \r
@@ -100,7 +110,6 @@ int main(int argc, char *argv[], char *envp[])
 int OpenTCP(const char *AddressString, short PortNumber)\r
 {\r
         int    fd, addrType;\r
-       char    *iface;\r
        uint8_t addrBuffer[16];\r
        \r
        // Parse IP Address\r
@@ -119,11 +128,11 @@ int OpenTCP(const char *AddressString, short PortNumber)
        }\r
        \r
        // Set remote port and address\r
-       ioctl(fd, 5, &PortNumber);\r
-       ioctl(fd, 6, addrBuffer);\r
+       _SysIOCtl(fd, 5, &PortNumber);\r
+       _SysIOCtl(fd, 6, addrBuffer);\r
        \r
        // Connect\r
-       if( ioctl(fd, 7, NULL) == 0 ) {\r
+       if( _SysIOCtl(fd, 7, NULL) == 0 ) {\r
                fprintf(stderr, "Unable to start connection\n");\r
                return -1;\r
        }\r

UCC git Repository :: git.ucc.asn.au