This is the mail archive of the
newlib@sourceware.org
mailing list for the newlib project.
[PATCH 028/114] Add implementation for realpath function for Phoenix.
- From: Jakub Sejdak <jakub dot sejdak at phoesys dot com>
- To: newlib at sourceware dot org
- Cc: Kuba Sejdak <jakub dot sejdak at phoesys dot com>
- Date: Mon, 11 Apr 2016 12:20:46 +0200
- Subject: [PATCH 028/114] Add implementation for realpath function for Phoenix.
- Authentication-results: sourceware.org; auth=none
- References: <1460370132-4880-1-git-send-email-jakub dot sejdak at phoesys dot com>
From: Kuba Sejdak <jakub.sejdak@phoesys.com>
---
newlib/libc/sys/phoenix/realpath.c | 122 +++++++++++++++++++++++++++++++++++++
1 file changed, 122 insertions(+)
create mode 100644 newlib/libc/sys/phoenix/realpath.c
diff --git a/newlib/libc/sys/phoenix/realpath.c b/newlib/libc/sys/phoenix/realpath.c
new file mode 100644
index 0000000..01220fe
--- /dev/null
+++ b/newlib/libc/sys/phoenix/realpath.c
@@ -0,0 +1,122 @@
+/* Copyright (C) 2012-2016 Phoenix Systems (http://www.phoesys.com/).
+ This file is part of the GNU C Library.
+
+ The GNU 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 2.1 of the License, or (at your option) any later version.
+
+ The GNU 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 GNU C Library; if not, write to the Free
+ Software Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA
+ 02111-1307 USA. */
+
+/* Written 2000 by Werner Almesberger */
+
+#include <errno.h>
+#include <stdlib.h>
+#include <string.h>
+#include <sys/dirent.h>
+#include <sys/stat.h>
+#include <unistd.h>
+
+static int resolve_path(char *path, char *result, char *pos)
+{
+ if (*path == '/') {
+ *result = '/';
+ pos = result+1;
+ ++path;
+ }
+
+ *pos = 0;
+ if (!*path)
+ return 0;
+
+ while (1) {
+ struct stat st;
+
+ char *slash = *path ? strchr(path,'/') : NULL;
+ if (slash)
+ *slash = 0;
+
+ if (!path[0] || (path[0] == '.' && (!path[1] || (path[1] == '.' && !path[2])))) {
+ --pos;
+ if (pos != result && path[0] && path[1])
+ while (*--pos != '/');
+ }
+ else {
+ strcpy(pos,path);
+ if (lstat(result,&st) < 0)
+ return -1;
+
+ if (S_ISLNK(st.st_mode)) {
+ char buf[PATH_MAX_SIZE];
+
+ if (readlink(result,buf,sizeof(buf)) < 0)
+ return -1;
+
+ *pos = 0;
+ if (slash) {
+ *slash = '/';
+ strcat(buf, slash);
+ }
+
+ strcpy(path,buf);
+ if (*path == '/')
+ result[1] = 0;
+
+ pos = strchr(result,0);
+ continue;
+ }
+
+ pos = strchr(result,0);
+ }
+
+ if (slash) {
+ *pos++ = '/';
+ path = slash + 1;
+ }
+
+ *pos = 0;
+ if (!slash)
+ break;
+ }
+
+ return 0;
+}
+
+char *realpath(const char *path, char *resolved_path)
+{
+ char cwd[PATH_MAX_SIZE];
+ char *path_copy;
+ int res;
+
+ if (!*path) {
+ errno = ENOENT;
+ return NULL;
+ }
+
+ if (!getcwd(cwd, sizeof(cwd)))
+ return NULL;
+
+ strcpy(resolved_path, "/");
+ if (resolve_path(cwd, resolved_path, resolved_path))
+ return NULL;
+
+ strcat(resolved_path, "/");
+ path_copy = strdup(path);
+ if (!path_copy)
+ return NULL;
+
+ res = resolve_path(path_copy, resolved_path, strchr(resolved_path, 0));
+ free(path_copy);
+ if (res)
+ return NULL;
+
+ return resolved_path;
+}
--
2.5.0