Compare commits

...
5 Commits
Author SHA1 Message Date
gandc f720ca31d3 Updated README 2025-02-27 20:36:59 +03:00
gandc 104ee954f4 remove old files 2025-02-27 20:32:33 +03:00
gandc 1216c17b87 Updated for new file_handling 2025-02-27 20:29:50 +03:00
gandc cb20bd27b4 new file_handling method 2025-02-27 20:29:21 +03:00
gandc f329180197 Update CmakeLists 2025-02-27 20:28:24 +03:00
9 changed files with 29 additions and 52 deletions

No files matched your search

+1 -1
View File
@@ -1,4 +1,4 @@
cmake_minimum_required(VERSION 3.10) cmake_minimum_required(VERSION 3.10)
project(ConsoleUtility C) project(ConsoleUtility C)
set(CMAKE_C_STANDARD 99) 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)
+2 -2
View File
@@ -2,8 +2,8 @@
## Build Process ## Build Process
```sh ```sh
mkdir build git clone https://git.gandc.ru/gandc/pr2_sysprog.git && cd pr2_sysprog
cd build mkdir build && cd build
cmake .. cmake ..
make make
``` ```
-11
View File
@@ -1,11 +0,0 @@
#include "error_handling.h"
#include <stdio.h>
FILE *open_error_file(const char *path) {
FILE *file = fopen(path, "w");
if (!file) {
fprintf(stderr, "Error: Cannot open error file %s\n", path);
return NULL;
}
return file;
}
-5
View File
@@ -1,5 +0,0 @@
#ifndef ERROR_HANDLING_H
#define ERROR_HANDLING_H
#include <stdio.h>
FILE *open_error_file(const char *path);
#endif
+11
View 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
View 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
-11
View File
@@ -1,11 +0,0 @@
#include "logging.h"
#include <stdio.h>
FILE *open_log_file(const char *path) {
FILE *file = fopen(path, "w");
if (!file) {
fprintf(stderr, "Error: Cannot open log file %s\n", path);
return NULL;
}
return file;
}
-5
View File
@@ -1,5 +0,0 @@
#ifndef LOGGING_H
#define LOGGING_H
#include <stdio.h>
FILE *open_log_file(const char *path);
#endif
+10 -17
View File
@@ -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) {