mcat.c

/*
 * how to use dyngets
 *
 * this program is just like cat(1) but copies line by line, not character by character
 *
 * Matt Bishop, ECS 36A
 * * May 14, 2024: original version
 */
#include <stdio.h>
#include <string.h>

/*
 * forward declatations
 */
char *dyngets(char *, int, FILE *);		/* get a line of "unlimited" length */

/*
 * dumps the contents of a file to the standard output
 * prefixed by line number and length
 */
void filedump(FILE *f)
{
	char *in;		/* points to input buffer from dyngets */
	int lineno = 0;		/* current line number */

	/* loop through the file printing a line at a time */
	/* prefix each by line number and length           */
	while((in = dyngets(NULL, 20, f)) != NULL){
		lineno += 1;			/* one more line */
		/* print line number, length, and line (note newline is preserved) */
		printf("%4d(%3d): %s", i, (int)strlen(in), in);
	}
}
	
/*
 *the main program
 */
int main(int argc, char *argv[])
{
	int i;			/* counter in a for loop */
	FILE *fp;		/* points to input file */

	/*
	 * no options; process command line files one by one
	 */
	for(i = 1; i < argc; i++){
		/* open the file; tally number of errors on error */
		if ((fp = fopen(argv[i], "r")) != NULL){
			/* it lives -- dump it and close it */
			filedump(fp);
			fclose(fp);
		}
		else{
			/* can't read it -- give file name and why */
			perror(argv[i]);
		}
	}

	/* th-th-that's all, folks! */
	return(0);
}


UC Davis sigil
Matt Bishop
Office: 2209 Watershed Sciences
Phone: +1 (530) 752-8060
Email: mabishop@ucdavis.edu
ECS 36A, Programming & Problem Solving
Version of April 2, 2024 at 12:13PM

You can get the raw source code here.

Valid HTML 4.01 Transitional Built with BBEdit Built on a Macintosh