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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 26, 20262026-05-26T00:39:36+00:00 2026-05-26T00:39:36+00:00

I am working on a custom shell for a systems programming class. We were

  • 0

I am working on a custom shell for a systems programming class. We were instructed to implement the built-in setenv() and unsetenv() commands with a hint of check man pages for putenv().

My issue is that setenv(char*, char*, int) and putenv(char*) do not seem to be working at all. My code for executing a command entered is as follows:

//... skipping past stuff for IO redirection
pid = fork();
if(pid == 0){
    //child
    if(!strcmp(_simpleCommands[0]->_arguments[0],"printenv")){
        //check if command is "printenv"
        extern char **environ;
        int i;
        for(i = 0; environ[i] != NULL; i++){
            printf("%s\n",environ[i]);
        }
        exit(0);
    }
    if(!strcmp(_simpleCommands[0]->_arguments[0],"setenv")){
        //if command is "setenv" get parameters char* A, char* B
        char * p = _simpleCommands[0]->_arguments[1];
        char * s = _simpleCommands[0]->_arguments[2];

        //putenv(char* s) needs to be formatted A=B; A is variable B is value
        char param[strlen(p) + strlen(s) + 1];
        strcat(param,p);
        strcat(param,"=");
        strcat(param,s);
        putenv(param);
        //setenv(p,s,1);
        exit(0);
    }
    if(!strcmp(_simpleCommands[0]->_arguments[0],"unsetenv")){
        //remove environment variable
        unsetenv(_simpleCommands[0]->_arguments[0]);
        exit(0);
    }
    //execute command
    execvp(_simpleCommands[0]->_arguments[0],_simpleCommands->_arguments);
    perror("-myshell");
    _exit(1);
}

//omitting restore IO defaults...

If I run printenv it works properly, but if I try to set a new variable using either putenv() or setenv() my printenv() command returns the exact same thing, so it does not appear to be working.

As a side note, the problem may not be with the functions or how I called them, because my shell is executing the commands as though it had to format a wildcard (* or ?) which I am not sure should happen.

  • 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-26T00:39:37+00:00Added an answer on May 26, 2026 at 12:39 am

    You appear to be calling fork unconditionally before examining the command line. But some shell built-in commands need to run in the parent process, so that their effect persists. All the built-ins that manipulate the environment fall in this category.

    As an aside, I wouldn’t try to use the C library’s environment manipulation functions if I were writing a shell. I’d use three-argument main, copy envp into a data structure under my full control, and then feed that back into execve. This is partially because I’m a control freak, and partially because it’s nigh-impossible to do anything complicated with setenv and/or putenv and not have a memory leak. See this older SO question for gory details.

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

Sidebar

Related Questions

I have a working custom UserNamePasswordValidator that calls into my Oracle DB. This class
I'm working on a custom mark-release style memory allocator for the D programming language
I have a working custom search suggestions class (via http://developer.android.com/guide/topics/search/adding-custom-suggestions.html ). It currently returns
I've been working on custom Vector class. Everything works perfectly fine on Microsoft compiler,
I am working on a custom button class for my XNA game. Consequently I
I often find linq being problematic when working with custom collection object. They are
In some instances, I prefer working with custom objects instead of strongly typed datasets
I'm working on a custom CMS for my own use and was thinking about
I'm working on a custom version of a GNU/Linux live distribution. I need to
I'm working on a custom user control that essentially displays a name value pair

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.