-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathmisc.c
More file actions
154 lines (125 loc) · 2.78 KB
/
misc.c
File metadata and controls
154 lines (125 loc) · 2.78 KB
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
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
#include "includes.h"
#include "defines.h"
#include "msgiddbd.h"
#include "misc.h"
extern struct ShareData *g;
void *safe_realloc(void *o, size_t size)
{
void *m = realloc(o, size);
if (!m)
{
perror("realloc");
exit(255);
}
return m;
}
void *safe_calloc(size_t size)
{
void *m = calloc(size, 1);
if (!m)
{
perror("calloc");
exit(255);
}
return m;
}
void *safe_malloc(size_t size)
{
void *m = malloc(size);
if (!m)
{
perror("malloc");
exit(255);
}
return m;
}
/* write the formatted string fmt and it's arguments into the socket d */
ssize_t sockwrite(fd_t d, const char *fmt, ...)
{
int32_t n;
char buf[SOCK_BUFSIZE + 3];
va_list ap;
va_start(ap, fmt);
n = vsnprintf(buf, SOCK_BUFSIZE + 3, fmt, ap);
va_end(ap);
#if DEBUG_LOG_TRAFFIC
logmsg(LOG_DEBUG, "fd=%d >> %.*s", d, n, buf);
#endif
buf[n++] = '\n';
buf[n] = '\0';
return send(d, buf, n, 0);
}
time_t get_time_usec(void) /* {{{ */
{
struct timeval tv;
gettimeofday(&tv, NULL);
return tv.tv_sec * 1000000 + tv.tv_usec;
} /* }}} */
/* basename_r: a thread-safe non-allocating version of basename {{{ */
char *basename_r(const char *path, char *bname)
{
const char *endp, *startp;
/* Empty or NULL string gets treated as "." */
if (path == NULL || *path == '\0') {
(void)strcpy(bname, ".");
return(bname);
}
/* Strip trailing slashes */
endp = path + strlen(path) - 1;
while (endp > path && *endp == '/')
endp--;
/* All slashes becomes "/" */
if (endp == path && *endp == '/') {
(void)strcpy(bname, "/");
return(bname);
}
/* Find the start of the base */
startp = endp;
while (startp > path && *(startp - 1) != '/')
startp--;
if (endp - startp + 2 > MAXPATHLEN) {
errno = ENAMETOOLONG;
return(NULL);
}
(void)strncpy(bname, startp, endp - startp + 1);
bname[endp - startp + 1] = '\0';
return(bname);
} /* }}} */
#if 0
int make_full_path(char *buf, char *fmt, ...) /* {{{ */
{
va_list args;
uint32_t buf_left = PATH_MAX;
char internalbuf[PATH_MAX], *p = internalbuf;
va_start(args, fmt);
strcpy(p, g->cfg.local_dbroot);
p += g->cfg.local_dbroot_len;
buf_left -= g->cfg.local_dbroot_len;
/* cope with dbroot having no trailing slash */
*p++ = '/';
buf_left--;
p += vsnprintf(p, buf_left, fmt, args);
buf_left -= (p - buf);
realpath(internalbuf, buf);
va_end(args);
return 0;
} /* }}} */
int make_full_logpath(char *buf, char *fmt, ...) /* {{{ */
{
va_list args;
uint32_t buf_left = PATH_MAX;
char internalbuf[PATH_MAX], *p = internalbuf;
va_start(args, fmt);
strcpy(p, g->cfg.local_logdir);
p += g->cfg.local_logdir_len;
buf_left -= g->cfg.local_logdir_len;
/* cope with logdir having no trailing slash */
*p++ = '/';
buf_left--;
p += vsnprintf(p, buf_left, fmt, args);
buf_left -= (p - buf);
realpath(internalbuf, buf);
va_end(args);
return 0;
} /* }}} */
#endif