/*-----------------------------------------------------------------------*
 * filename - writea.cas
 *
 * function(s)
 *    _write - write to a file (untranslated)
 *-----------------------------------------------------------------------*/

/*
 *      C/C++ Run Time Library - Version 5.0
 *
 *      Copyright (c) 1987, 1992 by Borland International
 *      All Rights Reserved.
 *
 */


#pragma inline
#include <asmrules.h>
#include <io.h>
#include <fcntl.h>
#include <_io.h>
#include <RtlData.h>

/*-----------------------------------------------------------------------*

Name            _write - writes to a file

Usage           int _write(int handle, const void *buf, unsigned nbyte);

Prototype in    io.h

Description     Both write and _write are functions that write a
                buffer of data to the file or device named by the given
                handle.

                handle is a file handle obtained from a creat, open, dup,
                dup2, or fcntl call.

                These functions attempt to write nbyte bytes from the buffer
                pointed to by buf to the file associated with handle. Except
                when write is used to write to a text file, the number of
                bytes written to the file will be no more than the number
                requested.

                On text files, when write sees a linefeed (LF) character, it
                outputs a CR-LF pair. _write does no such translation, since
                all of its files are binary files.

                If the number of bytes actually written is less than that
                requested, the condition should be considered an error and
                probably indicates a full disk.

                For disk or diskette files, writing always proceeds from the
                current file pointer (see lseek). For devices, bytes are directly
                sent to the device.

                For files opened with the O_APPEND option, the file pointer is
                positioned to EOF by write (but not by _write) before writing
                the data.

Return value    The number of bytes written are returned by both
                functions. A write to a text file does not count generated
                carriage returns. In case of error, each function returns -1
                and sets the global variable errno to one of the following:

                    EACCES  Permission denied
                    EBADF   Bad file number


*------------------------------------------------------------------------*/

#ifdef _Windows
extern void (*__WriteBufFPtr)(char *Buffer, unsigned Count);
#endif

int _CType _FARFUNC _write(int fd, const void *buf, unsigned len)
{
    _QRTLDataBlock;

    if (_QRTLInstanceData(_openfd)[fd] & O_RDONLY)
        return __IOerror(e_accessDenied);

#ifdef _Windows
    if (__WriteBufFPtr)
        if (isatty(fd))
            {
            __WriteBufFPtr((char *)buf, len);
            return len;
            }
#endif
    pushDS_
    asm mov ah,40h
    asm mov bx,fd
    asm mov cx,len
    asm LDS_    dx, buf
    asm int 21h
        popDS_
    asm jc  _writeFailed
    asm push    ax

    _QRTLInstanceData(_openfd) [fd] |= O_CHANGED;

    asm pop ax
    return  _AX;

_writeFailed:
    return __IOerror (_AX);
}
