-
Notifications
You must be signed in to change notification settings - Fork 0
/
common.h
110 lines (90 loc) · 1.96 KB
/
common.h
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
/*
* contains common headers and some error handling methods
*/
#ifndef _COMMON_H
#define _COMMON_H
#include <stdio.h>
#include <stdarg.h>
#include <stdlib.h>
#include <string.h>
#include <unistd.h>
#include <errno.h>
#include <fcntl.h>
#include <syslog.h>
#include <signal.h>
#define MAXLINE 4096
static void handle_error(int errnoflag, int error, const char *fmt, va_list ap)
{
char buf[MAXLINE];
vsnprintf(buf, MAXLINE, fmt, ap);
if (errnoflag)
snprintf(buf + strlen(buf), MAXLINE - strlen(buf), ": %s"
, strerror(error));
strcat(buf, "\n");
fputs(buf, stderr);
}
void err_quit(const char *fmt, ...)
{
va_list ap;
va_start(ap, fmt);
handle_error(0, 0, fmt, ap);
va_end(ap);
exit(1);
}
void err_exit(int error, const char *fmt, ...)
{
va_list ap;
va_start(ap, fmt);
handle_error(1, error, fmt, ap);
va_end(ap);
exit(1);
}
void err_sys(const char *fmt, ...)
{
va_list ap;
va_start(ap, fmt);
handle_error(1, errno, fmt, ap);
va_end(ap);
exit(1);
}
void err_ret(const char *fmt, ...)
{
va_list ap;
va_start(ap, fmt);
handle_error(1, errno, fmt, ap);
va_end(ap);
}
void
set_fl(int fd, int flags) /* flags are file status flags to turn on */
{
int val;
if ( (val = fcntl(fd, F_GETFL, 0)) < 0)
{
printf("fcntl F_GETFL error");
exit(1);
}
val |= flags; /* turn on flags */
if (fcntl(fd, F_SETFL, val) < 0)
{
printf("fcntl F_SETFL error");
exit(1);
}
}
void
clr_fl(int fd, int flags)
{
int val;
if ((val = fcntl(fd, F_GETFL, 0)) == -1)
{
syslog(LOG_ERR, __FILE__, __LINE__,"fcntl() error : %s", strerror(errno));
exit(1);
}
val &= ~flags; /* turn flags off */
if (fcntl(fd, F_SETFL, val) == -1)
{
syslog(LOG_ERR, __FILE__, __LINE__,"fcntl() error : %s", strerror(errno));
exit(1);
}
return;
}
#endif