#include <stdio.h>
#include <unistd.h>
#include <termios.h>
#include <fcntl.h>

enum{ERROR=-1,SUCCESS};
typedef unsigned long Ulong;
typedef struct termios TTY;


speed_t baud_konstante[] = {   B0,B50,B75,B110,B134,B150,B200,B300,B600,B1200,
                               B1800,B2400,B4800,B9600,B19200,B38400,B57600,
                               B115200,B230400,B460800
                           };

Ulong baud_werte[] = {   0,50,75,110,134,150,200,300,600,1200,
                         1800,2400,4800,9600,19200,38400,57600,
                         115200,230400,460800
                     };

Ulong baudrate_wert(speed_t baud)
{
 int i;

 for(i=0; i<=19; i++)
   {
       if(baud == baud_konstante[i])
           return(baud_werte[i]);
   }
 return ERROR;
}


int main(int argc, char **argv)
{
 int fd;
 speed_t baud_input,  baud_output;
 TTY terminal;

 if(argc != 2)
   {
     printf("Bitte einen Gerätepfad mitangeben!!!\n");
     exit(0);
   }

 if((fd =open(argv[1], O_RDWR | O_NONBLOCK)) == ERROR)
   {
     printf("Konnte Gerätedatei %s nicht öffnen!!\n",argv[1]);
     exit(1);
   }

 if(isatty(fd) == 0)
   {
      printf("%s ist keine tty.......\n",argv[1]);
      exit(2);
   }

 if(tcgetattr(fd, &terminal) == ERROR)
   {
      printf("Fehler bei tcgetattr......\n");
      exit(3);
   }

 if(( baud_input = baudrate_wert(cfgetispeed(&terminal))) == ERROR)
   {
      printf("Fehler bei baud_input........\n");
      exit(4);
   }

 if(( baud_output = baudrate_wert(cfgetospeed(&terminal))) == ERROR)
   {
      printf("Fehler bei baud_output........\n");
      exit(5);
   }

 printf("Eingabe-Baudrate : %lu\n",baud_input);
 printf("Ausgabe_Baudrate : %lu\n",baud_output);

 return 0;
}