-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathcppheader.h
More file actions
253 lines (186 loc) · 4.19 KB
/
cppheader.h
File metadata and controls
253 lines (186 loc) · 4.19 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
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
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
//Book : many references taken from the Book.
//Advanced Programming in unix environment
//For more underdstanding Page No. is mentioned along side
//#include<iostream>
#include<string.h>
#include<cstring>
#include<pwd.h>
//#include<stdin>
#include<stdlib.h>
#include<sys/wait.h>
#include<errno.h> //page 14 Book
#include<unistd.h>
#include<stdio.h>
#include<fcntl.h>
#include<sys/types.h>
using namespace std;
int ret;
char cwd[1000];
int count_parameters;
char *HOSTNAME;
char *USER;
char *PS1;
char *HOME;
char *PATH;
// signal in case of ctrl +c
void sigintHandler(int sig_num)
{
signal(SIGINT, sigintHandler);
fflush(stdout);
}
char buffer[255];
char bufferPath[200];
char hostbuffer[100];
void command_set()
{
struct passwd *pwd;
pwd=getpwuid(getuid());
HOME=pwd->pw_dir;
getlogin_r(buffer,255);
USER=buffer;
gethostname(hostbuffer,sizeof(hostbuffer));
HOSTNAME=hostbuffer;
}
//my rc file
void jairc()
{
int fd=open("/home/jeevesh/Desktop/os_iiith/Assignment1/jairc.txt",O_RDONLY);
//char bufferPath[200];
int countChar = read(fd, bufferPath, 200);
bufferPath[countChar] = '\0';
//printf(" eeee %d %s",fd,bufferPath);
close(fd);
char mybuffer[200];
int j=0;
for(int i=8;i<=countChar;i++,j++)
{mybuffer[j]=bufferPath[i];
bufferPath[i]='\0';}
//cout<<mybuffer;
//PATH=bufferPath;
setenv("PATH",mybuffer,1);
//cout<<getenv("PATH");
}
char temp[7]="/home";
void directory_ch(char *args)
{
char *c=temp;
if((strcmp(args,"~")==0) || (strcmp(args,"~/")==0))
chdir(c);
else
chdir(args);
/* char *c="/home";
// char *d="/home/jeevseh";
if(args[0]=='\0')
{
chdir(c);
}
else if((strcmp(args,"~")==0) || (strcmp(args,"~/")==0))
{
chdir(c);
}
/*else if(strcmp(args,".."))
{
char cwda[255];
int i=0,j;
getcwd(cwda,sizeof(cwda));
for(i=1;i<sizeof(cwda);i++)
{
if(cwda[i]=='/')
{
cwda[i]='\0';
break;
}
}
chdir(cwda);
}
else if(strcmp(args,"/"))
{
char jai[255];
jai[0]='\0';
chdir(jai);
}
else
{
char path[255];
char cwd[255];
getcwd(cwd,sizeof(cwd));
for(int i=0;i<sizeof(cwd);i++)
path[i]=cwd[i];
strcat(path,"/");
strcat(path,args);
// cout<<path;
chdir(path);
}
*/
}
int jai(char * l,char **arg)
{
count_parameters=0;
while(*l != '\0')
{
while(*l == ' ' || *l == '\t' || *l == '\n' )
*l++='\0';
*arg=l;
//printf("\t %s \n",*arg);
++count_parameters;
++arg;
while(*l != ' ' && *l != '\t' && *l != '\n' && *l != '\0')
l++;
}
*arg=NULL;
return 0;
}
void jaiexecute(char **argv)
{
pid_t pid;
int status;
if((pid = fork())<0)
{
printf("****Error: forking child process failed\n");
exit(1);
}
else if(pid == 0)
{
if(strcmp(argv[count_parameters-1],"&")==0)
{
argv[count_parameters-1]=NULL;
if((execvp(*argv,argv)) < 0)
{
ret=-1;
printf("***Error: Command Not found\n");
exit(1);
}
}
if((execvp(*argv,argv))<0){
ret=-1;
printf("***Error: Command Not found\n");
exit(1);
}
//else( execvp(*argv,argv) < 0)
//{
///printf("***Error: Command Not found\n");
// exit(1);
//}
}
else{
while(wait(&status)!=pid);
}
}
void promptcall() //To display on prompt
{
char buffer[255];
getlogin_r(buffer,255);
char displayprompt[1001];
if(getcwd(cwd,sizeof(cwd))!=NULL)
{
strcpy(displayprompt,buffer);
strcat(displayprompt,cwd);
strcat(displayprompt,"$ ");
PS1=displayprompt;
printf("%s",displayprompt);
}
else
{
perror("getcwd() error"); // refer perror in Book
}
}