xref: /openbsd-src/usr.bin/sort/mem.c (revision fbdabba60c1c72dd05026f7b7b495c0c9f3dca89)
1*fbdabba6Smillert /*	$OpenBSD: mem.c,v 1.6 2015/04/02 20:30:45 millert Exp $	*/
279428148Smillert 
379428148Smillert /*-
479428148Smillert  * Copyright (C) 2009 Gabor Kovesdan <gabor@FreeBSD.org>
579428148Smillert  * Copyright (C) 2012 Oleg Moskalenko <mom040267@gmail.com>
679428148Smillert  * All rights reserved.
779428148Smillert  *
879428148Smillert  * Redistribution and use in source and binary forms, with or without
979428148Smillert  * modification, are permitted provided that the following conditions
1079428148Smillert  * are met:
1179428148Smillert  * 1. Redistributions of source code must retain the above copyright
1279428148Smillert  *    notice, this list of conditions and the following disclaimer.
1379428148Smillert  * 2. Redistributions in binary form must reproduce the above copyright
1479428148Smillert  *    notice, this list of conditions and the following disclaimer in the
1579428148Smillert  *    documentation and/or other materials provided with the distribution.
1679428148Smillert  *
1779428148Smillert  * THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND
1879428148Smillert  * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
1979428148Smillert  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
2079428148Smillert  * ARE DISCLAIMED.  IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE
2179428148Smillert  * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
2279428148Smillert  * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
2379428148Smillert  * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
2479428148Smillert  * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
2579428148Smillert  * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
2679428148Smillert  * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
2779428148Smillert  * SUCH DAMAGE.
2879428148Smillert  */
2979428148Smillert 
3079428148Smillert #include <err.h>
317c91f203Smillert #include <stdio.h>
3279428148Smillert #include <string.h>
3379428148Smillert 
3479428148Smillert #include "mem.h"
3579428148Smillert 
3679428148Smillert /*
3779428148Smillert  * malloc() wrapper.
3879428148Smillert  */
3979428148Smillert void *
sort_malloc(size_t size)4079428148Smillert sort_malloc(size_t size)
4179428148Smillert {
4279428148Smillert 	void *ptr;
4379428148Smillert 
4479428148Smillert 	if ((ptr = malloc(size)) == NULL)
4579428148Smillert 		err(2, NULL);
4679428148Smillert 	return ptr;
4779428148Smillert }
4879428148Smillert 
4979428148Smillert /*
508fd2d339Smillert  * calloc() wrapper.
518fd2d339Smillert  */
528fd2d339Smillert void *
sort_calloc(size_t nmemb,size_t size)538fd2d339Smillert sort_calloc(size_t nmemb, size_t size)
548fd2d339Smillert {
558fd2d339Smillert 	void *ptr;
568fd2d339Smillert 
578fd2d339Smillert 	if ((ptr = calloc(nmemb, size)) == NULL)
588fd2d339Smillert 		err(2, NULL);
598fd2d339Smillert 	return ptr;
608fd2d339Smillert }
618fd2d339Smillert 
628fd2d339Smillert /*
6379428148Smillert  * free() wrapper.
6479428148Smillert  */
6579428148Smillert void
sort_free(void * ptr)6679428148Smillert sort_free(void *ptr)
6779428148Smillert {
6879428148Smillert 	free(ptr);
6979428148Smillert }
7079428148Smillert 
7179428148Smillert /*
7279428148Smillert  * reallocarray() wrapper.
7379428148Smillert  */
7479428148Smillert void *
sort_reallocarray(void * ptr,size_t nmemb,size_t size)7579428148Smillert sort_reallocarray(void *ptr, size_t nmemb, size_t size)
7679428148Smillert {
7779428148Smillert 	if ((ptr = reallocarray(ptr, nmemb, size)) == NULL)
7879428148Smillert 		err(2, NULL);
7979428148Smillert 	return ptr;
8079428148Smillert }
8179428148Smillert 
8279428148Smillert char *
sort_strdup(const char * str)8379428148Smillert sort_strdup(const char *str)
8479428148Smillert {
8579428148Smillert 	char *dup;
8679428148Smillert 
8779428148Smillert 	if ((dup = strdup(str)) == NULL)
8879428148Smillert 		err(2, NULL);
8979428148Smillert 	return dup;
9079428148Smillert }
917c91f203Smillert 
927c91f203Smillert int
sort_asprintf(char ** ret,const char * fmt,...)937c91f203Smillert sort_asprintf(char **ret, const char *fmt, ...)
947c91f203Smillert {
957c91f203Smillert 	int len;
967c91f203Smillert 	va_list ap;
977c91f203Smillert 
987c91f203Smillert 	va_start(ap, fmt);
997c91f203Smillert 	len = vasprintf(ret, fmt, ap);
1007c91f203Smillert 	va_end(ap);
1017c91f203Smillert 
1027c91f203Smillert 	if (len == -1)
1037c91f203Smillert 		err(2, NULL);
1047c91f203Smillert 	return len;
1057c91f203Smillert }
106