From 669fcb0b6be1085064267e817764fbb4f578a675 Mon Sep 17 00:00:00 2001 From: pankunull Date: Sat, 30 Aug 2025 14:00:35 +0200 Subject: Added files. --- Makefile | 58 +++++++++++++++++ Makefile.clang | 1 + chproc.c | 192 +++++++++++++++++++++++++++++++++++++++++++++++++++++++++ 3 files changed, 251 insertions(+) create mode 100644 Makefile create mode 100755 Makefile.clang create mode 100644 chproc.c diff --git a/Makefile b/Makefile new file mode 100644 index 0000000..4dcc90a --- /dev/null +++ b/Makefile @@ -0,0 +1,58 @@ +# Compiler and flags +CC = gcc +CFLAGS = -Wall -O2 + +# Source and target names +TARGET = chproc +SRC = $(TARGET).c +PRE = $(TARGET).i +ASM = $(TARGET).s +OBJ = $(TARGET).o + +# Example argument for test +ARG = --list init + +# Default: build executable +all: $(TARGET) + +# Stage 1: Preprocess +$(PRE): $(SRC) + $(CC) -E $(CFLAGS) $< -o $@ + +# Stage 2: Compile to assembly +$(ASM): $(PRE) + $(CC) -S $(CFLAGS) $< -o $@ + +# Stage 3: Assemble to object file +$(OBJ): $(ASM) + $(CC) -c $(CFLAGS) $< -o $@ + +# Stage 4: Link object to executable +$(TARGET): $(OBJ) + $(CC) $(CFLAGS) $< -o $@ + +# Run tests +define run_test + @name="$(1) $(2)"; \ + width=40; \ + printf "%s" "$$name"; \ + dots=$$((width - $${#name})); \ + for i in $$(seq 1 $$dots); do printf "."; done; \ + ./$(TARGET) $(2) >/dev/null 2>&1; \ + ec=$$?; \ + if [ $$ec -eq 0 ] || [ $$ec -eq 2 ]; then \ + echo " OK"; \ + else \ + echo " FAILED (exit $$ec)"; \ + exit 1; \ + fi +endef + +test: $(TARGET) + $(call run_test,$(TARGET) no args,) + $(call run_test,$(TARGET),$(ARG),) + +# Clean all generated files +clean: + rm -v -f $(PRE) $(ASM) $(OBJ) $(TARGET) + diff --git a/Makefile.clang b/Makefile.clang new file mode 100755 index 0000000..e275c31 --- /dev/null +++ b/Makefile.clang @@ -0,0 +1 @@ +clang -Weverything -o chproc_clang chproc.c diff --git a/chproc.c b/chproc.c new file mode 100644 index 0000000..6d407fb --- /dev/null +++ b/chproc.c @@ -0,0 +1,192 @@ +// 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