Question: Write a C program that sorts the environment variables passed to the program based on environment variable names. In the class, you were shown with

Write a C program that sorts the environment variables passed to the program based on environment variable names. In the class, you were shown with a program printing out all the environment variables passed to the program. Each environment variable has a variable name (the part before the '=' sign) and the a variable value (the part after the '=' sign). For example, the following three entries in envp (i.e., three environment variables), the names are USER, PWD, and HOME, respectively, and the values are ubuntu, /tmp, and /home/ubuntu.

envp[5] = "USER=ubuntu"

envp[6] = "PWD=/tmp"

envp[7] = "HOME=/home/ubuntu"

Your program needs to sort the environment variables. You can directly sort the environment variables by exchanging the pointers saved in envp. You may also choose to create another data structure of your choice (e.g., another array of pointers, or linked list). But, no matter which method you choose, your program must print out the environment variables, including their names and values, in ascending order determined by applyingstrcmp() on their names.

For example, the three entries above are sorted by calling strcmp() to compare USER, PWD, and HOME. Since the strcmp() calls determine that "HOME" < "PWD" and "PWD"<"USER", your program should print out

HOME=/home/ubuntu

PWD=/tmp

USER=ubuntu

Step by Step Solution

There are 3 Steps involved in it

1 Expert Approved Answer
Step: 1 Unlock blur-text-image
Question Has Been Solved by an Expert!

Get step-by-step solutions from verified subject matter experts

Step: 2 Unlock
Step: 3 Unlock

Students Have Also Explored These Related Databases Questions!