Blame utils.h

Packit Service 80a84b
/*
Packit Service 80a84b
	Copyright(C) 2016, Red Hat, Inc., Stanislav Kozina
Packit Service 80a84b
Packit Service 80a84b
	This program is free software: you can redistribute it and/or modify
Packit Service 80a84b
	it under the terms of the GNU General Public License as published by
Packit Service 80a84b
	the Free Software Foundation, either version 3 of the License, or
Packit Service 80a84b
	(at your option) any later version.
Packit Service 80a84b
Packit Service 80a84b
	This program is distributed in the hope that it will be useful,
Packit Service 80a84b
	but WITHOUT ANY WARRANTY; without even the implied warranty of
Packit Service 80a84b
	MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
Packit Service 80a84b
	GNU General Public License for more details.
Packit Service 80a84b
Packit Service 80a84b
	You should have received a copy of the GNU General Public License
Packit Service 80a84b
	along with this program.  If not, see <http://www.gnu.org/licenses/>.
Packit Service 80a84b
*/
Packit Service 80a84b
Packit Service 80a84b
#ifndef UTILS_H_
Packit Service 80a84b
#define	UTILS_H_
Packit Service 80a84b
Packit Service 80a84b
#include <stdarg.h>
Packit Service 80a84b
#include <stdio.h>
Packit Service 80a84b
#include <stdbool.h>
Packit Service 80a84b
#include <string.h>
Packit Service 80a84b
#include <errno.h>
Packit Service 80a84b
Packit Service 80a84b
/*
Packit Service 80a84b
 * Changes to file format that keep backward compatibility call for
Packit Service 80a84b
 * incementing the minor number, changes that don't calls for
Packit Service 80a84b
 * incrementing the major number.
Packit Service 80a84b
 */
Packit Service 80a84b
#define _VERSION(s,j) _STR(s,j)
Packit Service 80a84b
#define _STR(s,j) _STR2(Version: s##.j\n)
Packit Service 80a84b
#define _STR2(s) #s
Packit Service 80a84b
Packit Service 80a84b
#define FILEFMT_VERSION_MAJOR	1
Packit Service 80a84b
#define FILEFMT_VERSION_MINOR	0
Packit Service 80a84b
#define FILEFMT_VERSION_STRING	\
Packit Service 80a84b
	_VERSION(FILEFMT_VERSION_MAJOR,FILEFMT_VERSION_MINOR)
Packit Service 80a84b
Packit Service 80a84b
#define	fail(fmt, ...)	{					\
Packit Service 80a84b
	fprintf(stderr, "%s():%d ", __func__, __LINE__);	\
Packit Service 80a84b
	fprintf(stderr, fmt, ## __VA_ARGS__);			\
Packit Service 80a84b
	exit(1);						\
Packit Service 80a84b
}
Packit Service 80a84b
Packit Service 80a84b
static inline void safe_asprintf(char **strp, const char *fmt, ...)
Packit Service 80a84b
{
Packit Service 80a84b
	va_list arglist;
Packit Service 80a84b
Packit Service 80a84b
	va_start(arglist, fmt);
Packit Service 80a84b
	if (vasprintf(strp, fmt, arglist) == -1)
Packit Service 80a84b
		fail("asprintf failed: %s", strerror(errno));
Packit Service 80a84b
	va_end(arglist);
Packit Service 80a84b
}
Packit Service 80a84b
Packit Service 80a84b
static inline void *safe_zmalloc(size_t size)
Packit Service 80a84b
{
Packit Service 80a84b
	void *result = malloc(size);
Packit Service 80a84b
Packit Service 80a84b
	if (result == NULL)
Packit Service 80a84b
		fail("Malloc of size %zu failed", size);
Packit Service 80a84b
	memset(result, 0, size);
Packit Service 80a84b
	return result;
Packit Service 80a84b
}
Packit Service 80a84b
Packit Service 80a84b
static inline void *safe_realloc(void *ptr, size_t size)
Packit Service 80a84b
{
Packit Service 80a84b
	void *result = realloc(ptr, size);
Packit Service 80a84b
Packit Service 80a84b
	if (result == NULL)
Packit Service 80a84b
		fail("Rellloc of size %zu failed: %s\n", size, strerror(errno));
Packit Service 80a84b
	return result;
Packit Service 80a84b
}
Packit Service 80a84b
Packit Service 80a84b
static inline void *safe_strdup(const char *s)
Packit Service 80a84b
{
Packit Service 80a84b
	void *result = strdup(s);
Packit Service 80a84b
Packit Service 80a84b
	if (result == NULL)
Packit Service 80a84b
		fail("strdup() of \"%s\" failed", s);
Packit Service 80a84b
	return result;
Packit Service 80a84b
}
Packit Service 80a84b
Packit Service 80a84b
static inline void *safe_strdup_or_null(const char *s)
Packit Service 80a84b
{
Packit Service 80a84b
	if (s == NULL)
Packit Service 80a84b
		return NULL;
Packit Service 80a84b
	return safe_strdup(s);
Packit Service 80a84b
}
Packit Service 80a84b
Packit Service 80a84b
static inline bool safe_streq(const char *s1, const char *s2)
Packit Service 80a84b
{
Packit Service 80a84b
	if ((s1 == NULL) != (s2 == NULL))
Packit Service 80a84b
		return false;
Packit Service 80a84b
	if (s1)
Packit Service 80a84b
		return !strcmp(s1, s2);
Packit Service 80a84b
	return true;
Packit Service 80a84b
}
Packit Service 80a84b
Packit Service 80a84b
static inline bool safe_strendswith(const char *s1, const char *s2)
Packit Service 80a84b
{
Packit Service 80a84b
	int len1, len2;
Packit Service 80a84b
Packit Service 80a84b
	if ((s1 == NULL) != (s2 == NULL))
Packit Service 80a84b
		return false;
Packit Service 80a84b
Packit Service 80a84b
	if (!s1)
Packit Service 80a84b
		return true;
Packit Service 80a84b
Packit Service 80a84b
	len1 = strlen(s1);
Packit Service 80a84b
	len2 = strlen(s2);
Packit Service 80a84b
Packit Service 80a84b
	if ((len1 == 0) || (len2 == 0))
Packit Service 80a84b
		return false;
Packit Service 80a84b
Packit Service 80a84b
	if (len2 > len1)
Packit Service 80a84b
		return false;
Packit Service 80a84b
Packit Service 80a84b
	return strcmp(s1 + len1 - len2, s2) == 0;
Packit Service 80a84b
}
Packit Service 80a84b
Packit Service 80a84b
static inline ssize_t safe_getline(char **lineptr, size_t *n, FILE *stream)
Packit Service 80a84b
{
Packit Service 80a84b
	ssize_t ret = getline(lineptr, n, stream);
Packit Service 80a84b
Packit Service 80a84b
	if ((ret == -1) && (errno != ENOENT))
Packit Service 80a84b
		fail("getline failed: %s\n", strerror(errno));
Packit Service 80a84b
Packit Service 80a84b
	return ret;
Packit Service 80a84b
}
Packit Service 80a84b
Packit Service 80a84b
static inline FILE *safe_fopen(char *filename)
Packit Service 80a84b
{
Packit Service 80a84b
	FILE *file;
Packit Service 80a84b
Packit Service 80a84b
	file = fopen(filename, "r");
Packit Service 80a84b
	if (!file)
Packit Service 80a84b
		fail("Failed to open kABI file: %s\n", filename);
Packit Service 80a84b
Packit Service 80a84b
	return file;
Packit Service 80a84b
}
Packit Service 80a84b
Packit Service 80a84b
typedef enum {
Packit Service 80a84b
	WALK_CONT,
Packit Service 80a84b
	WALK_STOP,
Packit Service 80a84b
	WALK_SKIP,
Packit Service 80a84b
} walk_rv_t;
Packit Service 80a84b
Packit Service 80a84b
extern void walk_dir(char *, bool, walk_rv_t (*)(char *, void *), void *);
Packit Service 80a84b
extern int check_is_directory(char *);
Packit Service 80a84b
extern void rec_mkdir(char *);
Packit Service 80a84b
extern void safe_rename(const char *, const char *);
Packit Service 80a84b
extern char *path_normalize(char *);
Packit Service 80a84b
extern char *filenametotype(const char *);
Packit Service 80a84b
extern char *filenametosymbol(const char *);
Packit Service 80a84b
Packit Service 80a84b
extern void global_string_keeper_init(void);
Packit Service 80a84b
extern void global_string_keeper_free(void);
Packit Service 80a84b
extern const char *global_string_get_copy(const char *string);
Packit Service 80a84b
extern const char *global_string_get_move(char *string);
Packit Service 80a84b
Packit Service 80a84b
#endif /* UTILS_H */