Google Groups no longer supports new Usenet posts or subscriptions. Historical content remains viewable.
Dismiss

how to do a seek() in lisp

215 views
Skip to first unread message

Lin Jingxian

unread,
Jul 22, 2005, 1:29:05 AM7/22/05
to
hi,
I want to create an empty file and then seek the file pointer position to
make ls
think it has some contents. what common lisp function I can use ? thanks.


Christopher C. Stacy

unread,
Jul 22, 2005, 2:41:20 AM7/22/05
to
"Lin Jingxian" <cckk...@yahoo.ie> writes:

There is no ANSI standard way to do that in Common Lisp.
What are you actually trying to accomplish?

R. Mattes

unread,
Jul 22, 2005, 10:31:13 AM7/22/05
to

Looks like Lin wants to create a sparse file (i.e. one of
those monster files with "holes" in it -- great for practical
jokes :-)

Cheers Ralf Mattes

Peter Seibel

unread,
Jul 22, 2005, 11:41:21 AM7/22/05
to
"Lin Jingxian" <cckk...@yahoo.ie> writes:

I'm not sure if this is what you mean but:

CL-USER> (with-open-file (out "/tmp/bigfile" :direction :io :element-type '(unsigned-byte 8) :if-exists :supersede)
(file-position out (1- (* 1024 1024)))
(write-byte 0 out))
0

[peter@beagle tmp]$ ls -l bigfile
-rw-r--r-- 1 peter wheel 1048576 Jul 22 08:40 bigfile

-Peter

--
Peter Seibel * pe...@gigamonkeys.com
Gigamonkeys Consulting * http://www.gigamonkeys.com/
Practical Common Lisp * http://www.gigamonkeys.com/book/

Pascal Bourguignon

unread,
Jul 22, 2005, 1:04:24 PM7/22/05
to
"Lin Jingxian" <cckk...@yahoo.ie> writes:

Normally:

(with-open-file (out "/tmp/content.data" :direction :output
:if-exists :supersede :if-does-not-exist :create
:element-type '(unsigned-byte 8))
(file-position out 10000)
(write-byte 0 out))


Unfortunately, some implementations on unix don't like it and give:

*** - cannot position
#<OUTPUT BUFFERED FILE-STREAM (UNSIGNED-BYTE 8) #P"/tmp/content.data">
beyond EOF


Clearly, this is an implementation problem. I see nothing in clhs
file-position that prevent to implement file-position as lseek on
unix.

In particular, clhs file-position says:

Exceptional Situations:

If position-spec is supplied, but is too large or otherwise
inappropriate, an error is signaled.

but a possition greater than the current file size is not too large
and neither inappropriate on a unix system.

The problems are

- CLHS allows this behavior (it doesn't define a too large file size
and neither an inappropriate file size).

- there is no CLRFI specifying a common meaning for these notions on
POSIX systems, therefore different implementations on a POSIX system
diverge.

- there is no CLRFI specifying a common meaning for these notions on
UNIX systems, therefore different implementations on a UNIX system
diverge.


PS: On some other implementations (eg. SBCL on Linux), it works as expected:

$ ls -l /tmp/content.data
-rw-r--r-- 1 pjb pjb 10001 2005-07-22 19:02 /tmp/content.data


--
__Pascal Bourguignon__ http://www.informatimago.com/

This is a signature virus. Add me to your signature and help me to live

Peter Seibel

unread,
Jul 22, 2005, 4:48:03 PM7/22/05
to
Pascal Bourguignon <p...@informatimago.com> writes:

> "Lin Jingxian" <cckk...@yahoo.ie> writes:
>
>> hi,
>> I want to create an empty file and then seek the file pointer position to
>> make ls
>> think it has some contents. what common lisp function I can use ? thanks.
>
> Normally:
>
> (with-open-file (out "/tmp/content.data" :direction :output
> :if-exists :supersede :if-does-not-exist :create
> :element-type '(unsigned-byte 8))
> (file-position out 10000)
> (write-byte 0 out))

How does that impl handle it if you specify :io instead of :output?

0 new messages