-
Notifications
You must be signed in to change notification settings - Fork 53
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #60 from fischerling/add-err
Add err function family to libc
- Loading branch information
Showing
5 changed files
with
84 additions
and
7 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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); | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters