From 3a4dd7dd423023b35d501bdad966f13e18703cef Mon Sep 17 00:00:00 2001 From: pankunull Date: Sat, 30 Aug 2025 20:15:59 +0200 Subject: The program got out of hand, I'm doing stuff it doesn't need. Good exercise! --- Makefile | 16 +++ src/chproc.c | 322 ++++++++++++++++++++++++++++++++++++++++++++--------------- 2 files changed, 255 insertions(+), 83 deletions(-) diff --git a/Makefile b/Makefile index af3c7f7..23a3caa 100644 --- a/Makefile +++ b/Makefile @@ -6,6 +6,10 @@ CFLAGS = -Wall -O2 SRC_DIR = src BIN_DIR = bin +# Installation directory +PREFIX ?= /usr/local +BINDIR ?= $(PREFIX)/bin + # Source and target names TARGET = chproc SRC = $(SRC_DIR)/$(TARGET).c @@ -57,6 +61,18 @@ define run_test fi endef +# Install +install: $(BIN) + @echo "Installing $(TARGET) to $(BINDIR)" + mkdir -p $(BINDIR) + cp -v $(BIN) $(BINDIR)/ + +# Uninstall +uninstall: + @echo "Removing $(TARGET) from $(BINDIR)" + rm -v -f $(BINDIR)/$(TARGET) + +# Test test: $(BIN) $(call run_test,$(TARGET),no args) $(call run_test,$(TARGET),$(ARG)) diff --git a/src/chproc.c b/src/chproc.c index 46876a5..b4ebee9 100644 --- a/src/chproc.c +++ b/src/chproc.c @@ -1,8 +1,8 @@ // SPDX-License-Identifier: GPL-2.0-only /* - * chproc.c - simple process checker + * chproc.c - simple process manager * - * Iterate over /proc and print PIDs of processes matching the given name. + * Features: list and kill. */ #include @@ -20,52 +20,96 @@ #include #define PROGRAM_NAME "chproc" +#define PROGRAM_VERSION "0.1" -struct options { - int list; +#define ARRAY_SIZE(x) (sizeof(x) / sizeof((x)[0])) + +#define DIR_PROC "/proc" + + +struct flag_options { + int name; int all; int kill; + int help; + int version; +}; + + +struct data_options { char *process_name; char *process_pid; }; -static void options_init(struct options *x) -{ - x->list = false; - x->all = false; - x->kill = false; - x->process_pid = NULL; - x->process_name = NULL; -} +struct options { + struct flag_options flags; + struct data_options data; +}; + + +struct flag_handler { + int *flag; + int id; +}; -static void usage(void) +enum flag_index { + F_NAME, + F_ALL, + F_KILL, + F_HELP, + F_VERSION +}; + + +static int usage(void) { fprintf(stderr, "Usage: %s