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