int my_user(int fd, FILE *fp, FILE *f_stdin, size_t len) { char *buff; int s; buff = NULL; if ((s = getline(&buff, &len, fp)) < 0) return (my_perror(EUNAVAILABLE, FAILURE, fd, 0)); putcolor(buff); if ((size_t)s > strlen(buff) && buff[0] != '3' && !my_free(buff)) return (SUCCESS); putstr("Please enter your password:\n"); if (getline(&buff, &len, f_stdin) < 0 || fputstr(fd, buff) == -1) return (my_perror(EUNAVAILABLE, FAILURE, fd, 0)); if (getline(&buff, &len, fp) < 0) return (my_perror(EUNAVAILABLE, FAILURE, fd, 0)); if (buff[strlen(buff) - 2] == '\r' && putcolor(buff) && !my_free(buff)) return (SUCCESS); putstr("Please confirm your password:\n"); if (getline(&buff, &len, f_stdin) < 0 || fputstr(fd, buff) == -1) return (my_perror(EUNAVAILABLE, FAILURE, fd, 0)); if (getline(&buff, &len, fp) < 0) return (my_perror(EUNAVAILABLE, FAILURE, fd, 0)); putcolor(buff); free(buff); return (SUCCESS); }
int complete_put(int fd, FILE *fp, FILE *f, struct stat stat) { int i; size_t len; char *buff; len = 0; buff = my_get_str(stat.st_size); if (fputstr(fd, buff) == -1) return (my_perror(EUNAVAILABLE, FAILURE, fd, 0)); if (fputstr(fd, "\n") == -1) return (my_perror(EUNAVAILABLE, FAILURE, fd, 0)); buff = my_free(buff); putstr("0%"); i = process_put(fd, fp, f, stat); putstr("\r100%%\n"); if (i >= 0) return (i); if (fclose(f) == -1) return (my_perror(EPTYPE, SUCCESS, fd, 0)); while ((i = getline(&buff, &len, fp)) >= 0 && buff[i - 2] != '\r') putstr(buff); if (i == -1) return (my_perror(EUNAVAILABLE, FAILURE, fd, 0)); putcolor(buff); my_free(buff); return (SUCCESS); }
/* ** Write the previously prepared actions to the server. */ static int write_actions(t_player *p, int const fd, int i) { char *part1; char *part2; while (++i < 10 && p->actions[i] >= 0) { if (i == 9 && p->actions[i] != LOOK) p->actions[i] = LOOK; if (i < 9 && p->actions[i + 1] < 0 && p->actions[i] != LOOK) p->actions[i + 1] = LOOK; part1 = p->actions[i] > PUT + NBR_OBJS ? "prend " : "pose "; part2 = NULL; if (p->actions[i] > PUT + NBR_OBJS) part2 = p->objects[p->actions[i] - 49]; else if (p->actions[i] < NBR_WR_ACTIONS) part1 = g_actions[p->actions[i]].name; else part2 = p->objects[p->actions[i] - 10]; if (p->actions[i] != BROADCAST && (fputstr(fd, part1) == -1 || (part2 && fputstr(fd, part2) == -1) || fputstr(fd, "\n") == -1)) return (EXIT_FAILURE); if (p->actions[i] == LOOK && i < 9) p->actions[i + 1] = -1; } return (1); }
int fputs(hal_uart_name_t u, const char *s) { fputstr(u, s); fputchar(u, '\n'); return 0; }
int init_put(int fd, FILE *fp, char **buf, char **tmp) { int i; size_t len; char *buff; i = 0; len = 0; if (!((*tmp) = strdup(*buf))) return (my_perror(EALLOC, FAILURE, fd, 0)); buff = (*buf) + 4; while (buff[0] == ' ' || buff[0] == '\t') buff += 1; while (buff[i] && buff[i] != '\n' && buff[i] != ' ' && buff[i] != '\t') i += 1; if (buff[i] != '\n') { *tmp = my_free(*tmp); fputstr(fd, "\n"); if (getline(tmp, &len, fp) == -1) return (my_perror(EUNAVAILABLE, FAILURE, fd, 0)); free(*tmp); return (my_perror(ESYNTAX, SUCCESS, 0, 0)); } buff[i] = '\0'; (*buf) = buff; return (-1); }
int process_put(int fd, FILE *fp, FILE *f, struct stat stat) { char *buff; size_t len; int s; int i; s = 0; len = 0; buff = 0; while ((i = getline(&buff, &len, f)) >= 0) { s += i; disp_percentage(100 * s / stat.st_size); if (write(fd, buff, i) == -1) return (my_perror(EUNAVAILABLE, FAILURE, fd, 0)); if (buff[i - 1] != '\n') if (fputstr(fd, "\n") == -1) return (my_perror(EUNAVAILABLE, FAILURE, fd, 0)); if ((i = getline(&buff, &len, fp)) == -1) return (my_perror(EUNAVAILABLE, FAILURE, fd, 0)); if (i >= 2 && buff[i - 2] == '\r' && putstr(remove_ret(buff)) && !my_free(buff)) return (SUCCESS); } my_free(buff); return (-1); }
int my_put(int fd, FILE *fp, char *buff, size_t len) { struct stat stat; FILE *f; char *tmp; char *cpy; int ret; cpy = buff; if ((ret = init_put(fd, fp, &buff, &tmp)) >= 0) return (ret); if (access(buff, F_OK) || lstat(buff, &stat) == -1 || stat.st_size == 4096 || !(f = fopen(buff, "r"))) { tmp = my_free(tmp); if (fputstr(fd, "\n") == -1 || getline(&tmp, &len, fp) == -1) return (my_perror(EUNAVAILABLE, FAILURE, fd, 0)); free(tmp); if (stat.st_size == 4096) return (my_perror(EARGNOTIMP, SUCCESS, 0, 0)); return (my_perror(ENOTTAKEN, SUCCESS, 0, 0)); } free(cpy); putstr(FSTATOK); if (rfputstr(fd, tmp) == -1 || (tmp = my_free(tmp))) return (my_perror(EUNAVAILABLE, FAILURE, fd, 0)); return (complete_put(fd, fp, f, stat)); }
static char *my_msg_loop(char **dest_list, t_fd *self, t_fd *fds, char *msg) { int i; t_fd *ptr; char *str; i = 0; while (dest_list[i]) { ptr = fds; while (ptr && (!ptr->nick || strcmp(ptr->nick, dest_list[i]))) ptr = ptr->next; if (!ptr) { str = err_serv("401 ", self->nick, dest_list[i], " :No such nick\r\n"); free_tab(dest_list); free(msg); return (str); } if (fputstr(ptr->fd, msg) == -1) return (NULL); ++i; } free(msg); free_tab(dest_list); return (strdup("\r\n")); }
int handle_client(int fd, char **pwds) { char *buff; size_t len; char *cmd; FILE *fp; len = 0; if (!(fp = fdopen(fd, "r+"))) return (my_perror(EOPEN, FAILURE, 0, fd)); fputstr(fd, "220 Service ready for new user.\r\n"); if (!(buff = my_user(fd, fp, "Anonymous\n", pwds))) return (FAILURE); buff = my_free(buff); while (getline(&buff, &len, fp) >= 0 && strcmp(buff, "quit\r\n")) { if (!(cmd = strdup(buff))) return (my_perror(EALLOC, FAILURE, 0, fd)); if (!handle_cmd(fd, fp, cmd, pwds)) return (FAILURE); } if (!buff || strcmp(buff, "quit\n") || my_free(buff)) return (FAILURE); if (fclose(fp) == -1) return (my_perror(ECCLOSE, FAILURE, fd, 0)); return (SUCCESS); }
int display_prompt(int fd, char **pwds) { char *prompt; int i; int s; if (pwds && pwds[HOME]) { s = strlen(pwds[HOME]); i = s - 1; while (--i >= 0 && pwds[HOME][i] != '/'); if (!(prompt = malloc(sizeof(*prompt) * (strlen(EOP) + s - (i + 1))))) return (my_perror(EALLOC, FAILURE, 0, fd)); strncpy(prompt, pwds[HOME] + i + 1, s - (i + 2)); prompt[s - (i + 2)] = '\0'; strcat(prompt, EOP); } else if (!(prompt = DEFAULT_PROMPT)) return (my_perror(EALLOC, FAILURE, 0, fd)); if (fputstr(fd, prompt) == -1) return (my_perror(EWRITE, FAILURE, 0, fd)); free(prompt); return (SUCCESS); }
/* ** First actions done by player. */ static int init_player_actions(int const fd, t_player *p, int const connect_nbr) { int i; i = 0; if (!connect_nbr) { p->actions[i++] = FORK; if (fputstr(fd, "fork\n") == -1) return (0); } p->actions[i++] = LOOK; if (fputstr(fd, "voir\n") == -1) return (0); p->actions[i] = -1; return (1); }
/* ** If asked to elevate: ** Then check if it successfully begun. */ int check_elevation(t_player *p, char *data) { if (p->actions[0] == ELEVATION) { if (strcmp(data, "elevation en cours")) { p->rising = 0; fputstr(2, "\033[31mElevation failed!!\033[0m\n"); } else { p->rising = 1; putstr("\033[32mElevation begun!!\033[0m\n"); } return (1); } if (p->rising && !strcmp(data, "ko")) { p->rising = 0; fputstr(2, "\033[31mElevation failed!!\033[0m\n"); return (0); } return (1); }
static int send_grouped_msg(t_fd *fds, t_fd *self, char *channel, char *msg) { t_fd *ptr; int i; ptr = fds; while (ptr) { i = 0; if (ptr != self && ptr->channels) { while (ptr->channels[i] && strcmp(ptr->channels[i], channel)) i += 1; if (ptr->channels[i]) if (fputstr(ptr->fd, msg) == -1) return (0); } ptr = ptr->next; } return (1); }
int putstr(const char *s) { return fputstr(DEFAULT_UART, s); }