
#include <io.h>
#include <stdio.h>

/*
 * On DOS (and maybe win95), using {l,f}seek to grow a file
 * guarantees that the intervening bits will be not what you expect.
 * 
 * This is written specifically for hash, which calls lseek 3 times, 
 * always using SEEK_SET
 */

long
wacky_lseek_for_dos (int handle, long offset, int origin)
{
  long end_pos;
  long cur_pos;
  static char wacky_zeroes[512];

  if (origin != SEEK_SET) {
    return -1;
  }

  end_pos = _lseek (handle, 0, SEEK_END);
  /* if we are trying to grow the file... */
  if (offset > end_pos) {
    int written = 0;

    cur_pos = end_pos;
    while (cur_pos  < offset) {
      written = _write (handle,
			&wacky_zeroes,
			(offset - cur_pos) < 512 ? (offset - cur_pos) : 512);

      if (written == -1) {
	return -1;
      } else { 
	cur_pos += written;
      }
    }
    return (offset);
  } else {
    return (_lseek (handle, offset, SEEK_SET));
  }
}

