-
Notifications
You must be signed in to change notification settings - Fork 9
/
Copy pathlogtofile_connect.c
78 lines (73 loc) · 2.09 KB
/
logtofile_connect.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
/*-------------------------------------------------------------------------
*
* logtofile_connect.c
* Functions to parse connect and disconnect messages
*
* Copyright (c) 2020-2024, Francisco Miguel Biete Banon
*
* This code is released under the PostgreSQL licence, as given at
* http://www.postgresql.org/about/licence/
*-------------------------------------------------------------------------
*/
#include "logtofile_connect.h"
/**
* @brief From a list of messages, optionally translated, get the unique prefixes
* @param messages: list of messages
* @param num_messages: number of messages
* @param num_unique: number of unique prefixes
* @return char **: list of unique prefixes
*/
char **
PgAuditLogToFile_connect_UniquePrefixes(const char **messages, const size_t num_messages, size_t *num_unique)
{
bool is_unique;
char **prefixes = NULL;
char *prefix, *dup;
#ifdef ENABLE_NLS
char *message;
#else
const char *message;
#endif
size_t i, j;
*num_unique = 0;
prefixes = palloc(num_messages * sizeof(char *));
if (prefixes != NULL) {
for (i = 0; i < num_messages; i++) {
#ifdef ENABLE_NLS
// Get translation - static copy
message = gettext(messages[i]);
#else
// Pointer to original = static copy
message = messages[i];
#endif
// Get a copy that we can modify
dup = pstrdup(message);
if (dup != NULL) {
prefix = strtok(dup, "%");
if (prefix != NULL) {
// Search duplicated
is_unique = true;
for (j = 0; j < i; j++) {
if (prefixes[j] != NULL) {
if (strcmp(prefixes[j], prefix) == 0) {
// Skip - prefix already present
is_unique = false;
}
}
}
if (is_unique) {
prefixes[i] = palloc((strlen(prefix) + 1) * sizeof(char));
if (prefixes[i] != NULL) {
strcpy(prefixes[i], prefix);
*num_unique += 1;
}
} else {
prefixes[i] = NULL;
}
}
pfree(dup);
}
}
}
return prefixes;
}