* Makefile.am (EXTRA_libguile_la_SOURCES): New variable to hold
[bpt/guile.git] / libguile / socket.c
index 4c17b02..e4a8177 100644 (file)
@@ -1,4 +1,4 @@
-/*     Copyright (C) 1996 Free Software Foundation, Inc.
+/*     Copyright (C) 1996,1997 Free Software Foundation, Inc.
  * 
  * This program is free software; you can redistribute it and/or modify
  * it under the terms of the GNU General Public License as published by
@@ -43,7 +43,6 @@
 #include <stdio.h>
 
 #include "_scm.h"
-#include "filesys.h"
 #include "unif.h"
 #include "feature.h"
 #include "fports.h"
@@ -465,7 +464,7 @@ scm_addr_vector (address, proc)
       ve[2] = scm_ulong2num ((unsigned long) ntohs (nad->sin_port));
     }
   else
-    scm_misc_error (proc, "Unrecognised socket address type: %s",
+    scm_misc_error (proc, "Unrecognised address family: %s",
                    scm_listify (SCM_MAKINUM (fam), SCM_UNSPECIFIED));
 
   return result;
@@ -559,37 +558,20 @@ scm_getpeername (sock)
   return result;
 }
 
-SCM_PROC (s_recv, "recv", 2, 1, 0, scm_recv);
+SCM_PROC (s_recv, "recv!", 2, 1, 0, scm_recv);
 
 SCM
-scm_recv (sock, buff_or_size, flags)
+scm_recv (sock, buf, flags)
      SCM sock;
-     SCM buff_or_size;
+     SCM buf;
      SCM flags;
 {
   int rv;
   int fd;
   int flg;
-  SCM tok_buf;
-  char *p;
-  int size;
-  int allocated = 0;
 
   SCM_ASSERT (SCM_NIMP (sock) && SCM_FPORTP (sock), sock, SCM_ARG1, s_recv);
-  if (SCM_INUMP (buff_or_size))
-    {
-      size = SCM_INUM (buff_or_size);
-      tok_buf = scm_makstr (size, 0);
-      allocated = 1;
-    }
-  else
-    {
-      SCM_ASSERT (SCM_NIMP (buff_or_size) && SCM_STRINGP (buff_or_size),
-             buff_or_size, SCM_ARG2, s_recv);
-      tok_buf = buff_or_size;
-      size = SCM_LENGTH (tok_buf);
-    }
-  p = SCM_CHARS (tok_buf);
+  SCM_ASSERT (SCM_NIMP (buf) && SCM_STRINGP (buf), buf, SCM_ARG2, s_recv);
   fd = fileno ((FILE *)SCM_STREAM (sock));
 
   if (SCM_UNBNDP (flags))
@@ -597,14 +579,11 @@ scm_recv (sock, buff_or_size, flags)
   else
     flg = scm_num2ulong (flags, (char *) SCM_ARG3, s_recv);
 
-  SCM_SYSCALL (rv = recv (fd, p, size, flg));
+  SCM_SYSCALL (rv = recv (fd, SCM_CHARS (buf), SCM_LENGTH (buf), flg));
   if (rv == -1)
     scm_syserror (s_recv);
 
-  return scm_cons (allocated
-                  ? scm_vector_set_length_x (tok_buf, (SCM) SCM_MAKINUM (rv))
-                  : tok_buf,
-                  SCM_MAKINUM (rv));
+  return SCM_MAKINUM (rv);
 }
 
 SCM_PROC (s_send, "send", 2, 1, 0, scm_send);
@@ -634,50 +613,63 @@ scm_send (sock, message, flags)
   return SCM_MAKINUM (rv);
 }
 
-SCM_PROC (s_recvfrom, "recvfrom", 2, 1, 0, scm_recvfrom);
+SCM_PROC (s_recvfrom, "recvfrom!", 2, 3, 0, scm_recvfrom);
 
 SCM
-scm_recvfrom (sock, buff_or_size, flags)
+scm_recvfrom (sock, buf, flags, start, end)
      SCM sock;
-     SCM buff_or_size;
+     SCM buf;
      SCM flags;
+     SCM start;
+     SCM end;
 {
   int rv;
   int fd;
   int flg;
-  SCM tok_buf;
-  char *p;
-  int size;
-  int allocated = 0;
+  int offset = 0;
+  int cend;
   int tmp_size;
   SCM address;
 
-  SCM_ASSERT (SCM_NIMP (sock) && SCM_FPORTP (sock), sock, SCM_ARG1, s_recvfrom);
-  if (SCM_INUMP (buff_or_size))
-    {
-      size = SCM_INUM (buff_or_size);
-      tok_buf = scm_makstr (size, 0);
-      allocated = 1;
-    }
+  SCM_ASSERT (SCM_NIMP (sock) && SCM_FPORTP (sock), sock, SCM_ARG1,
+             s_recvfrom);
+  SCM_ASSERT (SCM_NIMP (buf) && SCM_STRINGP (buf), buf, SCM_ARG2, s_recvfrom);
+  cend = SCM_LENGTH (buf);
+  
+  if (SCM_UNBNDP (flags))
+    flg = 0;
   else
     {
-      SCM_ASSERT (SCM_NIMP (buff_or_size) && SCM_STRINGP (buff_or_size),
-             buff_or_size, SCM_ARG2, s_recvfrom);
-      tok_buf = buff_or_size;
-      size = SCM_LENGTH (tok_buf);
+      flg = scm_num2ulong (flags, (char *) SCM_ARG3, s_recvfrom);
+
+      if (!SCM_UNBNDP (start))
+       {
+         offset = (int) scm_num2long (start,
+                                      (char *) SCM_ARG4, s_recvfrom);
+         
+         if (offset < 0 || offset >= cend)
+           scm_out_of_range (s_recvfrom, start);
+
+         if (!SCM_UNBNDP (end))
+           {
+             int tend = (int) scm_num2long (end,
+                                            (char *) SCM_ARG5, s_recvfrom);
+      
+             if (tend <= offset || tend > cend)
+               scm_out_of_range (s_recvfrom, end);
+
+             cend = tend;
+           }
+       }
     }
-  p = SCM_CHARS (tok_buf);
-  fd = fileno ((FILE *)SCM_STREAM (sock));
 
-  if (SCM_UNBNDP (flags))
-    flg = 0;
-  else
-    flg = scm_num2ulong (flags, (char *) SCM_ARG3, s_recvfrom);
+  fd = fileno ((FILE *)SCM_STREAM (sock));
 
   tmp_size = scm_addr_buffer_size;
-  SCM_SYSCALL (rv = recvfrom (fd, p, size, flg,
-                         (struct sockaddr *) scm_addr_buffer,
-                         &tmp_size));
+  SCM_SYSCALL (rv = recvfrom (fd, SCM_CHARS (buf) + offset,
+                             cend - offset, flg,
+                             (struct sockaddr *) scm_addr_buffer,
+                             &tmp_size));
   if (rv == -1)
     scm_syserror (s_recvfrom);
   if (tmp_size > 0)
@@ -685,13 +677,7 @@ scm_recvfrom (sock, buff_or_size, flags)
   else
     address = SCM_BOOL_F;
 
-  return scm_listify (allocated
-                     ? scm_vector_set_length_x (tok_buf,
-                                                (SCM) SCM_MAKINUM (rv))
-                     : tok_buf,
-                     SCM_MAKINUM (rv),
-                     address,
-                     SCM_UNDEFINED);
+  return scm_cons (SCM_MAKINUM (rv), address);
 }
 
 SCM_PROC (s_sendto, "sendto", 4, 0, 1, scm_sendto);