-
Notifications
You must be signed in to change notification settings - Fork 2
Expand file tree
/
Copy pathoscode.c
More file actions
266 lines (201 loc) · 4.8 KB
/
oscode.c
File metadata and controls
266 lines (201 loc) · 4.8 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
254
255
256
257
258
259
260
261
262
263
264
265
/**
* ---------------------------------------
*
* NAME : SHASHANK SHEKHAR
* ID : 1001767592
* CSE 3320 : OPERATING SYSTEMS
*
* ---------------------------------------
*/
#include <sys/types.h>
#include <stdlib.h>
#include <stdio.h>
#include <unistd.h>
#include <dirent.h>
#include <string.h>
#include <time.h>
#include <curses.h>
#include <sys/stat.h>
#define MAX_LEN 256
#define BIG_MAX_LEN 1024
void init_WelcomePage();
void list_Directories();
void list_FilesInDirectory();
void getAllOperations();
void edit_Directory();
void run_File();
void remove_File();
void move_Directory();
pid_t child;
DIR *d;
struct dirent *de;
int i, c, k;
char s[MAX_LEN], cmd[MAX_LEN],file[MAX_LEN][BIG_MAX_LEN],dir[MAX_LEN][BIG_MAX_LEN*2];
int num;
int choice;
void init_WelcomePage(){
time_t my_time;
my_time = time(NULL);
printf("\t ------------------------------------------------ \n \t\t This is my shell \n\t ------------------------------------------------\n\n\t Your current time : %s\n", ctime(&my_time));
list_Directories();
list_FilesInDirectory();
}
/**
* list_Directories()
* ------------------
*
* 1. Print the path for the current working directory.
* 2. List down all the available directories.
*
*/
void list_Directories(){
getcwd(s, 200);
printf("\t Current Working Directory: %s \n", s);
d = opendir(".");
c = 0;
printf("\t ------------------ \n\t Available Directories\n\t ------------------ \n");
printf("\t Sno. \t\t Name \n\t ------------------------------\n\n");
while ((de = readdir(d)))
{
if ((de -> d_type) & DT_DIR)
{
printf("\t %d \t\t %s \n", c++, de->d_name);
strcpy(dir[c], de -> d_name);
}
}
closedir(d);
}
/**
* list_FilesInDirectory()
* -----------------------
*
* 1. Print all the files in my working directory.
*/
void list_FilesInDirectory(){
d = opendir(".");
c = 0;
printf("\t ------------------ \n\t Available Files\n\t ------------------ \n");
printf("\t Sno. \t\t Name \n\t ------------------------------\n\n");
while ((de = readdir(d))){
if (((de -> d_type) & DT_REG)){
printf("\t %d \t\t %s \n", c++, de->d_name);
strcpy(file[c], de -> d_name);
}
}
closedir(d);
}
/**
* getAllOperations()
* --------------------
*
* 1. List the available commands.
*/
void getAllOperations(){
printf("\n\t ------------------------------ \n\t Available Operations\n\t ------------------------------ \n\n");
printf("\t Sno. \t\t Command \t\t Purpose \n\t ------------------------------------------------------------\n\n");
printf("\t 1. \t\t d \t\t Display \n \t 2. \t\t e \t\t Edit \n \t 3. \t\t r \t\t Run \n \t 4. \t\t c \t\t Change Directory \n \t 5. \t\t m \t\t Move Directory \n \t 6. \t\t f \t\t Remove File \n \t 7. \t\t q \t\t Quit\n\n");
}
/**
* edit_Directory()
* -----------------
*
* 1. Open the file in text editor.
* 2. Default editor : gedit.
* 3. Available alternatives : pico, source, vim, nano, code(VS Code).
*/
void edit_Directory(){
printf("which file you want to edit with?: ");
scanf("%d", &num);
strcpy(s,file[num]);
strcpy(cmd, "gedit ");
strcat(cmd, s);
system(cmd);
}
/**
* run_File()
* ----------
*
* 1. Run the exexcutable file.
* 2. Usage : ./filname
*/
void run_File(){
printf(" (Hint : ./filename) : ");
scanf("%s",cmd);
system(cmd);
}
/**
* change_Directory()
* ------------------
*
* 1. Change the current directory.
*/
void change_Directory(){
printf("Which directory you want to Change?: ");
scanf("%d", &num);
strcpy(cmd, dir[num]);
chdir(cmd);
}
/**
* remove_File()
* -------------
*
* 1. Remove the file from the directory.
*/
void remove_File(){
printf("Directory to remove : ");
scanf("%s", cmd);
rmdir(cmd);
}
/**
* move_Directory()
* -----------------
*
* 1. Move the file from one directory to another.
* 2. USAGE : source_directory, destination_directory.
*/
void move_Directory(){
char src_folder[BIG_MAX_LEN];
char dest_folder[BIG_MAX_LEN];
printf("Source File/Directory : ");
scanf("%s", src_folder);
printf("Destination File/Directory : ");
scanf("%s", dest_folder);
if (rename(src_folder, dest_folder) ){
perror( NULL );
printf("%s moved to : %s . DONE.",src_folder, dest_folder);
}else{
printf("Error in moving the file/directory.");
}
}
int main(void)
{
while (1)
{
init_WelcomePage();
getAllOperations();
d = opendir(".");
choice = 0;
printf(" > ");
choice = getchar();
strcpy(cmd, "");
switch(choice)
{
case 'd' : list_Directories();
break;
case 'e' : edit_Directory();
break;
case 'r' : run_File();
break;
case 'c': change_Directory();
break;
case 'm' : move_Directory();
break;
case 'f' : remove_File();
break;
case 'h' : getAllOperations();
break;
case 'q' : exit(0);
break;
}
}
}