summaryrefslogtreecommitdiffhomepage
path: root/src/cmds/ntux_cmd_fspath.c
diff options
context:
space:
mode:
Diffstat (limited to 'src/cmds/ntux_cmd_fspath.c')
-rw-r--r--src/cmds/ntux_cmd_fspath.c91
1 files changed, 91 insertions, 0 deletions
diff --git a/src/cmds/ntux_cmd_fspath.c b/src/cmds/ntux_cmd_fspath.c
new file mode 100644
index 0000000..9b25062
--- /dev/null
+++ b/src/cmds/ntux_cmd_fspath.c
@@ -0,0 +1,91 @@
+/***********************************************************/
+/* ntux: native translation und extension */
+/* Copyright (C) 2016--2022 SysDeer Technologies, LLC */
+/* Released under GPLv2 and GPLv3; see COPYING.NTUX. */
+/***********************************************************/
+
+#include <psxabi/sys_sysapi.h>
+#include <psxabi/sys_stat.h>
+#include <psxabi/sys_errno.h>
+
+#include <psxxfi/xfi_ofd.h>
+#include <psxxfi/xfi_stat.h>
+#include <psxxfi/xfi_unicode.h>
+
+#include <ntux/ntux.h>
+#include "ntux_driver_impl.h"
+#include "ntux_nolibc_impl.h"
+#include "ntux_errinfo_impl.h"
+
+static int ntux_cmd_fspath_ret(void * buf, int ret)
+{
+ if (buf)
+ ntux_free(buf);
+
+ return ret;
+}
+
+int ntux_cmd_fspath(const struct ntux_driver_ctx * dctx, const char * dunit)
+{
+ intptr_t ret;
+ int fdout;
+ int fdcwd;
+ const unsigned char * unit;
+ char * str;
+ void * buf = 0;
+ const size_t bufsize = 0x10000;
+
+ /* init */
+ ntux_driver_set_ectx(
+ dctx,0,dunit);
+
+ unit = (const unsigned char *)dunit;
+
+ /* fdctx */
+ fdout = ntux_driver_fdout(dctx);
+ fdcwd = ntux_driver_fdcwd(dctx);
+
+ /* buffer */
+ if (!(buf = ntux_calloc(1,bufsize)))
+ if (ntux_errno_set(dctx,ENOMEM))
+ return ntux_cmd_fspath_ret(
+ buf,
+ NTUX_SYSTEM_ERROR(dctx));
+
+ str = (char *)buf;
+
+ /* root-relative notation? */
+ if (dctx->cctx->drvflags & NTUX_DRIVER_RPATH) {
+ if ((ret = __sys_fs_rpath(
+ fdcwd,
+ (const char *)unit,
+ 0,buf,bufsize)) < 0)
+ return ntux_cmd_fspath_ret(buf,ret);
+
+ } else if (dctx->cctx->drvflags & NTUX_DRIVER_APATH) {
+ if ((ret = __sys_fs_apath(
+ fdcwd,
+ (const char *)unit,
+ 0,buf,bufsize)) < 0)
+ return ntux_cmd_fspath_ret(buf,ret);
+ } else if (dctx->cctx->drvflags & NTUX_DRIVER_NPATH) {
+ if ((ret = __sys_fs_npath(
+ fdcwd,
+ (const char *)unit,
+ 0,buf,bufsize)) < 0)
+ return ntux_cmd_fspath_ret(buf,ret);
+
+ } else if (dctx->cctx->drvflags & NTUX_DRIVER_DPATH) {
+ if ((ret = __sys_fs_dpath(
+ fdcwd,
+ (const char *)unit,
+ 0,buf,bufsize)) < 0)
+ return ntux_cmd_fspath_ret(buf,ret);
+ }
+
+ /* outupt */
+ ntux_dprintf(fdout,"%s\n",str);
+
+ /* all done */
+ return ntux_cmd_fspath_ret(buf,0);
+}