Background is I have an existing application which lists directory entries; strace reveals it just calls getdents and lists them in the order returned. I would like them displayed in the same order as a call to ls with no arguments. Is it possible to update the directory data in some way to achieve this?
FS is ext4, if that makes any difference.
If you really are determined to change this program’s behaviour (of which I assume that you don’t have the source code available), you can use
LD_PRELOADto hook the call toopendirandreaddirand replace it with your own, sorting wrapper. An example how such a hook could look like is the following:It overrides
opendirandreaddirto return the entries in reverse order (you can adapt this for sorting too). This is how you use it with a programtestthat simply lists the directory entries in the order they are received:Hah! This works. We just hooked a libc function.