I'm wondering how did you dertermine the end of a script/file? I'm especially interested for old Unix versions (like V6).

Is there a '\0' after the last written character?

  • 2
    Shell scripts are text files and as such should ideally not include any zero bytes, although doing so will probably not impair normal use. – goldilocks Jan 5 '15 at 21:38

Userland programs under even older Unixes did not see "pad" bytes at the end of a file. I know that MS-DOS or CP/M would fill disk blocks with Ctrl-Z characters, so not only did a file reading algorithm have to check for end-of-disk-blocks, it also had to check for padding bytes.

Unixes never did that sort of thing. Programs read bytes until the end-of-file condition happens, which for the read(2) system call means returning 0. Unfortuately a long-running system call can be interrupted, which causes read() to return the error code (-1), and the global symbol errno evaluates to EINTR, so Unixes also traditioally introduce some goofiness into reading certain devices.

There's also a file system aspect to this all: Unix filesystems would put data into disk blocks, and keep a file-size-in-bytes value in the inode. Some other OSes only kept file size in blocks. If data was smaller than a block, the problem bubbled up into userland, with pad bytes or other nonsense.

  • 5
    CP/M would fill 128-byte records with Ctrl+Z, but DOS has never done so - it's always recorded the exact file length just like Unix (though many programs did accept ^Z as an end-of-file marker). – psmears Jan 5 '15 at 22:20
  • @psmears I have seen DOS files with ^Z padding bytes, way back at the dawn of time. I think it might have been a DOS 1.x issue, or possibly only an issue with programs that were written for CP/M and then ported to DOS 1.x. IIRC, WordStar files had ^Z padding bytes... this would have been mid-1980's. Note that even for a while after DOS 2.0 came out and changed everything, there were still a few programs floating around that had been written for DOS 1.x and they still worked (in other words DOS 2.x was backward-compatible). – steveha Jan 5 '15 at 22:39
  • 3
    @steveha: I suspect "written for CP/M and then ported" is the explanation (as it is for a lot of ancient weird DOS stuff :) ) – psmears Jan 5 '15 at 22:41
  • How were binary files (that can contain any byte sequence) handled on systems that used padding? – orion Jan 6 '15 at 8:35
  • 1
    @orion good question - I suggest you make it a new separate one. – peterph Jan 6 '15 at 19:36

Not necessarily. The shell interpreter reads the file with the script using some (more or less complex) syscall wrapper (e.g. read() of fread()) and that will signal the end-of-file condition when it reaches the last byte of the file (which doesn't need to be zero).

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.