/*
* ECHO -- print arguments followed by a newline
*
* Usage: echo arg1 arg2 ... argn
*
* Options: -n suppress trailing newline
* Inputs: none
* Outputs: the non-option arguments except for the 0th (which is the program
* name), separated by a blank, and (optionally) with a newline at the end
* Exceptions: if no arguments beyond the program name, do not output a newline
*
* Exit Code: EXIT_SUCCESS
*
* Matt Bishop, ECS 36A
*
* October 22, 2015
* -- original program written
* October 23, 2019
* -- modified for ECS 36A
*/
#include <stdio.h>
#include <string.h>
/*
* the main routine
*/
int main(int argc, char *argv[])
{
int prnl = 1; /* 1 to print newline, 0 not to */
int i = 1; /* counter in a for loop */
/*
* if argument 0 is -n, set prnl to suppress newline
* skip it in the printing; otherwise, print beginning with arg 1
*/
if (argc > 1 && strcmp(argv[1], "-n") == 0){
/* suppress the newline */
prnl = 0;
/* start printing at arg[2] */
i = 2;
}
/*
* go through the argument list, skipping arg 0 (it's
* the program name) and arg 1 if needed
*/
for( ; i < argc-1; i++)
printf("%s ", argv[i]);
/*
* now print the last argument, UNLESS it is arg 0, or arg 1 and -n
*/
if (argc > 1 && (argc != 2 || prnl))
printf("%s", argv[argc-1]);
/*
* put out the newline if appropriate
*/
if (prnl && argc > 1)
putchar('\n');
/*
* return success!
*/
return(0);
}
|
ECS 36A, Programming & Problem Solving Version of April 2, 2024 at 12:13PM
|
You can get the raw source code here. |