Why Is Escaping Data Considered So Magical?

Cameron Simpson cs at zip.com.au
Wed Jun 30 19:05:42 EDT 2010


On 30Jun2010 12:19, Paul Rubin <no.email at nospam.invalid> wrote:
| Cameron Simpson <cs at zip.com.au> writes:
| > The original V7 (and probably earlier) UNIX filesystem has 16 byte directory
| > entries: 2 bytes for an inode and 14 bytes for the name. You could use 14
| > bytes of that name, and strncpy makes it effective to work with that data
| > structure.  
| 
| Why not use memcpy for that?

Because when you've pulled names _out_ of the directory structure they're
conventional C strings, ready for conventional C string mucking about:
NUL terminated, with no expectation that any memory is allocated beyond
the NUL.

Think of strncpy as a conversion function. Your source is a conventional
C string of unknown size, your destination is a NUL padded buffer of
known size. "Copy at most n bytes of this string into the buffer, pad
with NULs."

Cheers,
-- 
Cameron Simpson <cs at zip.com.au> DoD#743
http://www.cskk.ezoshosting.com/cs/

We had the experience, but missed the meaning.  - T.S. Eliot



More information about the Python-list mailing list