commit f19055b6e9735d304e20629955e65833df1a8699
parent 63a97679fe23f4d6ed46cc134fe3fd205c912e22
Author: Naveen Narayanan <zerous@nocebo.space>
Date: Sat, 25 Sep 2021 17:31:03 +0200
Handle unsent bytes
Diffstat:
1 file changed, 13 insertions(+), 8 deletions(-)
diff --git a/finger.c b/finger.c
@@ -53,7 +53,7 @@ main(int argc, char **argv)
struct addrinfo *res, *p;
char user[32], hostname[MAXHOSTNAMELEN+1];
char *msg = buf;
- int status, sockfd, byt, len, err;
+ int status, sockfd, byt, len, err, tosend;
err = 0;
@@ -72,6 +72,7 @@ main(int argc, char **argv)
goto err1;
}
+ len = strlen(user);
for (p = res; p != NULL; p = p->ai_next) {
sockfd = socket(res->ai_family, res->ai_socktype,
res->ai_protocol);
@@ -89,17 +90,21 @@ main(int argc, char **argv)
goto err3;
}
- len = strlen(user);
memcpy(msg, user, len);
msg[len] = '\r';
msg[len+1] = '\n';
+ tosend = len+2;
- byt = send(sockfd, msg, len+2, 0);
- if (byt == -1) {
- fprintf(stderr, "send failed: %s\n", strerror(errno));
- err = 1;
- goto err3;
- }
+ do {
+ byt = send(sockfd, msg, tosend, 0);
+ if (byt == -1) {
+ fprintf(stderr, "send failed: %s\n", strerror(errno));
+ err = 1;
+ goto err3;
+ }
+ tosend -= byt;
+ msg += byt;
+ } while (tosend > 0);
do {
byt = recv(sockfd, msg, BUFSZ, 0);