Skip to content

Commit

Permalink
Merge pull request #60 from fischerling/add-err
Browse files Browse the repository at this point in the history
Add err function family to libc
  • Loading branch information
Galfurian authored Mar 15, 2024
2 parents 66d94dc + 8f45516 commit ca116ec
Show file tree
Hide file tree
Showing 5 changed files with 84 additions and 7 deletions.
1 change: 1 addition & 0 deletions libc/CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@
# Add the library.
add_library(
libc
${CMAKE_SOURCE_DIR}/libc/src/err.c
${CMAKE_SOURCE_DIR}/libc/src/shadow.c
${CMAKE_SOURCE_DIR}/libc/src/unistd/dup.c
${CMAKE_SOURCE_DIR}/libc/src/stdio.c
Expand Down
20 changes: 20 additions & 0 deletions libc/inc/err.h
Original file line number Diff line number Diff line change
@@ -0,0 +1,20 @@
/// @file err.h
/// @brief Contains err functions
/// @copyright (c) 2024 This file is distributed under the MIT License.
/// See LICENSE.md for details.

#pragma once

#include <stdarg.h>

/// @brief Print formatted error message on stderr and exit
/// @param eval The exit value.
/// @param fmt The format string.
void err(int eval, const char *fmt, ...);
void verr(int eval, const char *fmt, va_list args);

/// @brief Print formatted message on stderr without appending an error message and exit
/// @param eval The exit value.
/// @param fmt The format string.
void errx(int eval, const char *fmt, ...);
void verrx(int eval, const char *fmt, va_list args);
10 changes: 9 additions & 1 deletion libc/inc/stdio.h
Original file line number Diff line number Diff line change
Expand Up @@ -83,13 +83,21 @@ int printf(const char *fmt, ...);
int sprintf(char *str, const char *fmt, ...);

#ifndef __KERNEL__
/// @brief The same as sprintf, but it putput on file.
/// @brief Write formatted output to a file.
/// @param fd The file descriptor associated with the file.
/// @param fmt Format string, following the same specifications as printf.
/// @param ... The list of arguments.
/// @return On success, the total number of characters written is returned.
/// On failure, a negative number is returned.
int fprintf(int fd, const char *fmt, ...);

/// @brief Write formatted data from variable argument list to a file.
/// @param fd The file descriptor associated with the file.
/// @param fmt Format string, following the same specifications as printf.
/// @param args A variable arguments list.
/// @return On success, the total number of characters written is returned.
/// On failure, a negative number is returned.
int vfprintf(int fd, const char *fmt, va_list args);
#endif

/// @brief Write formatted data from variable argument list to string.
Expand Down
42 changes: 42 additions & 0 deletions libc/src/err.c
Original file line number Diff line number Diff line change
@@ -0,0 +1,42 @@
/// @file err.h
/// @brief Contains err functions
/// @copyright (c) 2024 This file is distributed under the MIT License.
/// See LICENSE.md for details.
#include <err.h>
#include <stdio.h>
#include <stdarg.h>
#include <stdio.h>
#include <sys/unistd.h>

void verr(int status, const char *fmt, va_list ap)
{
if (fmt) {
vfprintf(STDERR_FILENO, fmt, ap);
fprintf(STDERR_FILENO, ": ");
}
perror(0);
exit(status);
}

void verrx(int status, const char *fmt, va_list ap)
{
if (fmt) vfprintf(STDERR_FILENO, fmt, ap);
fprintf(STDERR_FILENO, "\n");
exit(status);
}

void err(int status, const char *fmt, ...)
{
va_list ap;
va_start(ap, fmt);
verr(status, fmt, ap);
va_end(ap);
}

void errx(int status, const char *fmt, ...)
{
va_list ap;
va_start(ap, fmt);
verrx(status, fmt, ap);
va_end(ap);
}
18 changes: 12 additions & 6 deletions libc/src/vsprintf.c
Original file line number Diff line number Diff line change
Expand Up @@ -690,14 +690,10 @@ int sprintf(char *str, const char *fmt, ...)
return len;
}

int fprintf(int fd, const char *fmt, ...)
int vfprintf(int fd, const char *fmt, va_list args)
{
char buffer[4096];
va_list ap;

va_start(ap, fmt);
int len = vsprintf(buffer, fmt, ap);
va_end(ap);
int len = vsprintf(buffer, fmt, args);

if (len > 0) {
if (write(fd, buffer, len) <= 0) {
Expand All @@ -707,3 +703,13 @@ int fprintf(int fd, const char *fmt, ...)
}
return -1;
}

int fprintf(int fd, const char *fmt, ...)
{
va_list ap;
va_start(ap, fmt);
int ret = vfprintf(fd, fmt, ap);
va_end(ap);

return ret;
}

0 comments on commit ca116ec

Please sign in to comment.