-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathcommand.c
More file actions
167 lines (153 loc) · 4.35 KB
/
Copy pathcommand.c
File metadata and controls
167 lines (153 loc) · 4.35 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
/* "command.c" - Gabriel Bauer (@ToCodeABluejay)
*
*Copyright (c) 2021 Gabriel Bauer (@ToCodeABluejay)
*
*Permission is hereby granted, free of charge, to any person obtaining a copy
*of this software and associated documentation files (the "Software"), to deal
*in the Software without restriction, including without limitation the rights
*to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
*copies of the Software, and to permit persons to whom the Software is
*furnished to do so, subject to the following conditions:
*
*The above copyright notice and this permission notice shall be included in all
*copies or substantial portions of the Software.
*
*THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
*IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
*FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
*AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
*LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
*OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
*SOFTWARE.
*/
#include "shell.h"
/*iswhitespace() takes in a character, k
*and returns true if it is either a space
*or a tab, and false otherwise
*/
bool iswhitespace(char k){return(k==' '||k=='\t'||k=='\n')?true:false;}
char *getArgv(struct command *k)
{
static char argv[16384] = {0};
int i;
for(i=0;strlen(k->argv[i]); i++)
{
strcat(argv, k->argv[i]);
strcat(argv, " ");
}
return argv;
}
void getCommands(struct command *k)
/*getCommands() is a very simple command
*which gets the next string of commands
*to parse and splits it up into its various
*components using strtok(), and
*builds a data-type command, sets the argv
*data-section equal to the output of the
*previous parts and the argc section equal
*to the number of arguments in argv, and
*then finally sends that command back to
*get executed by execCmd()
*/
{
char cmdl[16384];
fgets(cmdl, 16384, stdin);
long long i=0;
/*Just a quick preliminary measure to make sure
*that on each run, we start on a clean slate
*/
for(int j=0; j<64; j++) strcpy(k->argv[j], "\0");
const char s[3] = " \n";
char *tok;
i=0;
// Get and copy the first arg
tok = strtok(cmdl, s);
strcpy(k->argv[i], tok);
// Continue to get and copy args until end
while (true)
{
i++;
tok = strtok(NULL, s);
if (tok)
{
strcpy(k->argv[i], tok);
//printf("%s", k->argv[i]); //Debuggers...debuggers everywhere, and before you even ask, yes I also use gdb ;)
}
else
break;
}
}
bool ShellCommand(struct command *k)
{
if (!strcmp(k->argv[0], "quit"))
{
exit(0);
return true;
}
else if (!strcmp(k->argv[0], "pwd"))
{
printf("%s\n", get_current_dir_name());
return true;
}
else if (!strcmp(k->argv[0], "cd"))
{
cd(k);
return true;
}
else if (!strcmp(k->argv[0], "ls"))
{
ls(k);
return true;
}
else if (!strcmp(k->argv[0], "help"))
{
printf("Shell - MIT License\nThis shell provides the following internal functions.\n\ncd: Allows you to change the working directory\npwd: Prints the current working directory\nquit: Exits the application\nls: Prints the contents of a given directory\n\t- defaults the the current working directory\nhelp: Prints this dialogue\n");
return true;
}
else
return false;
}
void execCmd(struct command *k)
/*execCmd() takes in a command
*data-type, and executes it
*/
{
//printf("%s", k->argv[0]); //Just a good way to make sure that our tokens have transfered ;)
if(!ShellCommand(k))
{
int c_pid = fork();
if (c_pid < 0)
{
fprintf(stderr, "Failed to create child process! Shell failure");
exit(1);
}
if (!c_pid)
{
char exec[768] = "\0";
//printf("Child PID is %i\n", getpid());
if(k->argv[0][0]!='/'&&strncmp(k->argv[0], "./", 2))
{
strcpy(exec, "/bin/");
strcat(exec, k->argv[0]);
}
else
strcpy(exec, k->argv[0]);
//printf ("[%s]\n", exec); //Just another...useful debugger! ;)
if(execle(exec, getArgv(k), NULL, environ))
{
if (!isnotsu)
{
strcpy(exec, "/sbin/");
strcat(exec, k->argv[0]);
if(execle(exec, getArgv(k), NULL, environ))
fprintf(stderr, "Execution failure! Error: %i\n", errno);
}
}
else if (!isnotsu)
fprintf(stderr, "Execution failure! Error: %i\n", errno);
exit(0);
}
else
wait(&c_pid);
}
}