Hrm, removed the:

Code:
 if((q = strchr(p, '\n')))
   *q = '\0';
Lines and it works fine, was that part meant for something? heh