'pattern' is owned by the caller, so you need to do 'strdup(pattern)' here