Загрузить файлы в «/»

This commit is contained in:
2025-04-23 16:48:58 +00:00
parent bf6134fa93
commit 8df0dfd308
5 changed files with 300 additions and 0 deletions

26
module_01.c Normal file
View File

@@ -0,0 +1,26 @@
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <errno.h>
#include <sys/stat.h>
#define BOLD_GRAY "\033[1;90m"
#define RED "\033[0;31m"
#define WHITE "\033[0;37m"
#define RESET "\033[0m"
int module_01_run(const char *filename) {
char log_msg[1024];
struct stat st;
if (stat(filename, &st) != 0) {
snprintf(log_msg, sizeof(log_msg), BOLD_GRAY " Main: " RED "Cannot stat file: %s" RESET, strerror(errno));
printf("%s\n", log_msg);
return 1;
}
int is_regular = S_ISREG(st.st_mode);
snprintf(log_msg, sizeof(log_msg), BOLD_GRAY " Main: " WHITE "File is %s" RESET, is_regular ? "regular file" : "not a regular file");
printf("%s\n", log_msg);
return 0;
}

86
module_02.c Normal file
View File

@@ -0,0 +1,86 @@
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <errno.h>
#define BOLD_GRAY "\033[1;90m"
#define RED "\033[0;31m"
#define WHITE "\033[0;37m"
#define RESET "\033[0m"
// Размер буфера для чтения
#define BUFFER_SIZE 4096
int module_02_run(const char *filename) {
char log_msg[1024];
FILE *file = fopen(filename, "rb");
if (!file) {
snprintf(log_msg, sizeof(log_msg), BOLD_GRAY " Library text_analyzer: " RED "File type not recognized: %s" RESET, strerror(errno));
printf("%s\n", log_msg);
return 1;
}
unsigned char *buffer = malloc(BUFFER_SIZE);
if (!buffer) {
snprintf(log_msg, sizeof(log_msg), BOLD_GRAY " Library text_analyzer: " RED "Memory allocation failed" RESET);
printf("%s\n", log_msg);
fclose(file);
return 1;
}
size_t read_bytes = fread(buffer, 1, BUFFER_SIZE, file);
fclose(file);
if (read_bytes == 0) {
snprintf(log_msg, sizeof(log_msg), BOLD_GRAY " Library text_analyzer: " RED "File type not recognized: Empty file" RESET);
printf("%s\n", log_msg);
free(buffer);
return 1;
}
// Проверяем наличие ANSI-кодов
int has_ansi = 0;
for (size_t i = 0; i < read_bytes - 1; i++) {
if (buffer[i] == '\x1B' && buffer[i + 1] == '[') {
has_ansi = 1;
break;
}
}
// Проверяем JSON
int is_json = 0;
for (size_t i = 0; i < read_bytes; i++) {
if (buffer[i] == ' ' || buffer[i] == '\t' || buffer[i] == '\n' || buffer[i] == '\r') continue;
if (buffer[i] == '{' || buffer[i] == '[') {
is_json = 1;
}
break;
}
// Проверяем CSV
int is_csv = 0;
int comma_count = 0;
for (size_t i = 0; i < read_bytes; i++) {
if (buffer[i] == ',' || buffer[i] == ';') {
comma_count++;
if (comma_count >= 3) {
is_csv = 1;
break;
}
}
}
if (has_ansi) {
snprintf(log_msg, sizeof(log_msg), BOLD_GRAY " Library text_analyzer: " WHITE "Text file with ANSI escape sequences (likely terminal log)" RESET);
} else if (is_json) {
snprintf(log_msg, sizeof(log_msg), BOLD_GRAY " Library text_analyzer: " WHITE "Text file, likely JSON format" RESET);
} else if (is_csv) {
snprintf(log_msg, sizeof(log_msg), BOLD_GRAY " Library text_analyzer: " WHITE "Text file, likely CSV format" RESET);
} else {
snprintf(log_msg, sizeof(log_msg), BOLD_GRAY " Library text_analyzer: " WHITE "Text file, unknown structure (possibly plain text or log)" RESET);
}
printf("%s\n", log_msg);
free(buffer);
return 0;
}

69
module_03.c Normal file
View File

@@ -0,0 +1,69 @@
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <errno.h>
#include <sys/stat.h>
#include <sys/sysmacros.h>
#include <unistd.h>
//#include "log.h"
#define BOLD_GRAY "\033[1;90m"
#define RED "\033[0;31m"
#define WHITE "\033[0;37m"
#define RESET "\033[0m"
int module_03_run(const char *filename) {
char log_msg[1024];
struct stat st;
if (stat(filename, &st) != 0) {
snprintf(log_msg, sizeof(log_msg), BOLD_GRAY " Library libstat: " RED "File type not recognized: %s" RESET, strerror(errno));
printf("%s\n", log_msg);
// log_message(log_msg);
return 1;
}
const char *type;
char extra_info[512] = "";
if (S_ISREG(st.st_mode)) {
type = "regular file";
} else if (S_ISDIR(st.st_mode)) {
type = "directory";
} else if (S_ISCHR(st.st_mode)) {
type = "character device";
snprintf(extra_info, sizeof(extra_info), ", Major: %d, Minor: %d",
major(st.st_rdev), minor(st.st_rdev));
} else if (S_ISBLK(st.st_mode)) {
type = "block device";
snprintf(extra_info, sizeof(extra_info), ", Major: %d, Minor: %d",
major(st.st_rdev), minor(st.st_rdev));
} else if (S_ISFIFO(st.st_mode)) {
type = "FIFO (named pipe)";
} else if (S_ISSOCK(st.st_mode)) {
type = "socket";
} else if (S_ISLNK(st.st_mode)) {
type = "symbolic link";
char link_target[256];
ssize_t len = readlink(filename, link_target, sizeof(link_target) - 1);
if (len != -1) {
link_target[len] = '\0';
snprintf(extra_info, sizeof(extra_info), ", Points to: %s", link_target);
} else {
snprintf(extra_info, sizeof(extra_info), ", Error reading link: %s", strerror(errno));
}
} else {
type = "unknown";
}
if (strncmp(filename, "/proc/", 6) == 0) {
type = "procfs pseudo-file";
} else if (strncmp(filename, "/sys/", 5) == 0) {
type = "sysfs pseudo-file";
}
snprintf(log_msg, sizeof(log_msg), BOLD_GRAY " Library libstat: " WHITE "Size: %ld bytes, Permissions: %o, Type: %s%s" RESET,
(long)st.st_size, st.st_mode & 0777, type, extra_info);
printf("%s\n", log_msg);
// log_message(log_msg);
return 0;
}

44
module_04.c Normal file
View File

@@ -0,0 +1,44 @@
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <errno.h>
#include <magic.h>
#define BOLD_GRAY "\033[1;90m"
#define RED "\033[0;31m"
#define WHITE "\033[0;37m"
#define RESET "\033[0m"
int module_04_run(const char *filename) {
char log_msg[1024];
magic_t magic = magic_open(MAGIC_MIME_TYPE | MAGIC_ERROR | MAGIC_NO_CHECK_CDF);
if (!magic) {
snprintf(log_msg, sizeof(log_msg), BOLD_GRAY " Library libmagic: " RED "File type not recognized: Error initializing libmagic" RESET);
printf("%s\n", log_msg);
return 1;
}
if (magic_load(magic, NULL) != 0) {
snprintf(log_msg, sizeof(log_msg), BOLD_GRAY " Library libmagic: " RED "File type not recognized: Error loading magic database: %s" RESET,
magic_error(magic));
printf("%s\n", log_msg);
magic_close(magic);
return 1;
}
const char *mime = magic_file(magic, filename);
if (!mime || strlen(mime) == 0 || strstr(mime, "\xFF") || strstr(mime, "\x00")) {
mime = "text/plain";
snprintf(log_msg, sizeof(log_msg), BOLD_GRAY " Library libmagic: " RED "Invalid MIME, assuming text/plain" RESET);
printf("%s\n", log_msg);
}
magic_setflags(magic, 0);
const char *desc = magic_file(magic, filename);
snprintf(log_msg, sizeof(log_msg), BOLD_GRAY " Library libmagic: " WHITE "MIME: %s, Description: %s" RESET,
mime, desc ? desc : "Unknown");
printf("%s\n", log_msg);
magic_close(magic);
return 0;
}

75
module_05.c Normal file
View File

@@ -0,0 +1,75 @@
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <errno.h>
#include <iconv.h>
#define BOLD_GRAY "\033[1;90m"
#define RED "\033[0;31m"
#define WHITE "\033[0;37m"
#define RESET "\033[0m"
// Размер буфера для чтения
#define BUFFER_SIZE 4096
int module_05_run(const char *filename) {
char log_msg[1024];
FILE *file = fopen(filename, "rb");
if (!file) {
snprintf(log_msg, sizeof(log_msg), BOLD_GRAY " Library iconv: " RED "File type not recognized: %s" RESET, strerror(errno));
printf("%s\n", log_msg);
return 1;
}
unsigned char *buffer = malloc(BUFFER_SIZE);
if (!buffer) {
snprintf(log_msg, sizeof(log_msg), BOLD_GRAY " Library iconv: " RED "Memory allocation failed" RESET);
printf("%s\n", log_msg);
fclose(file);
return 1;
}
size_t read_bytes = fread(buffer, 1, BUFFER_SIZE, file);
fclose(file);
if (read_bytes == 0) {
snprintf(log_msg, sizeof(log_msg), BOLD_GRAY " Library iconv: " RED "File type not recognized: Empty file" RESET);
printf("%s\n", log_msg);
free(buffer);
return 1;
}
iconv_t cd = iconv_open("UTF-8", "UTF-8");
if (cd == (iconv_t)-1) {
snprintf(log_msg, sizeof(log_msg), BOLD_GRAY " Library iconv: " RED "Error initializing iconv" RESET);
printf("%s\n", log_msg);
free(buffer);
return 1;
}
char *inbuf = (char *)buffer;
size_t inbytesleft = read_bytes;
char *outbuf = malloc(BUFFER_SIZE);
if (!outbuf) {
snprintf(log_msg, sizeof(log_msg), BOLD_GRAY " Library iconv: " RED "Memory allocation failed" RESET);
printf("%s\n", log_msg);
iconv_close(cd);
free(buffer);
return 1;
}
char *outptr = outbuf;
size_t outbytesleft = BUFFER_SIZE;
size_t result = iconv(cd, &inbuf, &inbytesleft, &outptr, &outbytesleft);
if (result == (size_t)-1) {
snprintf(log_msg, sizeof(log_msg), BOLD_GRAY " Library iconv: " WHITE "Not UTF-8, possible binary or other encoding" RESET);
} else {
snprintf(log_msg, sizeof(log_msg), BOLD_GRAY " Library iconv: " WHITE "Compatible with UTF-8" RESET);
}
printf("%s\n", log_msg);
iconv_close(cd);
free(outbuf);
free(buffer);
return 0;
}