xref: /freebsd-src/sbin/ipf/libipf/ipf_perror.c (revision 44bc30192139b0b3c95510ab3b35802bcc6d63e4)
141edb306SCy Schubert #include <fcntl.h>
241edb306SCy Schubert #include <sys/ioctl.h>
341edb306SCy Schubert #include "ipf.h"
441edb306SCy Schubert 
541edb306SCy Schubert void
ipf_perror(int err,char * string)6efeb8bffSCy Schubert ipf_perror(int err, char *string)
741edb306SCy Schubert {
841edb306SCy Schubert 	if (err == 0)
941edb306SCy Schubert 		fprintf(stderr, "%s\n", string);
1041edb306SCy Schubert 	else
1141edb306SCy Schubert 		fprintf(stderr, "%s: %s\n", string, ipf_strerror(err));
1241edb306SCy Schubert }
1341edb306SCy Schubert 
1441edb306SCy Schubert int
ipf_perror_fd(int fd,ioctlfunc_t iocfunc,char * string)15efeb8bffSCy Schubert ipf_perror_fd( int fd, ioctlfunc_t iocfunc, char *string)
1641edb306SCy Schubert {
1741edb306SCy Schubert 	int save;
1841edb306SCy Schubert 	int realerr;
1941edb306SCy Schubert 
2041edb306SCy Schubert 	save = errno;
2141edb306SCy Schubert 	if ((*iocfunc)(fd, SIOCIPFINTERROR, &realerr) == -1)
2241edb306SCy Schubert 		realerr = 0;
2341edb306SCy Schubert 
2441edb306SCy Schubert 	errno = save;
2541edb306SCy Schubert 	fprintf(stderr, "%d:", realerr);
2641edb306SCy Schubert 	ipf_perror(realerr, string);
27*2582ae57SCy Schubert 	return (realerr ? realerr : save);
2841edb306SCy Schubert 
2941edb306SCy Schubert }
3041edb306SCy Schubert 
3141edb306SCy Schubert void
ipferror(int fd,char * msg)32efeb8bffSCy Schubert ipferror(int fd, char *msg)
3341edb306SCy Schubert {
3441edb306SCy Schubert 	if (fd >= 0) {
3541edb306SCy Schubert 		ipf_perror_fd(fd, ioctl, msg);
3641edb306SCy Schubert 	} else {
3741edb306SCy Schubert 		fprintf(stderr, "0:");
3841edb306SCy Schubert 		perror(msg);
3941edb306SCy Schubert 	}
4041edb306SCy Schubert }
41