On Friday 04 April 2008 11:27, Michele Sanges wrote:
> Il giorno ven, 28/03/2008 alle 09.06 -0400, Paul Fox ha scritto:
> > is the proper solution simply to open it twice, once for reading,
> > and once for writing?  this guarantees a writer (which will never
> > write anything).
> 
> Following the suggestion of Paul, now I open the fifo twice to use the
> opportunity to make the read blocking.

This seems to be a good idea. I am committing the attached patch
to svn. Saves ~70 bytes.
--
vda
diff -d -urpN busybox.6/miscutils/fbsplash.c busybox.7/miscutils/fbsplash.c
--- busybox.6/miscutils/fbsplash.c	2008-03-31 21:26:17.000000000 +0200
+++ busybox.7/miscutils/fbsplash.c	2008-04-04 22:32:36.000000000 +0200
@@ -360,6 +360,8 @@ int fbsplash_main(int argc ATTRIBUTE_UNU
 {
 	const char *fb_device, *cfg_filename, *fifo_filename;
 	FILE *fp = fp; // for compiler
+	char *num_buf;
+	unsigned num;
 	bool bCursorOff;
 
 	INIT_G();
@@ -392,49 +394,7 @@ int fbsplash_main(int argc ATTRIBUTE_UNU
 		return EXIT_SUCCESS;
 
 	fp = xfopen_stdin(fifo_filename);
-
-	while (1) {
-		struct stat statbuf;
-		unsigned num;
-		char *num_buf;
-
-		fb_drawprogressbar(0);
-		// Block on read, waiting for some input.
-		// Use of <stdio.h> style I/O allows to correctly
-		// handle a case when we have many buffered lines
-		// already in the pipe
-		while ((num_buf = xmalloc_fgetline(fp)) != NULL) {
-			if (strncmp(num_buf, "exit", 4) == 0) {
-				DEBUG_MESSAGE("exit");
- exit_cmd:
-				if (bCursorOff) {
-					// restore cursor
-					full_write(STDOUT_FILENO, "\x1b" "[?25h", 6);
-				}
-				return EXIT_SUCCESS;
-			}
-			num = atoi(num_buf);
-			if (isdigit(num_buf[0]) && (num <= 100)) {
-#if DEBUG
-				char strVal[10];
-				sprintf(strVal, "%d", num);
-				DEBUG_MESSAGE(strVal);
-#endif
-				fb_drawprogressbar(num);
-			}
-			free(num_buf);
-		}
-		// We got EOF/error on fp
-		if (ferror(fp))
-			goto exit_cmd;
-		fclose(fp);
-		if (LONE_DASH(fifo_filename)
-		 || stat(fifo_filename, &statbuf) != 0
-		 || !S_ISFIFO(statbuf.st_mode)
-		) {
-			goto exit_cmd;
-		}
-		// It's really a named pipe!
+	if (fp != stdin) {
 		// For named pipes, we want to support this:
 		//  mkfifo cmd_pipe
 		//  fbsplash -f cmd_pipe .... &
@@ -442,10 +402,37 @@ int fbsplash_main(int argc ATTRIBUTE_UNU
 		//  echo 33 >cmd_pipe
 		//  ...
 		//  echo 66 >cmd_pipe
-		// This means that on EOF, we need to close/open cmd_pipe
-		// (just reading again works too, but it hogs CPU)
-		fp = xfopen_stdin(fifo_filename); // blocks on open
-	} // end of while (1)
+		// This means that we don't want fbsplash to get EOF
+		// when last writer closes input end.
+		// The simplest way is to open fifo for writing too
+		// and become an additional writer :)
+		open(fifo_filename, O_WRONLY); // errors are ignored
+	}
+
+	fb_drawprogressbar(0);
+	// Block on read, waiting for some input.
+	// Use of <stdio.h> style I/O allows to correctly
+	// handle a case when we have many buffered lines
+	// already in the pipe
+	while ((num_buf = xmalloc_fgetline(fp)) != NULL) {
+		if (strncmp(num_buf, "exit", 4) == 0) {
+			DEBUG_MESSAGE("exit");
+			break;
+		}
+		num = atoi(num_buf);
+		if (isdigit(num_buf[0]) && (num <= 100)) {
+#if DEBUG
+			char strVal[10];
+			sprintf(strVal, "%d", num);
+			DEBUG_MESSAGE(strVal);
+#endif
+			fb_drawprogressbar(num);
+		}
+		free(num_buf);
+	}
+
+	if (bCursorOff) // restore cursor
+		full_write(STDOUT_FILENO, "\x1b" "[?25h", 6);
 
 	return EXIT_SUCCESS;
 }
_______________________________________________
busybox mailing list
[email protected]
http://busybox.net/cgi-bin/mailman/listinfo/busybox

Reply via email to