-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathshell.c
More file actions
126 lines (116 loc) · 3.29 KB
/
Copy pathshell.c
File metadata and controls
126 lines (116 loc) · 3.29 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
#include <stdio.h>
#include <sys/types.h>
#include <unistd.h>
#include <string.h>
#include <sys/wait.h>
#include <stdlib.h>
#define clear() printf("\033[H\033[J")
#define size 1024
void parse(char *line, char **argv)
{
while (*line != '\0')
{ /* if not the end of line ....... */
while (*line == ' ' || *line == '\t' || *line == '\n')
*line++ = '\0'; /* replace white spaces with \0 */
*argv++ = line; /* save the argument position */
while (*line != '\0' && *line != ' ' &&
*line != '\t' && *line != '\n')
line++; /* skip the argument until ... */
}
*argv = '\0'; /* mark the end of argument list */
}
void cd_case(char **argv)
{
if (argv[1] == NULL || *argv[1] == '~')
{
const char *home = getenv("HOME");
if (home != NULL)
{
if (chdir(home) == 0)
return;
else
perror("cd");
}
else
fprintf(stderr, "cd:Home environment variable is not set\n");
}
else
{
if (chdir(argv[1]) == 0)
return;
else
perror("CD");
}
}
void execute(char **argv)
{
pid_t pid;
int status;
if ((pid = fork()) < 0)
{ /* fork a child process */
printf("*** ERROR: forking child process failed\n");
exit(1);
}
else if (pid == 0)
{ /* for the child process: */
if (execvp(*argv, argv) < 0)
{ /* execute the command */
perror("Command error");
exit(1);
}
}
else
{ /* for the parent: */
while (wait(&status) != pid) /* wait for completion */
;
}
}
void command_input(char *line) {
int i = 0;
char character;
while ((character = getchar()) != EOF && i < size - 1) {
if (character == '\n') {
break; // Stop reading at newline
}
line[i] = character;
i++;
}
line[i] = '\0'; // Null-terminate the input
}
void main(void)
{
char line[size]; /* the input line */
char *argv[64]; /* the command line argument */
// clear();
while (1)
{ /* repeat until done .... */
char *username = getenv("USER");
char *dir = getcwd(NULL, 0);
printf("\033[0;32m%s\033[0m:\033[0;34m%s\033[0m$ ", username, dir); /* display a prompt */
command_input(line); /* read in the command line */
size_t len = strlen(line);
if (len > 0 && line[len - 1] == '\n') // remove new line from the end of input
{
line[len - 1] = '\0';
}
parse(line, argv); /* parse the line */
if (argv[0] == NULL)
continue; // Handle empty input
if (strcmp(argv[0], "exit") == 0) /* is it an "exit"? */
exit(0); /* exit if it is */
else if (strcmp(argv[0], "cd") == 0)
{
cd_case(argv);
}
else if (strcmp(argv[0], "clear") == 0)
{
execute(argv);
continue;
}
else
execute(argv); /* otherwise, execute the command */
printf("\n");
free(dir);
}
free(argv);
}