hop-2012/util.c

63 lines
1.2 KiB
C

/* Copyright (C) 2010 Tony Garnock-Jones. All rights reserved. */
#include <stdlib.h>
#include <string.h>
#include <stdio.h>
#include <stdarg.h>
#include "cmsg_private.h"
cmsg_bytes_t cmsg_cstring_bytes(char const *cstr) {
cmsg_bytes_t result;
result.len = strlen(cstr);
result.bytes = (void *) cstr;
return result;
}
cmsg_bytes_t cmsg_bytes_malloc_dup(cmsg_bytes_t src) {
cmsg_bytes_t result;
result.len = src.len;
result.bytes = malloc(src.len);
if (result.bytes != NULL) {
memcpy(result.bytes, src.bytes, src.len);
}
return result;
}
cmsg_bytes_t cmsg_bytes_malloc(size_t amount) {
cmsg_bytes_t result;
result.len = amount;
result.bytes = malloc(amount);
return result;
}
void cmsg_bytes_free(cmsg_bytes_t bytes) {
free(bytes.bytes);
}
void die(char const *format, ...) {
va_list vl;
va_start(vl, format);
fprintf(stderr, "ERROR: ");
vfprintf(stderr, format, vl);
va_end(vl);
exit(1);
}
void warn(char const *format, ...) {
va_list vl;
va_start(vl, format);
fprintf(stderr, "WARNING: ");
vfprintf(stderr, format, vl);
va_end(vl);
}
void info(char const *format, ...) {
va_list vl;
va_start(vl, format);
fprintf(stderr, "INFO: ");
vfprintf(stderr, format, vl);
va_end(vl);
}