fixed
This commit is contained in:
parent
1b9dc6d7dc
commit
0737916f1a
17
README.md
17
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
|
||||
|
1
TODO
1
TODO
@ -49,5 +49,6 @@ BUGS:
|
||||
xargs (getopt with glibc)
|
||||
|
||||
FIX:
|
||||
ps (proc_parser.h)
|
||||
echo (escape)
|
||||
que (unicode)
|
||||
|
@ -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;
|
||||
}
|
||||
|
@ -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
|
||||
|
@ -1,6 +1,6 @@
|
||||
#include <stdlib.h>
|
||||
#include <sys/stat.h>
|
||||
#include <string.h>
|
||||
#include "parse_mode.h"
|
||||
|
||||
#define U (S_ISUID | S_IRWXU)
|
||||
#define G (S_ISGID | S_IRWXG)
|
||||
|
@ -1,7 +1,7 @@
|
||||
#ifndef _PARSE_MOUNT_H
|
||||
#define _PARSE_MOUNT_H
|
||||
#include <sys/mount.h>
|
||||
#include <stdlib.h>
|
||||
#include <sys/mount.h>
|
||||
|
||||
typedef struct {
|
||||
char *opt;
|
||||
|
@ -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;
|
||||
}
|
||||
|
||||
|
@ -7,7 +7,6 @@
|
||||
|
||||
struct mu_proc {
|
||||
/* from status */
|
||||
char prog[PATH_MAX + 1];
|
||||
uid_t uid;
|
||||
gid_t gid;
|
||||
long vmrss;
|
||||
|
@ -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;
|
||||
}
|
||||
|
50
src/ps.c
50
src/ps.c
@ -1,3 +1,4 @@
|
||||
#include <fcntl.h>
|
||||
#include <stdio.h>
|
||||
#include <string.h>
|
||||
#include <stdlib.h>
|
||||
@ -6,9 +7,34 @@
|
||||
#include <errno.h>
|
||||
#include <pwd.h>
|
||||
#include <time.h>
|
||||
#include <limits.h>
|
||||
#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;
|
||||
|
@ -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);
|
||||
}
|
||||
|
Loading…
Reference in New Issue
Block a user