Advanced Programming in the UNIX® Environment

(lily) #1
ptg10805159

Appendix C Chapter 18 Solutions 943


if ((cmptr = calloc(1, CMSG_LEN(2*sizeof(int)))) == NULL)
err_sys("calloc error");
msg.msg_control = cmptr;
msg.msg_controllen = CMSG_LEN(2*sizeof(int));
/* continue initializing msghdr... */
cmptr->cmsg_len = CMSG_LEN(2*sizeof(int));
cmptr->cmsg_level = SOL_SOCKET;
cmptr->cmsg_type = SCM_RIGHTS;
ip = (int *)CMSG_DATA(cmptr);
*ip++ = fd1;
*ip = fd2;
This approach works on all four platforms covered in this book. The second
option is to pack two separatecmsghdrstructures into a single message:
struct msghdr msg;
struct cmsghdr *cmptr;
if ((cmptr = calloc(1, 2*CMSG_LEN(sizeof(int)))) == NULL)
err_sys("calloc error");
msg.msg_control = cmptr;
msg.msg_controllen = 2*CMSG_LEN(sizeof(int));
/* continue initializing msghdr... */
cmptr->cmsg_len = CMSG_LEN(sizeof(int));
cmptr->cmsg_level = SOL_SOCKET;
cmptr->cmsg_type = SCM_RIGHTS;
*(int *)CMSG_DATA(cmptr) = fd1;
cmptr = CMPTR_NXTHDR(&msg, cmptr);
cmptr->cmsg_len = CMSG_LEN(sizeof(int));
cmptr->cmsg_level = SOL_SOCKET;
cmptr->cmsg_type = SCM_RIGHTS;
*(int *)CMSG_DATA(cmptr) = fd2;
Unlike the first approach, this method works only on FreeBSD 8.0.

Chapter 18


18.1 Note that you have to terminate theresetcommand with a line feed character,
not a return, since the terminal is in noncanonical mode.

18.2 It builds a table for each of the 128 characters and sets the high-order bit (the
parity bit) according to the user ’s specification. It then uses 8-bit I/O, handling
the parity generation itself.
18.3 If you happen to be on a windowing terminal, you don’t need to log in twice.
Youcan do this experiment between two separate windows. Under Solaris,
execute stty -a with standardinput redirected from the terminal window
runningvi.This shows thatvisets MIN to 1 and TIME to 1.Acall toreadwill
wait for at least one character to be typed, but after that character is entered,
readwaits only one-tenth of a second for additional characters beforereturning.
Free download pdf