99 ways to program a hex, Part 10: C99, const and restrict correctness, assertive

It's pretty much the same as yesterday's version [1], only in C99 [2]:

/*************************************************************************
*************************************************************************/
/* Style: C99, const and restrict correctness, assertive */
#include <stdio.h>
#include <ctype.h>
#include <string.h>
#include <stdlib.h>
#include <assert.h>
#define LINESIZE 16
static void do_dump (FILE *const restrict,FILE *const restrict);
/****************************************************************/
int main(const int argc,char *const restrict argv[])
{
assert(argc >= 1);
assert(argv != NULL);
assert(argv[0] != NULL);
if (argc == 1)
do_dump(stdin,stdout);
else
{
for (int i = 1 ; i < argc ; i++)
{
FILE *fp = fopen(argv[i],"rb");
if (fp == NULL)
{
perror(argv[i]);
continue;
}
printf("-----%s-----\n",argv[i]);
do_dump(fp,stdout);
fclose(fp);
}
}
return EXIT_SUCCESS;
}
/******************************************************************/
static void do_dump(FILE *const restrict fpin,FILE *const restrict fpout)
{
unsigned char buffer[BUFSIZ];
size_t offset;
size_t bread;
assert(fpin != NULL);
assert(fpout != NULL);
offset = 0;
while((bread = fread(buffer,1,BUFSIZ,fpin)) > 0)
{
unsigned char *pbyte = buffer;
while (bread > 0)
{
char ascii[LINESIZE + 1];
fprintf(fpout,"%08lX: ",(unsigned long)offset);
size_t j = 0;
do
{
fprintf(fpout,"%02X ",*pbyte);
if (isprint(*pbyte))
ascii [j] = *pbyte;
else
ascii [j] = '.';
pbyte ++;
offset ++;
j ++;
bread --;
} while ((j < LINESIZE) && (bread > 0));
ascii [j] = '\0';
if (j < LINESIZE)
for (size_t i = j ; i < LINESIZE ; i++) fprintf(fpout," ");
fprintf(fpout,"%s\n",ascii);
}
if (fflush(fpout) == EOF)
{
perror("output");
exit(EXIT_FAILURE);
}
}
}
/***************************************************************/

Not much to say about today's version.

=> [1] /boston/2012/01/17.1 | [2] http://en.wikipedia.org/wiki/C99 | [3] /boston/2012/01/17.1 | [4] /boston/2012/01/19.2

=> Gemini Mention this post | Contact the author

Proxy Information
Original URL
gemini://gemini.conman.org/boston/2012/01/18.1
Status Code
Success (20)
Meta
text/gemini
Capsule Response Time
846.701234 milliseconds
Gemini-to-HTML Time
1.697965 milliseconds

This content has been proxied by September (3851b).