-
Notifications
You must be signed in to change notification settings - Fork 0
/
sshell.c
79 lines (71 loc) · 1.32 KB
/
sshell.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
#include <stdlib.h>
#include <string.h>
#include <stdio.h>
#include <unistd.h>
#include <sys/wait.h>
#include <dirent.h>
#include "main.h"
/**
* main - a simple shell program
* @av: an array of input parameters
* @ac: number of input parameters from the terminal
* @env: the environment variable
*
* Return: does not return
*/
int main(int ac, char **av, char **env)
{
char *buffer;
char **argv;
char *bufcpy;
char *fn;
size_t len = 0;
char *token;
pid_t id;
buffer = (char *)malloc(1024);
env = NULL;
if (ac != 1)
{
perror("");
}
bufcpy = (char *)malloc(1024);
/* never ending loop to keep the shell always active*/
while (1)
{
/*fn[50] = "ls";
fn = ch_fn(fn);*/
printf("#cisfun$ ");
getline(&buffer, &len, stdin);
strcpy(bufcpy, buffer);
token = strtok(buffer, " ");
/* execute the shell program in the child process*/
while (token != NULL)
{
token = strtok(NULL, " ");
i++;
}
free(buffer);
argv = buffer_split(i, bufcpy);
free(bufcpy);
argv[0] = fn;
id = fork();
int i = 0, check;
/* only child proces would enter this loop*/
if (id == 0)
{
check = execve(argv[0], argv, env);
if (check == -1)
{
dprintf(STDERR_FILENO, "%s: ", av[0]);
perror("");
exit(99);
}
}
else
{
wait(NULL);
/*release_strings(*argv);*/
/*free(argv);*/
}
}
}