[<<Previous Entry] [^^Up^^] [Next Entry>>] [Menu] [About The Guide]
 Решения
Ответ без ввода новой строки

reply.c.:

  1 #include <stdio.h>
  2 #include <stdlib.h>
  3 #include <unistd.h>
  4 #include <termios.h>
  5 static char reply(char *);
  6
  7 main()
  8 {
  9     char c;
 10
 11     if( !isatty(0) ){
 12         fprintf(stderr,"standard input has been redirected\n");
 13         exit(1);
 14     }
 15
 16     c = reply("Are you going to dinner");
 17
 18     switch(c) {
 19         case 'y':
 20             printf("You have selected y.\n");
 21             break;
 22         case 'n':
 23             printf("You have selected n.\n");
 24             break;
 25         default :
 26             printf("Invalid reply.\n");
 27             break;
 28         }
 29 }
 30
 31 static char reply(char *question)
 32 {
 33     char c;
 34     struct termios tty, savtty;
 35
 36     printf("%s (y/n)? ", question);
 37     fflush(stdout);
 38     tcgetattr(0, &tty);
 39     savtty = tty;
 40     tty.c_lflag &= ~(ISIG | ICANON);
 41     tty.c_cc[VMIN] = 1;
 42     tcsetattr(0, TCSAFLUSH, &tty);
 43     read(0, &c, 1);
 44     tcsetattr(0, TCSANOW, &savtty);
 45     putchar('\n');
 46     return(c);
 47 }
Строчный редактор
linput.c

  1 #include <stdio.h>
  2 #include <stdlib.h>
  3 #include <ctype.h>
  4 #include <termios.h>
  5 #include <unistd.h>
  6 #include <stdlib.h>
  7 #define   BEL   "\07"           /* cntl-G */
  8 #define   MAXLINE  512
  9 #define   LINESIZE  40
 10 #define   BACKSPACE   write(1, "\b \b", 3)
 11
 12 main(int argc, char *argv[])
 13 {
 14     char line[MAXLINE], ch;
 15     int pos, newpos, savpos, i;
 16     struct termios tty, savetty;
 17
 18     if (!isatty(0)) {
 19         perror(argv[0]);
 20         exit(1);
 21     }
 22     if (tcgetattr(0, &tty) == -1) {
 23         perror(argv[0]);
 24         exit(2);
 25     }
 26     /* turnoff canonical input and echoing */
 27     savetty = tty;
 28     tty.c_cc[VMIN] = 1;
 29     tty.c_cc[VTIME] = 1;
 30     tty.c_lflag &= ~(ISIG | ICANON | ECHO);
 31     if (tcsetattr(0, TCSANOW, &tty) == -1) {
 32         perror(argv[0]);
 33         exit(3);
 34     }
 35
 36     pos = 0;
 37     while (read(0, &ch, 1) > 0) {
 38         /* if EOF in first position stop */
 39         if (ch == CEOT) {  /* cntl-D in termios.h */
 40             if (pos == 0)
 41                 break;
 42         }
 43         /* if ERASE character then erase previous character */
 44         else if (ch == tty.c_cc[VERASE]) {
 45             if (pos > 0) {
 46                 BACKSPACE;
 47                 --pos;
 48             }
 49         }
 50         /* if KILL character erase all data on current line */
 51         else if (ch == tty.c_cc[VKILL]) {
 52             while (pos > 0) {
 53                 BACKSPACE;
 54                 --pos;
 55             }
 56         }
 57         /* if cntl-W erase until beginning of last word */
 58         else if (ch == CWERASE) {  /* cntrl-W in termios.h */
 59             while (pos > 0 && isspace(line[pos-1])) {
 60                 BACKSPACE;
 61                 --pos;          /* trailing blanks */
 62             }
 63             while (pos > 0 && !isspace(line[pos-1])) {
 64                 BACKSPACE;
 65                 --pos;          /* last word */
 66             }
 67         }
 68         /* if new-line start a new line */
 69         else if (ch == '\n') {
 70             write(1, &ch, 1);
 71             pos = 0;
 72         }
 73         /* if not printable ring bell instead */
 74         else if (!isprint(ch)) {
 75             write(1, BEL, 1);
 76         }
 77         /* write character to screen and store it */
 78         else {
 79             write(1, &ch, 1);
 80             line[pos++] = ch;
 81         }
 82         /* wrap-around -- OPTIONAL */
 83         if (pos >= LINESIZE && !isspace(ch)) {
 84             savpos = pos;
 85             /* find begining of last word */
 86             while (pos > 0 && !isspace(line[pos-1]))
 87                 --pos;
 88             if (pos > 0) {
 89                 newpos = 0;
 90                 /* copy last word to beginning of line */
 91                 for (i = pos; i < savpos; i++) {
 92                     BACKSPACE;
 93                     line[newpos++] = line[i];
 94                 }
 95                 pos = newpos;
 96                 /* display word at beginning of line */
 97                 write(1, "\n", 1);
 98                 for (i = 0; i < pos; i++)
 99                     write(1, &line[i], 1);
 100             }
 101             else
 102                 write(1, "\n", 1);
 103         }
 104     }
 105
 106     /* reset terminal settings */
 107     tcsetattr(0, TCSANOW, &savetty);
 108 }