소스 검색

package/maccalc: don't expect to get all data in one read

Signed-off-by: Alexander Gordeev <[email protected]>

SVN-Revision: 28266
Gabor Juhos 14 년 전
부모
커밋
ffb56132e4
1개의 변경된 파일30개의 추가작업 그리고 1개의 파일을 삭제
  1. 30 1
      package/maccalc/src/main.c

+ 30 - 1
package/maccalc/src/main.c

@@ -9,6 +9,7 @@
  *
  */
 
+#include <errno.h>
 #include <stdlib.h>
 #include <stdio.h>
 #include <stdint.h>
@@ -124,6 +125,34 @@ static int maccalc_do_mac2bin(int argc, const char *argv[])
 	return 0;
 }
 
+static ssize_t read_safe(int fd, void *buf, size_t count)
+{
+	ssize_t total = 0;
+	ssize_t r;
+
+	while(count > 0) {
+		r = read(fd, buf, count);
+		if (r == 0)
+			/* EOF */
+			break;
+		if (r < 0) {
+			if (errno == EINTR)
+				/* interrupted by a signal, restart */
+				continue;
+			/* error */
+			total = -1;
+			break;
+		}
+
+		/* ok */
+		total += r;
+		count -= r;
+		buf += r;
+	}
+
+	return total;
+}
+
 static int maccalc_do_bin2mac(int argc, const char *argv[])
 {
 	unsigned char mac[MAC_ADDRESS_LEN];
@@ -134,7 +163,7 @@ static int maccalc_do_bin2mac(int argc, const char *argv[])
 		return ERR_INVALID;
 	}
 
-	c = read(STDIN_FILENO, mac, sizeof(mac));
+	c = read_safe(STDIN_FILENO, mac, sizeof(mac));
 	if (c != sizeof(mac)) {
 		fprintf(stderr, "failed to read from stdin\n");
 		return ERR_IO;