-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmycommands.c
140 lines (124 loc) · 2.41 KB
/
mycommands.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
#include<stdio.h>
#include<stdlib.h>
#include<string.h>
#include<unistd.h>
#include<sys/types.h>
#include<sys/wait.h>
void mykjobs( pid_t pd , int sig)
{
kill(pd , sig);
return;
}
void mykillallbg(int *jbs , int *flagit , int end)
{
int a;
for (a = 0; a < end; a++)
{
if (flagit[a]==1) {
kill(jbs[a] , 9);
}
}
return ;
}
void myfg(pid_t pd)
{
printf("Running process with pid: %d\n",pd );
int status;
waitpid(pd,&status,0);
}
void myjobs(char njbs[][100] , int *jbs ,int *flagit, int end)
{
int i,j=1;
for(i=0;i<end;i++)
{
if(flagit[i]==1)
{
printf("[%d] %s [%d]\n",i , njbs[i] , jbs[i]);
}
}
return ;
}
void mycd(char *path, char *current_directory)
{
if((path==NULL)||(path[0]=='~'))
path=current_directory;
chdir(path);
return;
}
void myecho(char **arguments)
{ int i=1;
char *toprint;
toprint=arguments[i];
while(toprint!=NULL)
{
printf("%s ",toprint);
i++;
toprint = arguments[i];
}
printf("\n");
return;
}
void mypwd()
{
char pwd[1000];
getcwd(pwd,1000);
printf("%s\n",pwd);
return;
}
void check1(char mem[], FILE *fp)
{
int temp =0;
for(temp=0;temp>=0;temp=temp)
{
fgets(mem,100,fp);
if(strstr(mem,"VmPeak") !=NULL)
{
break;
}
}
}
void concatenate(char *a,char *b)
{
strcat(a,b);
}
void mypinfo(char **final)
{
FILE * fp;
char mem[100];
if(final[1] == NULL)
{
char proc_path[1024] = "~/./a.out";
char status[100];
int pid_curr = getpid();
fp = fopen ("/proc/self/status", "r");
char proc_status[100],proc_mem[100],temp4[100],temp5[100];
int temp = 0;
fgets(status,100,fp);
fscanf(fp,"%s %s",status,proc_status);
check1(mem,fp);
fscanf(fp, "%s %s",mem,proc_mem);
printf("Pid : %d\nStatus : %s\nMemory : %s\nExecutable Path : %s\n",pid_curr,proc_status,proc_mem,proc_path);
fclose(fp);
}
else
{
char statf[1024] = "/proc/";
char pathf[1024] = "/proc/";
char proc_status[100],proc_mem[100];
concatenate(statf,final[1]);concatenate(pathf,final[1]);concatenate(statf,"/status");concatenate(pathf,"/cmdline");
size_t buflen = 1024;
char proc_path[buflen];
char status[100];
fp = fopen(pathf,"r");
fgets(proc_path,1024,fp);
fclose(fp);
fp = fopen (statf, "r");
char temp5[100];
fgets(status,100,fp);
fscanf(fp,"%s %s",mem,proc_status);
check1(mem,fp);
fscanf(fp,"%s %s",status,proc_mem);
printf("Pid : %s\nStatus : %s\nMemory : %s\nExecutable Path : %s\n",final[1],proc_status,proc_mem,proc_path);
fclose(fp);
}
}