Compare commits
15
Commits
365e7c8e6c
...
main
| Author | SHA1 | Date | |
|---|---|---|---|
|
|
f720ca31d3
|
||
|
|
104ee954f4
|
||
|
|
1216c17b87
|
||
|
|
cb20bd27b4
|
||
|
|
f329180197
|
||
|
|
fec3d4637c
|
||
|
|
3e202bc314
|
||
|
|
573887adce
|
||
|
|
bbedbefe62
|
||
|
|
d6bb1809f3
|
||
|
|
c008d95286
|
||
|
|
2476d1a2c4
|
||
|
|
15348b640e
|
||
|
|
5b5b54dd31
|
||
|
|
b73771ad92
|
No files matched your search
@@ -1,7 +1,5 @@
|
|||||||
# Ignore build directory
|
|
||||||
build/
|
build/
|
||||||
|
|
||||||
# Ignore CMake generated files
|
|
||||||
CMakeCache.txt
|
CMakeCache.txt
|
||||||
CMakeFiles/
|
CMakeFiles/
|
||||||
cmake_install.cmake
|
cmake_install.cmake
|
||||||
|
|||||||
@@ -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)
|
||||||
@@ -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
|
||||||
|
```
|
||||||
Whitespace-only changes.
Whitespace-only changes.
@@ -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;
|
||||||
|
}
|
||||||
@@ -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
|
||||||
Whitespace-only changes.
Whitespace-only changes.
+96
@@ -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;
|
||||||
|
}
|
||||||
@@ -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);
|
||||||
|
}
|
||||||
@@ -0,0 +1,5 @@
|
|||||||
|
#ifndef PROCESSES_H
|
||||||
|
#define PROCESSES_H
|
||||||
|
#include <stdio.h>
|
||||||
|
void list_processes(FILE *output);
|
||||||
|
#endif
|
||||||
+12
@@ -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();
|
||||||
|
}
|
||||||
@@ -0,0 +1,5 @@
|
|||||||
|
#ifndef USERS_H
|
||||||
|
#define USERS_H
|
||||||
|
#include <stdio.h>
|
||||||
|
void list_users(FILE *output);
|
||||||
|
#endif
|
||||||
Reference in new issue
Block a user