This appendix contains three sample programs showing terminal (tty)
modem control: an ULTRIX program showing an outgoing phone call, a DIGITAL UNIX program
showing an outgoing phone call, and a DIGITAL UNIX program showing an incoming phone
call.
The ULTRIX system uses TIOCCAR, TIOCNAR, and TIOCWONLINE requests to
the
ioctl()
system call.
These requests are not supported
on a DIGITAL UNIX system.
See
Section 7.8
for more information.
Example D-1 demonstrates how an ULTRIX application interacts with a modem for outgoing calls. Error checking of the return values of the system calls is purposely omitted to simplify the example.
fd = open(dcname, O_RDWR|O_NDELAY); [1] ioctl(fd, TIOCMODEM, &temp); [2] ioctl(fd, TIOCNCAR); [3]
.
.
.
/* * Dial the phone number and negotiate with auto calling unit. */
.
.
.
ioctl(fd, TIOCCAR); [4] alarm(40); ioctl(fd, TIOCWONLINE); [5] alarm(0);
Opens the line and does not wait for carrier. [Return to example]
Monitors the modem signals. [Return to example]
Allows read and write calls to succeed regardless of whether carrier is present. [Return to example]
Allows read and write calls to succeed only if carrier is present. [Return to example]
Waits for carrier. [Return to example]
int fd, flags; struct termios tty_termios; [1] fd = open(ttyname,O_RDWR | O_NONBLOCK); [2] tcgetattr(fd,&tty_termios); [3] if ((tty_termios.c_cflag & CLOCAL) == 0) { tty_termios.c_cflag |= CLOCAL; [4] tcsetattr(fd,TCSANOW,&tty_termios); } flags = fcntl(fd, F_GETFL) fcntl(fd, F_SETFL, flags & ~O_NONBLOCK) [5]
.
.
.
/* * dial phone number and negotiate with modem. */
.
.
.
tty_termios.c_cflag &= ~CLOCAL; [6] tcsetattr(fd,&tty_termios); [7] alarm(40); [8] read(fd,buffer,count); [9] alarm(0); [10]
Contains information about the serial line that can be inspected
and altered using the POSIX
tcgetattr()
and
tcsetattr()
library routines.
[Return to example]
Opens the terminal line. The CLOCAL flag is usually set by default, allowing you to ignore modem status lines. Use O_NONBLOCK in case CLOCAL is not set. [Return to example]
Gets current line attributes. [Return to example]
Sets CLOCAL, if it is not set. The line must be in local mode in order to talk to the modem. [Return to example]
Turns off O_NONBLOCK; in local mode the application does not need it. [Return to example]
Puts the line into modem mode by turning off CLOCAL. The next I/O operation to the line will block until carrier is present. [Return to example]
Watches for modem signals. [Return to example]
Sets a timer so the application does not wait forever. [Return to example]
This
read()
call blocks, pending the appearance
of modem signals.
[Return to example]
Turns off timer; the connection with the remote system has been established. [Return to example]
int fd; int lined; struct termios tty_termios; [1] fd = open(ttyname,O_RDWR | O_NONBLOCK); [2] flags = fcntl(fd, F_GETFL) fcntl(fd, F_SETFL, flags & ~O_NONBLOCK) [3] setsid(); [4] ioctl(0, TIOCSCTTY, 0); [5] lined = 0; ioctl(0, TIOCSETD, &lined); [6] tcgetattr(fd,&tty_termios); [7] tty_termios.c_cflag &= ~CLOCAL; [8] tty_termios.c_cflag &= ~CBAUD; tty_termios.c_cflag |= B2400; [9] tcsetattr(fd,TCSANOW,&tty_termios); [10] for (;;) { write(fd,"\r\nlogin: ",9); [11] read(fd,buffer,count); [12] if (valid_login_name(buffer)) execl("/usr/bin/login",buffer); [13] }
Contains information about the serial line that can be inspected
and altered using the POSIX
tcgetattr()
and
tcsetattr()
library routines.
[Return to example]
Opens the terminal line. The CLOCAL flag is usually set by default, allowing you to ignore modem status lines. Use O_NONBLOCK in case CLOCAL is not set. [Return to example]
Turns off O_NONBLOCK; you do not need it. [Return to example]
Creates a new session, becomes session leader for new session, and becomes process leader for new process group. [Return to example]
Sets the controlling terminal. [Return to example]
Sets the line discipline to 0 (POSIX). [Return to example]
Gets current line attributes. [Return to example]
Turns off CLOCAL for a modem. [Return to example]
Clears baud rate bits and sets them to 2400. [Return to example]
Sets the line attributes. [Return to example]
This write call, containing the login message, blocks until someone dials in on the modem and all the signals are present. [Return to example]
Gets the user's login name. [Return to example]
Executes the login program, if the login name is valid. [Return to example]