Skip to content
Permalink
Browse files

Step 11: continue to apply the methodology.

  • Loading branch information...
Ben Laurie
Ben Laurie committed Apr 28, 2012
1 parent 81b0c3c commit 1df47b5c640bf0db2c3ba18190bb3f89ec159ea5
Showing with 141 additions and 68 deletions.
  1. +7 −0 contrib/bzip2/CONVERSION
  2. +34 −41 contrib/bzip2/bzip2.c
  3. +74 −26 contrib/bzip2/bzip2_wrapped.c
  4. +26 −1 contrib/bzip2/bzip2_wrapped.h
@@ -86,3 +86,10 @@
but luckily the child never does anything but set it to NULL. So
invent a new wrapped function that does this. Note that once more
we leave dangling ends to be dealt with later.

11. Continue mechanically. Note that we remove a cast on smallMode,
which could undo our constification.

Eventually, it all builds.

But there's one more thing to do...
@@ -59,9 +59,9 @@
#include "capsicum.h"
#include <assert.h>

#define ERROR_IF_EOF(i) { if ((i) == EOF) ioError(); }
#define ERROR_IF_NOT_ZERO(i) { if ((i) != 0) ioError(); }
#define ERROR_IF_MINUS_ONE(i) { if ((i) == (-1)) ioError(); }
#define ERROR_IF_EOF(i) { if ((i) == EOF) wrapped_ioError(); }
#define ERROR_IF_NOT_ZERO(i) { if ((i) != 0) wrapped_ioError(); }
#define ERROR_IF_MINUS_ONE(i) { if ((i) == (-1)) wrapped_ioError(); }


/*---------------------------------------------*/
@@ -89,12 +89,6 @@
# define APPEND_FLAG(root, name) \
root=snocString((root), (name))

# ifdef __GNUC__
# define NORETURN __attribute__ ((noreturn))
# else
# define NORETURN /**/
# endif

# ifdef __DJGPP__
# include <io.h>
# include <fcntl.h>
@@ -159,7 +153,6 @@
Some more stuff for all platforms :-)
--*/

typedef char Char;
typedef short Int16;
typedef unsigned short UInt16;

@@ -171,6 +164,12 @@ typedef unsigned short UInt16;
#define blockSize100k _blockSize100k
#undef verbosity
#define verbosity _verbosity
#undef smallMode
#define smallMode _smallMode
#undef noisy
#define noisy _noisy
#undef forceOverwrite
#define forceOverwrite _forceOverwrite

Int32 verbosity;
Bool keepInputFiles, smallMode, deleteOutputOnInterrupt;
@@ -191,26 +190,27 @@ Int32 exitValue;
Int32 opMode;
Int32 srcMode;

#define FILE_NAME_LEN 1034

Int32 longestFileName;
#undef inName
#define inName _inName
Char inName [FILE_NAME_LEN];
Char outName[FILE_NAME_LEN];
Char tmpName[FILE_NAME_LEN];
#undef progName
#define progName _progName
Char *progName;
Char progNameReally[FILE_NAME_LEN];
FILE *outputHandleJustInCase;

void wrapped_clear_outputHandleJustInCase(void) {
outputHandleJustInCase = NULL;
}

#undef workFactor
#define workFactor _workFactor
Int32 workFactor;

static void panic ( const Char* ) NORETURN;
static void ioError ( void ) NORETURN;
static void outOfMemory ( void ) NORETURN;
static void configError ( void ) NORETURN;
static void crcError ( void ) NORETURN;
static void cleanUpAndFail ( Int32 ) NORETURN;
static void compressedStreamEOF ( void ) NORETURN;

static void copyFileName ( Char*, Char* );
static void* myMalloc ( Int32 );
@@ -362,8 +362,7 @@ void cleanUpAndFail ( Int32 ec )


/*---------------------------------------------*/
static
void panic ( const Char* s )
void wrapped_panic ( const Char* s )
{
fprintf ( stderr,
"\n%s: PANIC -- internal consistency error:\n"
@@ -377,8 +376,7 @@ void panic ( const Char* s )


/*---------------------------------------------*/
static
void crcError ( void )
void wrapped_crcError ( void )
{
fprintf ( stderr,
"\n%s: Data integrity error when decompressing.\n",
@@ -390,8 +388,7 @@ void crcError ( void )


/*---------------------------------------------*/
static
void compressedStreamEOF ( void )
void wrapped_compressedStreamEOF ( void )
{
if (noisy) {
fprintf ( stderr,
@@ -407,8 +404,7 @@ void compressedStreamEOF ( void )


/*---------------------------------------------*/
static
void ioError ( void )
void wrapped_ioError ( void )
{
fprintf ( stderr,
"\n%s: I/O or other error, bailing out. "
@@ -488,8 +484,7 @@ void mySIGSEGVorSIGBUScatcher ( IntNative n )


/*---------------------------------------------*/
static
void outOfMemory ( void )
void wrapped_outOfMemory ( void )
{
fprintf ( stderr,
"\n%s: couldn't allocate enough memory\n",
@@ -500,8 +495,7 @@ void outOfMemory ( void )


/*---------------------------------------------*/
static
void configError ( void )
void wrapped_configError ( void )
{
fprintf ( stderr,
"bzip2: I'm not configured correctly for this platform!\n"
@@ -681,8 +675,7 @@ void applySavedTimeInfoToOutputFile ( Char *dstName )
# endif
}

static
void applySavedFileAttrToOutputFile ( IntNative fd )
void wrapped_applySavedFileAttrToOutputFile ( IntNative fd )
{
# if BZ_UNIX
IntNative retVal;
@@ -702,7 +695,7 @@ static void unwrap_applySavedFileAttrToOutputFile(int fd) {

lc_read_int(fd, &ofd);
assert(ofd == output_fd);
applySavedFileAttrToOutputFile(ofd);
wrapped_applySavedFileAttrToOutputFile(ofd);
lc_write_void(fd);
}

@@ -768,7 +761,7 @@ void compress ( Char *name )
deleteOutputOnInterrupt = False;

if (name == NULL && srcMode != SM_I2O)
panic ( "compress: bad modes\n" );
wrapped_panic ( "compress: bad modes\n" );

switch (srcMode) {
case SM_I2O:
@@ -907,7 +900,7 @@ void compress ( Char *name )
break;

default:
panic ( "compress: bad srcMode" );
wrapped_panic ( "compress: bad srcMode" );
break;
}

@@ -951,7 +944,7 @@ void uncompress ( Char *name )
deleteOutputOnInterrupt = False;

if (name == NULL && srcMode != SM_I2O)
panic ( "uncompress: bad modes\n" );
wrapped_panic ( "uncompress: bad modes\n" );

cantGuess = False;
switch (srcMode) {
@@ -1084,7 +1077,7 @@ void uncompress ( Char *name )
break;

default:
panic ( "uncompress: bad srcMode" );
wrapped_panic ( "uncompress: bad srcMode" );
break;
}

@@ -1146,7 +1139,7 @@ void testf ( Char *name )
deleteOutputOnInterrupt = False;

if (name == NULL && srcMode != SM_I2O)
panic ( "testf: bad modes\n" );
wrapped_panic ( "testf: bad modes\n" );

copyFileName ( outName, (Char*)"(none)" );
switch (srcMode) {
@@ -1205,7 +1198,7 @@ void testf ( Char *name )
break;

default:
panic ( "testf: bad srcMode" );
wrapped_panic ( "testf: bad srcMode" );
break;
}

@@ -1334,7 +1327,7 @@ void *myMalloc ( Int32 n )
void* p;

p = malloc ( (size_t)n );
if (p == NULL) outOfMemory ();
if (p == NULL) wrapped_outOfMemory ();
return p;
}

@@ -1413,7 +1406,7 @@ IntNative main ( IntNative argc, Char *argv[] )
if (sizeof(Int32) != 4 || sizeof(UInt32) != 4 ||
sizeof(Int16) != 2 || sizeof(UInt16) != 2 ||
sizeof(Char) != 1 || sizeof(UChar) != 1)
configError();
wrapped_configError();

/*-- Initialise --*/
outputHandleJustInCase = NULL;

0 comments on commit 1df47b5

Please sign in to comment.
You can’t perform that action at this time.