unp.h
#ifndef _UNP_H_
#define _UNP_H_
#include <unistd.h>
#include <stdio.h>
#include <stdlib.h>
#include <errno.h>
#include <fcntl.h>
#include <stdarg.h>
#include <signal.h>
#include <string.h> /* for strerror */
#include <sys/wait.h>
#include <sys/select.h>
#include <sys/socket.h>
#include <netinet/in.h>
#include <arpa/inet.h>
#include <sys/types.h>
#define MAXLINE 4096
#define SERV_PORT 6666
typedef void Sigfunc(int);
#define max(a,b) a>b?a:b
ssize_t readn(int fd,void *ptr,size_t n);
ssize_t writen(int fd,const void *ptr,size_t n);
ssize_t Read(int fd,void *buff,size_t n);
void Write(int fd,const void *buff,size_t n);
char *Fgets(char *buff,int nbytes,FILE *fp);
int Fputs(const char *str,FILE *fp);
void Close(int fd);
void err_quit(char *fmt,...);
int Socket(int family,int type,int protocol);
void Connect(int sockfd,const struct sockaddr *servaddr,socklen_t addrlen);
void Bind(int sockfd,const struct sockaddr *myaddr,socklen_t addrlen);
void Listen(int sockfd,int backlog);
int Accept(int sockfd,struct sockaddr *cliaddr,socklen_t *addrlen);
int Fork();
Sigfunc *Signal(int signo,Sigfunc *func);
int Select(int maxfdp1,fd_set *readsex,fd_set *writeset,fd_set *exceptset,
struct timeval *timeout);
char *sock_ntop(const struct sockaddr *sockaddr, socklen_t addrlen);
#endif
unp.c
#include "unp.h"
ssize_t readn(int fd,void *ptr,size_t n){
size_t nleft;
ssize_t nread;
nleft=n;
while(nleft > 0){
if((nread=read(fd,ptr,nleft)) <0){
if(nleft == n)
return -1;
else
break;
}else if(nread == 0)
break;
nleft -= nread;
ptr += nread;
}
return(n-nleft);
}
ssize_t writen(int fd,const void *ptr,size_t n){
size_t nleft;
ssize_t nwritten;
nleft=n;
while(nleft > 0){
if((nwritten=write(fd,buff,nleft)) < 0){
if(nleft == n)
return -1;
else
break;
}else if(nwritten == 0)
break;
nleft -= nwritten;
ptr += nwritten;
}
return(n-nleft);
}
ssize_t Read(int fd,void *buff,size_t n){
int nread;
if((nread=read(fd,buff,n)) < 0)
err_quit("read error");
return nread;
}
void Write(int fd,const void *buff,size_t n){
if(write(fd,buff,n) != n)
err_quit("write error");
}
char *Fgets(char *buff,int nbytes,FILE *fp){
char *ptr=fgets(buff,nbytes,fp);
if(ptr==NULL && ferror(fp))
err_quit("fgets error");
return ptr;
}
int Fputs(const char *str,FILE *fp){
int ret=fputs(str,fp);
if(ret == EOF)
err_quit("fputs error");
return ret;
}
void Close(int fd){
if(close(fd)<0)
err_quit("close error");
}
void err_quit(char *fmt,...){
int errno_save=errno;
va_list va;
va_start(va,fmt);
vfprintf(stderr,fmt,va);
if(errno_save != 0)
fprintf(stderr," :%s",strerror(errno_save));
fprintf(stderr,"
");
va_end(va);
exit(0);
}
int Socket(int family,int type,int protocol){
int sockfd=socket(family,type,protocol);
if(sockfd == -1)
err_quit("socket error");
return sockfd;
}
void Connect(int sockfd,const struct sockaddr *servaddr,socklen_t addrlen){
if(connect(sockfd,servaddr,addrlen) == -1)
err_quit("connect error");
}
void Bind(int sockfd,const struct sockaddr *myaddr,socklen_t addrlen){
if(bind(sockfd,myaddr,addrlen) == -1)
err_quit("bind error");
}
void Listen(int sockfd,int backlog){
if(listen(sockfd,backlog) == -1)
err_quit("listen error");
}
int Accept(int sockfd,struct sockaddr *cliaddr,socklen_t *addrlen){
int connfd=accept(sockfd,cliaddr,addrlen);
if(connfd < 0)
err_quit("accept error");
return connfd;
}
int Fork(){
pid_t pid=fork();
if(pid < 0)
err_quit("fork error");
return pid;
}
Sigfunc *Signal(int signo,Sigfunc *func){
Sigfunc *sigfunc=signal(signo,func);
if(sigfunc == SIG_ERR)
err_quit("signal error");
return sigfunc;
}
int Select(int maxfdp1,fd_set *readsex,fd_set *writeset,fd_set *exceptset,
struct timeval *timeout){
int ret;
if((ret=select(maxfdp1,readsex,writeset,exceptset,timeout)) < 0)
err_quit("select error");
return ret;
}
char *sock_ntop(const struct sockaddr *sa, socklen_t addrlen){
char portstr[8];
static char str[128];
switch(sa->sa_family){
case AF_INET:{
struct sockaddr_in *sin=(struct sockaddr_in *)sa;
if(inet_ntop(AF_INET,&sin->sin_addr,str,sizeof(str)) == NULL)
return(NULL);
if(ntohs(sin->sin_port) != 0){
snprintf(portstr,sizeof(portstr),":%d",
ntohs(sin->sin_port));
strcat(str,portstr);
}
return(str);
}
case AF_INET6:{
struct sockaddr_in6 *sin=(struct sockaddr_in6 *)sa;
if(inet_ntop(AF_INET6,&sin->sin6_addr,str,sizeof(str)) == NULL)
return(NULL);
if(ntohs(sin->sin6_port) != 0){
snprintf(portstr,sizeof(portstr),":%d",
ntohs(sin->sin6_port));
strcat(str,portstr);
}
return(str);
}
default:
printf("unknown sockaddr family
");
break;
}
}