diff options
author | Erik Andersen <andersen@codepoet.org> | 2000-02-08 19:58:47 +0000 |
---|---|---|
committer | Erik Andersen <andersen@codepoet.org> | 2000-02-08 19:58:47 +0000 |
commit | e49d5ecbbe51718fa925b6890a735e5937cc2aa2 (patch) | |
tree | c90bda10731ad9333ce3b404f993354c9fc104b8 /mnc.c | |
parent | c0bf817bbc5c7867fbe8fb76d5c39f8ee802692f (diff) | |
download | busybox-w32-e49d5ecbbe51718fa925b6890a735e5937cc2aa2.tar.gz busybox-w32-e49d5ecbbe51718fa925b6890a735e5937cc2aa2.tar.bz2 busybox-w32-e49d5ecbbe51718fa925b6890a735e5937cc2aa2.zip |
Some formatting updates (ran the code through indent)
-Erik
Diffstat (limited to 'mnc.c')
-rw-r--r-- | mnc.c | 118 |
1 files changed, 55 insertions, 63 deletions
@@ -1,3 +1,4 @@ | |||
1 | /* vi: set sw=4 ts=4: */ | ||
1 | /* mnc: mini-netcat - built from the ground up for LRP | 2 | /* mnc: mini-netcat - built from the ground up for LRP |
2 | Copyright (C) 1998 Charles P. Wright | 3 | Copyright (C) 1998 Charles P. Wright |
3 | 4 | ||
@@ -39,101 +40,92 @@ | |||
39 | 40 | ||
40 | #define BUFSIZE 100 | 41 | #define BUFSIZE 100 |
41 | 42 | ||
42 | static const char mnc_usage[] = | 43 | static const char mnc_usage[] = |
43 | "mnc [IP] [port]\n\n" | ||
44 | "mini-netcat opens a pipe to IP:port\n"; | ||
45 | 44 | ||
46 | int | 45 | "mnc [IP] [port]\n\n" "mini-netcat opens a pipe to IP:port\n"; |
47 | mnc_main(int argc, char **argv) | 46 | |
47 | int mnc_main(int argc, char **argv) | ||
48 | { | 48 | { |
49 | int sfd; | 49 | int sfd; |
50 | int result; | 50 | int result; |
51 | int len; | 51 | int len; |
52 | char ch[BUFSIZE]; | 52 | char ch[BUFSIZE]; |
53 | 53 | ||
54 | struct sockaddr_in address; | 54 | struct sockaddr_in address; |
55 | struct hostent *hostinfo; | 55 | struct hostent *hostinfo; |
56 | 56 | ||
57 | fd_set readfds, testfds; | 57 | fd_set readfds, testfds; |
58 | 58 | ||
59 | if (argc<=1 || **(argv+1) == '-' ) { | 59 | if (argc <= 1 || **(argv + 1) == '-') { |
60 | usage( mnc_usage); | 60 | usage(mnc_usage); |
61 | } | 61 | } |
62 | argc--; | 62 | argc--; |
63 | argv++; | 63 | argv++; |
64 | 64 | ||
65 | sfd = socket(AF_INET, SOCK_STREAM, 0); | 65 | sfd = socket(AF_INET, SOCK_STREAM, 0); |
66 | 66 | ||
67 | hostinfo = (struct hostent *) gethostbyname(*argv); | 67 | hostinfo = (struct hostent *) gethostbyname(*argv); |
68 | 68 | ||
69 | if (!hostinfo) | 69 | if (!hostinfo) { |
70 | { | 70 | exit(1); |
71 | exit(1); | 71 | } |
72 | } | ||
73 | 72 | ||
74 | address.sin_family = AF_INET; | 73 | address.sin_family = AF_INET; |
75 | address.sin_addr = *(struct in_addr *) *hostinfo->h_addr_list; | 74 | address.sin_addr = *(struct in_addr *) *hostinfo->h_addr_list; |
76 | address.sin_port = htons(atoi(*(++argv))); | 75 | address.sin_port = htons(atoi(*(++argv))); |
77 | 76 | ||
78 | len = sizeof(address); | 77 | len = sizeof(address); |
79 | 78 | ||
80 | result = connect(sfd, (struct sockaddr *)&address, len); | 79 | result = connect(sfd, (struct sockaddr *) &address, len); |
81 | 80 | ||
82 | if (result < 0) | 81 | if (result < 0) { |
83 | { | 82 | exit(2); |
84 | exit(2); | 83 | } |
85 | } | ||
86 | 84 | ||
87 | FD_ZERO(&readfds); | 85 | FD_ZERO(&readfds); |
88 | FD_SET(sfd, &readfds); | 86 | FD_SET(sfd, &readfds); |
89 | FD_SET(fileno(stdin), &readfds); | 87 | FD_SET(fileno(stdin), &readfds); |
90 | 88 | ||
91 | while(1) | 89 | while (1) { |
92 | { | 90 | int fd; |
93 | int fd; | ||
94 | int ofd; | 91 | int ofd; |
95 | int nread; | 92 | int nread; |
96 | 93 | ||
97 | testfds = readfds; | 94 | testfds = readfds; |
98 | 95 | ||
99 | result = select(FD_SETSIZE, &testfds, (fd_set *) NULL, (fd_set *) NULL, (struct timeval *) 0); | 96 | result = |
97 | select(FD_SETSIZE, &testfds, (fd_set *) NULL, (fd_set *) NULL, | ||
98 | (struct timeval *) 0); | ||
100 | 99 | ||
101 | if(result < 1) | 100 | if (result < 1) { |
102 | { | 101 | exit(3); |
103 | exit(3); | 102 | } |
104 | } | ||
105 | 103 | ||
106 | for(fd = 0; fd < FD_SETSIZE; fd++) | 104 | for (fd = 0; fd < FD_SETSIZE; fd++) { |
107 | { | 105 | if (FD_ISSET(fd, &testfds)) { |
108 | if(FD_ISSET(fd,&testfds)) | ||
109 | { | ||
110 | int trn = 0; | 106 | int trn = 0; |
111 | int rn; | 107 | int rn; |
112 | 108 | ||
113 | ioctl(fd, FIONREAD, &nread); | 109 | ioctl(fd, FIONREAD, &nread); |
114 | 110 | ||
115 | if(fd == sfd) | 111 | if (fd == sfd) { |
116 | { | 112 | if (nread == 0) |
117 | if (nread == 0) | 113 | exit(0); |
118 | exit(0); | ||
119 | ofd = fileno(stdout); | 114 | ofd = fileno(stdout); |
120 | } | 115 | } else { |
121 | else | ||
122 | { | ||
123 | ofd = sfd; | 116 | ofd = sfd; |
124 | } | 117 | } |
125 | 118 | ||
126 | 119 | ||
127 | 120 | ||
128 | do | 121 | do { |
129 | { | ||
130 | rn = (BUFSIZE < nread - trn) ? BUFSIZE : nread - trn; | 122 | rn = (BUFSIZE < nread - trn) ? BUFSIZE : nread - trn; |
131 | trn += rn; | 123 | trn += rn; |
132 | read(fd, ch, rn); | 124 | read(fd, ch, rn); |
133 | write(ofd, ch, rn); | 125 | write(ofd, ch, rn); |
134 | } | 126 | } |
135 | while (trn < nread); | 127 | while (trn < nread); |
136 | } | 128 | } |
137 | } | 129 | } |
138 | } | 130 | } |
139 | } | 131 | } |