From 5d24364332271f5b1cd4b91853b6f778112235d3 Mon Sep 17 00:00:00 2001 From: Your Name Date: Fri, 5 Jul 2024 19:54:49 +0300 Subject: [PATCH] fixed ps, readlink --- TODO | 2 +- include/libmu/proc_parser.h | 16 +++++ src/coreutils/readlink/readlink.c | 4 +- src/coreutils/yes/build.sh | 3 - src/coreutils/yes/yes.c | 24 ------- src/procps/ps/ps.c | 113 ++++++++++++++++++++---------- 6 files changed, 96 insertions(+), 66 deletions(-) create mode 100644 include/libmu/proc_parser.h delete mode 100755 src/coreutils/yes/build.sh delete mode 100644 src/coreutils/yes/yes.c diff --git a/TODO b/TODO index 8f9e382..7c5f761 100644 --- a/TODO +++ b/TODO @@ -49,6 +49,6 @@ BUGS: xargs (getopt with glibc) FIX: - ps + ps (proc_parser.h) echo (escape) que (unicode) diff --git a/include/libmu/proc_parser.h b/include/libmu/proc_parser.h new file mode 100644 index 0000000..e3743d5 --- /dev/null +++ b/include/libmu/proc_parser.h @@ -0,0 +1,16 @@ +#ifndef _PROC_PARSER +#define _PROC_PARSER + +#include +#include +#include +#include +#include + +struct { + char *cmdline; + char state; + long nice; +} mu_proc; + +#endif diff --git a/src/coreutils/readlink/readlink.c b/src/coreutils/readlink/readlink.c index aa3ea88..4b96bc4 100644 --- a/src/coreutils/readlink/readlink.c +++ b/src/coreutils/readlink/readlink.c @@ -26,7 +26,7 @@ int main(int argc, char **argv) { for (int i = 0; i < argc; i++) { char path[PATH_MAX + 1]; - ssize_t r = readlink(argv[i], path, sizeof(path)); + ssize_t r = readlink(argv[i], path, sizeof(path) - 1); if (r < 0) { fprintf(stderr, "readlink: %s: %s\n", argv[i], strerror(errno)); return 1; @@ -37,6 +37,8 @@ int main(int argc, char **argv) { return 1; } + path[r] = '\0'; + if (n_flag) fputs(path, stdout); diff --git a/src/coreutils/yes/build.sh b/src/coreutils/yes/build.sh deleted file mode 100755 index 6d8974b..0000000 --- a/src/coreutils/yes/build.sh +++ /dev/null @@ -1,3 +0,0 @@ -#!/bin/sh -project_dir=$(pwd) -echo ./*.c $CFLAGS -o $OUTPUT$(basename $project_dir) | xargs $CC diff --git a/src/coreutils/yes/yes.c b/src/coreutils/yes/yes.c deleted file mode 100644 index 2e07bef..0000000 --- a/src/coreutils/yes/yes.c +++ /dev/null @@ -1,24 +0,0 @@ -#include -#include -#include -#include - -void print(const char *buf, ssize_t len) { - if (write(STDOUT_FILENO, buf, len) < 0) - exit(1); -} - -int main(const int argc, const char **argv) { - if (argc == 1) - while (1) - print("y\n", 2); - - while (1) { - for (ssize_t i = 1; i < argc; i++) { - print(argv[i], strlen(argv[i])); - print(" ", 1); - } - - print("\n", 1); - } -} diff --git a/src/procps/ps/ps.c b/src/procps/ps/ps.c index cc0b88d..3d6f43b 100644 --- a/src/procps/ps/ps.c +++ b/src/procps/ps/ps.c @@ -6,63 +6,102 @@ #include #include #include - -int open_file(const char *path) { - int fd = open(path, O_RDONLY); - if (fd < 0) { - fprintf(stderr, "ps: %s: %s\n", path, strerror(errno)); - return -1; - } - - return fd; -} +#include +#include int pscan(const char *pid) { char path[PATH_MAX + 1]; - char cmdline[PATH_MAX + 1]; /* Arguments */ - snprintf(path, sizeof(path), "/proc/%s/cmdline", pid); - int fd = open_file(path); - if (fd == -1) + snprintf(path, sizeof(path), "/proc/%s/status", pid); + int fd = open(path, O_RDONLY); + if (fd < 0) { + fprintf(stderr, "ps: %s: %s\n", path, strerror(errno)); return 1; + } + + char status[2048]; + ssize_t n = read(fd, status, sizeof(status) - 1); + status[n] = '\0'; - read(fd, cmdline, sizeof(cmdline)); close(fd); - printf("%5s %5s\n", pid, cmdline); + char *prog = "none"; + char *state = "none"; + char *user = "none"; + + /* Parsing */ + char *token = strtok(status, "\n"); + while (token) { + char *val = strchr(token, ':'); + if (val == NULL) { + fprintf(stderr, "ps: incorrect %s file\n", path); + return 1; + } + + *val = '\0'; + val++; + + /* Strip */ + while (isspace(*val)) + val++; + + /* Write values */ + if (!strncmp(token, "State", 5)) + state = val; + + else if (!strncmp(token, "Name", 4)) + prog = val; + + else if (!strncmp(token, "Uid", 3)) { + struct passwd *pw = getpwuid(atoi(val)); + if (pw != NULL) + user = pw->pw_name; + } + + token = strtok(NULL, "\n"); + } + + printf("%s\t%s\t%s\t%s\n", pid, state, user, prog); return 0; } int main(int argc, char **argv) { int opt; - while ((opt = getopt(argc, argv, "o:")) != -1) { - switch (opt) { - case 'o': - break; - - default: - printf("ps [o] [PID]\n\t-o Options (nice, tty, cmd, pid, nice)\n"); - return 0; - } + while ((opt = getopt(argc, argv, "")) != -1) { + puts("ps [PID]"); + return 0; } - DIR *dp = opendir("/proc"); - if (dp == NULL) { - fprintf(stderr, "ps: /proc: %s\n", strerror(errno)); - return 1; - } + argv += optind; + argc -= optind; - printf("PID CMD\n"); + printf("PID\tSTATE\tUSER\tCMD\n"); int ret = 0; - struct dirent *ep; - while ((ep = readdir(dp)) != NULL) - if (atoi(ep->d_name)) - if (pscan(ep->d_name)) - ret = 1; + if (argc == 0) { + DIR *dp = opendir("/proc"); + if (dp == NULL) { + fprintf(stderr, "ps: /proc: %s\n", strerror(errno)); + return 1; + } + + struct dirent *ep; + while ((ep = readdir(dp)) != NULL) + if (atoi(ep->d_name)) + if (pscan(ep->d_name)) + ret = 1; + + closedir(dp); + } + + else { + for (int i = 0; i < argc; i++) + if (atoi(argv[i])) + if (pscan(argv[i])) + ret = 1; + } - closedir(dp); return ret; }