This is the mail archive of the guile@cygnus.com mailing list for the guile project.


Index Nav: [Date Index] [Subject Index] [Author Index] [Thread Index]
Message Nav: [Date Prev] [Date Next] [Thread Prev] [Thread Next]

read-line on socket bug (guile-core-19980818 on linux)


In the following program, read-line returns only empty strings when I
telnet to the port:

#!/usr/local/bin/guile \
-s
!#
(let ((p (socket AF_INET SOCK_STREAM 0)))
  (bind p AF_INET INADDR_ANY 8000)
  (listen p 10)
  (select (list p) () ())
  (let ((q (car (accept p))))
    (let loop ()
      (let ((l (read-line q)))
	(if (not (eof-object? l))
	    (begin
	      (display "Got one line: ")
	      (display l)
	      (newline)
	      (loop))
	    (close-port q)))))
  (close-port p))

I have traced this to fact that socket ports are treated like file
ports (see scm_sock_fd_to_port, socket.c, line 93), but scm_fgets
(fports.c, line 299) relies on ftell to know how many characters are
read. On linux, ftell returns -1 on sockets.

I can think of a few different solutions to the problem:

1. Don't support read-line on sockets ;-(

2. Fix scm_fgets to check if ftell returns -1 and fall back to a
fgetc loop

3. Fix scm_fgets to check if ftell returns -1 and fall back to strlen
to know how many characters were read.

4. Introduce sockets as a new port type (scm_tc16_socket) using
scm_generic_fgets

Ole