Updated for new file_handling
This commit is contained in:
27
src/main.c
27
src/main.c
@@ -5,40 +5,35 @@
|
|||||||
#include <string.h>
|
#include <string.h>
|
||||||
#include "users.h"
|
#include "users.h"
|
||||||
#include "processes.h"
|
#include "processes.h"
|
||||||
#include "logging.h"
|
#include "file_handling.h"
|
||||||
#include "error_handling.h"
|
|
||||||
|
|
||||||
void setup_error_redirection(char *error_file, FILE **err_fp) {
|
void setup_error_redirection(char *error_file, FILE **err_fp) {
|
||||||
if (error_file) {
|
if (error_file) {
|
||||||
FILE *temp_err_fp = fopen(error_file, "w");
|
*err_fp = open_file(error_file, "w", "Failed to open error log file");
|
||||||
if (!temp_err_fp) {
|
if (!(*err_fp)) {
|
||||||
perror("Failed to open error log file");
|
|
||||||
exit(1);
|
exit(1);
|
||||||
}
|
}
|
||||||
fflush(stderr); // Ensure all previous stderr output is flushed
|
fflush(stderr);
|
||||||
if (dup2(fileno(temp_err_fp), STDERR_FILENO) == -1) {
|
if (dup2(fileno(*err_fp), STDERR_FILENO) == -1) {
|
||||||
perror("Failed to redirect stderr to file");
|
perror("Failed to redirect stderr to file");
|
||||||
fclose(temp_err_fp);
|
fclose(*err_fp);
|
||||||
exit(1);
|
exit(1);
|
||||||
}
|
}
|
||||||
*err_fp = temp_err_fp;
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
void setup_log_redirection(char *log_file, FILE **log_fp) {
|
void setup_log_redirection(char *log_file, FILE **log_fp) {
|
||||||
if (log_file) {
|
if (log_file) {
|
||||||
FILE *temp_log_fp = fopen(log_file, "w");
|
*log_fp = open_file(log_file, "w", "Failed to open log file");
|
||||||
if (!temp_log_fp) {
|
if (!(*log_fp)) {
|
||||||
perror("Failed to open log file");
|
|
||||||
exit(1);
|
exit(1);
|
||||||
}
|
}
|
||||||
fflush(stdout);
|
fflush(stdout);
|
||||||
if (dup2(fileno(temp_log_fp), STDOUT_FILENO) == -1) {
|
if (dup2(fileno(*log_fp), STDOUT_FILENO) == -1) {
|
||||||
perror("Failed to redirect stdout to file");
|
perror("Failed to redirect stdout to file");
|
||||||
fclose(temp_log_fp);
|
fclose(*log_fp);
|
||||||
exit(1);
|
exit(1);
|
||||||
}
|
}
|
||||||
*log_fp = temp_log_fp;
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
@@ -49,7 +44,6 @@ int main(int argc, char *argv[]) {
|
|||||||
int show_users = 0, show_processes = 0;
|
int show_users = 0, show_processes = 0;
|
||||||
FILE *log_fp = stdout, *err_fp = stderr;
|
FILE *log_fp = stdout, *err_fp = stderr;
|
||||||
|
|
||||||
// Early error redirection handling to ensure stderr is set up before parsing arguments
|
|
||||||
for (int i = 1; i < argc; i++) {
|
for (int i = 1; i < argc; i++) {
|
||||||
if ((strcmp(argv[i], "-e") == 0 || strcmp(argv[i], "--errors") == 0) && i + 1 < argc) {
|
if ((strcmp(argv[i], "-e") == 0 || strcmp(argv[i], "--errors") == 0) && i + 1 < argc) {
|
||||||
error_file = argv[i + 1];
|
error_file = argv[i + 1];
|
||||||
@@ -86,7 +80,6 @@ int main(int argc, char *argv[]) {
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
// Ensure log redirection is handled after argument parsing
|
|
||||||
setup_log_redirection(log_file, &log_fp);
|
setup_log_redirection(log_file, &log_fp);
|
||||||
|
|
||||||
if (show_users) {
|
if (show_users) {
|
||||||
|
|||||||
Reference in New Issue
Block a user