* * * * * 99 ways to program a hex, Part 11: C89, const correctness, assertive, GCC extensions Today's code is identical to part 9 [1] save for one line—it uses a GCC extention [2] to notify the compiler that the do_dump() function accepts NULL pointers, and that it won't throw any exceptions. > /************************************************************************* > * > * Copyright 2012 by Sean Conner. All Rights Reserved. > * > * This program is free software; you can redistribute it and/or > * modify it under the terms of the GNU General Public License > * as published by the Free Software Foundation; either version 2 > * of the License, or (at your option) any later version. > * > * This program is distributed in the hope that it will be useful, > * but WITHOUT ANY WARRANTY; without even the implied warranty of > * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the > * GNU General Public License for more details. > * > * You should have received a copy of the GNU General Public License > * along with this program; if not, write to the Free Software > * Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA. > * > * Comments, questions and criticisms can be sent to: sean@conman.org > * > *************************************************************************/ > > /* Style: C89, const correctness, assertive, GCC extensions */ > > #include > #include > #include > #include > #include > > #define LINESIZE 16 > > static void do_dump (FILE *const,FILE *const) __attribute__((nonnull,nothrow)); > > /****************************************************************/ > > int main(const int argc,char *const argv[]) > { > assert(argc >= 1); > assert(argv != NULL); > assert(argv[0] != NULL); > > if (argc == 1) > do_dump(stdin,stdout); > else > { > int i; > > for (i = 1 ; i < argc ; i++) > { > FILE *fp; > > 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 fpin,FILE *const fpout) > { > unsigned char buffer[BUFSIZ]; > unsigned char *pbyte; > size_t offset; > size_t bread; > size_t j; > char ascii[LINESIZE + 1]; > > assert(fpin != NULL); > assert(fpout != NULL); > > offset = 0; > > while((bread = fread(buffer,1,BUFSIZ,fpin)) > 0) > { > pbyte = buffer; > while (bread > 0) > { > fprintf(fpout,"%08lX: ",(unsigned long)offset); > 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) > { > size_t i; > > for (i = j ; i < LINESIZE ; i++) fprintf(fpout," "); > } > fprintf(fpout,"%s\n",ascii); > } > > if (fflush(fpout) == EOF) > { > perror("output"); > exit(EXIT_FAILURE); > } > } > } > > /***************************************************************/ > * Part 10: C99, const and restrict correctness, assertive [3] * Part 12: C99, const and restrict correctness, assertive, GCC extensions [4] [1] gopher://gopher.conman.org/0Phlog:2012/01/17.1 [2] http://gcc.gnu.org/onlinedocs/gcc/Function-Attributes.html [3] gopher://gopher.conman.org/0Phlog:2012/01/18.1 [4] gopher://gopher.conman.org/0Phlog:2012/01/20.2 Email Sean Conner at sean@conman.org .