I think you can pass sizeof(str) for the 2nd argument of snprintf and it will do the right thing (ensure that it's always null-terminated). Then, you don't need the explicit assignment of '\0' above.

Same thing for elsewhere.