void mover_a_disco(pagina_t* * pagina, uint32_t pid, uint16_t id_segmento) { //Convierte cada id a string y despues los concatena de 2 en 2 char *nombre_archivo=generar_nombre_archivo_swap(pid, id_segmento, (*pagina)->id); char* path; path=concat_string("en_disco/",nombre_archivo); path=concat_string(path,".txt"); free(nombre_archivo); //crea un archivo y lo guarda en una carpeta interna. //el nombre se compone de pid, idsegmento y id pagina FILE* arch = fopen(path, "w"); free(path); marco_t* marco= buscar_marco_segun_id((*pagina)->marco); //Preparo el string que voy a escribir //Escribo en el archivo fwrite(marco->datos, sizeof(char), 256, arch); (*pagina)->en_disco=true; (*pagina)->tiene_marco=false; fclose(arch); aumento_cantidad_archivos_swap(); if(get_cantidad_archivos_swap() == cantidad_swap() * 4096){ loggear_info("Espacio de swap lleno."); } }
void Test_concat_string(CuTest * tc) { printf("Testing Function: concat_string\n"); char str1[]= ""; char * str2 = NULL; char str3[] = "whatup "; char * s1; int out; s1 = concat_string(str1,str3); out = strcmp(s1,"whatup "); CuAssertIntEquals(tc,0,out); free(s1); s1 = concat_string(str2,str3); out = strcmp(s1,"whatup "); CuAssertIntEquals(tc,0,out); free(s1); s1 = concat_string(str1,str3); out = strcmp(s1,"whatup "); CuAssertIntEquals(tc,0,out); free(s1); s1 = concat_string(str1,str2); out = strcmp(s1,""); CuAssertIntEquals(tc,0,out); free(s1); }
char* generar_nombre_archivo_swap(uint32_t pid, uint16_t id_segmento, uint16_t id_pagina) { char *nombre_archivo; nombre_archivo=concat_string(string_itoa(pid),string_itoa(id_segmento)); nombre_archivo=concat_string(nombre_archivo,string_itoa(id_pagina)); return nombre_archivo; }
static int header_value_cb(http_parser *p, const char *buf, size_t len) { request *req = get_current_request(p); PyObject *obj; /* DEBUG("field value:%.*s", (int)len, buf); */ if(likely(req->value== NULL)){ obj = PyBytes_FromStringAndSize(buf, len); }else{ obj = concat_string(req->value, buf, len); } if(unlikely(obj == NULL)){ req->bad_request_code = 500; return -1; } if(unlikely(PyBytes_GET_SIZE(obj) > LIMIT_REQUEST_FIELD_SIZE)){ req->bad_request_code = 400; return -1; } req->value = obj; req->last_header_element = VALUE; return 0; }
void level_snap(int i, const char *path) { char *filename; /* Convert the level name to a PNG filename. */ filename = concat_string(path, "/", base_name_sans(level_v[i].file, ".sol"), ".png", NULL); /* Initialize the game for a snapshot. */ if (game_client_init(level_v[i].file)) { union cmd cmd; cmd.type = CMD_GOAL_OPEN; game_proxy_enq(&cmd); game_client_sync(NULL); /* Render the level and grab the screen. */ video_clear(); game_client_fly(1.0f); game_kill_fade(); game_client_draw(POSE_LEVEL, 0); video_snap(filename); video_swap(); } free(filename); }
int get_next_line(int fd, char **line) { char *ret; int nb_read; static char *old; char buf[BUFF_SIZE]; while (!(ret = get_next_nl(&old))) { nb_read = read(fd, buf, BUFF_SIZE); if (!nb_read) { ret = old; *line = ret; return (0); } else if (nb_read == -1) { **line = '\0'; return (-1); } old = concat_string(old, buf, nb_read); } *line = ret; return (1); }
static int start_keybd(int c, int d) { if (d) { if (c == KEY_EXIT) return start_action(GUI_BACK, 0); if (c == SDLK_c && config_cheat()) { set_cheat(); return goto_state(&st_start); } else if (c == KEY_LEVELSHOTS && config_cheat()) { char *dir = concat_string("Screenshots/shot-", set_id(curr_set()), NULL); int i; fs_mkdir(dir); /* Iterate over all levels, taking a screenshot of each. */ for (i = 0; i < MAXLVL; i++) if (level_exists(i)) level_snap(i, dir); free(dir); } else if (config_tst_d(CONFIG_KEY_SCORE_NEXT, c)) return start_score(+1); } return 1; }
static void make_dirs_and_migrate(void) { Array items; int i; const char *src; char *dst; if (fs_mkdir("Replays")) { if ((items = fs_dir_scan("", is_replay))) { for (i = 0; i < array_len(items); i++) { src = DIR_ITEM_GET(items, i)->path; dst = concat_string("Replays/", src, NULL); fs_rename(src, dst); free(dst); } fs_dir_free(items); } } if (fs_mkdir("Scores")) { if ((items = fs_dir_scan("", is_score))) { for (i = 0; i < array_len(items); i++) { src = DIR_ITEM_GET(items, i)->path; dst = concat_string("Scores/", src + sizeof ("neverballhs-") - 1, ".txt", NULL); fs_rename(src, dst); free(dst); } fs_dir_free(items); } } fs_mkdir("Screenshots"); }
static int header_field_cb(http_parser *p, const char *buf, size_t len) { request *req = get_current_request(p); PyObject *env = NULL, *obj = NULL; #ifdef PY3 char *c1, *c2; PyObject *f, *v; #endif /* DEBUG("field key:%.*s", (int)len, buf); */ if(req->last_header_element != FIELD){ env = req->environ; if(LIMIT_REQUEST_FIELDS <= req->num_headers){ req->bad_request_code = 400; return -1; } #ifdef PY3 //TODO CHECK ERROR c1 = PyBytes_AS_STRING(req->field); f = PyUnicode_DecodeLatin1(c1, strlen(c1), NULL); c2 = PyBytes_AS_STRING(req->value); v = PyUnicode_DecodeLatin1(c2, strlen(c2), NULL); PyDict_SetItem(env, f, v); Py_DECREF(f); Py_DECREF(v); #else PyDict_SetItem(env, req->field, req->value); #endif Py_DECREF(req->field); Py_DECREF(req->value); req->field = NULL; req->value = NULL; req->num_headers++; } if(likely(req->field == NULL)){ obj = get_http_header_key(buf, len); }else{ char temp[len]; key_upper(temp, buf, len); obj = concat_string(req->field, temp, len); } if(unlikely(obj == NULL)){ req->bad_request_code = 500; return -1; } if(unlikely(PyBytes_GET_SIZE(obj) > LIMIT_REQUEST_FIELD_SIZE)){ req->bad_request_code = 400; return -1; } req->field = obj; req->last_header_element = FIELD; return 0; }
void swap_out(uint32_t pid, uint16_t id_segmento, pagina_t* * pagina) { // ----Abro el archivo---- uint16_t id_pagina = (*pagina)->id; //Convierte cada id a string y despues los concatena de 2 en 2 char *nombre_archivo=generar_nombre_archivo_swap(pid, id_segmento, id_pagina); char* path; path=concat_string("en_disco/",nombre_archivo); path=concat_string(path,".txt"); free(nombre_archivo); //crea un archivo y lo guarda en una carpeta interna. //el nombre se compone de pid, idsegmento y id pagina FILE* arch = fopen(path,"r"); // ----Le consigo un marco a la pagina---- marco_t* marco = buscar_marco_libre(); if(marco==NULL){ swap_in(pagina, id_segmento, pid); }else{ (*pagina)->marco=marco->id; } //Ahora pagina es de su marco (*pagina)->tiene_marco=true; (*pagina)->en_disco=false; //Ahora el marco es de su pagina marco = buscar_marco_segun_id((*pagina)->marco); marco->id_proceso=pid; (*pagina)->marco=marco->id; marco->ocupado=true; fread(marco->datos,sizeof(char),256,arch); // ----Finalizo---- remove(path); free(path); disminuyo_cantidad_archivos_swap(); loggear_trace("Va a memoria pagina %d del segmento %d del proceso %d en marco %d.", (*pagina)->id, id_segmento, marco->id_proceso, marco->id); }
int fs_rename(const char *src, const char *dst) { const char *write_dir; char *real_src, *real_dst; int rc = 0; if ((write_dir = fs_get_write_dir())) { real_src = concat_string(write_dir, "/", src, NULL); real_dst = concat_string(write_dir, "/", dst, NULL); rc = file_rename(real_src, real_dst); free(real_src); free(real_dst); } return rc; }
void destruir_archivos_swapp_proceso(uint32_t pid, segmento_t* segmento) { int i; char* nombre_archivo; for(i=0;i<list_size(segmento->paginas);i++) { pagina_t* pagina=list_get(segmento->paginas,i); if(pagina->en_disco) { nombre_archivo=generar_nombre_archivo_swap(pid, segmento->id, pagina->id); char* path; path=concat_string("en_disco/",nombre_archivo); path=concat_string(path,".txt"); free(nombre_archivo); remove(path); free(path); } } }
int main(int argc, char* argv[]) { MCAST_OPTIONS_T options; process_command_line(argc, argv, &options); // // Create output directory // if (create_output_directory( options.output_dirname, options.allow_clobber, FALSE /* Don't print warning messages */ ) != 0) { // Failed to create output directory. die("Couldn't create output directory %s.\n", options.output_dirname); } // // If needed, convert motif file to MEME format using transfac2meme. // char *motif_basename = basename(options.motif_filename); // Using GNU basename if (options.motif_format == TRANSFAC_FORMAT) { // Build the name for the new MEME format file in the output directory. char *meme_filename = concat_string(motif_basename, ".meme"); char *meme_path = make_path_to_file(options.output_dirname, meme_filename); myfree(meme_filename); run_transfactomeme( options.motif_filename, meme_path, options.bg_filename ); // Replace motif file name with new name. options.motif_filename = meme_path; } // // Build the HMM using mhmm. // char *hmm_basename = concat_string(motif_basename, ".mhmm"); char *hmm_path = make_path_to_file(options.output_dirname, hmm_basename); myfree(hmm_basename); run_mhmm(options.motif_filename, hmm_path); // // Read and score the sequences using mhmmscan. // char *score_path = make_path_to_file( options.output_dirname, options.text_only == TRUE ? "mcast.txt" : "mcast.html" ); run_mhmmscan(&options, hmm_path, options.seq_filename, score_path); // // Clean up // if (options.motif_format == TRANSFAC_FORMAT) { // If transfac format was used we have to // clean up the string naming the MEME format // motif file. myfree(options.motif_filename); } myfree(hmm_path); myfree(score_path); return 0; }
static int set_load(struct set *s, const char *filename) { fs_file fin; char *scores, *level_name; /* Skip "Misc" set when not in dev mode. */ if (strcmp(filename, SET_MISC) == 0 && !config_cheat()) return 0; fin = fs_open(filename, "r"); if (!fin) { log_printf("Failure to load set file %s\n", filename); //senquack - added error reporting: log_printf(fs_error()); return 0; } memset(s, 0, sizeof (struct set)); /* Set some sane values in case the scores are missing. */ score_init_hs(&s->time_score, 359999, 0); score_init_hs(&s->coin_score, 359999, 0); SAFECPY(s->file, filename); if (read_line(&s->name, fin) && read_line(&s->desc, fin) && read_line(&s->id, fin) && read_line(&s->shot, fin) && read_line(&scores, fin)) { sscanf(scores, "%d %d %d %d %d %d", &s->time_score.timer[RANK_HARD], &s->time_score.timer[RANK_MEDM], &s->time_score.timer[RANK_EASY], &s->coin_score.coins[RANK_HARD], &s->coin_score.coins[RANK_MEDM], &s->coin_score.coins[RANK_EASY]); free(scores); s->user_scores = concat_string("Scores/", s->id, ".txt", NULL); s->cheat_scores = concat_string("Scores/", s->id, "-cheat.txt", NULL); s->count = 0; while (s->count < MAXLVL && read_line(&level_name, fin)) { s->level_name_v[s->count] = level_name; s->count++; } fs_close(fin); return 1; } free(s->name); free(s->desc); free(s->id); free(s->shot); fs_close(fin); return 0; }