Static Analysis ftw
Stratic analysis tools would raise a bunch of issues here.
strlen returns a size_t, which is unsigned. Mixing signed and unsigned with an inequality if statement is a big red flag.
I agree with an earlier point, concating strings and general path manipulation should be a solved problem.