COMP1521 2020T2 Q5 · 10 marks · C Topic: Processes & syscalls · Source: https://cgi.cse.unsw.edu.au/~cs1521/26T1/resources/practice_exams.html
Summary: Compare whether two environment-variable values are equal
Write a C program, practice_q5.c , which takes two names of environment variables as arguments. It should print 1 iff both environment variables are set to the same value. Otherwise, if the environment variables differ in value, or either environment variable is not set, it should print 0 .
The shell command export sets an environment variable to a value; the shell command unset unsets an environment variable. In the following example, export is used to set the environment variables VAR1 , VAR2 and VAR3 , and unset is used to ensure environment variables VAR4 and VAR5 are unset.
export VAR1=hello export VAR2=good-bye export VAR3=hello unset VAR4 unset VAR5 ./practice_q5 VAR1 VAR2 0 ./practice_q5 VAR3 VAR1 1 ./practice_q5 VAR2 VAR4 0 ./practice_q5 VAR4 VAR5 0
There is no supplied code for this question.
Your program can assume it is always given 2 arguments.
Your program should always print one line of output. The line of output should contain only 0 or 1.
Your solution must be in C only.
You are not permitted to run external programs. You are not permitted to use system, popen, posix_spawn, fork or exec.
🤖 Adapted (the online judge has no environment-variable channel): read TWO lines from stdin = the two values. A line
<unset>means the variable is unset. Print1iff both are set and equal, else0.Verified on the sandbox AND cross-checked against an independent oracle.