<commit_before>/*	$Id$ */
/*
 * Copyright (c) 2012, 2014--2017 <NAME> <<EMAIL>>
 *
 * Permission to use, copy, modify, and distribute this software for any
 * purpose with or without fee is hereby granted, provided that the above
 * copyright notice and this permission notice appear in all copies.
 *
 * THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES
 * WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF
 * MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR
 * ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES
 * WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN
 * ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF
 * OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE.
 */
#ifdef HAVE_CONFIG_H
#include "config.h"
#endif
#include <sys/stat.h>
#include <sys/socket.h>
#include <sys/uio.h>
#include <sys/wait.h>

#include <assert.h>
#include <errno.h>
#include <fcntl.h>
#include <poll.h>
#include <stdarg.h>
#include <stdio.h>
#include <stdint.h>
#include <stdlib.h>
#include <string.h>
#include <unistd.h>

#include "kcgi.h"
#include "extern.h"

int
kxvasprintf(const char *file, int line, 
	char **p, const char *fmt, va_list ap)
{
	int	 len;

	if (-1 != (len = vasprintf(p, fmt, ap)))
		return(len);

	kxwarn(file, line, "vasprintf");
	return(-1);
}

int
kxasprintf(const char *file, int line, char **p, const char *fmt, ...)
{
	va_list	 ap;
	int	 len;

	va_start(ap, fmt);
	len = vasprintf(p, fmt, ap);
	va_end(ap);
	if (len != -1)
		return(len);

	kxwarn(file, line, "vasprintf");
	return(-1);
}

void *
kxcalloc(const char *file, int line, size_t nm, size_t sz)
{
	void	 *p;

	if (0 == nm || 0 == sz)
		kxwarnx(file, line, "calloc(%zu, %zu) "
			"(non-portable zero-length)", nm, sz);
	if (NULL != (p = calloc(nm, sz)))
		return(p);
	kxwarn(file, line, "calloc(%zu, %zu)", nm, sz);
	return(p);
}

void *
kxmalloc(const char *file, int line, size_t sz)
{
	void	 *p;

	if (0 == sz)
		kxwarnx(file, line, "malloc(0) "
			"(non-portable zero-length)");
	if (NULL != (p = malloc(sz)))
		return(p);
	kxwarn(file, line, "malloc(%zu)", sz);
	return(p);
}

void *
kxrealloc(const char *file, int line, void *pp, size_t sz)
{
	void	 *p;

	if (0 == sz)
		kxwarnx(file, line, "realloc(x, 0) "
			"(non-portable zero-length)");
	if (NULL != (p = realloc(pp, sz)))
		return(p);
	kxwarn(file, line, "realloc(%p, %zu)", pp, sz);
	return(p);
}

void *
kxreallocarray(const char *file, 
	int line, void *pp, size_t nm, size_t sz)
{
	void	 *p;

	if (0 == sz || 0 == nm)
		kxwarnx(file, line, "reallocarray(x, %zu, %zu) "
			"(non-portable zero-length)", nm, sz);
	if (NULL != (p = reallocarray(pp, nm, sz)))
		return(p);
	kxwarn(file, line, "reallocarray(%p, %zu, %zu)", pp, nm, sz);
	return(p);
}

char *
kxstrdup(const char *file, int line, const char *cp)
{
	char	*p;

	if (NULL == cp)
		kxwarnx(file, line, "strdup(NULL) "
			"(non-portable null pointer)");
	if (NULL != (p = strdup(cp)))
		return(p);
	kxwarn(file, line, "strdup(%p)", cp);
	return(p);
}

void
kxwarnx(const char *file, int line, const char *fmt, ...)
{
	char		buf[1024];
	va_list		ap;

	va_start(ap, fmt);
	(void)vsnprintf(buf, sizeof(buf), fmt, ap);
	va_end(ap);
	fprintf(stderr, "%s:%d: %s\n", file, line, buf);
}

void
kxwarn(const char *file, int line, const char *fmt, ...)
{
	int		e = errno;
	char		buf[1024];
	va_list		ap;

	va_start(ap, fmt);
	(void)vsnprintf(buf, sizeof(buf), fmt, ap);
	va_end(ap);
	fprintf(stderr, "%s:%d: %s: %s\n", 
		file, line, buf, strerror(e));
}

enum kcgi_err
kxwaitpid(pid_t pid)
{<commit_msg>Note that internal functions should check for bad printables.<commit_after>/*	$Id$ */
/*
 * Copyright (c) 2012, 2014--2017 <NAME> <<EMAIL>>
 *
 * Permission to use, copy, modify, and distribute this software for any
 * purpose with or without fee is hereby granted, provided that the above
 * copyright notice and this permission notice appear in all copies.
 *
 * THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES
 * WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF
 * MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR
 * ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES
 * WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN
 * ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF
 * OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE.
 */
#ifdef HAVE_CONFIG_H
#include "config.h"
#endif
#include <sys/stat.h>
#include <sys/socket.h>
#include <sys/uio.h>
#include <sys/wait.h>

#include <assert.h>
#include <errno.h>
#include <fcntl.h>
#include <poll.h>
#include <stdarg.h>
#include <stdio.h>
#include <stdint.h>
#include <stdlib.h>
#include <string.h>
#include <unistd.h>

#include "kcgi.h"
#include "extern.h"

int
kxvasprintf(const char *file, int line, 
	char **p, const char *fmt, va_list ap)
{
	int	 len;

	if (-1 != (len = vasprintf(p, fmt, ap)))
		return(len);

	kxwarn(file, line, "vasprintf");
	return(-1);
}

int
kxasprintf(const char *file, int line, char **p, const char *fmt, ...)
{
	va_list	 ap;
	int	 len;

	va_start(ap, fmt);
	len = vasprintf(p, fmt, ap);
	va_end(ap);
	if (len != -1)
		return(len);

	kxwarn(file, line, "vasprintf");
	return(-1);
}

void *
kxcalloc(const char *file, int line, size_t nm, size_t sz)
{
	void	 *p;

	if (0 == nm || 0 == sz)
		kxwarnx(file, line, "calloc(%zu, %zu) "
			"(non-portable zero-length)", nm, sz);
	if (NULL != (p = calloc(nm, sz)))
		return(p);
	kxwarn(file, line, "calloc(%zu, %zu)", nm, sz);
	return(p);
}

void *
kxmalloc(const char *file, int line, size_t sz)
{
	void	 *p;

	if (0 == sz)
		kxwarnx(file, line, "malloc(0) "
			"(non-portable zero-length)");
	if (NULL != (p = malloc(sz)))
		return(p);
	kxwarn(file, line, "malloc(%zu)", sz);
	return(p);
}

void *
kxrealloc(const char *file, int line, void *pp, size_t sz)
{
	void	 *p;

	if (0 == sz)
		kxwarnx(file, line, "realloc(x, 0) "
			"(non-portable zero-length)");
	if (NULL != (p = realloc(pp, sz)))
		return(p);
	kxwarn(file, line, "realloc(%p, %zu)", pp, sz);
	return(p);
}

void *
kxreallocarray(const char *file, 
	int line, void *pp, size_t nm, size_t sz)
{
	void	 *p;

	if (0 == sz || 0 == nm)
		kxwarnx(file, line, "reallocarray(x, %zu, %zu) "
			"(non-portable zero-length)", nm, sz);
	if (NULL != (p = reallocarray(pp, nm, sz)))
		return(p);
	kxwarn(file, line, "reallocarray(%p, %zu, %zu)", pp, nm, sz);
	return(p);
}

char *
kxstrdup(const char *file, int line, const char *cp)
{
	char	*p;

	if (NULL == cp)
		kxwarnx(file, line, "strdup(NULL) "
			"(non-portable null pointer)");
	if (NULL != (p = strdup(cp)))
		return(p);

	kxwarn(file, line, "strdup(%p)", cp);
	return(p);
}

void
kxwarnx(const char *file, int line, const char *fmt, ...)
{
	char		buf[1024];
	va_list		ap;

	va_start(ap, fmt);
	(void)vsnprintf(buf, sizeof(buf), fmt, ap);
	va_end(ap);

	/* FIXME: filter bad characters. */

	fprintf(stderr, "%s:%d: %s\n", file, line, buf);
}

void
kxwarn(const char *file, int line, const char *fmt, ...)
{
	int		e = errno;
	char		buf[1024];
	va_list		ap;

	va_start(ap, fmt);
	(void)vsnprintf(buf, sizeof(buf), fmt, ap);
	va_end(ap);

	/* FIXME: filter bad characters. */

	fprintf(stderr, "%s:%d: %s: %s\n", 
		file, line, buf, strerror(e));
}

enum kcgi_err
kxwaitpid(pid_t pid)
{
{'fraction_non_alphanumeric': 0.17459027495597995, 'fraction_numerical': 0.00866856291480428, 'mean_word_length': 4.0265486725663715, 'pattern_counts': {'":': 0, '<': 37, '<?xml version=': 0, '>': 37, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '1483913', 'n_tokens_mistral': 3151, 'n_tokens_neox': 2955, 'n_words': 910}