-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathlist_sub_file.c
165 lines (116 loc) · 4.52 KB
/
list_sub_file.c
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
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
#include <stdio.h>
#include <ctype.h>
#include <dirent.h>
#include <string.h>
#include <stdlib.h>
#include "Numeric_checker.c"
/*
int check(char string[]){
int length = strlen(string);
//int test_i;
//printf("%d\n", length);
for(int walk = 0; walk <= length; walk++){
//test_i = isdigit(string[walk]);
//printf("walk: %d char: %c test_i: %d\n", walk, string[walk], test_i);
if(isdigit(string[walk]) == 0 && string[walk] != '\0'){
return 0;
}
}
return 1;
}
*/
typedef struct node{
int pid;
int ppid;
char final[100];
int vsize;
int visted;
struct node *sub_p;
} node;
struct node* print_process_tree(struct node * rt, int tab, struct node* head) {
struct node* temp = head;
while (temp != NULL) {
if(temp->visted == 0){
//printf("tempppid<%d>, rtpid<%d>\n", temp->ppid, rt->pid);
if(temp->ppid == 0){ // base node
temp->visted = 1;
tab = 0;
//get rid of the '(' at the start or each string and print the nessceary info from each node
int length = strlen(temp->final) - 2;
printf("(%d) %.*s, %d kb\n",
temp->pid, length, temp->final + 1, temp->vsize);
//go find any other node with ppid 0
print_process_tree(temp, tab, head);
//look for all the children of this node
return print_process_tree(temp, tab + 1, head);
}
//find all the parents children
if(temp->ppid == rt->pid){
temp->visted = 1;
//find all the childs(now new parents) chidlren
print_process_tree(rt, tab, head);
// this just adds the approriate number of tabs for the given line
for(int j = 0; j < tab +1 ; j++)
printf(" ");
//get rid of the '(' at the start or each string and print the nessceary info from each node
int length = strlen(temp->final) - 2;
printf("(%d) %.*s, %d kb\n",
temp->pid, length, temp->final + 1, temp->vsize);
tab++;
return print_process_tree(temp, tab + 1, head);
}
}
//move to the next node in the linked list
temp = temp->sub_p;
}
// return your root node
return rt;
}
int main(void)
{
struct dirent *de;
DIR *dr = opendir("/proc");
struct node *head = NULL;
struct node *n_prev = NULL;
while ((de = readdir(dr)) != NULL){
if(check(de->d_name) >= 1){ // make sure each directory only has numbers in the name
char file_n[] = "/proc/"; //make a string with the file you want to open
strcat(file_n, de->d_name);
strcat(file_n, "/stat");
FILE *fp; //create a file data type so you can pass somthing to fopen
//info on each file
int pid; //this files pid
int ppid; // this files ppid
char executable[100]; //this files name of its executable
int Vsize; // this files vsize
fp= fopen(file_n, "r"); // open and read from file
if(fp == NULL){
}
else{
fscanf(fp, "%d %s %*s %d %*s %*s %*s %*s %*s %*s %*s %*s %*s %*s %*s %*s %*s %*s %*s %*s %*s %*s %d", &pid, executable, &ppid, &Vsize);
fclose(fp);
}
//make nodes for trees
struct node *n_new = malloc(sizeof(struct node));
// add all member info for each node
n_new->pid = pid;
n_new->ppid = ppid;
n_new->vsize = Vsize;
n_new->visted = 0; // a variable to track to see if recursion has already printed said node
strcpy(n_new->final, executable);
n_new->sub_p = NULL;
//make a ll of nodes
if (head == NULL)
head = n_new;
else
n_prev->sub_p = n_new;
// Save nodes addy
n_prev = n_new;
}
}
//create a root node to pass to recursive funciton make it null to start
struct node *rt = NULL;
print_process_tree(rt, 0, head);// recurisive print call
closedir(dr);
return 0;
}