home *** CD-ROM | disk | FTP | other *** search
/ InfoMagic Source Code 1993 July / THE_SOURCE_CODE_CD_ROM.iso / gnu / fax-3.2.1 / lib / libutil / file.c < prev    next >
Encoding:
C/C++ Source or Header  |  1992-07-31  |  1.4 KB  |  72 lines

  1. /*
  2.   This file is part of the NetFax system.
  3.  
  4.   (c) Copyright 1989 by David M. Siegel and Sundar Narasimhan.
  5.       All rights reserved.
  6.  
  7.     This program is free software; you can redistribute it and/or modify
  8.     it under the terms of the GNU General Public License as published by
  9.     the Free Software Foundation.
  10.  
  11.     This program is distributed in the hope that it will be useful, 
  12.     but WITHOUT ANY WARRANTY; without even the implied warranty of 
  13.     MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
  14.     GNU General Public License for more details.
  15.  
  16.     You should have received a copy of the GNU General Public License
  17.     along with this program; if not, write to the Free Software
  18.     Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
  19. */
  20.  
  21. #include <stdio.h>
  22. #include <fcntl.h>
  23. #include <sys/types.h>
  24. #include <sys/ioctl.h>
  25.  
  26. #include "file.h"
  27.  
  28. #define BSD_NONBLOCKING
  29.  
  30. int
  31. file_set_nonblocking(fd)
  32. int fd;
  33. {
  34. #ifdef BSD_NONBLOCKING
  35.     int on = 1;
  36.  
  37.     if (ioctl(fd, FIONBIO, &on) < 0)
  38.       return (-1);
  39. #else
  40.     int flags;
  41.  
  42.     flags = fcntl(fd, F_GETFL, 0);
  43.     flags |= O_NDELAY;
  44.  
  45.     if (fcntl(fd, F_SETFL, flags) < 0)
  46.       return (-1);
  47. #endif
  48.  
  49.     return (0);
  50. }
  51.  
  52. int
  53. file_read(fd, buf, buflen)
  54. int fd;
  55. char *buf;
  56. int buflen;
  57. {
  58.     int count = 0;
  59.     int rc;
  60.  
  61.     while (count < buflen) {
  62.     rc = read(fd, buf, buflen-count);
  63.     if (rc > 0) {
  64.         count += rc;
  65.         buf += rc;
  66.     } else
  67.       return (rc);
  68.     }
  69.  
  70.     return (count);
  71. }
  72.