home *** CD-ROM | disk | FTP | other *** search
/ Usenet 1994 January / usenetsourcesnewsgroupsinfomagicjanuary1994.iso / sources / unix / volume25 / pty4 / part02 / ptymisc.c < prev    next >
Encoding:
C/C++ Source or Header  |  1992-02-18  |  2.0 KB  |  118 lines

  1. #include <sys/types.h>
  2. #include <sys/file.h>
  3. #include <sys/time.h>
  4. #include <errno.h>
  5. extern int errno;
  6. #include <fcntl.h>
  7. #include "ptymisc.h"
  8. #include "config/fdsettrouble.h"
  9.  
  10. #ifdef DESPERATE_FD_SET
  11. #undef fd_set
  12. #define fd_set long
  13. #endif
  14.  
  15. extern long time();
  16. long now()
  17. {
  18.  return time((long *) 0);
  19. }
  20.  
  21. int gaargh(n) int n;
  22. {
  23.  struct timeval t;
  24.  
  25.  t.tv_sec = 0;
  26.  t.tv_usec = n;
  27.  return select(0,(fd_set *) 0,(fd_set *) 0,(fd_set *) 0,&t);
  28. }
  29.  
  30. int setnonblock(fd)
  31. int fd;
  32. {
  33.  return fcntl(fd,F_SETFL,fcntl(fd,F_GETFL,0) | FNDELAY); /* XXX */
  34. }
  35.  
  36. int unsetnonblock(fd)
  37. int fd;
  38. {
  39.  return fcntl(fd,F_SETFL,fcntl(fd,F_GETFL,0) & ~FNDELAY); /*XXX*/
  40. }
  41.  
  42. int forceopen(fd)
  43. int fd;
  44. {
  45.  int fdnull;
  46.  if (fcntl(fd,F_GETFL,0) != -1) /* it's open already */
  47.    return 0;
  48.  fdnull = open("/dev/null",O_RDWR,0);
  49.  if (fdnull == -1)
  50.    fdnull = open("/",O_RDONLY,0);
  51.  if (fdnull == -1)
  52.    return -1;
  53.  if (fdnull != fd)
  54.   {
  55.    if (dup2(fdnull,fd) == -1)
  56.     {
  57.      close(fdnull);
  58.      return -1;
  59.     }
  60.    close(fdnull);
  61.   }
  62.  return 0;
  63. }
  64.  
  65. int respeq(resp,str)
  66. char *resp;
  67. char *str;
  68. {
  69.  return scan_strncmp(resp,str,6) == 6;
  70. }
  71.  
  72. int bread(fd,buf,n)
  73. int fd; char *buf; int n;
  74. {
  75.  int r; int tot; tot = 0;
  76.  while (n)
  77.   {
  78.    r = read(fd,buf,n);
  79.    if (r == 0) break;
  80.    if (r == -1)
  81.      if ((errno == EINTR) || (errno == EWOULDBLOCK)) continue;
  82.      else return -1; /* XXX: losing data! */
  83.    buf += r; tot += r; n -= r;
  84.   }
  85.  return tot;
  86. }
  87.  
  88. int bwrite(fd,buf,n)
  89. int fd; char *buf; int n;
  90. {
  91.  int w; int tot; tot = 0;
  92.  while (n)
  93.   {
  94.    w = write(fd,buf,n);
  95.    if (w == 0) break; /* XXX: can happen under System V [sigh] */
  96.    if (w == -1)
  97.      if ((errno == EINTR) || (errno == EWOULDBLOCK)) continue;
  98.      else return -1; /* XXX: losing data! */
  99.    buf += w; tot += w; n -= w;
  100.   }
  101.  return tot;
  102. }
  103.  
  104. int lflock(fd)
  105. int fd;
  106. {
  107.  /* must depend only on write access */
  108.  /* does not need to disappear automatically upon close() */
  109.  /* but should disappear automatically upon crash of any sort */
  110.  return flock(fd,LOCK_EX);
  111. }
  112.  
  113. int lfunlock(fd)
  114. int fd;
  115. {
  116.  return flock(fd,LOCK_UN);
  117. }
  118.