void ft_mkdir(t_data *uni) { char *str; uni->i = 5; if (ft_strlen(uni->buf) < 5) { ft_send(uni->cs, 0, "ERROR need a name\n", 18); return ; } else { str = ft_strdup(uni->buf); while (uni->i >= 0) { str[uni->i] = ' '; uni->i--; } if ((str = ft_strtrim(str)) == NULL) { ft_send(uni->cs, 0, "ERROR need a name\n", 18); return ; } mkdir(str, 0777); ft_send(uni->cs, 0, "SUCCESS\n", 8); } }
void be_client(char *ip, int port) { t_socket sock; fd_set rdfs; char *ask; char *prompt; sock = init_connection(ip, port); while (1) { init_socks(sock, &rdfs); if (FD_ISSET(STDIN_FILENO, &rdfs)) client_in(sock); else if (FD_ISSET(sock, &rdfs)) { client_out(sock); ask = ft_strdup("/gp\n"); ft_send(sock, ask); free(ask); prompt = get_message(sock); ft_putstr("\n"); ft_putstr(prompt); free(prompt); } } }
t_bool live(t_env *env, t_player *player) { t_bool action; t_bool alive; int i; t_cmds *cmds; i = 0; alive = TRUE; action = TRUE; cmds = ft_memalloc(sizeof(t_cmds)); cmds->need_c = TRUE; cmds->nb_cmd = 0; while (alive) { process_ia(env, player, &cmds); if ((action = ft_recv(env, player, cmds->order[0])) == TRUE) reorder_cmd(&cmds); if (action == TRUE || !i) ft_send(env->s_sock, &cmds->order[0]); else if (action == END) break ; action = FALSE; i++; } free(cmds); return (TRUE); }
int s_ls(char *arg) { DIR *od; struct dirent *rd; unsigned char *data; unsigned char *tmp; if (!(od = opendir("."))) return (ft_error("opendir() error")); data = (unsigned char *)ft_strdup(""); while ((rd = readdir(od))) if ((rd->d_name && rd->d_name[0] != '.') || \ (arg && ft_strequ(arg, "-a"))) { tmp = (unsigned char *)ft_strjoin((char *)data, rd->d_name); free(data); data = (unsigned char *)ft_strjoin((char *)tmp, "\n"); free(tmp); } if (closedir(od) == -1) return (ft_error("closedir() error")); if (!*data) free(data), data = (unsigned char *)ft_strdup("EMPTY_SCUMBAG4242"); if (ft_send(data, ft_strlen_uc(data)) == -1) return (-1); return (0); }
void ft_cd(t_data *uni) { char *str; int m; char *pwd; pwd = (char *)malloc(sizeof(char) * 2000); m = 0; uni->i = 2; pwd = getcwd(pwd, 2000); str = ft_strdup(uni->buf); while (uni->i >= 0) { str[uni->i] = ' '; uni->i--; } str = ft_strtrim(str); if ((uni->i = chdir(str)) == -1) { ft_send(uni->cs, 0, "ERROR wrong path\n", 17); free(str); free(pwd); return ; } test_dir(uni, pwd); free(str); free(pwd); }
void get_pwd(int sock) { char *buff; errno = 0; if ((buff = (char*)malloc(sizeof(char) * 2000)) == NULL) { send(sock, "erreur de malloc\n", 17, 0); return ; } if ((buff = getcwd(buff, 1023)) != NULL) { ft_send(sock, 0, buff, ft_strlen(buff)); ft_send(sock, 0, "SUCCESS\n", 8); } else gest_err(sock); }
void ft_ls(t_data *uni, int m) { char *rep; DIR *flux; if ((m = check_arg(uni, 0)) == 2) return ; if ((flux = opendir("./")) == NULL) { ft_send(uni->cs, 0, "ERROR opendir fail\n", 19); return ; } rep = make_ls(m, flux); closedir(flux); ft_send(uni->cs, 0, rep, ft_strlen(rep)); ft_strdel(&rep); return ; }
void test_dir(t_data *uni, char *pwd) { char *where; int m; m = 0; where = (char *)malloc(sizeof(char) * 2000); where = getcwd(where, 2000); if (ft_strlen(where) < ft_strlen(uni->pwd)) { m = 1; chdir(pwd); ft_send(uni->cs, 0, "ERROR access denied\n", 20); } else { m = 0; ft_send(uni->cs, 0, "SUCCESS\n", 8); } free(where); return ; }
void ft_put(char *cmd, int cs, char *pwd) { int fd; char *path; int r; char buf[1024]; path = ft_strjoin(pwd, cmd); if ((fd = open(path, O_WRONLY | O_CREAT | O_TRUNC, S_IRUSR | S_IWUSR)) != -1) { send(cs, "S", 1, 0); while ((r = recv(cs, buf, 1023, 0)) == 1023) ft_send(fd, cs, buf, r); if (r > 0) ft_send(fd, cs, buf, r); if (r < 0) send(cs, "E2", 2, 0); close(fd); } else send(cs, "E1", 2, 0); }
static void client_in(t_socket sock) { char *message; if ((message = get_next_line(STDIN_FILENO)) != NULL) { if (!strcmp(message, "/exit") || !strcmp(message, "/quit")) { free(message); ft_exit("Client exited\n"); } message = ft_strcleanjoin(message, "\n"); ft_send(sock, message); free(message); } }
void do_it(t_truck *b, int sock) { while ((b->i = read(0, b->line, 2048)) > 0) { b->line[b->i] = '\0'; ft_send(sock, 0, b->line, ft_strlen(b->line)); if ((ft_strnstr(b->line, "quit", 4)) != NULL) break ; b->r = c_trans(b, sock); if (b->r == 0) { ft_recv(sock, &b->buff); printf("%s\n", b->buff); } ft_strdel(&b->buff); ft_bzero(b->line, 2048); write(1, "client$$$:", 10); } return ; }
G_MODULE_EXPORT gboolean on_btnSend_clicked(void) { char *item; if(!ft_connected(&ft)){ status("Not connected ya fool!"); return FALSE; } settimeout(0); lastfraction = 0; while((item = queue_next(&file_queue))){ const char *basename = strrchr(item, G_DIR_SEPARATOR); QUEUE_REM(item); if(!basename++) basename = item; status("Waiting for confirmation for \"%s\"", item); while(gtk_events_pending()) gtk_main_iteration(); if(ft_send(&ft, callback, item, 1)){ status("Couldn't send %s: %s", basename, ft_lasterr(&ft)); CLOSE(); break; }else{ status("Sent %s", basename); STAY_OPEN(); } } gtk_widget_set_sensitive(btnSend, FALSE); return FALSE; }
int s_exec_get(unsigned char *cmd, unsigned char *arg, int r) { unsigned char *data; unsigned int size; int fd; if (check_ftp((char *)cmd, arg, &r, 0) == -1) return (-1); if (!r) return (0); size = 0; data = (unsigned char *)ft_strdup(""); if ((fd = open((char *)arg, O_RDONLY)) == -1) return (ft_error("open() error")); if ((r = s_exec_get_(fd, &data, &size)) == -1) return (ft_error("read() error")); if (close(fd) == -1) return (ft_error("close() error")); if (ft_send(data, size) == -1) return (-1); return (0); }
int s_exec_put(unsigned char *cmd, unsigned char *arg, int r) { unsigned char *data; int fd; unsigned int size; if (check_ftp((char *)cmd, arg, &r, 0) == -1) return (-1); if (!r) return (0); if ((fd = open((char *)arg, O_WRONLY | O_CREAT | O_TRUNC, 0700)) == -1) return (ft_error("open() error")); if (ft_recv(&data, &size) == -1) return (-1); if (write(fd, data, size) == -1) return (ft_error("write() error")); free(data); if (close(fd) == -1) return (ft_error("close() error")); if (ft_send((unsigned char *)ft_strdup("OK"), 2) == -1) return (-1); return (0); }
int ft_ls(t_ctx *ctx) { DIR *dirp; struct dirent *dp; char *res; char *tmp; char *tmp2; (void)ctx; res = NULL; if (NULL == (dirp = opendir("."))) return (ERR); while ((dp = readdir(dirp)) != NULL) { tmp = ft_strjoin(dp->d_name, "\n"); tmp2 = ft_strjoin(res, tmp); ft_memdel((void **)&tmp); ft_memdel((void **)&res); res = tmp2; } closedir(dirp); ft_send(res, STR_SUCCESS); return (OK); }