feat: move source files into retoor_c directory and add build echo messages

- Add echo messages for both build targets to indicate which project is being compiled
- Update isspam.c path to reflect new retoor_c/ subdirectory structure
- Keep binary output location unchanged at project root
This commit is contained in:
2024-11-30 21:05:06 +00:00
parent 17ff82c199
commit e7725601b2
6 changed files with 3 additions and 1 deletions
+390
View File
@@ -0,0 +1,390 @@
#include "rmalloc.h"
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <unistd.h>
#include "rstring_list.h"
#include "rstr.h"
#include <ctype.h>
#define sl rstring_list_t
#define slf rstring_list_free
#define sla rstring_list_add
#define sln rstring_list_new
#define rb rbuffer_t
#define rbf rbuffer_free
#define rbs rbuffer_to_string
#define rbw rbuffer_write
#define rbn rbuffer_new
char *forbidden_words[] = {
"recovery", "techie", "http", "https", "digital", "hack", "::", "//", "com",
"@", "crypto", "bitcoin", "wallet", "hacker", "welcome", "whatsapp", "email", "cryptocurrency",
"stolen", "freeze", "quick", "crucial", "tracing", "scammers", "expers", "hire", "century",
"transaction", "essential", "managing", "contact", "contacting", "understanding", "assets", "funds", NULL};
bool show_capitalized = false;
bool show_sentences = false;
bool show_words = false;
bool show_numbers = false;
bool show_forbidden_words = true;
bool file_exists(char * path){
FILE * f = fopen(path, "r");
bool result = f != NULL;
if(f){
fclose(f);
}
return result;
}
void sld(sl *lst) {
for (ulonglong i = 0; i < lst->count; i++) {
printf("<%llu:%s>\n", i, lst->strings[i]);
}
}
char *remove_preserved_chars(char *content) {
char *cc = (char *)malloc(strlen(content) + 1);
*cc = 0;
char *ccp = cc;
while (*content) {
if (*content == '<' || *content == '>' || *content == ':') {
content++;
continue;
}
*ccp = *content;
ccp++;
*ccp = 0;
content++;
}
return cc;
}
char *slds(sl *lst) {
str_t *buffer = strn(1337);
for (ulonglong i = 0; i < lst->count; i++) {
char *temp = (char *)malloc(strlen(lst->strings[i]) + 20);
char *cc = remove_preserved_chars(lst->strings[i]);
sprintf(temp, "<%llu:%s>\n", i, cc);
free(cc);
stra(buffer, temp);
free(temp);
}
return strc(buffer);
}
bool isws(char c) { return c == '\t' || c == '\n' || c == ' ' || c == ','; }
char *stripws(char *content) {
char *cc = (char *)malloc(strlen(content) + 1);
*cc = 0;
char *ccp = cc;
while (*content) {
if (!isws(*content)) {
*ccp = *content;
ccp++;
*ccp = 0;
}
content++;
}
return cc;
}
char *fread_till_eof(FILE *f) {
char c;
str_t *buffer = strn(1337);
while ((c = fgetc(f)) != EOF) {
strac(buffer, c);
}
char *content = strc(buffer);
return content;
}
rstring_list_t *get_sentences(char *content) {
rstring_list_t *sentences = rstring_list_new();
char *sentence_buffer = (char *)malloc(strlen(content) + 1);
char *sentence_buffer_p = sentence_buffer;
// rbuffer_t * buffer = rbuffer_new(NULL,0);
bool in_line = false;
while (*content) {
if ((*content == ' ' || *content == '\t' || *content == '\n') && !in_line) {
content++;
continue;
} else {
in_line = true;
}
if (*content == '.') {
*sentence_buffer_p = *content;
sentence_buffer_p++;
*sentence_buffer_p = 0;
rstring_list_add(sentences, sentence_buffer);
sentence_buffer_p = sentence_buffer;
*sentence_buffer = 0;
content++;
in_line = false;
continue;
}
*sentence_buffer_p = *content;
sentence_buffer_p++;
*sentence_buffer_p = 0;
content++;
}
free(sentence_buffer);
return sentences;
}
rstring_list_t *get_words(char *content) {
rstring_list_t *words = rstring_list_new();
char *word_buffer = (char *)malloc(strlen(content) + 1);
char *word_buffer_p = word_buffer;
*word_buffer_p = 0;
// rbuffer_t * buffer = rbuffer_new(NULL,0);
while (*content) {
if (*content == ' ' || *content == '\t' || *content == '\n') {
if (word_buffer_p != word_buffer) {
rstring_list_add(words, word_buffer);
word_buffer_p = word_buffer;
*word_buffer = 0;
}
content++;
continue;
}
*word_buffer_p = *content;
word_buffer_p++;
*word_buffer_p = 0;
content++;
}
free(word_buffer);
return words;
}
bool is_fully_capitalized_word(char *word) {
while (*word) {
if (isalnum(*word) && toupper(*word) != *word)
return false;
word++;
}
return true;
}
sl *get_capitalized_words(char *content) {
sl *capitalized_words = sln();
sl *sentences = get_sentences(content);
for (uint j = 0; j < sentences->count; j++) {
char *sentence = sentences->strings[j];
sl *all_words = get_words(sentence);
// Always skip the first word since sentences start with
for (uint i = 0; i < all_words->count; i++) {
if (is_fully_capitalized_word(all_words->strings[i])) {
rstring_list_add(capitalized_words, all_words->strings[i]);
}
}
slf(all_words);
}
slf(sentences);
return capitalized_words;
}
char *clean_content(char *content) {
char *allowed_ichars = "01234567891abcdefghijklmnopqrstuvwxyz \n.,!?";
char *clean_content = (char *)malloc(strlen(content) + 1);
char *clean_content_p = clean_content;
*clean_content_p = 0;
while (*content) {
if (strchr(allowed_ichars, tolower(*content))) {
*clean_content_p = *content;
clean_content_p++;
*clean_content_p = 0;
}
content++;
}
return clean_content;
}
sl *get_numbers(char *content) {
char *cc = clean_content(content);
char *ccc = stripws(cc);
char *cccp = ccc;
free(cc);
char *number_buffer = (char *)malloc(strlen(ccc) + 1);
*number_buffer = 0;
char *number_buffer_p = number_buffer;
sl *numbers = sln();
while (*cccp) {
if (isdigit((*cccp))) {
*number_buffer_p = *cccp;
number_buffer_p++;
*number_buffer_p = 0;
} else if (number_buffer != number_buffer_p) {
sla(numbers, number_buffer);
*number_buffer = 0;
number_buffer_p = number_buffer;
}
cccp++;
}
free(number_buffer);
free(ccc);
return numbers;
}
bool stricmp(char *word1, char *word2) {
while (*word1 && tolower(*word1) == tolower(*word2)) {
word1++;
word2++;
}
return *word1 == *word2;
}
bool containswordi(sl *words, char *word) {
for (uint i = 0; i < words->count; i++) {
if (stricmp(words->strings[i], word))
return true;
}
return false;
}
sl *get_forbidden_words(char *content) {
sl *words = get_words(content);
sl *found = sln();
for (int j = 0; forbidden_words[j] != NULL; j++) {
if (containswordi(words, forbidden_words[j])) {
rstring_list_add(found, forbidden_words[j]);
}
}
slf(words);
return found;
}
unsigned int total = 0;
void analyze(FILE *f) {
total = total + 1;
printf("#%u\n", total);
char *data = fread_till_eof(f);
str_t *all = strn(1337);
char *sbuf = NULL;
char *clean_data = clean_content(data);
free(clean_data);
// All capitalized words
sl *capitalized_words = get_capitalized_words(data);
ulonglong capitalized_words_count = capitalized_words->count;
printf("Capitalized words: %llu\n", capitalized_words_count);
if(show_capitalized)
sld(capitalized_words);
sbuf = slds(capitalized_words);
stra(all, sbuf);
free(sbuf);
sl *sentences = get_sentences(data);
// All sentences
printf("Sentences: %llu\n", sentences->count);
if(show_sentences)
sld(sentences);
sbuf = slds(sentences);
stra(all, sbuf);
free(sbuf);
sl *words = get_words(data);
// All words
printf("Words: %llu\n", words->count);
if(show_words)
sld(words);
sbuf = slds(words);
stra(all, sbuf);
free(sbuf);
// Numbers
sl *numbers = get_numbers(data);
printf("Numbers: %llu\n", numbers->count);
if(show_numbers)
sld(numbers);
sbuf = slds(numbers);
stra(all, sbuf);
free(sbuf);
// Forbidden words
sl *fw = get_forbidden_words(data);
printf("Forbidden words: %llu\n", fw->count);
if(show_forbidden_words)
sld(fw);
sbuf = slds(fw);
stra(all, sbuf);
free(sbuf);
strd(all);
if(words->count){
double capitalized_word_percentage = 100 * ((double)capitalized_words->count / (double)words->count);
printf("Capitalized percentage: %f%%\n",capitalized_word_percentage);
double forbidden_word_percentage = 100 * ((double)fw->count / (double)words->count);
printf("Forbidden percentage: %f%%\n",forbidden_word_percentage);
ulonglong word_count_per_sentence = words->count / (sentences->count ? sentences->count : 1);
printf("Word count per sentence: %llu\n", word_count_per_sentence);
}
slf(capitalized_words);
slf(sentences);
slf(words);
slf(numbers);
slf(fw);
free(data);
}
void analyze_file(char *path) {
FILE *f = fopen(path, "r");
analyze(f);
fclose(f);
}
int main(int argc, char *argv[]) {
if (argc > 1) {
for (int i = 1; i < argc; i++) {
if(!strcmp(argv[1],"--hide-capitalized")){
show_capitalized=false;
}else if(!strcmp(argv[1],"--show-sentences")){
show_sentences=true;
}else if(!strcmp(argv[1],"--show-words")){
show_words=true;
}else if(!strcmp(argv[1],"--show-numbers")){
show_words=true;
}else if(!strcmp(argv[1],"--hide-forbidden-words")){
show_forbidden_words=false;
}else if(!strcmp(argv[1],"help") || !strcmp(argv[1],"--help")){
printf("%s",
"Usage: spam [file] [file] [file]\n"
"Flag defaults:\n"
" hide-capitalized = true\n"
" show-sentences = false\n"
" show-words = false\n"
" show-numbers = false\n"
" hide-forbidden-words = false\n");
return 0;
}
printf("File: %s\n", argv[i]);
analyze_file(argv[i]);
printf("%s\n", rmalloc_stats());
printf("\n");
}
return 0;
}
analyze(stdin);
printf("%s\n", rmalloc_stats());
return 0;
}
+220
View File
@@ -0,0 +1,220 @@
// RETOOR - Nov 28 2024
#ifndef RMALLOC_H
#define RMALLOC_H
#ifndef RMALLOC_OVERRIDE
#define RMALLOC_OVERRIDE 1
#endif
#ifdef _POSIX_C_SOURCE
#define _POSIX_C_SOURCE_TEMP _POSIX_C_SOURCE
#undef _POSIX_C_SOURCE
#endif
#ifndef _POSIX_C_SOURCE
#undef _POSIX_C_SOURCE
#define _POSIX_C_SOURCE 200112L
#endif
#ifndef ulonglong
#define ulonglong unsigned long long
#endif
#include <locale.h>
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#ifndef RTEMP_H
#define RTEMP_H
#ifndef RTYPES_H
#define RTYPES_H
#ifdef _POSIX_C_SOURCE
#define _POSIX_C_SOURCE_TEMP _POSIX_C_SOURCE
#undef _POSIX_C_SOURCE
#endif
#ifndef _POSIX_C_SOURCE
#undef _POSIX_C_SOURCE
#define _POSIX_C_SOURCE 200112L
#endif
#include <stdbool.h>
#include <stdint.h> // uint
#include <string.h>
#include <sys/types.h> // ulong
#ifndef ulonglong
#define ulonglong unsigned long long
#endif
#ifndef uint
typedef unsigned int uint;
#endif
#ifndef byte
typedef unsigned char byte;
#endif
#ifdef _POSIX_C_SOURCE_TEMP
#undef _POSIX_C_SOURCE
#define _POSIX_C_SOURCE _POSIX_C_SOURCE_TEMP
#undef _POSIX_C_SOURCE_TEMP
#else
#undef _POSIX_C_SOURCE
#endif
#endif
#include <pthread.h>
#ifndef RTEMPC_SLOT_COUNT
#define RTEMPC_SLOT_COUNT 20
#endif
#ifndef RTEMPC_SLOT_SIZE
#define RTEMPC_SLOT_SIZE 1024 * 64 * 128
#endif
bool _rtempc_initialized = false;
pthread_mutex_t _rtempc_thread_lock;
bool rtempc_use_mutex = true;
byte _current_rtempc_slot = 1;
char _rtempc_buffer[RTEMPC_SLOT_COUNT][RTEMPC_SLOT_SIZE];
char *rtempc(char *data) {
if (rtempc_use_mutex) {
if (!_rtempc_initialized) {
_rtempc_initialized = true;
pthread_mutex_init(&_rtempc_thread_lock, NULL);
}
pthread_mutex_lock(&_rtempc_thread_lock);
}
uint current_rtempc_slot = _current_rtempc_slot;
_rtempc_buffer[current_rtempc_slot][0] = 0;
strcpy(_rtempc_buffer[current_rtempc_slot], data);
_current_rtempc_slot++;
if (_current_rtempc_slot == RTEMPC_SLOT_COUNT) {
_current_rtempc_slot = 0;
}
if (rtempc_use_mutex)
pthread_mutex_unlock(&_rtempc_thread_lock);
return _rtempc_buffer[current_rtempc_slot];
}
#define sstring(_pname, _psize) \
static char _##_pname[_psize]; \
_##_pname[0] = 0; \
char *_pname = _##_pname;
#define string(_pname, _psize) \
char _##_pname[_psize]; \
_##_pname[0] = 0; \
char *_pname = _##_pname;
#define sreset(_pname, _psize) _pname = _##_pname;
#define sbuf(val) rtempc(val)
#endif
#ifdef _POSIX_C_SOURCE_TEMP
#undef _POSIX_C_SOURCE
#define _POSIX_C_SOURCE _POSIX_C_SOURCE_TEMP
#undef _POSIX_C_SOURCE_TEMP
#else
#undef _POSIX_C_SOURCE
#endif
ulonglong rmalloc_count = 0;
ulonglong rmalloc_alloc_count = 0;
ulonglong rmalloc_free_count = 0;
ulonglong rmalloc_total_bytes_allocated = 0;
void *_rmalloc_prev_realloc_obj = NULL;
size_t _rmalloc_prev_realloc_obj_size = 0;
void *rmalloc(size_t size) {
void *result;
while (!(result = malloc(size))) {
fprintf(stderr, "Warning: malloc failed, trying again.\n");
}
rmalloc_count++;
rmalloc_alloc_count++;
rmalloc_total_bytes_allocated += size;
return result;
}
void *rcalloc(size_t count, size_t size) {
void *result;
while (!(result = calloc(count, size))) {
fprintf(stderr, "Warning: calloc failed, trying again.\n");
}
rmalloc_alloc_count++;
rmalloc_count++;
rmalloc_total_bytes_allocated += count * size;
return result;
}
void *rrealloc(void *obj, size_t size) {
if (!obj) {
rmalloc_count++;
}
rmalloc_alloc_count++;
if (obj == _rmalloc_prev_realloc_obj) {
rmalloc_total_bytes_allocated += size - _rmalloc_prev_realloc_obj_size;
_rmalloc_prev_realloc_obj_size = size - _rmalloc_prev_realloc_obj_size;
} else {
_rmalloc_prev_realloc_obj_size = size;
}
void *result;
while (!(result = realloc(obj, size))) {
fprintf(stderr, "Warning: realloc failed, trying again.\n");
}
_rmalloc_prev_realloc_obj = result;
return result;
}
char *rstrdup(const char *s) {
if (!s)
return NULL;
char *result;
size_t size = strlen(s) + 1;
result = rmalloc(size);
memcpy(result, s, size);
rmalloc_total_bytes_allocated += size;
return result;
}
void *rfree(void *obj) {
rmalloc_count--;
rmalloc_free_count++;
free(obj);
return NULL;
}
#if RMALLOC_OVERRIDE
#define malloc rmalloc
#define calloc rcalloc
#define realloc rrealloc
#define free rfree
#define strdup rstrdup
#endif
char *rmalloc_lld_format(ulonglong num) {
char res[100];
res[0] = 0;
sprintf(res, "%'llu", num);
char *resp = res;
while (*resp) {
if (*resp == ',')
*resp = '.';
resp++;
}
return sbuf(res);
}
char *rmalloc_bytes_format(int factor, ulonglong num) {
char *sizes[] = {"B", "KB", "MB", "GB", "TB", "PB", "EB", "ZB", "YB"};
if (num > 1024) {
return rmalloc_bytes_format(factor + 1, num / 1024);
}
char res[100];
res[0] = 0;
sprintf(res, "%s %s", rmalloc_lld_format(num), sizes[factor]);
return sbuf(res);
}
char *rmalloc_stats() {
static char res[300];
res[0] = 0;
setlocale(LC_NUMERIC, "en_US.UTF-8");
sprintf(res, "Memory usage: %s, %s (re)allocated, %s unqiue free'd, %s in use.", rmalloc_bytes_format(0, rmalloc_total_bytes_allocated),
rmalloc_lld_format(rmalloc_alloc_count), rmalloc_lld_format(rmalloc_free_count), rmalloc_lld_format(rmalloc_count));
setlocale(LC_NUMERIC, "");
return res;
}
#endif
+52
View File
@@ -0,0 +1,52 @@
#ifndef RSTR_H
#define RSTR_H
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
typedef struct str_t {
char *content;
size_t length;
size_t buffer_size;
size_t size;
} str_t;
str_t *strn(size_t buffer_size) {
str_t *result = (str_t *)malloc(sizeof(str_t));
result->length = 0;
result->size = buffer_size;
result->buffer_size = buffer_size;
result->content = (char *)malloc(buffer_size);
result->content[0] = 0;
return result;
}
void stra(str_t *str, const char *to_append) {
size_t required_new_length = str->length + strlen(to_append) + 1;
str->length += strlen(to_append);
if (required_new_length > str->size) {
str->size += required_new_length + str->buffer_size;
str->content = (char *)realloc(str->content, str->size + 1);
} else {
// printf("NO NDEED\n");
}
strcat(str->content, to_append);
str->content[str->length] = 0;
}
void strac(str_t *str, const char c) {
char to_append[] = {c, 0};
stra(str, to_append);
}
void strd(str_t *str) {
if (str->content) {
free(str->content);
}
free(str);
}
char *strc(str_t *str) {
char *content = str->content;
str->content = NULL;
strd(str);
return content;
}
#endif
+46
View File
@@ -0,0 +1,46 @@
#ifndef RSTRING_LIST_H
#define RSTRING_LIST_H
#include <stdbool.h>
#include <stdlib.h>
#include <string.h>
typedef struct rstring_list_t {
unsigned long long size;
unsigned long long count;
char **strings;
} rstring_list_t;
rstring_list_t *rstring_list_new() {
rstring_list_t *rsl = (rstring_list_t *)malloc(sizeof(rstring_list_t));
memset(rsl, 0, sizeof(rstring_list_t));
return rsl;
}
void rstring_list_free(rstring_list_t *rsl) {
for (unsigned long long i = 0; i < rsl->size; i++) {
free(rsl->strings[i]);
}
if (rsl->strings)
free(rsl->strings);
free(rsl);
}
void rstring_list_add(rstring_list_t *rsl, char *str) {
if (rsl->count == rsl->size) {
rsl->size++;
rsl->strings = (char **)realloc(rsl->strings, sizeof(char *) * rsl->size);
}
rsl->strings[rsl->count] = strdup(str);
rsl->count++;
}
bool rstring_list_contains(rstring_list_t *rsl, char *str) {
for (unsigned long long i = 0; i < rsl->count; i++) {
if (!strcmp(rsl->strings[i], str))
return true;
}
return false;
}
#endif
+41
View File
@@ -0,0 +1,41 @@
#
# [USAGE]
#
# This quick & dirty script will summarize the output
# generated by the isspam application.
# To use, you do:
# ./isspam ./your-content/*.txt > output.txt
# Then you execute: python totals.py output.txt
# - retoor
import sys
import pathlib
totals = {}
count = 0
with pathlib.Path(sys.argv[1]).open("r") as f:
data = f.read()
for line in data.split("\n"):
if line.startswith("<"):
continue
parts = line.split(": ")
if(len(parts) < 2):
continue
key = parts[0]
if key == "File":
count += 1
if not key in ["File","Memory usage"]:
if key not in totals:
totals[key] = 0.0
value = float(parts[1].replace("%",""))
totals[key] += value
else:
value = parts[1]
for key, value in totals.items():
print(key.count("percentage"))
if key.count("percentage") > 0:
value = value / count
print(key,":",value)