home *** CD-ROM | disk | FTP | other *** search
- Newsgroups: comp.unix.questions
- Path: sparky!uunet!zaphod.mps.ohio-state.edu!caen!umeecs!umn.edu!csus.edu!netcom.com!netcomsv!ramtek!jrb
- From: jrb@ramtek.uucp (James Burgess)
- Subject: Can I use stty(1) from inside a C program?
- Message-ID: <1992Nov20.221031.1188@ramtek.com>
- Followup-To: comp.unix.questions
- Keywords: stty, fork, ioctl, easy
- Sender: jrb@ramtek.com (James Burgess)
- Organization: Ramtek Corporation, San Jose, CA
- Distribution: na
- Date: Fri, 20 Nov 1992 22:10:31 GMT
- Lines: 63
-
- Hi,
- my goal is to have my program talk to several different input
- devices, all of which have RS233 interfaces, but use different
- flavours of RS232; 7bit/8bit, even parity/no parity etc.
-
- To begin with I was using ioctl() to set the appropriate modes, no
- problem. But now, I have many differnt devices to contend with
- so I thought I'd add a configuration file to my program... hmm... how
- to parse out the file and do the right thing with ioctl() in under 20
- lines of code, perhaps I can use /bin/stty to do it for me? so I put
- in something like the following:
-
- static char *stty_argv[] = {
- "stty",
- "cs7", /* 7 bit data */
- "parenb", /* parity check */
- "-parodd", /* even parity */
- "cstopb", /* two stop bits */
- "-icanon", /* no buffering */
- NULL,
- };
-
- int pid;
- int status
-
- if ( (pid = fork()) == 0)
- {
- if (dup2( fd, 0 ) == -1)
- {
- perror( "dup2" );
- _exit(1);
- }
-
- execvp( "stty", stty_argv );
- perror( "exec" );
- _exit(1);
- }
- else if ( pid > 0 )
- {
- if ( waitpid( pid, &status, 0) != pid )
- {
- perror( "wait" );
- exit(1);
- }
-
- printf("status of child was %d\n", (status >> 8) & 0xff );
- }
- else
- {
- perror( "fork");
- exit(1);
- }
-
- Alas, stty runs ok, but the tty modes stay the same :-(
-
- 1) Why doesn't this work ?
-
- 2) Can anybody think of a better way that avoids parsing a file and
- calling ioctl()?
-
- James Burgess.
- ___
- jrb@ramtek.com Tel. (408) 954 2839
-