I am writing a simple shell and the command (ls & whoami;) works however, when I only type the command ls the code breaks due to a segmentation fault on line 51. I am wonder how to go about this I have used Valgrind and a ton of printf statements but nothing is seeming to work. Let me know what you think.
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <unistd.h>
#include <wait.h>
#define MAX_LINE 80
#define MAX_CHAR 100
int main()
{
int should_run = 1;
char line[MAX_CHAR];
char *args[MAX_LINE/2 +1];
int ampersandFlag = 0;
/*---------------------------------------------------*/
/* Start of The Program */
while(should_run){
printf("osh>");
fgets(line,MAX_CHAR,stdin);
/* get rid of newline char */
int k = 0;
while(line[k] != '\n'){
k++;
}
line[k] = '\0';
/* get rid of semicolon char */
int l = 0;
while(line[l] != ';'){
l++;
}
line[l] = '\0';
int i = 0;
args[i] = strtok(line, " ");
if(args[i] == NULL){
return 1;
}
while(args[i] != NULL){
printf("%s\n", args[i]);
i++;
args[i] = strtok(NULL, " ");
}
/* Handles the exit command. */
int a = 0;
if(strcmp(args[a], "exit") == 0){
exit(0);
}
if(strcmp(args[1], "&") == 0){
args[1] = NULL;
ampersandFlag = 1;
}
/*---------------------------------------------------*/
/* Start Forking */
int returnForkValue = fork();
int status;
if(returnForkValue < 0){
perror("Fork Failed");
}
else if(returnForkValue == 0){
printf("This is the child\n");
if(ampersandFlag == 1){
int stat;
int retFork = fork();
if(retFork == 0){
printf("This is second child");
execvp(args[2],args);
}
else{
printf("This is the second parent");
waitpid(retFork, &stat, 0);
}
}
execvp(args[0],args);
printf("This won't be printed if execvp is successul\n");
}
else{
printf("This is the parent\n");
waitpid(returnForkValue, &status, 0);
}
}
return 0;
}
If you have exactly one token, only args[0]
will point to a valid string. The next args[1]
will be a NULL
pointer. All other elements of args
will be uninitialized.
In this case the line
if(strcmp(args[1], "&") == 0){
will try to use a NULL
pointer as a string.
After the loop
while(args[i] != NULL){
the variable i
will contain the number of tokens.
You should only use index values < i
to access args
.
The code to execute the second command is wrong. Instead of
execvp(args[2],args);
you would have to use
execvp(args[2],&args[2]);
but this is only valid if the number of tokens is at least 3.
BTW: Your code is a bit inconsistent. To check for "exit"
you use variable a
as index for args
, for other comparisons you use hard-coded numbers.
When I run the program with valgrind
and enter ls
I get an error message showing the NULL
pointer access.
==30002== Invalid read of size 1
==30002== at 0x483EED4: strcmp (in /usr/lib/x86_64-linux-gnu/valgrind/vgpreload_memcheck-amd64-linux.so)
==30002== by 0x10943A: main (test.c:58)
==30002== Address 0x0 is not stack'd, malloc'd or (recently) free'd
I get an additional error message
==30002== Conditional jump or move depends on uninitialised value(s)
==30002== at 0x10933C: main (test.c:34)
This happens because your loop
while(line[l] != ';'){
does not check for the terminating NUL
( '\0'
) character and will read past the end of the string if it doesn't contain a semicolon.
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.