From 6e3276cb4baf3642532e192238fefe0c0baf2c40 Mon Sep 17 00:00:00 2001 From: turbocat Date: Fri, 12 Jan 2024 23:01:16 +0000 Subject: [PATCH] NewLib: - Added basic support for dirent.h; - Added memory.h for compatibility; - Added ftruncate() function; - Fixed date and time structures in ksys_file_info_t. git-svn-id: svn://kolibrios.org@9954 a494cfbc-eb01-0410-851d-a64ba20cac60 --- contrib/sdk/sources/newlib/libc/Makefile | 14 ++- contrib/sdk/sources/newlib/libc/Tupfile.lua | 10 +++ .../sdk/sources/newlib/libc/include/dirent.h | 41 +++++++++ .../sdk/sources/newlib/libc/include/memory.h | 11 +++ .../sources/newlib/libc/include/sys/ksys.h | 86 +++++++++++++++---- .../sources/newlib/libc/include/sys/unistd.h | 8 +- .../sdk/sources/newlib/libc/sys/ftruncate.c | 24 ++++++ contrib/sdk/sources/newlib/libc/sys/stat.c | 5 +- 8 files changed, 175 insertions(+), 24 deletions(-) create mode 100644 contrib/sdk/sources/newlib/libc/include/dirent.h create mode 100644 contrib/sdk/sources/newlib/libc/include/memory.h create mode 100644 contrib/sdk/sources/newlib/libc/sys/ftruncate.c diff --git a/contrib/sdk/sources/newlib/libc/Makefile b/contrib/sdk/sources/newlib/libc/Makefile index 25e7590b2e..d5937c6dfd 100644 --- a/contrib/sdk/sources/newlib/libc/Makefile +++ b/contrib/sdk/sources/newlib/libc/Makefile @@ -133,6 +133,7 @@ CORE_SRCS:= \ sys/unlink.c \ sys/write.c \ sys/io_alloc.S \ + sys/ftruncate.c \ time/asctime.c \ time/asctime_r.c \ time/clock.c \ @@ -508,6 +509,13 @@ MATH_SRCS = e_acos.c e_acosh.c e_asin.c e_atan2.c e_atanh.c e_cosh.c e_exp.c e_ f_log10.S f_log10f.S f_logf.S f_tan.S f_tanf.S +POSIX_SRCS = opendir.c \ + closedir.c \ + readdir.c \ + seekdir.c \ + telldir.c \ + rewinddir.c + STATIC_OBJS = $(patsubst %.S, %.o, $(patsubst %.c, %.o, $(STATIC_SRCS))) LIBCRT_OBJS = $(patsubst %.S, %.o, $(patsubst %.c, %.o, $(LIBCRT_SRCS))) @@ -531,6 +539,7 @@ STDLIB_OBJS = $(patsubst %.S, stdlib/%.o, $(patsubst %.asm, stdlib/%.o,\ MATH_OBJS = $(patsubst %.S, math/%.o, $(patsubst %.asm, math/%.o,\ $(patsubst %.c, math/%.o, $(MATH_SRCS)))) +POSIX_OBJS = $(patsubst %.c, posix/%.o, $(POSIX_SRCS)) PRINTF_OBJS= stdio/vfprintf.o \ stdio/vfiprintf.o \ @@ -563,7 +572,8 @@ LIB_SRCS+= \ $(CORE_SRCS) \ $(STDIO_SRCS) \ $(STRING_SRCS) \ - $(STDLIB_SRCS) + $(STDLIB_SRCS) \ + $(POSIX_SRCS) LIB_OBJS+= \ $(CORE_OBJS) \ @@ -572,7 +582,7 @@ LIB_OBJS+= \ $(STDIO_OBJS) \ $(PRINTF_OBJS) \ $(MATH_OBJS) \ - $(DIRENT_OBJS) + $(POSIX_OBJS) LIB_OBJS+= time/wcsftime.o diff --git a/contrib/sdk/sources/newlib/libc/Tupfile.lua b/contrib/sdk/sources/newlib/libc/Tupfile.lua index 0b6735e032..09586c85e6 100644 --- a/contrib/sdk/sources/newlib/libc/Tupfile.lua +++ b/contrib/sdk/sources/newlib/libc/Tupfile.lua @@ -116,6 +116,7 @@ CORE_SRCS = { "sys/unlink.c", "sys/write.c", "sys/io_alloc.S", + "sys/ftruncate.c", "time/asctime.c", "time/asctime_r.c", "time/clock.c", @@ -453,6 +454,14 @@ STDIO_SRCS = { "wscanf.c", "wsetup.c" } +POSIX_SRCS = { + "opendir.c", + "closedir.c", + "readdir.c", + "seekdir.c", + "telldir.c", + "rewinddir.c" +} MATH_SRCS = { "e_acos.c", "e_acosh.c", "e_asin.c", "e_atan2.c", "e_atanh.c", "e_cosh.c", "e_exp.c", "e_fmod.c", @@ -505,6 +514,7 @@ LIB_SRCS += CORE_SRCS LIB_SRCS += prepend("stdio/", STDIO_SRCS) LIB_SRCS += prepend("string/", STRING_SRCS) LIB_SRCS += prepend("stdlib/", STDLIB_SRCS) +LIB_SRCS += prepend("posix/", POSIX_SRCS) LIB_SRCS += prepend("math/", MATH_SRCS) ALL_OBJS = {} diff --git a/contrib/sdk/sources/newlib/libc/include/dirent.h b/contrib/sdk/sources/newlib/libc/include/dirent.h new file mode 100644 index 0000000000..56c2fb39c0 --- /dev/null +++ b/contrib/sdk/sources/newlib/libc/include/dirent.h @@ -0,0 +1,41 @@ +/* + * Copyright (C) KolibriOS team 2024. All rights reserved. + * Distributed under terms of the GNU General Public License +*/ + +#ifndef _DIRENT_H_ +#define _DIRENT_H_ + +#include +#include +#include + +_BEGIN_STD_C + +#define DT_UNKNOWN 0 +#define DT_DIR 4 +#define DT_REG 8 + +struct dirent { + ino_t d_ino; + unsigned d_type; + char d_name[KSYS_FNAME_UTF8_SIZE]; +}; + +typedef struct { + char* path; + uint32_t pos; + struct dirent last_entry; +} DIR; + +DIR* _DEFUN(opendir, (name), const char *name); +int _DEFUN(closedir, (dirp), register DIR *dirp); +void _DEFUN(seekdir, (dirp, loc), DIR *dirp _AND long loc); +void _DEFUN(rewinddir, (dirp), DIR *dirp); +long _DEFUN(telldir, (dirp), DIR *dirp); + +struct dirent *_DEFUN(readdir, (dirp), register DIR *dirp); + +_END_STD_C + +#endif /* _DIRENT_H_ */ diff --git a/contrib/sdk/sources/newlib/libc/include/memory.h b/contrib/sdk/sources/newlib/libc/include/memory.h new file mode 100644 index 0000000000..58ab006ee7 --- /dev/null +++ b/contrib/sdk/sources/newlib/libc/include/memory.h @@ -0,0 +1,11 @@ +/* + * Copyright (C) KolibriOS team 2024. All rights reserved. + * Distributed under terms of the GNU General Public License +*/ + +#ifndef _MEMORY_H_ +#define _MEMORY_H_ + +#include + +#endif // _MEMORY_H_ diff --git a/contrib/sdk/sources/newlib/libc/include/sys/ksys.h b/contrib/sdk/sources/newlib/libc/include/sys/ksys.h index ad9555aa08..d1593194db 100644 --- a/contrib/sdk/sources/newlib/libc/include/sys/ksys.h +++ b/contrib/sdk/sources/newlib/libc/include/sys/ksys.h @@ -48,7 +48,7 @@ typedef union { uint8_t sec; uint8_t _zero; }; -} ksys_time_t; +} ksys_time_bcd_t; typedef union { uint32_t val; @@ -58,7 +58,7 @@ typedef union { uint8_t day; uint8_t _zero; }; -} ksys_date_t; +} ksys_date_bcd_t; typedef union { uint32_t val; @@ -93,6 +93,7 @@ typedef struct { }; union { uint32_t flags; + uint32_t enc_name; char* args; }; }; @@ -113,19 +114,47 @@ typedef struct { uint32_t rw_bytes; } ksys_file_status_t; +#define KSYS_FNAME_UTF8_SIZE 520 +#define KSYS_FNAME_CP866_SIZE 264 + +typedef struct { + uint8_t sec; + uint8_t min; + uint8_t hour; + uint8_t _zero; +} ksys_ftime_t; + +typedef struct { + uint8_t day; + uint8_t month; + uint16_t year; +} ksys_fdate_t; + typedef struct { uint32_t attr; uint32_t name_enc; - ksys_time_t ctime; - ksys_date_t cdate; - ksys_time_t atime; - ksys_date_t adate; - ksys_time_t mtime; - ksys_date_t mdate; + ksys_ftime_t ctime; + ksys_fdate_t cdate; + ksys_ftime_t atime; + ksys_fdate_t adate; + ksys_ftime_t mtime; + ksys_fdate_t mdate; uint64_t size; char name[0]; } ksys_file_info_t; +typedef struct { + uint32_t version; + uint32_t blocks; + uint32_t files; + uint8_t __reserved[20]; +} ksys_dir_entry_header_t; + +typedef struct { + ksys_dir_entry_header_t header; + ksys_file_info_t info; +} ksys_dir_entry_t; + #define KSYS_THREAD_INFO_SIZE 1024 typedef union { @@ -424,15 +453,15 @@ KOSAPI ksys_oskey_t _ksys_get_key(void) /*==================== Function 3 - get system time. ===================*/ -KOSAPI ksys_time_t _ksys_get_time(void) +KOSAPI ksys_time_bcd_t _ksys_get_time(void) { - ksys_time_t c_time; + ksys_time_bcd_t bcd_time; asm_inline( "int $0x40" - : "=a"(c_time) + : "=a"(bcd_time) : "a"(3) : "memory"); - return c_time; + return bcd_time; } /*=================== Function 4 - draw text string. ===================*/ @@ -866,14 +895,14 @@ KOSAPI uint64_t _ksys_get_ns_count(void) /*=================== Function 29 - get system date. ===================*/ -KOSAPI ksys_date_t _ksys_get_date(void) +KOSAPI ksys_date_bcd_t _ksys_get_date(void) { - ksys_date_t val; + ksys_date_bcd_t bcd_date; asm_inline( "int $0x40" - : "=a"(val) + : "=a"(bcd_date) : "a"(29)); - return val; + return bcd_date; } /*===========+ Function 30 - work with the current folder.==============*/ @@ -1528,6 +1557,20 @@ KOSAPI ksys_file_status_t _ksys_file_read(const char* name, uint64_t offset, uin return _ksys_file(&f); } +KOSAPI ksys_file_status_t _ksys_file_read_dir(const char* name, uint32_t offset, + uint32_t enc, uint32_t blocks, void* buff) +{ + ksys_file_t f; + f.func_num = KSYS_FILE_READ_DIR; + f.offset = offset; + f.enc_name = enc; + f.data_size = blocks; + f.data = buff; + f.zero = 0; + f.path_ptr = (char*)name; + return _ksys_file(&f); +} + /*===================== Function 70, subfunction 2 =====================*/ /*============ Create/rewrite file with long names support. ============*/ @@ -1574,6 +1617,15 @@ KOSAPI int _ksys_file_set_size(const char* name, uint64_t size) /*========== Function 70, subfunction 5 - get information on file/folder. =====*/ +enum KSYS_FILE_ATTR { + KSYS_FILE_ATTR_RO = 0x1, + KSYS_FILE_ATTR_HIDDEN = 0x2, + KSYS_FILE_ATTR_SYS = 0x4, + KSYS_FILE_ATTR_VOL_LABEL = 0x8, + KSYS_FILE_ATTR_DIR = 0x10, + KSYS_FILE_ATTR_ARCHIVE = 0x20 +}; + KOSAPI int _ksys_file_info(const char* name, ksys_file_info_t* info) { ksys_file_t f; @@ -1624,6 +1676,8 @@ KOSAPI int _ksys_file_delete(const char* path) return _ksys_file(&f).status; } +#define _ksys_rmdir(x) _ksys_file_delete(x) + /*============= Function 70, subfunction 9 - create folder. =============*/ KOSAPI int _ksys_mkdir(const char* path) diff --git a/contrib/sdk/sources/newlib/libc/include/sys/unistd.h b/contrib/sdk/sources/newlib/libc/include/sys/unistd.h index e4cda592be..7a38dd5d3d 100644 --- a/contrib/sdk/sources/newlib/libc/include/sys/unistd.h +++ b/contrib/sdk/sources/newlib/libc/include/sys/unistd.h @@ -10,10 +10,11 @@ extern "C" { #define __need_ptrdiff_t #include #include +#include #include #include -extern char **environ; +//extern char **environ; void _EXFUN(_exit, (int __status ) _ATTRIBUTE ((__noreturn__))); @@ -165,7 +166,7 @@ _READ_WRITE_RETURN_TYPE _EXFUN(read, (int __fd, void *__buf, size_t __nbyte )); int _EXFUN(rresvport, (int *__alport)); int _EXFUN(revoke, (char *__path)); #endif -int _EXFUN(rmdir, (const char *__path )); +#define rmdir(__path) _ksys_rmdir(__path) #if __BSD_VISIBLE int _EXFUN(ruserok, (const char *rhost, int superuser, const char *ruser, const char *luser)); #endif @@ -244,11 +245,12 @@ int _EXFUN(_execve, (const char *__path, char * const __argv[], char * const #if defined(__CYGWIN__) || defined(__rtems__) || defined(__aarch64__) || defined (__arm__) || defined(__sh__) || defined(__SPU__) #if !defined(__INSIDE_CYGWIN__) -int _EXFUN(ftruncate, (int __fd, off_t __length)); int _EXFUN(truncate, (const char *, off_t __length)); #endif #endif +int _EXFUN(ftruncate, (int __fd, off_t __length)); + #if defined(__CYGWIN__) || defined(__rtems__) int _EXFUN(getdtablesize, (void)); int _EXFUN(setdtablesize, (int)); diff --git a/contrib/sdk/sources/newlib/libc/sys/ftruncate.c b/contrib/sdk/sources/newlib/libc/sys/ftruncate.c new file mode 100644 index 0000000000..a25e349538 --- /dev/null +++ b/contrib/sdk/sources/newlib/libc/sys/ftruncate.c @@ -0,0 +1,24 @@ + +#include +#include +#include +#include "glue.h" +#include "io.h" + +int +_DEFUN (ftruncate, (fd, len), + int fd _AND + off_t len) +{ + __io_handle *ioh; + if ((fd < 0) || (fd >=64)) + { + errno = EBADF; + return (-1); + } + ioh = &__io_tab[fd]; + if (_ksys_file_set_size(ioh->name, len)) + return (-1); + + return 0; +} diff --git a/contrib/sdk/sources/newlib/libc/sys/stat.c b/contrib/sdk/sources/newlib/libc/sys/stat.c index 658bfca966..014e61e054 100644 --- a/contrib/sdk/sources/newlib/libc/sys/stat.c +++ b/contrib/sdk/sources/newlib/libc/sys/stat.c @@ -27,7 +27,6 @@ _DEFUN (stat, (path, buf), const char *path _AND struct stat *buf) { - ksys_file_info_t info; struct tm time; @@ -41,11 +40,11 @@ _DEFUN (stat, (path, buf), buf->st_size = info.size; - if (info.attr & 0x10) + if (info.attr & (KSYS_FILE_ATTR_DIR | KSYS_FILE_ATTR_VOL_LABEL)) buf->st_mode = S_IFDIR; else { - if (info.attr & 0x07) + if (info.attr & (KSYS_FILE_ATTR_SYS | KSYS_FILE_ATTR_HIDDEN | KSYS_FILE_ATTR_RO)) buf->st_mode = S_IFREG|S_IRUSR|S_IXUSR; else buf->st_mode = S_IFREG|S_IRUSR|S_IWUSR|S_IXUSR;