int ft_select(int ac, char **av, t_select *params) { fputs("cl"); if (!(params = init_list(ac, av, params))) return (-1); ft_get_col_n(params); ft_check_size(params); save_term(params, 0); get_key(params); return (0); }
int main(int argc, char **argv) { int opt, pnum; char *lfile; int lflag = 0, pflag = 0; static struct option long_options[] = { { "port", required_argument, 0, 'p' }, { "log", optional_argument, 0, 'l' }, { "encrypt", required_argument, 0, 'e' }, { "debug", no_argument, 0, 'd' }, { 0, 0, 0, 0 } }; while ((opt = getopt_long(argc, argv, "p:l:e:d", long_options, NULL)) != -1) { if (opt == 'p') { pnum = atoi(optarg); pflag = 1; } else if (opt == 'l') { lfile = optarg; lflag = 1; } else if (opt == 'e') { enckeyf = optarg; eflag = 1; } else if (opt == 'd') { dflag = 1; } else pexit(); } if (!pflag) pexit(); if (lflag) { logffd = creat(lfile, S_IRWXU); if (logffd <= -1) error_exit("failed creat()", errno); } if (eflag) setup_encryption(); save_term(); atexit(restore_term); struct termios non_canonical_input_mode; tcgetattr(STDIN_FILENO, &non_canonical_input_mode); non_canonical_input_mode.c_iflag = ISTRIP; //only lower 7 bits non_canonical_input_mode.c_oflag = 0; // no processing non_canonical_input_mode.c_lflag = 0; // no processing if (tcsetattr(STDIN_FILENO, TCSANOW, &non_canonical_input_mode) <= -1) error_exit("failed to establish terminal ", errno); struct sockaddr_in addy; struct hostent *server = gethostbyname("localhost"); int socket_id = socket(AF_INET, SOCK_STREAM, 0); if (socket_id <= -1) error_exit("failed to open socket", errno); memset((char *)&addy, 0, sizeof(addy)); addy.sin_family = AF_INET; addy.sin_port = htons(pnum); memcpy((char *)&addy.sin_addr.s_addr, (char *)server->h_addr, server->h_length); if (connect(socket_id, (struct sockaddr*)&addy, sizeof(addy)) <= -1) error_exit("error connecting \n", errno); struct pollfd poll_s[2]; poll_s[0].fd = STDIN_FILENO; poll_s[1].fd = socket_id; poll_s[0].events = POLLIN | POLLHUP | POLLERR; poll_s[1].events = POLLIN | POLLHUP | POLLERR; for (;;) { if (poll(poll_s, 2, 0) <= -1) { fprintf(stderr, "poll failed"); exit(1); } if (poll(poll_s, 2, 0) == 0) continue; if (poll_s[0].revents & POLLIN) { char frmstdin[256]; int numread = sread(STDIN_FILENO, frmstdin, 256); do_write(frmstdin, STDOUT_FILENO, numread); if (eflag) { int n=0; while (n < numread) { if (frmstdin[n] != '\r' && frmstdin[n] != '\n' && (mcrypt_generic(td, &frmstdin[n], 1) != 0)) { error_exit("failed to encrypt", errno); } n++; } } if (lflag) do_write_log(frmstdin, numread, tserver); do_write(frmstdin, poll_s[1].fd, numread); } if (poll_s[1].revents & POLLIN) { char frmsrv[256]; int numread = sread(poll_s[1].fd, frmsrv, 256); if (numread == 0) { if (close(socket_id) <= -1) error_exit("failed to close", errno); exit(0); } if (lflag) do_write_log(frmsrv, numread, fserver); if (eflag && (mdecrypt_generic(td, &frmsrv, numread) != 0)) error_exit("failed to encrypt", errno); do_write(frmsrv, STDOUT_FILENO, numread); } if (poll_s[1].revents & (POLLHUP | POLLERR)) { if (close(socket_id) <= -1) error_exit("failed to close", errno); exit(0); } } exit(0); }