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

@ -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';
/* Uid */
char *name = "unknow";
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";
char *state = "none";
char *user = "none";
/* Print */
char virt[MU_HUMAN_BUF_SIZE + 1];
strcpy(virt, mu_humansize((off_t)proc.vsize, 1024));
/* 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;
}
char rss[MU_HUMAN_BUF_SIZE + 1];
strcpy(rss, mu_humansize((off_t)proc.vmrss * 1024, 1024));
*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);
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,19 +54,23 @@ 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;