forked from mmaydaniuk/cxcomm
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathdirent.c
150 lines (118 loc) · 2.8 KB
/
dirent.c
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
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
/***********************************************************************
**
** This file is part of the CXCOMM project.
**
** Copyright (C) 2015 Michael Maydaniuk. All rights reserved.
**
**
**
** This file may be distributed and/or modified under the terms of the
** GNU General Public License version 2 as published by the Free Software
** Foundation and appearing in the file gpl-2.0.txt included in the
** packaging of this file.
**
** 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., 51 Franklin Street, Fifth Floor, Boston, MA
** 02110-1301 USA
**
**
**
**********************************************************************/
/*
* dirent.c
*
* Try to simulate the POSIX dirent functions on the TRS-80
*/
#include "dirent.h"
#include "floppy.h"
#include "malloc.h"
#ifndef NULL
#define NULL 0
#endif
dirent* dir_entry;
/*
* opendir - open up the directory
*
*/
DIR* opendir(char path)
{
DIR* d = (DIR*) malloc(sizeof(DIR));
memset(d,0,sizeof(DIR));
dir_entry = (dirent*) malloc(sizeof(dirent));
if (!dskcon(DSKCON_READ,d->buffer, path, 17, 3))
return NULL;
d->cur_offset = 0;
d->cur_sector = 3;
d->drive_number = path;
return d;
}
/*
* closedir
*
* close dir and free memory
*/
void closedir(DIR* dp) {
if (dp != NULL)
free((char*) dp);
if (dir_entry != NULL)
free((char*) dir_entry);
}
/*
* readdir
*
* return next entry from directory
*/
dirent* readdir(DIR* dir)
{
char* entry;
entry = dir->buffer + dir->cur_offset;
// last directory entry
// shouldn't have to cast 0xFF should I? Is it a compiler issue
// or programmer one?
if (*entry == (char) 0xFF) {
return NULL;
}
// if this a valid entry
if (*entry != 0x00) {
memset(dir_entry, 0, sizeof(dirent));
char* p = (char*) dir_entry;
char* src = (char*) entry;
memcpy(p,src, 8);
p += 9;
src += 8;
memcpy(p,src, 3);
p += 4;
src += 3;
memcpy(p,src,21);
dir->cur_offset += 32; // point to the next entry
// get next sector, note relies on cur_offset being 256 bytes (ie. 0)
if (dir->cur_offset == 0 && ++dir->cur_sector < 19) {
dskcon(DSKCON_READ,dir->buffer, dir->drive_number, 17, dir->cur_sector);
}
return dir_entry;
}
}
#ifdef CXCOMM_TEST
int dirent_test() {
DIR *dp;
dirent *ep;
dp = opendir(0);
int i = 0;
if (dp != NULL) {
while ((ep = readdir(dp)) != NULL) {
printf("%s.%s\n",ep->d_name,ep->d_ext);
++i;
}
closedir (dp);
} else {
printf("Failed to open");
}
return 0;
}
#endif