Skip to content

Commit 367f483

Browse files
committed
Add support for Ctrl+C
1 parent b339d0e commit 367f483

5 files changed

Lines changed: 28 additions & 20 deletions

File tree

include/lsh.h

Lines changed: 2 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -10,10 +10,8 @@
1010
void lsh_loop(void);
1111

1212
/**
13-
* @brief Signal handler for SIGINT (Ctrl+C). Forces the user to use 'exit' to quit the shell.
14-
* @param signum The signal number (should be SIGINT).
15-
* @return void.
13+
* @brief Prints the prompt line.
1614
*/
17-
void sigint_handler(int signum);
15+
void lsh_print_prompt(void);
1816

1917
#endif

include/lsh_helpers.h

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -13,4 +13,10 @@ char *get_user_at_hostname();
1313
*/
1414
char *get_current_dir();
1515

16+
/**
17+
* @brief Signal handler for SIGINT (Ctrl+C).
18+
* @param signum The signal number (should be SIGINT).
19+
*/
20+
void sigint_handler(int signum);
21+
1622
#endif

src/lsh.c

Lines changed: 12 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -82,7 +82,7 @@ int lsh_execute(char **args)
8282
char *lsh_read_line(void)
8383
{
8484
char *line = NULL;
85-
size_t bufsize = 0; // have getline allocate a buffer for us
85+
size_t bufsize = 0;
8686
ssize_t bytes_read;
8787

8888
bytes_read = getline(&line, &bufsize, stdin);
@@ -153,21 +153,25 @@ char **lsh_split_line(char *line)
153153
return tokens;
154154
}
155155

156+
void lsh_print_prompt(void)
157+
{
158+
char *user_information = get_user_at_hostname();
159+
char *cwd = get_current_dir();
160+
printf("%s:%s> ", user_information, cwd);
161+
fflush(stdout);
162+
free(user_information);
163+
free(cwd);
164+
}
165+
156166
void lsh_loop(void)
157167
{
158168
char *line;
159169
char **args;
160170
int status;
161-
char *cwd;
162-
char *user_information = get_user_at_hostname();
163171

164172
do
165173
{
166-
167-
cwd = get_current_dir();
168-
169-
printf("%s:%s> ", user_information, cwd);
170-
fflush(stdout);
174+
lsh_print_prompt();
171175

172176
line = lsh_read_line();
173177

@@ -183,9 +187,6 @@ void lsh_loop(void)
183187

184188
free(line);
185189
free(args);
186-
free(cwd);
187190

188191
} while (status);
189-
190-
free(user_information);
191192
}

src/lsh_helpers.c

Lines changed: 7 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -5,12 +5,14 @@
55
#include <errno.h>
66
#include <stdbool.h>
77
#include "lsh_colors.h"
8+
#include "lsh.h"
89

9-
/**
10-
* @brief Signal handler for SIGINT.
11-
* @param signum The signal number.
12-
*/
13-
void sigint_handler(int signum) {}
10+
void sigint_handler(int signum)
11+
{
12+
(void)signum;
13+
printf("\n");
14+
lsh_print_prompt();
15+
}
1416

1517
/**
1618
* @return Wheter the terminal supports ANSI escape codes or not.

src/main.c

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,7 @@
22
#include <stdlib.h>
33
#include <signal.h>
44
#include "lsh.h"
5+
#include "lsh_helpers.h"
56

67
int main(int argc, char **argv)
78
{

0 commit comments

Comments
 (0)