Check for SYS/GL during library init. Reason is that
[AROS.git] / workbench / network / stacks / AROSTCP / netlib / set_socket_stdio.c
blob32f17c5d88451abd3d934f3ca3122a7ef1d7fe3f
1 /* $Id$
3 * set_socket_stdio.c - redirect stdio to/from a socket
5 * Copyright © 1994 AmiTCP/IP Group,
6 * Network Solutions Development Inc.
7 * All rights reserved.
8 */
10 /****** net.lib/set_socket_stdio ****************************************
12 NAME
13 set_socket_stdio - redirect stdio to/from a socket
15 SYNOPSIS
16 int set_socket_stdio(int sock);
18 FUNCTION
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).
27 RETURN VALUES
28 0 if successful, -1 on error. Global variable 'errno' contains
29 the specific error code set by the failing function.
31 NOTES
32 This module pulls in the link library stdio modules.
34 SEE ALSO
35 dup2()
36 *****************************************************************************
40 #include <bsdsocket.h>
42 #include <stdio.h>
43 #include <stdlib.h>
45 int
46 set_socket_stdio(int sock)
49 * Replace the stdio with the sock
51 if (sock >= 0
52 && dup2(sock, fileno(stdin)) >= 0
53 && dup2(sock, fileno(stdout)) >= 0
54 && dup2(sock, fileno(stderr)) >= 0)
56 /*
57 * line buffered mode
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))
66 CloseSocket(sock);
68 return 0;
70 return -1;