about summary refs log tree commit diff
path: root/csrc
diff options
context:
space:
mode:
authormakefunstuff <[email protected]>2024-07-13 22:17:02 +0200
committermakefunstuff <[email protected]>2024-07-13 22:17:02 +0200
commit1072f6346489d41f38e565a408be0d12c1c898b2 (patch)
treeb5cccfcbbcbecba281edac79c2f86250182e06a5 /csrc
parent714036825c38be947eb88a82c02425d7932d2919 (diff)
downloadtinkerbunk-1072f6346489d41f38e565a408be0d12c1c898b2.tar.gz
ps
Diffstat (limited to 'csrc')
-rw-r--r--csrc/main.c6
-rw-r--r--csrc/ps.c45
-rw-r--r--csrc/ps.h12
3 files changed, 63 insertions, 0 deletions
diff --git a/csrc/main.c b/csrc/main.c
index f8393e5..7ba36eb 100644
--- a/csrc/main.c
+++ b/csrc/main.c
@@ -1,5 +1,6 @@
 #include "cp.h"
 #include "http.h"
+#include "ps.h"
 #include "sh.h"
 #include "stat.h"
 #include "wc.h"
@@ -41,5 +42,10 @@ int main(int argc, char *argv[]) {
   } else if (strncmp(command, "http", 4) == 0) {
     int port = atoi(argv[2]);
     start_server(port);
+  } else if (strncmp(command, "ps", 2) == 0) {
+    if (ps() == PS_ERR) {
+      return 1;
+    };
   }
+  return 0;
 }
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;
+}
diff --git a/csrc/ps.h b/csrc/ps.h
new file mode 100644
index 0000000..24839f3
--- /dev/null
+++ b/csrc/ps.h
@@ -0,0 +1,12 @@
+#ifndef __PS_H__
+#define __PS_H__
+
+typedef enum {
+  PS_OK,
+  PS_ERR,
+} ps_status_t;
+
+void print_process_info(const char *pid);
+ps_status_t ps();
+
+#endif