From 60f08b0a4676943651bc7b66cb8531325b1c6132 Mon Sep 17 00:00:00 2001 From: FreeArtMan Date: Tue, 24 May 2016 00:00:45 +0100 Subject: Added resize c --- .gitignore | 1 + buf.c | 15 +++++++++++++++ buf.h | 3 +++ cmd/cmd_close.c | 7 +++++++ cmd/cmd_pos.c | 2 +- cmd/cmd_read.c | 3 +-- cmd/cmd_resize.c | 40 ++++++++++++++++++++++++++++++++++++++++ cmd/cmd_seek.c | 2 +- core.c | 26 ++++++++++++++++++++++++++ core.h | 1 + ihe.c | 14 ++++++++------ ihe.h | 2 +- 12 files changed, 105 insertions(+), 11 deletions(-) create mode 100644 cmd/cmd_resize.c diff --git a/.gitignore b/.gitignore index 313f488..d28332c 100644 --- a/.gitignore +++ b/.gitignore @@ -4,3 +4,4 @@ TODO test/* *.so *.a +log.txt diff --git a/buf.c b/buf.c index 440ac76..d0519e5 100644 --- a/buf.c +++ b/buf.c @@ -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) diff --git a/buf.h b/buf.h index 8ca6014..72bbc45 100644 --- a/buf.h +++ b/buf.h @@ -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; diff --git a/core.c b/core.c index 90f8fd9..7de3085 100644 --- a/core.c +++ b/core.c @@ -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; diff --git a/core.h b/core.h index da8c911..638cf3c 100644 --- a/core.h +++ b/core.h @@ -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 ); diff --git a/ihe.c b/ihe.c index a2769f5..9e0cb20 100644 --- a/ihe.c +++ b/ihe.c @@ -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) ); diff --git a/ihe.h b/ihe.h index fd2c0b2..798915a 100644 --- a/ihe.h +++ b/ihe.h @@ -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 -- cgit v1.2.3