File tree Expand file tree Collapse file tree
Expand file tree Collapse file tree Original file line number Diff line number Diff line change 1+ UNIX command line interpreter
Original file line number Diff line number Diff line change 1+ #include <stdio.h>
2+
3+ /**
4+ * print_hello - prints Hello, World!
5+ */
6+ void print_hello (void )
7+ {
8+ printf ("Hello, World!\n" );
9+ }
10+
11+ /**
12+ * main - Entry point
13+ * Description: Calls a function to print "Hello, World!"
14+ * Return: 0 if successful, non-zero otherwise
15+ */
16+ int main (void )
17+ {
18+ print_hello ();
19+ return (0 );
20+ }
21+
Original file line number Diff line number Diff line change 1+ #include "shell.h"
2+
3+ int shell_exit (char * * args ) {
4+ return 0 ;
5+ }
6+
7+ int shell_env (char * * args ) {
8+ for (char * * env = environ ; * env != 0 ; env ++ ) {
9+ char * thisEnv = * env ;
10+ printf ("%s\n" , thisEnv );
11+ }
12+ return 1 ;
13+ }
14+
Original file line number Diff line number Diff line change 1+ #include "shell.h"
2+
3+ int execute (char * * args ) {
4+ if (args [0 ] == NULL ) {
5+
6+ return 1 ;
7+ }
8+
9+ if (strcmp (args [0 ], "exit" ) == 0 ) {
10+ return shell_exit (args );
11+ }
12+
13+ if (strcmp (args [0 ], "env" ) == 0 ) {
14+ return shell_env (args );
15+ }
16+
17+ return launch (args );
18+ }
19+
20+ int launch (char * * args ) {
21+ pid_t pid , wpid ;
22+ int status ;
23+
24+ pid = fork ();
25+ if (pid == 0 ) {
26+
27+ if (execvp (args [0 ], args ) == -1 ) {
28+ perror ("shell" );
29+ }
30+ exit (EXIT_FAILURE );
31+ } else if (pid < 0 ) {
32+
33+ perror ("shell" );
34+ } else {
35+
36+ do {
37+ wpid = waitpid (pid , & status , WUNTRACED );
38+ } while (!WIFEXITED (status ) && !WIFSIGNALED (status ));
39+ }
40+
41+ return 1 ;
42+ }
43+
Original file line number Diff line number Diff line change 1+ #include "shell.h"
2+
3+ int main (int argc , char * * argv ) {
4+ /* Load config files, if any */
5+
6+ /* Run command loop */
7+ loop ();
8+
9+ /* Perform any shutdown/cleanup */
10+
11+ return EXIT_SUCCESS ;
12+ }
13+
14+ void loop (void ) {
15+ char * line ;
16+ char * * args ;
17+ int status ;
18+
19+ do {
20+ printf ("#cisfun$ " );
21+ line = read_line ();
22+ args = split_line (line );
23+ status = execute (args );
24+
25+ free (line );
26+ free (args );
27+ } while (status );
28+ }
29+
Original file line number Diff line number Diff line change 1+ #include "shell.h"
2+
3+ char * read_line (void ) {
4+ char * line = NULL ;
5+ ssize_t bufsize = 0 ;
6+ getline (& line , & bufsize , stdin );
7+ return line ;
8+ }
9+
10+ char * * split_line (char * line ) {
11+ int bufsize = 64 , position = 0 ;
12+ char * * tokens = malloc (bufsize * sizeof (char * ));
13+ char * token ;
14+
15+ token = strtok (line , " \t\r\n\a" );
16+ while (token != NULL ) {
17+ tokens [position ++ ] = token ;
18+
19+ if (position >= bufsize ) {
20+ bufsize += 64 ;
21+ tokens = realloc (tokens , bufsize * sizeof (char * ));
22+ }
23+
24+ token = strtok (NULL , " \t\r\n\a" );
25+ }
26+ tokens [position ] = NULL ;
27+ return tokens ;
28+ }
29+
Original file line number Diff line number Diff line change 1+ #include <stdio.h>
2+ #include <stdlib.h>
3+ #include <string.h>
4+ #include <unistd.h>
5+ #include <sys/wait.h>
6+ #include <sys/types.h>
7+
8+ #define MAX_CMD_LEN 100
9+ #define PROMPT "#cisfun$ "
10+
11+ /**
12+ * main - Entry point for the simple shell
13+ */
14+ int main (void )
15+ {
16+ char command [MAX_CMD_LEN ];
17+ pid_t pid ;
18+ int status ;
19+
20+ while (1 ) {
21+ printf (PROMPT );
22+
23+
24+ if (fgets (command , MAX_CMD_LEN , stdin ) == NULL ) {
25+ if (feof (stdin )) {
26+ printf ("\n" );
27+ break ;
28+ } else {
29+ continue ; /
30+ }
31+ }
32+
33+
34+ size_t len = strlen (command );
35+ if (len > 0 && command [len - 1 ] == '\n' ) {
36+ command [len - 1 ] = '\0' ;
37+ }
38+
39+
40+ pid = fork ();
41+ if (pid == -1 ) {
42+ perror ("fork" );
43+ continue ;
44+ }
45+
46+ if (pid == 0 ) { // Child process
47+
48+ execlp (command , command , NULL );
49+
50+
51+ printf ("./shell: %s: No such file or directory\n" , command );
52+ exit (EXIT_FAILURE );
53+ } else {
54+ waitpid (pid , & status , 0 );
55+ }
56+ }
57+
58+ return 0 ;
59+ }
60+
Original file line number Diff line number Diff line change 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/types.h>
9+ #include <sys/wait.h>
10+
11+ /* Function prototypes */
12+ void loop (void );
13+ char * read_line (void );
14+ char * * split_line (char * line );
15+ int execute (char * * args );
16+ int launch (char * * args );
17+ int shell_exit (char * * args );
18+ int shell_env (char * * args );
19+
20+ #endif
21+
You can’t perform that action at this time.
0 commit comments