diff options
author | FreeArtMan <dos21h@gmail.com> | 2016-05-24 00:00:45 +0100 |
---|---|---|
committer | FreeArtMan <dos21h@gmail.com> | 2016-05-24 00:00:45 +0100 |
commit | 60f08b0a4676943651bc7b66cb8531325b1c6132 (patch) | |
tree | 5cea9737ea510f579cefa6a4712bf8caf0577da7 | |
parent | cb141140579d796ae5cafe6da52b4f0b87be6a84 (diff) | |
download | ihe-60f08b0a4676943651bc7b66cb8531325b1c6132.tar.gz ihe-60f08b0a4676943651bc7b66cb8531325b1c6132.zip |
Added resize c
-rw-r--r-- | .gitignore | 1 | ||||
-rw-r--r-- | buf.c | 15 | ||||
-rw-r--r-- | buf.h | 3 | ||||
-rw-r--r-- | cmd/cmd_close.c | 7 | ||||
-rw-r--r-- | cmd/cmd_pos.c | 2 | ||||
-rw-r--r-- | cmd/cmd_read.c | 3 | ||||
-rw-r--r-- | cmd/cmd_resize.c | 40 | ||||
-rw-r--r-- | cmd/cmd_seek.c | 2 | ||||
-rw-r--r-- | core.c | 26 | ||||
-rw-r--r-- | core.h | 1 | ||||
-rw-r--r-- | ihe.c | 14 | ||||
-rw-r--r-- | ihe.h | 2 |
12 files changed, 105 insertions, 11 deletions
@@ -4,3 +4,4 @@ TODO test/* *.so *.a +log.txt @@ -11,6 +11,21 @@ buf_t* buf_init() } +int buf_not_null( buf_t *bf ) +{ + + if ( bf == NULL ) + return 0; + + if ( bf->buf == NULL ) + return 0; + + if ( bf->buf_size < 0 ) + return 0; + + return 1; +} + int buf_size( buf_t *bf, int size ) { if (bf->buf != NULL) @@ -17,6 +17,9 @@ typedef struct buf_t create empty buffer structure */ buf_t* buf_init(); + +int buf_not_null( buf_t *bf ); + /* if empty setup new buffer if not empty resize? diff --git a/cmd/cmd_close.c b/cmd/cmd_close.c index d1d6fc7..a789cd0 100644 --- a/cmd/cmd_close.c +++ b/cmd/cmd_close.c @@ -22,5 +22,12 @@ int c_close( cmd_arg_t *arg ) return -1; } + // clean buf + if ( g_buf->buf != NULL ) + { + buf_zero( g_buf ); + buf_free( g_buf ); + } + return 0; }
\ No newline at end of file diff --git a/cmd/cmd_pos.c b/cmd/cmd_pos.c index 845d69d..272ba3f 100644 --- a/cmd/cmd_pos.c +++ b/cmd/cmd_pos.c @@ -19,7 +19,7 @@ int c_pos( cmd_arg_t *arg ) return -1; } - printf("POS:%d\n",fret); + printf("POS %d\n",fret); return 0; }
\ No newline at end of file diff --git a/cmd/cmd_read.c b/cmd/cmd_read.c index 965ae5a..1a4aa89 100644 --- a/cmd/cmd_read.c +++ b/cmd/cmd_read.c @@ -14,8 +14,7 @@ int c_read( cmd_arg_t *arg ) if ( g_buf->buf == NULL ) { - printf("Buffer mem not allocated\n"); - return -1; + buf_resize( g_buf, g_file->blk_size ); } ret = file_read_blk( g_file, g_buf->buf ); diff --git a/cmd/cmd_resize.c b/cmd/cmd_resize.c new file mode 100644 index 0000000..532c43c --- /dev/null +++ b/cmd/cmd_resize.c @@ -0,0 +1,40 @@ +#include "buf.h" +#include "core.h" + +#include "libcmd/cmd.h" +#include "libcmd/cmd_parse.h" + +extern file_t *g_file; +extern buf_t *g_buf; +extern int g_flags; + +int c_resize( cmd_arg_t *arg ) +{ + int argc = arg->argc; + char **argv = arg->argv; + int *type = arg->type; + + int resize; + + if ( argc != 1 ) + { + printf("Needed one argument\n"); + return -1; + } + + printf("%d %s\n",type[0],argv[0]); + + if ((type[0] != CMDT_INT) && (type[0] != CMDT_HEX) ) + { + printf("Unknown type of argument\n"); + return -1; + } + + resize = atoi( argv[0] ); + if (resize < 0) + return -1; + + file_resize( g_file, resize ); + + return 0; +}
\ No newline at end of file diff --git a/cmd/cmd_seek.c b/cmd/cmd_seek.c index b563231..fd3dcf5 100644 --- a/cmd/cmd_seek.c +++ b/cmd/cmd_seek.c @@ -51,7 +51,7 @@ int c_seek( cmd_arg_t *arg ) { fret = file_seek( g_file, offset ); } - if ( fret != 0 ) + if ( fret < 0 ) { printf("Cannot seek postion to %zd\n", offset); return -1; @@ -216,6 +216,32 @@ int file_size( file_t *ft ) } +int file_resize( file_t *ft, size_t size ) +{ + int ret = 0; + off_t cur_pos; + off_t cur_size; + off_t resize; + uint8_t *buf; + + cur_pos = fd_pos( ft->fd ); + cur_size = fd_size( ft->fd ); + if (cur_size < 0) + { + return -1; + } + buf = malloc( size ); + memset( buf, 0, size ); + fd_seek( ft->fd, cur_size, FD_SEEK_SET ); + resize = fd_write( ft->fd, buf, size ); + fd_seek( ft->fd, cur_pos, FD_SEEK_SET ); + + ret = resize; + + return ret; +} + + int file_open( file_t *ft, const char *filename, int flags, int mode ) { int ret = 0; @@ -106,6 +106,7 @@ int file_seek( file_t *ft, off_t offset ); //seek by offset int file_seekp( file_t *ft, off_t offset ); int file_pos( file_t *ft ); int file_size( file_t *ft ); +int file_resize( file_t *ft, size_t size ); int file_s_bufs( file_t *ft, unsigned int size ); int file_s_mode( file_t *ft, int mode ); int file_close( file_t *ft ); @@ -392,16 +392,17 @@ cmd_table_t tab[] = { {"open", c_open}, {"close", c_close}, {"info", c_info}, - //{"seek", c_seek}, - //{"pos", c_pos}, + {"seek", c_seek}, + {"pos", c_pos}, {"size", c_size}, {"blk", c_blk}, - //{"read", c_read}, - //{"dump", c_dump}, - //{"dumpx", c_dumpx}, - //{"dumps", c_dumps}, + {"read", c_read}, + {"dump", c_dump}, + {"dumpx", c_dumpx}, + {"dumps", c_dumps}, //{"write", c_write}, //{"writes", c_writes}, + {"resize", c_resize}, {"flags", c_flags}, {"manifesto", c_manifesto}, {"ls", c_ls}, @@ -431,6 +432,7 @@ int main( int argc, char **argv ) //init basic buffer g_buf = buf_init(); buf_size( g_buf, DEFAULT_BLK_SIZE ); + buf_zero( g_buf ); struct term_screen ts; memset( &ts, 0, sizeof(ts) ); @@ -32,7 +32,7 @@ int c_seek( cmd_arg_t *arg ); int c_size( cmd_arg_t *arg ); int c_write( cmd_arg_t *arg ); int c_writes( cmd_arg_t *arg ); - +int c_resize( cmd_arg_t *arg ); #endif
\ No newline at end of file |