xref: /freebsd/sbin/ipf/libipf/ipf_perror.c (revision 9768746b)
1 #include <fcntl.h>
2 #include <sys/ioctl.h>
3 #include "ipf.h"
4 
5 void
6 ipf_perror(int err, char *string)
7 {
8 	if (err == 0)
9 		fprintf(stderr, "%s\n", string);
10 	else
11 		fprintf(stderr, "%s: %s\n", string, ipf_strerror(err));
12 }
13 
14 int
15 ipf_perror_fd( int fd, ioctlfunc_t iocfunc, char *string)
16 {
17 	int save;
18 	int realerr;
19 
20 	save = errno;
21 	if ((*iocfunc)(fd, SIOCIPFINTERROR, &realerr) == -1)
22 		realerr = 0;
23 
24 	errno = save;
25 	fprintf(stderr, "%d:", realerr);
26 	ipf_perror(realerr, string);
27 	return (realerr ? realerr : save);
28 
29 }
30 
31 void
32 ipferror(int fd, char *msg)
33 {
34 	if (fd >= 0) {
35 		ipf_perror_fd(fd, ioctl, msg);
36 	} else {
37 		fprintf(stderr, "0:");
38 		perror(msg);
39 	}
40 }
41