#include <stdio.h>
#include <stdlib.h>
#include <unistd.h>
#include <string.h>
#include <errno.h>
#include <sys/types.h>
#include <sys/socket.h>
#include <sys/un.h>
#include <time.h>

#define K	1024
#define BUFSIZE	4096

int main(int argc, char *argv[])
{
  struct sockaddr_un name;
  char cbuf[K], buf[BUFSIZE], *path = "/tmp/serv_sock", *username;
  int r;

  if (argc > 1) path = argv[1];

  /* Create socket, initially un-connected. */
  int sock = socket(AF_UNIX, SOCK_DGRAM, 0);
  if (sock < 0) {
    perror("socket");
    exit(1);
  }
  
  username = getenv("USER");
  if (username == NULL) username = "???";

  /* Define name (filesystem socket file) to send to: */
  name.sun_family = AF_UNIX;
  strcpy(name.sun_path, path);

  
  while(1) {
    if (fgets(cbuf, K, stdin) == NULL) break;
    sprintf(buf, "%s: %s", username, cbuf);
    if (sendto(sock, buf, strlen(buf)+1, 0, (struct sockaddr *)&name, sizeof(struct sockaddr_un)) < 0) {
      perror("sendto");
      exit(1);
    }
  }
  close(sock);
  return 0;
}
