#include <bee2/core/mem.h>
#include <bee2/core/util.h>
#include "bee2/cmd.h"
#include "whereami.h"
err_t cmdSysExePath(char* path, size_t* count)
{
int len;
ASSERT(memIsValid(count, O_PER_S));
if (!path)
{
len = wai_getExecutablePath(0, 0, 0);
if (len < 0)
return ERR_SYS;
if (++len <= 0 || (int)(size_t)len != len)
return ERR_OVERFLOW;
*count = (size_t)len;
}
else
{
ASSERT(memIsValid(path, *count));
if (*count < 1)
return ERR_OUTOFMEMORY;
len = wai_getExecutablePath(path, (int)(*count - 1), 0);
if (len < 0)
return ERR_SYS;
*count = (size_t)(len + 1);
}
return ERR_OK;
}
err_t cmdSysModulePath(char* path, size_t* count)
{
int len;
ASSERT(memIsValid(count, O_PER_S));
if (!path)
{
len = wai_getModulePath(0, 0, 0);
if (len < 0)
return ERR_SYS;
if (++len <= 0 || (int)(size_t)len != len)
return ERR_OVERFLOW;
*count = (size_t)len;
}
else
{
ASSERT(memIsValid(path, *count));
if (*count < 1)
return ERR_OUTOFMEMORY;
len = wai_getModulePath(path, (int)(*count - 1), 0);
if (len < 0)
return ERR_SYS;
*count = (size_t)(len + 1);
}
return ERR_OK;
}