char *get_app_dir() { char *path = NULL, *dir = NULL; #ifdef _WIN32 char drive_buf[_MAX_DRIVE], dir_buf[_MAX_DIR]; if ((path = get_exec_path()) == NULL) goto end; if(_splitpath_s(path, drive_buf, _MAX_DRIVE, dir_buf, _MAX_DIR, NULL, 0, NULL, 0) != 0) { perror("get_app_dir"); goto end; } if ((dir = _strdup(dir_buf)) == NULL) { perror("get_app_dir"); goto end; } #else char *buf = NULL; if ((path = get_exec_path()) == NULL) goto end; /* dirname returns a pointer to internal storage space allocated on the first call */ if ((buf = dirname(path)) == NULL) { perror("get_app_dir"); goto end; } if ((dir = strdup(buf)) == NULL) { perror("get_app_dir"); goto end; } #endif end: free(path); return dir; }
void process_entry(t_sh_token *token, t_envp *envp) { t_int32 ret; t_mysh_er er; t_struct_linker job_list; init_linker(&job_list); while (token) { er = 0; if (token->up && token->flag & P_SEPARATOR_L) { if ((ret = check_if_builtin(token->up->str)) >= 0) { if (test_job(&job_list, token->flag)) er |= msh_builtin(&job_list, ret, token, envp); } else if ((ret = get_exec_path(envp, token))) er |= ret; else if (token->flag & P_SCOL_F || test_job(&job_list, token->flag)) er |= msh_exec(&job_list, token, envp); } msh_error(er, token->up); token = token->next; } free_jobs((t_job *)job_list.first); }
void acl_proctl_daemon_path(char *buf, size_t size) { const char *myname = "acl_proctl_daemon_path"; if (buf == NULL || size <= 0) { acl_msg_error("%s(%d): input invalid", myname, __LINE__); return; } get_exec_path(buf, size); }
void main(int argc, char *argv[]) { get_exec_path( 0 ); std::cout << execution_path << std::endl; if ( execution_path.find( "build" ) != std::string::npos ) { get_exec_path( 4 ); std::cout << execution_path << std::endl; } fonts_dir.insert( 0, execution_path ); meshes_dir.insert( 0, execution_path ); sprites_dir.insert( 0, execution_path ); shaders_dir.insert( 0, execution_path ); textures_dir.insert( 0, execution_path ); system( "pause" ); ///the line below turns on memory leak detection in debug mode _CrtSetDbgFlag ( _CRTDBG_ALLOC_MEM_DF | _CRTDBG_LEAK_CHECK_DF ); //_CrtSetBreakAlloc( 657 ); dbg_log::FileLog_Mgr::RegisterNewLog( execution_path + "Memory Issues.log", true ); dbg_log::FileLog_Mgr::Start(); /// Initialize the OpenGL engine get_cooper3d().Init(); /// Initialize the game assets, controls, shaders, etc get_game_engine().Init(); ///Setting the update and draw functions to call get_cooper3d().set_Update_f( std::bind( &game::Update, &get_game_engine() ) ); get_cooper3d().set_Draw_f( std::bind( &game::Draw, &get_game_engine() ) ); /// Initiate game model get_cooper3d().Run(); get_game_engine().Deinit(); dbg_log::FileLog_Mgr::CloseLogs(); exit(0); }
char* get_root() { int bufsize = 1023; char *buf = calloc(bufsize, sizeof(char)); char *root = calloc(bufsize, sizeof(char)); get_exec_path(buf, bufsize); int index = lastIndexOf(buf, "/"); substr(root, bufsize, buf, index); strcat(root, "/../../../html"); log_info("Setting root: %s", root); free(buf); return root; }
static void get_asm_insns(uint8_t *image, size_t len, unsigned long base, int opcodes) { int count, i, pc = 0; char tpath[256]; struct disassemble_info info; disassembler_ftype disassemble; bfd *bfdf; memset(tpath, 0, sizeof(tpath)); get_exec_path(tpath, sizeof(tpath)); bfdf = bfd_openr(tpath, NULL); assert(bfdf); assert(bfd_check_format(bfdf, bfd_object)); init_disassemble_info(&info, stdout, (fprintf_ftype) fprintf); info.arch = bfd_get_arch(bfdf); info.mach = bfd_get_mach(bfdf); info.buffer = image; info.buffer_length = len; disassemble_init_for_target(&info); disassemble = disassembler(bfdf); assert(disassemble); do { printf("%4x:\t", pc); count = disassemble(pc, &info); if (opcodes) { printf("\n\t"); for (i = 0; i < count; ++i) printf("%02x ", (uint8_t) image[pc + i]); } printf("\n"); pc += count; } while(count > 0 && pc < len); bfd_close(bfdf); }
int32_t disas_executable_and_examine(void) { int32_t rtrn; uint64_t file_size; uint32_t count = 0; int32_t file auto_close = 0; char *file_buffer = NULL; char *exec_path auto_free = NULL; /* Ask the file module for the path of the binary to test. */ rtrn = get_exec_path(&exec_path); if(rtrn < 0) { output(ERROR, "Can't get exec path\n"); return -1; } /* Open the target binary. */ file = open(exec_path, O_RDONLY); if(file < 0) { output(ERROR, "open: %s\n", strerror(errno)); return (-1); } /* Read file in to memory. */ rtrn = map_file_in(file, &file_buffer, &file_size, READ); if(rtrn < 0) { output(ERROR, "Can't memory map file\n"); return (-1); } /* Create a const pointer to the file buffer so we can avoid a warning when using cs_disasm_iter(). */ const uint8_t *file_buffer_copy = (uint8_t *)file_buffer; /* Create capstone handle.*/ csh handle; cs_open(CS_ARCH_X86, CS_MODE_32, &handle); /* Allocate memory cache for 1 instruction, to be used by cs_disasm_iter later. */ cs_insn *insn = cs_malloc(handle); uint64_t address = 0; /* Disassemble one instruction at a time & store the result into the insn variable above */ while(cs_disasm_iter(handle, (const uint8_t **)&file_buffer_copy, (unsigned long *)&file_size, &address, insn)) { /* Check for branchs in the disasembly, maybe switch to a hash map in the future. */ if(strncmp(insn->mnemonic, "jne", 3) == 0) { goto found_branch; } else if(strncmp(insn->mnemonic, "je", 2) == 0) { goto found_branch; } else if(strncmp(insn->mnemonic, "jg", 2) == 0) { goto found_branch; } else if(strncmp(insn->mnemonic, "jle", 3) == 0) { goto found_branch; } else if(strncmp(insn->mnemonic, "jl", 2) == 0) { goto found_branch; } else if(strncmp(insn->mnemonic, "jge", 3) == 0) { goto found_branch; } else if(strncmp(insn->mnemonic, "jo", 2) == 0) { goto found_branch; } else if(strncmp(insn->mnemonic, "jno", 3) == 0) { goto found_branch; } else if(strncmp(insn->mnemonic, "js", 2) == 0) { goto found_branch; } else if(strncmp(insn->mnemonic, "jns", 3) == 0) { goto found_branch; } else if(strncmp(insn->mnemonic, "jz", 2) == 0) { goto found_branch; } else if(strncmp(insn->mnemonic, "jb", 2) == 0) { goto found_branch; } else if(strncmp(insn->mnemonic, "jnae", 4) == 0) { goto found_branch; } else if(strncmp(insn->mnemonic, "jc", 2) == 0) { goto found_branch; } else if(strncmp(insn->mnemonic, "jnb", 3) == 0) { goto found_branch; } else if(strncmp(insn->mnemonic, "jae", 3) == 0) { goto found_branch; } else if(strncmp(insn->mnemonic, "jnc", 3) == 0) { goto found_branch; } else if(strncmp(insn->mnemonic, "jbe", 3) == 0) { goto found_branch; } else if(strncmp(insn->mnemonic, "jna", 3) == 0) { goto found_branch; } else if(strncmp(insn->mnemonic, "ja", 2) == 0) { goto found_branch; } else if(strncmp(insn->mnemonic, "jnbe", 4) == 0) { goto found_branch; } else if(strncmp(insn->mnemonic, "jl", 2) == 0) { goto found_branch; } else if(strncmp(insn->mnemonic, "jnge", 4) == 0) { goto found_branch; } else if(strncmp(insn->mnemonic, "jge", 3) == 0) { goto found_branch; } else if(strncmp(insn->mnemonic, "jng", 3) == 0) { goto found_branch; } else if(strncmp(insn->mnemonic, "jnle", 4) == 0) { goto found_branch; } else if(strncmp(insn->mnemonic, "jp", 2) == 0) { goto found_branch; } else if(strncmp(insn->mnemonic, "jpe", 3) == 0) { goto found_branch; } else if(strncmp(insn->mnemonic, "jnp", 3) == 0) { goto found_branch; } else if(strncmp(insn->mnemonic, "jpo", 3) == 0) { goto found_branch; } else if(strncmp(insn->mnemonic, "jcxz", 4) == 0) { goto found_branch; } else if(strncmp(insn->mnemonic, "jecxz", 5) == 0) { goto found_branch; } else { /* We didn't find a branch instruction so cotinue looping */ continue; } found_branch: /*map->exec_ctx->address_index = realloc(map->exec_ctx->address_index, sizeof(char *) * count); if(map->exec_ctx->address_index == NULL) { output(ERROR, "Can't realloc address index: %s\n", strerror(errno)); return -1; } rtrn = asprintf(&map->exec_ctx->address_index[count], "%s", insn->address); if(rtrn < 0) { output(ERROR, "Can't alloc address index slot: %s\n", strerror(errno)); return -1; } */ count++; continue; } /* Save the offset of where the program ends. */ rtrn = set_end_offset(insn[0].address); if(rtrn < 0) { output(ERROR, "Can't set end offset\n"); return -1; } cs_free(insn, 1); // release the cache memory when done munmap(file_buffer, (size_t)file_size); return 0; }