-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathos.c
More file actions
118 lines (114 loc) · 2.56 KB
/
os.c
File metadata and controls
118 lines (114 loc) · 2.56 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
#include "header.h"
int main (){
pname=0;
in = dup(0);
out = dup(1);
char hostname[1024];
hostname[1023] = '\0';
cwd[1023] = '\0';
gethostname(hostname, 1023);//sys_name
struct passwd *hello;
hello=getpwuid(getuid());//user_name
getcwd(cwd,sizeof (cwd));
int cwdlen=strlen(cwd);
int k=20,l;
int fd = creat("pid", 0600);
close(fd);
strcpy (paths,cwd);
strcat(paths,"/pid");
if (signal(SIGINT, handle_signals) == SIG_ERR) {
printf("failed to register interrupts with kernel\n");
}
while ( sigsetjmp( ctrlc_buf, 1 ) );
signal(SIGTSTP, sighandler);
struct sigaction sa;
sigemptyset(&sa.sa_mask);
sa.sa_flags = 0;
sa.sa_handler = child_handler;
sigaction(SIGCHLD, &sa, NULL);
while(1 == 1){
FILE *fp = NULL;
fp = fopen(paths,"r+");
if(!fp){
perror("does not exist");
}
while(1 == 1){
char st[1024],stor[1024];
fgets(st,200,fp);
if(feof(fp) != 0)
break;
if(st[0]=='0')
continue;
int l = strlen(st+1);
char store[1024] = {'\0'};
for(int i=1;st[i]!=' ';i++){
store[i-1] = st[i];
}
int val = calculater(store,strlen(store)+1);
char save[1024] = "/proc/";
strcat(save,store);
strcat(save,"/status");
FILE *fil = fopen(save,"r");
if(!fil){
fprintf(stderr,"\n[%s] has exited\n",store);
fseek(fp,-l-1,SEEK_CUR);
fputc('0',fp);
fseek(fp,(l),SEEK_CUR);
} else {
while(1==1){
fgets(stor,200,fil);
if(feof(fil)) break;
if(strstr(stor,"State") != NULL){
if(strstr(stor,"Z")){
fprintf(stderr,"\n[%s] has exited\n",store);
fseek(fp,-l-1,SEEK_CUR);
fputc('0',fp);
fseek(fp,(l),SEEK_CUR);
break;
}
}
}
fclose(fil);
}
}
fclose(fp);
pwd[1023] = '\0';
getcwd(pwd,sizeof (pwd));
if (strstr(pwd,cwd)!=NULL)
fprintf(stderr,"%s<%s@%s:%s~%s>%s$ ",KGRN,hello->pw_name,hostname,KBLU,pwd+cwdlen,KWHT);
else
fprintf(stderr,"%s<%s@%s:%s%s>%s$ ",KGRN,hello->pw_name,hostname,KBLU,pwd,KWHT);
memset(inp,0,sizeof(inp));
char x=getchar();
if (x==EOF){
printf("\n");
continue;
}
if (x=='\n')
scanf("%[^\n]s",inp);
else{
inp[0]=x;
scanf("%[^\n]s",inp+1);
}
if (strlen(inp)<=0) continue;
if ( inp[strlen(inp)-1]!=';') inp[strlen(inp)]=';';
cnt=0;
char *string=inp;
char *cmd = strchr(string,';');
while (cmd){
memset(input,0,sizeof(input));
*cmd++='\0';
char *temp=string;
if (strlen(temp)==0) continue;
if(!strstr(temp,"|"))
verify_cmd(temp);
else
pipeLoop(temp);
dup2(0,in);
dup2(1,out);
string=cmd;
cmd = strchr (string,';');
}
}
return 0;
}