forked from google/xsecurelock
-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathbanner.c
53 lines (44 loc) · 1.25 KB
/
banner.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
#define _GNU_SOURCE
#include <stdio.h>
#include <stdlib.h>
#include "env_settings.h" // for GetStringSetting
#include "logging.h" // for Log, LogErrno
#include "banner.h"
/*! \brief Read banner message from file
*
* \param banner Array of pointers to store the banner lines
* \param num_lines The number of lines read from the file
* \param max_lines Maximum number of lines to read
* \return 0 if sucsessful, 1 if unable to open file, 2 if error reading
*/
int ReadBannerFile(char **banner, int *num_lines, int max_lines) {
int rtn = 0;
const char *filename =
GetStringSetting("XSECURELOCK_BANNER_FILE", BANNER_FILENAME);
FILE *fp;
fp = fopen(filename, "r");
if (fp == NULL) {
*num_lines = 0;
Log("Failed to open banner file %s", filename);
return 1;
}
char **line = banner;
size_t line_len;
*num_lines = 0;
ssize_t read;
for (int i=0;i < max_lines; i++) {
banner[i] = NULL;
}
while ((read = getline(line, &line_len, fp)) != -1) {
// Remove last newline
(*line)[read - 1] = 0;
(*num_lines)++;
if (*num_lines >= max_lines) {
rtn = 2;
break;
}
line++;
}
fclose(fp);
return rtn;
}