forked from YJesus/Unhide
-
Notifications
You must be signed in to change notification settings - Fork 0
/
unhide-output.c
208 lines (170 loc) · 4.63 KB
/
unhide-output.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
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
/*
http://www.unhide-forensics.info
*/
/*
Copyright © 2010-2021 Yago Jesus & Patrick Gouin
This program is free software: you can redistribute it and/or modify
it under the terms of the GNU General Public License as published by
the Free Software Foundation, either version 3 of the License, or
(at your option) any later version.
This program is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
GNU General Public License for more details.
You should have received a copy of the GNU General Public License
along with this program. If not, see <http://www.gnu.org/licenses/>.
*/
#include <stdio.h>
#include <stdlib.h>
#include <stdarg.h>
#include <errno.h>
#ifdef __linux__
#include <linux/limits.h>
#else
#include <limits.h>
#endif
#include <string.h>
#include <time.h>
#include "unhide-output.h"
/*
* Print a message to a file stream (and log the message if necessary).
*/
void vfmsg(FILE * unlog, FILE* fp, const char* fmt, va_list ap)
{
char buf[BUFSIZ];
vsnprintf(buf, BUFSIZ, fmt, ap);
fputs(buf, fp);
fflush(fp) ;
// fputs(buf, stderr);
if (NULL != unlog)
fputs(buf, unlog);
}
/*
* Print a message to a stdout (and log the message if necessary), appending \n.
*/
void msgln(FILE * unlog, int indent, const char* fmt, ...)
{
char buf[BUFSIZ];
va_list ap;
if(1 == indent)
{
strncpy(buf, "\t", BUFSIZ-1);
strncat(buf, fmt, BUFSIZ-strlen(buf)-1);
}
else
{
strncpy(buf, fmt, BUFSIZ-1);
}
buf[BUFSIZ-1] = 0 ;
strncat(buf, "\n", BUFSIZ-1-strlen(buf));
va_start(ap, fmt);
vfmsg(unlog, stdout, buf, ap);
va_end(ap);
}
/*
* Print a warning message to a stderr (and log the message if necessary),
* appending \n, only if in verbose mode.
*
* If errno is not 0, then information about the last error is printed too.
*/
void warnln(int verbose, FILE * unlog, const char* fmt, ...)
{
char buf[BUFSIZ];
va_list ap;
int e = errno; /* save it in case some other function fails */
if (!verbose)
{
return;
}
strncpy(buf, "Warning : ", BUFSIZ);
strncat(buf, fmt, BUFSIZ-1-strlen(buf));
if (e != 0)
{
strncat(buf, " [", BUFSIZ-1-strlen(buf));
strncat(buf, strerror(e), BUFSIZ-1-strlen(buf));
strncat(buf, "]", BUFSIZ-1-strlen(buf));
}
strncat(buf, "\n", BUFSIZ-1-strlen(buf));
va_start(ap, fmt);
vfmsg(unlog, stderr, buf, ap);
va_end(ap);
}
/*
* Print an error to stderr and exit with code 1.
*
* If errno is not 0, then information about the last error is printed too.
*/
void die(FILE * unlog, const char* fmt, ...)
{
va_list ap;
char buf[BUFSIZ];
int e = errno; /* save it in case some other function fails */
strncpy(buf, "Error : ", BUFSIZ);
strncat(buf, fmt, BUFSIZ-1-strlen(buf));
if (e != 0)
{
strncat(buf, " [", BUFSIZ-1-strlen(buf));
strncat(buf, strerror(e), BUFSIZ-1-strlen(buf));
strncat(buf, "]", BUFSIZ-1-strlen(buf));
}
strncat(buf, "\n", BUFSIZ-1-strlen(buf));
va_start(ap, fmt);
vfmsg(unlog, stderr, buf, ap);
va_end(ap);
exit(1);
}
/*
* Initialize and write a header to the log file.
*/
FILE *init_log(int logtofile, const char *header, const char *basename, int hfriend)
{
FILE *fh ;
char filename[PATH_MAX] ;
time_t scantime;
struct tm *tmPtr;
char cad[80];
if (0 == logtofile)
{
return(NULL);
}
scantime = time(NULL);
tmPtr = localtime(&scantime);
sprintf(filename, "%s_%4d-%02d-%02d_%02dh%02dm%02ds.%s", basename, tmPtr->tm_year+1900, tmPtr->tm_mon + 1, tmPtr->tm_mday, tmPtr->tm_hour, tmPtr->tm_min, tmPtr->tm_sec, "log" );
fh = fopen(filename, "w");
if (NULL == fh)
{
logtofile = 0; // inhibit write to log file
warnln(1, NULL, "Unable to open log file (%s)!", filename) ;
return(NULL) ;
}
fputs(header, fh);
strftime( cad, 80, "%H:%M:%S, %F", tmPtr );
fprintf(fh, "\n%s scan starting at: %s\n", basename, cad) ;
if (hfriend != 0)
{
printf("\n%s scan starting at: %s\n", basename, cad) ;
}
fflush(stdout) ;
return(fh);
}
/* Write a footer and close the log file. */
void close_log(FILE *fh, const char *basename, int hfriend)
{
if (NULL == fh)
{
return ;
}
time_t scantime;
char cad[80];
struct tm *tmPtr;
scantime = time(NULL);
tmPtr = localtime(&scantime);
strftime( cad, 80, "%H:%M:%S, %F", tmPtr );
fprintf(fh, "%s scan ending at: %s\n", basename, cad );
if (hfriend != 0)
{
printf("%s scan ending at: %s\n", basename, cad );
}
fflush(stdout) ;
fclose(fh);
}