radare2/libr/core/file.c
pancake 9bd9cbe135 * Fix LIBR_PLUGINS in env.sh
* Initial implementation of the visual mode in core
* Added pc and ps print formats (c code and string)
* Added '/' command in core (search hexa and str)
* Add '-d' to radare2 test program
* New r_file_path to resolve file path thru $PATH
* 'make install' works everywhere
2009-02-09 01:54:09 +01:00

69 lines
1.4 KiB
C

/* radare - LGPL - Copyright 2009 pancake<nopcode.org> */
#include <r_core.h>
u64 r_core_file_resize(struct r_core_t *core, u64 newsize)
{
if (newsize == 0 && core->file)
return core->file->size;
return 0LL;
}
struct r_core_file_t *r_core_file_open(struct r_core_t *r, const char *file, int mode)
{
struct r_core_file_t *fh;
int fd;
char *p;
fd = r_io_open(&r->io, file, mode, 0644);
if (fd == -1)
return NULL;
fh = MALLOC_STRUCT(struct r_core_file_t);
fh->fd = fd;
fh->uri = strdup(file);
fh->filename = fh->uri;
p = strstr(fh->filename, "://");
if (p != NULL)
fh->filename = p+3;
fh->rwx = mode;
r->file = fh;
fh->size = r_io_size(&r->io, fd);
r_core_block_read(r, 0);
list_add(&(fh->list), &r->files);
return fh;
}
int r_core_file_set(struct r_core_t *r, struct r_core_file_t *fh)
{
r->file = fh;
return R_TRUE;
}
int r_core_file_close(struct r_core_t *r, struct r_core_file_t *fh)
{
int ret = r_io_close(&r->io, fh->fd);
list_del(&(fh->list));
return ret;
}
struct r_core_file_t *r_core_file_get_fd(struct r_core_t *core, int fd)
{
struct list_head *pos;
list_for_each_prev(pos, &core->files) {
struct r_core_file_t *fh = list_entry(pos, struct r_core_file_t, list);
if (fh->fd == fd)
return fh;
}
return NULL;
}
int r_core_file_close_fd(struct r_core_t *core, int fd)
{
int ret = r_io_close(&core->io, fd);
struct r_core_file_t *fh = r_core_file_get_fd(core, fd);
if (fh != NULL)
list_del(&(fh->list));
return ret;
}