-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathmain.cpp
More file actions
132 lines (112 loc) · 4.1 KB
/
Copy pathmain.cpp
File metadata and controls
132 lines (112 loc) · 4.1 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
#include "main.h"
using namespace std;
int main(int argc,char *argv[]){
//get enviorenment variables
char *userName = getenv("USER");
char *defaultEditor = getenv("EDITOR");
commandNumber = 0;
//allocate memory for commands
allCommands = (char**) calloc(MAX_COMMAND_NUMBER,sizeof(char*));
for(int i=0;i<MAX_COMMAND_NUMBER;i++){
allCommands[i] = (char*) calloc(MAX_COMMAND_LENGTH,sizeof(char));
}
command = (char*) calloc(MAX_COMMAND_LENGTH,sizeof(char));
//main loop
while(true){
//user signature and get command
printf("%s >>> ",userName);
fgets(command,MAX_COMMAND_LENGTH,stdin);
command[strcspn(command, "\n")] = 0;
//save command to list and increment command number
strcpy(allCommands[commandNumber],command);
commandNumber++;
command = strtok(command," ");
//listdir command
if(!strcmp(command,listdirCmd)){
//open directory and prints all filenames.
DIR *currentDirectory = opendir(".");
dirent *directoryPt;
while ((directoryPt = readdir(currentDirectory)) != NULL){
printf("%s\n",directoryPt->d_name);
}
}
//mycomputername command
else if(!strcmp(command,mycomputernameCmd)){
//get and prints hostname
char hostName[HOST_NAME_MAX];
gethostname(hostName, HOST_NAME_MAX);
printf("%s\n",hostName);
}
//whatsmyipcommmand
else if(!strcmp(command,whatsmyipCmd)){
system("hostname -i");
}
//hellotext command
else if(!strcmp(command,hellotextCmd)){
system(defaultEditor);
}
//printfile commands
else if(!strcmp(command,printfileCmd)){
//get file name
fileName = strtok(NULL," ");
char *redirectionSymbol = strtok(NULL," ");
//control redirection symbol and if it is found execute cp command
if(redirectionSymbol!=NULL && !strcmp(redirectionSymbol, ">")){
char *outFileName = strtok(NULL," ");
char *executedCommand = (char*) calloc(MAX_COMMAND_LENGTH,sizeof(char));
sprintf(executedCommand,"cp %s > %s",fileName,outFileName);
system(executedCommand);
free(executedCommand);
}
//if this is just print a file command open file and print it line by line with enter
else{
FILE *InputFile = fopen(fileName, "r"), *outFile;
//if file couldn't open
if(InputFile == NULL) {
printf("Cannot open %s\n",fileName);
continue;
}
while (fgets(line,MAX_LINE_LENGTH,InputFile)) {
line[strcspn(line, "\n")] = 0;
printf("%s", line);
cin.get();
}
fclose(InputFile);
}
}
//dididothat command: read command to be searched and try to find it in last 15 command.
else if(!strcmp(command,dididothatCmd)){
char *searchCommand = strtok(NULL,"/0");
searchCommand++;
searchCommand[strcspn(searchCommand, "\"")] = 0;
bool isFound = false;
for(int i = commandNumber-1; i > commandNumber - 16 && i>=0; i--){
if(!strcmp(searchCommand,allCommands[i])){
isFound = true;
break;
}
}
if(isFound){
printf("YES\n");
}
else{
printf("NO\n");
}
}
//exit command.
else if(!strcmp(command,exitCmd)){
break;
}
//invalid commands
else{
printf("No command found as %s\n",command);
}
}
//free memory for commands
free(command);
for(int i=0;i<MAX_COMMAND_NUMBER;i++){
free(allCommands[i]);
}
free(allCommands);
return 0;
}