home *** CD-ROM | disk | FTP | other *** search
/ Aminet 18 / aminetcdnumber181997.iso / Aminet / dev / gcc / ixemulsrc.lha / ixemul / library / set_socket_stdio.c < prev    next >
C/C++ Source or Header  |  1996-12-11  |  2KB  |  67 lines

  1. /*
  2.  *    set_socket_stdio.c - redirect stdio to/from a socket
  3.  *
  4.  *    Copyright © 1994 AmiTCP/IP Group,
  5.  *             Network Solutions Development Inc.
  6.  *             All rights reserved.
  7.  *    Portions Copyright © 1995 Jeff Shepherd
  8.  *
  9.  *    $Id: set_socket_stdio.c,v 4.2 1994/09/30 00:35:04 jraja Exp $
  10.  */
  11.  
  12. /****** net.lib/set_socket_stdio ****************************************
  13.  
  14.     NAME
  15.     set_socket_stdio - redirect stdio to/from a socket
  16.  
  17.     SYNOPSIS
  18.     int set_socket_stdio(int sock);
  19.  
  20.     FUNCTION
  21.     Redirect stdio (stdin, stdout and stderr) to/from socket 'sock'.
  22.     This is done by dup2()'ing 'sock' to the level 1 files underneath
  23.     the stdin, stdout and stderr.
  24.  
  25.     The original 'sock' reference is closed on successful return, so
  26.     the socket descriptor given as argument should not be used after
  27.     this function returns (successfully).
  28.  
  29.     RETURN VALUES
  30.     0 if successful, -1 on error. Global variable 'errno' contains
  31.     the specific error code set by the failing function.
  32.  
  33.     NOTES
  34.     This module pulls in the link library stdio modules.
  35.  
  36.     SEE ALSO
  37.     dup2()
  38. *****************************************************************************
  39. *
  40. */
  41.  
  42. #define _KERNEL
  43. #include "ixemul.h"
  44.  
  45. #include <stdio.h>
  46. #include <stdlib.h>
  47.  
  48. int
  49. set_socket_stdio(int sock)
  50. {
  51.     /*
  52.      * Replace the stdio with the sock
  53.      */
  54.     if (sock >= 0 && syscall(SYS_dup2,sock, 0) >= 0
  55.       && syscall(SYS_dup2, sock, 1) >= 0
  56.       && syscall(SYS_dup2, sock, 2) >= 0) {
  57.     /*
  58.      * Close the original reference to the sock if necessary
  59.      */
  60.     if (sock > 2)
  61.         syscall(SYS_close, sock);
  62.  
  63.     return 0;
  64.     }
  65.     return -1;
  66. }
  67.