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 4323552
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 21, 20262026-05-21T09:01:03+00:00 2026-05-21T09:01:03+00:00

I am writing a c program for a class that is a small shell.

  • 0

I am writing a c program for a class that is a small shell. The user inputs a command, and the code executes it using the exec() function.

I need to have a fork in the process so all the work is done in the child process. The only problem is that the child won’t terminate properly and execute the command. When I run the code without the fork, it executes commands perfectly.

The problem seems to be coming from where I am creating the string to be used in the execv call. It’s the line of code where I call strcpy. If I comment that out, things work fine. I also tried changing it to strncat with the same problem. I’m clueless as to what’s causing this and welcome any help.

#include <sys/wait.h>
#include <vector>
#include <sstream>
#include <cstdlib>
#include <stdio.h>
#include <iostream>
#include <string.h>
#include <unistd.h>

using namespace std;

string *tokenize(string line);
void setCommand(string *ary);

string command;
static int argument_length;

int main() {
    string argument;
    cout << "Please enter a unix command:\n";
    getline(cin, argument);
    string *ary = tokenize(argument);

    //begin fork process
    pid_t pID = fork();
    if (pID == 0) { // child
        setCommand(ary);

        char *full_command[argument_length];
        for (int i = 0; i <= argument_length; i++) {
            if (i == 0) {
                full_command[i] = (char *) command.c_str();
                //  cout<<"full_command " <<i << " = "<<full_command[i]<<endl;
            } else if (i == argument_length) {
                full_command[i] = (char *) 0;
            } else {
                full_command[i] = (char *) ary[i].c_str();
            //  cout<<"full_command " <<i << " = "<<full_command[i]<<endl;
            }
        }    

        char* arg1;
        const char *tmpStr=command.c_str();        
        strcpy(arg1, tmpStr);
        execv((const char*) arg1, full_command);
        cout<<"I'm the child"<<endl;
    } else if (pID < 0) { //error
        cout<<"Could not fork"<<endl;
    } else { //Parent
        int childExitStatus;
        pid_t wpID = waitpid(pID, &childExitStatus, WCONTINUED);
        cout<<"wPID = "<< wpID<<endl;
        if(WIFEXITED(childExitStatus))
            cout<<"Completed "<<ary[0]<<endl;
        else
            cout<<"Could not terminate child properly."<<WEXITSTATUS(childExitStatus)<<endl;
    }

    // cout<<"Command = "<<command<<endl;
    return 0;
}

string *tokenize(string line) //splits lines of text into seperate words
{
    int counter = 0;
    string tmp = "";
    istringstream first_ss(line, istringstream::in);
    istringstream second_ss(line, istringstream::in);

    while (first_ss >> tmp) {
        counter++;
    }

    argument_length = counter;
    string *ary = new string[counter];
    int i = 0;
    while (second_ss >> tmp) {
        ary[i] = tmp;
        i++;
    }

    return ary;
}

void setCommand(string *ary) {
    command = "/bin/" + ary[0];

// codeblock paste stops here
  • 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-05-21T09:01:04+00:00Added an answer on May 21, 2026 at 9:01 am

    You said:

    Its the line of code where I call
    strcpy.

    You haven’t allocated any memory to store your string. The first parameter to strcpy is the destination pointer, and you’re using an uninitialized value for that pointer. From the strcpy man page:

    char *strcpy(char *s1, const char *s2);

    The stpcpy() and strcpy() functions copy the string s2 to s1 (including
    the terminating `\0′ character).

    There may be other issues, but this is the first thing I picked up on.

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

Sidebar

Related Questions

If you are writing a program that is executable from the command line, you
I'm writing a program (for Mac OS X, using Objective-C) and I need to
I am writing a program for class that is asking us to create a
I'm writing a program for iPhone that will first let the user take a
I'm writing a program using jdbc that will be an interface to database(smth like
I'm writing a program for my visual basic class that computes the prices of
I am writing a small program in C++. There is a class. Can you
I'm writing a C++ program for class that requires a set of N child
I'm writing a java program that works with .class files. I want to be
I'm writing a simple program. There is only one class in it. There is

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.