⭐ 欢迎来到虫虫下载站! | 📦 资源下载 📁 资源专辑 ℹ️ 关于我们
⭐ 虫虫下载站

📄 ttyfuncs.c

📁 Modem通讯程序包
💻 C
📖 第 1 页 / 共 2 页
字号:
      tty->c_cflag &= ~(PARENB | PARODD);      tty->c_cflag |= (PARENB);      break;   default:      return -EINVAL;  }  return 0;}/* Set the line speed of a terminal line. */static inttty_set_speed(struct termios *tty, char *speed){   speed_t spd;   spd = tty_find_speed( speed );   if( cfsetispeed( tty, spd ) == -1 )   {      FAIL( "setispeed" );      exit( 1 );   }   if( cfsetospeed( tty, spd ) == -1 )   {      FAIL( "cfsetospeed" );      exit( 1 );   }   tty->c_cflag |= CREAD;   tty->c_cflag &= ~(CLOCAL);   return 0;} /* Put a terminal line in a transparent state. */inttty_set_raw(struct termios *tty){   int i;   speed_t ospeed;   speed_t ispeed;      for(i = 0; i < NCCS; i++)      tty->c_cc[i] = '\0';   tty->c_cc[VMIN] = 1;   tty->c_cc[VTIME] = 5;   tty->c_iflag = IGNBRK ; /*| IGNPAR;*/   tty->c_oflag = 0;   tty->c_lflag = 0;   ospeed = cfgetospeed( tty );   ispeed = cfgetispeed( tty );      tty->c_cflag = CRTSCTS | HUPCL | CREAD;   cfsetispeed( tty, ispeed );   cfsetospeed( tty, ospeed );   return 0;}/* Fetch the state of a terminal. */static inttty_get_state( int fd, struct termios* tty ){   if (fd >= 0)    {      if (tcgetattr(fd, tty ) < 0)       {         FAIL( "tcgetattr" );         exit( 1 );      }      return 0;   }   return -1;} /* Set the state of a terminal. */static inttty_set_state( int fd, struct termios *tty){   if (fd >= 0)    {      if (tcsetattr(fd, TCSANOW, tty) < 0)       {         FAIL( "tcsetattr" );         exit( 1 );      }      return 0;   }   return -1; }  /* Return the TTY link's file descriptor. */inttty_askfd(){     if( ttyData == NULL )      return -1;   return ttyData->fd;}/* Set the number of databits a terminal line. */inttty_databits(char *bits){     if( ttyData == NULL )      return -1;   if (tty_set_databits(&ttyData->tty_current, bits) < 0)       return -1;   return tty_set_state(ttyData->fd, &ttyData->tty_current);}/* Set the number of stopbits of a terminal line. */inttty_stopbits(char *bits){     if( ttyData == NULL )      return -1;   if (tty_set_stopbits(&ttyData->tty_current, bits) < 0)       return -1;   return tty_set_state(ttyData->fd, &ttyData->tty_current);}/* Set the type of parity of a terminal line. */inttty_parity(char *type){     if( ttyData == NULL )      return -1;   if (tty_set_parity(&ttyData->tty_current, type) < 0)       return -1;   return tty_set_state( ttyData->fd, &ttyData->tty_current);}   /* Set the line speed of a terminal line. */inttty_speed(char *speed){   if (tty_set_speed(&ttyData->tty_current, speed) < 0)       return -1;   return tty_set_state( ttyData->fd, &ttyData->tty_current);}/* Hangup the line. */inttty_hangup(void){   struct sigaction act;   struct sigaction saved;   struct termios trm;   struct termios old;      if( ttyData == NULL )      return -1;   if( ttyData->fd == -1 )      return 0;   /* Ignore SIGHUP during hangup. Afterward we reset the SIGHUP signal */   if( sigaction( SIGHUP, NULL, &saved ) == -1 ||       sigaction( SIGHUP, NULL, &act   ) == -1 )   {      FAIL( "sigaction" );       exit( 1 );   }   act.sa_handler = SIG_IGN;   if( sigaction( SIGHUP, &act, NULL ) == -1 )   {      FAIL( "sigaction" );      exit( 1 );   }   if ( tcgetattr( ttyData->fd, &trm ) == -1 )   {      FAIL( "tcgetattr" );      exit( 1 );   }   if( tcgetattr( ttyData->fd, &old ) == -1 )   {      FAIL( "tcgetattr" );      exit( 1 );   }      if( cfsetospeed( &trm, B0 ) == -1 )   {      FAIL( "cfsetospeed" );      exit( 1 );   }      if( tcsetattr( ttyData->fd, TCSANOW, &trm ) == -1 )   {      FAIL( "tcsetattr" );      exit( 1 );   }      sleep( 1 );      /* restore terminal settings */   tcsetattr( ttyData->fd, TCSANOW, &old );      /* restore SIGHUP */   if( sigaction( SIGHUP, &saved, NULL ) == -1 )   {      FAIL( "sigaction" );      exit( 1 );   }      return 0;}   /* Flush input on the terminal. */inttty_flush( int fd){   return(0);}/* Reset terminal settings */int tty_reset(){   int result;   if( ttyData == NULL )      return -1;   if( ttyData->fd < 0 )      return -1;   result = tty_set_state( ttyData->fd, &ttyData->tty_saved );   return result;}/* Close down a terminal line. */inttty_close(void){   int result;   if( ttyData == NULL )      return -1;   if( ttyData->fd < 0 )      return -1;   close( ttyData->fd );   ttyData->fd = -1;   result = tty_lock( ttyData, 0);   tty_free( ttyData );   ttyData = NULL;         return result;}/* Open and initialize a terminal line. */inttty_open(char *name){   int flags;   ttyData = tty_alloc();   if( ttyData == NULL )   {      FAIL( "tty_alloc" );      exit( 1 );   }      ttyData->fd = -1;      /* Try opening the TTY device. */   if( name != NULL )   {      ttyData->dev = strdup(  make_path( name ) );      /* Now - can we lock it? */      if( tty_lock( ttyData, 1) )       {         return -1;      }      ttyData->fd = open( ttyData->dev, O_RDWR | O_NONBLOCK );      if( ttyData->fd < 0 )       {         tty_lock( ttyData, 0);         FAIL1( "open", ttyData->dev );         exit( 1 );      }   }    else      return -1;         /* Fetch the current state of the terminal. */   if (tty_get_state( ttyData->fd, &ttyData->tty_saved) < 0)    {      return -1;   }      memcpy ((char *)&ttyData->tty_current,      (char *)&ttyData->tty_saved, sizeof(struct termios));   /* Put this terminal line in a 8-bit transparent mode. */   if (tty_set_raw( &ttyData->tty_current ) < 0)    {      return -1;   }      /* If we are running in MASTER mode, set the default speed. */   if ((name != NULL) && (tty_set_speed(&ttyData->tty_current, "38400") != 0))    {      FAILn( "tty_open: cannot set 38400 bps!", "");      exit( 1 );   }     /* Set up a completely 8-bit clean line. */   if (tty_set_databits(&ttyData->tty_current, "8") ||       tty_set_stopbits(&ttyData->tty_current, "1") ||        tty_set_parity(&ttyData->tty_current, "N"))    {        FAILn( "libtty: tty_open: cannot set 8N1 mode!", "" );      exit( 1 );   }      /* Set the new line mode. */   if( tty_set_state( ttyData->fd, &ttyData->tty_current) < 0)       return -1;   /* Clear the NDELAY flag now (line is in CLOCAL) */   flags = fcntl( ttyData->fd, F_GETFL, 0);   fcntl( ttyData->fd, F_SETFL, flags & ~O_NONBLOCK );  /* OK, all done.  Lock this terminal line. */   return ttyData->fd;}void tty_sendbreak(void){   if( ttyData == NULL )      return;   if (ttyData->fd >= 0)      tcsendbreak(ttyData->fd, 0);} /* * creates absolute path name from path, possibly appending * /dev/ in front of path */char* make_path( char* path ){   static char buf[_POSIX_PATH_MAX];   /* absolute pathname */   if( path[0] == '/' )      return path;   sprintf( buf, "/dev/%s", path );   return buf;   }/* * create valid lock file name e.g. * /var/spool/LCK..cua1 from dev * */char* lock_file_name( char* dev ){   static char lock_file[_POSIX_PATH_MAX];   dev = basename( dev );   sprintf( lock_file, "%s/LCK..%s", _PATH_LOCKD, dev );   return lock_file;}/* * EOF libtty/ttyfuncs.c */

⌨️ 快捷键说明

复制代码 Ctrl + C
搜索代码 Ctrl + F
全屏模式 F11
切换主题 Ctrl + Shift + D
显示快捷键 ?
增大字号 Ctrl + =
减小字号 Ctrl + -