/* * Sample program for ioctl system call - No.54 * * ioctl - control device * * Synopsis * #include * * int ioctl(int d, int request, ...); * * [The "third" argument is traditionally char *argp, and * will be so named for this discussion.] * * Description * The ioctl function manipulates the underlying device * parameters of special files. In particular, many operating * characteristics of character special files (e.g. terminals) * may be controlled with ioctl requests. The argument d * must be an open file descriptor. * * An ioctl request has encoded in it whether the argument is * an in parameter or out parameter, and the size of the * argument argp in bytes. Macros and defines used in specifying * an ioctl request are located in the file . * * Types - * struct termio * { * unsigned short int c_iflag; * unsigned short int c_oflag; * unsigned short int c_cflag; * unsigned short int c_lflag; * unsigned char c_line; * unsigned char c_cc[8 ]; * }; * * Macros - * #define __ARCH_I386_IOCTLS_H__ * #define TCGETS 0x5401 * #define TCSETS 0x5402 * #define TCSETSW 0x5403 * #define TCSETSF 0x5404 * #define TCGETA 0x5405 * set terminal attributes * * #define TCSETA 0x5406 * #define TCSETAW 0x5407 * #define TCSETAF 0x5408 * set teminal attributes and flush the buffer * * #define TCSBRK 0x5409 * #define TCXONC 0x540A * #define TCFLSH 0x540B * .............................. * * * #define ECHO 0000010 * 'enable echoing' bit of c_lflag * * * see 'man ioctl_list' or 'man termios' for more information */ #include #include #include #include #include #include #define BYTESIZE 8 void printbits(unsigned short int flag) { int i; for (i = 0; i < BYTESIZE * sizeof(flag); ++i) printf("%d", (flag << i & 1 << BYTESIZE * sizeof(flag) -1) ? 1 : 0); putchar('\n'); } print_termios(struct termio *tty) { int i; printf("termio.c_iflag: "); printbits(tty->c_iflag); printf("termio.c_oflag: "); printbits(tty->c_oflag); printf("termio.c_cflag: "); printbits(tty->c_cflag); printf("termio.c_lflag: "); printbits(tty->c_lflag); printf("termio.c_line: %d\n", tty->c_line); for (i = 0; i < 8; i++) printf("termio.c_cc[%d]: %d\n", i, tty->c_cc[i]); } main() { int fd; char name[20]; struct termio tty, tty_bak; /* The path /dev/pts/0 is a device file of the terminal emulator such as kterm. 0 is for the terminal opened fist, and 1 for the terminal opened second. So when you test this program, you need to open only one kterm on the desktop */ fd = open("/dev/pts/0", O_RDWR); /* get terminal attributes */ ioctl(fd, TCGETA, &tty); tty_bak = tty; print_termios(&tty); /* disable echoing by unsetting ECHO bit of c_lflag */ tty.c_lflag &= ~ECHO; ioctl(fd, TCSETAF, &tty); printf("\nechoing disabled\n"); printf("termio.c_lflag: "); printbits(tty.c_lflag); printf("name = "); fgets(name, sizeof name, stdin); name[strlen(name)-1] = '\0'; printf("\nhello, %s.\n", name); /* recover the initial setting */ ioctl(fd, TCSETAF, &tty_bak); exit(0); }