- 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
This commit is contained in:
turbocat
2024-03-07 20:39:33 +00:00
parent 64d4ca96c4
commit aae3ae86cc
7 changed files with 134 additions and 93 deletions

View File

@@ -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 <libgen.h>
#include <string.h>
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 */

View File

@@ -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 <libgen.h>
#include <string.h>
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 */

View File

@@ -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 <stdlib.h>
#include <limits.h>
#include <sys/errno.h>
#include <sys/unistd.h>
#include <sys/ksys.h>
#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 */