void get_conf_file(t_vars *v) { char **conf_lines; char *conf; int i; i = 0; if ((conf = get_the_conf_file(".42shrc"))) { conf_lines = get_file_lines(conf); while (conf_lines[i]) { if ((v->argc = get_argc(conf_lines[i])) > 1) { v->argv = get_argv(conf_lines[i]); global_exec(v); v->argv = my_xfree_tab(v->argv); } else my_puterror("Bad config line format in config file.\n"); i += 1; } conf_lines = my_xfree_tab(conf_lines); conf = my_xxfree(conf); } }
long parda_seperate_file(char inputFileName[], int processor_number, long lines) { if (lines == -1) lines = get_file_lines(inputFileName); int psize = processor_number; if(!is_binary) seperate_textfile(inputFileName, psize, lines); else seperate_binaryfile(inputFileName, psize, lines); char linesFile[200]; sprintf(linesFile, "%s_lines_%ld.txt", inputFileName, lines); FILE* tfile = fopen(linesFile, "w"); fprintf(tfile, "%ld", lines); fclose(tfile); return lines; }
void print_game_information(int gameCounter) { FILE *playLog; int playNumber; char dateExtended[32]; char playerName1[30]; char playerName2[30]; int dimension; int moveX; char moveY; int lineCounter=0; int headLinesNumber=6; char logName[255]; sprintf(logName, "data/logs/TripletsLog-%d.txt", gameCounter); lineCounter=get_file_lines(logName); lineCounter-=headLinesNumber; lineCounter++; // to add the last line which doesn't have /n !! playLog = fopen(logName, "rt"); if(playLog == NULL) { printf("\nERR: Unable to read Log!"); } else { gameCounter=0; dimension=0; fscanf(playLog, "--Triplets Log--\n"); fscanf(playLog, "Game #%d\n", &gameCounter); fscanf(playLog, "Matrix Dimension: %dx%d\n", &dimension, &dimension); fscanf(playLog, "Started on: %s\n", dateExtended); fscanf(playLog, "Player %s ; Play %d ; Move [%d][%c]\n", playerName1, &playNumber, &moveX, &moveY); // for the first player fscanf(playLog, "Player %s ; Play %d ; Move [%d][%c]\n", playerName2, &playNumber, &moveX, &moveY); // for the first player printf("#%d Game - Players: %s vs. %s | Date: %s\n", gameCounter, playerName1, playerName2, dateExtended); } fclose(playLog); }
int recursively_install(char* pkg_name, char* pkg_version, char* install_root_name, char* link_to_root, char* overlay_path, int is_upgrade, int overwrite_config, int overwrite_other_package_files, char* tmp_dir, opkg_conf* conf, string_map* package_data, string_map* install_called_pkgs) { int err=0; /* variables not allocated in this function, do not need to be freed */ string_map* install_pkg_data = get_package_with_version(package_data, pkg_name, pkg_version); char* src_file_path = get_string_map_element(install_pkg_data, "Install-File-Location"); char* src_id = get_string_map_element(install_pkg_data, "Source-ID"); char* pkg_filename = get_string_map_element(install_pkg_data, "Filename"); string_map* pkg_dependencies = get_string_map_element(install_pkg_data, "Required-Depends"); char* install_root_path = get_string_map_element(conf->dest_names, install_root_name); char* link_root_path = link_to_root != NULL && safe_strcmp(link_to_root, install_root_name) != 0 ? get_string_map_element(conf->dest_names, link_to_root) : NULL; char* base_url = NULL; /* variables that may need to be freed */ char* pkg_dest = NULL; string_map* files_to_link = NULL; char* info_dir = NULL; char* control_name_prefix = NULL; char* list_file_name = NULL; string_map* conf_files = NULL; string_map* copied_conf_files = NULL; int install_root_len = strlen(install_root_path); char* fs_terminated_install_root = install_root_path[install_root_len-1] == '/' ? strdup(install_root_path) : dynamic_strcat(2, install_root_path, "/"); int overlay_root_len; char* fs_terminated_overlay_root = NULL; if(overlay_path != NULL) { overlay_root_len = strlen(overlay_path); fs_terminated_overlay_root = overlay_path[overlay_root_len-1] == '/' ? strdup(overlay_path) : dynamic_strcat(2, overlay_path, "/"); } else { fs_terminated_overlay_root = strdup(fs_terminated_install_root); } int link_root_len; char* fs_terminated_link_root = NULL; if(link_root_path != NULL) { link_root_len = strlen(link_root_path); fs_terminated_link_root = link_root_path[link_root_len-1] == '/' ? strdup(link_root_path) : dynamic_strcat(2, link_root_path, "/"); } set_string_map_element(install_called_pkgs, pkg_name, strdup("D")); if(pkg_dependencies != NULL) { //recurse unsigned long num_deps; char** deps = get_string_map_keys(pkg_dependencies, &num_deps); int dep_index; for(dep_index=0; err == 0 && dep_index < num_deps ; dep_index++) { if(get_string_map_element(install_called_pkgs, deps[dep_index]) == NULL ) { char** dep_def = get_string_map_element(pkg_dependencies, deps[dep_index]); int is_current; char* matching_version; string_map* dep_pkg = get_package_current_or_latest_matching(package_data, deps[dep_index], dep_def, &is_current, &matching_version); if(dep_pkg != NULL) { char* dep_status = get_string_map_element(dep_pkg, "Status"); if(strstr(dep_status, " half-installed") != NULL) { err = recursively_install(deps[dep_index], matching_version, install_root_name, link_to_root, overlay_path, is_upgrade, overwrite_config, overwrite_other_package_files, tmp_dir, conf, package_data, install_called_pkgs); } } else { err = 1; } } } } if(install_root_path == NULL || ( src_id == NULL && install_pkg_data == NULL) || (pkg_filename == NULL && install_pkg_data == NULL) ) { //sanity check err = 1; } if(err == 0) { printf("Preparing to install package %s...\n", pkg_name); } if(err == 0 && src_file_path == NULL) { //determine source url string_map* src_lists[2] = { conf->gzip_sources, conf->plain_sources }; int src_list_index; for(src_list_index=0; src_list_index < 2 && base_url == NULL; src_list_index++) { base_url = (char*)get_string_map_element(src_lists[src_list_index], src_id); } err = base_url == NULL ? 1 : err; if(err == 1) { fprintf(stderr, "ERROR: Could determine download URL for package %s\n", pkg_name); } } if(err == 0 && src_file_path == NULL) { //download package printf("\tDownloading...\n"); char* src_url = dynamic_strcat(3, base_url, "/", pkg_filename); pkg_dest = dynamic_strcat(3, tmp_dir, "/", pkg_name); FILE* package_file = fopen(pkg_dest, "w"); if(package_file != NULL) { err = write_url_to_stream(src_url, "gpkg", NULL, package_file, NULL); fclose(package_file); } else { err = 1; } if(err == 1) { fprintf(stderr, "ERROR: Could not download package %s\n", pkg_name); } free(src_url); } if(err == 0 && src_file_path != NULL) { pkg_dest = strdup(src_file_path); } if(err == 0 && src_file_path == NULL) { //check md5sum char* md5sum = file_md5sum_alloc(pkg_dest); char* expected_md5sum = (char*)get_string_map_element(install_pkg_data, "MD5Sum"); //printf("md5sum = %s\n", md5sum); //printf("package md5sum = %s\n", (char*)get_string_map_element(install_pkg_data, "MD5Sum")); if(md5sum == NULL || expected_md5sum == NULL) { fprintf(stderr, "ERROR: Expected MD5Sum for %s not specified, cannot verify package\n", pkg_name); err = 1; } else if (safe_strcmp(md5sum, expected_md5sum) != 0) { fprintf(stderr, "ERROR: MD5Sum mismatch for %s package\n", pkg_name); fprintf(stderr, " Expected: %s\n", expected_md5sum); fprintf(stderr, " Downloaded: %s\n\n", md5sum); err = 1; } else { printf("\tDownloaded %s successfully.\n\tInstalling %s...\n", pkg_name, pkg_name); } if(md5sum != NULL) { free(md5sum); } } if(err == 0) { // Extract list file contaiing list of files to install info_dir = dynamic_strcat(2, fs_terminated_overlay_root, "usr/lib/opkg/info"); control_name_prefix = dynamic_strcat(4, info_dir, "/", pkg_name, "."); list_file_name = dynamic_strcat(4, info_dir, "/", pkg_name, ".list"); mkdir_p(info_dir, S_IRWXU | S_IRGRP | S_IXGRP | S_IROTH | S_IXOTH ); FILE* list_file = fopen(list_file_name, "w"); deb_extract( pkg_dest, list_file, extract_quiet | extract_data_tar_gz | extract_list, NULL, NULL, &err); fclose(list_file); if(err) { rm_r(list_file_name); fprintf(stderr, "ERROR: could not extract file list from packge %s.\n", pkg_name); fprintf(stderr, " package file may be corrupt\n\n"); } } if(err == 0) { //extract control files deb_extract( pkg_dest, stderr, extract_control_tar_gz | extract_all_to_fs| extract_preserve_date | extract_unconditional, control_name_prefix, NULL, &err); if(err) { fprintf(stderr, "ERROR: could not extract control files from packge %s.\n", pkg_name); fprintf(stderr, " package file may be corrupt\n\n"); } } if(err == 0) { //check for file conflicts & correct list file to contain real root name in file paths unsigned long num_list_lines; char** list_file_lines = get_file_lines(list_file_name, &num_list_lines); char* conf_file_path = dynamic_strcat(4, info_dir, "/", pkg_name, ".conffiles"); if(path_exists(conf_file_path)) { unsigned long num_conf_lines; char** conf_file_lines = get_file_lines(conf_file_path, &num_conf_lines); int conf_line_index; conf_files = initialize_string_map(1); for(conf_line_index=0; conf_line_index < num_conf_lines; conf_line_index++) { char* adjusted_conf_path = dynamic_strcat(2, fs_terminated_install_root, conf_file_lines[conf_line_index] + 1); set_string_map_element(conf_files, adjusted_conf_path, strdup("D")); free(adjusted_conf_path); } free_null_terminated_string_array(conf_file_lines); } free(conf_file_path); FILE* list_file = fopen(list_file_name, "w"); int line_index; for(line_index=0; line_index < num_list_lines && (!err) ; line_index++) { int line_len = strlen( list_file_lines[line_index] ); if(line_len > 2) { if(list_file_lines[line_index][0] == '.' && list_file_lines[line_index][1] == '/' && list_file_lines[line_index][line_len-1] != '/') { char* adjusted_file_path = dynamic_strcat(2, fs_terminated_install_root, list_file_lines[line_index] + 2); int is_conf_file = conf_files != NULL ? (get_string_map_element(conf_files, adjusted_file_path) != NULL ? 1 : 0) : 0; if(strcmp(pkg_name, "opkg") == 0 && strcmp(list_file_lines[line_index], "./bin/opkg") == 0 && path_exists("/bin/opkg") == PATH_IS_SYMLINK) { //very special case: we're installing opkg, and here all the preliminary checks have already been passed //remove symlink placeholder to gpkg from /bin/opkg if it exists rm_r("/bin/opkg"); } err = path_exists(adjusted_file_path) && is_conf_file == 0 && overwrite_other_package_files == 0 ? 1 : 0; if(err) { fprintf(stderr, "ERROR: file '%s'\n", adjusted_file_path); fprintf(stderr, " from package %s already exists.\n\n", pkg_name); } else { fprintf(list_file, "%s\n", adjusted_file_path); if(link_root_path != NULL) { char* link_to_path = dynamic_strcat(2, fs_terminated_link_root, list_file_lines[line_index] + 2); files_to_link = files_to_link == NULL ? initialize_string_map(1) : files_to_link; set_string_map_element(files_to_link, adjusted_file_path, link_to_path); //don't free link_to_path, should be freed with files_to_link map } if(is_conf_file && path_exists(adjusted_file_path) && overwrite_config == 0) { char* tmp_conf_path = dynamic_strcat(2, tmp_dir, adjusted_file_path); cp(adjusted_file_path, tmp_conf_path); copied_conf_files = copied_conf_files == NULL ? initialize_string_map(1) : copied_conf_files; set_string_map_element(copied_conf_files, adjusted_file_path, tmp_conf_path); //don't free tmp_conf_path, should be freed with copied_conf_files map } } free(adjusted_file_path); } } } fclose(list_file); if(list_file_lines != NULL) { free_null_terminated_string_array(list_file_lines); } } if(err == 0) { //run preinst err = run_script_if_exists(install_root_path, link_root_path, pkg_name, "preinst", (is_upgrade ? "upgrade" : "install") ); } if(err == 0) { //extract package files deb_extract( pkg_dest, stderr, extract_data_tar_gz | extract_all_to_fs| extract_preserve_date| extract_unconditional, fs_terminated_overlay_root, NULL, &err); if(err) { fprintf(stderr, "ERROR: could not extract application files from packge %s.\n", pkg_name); fprintf(stderr, " package file may be corrupt\n\n"); } //move any conf files back if(copied_conf_files != NULL) { unsigned long num_conf_paths; char** conf_paths = get_string_map_keys(copied_conf_files, &num_conf_paths); int conf_index; for(conf_index=0; conf_index < num_conf_paths; conf_index++) { char* tmp_conf_path = get_string_map_element(copied_conf_files, conf_paths[conf_index]); cp(tmp_conf_path, conf_paths[conf_index]); } destroy_string_map(copied_conf_files, DESTROY_MODE_FREE_VALUES, &num_conf_paths); if(conf_paths != NULL ) { free_null_terminated_string_array(conf_paths); } copied_conf_files = NULL; } } if(err == 0 && files_to_link != NULL) { unsigned long num_files; char** real_files = get_string_map_keys(files_to_link, &num_files); int file_index; if(num_files > 0) { char* link_file_name = dynamic_strcat(4, info_dir, "/", pkg_name, ".linked"); FILE* link_file = fopen(link_file_name, "w"); for(file_index=0; link_file != NULL && file_index < num_files; file_index++) { char* link_path = get_string_map_element(files_to_link, real_files[file_index]); if(!path_exists(link_path)) { int sym_success; mkdir_p(link_path, S_IRWXU | S_IRGRP | S_IXGRP | S_IROTH | S_IXOTH ); rm_r(link_path); sym_success = symlink(real_files[file_index], link_path); fprintf(link_file, "%s\n", link_path); } } if(link_file != NULL) { fclose(link_file); } free(link_file_name); } destroy_string_map(files_to_link, DESTROY_MODE_FREE_VALUES, &num_files); free_null_terminated_string_array(real_files); files_to_link = NULL; } if(err == 0) { //run postinst int warn = run_script_if_exists(install_root_path, link_root_path, pkg_name, "postinst", (is_upgrade ? "upgrade" : "install") ); if(warn != 0) { fprintf(stderr, "Warning: postinstall script failed for package %s.\n", pkg_name); } } if(err == 0) { // remove downloaded package file in tmp dir & print success if(src_file_path == NULL) { rm_r(pkg_dest); } printf("\tSuccessfully installed %s.\n", pkg_name); } //cleanup unsigned long num_destroyed; free_if_not_null(pkg_dest); free_if_not_null(info_dir); free_if_not_null(control_name_prefix); free_if_not_null(list_file_name); free_if_not_null(fs_terminated_install_root); free_if_not_null(fs_terminated_overlay_root); free_if_not_null(fs_terminated_link_root); if(files_to_link != NULL) { destroy_string_map(files_to_link, DESTROY_MODE_FREE_VALUES, &num_destroyed); } if(conf_files != NULL) { destroy_string_map(conf_files, DESTROY_MODE_FREE_VALUES, &num_destroyed); } if(copied_conf_files!= NULL){ destroy_string_map(copied_conf_files, DESTROY_MODE_FREE_VALUES, &num_destroyed); } return err; }
static int masm_main_loop(char * obj_file,char * src_file) { FILE * obj_fp, *src_fp; char buf[BUFSIZ] = {0}; int length; char * p, * q; src_fp = obj_fp = NULL; uint32_t counter = 0; uint32_t lines = 0; hash_table * label_hash = hash_create(512); char op_name[128]; char label[128]; char rd[6]; char rs[6]; char rt[6]; char imm[20]; int32_t rd_num,rs_num,rt_num,imm_num; if((src_fp = fopen(src_file,"r")) == NULL) { printf("Can not open %s:%s\n",src_file,strerror(errno)); } if((obj_fp = fopen(obj_file,"w+")) == NULL) { printf("Can not open %s:%s\n",obj_file,strerror(errno)); } int total_lines = get_file_lines(src_fp); uint32_t * instruction = calloc(1,total_lines * sizeof(uint32_t)); var_t * var = calloc(1,total_lines * sizeof(var_t)); int var_count = 0; fseek(src_fp,0L,SEEK_SET); while(1) { fgets(buf,BUFSIZ,src_fp); if(feof(src_fp)) { break; } lines++; length = strlen(buf); p = buf; //skip whitespace while(length > 0 &&isspace(p[0])) { length--; p++; } //printf("length=%d\t%s",length,buf+i); if(p[0] == ';' || p[0] == '\0') { continue; } q = get_first_token(p); strncpy(op_name, p , q-p); op_name[q-p] = '\0'; if(line_has_label(p)) { /* it is label */ label_t l; l.name = op_name; l.real_line = lines; l.line = counter; hash_add_item(&label_hash,str2hash(op_name),&l); p = skip_label_wthie(q); /* 获得字符串 */ q = get_opcode_token(p); strncpy(op_name, p , q-p); op_name[q-p] = '\0'; //printf("%s",op_name); } /* p now a opcode start q-p is opecode */ int op_index = verify_opcode(op_name,lines); q = skip_wthie(q); p = q; /* now at rd */ switch(op_index) { #if 1 case ADD: case SUB: case MUL: case DIV: case MOD: case AND: case OR: case NOT: case XOR: case LWORD: case SWORD: /* 获得字符串 */ q = get_reg_token(p); strncpy(rd, p , q-p); rd[q-p] = '\0'; q = skip_reg_wthie(q); p = q; q = get_reg_token(p); strncpy(rs, p , q-p); rs[q-p] = '\0'; q = skip_reg_wthie(q); p = q; q = get_reg_token(p); strncpy(rt, p , q-p); rt[q-p] = '\0'; rd_num = get_reg_index(rd,lines); rs_num = get_reg_index(rs,lines); rt_num = get_reg_index(rt,lines); instruction[counter] = (op_index << 26) | (rd_num << 21) | (rs_num << 16)| (rt_num << 11); break; ///C语言中的左移就是逻辑左移,而右移, ///对于无符号数来说就是逻辑右移, ///对有符号来说就是算术右移 ///想要实现符号左移,比较麻烦一点,首先保存最高位,然后左移之后补上最高位。 case SLL: case SLR: case SAL: case SAR: case ADDI: case ANDI: ///这里的立即数是0扩展的。 case ORI: case XORI: case LUI: ///哦,载入高16位数啊,靠,那么低位怎么载入呢?用ori q = get_reg_token(p); strncpy(rd, p , q-p); rd[q-p] = '\0'; q = skip_reg_wthie(q); p = q; q = get_reg_token(p); strncpy(rs, p , q-p); rs[q-p] = '\0'; q = skip_reg_wthie(q); p = q; q = get_reg_token(p); strncpy(imm, p , q-p); imm[q-p] = '\0'; rd_num = get_reg_index(rd,lines); rs_num = get_reg_index(rs,lines); imm_num = atoi(imm); if(imm_num > 32767 || imm_num < -32768) { printf("________\n"); printf("[ERROR 6] line: %d imm num is too lager or too smaller\n",lines); } instruction[counter] = (op_index << 26) | (rd_num << 21) | (rs_num << 16)| (imm_num & 0x0000ffff); break; case LESS: case GREAT: case LESSE: case GREATE: case LESSU: case GREATU: case LESSEU: case GREATEU: case EQUAL: case UEQUAL: q = get_reg_token(p); strncpy(rd, p , q-p); rd[q-p] = '\0'; q = skip_reg_wthie(q); p = q; q = get_reg_token(p); strncpy(rs, p , q-p); rs[q-p] = '\0'; q = skip_reg_wthie(q); p = q; q = get_reg_token(p); strncpy(label, p , q-p); label[q-p] = '\0'; rd_num = get_reg_index(rd,lines); rs_num = get_reg_index(rs,lines); var[var_count].name = malloc(strlen(label) + 1); strcpy(var[var_count].name, label); var[var_count].line = counter; var_count++; instruction[counter] = (op_index << 26) | (rd_num << 21) | (rs_num << 16) | 0x0; break; case JMP: q = get_reg_token(p); strncpy(label, p , q-p); label[q-p] = '\0'; var[var_count].name = malloc(strlen(label) + 1); strcpy(var[var_count].name, label); var[var_count].line = counter; var_count++; instruction[counter] = (op_index << 26); break; /* 存储指令 */ case MOV: q = get_reg_token(p); strncpy(rd, p , q-p); rd[q-p] = '\0'; q = skip_reg_wthie(q); p = q; q = get_reg_token(p); strncpy(rs, p , q-p); rs[q-p] = '\0'; rd_num = get_reg_index(rd,lines); rs_num = get_reg_index(rs,lines); instruction[counter] = (op_index << 26) | (rd_num << 21) | (rs_num << 16) | 0x0; break; default: break; #endif } counter++; } /* 第二趟汇编 */ struct blist * head; for(int i = 0; i < var_count; i++) { if((head = hash_lookup_item(label_hash,str2hash(var[i].name),&var[i])) != NULL) { label_t * node = head->item; int imm_2 = node->line - var[i].line; if((instruction[var[i].line] >> 26) == JMP) { if(imm_2 > 33554431 || imm_2 < -33554432) { printf("[ERROR 7] line: %d imm num is too lager or too smaller\n",lines); } instruction[var[i].line] |= imm_2 & 0x03ffffff; } else { if(imm_2 > 32767 || imm_2 < -32768) { printf("[ERROR 6] line: %d imm num is too lager or too smaller\n",lines); } instruction[var[i].line] |= imm_2 & 0x0000ffff; } }
void loadLogs(int gameCounter) { FILE *playLog; int playNumber; char dateExtended[32]; char playerName[30]; int dimension; int i; int moveX; char moveY; int lineCounter=0; int headLinesNumber=6; char logName[255]; sprintf(logName, "data/logs/TripletsLog-%d.txt", gameCounter); lineCounter=get_file_lines(logName); lineCounter-=headLinesNumber; lineCounter++; // to add the last line which doesn't have /n !! playLog = fopen(logName, "rt"); if(playLog == NULL) { printf("\n\tERR: Unable to read Log!"); } else { gameCounter=0; dimension=0; fscanf(playLog, "--Triplets Log--\n"); fscanf(playLog, "Game #%d\n", &gameCounter); fscanf(playLog, "Matrix Dimension: %dx%d\n", &dimension, &dimension); fscanf(playLog, "Started on: %s\n", dateExtended); board_set_size(dimension); board_set_empty(); clearscr(); board_print_raw(); init_players(); for (i=0; i<lineCounter; i++) { printf("\nPress any key to print next move..."); readchar(); clearscr(); fscanf(playLog, "Player %s ; Play %d ; Move [%d][%c]\n", playerName, &playNumber, &moveX, &moveY); board_set_content_row_col(moveX, moveY); if (i==lineCounter-1) { finish_gamePL(get_current_game_ptr()->board); } board_print_raw(); printf("\nPlayer Name: %s - Play Number: %d - Move: [%d][%c]\n",playerName, playNumber, moveX, moveY); // was just to test if it's reading right cmp.tmp=cmp.current_player_move; //swap current player cmp.current_player_move=cmp.previous_player_move; // cmp.previous_player_move=cmp.tmp; } printf("\nWINNER: %s\a\n", playerName); printf("\nPress any key to go back to menu..."); readchar(); } fclose(playLog); }