summaryrefslogtreecommitdiffhomepage
path: root/src/cmds/ntux_cmd_fspath.c
blob: 12d575cdd60f7844bd935efcbc0727c391f452a3 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
/***********************************************************/
/*  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);
}