diff options
author | makefunstuff <[email protected]> | 2024-07-13 22:17:02 +0200 |
---|---|---|
committer | makefunstuff <[email protected]> | 2024-07-13 22:17:02 +0200 |
commit | 1072f6346489d41f38e565a408be0d12c1c898b2 (patch) | |
tree | b5cccfcbbcbecba281edac79c2f86250182e06a5 /csrc/ps.c | |
parent | 714036825c38be947eb88a82c02425d7932d2919 (diff) | |
download | tinkerbunk-1072f6346489d41f38e565a408be0d12c1c898b2.tar.gz |
ps
Diffstat (limited to '')
-rw-r--r-- | csrc/ps.c | 45 |
1 files changed, 45 insertions, 0 deletions
diff --git a/csrc/ps.c b/csrc/ps.c new file mode 100644 index 0000000..3481498 --- /dev/null +++ b/csrc/ps.c @@ -0,0 +1,45 @@ +#include "ps.h" +#include <ctype.h> +#include <dirent.h> +#include <stdio.h> +#include <string.h> + +void print_process_info(const char *pid) { + char path[256], line[256]; + FILE *status_file; + + snprintf(path, sizeof(path), "/proc/%s/status", pid); + status_file = fopen(path, "r"); + if (!status_file) { + return; + } + + printf("Process ID: %s\n", pid); + while (fgets(line, sizeof(line), status_file)) { + if (strncmp(line, "Name:", 5) == 0 || strncmp(line, "State:", 6) == 0 || + strncmp(line, "PPid:", 5) == 0) { + printf("%s", line); + } + } + fclose(status_file); + printf("\n"); +} + +ps_status_t ps() { + DIR *proc_dir = opendir("/proc"); + struct dirent *entry; + + if (!proc_dir) { + perror("opendir"); + return PS_ERR; + } + + while ((entry = readdir(proc_dir))) { + if (entry->d_type == DT_DIR && isdigit(entry->d_name[0])) { + print_process_info(entry->d_name); + } + } + + closedir(proc_dir); + return PS_OK; +} |