/* ** brief: set the champion in the correct position on the list depending his number ** @corewar: our structure for the virtual machine ** @champion: our structure for each champion ** return: {0 if succeed} {1 if number already taken} */ static unsigned int my_order_champion(t_corewar *corewar, t_champion *champion) { t_champion *champion_tmp; champion_tmp = corewar->champions; while (champion_tmp && champion_tmp->next) { if (champion_tmp->number == champion->number) return (my_fprintf(2, "number \"%i\" already used\n", champion->number)); champion_tmp = champion_tmp->next; } if (!corewar->champions || corewar->champions->number > champion->number) { champion->next = corewar->champions; corewar->champions = champion; } else if (corewar->champions->number < champion->number) { champion_tmp->next = champion; champion_tmp->next->next = NULL; } else return (my_fprintf(2, "prog number %i already used.\n", champion->number)); ++corewar->nbr_champions; return (0); }
int exec_line(t_param *param, char *line) { t_pipeline pipeline; pipeline.line = line; pipeline.nb_pipe = get_nb_pipe(pipeline.line); if (!verify(&pipeline)) return (-42); if ((param->tty) && (param->debug)) my_fprintf(2, "%[6]Exec line `%S' (%d pipes)%[]\n", pipeline.line, pipeline.nb_pipe); pipeline.input = extract_input(pipeline.line); pipeline.output = extract_output(pipeline.line); if (verify_pipe_redirless(pipeline.line) == -1) return (-42); if ((param->tty) && (param->debug)) { if (pipeline.input.type != NONE) my_fprintf(2, "%[6]Input file : %S%[]\n", pipeline.input.file); if (pipeline.output.type != NONE) my_fprintf(2, "%[6]Output file : %S%[]\n", pipeline.output.file); } exec_pipeline(param, &pipeline); return (pipeline.status); }
/* ** brief: get all the parameters for the virtual machine ** @argc: number of arguments pass to the program ** @argv: each arguments pass to the program ** @corewar: our structure for the virtual machine ** return: {0 if succeed} {1 if error during parsing} */ static unsigned int my_get_params(int argc, char **argv, t_corewar *corewar) { t_champion *champion; int i; i = 1; if (!my_strcmp(argv[i], "-dump")) { if (i + 1 >= argc || my_str_isnum(argv[i + 1]) || (corewar->dump = my_getnbr(argv[i + 1])) < 0) return (my_fprintf(2, "Dump must have a positive number.\n")); i += 2; } else corewar->dump = -1; while (i < argc) { if (!(champion = my_malloc(sizeof(*champion)))) return (my_fprintf(2, "At my_get_params : Malloc failed.\n")); if (my_champion(argc, argv, &i, champion)) return (1); if (my_order_champion(corewar, champion)) return (1); } return (0); }
static int set_arena(t_corewar *corewar, unsigned char **arena) { int i; int index; t_champion *tmp; tmp = corewar->champions; if ((*arena = my_malloc(MEM_SIZE * sizeof(char))) == NULL) return (my_fprintf(2, "At set_arena : Malloc failed.\n")); set_empty(corewar, arena); i = 0; while (tmp) { index = tmp->address; tmp->pc = index; while ((tmp->code) && (i < tmp->size) && (index < MEM_SIZE)) if ((*arena)[index] == 0) (*arena)[(index++) % MEM_SIZE] = tmp->code[i++]; else return (my_fprintf(2, "In \"set_arena\", address conflict\n")); i = 0; tmp = tmp->next; } return (0); }
void exec_cmd_low_level(t_param *param, t_cmd *cmd) { char *path; char *cmd_path; if (my_strstr(cmd->arg[0], "/") == 0) { while ((path = get_next_path(param->env))) { cmd_path = xsmalloc(my_strlen(path) + my_strlen(cmd->arg[0]) + 2); my_strcpy(cmd_path, path); my_strcat(cmd_path, "/"); my_strcat(cmd_path, cmd->arg[0]); xsfree(path); if (access(cmd_path, F_OK) != -1) execve(cmd_path, (cmd->arg), environ); xsfree(cmd_path); } my_fprintf(2, "%s: command not found\n", cmd->arg[0]); } else { execve(cmd->arg[0], cmd->arg, environ); my_fprintf(2, "%s: can't exec\n", cmd->arg[0]); } free_all(param); exit(1); }
/* ** brief: launch the differents functions for the virtual machine ** @argc: number of arguments pass to the program ** @argv: each arguments pass to the program ** return: {0 if succeed} {1 if anything went wrong} */ int main(int argc, char **argv) { t_corewar corewar; if (argc == 1) { my_fprintf(2, "Usage: corewar [-dump nbr_cycle] [[-n "); my_fprintf(2, "prog_number] [-a load_address ] prog_name] ...\n"); return (1); } corewar.champions = NULL; corewar.nbr_champions = 0; if (my_get_params(argc, argv, &corewar)) { my_free(); return (1); } if (corewar.nbr_champions < 1 || corewar.nbr_champions > MAX_ARGS_NUMBER) { my_free(); return (my_fprintf(2, "You need 1 to %i champions.\n", MAX_ARGS_NUMBER)); } my_champion_adress(&corewar); my_vm(&corewar); my_free(); return (0); }
/* * get_tcp6_socket -- get us a socket connected to the server. * * Parameters: "machine" is the machine the server is running on. * "port" is the portnumber to connect to. * * Returns: Socket connected to the news server if * all is ok, else -1 on error. * * Side effects: Connects to server via IPv4 or IPv6. * * Errors: Printed via my_fprintf. */ static int get_tcp6_socket( char *machine, unsigned short port) { char mymachine[MAXHOSTNAMELEN + 1]; char myport[12]; int s = -1, err = -1; struct addrinfo hints, *res, *res0; snprintf(mymachine, sizeof(mymachine), "%s", machine); snprintf(myport, sizeof(myport), "%d", port); /* just in case */ # ifdef AF_UNSPEC # define ADDRFAM AF_UNSPEC # else # ifdef PF_UNSPEC # define ADDRFAM PF_UNSPEC # else # define ADDRFAM AF_INET # endif /* PF_UNSPEC */ # endif /* AF_UNSPEC */ memset(&hints, 0, sizeof(hints)); /* hints.ai_flags = AI_CANONNAME; */ hints.ai_family = ADDRFAM; hints.ai_socktype = SOCK_STREAM; res = (struct addrinfo *) 0; res0 = (struct addrinfo *) 0; err = getaddrinfo(mymachine, myport, &hints, &res0); if (err != 0) { my_fprintf(stderr, "\ngetaddrinfo: %s\n", gai_strerror(err)); return -1; } err = -1; for (res = res0; res; res = res->ai_next) { if ((s = socket(res->ai_family, res->ai_socktype, res->ai_protocol)) < 0) continue; if (connect(s, res->ai_addr, res->ai_addrlen) < 0) close(s); else { err = 0; break; } } if (res0 != NULL) freeaddrinfo(res0); if (err < 0) { my_fprintf(stderr, _(txt_error_socket_or_connect_problem)); return -1; } return s; }
int globing_substitution(t_cmd *cmd) { int iarg; t_vector *new_files; int state; char **string; state = (cmd->arg[iarg = 1]) ? 0 : 1; while (cmd->arg[iarg]) if (is_globbing(string = clear_inhib_str(cmd->arg[iarg]))) { if (brace_parse(string, cmd, iarg, new_files)) continue; new_files = find_all_matching_files(string); qsort(new_files->data, vector_size(new_files), sizeof(void *), my_globcmp); state = vector_size(new_files); add_files_to_arg_tab(cmd, iarg, new_files); vector_free(new_files); iarg += state; } else if ((state = 1)) cmd->arg[(iarg += state) - state] = string[0]; if (state == 0) my_fprintf(2, "%s: No match.\n", cmd->arg[0]); return ((state == 0) ? 1 : 0); }
void file_dfs(char *arg, t_vector *stack, t_vector *dirs, char *name) { struct stat dd; struct dirent *dp; char *path_name; char *dirname; DIR *dir; dirname = (vector_size(dirs) ? dir_from_vector(dirs) : my_strdup("./")); if ((dir = opendir(dirname))) { while ((dp = readdir(dir)) != NULL) { path_name = complete_path(dirname, dp->d_name); if (lstat(path_name, &dd) == -1) my_fprintf(2, "globbing: can't stat %s\n", path_name); if ((name[0] != '.') && (dp->d_name[0] == '.')); else if ((S_ISDIR(dd.st_mode) || (!my_strstr(arg, "/"))) && (match(dp->d_name, name))) matching_file(arg, stack, dirs, dp); xsfree(path_name); } closedir(dir); } xsfree(dirname); }
int main(int argc, char **argv) { char **map; int fd; t_vect *c; int b; int y; if (argc < 2) { my_printf("[*] OPT : %s <filename>\n", argv[0]); return (0); } if (get_infos(&fd, argv[1]) == 1) return (1); map = my_gettab(fd, &y); if (!(c = check_bsq(map, &b, y))) { my_fprintf(2, "Erreur : impossible de trouver un carré ..\n"); return (1); } fill_carre(&map, *c, b, y); my_show_tab(map); free_tab(&map); return (0); }
/* ** Write the header of the file (minus the prog size) ** init the linked lists storing label definition and reference ** Read every line of the file fd_champ, ** split them into a word tab and send them ** to process_line ** Then launch late_operation ** if any error is encountered during these operations, ** return -1, else return 0 */ int my_parsefile(int fd_champ, int fd_binary) { char *line; char **lexed_line; int j; int fileoffset; t_lateinfo info; my_init_info(&info); if ((fileoffset = write_header(fd_champ, fd_binary)) == -1) return (-1); j = 1; while ((line = get_next_line(fd_champ)) != NULL) { if (line[0] != COMMENT_CHAR && line[0] != '.' && line[0] && line[0]) { if ((lexed_line = my_epur(all_in_tab(line))) == NULL || process_line(lexed_line, fd_binary, &fileoffset, &info) == -1) { my_fprintf(2, "at line %i : \"%s\" ", j, line); return (-1); } } free(line); ++j; } return (late_operation(fd_binary, fileoffset, &info)); }
int check_values(char **map) { int y; int x; char **tab; y = 0; while (map[y]) { x = 0; tab = str_to_word_tab(map[y]); while (tab[x]) { if (my_atoi(tab[x]) < 0 || my_atoi(tab[x]) > 70) { my_fprintf("The value %d at (%d;%d) is wrong\n", my_atoi(tab[x]), x, y); free_double_tab(tab); return (0); } x++; } free_double_tab(tab); y++; } return (1); }
/* * get_dnet_socket -- get us a socket connected to the server. * * Parameters: "machine" is the machine the server is running on. * "service" is the name of the service to connect to. * * Returns: Socket connected to the news server if * all is ok, else -1 on error. * * Side effects: Connects to server. * * Errors: Printed via nerror. */ int get_dnet_socket( char *machine, char *service) { # ifdef NNTP_ABLE int s, area, node; struct sockaddr_dn sdn; struct nodeent *getnodebyname(), *np; memset((char *) &sdn, '\0', sizeof(sdn)); switch (s = sscanf(machine, "%d%*[.]%d", &area, &node)) { case 1: node = area; area = 0; /* FALLTHROUGH */ case 2: node += area * 1024; sdn.sdn_add.a_len = 2; sdn.sdn_family = AF_DECnet; sdn.sdn_add.a_addr[0] = node % 256; sdn.sdn_add.a_addr[1] = node / 256; break; default: if ((np = getnodebyname(machine)) == NULL) { my_fprintf(stderr, _(txt_gethostbyname), "", machine); return -1; } else { memcpy((char *) sdn.sdn_add.a_addr, np->n_addr, np->n_length); sdn.sdn_add.a_len = np->n_length; sdn.sdn_family = np->n_addrtype; } break; } sdn.sdn_objnum = 0; sdn.sdn_flags = 0; sdn.sdn_objnamel = strlen("NNTP"); memcpy(&sdn.sdn_objname[0], "NNTP", sdn.sdn_objnamel); if ((s = socket(AF_DECnet, SOCK_STREAM, 0)) < 0) { nerror("socket"); return -1; } /* And then connect */ if (connect(s, (struct sockaddr *) &sdn, sizeof(sdn)) < 0) { nerror("connect"); close(s); return -1; } return s; # else return -1; # endif /* NNTP_ABLE */ }
static void post_deflicker_save_sidecar_file(int type, char* photo_filename, float ev) { /* find and strip extension */ char* ext = photo_filename + strlen(photo_filename) - 1; while (ext > photo_filename && *ext != '/' && *ext != '.') ext--; if (*ext != '.') return; *ext = 0; /* find and strip base filename (e.g. IMG_1234) */ char* p = ext; while (p > photo_filename && *p != '/') p--; if (*p != '/') return; *p = 0; /* path components */ char* dir = photo_filename; /* A:/DCIM/100CANON */ char* basename = p+1; /* IMG_1234 */ char* extension = ext+1; /* CR2 */ //~ NotifyBox(2000, "'%s'\n'%s'\n'%s'", dir, basename, extension); char sidecar[100]; snprintf(sidecar, sizeof(sidecar), "%s/%s.%s", dir, basename, type ? "UFR" : "XMP"); FILE* f = FIO_CreateFile(sidecar); if (!f) return; if (type == 0) { /* not sure */ int evi = ev * 100000; my_fprintf(f, xmp_template, FMT_FIXEDPOINT5S(evi)); } else if (type == 1) { char raw[100]; char jpg[100]; snprintf(raw, sizeof(raw), "%s.%s", basename, extension); snprintf(jpg, sizeof(jpg), "%s.JPG", basename); ev = COERCE(ev, -6, 6); int evi = ev * 100000; my_fprintf(f, ufraw_template, raw, jpg, FMT_FIXEDPOINT5(evi)); } FIO_CloseFile(f); }
int get_infos(int *fd, char *s) { if ((*fd = open(s, O_RDONLY)) == -1) { my_fprintf(2, "[-] Failed to open file '%s'\n", s); return (1); } return (0); }
/** * ubiutils_print_text - print text and fold it. * @stream: file stream to print to * @text: text to print * @width: maximum allowed text width * * Print text and fold it so that each line would not have more then @width * characters. */ void ubiutils_print_text(FILE *stream, const char *text, int width) { int pos, bpos = 0; const char *p; char line[1024]; if (width > 1023) { my_fprintf(stream, "%s\n", text); return; } p = text; pos = 0; while (p[pos]) { while (!isspace(p[pos])) { line[pos] = p[pos]; if (!p[pos]) break; ++pos; if (pos == width) { line[pos] = '\0'; my_fprintf(stream, "%s\n", line); p += pos; pos = 0; } } while (pos < width) { line[pos] = p[pos]; if (!p[pos]) { bpos = pos; break; } if (isspace(p[pos])) bpos = pos; ++pos; } line[bpos] = '\0'; my_fprintf(stream, "%s\n", line); p += bpos; pos = 0; while (p[pos] && isspace(p[pos])) ++p; } }
int get_tty_fd() { int fd; if ((fd = open("/dev/tty", O_WRONLY)) == -1) { my_fprintf(2, "[-] Failed to Open /dev/tty ..\n"); exit(1); } return (fd); }
int catch_error(t_maze **maze, char *av1, t_player **player) { t_maze *m; m = *maze; my_printf("[*] Checking Errors .. \n"); if ((m->fd = open(av1, O_RDONLY)) == -1) { my_fprintf(2, "\t[-] Failed to open file '%s'..\n", av1); return (1); } if ((m->map = get_maze_file(av1, m)) == NULL) { my_fprintf(2, "\t[-] Failed to get Maze ... \n"); return (1); } if ((*player = get_player_infos(m)) == NULL) { my_fprintf(2, "\t[-] Player Not Found ..\n"); return (1); } return (0); }
/** * ubiutils_get_bytes - convert a string containing amount of bytes into an * integer * @str: string to convert * * This function parses @str which may have one of 'KiB', 'MiB', or 'GiB' * size specifiers. Returns positive amount of bytes in case of success and %-1 * in case of failure. */ long long ubiutils_get_bytes(const char *str) { char *endp; long long bytes = strtoull(str, &endp, 0); if (endp == str || bytes < 0) { my_fprintf(stderr, "incorrect amount of bytes: \"%s\"\n", str); return -1; } if (*endp != '\0') { int mult = get_multiplier(endp); if (mult == -1) { my_fprintf(stderr, "bad size specifier: \"%s\" - " "should be 'KiB', 'MiB' or 'GiB'\n", endp); return -1; } bytes *= mult; } return bytes; }
void show_alloc_mem() { void *addr; void *end; t_header *tmp; int offset; int size; addr = gl_lowlimit; end = sbrk(0); my_fprintf(2, "break : 0x%x\n", end); if (addr == NULL) return ; while ((int)addr <= (int)end) { tmp = addr; offset = sizeof(*tmp) + tmp->size + (int)tmp; size = tmp->size + sizeof(*tmp); if (tmp->magic == MALLOC_MAGIC) my_fprintf(2, "0x%x - 0x%x : %d octets\n", tmp, offset, size); addr = (void *)((int)addr + tmp->size + sizeof(*tmp)); } }
static MENU_SELECT_FUNC(config_preset_toggle) { menu_numeric_toggle(&config_new_preset_index, delta, 0, config_preset_num); if (!config_new_preset_index) { FIO_RemoveFile(config_preset_file); } else { FILE* f = FIO_CreateFile(config_preset_file); if (f) { if (config_new_preset_index == 1) my_fprintf(f, "Startup mode"); else if (config_new_preset_index == 2) my_fprintf(f, "Startup key"); else my_fprintf(f, "%s", config_preset_choices[config_new_preset_index]); FIO_CloseFile(f); } } }
int catch_error(char **term, char *bp, struct termios *t, struct termios *save) { char *tterm; tterm = *term; if ((tterm = my_getenv("TERM")) == NULL) { my_fprintf(2, "[-] Faild to get TERM env variable\n"); return (1); } if ((tgetent(bp, tterm)) != 1) { my_fprintf(2, "[-] Failed to tgetent(%s)..\n", term); return (1); } if (my_getattr(t, save) == 1) { my_fprintf(2, "[-] Failed to getattr..\n"); return (1); } return (0); }
/* ** compil a single instruction (lexed_line): ** check the params and write the einstruction ** if an error is encountered, return -1 ** else return how many byte has been writed */ int my_compil(char **lexed_line, op_t *optodo, int fd_binary) { int i; int ret; int writed; writed = 0; i = my_tablen(lexed_line); if (my_check_params(i, optodo, lexed_line) == -1) return (-1); if ((ret = write(fd_binary, &(optodo->code), 1)) == -1) { my_fprintf(2, "write() error. (opcode)\n"); return (-1); } writed += ret; if ((ret = my_write_args(lexed_line, optodo, fd_binary)) == -1) { my_fprintf(2, "write() error. (arg)\n"); return (-1); } writed += ret; return (writed); }
char *my_strcut(char *str, char *cut) { int len; char *dest; len = my_strlen(cut); if (len > my_strlen(str)) return (str); if (!(dest = malloc(sizeof(char) * (my_strlen(str) + 1)))) { my_fprintf(STDERR, "my_strcut : malloc failed\n"); return (NULL); } clearstr(dest, my_strlen(str)); my_strcut_cpy(str, dest, cut, len); return (dest); }
int builtin_setalias_safe(t_vars *var, t_list *list) { char *name; char *val; if (!alias_get(list, var->name)) { if (!(name = strdup(var->name)) || !(val = strdup(var->val))) return (ID_FAIL_STRDUP); alias_add(list, name, val, 1); } else { my_fprintf(STDERR_FILENO, MSG_SETENV_VAR_EXISTS, var->name); return (-1); } return (0); }
void console_puts(const char* str) // don't DebugMsg from here! { #define NEW_CHAR(c) CONSOLE_BUFFER(console_buffer_index++) = (c) #ifdef CONFIG_QEMU qprintf("%s", str); #endif #ifdef CONSOLE_DEBUG bmp_printf(FONT_MED, 0, 0, "%s ", str); if (console_log_file) my_fprintf( console_log_file, "%s", str ); #endif const char* c = str; while (*c) { if (*c == '\n') { if (mod(console_buffer_index, CONSOLE_W) == 0) NEW_CHAR(' '); while (mod(console_buffer_index, CONSOLE_W) != 0) NEW_CHAR(' '); } else if (*c == '\t') { NEW_CHAR(' '); while (mod(mod(console_buffer_index, CONSOLE_W), 4) != 0) NEW_CHAR(' '); } else if (*c == 8) { console_buffer_index = mod(console_buffer_index - 1, BUFSIZE); console_buffer[console_buffer_index] = ' '; } else NEW_CHAR(*c); c++; } console_buffer_index = mod(console_buffer_index, BUFSIZE); }
static int check() { if (IND_SIZE != 2) return (my_fprintf(2, "In \"op.h\", please set IND_SIZE = 2")); if (REG_SIZE != 4) return (my_fprintf(2, "In \"op.h\", please set REG_SIZE = 4")); if (DIR_SIZE != REG_SIZE) return (my_fprintf(2, "In \"op.h\", please set DIR_SIZE = REG_SIZE")); if (CYCLE_TO_DIE <= 0) return (my_fprintf(2, "In \"op.h\", CYCLE_TO_DIE must be positive")); if (CYCLE_DELTA <= 0) return (my_fprintf(2, "In \"op.h\", CYCLE_DELTA must be positive")); if (CYCLE_DELTA >= CYCLE_TO_DIE) return (my_fprintf(2, "In \"op.h\", CYCLE_DELTA > CYCLE_TO_DIE")); if (NBR_LIVE <= 0) return (my_fprintf(2, "In \"op.h\", NBR_LIVE must be positive")); return (0); }
static void find_response_curve(const char* fname) { char fname_real[255]; snprintf(fname_real, sizeof(fname_real), "ML/LOGS/%s", fname); FILE* f = FIO_CreateFile(fname_real); ensure_movie_mode(); clrscr(); set_lv_zoom(5); msleep(1000); for (int i = 0; i < 64*2; i+=8) bmp_draw_rect(COLOR_BLACK, i*5+40, 0, 8*5, 380); draw_line( 40, 190, 720-40, 190, COLOR_BLACK); extern int bv_auto; //int bva0 = bv_auto; bv_auto = 0; // make sure it won't interfere bv_enable(); // for enabling fine 1/8 EV increments int ma = (lens_info.raw_aperture_min + 7) & ~7; for (int i = 0; i < 64*2; i++) { int a = (i/2) & ~7; // change aperture in full-stop increments lens_set_rawaperture(ma + a); lens_set_rawshutter(96 + i - a); // shutter can be changed in finer increments msleep(400); int Y,U,V; get_spot_yuv(180, &Y, &U, &V); dot( i*5 + 40 - 16, 380 - Y*380/255 - 16, COLOR_BLUE, 3); // dot has an offset of 16px my_fprintf(f, "%d %d %d %d\n", i, Y, U, V); } FIO_CloseFile(f); beep(); //~ call("dispcheck"); lens_set_rawaperture(ma); lens_set_rawshutter(96); }
int check_quote(char *str, char *inhib) { char c; int last; int ini; c = 0; last = my_strlen(str); if ((ini = is_inhibited(str, inhib, last))) { if (ini == 1) c = '\''; else if (ini == 2) c = '"'; else if (ini == 3) c = '\\'; my_fprintf(2, "Unmatched %c.\n", c); return (1); } return (0); }
int builtin_setalias(t_cmd *cmd, t_data *data) { if (cmd->args[1]) { if (!strncmp(cmd->args[1], "--", 2)) { if (!strcmp(cmd->args[1], "--safe")) builtin_setvars_base(cmd, data->alias, &builtin_setalias_safe); if (!strcmp(cmd->args[1], "--help")) my_printf(MSG_SETALIAS_HELP); } else return (builtin_setvars_base(cmd, data->alias, &builtin_setalias_new)); } else { my_fprintf(STDERR_FILENO, MSG_SETALIAS_HELP); return (-1); } return (1); }