Hacker News new | ask | show | jobs
by PhilipRoman 698 days ago
Implementation issues aside, while technically it should be possible to seek a file descriptor from shell through a suitable helper program in C, I believe none of the POSIX utilities provide this facility
1 comments

head, read, and sed can be used for seeking forward according to POSIX (see the INPUT FILES section here <https://pubs.opengroup.org/onlinepubs/9799919799/utilities/V...>). I doubt non-GNU implementations support it though.
If it’s in POSIX, chances are the BSDs implement it, too.

I think seeking a specific number of bytes and then writing data there will be a problem, though.

For seeking n bytes, read nor sed will work; they work with lines.

sed is the only one of those that can write, and POSIX doesn’t appear to have the -i option for in-place editing (https://pubs.opengroup.org/onlinepubs/9699919799/utilities/s...)

So, I think head for seeking followed by sed (or ed or vi, but sed is the simpler tool, I think) for replacing the first n characters, redirecting to a temp file and then doing a mv is your only option.

Advantage will be that writes will be atomic; disadvantage that it will be slow

head was used for this purpose in the xz backdoor.
I think dd might be more reliable. (Is dd POSIX?)