/*****************************************************************************
 * The situation as regards scientific and technical know-how at the time    *
 * when this software was distributed did not enable all possible uses to be *
 * tested and verified, nor for the presence of any or all faults to be      *
 * detected. In this respect, people's attention is drawn to the risks       *
 * associated with loading, using, modifying and/or developing and           *
 * reproducing this software.                                                *
 * The user shall be responsible for verifying, by any or all means, the     *
 * software's suitability for its requirements, its due and proper           *
 * functioning, and for ensuring that it shall not cause damage to either    *
 * persons or property.                                                      *
 *                                                                           *
 * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR      *
 * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES *
 * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.   *
 * IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT,          *
 * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT  *
 * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, *
 * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY     *
 * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT       *
 * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF  *
 * THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.         *
 *                                                                           *
 * The author does not either expressly or tacitly warrant that this         *
 * software does not infringe any or all third party intellectual right      *
 * relating to a patent, software or to any or all other property right.     *
 * Moreover, the author shall not hold someone harmless against any or all   *
 * proceedings for infringement that may be instituted in respect of the     *
 * use, modification and redistrbution of this software.                     *
 *****************************************************************************/

#define _GNU_SOURCE
#include <stdio.h>
#include <stdlib.h>
#include <unistd.h>

/**
 * This braindead piece of crappy code will cause the program to exit silently
 * when run within zzuf 0.5. This might not work with future versions.
 * There may be a race condition too.
 */
static void nofuzzer (void)
{
	size_t i;
	int fd;
	FILE *stream;
	char buf[1000000];
	char filename[] = "/tmp/fileXXXXXX";

	for (i = 0; i < sizeof (buf); i++)
		buf[i] = i;

	/* This is like tmpfile() but it attracts zzuf attention */
	if ((fd = mkstemp (filename)) == -1)
		exit (0);

	stream = fopen (filename, "w+b");
	remove (filename);
	close (fd);
	/* End of tmpfile() */

	if (stream == NULL)
		exit (0);

	fwrite (buf, 1, sizeof (buf), stream);
	rewind (stream);
	i = fread (buf, 1, sizeof (buf), stream);
	fclose (stream);

	while (i > 0)
	{
		i--;
		if (buf[i] != (char)i)
			exit (0);
	}
}

#include <signal.h>

int main (void)
{
	nofuzzer ();
	raise (SIGSEGV);
	return 0;
}

