Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • Home
  • SEARCH
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 8584713
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 11, 20262026-06-11T21:50:11+00:00 2026-06-11T21:50:11+00:00

The code I have is here. Essentially, the custom shell needs to take echo,

  • 0

The code I have is here. Essentially, the custom shell needs to take echo, cd and quit as the commands and fork a child for the commands. There are no errors when it compiles but it is not running. It doesn’t echo the argument when I give, say “echo hello”…it goes into the right functions and all but I cannot pinpoint the error. I am guessing I am making a mistake with the execlp function. Can someone please help me out?

#include <stdio.h>
#include <sys/types.h>
#include <sys/wait.h>
#include <unistd.h>
#include <string.h>

int main()
{

    char command[256];
    char * parsedCmd;
    char * argument;

    //char *sep[]=" ";

    while (1<2)
    {
        printf("\nOS Assignment 1@user: ");
        fgets(command, sizeof(command), stdin); 

        parsedCmd=strtok(command, " ");
        argument=strtok(NULL, " ");

        //printf("\n%s\n", argument);

        if (strncmp("quit", command, 4)==0)
            break;
        if (strncmp("cd", parsedCmd, 2)==0)
        {
            printf("\nExecuting cd\n");
            execCD(argument);   
        }
        if (strncmp("echo", parsedCmd, 4)==0)
        {
            printf("\nEchoing now...\n");
            shellEcho(argument);
        }       
        else
            printf("\nOur shell is simple. Try either cd, echo or quit :) ...\n");  
    }   
}

int execCD(char *receive)
{
    printf("\nExecuting cd as Child...\n"); 
    printf("\nDirectory to cd is %s\n", receive);
    pid_t pid;

    pid=fork();

    if (pid<0)
    {
        fprintf(stderr, "\nFork Failed\n");
        return 1;
    }

    else if (pid==0)
    {
        execlp(receive, "cd", NULL);
    }

    else
    {
        wait(NULL);
        printf("Child Complete");
    }

    return 0;
}

int shellEcho(char *receive)
{
    printf("\nExecuting echo as Child...\n");   

    pid_t pid;

    pid=fork();

    if (pid<0)
    {
        fprintf(stderr, "\nFork Failed\n");
        return 1;
    }

    else if (pid==0)
    {
        execlp(receive, "echo", NULL);
    }

    else
    {
        wait(NULL);
        printf("Child Complete");
    }

    return 0;
}
  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-06-11T21:50:12+00:00Added an answer on June 11, 2026 at 9:50 pm

    You have the arguments to execlp backwards. Also, note that by convention, you need to specify “echo” twice: once as the name of the program to execute, and the second time as the 0th argument (which is usually, but not necessarily, the name of the program).

    int shellEcho(char *receive)
    {
        printf("\nExecuting echo as Child...\n");   
        pid_t pid;
        pid=fork();
    
        if (pid<0) {
            fprintf(stderr, "\nFork Failed\n");
            return 1;
        } else if (pid==0) {
            execlp("echo", "echo", receive, NULL);
        } else {
            wait(NULL);
            printf("Child Complete");
        }
    
        return 0;
    }
    

    Also, note that “cd” cannot be executed, as it must be implemented by the shell (it is not a separate program that can be run). It’s probably more appropriate to discuss that issue with your teacher rather than pursuing it futher here.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I have been doings bits here and there on some code which essentially pulls
enter code here I have a table on SQL server 2005 with bigint primary
enter code here I have a ListView that have a different Layout XML for
I have some code here that uses bitsets to store many 1 bit values
I have this code here printf '$request1 = select * from whatever where this
I have this code here, which is intended to allow any type of arguments:
I have this code here, {foreach from=$cart.cartItems item=item name=cart} <div id=cart2Produkt> <p>{if $item.Product.ID} <a
I have some code here which works perfectly in firefox but not in chrome
I have my code here, it works fine from my home, where my user
I have this code here: var Person = (function() { var name; var PersonConstructor

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.