forked from HACKERCHANNEL/PSL1GHT
-
Notifications
You must be signed in to change notification settings - Fork 69
Expand file tree
/
Copy pathwrite.c
More file actions
73 lines (62 loc) · 1.81 KB
/
write.c
File metadata and controls
73 lines (62 loc) · 1.81 KB
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
#include <_ansi.h>
#include <_syslist.h>
#include <sys/reent.h>
#include <sys/errno.h>
#include <sys/types.h>
#include <net/socket.h>
#include <sys/lv2errno.h>
#include <netinet/in.h>
#include <string.h>
#include <sys/tty.h>
#include <sys/file.h>
#include "../../configure.h"
#ifdef _STDIO_TO_NET
//optionally initialize directing stdio to ethernet
static int _stdio_sock;
static struct sockaddr_in _stdio_saddr;
__attribute__((constructor(1000)))
void init_stdio_to_net(void)
{
// TODO: can netInitialize be run twice?
// if not - could it be run implicitly by the crt?
//netInitialize();
// Connect the socket, hoping for the best.
// We have little means of notifying the user if it fails.
_stdio_sock = socket(PF_INET, SOCK_STREAM, IPPROTO_TCP);
memset(&_stdio_saddr, 0, sizeof(_stdio_saddr));
_stdio_saddr.sin_len = sizeof(_stdio_saddr);
_stdio_saddr.sin_family = AF_INET;
inet_pton(AF_INET, _STDIO_NET_IP, &_stdio_saddr.sin_addr);
_stdio_saddr.sin_port = htons(_STDIO_NET_PORT);
connect(_stdio_sock,(struct sockaddr*)&_stdio_saddr, \
sizeof(_stdio_saddr));
}
#endif
_ssize_t
_DEFUN(__librt_write_r,(r,fd,ptr,len),
struct _reent *r _AND
int fd _AND
const void *ptr _AND
size_t len)
{
int ret = 0;
if(fd&SOCKET_FD_MASK)
return send(fd,ptr,len,0);
if(fd==STDOUT_FILENO || fd==STDERR_FILENO) {
u32 done = 0;
#ifndef _STDIO_TO_NET
ret = sysTtyWrite(fd,ptr,len,&done);
if(ret) (_ssize_t)lv2errno_r(r,ret);
#else
done=write(_stdio_sock, ptr, len);
if(ret==-1) (_ssize_t)lv2errno_r(r,ret);
#endif
return (_ssize_t)done;
} else {
u64 done = 0;
ret = sysLv2FsWrite(fd,ptr,len,&done);
if(ret) (_ssize_t)lv2errno_r(r,ret);
return (_ssize_t)done;
}
return (_ssize_t)-1;
}