ccx-utils

Miscellaneous utilities written in C
git clone https://ccx.te2000.cz/git/ccx-utils
Log | Files | Refs

socketpair.c (749B)


      1 #include <unistd.h>
      2 
      3 #include <skalibs/types.h>
      4 #include <skalibs/strerr.h>
      5 #include <skalibs/djbunix.h>
      6 #include <skalibs/exec.h>
      7 #include <skalibs/socket.h>
      8 
      9 #define USAGE "socketpair fd0 fd1 prog..."
     10 #define PROG "socketpair"
     11 
     12 int main (int argc, char const *const *argv)
     13 {
     14   int fd0, fd1;
     15   int sv[2];
     16   if ((argc < 4)
     17    || !uint0_scan(argv[1], (unsigned int *)&fd0)
     18    || !uint0_scan(argv[2], (unsigned int *)&fd1)
     19    || (fd0 == fd1))
     20     strerr_dieusage(100, USAGE) ;
     21 
     22   if (ipc_pair_b(sv) != 0) {
     23     strerr_diefu1sys(111, "create socketpair") ;
     24   }
     25 
     26   if (sv[1] == fd0) sv[1] = dup(sv[1]) ;
     27   if ((sv[1] == -1)
     28    || (fd_move(fd0, sv[0]) == -1)
     29    || (fd_move(fd1, sv[1]) == -1))
     30     strerr_diefu1sys(111, "move fds") ;
     31   xexec(argv+3) ;
     32 }