/
write.c
76 lines (69 loc) · 1.47 KB
/
write.c
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
/*
* _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 <errno.h>
#include <fcntl.h>
#include <unistd.h>
#include <signal.h>
#include <sys/ioctl.h>
#include <sys/stat.h>
#include <sys/types.h>
#include <mint/mintbind.h>
#include "lib.h"
ssize_t __write (int fd, const void *buf, size_t size)
{
unsigned char c, *foo;
unsigned flags;
long r;
struct xattr statbuf;
_DOSTIME timebuf;
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) {
__set_errno (-r);
return -1;
}
if (size && r == 0) {
if (__mint >= 9)
{
if ((r = Fcntl (fd, &statbuf, FSTAT)) < 0)
{
__set_errno (-r);
return -1;
}
if (((mode_t) statbuf.st_mode & S_IFMT) == S_IFREG)
{
__set_errno (ENOSPC);
return -1;
}
}
else if (Fdatime (&timebuf, fd, 0) == 0
&& Fseek (0L, fd, SEEK_CUR) >= 0)
{
__set_errno (ENOSPC);
return -1;
}
}
return r;
}
weak_alias (__write, write)