-
Notifications
You must be signed in to change notification settings - Fork 1
/
utils.c
151 lines (134 loc) · 2.98 KB
/
utils.c
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
// utilities that may be needed in more than one place
#include <apr_strings.h>
#include "utils.h"
#include <stdarg.h>
#include <stdio.h>
#include <stdlib.h>
#include <apr_errno.h>
#include <string.h>
void lfd_log(enum err_levels lvl, char * fmt, ...)
{
va_list ap;
va_start(ap, fmt);
//for now just ignore the error level
vprintf(fmt, ap);
printf("\n");
va_end(ap);
}
static const char * err_source_str(enum err_source_t errsrc)
{
switch (errsrc)
{
case ERR_SOURCE_APR:
return "APR";
case ERR_SOURCE_LINUX:
return "LINUX";
default:
return "<unknown-err-src>";
}
}
static const char * err_source_strerror(enum err_source_t errsrc, int rc, char * error_buff, size_t size)
{
char * error_msg = NULL;
switch(errsrc)
{
case ERR_SOURCE_APR:
error_msg = apr_strerror(rc, error_buff, size);
break;
case ERR_SOURCE_LINUX:
// the kernel internally uses "-errno" vals; we must negate it once more
error_msg = strerror_r(-rc, error_buff, size);
/*
if (ret != 0)
{
printf("{{nested error while printing errmsg: strerror_r returned %d %s for for errnum = %d}}\n", ret, strerror(ret), -rc);
}
error_msg = error_buff;
*/
break;
default:
error_msg = "<cannot-determine-err-msg>";
break;
}
return error_msg;
}
void lfd_log_err_impl(enum err_source_t errsrc, const char * file, int line, const char * func, int rc, char * fmt, ...)
{
char error_buff[STR_ERROR_MAX_LEN];
va_list ap;
memset(error_buff, 0, sizeof(error_buff));
printf("[ERROR][%s] %s:%d %s() -- ", err_source_str(errsrc), file, line, func);
va_start(ap, fmt);
vprintf(fmt, ap);
va_end(ap);
printf("\n");
printf("\t rc = %d, strerr = [%s]\n", rc, err_source_strerror(errsrc, rc, error_buff, STR_ERROR_MAX_LEN));
}
void bug(const char* p_text)
{
printf("%s\n", p_text);
exit(1);
}
apr_size_t lfd_ascii_bin_to_ascii(const char* p_in, char* p_out, apr_size_t in_len)
{
/* Task: translate all \n into \r\n. Note that \r\n becomes \r\r\n. That's
* what wu-ftpd does, and it's easier :-)
*/
apr_size_t indexx = 0;
apr_size_t written = 0;
while (indexx < in_len)
{
char the_char = p_in[indexx];
if (the_char == '\n')
{
*p_out++ = '\r';
written++;
}
*p_out++ = the_char;
written++;
indexx++;
}
return written;
}
/**
* @brief translate all \r\n into plain \n. A plain \r not followed by \n must not be removed.
*/
struct ascii_to_bin_ret lfd_ascii_ascii_to_bin(char* p_buf, apr_size_t in_len, int prev_cr)
{
struct ascii_to_bin_ret rc;
apr_size_t indexx = 0;
apr_size_t written = 0;
char* p_out = p_buf + 1;
rc.last_was_cr = 0;
if (prev_cr && (!in_len || p_out[0] != '\n'))
{
p_buf[0] = '\r';
rc.p_buf = p_buf;
written++;
}
else
{
rc.p_buf = p_out;
}
while (indexx < in_len)
{
char the_char = p_buf[indexx + 1];
if (the_char != '\r')
{
*p_out++ = the_char;
written++;
}
else if (indexx == in_len - 1)
{
rc.last_was_cr = 1;
}
else if (p_buf[indexx + 2] != '\n')
{
*p_out++ = the_char;
written++;
}
indexx++;
}
rc.stored = written;
return rc;
}