mirror of
https://github.com/clockworkpi/Menu.git
synced 2026-03-23 12:22:46 +01:00
first commit
This commit is contained in:
43
60_Utils/02_Bean/beans/shell/utils/pid.bean
Executable file
43
60_Utils/02_Bean/beans/shell/utils/pid.bean
Executable file
@@ -0,0 +1,43 @@
|
||||
|
||||
/*
|
||||
slow
|
||||
*/
|
||||
func find_pid_by_cmdline(cmdline. char) int {
|
||||
import rt::c;
|
||||
alias string = std::string,
|
||||
strings = std::strings;
|
||||
var dir void = opendir("/proc");
|
||||
defer closedir(dir);
|
||||
using ent. struct_dirent {
|
||||
while (ent = readdir(dir)) ~= null {
|
||||
if ent.d_type == DT_DIR {
|
||||
if ent.d_name[0] == '.' and !ent.d_name[1] or
|
||||
ent.d_name[1] == '.' and !ent.d_name[2] {
|
||||
continue;
|
||||
}
|
||||
if isnumeric(&ent.d_name) {
|
||||
lambda pread(cmd. char, lines. strings) bool [
|
||||
var stream void = popen(cmd, "r");
|
||||
if stream == null;
|
||||
return false;
|
||||
defer fclose(stream);
|
||||
var line(0x200) char;
|
||||
while fgets(&line, 0x200, stream) ~= null;
|
||||
lines->add(&line);
|
||||
return true;
|
||||
];
|
||||
static cmd string;
|
||||
static lines strings;
|
||||
lines->clear();
|
||||
if pread(cmd->format("sudo cat /proc/%s/cmdline", &ent.d_name), &lines) {
|
||||
if !lines->empty() {
|
||||
if !strcmp(lines->first_item(), cmdline);
|
||||
return atoi(&ent.d_name);
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
return -1;
|
||||
}
|
||||
Reference in New Issue
Block a user