View of xos/tests/read.c


XOS | Parent Directory | View | Download

/* Copyright (C) 2008  Emmanuel Varoquaux
 
   This file is part of XOS.
 
   This program is free software: you can redistribute it and/or modify
   it under the terms of the GNU General Public License as published by
   the Free Software Foundation, either version 3 of the License, or
   (at your option) any later version.
 
   This program is distributed in the hope that it will be useful,
   but WITHOUT ANY WARRANTY; without even the implied warranty of
   MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
   GNU General Public License for more details.
 
   You should have received a copy of the GNU General Public License
   along with this program.  If not, see <http://www.gnu.org/licenses/>. */
 
#include <termios.h>
#include <unistd.h>
#include <stdio.h>
 
static struct termios stermios;
static struct termios termios;
 
static void keywait()
{
  char c;
 
  printf("Appuyez sur une touche pour continuer...");
  fflush(stdout);
  termios.c_lflag = ISIG;
  termios.c_cc[VMIN] = 1;
  termios.c_cc[VTIME] = 0;
  tcsetattr(0, TCSANOW, &termios);
  read(0, &c, 1);
  printf("\n\n");
}
 
int main()
{
  char buf[10];
  int n;
 
  tcgetattr(0, &stermios);
 
#if 1
  /* Mode canonique */
  printf("Cas : Mode canonique\n");
  do {
    printf("Attente de 5 secondes...");
    fflush(stdout);
    sleep(5);
    printf("\nLecture : ");
    fflush(stdout);
    n = read(0, buf, 10);
    printf("\nJ'ai lu %d : %.*s\n", n, n, buf);
  }
  while (n);
  keywait();
#endif
 
#if 1
  /* Case A: MIN > 0, TIME > 0 */
  termios = stermios;
  termios.c_lflag = ISIG | ECHO;
  termios.c_cc[VMIN] = 5;
  termios.c_cc[VTIME] = 50;
  tcsetattr(0, TCSANOW, &termios);
  printf("Cas : MIN = %d, TIME = %d\n", termios.c_cc[VMIN], termios.c_cc[VTIME]);
  printf("Attente de 5 secondes...");
  fflush(stdout);
  sleep(5);
  printf("\nLecture : ");
  fflush(stdout);
  n = read(0, buf, 10);
  printf("\nJ'ai lu %d : %.*s\n", n, n, buf);
  keywait();
#endif
 
#if 1
  /* Case B: MIN > 0, TIME = 0 */
  termios = stermios;
  termios.c_lflag = ISIG | ECHO;
  termios.c_cc[VMIN] = 5;
  termios.c_cc[VTIME] = 0;
  tcsetattr(0, TCSANOW, &termios);
  printf("Cas : MIN = %d, TIME = %d\n", termios.c_cc[VMIN], termios.c_cc[VTIME]);
  printf("Attente de 5 secondes...");
  fflush(stdout);
  sleep(5);
  printf("\nLecture : ");
  fflush(stdout);
  n = read(0, buf, 10);
  printf("\nJ'ai lu %d : %.*s\n", n, n, buf);
  keywait();
#endif
 
#if 1
  /* Case C: MIN = 0, TIME > 0 */
  termios = stermios;
  termios.c_lflag = ISIG | ECHO;
  termios.c_cc[VMIN] = 0;
  termios.c_cc[VTIME] = 50;
  tcsetattr(0, TCSANOW, &termios);
  printf("Cas : MIN = %d, TIME = %d\n", termios.c_cc[VMIN], termios.c_cc[VTIME]);
  printf("Attente de 5 secondes...");
  fflush(stdout);
  sleep(5);
  printf("\nLecture : ");
  fflush(stdout);
  n = read(0, buf, 10);
  printf("\nJ'ai lu %d : %.*s\n", n, n, buf);
  keywait();
#endif
 
#if 1
  /* Case D: MIN = 0, TIME = 0 */
  termios = stermios;
  termios.c_lflag = ISIG | ECHO;
  termios.c_cc[VMIN] = 0;
  termios.c_cc[VTIME] = 0;
  tcsetattr(0, TCSANOW, &termios);
  printf("Cas : MIN = %d, TIME = %d\n", termios.c_cc[VMIN], termios.c_cc[VTIME]);
  printf("Attente de 5 secondes...");
  fflush(stdout);
  sleep(5);
  printf("\nLecture : ");
  fflush(stdout);
  n = read(0, buf, 10);
  printf("\nJ'ai lu %d : %.*s\n", n, n, buf);
  keywait();
#endif
 
  tcsetattr(0, TCSANOW, &stermios);
  return 0;
}