You are on page 1of 3

#include <stdio.

h>

#include <stdlib.h>

#include <string.h>

#include <unistd.h>

#include <sys/wait.h>

#define MAX_COMMAND_LENGTH 25

#define MAX_ARGS 10

void parse_command(char *input, char **args) {

char *token;

int i = 0;

token = strtok(input, " \n");

while (token != NULL && i < MAX_ARGS - 1) {

args[i++] = token;

token = strtok(NULL, " \n");

args[i] = NULL;

void execute_command(char **args) {

pid_t pid;

int status;

pid = fork();

if (pid < 0) {

perror("fork");

exit(EXIT_FAILURE);

} else if (pid == 0) {
// Child process

if (execvp(args[0], args) == -1) {//ls -l

perror("This is not commond.");

exit(EXIT_FAILURE);

} else {

// Parent process

do {

waitpid(pid, &status, WUNTRACED);

} while (!WIFEXITED(status) && !WIFSIGNALED(status));//wait krna hai

int main() {

char input[MAX_COMMAND_LENGTH];

char *arguments[MAX_ARGS];//ls -l

while (1) {

printf("NARENDRA_SHELL_$ ");

fgets(input, MAX_COMMAND_LENGTH, stdin);

if (strcmp(input, "exit\n") == 0) {

break;

parse_command(input, arguments);
if (strcmp(arguments[0], "cd") == 0) {

if (chdir(arguments[1]) != 0) {

perror("chdir");

} else {

execute_command(arguments);

return 0;

You might also like