@kas1e
char **GetFileNameInDirectory (const char* dir)
{
char **filenames_pp = NULL;
struct ExamineData *data_p = IDOS->ExamineObjectTags (EX_StringNameInput, dir, TAG_END);
if (data_p)
{
if (EXD_IS_DIRECTORY (data_p))
{
APTR context_p = IDOS->ObtainDirContextTags (EX_StringNameInput, dir,
EX_DataFields, (EXF_NAME | EXF_LINK | EXF_TYPE),
TAG_END);
if (context_p)
{
struct List *names_p = IExec->AllocSysObjectTags (ASOT_LIST, TAG_DONE);
if (names_p)
{
struct ExamineData *dat_p;
size_t num_entries = 0;
/*
context_p takes care of deleting the returned dat_p objects
when we call ReleaseDirContext, so we mustn't call
FreeDosObject on them
*/
while ((dat_p = IDOS->ExamineDir (context_p)))
{
if (EXD_IS_FILE (dat_p))
{
char *copied_name_p = CopyToNewString (dat_p -> Name);
if (copied_name_p)
{
struct Node *node_p = IExec->AllocSysObjectTags (ASOT_NODE,
ASONODE_Name, copied_name_p,
ASONODE_Size, sizeof (struct Node),
TAG_DONE);
if (node_p)
{
IExec->AddTail (names_p, node_p);
++ num_entries;
}
}
}
}
if (ERROR_NO_MORE_ENTRIES != IDOS->IoErr ())
{
IDOS->Printf ("Failed to obtain directory context for "%s"n", dir);
IDOS->PrintFault (IDOS->IoErr (), NULL); /* failure - why ? */
}
else
{
filenames_pp = (char **) IExec->AllocVecTags (num_entries * sizeof (char *), TAG_DONE);
if (filenames_pp)
{
char *current_filename_pp = filenames_pp;
struct Node *node_p = IExec->GetHead (names_p);
struct Node *next_node_p = NULL;
while (node_p)
{
if (node_p -> ln_Name)
{
*current_filename_pp = node_p -> ln_Name;
++ current_filename_pp;
(node_p -> ln_Name = NULL;
}
next_node_p = IExec->GetSucc (node_p);
IExec->FreeSysObject (ASOT_NODE, node_p);
node_p = next_node_p;
}
}
}
IExec->FreeSysObject (ASOT_LIST, (APTR) list_p);
}
IDOS->ReleaseDirContext (context_p); /* NULL safe */
} /* if (context_p) */
else
{
IDOS->Printf ("oopsn");
IDOS->PrintFault (IDOS->IoErr (), NULL); /* no context - why ? */
}
} /* if (EXD_IS_DIRECTORY (data_p)) */
IDOS->FreeDosObject (DOS_EXAMINEDATA, data_p);
} /* if (data_p) */
return filenames_pp;
}
char *CopyToNewString (const char *src_p)
{
char *dest_p = NULL;
if (src_p)
{
size_t len = strlen (src_p);
dest_p = (char *) IExec->AllocVecTags (len + 1, TAG_DONE);
if (dest_p)
{
strcpy (dest_p, start_p);
}
}
return dest_p;
}