Compare commits
10 Commits
365e7c8e6c
...
fec3d4637c
| Author | SHA1 | Date | |
|---|---|---|---|
| fec3d4637c | |||
| 3e202bc314 | |||
| 573887adce | |||
| bbedbefe62 | |||
| d6bb1809f3 | |||
| c008d95286 | |||
| 2476d1a2c4 | |||
| 15348b640e | |||
| 5b5b54dd31 | |||
| b73771ad92 |
2
.gitignore
vendored
2
.gitignore
vendored
@ -1,7 +1,5 @@
|
||||
# Ignore build directory
|
||||
build/
|
||||
|
||||
# Ignore CMake generated files
|
||||
CMakeCache.txt
|
||||
CMakeFiles/
|
||||
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/logging.c src/error_handling.c)
|
||||
31
README.md
Normal file
31
README.md
Normal file
@ -0,0 +1,31 @@
|
||||
# Console Utility
|
||||
|
||||
## Build Process
|
||||
```sh
|
||||
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
|
||||
```
|
||||
@ -0,0 +1,11 @@
|
||||
#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;
|
||||
}
|
||||
@ -0,0 +1,5 @@
|
||||
#ifndef ERROR_HANDLING_H
|
||||
#define ERROR_HANDLING_H
|
||||
#include <stdio.h>
|
||||
FILE *open_error_file(const char *path);
|
||||
#endif
|
||||
@ -0,0 +1,11 @@
|
||||
#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;
|
||||
}
|
||||
@ -0,0 +1,5 @@
|
||||
#ifndef LOGGING_H
|
||||
#define LOGGING_H
|
||||
#include <stdio.h>
|
||||
FILE *open_log_file(const char *path);
|
||||
#endif
|
||||
103
src/main.c
103
src/main.c
@ -0,0 +1,103 @@
|
||||
#include <stdio.h>
|
||||
#include <stdlib.h>
|
||||
#include <unistd.h>
|
||||
#include <getopt.h>
|
||||
#include <string.h>
|
||||
#include "users.h"
|
||||
#include "processes.h"
|
||||
#include "logging.h"
|
||||
#include "error_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");
|
||||
exit(1);
|
||||
}
|
||||
fflush(stderr); // Ensure all previous stderr output is flushed
|
||||
if (dup2(fileno(temp_err_fp), STDERR_FILENO) == -1) {
|
||||
perror("Failed to redirect stderr to file");
|
||||
fclose(temp_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");
|
||||
exit(1);
|
||||
}
|
||||
fflush(stdout);
|
||||
if (dup2(fileno(temp_log_fp), STDOUT_FILENO) == -1) {
|
||||
perror("Failed to redirect stdout to file");
|
||||
fclose(temp_log_fp);
|
||||
exit(1);
|
||||
}
|
||||
*log_fp = temp_log_fp;
|
||||
}
|
||||
}
|
||||
|
||||
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;
|
||||
|
||||
// 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];
|
||||
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;
|
||||
}
|
||||
}
|
||||
|
||||
// Ensure log redirection is handled after argument parsing
|
||||
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
src/users.c
12
src/users.c
@ -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
|
||||
Loading…
x
Reference in New Issue
Block a user