Files
libakstdlib/src/stdlib.c

129 lines
4.3 KiB
C

#include <akstdlib.h>
#include <stdio.h>
#include <stdlib.h>
#include <errno.h>
#include <string.h>
akerr_ErrorContext AKERR_NOIGNORE *aksl_malloc(size_t size, void **dst)
{
PREPARE_ERROR(e);
FAIL_ZERO_RETURN(e, dst, AKERR_NULLPOINTER, "NULL");
*dst = malloc(size);
FAIL_ZERO_RETURN(e, *dst, errno, "%ld bytes", size);
SUCCEED_RETURN(e);
}
akerr_ErrorContext AKERR_NOIGNORE *aksl_free(void *ptr)
{
PREPARE_ERROR(e);
FAIL_ZERO_RETURN(e, ptr, AKERR_NULLPOINTER, "NULL");
free(ptr);
SUCCEED_RETURN(e);
}
akerr_ErrorContext AKERR_NOIGNORE *aksl_memset(void *s, int c, size_t n)
{
PREPARE_ERROR(e);
FAIL_ZERO_RETURN(e, s, AKERR_NULLPOINTER, "s=%p", s);
FAIL_ZERO_RETURN(e, memset(s, c, n), errno, "Failed to memset");
SUCCEED_RETURN(e);
}
akerr_ErrorContext AKERR_NOIGNORE *aksl_memcpy(void *d, void *s, size_t n)
{
PREPARE_ERROR(e);
FAIL_ZERO_RETURN(e, d, AKERR_NULLPOINTER, "d=%p, s=%p", d, s);
FAIL_ZERO_RETURN(e, s, AKERR_NULLPOINTER, "d=%p, s=%p", d, s);
FAIL_ZERO_RETURN(e, (memcpy(d, s, n) == d), errno, "Failed to memcpy");
SUCCEED_RETURN(e);
}
akerr_ErrorContext AKERR_NOIGNORE *aksl_fopen(
char *pathname,
char *mode,
FILE **fp)
{
PREPARE_ERROR(e);
FAIL_ZERO_RETURN(e, fp, AKERR_NULLPOINTER, "NULL");
*fp = fopen(pathname, mode);
FAIL_ZERO_RETURN(e, *fp, errno, "%s", pathname);
SUCCEED_RETURN(e);
}
akerr_ErrorContext AKERR_NOIGNORE *aksl_fread(
void *ptr,
size_t size, size_t nmemb,
FILE *fp)
{
size_t nmemr;
PREPARE_ERROR(e);
FAIL_ZERO_RETURN(e, fp, AKERR_NULLPOINTER, "NULL");
nmemr = fread(ptr, size, nmemb, fp);
if ( nmemr != nmemb ) {
FAIL_NONZERO_RETURN(e, feof(fp), AKERR_EOF, "EOF reached");
FAIL_NONZERO_RETURN(e, ferror(fp), AKERR_IO, "Error reading file");
}
SUCCEED_RETURN(e);
}
akerr_ErrorContext AKERR_NOIGNORE *aksl_fwrite(
void *ptr,
size_t size, size_t nmemb,
FILE *fp)
{
size_t nmemw;
PREPARE_ERROR(e);
FAIL_ZERO_RETURN(e, fp, AKERR_NULLPOINTER, "NULL argument");
nmemw = fwrite(ptr, size, nmemb, fp);
if ( nmemw != nmemb ) {
FAIL_NONZERO_RETURN(e, feof(fp), AKERR_EOF, "EOF reached");
FAIL_NONZERO_RETURN(e, ferror(fp), AKERR_IO, "Error reading file");
}
SUCCEED_RETURN(e);
}
akerr_ErrorContext AKERR_NOIGNORE *aksl_fclose(FILE *stream)
{
PREPARE_ERROR(e);
FAIL_ZERO_RETURN(e, stream, AKERR_NULLPOINTER, "NULL");
FAIL_NONZERO_RETURN(e, fclose(stream), errno, "Failed to fclose");
SUCCEED_RETURN(e);
}
akerr_ErrorContext AKERR_NOIGNORE *aksl_printf(int *count, const char *restrict format, ...)
{
va_list args;
PREPARE_ERROR(e);
FAIL_ZERO_RETURN(e, count, AKERR_NULLPOINTER, "count=%p, format=%p", (void *)count, (void *)format);
FAIL_ZERO_RETURN(e, format, AKERR_NULLPOINTER, "count=%p, format=%p", (void *)count, (void *)format);
*count = vprintf(format, args);
FAIL_NONZERO_RETURN(e, (*count == -1), errno, "Short write");
SUCCEED_RETURN(e);
}
akerr_ErrorContext AKERR_NOIGNORE *aksl_fprintf(int *count, FILE *restrict stream, const char *restrict format, ...)
{
va_list args;
PREPARE_ERROR(e);
FAIL_ZERO_RETURN(e, count, AKERR_NULLPOINTER, "count=%p, stream=%p, format=%p", (void *)count, (void *)stream, (void *)format);
FAIL_ZERO_RETURN(e, stream, AKERR_NULLPOINTER, "count=%p, stream=%p, format=%p", (void *)count, (void *)stream, (void *)format);
FAIL_ZERO_RETURN(e, format, AKERR_NULLPOINTER, "count=%p, stream=%p, format=%p", (void *)count, (void *)stream, (void *)format);
*count = vfprintf(stream, format, args);
FAIL_NONZERO_RETURN(e, (*count == -1), errno, "Short write");
SUCCEED_RETURN(e);
}
akerr_ErrorContext AKERR_NOIGNORE *aksl_sprintf(int *count, char *restrict str, const char *restrict format, ...)
{
va_list args;
PREPARE_ERROR(e);
FAIL_ZERO_RETURN(e, count, AKERR_NULLPOINTER, "count=%p, str=%p, format=%p", (void *)count, (void *)str, (void *)format);
FAIL_ZERO_RETURN(e, str, AKERR_NULLPOINTER, "count=%p, str=%p, format=%p", (void *)count, (void *)str, (void *)format);
FAIL_ZERO_RETURN(e, format, AKERR_NULLPOINTER, "count=%p, str=%p, format=%p", (void *)count, (void *)str, (void *)format);
*count = vsprintf(str, format, args);
FAIL_NONZERO_RETURN(e, (*count == -1), errno, "Short write");
SUCCEED_RETURN(e);
}