int init_champs(t_data *data) { int i; i = 0; while (i < 4) { if (!(data->champ[i] = malloc(sizeof(t_champion))) || !(data->champ[i]->pc = malloc(sizeof(t_pc))) || !(data->champ[i]->pc->next = malloc(sizeof(t_pc)))) return (my_put_error(MALLOC_ERROR, 1)); my_bzero(data->champ[i]->pc->next->reg, sizeof(int) * 16, 0); data->champ[i]->pc->next->champ = data->champ[i]; data->champ[i]->pc->next->reg[0] = -1; data->champ[i]->pc->next->cycle = 0; data->champ[i]->pc->next->carry = 0; data->champ[i]->pc->next->run = 0; data->champ[i]->pc->next->next = NULL; data->champ[i]->pc->next->prev = data->champ[i]->pc; data->champ[i]->pc->prev = NULL; data->champ[i]->valid = -1; data->champ[i]->alive = -1; data->champ[i]->order = -1; data->champ[i]->cur = data->champ[i]->pc->next; my_bzero(data->champ[i]->name, PROG_NAME_LENGTH + 1, 0), i += 1; } return (0); }
t_nb *inf_sub(t_nb *n1, t_nb *n2, t_base *s_base, char *ops) { t_nb *res; int sign; if (!(res = malloc(sizeof(*res)))) return (NULL); if (ops) res->sign = get_sign(n1, n2, ops[3]); else res->sign = 0; sign = order_operands(&n1, &n2); if (!(res->nb = malloc(sizeof(*res) * (n1->len + 3)))) return (NULL); my_bzero(res->nb, n1->len + 3 + 3); do_sub_or_add(n1, n2, res, s_base); res->sign = (!res->sign && sign) || (!sign && res->sign == 3) ? 1 : res->sign; res->sign = res->sign == 1 ? 1 : res->sign; res->sign = res->sign == 2 ? 0 : res->sign; res->sign = res->sign == 3 && sign ? 0 : res->sign; if (ops) res->len = clean_res(res, n1->len + 3, s_base->base, ops[3]); else res->len = clean_res(res, n1->len + 3, s_base->base, 0); return (res); }
void copy_envline(char *dest, const char *name, const char *value) { my_bzero(dest, my_strlen(name) + my_strlen(value) + 2); my_strcpy(dest, name); strcat(dest, "="); strcat(dest, value); }
int get_args_entries(int opt_idx, int opt, t_lst **lst, char **argv) { int idx; t_blk *blk; int len[6]; int nb_files; idx = 0; nb_files = 0; my_bzero(len, 6 * sizeof(len[0])); while (++idx < opt_idx) { if ((blk = get_entry(argv[idx], opt | O_ARG, len))) { if (insert_entry(lst, blk, opt, argv[idx]) > 0) ++nb_files; free(blk); } else if (blk == (void *)-1) { print_error(argv[idx], strerror(errno)); exit(2); } } return (nb_files > 0 ? nb_files - 1 : 0); }
char *my_strtrim(char *s) { size_t i; size_t j; int space; if (!s) return (NULL); i = 0; j = 0; space = 1; while (s[i] != '\0') { if (my_isspace(s[i]) == 0) { if (j > 0 && space == 1) s[j++] = ' '; space = 0; s[j] = s[i]; j++; } else space = 1; i++; } my_bzero((s + j), i - j); return (s); }
t_bunny_response inaddr_text(void *param) { t_key *key; t_key send; t_msg *msg; static mousetab array; static int unsetted = 1; key = param; msg = &key->params->data->message; if (unsetted == 1) set(&array, &unsetted, key->params->connect_buttons); set_inaddr_text(key, msg, &unsetted); if (key->state == GO_DOWN && key->key != BKS_RETURN && key->key != BKS_UNKNOWN && key->key != BKS_ESCAPE) array[key->key](key->params->data); free(key->params->connect_buttons->text); if (((key->params->connect_buttons->text = malloc(my_strlen(msg->msg) + 1))) == NULL) return (EXIT_ON_ERROR); my_bzero(key->params->connect_buttons->text, my_strlen(msg->msg) + 1); my_strcpy(key->params->connect_buttons->text, msg->msg); if (key->state == GO_DOWN && key->key == BKS_RETURN) inaddr_text_add(key, &send); if (key->key == BKS_ESCAPE) key->params->current = &connect_srv; return (GO_ON); }
int toto() { char toto[] = "Coucou Ca Va ?"; char test[20]; char dest[7]; printf("sa marche ? toto fait %d char\n", my_strlen("toto")); my_memcpy(dest, toto, 14); dest[14] = 0; printf("dest = [%s]\n", dest); printf("strchr = [%s]\n", my_strchr(toto, 'a')); printf("strcmp toto, dest = %d\n", my_strcmp(toto + 1, dest)); dest[3] = 'a'; printf("strncmp toto, dest = %d\n", my_strncmp(toto, dest, 3)); my_bzero(toto, 5); printf("bzero = [%s]\n", toto + 5); printf("strcasecmp = %d\n", strcasecmp("SALUT", "123456")); my_write(1, "test\n", 5); printf("rindex my toto with the char 'a' : [%s]\n", my_rindex("caca coco coucou", 'i')); test[14] = 0; printf("apres le memset = [%s]\n", my_memset(test, 'A', 14)); strcpy(dest, "Coucou"); printf("memmove result = [%s]\n", memmove(dest, dest + 1, 20)); printf("dest = [%s]\n", dest); }
void my_bzero(void *s, size_t n) { if (n) { ((unsigned char *)s)[n - 1] = 0; my_bzero(s, n - 1); } }
int load_object(char *file, t_object_container *obj) { my_bzero(obj, sizeof(t_object_container)); if (load_object_(&obj->original, file) || load_object_(&obj->polygons, file) || load_object_(&obj->transform, file)) return (1); obj->next = obj + 1; return (0); }
char *recup_entry(t_program *tetris) { my_bzero(tetris->start.key, tetris->start.maxl + 1); if (read(0, tetris->start.key, tetris->start.maxl) <= 0) return (NULL); flushinp(); if (!tetris->start.key[0]) return (NULL); return (tetris->start.key); }
void MlxLib::clear_game(void) { int y = _sqsize * _szline; while ((y / (_sqsize * _szline)) < (_h - 1)) { my_bzero(_data + y + (_sqsize * _bpp / 8), _szline - (2 * _sqsize * (_bpp / 8))); y += _szline; } }
static int send_specs(int fd, t_scene_spec *spec) { char tmp[sizeof(t_scene_spec)]; my_bzero(tmp, sizeof(t_scene_spec)); create_specs_packet(tmp, spec); if (write(fd, tmp, sizeof(t_scene_spec)) == -1 || read_ok(fd)) return (1); write(1, "Specs sent !\n", 13); return (0); }
int is_valid(char *base, char *ops) { int base_len; char dup[256]; my_bzero(dup, 256); if ((base_len = check_base(base, dup)) < 2) { if (!base_len) { my_putstr("Providing twice the same character in the base string is \ nonsense.\n"); return (0); } }
/* * Helper allocation function */ void *my_memalloc(size_t size) { void *ptr = NULL; ptr = 0; if(size == 0) { return NULL; } else { ptr = malloc(size); if(!ptr) { return NULL; } else { my_bzero(ptr, size); return ptr; } } }
char exe_cmd(t_prog *s_prog, char **av, char **environ, WINDOW **wins) { size_t pathlen; char pathname[NAME_MAX + PATH_MAX]; pid_t pid; int status; int fds[2]; if (pipe(fds)) return (-1); pathlen = my_strlen(s_prog->path); my_strncpy(pathname, s_prog->path, PATH_MAX); if (pathname[pathlen] != '/') pathname[(pathlen++)] = '/'; my_strncpy(pathname + pathlen, s_prog->name, NAME_MAX); pathname[NAME_MAX + PATH_MAX - 1] = 0; if ((pid = fork()) > -1) { if (!pid) { /* restore_termcap(tsave); */ close(fds[0]); if ((dup2(fds[1], 1)) < 0) exit(EXIT_FAILURE); execve(pathname, av, environ); exit(EXIT_FAILURE); } else { close(fds[1]); my_bzero(pathname, sizeof(pathname)); while (read(fds[0], pathname, NAME_MAX + PATH_MAX - 1) > 0) { pathname[NAME_MAX + PATH_MAX - 1] = 0; waddstr(wins[WIN_SH], pathname); wrefresh(wins[WIN_SH]); } waitpid(pid, &status, 0); free_strtab(av); return (WEXITSTATUS(status)); } } free_strtab(av); waddstr(wins[WIN_SH], "err: cannot fork process\n"); return (-2); }
int select_history(char *str, t_hist *hist, char arrow, int test) { static t_hist *tmp = NULL; if (test == 1 || tmp == NULL) tmp = hist; my_bzero(str, my_strlen(str)); if (tmp != NULL && arrow == DOWN && tmp->next != NULL) { my_strcpy(str, tmp->line); tmp = tmp->next; } else if (tmp != NULL && arrow == UP && tmp->prev != NULL) { my_strcpy(str, tmp->line); tmp = tmp->prev; } return (0); }
void clear_line(t_buff *p_buff) { t_buff *tmp; tmp = p_buff; if (p_buff) { p_buff = p_buff->next; tmp->next = NULL; my_bzero(tmp->buff, BUFF_SIZE); tmp->len = 0; } while (p_buff) { tmp = p_buff; p_buff = p_buff->next; free(tmp); } }
int save_ini(t_data *data) { if (data->itfc.save.need_save) { if (data->rt.img == NULL) my_puterr("Save: Could not find any scene to save"); else { my_strcat(data->itfc.save.file, ".ini"); save_new_ini(data); } data->itfc.save.need_save = false; my_bzero(data->itfc.save.file, FILE_LEN); data->itfc.save.curs = 0; data->itfc.button[SAVE] = false; } else text(data->itfc.save.file, &data->itfc, TXT_X, TXT_Y); return (0); }
int save_bmp(t_data *data) { if (data->itfc.save.need_save) { if (data->rt.img == NULL) my_puterr("Save: Could not find any image to save"); else { my_strcat(data->itfc.save.file, ".bmp"); bunny_save_pixelarray(data->rt.img, data->itfc.save.file); } data->itfc.save.need_save = false; my_bzero(data->itfc.save.file, FILE_LEN); data->itfc.save.curs = 0; data->itfc.button[SAVE] = false; } else text(data->itfc.save.file, &data->itfc, TXT_X, TXT_Y); return (0); }
t_epur init_epur(char *str) { t_epur e; int count; char *tmp2; e.com = xmalloc(sizeof(char) * 5); e.com[0] = '|'; e.com[1] = '&'; e.com[2] = ';'; e.com[3] = '>'; e.com[4] = '<'; e.k = 0; e.i = 0; tmp2 = first_epur(str); count = count_size(tmp2, &e); e.tmp = xmalloc(sizeof(char) * (my_strlen(tmp2) + count) + 1); my_bzero(e.tmp, my_strlen(tmp2) + count); xfree(tmp2); return (e); }
static int init_mysh(t_term *s_term, char **environ) { char *env_path; my_bzero((void *)s_term, sizeof(*s_term)); if (environ && *environ) { get_environ(s_term, environ); static_term(s_term); init_signals(); s_term->tsave = init_termcap(s_term->environ); s_term->pid = my_getpid(); s_term->progs = NULL; if ((env_path = my_getenv(environ, "PATH=")) && (s_term->progs = get_path_progs(env_path)) && !add_builtins(s_term->progs)) return (1); } return (0); }
void change_path(t_data *data, char *pwd_asked, char *start_pwd, bool go_home) { char *act_pwd; int j; if (start_pwd == NULL) return ; if (go_home) return (go_to_home(data)); if ((act_pwd = malloc(sizeof(char) * my_strlen(start_pwd) + 1)) == NULL) exit(1); my_bzero(act_pwd, my_strlen(start_pwd) + 1); my_strcpy(act_pwd, start_pwd); act_pwd = get_real_path(pwd_asked, act_pwd); j = get_pos_from_env(data, "PWD"); free(data->env[j]); data->env[j] = act_pwd; free(data->pwd); data->pwd = my_strdup(act_pwd); }
void opt(char **av, char *tabb) { int i; int y; int bo; i = 1; my_bzero(tabb, 3); while (av[i] != NULL && av[i][0] == '-') { y = 0; if (av[i][y] == '-') bo = test_opt(av, tabb, y, i); if (bo == 1) { my_putstr(av[i], 1); if (av[i + 1] != NULL && tabb[2] != 's') my_putchar(' ', 1); } i++; } }
t_nb *inf_div(t_nb *n1, t_nb *n2, t_base *s_base, char *ops) { t_nb *res; if (zero_check(n2, s_base->base[0], ops[3], ops[2])) { my_putstr(SYNTAXE_ERROR_MSG); return (NULL); } if (!(res = malloc(sizeof(*res)))) return (NULL); res->sign = get_sign(n1, n2, ops[3]); if (!(res->nb = malloc(sizeof(*res) * (n1->len + 2)))) return (NULL); my_bzero(res->nb, n1->len + 2); if (do_div(res, n1, n2, s_base) < 0) return (NULL); set_ret_sign(res); if (!(res->len = clean_res(res, n1->len + 2, s_base->base, ops[3]))) return (NULL); return (res); }
void printfield(int line, t_item *item, int win) { char buff[12]; char *name; char *temp; my_bzero(buff, 11); strftime(buff, 12, "%a %d %Y", localtime(&item->s_stat.st_mtime)); name = item->name; if (S_ISDIR(item->s_stat.st_mode)) temp = "/"; else temp = " "; if ((name = my_stradd(temp, name)) == NULL) return ; wmove(g_ctrl.menu[win].win, line, 1); my_nputnstr(item->right, win, RIGHT_WIDTH - 3); wmove(g_ctrl.menu[win].win, line, 2 + RIGHT_WIDTH); my_nputnstr(name, win, WWIDTH - (NB_COLS + 1) - (TIME_WIDTH + 1) - (RIGHT_WIDTH + 1)); wmove(g_ctrl.menu[win].win, line, WWIDTH - (TIME_WIDTH + 1)); my_nputnstr(buff, win, TIME_WIDTH - 1); free(name); }
int main(void){ /* stack buffers */ char sbuf1[SHORTBUF]; char sbuf2[LONGBUF]; /* heap buffers */ char *hbuf1 = NULL; char *hbuf2 = NULL; uint64_t cycles; int i; hbuf1 = (char *)malloc(SHORTBUF); hbuf2 = (char *)malloc(LONGBUF); if((!hbuf1) || (!hbuf2)){ fprintf(stderr, "malloc failed\n"); exit(EXIT_FAILURE); } //just for load libc addr bzero(sbuf1, SHORTBUF); /* ------ test short buffers ------ */ cycles = _rdtsc(); for(i = NSAMPLES; i > 0; i--){ bzero(sbuf1, SHORTBUF); } cycles = _rdtsc() - cycles; printf("[STACK] [%d] bzero: %" PRIu64 " cycles\n", SHORTBUF, cycles); cycles = _rdtsc(); for(i = NSAMPLES; i > 0; i--){ bzero(hbuf1, SHORTBUF); } cycles = _rdtsc() - cycles; printf("[HEAP] [%d] bzero: %" PRIu64 " cycles\n", SHORTBUF, cycles); cycles = _rdtsc(); for(i = NSAMPLES; i > 0; i--){ my_bzero(sbuf1, SHORTBUF); } cycles = _rdtsc() - cycles; printf("[STACK] [%d] my_bzero: %" PRIu64 " cycles\n", SHORTBUF, cycles); cycles = _rdtsc(); for(i = NSAMPLES; i > 0; i--){ my_bzero(hbuf1, SHORTBUF); } cycles = _rdtsc() - cycles; printf("[HEAP] [%d] my_bzero: %" PRIu64 " cycles\n", SHORTBUF, cycles); puts(""); /* ------ test long buffers ------ */ cycles = _rdtsc(); for(i = NSAMPLES; i > 0; i--){ bzero(sbuf2, LONGBUF); } cycles = _rdtsc() - cycles; printf("[STACK] [%d] bzero: %" PRIu64 " cycles\n", LONGBUF, cycles); cycles = _rdtsc(); for(i = NSAMPLES; i > 0; i--){ bzero(hbuf2, LONGBUF); } cycles = _rdtsc() - cycles; printf("[HEAP] [%d] bzero: %" PRIu64 " cycles\n", LONGBUF, cycles); cycles = _rdtsc(); for(i = NSAMPLES; i > 0; i--){ my_bzero(sbuf2, LONGBUF); } cycles = _rdtsc() - cycles; printf("[STACK] [%d] my_bzero: %" PRIu64 " cycles\n", LONGBUF, cycles); cycles = _rdtsc(); for(i = NSAMPLES; i > 0; i--){ my_bzero(hbuf2, LONGBUF); } cycles = _rdtsc() - cycles; printf("[HEAP] [%d] my_bzero: %" PRIu64 " cycles\n", LONGBUF, cycles); free(hbuf1); free(hbuf2); hbuf1 = NULL; hbuf2 = NULL; return 0; }
int main(int argc, char *argv[]) { int sockfd, portno, n, bytesRead, running; struct sockaddr_in serv_addr; struct hostent *server; fd_set master, readfd; char *buffer, *nickname; buffer = (char *)malloc(sizeof(char) * 256); running = 1; if (argc < 3) { my_str("Usage: /client host port\n"); exit(1); } portno = my_atoi(argv[2]); sockfd = socket(AF_INET, SOCK_STREAM, 0); if (sockfd < 0) error("ERROR opening socket"); server = gethostbyname(argv[1]); if (server == NULL) { my_str("No host with that name found!\n"); exit(1); } my_bzero((char *) &serv_addr, sizeof(serv_addr)); serv_addr.sin_family = AF_INET; my_bcopy((char *)server->h_addr, (char *)&serv_addr.sin_addr.s_addr, server->h_length); serv_addr.sin_port = htons(portno); my_bzero(buffer, 256); my_str("Please enter your nickname: "); bytesRead = read(0, buffer, 255); buffer = inputClean(buffer); my_str("\n> "); if (connect(sockfd,(struct sockaddr *) &serv_addr,sizeof(serv_addr)) < 0) error("ERROR connecting"); if (write(sockfd, buffer, my_strlen(buffer)) < 0) /* write username */ error("write() error!\n"); FD_ZERO(&master); FD_ZERO(&readfd); FD_SET(sockfd, &master); FD_SET(0, &master); while(running == 1) { readfd = master; if(select(sockfd + 1, &readfd, NULL, NULL, NULL) < 0) error("Select() failed\n"); if(FD_ISSET(0, &readfd)) /* reading from console */ { my_bzero(buffer, 256); my_str("> "); bytesRead = read(0, buffer, 255); if (bytesRead < 0) error("read() error!\n"); buffer = inputClean(buffer); buffer[bytesRead] = '\0'; if (my_strcmp(buffer, "/exit") == 0) { my_str("Disconnecting from server.\n"); close(sockfd); exit(0); } else if (write(sockfd, buffer, my_strlen(buffer)) < 0) error("write() error!\n"); } else if(FD_ISSET(sockfd, &readfd)) /* reading from server */ { my_bzero(buffer, 256); bytesRead = read(sockfd, buffer, 255); if (bytesRead < 0) error("read() from server error!\n"); else if (bytesRead == 0) { my_str("Connection to server interrupted!\n"); close(sockfd); exit(0); } else { my_char('\n'); buffer[bytesRead] = '\0'; my_str(buffer); my_char('\n'); my_str("> "); } } } return 0; }
int main(int argc, char *argv[]) { int listener, newsockfd, portno, i, j, bytesRead; fd_set master, readfd; struct s_node *userList = NULL; struct user *newUser = NULL; socklen_t clilen; char buffer[256]; char *messageSending; struct sockaddr_in serv_addr, cli_addr; int n, maxFileDescriptors = 0; char **parsedInput; if (argc < 2) { my_str("Usage: /server [port]\n"); exit(1); } listener = socket(AF_INET, SOCK_STREAM, 0); if (listener < 0) { my_str("ERROR opening socket"); exit(1); } my_bzero((char *) &serv_addr, sizeof(serv_addr)); portno = my_atoi(argv[1]); serv_addr.sin_family = AF_INET; serv_addr.sin_addr.s_addr = INADDR_ANY; serv_addr.sin_port = htons(portno); my_memset(&(serv_addr.sin_zero), '\0', 8); if (bind(listener, (struct sockaddr *) &serv_addr, sizeof(serv_addr)) < 0) { my_str("Binding failed.\n"); exit(1); } listen(listener,5); FD_ZERO(&master); FD_ZERO(&readfd); FD_SET(listener, &master); maxFileDescriptors = listener; my_str("Server is now online!\n\n"); while(1) { readfd = master; if (select(maxFileDescriptors + 1, &readfd, NULL, NULL, NULL) < 0) error("select() failed!\n"); for(i = 3; i <= maxFileDescriptors; i++) { if(FD_ISSET(i, &readfd)) { if(i == listener) /* we got a new connection */ { my_str("Got a new connection!\n"); clilen = sizeof(cli_addr); newsockfd = accept(listener, (struct sockaddr *) &cli_addr, &clilen); if (newsockfd < 0) error("ERROR on accept"); /* read in username */ my_bzero(buffer, 256); bytesRead = read(newsockfd, buffer, 255); if (bytesRead > 0) /* was valid username */ { newUser = (struct user *)malloc(sizeof(struct user)); newUser->fd = newsockfd; newUser->nick = my_strconcat(buffer, ""); /* hacky lmao */ add_node(new_node(newUser, NULL, NULL), &userList); my_str("New user entry: "); my_str(buffer); my_char('\n'); messageSending = my_strconcat(buffer, " has connected."); for(j = 3; j <= maxFileDescriptors; j++) { if(FD_ISSET(j, &master)) { if(j != listener && j != i) { if (write(j, messageSending, my_strlen(messageSending)) < 0) error("write() error!\n"); } } } } else my_str("no nickname received...\n"); FD_SET(newsockfd, &master); if (newsockfd > maxFileDescriptors) maxFileDescriptors = newsockfd; } else /* we got text */ { bytesRead = read(i, buffer, 255); messageSending = ""; if (bytesRead > 1) /* got a message, not just a newline */ { buffer[bytesRead] = '\0'; if (buffer[0] == '/') /* special command */ { parsedInput = my_str2vect(buffer); if (my_strcmp(parsedInput[0], "/nick") == 0) { if (parsedInput[1] != NULL) { messageSending = my_strconcat(getUserNick(userList, i), " changed their nick to "); removeUser(&userList, i); newUser = (struct user *)malloc(sizeof(struct user)); newUser->fd = i; newUser->nick = parsedInput[1]; add_node(new_node(newUser, NULL, NULL), &userList); messageSending = my_strconcat(messageSending, getUserNick(userList, i)); } } else if (my_strcmp(parsedInput[0], "/me") == 0) { messageSending = my_strconcat(getUserNick(userList, i), " "); messageSending = my_strconcat(messageSending, my_vect2str(&parsedInput[1])); }else if (my_strcmp(parsedInput[0], "/exit") == 0) { messageSending = ""; }else /* invalid command */ { if (write(i, "Invalid command.", my_strlen("Invalid command.")) < 0) error("wrote() error!\n"); } } else /* normal message */ { messageSending = my_strconcat(getUserNick(userList, i), ": "); messageSending = my_strconcat(messageSending, buffer); } if (my_strcmp(messageSending, "") != 0) /* if wrote message to send, send it */ { my_str(messageSending); my_char('\n'); for(j = 3; j <= maxFileDescriptors; j++) { if(FD_ISSET(j, &master)) { if(j != listener && j != i) { if (write(j, messageSending, my_strlen(messageSending)) < 0) error("write() error!\n"); } } } } } else if (bytesRead == 0) /* we closed the file descriptor on client */ { messageSending = my_strconcat("SERVER: ", getUserNick(userList, i)); messageSending = my_strconcat(messageSending, " disconnected.\n"); my_str(messageSending); removeUser(&userList, i); for(j = 3; j <= maxFileDescriptors; j++) { if(FD_ISSET(j, &master)) { if(j != listener && j != i) { /* change to "SERVER: /nick disconnected" */ if (write(j, messageSending, my_strlen(messageSending)) < 0) error("write() error!\n"); } } } close(i); FD_CLR(i, &master); continue; } else if (bytesRead < 0) error("Read error...\n"); } } } } return 0; }
void bzero (void *d, size_t n) { TEST_ABORT; my_bzero (d, n); }