#include #include #include #include #include #include #include #include #include #include #define PORT 9000 #define HOST "127.0.0.1" #define BUF_SIZE 2048 int sock; /* Signal Handler for SIGINT */ void sigintHandler(int sig_num) { errno = 0; if (fcntl(sock, F_GETFD) != -1 || errno != EBADF) { write(sock, "exit\n", 6); close(sock); } printf("Terminating client\n"); fflush(stdout); exit(-1); } static inline void die(const char *msg) { perror(msg); exit(-1); } int write_all(int sock, char *buffer, int size) { int nwrite, sent = 0; while (sent < size) { if ((nwrite = write(sock, buffer + sent, size - sent)) < 0) { perror("Write"); exit(1); } sent += nwrite; } return sent; } void send_file(int server_fd, char *path) { char buffer[512]; int file_fd; if ((file_fd = open(path, O_RDONLY)) == -1) { perror("Open"); } FILE *file = fdopen(file_fd, "r"); fseek(file, 0L, SEEK_END); long int sz = ftell(file); fseek(file, 0L, SEEK_SET); int length = snprintf(NULL, 0, "%ld", sz); char msg[length + 1 + 3]; sprintf(msg, "< 0) { server_buffer[length] = '\0'; printf("%s", server_buffer); if (server_buffer[length - 1] == '\n') { printf("$> "); fflush(stdout); } } } } void read_stdin(int *done) { char stdinp_buffer[BUF_SIZE]; ssize_t length; while (!*done) { if ((length = read(STDIN_FILENO, stdinp_buffer, BUF_SIZE)) > 0) { switch (parse(sock, stdinp_buffer, length)) { case CONTINUE: continue; case BREAK: *done = 1; break; default: break; } } } }