Blob Blame History Raw
/***********************************************************/
/*  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)
			if (ntux_errno_set(dctx,ret))
				return ntux_cmd_fspath_ret(
					buf,
					NTUX_SYSTEM_ERROR(dctx));

	/* posix absolute notation? */
	} else if (dctx->cctx->drvflags & NTUX_DRIVER_APATH) {
		if ((ret = __sys_fs_apath(
				fdcwd,
				(const char *)unit,
				0,buf,bufsize)) < 0)
			if (ntux_errno_set(dctx,ret))
				return ntux_cmd_fspath_ret(
					buf,
					NTUX_SYSTEM_ERROR(dctx));

	/* native tool notation? */
	} else if (dctx->cctx->drvflags & NTUX_DRIVER_NPATH) {
		if ((ret = __sys_fs_npath(
				fdcwd,
				(const char *)unit,
				0,buf,bufsize)) < 0)
			if (ntux_errno_set(dctx,ret))
				return ntux_cmd_fspath_ret(
					buf,
					NTUX_SYSTEM_ERROR(dctx));

	/* native driver notation? */
	} else if (dctx->cctx->drvflags & NTUX_DRIVER_DPATH) {
		if ((ret = __sys_fs_dpath(
				fdcwd,
				(const char *)unit,
				0,buf,bufsize)) < 0)
			if (ntux_errno_set(dctx,ret))
				return ntux_cmd_fspath_ret(
					buf,
					NTUX_SYSTEM_ERROR(dctx));

	/* [default] posix absolute notation */
	} else {
		if ((ret = __sys_fs_apath(
				fdcwd,
				(const char *)unit,
				0,buf,bufsize)) < 0)
			if (ntux_errno_set(dctx,ret))
				return ntux_cmd_fspath_ret(
					buf,
					NTUX_SYSTEM_ERROR(dctx));
	}

	/* outupt */
	ntux_dprintf(fdout,"%s\n",str);

	/* all done */
	return ntux_cmd_fspath_ret(buf,0);
}