Config Router

  • Google Sheets
  • CCNA Online training
    • CCNA
  • CISCO Lab Guides
    • CCNA Security Lab Manual With Solutions
    • CCNP Route Lab Manual with Solutions
    • CCNP Switch Lab Manual with Solutions
  • Juniper
  • Linux
  • DevOps Tutorials
  • Python Array
You are here: Home / Writing a simple shell in C using fork/execvp

Writing a simple shell in C using fork/execvp

August 19, 2021 by James Palmer

The invalid option is because fgets() keeps the ‘n’ when you press enter, try this
if(!fgets(line, BUFFER_LEN, stdin))
break;
size_t length = strlen(line);
if (line[length – 1] == ‘n’)
line[length – 1] = ‘’;

when you are trying to call ls -l you are passing “-ln” as the option, hence the message.
You will have to change
strcmp(line, “exitn”)

to
strcmp(line, “exit”)

#include
#include
#include
#include
#include

#define BUFFER_LEN 1024

int main(){
char user_input[BUFFER_LEN]; //get command line
char* argv[120]; //user command
int argc ; //argument count
char* path= “/bin/”; //set path at bin
char file_path[50];//full file path

while(1){

printf(“Simple Shell>> “); // Greeting shell during startup

if(!fgets(user_input,BUFFER_LEN, stdin)){
break; //break if the command length exceed the defined BUFFER_LEN
}

size_t length = strlen(user_input);

if(length == 0){
break;
}

if (user_input[length – 1] == ‘n’){
user_input[length – 1] = ‘’; // replace last char by ‘’ if it is new line char
}

//split command using spaces
char *token;
token = strtok(user_input,” “);
int argc=0;
if(token == NULL){
continue;
}
while(token!=NULL){
argv[argc]=token;
token = strtok(NULL,” “);
argc++;
}

argv[argc]=NULL;

strcpy(file_path, path); //Assign path to file_path
strcat(file_path, argv[0]); //conctanate command and file path

if (access(file_path,F_OK)==0){ //check the command is available in /bin

pid_t pid, wpid;
int status;

pid = fork();
if (pid == 0) { //child process
if (execvp(file_path,argv) == -1) {
perror(“Child proccess end”);
}
exit(EXIT_FAILURE);
}
else if (pid > 0) { // parent process
wpid = waitpid(pid, &status, WUNTRACED);
while (!WIFEXITED(status) && !WIFSIGNALED(status)){
wpid = waitpid(pid, &status, WUNTRACED);
}
}
else {
perror(“Fork Failed”); //process id can not be null
}
}
else {
printf(“Command is not available in the binn”); //Command is not available in the bin
}

}
}

Related

Filed Under: Uncategorized

Recent Posts

  • How do I give user access to Jenkins?
  • What is docker volume command?
  • What is the date format in Unix?
  • What is the difference between ARG and ENV Docker?
  • What is rsync command Linux?
  • How to Add Music to Snapchat 2021 Android? | How to Search, Add, Share Songs on Snapchat Story?
  • How to Enable Snapchat Notifications for Android & iPhone? | Steps to Turn on Snapchat Bitmoji Notification
  • Easy Methods to Fix Snapchat Camera Not Working Black Screen Issue | Reasons & Troubleshooting Tips to Solve Snapchat Camera Problems
  • Detailed Procedure for How to Update Snapchat on iOS 14 for Free
  • What is Snapchat Spotlight Feature? How to Make a Spotlight on Snapchat?
  • Snapchat Hack Tutorial 2021: Can I hack a Snapchat Account without them knowing?

Copyright © 2025 · News Pro Theme on Genesis Framework · WordPress · Log in