This repository has been archived by the owner on Jan 31, 2024. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 0
/
shell.c
152 lines (116 loc) · 2.49 KB
/
shell.c
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
/**
* written by Miradil Zeynalli
* as Final Project from
* CSCI 3614 - Systems Programming
*
* 17.05.2017
*
*/
/* INCLUDES */
#include <stdio.h>
#include <string.h>
#include <stdlib.h>
#include <fcntl.h>
#include <sys/types.h>
#include <unistd.h>
#include <sys/stat.h>
#include <sys/wait.h>
#include <signal.h>
#include <sys/types.h>
#include <errno.h>
/* END OF INCLUDES */
/* DEFINES */
#define LINE_LEN 100
#define MAX_COMMANDS 20
#define MAX_ARGS 30
#define DELIMITERS " \r\n\0\t"
/* END OF DEFINES */
/* STRUCTURES */
typedef struct
{
char** line;
char** how;
}command_t;
/* END OF STRUCTURES */
/* GLOBAL VARIABLES */
command_t* cmd;
/* END OF GLOBAL VARIABLES */
/* FUNCTION PROTOTYPES */
void initStruct(); //initialize struct
void printPrompt(); //print prompt message
void readLine(char* buff); //get input line
void parseLine(char* line); //parse line into command and arguments
char* lookupPath(char **, char **);
int parsePath(char **);
void exec();
int executeFileInCommand(char *, char **, char *);
int executeFileOutCommand(char *, char **, char *);
void executePipedCommand(char **, char **, char *, char *);
/* END OF FUNCTION PROTOTYPES */
int main()
{
char* line = calloc(LINE_LEN, sizeof(char));
while(1)
{
initStruct();
printPrompt();
readLine(line);
parseLine(line);
exec(0);
}
exit(0);
}
void initStruct()
{
cmd = calloc(MAX_COMMANDS, sizeof(*cmd));
for(int i = 0; i < MAX_COMMANDS; i++)
{
cmd[i].line = calloc(MAX_ARGS, sizeof(char*));
cmd[i].how = calloc(2, sizeof(char));
}
}
void printPrompt()
{
printf("in the nut shell > ");
}
void readLine(char* buff)
{
fgets(buff, LINE_LEN, stdin);
if(!(strcmp(buff, "exit")) || (!strcmp(buff, "quit")))
exit(0);
}
void parseLine(char* line)
{
//printf("parsed one is = %s\n", line);
int i = 0, j = 0;
char* temp;
cmd[i].line[j] = strtok(line, DELIMITERS);
while(cmd[i].line[j] != NULL)
{
temp = strtok(NULL, DELIMITERS);
if(strcmp(temp, "&") || strcmp(temp, "&&") || strcmp(temp, "||") || strcmp(temp, "|")
|| strcmp(temp, ">") || strcmp(temp, ">>")) cmd[i].line[++j] = temp;
else
cmd[i++].how = temp;
}
}
void exec(int in)
{
pid_t pid;
int st; //status
//printf("command is = %s\n", *cmd[in].line);
if((pid = fork()) < 0)
{
perror("Fork failed!\n");
exit(1);
}
else if(!pid)
{
if(execvp(*(cmd[in].line), cmd[in].line) < 0)
{
perror("Execution Failed!\n");
exit(1);
}
}
else wait(&st);
}