diff --git a/include/akstdlib.h b/include/akstdlib.h index a61c5da..a2aec1a 100644 --- a/include/akstdlib.h +++ b/include/akstdlib.h @@ -21,5 +21,10 @@ akerr_ErrorContext AKERR_NOIGNORE *aksl_printf(int *count, const char *restrict akerr_ErrorContext AKERR_NOIGNORE *aksl_fprintf(int *count, FILE *restrict stream, const char *restrict format, ...); akerr_ErrorContext AKERR_NOIGNORE *aksl_sprintf(int *count, char *restrict str, const char *restrict format, ...); +akerr_ErrorContext AKERR_NOIGNORE *aksl_atoi(const char *nptr, int *dest); +akerr_ErrorContext AKERR_NOIGNORE *aksl_atol(const char *nptr, long *dest); +akerr_ErrorContext AKERR_NOIGNORE *aksl_atoll(const char *nptr, long long *dest); + +akerr_ErrorContext AKERR_NOIGNORE *aksl_realpath(const char *restrict path, char *restrict resolved_path); #endif diff --git a/src/stdlib.c b/src/stdlib.c index fcd6f4d..3d2507f 100644 --- a/src/stdlib.c +++ b/src/stdlib.c @@ -126,3 +126,40 @@ akerr_ErrorContext AKERR_NOIGNORE *aksl_sprintf(int *count, char *restrict str, FAIL_NONZERO_RETURN(e, (*count == -1), errno, "Short write"); SUCCEED_RETURN(e); } + +akerr_ErrorContext AKERR_NOIGNORE *aksl_atoi(const char *nptr, int *dest) +{ + PREPARE_ERROR(e); + FAIL_ZERO_RETURN(e, nptr, AKERR_NULLPOINTER, "nptr=%p, dest=%p", (void *)nptr, (void *)dest); + FAIL_ZERO_RETURN(e, dest, AKERR_NULLPOINTER, "nptr=%p, dest=%p", (void *)nptr, (void *)dest); + *dest = atoi(nptr); + SUCCEED_RETURN(e); +} + +akerr_ErrorContext AKERR_NOIGNORE *aksl_atol(const char *nptr, long *dest) +{ + PREPARE_ERROR(e); + FAIL_ZERO_RETURN(e, nptr, AKERR_NULLPOINTER, "nptr=%p, dest=%p", (void *)nptr, (void *)dest); + FAIL_ZERO_RETURN(e, dest, AKERR_NULLPOINTER, "nptr=%p, dest=%p", (void *)nptr, (void *)dest); + *dest = atol(nptr); + SUCCEED_RETURN(e); +} + +akerr_ErrorContext AKERR_NOIGNORE *aksl_atoll(const char *nptr, long long *dest) +{ + PREPARE_ERROR(e); + FAIL_ZERO_RETURN(e, nptr, AKERR_NULLPOINTER, "nptr=%p, dest=%p", (void *)nptr, (void *)dest); + FAIL_ZERO_RETURN(e, dest, AKERR_NULLPOINTER, "nptr=%p, dest=%p", (void *)nptr, (void *)dest); + *dest = atoll(nptr); + SUCCEED_RETURN(e); +} + +akerr_ErrorContext AKERR_NOIGNORE *aksl_realpath(const char *restrict path, char *restrict resolved_path) +{ + char *result = NULL; + PREPARE_ERROR(e); + FAIL_ZERO_RETURN(e, path, AKERR_NULLPOINTER, "path=%p, dest=%p", (void *)path, (void *)resolved_path); + result = realpath(path, resolved_path); + FAIL_ZERO_RETURN(e, result, errno, "Error"); + SUCCEED_RETURN(e); +}