C with Linux Environment Variables LOOK AT MY CODE BELOW. WE ARE SUPPOSED TO BUI
ID: 3853413 • Letter: C
Question
C with Linux Environment Variables
LOOK AT MY CODE BELOW. WE ARE SUPPOSED TO BUILD ON THAT CODE
Here is my code from previous task that is mentioned. Please build upon it.
#include
#include
#include
#include
void main(){
int main()
{
char *input;
int count = 0;
while ( count < 5 )
{
input = readline("Enter Command: ");
add_history(input);
system(input);
++count;
}
return 0;
}
Task 1: Environment Variables (16 points) Every Unix process uses/includes an array of strings called the "environment." Each of these strings is of the form "name value" (This is by convention. There is no requirement that every string in the environment be of this form). There are various functions available for accessing these strings by the "name" contained in each one. These name/value pairs are referred to as environment variables. Below is a list of some of the functions you can use to access/manipulate these environment variables: setenv, putenv: change/set the value of a variable given the name getenv: get the value of an environment variable given the name. unsetenv: remove an environment variable. There is also a global variable named environ that can be used to access the environment strings. For the details on any of the above, use the unix man command ("man environ" or "man setenv", ...) You are to extend the code that you wrote in Task 0 so that it reads and executes the following commandsExplanation / Answer
NOTE: I have used unix system calls in 'C' to demonstrate the environment variable operations. Let me know if you are expecting something else with clear description of what exactly required. I will revert back within 24 hours.
Code:
/*
* C Program to Show Environment variables Operations
*/
#include <stdio.h>
#include <stdlib.h>
#include <unistd.h>
extern char **environ;
/* function prototype */
void print_all_environ(void);
void get_env(char *);
void set_env(char *, char *);
void rem_env(char *);
int main(int argc, char *argv[])
{
int choice = 0;
char name[50], value[50];
/* Providing menu options to demonstrate environment variable operations */
while (choice != 5){
printf(" ====================================");
printf(" Environment Variable operations Menu");
printf(" ====================================");
printf(" 1. Print Existing Environment Variables");
printf(" 2. Get a Given Enviornment Variable");
printf(" 3. Set a Environment Variable");
printf(" 4. Delete a Given Environment Variable");
printf(" 5. Quit");
printf(" Enter your choice(1/2/3/4/5)? ");
scanf("%d", &choice);
switch(choice){
case 1: print_all_environ();
break;
case 2: printf(" Enter the env variable name: ");
scanf("%s", name);
get_env(name);
break;
case 3: printf(" Enter the env variable to set: ");
scanf("%s", name);
printf(" Enter the value you want to set: ");
scanf("%s", value);
set_env(name, value);
break;
case 4: printf(" Enter the env variable to delete: ");
scanf("%s", name);
rem_env(name);
break;
}
}
return 0;
}
/* display all the given environment variables */
void print_all_environ(void)
{
int i = 0;
printf(" Environment Variables Available are");
while (environ[i]){
printf(" %s", environ[i++]);
}
printf(" ");
}
/* Get a given environment variable */
void get_env(char *str)
{
printf(" The environment variable for given variable name is");
printf(" '%s=%s'", str, getenv(str));
printf(" ");
}
/* Set a given environment variable with a value */
void set_env(char *str, char *value)
{
setenv(str, value, 1);
printf(" Environment varaible after setting the value is");
printf(" '%s=%s'", str, getenv(str));
printf(" ");
}
/* Remove a given environment variable */
void rem_env(char *str)
{
unsetenv(str);
printf(" Environment varaible after deleting the value is");
printf(" '%s=%s'", str, getenv(str));
print_all_environ();
}
Execution and output:
Unix Terminal> cc env.c
Unix Terminal> ./a.out
====================================
Environment Variable operations Menu
====================================
1. Print Existing Environment Variables
2. Get a Given Enviornment Variable
3. Set a Environment Variable
4. Delete a Given Environment Variable
5. Quit
Enter your choice(1/2/3/4/5)? 1
Environment Variables Available are
TERM_PROGRAM=Apple_Terminal
SHELL=/bin/false
TERM=xterm-256color
TMPDIR=/var/folders/zf/z2j63j0j0xn8mhxk5y1ktp65xvw_4r/T/
Apple_PubSub_Socket_Render=/private/tmp/com.apple.launchd.EnhdORef24/Render
TERM_PROGRAM_VERSION=388.1
TERM_SESSION_ID=DEB7F5B2-EAEB-4B54-821F-229B7507AC62
USER=bonkv
SSH_AUTH_SOCK=/private/tmp/com.apple.launchd.8oT95p7lkH/Listeners
__CF_USER_TEXT_ENCODING=0x7BBE2898:0x0:0x0
PATH=/usr/local/bin:/usr/bin:/bin:/usr/sbin:/sbin
PWD=/Users/bonkv/Chegg/C
LANG=en_US.UTF-8
XPC_FLAGS=0x0
XPC_SERVICE_NAME=0
SHLVL=1
HOME=/Users/bonkv
LOGNAME=bonkv
_=./a.out
OLDPWD=/Users/bonkv/Chegg/Python
====================================
Environment Variable operations Menu
====================================
1. Print Existing Environment Variables
2. Get a Given Enviornment Variable
3. Set a Environment Variable
4. Delete a Given Environment Variable
5. Quit
Enter your choice(1/2/3/4/5)? 2
Enter the env variable name: TERM
The environment variable for given variable name is 'TERM=xterm-256color'
====================================
Environment Variable operations Menu
====================================
1. Print Existing Environment Variables
2. Get a Given Enviornment Variable
3. Set a Environment Variable
4. Delete a Given Environment Variable
5. Quit
Enter your choice(1/2/3/4/5)? 3
Enter the env variable to set: TEST
Enter the value you want to set: TRUE
Environment varaible after setting the value is 'TEST=TRUE'
====================================
Environment Variable operations Menu
====================================
1. Print Existing Environment Variables
2. Get a Given Enviornment Variable
3. Set a Environment Variable
4. Delete a Given Environment Variable
5. Quit
Enter your choice(1/2/3/4/5)? 4
Enter the env variable to delete: TEST
Environment varaible after deleting the value is 'TEST=(null)'
Environment Variables Available are
TERM_PROGRAM=Apple_Terminal
SHELL=/bin/false
TERM=xterm-256color
TMPDIR=/var/folders/zf/z2j63j0j0xn8mhxk5y1ktp65xvw_4r/T/
Apple_PubSub_Socket_Render=/private/tmp/com.apple.launchd.EnhdORef24/Render
TERM_PROGRAM_VERSION=388.1
TERM_SESSION_ID=DEB7F5B2-EAEB-4B54-821F-229B7507AC62
USER=bonkv
SSH_AUTH_SOCK=/private/tmp/com.apple.launchd.8oT95p7lkH/Listeners
__CF_USER_TEXT_ENCODING=0x7BBE2898:0x0:0x0
PATH=/usr/local/bin:/usr/bin:/bin:/usr/sbin:/sbin
PWD=/Users/bonkv/Chegg/C
LANG=en_US.UTF-8
XPC_FLAGS=0x0
XPC_SERVICE_NAME=0
SHLVL=1
HOME=/Users/bonkv
LOGNAME=bonkv
_=./a.out
OLDPWD=/Users/bonkv/Chegg/Python
====================================
Environment Variable operations Menu
====================================
1. Print Existing Environment Variables
2. Get a Given Enviornment Variable
3. Set a Environment Variable
4. Delete a Given Environment Variable
5. Quit
Enter your choice(1/2/3/4/5)? 5
Unix Terminal>
Related Questions
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.