/*
	sys_filesize(fname)

	Returns the size (in bytes) of the file named "fname".
	Returns (-1) on error (e.g., if the file does not exist).
*/
#include <ncbi.h>
#include <gishlib.h>

long
sys_filesize(fname)
	CharPtr fname;
{
	struct stat	sbuf;

	if (stat(fname, &sbuf) == 0)
		return (long)sbuf.st_size;
	return -1;
}

int
sys_fpisfile(fp)
	FILE	*fp;
{
	struct stat	sbuf;

	if (fp == NULL)
		return 0;
	if (fstat(fileno(fp), &sbuf) == 0)
		return (sbuf.st_mode & S_IFREG);
	return 0;
}

int
sys_lockfile(fd, cmd, type, offset, whence, len)
	int	fd;
	int	cmd, type;
	int	whence;
	off_t	offset, len;
{
	struct flock	lock;
	int	rc;

	lock.l_type = type;
	lock.l_start = offset;
	lock.l_whence = whence;
	lock.l_len = len;

	while((rc = fcntl(fd, cmd, &lock)) == -1)
		switch (errno) {
		case EINTR:
			continue;
		default:
			break;
		}
	return rc;
}
