bug-gnulib
[Top][All Lists]
Advanced

[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]

Re: Mapping text files


From: Bruce Korb
Subject: Re: Mapping text files
Date: Tue, 19 Sep 2006 20:18:02 -0700
User-agent: Mozilla Thunderbird 1.0.7 (X11/20050923)

Paul Eggert wrote:
It has always been an irritant that you cannot mmap a text file
and be sure that there is a terminating NUL, without going to a
lot of hassle.

But the GNU coding standards say that programs generally must treat
NUL bytes as valid data.  There might be some minor benefit to
appending a NUL, but since a program should handle internal NUL
anyway, it'll need to check against the file size anyway.

If valid data is supposed to be ASCII text, then what meaning
do you give to the NUL byte?  If a data file has random trash
in it, the correct response is something along the lines of EINVAL
whenever strlen(filedata) != filestat.st_size.

I've sometimes felt the need for mapping a text file into memory,
and adding a trailing \n if the file ends in an incomplete line.
I suppose that might be a useful module for GNU programs.

That's a good idea.  As long as I'm writing code to ensure NUL
termination, I can ensure two extra bytes and append '\n'
when the last file byte is not a newline.

An argument against using mmap is that input (e.g., EIO) errors
typically cause signals to be sent to your program, and it's a pain to
catch them and do the right thing portably.

It's actually a pain to set up the map portably.  :)  I'll add that
caveat to the doc.  The bigger problem was relying on the separation
page between mapped segments.  It is not required to be there.
I'll be fixing that soon.

Cheers - Bruce





reply via email to

[Prev in Thread] Current Thread [Next in Thread]