# # Search for non-reentrant calls in source code. # # See http://developers.sun.com/solaris/articles/multithreaded.html # # For further details. # # S M Harrison 28/8/04 # import re,sys,string,os result_file = "report.html" bad_functions = [ "ctime","localtime","gmtime","asctime","strtok", "gethostbyname","gethostent","gethostbyaddr","getservent","getservbyname", "getservbyport","getprotobyname","getnetbyname","getnetbyaddr","getrpcbynam", "getrpcbynumber","getrpcent","rand","ctermid","tmpnam","readdir","getlogin", "getpwent","getpwnam","getpwuid","getspent","getspnam","getgrnam","getgrgid", "getnetent","getrpcbyname","tempnam","fgetpwent","fgetgrent","getgrent", "fgetspent","ecvt","gcvt","fcvt","lastlock" ] searches = {} print "Compiling re expressions..." for i in bad_functions : cre = re.compile( r"[^\w:.]%s\s*\("%i ) searches[i] = cre print "Done." c_exts = [".CPP",".C",".H"] context = 6 out = "" startpoint = "." if sys.argv[1:] : startpoint = sys.argv[1] for root, dirs, names in os.walk( startpoint ) : for i in names : f,e = os.path.splitext( i) if string.upper(e) in c_exts : p = os.path.join( root, i ) lines = file(p).readlines() count = 0 for i in lines: count = count + 1 for j in bad_functions: res = searches[j].search( i ) if res != None: out += '

WARNING! Found %s in file %s.

'%(j,p,p) out += "
\n"
						for k in xrange( max( 0,count-context ), min(len(lines)-1, context+count ) ) :
							tmp = lines[k]
							tmp = string.replace( tmp, j, ""+j+"" )
							out += tmp
						out += "
\n" out += "" file( result_file, "w" ).write( out ) print "Written",result_file