Answered step by step
Verified Expert Solution
Link Copied!

Question

1 Approved Answer

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 applying strcmp() 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

Step: 1

blur-text-image

Get Instant Access to Expert-Tailored Solutions

See step-by-step solutions with expert insights and AI powered tools for academic success

Step: 2

blur-text-image

Step: 3

blur-text-image

Ace Your Homework with AI

Get the answers you need in no time with our AI-driven, step-by-step assistance

Get Started

Recommended Textbook for

Expert Oracle9i Database Administration

Authors: Sam R. Alapati

1st Edition

1590590228, 978-1590590225

More Books

Students also viewed these Databases questions

Question

=+8. Be sure you considered consumer benefits.

Answered: 1 week ago

Question

=+4. Consider competitors' campaigns. How could yours stand out?

Answered: 1 week ago

Question

=+5. Review the six categories of 50 strategies.

Answered: 1 week ago