summaryrefslogtreecommitdiff
path: root/src/libs/resource/direct.c
blob: b3d3541fdfa6959845150994033af95d025a1c90 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
//Copyright Paul Reiche, Fred Ford. 1992-2002

/*
 *  This program is free software; you can redistribute it and/or modify
 *  it under the terms of the GNU General Public License as published by
 *  the Free Software Foundation; either version 2 of the License, or
 *  (at your option) any later version.
 *
 *  This program 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 General Public License for more details.
 *
 *  You should have received a copy of the GNU General Public License
 *  along with this program; if not, write to the Free Software
 *  Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA.
 */

#include "libs/strings/strintrn.h"
#include "libs/memlib.h"
#include "port.h"
#include "libs/uio.h"
#include <sys/stat.h>

DIRENTRY_REF
LoadDirEntryTable (uio_DirHandle *dirHandle, const char *path,
		const char *pattern, match_MatchType matchType)
{
	uio_DirList *dirList;
	COUNT num_entries;
	COUNT i;
	uio_DirHandle *dir;
	STRING_TABLE StringTable;
	STRING_TABLE_DESC *lpST;
	STRING lpLastString;

	dir = uio_openDirRelative (dirHandle, path, 0);
	assert(dir != NULL);
	dirList = uio_getDirList (dir, "", pattern, matchType);
	assert(dirList != NULL);
	num_entries = 0;

	// First, count the amount of space needed
	for (i = 0; i < dirList->numNames; i++)
	{
		struct stat sb;

		if (dirList->names[i][0] == '.')
		{
			dirList->names[i] = NULL;
			continue;
		}
		if (uio_stat (dir, dirList->names[i], &sb) == -1)
		{
			dirList->names[i] = NULL;
			continue;
		}
		if (!S_ISREG (sb.st_mode))
		{
			dirList->names[i] = NULL;
			continue;
		}
		num_entries++;
	}
	uio_closeDir (dir);

	if (num_entries == 0) {
		uio_DirList_free(dirList);
		return ((DIRENTRY_REF) 0);
	}

	StringTable = AllocStringTable (num_entries, 0);
	lpST = StringTable;
	if (lpST == 0)
	{
		FreeStringTable (StringTable);
		uio_DirList_free(dirList);
		return ((DIRENTRY_REF) 0);
	}
	lpST->size = num_entries;
	lpLastString = lpST->strings;

	for (i = 0; i < dirList->numNames; i++)
	{
		int size;
		STRINGPTR target;
		if (dirList->names[i] == NULL)
			continue;
		size = strlen (dirList->names[i]) + 1;
		target = HMalloc (size);
		memcpy (target, dirList->names[i], size);
		lpLastString->data = target;
		lpLastString->length = size;
		lpLastString++;
	}
	
	uio_DirList_free(dirList);
	return StringTable;
}