home *** CD-ROM | disk | FTP | other *** search
/ Programmer 7500 / MAX_PROGRAMMERS.iso / CLIPPER / MISC / EMXLIB8F.ZIP / EMX / LIB / IO / CHSIZE.C < prev    next >
Encoding:
C/C++ Source or Header  |  1993-01-02  |  1.0 KB  |  47 lines

  1. /* chsize.c (emx+gcc) -- Copyright (c) 1992-1993 by Eberhard Mattes */
  2.  
  3. #include <sys/emx.h>
  4. #include <io.h>
  5. #include <strings.h>
  6. #include <errno.h>
  7.  
  8. int chsize (int handle, long length)
  9. {
  10.   long n;
  11.   int i, j;
  12.   char zeros[4096];
  13.  
  14.   if (handle < 0 || handle >= _nfiles || (_files[handle] & F_DEV))
  15.     {
  16.       errno = EBADF;
  17.       return (-1);
  18.     }
  19.   n = (long)__lseek (handle, 0L, SEEK_END);
  20.   if (n == -1L)
  21.     return (-1);
  22.   if (__chsize (handle, length) != 0)
  23.     return (-1);
  24.   if (n < length)
  25.     {
  26.       bzero (zeros, sizeof (zeros));
  27.       if (__lseek (handle, n, SEEK_SET) == -1)
  28.         return (-1);
  29.       while (n < length)
  30.         {
  31.           i = length - n;
  32.           if (i > sizeof (zeros))
  33.             i = sizeof (zeros);
  34.           j = __write (handle, zeros, i);
  35.           if (j == -1)
  36.             return (-1);
  37.           if (j == 0)
  38.             {
  39.               errno = ENOSPC;
  40.               return (-1);
  41.             }
  42.           n += j;
  43.         }
  44.     }
  45.   return (0);
  46. }
  47.