Changing the way I used opendir() helped, actually. See above discussion for details.
In reply to Re^2: working with relative paths by apotheon in thread working with relative paths by apotheon