3 * set_socket_stdio.c - redirect stdio to/from a socket
5 * Copyright © 1994 AmiTCP/IP Group,
6 * Network Solutions Development Inc.
10 /****** net.lib/set_socket_stdio ****************************************
13 set_socket_stdio - redirect stdio to/from a socket
16 int set_socket_stdio(int sock);
19 Redirect stdio (stdin, stdout and stderr) to/from socket 'sock'.
20 This is done by dup2()'ing 'sock' to the level 1 files underneath
21 the stdin, stdout and stderr.
23 The original 'sock' reference is closed on successful return, so
24 the socket descriptor given as argument should not be used after
25 this function returns (successfully).
28 0 if successful, -1 on error. Global variable 'errno' contains
29 the specific error code set by the failing function.
32 This module pulls in the link library stdio modules.
36 *****************************************************************************
40 #include <bsdsocket.h>
46 set_socket_stdio(int sock
)
49 * Replace the stdio with the sock
52 && dup2(sock
, fileno(stdin
)) >= 0
53 && dup2(sock
, fileno(stdout
)) >= 0
54 && dup2(sock
, fileno(stderr
)) >= 0)
59 setvbuf(stdin
, NULL
, _IOLBF
, 0);
60 setvbuf(stdout
, NULL
, _IOLBF
, 0);
61 setvbuf(stderr
, NULL
, _IOLBF
, 0);
63 * Close the original reference to the sock if necessary
65 if (sock
> fileno(stderr
))