This commit is contained in:
Your Name 2024-07-10 16:59:36 +03:00
parent 34fbb11114
commit b1ab591e39
5 changed files with 41 additions and 68 deletions

View File

@ -47,4 +47,4 @@ int main(int argc, char **argv) {
} }
EOF EOF
echo $CFLAGS | xargs $CC mutils.c bin/*.c obj/*.o -Iconfigs -Ilibmu -omutils echo $CFLAGS | xargs $CC -Iconfigs -Ilibmu bin/*.c obj/*.o mutils.c -omutils

View File

@ -53,6 +53,9 @@ int mu_proc_status(const char *prog_name, const pid_t pid, struct mu_proc *proc_
else if (!strncmp(token, "Gid", 3)) else if (!strncmp(token, "Gid", 3))
proc_s->gid = strtoul(val, 0L, 10); proc_s->gid = strtoul(val, 0L, 10);
else if (!strncmp(token, "VmRSS", 3))
proc_s->vmrss = strtoul(val, 0L, 10);
token = strtok(NULL, "\n"); token = strtok(NULL, "\n");
} }
@ -72,7 +75,7 @@ int mu_proc_stat(const char *prog_name, const pid_t pid, struct mu_proc *proc_s)
} }
fscanf(fp, "%d %s %c %d %d %d %d %d %u %lu %lu %lu %lu %lu %lu", &proc_s->pid, proc_s->cmdline, &proc_s->state, &proc_s->ppid, &proc_s->pgrp, &proc_s->sid, &proc_s->tty, &proc_s->tpgid, &proc_s->flags, &proc_s->minflt, &proc_s->cminflt, &proc_s->majflt, &proc_s->cmajflt, &proc_s->utime, &proc_s->stime); fscanf(fp, "%d %s %c %d %d %d %d %d %u %lu %lu %lu %lu %lu %lu", &proc_s->pid, proc_s->cmdline, &proc_s->state, &proc_s->ppid, &proc_s->pgrp, &proc_s->sid, &proc_s->tty, &proc_s->tpgid, &proc_s->flags, &proc_s->minflt, &proc_s->cminflt, &proc_s->majflt, &proc_s->cmajflt, &proc_s->utime, &proc_s->stime);
fscanf(fp, "%ld %ld %ld %ld %ld %ld %llu %lu %ld %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, &proc_s->rsslim); 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); fclose(fp);
return 0; return 0;

View File

@ -10,6 +10,7 @@ struct mu_proc {
char prog[PATH_MAX + 1]; char prog[PATH_MAX + 1];
uid_t uid; uid_t uid;
gid_t gid; gid_t gid;
long vmrss;
/* from stat */ /* from stat */
pid_t pid; pid_t pid;
@ -36,7 +37,6 @@ struct mu_proc {
unsigned long long starttime; unsigned long long starttime;
unsigned long vsize; unsigned long vsize;
long rss; long rss;
long rsslim;
}; };
int mu_proc_status(const char *prog_name, const pid_t pid, struct mu_proc *proc_s); int mu_proc_status(const char *prog_name, const pid_t pid, struct mu_proc *proc_s);

View File

@ -41,9 +41,9 @@ static int print(const struct mntent *me) {
char used_s[MU_HUMAN_BUF_SIZE + 1]; char used_s[MU_HUMAN_BUF_SIZE + 1];
char avail_s[MU_HUMAN_BUF_SIZE + 1]; char avail_s[MU_HUMAN_BUF_SIZE + 1];
snprintf(total_s, sizeof(total_s), "%s", mu_humansize(total * block, block)); strcpy(total_s, mu_humansize(total * block, block));
snprintf(used_s, sizeof(used_s), "%s", mu_humansize(used * block, block)); strcpy(used_s, mu_humansize(used * block, block));
snprintf(avail_s, sizeof(avail_s), "%s", mu_humansize(avail * block, block)); strcpy(avail_s, mu_humansize(avail * block, block));
printf("%-20s %12s %10s %9s %9s %3jd%% %s\n", me->mnt_fsname, me->mnt_type, total_s, used_s, avail_s, capacity, me->mnt_dir); printf("%-20s %12s %10s %9s %9s %3jd%% %s\n", me->mnt_fsname, me->mnt_type, total_s, used_s, avail_s, capacity, me->mnt_dir);
} }

View File

@ -1,83 +1,49 @@
#include <fcntl.h>
#include <stdio.h> #include <stdio.h>
#include <string.h> #include <string.h>
#include <stdlib.h> #include <stdlib.h>
#include <unistd.h> #include <unistd.h>
#include <dirent.h> #include <dirent.h>
#include <limits.h>
#include <errno.h> #include <errno.h>
#include <ctype.h>
#include <pwd.h> #include <pwd.h>
#include <time.h>
#include "proc_parser.h"
#include "human.h"
static int pscan(const char *pid) { static int pscan(const pid_t pid) {
char path[PATH_MAX + 1]; struct mu_proc proc;
if (mu_proc_parser("ps", pid, &proc))
/* Arguments */
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; return 1;
}
char status[2048]; /* Uid */
ssize_t n = read(fd, status, sizeof(status) - 1); char *name = "unknow";
status[n] = '\0'; struct passwd *pw = getpwuid(proc.uid);
if (pw != NULL)
name = pw->pw_name;
close(fd); /* Time */
unsigned int rtime = (proc.utime + proc.stime) / sysconf(_SC_CLK_TCK);
char *prog = "none"; /* Print */
char *state = "none"; char virt[MU_HUMAN_BUF_SIZE + 1];
char *user = "none"; strcpy(virt, mu_humansize((off_t)proc.vsize, 1024));
/* Parsing */ char rss[MU_HUMAN_BUF_SIZE + 1];
char *token = strtok(status, "\n"); strcpy(rss, mu_humansize((off_t)proc.vmrss * 1024, 1024));
while (token) {
char *val = strchr(token, ':');
if (val == NULL) {
fprintf(stderr, "ps: incorrect %s file\n", path);
return 1;
}
*val = '\0'; 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);
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; return 0;
} }
int main(int argc, char **argv) { int main(int argc, char **argv) {
while (getopt(argc, argv, "") != -1) {
int opt; puts("ps [a] [PID]\n\t-a Print all processes\n");
while ((opt = getopt(argc, argv, "")) != -1) {
puts("ps [PID]");
return 0; return 0;
} }
argv += optind; argv += optind;
argc -= optind; argc -= optind;
printf("PID\tSTATE\tUSER\tCMD\n"); puts(" PID USER PRI NICE VIRT RSS S RTIME CMD");
int ret = 0; int ret = 0;
if (argc == 0) { if (argc == 0) {
@ -88,19 +54,23 @@ int main(int argc, char **argv) {
} }
struct dirent *ep; struct dirent *ep;
while ((ep = readdir(dp)) != NULL) while ((ep = readdir(dp)) != NULL) {
if (atoi(ep->d_name)) pid_t pid = strtoul(ep->d_name, 0L, 10);
if (pscan(ep->d_name)) if (pid)
if (pscan(pid))
ret = 1; ret = 1;
}
closedir(dp); closedir(dp);
} }
else { else {
for (int i = 0; i < argc; i++) for (int i = 0; i < argc; i++) {
if (atoi(argv[i])) pid_t pid = strtoul(argv[i], 0L, 10);
if (pscan(argv[i])) if (pid)
if (pscan(pid))
ret = 1; ret = 1;
}
} }
return ret; return ret;