diff --git a/libc/Makefile b/libc/Makefile index b0431658..684fc0f8 100644 --- a/libc/Makefile +++ b/libc/Makefile @@ -213,6 +213,7 @@ raise.o \ rand.o \ readdirents.o \ read.o \ +realpath.o \ removeat.o \ remove.o \ renameat.o \ diff --git a/libc/include/stdlib.h b/libc/include/stdlib.h index 6893686f..081ed2f1 100644 --- a/libc/include/stdlib.h +++ b/libc/include/stdlib.h @@ -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*); diff --git a/libc/realpath.cpp b/libc/realpath.cpp new file mode 100644 index 00000000..575f55e5 --- /dev/null +++ b/libc/realpath.cpp @@ -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 . + + realpath.cpp + Return the canonicalized filename. + +*******************************************************************************/ + +#include +#include +#include +#include +#include +#include +#include + +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 +}