File tree Expand file tree Collapse file tree
Expand file tree Collapse file tree Original file line number Diff line number Diff line change 1010void 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
Original file line number Diff line number Diff line change @@ -13,4 +13,10 @@ char *get_user_at_hostname();
1313 */
1414char * 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
Original file line number Diff line number Diff line change @@ -82,7 +82,7 @@ int lsh_execute(char **args)
8282char * 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+
156166void 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}
Original file line number Diff line number Diff line change 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.
Original file line number Diff line number Diff line change 22#include <stdlib.h>
33#include <signal.h>
44#include "lsh.h"
5+ #include "lsh_helpers.h"
56
67int main (int argc , char * * argv )
78{
You can’t perform that action at this time.
0 commit comments