Add dirname(3) and basename(3).

This commit is contained in:
Jonas 'Sortie' Termansen 2013-03-29 14:57:17 +01:00
parent 8cc4c6cb03
commit 70a661e1ab
4 changed files with 132 additions and 0 deletions

View File

@ -155,6 +155,7 @@ arpa/inet/inet_addr.o \
arpa/inet/inet_ntoa.o \
arpa/inet/inet_ntop.o \
arpa/inet/inet_pton.o \
basename.o \
calltrace.o \
canonicalize_file_name_at.o \
canonicalize_file_name.o \
@ -168,6 +169,7 @@ $(CPUDIR)/fork.o \
$(CPUDIR)/signal.o \
$(CPUDIR)/syscall.o \
creat.o \
dirname.o \
dispmsg_issue.o \
dlfcn.o \
dup2.o \

46
libc/basename.cpp Normal file
View File

@ -0,0 +1,46 @@
/*******************************************************************************
Copyright(C) Jonas 'Sortie' Termansen 2013.
This file is part of the Sortix C Library.
The Sortix 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 3 of the License, or (at your
option) any later version.
The Sortix 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 Sortix C Library. If not, see <http://www.gnu.org/licenses/>.
basename.cpp
Returns the name part of a path.
*******************************************************************************/
#include <libgen.h>
#include <string.h>
char* basename(char* path)
{
static char static_stuff[2];
if ( !path || !*path )
{
static_stuff[0] = '.';
static_stuff[1] = '\0';
return static_stuff;
}
size_t path_len = strlen(path);
while ( 2 <= path_len && path[path_len-1] == '/' )
path[--path_len] = '\0';
if ( strcmp(path, "/") == 0 )
return path;
char* last_slash = strchr(path, '/');
if ( !last_slash )
return path;
return last_slash + 1;
}

47
libc/dirname.cpp Normal file
View File

@ -0,0 +1,47 @@
/*******************************************************************************
Copyright(C) Jonas 'Sortie' Termansen 2013.
This file is part of the Sortix C Library.
The Sortix 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 3 of the License, or (at your
option) any later version.
The Sortix 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 Sortix C Library. If not, see <http://www.gnu.org/licenses/>.
dirname.cpp
Returns the directory part of a path.
*******************************************************************************/
#include <libgen.h>
#include <string.h>
char* dirname(char* path)
{
static char static_stuff[2];
if ( !path || !*path )
{
static_stuff[0] = '.';
static_stuff[1] = '\0';
return static_stuff;
}
size_t path_len = strlen(path);
while ( 2 <= path_len && path[path_len-1] == '/' )
path[--path_len] = '\0';
if ( strcmp(path, "/") == 0 )
return path;
char* last_slash = strchr(path, '/');
if ( !last_slash )
return dirname(NULL);
*last_slash = '\0';
return path;
}

37
libc/include/libgen.h Normal file
View File

@ -0,0 +1,37 @@
/*******************************************************************************
Copyright(C) Jonas 'Sortie' Termansen 2013.
This file is part of the Sortix C Library.
The Sortix 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 3 of the License, or (at your
option) any later version.
The Sortix 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 Sortix C Library. If not, see <http://www.gnu.org/licenses/>.
libgen.h
What's left of some old pattern matching library.
*******************************************************************************/
#ifndef INCLUDE_LIBGEN_H
#define INCLUDE_LIBGEN_H
#include <features.h>
__BEGIN_DECLS
char* dirname(char* path);
char* basename(char* path);
__END_DECLS
#endif