-
Notifications
You must be signed in to change notification settings - Fork 0
/
ps.c
218 lines (192 loc) · 4.93 KB
/
ps.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
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
/*
* Linux PS command simplest implementation
* Created by Muhammad Ilyas email: m_Ilyas@outlook.com
*******************************************************************************
* In order to use this command you have to given an argument in proper format
* Process ID -> ./myps \n
* Process Name -> ./myps -p PID
* Process State -> ./myps -p PID -s
* Process VM Size -> ./myps -p PID -v
* Process CMDLine -> ./myps -p PID -c
* All -> ./myps -p PID -s -v -c
*/
#include <sys/types.h>
#include <sys/stat.h>
#include <sys/stat.h>
#include <fcntl.h>
#include <stdlib.h>
#include <unistd.h>
#include <stdio.h>
#include <string.h>
#include <time.h>
#include <linux/limits.h>
#include <sys/times.h>
#define clear() printf("\033[H\033[J")
void readPID();
void readVMSize(int a,char * argv[]);
void readCMDline(int a,char * argv[]);
void readState(char * argv[]);
void readProcess(char* argv[]);
void HowToUse();
int main(int argc,char *argv[]){
//HowToUse();
/*
cmd line argument check here
*/
int i=1;
if(argv[1] == NULL){
readPID();
}
else if(strcmp(argv[i],"-p") != 0 ){
printf("Please enter -p with Process id i.e (-p 1)\n");
}
else if(argc>1){
//printf("test argc>1\n");
if(argc>1){
/*
option implementation of cmdline argument
*/
while(i!=argc){
// printf("%s\n",argv[i]);
if(strcmp(argv[i],"-p") == 0){
readProcess(argv);
}
else if(strcmp(argv[i],"-s") == 0){
readState(argv);
}
else if(strcmp(argv[i],"-v") == 0){
readVMSize(3,argv);
}
else if(strcmp(argv[i],"-c") == 0){
readCMDline(3,argv);
}
i++;
}
}
}
return 0;
}
/*
read process name form proc
*/
void readProcess(char* argv[]){
char a[100]="/proc/", b[10], c[10]="/comm";
strcat(a,argv[2]);
strcat(a,c);
//strcat(a,c);
FILE *fp;
char buff[255];
fp = fopen(a,"r");
if(!fp){
printf("Process not found\n");
}
else {
fscanf(fp, "%s",buff);
// cle ar();
printf("Process Name: %s \n",buff);
}
}
/*
reading process state form proc
*/
void readState(char * argv[]){
char a[10]="/proc/", c[10]="/status",buff[100],buff2[100];
strcat(a,argv[2]);strcat(a,c);
FILE *fp;
//printf("%s",a);
fp = fopen(a,"r");
if(!fp){
printf("Process not found\n");
}
else {
fgets(buff,100, fp);
fgets(buff2,100,fp);
clear();
}
printf("%s \n",buff);
printf("%s",buff2);
}
/*
reading cmdline from proc/pid/cmdline
*/
void readCMDline(int a,char * argv[]){
char h[50]="/proc/",l[10]="/cmdline",buff[250];
strcat(h,argv[2]);strcat(h,l);
printf("%s\n",h);
FILE *fp;
fp = fopen(h,"r");
if(!fp){
printf("Process not found\n");
}
else {
fgets(buff,255,fp);
// cle ar();
printf("%s \n",buff);
}
fclose(fp);
}
/*
reading process vm size from proc
*/
void readVMSize(int a,char * argv[]){
char h[40]="/proc/",g[10]="/statm",buff[250];
strcat(h,argv[2]);strcat(h,g);
//printf("%s",h);
FILE *fp;
fp = fopen(h,"r");
if(!fp){
printf("Process not found\n");
}
else {
fscanf(fp, "%s",buff);
// cle ar();
printf("Virtual Memory: %s \n",buff);
}
fclose(fp);
}
/*
reading PID && PPID and process name
*/
void readPID(){
char str[15],buff[255],d[50]= "/proc/",a[100]="/proc/", c[10]="/comm";
__pid_t PID = 0,ParentId=0;
PID = getpid();
ParentId = getppid();
printf("PID CMD\n");
printf("%i ",PID);
sprintf(str, "%d", PID);
strcat(a,str);
strcat(a,c);
//strcat(a,c);
//printf("\n%s\n",a);
FILE *fp;
//char buff[255];
fp = fopen(a,"r");
if(!fp){
printf("Process not found\n");
}
else {
fscanf(fp, "%s",buff);
// cle ar();
printf("%s \n",buff);
}
fclose(fp);
printf("%i ",ParentId);
sprintf(str, "%d", ParentId);
strcat(d,str);
strcat(d,c);
//strcat(a,c);
//printf("\n%s\n",d);
//FILE *fp;
//char buff[255];
fp = fopen(d,"r");
if(!fp){
printf("Process not found\n");
}
else {
fscanf(fp, "%s",buff);
// cle ar();
printf("%s \n",buff);
}
fclose(fp);
}