Skip to content

Commit bb2fd3f

Browse files
committed
master
1 parent 6668c87 commit bb2fd3f

File tree

9 files changed

+329
-0
lines changed

9 files changed

+329
-0
lines changed

arg_handle.c

+28
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,28 @@
1+
#include "shell.h"
2+
3+
4+
/**
5+
* main - Entry point for a shell that handles command arguments.
6+
*
7+
* Return: Exit status.
8+
*/
9+
10+
int argument_handling() {
11+
char *buffer = NULL;
12+
size_t bufsize = 0;
13+
char **args;
14+
15+
while (1) {
16+
printf("$ ");
17+
getline(&buffer, &bufsize, stdin);
18+
19+
args = parse_input(buffer);
20+
execute_command(args);
21+
22+
free(args);
23+
}
24+
25+
free(buffer);
26+
return 0;
27+
}
28+

bas_com.c

+31
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,31 @@
1+
#include "shell.h"
2+
3+
4+
/**
5+
* main - Entry point for a basic shell that executes single commands.
6+
*
7+
* Return: Exit status.
8+
*/
9+
10+
11+
int basic_command_execution(){
12+
char *buffer = NULL;
13+
size_t bufsize = 0;
14+
char *args[2];
15+
16+
while (1) {
17+
printf("$ ");
18+
getline(&buffer, &bufsize, stdin);
19+
20+
buffer[strcspn(buffer, "\n")] = 0;
21+
if (strlen(buffer) == 0) continue;
22+
23+
args[0] = buffer;
24+
args[1] = NULL;
25+
26+
execute_command(args);
27+
}
28+
29+
free(buffer);
30+
return 0;
31+
}

env.c

+42
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,42 @@
1+
#include "shell.h"
2+
3+
4+
/**
5+
* main - Entry point for a shell that can display environment variables.
6+
*
7+
* Return: Exit status.
8+
*/
9+
10+
11+
extern char **environ;
12+
13+
int environment_builtin_command() {
14+
char *buffer = NULL;
15+
size_t bufsize = 0;
16+
char **args;
17+
char **env;
18+
19+
while (1) {
20+
printf("$ ");
21+
getline(&buffer, &bufsize, stdin);
22+
23+
args = parse_input(buffer);
24+
25+
if (strcmp(args[0], "exit") == 0) {
26+
free(args);
27+
break;
28+
} else if (strcmp(args[0], "env") == 0) {
29+
for (env = environ; *env != NULL; env++) {
30+
printf("%s\n", *env);
31+
}
32+
} else {
33+
execute_command(args);
34+
}
35+
36+
free(args);
37+
}
38+
39+
free(buffer);
40+
return 0;
41+
}
42+

exit.c

+34
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,34 @@
1+
#include "shell.h"
2+
3+
4+
/**
5+
* main - Entry point for a shell that implements the 'exit' command.
6+
*
7+
* Return: Exit status.
8+
*/
9+
10+
11+
int exit_builtin_command() {
12+
char *buffer = NULL;
13+
size_t bufsize = 0;
14+
char **args;
15+
16+
while (1) {
17+
printf("$ ");
18+
getline(&buffer, &bufsize, stdin);
19+
20+
args = parse_input(buffer);
21+
22+
if (strcmp(args[0], "exit") == 0) {
23+
free(args);
24+
break;
25+
}
26+
27+
execute_command(args);
28+
free(args);
29+
}
30+
31+
free(buffer);
32+
return 0;
33+
}
34+

hsh

17.2 KB
Binary file not shown.

main.c

+64
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,64 @@
1+
#include "shell.h"
2+
#include <stdio.h>
3+
#include <stdlib.h>
4+
#include <string.h>
5+
6+
extern char** environ;
7+
8+
int main(int argc, char *argv[]) {
9+
char *input;
10+
size_t input_size = 0;
11+
size_t input_length;
12+
13+
if (argc > 1) {
14+
15+
FILE *input_file = fopen(argv[1], "r");
16+
if (input_file == NULL) {
17+
perror("Error opening input file");
18+
return EXIT_FAILURE;
19+
}
20+
21+
input = NULL;
22+
23+
while (getline(&input, &input_size, input_file) != -1) {
24+
25+
input_length = strlen(input);
26+
if (input_length > 0 && input[input_length - 1] == '\n') {
27+
input[input_length - 1] = '\0';
28+
}
29+
30+
31+
printf("You entered: %s\n", input);
32+
}
33+
34+
free(input);
35+
fclose(input_file);
36+
} else {
37+
38+
input = NULL;
39+
40+
while (1) {
41+
printf("($) ");
42+
getline(&input, &input_size, stdin);
43+
44+
45+
input_length = strlen(input);
46+
if (input_length > 0 && input[input_length - 1] == '\n') {
47+
input[input_length - 1] = '\0';
48+
}
49+
50+
if (strcmp(input, "exit") == 0) {
51+
free(input);
52+
exit(0);
53+
}
54+
55+
56+
printf("You entered: %s\n", input);
57+
}
58+
59+
free(input);
60+
}
61+
62+
return 0;
63+
}
64+

path_handle.c

+28
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,28 @@
1+
#include "shell.h"
2+
3+
4+
/**
5+
* main - Entry point for a shell that handles command arguments.
6+
*
7+
* Return: Exit status.
8+
*/
9+
10+
int environment_variable_handling() {
11+
char *buffer = NULL;
12+
size_t bufsize = 0;
13+
char **args;
14+
15+
while (1) {
16+
printf("$ ");
17+
getline(&buffer, &bufsize, stdin);
18+
19+
args = parse_input(buffer);
20+
execute_command(args);
21+
22+
free(args);
23+
}
24+
25+
free(buffer);
26+
return 0;
27+
}
28+

shell.c

+77
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,77 @@
1+
#include "shell.h"
2+
3+
char **parse_input(char *input) {
4+
int position = 0;
5+
char **tokens;
6+
char *token;
7+
8+
tokens = malloc(MAX_ARGS * sizeof(char*));
9+
if (!tokens) {
10+
fprintf(stderr, "allocation error\n");
11+
exit(EXIT_FAILURE);
12+
}
13+
14+
token = strtok(input, " \t\r\n\a");
15+
while (token != NULL) {
16+
tokens[position++] = token;
17+
if (position >= MAX_ARGS) {
18+
position = MAX_ARGS - 1;
19+
break;
20+
}
21+
token = strtok(NULL, " \t\r\n\a");
22+
}
23+
tokens[position] = NULL;
24+
return tokens;
25+
}
26+
27+
char *find_command_in_path(char *cmd) {
28+
char *path, *path_copy, *token, *full_path;
29+
struct stat st;
30+
31+
path = getenv("PATH");
32+
path_copy = strdup(path);
33+
full_path = malloc(BUFFER_SIZE * sizeof(char));
34+
35+
token = strtok(path_copy, ":");
36+
while (token != NULL) {
37+
snprintf(full_path, BUFFER_SIZE, "%s/%s", token, cmd);
38+
if (stat(full_path, &st) == 0) {
39+
free(path_copy);
40+
return full_path;
41+
}
42+
token = strtok(NULL, ":");
43+
}
44+
45+
free(path_copy);
46+
free(full_path);
47+
return NULL;
48+
}
49+
50+
void execute_command(char **args) {
51+
pid_t pid;
52+
int status;
53+
char *command_path;
54+
55+
command_path = find_command_in_path(args[0]);
56+
if (command_path == NULL) {
57+
printf("%s: command not found\n", args[0]);
58+
return;
59+
}
60+
61+
pid = fork();
62+
if (pid == 0) {
63+
if (execv(command_path, args) == -1) {
64+
perror("Error");
65+
}
66+
exit(EXIT_FAILURE);
67+
} else if (pid < 0) {
68+
perror("Error");
69+
} else {
70+
do {
71+
waitpid(pid, &status, WUNTRACED);
72+
} while (!WIFEXITED(status) && !WIFSIGNALED(status));
73+
}
74+
75+
free(command_path);
76+
}
77+

shell.h

+25
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,25 @@
1+
#ifndef SHELL_H
2+
#define SHELL_H
3+
4+
#include <stdio.h>
5+
#include <stdlib.h>
6+
#include <string.h>
7+
#include <unistd.h>
8+
#include <sys/wait.h>
9+
#include <sys/stat.h>
10+
11+
#define BUFFER_SIZE 1024
12+
#define MAX_ARGS 64
13+
14+
15+
char **parse_input(char *input);
16+
char *find_command_in_path(char *cmd);
17+
void execute_command(char **args);
18+
int basic_command_execution(void);
19+
int argument_handling(void);
20+
int environment_variable_handling(void);
21+
int exit_builtin_command(void);
22+
int environment_builtin_command(void);
23+
24+
#endif
25+

0 commit comments

Comments
 (0)