📄 dpmd_programs.c
字号:
#include <ctype.h>#include <dirent.h>#include <errno.h>#include <stdio.h>#include <stdlib.h>#include <string.h>#include <unistd.h>#define PRG_LENGTH 64struct prog_list { char name[PRG_LENGTH]; struct prog_list *next;};static struct prog_list *running_programs = 0L;/* int numeric_entry(const struct dirent *d) * * Select function for scandir() * */static int numeric_entry(const struct dirent *d) { return isdigit(d->d_name[0]);}static int free_programs(void){ struct prog_list *prog = running_programs; while (prog) { free(prog); prog = prog->next; } running_programs = 0L; return 0;}static int insert_programs(char *progname){ struct prog_list *prev, *prog; prev = prog = running_programs; while (prog) { prev = prog; prog = prog->next; } prog = (struct prog_list *)malloc(sizeof(*prog)); if (!prog) { return -1; } memset(prog, 0, sizeof(*prog)); memcpy(prog->name, progname, PRG_LENGTH); if (prev) prev->next = prog; else running_programs = prog; return 0;}int find_program(const char *progname){ struct prog_list *prog = running_programs; while (prog) { if (!strcmp(prog->name, progname)) return 1; prog = prog->next; } return 0;}int print_programs(void){ int i = 0; struct prog_list *prog = running_programs; while (prog) { printf("%d: %s\n", i++, prog->name); prog = prog->next; } return 0;}/* int get_running_programs(void) * * looks for running programs and fills the * global struct running_programs. * * Returns the length of the newly created list. */int get_running_programs(void){ struct dirent **namelist; int n=0, ret=0, n_chars=0; char file[PRG_LENGTH]; char program[PRG_LENGTH]; char *prg_basename; free_programs(); n = scandir("/proc", &namelist, numeric_entry, NULL); if (n < 0) { printf("scandir: %s\n", strerror(errno)); } else { while(n--) { snprintf(file, PRG_LENGTH - 1, "/proc/%s/exe", namelist[n]->d_name); free(namelist[n]); n_chars = readlink(file, program, PRG_LENGTH - 1); if (n_chars < 0) { /* probably this process is a kernel process or * user cannot read the link or * has disappeared while scanning, don't worry */#if 0 printf("%s: %s\n", file, strerror(errno));#endif continue; } /* terminate the string */ program[n_chars] = '\0'; prg_basename = rindex(program, '/'); prg_basename++; if (prg_basename == NULL) prg_basename = program; insert_programs(prg_basename); ret++; } } free(namelist); return ret;}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -