diff options
author | pankunull <panku_null@proton.me> | 2025-08-30 15:08:10 +0200 |
---|---|---|
committer | pankunull <panku_null@proton.me> | 2025-08-30 15:08:10 +0200 |
commit | 297e7abf41ec4be028b0a250c16c1ab800963d32 (patch) | |
tree | 9fdd00dc3a67f85f9cd8229caeee750ea22a79c2 | |
parent | 669fcb0b6be1085064267e817764fbb4f578a675 (diff) |
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).
-rw-r--r-- | Makefile | 33 | ||||
-rw-r--r-- | src/chproc.c (renamed from chproc.c) | 35 |
2 files changed, 46 insertions, 22 deletions
@@ -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) @@ -23,6 +23,7 @@ struct options { int list; + int all; int kill; char *process_name; char *process_pid; @@ -32,6 +33,7 @@ struct options { static void options_init(struct options *x) { x->list = false; + x->all = false; x->kill = false; x->process_pid = NULL; x->process_name = NULL; @@ -42,7 +44,8 @@ static void usage(void) { fprintf(stderr, "Usage: %s <option> [process_name, pid]\n" - " -l, --list <name> list all PID of that process\n" + " -l, --list <name> list all pid for that process\n" + " -a, --all list all processes\n" " -k, --kill <PID> kill a proces give then PID\n" , PROGRAM_NAME); } @@ -50,7 +53,8 @@ static void usage(void) static int process_list(DIR *dir, - const char *proc_name) + const char *proc_name, + struct options *x) { char path[PATH_MAX]; char buffer[256]; @@ -86,16 +90,23 @@ static int process_list(DIR *dir, continue; } - if (fgets(buffer, sizeof(buffer), f)) { - len = strlen(buffer); + if (!(fgets(buffer, sizeof(buffer), f))) + continue; + + len = strlen(buffer); - if (len > 0 && buffer[len-1] == '\n') - buffer[len-1] = '\0'; + if (len > 0 && buffer[len-1] == '\n') + buffer[len-1] = '\0'; + /* flag instead of re-writing a function */ + if (x->list) { if (strcmp(buffer, proc_name) == 0) { printf("%s - %s\n", entry->d_name, buffer); ++count; } + } else { + printf("%s - %s\n", entry->d_name, buffer); + ++count; } fclose(f); } @@ -140,6 +151,7 @@ int main(int argc, char *argv[]) struct option long_options[] = { {"list", required_argument, 0, 'l'}, + {"all", no_argument, 0, 'a'}, {"kill", required_argument, 0, 'k'}, {0, 0, 0, 0} }; @@ -153,18 +165,21 @@ int main(int argc, char *argv[]) return EXIT_FAILURE; } - if (argc < 3) { + if (argc < 2) { usage(); return 2; } - while ((opt = getopt_long(argc, argv, "l:k:", + while ((opt = getopt_long(argc, argv, "l:k:a", long_options, NULL)) != -1) { switch (opt) { case 'l': x.list = true; x.process_name = optarg; break; + case 'a': + x.all = true; + break; case 'k': x.kill = true; x.process_pid = optarg; @@ -177,8 +192,8 @@ int main(int argc, char *argv[]) } } - if (x.list) - if (process_list(dir_proc, x.process_name) == 1) + if (x.list || x.all) + if (process_list(dir_proc, x.process_name, &x) == 1) return 1; if (x.kill) |