Subversion Repositories Kolibri OS

Rev

Rev 6727 | Only display areas with differences | Regard whitespace | Details | Blame | Last modification | View Log | RSS feed

Rev 6727 Rev 6775
1
/*
1
/*
2
Kolibri OS port for gcc 5.4
2
Kolibri OS port for gcc 5.4
3
 
3
 
4
Started by Siemargl @Nov 2016
4
Started by Siemargl @Nov 2016
5
 
5
 
6
Contains realisation of directory handling functions:
6
Contains realization of directory handling functions:
7
    mkdir()
7
    mkdir()
8
    closedir()
8
    closedir()
9
    opendir()
9
    opendir()
10
    readdir()
10
    readdir()
11
    rewinddir()
11
    rewinddir()
12
 
12
 
13
    !!non reentrant
13
    !!non reentrant
14
*/
14
*/
15
 
15
 
16
#include 
16
#include 
17
#include 
17
#include 
18
#include 
18
#include 
19
#include 
19
#include 
20
#include 
20
#include 
21
#include 
21
#include 
22
#include "kos32sys1.h"
22
#include "kos32sys1.h"
-
 
23
#include "sys/kos_io.h"
23
 
24
 
24
/* defined in newlib headers
25
/* defined in newlib headers
25
int	_EXFUN(mkdir,( const char *_path, mode_t __mode ));
26
int	_EXFUN(mkdir,( const char *_path, mode_t __mode ));
26
 
27
 
27
struct dirent {
28
struct dirent {
28
  char d_namlen;
29
  char d_namlen;
29
  char d_name[256];
30
  char d_name[256];
30
};
31
};
31
 
32
 
32
typedef struct
33
typedef struct
33
{
34
{
34
//    struct systree_info2 fileinfo;
35
//    struct systree_info2 fileinfo;
35
    struct dirent entry;
36
    struct dirent entry;
36
//    __u8 bdfeheader[0x20];
37
//    __u8 bdfeheader[0x20];
37
//    struct bdfe_item bdfebase;
38
//    struct bdfe_item bdfebase;
38
//    __u8 bdfename[264];
39
//    __u8 bdfename[264];
39
} DIR;
40
} DIR;
40
 
41
 
41
int     closedir(DIR *dirp);
42
int     closedir(DIR *dirp);
42
DIR *       opendir(const char *_dirname);
43
DIR *       opendir(const char *_dirname);
43
struct dirent * readdir(DIR *_dirp);
44
struct dirent * readdir(DIR *_dirp);
44
void      rewinddir(DIR *_dirp);
45
void      rewinddir(DIR *_dirp);
45
*/
46
*/
46
 
47
 
47
static int32_t lastread_block = -1; // non reentrant, must me -1ed in closedir, or will assert
48
static int32_t lastread_block = -1; // non reentrant, must me -1ed in closedir, or will assert
48
static uint32_t lastread_dir_size;
49
static uint32_t lastread_dir_size;
49
static DIR  lastread_dir;
50
static DIR  lastread_dir;
50
 
51
 
51
DIR *opendir(const char *_dirname)
52
DIR *opendir(const char *_dirname)
52
{
53
{
53
    assert(sizeof(struct fs_dirinfo) == 25);  // check struct aligment
54
    assert(sizeof(struct fs_dirinfo) == 25);  // check struct aligment
54
    assert(lastread_block == -1);
55
    assert(lastread_block == -1);
55
 
56
 
56
    struct fs_dirinfo di;
57
    struct fs_dirinfo di;
57
    struct fs_dirheader dhead;
58
    struct fs_dirheader dhead;
58
 
59
 
59
    memset(&di, 0, sizeof di);
60
    memset(&di, 0, sizeof di);
60
    di.ppath = (char*)_dirname;
61
    di.ppath = (char*)_dirname;
61
    di.retval = (uint32_t)&dhead;
62
    di.retval = (uint32_t)&dhead;
62
    int rc = sf_file(1, &di);  // read dir size
63
    int rc = sf_file(1, &di);  // read dir size
63
    if(rc) {
64
    if(rc) {
64
        fprintf(stderr, "Error reading dir size %s\n", _dirname);
65
        fprintf(stderr, "Error reading dir size %s\n", _dirname);
65
        errno = rc;
66
        errno = rc;
66
        return NULL;
67
        return NULL;
67
    }
68
    }
68
    lastread_dir_size = dhead.totl_blocks;
69
    lastread_dir_size = dhead.totl_blocks;
69
 
70
 
70
    lastread_dir.entry.d_namlen = strlen(_dirname);
71
    lastread_dir.entry.d_namlen = strlen(_dirname);
71
    assert (lastread_dir.entry.d_namlen < sizeof(lastread_dir.entry.d_name));
72
    assert (lastread_dir.entry.d_namlen < sizeof(lastread_dir.entry.d_name));
72
    strcpy(lastread_dir.entry.d_name, _dirname);
73
    strcpy(lastread_dir.entry.d_name, _dirname);
73
 
74
 
74
    return &lastread_dir;
75
    return &lastread_dir;
75
};
76
};
76
 
77
 
77
 
78
 
78
int closedir(DIR *dirp)
79
int closedir(DIR *dirp)
79
{
80
{
80
    assert (lastread_block != -1);  // was opened
81
    assert (lastread_block != -1);  // was opened
81
 
82
 
82
    if (!dirp || lastread_block == -1)
83
    if (!dirp || lastread_block == -1)
83
    {
84
    {
84
        errno = EBADF;
85
        errno = EBADF;
85
        return -1;
86
        return -1;
86
    }
87
    }
87
    lastread_block = -1;
88
    lastread_block = -1;
88
    lastread_dir_size = 0;
89
    lastread_dir_size = 0;
89
    lastread_dir.entry.d_namlen = 0;
90
    lastread_dir.entry.d_namlen = 0;
90
    lastread_dir.entry.d_name[0] = '\0';
91
    lastread_dir.entry.d_name[0] = '\0';
91
 
92
 
92
    return 0;
93
    return 0;
93
};
94
};
94
 
95
 
95
 
96
 
96
struct dirent* readdir(DIR *dirp)
97
struct dirent* readdir(DIR *dirp)
97
{
98
{
98
    assert (lastread_block != -1);  // was opened
99
    assert (lastread_block != -1);  // was opened
99
 
100
 
100
    if (!dirp || lastread_block == -1)
101
    if (!dirp || lastread_block == -1)
101
    {
102
    {
102
        errno = EBADF;
103
        errno = EBADF;
103
        return NULL;
104
        return NULL;
104
    }
105
    }
105
    struct fs_dirinfo di;
106
    struct fs_dirinfo di;
106
 
107
 
107
    assert (lastread_block != -1);  // was opened
108
    assert (lastread_block != -1);  // was opened
108
 
109
 
109
    char retdir[sizeof(struct fs_dirheader) + sizeof(struct fsBDFE)];   // 1 block w/cp866 encoding
110
    char retdir[sizeof(struct fs_dirheader) + sizeof(struct fsBDFE)];   // 1 block w/cp866 encoding
110
    struct fsBDFE *bdfe = (struct fsBDFE *)(retdir + sizeof(struct fs_dirheader));
111
    struct fsBDFE *bdfe = (struct fsBDFE *)(retdir + sizeof(struct fs_dirheader));
111
    memset(&di, 0, sizeof di);
112
    memset(&di, 0, sizeof di);
112
    di.ppath = dirp->entry.d_name;
113
    di.ppath = dirp->entry.d_name;
113
    di.retval = (uint32_t)retdir;
114
    di.retval = (uint32_t)retdir;
114
    di.start = lastread_block;
115
    di.start = lastread_block;
115
    di.size = 1;
116
    di.size = 1;
116
 
117
 
117
    int rc = sf_file(1, &di);  // read dir
118
    int rc = sf_file(1, &di);  // read dir
118
    if(rc) {
119
    if(rc) {
119
        fprintf(stderr, "Error %d reading dir item %s\n", rc, dirp->entry.d_name);
120
        fprintf(stderr, "Error %d reading dir item %s\n", rc, dirp->entry.d_name);
120
        errno = rc;
121
        errno = rc;
121
        return NULL;
122
        return NULL;
122
    }
123
    }
123
 
124
 
124
    static struct dirent ent;
125
    static struct dirent ent;
125
    ent.d_namlen = strlen(bdfe->fname);
126
    ent.d_namlen = strlen(bdfe->fname);
126
    assert (ent.d_namlen < sizeof(ent.d_name));
127
    assert (ent.d_namlen < sizeof(ent.d_name));
127
    strcpy(ent.d_name, bdfe->fname);
128
    strcpy(ent.d_name, bdfe->fname);
128
    lastread_block++;
129
    lastread_block++;
129
 
130
 
130
    return &ent;
131
    return &ent;
131
};
132
};
132
 
133
 
133
 
134
 
134
void rewinddir(DIR *dirp)
135
void rewinddir(DIR *dirp)
135
{
136
{
136
    if (!dirp || lastread_block == -1)
137
    if (!dirp || lastread_block == -1)
137
    {
138
    {
138
        return;
139
        return;
139
    }
140
    }
140
 
141
 
141
    lastread_block = 0;
142
    lastread_block = 0;
142
}
143
}
143
 
144
 
144
 
145
 
145
int	mkdir(const char *_path, mode_t m)
146
int	mkdir(const char *_path, mode_t m)
146
{
147
{
147
    char   namebuffer[1050]; // need for save data after di!!!
148
    char   namebuffer[1050]; // need for save data after di!!!
148
    struct fs_dirinfo *di = (struct fs_dirinfo *)namebuffer;
149
    struct fs_dirinfo *di = (struct fs_dirinfo *)namebuffer;
149
 
150
 
150
debug_board_printf("mkdir start (%s)\n", _path);
151
//debug_board_printf("mkdir start (%s)\n", _path);
151
    memset(di, 0, sizeof(struct fs_dirinfo));
152
    memset(di, 0, sizeof(struct fs_dirinfo));
152
    //di.ppath = (char*)_path;  // dont work with 70.9
153
    //di.ppath = (char*)_path;  // dont work with 70.9
153
    strcpy(di->path, _path);
154
    strcpy(di->path, _path);
154
 
155
 
155
    int rc = sf_file(9, di);  // creat dir
156
    int rc = sf_file(9, di);  // creat dir
156
    if(rc) {
157
    if(rc) {
157
        fprintf(stderr, "Error %d creating dir item %s\n", rc, _path);
158
        fprintf(stderr, "Error %d creating dir item %s\n", rc, _path);
158
        errno = rc;
159
        errno = rc;
159
        return -1;
160
        return -1;
160
    }
161
    }
161
 
162
 
162
debug_board_printf("mkdir end (%s)\n", _path);
163
//debug_board_printf("mkdir end (%s)\n", _path);
163
    return 0;
164
    return 0;
164
}
165
}
165
 
166
 
166
////////////////////////////////////////////////////////////////////////////////////////
167
////////////////////////////////////////////////////////////////////////////////////////
167
void __attribute__ ((noinline)) debug_board_write_str(const char* str){
168
void __attribute__ ((noinline)) debug_board_write_str(const char* str){
168
  while(*str)
169
  while(*str)
169
    debug_board_write_byte(*str++);
170
    debug_board_write_byte(*str++);
170
}
171
}
171
 
172
 
172
void __attribute__ ((noinline)) debug_board_printf(const char *format,...)
173
void __attribute__ ((noinline)) debug_board_printf(const char *format,...)
173
{
174
{
174
        va_list ap;
175
        va_list ap;
175
        char log_board[300];
176
        char log_board[300];
176
 
177
 
177
        va_start (ap, format);
178
        va_start (ap, format);
178
        vsnprintf(log_board, sizeof log_board, format, ap);
179
        vsnprintf(log_board, sizeof log_board, format, ap);
179
        va_end(ap);
180
        va_end(ap);
180
        debug_board_write_str(log_board);
181
        debug_board_write_str(log_board);
181
}
182
}
182
 
183
 
183
__attribute__ ((noinline)) void trap(int n)
184
__attribute__ ((noinline)) void trap(int n)
184
{
185
{
185
    // nothing todo, just see n in debugger. use "bp trap" command
186
    // nothing todo, just see n in debugger. use "bp trap" command
186
    __asm__ __volatile__(
187
    __asm__ __volatile__(
187
    "nop"
188
    "nop"
188
    :
189
    :
189
    :"a"(n));
190
    :"a"(n));
190
}
191
}
191
 
192
 
192
 
193
 
193
 
194
 
194
/* tested example
195
/* tested example
195
void* read_folderdata(char* name)
196
void* read_folderdata(char* name)
196
{
197
{
197
    struct fs_dirinfo di;
198
    struct fs_dirinfo di;
198
    struct fs_dirheader dhead;
199
    struct fs_dirheader dhead;
199
    assert(sizeof di == 25);
200
    assert(sizeof di == 25);
200
 
201
 
201
    memset(&di, 0, sizeof di);
202
    memset(&di, 0, sizeof di);
202
    di.ppath = name;
203
    di.ppath = name;
203
    di.retval = (uint32_t)&dhead;
204
    di.retval = (uint32_t)&dhead;
204
    int rc = sf_file(1, &di);  // read dir size
205
    int rc = sf_file(1, &di);  // read dir size
205
    if(rc) {
206
    if(rc) {
206
        debug_board_printf("Error reading dir size %s", name);
207
        debug_board_printf("Error reading dir size %s", name);
207
        exit(1);
208
        exit(1);
208
    }
209
    }
209
    di.size = dhead.totl_blocks;
210
    di.size = dhead.totl_blocks;
210
 
211
 
211
    char *retdir = malloc(sizeof dhead + dhead.totl_blocks * sizeof(struct fsBDFE));
212
    char *retdir = malloc(sizeof dhead + dhead.totl_blocks * sizeof(struct fsBDFE));
212
    if(!retdir) {
213
    if(!retdir) {
213
        debug_board_printf("No memory for dir %s", name);
214
        debug_board_printf("No memory for dir %s", name);
214
        exit(1);
215
        exit(1);
215
    }
216
    }
216
    di.retval = (uint32_t)retdir;
217
    di.retval = (uint32_t)retdir;
217
    rc = sf_file(1, &di);  // read dir
218
    rc = sf_file(1, &di);  // read dir
218
    if(rc) {
219
    if(rc) {
219
        debug_board_printf("Error 2 reading dir size %s", name);
220
        debug_board_printf("Error 2 reading dir size %s", name);
220
        exit(1);
221
        exit(1);
221
    }
222
    }
222
 
223
 
223
    // manual clear mark flag (random junk in fname free space)
224
    // manual clear mark flag (random junk in fname free space)
224
    int i;
225
    int i;
225
    for (i = 0; i < dhead.totl_blocks; i++)
226
    for (i = 0; i < dhead.totl_blocks; i++)
226
        ((struct fsBDFE*)(retdir+32))[i].fname[259] = 0;
227
        ((struct fsBDFE*)(retdir+32))[i].fname[259] = 0;
227
 
228
 
228
    debug_board_printf("Loaded dir [%s] etnries %d,\n first file [%s]\n", name, ((struct fs_dirheader*)(retdir))->curn_blocks, ((struct fsBDFE*)(retdir+32))->fname);
229
    debug_board_printf("Loaded dir [%s] etnries %d,\n first file [%s]\n", name, ((struct fs_dirheader*)(retdir))->curn_blocks, ((struct fsBDFE*)(retdir+32))->fname);
229
 
230
 
230
    return retdir;
231
    return retdir;
231
}
232
}
232
*/
233
*/
-
 
234
 
-
 
235
// while not in newlib
-
 
236
int set_fileinfo(const char *path, fileinfo_t *info)
-
 
237
{
-
 
238
    int retval;
-
 
239
 
-
 
240
    __asm__ __volatile__ (
-
 
241
    "pushl $0 \n\t"
-
 
242
    "pushl $0 \n\t"
-
 
243
    "movl %1, 1(%%esp) \n\t"
-
 
244
    "pushl %%ebx \n\t"
-
 
245
    "pushl $0 \n\t"
-
 
246
    "pushl $0 \n\t"
-
 
247
    "pushl $0 \n\t"
-
 
248
    "pushl $6 \n\t"
-
 
249
    "movl %%esp, %%ebx \n\t"
-
 
250
    "movl $70, %%eax \n\t"
-
 
251
    "int $0x40 \n\t"
-
 
252
    "addl $28, %%esp \n\t"
-
 
253
    :"=a" (retval)
-
 
254
    :"r" (path), "b" (info));
-
 
255
   return retval;
-
 
256
};
-
 
257