Trim trailing cwd from buf.