diff loadtools/tpinterf.c @ 23:aca1948e9713

loadtool: initial version compiles and links
author Michael Spacefalcon <msokolov@ivan.Harhan.ORG>
date Sat, 04 May 2013 04:52:05 +0000
parents 67a39d8914a8
children 05af070c4b60
line wrap: on
line diff
--- a/loadtools/tpinterf.c	Sat May 04 04:36:29 2013 +0000
+++ b/loadtools/tpinterf.c	Sat May 04 04:52:05 2013 +0000
@@ -7,6 +7,7 @@
 #include <sys/types.h>
 #include <sys/time.h>
 #include <sys/errno.h>
+#include <stdio.h>
 #include <string.h>
 #include <strings.h>
 #include <stdlib.h>
@@ -102,20 +103,24 @@
 			if (errno == EINTR)
 				continue;
 			perror("select");
-			exit(1);
+			return(-1);
 		}
-		if (cc < 1)
+		if (cc < 1) {
+			fprintf(stderr,
+				"error: timeout waiting for command echo\n");
 			return(-1);
+		}
 		cc = read(target_fd, echobuf + rcvd, cmdlen + 2 - rcvd);
 		if (cc <= 0) {
 			perror("read after successful select");
-			exit(1);
+			return(-1);
 		}
 		rcvd += cc;
 	}
-	if (bcmp(echobuf, cmdbuf, cmdlen + 2))
+	if (bcmp(echobuf, cmdbuf, cmdlen + 2)) {
+		fprintf(stderr, "error: command echo mismatch\n");
 		return(-1);
-	else
+	} else
 		return(0);
 }
 
@@ -124,39 +129,45 @@
  * '=' prompt is received.  All intermediate output is passed to
  * stdout.
  *
- * Return value: 0 if '=' prompt received, -1 otherwise (timeout)
+ * Return value: 0 if '=' prompt received immediately,
+ * positive if some scribble came before the prompt, -1 on errors
+ * (timeout, read errors, etc).
  */
-tpinterf_pass_output()
+tpinterf_pass_output(timeout)
 {
 	char buf[512], *cp;
 	fd_set fds;
 	struct timeval tv;
-	int cc, newline = 1;
+	int cc, newline = 1, totout = 0;
 
 	for (;;) {
 		FD_ZERO(&fds);
 		FD_SET(target_fd, &fds);
-		tv.tv_sec = 1;
+		tv.tv_sec = timeout;
 		tv.tv_usec = 0;
 		cc = select(target_fd+1, &fds, NULL, NULL, &tv);
 		if (cc < 0) {
 			if (errno == EINTR)
 				continue;
 			perror("select");
-			exit(1);
+			return(-1);
 		}
-		if (cc < 1)
+		if (cc < 1) {
+			fprintf(stderr,
+		"error: timeout waiting for \'=\' prompt from target\n");
 			return(-1);
+		}
 		cc = read(target_fd, buf, sizeof buf);
 		if (cc <= 0) {
 			perror("read after successful select");
-			exit(1);
+			return(-1);
 		}
 		for (cp = buf; cc; cp++) {
 			cc--;
 			if (*cp == '=' && newline && !cc)
-				return(0);
+				return(totout);
 			putchar(*cp);
+			totout++;
 			if (*cp == '\n')
 				newline = 1;
 			else