#include "chat.h"
#define BLKSIZE  1024
void* reader(void *arg){
  int outfd = *((int *)arg);
  ssize_t bytesread;
  ssize_t byteswritten;
  char buf[BLKSIZE];
  while((bytesread = u_read(STDIN_FILENO, buf, BLKSIZE)) > 0){
      byteswritten = u_write(outfd, buf, bytesread);
      if (byteswritten != bytesread) {
        fprintf(stderr,
                "Error writing %ld bytes, %ld bytes written\n",
                (long)bytesread, (long)byteswritten);
        break;  }
    }
  fprintf(stderr, "reader exiting\n");
  kill(getpgrp(),SIGKILL);
  pthread_exit(NULL);
}
void *writer(void* arg){
  ssize_t bytesread;
  ssize_t byteswritten;
  char buf[BLKSIZE];
  int communfd = *((int *)arg);
  while((bytesread = u_read(communfd, buf, BLKSIZE)) > 0){
    byteswritten = u_write(STDOUT_FILENO, buf, bytesread);
    if (bytesread != byteswritten) {
      fprintf(stderr,
              "Error writing %ld bytes, %ld bytes written\n",
              (long)bytesread, (long)byteswritten);
      break;    }
  }
  fprintf(stderr, "writer exiting\n");
  kill(getpgrp(),SIGKILL);
  pthread_exit(NULL);
}