Add realpath(3).

This commit is contained in:
Jonas 'Sortie' Termansen 2013-03-19 18:53:56 +01:00
parent 0ce3d61cb9
commit 9d3937fe9e
3 changed files with 59 additions and 1 deletions

View file

@ -213,6 +213,7 @@ raise.o \
rand.o \
readdirents.o \
read.o \
realpath.o \
removeat.o \
remove.o \
renameat.o \

View file

@ -89,6 +89,7 @@ int putenv(char*);
void qsort(void*, size_t, size_t, int (*)(const void*, const void*));
int rand(void);
void* realloc(void*, size_t);
char* realpath(const char* restrict, char* restrict);
int setenv(const char*, const char*, int);
void srand(unsigned);
long strtol(const char* restrict, char** restrict, int);
@ -139,7 +140,6 @@ int posix_memalign(void**, size_t, size_t);
int posix_openpt(int);
char* ptsname(int);
long random(void);
char* realpath(const char* restrict, char* restrict);
unsigned short *seed48(unsigned short [3]);
void setkey(const char*);
char* setstate(char*);

57
libc/realpath.cpp Normal file
View file

@ -0,0 +1,57 @@
/*******************************************************************************
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/>.
realpath.cpp
Return the canonicalized filename.
*******************************************************************************/
#include <errno.h>
#include <fcntl.h>
#include <limits.h>
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <unistd.h>
char* realpath(const char* restrict path, char* restrict resolved_path)
{
char* ret_path = canonicalize_file_name(path);
if ( !ret_path )
return NULL;
if ( !resolved_path )
return ret_path;
#ifdef PATH_MAX
if ( PATH_MAX < strlen(ret_path) + 1 )
return errno = ENAMETOOLONG, (char*) NULL;
strcpy(resolved_path, ret_path);
free(ret_path);
return resolved_path;
#else
if ( !isatty(2) )
dup2(open("/dev/tty", O_WRONLY), 2);
fprintf(stderr, "%s:%u: %s(\"%s\", %p) = \"%s\": "
"This platform doesn't have PATH_MAX and the second argument "
"wasn't NULL - You cannot reliably allocate a sufficiently large "
"buffer and the POSIX standard specifies this as undefined "
"behavior, aborting.\n", __FILE__, __LINE__, __func__, path,
resolved_path, ret_path);
abort();
#endif
}