Answered step by step
Verified Expert Solution
Question
1 Approved Answer
Kindly solve this ASAP. thank you and definitely will give an headset for correct answer. Argument Passing Through the Command Line Objective To understand the
Kindly solve this ASAP. thank you and definitely will give an headset for correct answer.
Argument Passing Through the Command Line Objective To understand the operating system's roll in passing arguments and environment variables to a running program. Description Most of the utilities/commands in Linux are written in C. Many of these commands accept input(s) from the user, in the form of flags and arguments, and react differently to different inputs (e.g. gcc -o temp sample1.c). The OS also maintains a list of variables about the system running environment such as your home directory, terminal type, search path,... etc. The values are collectively known as the environment. The OS reads the user input from the command line, creates standard structures, and passes them, and the environment, to the command. If the programmer of this command can access these structures, by declaring arguments in the definition of the main program. A common convention uses argc and argv, envp as follows: void main(int arge, char **argv, char **envp) {...} where: int argc: argument count, which is the number of tokens typed on the command line. char **argv: argument vector, which is an array of these tokens. Char **envp: environment variables and their values. Now, arge, argv and envp are local variables the programmer can read and use to direct the execution of the command. Submission: Submit one compressed file that contains the following programs: 0. Printing all environment variables passed to the command "MyEnvironment" Your first deliverable is a C program (MyEnvironment.c) that prints all the environment variables of your system (Figure 0) SHELL=/bin/bash TERM=xterm-256color USER=elnasan NAME=DESKTOP-TAFOQD7 HOSTTYPE=x86_64 PWD=/home/elnasan/Labs/Lab3 LANG=en_US.UTF-8 SHLVL=1 HOME-/home/elnasan LOGNAME=elnasan XDG_DATA_DIRS=/usr/local/share:/usr/share/var/lib/snapd/desktop OLDPWD=/home/elnasan/Labs Figure 0 - Partial list of environment variables 1. Printing all arguments passed through the command MyCommand1" Your first deliverable is a C program (MyCommand1.c) that: prints all the arguments that are passed to it through the command line (Figure 1) $MyCommand1 Arg1 Arg2 $MyCommand1 Argi Arg2 Arg3 Arg4 argv[0]: MyCommandi argv[1]: Argi largv[2]: Arg2 argv[0]: MyCommandi argv[1]: Argi argv[2]: Arg2 argv[3]: Arg3 argv[4]: Arg4 Figure 1 -Sample runs of MyCommand1 with different inputs. 2. Storing and calculating the sum of whole numbers passed through the command line. (MyCommand2) Your second deliverable is a C program (MyCommand2.c) that: Takes a variable amount of non-negative whole numbers as arguments. Converts these numbers from strings to integers and stores them in intArray. Calculates the sum of all elements in intArray. Prints intArray and the sum. Assume: Arguments will always be non-negative whole numbers (no error-checking required) This means argument i can be converted to an integer using atoi (argv[i]). intArray is dynamically allocated and of size argc - 1: int *intArray; //use malloc to allocate the array Note: the first argument in the argument array is the first integer in argv, not the name of the command (intArray[0] = argv[1]); o For example: $MyCommand2 15 27 1 365 5 intArray 15 27 1 365 sum 413 5 Figure 2-a- intArray and sum after running: $My Command2 15 27 1 365 5 o Print an error message if no arguments are given (Figure 2-b). $ ./MyCommand2 15 27 1 365 5 intArray[@] = 15 intArray[1] = 27 intArray[2] = 1 intArray[3] = 365 intArray[4] = 5 Sum: 413 $ ./MyCommand2 Please input a list of non-negative whole numbers. Figure 2-b-Output of My Command2 3. Storing variable-length strings passed through the command line. (MyCommand3) Your third deliverable is a C program (MyCommand3.c) that: Stores the arguments in argv in stringArray (including argument 0,"./MyCommand3"). Fits each element of stringArray to the size of the argument +1 (to fit the null terminator). The stringArray is defined as follows: char **stringArray = NULL; Allocate a place holder for each element in the stringArray: stringArray = (char**) malloc((argc+1) * sizeof (char*)); Allocate the right size for each argument (based on the user input): stringArray(argIndex] = (char*) malloc((strlen (argv[argIndex]) + 1) * sizeof (char)); Copy the values typed by the user to the stringArray (Figure 3-a) and print them (Figure 3-b). stringArray M Y o m m a nd3 U M a s 10 R P 10 S U N Y lo H a v a d NULL Figure 3-a-stringArray populated after running: $My Command3 UMass RPI SUNY Harvard $ ./myCommand3 UMass RPI SUNY Harvard stringArray[O]: ./myCommands stringArray[1]: UMass stringArray[2] : RPI stringArray[3]: SUNY stringArray[4]: Harvard Figure 3-b-output after running: $My Command3 UMass RPI SUNY Harvard Try other examples (Figure 3-c). $ ./My Command3 UMass RPI SUNY Harvard Rochester MIT stringArray[@]: ./MyCommand3 stringArray[1]: UMass stringArray[2]: RPI stringArray[3]: SUNY stringArray[4]: Harvard stringArray[5]: Rochester stringArray[6]: MIT Figure 3-c- Output of MyCommand3 Important: make sure to free the space you allocate before your program exits. 4. Combine MyCommand2 and MyCommand3 into one command that runs one or the other based on user's choice (the first argument) Your fourth deliverable is a C program (MyCommand4.c) that: Takes "sum" or "print" as an argument. If the argument is "sum" it runs exactly like MyCommand2. If the argument is "print" it runs exactly like MyCommand3. Note: Most of the code from the previous sections can be directly recycled, but the code for MyCommand2 will have to be adjusted as, thanks to the additional argument, the numbers will begin at argument index 2, not 1. Print an error message if there are not enough arguments, or if "sum" or "print" is not entered (Figure 4). $ ./MyCommande sum 15 27 1 365 5 intArray[@] = 15 intArray[1] = 27 intArray[2] = 1 intArray[3] = 365 intArray[4] = 5 Sum: 413 $ ./MyCommand4 print UMass RPI SUNY Harvard Rochester MIT stringArray[@]: ./MyCommand stringArray[1]: print stringArray[2]: UMass stringArray[3]: RPI stringArray[4]: SUNY stringArray[5]: Harvard stringArray[6]: Rochester stringArray[7]: MIT $ ./My Command4 notsum 4 3 Usage: /My Command4 sum {list of numbers). or: /MyCommand4 print {list of strings). $ ./MyCommand Usage: /MyCommand4 sum {list of numbers). or: /MyCommand4 print {list of strings). Ciudutust af MucommandaStep by Step Solution
There are 3 Steps involved in it
Step: 1
Get Instant Access to Expert-Tailored Solutions
See step-by-step solutions with expert insights and AI powered tools for academic success
Step: 2
Step: 3
Ace Your Homework with AI
Get the answers you need in no time with our AI-driven, step-by-step assistance
Get Started