void trig_file_interests_ensure(void) { FILE *f; char linebuf[1024], *space; struct pkginfo *pkg; struct pkgbin *pkgbin; if (filetriggers_edited >= 0) return; f = fopen(triggersfilefile, "r"); if (!f) { if (errno == ENOENT) goto ok; ohshite(_("unable to read file triggers file '%.250s'"), triggersfilefile); } push_cleanup(cu_closestream, ~0, 1, f); while (fgets_checked(linebuf, sizeof(linebuf), f, triggersfilefile) >= 0) { struct dpkg_error err; char *slash; enum trig_options trig_opts = TRIG_AWAIT; space = strchr(linebuf, ' '); if (!space || linebuf[0] != '/') ohshit(_("syntax error in file triggers file '%.250s'"), triggersfilefile); *space++ = '\0'; slash = strchr(space, '/'); if (slash && strcmp("/noawait", slash) == 0) { trig_opts = TRIG_NOAWAIT; *slash = '\0'; } if (slash && strcmp("/await", slash) == 0) { trig_opts = TRIG_AWAIT; *slash = '\0'; } pkg = pkg_spec_parse_pkg(space, &err); if (pkg == NULL) ohshit(_("file triggers record mentions illegal " "package name '%.250s' (for interest in file " "'%.250s'): %.250s"), space, linebuf, err.str); pkgbin = &pkg->installed; trk_file_interest_change(linebuf, pkg, pkgbin, +2, trig_opts); } pop_cleanup(ehflag_normaltidy); ok: filetriggers_edited = 0; }
static int compute_n_err (const char* input_name) { FILE* p_file = fopen_sp_input_file('p',input_name,"txt",0); // closed char line[STRLEN_MAX]; fgets_checked(line,sizeof(line),p_file); int n_err = 0; read_skip_i(line,&n_err); fclose(p_file); return n_err; }
void trig_parse_ci(const char *file, trig_parse_cicb *interest, trig_parse_cicb *activate, struct pkginfo *pkg, struct pkgbin *pkgbin) { FILE *f; char linebuf[MAXTRIGDIRECTIVE], *cmd, *spc, *eol; int l; f = fopen(file, "r"); if (!f) { if (errno == ENOENT) return; /* No file is just like an empty one. */ ohshite(_("unable to open triggers ci file '%.250s'"), file); } push_cleanup(cu_closestream, ~0, 1, f); while ((l = fgets_checked(linebuf, sizeof(linebuf), f, file)) >= 0) { for (cmd = linebuf; c_iswhite(*cmd); cmd++) ; if (*cmd == '#') continue; for (eol = linebuf + l; eol > cmd && c_iswhite(eol[-1]); eol--) ; if (eol == cmd) continue; *eol = '\0'; for (spc = cmd; *spc && !c_iswhite(*spc); spc++) ; if (!*spc) ohshit(_("triggers ci file contains unknown directive syntax")); *spc++ = '\0'; while (c_iswhite(*spc)) spc++; if (strcmp(cmd, "interest") == 0 || strcmp(cmd, "interest-await") == 0) { parse_ci_call(file, cmd, interest, spc, pkg, pkgbin, TRIG_AWAIT); } else if (strcmp(cmd, "interest-noawait") == 0) { parse_ci_call(file, cmd, interest, spc, pkg, pkgbin, TRIG_NOAWAIT); } else if (strcmp(cmd, "activate") == 0 || strcmp(cmd, "activate-await") == 0) { parse_ci_call(file, cmd, activate, spc, pkg, pkgbin, TRIG_AWAIT); } else if (strcmp(cmd, "activate-noawait") == 0) { parse_ci_call(file, cmd, activate, spc, pkg, pkgbin, TRIG_NOAWAIT); } else { ohshit(_("triggers ci file contains unknown directive '%.250s'"), cmd); } } pop_cleanup(ehflag_normaltidy); /* fclose() */ }
void trig_file_interests_ensure(void) { FILE *f; char linebuf[1024], *space; struct pkginfo *pkg; const char *emsg; if (filetriggers_edited >= 0) return; f = fopen(triggersfilefile, "r"); if (!f) { if (errno == ENOENT) goto ok; ohshite(_("unable to read file triggers file `%.250s'"), triggersfilefile); } push_cleanup(cu_closestream, ~0, NULL, 0, 1, f); while (fgets_checked(linebuf, sizeof(linebuf), f, triggersfilefile) >= 0) { char *slash; enum trig_options trig_opts = trig_await; space = strchr(linebuf, ' '); if (!space || linebuf[0] != '/') ohshit(_("syntax error in file triggers file `%.250s'"), triggersfilefile); *space++ = '\0'; slash = strchr(space, '/'); if (slash && strcmp("/noawait", slash) == 0) { trig_opts = trig_noawait; *slash = '\0'; } emsg = pkg_name_is_illegal(space, NULL); if (emsg) ohshit(_("file triggers record mentions illegal " "package name `%.250s' (for interest in file " "`%.250s'): %.250s"), space, linebuf, emsg); pkg = pkg_db_find(space); trk_file_interest_change(linebuf, pkg, +2, trig_opts); } pop_cleanup(ehflag_normaltidy); ok: filetriggers_edited = 0; }
static diversion * read_diversions() { diversion *diversions = NULL; char linebuf[MAXDIVERTFILENAME]; static struct varbuf vb; varbufreset(&vb); varbufaddstr(&vb, admindir); varbufaddstr(&vb, "/" DIVERSIONSFILE); varbufaddc(&vb, 0); FILE *file = fopen(vb.buf, "r"); if (!file) ohshite(_("failed to open diversions file")); for (;;) { diversion *next = nfmalloc(sizeof(diversion)); if (fgets_checked(linebuf, sizeof(linebuf), file, vb.buf) < 0) break; chomp(linebuf); next->contest = strdup(linebuf); fgets_must(linebuf, sizeof(linebuf), file, vb.buf); chomp(linebuf); next->altname = strdup(linebuf); fgets_must(linebuf, sizeof(linebuf), file, vb.buf); chomp(linebuf); next->package = strdup(linebuf); next->next = diversions; diversions = next; } fclose(file); return invert_diversions_list(diversions); }
static struct Conv_Order_Data* constructor_Conv_Order_Data (const struct Integration_Test_Info*const int_test_info, const int error_type, const struct Simulation*const sim) { const int*const p_range = get_conv_order_range(int_test_info,'p'), *const ml_range = get_conv_order_range(int_test_info,'m'); const char*const input_name = compute_error_input_name_root(int_test_info,error_type,false,sim); const int n_err = compute_n_err(input_name); ptrdiff_t extents[] = { ml_range[1]+1, p_range[1]+1, n_err, }; const ptrdiff_t base_e = extents[0]*extents[1]; char**const var_names = malloc((unsigned)n_err * sizeof *var_names); // moved for (int i = 0; i < extents[2]; ++i) var_names[i] = malloc(STRLEN_MIN * sizeof *var_names); // moved struct Multiarray_i* cases_run = constructor_zero_Multiarray_i('C',2,&extents[0]); // moved struct Multiarray_d* h = constructor_zero_Multiarray_d('C',2,&extents[0]); // moved struct Multiarray_i* ex_ord = constructor_zero_Multiarray_i('C',3,extents); // moved struct Multiarray_d* l2_err = constructor_zero_Multiarray_d('C',3,extents); // moved struct Multiarray_d* conv_orders = constructor_zero_Multiarray_d('C',3,extents); // moved bool read_var_names = true; for (int p = p_range[0]; p <= p_range[1]; ++p) { for (int ml = ml_range[0]; ml <= ml_range[1]; ++ml) { const char*const input_name_curr = set_file_name_curr(ADAPT_0,p,ml,true,input_name); char line[STRLEN_MAX]; FILE* p_file = fopen_sp_input_file_unchecked('p',input_name_curr,"txt",0); // closed if (!p_file) continue; skip_lines(p_file,1); fgets_checked(line,sizeof(line),p_file); int data_i[n_err]; read_skip_i_1(line,1,data_i,n_err); if (read_var_names) { read_var_names = false; skip_lines(p_file,1); fgets_checked(line,sizeof(line),p_file); read_skip_c_2(line,1,var_names,n_err); } else { skip_lines(p_file,2); } fgets_checked(line,sizeof(line),p_file); double data_d[n_err+1]; read_skip_d_1(line,0,data_d,n_err+1); fclose(p_file); const ptrdiff_t ind_h = compute_index_sub_container(h->order,0,h->extents,(ptrdiff_t[]){ml,p}); h->data[ind_h] = data_d[0]; cases_run->data[ind_h] = 1; for (int n = 0; n < n_err; ++n) { const ptrdiff_t ind_e = n*base_e + p*extents[0] + ml; l2_err->data[ind_e] = data_d[n+1]; ex_ord->data[ind_e] = data_i[n]; } }} for (int p = p_range[0]; p <= p_range[1]; ++p) { for (int ml = ml_range[0]+1; ml <= ml_range[1]; ++ml) { for (int n = 0; n < n_err; ++n) { const ptrdiff_t ind_c_h = p*extents[0] + ml-1, ind_f_h = p*extents[0] + ml, ind_c_e = n*base_e + ind_c_h, ind_f_e = n*base_e + ind_f_h; double* data_l2 = l2_err->data, * data_h = h->data; if (data_h[ind_f_h] == 0.0 || data_h[ind_c_h] == 0.0) continue; conv_orders->data[ind_f_e] = log10(data_l2[ind_f_e]/data_l2[ind_c_e])/log10(data_h[ind_f_h]/data_h[ind_c_h]); }}} struct Conv_Order_Data*const cod = malloc(sizeof *cod); // free cod->var_names = (const char*const*) var_names; // free (2 levels) cod->cases_run = (struct const_Multiarray_i*) cases_run; // destructed cod->h = (struct const_Multiarray_d*) h; // destructed cod->ex_ord = (struct const_Multiarray_i*) ex_ord; // destructed cod->l2_err = (struct const_Multiarray_d*) l2_err; // destructed cod->conv_orders = (struct const_Multiarray_d*) conv_orders; // destructed return cod; }
void ensure_diversions(void) { static struct varbuf vb; struct stat stab1, stab2; char linebuf[MAXDIVERTFILENAME]; FILE *file; struct diversion *ov, *oicontest, *oialtname; varbufreset(&vb); varbufaddstr(&vb, admindir); varbufaddstr(&vb, "/" DIVERSIONSFILE); varbufaddc(&vb, 0); onerr_abort++; file = fopen(vb.buf,"r"); if (!file) { if (errno != ENOENT) ohshite(_("failed to open diversions file")); if (!diversionsfile) { onerr_abort--; return; } } else if (diversionsfile) { if (fstat(fileno(diversionsfile), &stab1)) ohshite(_("failed to fstat previous diversions file")); if (fstat(fileno(file), &stab2)) ohshite(_("failed to fstat diversions file")); if (stab1.st_dev == stab2.st_dev && stab1.st_ino == stab2.st_ino) { fclose(file); onerr_abort--; return; } } if (diversionsfile) fclose(diversionsfile); diversionsfile = file; setcloexec(fileno(diversionsfile), vb.buf); for (ov = diversions; ov; ov = ov->next) { ov->useinstead->divert->camefrom->divert = NULL; ov->useinstead->divert = NULL; } diversions = NULL; if (!file) { onerr_abort--; return; } while (fgets_checked(linebuf, sizeof(linebuf), file, vb.buf) >= 0) { oicontest = nfmalloc(sizeof(struct diversion)); oialtname = nfmalloc(sizeof(struct diversion)); oialtname->camefrom = findnamenode(linebuf, 0); oialtname->useinstead = NULL; fgets_must(linebuf, sizeof(linebuf), file, vb.buf); oicontest->useinstead = findnamenode(linebuf, 0); oicontest->camefrom = NULL; fgets_must(linebuf, sizeof(linebuf), file, vb.buf); oicontest->pkg = oialtname->pkg = strcmp(linebuf, ":") ? pkg_db_find(linebuf) : NULL; if (oialtname->camefrom->divert || oicontest->useinstead->divert) ohshit(_("conflicting diversions involving `%.250s' or `%.250s'"), oialtname->camefrom->name, oicontest->useinstead->name); oialtname->camefrom->divert = oicontest; oicontest->useinstead->divert = oialtname; oicontest->next = diversions; diversions = oicontest; } onerr_abort--; }
static int trk_explicit_fgets(char *buf, size_t sz) { return fgets_checked(buf, sz, trk_explicit_f, trk_explicit_fn.buf); }
/* XXX: This whole 2-pass approach is a disaster */ static gsl_matrix * csv_read_file(FILE *f) { char *lineptr; size_t linelen = 16384; size_t nfields; size_t nlines; long pos; double *fields; gsl_matrix *matrix; size_t i; lineptr = malloc(linelen); pos = ftell(f); nlines = 0; while (!ferror(f) && !feof(f)) { ssize_t rval = fgets_checked(lineptr, linelen, f); if (rval == -1) { fprintf(stderr, "Line too long on line: %d\n", (int) nlines); } for (i = 0; i < strlen(lineptr); i++) { if (!isspace(lineptr[i])) { nlines++; break; } } } fseek(f, pos, SEEK_SET); /* Assume that the first line is a header */ fgets(lineptr, linelen, f); nfields = csv_count_fields(lineptr); fields = calloc(nfields, sizeof(double)); matrix = gsl_matrix_alloc(nlines - 1, nfields); for (i = 0; i < nlines - 1; i++) { gsl_vector_view row; gsl_vector_view in; const char *parseval; fgets(lineptr, linelen, f); parseval = csv_read_double_fields(fields, nfields, lineptr); if (parseval != NULL) { fprintf(stderr, "Unexpected character on line %d: at position: %d", (int) i + 1, (int) (parseval - lineptr)); return NULL; } row = gsl_matrix_row(matrix, i); in = gsl_vector_view_array(fields, nfields); gsl_vector_memcpy(&row.vector, &in.vector); } free(lineptr); free(fields); return matrix; }