-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathbuiltin.c
More file actions
192 lines (171 loc) · 2.67 KB
/
Copy pathbuiltin.c
File metadata and controls
192 lines (171 loc) · 2.67 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
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
#include "header.h"
extern char *dir;
int make_int(char * str);
//PWD
int pwd(char **args)
{
//getcwd
char *cwd;
char home[20]="myshell";
char absoluteDir[200];
getcwd(absoluteDir,200);
cwd=strstr(absoluteDir, home);
//if cwd=home
if(strcmp(cwd,home)==0)
strcpy(cwd,"home");
else
{
char *temp=strtok(cwd ,home);
strcpy(cwd,"home");
cwd=strcat(cwd,temp);
}
printf("/%s\n",cwd);
return 1;
}
//END PWD
//ECHO
int echo(char **args)
{
int position=1;
char *word=args[position];
for(;(word!=NULL);)
{
printf("%s",word);
position++;
word=args[position];
}
printf("\n");
return 1;
}
//END ECHO
//CD
int cd(char **args)
{
//no argument
if(args[1]==NULL)
{
printf("Usage: cd dirName\n");
}
//cd
else
{
if(!(chdir(args[1])))
{
getdirectory();
}
else
fprintf(stderr,"error\n");
}
return 1;
}
//END CD
//PINFO
int pinfo(char **args)
{
printf("Pid: %s\n",args[1] );
if(args[1]!=NULL)
{
char infoFile[50],line[100],temp;
int r,file,j=0;
strcpy(infoFile,"/proc/");
strcat(infoFile,args[1]);
strcat(infoFile,"/status");
if((file=open(infoFile,O_RDONLY))!=-1)
{
while((r=read(file,&temp,sizeof(char)))!=0)
{
if(temp!='\n')
{
line[j]=temp;
j++;
}
else
{
if (strstr(line,"State")!=NULL)
printf("%s\n",line );
else if (strstr(line,"VmSize")!=NULL)
printf("%s\n",line );
/*else if (strstr(line,"State")!=NULL)
printf("%s\n",line );*/
memset(line,0,sizeof(line));
j=0;
}
}
}
//fclose(file);
//executable file
strcpy(infoFile,"/proc/");
strcat(infoFile,args[1]);
strcat(infoFile,"/cmdline");
//fclose(file);*/
FILE* fileex = fopen(infoFile, "r");
if(fileex)
{
printf("Executable file: ");
while((temp = getc(fileex)) != EOF)
{
putchar(temp);
}
fclose(fileex);
}
printf("\n");
}
}
//END PINFO
//JOBS
extern running_jobs *rjobs;
int jobs()
{
running_jobs *curr=rjobs;
int i;
for (i=1;curr!=NULL;curr=curr->next,i++) {
printf("[%d] %s [%d]\n",i,curr->jobName,curr->pid);
}
return 1;
}
//END JOBS
//FG
int fg(char ** args)
{
pid_t pid=make_int(args[1]),wpid;
int status;
do{
wpid=waitpid(pid, &status,WUNTRACED);
}while(!WIFEXITED(status) && !WIFSIGNALED(status));
}
int make_int(char * str)
{
int i=0,num=0;
for(;str[i]!='\0';i++)
{
num=num*10+(str[i]-'0');
}
return num;
}
//END FG
//KILLALLBG
int killallbg()
{
running_jobs *curr=rjobs;
for(;curr!=NULL;curr=curr->next)
{
pid_t pid=curr->pid;
kill(pid,SIGQUIT);
}
}
//KILLALLBG
//QUIT
int quit()
{
exit(0);
return 0;
}
//END QUIT
//kjob
int kjob(char **args)
{
pid_t pid=make_int(args[1]);
int sig=make_int(args[2]);
return kill(pid,sig);
}
//end kjobs