/* * =================================================================== * miscellaneous.c : Miscellaneous functions for safe dynamic memory * allocation. * * Copyright (C) 1995-1998 by Mark Austin. * * This software is provided "as is" without express or implied warranty. * Permission is granted to use this software on any computer system, * and to redistribute it freely, subject to the following restrictions: * * 1. The authors are not responsible for the consequences of use of * this software, even if they arise from defects in the software. * 2. The origin of this software must not be misrepresented, either * by explicit claim or by omission. * 3. Altered versions must be plainly marked as such, and must not * be misrepresented as being the original software. * 4. This notice is to remain intact. * * Written by: Mark Austin June 1995 * =================================================================== */ #include #ifdef __STDC__ #include #include #include #include #else #include #endif #include "miscellaneous.h" /* * ============================================================= * safeMalloc : Safe memory allocation with malloc(). * * Input : unsigned int uiSize -- no of bytes to be allocated. * char *cpFileName -- pointer to file name. * int iLineNo -- line number. * Output : void *vpTemp -- pointer to allocated memory. * ============================================================= */ void * safeMalloc( unsigned int uiSize, char *cpFileName, int iLineNo ) { void * vpTemp; vpTemp = malloc( (unsigned) uiSize ); if (vpTemp == (void *) NULL) { fprintf( stderr, "ERROR : malloc() failed in safeMalloc()\n"); fprintf( stderr, "ERROR : safeMalloc called in file %s : line %d\n", cpFileName, iLineNo ); exit (1); } else return( vpTemp ); } /* * ================================================================ * safeCalloc : Safe memory allocation with calloc(). * * Input : unsigned int uiNoItems -- no array elements. * unsigned int uiSize -- bytes in each array element. * char cpFileName -- pointer to file name. * int iLineNo -- line number. * Output : void *vpTemp -- pointer to allocated memory. * ================================================================ */ void *safeCalloc( unsigned int uiNoItems, unsigned int uiSize, char *cpFileName, int iLineNo ) { void *vpTemp; vpTemp = calloc( (unsigned) uiNoItems, (unsigned) uiSize ); if (vpTemp == (void *) NULL) { fprintf( stderr, "ERROR : calloc() failed in safeCalloc()\n"); fprintf( stderr, "ERROR : safeCalloc called in file %s : line %d\n", cpFileName, iLineNo ); exit (1); } else return( vpTemp ); } /* * =========================================================== * SaveString : safe allocation of character strings * * Input : char *cpName -- pointer to character string. * char cpFileName -- pointer to file name. * int iLineNo -- line number. * Output : char *cpTemp -- pointer to allocated memory. * =========================================================== */ char *saveString( char *cpName, char *cpFileName, int iLineNo ) { char *cpTemp; cpTemp = (char *) safeMalloc((unsigned)(strlen( cpName ) + 1), cpFileName, iLineNo ); strcpy(cpTemp, cpName); return cpTemp; } /* * ============================================================================= * fatalError : Print Variable number of character strings error * messages, then abort program execution. * * Input : char *cpFirst -- one (or more) character strings separated by commas. * The list is terminated with (char *) NULL. * Output : void -- * ============================================================================= */ #ifdef __STDC__ void fatalError( char *cpFirst, ... ) { va_list arg_ptr; char *cpMessage; /* [a] : Print list of character string error messages */ va_start(arg_ptr, cpFirst ); for(cpMessage = cpFirst; cpMessage != NULL; cpMessage = va_arg(arg_ptr, char *)) printf("FATAL ERROR >> \"%s\"\n", cpMessage); va_end(arg_ptr); /* [b] : Terminate program execution */ exit(1); } #else /* Start case not STDC */ void fatalError( va_alist ) va_dcl { va_list arg_ptr; char *cpMessage; char *cpFirst; va_start(arg_ptr); cpFirst = va_arg(arg_ptr, char *); for(cpMessage = cpFirst; cpMessage != NULL; cpMessage = va_arg(arg_ptr, char *)) printf("FATAL ERROR >> \"%s\"\n", cpMessage); va_end(arg_ptr); exit(1); } #endif /* End case not STDC */ /* * ---------------------------------------------------------------------- * Check FILE for NULL, if so, allow another try by returning or else * exit the program entirely. Rather than exit, this could be improved * by listing the directory or providing some help in finding the correct * file names. * ---------------------------------------------------------------------- */ void CheckFile( FILE * FpCheck ) { enum { MaxChar = 10 }; char caPrompt[MaxChar]; if( NULL == FpCheck ) { fflush( stdin ); /* clear the input buffer */ printf( "\nError opening file(s). Do you want to exit (y/n)?n\b" ); scanf( "%c%*c", caPrompt ); if( '\0' == caPrompt[0] || 'n' == tolower( caPrompt[0] ) ) return; else system( "ls" ); } return; }