Discarded some code that does not work well with the needs

This commit is contained in:
2024-02-16 02:22:24 +01:00
parent 96b522d521
commit 9cd9ca03af
9 changed files with 50 additions and 279 deletions

View File

@@ -8,8 +8,11 @@
#include <string.h>
#include <errno.h>
#include <fcntl.h>
#include <dirent.h>
#include <sys/types.h>
#include <sys/wait.h>
#include <sys/stat.h>
#include <ftw.h>
char* format(const char* fmt, ...) {
va_list args;
@@ -26,7 +29,7 @@ char* format(const char* fmt, ...) {
// Allocate a buffer for the formatted string
char* buffer = calloc(length, sizeof(char));
if (buffer == NULL) {
logmsg(LOG_ERROR, "Failed to allocate memory for the formatted string.");
log_message(LOG_ERROR, "Failed to allocate memory for the formatted string.");
return NULL;
}
@@ -40,7 +43,7 @@ char* format(const char* fmt, ...) {
return buffer;
}
void logmsg(LogLevel level, const char* fmt, ...) {
void log_message(LogLevel level, const char* fmt, ...) {
va_list args;
va_start(args, fmt);
@@ -90,7 +93,7 @@ void logmsg(LogLevel level, const char* fmt, ...) {
va_end(args);
}
int exec(char** outb, char** errb, const char* file, ...) {
int execute(char** outb, char** errb, const char* file, ...) {
// Count the number of arguments
int argc = 1; // The first argument is the file name
@@ -105,7 +108,7 @@ int exec(char** outb, char** errb, const char* file, ...) {
// Allocate an array for the arguments
char** argv = calloc(argc + 1, sizeof(char*));
if (argv == NULL) {
logmsg(LOG_ERROR, "Failed to allocate memory for the arguments array.");
log_message(LOG_ERROR, "Failed to allocate memory for the arguments array.");
return -1;
}
@@ -114,7 +117,7 @@ int exec(char** outb, char** errb, const char* file, ...) {
argv[0] = strdup(file);
if (argv[0] == NULL) {
logmsg(LOG_ERROR, "Failed to allocate memory for the file name.");
log_message(LOG_ERROR, "Failed to allocate memory for the file name.");
free(argv);
va_end(args);
@@ -125,7 +128,7 @@ int exec(char** outb, char** errb, const char* file, ...) {
for (int i = 1; i < argc; i++) {
argv[i] = strdup(va_arg(args, const char*));
if (argv[i] == NULL) {
logmsg(LOG_ERROR, "Failed to allocate memory for the argument %d.", i);
log_message(LOG_ERROR, "Failed to allocate memory for the argument %d.", i);
for (int j = 0; j < i; j++)
free(argv[j]);
@@ -144,7 +147,7 @@ int exec(char** outb, char** errb, const char* file, ...) {
// Create a pipe for the standard output
int outp[2];
if (pipe(outp) != 0) {
logmsg(LOG_ERROR, "Failed to create a pipe for the standard output.");
log_message(LOG_ERROR, "Failed to create a pipe for the standard output.");
for (int i = 0; i < argc; i++)
free(argv[i]);
@@ -156,7 +159,7 @@ int exec(char** outb, char** errb, const char* file, ...) {
// Create a pipe for the standard error
int errp[2];
if (pipe(errp) != 0) {
logmsg(LOG_ERROR, "Failed to create a pipe for the standard error.");
log_message(LOG_ERROR, "Failed to create a pipe for the standard error.");
close(outp[0]);
close(outp[1]);
@@ -172,7 +175,7 @@ int exec(char** outb, char** errb, const char* file, ...) {
pid_t pid = fork();
if (pid == -1) {
logmsg(LOG_ERROR, "Failed to fork the process (%s).", strerror(errno));
log_message(LOG_ERROR, "Failed to fork the process (%s).", strerror(errno));
close(outp[0]);
close(outp[1]);
@@ -244,7 +247,7 @@ char* read_file(int fd) {
while ((n = read(fd, buffer, sizeof(buffer))) > 0) {
char* new_data = realloc(data, length + n + 1);
if (new_data == NULL) {
logmsg(LOG_ERROR, "Failed to allocate memory for the file data.");
log_message(LOG_ERROR, "Failed to allocate memory for the file data.");
free(data);
@@ -262,7 +265,7 @@ char* read_file(int fd) {
// Check for read errors
if (n < 0) {
logmsg(LOG_ERROR, "Failed to read the file (%s).", strerror(errno));
log_message(LOG_ERROR, "Failed to read the file (%s).", strerror(errno));
free(data);
@@ -276,14 +279,14 @@ bool copy_file(const char* src, const char* dst, mode_t mode) {
// Open the source file
int src_fd = open(src, O_RDONLY);
if (src_fd == -1) {
logmsg(LOG_ERROR, "Failed to open the source file %s (%s).", src, strerror(errno));
log_message(LOG_ERROR, "Failed to open the source file %s (%s).", src, strerror(errno));
return false;
}
// Open the destination file
int dst_fd = open(dst, O_WRONLY | O_CREAT | O_TRUNC, mode);
if (dst_fd == -1) {
logmsg(LOG_ERROR, "Failed to open the destination file %s (%s).", dst, strerror(errno));
log_message(LOG_ERROR, "Failed to open the destination file %s (%s).", dst, strerror(errno));
close(src_fd);
@@ -296,7 +299,7 @@ bool copy_file(const char* src, const char* dst, mode_t mode) {
while ((n = read(src_fd, buffer, sizeof(buffer))) > 0) {
if (write(dst_fd, buffer, n) != n) {
logmsg(LOG_ERROR, "Failed to write to the destination file %s (%s).", dst, strerror(errno));
log_message(LOG_ERROR, "Failed to write to the destination file %s (%s).", dst, strerror(errno));
close(src_fd);
close(dst_fd);
@@ -307,7 +310,7 @@ bool copy_file(const char* src, const char* dst, mode_t mode) {
// Check for read errors
if (n < 0) {
logmsg(LOG_ERROR, "Failed to read the source file %s (%s).", src, strerror(errno));
log_message(LOG_ERROR, "Failed to read the source file %s (%s).", src, strerror(errno));
close(src_fd);
close(dst_fd);