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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 11, 20262026-05-11T17:24:00+00:00 2026-05-11T17:24:00+00:00

I’m trying do to a basic bash with the use of system calls but

  • 0

I’m trying do to a basic bash with the use of system calls but I’m having some little problems with a pointer array.

To resume my code, I read commands from the stdin with read() to a buffer then I use strsep() to separate the command from the arguments and all the arguments into an array. Then I create a new process with fork() and execute that command with the associated arguments with execvp().

All this goes into a infinite loop until the user types “quit” (not coded yet). The problem is that after the first iteration, I need *pArgs to be empty, for the next command and arguments. And I don’t know how to do it…

Here’s my code:

#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <unistd.h>

int main(int argc, char **argv) {
    char bBuffer[BUFSIZ], *pArgs[10], *aPtr = NULL, *sPtr;
    int aCount;
    pid_t pid;

    while(1) {
        write(1, "\e[1;31mmyBash \e[1;32m# \e[0m", 27);
        read(0, bBuffer, BUFSIZ);

        sPtr = bBuffer;
        aCount = 0;

        do {
            aPtr = strsep(&sPtr, " ");
            pArgs[aCount++] = aPtr;
        } while(aPtr);

        pArgs[aCount-2][strlen(pArgs[aCount-2])-1] = '\0';

        // Debug code to output pArgs content
        write(1, "|>", 2);
        write(1, pArgs[0], strlen(pArgs[0]));
        write(1, "<|", 2);

        if(strlen(pArgs[0]) > 1) {
            pid = fork();

            if(pid == -1) {
                perror("fork");
                exit(1);
            }

            if(pid == 0) {
                execvp(pArgs[0], pArgs);
                exit(0);
            }
        }
    }

    return 0;
}

P.S: Sorry but I can’t provide an input and output test case at the moment. Hopefully, this is not that hard to understand and fix that you guys don’t need it. I’ll post it later if it’s needed though…

Just to clear things up:
I know I asked how to clear the array and I got an answer for that. But it seems now obvious to me that my problem wasn’t that, but the garbage that the buffer was collecting as pointed out by litb. It makes more sense to terminate the string with the null character than to clear the array. That’s why I’m marking litb’s answer as right one.

  • 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-11T17:24:00+00:00Added an answer on May 11, 2026 at 5:24 pm

    Your problem is that you don’t add a null character after the data read. So the strsep calls don’t know where to stop. In C, strings must be terminated by a null character (that’s called the terminating null character).

    // don't forget to add error handling at some point (s == -1)
    ssize_t s = read(0, bBuffer, BUFSIZ-1);
    bBuffer[s] = '\0';
    

    With that in place, i don’t see what array should be cleared now, since execvp will read arguments until the first null pointer. The do loop, however, adds that null pointer already, which is the null pointer returned by the last invocation of strsep.

    The problem would of course also be solved too by just clearing bBuffer (the data where *pArgs is pointing to after the first command was scanned). Note that you have to do that also before scanning the first time, since you can’t assume the chars in bBuffer array are initialized to any sensible values.

    memset(bBuffer, 0, sizeof bBuffer);
    

    Place that just before the read invocation (but in any case, read only maximally BUFSIZE-1, because the terminating null character must have space too!).

    But as I’ve shown above, you don’t need this memset call. Just add the terminating null character manually.

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

Sidebar

Ask A Question

Stats

  • Questions 98k
  • Answers 98k
  • Best Answers 0
  • User 1
  • Popular
  • Answers
  • Editorial Team

    How to approach applying for a job at a company ...

    • 7 Answers
  • Editorial Team

    How to handle personal stress caused by utterly incompetent and ...

    • 5 Answers
  • Editorial Team

    What is a programmer’s life like?

    • 5 Answers
  • Editorial Team
    Editorial Team added an answer There's really no need to use GQL here - it… May 11, 2026 at 7:38 pm
  • Editorial Team
    Editorial Team added an answer My guess is that in the linked CodeGuru article, the… May 11, 2026 at 7:38 pm
  • Editorial Team
    Editorial Team added an answer Have a look at emacswiki - autoindent As suggested there,… May 11, 2026 at 7:38 pm

Related Questions

I ran into a problem. Wrote the following code snippet: teksti = teksti.Trim() teksti
I am currently running into a problem where an element is coming back from
Seemingly simple, but I cannot find anything relevant on the web. What is the
Configuring TinyMCE to allow for tags, based on a customer requirement. My config is
Is it possible to replace javascript w/ HTML if JavaScript is not enabled on

Trending Tags

analytics british company computer developers django employee employer english facebook french google interview javascript language life php programmer programs salary

Top Members

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.