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

  • SEARCH
  • Home
  • 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 6923615
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 27, 20262026-05-27T10:33:27+00:00 2026-05-27T10:33:27+00:00

I’m writing a simple shell that accepts some standard commands like cd and ls

  • 0

I’m writing a simple shell that accepts some standard commands like cd and ls in C. I’m trying to implement a feature where the user can enter a “;” in between commands so that a bunch of commands can be written on the same line and be executed separately. So if I input “cd Desktop; ls” the shell should cd to Desktop and print the what’s in the directory. The problem is it only executes the first command. Here’s my main method:

char input[1024];

while(1)
{
    printf("%s ", prompt);
    fgets(input, 1024, stdin);

    char delims[] = ";";
    char *result = NULL;
    result = strtok( input, delims );

    while( result != NULL )
    {
        printf("%s\n", result);

        char * copy = malloc(strlen(result) + 1); //Create a copy of the input token
        strcpy(copy, result);

        format(copy);

        if(programs)
        {
            handle();
            cleanup(programs);
            programs = NULL;
        }
        free(copy);
        result = strtok( NULL, delims );
        cmdno++;
    }
}

First I try to break up the input into tokens based on “;” and then feed the token to the format() method which looks like this:

int format(char input[])
{
    input = strtok(input, "\n");
    ...
}

I know that strtok makes changes to the original string, which is why I create a copy of the token first before passing it to format. Is what I’m doing correct??

  • 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-27T10:33:28+00:00Added an answer on May 27, 2026 at 10:33 am

    You can’t mix multiple strtok calls. Here’s what’s happening:

    • You start splitting input so strtok takes note and stores stuff internally
    • You take a break from splitting input
      • You start splitting copy so again strtok takes note, thereby destroying the previous info
    • At this point strtok only knows about the copy business and doesn’t know anything about the original input.

    The main problem is that strtok doesn’t know that you’re doing two things at the same time. From its point of view, you simply started processing a different string before finishing the first string.


    Possible solutions:

    • Use strtok_r if you have it. It’s not standard C (but it is standard POSIX). The r stands for reentrant
    • Use your own splitting function (strchr / looping etc)
    • Change your program logic such that you don’t need to split copy before finishing with input

    About that last point:

    • Keep an array of char * and fill it with strtok without pausing to split sub-tokens. So each element should be a different command
    • When you’re done with the ";" split, start processing each of the array elements
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I have just tried to save a simple *.rtf file with some websites and
For some reason, after submitting a string like this Jack’s Spindle from a text
I've got a string that has curly quotes in it. I'd like to replace
I'm parsing an RSS feed that has an ’ in it. SimpleXML turns this
I'm trying to create an if statement in PHP that prevents a single post
I have a string like this: La Torre Eiffel paragonata all’Everest What PHP function
I have some data like this: 1 2 3 4 5 9 2 6
link Im having trouble converting the html entites into html characters, (&# 8217;) i
That's pretty much it. I'm using Nokogiri to scrape a web page what has
I am trying to understand how to use SyndicationItem to display feed which 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.