@@ -28,9 +28,9 @@ struct termios shell_tmodes;
28
28
pid_t shell_pgid ;
29
29
30
30
/*
31
- Definition of struct tokens. We need repeat it because using 'struct tokens *tokens'
32
- referencing to header's 'struct tokens' so that causes error of incomplete type
33
- */
31
+ Definition of struct tokens. We need repeat it because using 'struct tokens *tokens'
32
+ referencing to header's 'struct tokens' so that causes error of incomplete type
33
+ */
34
34
struct tokens {
35
35
size_t tokens_length ;
36
36
char * * tokens ;
@@ -48,28 +48,28 @@ typedef int cmd_fun_t(struct tokens *tokens);
48
48
49
49
/* Built-in command struct and lookup table */
50
50
typedef struct fun_desc {
51
- cmd_fun_t * fun ;
52
- char * cmd ;
53
- char * doc ;
51
+ cmd_fun_t * fun ;
52
+ char * cmd ;
53
+ char * doc ;
54
54
} fun_desc_t ;
55
55
56
56
fun_desc_t cmd_table [] = {
57
- {cmd_help , "?" , "show this help menu" },
58
- {cmd_exit , "exit" , "exit the command shell" },
59
- {cmd_pwd , "pwd" , "prints the current working directory" },
60
- {cmd_cd , "cd" , "changes current working directory on directory provided by arg" },
57
+ {cmd_help , "?" , "show this help menu" },
58
+ {cmd_exit , "exit" , "exit the command shell" },
59
+ {cmd_pwd , "pwd" , "prints the current working directory" },
60
+ {cmd_cd , "cd" , "changes current working directory on directory provided by arg" },
61
61
};
62
62
63
63
/* Prints a helpful description for the given command */
64
64
int cmd_help (unused struct tokens * tokens ) {
65
- for (unsigned int i = 0 ; i < sizeof (cmd_table ) / sizeof (fun_desc_t ); i ++ )
66
- printf ("%s - %s\n" , cmd_table [i ].cmd , cmd_table [i ].doc );
67
- return 1 ;
65
+ for (unsigned int i = 0 ; i < sizeof (cmd_table ) / sizeof (fun_desc_t ); i ++ )
66
+ printf ("%s - %s\n" , cmd_table [i ].cmd , cmd_table [i ].doc );
67
+ return 1 ;
68
68
}
69
69
70
70
/* Exits this shell */
71
71
int cmd_exit (unused struct tokens * tokens ) {
72
- exit (0 );
72
+ exit (0 );
73
73
}
74
74
75
75
/* Prints current working directory */
@@ -78,7 +78,7 @@ int cmd_pwd(unused struct tokens *tokens){
78
78
char * path_buff = NULL ;
79
79
path_buff = (char * )malloc (sizeof (char ) * MAX_PATH_SIZE );
80
80
if (!path_buff )
81
- return -1 ;
81
+ return -1 ;
82
82
/* Copy absolute path of working directory to path_buff */
83
83
getcwd (path_buff , MAX_PATH_SIZE );
84
84
fprintf (stdout , "Path to current directory: %s\n" , path_buff );
@@ -89,99 +89,99 @@ int cmd_pwd(unused struct tokens *tokens){
89
89
/* Changes current working directory */
90
90
int cmd_cd (struct tokens * tokens ){
91
91
if (chdir (tokens -> tokens [1 ]) == -1 ){
92
- printf ("No such file or directory\n" );
92
+ printf ("No such file or directory\n" );
93
93
}
94
94
return 1 ;
95
95
}
96
96
97
97
/* Execute program */
98
98
int shell_exec (struct tokens * tokens ){
99
- char * path = tokens_get_token (tokens , 0 );
100
- char * * args = (char * * )malloc (tokens -> tokens_length * sizeof (char * ));
101
- for (int i = 0 ; i < tokens -> tokens_length ; i ++ ){
102
- args [i ] = tokens_get_token (tokens , i );
103
- }
104
- pid_t cpid ;
105
- int status ;
106
- cpid = fork ();
107
- /* cpid > 0 - parent process, cpid == 0 - child process, cpid < 0 - error */
108
- if (cpid > 0 ) {
109
- wait (& status );
110
- } else if (cpid == 0 ){
111
- /* executes program according to path and given arguments */
112
- execv (path , args );
113
- exit (0 );
114
- } else {
115
- /* cannot fork current process */
116
- exit (1 );
117
- }
118
- return 1 ;
99
+ char * path = tokens_get_token (tokens , 0 );
100
+ char * * args = (char * * )malloc (tokens -> tokens_length * sizeof (char * ));
101
+ for (int i = 0 ; i < tokens -> tokens_length ; i ++ ){
102
+ args [i ] = tokens_get_token (tokens , i );
103
+ }
104
+ pid_t cpid ;
105
+ int status ;
106
+ cpid = fork ();
107
+ /* cpid > 0 - parent process, cpid == 0 - child process, cpid < 0 - error */
108
+ if (cpid > 0 ) {
109
+ wait (& status );
110
+ } else if (cpid == 0 ){
111
+ /* executes program according to path and given arguments */
112
+ execv (path , args );
113
+ exit (0 );
114
+ } else {
115
+ /* cannot fork current process */
116
+ exit (1 );
117
+ }
118
+ return 1 ;
119
119
}
120
120
121
121
/* Looks up the built-in command, if it exists. */
122
122
int lookup (char cmd []) {
123
- for (unsigned int i = 0 ; i < sizeof (cmd_table ) / sizeof (fun_desc_t ); i ++ )
124
- if (cmd && (strcmp (cmd_table [i ].cmd , cmd ) == 0 ))
125
- return i ;
126
- return -1 ;
123
+ for (unsigned int i = 0 ; i < sizeof (cmd_table ) / sizeof (fun_desc_t ); i ++ )
124
+ if (cmd && (strcmp (cmd_table [i ].cmd , cmd ) == 0 ))
125
+ return i ;
126
+ return -1 ;
127
127
}
128
128
129
129
/* Intialization procedures for this shell */
130
130
void init_shell () {
131
- /* Our shell is connected to standard input. */
132
- shell_terminal = STDIN_FILENO ;
131
+ /* Our shell is connected to standard input. */
132
+ shell_terminal = STDIN_FILENO ;
133
133
134
- /* Check if we are running interactively */
135
- shell_is_interactive = isatty (shell_terminal );
134
+ /* Check if we are running interactively */
135
+ shell_is_interactive = isatty (shell_terminal );
136
136
137
- if (shell_is_interactive ) {
138
- /* If the shell is not currently in the foreground, we must pause the shell until it becomes a
139
- * foreground process. We use SIGTTIN to pause the shell. When the shell gets moved to the
140
- * foreground, we'll receive a SIGCONT. */
141
- while (tcgetpgrp (shell_terminal ) != (shell_pgid = getpgrp ()))
142
- kill (- shell_pgid , SIGTTIN );
137
+ if (shell_is_interactive ) {
138
+ /* If the shell is not currently in the foreground, we must pause the shell until it becomes a
139
+ * foreground process. We use SIGTTIN to pause the shell. When the shell gets moved to the
140
+ * foreground, we'll receive a SIGCONT. */
141
+ while (tcgetpgrp (shell_terminal ) != (shell_pgid = getpgrp ()))
142
+ kill (- shell_pgid , SIGTTIN );
143
143
144
- /* Saves the shell's process id */
145
- shell_pgid = getpid ();
144
+ /* Saves the shell's process id */
145
+ shell_pgid = getpid ();
146
146
147
- /* Take control of the terminal */
148
- tcsetpgrp (shell_terminal , shell_pgid );
147
+ /* Take control of the terminal */
148
+ tcsetpgrp (shell_terminal , shell_pgid );
149
149
150
- /* Save the current termios to a variable, so it can be restored later. */
151
- tcgetattr (shell_terminal , & shell_tmodes );
152
- }
150
+ /* Save the current termios to a variable, so it can be restored later. */
151
+ tcgetattr (shell_terminal , & shell_tmodes );
152
+ }
153
153
}
154
154
155
155
int main (unused int argc , unused char * argv []) {
156
- init_shell ();
156
+ init_shell ();
157
157
158
- static char line [4096 ];
159
- int line_num = 0 ;
158
+ static char line [4096 ];
159
+ int line_num = 0 ;
160
160
161
- /* Please only print shell prompts when standard input is not a tty */
162
- if (shell_is_interactive )
163
- fprintf (stdout , "%d: " , line_num );
161
+ /* Please only print shell prompts when standard input is not a tty */
162
+ if (shell_is_interactive )
163
+ fprintf (stdout , "%d: " , line_num );
164
164
165
- while (fgets (line , 4096 , stdin )) {
166
- /* Split our line into words. */
167
- struct tokens * tokens = tokenize (line );
165
+ while (fgets (line , 4096 , stdin )) {
166
+ /* Split our line into words. */
167
+ struct tokens * tokens = tokenize (line );
168
168
169
- /* Find which built-in function to run. */
170
- int fundex = lookup (tokens_get_token (tokens , 0 ));
169
+ /* Find which built-in function to run. */
170
+ int fundex = lookup (tokens_get_token (tokens , 0 ));
171
171
172
- if (fundex >= 0 ) {
173
- cmd_table [fundex ].fun (tokens );
174
- } else {
175
- shell_exec (tokens );
176
- }
172
+ if (fundex >= 0 ) {
173
+ cmd_table [fundex ].fun (tokens );
174
+ } else {
175
+ shell_exec (tokens );
176
+ }
177
177
178
- if (shell_is_interactive )
179
- /* Please only print shell prompts when standard input is not a tty */
180
- fprintf (stdout , "%d: " , ++ line_num );
178
+ if (shell_is_interactive )
179
+ /* Please only print shell prompts when standard input is not a tty */
180
+ fprintf (stdout , "%d: " , ++ line_num );
181
181
182
- /* Clean up memory */
183
- tokens_destroy (tokens );
184
- }
182
+ /* Clean up memory */
183
+ tokens_destroy (tokens );
184
+ }
185
185
186
- return 0 ;
186
+ return 0 ;
187
187
}
0 commit comments