バグ発見。本質的なとこじゃないけど。
関数 trim() の中の2行

(誤)
memmove(str, p, q - p);
*(q + 1) = '\0';

(正)
memmove(str, p, q - p + 1);
*q = '\0';