/* Create a directory with the given prefix (e.g. "wireshark"). The path is created using g_get_tmp_dir and mkdtemp. */
const char* create_tempdir(char **namebuf, const char *pfx)
/* Create a directory with the given prefix (e.g. "wireshark"). The path is created using g_get_tmp_dir and mkdtemp. */
const char* create_tempdir(char **namebuf, const char *pfx)
{
  static char *td_path[3];
  static int td_path_len[3];
  static int idx;
  const char *tmp_dir;
  idx = (idx + 1) % 3;
  if (td_path[idx] == NULL) {
    td_path_len[idx] = INITIAL_PATH_SIZE;
    td_path[idx] = (char *)g_malloc(td_path_len[idx]);
  }
  tmp_dir = g_get_tmp_dir();
  while (g_snprintf(td_path[idx], td_path_len[idx], "%s%c%s" TMP_FILE_SUFFIX, tmp_dir, G_DIR_SEPARATOR, pfx) > td_path_len[idx]) {
    td_path_len[idx] *= 2;
    td_path[idx] = (char *)g_realloc(td_path[idx], td_path_len[idx]);
  }
  if (namebuf) {
    *namebuf = td_path[idx];
  }
  return mkdtemp(td_path[idx]);
}