Start creating a shell in c-programming

Start creating a shell in c-programming

Table of contents

No heading

No headings in the article.

Hello everyone, its Michael from outsideInn.

I’d be introducing us to how to start building your own Shell. When I had to build a shell using C-programming, I was so happy to put apply what I knew about C-programming on a project. Immediately I wrote the few lines of code, I got confused at the entry point. All this while I just thought I’d start working on it without making research.

Don’t make my mistake. Today I’d be introducing us to building our own shell using C-language.

Let’s begin!

I’d make this video quick and fast because it’s the entry point, so I’d be using standard function and writing everything in a single file.

The header files i’d use here are:

#include <stdio.h>

#include <stdlib.h>

#include <unistd.h>

#include <string.h>

#include <sys/wait.h>

Let’s define a max buffer size using a macro

What the compiler first looks for in any C-program is our main, let’s call our function here and call it shell.

Now let us define our function

First, we need a memory location to hold our input

We need a loop to keep accepting input continuously while we still have a valid input.

When I attempted to write the shell without researching, I got lost at how I’d call the shell. Well it 0is as simple as printing a prompt. Let’s call our prompt ‘miracle_says’. You can call yours what you want.

We need an if statement that catches if we have no input so as not to run into an infinite loop

Now, we copy the variable into a buffer.

Wait, now we can call out fork process. Let me give a quick summary of what this fork will do. We are trying to create a shell and everything has a point it begins. Now this fork is our stating point per say and it preps the system to start performing actions and it has already be defined in the unistd header file. For those that understand what a parent process and child is you understand what I am doing , If not stay tuned to my channel for more I’d explain in other videos. Now this function call returns a value, a signed integer and we store it In the variable pid the pid_t shows us this.

We know the parent and child processes will return values and we need to catch them

If the value returned is a value less than Zero we should tell the user the fork failed and continue to the next thing to do.

Now let’s catch the value of the child process first since we know it will return zero on success and use it to catch a situation that the command isn’t found.

Now we know the parent process is still on its way so let’s use and it’s still waiting it for the child process to return before it can fire to catch a command if present.

Fingers crossed!

Now let's run it!

This is just an introduction to the making of a shell. Some will do it simpler and easier but this is the way I found it to work for me If you Like what you’ve seen, share my video, like and subscribe

Remember a creative mind is a fertile land for growth.

I have a youTube video that helps with this

https://youtu.be/6vopsXiIMq8