From 0737916f1af1869f5c3670ce86ceb86fcb2dbbfa Mon Sep 17 00:00:00 2001 From: Your Name Date: Wed, 10 Jul 2024 22:59:28 +0300 Subject: [PATCH] fixed --- README.md | 17 ++++++++------- TODO | 1 + libmu/human.c | 13 ++++++------ libmu/human.h | 2 +- libmu/parse_mode.c | 2 +- libmu/parse_mount.h | 2 +- libmu/proc_parser.c | 10 +++++---- libmu/proc_parser.h | 1 - src/kill.c | 2 +- src/ps.c | 50 +++++++++++++++++++++++++++++++++++++++------ src/que.c | 6 +----- 11 files changed, 71 insertions(+), 35 deletions(-) diff --git a/README.md b/README.md index 1880a9b..bde78a8 100644 --- a/README.md +++ b/README.md @@ -1,15 +1,14 @@ -# micro-utils +# microutils Own implementation of *nix utils License: wtfpl https://trivial.technology/ - Unportable: - proc_parser - ps - dmesg - mount - umount - nproc - pivot_root + proc_parser + ps + dmesg + mount + umount + nproc + pivot_root diff --git a/TODO b/TODO index e7dc62c..7c5f761 100644 --- a/TODO +++ b/TODO @@ -49,5 +49,6 @@ BUGS: xargs (getopt with glibc) FIX: + ps (proc_parser.h) echo (escape) que (unicode) diff --git a/libmu/human.c b/libmu/human.c index 4a1d367..7ac644b 100644 --- a/libmu/human.c +++ b/libmu/human.c @@ -2,22 +2,23 @@ char mu_hs_buf[MU_HUMAN_BUF_SIZE + 1]; -char *mu_humansize(off_t n, off_t block) { +char *mu_humansize(off_t n, int block) { memset(mu_hs_buf, '\0', sizeof(mu_hs_buf)); char *postfixes = "BKMGTPE"; - + size_t i = 0; double size = n; - size_t i; - for (i = 0; i < strlen(postfixes) && size >= block; i++) - size /= block; + while (size > (off_t)block) { + size /= (off_t)block; + i++; + } if (i) snprintf(mu_hs_buf, sizeof(mu_hs_buf), "%.1f%c", size, postfixes[i]); else - snprintf(mu_hs_buf, sizeof(mu_hs_buf), "%ju", n); + snprintf(mu_hs_buf, sizeof(mu_hs_buf), "%jd", n); return mu_hs_buf; } diff --git a/libmu/human.h b/libmu/human.h index a775d0e..a8f1749 100644 --- a/libmu/human.h +++ b/libmu/human.h @@ -7,5 +7,5 @@ #define MU_HUMAN_BUF_SIZE 16 -char *mu_humansize(off_t n, off_t block); +char *mu_humansize(off_t n, int block); #endif diff --git a/libmu/parse_mode.c b/libmu/parse_mode.c index 806caf9..322fd65 100644 --- a/libmu/parse_mode.c +++ b/libmu/parse_mode.c @@ -1,6 +1,6 @@ #include -#include #include +#include "parse_mode.h" #define U (S_ISUID | S_IRWXU) #define G (S_ISGID | S_IRWXG) diff --git a/libmu/parse_mount.h b/libmu/parse_mount.h index 44e99b6..ed43d52 100644 --- a/libmu/parse_mount.h +++ b/libmu/parse_mount.h @@ -1,7 +1,7 @@ #ifndef _PARSE_MOUNT_H #define _PARSE_MOUNT_H -#include #include +#include typedef struct { char *opt; diff --git a/libmu/proc_parser.c b/libmu/proc_parser.c index 87a3451..c0e84bd 100644 --- a/libmu/proc_parser.c +++ b/libmu/proc_parser.c @@ -44,10 +44,7 @@ int mu_proc_status(const char *prog_name, const pid_t pid, struct mu_proc *proc_ val++; /* Write values */ - if (!strncmp(token, "Name", 4)) - strcpy(proc_s->prog, val); - - else if (!strncmp(token, "Uid", 3)) + if (!strncmp(token, "Uid", 3)) proc_s->uid = strtoul(val, 0L, 10); else if (!strncmp(token, "Gid", 3)) @@ -78,6 +75,11 @@ int mu_proc_stat(const char *prog_name, const pid_t pid, struct mu_proc *proc_s) fscanf(fp, "%ld %ld %ld %ld %ld %ld %llu %lu %ld", &proc_s->cutime, &proc_s->cstime, &proc_s->priority, &proc_s->nice, &proc_s->num_threads, &proc_s->itrealvalue, &proc_s->starttime, &proc_s->vsize, &proc_s->rss); fclose(fp); + /* Remove ( and ) from string */ + size_t len = strlen(proc_s->cmdline) - 1; + proc_s->cmdline[len] = '\0'; + + memmove(proc_s->cmdline, proc_s->cmdline + 1, len); return 0; } diff --git a/libmu/proc_parser.h b/libmu/proc_parser.h index cdc46ee..1325ffe 100644 --- a/libmu/proc_parser.h +++ b/libmu/proc_parser.h @@ -7,7 +7,6 @@ struct mu_proc { /* from status */ - char prog[PATH_MAX + 1]; uid_t uid; gid_t gid; long vmrss; diff --git a/src/kill.c b/src/kill.c index bbb7d11..8a0656f 100644 --- a/src/kill.c +++ b/src/kill.c @@ -121,7 +121,7 @@ int main(int argc, char **argv) { ret = 1; for (int i = 0; i < argc; i++) { - if (!strcmp(proc.prog, argv[i])) + if (!strcmp(proc.cmdline, argv[i])) if (my_kill(pid, signal)) ret = 1; } diff --git a/src/ps.c b/src/ps.c index 61feaf8..524769b 100644 --- a/src/ps.c +++ b/src/ps.c @@ -1,3 +1,4 @@ +#include #include #include #include @@ -6,9 +7,34 @@ #include #include #include +#include #include "proc_parser.h" #include "human.h" +static char c_flag; + +static char *get_cmdline(const pid_t pid) { + static char path[PATH_MAX + 1]; + snprintf(path, sizeof(path), "/proc/%d/cmdline", pid); + + int fd = open(path, O_RDONLY); + if (fd < 0) + return NULL; + + ssize_t size = read(fd, path, sizeof(path)); + close(fd); + + if (size == 0) + return NULL; + + for (ssize_t i = 0; i < size; i++) { + if (path[i] == '\0' && i != size - 1) + path[i] = ' '; + } + + return path; +} + static int pscan(const pid_t pid) { struct mu_proc proc; if (mu_proc_parser("ps", pid, &proc)) @@ -25,19 +51,31 @@ static int pscan(const pid_t pid) { /* Print */ char virt[MU_HUMAN_BUF_SIZE + 1]; - strcpy(virt, mu_humansize((off_t)proc.vsize, 1024)); + strcpy(virt, mu_humansize(proc.vsize, 1024)); char rss[MU_HUMAN_BUF_SIZE + 1]; - strcpy(rss, mu_humansize((off_t)proc.vmrss * 1024, 1024)); + strcpy(rss, mu_humansize(proc.vmrss * 1024, 1024)); - printf("%6d %8s %4ld %4ld %8s %8s %2c %02um:%02us %2s\n", proc.pid, name, proc.priority, proc.nice, virt, rss, proc.state, rtime / 60, rtime % 60, proc.prog); + char *prog = (c_flag) ? get_cmdline(pid) : proc.cmdline; + if (prog == NULL) + prog = proc.cmdline; + + printf("%6d %8s %4ld %4ld %8s %8s %2c %02um:%02us %2s\n", proc.pid, name, proc.priority, proc.nice, virt, rss, proc.state, rtime / 60, rtime % 60, prog); return 0; } int main(int argc, char **argv) { - while (getopt(argc, argv, "") != -1) { - puts("ps [a] [PID]\n\t-a Print all processes\n"); - return 0; + int opt; + while ((opt = getopt(argc, argv, "c")) != -1) { + switch (opt) { + case 'c': + c_flag = 1; + break; + + default: + puts("ps [c] [PID]\n\t-c Print cmdline instead of program name"); + return 0; + } } argv += optind; diff --git a/src/que.c b/src/que.c index 964fafb..9f87a16 100644 --- a/src/que.c +++ b/src/que.c @@ -42,8 +42,7 @@ static char *file_path; enum { FAILED_SAVE = 1, - SUCCESS_SAVE, - MARK + SUCCESS_SAVE }; static char status_type; static char modified_flag; @@ -419,9 +418,6 @@ static void statusBar(const char *path) { else if (status_type == SUCCESS_SAVE) ret = snprintf(info, sizeof(info), "File %s success saved", path); - else if (status_type == MARK) - ret = snprintf(info, sizeof(info), "Mark set at %ux %uy", curx, cury); - status_type = 0; bufAppend(info, ret); }