library/socket.c
int serv_listen(const char *name)
{
  int                sockfd, addrlen;
  struct sockaddr_un unix_addr;
  struct sockaddr    *sock_addr;

  sockfd = socket(AF_UNIX, SOCK_STREAM, 0);
  if (sockfd < 0) {
    warn("socket");
    return -1;
  }

  addrlen = strlen(name);
  if (addrlen >= sizeof (unix_addr.sun_path)) {
    warnx("serv_listen: socket name too long");
    return -2;
  }
  addrlen += sizeof (unix_addr.sun_family);

  memset(&unix_addr, 0, sizeof (unix_addr));
  unix_addr.sun_family = AF_UNIX;
  strcpy(unix_addr.sun_path, name);

  unlink(name);
  sock_addr = (struct sockaddr *) &unix_addr;
  if (bind(sockfd, sock_addr, addrlen) < 0) {
    warn("bind");
    return -3;
  }

  if (listen(sockfd, 5) < 0) {
    warn("listen");
    return -4;
  }
  return sockfd;
}
// Advanced Programming in the Unix Environment, Program 15.22
Advanced Programming in the Unix Environment, Program 15.22
Advanced Programming in the Unix Environment Program 15 . 22
int serv_listen(const char *name)
{
  int                sockfd, addrlen;
  struct sockaddr_un unix_addr;
  struct sockaddr    *sock_addr;
  sockfd = socket(AF_UNIX, SOCK_STREAM, 0);
  if (sockfd < 0) {
    warn("socket");
    return -1;
  }
  addrlen = strlen(name);
  if (addrlen >= sizeof (unix_addr.sun_path)) {
    warnx("serv_listen: socket name too long");
    return -2;
  }
  addrlen += sizeof (unix_addr.sun_family);
  memset(&unix_addr, 0, sizeof (unix_addr));
  unix_addr.sun_family = AF_UNIX;
  strcpy(unix_addr.sun_path, name);
  unlink(name);
  sock_addr = (struct sockaddr *) &unix_addr;
  if (bind(sockfd, sock_addr, addrlen) < 0) {
    warn("bind");
    return -3;
  }
  if (listen(sockfd, 5) < 0) {
    warn("listen");
    return -4;
  }
  return sockfd;
}
int serv_listen ( const char * name ) { int sockfd , addrlen ; struct sockaddr_un unix_addr ; struct sockaddr * sock_addr ; sockfd = socket ( AF_UNIX , SOCK_STREAM , 0 ) ; if ( sockfd < 0 ) { warn ( " " ) ; return -1 ; } addrlen = strlen ( name ) ; if ( addrlen >= sizeof ( unix_addr . sun_path ) ) { warnx ( " " ) ; return -2 ; } addrlen += sizeof ( unix_addr . sun_family ) ; memset ( & unix_addr , 0 , sizeof ( unix_addr ) ) ; unix_addr . sun_family = AF_UNIX ; strcpy ( unix_addr . sun_path , name ) ; unlink ( name ) ; sock_addr = ( struct sockaddr * ) & unix_addr ; if ( bind ( sockfd , sock_addr , addrlen ) < 0 ) { warn ( " " ) ; return -3 ; } if ( listen ( sockfd , 5 ) < 0 ) { warn ( " " ) ; return -4 ; } return sockfd ; }
Advanced Programming in the Unix Environment, Program 15.22
Advanced Programming in the Unix Environment Program 15 . 22