<commit_before>/*This code is part of the Epoch Init System.
* The Epoch Init System is maintained by Subsentient.
* This software is public domain.
* Please read the file UNLICENSE.TXT for more information.*/

/**This file contains functions and utilities used across Epoch
 * for miscellanious purposes, that don't really belong in a category of their own.**/

#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <ctype.h>
#include <time.h>
#include <unistd.h>
#include <dirent.h>
#include <signal.h>
#include "epoch.h"

/**Constants**/
Bool EnableLogging = false;
Bool LogInMemory = true; /*This is necessary so long as we have a readonly filesystem.*/
Bool BlankLogOnBoot = false;
char *MemLogBuffer = NULL;

/*Days in the month, for time stuff.*/
static const short MDays[] = { 31, 28, 31, 30, 31, 30, 31, 31, 30, 31, 30, 31 };

Bool AllNumeric(const char *InStream)
{ /*Is the string all numbers?*/
	if (!*InStream)
	{ /*No data? Don't say it's all numeric then.*/
		return false;
	}
	
	for (; *InStream != '\0'; ++InStream)
	{
		if (!isdigit(*InStream))
		{
			return false;
		}
	}
	
	return true;
}

rStatus WriteLogLine(const char *InStream, Bool AddDate)
{ /*This is pretty much the entire logging system.*/
	FILE *Descriptor = NULL;
	char Hr[16], Min[16], Sec[16], Month[16], Day[16], Year[16], OBuf[MAX_LINE_SIZE + 64] = { '\0' };
	static Bool FailedBefore = false;
	
	if (!EnableLogging)
	{
		return SUCCESS;
	}
	
	if (!LogInMemory && !(Descriptor = fopen(LOGDIR LOGFILE_NAME, "a")))
	{
		if (!FailedBefore)
		{
			FailedBefore = true;
			SpitWarning("Cannot write to log file. Log system is inoperative. Check permissions?");
		}
		
		return FAILURE;
	}
	
	GetCurrentTime(Hr, Min, Sec, Month, Day, Year);
	
	if (AddDate)
	{
		snprintf(OBuf, MAX_LINE_SIZE + 64, "[%s:%s:%s | %s/%s/%s] %s\n", Hr, Min, Sec, Month, Day, Year, InStream);
	}
	else
	{
		snprintf(OBuf, MAX_LINE_SIZE, "%s\n", InStream);
	}
	
	if (LogInMemory)
	{
		if (MemLogBuffer == NULL)
		{
			MemLogBuffer = malloc(1);
			*MemLogBuffer = '\0';
		}
		
		MemLogBuffer = realloc(MemLogBuffer, strlen(MemLogBuffer) + strlen(OBuf) + 1);
		
		strncat(MemLogBuffer, OBuf, strlen(OBuf));
	}
	else
	{
		fwrite(OBuf, 1, strlen(OBuf), Descriptor);
		
		fflush(Descriptor);
		fclose(Descriptor);
	}
	
	return SUCCESS;
}
	

Bool ObjectProcessRunning(const ObjTable *InObj)
{ /*This is so much better than the convoluted /proc method we used before,
	* but I was scared of using kill() for this purpose. I thought that
	* some processes would notice it and whine. Lucky me that it turns out
	* signal 0 is not real.*/
	pid_t InPID;
	

	if (InObj->Opts.StopMode != STOP_PIDFILE || !(InPID = ReadPIDFile(InObj)))
	{ /*We got a PID file requested and present? Get PID from that, otherwise 
		* get the PID from memory.*/
		InPID = InObj->ObjectPID;
	}
	<commit_msg>Don't report unstarted objects as running.<commit_after>/*This code is part of the Epoch Init System.
* The Epoch Init System is maintained by Subsentient.
* This software is public domain.
* Please read the file UNLICENSE.TXT for more information.*/

/**This file contains functions and utilities used across Epoch
 * for miscellanious purposes, that don't really belong in a category of their own.**/

#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <ctype.h>
#include <time.h>
#include <unistd.h>
#include <dirent.h>
#include <signal.h>
#include "epoch.h"

/**Constants**/
Bool EnableLogging = false;
Bool LogInMemory = true; /*This is necessary so long as we have a readonly filesystem.*/
Bool BlankLogOnBoot = false;
char *MemLogBuffer = NULL;

/*Days in the month, for time stuff.*/
static const short MDays[] = { 31, 28, 31, 30, 31, 30, 31, 31, 30, 31, 30, 31 };

Bool AllNumeric(const char *InStream)
{ /*Is the string all numbers?*/
	if (!*InStream)
	{ /*No data? Don't say it's all numeric then.*/
		return false;
	}
	
	for (; *InStream != '\0'; ++InStream)
	{
		if (!isdigit(*InStream))
		{
			return false;
		}
	}
	
	return true;
}

rStatus WriteLogLine(const char *InStream, Bool AddDate)
{ /*This is pretty much the entire logging system.*/
	FILE *Descriptor = NULL;
	char Hr[16], Min[16], Sec[16], Month[16], Day[16], Year[16], OBuf[MAX_LINE_SIZE + 64] = { '\0' };
	static Bool FailedBefore = false;
	
	if (!EnableLogging)
	{
		return SUCCESS;
	}
	
	if (!LogInMemory && !(Descriptor = fopen(LOGDIR LOGFILE_NAME, "a")))
	{
		if (!FailedBefore)
		{
			FailedBefore = true;
			SpitWarning("Cannot write to log file. Log system is inoperative. Check permissions?");
		}
		
		return FAILURE;
	}
	
	GetCurrentTime(Hr, Min, Sec, Month, Day, Year);
	
	if (AddDate)
	{
		snprintf(OBuf, MAX_LINE_SIZE + 64, "[%s:%s:%s | %s/%s/%s] %s\n", Hr, Min, Sec, Month, Day, Year, InStream);
	}
	else
	{
		snprintf(OBuf, MAX_LINE_SIZE, "%s\n", InStream);
	}
	
	if (LogInMemory)
	{
		if (MemLogBuffer == NULL)
		{
			MemLogBuffer = malloc(1);
			*MemLogBuffer = '\0';
		}
		
		MemLogBuffer = realloc(MemLogBuffer, strlen(MemLogBuffer) + strlen(OBuf) + 1);
		
		strncat(MemLogBuffer, OBuf, strlen(OBuf));
	}
	else
	{
		fwrite(OBuf, 1, strlen(OBuf), Descriptor);
		
		fflush(Descriptor);
		fclose(Descriptor);
	}
	
	return SUCCESS;
}
	

Bool ObjectProcessRunning(const ObjTable *InObj)
{ /*This is so much better than the convoluted /proc method we used before,
	* but I was scared of using kill() for this purpose. I thought that
	* some processes would notice it and whine. Lucky me that it turns out
	* signal 0 is not real.*/
	pid_t InPID = 0;
	

	if (InObj->Opts.StopMode != STOP_PIDFILE || !(InPID = ReadPIDFile(InObj)))
	{ /*We got a PID file requested and present? Get PID from that, otherwise 
		* get the PID from memory.*/
		InPID = InObj->ObjectPID;
	}
	
	if (InPID == 0) /*This means the object has no PID.*/
	{
		return false;
	}
	
{'fraction_non_alphanumeric': 0.1378061400482925, 'fraction_numerical': 0.016557433597792343, 'mean_word_length': 3.75327868852459, 'pattern_counts': {'":': 0, '<': 19, '<?xml version=': 0, '>': 23, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 6, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '1824034', 'n_tokens_mistral': 2289, 'n_tokens_neox': 2039, 'n_words': 684}