On Thursday 27 September 2007 08:43, Vladimir Dronnikov wrote:
> In addition I want to share new "led" applet
> It prints (when no parameters given) or sets (when a number given) the 
> keyboard leds state.
> When it recognizes the only parameter as valid file name it playbacks the 
> sequence of states.
> Example of SOS sequence is given at the top of the file


+static int kfd = -1;
...
+       kfd = open("/dev/console", O_RDWR);
+       if (kfd < 0)
+               bb_perror_msg_and_die("can't open console device");

Code which is smaller and needs no statics:

enum { kfd = 3 };
...
        xmove_fd(xopen("/dev/console", O_WRONLY), kfd);



+       // if no args specified -> dump current state
+       if (argc < 2) {
+               ioctl(kfd, KDGETLED, &state);
+               state = (state & 7) + '0';
+               write(1, &state, 1);
+               write(1, "\n", 1);
+       }
+       // if arg is not a file -> treat it as a new state
+       else if (stat(argv[1], &st)) {
+               ioctl(kfd, KDSETLED, atoi(argv[1]) & 7);
+       }
+       // if arg is a file -> read it char-by-char
+       else {
+               // open file
+               fd = open(argv[1], O_RDONLY);
+               if (fd < 0) {
+                       bb_perror_msg("can't open input file");
+                       goto clean_exit;
+               }

xopen().

+               // set cleanup
+               signal(SIGINT,  sig_quit);
+               signal(SIGQUIT, sig_quit);
+               signal(SIGTERM, sig_quit);
+               // read and display data
+               while (1 == read(fd, &c, 1) || EINTR == errno) {

With safe_read() instead of read(), you don't need to check EINTR.

+                       // . means 50 ms delay
+                       if ('*' == c) { usleep(50000); continue; }
+                       // - means 200 ms delay
+                       if ( '.' == c ) { usleep(200000); continue; }
+                       // * means 500 ms delay
+                       if ( '-' == c ) { usleep(500000); continue; }
+                       // ^ means rewind
+                       if ( '^' == c ) { lseek(fd, 0, 0); continue; }
+                       // 0-9 means new state
+                       if ( c < '0' || c > '9' ) continue;
+                       // set state
+                       ioctl(kfd, KDSETLED, (c-'0') & 7);
+               }
+               close(fd);

only if (ENABLE_FEATURE_CLEAN_UP)...

--
vda
_______________________________________________
busybox mailing list
[email protected]
http://busybox.net/cgi-bin/mailman/listinfo/busybox

Reply via email to