int generate_image(const char *source_file, char *arr, int rows, int cols) { FILE *fp; int temp; if(!(fp=fopen(source_file,"r"))){ fprintf(stderr, "Can't open input file \"%s\"\n", source_file); perror("Reason"); return ERRGEN; } for(int i=0, limit=rows*cols; i<limit; i+=cols) for(int j=0; j<cols; ++j){ fscanf(fp, "%d", &temp); arr[i+j] = match_level(temp); } if(ferror(fp)){ fprintf(stderr, "Error in reading from file \"%s\"\n", source_file); perror("Reason"); return ERRGEN; } if(fclose(fp)){ fprintf(stderr, "Can't close input file \"%s\"\n", source_file); perror("Reason"); return ERRGEN; } return 0; }
/* * Return the set of dumps that match *all* of the given patterns (we consider * an empty pattern to match .*, though). If 'ok' is true, will only match * dumps with SUCCESS status. * * Returns a newly allocated list of results, where all strings are also newly * allocated. Apparently some part of Amanda leaks under this condition. */ find_result_t * dumps_match( find_result_t *output_find, char *hostname, char *diskname, char *datestamp, char *level, int ok) { find_result_t *cur_result; find_result_t *matches = NULL; for(cur_result=output_find; cur_result; cur_result=cur_result->next) { char level_str[NUM_STR_SIZE]; g_snprintf(level_str, sizeof(level_str), "%d", cur_result->level); if((!hostname || *hostname == '\0' || match_host(hostname, cur_result->hostname)) && (!diskname || *diskname == '\0' || match_disk(diskname, cur_result->diskname)) && (!datestamp || *datestamp== '\0' || match_datestamp(datestamp, cur_result->timestamp)) && (!level || *level== '\0' || match_level(level, level_str)) && (!ok || g_str_equal(cur_result->status, "OK")) && (!ok || g_str_equal(cur_result->dump_status, "OK"))){ find_result_t *curmatch = g_new0(find_result_t, 1); memcpy(curmatch, cur_result, sizeof(find_result_t)); curmatch->timestamp = cur_result->timestamp; curmatch->write_timestamp = cur_result->write_timestamp; curmatch->hostname = cur_result->hostname; curmatch->diskname = cur_result->diskname; curmatch->level = cur_result->level; curmatch->label = cur_result->label? cur_result->label : NULL; curmatch->filenum = cur_result->filenum; curmatch->sec = cur_result->sec; curmatch->kb = cur_result->kb; curmatch->bytes = cur_result->bytes; curmatch->orig_kb = cur_result->orig_kb; curmatch->status = cur_result->status; curmatch->dump_status = cur_result->dump_status; curmatch->message = cur_result->message; curmatch->partnum = cur_result->partnum; curmatch->totalparts = cur_result->totalparts; curmatch->next = matches; matches = curmatch; } } return(matches); }
static gboolean test_match_level(void) { gboolean ok = TRUE; struct { char *expr, *str; gboolean should_match; } tests[] = { /* exact matches, optionally ignoring "^" */ { "3$", "2", FALSE }, { "3$", "3", TRUE }, { "3$", "4", FALSE }, { "3$", "32", FALSE }, { "^3$", "2", FALSE }, { "^3$", "3", TRUE }, { "^3$", "4", FALSE }, { "^3$", "32", FALSE }, /* prefix matches */ { "3", "2", FALSE }, { "3", "3", TRUE }, { "3", "4", FALSE }, { "3", "32", TRUE }, /* ranges */ { "2-5", "1", FALSE }, { "2-5", "13", FALSE }, { "2-5", "23", FALSE }, { "2-5", "2", TRUE }, { "2-5", "4", TRUE }, { "2-5", "5", TRUE }, { "2-5", "53", FALSE }, { "2-5", "63", FALSE }, { "2-5", "6", FALSE }, { "9-15", "8", FALSE }, { "9-15", "19", FALSE }, { "9-15", "91", FALSE }, { "9-15", "9", TRUE }, { "9-15", "14", TRUE }, { "9-15", "15", TRUE }, { "9-15", "152", FALSE }, { "9-15", "16", FALSE }, { "19-21", "18", FALSE }, { "19-21", "19", TRUE }, { "19-21", "21", TRUE }, { "19-21", "22", FALSE }, /* single range is the same as an exact match */ { "99-99", "98", FALSE }, { "99-99", "99", TRUE }, { "99-99", "100", FALSE }, /* reversed range never matches */ { "21-19", "18", FALSE }, { "21-19", "19", FALSE }, { "21-19", "21", FALSE }, { "21-19", "22", FALSE }, { NULL, NULL, FALSE }, }, *t; for (t = tests; t->expr; t++) { gboolean matched = match_level(t->expr, t->str); if (!!matched != !!t->should_match) { ok = FALSE; if (t->should_match) { g_fprintf(stderr, "%s should have matched level expr %s\n", t->str, t->expr); } else { g_fprintf(stderr, "%s unexpectedly matched level expr %s\n", t->str, t->expr); } } } return ok; }