/* $Id: dir 3.2.0.3 1998/07/26 10:48:38 stoklund Exp stoklund $ */
/* dir.c: directory operations.

   Copyright (C) 1992, 93, 94, 95 Free Software Foundation, Inc.
   Copyright (C) 1998 Jakob Stoklund Olesen

   This library is free software; you can redistribute it and/or
   modify it under the terms of the GNU Library General Public
   License as published by the Free Software Foundation; either
   version 2 of the License, or (at your option) any later version.

   This library is distributed in the hope that it will be useful,
   but WITHOUT ANY WARRANTY; without even the implied warranty of
   MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
   Library General Public License for more details.

   You should have received a copy of the GNU Library General Public
   License along with this library; if not, write to the Free Software
   Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA.  */

#include "kpathsea:config.h"

/* This file has been completely rewritten for RISC OS */

#include "kpathsea:riscos.h"
#include "OS:osfile.h"

/* Return true if FN is a directory or a symlink to a directory,
   false if not. */

boolean
dir_p P1C (const_string, fn)
{
  int len;
  string name;
  os_error *erp;
  fileswitch_object_type obj_type;

  assert (fn && *fn);

  /* ensure_prefix can't handle this */
  if (fn[0] == '@' && fn[1] == '\0')
    return true;

  name = riscos_ensure_prefix (fn);

  /* Remove trailing dots */
  for (len = strlen (name); len > 0 && name[len - 1] == '.';
       name[--len] = '\0')
    /* empty */ ;

  /* OS_File 5 to get object type */
  erp = xosfile_read (name, &obj_type, NULL, NULL, NULL, NULL);

#ifdef KPSE_DEBUG
  if (KPSE_DEBUG_P (KPSE_DEBUG_STAT))
    {
      char *type[] =
      {"None", "File", "Dir", "Image"};
      DEBUGF2 ("OS_File 5,\"%s\" => %s\n", name,
	       erp ? erp->errmess : type[obj_type]);
    }
#endif
  free (name);

  /* We accept directories and image files */
  return erp == NULL && (obj_type == 2 || obj_type == 3);
}
