/contrib/sdk/sources/newlib/libc/dirent/alphasort.c |
---|
0,0 → 1,7 |
#include <string.h> |
#include <dirent.h> |
int alphasort(const struct dirent **a, const struct dirent **b) |
{ |
return strcoll((*a)->d_name, (*b)->d_name); |
} |
/contrib/sdk/sources/newlib/libc/dirent/closedir.c |
---|
0,0 → 1,14 |
/* Copyright (C) 2021 Logaev Maxim (turbocat2001), GPLv2 */ |
#include <sys/dirent.h> |
#include <stdlib.h> |
int closedir(DIR *dir){ |
if(dir == NULL){ |
return -1; |
}else{ |
free(dir->objs); |
free(dir); |
return 0; |
} |
} |
/contrib/sdk/sources/newlib/libc/dirent/dir.c |
---|
0,0 → 1,16 |
/* Copyright (C) 2021 Logaev Maxim (turbocat2001), GPLv2 */ |
#include "ksys_fs.h" |
int rmdir(const char* dir){ |
return _ksys_file_delete(dir); |
} |
int mkdir(const char* dir, unsigned fake_mode){ |
return _ksys_mkdir(dir); |
} |
int chdir(char* dir){ |
_ksys_setcwd(dir); |
return 0; |
} |
/contrib/sdk/sources/newlib/libc/dirent/ksys_fs.h |
---|
0,0 → 1,110 |
/* Copyright (C) 2021 Logaev Maxim (turbocat2001), GPLv2 */ |
#ifndef _KSYS_FS_H_ |
#define _KSYS_FS_H_ |
#include <stdint.h> |
#include <stddef.h> |
#define asm_inline __asm__ __volatile__ |
#pragma pack(push,1) |
typedef struct{ |
unsigned p00; |
union{ |
uint64_t p04; |
struct { |
unsigned p04dw; |
unsigned p08dw; |
}; |
}; |
unsigned p12; |
union { |
unsigned p16; |
const char *new_name; |
void *bdfe; |
void *buf16; |
const void *cbuf16; |
}; |
char p20; |
const char *p21; |
}ksys70_t; |
typedef struct { |
unsigned attributes; |
unsigned name_cp; |
char creation_time[4]; |
char creation_date[4]; |
char last_access_time[4]; |
char last_access_date[4]; |
char last_modification_time[4]; |
char last_modification_date[4]; |
unsigned long long size; |
char name[0]; |
}ksys_bdfe_t; |
#pragma pack(pop) |
static inline |
int _ksys_work_files(const ksys70_t *k) |
{ |
int status; |
asm_inline( |
"int $0x40" |
:"=a"(status) |
:"a"(70), "b"(k) |
:"memory" |
); |
return status; |
} |
static inline |
int _ksys_file_delete(const char *name) |
{ |
ksys70_t k; |
k.p00 = 8; |
k.p20 = 0; |
k.p21 = name; |
return _ksys_work_files(&k); |
} |
static inline |
int _ksys_mkdir(const char *path) |
{ |
ksys70_t dir_opt; |
dir_opt.p00 = 9; |
dir_opt.p21 = path; |
return _ksys_work_files(&dir_opt); |
} |
static inline |
void _ksys_setcwd(char* dir){ |
asm_inline( |
"int $0x40" |
::"a"(30), "b"(1), "c"(dir) |
); |
} |
static inline |
void* _ksys_alloc(size_t size){ |
void *val; |
asm_inline( |
"int $0x40" |
:"=a"(val) |
:"a"(68),"b"(12),"c"(size) |
); |
return val; |
} |
static inline |
int _ksys_free(void *mem) |
{ |
int val; |
asm_inline( |
"int $0x40" |
:"=a"(val) |
:"a"(68),"b"(13),"c"(mem) |
); |
return val; |
} |
#endif |
/contrib/sdk/sources/newlib/libc/dirent/opendir.c |
---|
0,0 → 1,53 |
/* Copyright (C) 2021 Logaev Maxim (turbocat2001), GPLv2 */ |
#include <sys/dirent.h> |
#include "ksys_fs.h" |
#include <errno.h> |
#include <stdlib.h> |
#include <string.h> |
#define CHECK_DIR_ERR() if(_ksys_work_files(&inf)){ \ |
free((void*)inf.p16); \ |
errno = ENOTDIR; \ |
return NULL; \ |
} |
DIR* opendir(const char* path) |
{ |
DIR* list = malloc(sizeof(DIR)); |
if(list==NULL){ |
errno = ENOMEM; |
return NULL; |
} |
list->pos=0; |
unsigned num_of_file=0; |
static ksys70_t inf; |
inf.p00 = 1; |
inf.p04 = 0; |
inf.p12 = 2; |
inf.p16 = (unsigned) malloc(32+inf.p12*560); |
inf.p20 = 0; |
inf.p21 = (char*)path; |
CHECK_DIR_ERR(); |
num_of_file = *(unsigned*)(inf.p16+8); |
inf.p12 = num_of_file; |
free((void*)inf.p16); |
inf.p16 = (unsigned)_ksys_alloc(32+inf.p12*560); |
list->objs = (struct dirent*) malloc(num_of_file*sizeof(struct dirent)); |
CHECK_DIR_ERR(); |
for(int i=0; i<num_of_file; i++){ |
list->objs[i].d_ino = i; |
list->objs[i].d_type = *(unsigned*)(inf.p16+32+(264+40)*i); |
strcpy(list->objs[i].d_name,(char*)(inf.p16+32+40+(264+40)*i)); |
} |
list->num_objs = num_of_file; |
return list; |
} |
/contrib/sdk/sources/newlib/libc/dirent/readdir.c |
---|
0,0 → 1,14 |
/* Copyright (C) 2021 Logaev Maxim (turbocat2001), GPLv2 */ |
#include <sys/dirent.h> |
#include <stdlib.h> |
struct dirent* readdir(DIR *dir) |
{ |
if(dir->num_objs>dir->pos){ |
dir->pos++; |
return &dir->objs[dir->pos-1]; |
}else{ |
return NULL; |
} |
} |
/contrib/sdk/sources/newlib/libc/dirent/rewinddir.c |
---|
0,0 → 1,9 |
/* Copyright (C) 2021 Logaev Maxim (turbocat2001), GPLv2 */ |
#include <sys/dirent.h> |
void rewinddir(DIR *dir){ |
if(dir!=NULL){ |
dir->pos=0; |
} |
} |
/contrib/sdk/sources/newlib/libc/dirent/scandir.c |
---|
0,0 → 1,46 |
#include <dirent.h> |
#include <string.h> |
#include <stdlib.h> |
#include <errno.h> |
#include <stddef.h> |
#define SIZE_MAX 256 |
int scandir(const char *path, struct dirent ***res, |
int (*sel)(const struct dirent *), |
int (*cmp)(const struct dirent **, const struct dirent **)) |
{ |
DIR *d = opendir(path); |
struct dirent *de, **names=0, **tmp; |
size_t cnt=0, len=0; |
int old_errno = errno; |
if (!d) return -1; |
while ((errno=0), (de = readdir(d))) { |
if (sel && !sel(de)) continue; |
if (cnt >= len) { |
len = 2*len+1; |
if (len > SIZE_MAX/sizeof *names) break; |
tmp = realloc(names, len * sizeof *names); |
if (!tmp) break; |
names = tmp; |
} |
names[cnt] = malloc(sizeof(struct dirent)); |
if (!names[cnt]) break; |
memcpy(names[cnt++], de, sizeof(struct dirent)); |
} |
closedir(d); |
if (errno) { |
if (names) while (cnt-->0) free(names[cnt]); |
free(names); |
return -1; |
} |
errno = old_errno; |
if (cmp) qsort(names, cnt, sizeof *names, (int (*)(const void *, const void *))cmp); |
*res = names; |
return cnt; |
} |
/contrib/sdk/sources/newlib/libc/dirent/seekdir.c |
---|
0,0 → 1,12 |
/* Copyright (C) 2021 Logaev Maxim (turbocat2001), GPLv2 */ |
#include <sys/dirent.h> |
void seekdir(DIR *dir, unsigned pos) |
{ |
if(dir==NULL || pos>dir->num_objs){ |
return; |
} |
dir->pos=pos; |
return; |
} |
/contrib/sdk/sources/newlib/libc/dirent/telldir.c |
---|
0,0 → 1,12 |
/* Copyright (C) 2021 Logaev Maxim (turbocat2001), GPLv2 */ |
#include <sys/dirent.h> |
unsigned telldir(DIR *dir) |
{ |
if(dir!=NULL){ |
return dir->pos; |
}else{ |
return 0; |
} |
} |