aboutsummaryrefslogtreecommitdiff
path: root/miscutils/fbsplash.c
diff options
context:
space:
mode:
authorDenis Vlasenko <vda.linux@googlemail.com>2008-04-04 20:38:49 +0000
committerDenis Vlasenko <vda.linux@googlemail.com>2008-04-04 20:38:49 +0000
commit7f8f0fafdbe59a2c32894f729955c1ad65e5a4ce (patch)
treec8ccfdc4667385c66f99bee52798857203111bde /miscutils/fbsplash.c
parent7b6d9d687d30c0ef9662fc01f154a847da59410f (diff)
downloadbusybox-w32-7f8f0fafdbe59a2c32894f729955c1ad65e5a4ce.tar.gz
busybox-w32-7f8f0fafdbe59a2c32894f729955c1ad65e5a4ce.tar.bz2
busybox-w32-7f8f0fafdbe59a2c32894f729955c1ad65e5a4ce.zip
fbsplash: more compact support for named pipes + EOF scenario
function old new delta fbsplash_main 1121 1043 -78
Diffstat (limited to 'miscutils/fbsplash.c')
-rw-r--r--miscutils/fbsplash.c81
1 files changed, 34 insertions, 47 deletions
diff --git a/miscutils/fbsplash.c b/miscutils/fbsplash.c
index e8bdb405d..a288b3ca5 100644
--- a/miscutils/fbsplash.c
+++ b/miscutils/fbsplash.c
@@ -360,6 +360,8 @@ int fbsplash_main(int argc ATTRIBUTE_UNUSED, char **argv)
360{ 360{
361 const char *fb_device, *cfg_filename, *fifo_filename; 361 const char *fb_device, *cfg_filename, *fifo_filename;
362 FILE *fp = fp; // for compiler 362 FILE *fp = fp; // for compiler
363 char *num_buf;
364 unsigned num;
363 bool bCursorOff; 365 bool bCursorOff;
364 366
365 INIT_G(); 367 INIT_G();
@@ -392,49 +394,7 @@ int fbsplash_main(int argc ATTRIBUTE_UNUSED, char **argv)
392 return EXIT_SUCCESS; 394 return EXIT_SUCCESS;
393 395
394 fp = xfopen_stdin(fifo_filename); 396 fp = xfopen_stdin(fifo_filename);
395 397 if (fp != stdin) {
396 while (1) {
397 struct stat statbuf;
398 unsigned num;
399 char *num_buf;
400
401 fb_drawprogressbar(0);
402 // Block on read, waiting for some input.
403 // Use of <stdio.h> style I/O allows to correctly
404 // handle a case when we have many buffered lines
405 // already in the pipe
406 while ((num_buf = xmalloc_fgetline(fp)) != NULL) {
407 if (strncmp(num_buf, "exit", 4) == 0) {
408 DEBUG_MESSAGE("exit");
409 exit_cmd:
410 if (bCursorOff) {
411 // restore cursor
412 full_write(STDOUT_FILENO, "\x1b" "[?25h", 6);
413 }
414 return EXIT_SUCCESS;
415 }
416 num = atoi(num_buf);
417 if (isdigit(num_buf[0]) && (num <= 100)) {
418#if DEBUG
419 char strVal[10];
420 sprintf(strVal, "%d", num);
421 DEBUG_MESSAGE(strVal);
422#endif
423 fb_drawprogressbar(num);
424 }
425 free(num_buf);
426 }
427 // We got EOF/error on fp
428 if (ferror(fp))
429 goto exit_cmd;
430 fclose(fp);
431 if (LONE_DASH(fifo_filename)
432 || stat(fifo_filename, &statbuf) != 0
433 || !S_ISFIFO(statbuf.st_mode)
434 ) {
435 goto exit_cmd;
436 }
437 // It's really a named pipe!
438 // For named pipes, we want to support this: 398 // For named pipes, we want to support this:
439 // mkfifo cmd_pipe 399 // mkfifo cmd_pipe
440 // fbsplash -f cmd_pipe .... & 400 // fbsplash -f cmd_pipe .... &
@@ -442,10 +402,37 @@ int fbsplash_main(int argc ATTRIBUTE_UNUSED, char **argv)
442 // echo 33 >cmd_pipe 402 // echo 33 >cmd_pipe
443 // ... 403 // ...
444 // echo 66 >cmd_pipe 404 // echo 66 >cmd_pipe
445 // This means that on EOF, we need to close/open cmd_pipe 405 // This means that we don't want fbsplash to get EOF
446 // (just reading again works too, but it hogs CPU) 406 // when last writer closes input end.
447 fp = xfopen_stdin(fifo_filename); // blocks on open 407 // The simplest way is to open fifo for writing too
448 } // end of while (1) 408 // and become an additional writer :)
409 open(fifo_filename, O_WRONLY); // errors are ignored
410 }
411
412 fb_drawprogressbar(0);
413 // Block on read, waiting for some input.
414 // Use of <stdio.h> style I/O allows to correctly
415 // handle a case when we have many buffered lines
416 // already in the pipe
417 while ((num_buf = xmalloc_fgetline(fp)) != NULL) {
418 if (strncmp(num_buf, "exit", 4) == 0) {
419 DEBUG_MESSAGE("exit");
420 break;
421 }
422 num = atoi(num_buf);
423 if (isdigit(num_buf[0]) && (num <= 100)) {
424#if DEBUG
425 char strVal[10];
426 sprintf(strVal, "%d", num);
427 DEBUG_MESSAGE(strVal);
428#endif
429 fb_drawprogressbar(num);
430 }
431 free(num_buf);
432 }
433
434 if (bCursorOff) // restore cursor
435 full_write(STDOUT_FILENO, "\x1b" "[?25h", 6);
449 436
450 return EXIT_SUCCESS; 437 return EXIT_SUCCESS;
451} 438}