Usage: lockkeys key on | off | toggle

/*
 * Program to control the console lock keys states
 *
 * Build with “gcc -o lockkeys lockkeys.c”
 * cp lockkeys numlock.init.string /etc/
 * in /etc/inittab
 * “snum::sysinit:/etc/lockkeys numlock on </dev/console>;/dev/console 2>;&1”
 */
#include <stdio.h>
#include <fcntl.h>
#include <unistd.h>
#include <signal.h>
#include <sys/stat.h>
#include <linux/kd.h>
#include <sys/types.h>
#include <sys/ioctl.h>

#define ERROR -1

int main( int argc, char *argv[] )
{
        char c;
        int function, key;
int fd; /* File descriptor for console (/dev/tty/) */

        if ( argc != 3 ) {
                fprintf( stderr, “Usage: lockkeys key on | off | togglen” );
                exit( 1 );
        }

        if ( !strcmp( argv[2], “off” ))
                function=0;
        else if ( !strcmp( argv[2], “on” ))
                function=1;
        else if ( !strcmp( argv[2], “toggle” ))
                function=2;
        else {
                fprintf( stderr,
                        “lockkeys: function must be on, off, or togglen” );
                exit( 1 );
        }

        if ( !strcmp( argv[1], “numlock” ))
                key= LED_NUM;
        else if ( !strcmp( argv[1], “capslock” ))
                key= LED_CAP;
        else if ( !strcmp( argv[1], “scrolllock” ))
                key= LED_SCR;
        else {
                fprintf( stderr,
                        “lockkeys: key must be numlock, capslock, or scrolllockn” );
                exit( 1 );
        }

/* To be used as the fd in ioctl(). */
if ((fd = open(“/dev/console”, O_NOCTTY)) == ERROR)
{
perror(“open”);
exit(ERROR);
}

        if ( ioctl( fd, KDGETLED, &c ) != 0 ) {
                perror( “lockkeys: can’t get current lock state” );
                exit( 2 );
        }
        if ( function == 0 )
                c &= ~key;
        else if ( function == 1 )
                c |=  key;
        else
                c ^=  key;
        if ( ioctl( fd, KDSETLED, c ) != 0 ) {
                perror( “lockkeys: error setting lock state” );
                exit( 2 );
        }

close(fd);

        return 0;
}

未經允許不得轉載:GoMCU » Usage: lockkeys key on | off | toggle