|
发表于 2003-5-27 18:17:48
|
显示全部楼层
给你修改了一下:
- #include <stdio.h>
- #include <sys/types.h>
- #include <unistd.h>
- #include <termios.h>
- int main(void)
- {
- char c;
- int fd[2];
- pid_t pid;
- struct termios initialsettings, newsettings; //initialsettings保存初始终端设置
-
- //读取当前终端设置并保存
- tcgetattr( fileno(stdin), &initialsettings);
-
- //备份终端设置
- newsettings=initialsettings;
- //在newsettings中关闭echo
- newsettings.c_lflag &= ~ECHO;
- //用新参数对终端进行设置并检测返回值
- if(tcsetattr(fileno(stdin), TCSAFLUSH, &newsettings) != 0) {
- fprintf(stderr, "Could not set attributes\n");
- }
-
- pipe(fd);
- if ((pid = fork()) == -1) {
- perror("frok error");
- exit(-1);
- } else if (pid == 0) {
- while (read(fd[0], &c, 1) > 0) {
- if (c == 'x')
- break;
- if (c >= 'a' && c <= 'z')
- c -= 32;
- write(STDOUT_FILENO, &c, 1);
- }
- } else {
- while (c = getchar()) {
- write(fd[1], &c, 1);
- if (c == 'x')
- break;
- putchar(c);
- }
- }
-
- //恢复初始设置
- tcsetattr(fileno(stdin), TCSANOW, &initialsettings);
- exit(0);
- }
复制代码
编译时要加上-lcurses。 linux中要用getchar()函数。
下次粘贴代码时,请务必按置顶贴子的要求做。 |
|