Category : OS/2 Files
Archive   : EMXLIB8F.ZIP
Filename : WRITE.C
#include
#include
#include
#include
#include
#define CTRL_Z 0x1a
#define BEGIN do {
#define END } while (0)
#define WRTBUF \
BEGIN n = __write (handle, tmp, buf_cnt); \
if (n < 0) goto error; \
out_cnt += n; \
if (n != buf_cnt) {errno = ENOSPC; return (-1);} \
buf_cnt = 0; \
END
int write (int handle, const void *buf, size_t nbyte)
{
int out_cnt, lf_cnt, i, n, buf_cnt;
const char *src, *p;
char tmp[1024];
if (handle < 0 || handle >= _nfiles)
{
errno = EBADF; return (-1);
}
if (_files[handle] & O_APPEND)
__lseek (handle, 0L, SEEK_END);
if (nbyte == 0) /* Avoid truncation of file */
return (0);
src = buf;
if (_files[handle] & O_TEXT)
{
out_cnt = lf_cnt = 0;
buf_cnt = 0;
p = memchr (src, '\n', nbyte);
if (p == NULL)
goto write_bin;
for (i = 0; i < nbyte; ++i)
{
if (src[i] == '\n')
{
if (buf_cnt >= sizeof (tmp)) WRTBUF;
tmp[buf_cnt++] = '\r';
++lf_cnt;
}
if (buf_cnt >= sizeof (tmp)) WRTBUF;
tmp[buf_cnt++] = src[i];
}
if (buf_cnt != 0) WRTBUF;
return (out_cnt - lf_cnt);
}
write_bin:
n = __write (handle, src, nbyte);
if (n < 0) goto error;
if (n == 0 && !((_files[handle] & F_DEV) && *src == CTRL_Z))
{
errno = ENOSPC;
return (-1);
}
return (n);
error:
return (-1);
}
/* sys/write.c (emx+gcc) -- Copyright (c) 1992-1993 by Eberhard Mattes */
#include
#include
#include "syscalls.h"
int __write (int handle, const void *buf, size_t nbyte)
{
ULONG rc;
ULONG n;
rc = DosWrite (handle, buf, nbyte, &n);
if (rc == 0)
return (n);
else
{
_sys_set_errno (rc);
return (-1);
}
}
Very nice! Thank you for this wonderful archive. I wonder why I found it only now. Long live the BBS file archives!
This is so awesome! 😀 I’d be cool if you could download an entire archive of this at once, though.
But one thing that puzzles me is the “mtswslnkmcjklsdlsbdmMICROSOFT” string. There is an article about it here. It is definitely worth a read: http://www.os2museum.com/wp/mtswslnk/