From 297e7abf41ec4be028b0a250c16c1ab800963d32 Mon Sep 17 00:00:00 2001 From: pankunull Date: Sat, 30 Aug 2025 15:08:10 +0200 Subject: Added feature 'all' (-a/--all) to show all processes running on the machine. Instead of re-writing a new function, a simple flag was added (x->flag). --- Makefile | 33 ++++++---- chproc.c | 192 ------------------------------------------------------ src/chproc.c | 207 +++++++++++++++++++++++++++++++++++++++++++++++++++++++++++ 3 files changed, 228 insertions(+), 204 deletions(-) delete mode 100644 chproc.c create mode 100644 src/chproc.c diff --git a/Makefile b/Makefile index 4dcc90a..af3c7f7 100644 --- a/Makefile +++ b/Makefile @@ -2,21 +2,30 @@ CC = gcc CFLAGS = -Wall -O2 +# Directories +SRC_DIR = src +BIN_DIR = bin + # Source and target names TARGET = chproc -SRC = $(TARGET).c -PRE = $(TARGET).i -ASM = $(TARGET).s -OBJ = $(TARGET).o +SRC = $(SRC_DIR)/$(TARGET).c +PRE = $(BIN_DIR)/$(TARGET).i +ASM = $(BIN_DIR)/$(TARGET).s +OBJ = $(BIN_DIR)/$(TARGET).o +BIN = $(BIN_DIR)/$(TARGET) # Example argument for test ARG = --list init # Default: build executable -all: $(TARGET) +all: $(BIN) + +# Ensure bin directory exists +$(BIN_DIR): + mkdir -p $(BIN_DIR) # Stage 1: Preprocess -$(PRE): $(SRC) +$(PRE): $(SRC) | $(BIN_DIR) $(CC) -E $(CFLAGS) $< -o $@ # Stage 2: Compile to assembly @@ -28,7 +37,7 @@ $(OBJ): $(ASM) $(CC) -c $(CFLAGS) $< -o $@ # Stage 4: Link object to executable -$(TARGET): $(OBJ) +$(BIN): $(OBJ) $(CC) $(CFLAGS) $< -o $@ # Run tests @@ -38,7 +47,7 @@ define run_test printf "%s" "$$name"; \ dots=$$((width - $${#name})); \ for i in $$(seq 1 $$dots); do printf "."; done; \ - ./$(TARGET) $(2) >/dev/null 2>&1; \ + ./$(BIN) $(2) >/dev/null 2>&1; \ ec=$$?; \ if [ $$ec -eq 0 ] || [ $$ec -eq 2 ]; then \ echo " OK"; \ @@ -48,11 +57,11 @@ define run_test fi endef -test: $(TARGET) - $(call run_test,$(TARGET) no args,) - $(call run_test,$(TARGET),$(ARG),) +test: $(BIN) + $(call run_test,$(TARGET),no args) + $(call run_test,$(TARGET),$(ARG)) # Clean all generated files clean: - rm -v -f $(PRE) $(ASM) $(OBJ) $(TARGET) + rm -v -f $(BIN_DIR)/$(TARGET).* $(BIN) diff --git a/chproc.c b/chproc.c deleted file mode 100644 index 6d407fb..0000000 --- a/chproc.c +++ /dev/null @@ -1,192 +0,0 @@ -// SPDX-License-Identifier: GPL-2.0-only -/* - * chproc.c - simple process checker - * - * Iterate over /proc and print PIDs of processes matching the given name. - */ - -#include -#include -#include -#include -#include -#include -#include -#include -#include -#include -#include -#include -#include - -#define PROGRAM_NAME "chproc" - -struct options { - int list; - int kill; - char *process_name; - char *process_pid; -}; - - -static void options_init(struct options *x) -{ - x->list = false; - x->kill = false; - x->process_pid = NULL; - x->process_name = NULL; -} - - -static void usage(void) -{ - fprintf(stderr, - "Usage: %s