home *** CD-ROM | disk | FTP | other *** search
/ The C Users' Group Library 1994 August / wc-cdrom-cusersgrouplibrary-1994-08.iso / listings / v_10_11 / 1011088a < prev    next >
Text File  |  1992-10-05  |  1KB  |  47 lines

  1. #define STS_OK      0
  2. #define STS_BAD_LEN -10
  3. #define STS_ERROR   -1
  4.  
  5. extern int
  6. truncate_file (char * path, long len)
  7.    {
  8.      int infile, tmp_file; /* input file */
  9.      int ret = STS_OK;
  10.      char tf_name [L_tmpnam];
  11.  
  12.      do {
  13.           if (len < 0>
  14.                     {
  15.                     ret = STS_BAD_LENGTH;
  16.                     break;
  17.                     }
  18.           if (len == 0)
  19.                     {
  20.                     ret = truncate_to_zero (path);
  21.                     break;
  22.                     }
  23.           if ((infile = open (path, O_RDONLY, 0)) < 0)                  
  24.                     {
  25.                     ret = STS_ERROR;
  26.                     break;
  27.                     }
  28.           if (file_len (infile) <= len)
  29.                     {
  30.                     close (infile);
  31.                     ret = STS_SMALL_FILE;
  32.                     break;
  33.                     }
  34.  
  35.      /* after all error conditions are checked, we finally do 
  36.         some real work here */
  37.  
  38.      tmpnam (tf_name);
  39.      tmp_file = open (tf_name, O_RDWR | 0_CREAT, 0666);
  40.      ret = copy_file (infile, tmp_file, path, len);
  41.      unlink (tf_name);
  42.  
  43.      } while (FALSE);
  44.    return (ret);
  45.    }
  46.  
  47.