aboutsummaryrefslogtreecommitdiffstats
path: root/cmd/cmd_write.c
diff options
context:
space:
mode:
Diffstat (limited to 'cmd/cmd_write.c')
-rw-r--r--cmd/cmd_write.c91
1 files changed, 91 insertions, 0 deletions
diff --git a/cmd/cmd_write.c b/cmd/cmd_write.c
new file mode 100644
index 0000000..f5d6e01
--- /dev/null
+++ b/cmd/cmd_write.c
@@ -0,0 +1,91 @@
+#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;
+
+//support masks
+int c_write( cmd_arg_t *arg)
+{
+ /*
+ anonymous function
+ */
+ uint8_t hex2u8( uint8_t *buf )
+ {
+ uint8_t ret = 0x00;
+ unsigned long int uli;
+ char str[3];
+ str[0] = buf[0];
+ str[1] = buf[1];
+ str[2] = 0;
+
+ uli = strtoul( str, NULL, 16 );
+
+ ret = uli;
+
+ return ret;
+ }
+
+ int argc = arg->argc;
+ char **argv = arg->argv;
+ int i;
+ uint8_t *buf = NULL;
+ int fret;
+
+ if ( argc != 1 )
+ {
+ printf("One argument needed\n");
+ return -1;
+ }
+
+ if ( (strlen(argv[0])%2) != 0 )
+ {
+ printf("Input string should be ( str mod 2 == 0) \n");
+ return -1;
+ }
+
+ for (i=0;i<strlen(argv[0]);i++)
+ {
+ if ( !isxdigit(argv[0][i]) )
+ {
+ printf("CH %c not hexlike at pos %d\n", argv[0][i], i);
+ return -1;
+ }
+ }
+
+ if (strlen(argv[0]) > g_buf->size*2)
+ {
+ printf("Input param bigger then buffer\n");
+ return -1;
+ }
+
+
+ for (i=0; i<strlen(argv[0]); i+=2)
+ {
+ printf("%02x ",(unsigned char)hex2u8((unsigned char*)&argv[0][i]));
+ }
+ printf("\n");
+
+
+ buf = malloc(strlen(argv[0])/2);
+ for (i=0; i<(strlen(argv[0])/2); i++)
+ {
+ buf[i] = hex2u8((unsigned char*)&argv[0][i*2]);
+ }
+
+ memcpy( g_buf->buf, buf, strlen(argv[0])/2 );
+ fret = file_write_blk( g_file, g_buf->buf );
+ free( buf );
+
+ if ( fret < 0)
+ {
+ printf("Couldnt write block to file\n");
+ return -1;
+ }
+
+ return 0;
+} \ No newline at end of file