Author | Jakob Wakeling <[email protected]> |
Date | 2020-08-14 08:31:45 |
Commit | cb2f93b3b4b12ebf3ee6fa0c0ed2f20a64d41528 |
Parent | fd85dcb59b7bede580449046b4c0d8c45000a8fc |
Update error source files
Diffstat
M | CMakeLists.txt | | | 2 | -- |
D | ext/error.h | | | 48 | ------------------------------------------------ |
A | src/error.c | | | 55 | +++++++++++++++++++++++++++++++++++++++++++++++++++++++ |
A | src/error.h | | | 43 | +++++++++++++++++++++++++++++++++++++++++++ |
M | src/obfi.c | | | 14 | +++++++------- |
5 files changed, 105 insertions, 57 deletions
diff --git a/CMakeLists.txt b/CMakeLists.txt index d8756de..e57013d 100644 --- a/CMakeLists.txt +++ b/CMakeLists.txt @@ -9,5 +9,3 @@ SET(CMAKE_RUNTIME_OUTPUT_DIRECTORY ${PROJECT_SOURCE_DIR}/bin) FILE(GLOB SOURCES ${CMAKE_SOURCE_DIR}/src/*) ADD_EXECUTABLE(obfi ${SOURCES}) - -TARGET_INCLUDE_DIRECTORIES(obfi PRIVATE ${CMAKE_SOURCE_DIR}/ext) diff --git a/ext/error.h b/ext/error.h deleted file mode 100644 index 91c0796..0000000 --- a/ext/error.h +++ /dev/null @@ -1,48 +0,0 @@ -// error.h, version 1.0.0 -// OMKOV error library -// Copyright (C) 2020, Jakob Wakeling -// All rights reserved. - -/* -OMKOV Public Domain Licence, version 1.0 - -Permission is hereby granted to deal with this software and its associated -documentation files without restriction. - -THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR -IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS -FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE COPYRIGHT -HOLDERS OR CONTRIBUTORS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, -WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN -CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS WITH THE SOFTWARE. -*/ - -#ifndef OMKOV_ERROR_H_38W06M3W -#define OMKOV_ERROR_H_38W06M3W - -#ifdef __cplusplus -extern "C" { -#endif // __cplusplus - -#include <stdarg.h> -#include <stdio.h> -#include <stdlib.h> - -#define serrno strerror(errno) - -static void error(int status, const char *format, ...) { - va_list ap; va_start(ap, format); fflush(stdout); - vfprintf(stderr, format, ap); fputc('\n', stderr); - va_end(ap); exit(status); -} - -static void warn(const char *format, ...) { - va_list ap; va_start(ap, format); fflush(stdout); - vfprintf(stderr, format, ap); fputc('\n', stderr); - va_end(ap); return; -} - -#ifdef __cplusplus -} -#endif // __cplusplus -#endif // OMKOV_ERROR_H_38W06M3W diff --git a/src/error.c b/src/error.c new file mode 100644 index 0000000..f246723 --- /dev/null +++ b/src/error.c @@ -0,0 +1,55 @@ +// error.c, version 1.0.1 +// Error source file for OMKOV lib +// Copyright (C) 2020, Jakob Wakeling +// All rights reserved. + +/* +OMKOV Permissive Licence, version 1.0 + +Permission is hereby granted, free of charge, to any person obtaining a copy of +this software and associated documentation files (the "Software"), to deal with +the Software without restriction, including without limitation the rights to +use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of +the Software, and to permit persons to whom the Software is furnished to do so, +subject to the following conditions: + +* Redistributions of source code must retain the above copyright notice, this + list of conditions and the following disclaimers. +* Redistributions in binary form must reproduce the above copyright notice, this + list of conditions and the following disclaimers in the documentation and/or + other materials provided with the distribution. +* Neither the names of the copyright holders, nor the names of its contributors + may be used to endorse or promote products derived from this Software without + specific prior written permission. + +THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR +IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS +FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE COPYRIGHT +HOLDERS OR CONTRIBUTORS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, +WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN +CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS WITH THE SOFTWARE. +*/ + +#include "error.h" + +#include <errno.h> +#include <stdarg.h> +#include <stdio.h> +#include <stdlib.h> +#include <string.h> + +char *A0 = NULL; + +_Noreturn void error(int status, const char *format, ...) { + fflush(stdout); if (A0) { fputs(A0, stderr); fputs(": ", stderr); } + va_list ap; va_start(ap, format); vfprintf(stderr, format, ap); va_end(ap); + fputc('\n', stderr); exit(status); +} + +void warn(const char *format, ...) { + fflush(stdout); if (A0) { fputs(A0, stderr); fputs(": ", stderr); } + va_list ap; va_start(ap, format); vfprintf(stderr, format, ap); va_end(ap); + fputc('\n', stderr); return; +} + +char *serr(void) { return strerror(errno); } diff --git a/src/error.h b/src/error.h new file mode 100644 index 0000000..d0d4585 --- /dev/null +++ b/src/error.h @@ -0,0 +1,43 @@ +// error.h, version 1.0.1 +// Error header file for OMKOV lib +// Copyright (C) 2020, Jakob Wakeling +// All rights reserved. + +/* +OMKOV Permissive Licence, version 1.0 + +Permission is hereby granted, free of charge, to any person obtaining a copy of +this software and associated documentation files (the "Software"), to deal with +the Software without restriction, including without limitation the rights to +use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of +the Software, and to permit persons to whom the Software is furnished to do so, +subject to the following conditions: + +* Redistributions of source code must retain the above copyright notice, this + list of conditions and the following disclaimers. +* Redistributions in binary form must reproduce the above copyright notice, this + list of conditions and the following disclaimers in the documentation and/or + other materials provided with the distribution. +* Neither the names of the copyright holders, nor the names of its contributors + may be used to endorse or promote products derived from this Software without + specific prior written permission. + +THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR +IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS +FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE COPYRIGHT +HOLDERS OR CONTRIBUTORS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, +WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN +CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS WITH THE SOFTWARE. +*/ + +#ifndef OMKOV_LIB_ERROR_H_38W06M3W +#define OMKOV_LIB_ERROR_H_38W06M3W + +extern char *A0; + +extern _Noreturn void error(int status, const char *format, ...); +extern void warn(const char *format, ...); + +extern char *serr(void); + +#endif // OMKOV_LIB_ERROR_H_38W06M3W diff --git a/src/obfi.c b/src/obfi.c index 9b744e7..7140e49 100644 --- a/src/obfi.c +++ b/src/obfi.c @@ -45,19 +45,19 @@ CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS WITH THE SOFTWARE. static inline int cmp(const char *s1, const char *s2); static inline int run(const char *b, const long *c); -int main(int ac, char *av[]) { - if (ac == 1) { error(1, "%s: missing operand", av[0]); } +int main(int ac, char *av[]) { A0 = av[0]; + if (ac == 1) { error(1, "missing operand"); } FILE *fi = fopen(av[1], "r"); - if (!fi) { error(1, "%s: %s: %s", av[0], av[1], serrno); } + if (!fi) { error(1, "%s: %s", av[1], serr()); } fseek(fi, 0, SEEK_END); size_t fl = ftell(fi); rewind(fi); char *fb = malloc(fl + 1 * sizeof (*fb)); fb[fl] = 0; - if (!fb) { error(1, "%s: %s", av[0], serrno); } + if (!fb) { error(1, "%s", serr()); } long *fc = malloc(fl * sizeof (*fc)); - if (!fc) { error(1, "%s: %s", av[0], serrno); } + if (!fc) { error(1, "%s", serr()); } fread(fb, 1, fl, fi); fclose(fi); @@ -109,14 +109,14 @@ int main(int ac, char *av[]) { } size_t l = 1024, t = 0, *S = malloc(l * sizeof (*S)); - if (!S) { error(1, "%s: %s", av[0], serrno); } + if (!S) { error(1, "%s", serr()); } for (size_t i = 0; fb[i]; ++i) switch (fb[i]) { case 5: { S[t++] = i; break; } case 6: { --t; fc[S[t]] = i - S[t]; fc[i] = S[t] - i; break; } } - if (run(fb, fc)) { error(1, "%s: %s", av[0], serrno); } + if (run(fb, fc)) { error(1, "%s", serr()); } free(S); free(fb); free(fc); return 0; }