static void find_binary(u8* fname) { u8* env_path = 0; struct stat st; if (strchr(fname, '/') || !(env_path = getenv("PATH"))) { target_path = ck_strdup(fname); if (stat(target_path, &st) || !S_ISREG(st.st_mode) || !(st.st_mode & 0111) || st.st_size < 4) FATAL("Program '%s' not found or not executable", fname); } else { while (env_path) { u8 *cur_elem, *delim = strchr(env_path, ':'); if (delim) { cur_elem = ck_alloc(delim - env_path + 1); memcpy(cur_elem, env_path, delim - env_path); delim++; } else cur_elem = ck_strdup(env_path); env_path = delim; if (cur_elem[0]) target_path = alloc_printf("%s/%s", cur_elem, fname); else target_path = ck_strdup(fname); ck_free(cur_elem); if (!stat(target_path, &st) && S_ISREG(st.st_mode) && (st.st_mode & 0111) && st.st_size >= 4) break; ck_free(target_path); target_path = 0; } if (!target_path) FATAL("Program '%s' not found or not executable", fname); } }
static void find_obj(u8* argv0) { u8 *afl_path = getenv("AFL_PATH"); u8 *slash, *tmp; if (afl_path) { tmp = alloc_printf("%s/afl-llvm-rt.o", afl_path); if (!access(tmp, R_OK)) { obj_path = afl_path; ck_free(tmp); return; } ck_free(tmp); } slash = strrchr(argv0, '/'); if (slash) { u8 *dir; *slash = 0; dir = ck_strdup(argv0); *slash = '/'; tmp = alloc_printf("%s/afl-llvm-rt.o", dir); if (!access(tmp, R_OK)) { obj_path = dir; ck_free(tmp); return; } ck_free(tmp); ck_free(dir); } if (!access(AFL_PATH "/afl-llvm-rt.o", R_OK)) { obj_path = AFL_PATH; return; } FATAL("Unable to find 'afl-llvm-rt.o' or 'afl-llvm-pass.so'. Please set AFL_PATH"); }
static struct output * get_openfile (struct output **file_ptrs, const char *mode, int fail) { struct buffer *b; char *file_name; struct output *p; b = read_filename(); file_name = get_buffer(b); for (p=*file_ptrs; p; p=p->link) if (strcmp(p->name, file_name) == 0) break; if (posixicity == POSIXLY_EXTENDED) { /* Check whether it is a special file (stdin, stdout or stderr) */ struct special_files *special = special_files; /* std* sometimes are not constants, so they cannot be used in the initializer for special_files */ my_stdin = stdin; my_stdout = stdout; my_stderr = stderr; for (special = special_files; special->outf.name; special++) if (strcmp(special->outf.name, file_name) == 0) { special->outf.fp = *special->pfp; free_buffer (b); return &special->outf; } } if (!p) { p = OB_MALLOC(&obs, 1, struct output); p->name = ck_strdup(file_name); p->fp = ck_fopen(p->name, mode, fail); p->missing_newline = false; p->link = *file_ptrs; *file_ptrs = p; }
struct open_file *p; for (p=open_files; p; p=p->link) { if (fp == p->fp) { free(p->name); break; } } if (!p) { p = MALLOC(1, struct open_file); p->link = open_files; open_files = p; } p->name = ck_strdup(name); p->fp = fp; p->temp = false; } /* Panic on failing fopen */ FILE * ck_fopen(const char *name, const char *mode, int fail) { FILE *fp; fp = fopen (name, mode); if (!fp) { if (fail) panic(_("couldn't open file %s: %s"), name, strerror(errno));
int read_config_file(const char *filename, int *_argc, char ***_argv) { FILE *fh; char line[MAX_LINE_LEN + 1]; char *val, *ptr; u8 *tmp; u32 idx, i; APPEND_STRING(fargv, fargc, ck_strdup((u8*)*_argv[0])); fh = fopen(filename, "r"); if (!fh) PFATAL("Unable to read config from: %s", filename); while (!feof(fh) && fargc < MAX_ARGS && fgets(line, MAX_LINE_LEN, fh)) { /* Skip comments and empty lines */ if (line[0] == '\n' || line[0] == '\r' || line[0] == '#') continue; /* NULL terminate the key */ idx = strcspn(line, " \t="); if (idx == strlen(line)) FATAL("Config key error at line: %s", line); line[idx] = '\0'; /* Find the beginning of the value. */ val = line + (idx + 1); idx = strspn(val, " \t="); if (idx == strlen(val)) FATAL("Config value error at line: %s", line); val = val + idx; /* Trim the unwanted characters from the value */ ptr = val + (strlen(val) - 1); while(*ptr && *ptr < 0x21) { *ptr = 0; ptr--; } /* Done! Now we have a key/value pair. If the flag is set to 'false' we will disregard this line. If the value is 'true', we will set the flag without a value. In any other case, we will set the flag and value */ if (val[0] == '\0') FATAL("Empty value in config line: %s", line); if (strcasecmp("false", val) == 0) continue; tmp = ck_alloc(strlen(line) + 3); sprintf((char*)tmp, "--%s", line); APPEND_STRING(fargv, fargc, tmp); if (strncasecmp("true", val, 3) != 0) APPEND_STRING(fargv, fargc, ck_strdup((u8*)val)); } /* Copy arguments from command line into our array */ for (i=1; i<*_argc && fargc < MAX_ARGS; ++i) APPEND_STRING(fargv, fargc, ck_strdup((u8*)(*_argv)[i])); /* Replace original flags */ *_argc = fargc; *_argv = (char **)fargv; fclose(fh); return 0; }