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
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))
proc_s->gid = strtoul(val, 0L, 10);
else if (!strncmp(token, "VmRSS", 3))
proc_s->vmrss = strtoul(val, 0L, 10);
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, "%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);
return 0;

View File

@ -10,6 +10,7 @@ struct mu_proc {
char prog[PATH_MAX + 1];
uid_t uid;
gid_t gid;
long vmrss;
/* from stat */
pid_t pid;
@ -36,7 +37,6 @@ struct mu_proc {
unsigned long long starttime;
unsigned long vsize;
long rss;
long rsslim;
};
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 avail_s[MU_HUMAN_BUF_SIZE + 1];
snprintf(total_s, sizeof(total_s), "%s", mu_humansize(total * block, block));
snprintf(used_s, sizeof(used_s), "%s", mu_humansize(used * block, block));
snprintf(avail_s, sizeof(avail_s), "%s", mu_humansize(avail * block, block));
strcpy(total_s, mu_humansize(total * block, block));
strcpy(used_s, mu_humansize(used * 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);
}

View File

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