radare2/libr/io/desc.c
pancake 1dd4042505 * Lot of work on refactoring r_io
- Added desc.c to handle file descriptors (avoid dupped, register handlers, ..)
  - Fix some bugs and optimize io paths
  - Added resolution methods
  - Maps are incomplete but better interfaced. Needs more work
  - Added io/t/map.c test program (not yet working)
* Some fixups in many vapi files
  - Fix broken ones (syntax parsing error)
  - Complete them a bit more
* Initial working version of a demo of list.h vala interface
  - Implemented as a Generic class
  - Designed as an Iterator, so 'foreach' can be used to iterate
  - Added example program iterating from vala and generating a list in C
2009-09-05 23:58:02 +00:00

57 lines
1.2 KiB
C

/* radare - LGPL - Copyright 2009 pancake<nopcode.org> */
#include <r_io.h>
R_API int r_io_desc_init(struct r_io_t *io)
{
INIT_LIST_HEAD(&io->desc);
return R_TRUE;
}
R_API int r_io_desc_add(struct r_io_t *io, int fd, const char *file, int flags, struct r_io_handle_t *handle)
{
struct r_io_desc_t *desc = MALLOC_STRUCT(struct r_io_desc_t);
if (desc == NULL)
return R_FALSE;
strncpy(desc->name, file, sizeof(desc->name));
desc->flags = flags;
desc->fd = fd;
desc->handle = handle;
list_add_tail(&(desc->list), &(io->desc));
return R_TRUE;
}
R_API int r_io_desc_del(struct r_io_t *io, int fd)
{
int ret = R_FALSE;
struct list_head *pos;
list_for_each_prev(pos, &io->desc) {
struct r_io_desc_t *d = list_entry(pos, struct r_io_desc_t, list);
if (d->fd == fd) {
list_del((&d->list));
ret = R_TRUE;
break;
}
}
return ret;
}
R_API struct r_io_desc_t *r_io_desc_get(struct r_io_t *io, int fd)
{
struct list_head *pos;
list_for_each_prev(pos, &io->desc) {
struct r_io_desc_t *d = list_entry(pos, struct r_io_desc_t, list);
if (d->fd == fd)
return d;
}
return NULL;
}
R_API int r_io_desc_generate(struct r_io_t *io)
{
int fd;
do fd = 0xf000 + rand()%0xfff;
while (r_io_desc_get(io, fd));
return fd;
}