OSDN Git Service

2006-10-31 Bob Duff <duff@adacore.com>
[pf3gnuchains/gcc-fork.git] / gcc / ada / g-socthi.adb
index 2c337e0..914b787 100644 (file)
@@ -6,7 +6,7 @@
 --                                                                          --
 --                                 B o d y                                  --
 --                                                                          --
---              Copyright (C) 2001-2003 Ada Core Technologies, Inc.         --
+--                     Copyright (C) 2001-2005, AdaCore                     --
 --                                                                          --
 -- GNAT is free software;  you can  redistribute it  and/or modify it under --
 -- terms of the  GNU General Public License as published  by the Free Soft- --
@@ -16,8 +16,8 @@
 -- or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU General Public License --
 -- for  more details.  You should have  received  a copy of the GNU General --
 -- Public License  distributed with GNAT;  see file COPYING.  If not, write --
--- to  the Free Software Foundation,  59 Temple Place - Suite 330,  Boston, --
--- MA 02111-1307, USA.                                                      --
+-- to  the  Free Software Foundation,  51  Franklin  Street,  Fifth  Floor, --
+-- Boston, MA 02110-1301, USA.                                              --
 --                                                                          --
 -- As a special exception,  if other files  instantiate  generics from this --
 -- unit, or you link  this unit with other files  to produce an executable, --
@@ -44,8 +44,8 @@ with Interfaces.C; use Interfaces.C;
 
 package body GNAT.Sockets.Thin is
 
-   Non_Blocking_Sockets : constant Fd_Set_Access
-     := New_Socket_Set (No_Socket_Set);
+   Non_Blocking_Sockets : constant Fd_Set_Access :=
+                            New_Socket_Set (No_Socket_Set);
    --  When this package is initialized with Process_Blocking_IO set
    --  to True, sockets are set in non-blocking mode to avoid blocking
    --  the whole process when a thread wants to perform a blocking IO
@@ -61,34 +61,36 @@ package body GNAT.Sockets.Thin is
    --  two attempts on a blocking operation.
 
    Thread_Blocking_IO : Boolean := True;
+   --  Comment required for this ???
+
+   Unknown_System_Error : constant C.Strings.chars_ptr :=
+                            C.Strings.New_String ("Unknown system error");
+
+   --  Comments required for following functions ???
 
    function Syscall_Accept
      (S       : C.int;
       Addr    : System.Address;
-      Addrlen : access C.int)
-      return    C.int;
+      Addrlen : access C.int) return C.int;
    pragma Import (C, Syscall_Accept, "accept");
 
    function Syscall_Connect
      (S       : C.int;
       Name    : System.Address;
-      Namelen : C.int)
-      return    C.int;
+      Namelen : C.int) return C.int;
    pragma Import (C, Syscall_Connect, "connect");
 
    function Syscall_Ioctl
      (S    : C.int;
       Req  : C.int;
-      Arg  : Int_Access)
-      return C.int;
+      Arg  : Int_Access) return C.int;
    pragma Import (C, Syscall_Ioctl, "ioctl");
 
    function Syscall_Recv
      (S     : C.int;
       Msg   : System.Address;
       Len   : C.int;
-      Flags : C.int)
-      return  C.int;
+      Flags : C.int) return C.int;
    pragma Import (C, Syscall_Recv, "recv");
 
    function Syscall_Recvfrom
@@ -97,16 +99,14 @@ package body GNAT.Sockets.Thin is
       Len     : C.int;
       Flags   : C.int;
       From    : Sockaddr_In_Access;
-      Fromlen : access C.int)
-      return    C.int;
+      Fromlen : access C.int) return C.int;
    pragma Import (C, Syscall_Recvfrom, "recvfrom");
 
    function Syscall_Send
      (S     : C.int;
       Msg   : System.Address;
       Len   : C.int;
-      Flags : C.int)
-      return  C.int;
+      Flags : C.int) return C.int;
    pragma Import (C, Syscall_Send, "send");
 
    function Syscall_Sendto
@@ -115,16 +115,19 @@ package body GNAT.Sockets.Thin is
       Len   : C.int;
       Flags : C.int;
       To    : Sockaddr_In_Access;
-      Tolen : C.int)
-      return  C.int;
+      Tolen : C.int) return C.int;
    pragma Import (C, Syscall_Sendto, "sendto");
 
    function Syscall_Socket
-     (Domain, Typ, Protocol : C.int)
-      return C.int;
+     (Domain   : C.int;
+      Typ      : C.int;
+      Protocol : C.int) return C.int;
    pragma Import (C, Syscall_Socket, "socket");
 
-   function  Non_Blocking_Socket (S : C.int) return Boolean;
+   procedure Disable_SIGPIPE (S : C.int);
+   pragma Import (C, Disable_SIGPIPE, "__gnat_disable_sigpipe");
+
+   function Non_Blocking_Socket (S : C.int) return Boolean;
    procedure Set_Non_Blocking_Socket (S : C.int; V : Boolean);
 
    --------------
@@ -134,8 +137,7 @@ package body GNAT.Sockets.Thin is
    function C_Accept
      (S       : C.int;
       Addr    : System.Address;
-      Addrlen : access C.int)
-      return    C.int
+      Addrlen : access C.int) return C.int
    is
       R   : C.int;
       Val : aliased C.int := 1;
@@ -164,6 +166,7 @@ package body GNAT.Sockets.Thin is
          Discard := Syscall_Ioctl (R, Constants.FIONBIO, Val'Unchecked_Access);
       end if;
 
+      Disable_SIGPIPE (R);
       return R;
    end C_Accept;
 
@@ -174,8 +177,7 @@ package body GNAT.Sockets.Thin is
    function C_Connect
      (S       : C.int;
       Name    : System.Address;
-      Namelen : C.int)
-      return    C.int
+      Namelen : C.int) return C.int
    is
       Res : C.int;
 
@@ -235,10 +237,9 @@ package body GNAT.Sockets.Thin is
    -------------
 
    function C_Ioctl
-     (S    : C.int;
-      Req  : C.int;
-      Arg  : Int_Access)
-      return C.int
+     (S   : C.int;
+      Req : C.int;
+      Arg : Int_Access) return C.int
    is
    begin
       if not Thread_Blocking_IO
@@ -260,8 +261,7 @@ package body GNAT.Sockets.Thin is
      (S     : C.int;
       Msg   : System.Address;
       Len   : C.int;
-      Flags : C.int)
-      return  C.int
+      Flags : C.int) return C.int
    is
       Res : C.int;
 
@@ -288,8 +288,7 @@ package body GNAT.Sockets.Thin is
       Len     : C.int;
       Flags   : C.int;
       From    : Sockaddr_In_Access;
-      Fromlen : access C.int)
-      return    C.int
+      Fromlen : access C.int) return C.int
    is
       Res : C.int;
 
@@ -314,8 +313,7 @@ package body GNAT.Sockets.Thin is
      (S     : C.int;
       Msg   : System.Address;
       Len   : C.int;
-      Flags : C.int)
-      return  C.int
+      Flags : C.int) return C.int
    is
       Res : C.int;
 
@@ -342,8 +340,7 @@ package body GNAT.Sockets.Thin is
       Len   : C.int;
       Flags : C.int;
       To    : Sockaddr_In_Access;
-      Tolen : C.int)
-      return  C.int
+      Tolen : C.int) return C.int
    is
       Res : C.int;
 
@@ -367,8 +364,7 @@ package body GNAT.Sockets.Thin is
    function C_Socket
      (Domain   : C.int;
       Typ      : C.int;
-      Protocol : C.int)
-      return     C.int
+      Protocol : C.int) return C.int
    is
       R   : C.int;
       Val : aliased C.int := 1;
@@ -388,7 +384,7 @@ package body GNAT.Sockets.Thin is
          Discard := Syscall_Ioctl (R, Constants.FIONBIO, Val'Unchecked_Access);
          Set_Non_Blocking_Socket (R, False);
       end if;
-
+      Disable_SIGPIPE (R);
       return R;
    end C_Socket;
 
@@ -416,10 +412,9 @@ package body GNAT.Sockets.Thin is
 
    function Non_Blocking_Socket (S : C.int) return Boolean is
       R : Boolean;
-
    begin
       Task_Lock.Lock;
-      R := Is_Socket_In_Set (Non_Blocking_Sockets, S);
+      R := (Is_Socket_In_Set (Non_Blocking_Sockets, S) /= 0);
       Task_Lock.Unlock;
       return R;
    end Non_Blocking_Socket;
@@ -433,7 +428,7 @@ package body GNAT.Sockets.Thin is
       Address : In_Addr)
    is
    begin
-      Sin.Sin_Addr   := Address;
+      Sin.Sin_Addr := Address;
    end Set_Address;
 
    ----------------
@@ -496,7 +491,9 @@ package body GNAT.Sockets.Thin is
    -- Socket_Error_Message --
    --------------------------
 
-   function Socket_Error_Message (Errno : Integer) return String is
+   function Socket_Error_Message
+     (Errno : Integer) return C.Strings.chars_ptr
+   is
       use type Interfaces.C.Strings.chars_ptr;
 
       C_Msg : C.Strings.chars_ptr;
@@ -505,10 +502,9 @@ package body GNAT.Sockets.Thin is
       C_Msg := C_Strerror (C.int (Errno));
 
       if C_Msg = C.Strings.Null_Ptr then
-         return "Unknown system error";
-
+         return Unknown_System_Error;
       else
-         return C.Strings.Value (C_Msg);
+         return C_Msg;
       end if;
    end Socket_Error_Message;