/*
 * getcwd() for the RISC OS build of libpq: canonicalises "@" (the
 * currently selected directory) with OS_FSControl 37.
 */

#include "cheader.h"

#include "kernel.h"

#define OS_FSControl 0x29
#define FSControl_CanonicalisePath 37

char *
getcwd(char *buf, size_t size)
{
	_kernel_swi_regs regs;

	if (buf == NULL || size == 0)
	{
		errno = EINVAL;
		return NULL;
	}

	regs.r[0] = FSControl_CanonicalisePath;
	regs.r[1] = (int) "@";
	regs.r[2] = (int) buf;
	regs.r[3] = 0;
	regs.r[4] = 0;
	regs.r[5] = (int) size;
	if (_kernel_swi(OS_FSControl, &regs, &regs) != NULL)
	{
		errno = ENOENT;
		return NULL;
	}
	if (regs.r[5] <= 0)
	{
		errno = ERANGE;
		return NULL;
	}
	return buf;
}
