4 ms·
There is a glob() function you can use in POSIX C also to get an array of strings. The getdents system call being used in the above program is the basis for im
by kazinator 2y ago
There is a glob() function you can use in POSIX C also to get an array of strings.
The getdents system call being used in the above program is the basis for implementing readdir.
It doesn't return a string, but rather a buffer of multiple directory entries.
The program isn't parsing a giant string; it is parsing out the directory entry structures, which are variable length and have a length field so the next one can be found.
The program writes each name including the null terminator, so that the output is suitable for utilities which understand that.
- db48x 2y agoThe problem is the phrase “suitable for shell pipelines”. If you are in a shell, you should not be doing anything like this. You should use a glob directly in the shell. You should not be calling an external program, having that program print out something, and then parsing it. Just use a glob right there in your shell script. If you do anything else, you are doing it wrong. Do I really have to say this again?