HelenOS sources
This source file includes following definitions.
- ofw_path_to_sipath
- ofw_child_it_first
- ofw_child_it_next
- ofw_child_it_end
- ofw_child_it_get_name
- ofw_child_it_get_path
- ofw_child_it_fini
- ofw_prop_it_first
- ofw_prop_it_next
- ofw_prop_it_end
- ofw_prop_it_get_name
- ofw_prop_it_get_data
- ofw_prop_it_fini
#include <assert.h>
#include <errno.h>
#include <ofw.h>
#include <stdio.h>
#include <stdlib.h>
#include <str.h>
#include <sysinfo.h>
static errno_t ofw_path_to_sipath(const char *ofwpath, char **sipath)
{
int rv;
size_t size;
size_t i;
if (str_cmp(ofwpath, "/") == 0)
ofwpath = "";
rv = asprintf(sipath, "firmware.ofw%s", ofwpath);
if (rv < 0)
return ENOMEM;
size = str_size(*sipath);
for (i = 0; i < size; i++)
if ((*sipath)[i] == '/')
(*sipath)[i] = '.';
return EOK;
}
errno_t ofw_child_it_first(ofw_child_it_t *it, const char *ofwpath)
{
errno_t rc;
char *sipath = NULL;
it->ofwpath = NULL;
it->keys = NULL;
it->keys_sz = 0;
it->pos = 0;
rc = ofw_path_to_sipath(ofwpath, &sipath);
if (rc != EOK)
goto error;
if (str_cmp(ofwpath, "/") == 0) {
it->ofwpath = str_dup("");
} else {
it->ofwpath = str_dup(ofwpath);
}
if (it->ofwpath == NULL) {
rc = ENOMEM;
goto error;
}
it->keys = sysinfo_get_keys(sipath, &it->keys_sz);
if (it->keys == NULL) {
rc = ENOENT;
goto error;
}
return EOK;
error:
if (sipath != NULL)
free(sipath);
if (it->ofwpath != NULL) {
free(it->ofwpath);
it->ofwpath = NULL;
}
return rc;
}
void ofw_child_it_next(ofw_child_it_t *it)
{
size_t adj;
assert(!ofw_child_it_end(it));
adj = str_nsize(it->keys + it->pos, it->keys_sz - it->pos) + 1;
assert(it->pos + adj <= it->keys_sz);
it->pos += adj;
}
bool ofw_child_it_end(ofw_child_it_t *it)
{
return it->pos >= it->keys_sz;
}
const char *ofw_child_it_get_name(ofw_child_it_t *it)
{
assert(!ofw_child_it_end(it));
return it->keys + it->pos;
}
errno_t ofw_child_it_get_path(ofw_child_it_t *it, char **rpath)
{
const char *name;
int rv;
assert(!ofw_child_it_end(it));
name = ofw_child_it_get_name(it);
rv = asprintf(rpath, "%s/%s", it->ofwpath, name);
if (rv < 0)
return ENOMEM;
return EOK;
}
void ofw_child_it_fini(ofw_child_it_t *it)
{
free(it->ofwpath);
free(it->keys);
it->keys = NULL;
it->keys_sz = 0;
}
errno_t ofw_prop_it_first(ofw_prop_it_t *it, const char *ofwpath)
{
errno_t rc;
char *sipath;
it->data = NULL;
it->data_sz = 0;
it->pos = 0;
rc = ofw_path_to_sipath(ofwpath, &sipath);
if (rc != EOK)
goto error;
it->data = sysinfo_get_data(sipath, &it->data_sz);
if (it->data == NULL) {
rc = ENOENT;
goto error;
}
return EOK;
error:
if (sipath != NULL)
free(sipath);
return rc;
}
void ofw_prop_it_next(ofw_prop_it_t *it)
{
size_t cur_size;
size_t value_size;
cur_size = str_nsize(it->data + it->pos, it->data_sz - it->pos);
if (((char *) it->data)[it->pos + cur_size] != 0)
return;
it->pos += cur_size + 1;
memcpy(&value_size, it->data + it->pos, sizeof(value_size));
it->pos += sizeof(value_size);
if ((it->pos >= it->data_sz) || (it->pos + value_size > it->data_sz))
return;
it->pos += value_size;
}
bool ofw_prop_it_end(ofw_prop_it_t *it)
{
return it->pos >= it->data_sz;
}
const char *ofw_prop_it_get_name(ofw_prop_it_t *it)
{
assert(!ofw_prop_it_end(it));
return it->data + it->pos;
}
const void *ofw_prop_it_get_data(ofw_prop_it_t *it, size_t *rsize)
{
size_t cur_size;
size_t value_size;
size_t pos;
assert(!ofw_prop_it_end(it));
pos = it->pos;
cur_size = str_nsize(it->data + pos, it->data_sz - pos);
if (((char *) it->data)[pos + cur_size] != 0)
return NULL;
pos += cur_size + 1;
memcpy(&value_size, it->data + pos, sizeof(value_size));
pos += sizeof(value_size);
*rsize = value_size;
return it->data + pos;
}
void ofw_prop_it_fini(ofw_prop_it_t *it)
{
free(it->data);
it->data = NULL;
it->data_sz = 0;
it->pos = 0;
}
HelenOS homepage, sources at GitHub