Refactor Read_Motd() into Read_TextFile()

Now this function allows to read arbitrary text files into arrays.
This commit is contained in:
Alexander Barton 2012-12-31 19:26:31 +01:00
parent 5d92198487
commit 9e1c25a889
1 changed files with 24 additions and 17 deletions

View File

@ -784,39 +784,44 @@ no_listenports(void)
} }
/** /**
* Read MOTD ("message of the day") file. * Read contents of a text file into an array.
*
* This function is used to read the MOTD and help text file, for exampe.
* *
* @param filename Name of the file to read. * @param filename Name of the file to read.
* @return true, when the file has been read in.
*/ */
static void static bool
Read_Motd(const char *filename) Read_TextFile(const char *Filename, const char *Name, array *Destination)
{ {
char line[127]; char line[127];
FILE *fp; FILE *fp;
int line_no = 1;
if (*filename == '\0') if (*Filename == '\0')
return; return false;
fp = fopen(filename, "r"); fp = fopen(Filename, "r");
if (!fp) { if (!fp) {
Config_Error(LOG_WARNING, "Can't read MOTD file \"%s\": %s", Config_Error(LOG_WARNING, "Can't read %s file \"%s\": %s",
filename, strerror(errno)); Name, Filename, strerror(errno));
return; return false;
} }
array_free(&Conf_Motd); array_free(Destination);
Using_MotdFile = true;
while (fgets(line, (int)sizeof line, fp)) { while (fgets(line, (int)sizeof line, fp)) {
ngt_TrimLastChr( line, '\n'); ngt_TrimLastChr(line, '\n');
/* add text including \0 */ /* add text including \0 */
if (!array_catb(&Conf_Motd, line, strlen(line) + 1)) { if (!array_catb(Destination, line, strlen(line) + 1)) {
Log(LOG_WARNING, "Cannot add MOTD text: %s", strerror(errno)); Log(LOG_WARNING, "Cannot read/add \"%s\", line %d: %s",
Filename, line_no, strerror(errno));
break; break;
} }
line_no++;
} }
fclose(fp); fclose(fp);
return true;
} }
/** /**
@ -1037,8 +1042,10 @@ Read_Config(bool TestOnly, bool IsStarting)
} }
/* No MOTD phrase configured? (re)try motd file. */ /* No MOTD phrase configured? (re)try motd file. */
if (array_bytes(&Conf_Motd) == 0) if (array_bytes(&Conf_Motd) == 0) {
Read_Motd(Conf_MotdFile); if (Read_TextFile(Conf_MotdFile, "MOTD", &Conf_Motd))
Using_MotdFile = true;
}
#ifdef SSL_SUPPORT #ifdef SSL_SUPPORT
/* Make sure that all SSL-related files are readable */ /* Make sure that all SSL-related files are readable */