/* Find the most appropriate command matching the command line  */
static const iwlist_cmd* find_command(const char *cmd)
/* Find the most appropriate command matching the command line  */
static const iwlist_cmd* find_command(const char *cmd)
{
  const iwlist_cmd *	found = NULL;
  int			ambig = 0;
  unsigned int		len = strlen(cmd);
  int			i;
  for(i = 0; iwlist_cmds[i].cmd != NULL; ++i)
    {
      if(strncasecmp(iwlist_cmds[i].cmd, cmd, len) != 0)
	continue;
      if(len == strlen(iwlist_cmds[i].cmd))
	return &iwlist_cmds[i];
      if(found == NULL)
	found = &iwlist_cmds[i];
      else
	if (iwlist_cmds[i].fn != found->fn)
	  ambig = 1;
    }
  if(found == NULL)
    {
      fprintf(stderr, "iwlist: unknown command `%s' (check 'iwlist --help').\n", cmd);
      return NULL;
    }
  if(ambig)
    {
      fprintf(stderr, "iwlist: command `%s' is ambiguous (check 'iwlist --help').\n", cmd);
      return NULL;
    }
  return found;
}