lab5 Pipe Signals
  • Introduction
  • Signals
    • kill() system call
    • Custom signal handler
  • Knowing about pipe
    • Pipe Overview
    • System call pipe()
  • Learn to use pipe
    • Pipe Creation
    • Pipe with fork
    • Exercise 1
  • Connecting two programs with pipe
    • Implement ls | less in C
  • Shell Examples
    • Get Current Path
    • Change Directory
    • Change Environment Variables
  • Reference
Powered by GitBook
On this page

Was this helpful?

  1. Shell Examples

Change Environment Variables

If you wish to change the environment variables, you can provide a new set directly in exec*() members, or by using setenv().

#include <stdlib.h>
int setenv(const char *name, const char *value, int overwrite);

Remember to set the overwrite flag to 1 in order to apply new configuration to existing variable!

/* Shell/setenv.c */
#include <stdio.h>
#include <stdlib.h>
#include <unistd.h>
#include <string.h>
#include <errno.h>

int main(int argc,char *argv[])
{ 
    char *command1[] = {"shutdown",NULL};
    printf("Running shutdown.. it is in /sbin :P \n\n");
    setenv("PATH","/bin:/usr/bin:.",1); 
    execvp(*command1,command1);

 if(errno == ENOENT) 
    printf("No Command found...\n\n"); 
 else
    printf("I dont know...\n"); 
    return 0;
}

You will get No Command found... because we hide shutdown from execvp by excluding the /sbin from PATH, in which programs find available commands to execute.

PreviousChange DirectoryNextReference

Last updated 5 months ago

Was this helpful?