fixed
This commit is contained in:
parent
1b9dc6d7dc
commit
0737916f1a
@ -1,10 +1,9 @@
|
|||||||
# micro-utils
|
# microutils
|
||||||
Own implementation of *nix utils
|
Own implementation of *nix utils
|
||||||
License: wtfpl
|
License: wtfpl
|
||||||
|
|
||||||
https://trivial.technology/
|
https://trivial.technology/
|
||||||
|
|
||||||
|
|
||||||
Unportable:
|
Unportable:
|
||||||
proc_parser
|
proc_parser
|
||||||
ps
|
ps
|
||||||
|
1
TODO
1
TODO
@ -49,5 +49,6 @@ BUGS:
|
|||||||
xargs (getopt with glibc)
|
xargs (getopt with glibc)
|
||||||
|
|
||||||
FIX:
|
FIX:
|
||||||
|
ps (proc_parser.h)
|
||||||
echo (escape)
|
echo (escape)
|
||||||
que (unicode)
|
que (unicode)
|
||||||
|
@ -2,22 +2,23 @@
|
|||||||
|
|
||||||
char mu_hs_buf[MU_HUMAN_BUF_SIZE + 1];
|
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));
|
memset(mu_hs_buf, '\0', sizeof(mu_hs_buf));
|
||||||
|
|
||||||
char *postfixes = "BKMGTPE";
|
char *postfixes = "BKMGTPE";
|
||||||
|
size_t i = 0;
|
||||||
double size = n;
|
double size = n;
|
||||||
size_t i;
|
|
||||||
|
|
||||||
for (i = 0; i < strlen(postfixes) && size >= block; i++)
|
while (size > (off_t)block) {
|
||||||
size /= block;
|
size /= (off_t)block;
|
||||||
|
i++;
|
||||||
|
}
|
||||||
|
|
||||||
if (i)
|
if (i)
|
||||||
snprintf(mu_hs_buf, sizeof(mu_hs_buf), "%.1f%c", size, postfixes[i]);
|
snprintf(mu_hs_buf, sizeof(mu_hs_buf), "%.1f%c", size, postfixes[i]);
|
||||||
|
|
||||||
else
|
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;
|
return mu_hs_buf;
|
||||||
}
|
}
|
||||||
|
@ -7,5 +7,5 @@
|
|||||||
|
|
||||||
#define MU_HUMAN_BUF_SIZE 16
|
#define MU_HUMAN_BUF_SIZE 16
|
||||||
|
|
||||||
char *mu_humansize(off_t n, off_t block);
|
char *mu_humansize(off_t n, int block);
|
||||||
#endif
|
#endif
|
||||||
|
@ -1,6 +1,6 @@
|
|||||||
#include <stdlib.h>
|
#include <stdlib.h>
|
||||||
#include <sys/stat.h>
|
|
||||||
#include <string.h>
|
#include <string.h>
|
||||||
|
#include "parse_mode.h"
|
||||||
|
|
||||||
#define U (S_ISUID | S_IRWXU)
|
#define U (S_ISUID | S_IRWXU)
|
||||||
#define G (S_ISGID | S_IRWXG)
|
#define G (S_ISGID | S_IRWXG)
|
||||||
|
@ -1,7 +1,7 @@
|
|||||||
#ifndef _PARSE_MOUNT_H
|
#ifndef _PARSE_MOUNT_H
|
||||||
#define _PARSE_MOUNT_H
|
#define _PARSE_MOUNT_H
|
||||||
#include <sys/mount.h>
|
|
||||||
#include <stdlib.h>
|
#include <stdlib.h>
|
||||||
|
#include <sys/mount.h>
|
||||||
|
|
||||||
typedef struct {
|
typedef struct {
|
||||||
char *opt;
|
char *opt;
|
||||||
|
@ -44,10 +44,7 @@ int mu_proc_status(const char *prog_name, const pid_t pid, struct mu_proc *proc_
|
|||||||
val++;
|
val++;
|
||||||
|
|
||||||
/* Write values */
|
/* Write values */
|
||||||
if (!strncmp(token, "Name", 4))
|
if (!strncmp(token, "Uid", 3))
|
||||||
strcpy(proc_s->prog, val);
|
|
||||||
|
|
||||||
else if (!strncmp(token, "Uid", 3))
|
|
||||||
proc_s->uid = strtoul(val, 0L, 10);
|
proc_s->uid = strtoul(val, 0L, 10);
|
||||||
|
|
||||||
else if (!strncmp(token, "Gid", 3))
|
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);
|
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);
|
||||||
|
|
||||||
|
/* 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;
|
return 0;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@ -7,7 +7,6 @@
|
|||||||
|
|
||||||
struct mu_proc {
|
struct mu_proc {
|
||||||
/* from status */
|
/* from status */
|
||||||
char prog[PATH_MAX + 1];
|
|
||||||
uid_t uid;
|
uid_t uid;
|
||||||
gid_t gid;
|
gid_t gid;
|
||||||
long vmrss;
|
long vmrss;
|
||||||
|
@ -121,7 +121,7 @@ int main(int argc, char **argv) {
|
|||||||
ret = 1;
|
ret = 1;
|
||||||
|
|
||||||
for (int i = 0; i < argc; i++) {
|
for (int i = 0; i < argc; i++) {
|
||||||
if (!strcmp(proc.prog, argv[i]))
|
if (!strcmp(proc.cmdline, argv[i]))
|
||||||
if (my_kill(pid, signal))
|
if (my_kill(pid, signal))
|
||||||
ret = 1;
|
ret = 1;
|
||||||
}
|
}
|
||||||
|
48
src/ps.c
48
src/ps.c
@ -1,3 +1,4 @@
|
|||||||
|
#include <fcntl.h>
|
||||||
#include <stdio.h>
|
#include <stdio.h>
|
||||||
#include <string.h>
|
#include <string.h>
|
||||||
#include <stdlib.h>
|
#include <stdlib.h>
|
||||||
@ -6,9 +7,34 @@
|
|||||||
#include <errno.h>
|
#include <errno.h>
|
||||||
#include <pwd.h>
|
#include <pwd.h>
|
||||||
#include <time.h>
|
#include <time.h>
|
||||||
|
#include <limits.h>
|
||||||
#include "proc_parser.h"
|
#include "proc_parser.h"
|
||||||
#include "human.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) {
|
static int pscan(const pid_t pid) {
|
||||||
struct mu_proc proc;
|
struct mu_proc proc;
|
||||||
if (mu_proc_parser("ps", pid, &proc))
|
if (mu_proc_parser("ps", pid, &proc))
|
||||||
@ -25,20 +51,32 @@ static int pscan(const pid_t pid) {
|
|||||||
|
|
||||||
/* Print */
|
/* Print */
|
||||||
char virt[MU_HUMAN_BUF_SIZE + 1];
|
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];
|
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;
|
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, "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;
|
return 0;
|
||||||
}
|
}
|
||||||
|
}
|
||||||
|
|
||||||
argv += optind;
|
argv += optind;
|
||||||
argc -= optind;
|
argc -= optind;
|
||||||
|
@ -42,8 +42,7 @@ static char *file_path;
|
|||||||
|
|
||||||
enum {
|
enum {
|
||||||
FAILED_SAVE = 1,
|
FAILED_SAVE = 1,
|
||||||
SUCCESS_SAVE,
|
SUCCESS_SAVE
|
||||||
MARK
|
|
||||||
};
|
};
|
||||||
static char status_type;
|
static char status_type;
|
||||||
static char modified_flag;
|
static char modified_flag;
|
||||||
@ -419,9 +418,6 @@ static void statusBar(const char *path) {
|
|||||||
else if (status_type == SUCCESS_SAVE)
|
else if (status_type == SUCCESS_SAVE)
|
||||||
ret = snprintf(info, sizeof(info), "File %s success saved", path);
|
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;
|
status_type = 0;
|
||||||
bufAppend(info, ret);
|
bufAppend(info, ret);
|
||||||
}
|
}
|
||||||
|
Loading…
Reference in New Issue
Block a user