kolibrios/contrib/sdk/sources/newlib/libc/dirent/opendir.c
turbocat 2287b1e29b newlibc:
Added new functions:
- dir.h: mkdir, rmdir, chdir
- dirent.h:  alphasort, opendir, closedir, readdir, rewinddir, seekdir, telldir, scandir

git-svn-id: svn://kolibrios.org@8759 a494cfbc-eb01-0410-851d-a64ba20cac60
2021-06-02 00:58:56 +00:00

54 lines
1.3 KiB
C

/* 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;
}