kd_generic.c
/* this not only copies files, but leaves holes if there's no data */
this not only copies files, but leaves holes if there's no data
this not only copies files but leaves holes if there ' s no data
int copy_file(const char *src, const char *dst)
{
   int fsrc, fdst;
   unsigned char buf[1024];
   int cnt, total;
   int write_last = 0;
   total = 0;
   if ((fsrc = open(src, O_RDONLY, 0)) < 0)
      return(-1);
   if ((fdst = open(dst, O_WRONLY|O_CREAT|O_TRUNC, 0644)) < 0) {
      close(fsrc);
      return(-1);
   }
   while (1) {
      cnt = read(fsrc, (void *) buf, sizeof(buf));
      if (cnt < 0) {
	 close(fsrc);
	 close(fdst);
	 return(-1);
      }
      if (cnt == 0)
	 break;
      total += cnt;
      if (memcmp(buf, zeros, cnt) == 0) {
	 write_last = 1;
	 if (lseek(fdst, cnt, SEEK_CUR) < 0) {
	    close(fsrc);
	    close(fdst);
	    return(-1);
	 }
      } else {
	 int wptr = 0;
	 while (cnt > 0) {
	    write_last = 0;
	    if ((wptr = write(fdst, (void *) (buf+wptr), cnt)) < 0) {
	       close(fsrc);
	       close(fdst);
	       return(-1);
	    }
	    cnt -= wptr;
	 }
      }
   }
   if (write_last) {
      if (lseek(fdst, -1, SEEK_CUR) < 0
          || write(fdst, zeros, 1) != 1) {
         log_error("copy_file", strerror(errno));
         close(fsrc);
         close(fdst);
         return(-1);
      }
   }
   close(fsrc);
   close(fdst);
   return(total);
}
int copy_file ( const char * src , const char * dst ) { int fsrc , fdst ; unsigned char buf [ 1024 ] ; int cnt , total ; int write_last = 0 ; total = 0 ; if ( ( fsrc = open ( src , O_RDONLY , 0 ) ) < 0 ) return ( -1 ) ; if ( ( fdst = open ( dst , O_WRONLY | O_CREAT | O_TRUNC , 0644 ) ) < 0 ) { close ( fsrc ) ; return ( -1 ) ; } while ( 1 ) { cnt = read ( fsrc , ( void * ) buf , sizeof ( buf ) ) ; if ( cnt < 0 ) { close ( fsrc ) ; close ( fdst ) ; return ( -1 ) ; } if ( cnt == 0 ) break ; total += cnt ; if ( memcmp ( buf , zeros , cnt ) == 0 ) { write_last = 1 ; if ( lseek ( fdst , cnt , SEEK_CUR ) < 0 ) { close ( fsrc ) ; close ( fdst ) ; return ( -1 ) ; } } else { int wptr = 0 ; while ( cnt > 0 ) { write_last = 0 ; if ( ( wptr = write ( fdst , ( void * ) ( buf + wptr ) , cnt ) ) < 0 ) { close ( fsrc ) ; close ( fdst ) ; return ( -1 ) ; } cnt -= wptr ; } } } if ( write_last ) { if ( lseek ( fdst , -1 , SEEK_CUR ) < 0 || write ( fdst , zeros , 1 ) != 1 ) { log_error ( " " , strerror ( errno ) ) ; close ( fsrc ) ; close ( fdst ) ; return ( -1 ) ; } } close ( fsrc ) ; close ( fdst ) ; return ( total ) ; }
this not only copies files, but leaves holes if there's no data
this not only copies files but leaves holes if there ' s no data
/* Need to write into the last location, at least on Solaris */