forked from radiopushka/pmmanager
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathlaunched.c
More file actions
100 lines (89 loc) · 2.25 KB
/
launched.c
File metadata and controls
100 lines (89 loc) · 2.25 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
#include<stdio.h>
#include<string.h>
#include<dirent.h>
#include<stdlib.h>
#include<ctype.h>
struct Node{
char* name;
int head;
struct Node* next;
};//linked list
struct Node* head;
void setmem(){
head=(struct Node*)malloc(sizeof(struct Node));
head->head=1;//create the linked list
}
void add(char* str){
struct Node* next=(struct Node*)malloc(sizeof(struct Node));
next->name=malloc(sizeof(char)*strlen(str));
strcpy(next->name,str);
next->next=head;
head->head=-1;
head=next;//add element
}
int gothrough(struct Node* in,char* look){//go through the list to find "look"
//in=in->next;
while(in->name!=NULL){
if(strcmp(in->name,look)==0){
return 1;
}
in=in->next;
}
return -1;
}
void close(){
free(head);
}
char* getprocessnamefromid(char* id){//function to get the name of the process from id
char *path=malloc(sizeof(char)*(strlen(id)+strlen("/proc/status/")));//allocate the string to hold the path
strcpy(path,"/proc/");// /proc/<ps id>/status
strcat(path,id);
strcat(path,"/status");//finished creating the path
FILE *f=fopen(path,"r");//open the file
free(path);
if(f==NULL){//check for failure to open process file, the process exited too fast
char* strbk="<exited>";
char *retpoint=malloc(sizeof(char)*strlen(strbk));
strcpy(retpoint,strbk);
return retpoint;
}
fseek(f,6,SEEK_SET);//file starts with "Name: ", we don't want that
char* line=NULL;
size_t bytebuff=0;
getline(&line,&bytebuff,f);//read the first line with the name
fclose(f);
line[strlen(line)-1]='\0';//omit \n
return line;
}
void showNotInList(char* filter){
DIR *d=opendir("/proc");
struct dirent *dd;
char* proc;
while((dd=readdir(d))!=NULL){
proc=dd->d_name;
if(isdigit(proc[0])){
char* namedisp=getprocessnamefromid(proc);
if(gothrough(head,namedisp)==-1&&!strstr(namedisp,filter)){
printf("NEW: %s %s\n",proc,namedisp);
add(namedisp);
}
free(namedisp);
}
}
closedir(d);
}
void intake(){//grab all processes before as refernce to not print them only print new ones
setmem();
DIR *d=opendir("/proc");
struct dirent *dd;
char* proc;
while((dd=readdir(d))!=NULL){
proc=dd->d_name;
if(isdigit(proc[0])){
char* namedisp=getprocessnamefromid(proc);
add(namedisp);
free(namedisp);
}
}
closedir(d);
}