Compare commits
3 Commits
fec3d4637c
...
1216c17b87
| Author | SHA1 | Date | |
|---|---|---|---|
| 1216c17b87 | |||
| cb20bd27b4 | |||
| f329180197 |
@ -1,4 +1,4 @@
|
||||
cmake_minimum_required(VERSION 3.10)
|
||||
project(ConsoleUtility C)
|
||||
set(CMAKE_C_STANDARD 99)
|
||||
add_executable(console_utility src/main.c src/users.c src/processes.c src/logging.c src/error_handling.c)
|
||||
add_executable(console_utility src/main.c src/users.c src/processes.c src/file_handling.c)
|
||||
11
src/file_handling.c
Normal file
11
src/file_handling.c
Normal file
@ -0,0 +1,11 @@
|
||||
#include "file_handling.h"
|
||||
#include <stdlib.h>
|
||||
#include <stdio.h>
|
||||
|
||||
FILE *open_file(const char *path, const char *mode, const char *error_message) {
|
||||
FILE *file = fopen(path, mode);
|
||||
if (!file) {
|
||||
perror(error_message);
|
||||
}
|
||||
return file;
|
||||
}
|
||||
5
src/file_handling.h
Normal file
5
src/file_handling.h
Normal file
@ -0,0 +1,5 @@
|
||||
#ifndef FILE_HANDLING_H
|
||||
#define FILE_HANDLING_H
|
||||
#include <stdio.h>
|
||||
FILE *open_file(const char *path, const char *mode, const char *error_message);
|
||||
#endif
|
||||
27
src/main.c
27
src/main.c
@ -5,40 +5,35 @@
|
||||
#include <string.h>
|
||||
#include "users.h"
|
||||
#include "processes.h"
|
||||
#include "logging.h"
|
||||
#include "error_handling.h"
|
||||
#include "file_handling.h"
|
||||
|
||||
void setup_error_redirection(char *error_file, FILE **err_fp) {
|
||||
if (error_file) {
|
||||
FILE *temp_err_fp = fopen(error_file, "w");
|
||||
if (!temp_err_fp) {
|
||||
perror("Failed to open error log file");
|
||||
*err_fp = open_file(error_file, "w", "Failed to open error log file");
|
||||
if (!(*err_fp)) {
|
||||
exit(1);
|
||||
}
|
||||
fflush(stderr); // Ensure all previous stderr output is flushed
|
||||
if (dup2(fileno(temp_err_fp), STDERR_FILENO) == -1) {
|
||||
fflush(stderr);
|
||||
if (dup2(fileno(*err_fp), STDERR_FILENO) == -1) {
|
||||
perror("Failed to redirect stderr to file");
|
||||
fclose(temp_err_fp);
|
||||
fclose(*err_fp);
|
||||
exit(1);
|
||||
}
|
||||
*err_fp = temp_err_fp;
|
||||
}
|
||||
}
|
||||
|
||||
void setup_log_redirection(char *log_file, FILE **log_fp) {
|
||||
if (log_file) {
|
||||
FILE *temp_log_fp = fopen(log_file, "w");
|
||||
if (!temp_log_fp) {
|
||||
perror("Failed to open log file");
|
||||
*log_fp = open_file(log_file, "w", "Failed to open log file");
|
||||
if (!(*log_fp)) {
|
||||
exit(1);
|
||||
}
|
||||
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");
|
||||
fclose(temp_log_fp);
|
||||
fclose(*log_fp);
|
||||
exit(1);
|
||||
}
|
||||
*log_fp = temp_log_fp;
|
||||
}
|
||||
}
|
||||
|
||||
@ -49,7 +44,6 @@ int main(int argc, char *argv[]) {
|
||||
int show_users = 0, show_processes = 0;
|
||||
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++) {
|
||||
if ((strcmp(argv[i], "-e") == 0 || strcmp(argv[i], "--errors") == 0) && i + 1 < argc) {
|
||||
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);
|
||||
|
||||
if (show_users) {
|
||||
|
||||
Loading…
x
Reference in New Issue
Block a user