#include "scan.h"
#include "strerr.h"
#include "socket_unix.h"
#include "socket_passfd.h"

void die_usage(void) {
  strerr_die1x(100, "passfd: usage: passfd fd socket");
}

int main(int argc, char **argv) {
  int fd, socket;

  if(!argv[1] || !scan_uint(argv[1], &fd))
    die_usage();

  socket = socket_unix_dgram();
  if(socket < 0)
    strerr_die1sys(111, "passfd: unable to make socket");

  if(!argv[2])
    die_usage();
  if(socket_unix_connect(socket, argv[2]) < 0)
    strerr_die3sys(111, "passfd: unable to connect to ", argv[2], ": ");

  if(socket_passfd(socket, fd) < 0)
    strerr_die5sys(111, "passfd: unable to pass fd ", argv[1], "to ", 
        argv[2], ": ");

  return 0;
}
