The -1 here is only "right" if IsDirReal(filename) == 1 below; otherwise, you'll needlessly fail when the name of a file would have *just* filled the available space (with one left for the '\0').