void do_write( tsh_session_t * psession) { int32 ret; char dir[MAX_PATH]; tfile_handle_t hfile; char openmode[2]; char * buf; if (psession->argc == 3 && !strcmp(psession->argv[1], "-a")) { strcpy(openmode, "a"); strcpy(dir, psession->argv[2]); } else if (psession->argc == 2) { strcpy(openmode, "w"); strcpy(dir, psession->argv[1]); } else { printf("Usage: write [-a] <file name>\n"); return; } buf = _get_input(); if ((ret = TFFS_fopen(psession->htffs, dir, openmode, &hfile)) != TFFS_OK) { print_error("TFFS_fopen", ret); return; } ret = TFFS_fwrite(hfile, strlen(buf), (unsigned char *)buf); if (ret < 0 || ret != strlen(buf)) { print_error("TFFS_fwrite", ret); return; } if ((ret = TFFS_fclose(hfile)) != TFFS_OK) { print_error("TFFS_fclose", ret); return; } printf("\n============================================================\n"); printf("wrote %zu bytes to file %s ok.\n", strlen(buf), dir); free(buf); }
static void _parse_input(CTX *ctx) { static char *params = NULL; int c = getch(); switch( c ) { case KEY_UP: _list_scroll( ctx, SCROLL_UP, 1 ); break; case KEY_DOWN: _list_scroll( ctx, SCROLL_DOWN, 1 ); break; case KEY_NPAGE: _list_scroll( ctx, SCROLL_DOWN, ctx->rows - 4 ); break; case KEY_PPAGE: _list_scroll( ctx, SCROLL_UP, ctx->rows - 4 ); break; case '\n': /* edit & compile code */ { char *func = ll_access( ctx->symbols.func, ctx->symbols.selected_offset ); char *sig = ll_access( ctx->symbols.sig, ctx->symbols.selected_offset ); char *lib = ll_access( ctx->symbols.lib, ctx->symbols.selected_offset ); /* check for signature */ /* the assumption here is that the sig by this stage has been validated elsewhere */ if( memcmp( sig, "??", 2 ) != 0 ) { /* has signature */ char path[500]; sprintf( path, "%s/.preloader/%s-%s.%s.c", getenv("HOME"), ctx->hash, _strip_path( ctx->filename ), func ); /* check if file exists */ struct stat s; /* populate if file doesn't exist or is empty */ if( stat( path, &s ) < 0 || !s.st_size ) { FILE *f = fopen( path, "a" ); if( f ) { char *code = code_gen( func, sig, _strip_path( lib ) ); if( code ) { fwrite( code, 1, strlen( code ), f ); N_FREE( code ); } fclose(f); } } /* TODO: hard-coded to vim now; allow editor selection through config */ _exec_target( "vim", path, NULL, EXEC_NOPROMPT ); /* compile code */ /* remove previous object file */ char obj_path[500]; strncpy( obj_path, path, sizeof( obj_path )- 1 ); obj_path[strlen( obj_path ) - 1] = '\0'; strncat( obj_path, "o", sizeof( obj_path ) - 1); _exec_target( "rm -f", obj_path, NULL, EXEC_NOPROMPT ); /* compile PIC code */ strncat( path, " -o ", sizeof( path ) - 1); strncat( path, obj_path, sizeof( path ) - 1); _exec_target( "gcc -fPIC -c", path, NULL, EXEC_NOPROMPT ); /* check if object file is present */ if( stat( obj_path, &s ) < 0 ) { /* compilation failed */ _exec_target( "echo", "Compilation failed! Please fix errors.", NULL, EXEC_PROMPT ); ctx->symbols.flags[ctx->symbols.selected_offset] |= SYMBOL_INVALID; ctx->symbols.flags[ctx->symbols.selected_offset] &= ~SYMBOL_SELECTED; } else { /* compilation succeeded */ ctx->symbols.flags[ctx->symbols.selected_offset] &= ~SYMBOL_INVALID; } } else { /* no signature */ _show_notification( ctx, "Please first enter a signature for this function!" ); } } break; case 's': /* edit function signature */ { char *sig = _get_input( ctx, "function signature", (char *)ll_access( ctx->symbols.sig, ctx->symbols.selected_offset ) ); if( ( sig = _validate_sig( sig ) ) ) database_add_sig( ctx->db, (char *)ll_access( ctx->symbols.func, ctx->symbols.selected_offset ), sig ); /* refresh sig list */ ctx->symbols.sig = database_get_sigs( ctx->db, &ctx->symbols.num_sigs ); } break; case 0x20: /* select symbol for preloading */ if( ctx->symbols.flags[ctx->symbols.selected_offset] & SYMBOL_INVALID ) { _show_notification( ctx, "Could not compile this wrapper. Please check code!" ); } else { ctx->symbols.flags[ctx->symbols.selected_offset] ^= SYMBOL_SELECTED; } break; case 'r': /* enter params then execute */ /* todo: check if function with invalid code is selected */ { char tmp[300]; sprintf( tmp, "'%s' args", _strip_path( ctx->filename ) ); params = _get_input( ctx, tmp, params ); } case 'R': /* execute with previous params */ /* todo: check if function with invalid code is selected */ _exec_target( ctx->filename, params, NULL, EXEC_PROMPT ); break; case 'q': ctx->running = 0; break; } }