Blame compat/strndup.c

Packit 1422b7
/* compatibility file for systems without strndup.
Packit 1422b7
 *
Packit 1422b7
 * Copyright 2015 Rainer Gerhards and Adiscon
Packit 1422b7
 *
Packit 1422b7
 * This file is part of liblognorm.
Packit 1422b7
 *
Packit 1422b7
 * Licensed under the Apache License, Version 2.0 (the "License");
Packit 1422b7
 * you may not use this file except in compliance with the License.
Packit 1422b7
 * You may obtain a copy of the License at
Packit 1422b7
 *
Packit 1422b7
 *       http://www.apache.org/licenses/LICENSE-2.0
Packit 1422b7
 *       -or-
Packit 1422b7
 *       see COPYING.ASL20 in the source distribution
Packit 1422b7
 *
Packit 1422b7
 * Unless required by applicable law or agreed to in writing, software
Packit 1422b7
 * distributed under the License is distributed on an "AS IS" BASIS,
Packit 1422b7
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
Packit 1422b7
 * See the License for the specific language governing permissions and
Packit 1422b7
 * limitations under the License.
Packit 1422b7
 */
Packit 1422b7
#include "config.h"
Packit 1422b7
#ifndef HAVE_STRNDUP
Packit 1422b7
Packit 1422b7
#include <stdlib.h>
Packit 1422b7
#include <string.h>
Packit 1422b7
char *
Packit 1422b7
strndup(const char *s, size_t n)
Packit 1422b7
{
Packit 1422b7
	const size_t len = strlen(s);
Packit 1422b7
	if(len <= n)
Packit 1422b7
		return strdup(s);
Packit 1422b7
	char *const new_s = malloc(len+1);
Packit 1422b7
	if(new_s == NULL)
Packit 1422b7
		return NULL;
Packit 1422b7
	memcpy(new_s, s, len);
Packit 1422b7
	new_s[len] = '\0';
Packit 1422b7
	return new_s;
Packit 1422b7
}
Packit 1422b7
#else /* #ifndef HAVE_STRNDUP */
Packit 1422b7
/* Solaris must have at least one symbol inside a file, so we provide
Packit 1422b7
 * it here ;-)
Packit 1422b7
 */
Packit 1422b7
void dummy_dummy_required_for_solaris_do_not_use(void)
Packit 1422b7
{
Packit 1422b7
}
Packit 1422b7
#endif /* #ifndef HAVE_STRNDUP */