Blame src/cmds/ntux_cmd_fspath.c

8ec5ce
/***********************************************************/
8ec5ce
/*  ntux: native translation und extension                 */
8ec5ce
/*  Copyright (C) 2016--2022  SysDeer Technologies, LLC    */
8ec5ce
/*  Released under GPLv2 and GPLv3; see COPYING.NTUX.      */
8ec5ce
/***********************************************************/
8ec5ce
8ec5ce
#include <psxabi/sys_sysapi.h>
8ec5ce
#include <psxabi/sys_stat.h>
8ec5ce
#include <psxabi/sys_errno.h>
8ec5ce
8ec5ce
#include <psxxfi/xfi_ofd.h>
8ec5ce
#include <psxxfi/xfi_stat.h>
8ec5ce
#include <psxxfi/xfi_unicode.h>
8ec5ce
8ec5ce
#include <ntux/ntux.h>
8ec5ce
#include "ntux_driver_impl.h"
8ec5ce
#include "ntux_nolibc_impl.h"
8ec5ce
#include "ntux_errinfo_impl.h"
8ec5ce
8ec5ce
static int ntux_cmd_fspath_ret(void * buf, int ret)
8ec5ce
{
8ec5ce
	if (buf)
8ec5ce
		ntux_free(buf);
8ec5ce
8ec5ce
	return ret;
8ec5ce
}
8ec5ce
8ec5ce
int ntux_cmd_fspath(const struct ntux_driver_ctx * dctx, const char * dunit)
8ec5ce
{
8ec5ce
	intptr_t		ret;
8ec5ce
	int			fdout;
8ec5ce
	int			fdcwd;
8ec5ce
	const unsigned char *	unit;
8ec5ce
	char *			str;
8ec5ce
	void *			buf     = 0;
8ec5ce
	const size_t		bufsize = 0x10000;
8ec5ce
8ec5ce
	/* init */
8ec5ce
	ntux_driver_set_ectx(
8ec5ce
		dctx,0,dunit);
8ec5ce
8ec5ce
	unit = (const unsigned char *)dunit;
8ec5ce
8ec5ce
	/* fdctx */
8ec5ce
	fdout = ntux_driver_fdout(dctx);
8ec5ce
	fdcwd = ntux_driver_fdcwd(dctx);
8ec5ce
8ec5ce
	/* buffer */
8ec5ce
	if (!(buf = ntux_calloc(1,bufsize)))
8ec5ce
		if (ntux_errno_set(dctx,ENOMEM))
8ec5ce
			return ntux_cmd_fspath_ret(
8ec5ce
				buf,
8ec5ce
				NTUX_SYSTEM_ERROR(dctx));
8ec5ce
8ec5ce
	str = (char *)buf;
8ec5ce
8ec5ce
	/* root-relative notation? */
8ec5ce
	if (dctx->cctx->drvflags & NTUX_DRIVER_RPATH) {
8ec5ce
		if ((ret = __sys_fs_rpath(
8ec5ce
				fdcwd,
8ec5ce
				(const char *)unit,
8ec5ce
				0,buf,bufsize)) < 0)
1d2357
			if (ntux_errno_set(dctx,ret))
1d2357
				return ntux_cmd_fspath_ret(
1d2357
					buf,
1d2357
					NTUX_SYSTEM_ERROR(dctx));
8ec5ce
fcced5
	/* posix absolute notation? */
8ec5ce
	} else if (dctx->cctx->drvflags & NTUX_DRIVER_APATH) {
8ec5ce
		if ((ret = __sys_fs_apath(
8ec5ce
				fdcwd,
8ec5ce
				(const char *)unit,
8ec5ce
				0,buf,bufsize)) < 0)
1d2357
			if (ntux_errno_set(dctx,ret))
1d2357
				return ntux_cmd_fspath_ret(
1d2357
					buf,
1d2357
					NTUX_SYSTEM_ERROR(dctx));
1d2357
fcced5
	/* native tool notation? */
8ec5ce
	} else if (dctx->cctx->drvflags & NTUX_DRIVER_NPATH) {
8ec5ce
		if ((ret = __sys_fs_npath(
8ec5ce
				fdcwd,
8ec5ce
				(const char *)unit,
8ec5ce
				0,buf,bufsize)) < 0)
1d2357
			if (ntux_errno_set(dctx,ret))
1d2357
				return ntux_cmd_fspath_ret(
1d2357
					buf,
1d2357
					NTUX_SYSTEM_ERROR(dctx));
8ec5ce
fcced5
	/* native driver notation? */
8ec5ce
	} else if (dctx->cctx->drvflags & NTUX_DRIVER_DPATH) {
8ec5ce
		if ((ret = __sys_fs_dpath(
8ec5ce
				fdcwd,
8ec5ce
				(const char *)unit,
8ec5ce
				0,buf,bufsize)) < 0)
1d2357
			if (ntux_errno_set(dctx,ret))
1d2357
				return ntux_cmd_fspath_ret(
1d2357
					buf,
1d2357
					NTUX_SYSTEM_ERROR(dctx));
fcced5
fcced5
	/* [default] posix absolute notation */
fcced5
	} else {
fcced5
		if ((ret = __sys_fs_apath(
fcced5
				fdcwd,
fcced5
				(const char *)unit,
fcced5
				0,buf,bufsize)) < 0)
fcced5
			if (ntux_errno_set(dctx,ret))
fcced5
				return ntux_cmd_fspath_ret(
fcced5
					buf,
fcced5
					NTUX_SYSTEM_ERROR(dctx));
8ec5ce
	}
8ec5ce
8ec5ce
	/* outupt */
8ec5ce
	ntux_dprintf(fdout,"%s\n",str);
8ec5ce
8ec5ce
	/* all done */
8ec5ce
	return ntux_cmd_fspath_ret(buf,0);
8ec5ce
}