From 26606098c2eb57d288b2e0edca471c71d0d7fa51 Mon Sep 17 00:00:00 2001 From: Carmelo Amoroso Date: Thu, 16 Sep 2010 12:22:34 +0200 Subject: libc: Add canonicalize_file_name function Add canonicalize_file_name function and its related tests. Required by elfutils and coreutils (readlink). Signed-off-by: Salvatore Cro Signed-off-by: Carmelo Amoroso --- libc/stdlib/Makefile.in | 2 +- libc/stdlib/canonicalize.c | 38 ++++++++++++++++++++++++++++++++++++++ libc/stdlib/realpath.c | 1 + 3 files changed, 40 insertions(+), 1 deletion(-) create mode 100644 libc/stdlib/canonicalize.c (limited to 'libc/stdlib') diff --git a/libc/stdlib/Makefile.in b/libc/stdlib/Makefile.in index 0f174eea5..760ccf7e6 100644 --- a/libc/stdlib/Makefile.in +++ b/libc/stdlib/Makefile.in @@ -12,7 +12,7 @@ include $(top_srcdir)libc/stdlib/malloc-simple/Makefile.in include $(top_srcdir)libc/stdlib/malloc-standard/Makefile.in CSRC-y := \ - abort.c getenv.c mkdtemp.c realpath.c mkstemp.c \ + abort.c getenv.c mkdtemp.c realpath.c canonicalize.c mkstemp.c \ rand.c random.c random_r.c setenv.c div.c ldiv.c lldiv.c \ getpt.c drand48-iter.c jrand48.c \ jrand48_r.c lrand48.c lrand48_r.c mrand48.c mrand48_r.c nrand48.c \ diff --git a/libc/stdlib/canonicalize.c b/libc/stdlib/canonicalize.c new file mode 100644 index 000000000..06e710ab7 --- /dev/null +++ b/libc/stdlib/canonicalize.c @@ -0,0 +1,38 @@ +/* + * canonicalize.c -- Return a malloc'd string containing the canonical + * absolute name of the named file. The last file name component need + * not exist, and may be a symlink to a nonexistent file. + * Copyright (C) 2009 STMicroelectronics + * Author: Salvatore Cro + * + * Licensed under the LGPL v2.1, see the file COPYING.LIB in this tarball. + */ + +#include +#include + +#ifdef __USE_GNU + +#ifndef PATH_MAX +# ifdef _POSIX_VERSION +# define PATH_MAX _POSIX_PATH_MAX +# else +# ifdef MAXPATHLEN +# define PATH_MAX MAXPATHLEN +# else +# define PATH_MAX 1024 +# endif +# endif +#endif + +char * canonicalize_file_name (const char *name) +{ + char *buf = (char *) malloc(PATH_MAX); + + if(unlikely(buf == NULL)) + return NULL; + + *buf='\0'; + return realpath (name, buf); +} +#endif diff --git a/libc/stdlib/realpath.c b/libc/stdlib/realpath.c index 80c25f098..cf9d45fa2 100644 --- a/libc/stdlib/realpath.c +++ b/libc/stdlib/realpath.c @@ -159,3 +159,4 @@ char *realpath(const char *path, char got_path[]) *new_path = '\0'; return got_path; } +libc_hidden_def(realpath) -- cgit v1.2.3