new function - realpath()
Imported from http://bugzilla.roxen.com/bugzilla/show_bug.cgi?id=3317
Reference: http://dan.allantgroup.com/realpath.diff
Reported by Dan Nelson dnelson_1901@yahoo.com
Here's a patch that adds a realpath() function to pike; it returns an absolute path without symlinks when given a relative path. realpath() is an X/Open standard, exists on Solaris/Tru64/AIX/*BSD/Linux, and is cumbersome to otherwise implement in Pike.
Index: src/builtin_functions.c
===================================================================
RCS file: /cvs/Pike/7.3/src/builtin_functions.c,v
retrieving revision 1.451
diff -u -p -r1.451 builtin_functions.c
--- src/builtin_functions.c 2002/11/25 00:41:17 1.451
+++ src/builtin_functions.c 2002/11/25 22:14:12
@@ -7542,6 +7542,34 @@ PMOD_EXPORT void f_function_defined(INT3
/*! @endmodule Function
*/
+#ifdef HAVE_REALPATH
+/*! @decl string realpath(string pathname)
+ *!
+ *! Returns the canonicalized absolute pathname
+ *!
+ *! This function resolves all symbolic links, extra ``/'' characters and
+ *! references to /./ and /../ in @[pathname], and returns the resulting
+ *! absolute path, or @tt{0@} (zero) if an error occurs.
+ *!
+ *! @seealso
+ *! @[getcwd()]
+ */
+PMOD_EXPORT void f_realpath(INT32 args)
+{
+ char *path, result[PATH_MAX+1];
+ get_all_args("realpath", args, "%s", &path);
+
+ if (!realpath(path, result))
+ {
+ push_int(0);
+ return;
+ }
+
+ pop_n_elems(args);
+ push_string(make_shared_string(result));
+}
+#endif
+
void init_builtin_efuns(void)
{
struct program *pike___master_program;
@@ -8156,5 +8184,9 @@ void init_builtin_efuns(void)
#else
ADD_INT_CONSTANT("__FLOAT_PRECISION_FLOAT__",1,0);
#endif
+#endif
+
+#ifdef HAVE_REALPATH
+ ADD_EFUN("realpath",f_realpath,tFunc(tStr,tStr),OPT_EXTERNAL_DEPEND);
#endif
}
Index: src/builtin_functions.h
===================================================================
RCS file: /cvs/Pike/7.3/src/builtin_functions.h,v
retrieving revision 1.22
diff -u -p -r1.22 builtin_functions.h
--- src/builtin_functions.h 2002/10/11 01:39:29 1.22
+++ src/builtin_functions.h 2002/11/25 22:14:12
@@ -115,6 +115,7 @@ PMOD_EXPORT void f_transpose(INT32 args)
PMOD_EXPORT void f__reset_dmalloc(INT32 args);
PMOD_EXPORT void f__locate_references(INT32 args);
PMOD_EXPORT void f_map_array(INT32 args);
+PMOD_EXPORT void f_realpath(INT32 args);
void init_builtin_efuns(void);
void init_builtin(void);
void exit_builtin(void);
Index: src/configure.in
===================================================================
RCS file: /cvs/Pike/7.3/src/configure.in,v
retrieving revision 1.649
diff -u -p -r1.649 configure.in
--- src/configure.in 2002/11/24 22:37:50 1.649
+++ src/configure.in 2002/11/25 22:14:15
@@ -3034,6 +3034,7 @@ AC_CHECK_FUNCS( \
usleep \
nanosleep \
thr_yield \
+ realpath \
)