home *** CD-ROM | disk | FTP | other *** search
/ The C Users' Group Library 1994 August / wc-cdrom-cusersgrouplibrary-1994-08.iso / listings / v_10_02 / 1002111a < prev    next >
Text File  |  1991-12-08  |  3KB  |  136 lines

  1.    #include <stdio.h>
  2.    #define SIZE_BUFFER 1024
  3.    #include <sys\types.h>
  4.    #include <sys\stat.h> 
  5. #ifdef BSD
  6.    #include <sys\file.h>  /* This header name varies */
  7. #else 
  8.    #include <fcntl.h>        
  9. #endif
  10.    static int copy_files();
  11.  
  12.    truncate_file(path, length)
  13.    /* Truncates a file to the length specified */
  14.    char *path;
  15.    int length;
  16.        {
  17.        int input_file, temp_file;
  18.        char temp_file_name[L_tmpnam];
  19.        struct stat file_status;
  20.        int ret;
  21.  
  22.        ret = 0;
  23.        if (length < 0)
  24.            {
  25.            ret = -10;
  26.            goto end;
  27.            }
  28.  
  29.        /* If 0 length, then just open/close it */
  30.        if (length == 0)
  31.            {
  32.            input_file = open(path, O_WRONLY | O_TRUNC, 0666);
  33.            if (input_file < 0)
  34.                {
  35.                ret = -1;
  36.                goto end;
  37.                }
  38.            close (input_file);
  39.            goto end;
  40.            }
  41.  
  42.        input_file = open(path, O_RDONLY, 0);
  43.        if (input_file < 0)
  44.            {
  45.            ret = -1;
  46.            goto end;
  47.            }
  48.  
  49.        /* Check the file length - no sense copying if shorter 
  50. already */
  51.        fstat(input_file, &file_status);
  52.        if (length > file_status.st_size)
  53.            {
  54.            close (input_file);
  55.            ret = -3;
  56.            goto end;
  57.            }
  58.  
  59.        /* Open a temporary file to hold the contents to be copied 
  60. back */
  61.        tmpnam(temp_file_name);
  62.        temp_file = open(temp_file_name, O_RDWR | O_CREAT, 0666);
  63.        if (temp_file < 0)
  64.            {
  65.            ret = -2;
  66.            goto end;
  67.            }
  68.  
  69.        /* Make a copy of the file */
  70.        ret = copy_files(input_file, temp_file, length);
  71.        if (ret == 0)
  72.            {
  73.            lseek(temp_file,0L,0);
  74.            close(input_file);
  75.            input_file = open(path, O_WRONLY | O_TRUNC, 0666);
  76.            if (input_file < 0)
  77.                ret = -12;
  78.            else
  79.               { 
  80.               ret = copy_files(temp_file, input_file, length);
  81.               close(input_file);  
  82.               } 
  83.           }
  84.  
  85.      close (temp_file);  
  86.      unlink(temp_file_name);    
  87.  
  88. end:
  89.      return ret;
  90.      }
  91.    
  92.  
  93. static int copy_files(file_in, file_out, length)
  94. /* Copies from file in to file out */
  95. int file_in;
  96. int file_out;
  97. int length;
  98.        {
  99.        int ret;
  100.        char buffer[SIZE_BUFFER];
  101.        int read_length;
  102.        int write_length;
  103.        int length_to_read;
  104.        int length_to_write;        
  105.        ret = 0;    
  106.        while (length > 0)
  107.            {
  108.            if (length > SIZE_BUFFER)
  109.                length_to_read = SIZE_BUFFER;
  110.            else
  111.                length_to_read = length;
  112.            length -= length_to_read;
  113.            read_length = read(file_in, buffer, length_to_read);
  114.            if (read_length != length_to_read)
  115.                {
  116.                ret = -4;
  117.                goto end;
  118.                }
  119.            length_to_write = read_length;
  120.            write_length = write(file_out, buffer, 
  121. length_to_write);
  122.            if (write_length != length_to_write)
  123.               {
  124.               ret = -5;
  125.               goto end;
  126.               }                    
  127.           }        
  128. end:
  129.     return ret;
  130.     }    
  131.  
  132. main()
  133.     {
  134.     printf("\n Ret is %d", truncate_file("a:temp", 1));
  135.     }
  136.