fixed ps, readlink

This commit is contained in:
Your Name 2024-07-05 19:54:49 +03:00
parent ec99d14ad6
commit 5d24364332
6 changed files with 96 additions and 66 deletions

2
TODO
View File

@ -49,6 +49,6 @@ BUGS:
xargs (getopt with glibc) xargs (getopt with glibc)
FIX: FIX:
ps ps (proc_parser.h)
echo (escape) echo (escape)
que (unicode) que (unicode)

View File

@ -0,0 +1,16 @@
#ifndef _PROC_PARSER
#define _PROC_PARSER
#include <stdio.h>
#include <string.h>
#include <unistd.h>
#include <stdlib.h>
#include <pwd.h>
struct {
char *cmdline;
char state;
long nice;
} mu_proc;
#endif

View File

@ -26,7 +26,7 @@ int main(int argc, char **argv) {
for (int i = 0; i < argc; i++) { for (int i = 0; i < argc; i++) {
char path[PATH_MAX + 1]; char path[PATH_MAX + 1];
ssize_t r = readlink(argv[i], path, sizeof(path)); ssize_t r = readlink(argv[i], path, sizeof(path) - 1);
if (r < 0) { if (r < 0) {
fprintf(stderr, "readlink: %s: %s\n", argv[i], strerror(errno)); fprintf(stderr, "readlink: %s: %s\n", argv[i], strerror(errno));
return 1; return 1;
@ -37,6 +37,8 @@ int main(int argc, char **argv) {
return 1; return 1;
} }
path[r] = '\0';
if (n_flag) if (n_flag)
fputs(path, stdout); fputs(path, stdout);

View File

@ -1,3 +0,0 @@
#!/bin/sh
project_dir=$(pwd)
echo ./*.c $CFLAGS -o $OUTPUT$(basename $project_dir) | xargs $CC

View File

@ -1,24 +0,0 @@
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <unistd.h>
void print(const char *buf, ssize_t len) {
if (write(STDOUT_FILENO, buf, len) < 0)
exit(1);
}
int main(const int argc, const char **argv) {
if (argc == 1)
while (1)
print("y\n", 2);
while (1) {
for (ssize_t i = 1; i < argc; i++) {
print(argv[i], strlen(argv[i]));
print(" ", 1);
}
print("\n", 1);
}
}

View File

@ -6,63 +6,102 @@
#include <dirent.h> #include <dirent.h>
#include <limits.h> #include <limits.h>
#include <errno.h> #include <errno.h>
#include <ctype.h>
int open_file(const char *path) { #include <pwd.h>
int fd = open(path, O_RDONLY);
if (fd < 0) {
fprintf(stderr, "ps: %s: %s\n", path, strerror(errno));
return -1;
}
return fd;
}
int pscan(const char *pid) { int pscan(const char *pid) {
char path[PATH_MAX + 1]; char path[PATH_MAX + 1];
char cmdline[PATH_MAX + 1];
/* Arguments */ /* Arguments */
snprintf(path, sizeof(path), "/proc/%s/cmdline", pid); snprintf(path, sizeof(path), "/proc/%s/status", pid);
int fd = open_file(path); int fd = open(path, O_RDONLY);
if (fd == -1) if (fd < 0) {
fprintf(stderr, "ps: %s: %s\n", path, strerror(errno));
return 1; return 1;
}
char status[2048];
ssize_t n = read(fd, status, sizeof(status) - 1);
status[n] = '\0';
read(fd, cmdline, sizeof(cmdline));
close(fd); close(fd);
printf("%5s %5s\n", pid, cmdline); 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));
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) {
int opt; int opt;
while ((opt = getopt(argc, argv, "o:")) != -1) { while ((opt = getopt(argc, argv, "")) != -1) {
switch (opt) { puts("ps [PID]");
case 'o': return 0;
break;
default:
printf("ps [o] [PID]\n\t-o Options (nice, tty, cmd, pid, nice)\n");
return 0;
}
} }
DIR *dp = opendir("/proc"); argv += optind;
if (dp == NULL) { argc -= optind;
fprintf(stderr, "ps: /proc: %s\n", strerror(errno));
return 1;
}
printf("PID CMD\n"); printf("PID\tSTATE\tUSER\tCMD\n");
int ret = 0; int ret = 0;
struct dirent *ep; if (argc == 0) {
while ((ep = readdir(dp)) != NULL) DIR *dp = opendir("/proc");
if (atoi(ep->d_name)) if (dp == NULL) {
if (pscan(ep->d_name)) fprintf(stderr, "ps: /proc: %s\n", strerror(errno));
ret = 1; return 1;
}
struct dirent *ep;
while ((ep = readdir(dp)) != NULL)
if (atoi(ep->d_name))
if (pscan(ep->d_name))
ret = 1;
closedir(dp);
}
else {
for (int i = 0; i < argc; i++)
if (atoi(argv[i]))
if (pscan(argv[i]))
ret = 1;
}
closedir(dp);
return ret; return ret;
} }