home *** CD-ROM | disk | FTP | other *** search
/ NetNews Usenet Archive 1992 #19 / NN_1992_19.iso / spool / comp / unix / internal / 1756 < prev    next >
Encoding:
Text File  |  1992-08-27  |  18.4 KB  |  764 lines

  1. Newsgroups: comp.unix.internals
  2. Path: sparky!uunet!haven.umd.edu!darwin.sura.net!udel!gvls1!jabber!candle!root
  3. From: root@candle.uucp (Bruce Momjian)
  4. Subject: select()/poll() source code posting
  5. Organization: a consultant's basement
  6. Date: Thu, 27 Aug 1992 21:22:35 GMT
  7. Message-ID: <1992Aug27.212235.1684@candle.uucp>
  8. X-Newsreader: Tin 1.1 PL5
  9. Lines: 753
  10.  
  11. Attached is a shar file containing an implementation of poll() and
  12. select() for systems that lack them.  It is implemented as a device
  13. driver.  I have called it pol() and selec() so as not to confuse them
  14. with the originals.  It only works on reads and writes.
  15.  
  16. This package is not ready for prime-time.  If there is enough interest,
  17. and if someone can help me with the intricacies of the tty structure
  18. under different modes, I would be glad to polish it up.
  19.  
  20. The shar file is 17k.  My thanks to those people in comp.unix.internals
  21. who helped me with some of the details.  This code was written by
  22. looking at other PD device driver implementaions, like fas(async),
  23. pty(ptys), herc(hercules graphics), and mouse(mouse).  The code was
  24. developed on AT&T 386 Unix SVr3.1, which has poll(), but only for
  25. streams. 
  26.  
  27. -------------------------------------------------------------------
  28. #!/bin/sh
  29. # This is a shell archive (shar 3.32)
  30. # made 08/27/1992 20:44 UTC by root@candle
  31. # Source directory /u/src/pol/tmp
  32. #
  33. # existing files WILL be overwritten
  34. #
  35. # This shar contains:
  36. # length  mode       name
  37. # ------ ---------- ------------------------------------------
  38. #    877 -rw-r--r-- INSTALL
  39. #    964 -rw-r--r-- README
  40. #   8399 -rw-r--r-- pol.c
  41. #    521 -rw-r--r-- pol.h
  42. #   2624 -rw-r--r-- pol.lib.c
  43. #    305 -rw-r--r-- poltest.c
  44. #     62 -rw-r--r-- selec.h
  45. #
  46. if touch 2>&1 | fgrep 'amc' > /dev/null
  47.  then TOUCH=touch
  48.  else TOUCH=true
  49. fi
  50. # ============= INSTALL ==============
  51. echo "x - extracting INSTALL (Text)"
  52. sed 's/^X//' << 'SHAR_EOF' > INSTALL &&
  53. XBasically, pol.c is installed as a device driver with this line in 
  54. Xthe mdevice file:
  55. X
  56. Xpol    oci    iHc    pol    0    12    1    1    -1
  57. X                    ^
  58. X                    | major number of your choice
  59. X
  60. XAdd an appropriate file in sdevice.d link this:
  61. X
  62. Xpol    Y    1    0    0    0    0    0    0    0
  63. X
  64. Xand add a file in node.d so a character device called /dev/pol getsc
  65. Xcreated:
  66. X
  67. Xpol    pol    c    0
  68. X
  69. XOnly one entry is needed, even though many programs can pol at the same
  70. Xtime. 
  71. X
  72. XAdd a subdirectory under /etc/conf/pack.d called pol and compile pol.c
  73. Xand copy it into /etc/conf/pack.d/pol/Driver.o.
  74. X
  75. XCompile pol.lib.c and install it in your personal C library.  It will be
  76. Xthe link points for pol() and selec().
  77. X
  78. XInstall pol.h and selec.h in /usr/include/sys.
  79. X
  80. XREADME         - this file
  81. Xpol.c        - device driver
  82. Xpol.lib.c    - C library module
  83. Xpol.h        - /usr/include/sys/pol.h file
  84. Xselec.h        - /usr/include/sys/selec.h file
  85. Xpoltest.c    - used to test the device driver
  86. SHAR_EOF
  87. $TOUCH -am 0827161092 INSTALL &&
  88. chmod 0644 INSTALL ||
  89. echo "restore of INSTALL failed"
  90. set `wc -c INSTALL`;Wc_c=$1
  91. if test "$Wc_c" != "877"; then
  92.     echo original size 877, current size $Wc_c
  93. fi
  94. # ============= README ==============
  95. echo "x - extracting README (Text)"
  96. sed 's/^X//' << 'SHAR_EOF' > README &&
  97. XBasically, this package implements poll() and select() as a device
  98. Xdriver.  It is called pol() and selec() so as not to be confused with
  99. Xthe real thing.
  100. X
  101. XThe driver accepts bit masks of read and write file descriptors.  It
  102. Xfollows the descriptors to the file, then inode tables and gets the
  103. Xmajor and minor device numbers.  It then finds the tty structures
  104. Xassociated with those devices and stores their addresses.
  105. X
  106. XIt scans through the addresses once, looking for read or written
  107. Xcharacters, then either returns or goes to sleep.  Upon invocation, it
  108. Xreplaces the standard tty input/output functions with its own functions,
  109. Xwhich wake up the driver and call the usual tty functions.  In this way,
  110. Xthe driver is woken up when tty activity occurs. 
  111. X
  112. XA C library library is provided to translate the normal poll() and
  113. Xselect() parameters into pol device driver parameters, and then call the
  114. Xdevice driver.
  115. X
  116. XBruce Momjian, root@candle.uucp (root%candle.uucp@bts.com)
  117. SHAR_EOF
  118. $TOUCH -am 0827153792 README &&
  119. chmod 0644 README ||
  120. echo "restore of README failed"
  121. set `wc -c README`;Wc_c=$1
  122. if test "$Wc_c" != "964"; then
  123.     echo original size 964, current size $Wc_c
  124. fi
  125. # ============= pol.c ==============
  126. echo "x - extracting pol.c (Text)"
  127. sed 's/^X//' << 'SHAR_EOF' > pol.c &&
  128. X/*
  129. X    pol.c    - poll/select() implemented as a device driver
  130. X
  131. X    Bruce Momjian (root%candle.uucp@bts.com
  132. X
  133. X*/
  134. X
  135. X/* tabs = 4 */
  136. X/* Include files */
  137. X#undef DEBUG
  138. X#undef USE_BUF    /* use tty->t/rbuf */
  139. X
  140. X#ifdef DEBUG
  141. X#define    DB(x)    x
  142. X#else
  143. X#define DB(x)
  144. X#endif
  145. X
  146. X#include <errno.h>
  147. X#include <sys/pol.h>
  148. X#include <sys/types.h>
  149. X#include <sys/param.h>
  150. X#include <sys/ioctl.h>
  151. X#include <sys/dir.h>
  152. X#include <sys/signal.h>
  153. X#include <sys/user.h>
  154. X#include <sys/file.h>
  155. X#include <sys/inode.h>
  156. X#include <sys/conf.h>
  157. X#include <sys/tty.h>
  158. X#include <sys/sxt.h>
  159. X#include <sys/sysmacros.h>
  160. X
  161. X#define TTIN_FUNCT        ttin
  162. X#define TTWRITE_FUNCT    ttwrite
  163. X#define POL_READ_FLAG        0
  164. X#define POL_WRITE_FLAG        1
  165. X
  166. Xint    pol_uses = 0;
  167. Xint pol_intr = 0;
  168. X
  169. Xextern struct tty sxt_tty[], pts_tty[];
  170. Xextern int sxtopen(), ptmopen();
  171. Xextern char sxt_buf[];
  172. Xextern int ttin(), ttwrite();
  173. X
  174. Xint ttinpol(), ttwrpol(), poltimeo();
  175. Xextern time_t lbolt;
  176. X
  177. Xstruct pol_table {
  178. X        caddr_t addr
  179. X#ifdef USE_BUF
  180. X                , addr2
  181. X#endif
  182. X        ;
  183. X        int        fd;
  184. X};
  185. X
  186. X/*---------------------------------------------------------------------------
  187. X**
  188. X**    polopen
  189. X**
  190. X**--------------------------------------------------------------------------*/
  191. Xpolopen(fdev, flags)
  192. Xint    fdev, flags;
  193. X{
  194. X    if (pol_uses == 0)
  195. X    {
  196. X        if (linesw[0].l_input != TTIN_FUNCT)
  197. X        {
  198. X            printf("pol error:  linesw structure corrupted.\n");
  199. X              u.u_error = ENOMSG;
  200. X              u.u_rval1 = -1;
  201. X            return -1;
  202. X        }
  203. X        if (linesw[0].l_write != TTWRITE_FUNCT)
  204. X        {
  205. X            printf("pol error:  linesw structure corrupted.\n");
  206. X            u.u_error = ENOMSG;
  207. X            u.u_rval1 = -1;
  208. X            return -1;
  209. X        }
  210. X        spltty();
  211. X        linesw[0].l_input = ttinpol;
  212. X        linesw[0].l_write = ttwrpol;
  213. X        spl0();
  214. X        pol_uses = 1;
  215. X    }        
  216. X    return 0;
  217. X}
  218. X
  219. X/*---------------------------------------------------------------------------
  220. X**
  221. X**    polclose
  222. X**
  223. X**--------------------------------------------------------------------------*/
  224. Xpolclose(fdev)
  225. Xint    fdev;
  226. X{
  227. X    pol_uses = 0;
  228. X    if (linesw[0].l_input != ttinpol)
  229. X    {
  230. X        printf("pol error:  linesw structure corrupted.\n");
  231. X        u.u_error = ENOMSG;
  232. X        u.u_rval1 = -1;
  233. X    }
  234. X    else if (linesw[0].l_write != ttwrpol)
  235. X    {
  236. X        printf("pol error:  linesw structure corrupted.\n");
  237. X        u.u_error = ENOMSG;
  238. X        u.u_rval1 = -1;
  239. X    }
  240. X    else    u.u_rval1 = 0;
  241. X    spltty();
  242. X    linesw[0].l_input = ttin;
  243. X    linesw[0].l_write = ttwrite;
  244. X    spl0();
  245. X    return u.u_rval1;
  246. X}
  247. X
  248. X/*---------------------------------------------------------------------------
  249. X**
  250. X**    polioctl
  251. X**
  252. X**--------------------------------------------------------------------------*/
  253. Xpolioctl(fdev, command, polfd_p, mode) 
  254. Xint    fdev, command, mode;
  255. Xstruct polfd *polfd_p;
  256. X{
  257. X    int     i,
  258. X            rfds_num = 0,
  259. X            wfds_num = 0,
  260. X            hits = 0,
  261. X            timeout_idx = 0;
  262. X    time_t    start_ticks = lbolt, timeout_ticks;
  263. X    struct polfd pol_s;
  264. X    struct pol_table rfds_pol[NPOLFILE],
  265. X                     wfds_pol[NPOLFILE];
  266. X    
  267. X    DB(printf("Start func\n"));
  268. X    if (copyin(polfd_p, &pol_s, sizeof(struct polfd)) != 0)
  269. X    {
  270. X        u.u_error = EFAULT;
  271. X        u.u_rval1 = -1;
  272. X        return -1;
  273. X    }
  274. X    if (command != POL_FDS)
  275. X    {
  276. X        u.u_error = EINVAL;        
  277. X        u.u_rval1 = -1;
  278. X        return -1;
  279. X    }
  280. X    
  281. X    if (pol_s.timeout != -1 && pol_s.timeout != 0)
  282. X    {
  283. X        timeout_ticks = start_ticks + pol_s.timeout * (HZ)/1000;
  284. X        timeout_idx = timeout(    poltimeo,
  285. X                                (caddr_t)NULL,
  286. X                                pol_s.timeout * (HZ)/1000);
  287. X    }
  288. X
  289. X    DB(printf("Start for loop.\n"));    
  290. X    DB(printf("Start while loop.\n"));
  291. X    if (poladdr( pol_s.rfds, rfds_pol, &rfds_num, POL_READ_FLAG) == -1)
  292. X    {
  293. X        if (timeout_idx != 0 && lbolt < timeout_ticks)
  294. X            untimeout(timeout_idx);
  295. X        return -1;
  296. X    }
  297. X    if (poladdr( pol_s.wfds, wfds_pol, &wfds_num, POL_WRITE_FLAG) == -1)
  298. X    {
  299. X        if (timeout_idx != 0 && lbolt < timeout_ticks)
  300. X            untimeout(timeout_idx);
  301. X        return -1;
  302. X    }
  303. X
  304. X    pol_s.rfds = pol_s.wfds = 0;
  305. X    
  306. X    while(1)
  307. X    {
  308. X        do
  309. X        {
  310. X            pol_intr = 0;
  311. X            spl0();
  312. X            for (i=0; i < rfds_num; i++)
  313. X                if (*(rfds_pol[i].addr) != 0)
  314. X                {
  315. X                    pol_s.rfds |= 1 << rfds_pol[i].fd;
  316. X                    hits++;
  317. X                }
  318. X#ifdef USE_BUF
  319. X                else if (*(rfds_pol[i].addr2) != 0)
  320. X                {
  321. X                    pol_s.rfds |= 1 << rfds_pol[i].fd;
  322. X                    hits++;
  323. X                }
  324. X#endif
  325. X            for (i=0; i < wfds_num; i++)
  326. X                if (*(wfds_pol[i].addr) != 0)
  327. X                {
  328. X                    pol_s.wfds |= 1 << wfds_pol[i].fd;
  329. X                    hits++;
  330. X                }
  331. X#ifdef USE_BUF
  332. X                else if (*(wfds_pol[i].addr2) != 0)
  333. X                {
  334. X                    pol_s.wfds |= 1 << wfds_pol[i].fd;
  335. X                    hits++;
  336. X                }
  337. X#endif
  338. X            if (hits != 0)
  339. X            {
  340. X                if (timeout_idx != 0 && lbolt < timeout_ticks)
  341. X                    untimeout(timeout_idx);
  342. X                DB(printf("got result\n"));
  343. X                if (copyout(&pol_s, polfd_p, sizeof(struct polfd)) != 0)
  344. X                {
  345. X                    u.u_error = EFAULT;
  346. X                    u.u_rval1 = -1;
  347. X                }
  348. X                else u.u_rval1 = hits;
  349. X                return u.u_rval1;
  350. X            }
  351. X            spltty();
  352. X        } while (pol_intr != 0);
  353. X        DB(printf("Going to sleep\n"));
  354. X        if (pol_s.timeout == 0 || (
  355. X            pol_s.timeout != -1 && lbolt >= timeout_ticks))
  356. X        {
  357. X                spl0();
  358. X                if (timeout_idx != 0 && lbolt < timeout_ticks)
  359. X                    untimeout(timeout_idx);
  360. X                u.u_rval1 = 0;
  361. X                return 0;
  362. X        }
  363. X        sleep((caddr_t *)&pol_uses, PSLEP);
  364. X        spl0();
  365. X    }
  366. X}
  367. X
  368. X/*---------------------------------------------------------------------------
  369. X**
  370. X**    wakepl
  371. X**
  372. X**--------------------------------------------------------------------------*/
  373. Xwakepl(caddr)
  374. Xcaddr_t caddr;
  375. X{
  376. X    if (pol_uses > 0)
  377. X    {
  378. X        DB(printf("wakepl called\n"));
  379. X        wakeup((caddr_t)&pol_uses);
  380. X    }
  381. X    return wakeup(caddr);
  382. X}
  383. X
  384. X/*---------------------------------------------------------------------------
  385. X**
  386. X**    poltimeo
  387. X**
  388. X**--------------------------------------------------------------------------*/
  389. Xpoltimeo()
  390. X{
  391. X    if (pol_uses > 0)
  392. X    {
  393. X        DB(printf("timedout wakeup\n"));
  394. X        wakeup((caddr_t)&pol_uses);
  395. X    }
  396. X}
  397. X    
  398. X/*---------------------------------------------------------------------------
  399. X**
  400. X**    ttinpol
  401. X**
  402. X**--------------------------------------------------------------------------*/
  403. Xttinpol(tty_p, flag)
  404. Xstruct tty *tty_p;
  405. Xint flag;
  406. X{
  407. X    if (pol_uses > 0)
  408. X    {
  409. X        DB(printf("ttinpol called\n"));
  410. X        wakeup((caddr_t)&pol_uses);
  411. X    }
  412. X    pol_intr = 1;
  413. X    return ttin(tty_p, flag);
  414. X}
  415. X
  416. X/*---------------------------------------------------------------------------
  417. X**
  418. X**    ttwrpol
  419. X**
  420. X**--------------------------------------------------------------------------*/
  421. Xttwrpol(tty_p)
  422. Xstruct tty *tty_p;
  423. X{
  424. X    if (pol_uses > 0)
  425. X    {
  426. X        DB(printf("ttwrpol called\n"));
  427. X        wakeup((caddr_t)&pol_uses);
  428. X    }
  429. X    pol_intr = 1;
  430. X    return ttwrite(tty_p);
  431. X}
  432. X
  433. X
  434. X/*---------------------------------------------------------------------------
  435. X**
  436. X**    poladdr
  437. X**
  438. X**--------------------------------------------------------------------------*/
  439. Xpoladdr(fmask, fds_pol, num_fds, flag)
  440. Xint fmask, *num_fds, flag;
  441. Xstruct pol_table *fds_pol;
  442. X{
  443. X    int i, j;
  444. X    struct inode *fd_inode;
  445. X    struct tty *major_tty;
  446. X    struct Link *fd_link;
  447. X    
  448. X    for (i = 0; i != -1 && i <= NPOLFILE; i++)
  449. X    {
  450. X        if ( (fmask & (1 << i)) == 0)
  451. X            continue;
  452. X        DB(printf("Found fd\n"));
  453. X        fds_pol->fd = i;
  454. X         fd_inode = (u.u_ofile[i])->f_up.f_uinode;
  455. X        if ( (fd_inode->i_ftype & IFMT) == IFCHR)
  456. X        {
  457. X            DB(printf("char device\n"));
  458. X            DB(printf("major %d\n",major(fd_inode->i_rdev)));
  459. X            DB(printf("minor %d\n",minor(fd_inode->i_rdev)));
  460. X            if ( (major_tty=cdevsw[major(fd_inode->i_rdev)].d_ttys) == 0)
  461. X                if (cdevsw[major(fd_inode->i_rdev)].d_open == sxtopen)
  462. X                { /* sxt */
  463. X                    fds_pol->addr = (caddr_t)sxt_buf +
  464. X                            (long)LINK(minor(fd_inode->i_rdev)) *
  465. X                            (long)(sizeof(struct Link) +
  466. X                             sizeof(struct Channel) * (MAXPCHAN-1)) +
  467. X                            (long)&((struct Link *)0)->chans[0] +
  468. X                                CHAN(minor(fd_inode->i_rdev)) *
  469. X                                sizeof(struct Channel) +
  470. X                                (long)&((struct Channel *)0)->tty;
  471. X                }
  472. X                else if (cdevsw[major(fd_inode->i_rdev)].d_open == ptmopen)
  473. X                {            /* master ptys */
  474. X                        fds_pol->addr = (caddr_t)pts_tty;
  475. X                        flag = !flag;                /* swap reads and writes*/
  476. X                }                    
  477. X                else
  478. X                {
  479. X                    u.u_error = ENXIO;        
  480. X                    u.u_rval1 = -1;
  481. X                    return -1;
  482. X                }
  483. X            else
  484. X                fds_pol->addr =
  485. X                    (caddr_t)major_tty +
  486. X                    minor(fd_inode->i_rdev) * sizeof(struct tty);
  487. X        }
  488. X        else if ( (fd_inode->i_ftype & IFMT) == IFIFO)
  489. X                    fds_pol->addr = (caddr_t)&fd_inode->i_size;
  490. X        else
  491. X        {
  492. X            u.u_error = EBADF;        
  493. X            u.u_rval1 = -1;
  494. X            return -1;
  495. X        }
  496. X        DB(printf("Got address\n"));
  497. X        if ( flag == POL_READ_FLAG)
  498. X        {
  499. X#ifdef USE_BUF
  500. X            fds_pol->addr2 = fds_pol->addr +
  501. X                            (long)&((struct tty *)0)->t_rbuf.c_count;
  502. X#endif
  503. X            fds_pol->addr += (long)&((struct tty *)0)->t_rawq.c_cc;
  504. X        }
  505. X        else
  506. X        {
  507. X#ifdef USE_BUF
  508. X            fds_pol->addr2 = fds_pol->addr +
  509. X                            (long)&((struct tty *)0)->t_tbuf.c_count;
  510. X#endif
  511. X            fds_pol->addr += (long)&((struct tty *)0)->t_outq.c_cc;
  512. X        }
  513. X        (*num_fds)++;
  514. X        fds_pol++;
  515. X        if ( (fmask &= ~(1 <<i)) == 0)
  516. X            break;
  517. X    }
  518. X    return 0;
  519. X}
  520. SHAR_EOF
  521. $TOUCH -am 0827154392 pol.c &&
  522. chmod 0644 pol.c ||
  523. echo "restore of pol.c failed"
  524. set `wc -c pol.c`;Wc_c=$1
  525. if test "$Wc_c" != "8399"; then
  526.     echo original size 8399, current size $Wc_c
  527. fi
  528. # ============= pol.h ==============
  529. echo "x - extracting pol.h (Text)"
  530. sed 's/^X//' << 'SHAR_EOF' > pol.h &&
  531. X/* sys/pol.h */
  532. X
  533. X#define POL_FDS    0x1234dcba
  534. X
  535. Xstruct polfd {
  536. X    int rfds, wfds;                /* file desc to poll */
  537. X    int timeout;            /* events of interest on fd */
  538. X};
  539. X
  540. X#define NPOLFILE    20
  541. X
  542. X/*#define DONT_HAVE_POLL  /* uncomment this out if you don't have poll.h */
  543. X
  544. X#ifdef DONT_HAVE_POLL
  545. X/*
  546. X * Structure of file descriptor/event pairs supplied in
  547. X * the poll arrays.
  548. X */
  549. Xstruct pollfd {
  550. X    int fd;                    /* file desc to poll */
  551. X    short events;            /* events of interest on fd */
  552. X    short revents;            /* events that occurred on fd */
  553. X};
  554. X#endif
  555. X
  556. SHAR_EOF
  557. $TOUCH -am 0827163292 pol.h &&
  558. chmod 0644 pol.h ||
  559. echo "restore of pol.h failed"
  560. set `wc -c pol.h`;Wc_c=$1
  561. if test "$Wc_c" != "521"; then
  562.     echo original size 521, current size $Wc_c
  563. fi
  564. # ============= pol.lib.c ==============
  565. echo "x - extracting pol.lib.c (Text)"
  566. sed 's/^X//' << 'SHAR_EOF' > pol.lib.c &&
  567. X/*
  568. X    pol.c    - poll/select() C interface
  569. X
  570. X    Bruce Momjian (root%candle.uucp@bts.com
  571. X
  572. X*/
  573. X
  574. X/* tabs = 4 */
  575. X/* Include files */
  576. X#include <fcntl.h>
  577. X
  578. X#include <sys/pol.h>
  579. X#include <sys/selec.h>
  580. X#include <poll.h>
  581. X
  582. Xstatic int pol_fd = -1;
  583. X
  584. X
  585. X/*---------------------------------------------------------------------------
  586. X**
  587. X**    pol()
  588. X**
  589. X**--------------------------------------------------------------------------*/
  590. Xpol(fds, nfds, timeout)
  591. Xstruct pollfd *fds;
  592. Xunsigned long nfds;
  593. Xint timeout;
  594. X{
  595. X    struct polfd    pl;
  596. X    int i, ret;
  597. X    
  598. X    if ( pol_fd == -1 && (pol_fd=open("/dev/pol",O_RDONLY)) == -1)
  599. X        return -1;
  600. X
  601. X    pl.rfds = pl.wfds = 0;
  602. X    pl.timeout = timeout;
  603. X
  604. X    for (i = 0; i < nfds; i++)
  605. X    {
  606. X        if ((fds[i].events & POLLIN) != 0)
  607. X            pl.rfds |= (1 << fds[i].fd);
  608. X        if ((fds[i].events & POLLOUT) != 0)
  609. X            pl.wfds |= (1 << fds[i].fd);
  610. X    }
  611. X    
  612. X    if ((ret=ioctl(pol_fd, POL_FDS, &pl)) == -1)
  613. X         return -1;
  614. X
  615. X    if (ret == 0 || ret == -1)
  616. X        pl.rfds = pl.wfds = 0;
  617. X    
  618. X    for (i=0; i < nfds; i++)
  619. X    {
  620. X        fds[i].revents = 0;
  621. X        if ( (pl.rfds & (1 << fds[i].fd)) != 0)
  622. X            fds[i].revents |= POLLIN;
  623. X        if ( (pl.wfds & (1 << fds[i].fd)) != 0)
  624. X            fds[i].revents |= POLLOUT;
  625. X    }
  626. X    return ret;
  627. X}
  628. X    
  629. X/*---------------------------------------------------------------------------
  630. X**
  631. X**    selec()
  632. X**
  633. X**--------------------------------------------------------------------------*/
  634. Xselec(nfds, rfds, wfds, expfds, timeout)
  635. Xunsigned long nfds, *rfds, *wfds, *expfds; /* exceptions not implemented */
  636. Xstruct timeval *timeout;
  637. X{
  638. X    struct polfd    pl;
  639. X    int i, ret;
  640. X    
  641. X    if ( pol_fd == -1 && (pol_fd=open("/dev/pol",O_RDONLY)) == -1)
  642. X        return -1;
  643. X
  644. X    pl.rfds = pl.wfds = 0;
  645. X    if (timeout == 0L)
  646. X        pl.timeout = -1;
  647. X    else
  648. X        pl.timeout = timeout->tv_sec * 1000 + timeout->tv_usec / 1000;
  649. X
  650. X    for (i = 0; i < nfds; i++)
  651. X    {
  652. X        if ( (*rfds & (1 << i)) != 0)
  653. X            pl.rfds |= 1 << i;
  654. X        if ( (*wfds & (1 << i)) != 0)
  655. X            pl.wfds |= 1 << i;
  656. X    }
  657. X    
  658. X    if ((ret=ioctl(pol_fd, POL_FDS, &pl)) == -1)
  659. X         return -1;
  660. X
  661. X    if (ret == 0 || ret == -1)
  662. X        pl.rfds = pl.wfds = 0;
  663. X    *rfds = *wfds = *expfds = 0;
  664. X
  665. X    for (i=0; i < nfds; i++)
  666. X    {
  667. X        if ( (pl.rfds & (1 << i)) != 0)
  668. X            (*rfds) |= (1 << i);
  669. X        if ( (pl.wfds & (1 << i)) != 0)
  670. X            (*wfds) |= (1 << i);
  671. X    }
  672. X    return ret;
  673. X}
  674. X    
  675. X/*---------------------------------------------------------------------------
  676. X**
  677. X**    polclose()
  678. X**
  679. X**--------------------------------------------------------------------------*/
  680. Xpolclose()
  681. X{
  682. X    if (pol_fd != -1)
  683. X        return close(pol_fd);
  684. X    return 0;
  685. X}
  686. X/*---------------------------------------------------------------------------
  687. X**
  688. X**    selecclose()
  689. X**
  690. X**--------------------------------------------------------------------------*/
  691. Xselecclose()
  692. X{
  693. X    return polclose();
  694. X}
  695. SHAR_EOF
  696. $TOUCH -am 0827154392 pol.lib.c &&
  697. chmod 0644 pol.lib.c ||
  698. echo "restore of pol.lib.c failed"
  699. set `wc -c pol.lib.c`;Wc_c=$1
  700. if test "$Wc_c" != "2624"; then
  701.     echo original size 2624, current size $Wc_c
  702. fi
  703. # ============= poltest.c ==============
  704. echo "x - extracting poltest.c (Text)"
  705. sed 's/^X//' << 'SHAR_EOF' > poltest.c &&
  706. X#include <stdio.h>
  707. X#include <fcntl.h>
  708. X#include <poll.h>
  709. X
  710. Xmain()
  711. X{
  712. X    int fd;
  713. X    struct pollfd pl;
  714. X    int x;
  715. X    
  716. X
  717. X    printf("fd = %d\n",fd);
  718. X    pl.fd = 0;
  719. X    pl.events = POLLIN;
  720. X    printf("Begin ioctl, return\n");
  721. X    if ((x=pol(&pl, 1, 5000)) == -1)
  722. X         halt("PERROR : ioctl fail\n");
  723. X    printf("End ioctl\n");
  724. X    close(fd);
  725. X}     
  726. SHAR_EOF
  727. $TOUCH -am 0827160292 poltest.c &&
  728. chmod 0644 poltest.c ||
  729. echo "restore of poltest.c failed"
  730. set `wc -c poltest.c`;Wc_c=$1
  731. if test "$Wc_c" != "305"; then
  732.     echo original size 305, current size $Wc_c
  733. fi
  734. # ============= selec.h ==============
  735. echo "x - extracting selec.h (Text)"
  736. sed 's/^X//' << 'SHAR_EOF' > selec.h &&
  737. X/* sys/selec.h */
  738. Xstruct timeval {
  739. X        long tv_usec, tv_sec;
  740. X};
  741. SHAR_EOF
  742. $TOUCH -am 0827152292 selec.h &&
  743. chmod 0644 selec.h ||
  744. echo "restore of selec.h failed"
  745. set `wc -c selec.h`;Wc_c=$1
  746. if test "$Wc_c" != "62"; then
  747.     echo original size 62, current size $Wc_c
  748. fi
  749. exit 0
  750.  
  751.  
  752.  
  753.  
  754.  
  755. -- 
  756. Bruce Momjian                      |  830 Blythe Avenue
  757. root%candle.uucp@bts.com           |  Drexel Hill, Pennsylvania 19026 
  758.                                    |  (215) 353-9879(w)  853-3000(h)
  759.  
  760. -- 
  761. Bruce Momjian                      |  830 Blythe Avenue
  762. root%candle.uucp@bts.com           |  Drexel Hill, Pennsylvania 19026 
  763.                                    |  (215) 353-9879(w)  853-3000(h)
  764.