1
0
mirror of https://github.com/adtools/clib2.git synced 2025-12-08 14:59:05 +00:00
Files
amiga-clib2/library/dirent_opendir.c
Olaf Barthel 1550c6fef3 - When using the wildcard expansion code for command line
parameters (which is by default linked in with libunix.a),
  regular expressions can no longer prompt dos.library requesters
  to appear. However, to be on the safe side, if you are expecting
  to pass regular expressions on the command line, do not use
  the wildcard expansion code such as by overriding the library
  symbols with dummy functions such as are used in the file
  "stdlib_wildcard_expand.c".

- Added a new variable '__open_locale' which can be used to
  restrict all library functions to use the "C" language locale
  rather than the current system locale settings. In addition
  to that, two new functions __locale_exit() and __locale_init()
  can be used to close and (re-)open the system locale at a
  later time.

- Local ("static") functions are now identified by the STATIC
  qualifier. This was done in preparation for changes that will
  deal with global and local data and the issue of thread safety.


git-svn-id: file:///Users/olsen/Code/migration-svn-zu-git/logical-line-staging/clib2/trunk@14839 87f5fb63-7c3d-0410-a384-fd976d0f7a62
2005-02-25 10:14:22 +00:00

277 lines
6.3 KiB
C

/*
* $Id: dirent_opendir.c,v 1.7 2005-02-25 10:14:21 obarthel Exp $
*
* :ts=4
*
* Portable ISO 'C' (1994) runtime library for the Amiga computer
* Copyright (c) 2002-2005 by Olaf Barthel <olsen@sourcery.han.de>
* All rights reserved.
*
* Redistribution and use in source and binary forms, with or without
* modification, are permitted provided that the following conditions
* are met:
*
* - Redistributions of source code must retain the above copyright
* notice, this list of conditions and the following disclaimer.
*
* - Neither the name of Olaf Barthel nor the names of contributors
* may be used to endorse or promote products derived from this
* software without specific prior written permission.
*
* THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
* AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
* IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
* ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR CONTRIBUTORS BE
* LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
* CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
* SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
* INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
* CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
* ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
* POSSIBILITY OF SUCH DAMAGE.
*/
#ifndef _STDLIB_NULL_POINTER_CHECK_H
#include "stdlib_null_pointer_check.h"
#endif /* _STDLIB_NULL_POINTER_CHECK_H */
/****************************************************************************/
#ifndef _STDLIB_MEM_DEBUG_H
#include "stdlib_mem_debug.h"
#endif /* _STDLIB_MEM_DEBUG_H */
/****************************************************************************/
#ifndef _DIRENT_HEADERS_H
#include "dirent_headers.h"
#endif /* _DIRENT_HEADERS_H */
/****************************************************************************/
/* The following is not part of the ISO 'C' (1994) standard. */
/****************************************************************************/
#if defined(UNIX_PATH_SEMANTICS)
/****************************************************************************/
STATIC struct Node *
find_by_name(struct List * list,const char * name)
{
struct Node * result = NULL;
struct Node * node;
for(node = list->lh_Head ; node->ln_Succ != NULL ; node = node->ln_Succ)
{
if(strcasecmp(node->ln_Name,name) == 0)
{
result = node;
break;
}
}
return(result);
}
/****************************************************************************/
#endif /* UNIX_PATH_SEMANTICS */
/****************************************************************************/
DIR *
opendir(const char * path_name)
{
#if defined(UNIX_PATH_SEMANTICS)
struct name_translation_info path_name_nti;
#endif /* UNIX_PATH_SEMANTICS */
struct DirectoryHandle * dh = NULL;
DIR * result = NULL;
ENTER();
SHOWSTRING(path_name);
assert( path_name != NULL );
if(__check_abort_enabled)
__check_abort();
#if defined(CHECK_FOR_NULL_POINTERS)
{
if(path_name == NULL)
{
SHOWMSG("invalid parameter");
__set_errno(EFAULT);
goto out;
}
}
#endif /* CHECK_FOR_NULL_POINTERS */
dh = malloc(sizeof(*dh));
if(dh == NULL)
{
SHOWMSG("memory allocation failed");
goto out;
}
memset(dh,0,sizeof(*dh));
#if defined(UNIX_PATH_SEMANTICS)
{
struct Node * node;
NewList((struct List *)&dh->dh_VolumeList);
if(__unix_path_semantics)
{
if(__translate_unix_to_amiga_path_name(&path_name,&path_name_nti) != 0)
goto out;
SHOWSTRING(path_name);
if(path_name_nti.is_root)
{
struct DosList * dol;
UBYTE * name;
SHOWMSG("collecting volume names");
dh->dh_ScanVolumeList = TRUE;
PROFILE_OFF();
dol = LockDosList(LDF_VOLUMES|LDF_READ);
PROFILE_ON();
while((dol = NextDosEntry(dol,LDF_VOLUMES|LDF_READ)) != NULL)
{
name = BADDR(dol->dol_Name);
if(name != NULL && name[0] > 0)
{
size_t len;
len = name[0];
node = malloc(sizeof(*node) + len + 2);
if(node == NULL)
{
UnLockDosList(LDF_VOLUMES|LDF_READ);
__set_errno(ENOMEM);
goto out;
}
node->ln_Name = (char *)(node + 1);
memmove(node->ln_Name,&name[1],len);
node->ln_Name[len++] = ':';
node->ln_Name[len] = '\0';
/* Check if the name is already on the list. Mind you,
this is not the most sophisticated algorithm but then
the number of volumes should be small. */
if(find_by_name((struct List *)&dh->dh_VolumeList,node->ln_Name) != NULL)
{
free(node);
continue;
}
D(("adding '%s'",node->ln_Name));
AddTail((struct List *)&dh->dh_VolumeList,node);
}
}
UnLockDosList(LDF_VOLUMES|LDF_READ);
/* Bail out if we cannot present anything. */
if(IsListEmpty((struct List *)&dh->dh_VolumeList))
{
__set_errno(ENOMEM);
goto out;
}
}
}
}
#endif /* UNIX_PATH_SEMANTICS */
if(NOT dh->dh_ScanVolumeList)
{
LONG status;
SHOWMSG("we are supposed to scan a directory");
SHOWSTRING(path_name);
PROFILE_OFF();
dh->dh_DirLock = Lock((STRPTR)path_name,SHARED_LOCK);
PROFILE_ON();
if(dh->dh_DirLock == ZERO)
{
SHOWMSG("couldn't get a lock on it");
__set_errno(__translate_access_io_error_to_errno(IoErr()));
goto out;
}
assert( (((ULONG)&dh->dh_FileInfo) & 3) == 0 );
PROFILE_OFF();
status = Examine(dh->dh_DirLock,&dh->dh_FileInfo);
PROFILE_ON();
if(status == DOSFALSE)
{
SHOWMSG("couldn't examine it");
__set_errno(__translate_io_error_to_errno(IoErr()));
goto out;
}
if(dh->dh_FileInfo.fib_DirEntryType < 0)
{
SHOWMSG("this isn't a directory");
__set_errno(ENOTDIR);
goto out;
}
}
SHOWMSG("OK, done");
assert( __directory_list.mlh_Head != NULL );
AddTail((struct List *)&__directory_list,(struct Node *)dh);
result = (DIR *)dh;
dh = NULL;
out:
if(dh != NULL)
{
SHOWMSG("ouch. cleaning up");
#if defined(UNIX_PATH_SEMANTICS)
{
struct Node * node;
while((node = RemHead((struct List *)&dh->dh_VolumeList)) != NULL)
free(node);
}
#endif /* UNIX_PATH_SEMANTICS */
PROFILE_OFF();
UnLock(dh->dh_DirLock);
PROFILE_ON();
free(dh);
}
RETURN(result);
return(result);
}