/*
 * _write: like write, but takes a long instead of an int. Written by
 * Eric R. Smith and placed in the public domain.
 */

/* BUG: under TOS, CRMOD doesn't work unless RAW is on or SIGINT is
 * being caught
 */

#include <osbind.h>
#include <fcntl.h>
#include <ioctl.h>
#include <errno.h>
#include <unistd.h>
#include <signal.h>
#include "lib.h"

extern __Sigfunc _sig_handler[]; /* TOS fakes for signal handling */

long
_write(fd, buf, size)
	int fd;
	const void *buf;
	long size;
{
	unsigned char c, *foo;
	unsigned flags;
	long r;
	extern int __mint;

	if (__mint == 0 && isatty(fd)) {
		r = __OPEN_INDEX(fd);
		if (r < 0 || r >= __NHANDLES)
			r = __NHANDLES - 1;
		flags = __open_stat[r].flags;
		if ((flags & RAW) || _sig_handler[SIGINT] != SIG_DFL) {
			foo = (unsigned char *) buf;
			r = size;
			while (r-- > 0) {
				c = *foo++;
				if (c == '\n' && (flags & CRMOD))
					_console_write_byte(fd, '\r');
				_console_write_byte(fd, c);
			}
			return size;
		}
	}

	r = Fwrite(fd, size, buf);
	if (r < 0) {
		errno = -r;
		return -1;
	}

	return r;
}

int
write(fd, buf, size)
	int fd;
	const void *buf;
	unsigned size;
{
	return _write(fd, buf, (long)size);
}
