home *** CD-ROM | disk | FTP | other *** search
/ NetNews Usenet Archive 1992 #27 / NN_1992_27.iso / spool / comp / unix / question / 13708 < prev    next >
Encoding:
Text File  |  1992-11-20  |  1.9 KB  |  77 lines

  1. Newsgroups: comp.unix.questions
  2. Path: sparky!uunet!zaphod.mps.ohio-state.edu!caen!umeecs!umn.edu!csus.edu!netcom.com!netcomsv!ramtek!jrb
  3. From: jrb@ramtek.uucp (James Burgess)
  4. Subject: Can I use stty(1) from inside a C program?
  5. Message-ID: <1992Nov20.221031.1188@ramtek.com>
  6. Followup-To: comp.unix.questions
  7. Keywords: stty, fork, ioctl, easy
  8. Sender: jrb@ramtek.com (James Burgess)
  9. Organization: Ramtek Corporation, San Jose, CA
  10. Distribution: na
  11. Date: Fri, 20 Nov 1992 22:10:31 GMT
  12. Lines: 63
  13.  
  14. Hi,
  15. my goal is to have my program talk to several different input
  16. devices, all of which have RS233 interfaces, but use different
  17. flavours of RS232; 7bit/8bit, even parity/no parity etc.
  18.  
  19. To begin with I was using ioctl() to set the appropriate modes, no 
  20. problem.  But now, I have many differnt devices to contend with
  21. so I thought I'd add a configuration file to my program... hmm... how
  22. to parse out the file and do the right thing with ioctl() in under 20
  23. lines of code, perhaps I can use /bin/stty to do it for me? so I put 
  24. in something like the following:
  25.  
  26.     static char *stty_argv[] = {
  27.     "stty",
  28.     "cs7",      /* 7 bit data */
  29.     "parenb",   /* parity check */
  30.     "-parodd",  /* even parity  */
  31.     "cstopb",   /* two stop bits */
  32.     "-icanon",  /* no buffering  */
  33.     NULL,
  34.     };
  35.  
  36.     int pid;
  37.     int status
  38.  
  39.     if ( (pid = fork()) == 0)
  40.     {
  41.         if (dup2( fd, 0 ) == -1)
  42.         {
  43.             perror( "dup2" );
  44.             _exit(1);
  45.         }
  46.  
  47.         execvp( "stty", stty_argv );
  48.         perror( "exec" );
  49.         _exit(1);
  50.     }
  51.     else if ( pid > 0 )
  52.     {
  53.         if ( waitpid( pid, &status, 0) != pid )
  54.         {
  55.             perror( "wait" );
  56.             exit(1);
  57.         }
  58.  
  59.         printf("status of child was %d\n", (status >> 8) & 0xff );
  60.     }
  61.     else
  62.     {
  63.         perror( "fork");
  64.         exit(1);
  65.     }
  66.  
  67. Alas, stty runs ok, but the tty modes stay the same :-(
  68.  
  69. 1) Why doesn't this work ?
  70.  
  71. 2) Can anybody think of a better way that avoids parsing a file and 
  72. calling ioctl()?
  73.  
  74. James Burgess. 
  75. ___
  76. jrb@ramtek.com  Tel. (408) 954 2839
  77.