Compare commits

..
15 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
gandc fec3d4637c Add README 2025-02-23 01:02:35 +03:00
gandc 3e202bc314 users 2025-02-23 01:01:17 +03:00
gandc 573887adce processes 2025-02-23 01:00:54 +03:00
gandc bbedbefe62 logging 2025-02-23 01:00:31 +03:00
gandc d6bb1809f3 error_handling 2025-02-23 01:00:02 +03:00
gandc c008d95286 first version of CmakeLists 2025-02-23 00:59:20 +03:00
gandc 2476d1a2c4 fixed log output 2025-02-23 00:58:27 +03:00
gandc 15348b640e next verison 2025-02-23 00:52:58 +03:00
gandc 5b5b54dd31 first version 2025-02-23 00:51:46 +03:00
gandc b73771ad92 update gitignore 2025-02-22 22:00:11 +03:00
14 changed files with 185 additions and 2 deletions

No files matched your search

-2
View File
@@ -1,7 +1,5 @@
# Ignore build directory
build/
# Ignore CMake generated files
CMakeCache.txt
CMakeFiles/
cmake_install.cmake
+4
View File
@@ -0,0 +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/file_handling.c)
+31
View File
@@ -0,0 +1,31 @@
# Console Utility
## Build Process
```sh
git clone https://git.gandc.ru/gandc/pr2_sysprog.git && cd pr2_sysprog
mkdir build && cd build
cmake ..
make
```
## Usage Examples
- Display users and home directories:
```sh
./console_utility -u
```
- Display running processes:
```sh
./console_utility -p
```
- Redirect output to a log file:
```sh
./console_utility -u -l output.log
```
- Redirect errors to a file:
```sh
./console_utility -p -e errors.log
```
- Show help:
```sh
./console_utility -h
```
View File
Whitespace-only changes.
View File
Whitespace-only changes.
+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
View File
Whitespace-only changes.
View File
Whitespace-only changes.
+96
View File
@@ -0,0 +1,96 @@
#include <stdio.h>
#include <stdlib.h>
#include <unistd.h>
#include <getopt.h>
#include <string.h>
#include "users.h"
#include "processes.h"
#include "file_handling.h"
void setup_error_redirection(char *error_file, FILE **err_fp) {
if (error_file) {
*err_fp = open_file(error_file, "w", "Failed to open error log file");
if (!(*err_fp)) {
exit(1);
}
fflush(stderr);
if (dup2(fileno(*err_fp), STDERR_FILENO) == -1) {
perror("Failed to redirect stderr to file");
fclose(*err_fp);
exit(1);
}
}
}
void setup_log_redirection(char *log_file, FILE **log_fp) {
if (log_file) {
*log_fp = open_file(log_file, "w", "Failed to open log file");
if (!(*log_fp)) {
exit(1);
}
fflush(stdout);
if (dup2(fileno(*log_fp), STDOUT_FILENO) == -1) {
perror("Failed to redirect stdout to file");
fclose(*log_fp);
exit(1);
}
}
}
int main(int argc, char *argv[]) {
int opt;
char *log_file = NULL;
char *error_file = NULL;
int show_users = 0, show_processes = 0;
FILE *log_fp = stdout, *err_fp = stderr;
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];
setup_error_redirection(error_file, &err_fp);
}
}
struct option long_options[] = {
{"users", no_argument, 0, 'u'},
{"processes", no_argument, 0, 'p'},
{"log", required_argument, 0, 'l'},
{"errors", required_argument, 0, 'e'},
{"help", no_argument, 0, 'h'},
{0, 0, 0, 0}
};
while ((opt = getopt_long(argc, argv, "upl:e:h", long_options, NULL)) != -1) {
switch (opt) {
case 'u': show_users = 1; break;
case 'p': show_processes = 1; break;
case 'l': log_file = optarg; break;
case 'e': error_file = optarg; break;
case 'h':
printf("Usage: %s [options]\n", argv[0]);
printf(" -u, --users List users and home directories\n");
printf(" -p, --processes List running processes\n");
printf(" -l, --log PATH Redirect output to a file\n");
printf(" -e, --errors PATH Redirect errors to a file\n");
printf(" -h, --help Show this help message\n");
return 0;
default:
fprintf(stderr, "Unknown option. Use -h for help.\n");
return 1;
}
}
setup_log_redirection(log_file, &log_fp);
if (show_users) {
list_users(log_fp);
}
if (show_processes) {
list_processes(log_fp);
}
if (log_fp != stdout) fclose(log_fp);
if (err_fp != stderr) fclose(err_fp);
return 0;
}
+16
View File
@@ -0,0 +1,16 @@
#include "processes.h"
#include <stdio.h>
#include <stdlib.h>
void list_processes(FILE *output) {
FILE *fp = popen("ps -eo pid,comm --sort=pid", "r");
if (fp == NULL) {
fprintf(output, "Error opening process list\n");
return;
}
char line[256];
while (fgets(line, sizeof(line), fp) != NULL) {
fprintf(output, "%s", line);
}
pclose(fp);
}
+5
View File
@@ -0,0 +1,5 @@
#ifndef PROCESSES_H
#define PROCESSES_H
#include <stdio.h>
void list_processes(FILE *output);
#endif
+12
View File
@@ -0,0 +1,12 @@
#include "users.h"
#include <stdio.h>
#include <stdlib.h>
#include <pwd.h>
void list_users(FILE *output) {
struct passwd *pw;
while ((pw = getpwent()) != NULL) {
fprintf(output, "%s: %s\n", pw->pw_name, pw->pw_dir);
}
endpwent();
}
+5
View File
@@ -0,0 +1,5 @@
#ifndef USERS_H
#define USERS_H
#include <stdio.h>
void list_users(FILE *output);
#endif