From aae3ae86cc0073f59fe2685b01b017a9ce0374d1 Mon Sep 17 00:00:00 2001 From: turbocat Date: Thu, 7 Mar 2024 20:39:33 +0000 Subject: [PATCH] Newlib: - Added missing basename.c; - Added original dirname.c; - Added getcwd (extension of POSIX.1 standard, as in the original NewLib). git-svn-id: svn://kolibrios.org@9989 a494cfbc-eb01-0410-851d-a64ba20cac60 --- contrib/sdk/sources/newlib/libc/Makefile | 18 +++-- contrib/sdk/sources/newlib/libc/Tupfile.lua | 12 +++- .../sdk/sources/newlib/libc/string/dirname.c | 67 ------------------- contrib/sdk/sources/newlib/libc/sys/getcwd.c | 17 ----- .../sdk/sources/newlib/libc/unix/basename.c | 28 ++++++++ .../sdk/sources/newlib/libc/unix/dirname.c | 32 +++++++++ contrib/sdk/sources/newlib/libc/unix/getcwd.c | 53 +++++++++++++++ 7 files changed, 134 insertions(+), 93 deletions(-) delete mode 100644 contrib/sdk/sources/newlib/libc/string/dirname.c delete mode 100644 contrib/sdk/sources/newlib/libc/sys/getcwd.c create mode 100644 contrib/sdk/sources/newlib/libc/unix/basename.c create mode 100644 contrib/sdk/sources/newlib/libc/unix/dirname.c create mode 100644 contrib/sdk/sources/newlib/libc/unix/getcwd.c diff --git a/contrib/sdk/sources/newlib/libc/Makefile b/contrib/sdk/sources/newlib/libc/Makefile index 77208b6d4f..5c04d1d2d5 100644 --- a/contrib/sdk/sources/newlib/libc/Makefile +++ b/contrib/sdk/sources/newlib/libc/Makefile @@ -116,8 +116,7 @@ CORE_SRCS:= \ sys/clock_gettime.c \ sys/close.c \ sys/conio.c \ - sys/chdir.c \ - sys/getcwd.c \ + sys/chdir.c \ sys/errno.c \ sys/fstat.c \ sys/gettod.c \ @@ -326,8 +325,7 @@ STRING_SRCS= \ wmemcmp.c \ wmemcpy.c \ wmemmove.c \ - wmemset.c \ - dirname.c + wmemset.c STDIO_SRCS= \ @@ -517,6 +515,10 @@ POSIX_SRCS = opendir.c \ telldir.c \ rewinddir.c +UNIX_SRCS = basename.c \ + dirname.c \ + getcwd.c + STATIC_OBJS = $(patsubst %.S, %.o, $(patsubst %.c, %.o, $(STATIC_SRCS))) LIBCRT_OBJS = $(patsubst %.S, %.o, $(patsubst %.c, %.o, $(LIBCRT_SRCS))) @@ -542,6 +544,8 @@ MATH_OBJS = $(patsubst %.S, math/%.o, $(patsubst %.asm, math/%.o,\ POSIX_OBJS = $(patsubst %.c, posix/%.o, $(POSIX_SRCS)) +UNIX_OBJS = $(patsubst %.c, unix/%.o, $(UNIX_SRCS)) + PRINTF_OBJS= stdio/vfprintf.o \ stdio/vfiprintf.o \ stdio/svfprintf.o \ @@ -574,7 +578,8 @@ LIB_SRCS+= \ $(STDIO_SRCS) \ $(STRING_SRCS) \ $(STDLIB_SRCS) \ - $(POSIX_SRCS) + $(POSIX_SRCS) \ + $(UNIX_SRCS) LIB_OBJS+= \ $(CORE_OBJS) \ @@ -583,7 +588,8 @@ LIB_OBJS+= \ $(STDIO_OBJS) \ $(PRINTF_OBJS) \ $(MATH_OBJS) \ - $(POSIX_OBJS) + $(POSIX_OBJS) \ + $(UNIX_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 7d0a4d57dc..375803c9ef 100644 --- a/contrib/sdk/sources/newlib/libc/Tupfile.lua +++ b/contrib/sdk/sources/newlib/libc/Tupfile.lua @@ -100,7 +100,6 @@ CORE_SRCS = { "sys/close.c", "sys/conio.c", "sys/chdir.c", - "sys/getcwd.c", "sys/errno.c", "sys/fstat.c", "sys/gettod.c", @@ -307,8 +306,7 @@ STRING_SRCS = { "wmemcmp.c", "wmemcpy.c", "wmemmove.c", - "wmemset.c", - "dirname.c" + "wmemset.c" } STDIO_SRCS = { @@ -455,6 +453,7 @@ STDIO_SRCS = { "wscanf.c", "wsetup.c" } + POSIX_SRCS = { "opendir.c", "closedir.c", @@ -464,6 +463,12 @@ POSIX_SRCS = { "rewinddir.c" } +UNIX_SRCS = { + "getcwd.c", + "dirname.c", + "basename.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", "e_hypot.c", "e_j0.c", "e_j1.c", "e_jn.c", "e_log.c", "e_log10.c", "e_pow.c", "e_rem_pio2.c", @@ -516,6 +521,7 @@ 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("unix/", UNIX_SRCS) LIB_SRCS += prepend("math/", MATH_SRCS) ALL_OBJS = {} diff --git a/contrib/sdk/sources/newlib/libc/string/dirname.c b/contrib/sdk/sources/newlib/libc/string/dirname.c deleted file mode 100644 index b889f7d0c6..0000000000 --- a/contrib/sdk/sources/newlib/libc/string/dirname.c +++ /dev/null @@ -1,67 +0,0 @@ -/* dirname - return directory part of PATH. - Copyright (C) 1996-2019 Free Software Foundation, Inc. - This file is part of the GNU C Library. - Contributed by Ulrich Drepper , 1996. - The GNU C Library is free software; you can redistribute it and/or - modify it under the terms of the GNU Lesser General Public - License as published by the Free Software Foundation; either - version 2.1 of the License, or (at your option) any later version. - The GNU C Library 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 - Lesser General Public License for more details. - You should have received a copy of the GNU Lesser General Public - License along with the GNU C Library; if not, see - . */ - -#include -#include - -char * -dirname (char *path) -{ - static const char dot[] = "."; - char *last_slash; - /* Find last '/'. */ - last_slash = path != NULL ? strrchr (path, '/') : NULL; - if (last_slash != NULL && last_slash != path && last_slash[1] == '\0') - { - /* Determine whether all remaining characters are slashes. */ - char *runp; - for (runp = last_slash; runp != path; --runp) - if (runp[-1] != '/') - break; - /* The '/' is the last character, we have to look further. */ - if (runp != path) - last_slash = memrchr (path, '/', runp - path); - } - if (last_slash != NULL) - { - /* Determine whether all remaining characters are slashes. */ - char *runp; - for (runp = last_slash; runp != path; --runp) - if (runp[-1] != '/') - break; - /* Terminate the path. */ - if (runp == path) - { - /* The last slash is the first character in the string. We have to - return "/". As a special case we have to return "//" if there - are exactly two slashes at the beginning of the string. See - XBD 4.10 Path Name Resolution for more information. */ - if (last_slash == path + 1) - ++last_slash; - else - last_slash = path + 1; - } - else - last_slash = runp; - last_slash[0] = '\0'; - } - else - /* This assignment is ill-designed but the XPG specs require to - return a string containing "." in any case no directory part is - found and so a static and constant string is required. */ - path = (char *) dot; - return path; -} \ No newline at end of file diff --git a/contrib/sdk/sources/newlib/libc/sys/getcwd.c b/contrib/sdk/sources/newlib/libc/sys/getcwd.c deleted file mode 100644 index e19826ba8b..0000000000 --- a/contrib/sdk/sources/newlib/libc/sys/getcwd.c +++ /dev/null @@ -1,17 +0,0 @@ -/* - * Copyright (C) KolibriOS team 2004-2024. All rights reserved. - * Distributed under terms of the GNU General Public License -*/ - -#include -#include - -char *getcwd(char *buf, unsigned size){ - if(!buf){ - if((buf = malloc(size))==NULL){ - return NULL; - } - } - _ksys_getcwd(buf, size); - return(buf); -} diff --git a/contrib/sdk/sources/newlib/libc/unix/basename.c b/contrib/sdk/sources/newlib/libc/unix/basename.c new file mode 100644 index 0000000000..d1dd9ed976 --- /dev/null +++ b/contrib/sdk/sources/newlib/libc/unix/basename.c @@ -0,0 +1,28 @@ +#ifndef _NO_BASENAME +/* Copyright 2005 Shaun Jackman + * Permission to use, copy, modify, and distribute this software + * is freely granted, provided that this notice is preserved. + */ + +#include +#include + +char* +_DEFUN (basename, (path), + char *path) +{ + char *p; + if( path == NULL || *path == '\0' ) + return "."; + p = path + strlen(path) - 1; + while( *p == '/' ) { + if( p == path ) + return path; + *p-- = '\0'; + } + while( p >= path && *p != '/' ) + p--; + return p + 1; +} + +#endif /* !_NO_BASENAME */ diff --git a/contrib/sdk/sources/newlib/libc/unix/dirname.c b/contrib/sdk/sources/newlib/libc/unix/dirname.c new file mode 100644 index 0000000000..164da7f782 --- /dev/null +++ b/contrib/sdk/sources/newlib/libc/unix/dirname.c @@ -0,0 +1,32 @@ +#ifndef _NO_DIRNAME + +/* Copyright 2005 Shaun Jackman + * Permission to use, copy, modify, and distribute this software + * is freely granted, provided that this notice is preserved. + */ + +#include +#include + +char * +_DEFUN (dirname, (path), + char *path) +{ + char *p; + if( path == NULL || *path == '\0' ) + return "."; + p = path + strlen(path) - 1; + while( *p == '/' ) { + if( p == path ) + return path; + *p-- = '\0'; + } + while( p >= path && *p != '/' ) + p--; + return + p < path ? "." : + p == path ? "/" : + (*p = '\0', path); +} + +#endif /* !_NO_DIRNAME */ diff --git a/contrib/sdk/sources/newlib/libc/unix/getcwd.c b/contrib/sdk/sources/newlib/libc/unix/getcwd.c new file mode 100644 index 0000000000..646e63994d --- /dev/null +++ b/contrib/sdk/sources/newlib/libc/unix/getcwd.c @@ -0,0 +1,53 @@ +#ifndef _NO_GETCWD + +/* + * Copyright (C) KolibriOS team 2024. All rights reserved. + * Distributed under terms of the GNU General Public License +*/ + +#include +#include + +#include +#include +#include + +#ifndef _REENT_ONLY + +char * +_DEFUN (getcwd, (buf, size), + char *buf _AND + size_t size) +{ + if (buf != NULL && size == 0) + { + errno = EINVAL; + return NULL; + } + + if (buf == NULL) + { + if (size == 0) + size = PATH_MAX; + + buf = malloc(size); + if (buf == NULL) + { + errno = ENOMEM; + return NULL; + } + } + + _ksys_getcwd(buf, size); + + if (access(buf, R_OK) == -1) + { + errno = EACCES; + return NULL; + } + + return buf; +} + +#endif /* _REENT_ONLY */ +#endif /* !_NO_GETCWD */