-
Notifications
You must be signed in to change notification settings - Fork 0
/
shellloop.c
93 lines (81 loc) · 3 KB
/
shellloop.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
#include <unistd.h> // For getcwd(), gethostname(), and possibly other POSIX-like system calls
#include <stdlib.h> // For malloc(), free(), exit(), and getenv()
#include <stdio.h> // For perror() and snprintf()
#include <string.h> // For strlen(), strncmp(), and strdup()
#include <limits.h> // For PATH_MAX
#include <pwd.h> // For getpwuid() and struct passwd
#include <readline/readline.h> // For readline()
#include <readline/history.h> // For add_history()
//added manual def of PATH_MAX
#ifndef PATH_MAX
#define PATH_MAX 4096
#endif
#include "shellloop.h"
#include "parse.h"
#include "execute.h"
//command input loop
void ksh_loop(void) {
char *line;
char **args;
int status;
char cwd[PATH_MAX]; // Using PATH_MAX for portability
char *user;
char prompt[PATH_MAX + 50]; // Ensure buffer is large enough for prompt
char hostname[1024]; // hostname buffer
char *home_directory;
char *relative_path;
hostname[1023] = '\0'; // Ensure null termination
gethostname(hostname, 1023); // Get the hostname
do {
// Get current working directory
if (getcwd(cwd, sizeof(cwd)) == NULL) {
perror("ksh");
exit(EXIT_FAILURE);
}
// Get username
user = getenv("USER");
if (user == NULL) {
struct passwd *pw = getpwuid(getuid());
if (pw == NULL) {
perror("ksh: unable to fetch username");
exit(EXIT_FAILURE);
}
user = pw->pw_name;
}
// attempt to abbreviate home directory
home_directory = getenv("HOME");
if (home_directory && strncmp(cwd, home_directory, strlen(home_directory)) == 0) {
// If cwd contains the home directory, replace it with '~' for abbreviation
int len = strlen(cwd) - strlen(home_directory) + 2;
relative_path = malloc(len);
if (relative_path == NULL) {
perror("ksh: unable to allocate memory");
exit(EXIT_FAILURE);
}
//relative path starts with ~
snprintf(relative_path, len, "~%s", cwd + strlen(home_directory));
} else {
// Allocate and copy the cwd as is if it doesn't start with home_director
relative_path = strdup(cwd);
if(relative_path == NULL) {
perror("ksh: unable to allocate memory");
exit(EXIT_FAILURE);
}
}
// Construct and display prompt
snprintf(prompt, sizeof(prompt), "[%s@%s]-[%s] $ ", user, hostname, relative_path);
// Read line from user input using readline
line = readline(prompt);
if (line && *line) {
add_history(line); // Add the line to history only if it's not empty
}
// Split line into args
args = ksh_split_line(line);
// Execute command
status = ksh_execute(args);
// Free memory
free(line);
free(args);
free(relative_path);
} while (status);
}