+ Add sys_tmpnam() and sys_basename()

This commit is contained in:
carl 2019-03-17 22:23:56 +08:00
parent da71e5a018
commit 6bfac1d24d
3 changed files with 73 additions and 1 deletions

View file

@ -0,0 +1,57 @@
/* Copyright (c) 2019. See the file License in
* the root directory for more information.
*
* Contains path related utilities.
*/
#include <string.h>
void sys_basename(char *str, register char *dst)
{
register char *p1 = str;
register char *p2 = p1;
register char *end;
register char *start;
int len = strlen(str);
/* Point to the end of the string. */
p1 = p1 + len - 1;
end = p1;
while ((*p1 == '/') || (*p1 == '\\'))
{
if (p1 == str)
{
dst[0] = *p1;
dst[1] = '\0';
return;
}
p1--;
}
/* Only a volume specification */
if (*p1 == ':')
{
strcpy(dst,str);
return;
}
/* Do a reverse search. */
p2 = p1;
len = 0;
while (p2 != str)
{
if ((*p1 == '/') || (*p1 == '\\') || (*p1 == ':'))
{
strncpy(dst,p2,len);
dst[len] = '\0';
return;
}
p2 = p1;
len++;
p1--;
}
/* Only a pathname */
strcpy(dst,str);
}

View file

@ -2,7 +2,7 @@ clibrary {
name = "lib", name = "lib",
srcs = { srcs = {
"./access.c", "./break.c", "./chmode.c", "./close.c", "./access.c", "./break.c", "./chmode.c", "./close.c",
"./create.c", "./filesize.c", "./create.c", "./filesize.c","./basename.c","./tmpnam.c",
--"./lock.c", --"./lock.c",
"./modtime.c", "./open.c", "./read.c", "./remove.c", "./modtime.c", "./open.c", "./read.c", "./remove.c",
"./rename.c", "./seek.c", "./stop.c", "./system.c", "./rename.c", "./seek.c", "./stop.c", "./system.c",

View file

@ -67,4 +67,19 @@ time_t sys_modtime(char *);
/* return value for sys_break */ /* return value for sys_break */
#define ILL_BREAK ((char *)0) #define ILL_BREAK ((char *)0)
/* Extract the base name from a full path specification
* in "str" and returns it in "dst".
*
* "dst" should be large enough to receive the copied
* data.
*
* Supports both DOS and UNIX style paths.
* */
void sys_basename(const char *str, register char *dst);
/* Creates a temporary filename. This has
* the same semantics as ISO C90 tmpnam() */
char* sys_tmpnam(char *buffer);
#endif /* __SYSTEM_INCLUDED__ */ #endif /* __SYSTEM_INCLUDED__ */