[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]

truncate and ftruncate for the MiNTlib



Here is an implementation of truncate/ftruncate for the MiNTlib. There
is a special case for truncate, if the filesystem does not recognize
FTRUNCATE and the length is zero, Fcreate is used to truncate the
file. This only works for tosfs if the file isn't already open. The
rest is quite straight forward.

Andreas.

P.S.: Sorry for the bogus mail i sent out yesterday. Just one wrong
word... :-(

------------------------- cut here -------------------------
#include <compiler.h>
#include <limits.h>
#include <errno.h>
#include <mintbind.h>
#include <ioctl.h>
#ifdef __TURBOC__
#include <sys\types.h>
#else
#include <sys/types.h>
#endif
#include "lib.h"

extern int __mint;

int
truncate (_filename, length)
     const char *_filename;
     off_t length;
{
  int fh, res;
  char filename[PATH_MAX];

  (void) _unx2dos (_filename, filename);
  fh = Fopen (filename, 2);
  if (fh < 0)
    {
      errno = -fh;
      return -1;
    }
  res = -EINVAL;
  if (__mint > 90)
    res = Fcntl (fh, (long) &length, FTRUNCATE);
  Fclose (fh);
  if (res == -EINVAL && length == 0)
    {
      res = Fcreate (filename, 0);
      if (res >= 0)
	Fclose (res);
    }
  if (res < 0)
    {
      errno = -res;
      return -1;
    }
  return 0;
}

int
ftruncate (fd, length)
     int fd;
     off_t length;
{
  int res;

  if (__mint > 90)
    res = Fcntl (fd, (long) &length, FTRUNCATE);
  else
    res = -EINVAL;

  if (res < 0)
    {
      errno = -res;
      return -1;
    }
  return 0;
}
------------------------- cut here -------------------------