C and AWK questions (longish)

moss at BRL-VLD.ARPA moss at BRL-VLD.ARPA
Wed Aug 15 02:18:16 AEST 1984


From:      "Gary S. Moss (DRXBR-VLD-V)" <moss at BRL-VLD.ARPA>

 
> We are searching for debugging techniques to track down malloc/realloc 
> aborts.  Occasionally, our programs get memory faults within the stdio's 
> _flsbuf() subroutine.  Does anyone have hints on tracking down the types of 
> things that would clobber the malloc buffers?  

I believe there are DEBUG defines you can turn on in the malloc source
to compile a debugging version (depending on your OS).

> Does anyone have a routine which performs the same expansion on its 
> argument as does the Bourne shell?  That is, would expand [], ?, *, [!] 
> into appropriate file names?  

I have a boolean subroutine which returns whether or not a string matches
a pattern which can contain those operators.  The only caviat is that
although it mimics sh(1) file name expansion, it was not meant for use
with file names so it does not require that '/' or '.' at the beginning
of a file or directory name be matched explicitly.  This will be left
as an exercise to the reader [:-)].

______________ tear here __ match.c _______________________________________
/*
 *	SCCS id:	@(#) match.c	1.2
 *	Last edit: 	8/14/84 at 11:33:26
 *	Retrieved: 	8/14/84 at 11:33:45
 *	SCCS archive:	/vld/moss/work/libWIND/s.match.c
 *
 *	Author:		Gary S. Moss
 *			U. S. Army Ballistic Research Laboratory
 *			Aberdeen Proving Ground
 *			Maryland 21005
 *			(301)278-6647 or AV-283-6647
 */
static
char	sccsTag[] = "@(#) match.c	1.2	last edit 8/14/84 at 11:33:26";
#include <stdio.h>
#include <string.h>
#include "./ascii.h"
extern void	prnt1Err();

/*	m a t c h ( )
 *	if string matches pattern, return 1, else return 0
 *	special characters:
 *		*	Matches any string including the null string.
 *		?	Matches any single character.
 *		[...]	Matches any one of the characters enclosed.
 *		[!..]	Matchea any character NOT enclosed.
 *		-	May be used inside brackets to specify range
 *			(i.e. str[1-58] matches str1, str2, ... str5, str8)
 *		\	Escapes special characters.
 */
match(	 pattern,  string )
register
char	*pattern, *string;
{
	do {
		switch( pattern[0] ) {
		case '*': /* Match any string including null string.	*/
			if( pattern[1] == '0' || string[0] == '0' )
				return	1;
			while( string[0] != '0' ) {
				if( match( &pattern[1], string ) )
					return	1;
				++string;
			}
			return	0;
		case '?': /* Match any character.			*/
			break;
		case '[': /* Match one of the characters in brackets
				unless first is a '!', then match
				any character not inside brackets.
			   */
			{ register char	*rgtBracket;
			  static int	negation;

			++pattern; /* Skip over left bracket.		*/
			/* Find matching right bracket.			*/
			if( (rgtBracket = strchr( pattern, ']' )) == NULL ) {
				prnt1Err( "Unmatched '['." );
				return	0;
			}
			/* Check for negation operator.			*/
			if( pattern[0] == '!' ) {
				++pattern;
				negation = 1;
			} else {
				negation = 0;
			}	
			/* Traverse pattern inside brackets.		*/
			for(	;
				pattern < rgtBracket
			     &&	pattern[0] != string[0];
				++pattern
				)
			{
				if(	pattern[ 0] == '-'
				    &&	pattern[-1] != '\\'
					)
				{
					if(	pattern[-1] <= string[0]
					    &&	pattern[-1] != '['
					    &&	pattern[ 1] >= string[0]
					    &&	pattern[ 1] != ']'
					)
						break;
				}
			}
			if( pattern == rgtBracket ) {
				if( ! negation ) {
					return	0;
				}
			} else {
				if( negation ) {
					return	0;
				}
			}
			pattern = rgtBracket; /* Skip to right bracket.	*/
			break;
			}
		case '\\': /* Escape special character.			*/
			++pattern;
			/* WARNING: falls through to default case.	*/
		default:  /* Compare characters.			*/
			if( pattern[0] != string[0] )
				return	0;
		}
		++pattern;
		++string;
	} while( pattern[0] != '0' && string[0]  != '0' );
	if( (pattern[0] == '0' || pattern[0] == '*' ) && string[0]  == '0' )
		return	1;
	else	return	0;
}
______________ tear here __ matchtest.c ___________________________________
/*
 *	SCCS id:	@(#) matchtest.c	1.1
 *	Last edit: 	8/14/84 at 11:32:48
 *	Retrieved: 	8/14/84 at 11:33:49
 *	SCCS archive:	/vld/moss/work/libWIND/s.matchtest.c
 *
 *	Author:		Gary S. Moss
 *			U. S. Army Ballistic Research Laboratory
 *			Aberdeen Proving Ground
 *			Maryland 21005
 *			(301)278-6647 or AV-283-6647
 */
#if ! defined( lint )
static char
sccsTag[] = "@(#) matchtest.c	1.1	last edit 8/14/84 at 11:32:48";
#endif
#include <stdio.h>
extern int	match();
char	*usage[] = {
"",
"matchtest(1.1)",
"",
"Usage: matchtest [pattern string]",
"",
"If no arguments are given, the program reads words on its standard input.",
"The program writes to its standard output.",
0
};
void		prntUsage(), prnt1Err();
static char	*pattern, *string;
static char	patbuf[BUFSIZ], strbuf[BUFSIZ];
/*	m a i n ( )
 */
main( argc, argv )
char	*argv[];
{
	if( ! parsArgv( argc, argv ) ) {
		prntUsage();
		exit( 1 );
	}
	if( pattern != NULL ) {
		if( match( pattern, string ) ) {
			(void) printf(	"'%s' matches '%s'.\n",
					pattern,
					string
					);
			exit( 0 );
		} else {
			(void) printf(	"'%s' does not match '%s'.\n",
					pattern,
					string
					);
			exit( 1 );
		}
	}
	while( scanf( "%s %s", patbuf, strbuf ) == 2 ) {
		if( match( patbuf, strbuf ) ) {
			(void) printf( "'%s' matches '%s'.\n", patbuf, strbuf );
		} else {
			(void) printf(	"'%s' does not match '%s'.\n",
					patbuf,
					strbuf
					);
		}
	}		
	exit( 0 );
}

/*	p a r s A r g v ( )
 */
parsArgv( argc, argv )
register char	**argv;
{
	register int	c;
	extern int	optind;
	extern char	*optarg;

	/* Parse options.					*/
	while( (c = getopt( argc, argv, "" )) != EOF ) {
		switch( c ) {
		case '?' :
			return	0;
		}
	}
	if( argc - optind != 2 ) {
		if( argc == optind ) {
			pattern = string = NULL;
		} else {
			(void) fprintf( stderr, "Arg count wrong!\n" );
			return	0;
		}
	} else {
		pattern = argv[optind++];
		string = argv[optind++];
	}
	return	1;
}

/*	p r n t U s a g e ( )
 *	Print usage message.
 */
void
prntUsage() {
	register char	**p = usage;
	while( *p )
		(void) fprintf( stderr, "%s\n", *p++ );
	return;
}

/*	p r n t 1 E r r ( )
 *	Print error message with 1 argument.
 */
void
prnt1Err( str )
char	*str;
{
	(void) fprintf( stderr, str );
	return;
}
________________________________________________________________________
-- Moss.



More information about the Comp.lang.c mailing list