🚀 go-pugleaf

RetroBBS NetNews Server

Inspired by RockSolid Light RIP Retro Guy

Article View: comp.lang.functional
Article #935

Completely functional program in C

#935
From: Mike Goodrich
Date: Wed, 23 Dec 1992 21:07
110 lines
2727 bytes
Greetings,
	The following is an example of a functional program in C I wrote after
reading MaClannan's book on FP. I would like to invite comments from the FP
community. I was pleased to find that the Sun C optimizer knows about tail recursion optimization so that this program runs as fast as the conventional
C version that it is based on.





/*
 * Hexdmp.c
 * 
 * Author: Mike Goodrich (804) 864-1790   goodrich@128.155.25.38  (goodrich@lynx.larc.nasa.gov)
 * 
 * Purpose to dump a given file to STDOUT in hex and ascii
 *
 * NOTE: This version was rewritten to adhere to the 'functional program' paradigm
 *
 * Usage:	hexdmp <filename>
 * 
 * Note:	If compiling under Microsoft C must use -J to overide signed char
 * default.
 */



#include	<stdio.h>
#define REC_SIZE	16
static unsigned char HexTab[] = {'0', '1', '2', '3', '4', '5', '6', '7', '8', '9',
'a', 'b', 'c', 'd', 'e', 'f'};

void            PutHexLine(), PutTextLine(), DoLine(), DumpFile();


main(argc, argv)
	int             argc;
	char          **argv;
{
	unsigned char   buff[REC_SIZE];

	if (argc < 2) {
		printf("\nUsage: hexdmp <filename>\n");
		exit(0);
	}
	DumpFile(fopen(argv[1], "rb"), buff);
}

void
PutTextLine(NumRead, buff)
	short           NumRead;
	unsigned char  *buff;
{
	switch (NumRead) {
	case 0:
		return;
	default:
		if (*buff < ' ' || *buff > '~') {	/* printable ? */
			printf(".");
		} else {
			printf("%c", *buff);
		}
		PutTextLine(NumRead - 1, buff + 1);
	}
}

void
PutHexLine(NumRead, buff)
	short           NumRead;
	unsigned char  *buff;
{
	switch (NumRead) {
	case 0:
		printf("\t");
		return;
	default:
		printf("%02x ", *buff);
		PutHexLine(NumRead - 1, buff + 1);
	}
}

void
DoLine(NumRead, buff, count, fp)
	short           NumRead, count;
	unsigned char  *buff;
	FILE           *fp;
{
	switch (NumRead) {
	case 0:
		return;
	default:
		printf("\n<%d>\t", count * REC_SIZE);
		PutHexLine(NumRead, buff);
		PutTextLine(NumRead, buff);
		DoLine(fread(buff, 1, REC_SIZE, fp), buff, count + 1, fp);
	}
}

void 
DumpFile(fp, buff)
	FILE *fp;
	unsigned char  *buff;
{
	if (fp == NULL) {
		printf("Error: cannot open input file\n");
		exit(-1);
	}
	DoLine(fread(buff, 1, REC_SIZE, fp), buff, 0, fp);
}
Technical Details
Message-ID: <1hakebINNdsa@rave.larc.nasa.gov>
Path: arc1-ca.pugleaf.net!sparky!uunet!news.larc.nasa.gov!lynx.larc.nasa.gov!goodrich

Show Full Headers
Complete Article Headers
Path: arc1-ca.pugleaf.net!sparky!uunet!news.larc.nasa.gov!lynx.larc.nasa.gov!goodrich
From: goodrich@lynx.larc.nasa.gov (Mike Goodrich)
Newsgroups: comp.lang.functional
Subject: Completely functional program in C
Date: 23 Dec 1992 21:07:23 GMT
Organization: NASA Langley Research Center, Hampton, VA  USA
Lines: 113
Distribution: world
Message-ID: <1hakebINNdsa@rave.larc.nasa.gov>
NNTP-Posting-Host: lynx.larc.nasa.gov