%% options copyright owner = Dirk Krause copyright year = 2015-xxxx license = bsd %% module /** @file dk-fic.c File integrity checker. */ #include "dk4conf.h" #if DK4_ON_WINDOWS #ifndef WINDOWS_H_INCLUDED #include #define WINDOWS_H_INCLUDED #endif #endif #if DK4_HAVE_UNISTD_H #ifndef UNISTD_H_INCLUDED #include #define UNISTD_H_INCLUDED 1 #endif #endif #if DK4_HAVE_PROCESS_H #ifndef PROCESS_H_INCLUDED #include #define PROCESS_H_INCLUDED 1 #endif #endif #if DK4_HAVE_SIGNAL_H #ifndef SIGNAL_H_INCLUDED #include #define SIGNAL_H_INCLUDED 1 #endif #endif #if DK4_HAVE_IO_H #ifndef IO_H_INCLUDED #include #define IO_H_INCLUDED 1 #endif #endif #if DK4_HAVE_FCNTL_H #ifndef FCNTL_H_INCLUDED #include #define FCNTL_H_INCLUDED 1 #endif #endif #include "dk4types.h" #include "dk4const.h" #include "dk4vers.h" #include "dk4app.h" #include "dk4aopt.h" #include "dk4mem.h" #include "dk4mema.h" #include "dk4fput.h" #include "dk4mpl.h" #include "dk4enc.h" #include "dk4tspdk.h" #include "dk4fopda.h" #include "dk4pathd.h" #include "dk4md.h" #include "dk4maodd.h" #include "dk4filed.h" #include "dk4fileid.h" #include "dk4filei.h" #include "dk4fileia.h" #include "dk4strd.h" #include "dk4numl.h" #include "dk4maidddu.h" $!trace-include /** Default help text, shown if help text file is not found. */ static const dkChar * const dk_fic_help_text[] = { $!text macro=dkT File integrity checker Create checksum lists for files or verify files against a checksum list. dk-fic [] [] Options: -c --check Check (verify a checksum list). -v --verbose Messages for succeeded tests. -q --quiet No messages for failed tests. -l --line-size= Size for input line buffer. --help Show this short help text. --manual *** SHOW FULL MANUAL. *** --version Show version information. --license Show license information. Typical operations: find -type f | dk-fic > checklist.txt Create check- dk-ls -r -tf -pn | dk-fic > checklist.txt sum list. dk-fic -c < checklist.txt Verify files. http://dktools.sourceforge.net $!end }; /** License conditions. */ static const dkChar * const dk_fic_license_text[] = { $!text macro=dkT,preprocessor This software uses code from the following projects, either directly or as a library: dktools Dirk Krause's tools and libraries. See http://dktools.sourceforge.net/ for more information. #if DK4_HAVE_ZLIB_H zlib Data compression library. See http://www.zlib.net/ for more information. #endif #if DK4_HAVE_BZLIB_H bzip2 Data compression program and library. See http://www.bzip.org/ for more information. #endif All the licenses below apply to the program. Licenses for used libraries are shown as found on my Scientific Linux 6.x computer in the /usr/share/doc directory on 2015-04-01. Check the project homepages of the used libraries for additional information and/or updated license terms. DK tools and libraries license ============================== Copyright (c) 2015-2016, Dirk Krause All rights reserved. Redistribution and use in source and binary forms, with or without modification, are permitted provided that the following conditions are met: * Redistributions of source code must retain the above copyright notice, this list of conditions and the following disclaimer. * Redistributions in binary form must reproduce the above copyright notice, this list of conditions and the following disclaimer in the documentation and/or other materials provided with the distribution. * Neither the name of the Dirk Krause nor the names of contributors may be used to endorse or promote products derived from this software without specific prior written permission. THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. #if DK4_HAVE_ZLIB_H Zlib license ============ (C) 1995-2004 Jean-loup Gailly and Mark Adler This software is provided 'as-is', without any express or implied warranty. In no event will the authors be held liable for any damages arising from the use of this software. Permission is granted to anyone to use this software for any purpose, including commercial applications, and to alter it and redistribute it freely, subject to the following restrictions: 1. The origin of this software must not be misrepresented; you must not claim that you wrote the original software. If you use this software in a product, an acknowledgment in the product documentation would be appreciated but is not required. 2. Altered source versions must be plainly marked as such, and must not be misrepresented as being the original software. 3. This notice may not be removed or altered from any source distribution. Jean-loup Gailly Mark Adler jloup@gzip.org madler@alumni.caltech.edu #endif #if DK4_HAVE_BZLIB_H Bzip2 and libbzip2 library license ================================== This program, "bzip2", the associated library "libbzip2", and all documentation, are copyright (C) 1996-2007 Julian R Seward. All rights reserved. Redistribution and use in source and binary forms, with or without modification, are permitted provided that the following conditions are met: 1. Redistributions of source code must retain the above copyright notice, this list of conditions and the following disclaimer. 2. The origin of this software must not be misrepresented; you must not claim that you wrote the original software. If you use this software in a product, an acknowledgment in the product documentation would be appreciated but is not required. 3. Altered source versions must be plainly marked as such, and must not be misrepresented as being the original software. 4. The name of the author may not be used to endorse or promote products derived from this software without specific prior written permission. THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. Julian Seward, jseward@bzip.org bzip2/libbzip2 version 1.0.5 of 10 December 2007 #endif $!end }; /** Keywords used by the program, not localized. */ static const dkChar * const dk_fic_kwnl[] = { $!string-table macro=dkT # # 0 Application group name # dktools # # 1 Help file text name # dk-fic.txt # # 2 String table file # dk-fic.str # # 3 Preference name for line buffer size. # line.size # # 4 File name for standard input # stdin # # 5 File opening mode for binary reading # rb # # 6 Path separator for Windows # \\ # # 7 First output line if SHA-512 not available # \# dk-fic 3\n # # 8 First output line if SHA-512 is available # \# dk-fic 4\n # # 9 Keyword dk-fic must appear in line 1 # dk-fic $!end }; /** Default keywords, localized. */ static const dkChar * const dk_fic_kw_def[] = { $!string-table macro=dkT # # 0 Error message: Failed to set up signal handlers # Failed to set up signal handlers! # # 1 Error message: Failed to restore signal handlers # Failed to restore signal handlers! # # 2 Test passed # PASSED # # 3 Test failed # FAILED # # 4 Error message: Failed to read data from file! # Failed to read data from file! # # 5 6 Error message: Failed to convert file size to text! # Failed to convert file size to text for file:\n\t" "! # # 7 8 Error message: Failed to finish at least one message digest! # Failed to finish at least one message digest for file:\n\t" "! # # 9 10 Error message: Failed to add data to message digest! # Failed to add data to message digest for file:\n\t" "! # # 11 12 Error message: Failed to initialize message digest! # Failed to initialize at least one message digest for file:\n\t" "! # # 13 14 Error message: not a regular file! # Not a regular file:\n\t" "! # # 15 16 Error message: Failed to obtain file size # Failed to obtain file size:\n\t" "! # # 17 Warning: SHA-512 not supported # SHA-512 message digest not supported, SHA-512 checksums ignored. # # 18 Syntax error # Syntax error, first line must be "# dk-fic 3" or "# dk-fic 4"! # # 19 20 Syntax error, data line expected: # Syntax error, expected:\n\tmd5 sha-1 ripemd-160 sha-512 size name Syntax error, expected:\n\tmd5 sha-1 ripemd-160 size name # # 21 22 Syntax, error, invalid file size: # Syntax error, invalid file size: " "! # # 23 Error message: Failed to set up text line processing! # Failed to set up text line processing! # # 24 Error message: Missing support for a message digest! # Build problem: At least one required message digest type is not supported! # # 25 Error message: Options -q and -v are mutually exclusive! # Options -q and -v are mutually exclusive! # # 26 27 Error message: Encoding not found! # Encoding not found: " "! # # 28 Error message: Problems while writing output! # Problems while writing output! # # 29 Error message: Signal SIGPIPE received! # Signal SIGPIPE received! # # 30 Error message: Aborted by SIGINT signal! # Aborted by SIGINT signal! # # 31 Error message: Aborted by SIGTERM signal! # Aborted by SIGTERM signal! # # 32 33 34 Decoding/encoding/processing error # Decoding error! Encoding error! Processing error! # # 35 36 37 38 39 40 Decoding/encoding/processing error! # Decoding error!\n\tByte: Encoding error!\n\tByte: Processing error!\n\tByte: ,\n\tCharacter: ,\n\tPosition in line: . $!end }; /* VARIABLES */ /** Options for the program. */ static const dk4_option_specification_t dk_fic_options[] = { /* Check (input is a previously generated checksum list. */ { dkT('c'), dkT("check"), DK4_OPT_ARG_NONE }, /* Input line size. */ { dkT('l'), dkT("line-size"), DK4_OPT_ARG_SIZE }, /* Quite when checking (only set exit status code). */ { dkT('q'), dkT("quiet"), DK4_OPT_ARG_NONE }, /* Verbose (write report line for succeeded test too). */ { dkT('v'), dkT("verbose"), DK4_OPT_ARG_NONE }, /* Expected input encoding. */ { dkT('i'), dkT("input-encoding"), DK4_OPT_ARG_STRING }, }; /** Data buffer for reading the file. */ static char data_buf[4096]; /** Default input line buffer. */ static dkChar ibuf_def[256 + DK4_MAX_PATH]; #if DK4_HAVE_OPENSSL_MD5_H && DK4_HAVE_OPENSSL_SHA_H && DK4_HAVE_OPENSSL_RIPEMD_H static dk4_md_ctx_t ctx_md5; static dk4_md_ctx_t ctx_sha1; static dk4_md_ctx_t ctx_ripemd160; #if DK4_HAVE_SHA512 static dk4_md_ctx_t ctx_sha512; #endif #endif /** Allocated input buffer for larger line size. */ static dkChar *ibuf_all = NULL; /** Pointer to real input buffer to use. */ static dkChar *ibuf_ptr = ibuf_def; /** Input line buffer size. */ static size_t ibuf_sz = DK4_SIZEOF(ibuf_def,dkChar); /** Application structure. */ static dk4_app_t *app = NULL; /** Message array, localized messages or default texts. */ static const dkChar * const *dk_fic_msg = dk_fic_kw_def; /** Number of elements in the dk_fic_msg array. */ static size_t dk_fic_sz_msg = sizeof(dk_fic_kw_def)/sizeof(DK4_PCDKCHAR) - 1; /** Number of elements in the dk_fic_options array. */ static const size_t dk_fic_sz_options = sizeof(dk_fic_options)/sizeof(dk4_option_specification_t); /** Length of file size to print. */ static size_t dk_fic_num_len = 2 * sizeof(dk4_um_t); /** Number of message digests (3 or 4). */ static size_t dk_fic_num_mds = 4; /** Flag: Write error occured while writing output. */ static int dk_fic_write_error = 0; /** Exit status code. */ static int exval = EXIT_FAILURE; /** Flag: Verify checksums instead of creating them. */ static int dk_fic_verify = 0; /** Verbosity level (0=silent, 1=errors, 2=errors+success). */ static int dk_fic_verbosity = 1; /** Input encoding expected on standard input. */ static int dk_fic_ie_stdin = DK4_FILE_ENCODING_ASCII; /** Input encoding expected on files. */ static int dk_fic_ie_file = DK4_FILE_ENCODING_ASCII; /** Flag: Current line to process is first line of data stream. */ static int dk_fic_first_line = 0; /* SIGNAL HANDLING VARIABLES AND HANDLER FUNCTIONS */ #ifdef SIGPIPE /** Indicator: SIGPIPE signal received. */ static DK4_VOLATILE dk4_sig_atomic_t sig_had_pipe = 0; #endif /** Indicator: SIGINT signal received. */ static DK4_VOLATILE dk4_sig_atomic_t sig_had_int = 0; /** Indicator: SIGTERM signal received. */ static DK4_VOLATILE dk4_sig_atomic_t sig_had_term = 0; /** Pass a volatile pointer to an atomic integer. This function is necessary as some compilers mis-optimize direct access to volatile variables (at least if you believe one of the coding standards). @param ptr Address of atomic integer variable. @return The unmodified pointer. */ static DK4_VOLATILE dk4_sig_atomic_t * sig_pass_pointer(DK4_VOLATILE dk4_sig_atomic_t *ptr) { return ptr; } #ifdef SIGPIPE /** Handler for SIGPIPE signal. @param signo Signal number (always SIGPIPE, ignored). */ static void sig_handler_pipe(int signo) { *sig_pass_pointer(&sig_had_pipe) = 1; } #endif /** Handler for SIGINT signal. @param signo Signal number (always SIGINT, ignored). */ static void sig_handler_int(int signo) { *sig_pass_pointer(&sig_had_int) = 1; } /** Handler for SIGTERM signal. @param signo Signal number (always SIGTERM, ignored). */ static void sig_handler_term(int signo) { *sig_pass_pointer(&sig_had_term) = 1; } /** Read value from volatile atomic type. This function is necessary as some compilers mis-optimize direct access to volatile variables (at least if you believe one of the coding standards). @param ap Pointer to volatile atomic variable. @return Contents of the variable. */ static dk4_sig_atomic_t sig_read_atomic(DK4_VOLATILE dk4_sig_atomic_t *ap) { return (*ap); } /** Check whether we can continue or if a signal was received. @param check_pipe Flag: Check for occured SIGPIPE signal too. @return 1 if the program can continue, 0 if a signal was received. */ static int sig_can_continue(int check_pipe) { int back = 1; #ifdef SIGPIPE if (0 != check_pipe) { if (0 != sig_read_atomic(&sig_had_pipe)) { back = 0; } } #endif if (0 != sig_read_atomic(&sig_had_int )) { back = 0; } if (0 != sig_read_atomic(&sig_had_term)) { back = 0; } return back; } /* FUNCTIONS */ static int dk_fic_can_continue(void) { int back; back = sig_can_continue(1); if (0 != back) { if (0 != dk_fic_write_error) { back = 0; } } return back; } #if DK4_HAVE_OPENSSL_MD5_H && DK4_HAVE_OPENSSL_SHA_H && DK4_HAVE_OPENSSL_RIPEMD_H /** Report an error with position. @param i1 Index of first message part in array. @param i2 Index of alternative first message part. @param bno Current byte number. @param lno Current line number. @param cno Current character number. @param cil Current character number within line. @param fn File name processed. */ static void dk_fic_report_with_position( size_t i1, size_t i2, dk4_um_t bno, dk4_um_t lno, dk4_um_t cno, dk4_um_t cil, const dkChar *fn ) { dkChar b1[8*(1+sizeof(dk4_um_t))+16]; dkChar b2[8*(1+sizeof(dk4_um_t))+16]; dkChar b3[8*(1+sizeof(dk4_um_t))+16]; const dkChar *oldlogname = NULL; dk4_um_t oldlogline = (dk4_um_t)0UL; int allbuffersok = 0; oldlogname = dk4app_get_log_source_file(app); oldlogline = dk4app_get_log_source_line(app); dk4app_set_log_source_file(app, fn); dk4app_set_log_source_line(app, lno); if (0 != dk4ma_write_decimal_unsigned(b3,DK4_SIZEOF(b3,dkChar),cil,0,NULL)) { if (0 != dk4ma_write_decimal_unsigned(b2,DK4_SIZEOF(b2,dkChar),cno,0,NULL)) { if (0 != dk4ma_write_decimal_unsigned(b1,DK4_SIZEOF(b1,dkChar),bno,0,NULL)) { allbuffersok = 1; } } } if (0 != allbuffersok) { dk4app_log_7( app, dk_fic_msg, dk_fic_sz_msg, DK4_LL_ERROR, i1, 38, 39, 40, b1, b2, b3 ); } else { dk4app_log_1(app, dk_fic_msg, dk_fic_sz_msg, DK4_LL_ERROR, i2); } dk4app_set_log_source_line(app, oldlogline); dk4app_set_log_source_file(app, oldlogname); } /** Report decoding/encoding and processing errors. @param er_en Error report for decoding/encoding errors. @param er_pr Error report for processing errors. @param fn File name. */ static void dk_fic_report_errors( dk4_er_t *er_en, dk4_er_t *er_pr, const dkChar *fn ) { static int had_decoding_errors = 0; static int had_encoding_errors = 0; static int had_processing_errors = 0; if (NULL != er_en) { switch (er_en->ec) { case DK4_E_DECODING_FAILED : { if (3 > had_decoding_errors) { dk_fic_report_with_position( 35, 32, er_en->dt.fpos.byteno, er_en->dt.fpos.lineno, er_en->dt.fpos.charno, er_en->dt.fpos.charinline, fn ); } if (2 <= had_decoding_errors) { had_decoding_errors++; } } break; case DK4_E_ENCODING_FAILED : { if (3 > had_encoding_errors) { dk_fic_report_with_position( 36, 33, er_en->dt.fpos.byteno, er_en->dt.fpos.lineno, er_en->dt.fpos.charno, er_en->dt.fpos.charinline, fn ); } if (2 <= had_encoding_errors) { had_encoding_errors++; } } break; } } if (NULL != er_pr) { if (DK4_E_NONE != er_pr->ec) { if (3 > had_processing_errors) { dk_fic_report_with_position( 37, 34, er_pr->dt.fpos.byteno, er_pr->dt.fpos.lineno, er_pr->dt.fpos.charno, er_pr->dt.fpos.charinline, fn ); } if (2 <= had_processing_errors) { had_processing_errors++; } } } } /** Write one ASCII-Hex encoded message digest to stdout. @param digptr Start address of text buffer. @param sz Digest size. */ static void dk_fic_output_digest(const char *digptr, size_t sz) { while (0 < sz--) { if (0 == dk4fputc((dkChar)(*(digptr++)), stdout, NULL)) { dk_fic_write_error = 1; } } } /** Create checksum line for one file. @param filename File name. @param filesize File size. */ static void dk_fic_checksum_file(const dkChar *filename, dk4_um_t filesize) { dkChar fszbuf[8*sizeof(dk4_um_t)+16]; const dkChar *oldsourcename; const char *md5ptr; const char *sha1ptr; const char *ripemd160ptr; #if DK4_HAVE_SHA512 const char *sha512ptr; #endif FILE *fipo; dk4_um_t oldsourceline; size_t rdb; size_t md5sz; size_t sha1sz; size_t ripemd160sz; size_t sl; #if DK4_HAVE_SHA512 size_t sha512sz; #endif int tests = DK4_FOPEN_SC_IS_REGULAR; int ok = 1; int res; int cc; res = dk4md_ctx_init( &ctx_md5, DK4_MD_MD5, DK4_BINARY_TO_TEXT_ENCODING_A85, NULL ); if (0 == res) { ok = 0; } res = dk4md_ctx_init( &ctx_sha1, DK4_MD_SHA1, DK4_BINARY_TO_TEXT_ENCODING_A85, NULL ); if (0 == res) { ok = 0; } res = dk4md_ctx_init( &ctx_ripemd160, DK4_MD_RIPEMD_160, DK4_BINARY_TO_TEXT_ENCODING_A85, NULL ); if (0 == res) { ok = 0; } #if DK4_HAVE_SHA512 res = dk4md_ctx_init( &ctx_sha512, DK4_MD_SHA512, DK4_BINARY_TO_TEXT_ENCODING_A85, NULL ); if (0 == res) { ok = 0; } #endif if (0 != ok) { fipo = dk4fopen_app(filename, dk_fic_kwnl[5], tests, app); if (NULL != fipo) { cc = 1; while (1 == cc) { if (dk_fic_can_continue()) { rdb = fread(data_buf, 1, sizeof(data_buf), fipo); if (0 < rdb) { res = dk4md_ctx_add(&ctx_md5, data_buf, rdb, NULL); if (0 == res) { cc = -1; } res = dk4md_ctx_add(&ctx_sha1, data_buf, rdb, NULL); if (0 == res) { cc = -1; } res = dk4md_ctx_add(&ctx_ripemd160, data_buf, rdb, NULL); if (0 == res) { cc = -1; } #if DK4_HAVE_SHA512 res = dk4md_ctx_add(&ctx_sha512, data_buf, rdb, NULL); if (0 == res) { cc = -1; } #endif } else { cc = 0; if (0 != ferror(fipo)) { cc = -1; /* ERROR Read attempt failed */ oldsourcename = dk4app_get_log_source_file(app); oldsourceline = dk4app_get_log_source_line(app); dk4app_set_log_source_file(app, filename); dk4app_set_log_source_line(app, (dk4_um_t)0UL); dk4app_log_1( app, dk_fic_msg, dk_fic_sz_msg, DK4_LL_ERROR, 4 ); dk4app_set_log_source_file(app, oldsourcename); dk4app_set_log_source_line(app, oldsourceline); exval = EXIT_FAILURE; } } } else { cc = -1; } } fclose(fipo); if (0 == cc) { md5ptr = NULL; sha1ptr = NULL; ripemd160ptr = NULL; md5sz = 0; sha1sz = 0; ripemd160sz = 0; #if DK4_HAVE_SHA512 sha512ptr = NULL; sha512sz = 0; #endif res = dk4md_ctx_finish(&md5ptr, &md5sz, &ctx_md5, NULL); if (0 == res) { cc = -1; } res = dk4md_ctx_finish(&sha1ptr, &sha1sz, &ctx_sha1, NULL); if (0 == res) { cc = -1; } res = dk4md_ctx_finish(&ripemd160ptr,&ripemd160sz,&ctx_ripemd160,NULL); if (0 == res) { cc = -1; } #if DK4_HAVE_SHA512 res = dk4md_ctx_finish(&sha512ptr, &sha512sz, &ctx_sha512, NULL); if (0 == res) { cc = -1; } #endif if (0 == cc) { res = dk4ma_write_decimal_unsigned( fszbuf, DK4_SIZEOF(fszbuf,dkChar), filesize, 0, NULL ); if (0 != res) { dk_fic_output_digest(md5ptr, md5sz); if (0 == dk4fputc(dkT(' '), stdout, NULL)) { dk_fic_write_error = 1; } dk_fic_output_digest(sha1ptr, sha1sz); if (0 == dk4fputc(dkT(' '), stdout, NULL)) { dk_fic_write_error = 1; } dk_fic_output_digest(ripemd160ptr, ripemd160sz); #if DK4_HAVE_SHA512 if (0 == dk4fputc(dkT(' '), stdout, NULL)) { dk_fic_write_error = 1; } dk_fic_output_digest(sha512ptr, sha512sz); #endif if (0 == dk4fputc(dkT(' '), stdout, NULL)) { dk_fic_write_error = 1; } sl = dk4str_len(fszbuf); while (sl++ < dk_fic_num_len) { if (0 == dk4fputc(dkT(' '), stdout, NULL)) { dk_fic_write_error = 1; } } if (0 == dk4fputs(fszbuf, stdout, NULL)) { dk_fic_write_error = 1; } if (0 == dk4fputc(dkT(' '), stdout, NULL)) { dk_fic_write_error = 1; } if (0 == dk4fputs(filename, stdout, NULL)) { dk_fic_write_error = 1; } if (0 == dk4fputc(dkT('\n'), stdout, NULL)) { dk_fic_write_error = 1; } } else { /* ERROR: Failed to convert file size to text */ dk4app_log_3( app, dk_fic_msg, dk_fic_sz_msg, DK4_LL_ERROR, 5, 6, filename ); exval = EXIT_FAILURE; } } else { /* ERROR: Failed to finish at least one message digest */ dk4app_log_3( app, dk_fic_msg, dk_fic_sz_msg, DK4_LL_ERROR, 7, 8, filename ); exval = EXIT_FAILURE; } } else { if (-1 == cc) { /* ERROR: Failed to add data to message digest context */ dk4app_log_3( app, dk_fic_msg, dk_fic_sz_msg, DK4_LL_ERROR, 9, 10, filename ); exval = EXIT_FAILURE; } } } else { /* ERROR: Failed to open file, already reported */ exval = EXIT_FAILURE; } } else { /* ERROR: Failed to initialize at least one ctx */ dk4app_log_3( app, dk_fic_msg, dk_fic_sz_msg, DK4_LL_ERROR, 11, 12, filename ); exval = EXIT_FAILURE; } } /** Line handler when creating a checksum list. @param obj Object to modify. @param line Text line to process. @param lineno Current line number. @param erp Error report, may be NULL. @return DK4_TSP_RES_OK, DK4_TSP_RES_ERROR, or DK4_TSP_RES_FATAL. */ static int dk_fic_create_line_handler( void *obj, dkChar *line, dk4_um_t lineno, dk4_er_t *erp ) { dk4_file_info_t fi; dk4_er_t er; dk4_um_t filesize; int res; int back = DK4_TSP_RES_OK; dk4str_delnl(line); dk4path_correct_sep(line); filesize = (dk4_um_t)0UL; dk4error_init(&er); dk4fileinfo_init(&fi); if (0 != dk4fileinfo_app(&fi, line, app)) { res = dk4fileinfo_size( &filesize, &fi, DK4_FILE_INFO_CONTENTS_DATA_TARGET, &er ); if (0 != res) { if (0 != dk4fileinfo_exists_and_is_regular(&fi, &er)) { dk_fic_checksum_file(line, filesize); } else { /* ERROR: Not a regular file */ dk4app_log_3( app, dk_fic_msg, dk_fic_sz_msg, DK4_LL_ERROR, 13, 14, line ); exval = EXIT_FAILURE; } } else { /* ERROR: Failed to obtain file size */ dk4app_log_3( app, dk_fic_msg, dk_fic_sz_msg, DK4_LL_ERROR, 15, 16, line ); exval = EXIT_FAILURE; } } else { /* ERROR: Failed to information about file, already reported */ exval = EXIT_FAILURE; } return back; } /** Compare current checksum against checksum from file. @param curcs Current checksum. @param orics Original checksum from file. @param curlen Current checksum length. @return 1 if both checksums are equal, 0 otherwise. */ static int dk_fic_compare_checksums( const char *curcs, const dkChar *orics, size_t curlen ) { int back = 0; $? "+ dk_fic_compare_checksums \"%!8s\"", curcs if ((NULL != curcs) && (NULL != orics) && (0 < curlen)) { back = 1; while ((1 == back) && (0 < curlen--)) { if ((dkChar)(*(curcs++)) != *(orics++)) { back = 0; } } } $? "- dk_fic_compare_checksums %d", back return back; } /** Line handler when verifying a checksum list. @param obj Object to modify. @param line Text line to process. @param lineno Current line number. @param erp Error report, may be NULL. @return DK4_TSP_RES_OK, DK4_TSP_RES_ERROR, or DK4_TSP_RES_FATAL. */ static int dk_fic_verify_line_handler( void *obj, dkChar *line, dk4_um_t lineno, dk4_er_t *erp ) { dk4_file_info_t fi; /* File information */ dk4_er_t er; /* Error report */ FILE *fipo; /* File to read */ const dkChar *oldsourcename; /* Source file */ const dkChar *endptr; /* Text after size */ dkChar *tok[16]; /* Tokens in input line */ const char *md5ptr; /* Encoded message digest */ const char *sha1ptr; /* Encoded message digest */ const char *ripemd160ptr; /* Encoded message digest */ #if DK4_HAVE_SHA512 const char *sha512ptr; /* Encoded message digest */ #endif dk4_um_t oldsourceline; /* Source line number */ dk4_um_t filesize; /* Current file size */ dk4_um_t um; /* File size from input line */ size_t ntok; /* Number of tokens in input line */ size_t i; /* Index to initialize tokens */ size_t rdb; /* Number of bytes read */ size_t md5sz; /* Encoded digest length */ size_t sha1sz; /* Encoded digest length */ size_t ripemd160sz; /* Encoded digest length */ #if DK4_HAVE_SHA512 size_t sha512sz; /* Encoded digest length */ #endif int res; /* Operations result */ int cc; /* Flag: Can continue */ int tests = DK4_FOPEN_SC_IS_REGULAR; int back = DK4_TSP_RES_ERROR; int ok = 0; /* Flag: Check passed */ dk4str_delnl(line); $? "+ verify_line_handler \"%!ds\"", line /* dk4path_correct_sep(line); Correct file name only! */ for (i = 0; i < 16; i++) { tok[i] = NULL; } if (0 != dk_fic_first_line) { $? ". first line" dk_fic_first_line = 0; ntok = dk4str_tokenize(tok, 16, line, NULL, NULL); if (3 == ntok) { $? ". 3 tokens" if (dkT('#') == *(tok[0])) { if (0 == dk4str_cmp(dk_fic_kwnl[9], tok[1])) { endptr = NULL; if (0 != dk4ma_input_dk_dec_dk4_um_t(&um, tok[2], &endptr, 1, NULL)) { if ((dk4_um_t)3UL == um) { $? ". 3 checksums" dk_fic_num_mds = 3; back = DK4_TSP_RES_OK; } else { if ((dk4_um_t)4UL == um) { $? ". 4 checksums" dk_fic_num_mds = 4; back = DK4_TSP_RES_OK; #if !DK4_HAVE_SHA512 /* Warning SHA-512 checksum ignored, not supported */ dk4app_log_1( app, dk_fic_msg, dk_fic_sz_msg, DK4_LL_WARNING, 17 ); #endif } else { $? "! not 3 or 4" } } } else { $? "! not a number" } } else { $? "! not dk-fic" } } else { $? "! not comment" } } else { $? "! wrong no tokens" } if (DK4_TSP_RES_ERROR == back) { /* ERROR: Syntax error */ dk4app_log_1( app, dk_fic_msg, dk_fic_sz_msg, DK4_LL_ERROR, 18 ); exval = EXIT_FAILURE; } } else { $? ". data line" ntok = dk4str_tokenize(tok, 16, line, NULL, NULL); $? ". ntok = %u", (unsigned)ntok if ((5 == ntok) && (3 == dk_fic_num_mds)) { $? ". ntok 5 ok" back = DK4_TSP_RES_OK; } else { if ((6 == ntok) && (4 == dk_fic_num_mds)) { $? ". ntok 6 ok" back = DK4_TSP_RES_OK; } } if (DK4_TSP_RES_OK == back) { $? ". ntok ok" #if VERSION_BEFORE_20150821 back = DK4_TSP_RES_ERROR; #endif dk4path_correct_sep(tok[ntok - 1]); dk4error_init(&er); dk4fileinfo_init(&fi); if (0 != dk4fileinfo_app(&fi, tok[ntok - 1], app)) { $? ". fileinfo" filesize = (dk4_um_t)0UL; res = dk4fileinfo_size( &filesize, &fi, DK4_FILE_INFO_CONTENTS_DATA_TARGET, &er ); if (0 != res) { $? ". file size" res = dk4ma_input_dk_dec_dk4_um_t(&um, tok[ntok-2], &endptr, 1, NULL); if (0 != res) { $? ". um" if (um == filesize) { $? ". um = fs" ok = 1; /* Initialize message digest contexts. */ res = dk4md_ctx_init( &ctx_md5, DK4_MD_MD5, DK4_BINARY_TO_TEXT_ENCODING_A85, NULL ); if (0 == res) { ok = 0; } res = dk4md_ctx_init( &ctx_sha1, DK4_MD_SHA1, DK4_BINARY_TO_TEXT_ENCODING_A85, NULL ); if (0 == res) { ok = 0; } res = dk4md_ctx_init( &ctx_ripemd160,DK4_MD_RIPEMD_160, DK4_BINARY_TO_TEXT_ENCODING_A85, NULL ); if (0 == res) { ok = 0; } #if DK4_HAVE_SHA512 if (6 == ntok) { $? ". init sha512" res = dk4md_ctx_init( &ctx_sha512,DK4_MD_SHA512,DK4_BINARY_TO_TEXT_ENCODING_A85,NULL ); if (0 == res) { $? "! init sha512" ok = 0; } } #endif if (0 != ok) { $? ". ctx init" ok = 0; fipo = dk4fopen_app(tok[ntok-1], dk_fic_kwnl[5], tests, app); if (NULL != fipo) { $? ". fopen" ok = 1; cc = 1; while (1 == cc) { $? ". loop start" if (dk_fic_can_continue()) { $? ". can continue" rdb = fread(data_buf, 1, sizeof(data_buf), fipo); if (0 < rdb) { $? ". bytes found" /* Add data from file to message digest contexts. */ res = dk4md_ctx_add(&ctx_md5, data_buf, rdb, NULL); if (0 == res) { ok = 0; cc = -1; } res = dk4md_ctx_add(&ctx_sha1, data_buf, rdb, NULL); if (0 == res) { ok = 0; cc = -1; } res = dk4md_ctx_add(&ctx_ripemd160,data_buf,rdb,NULL); if (0 == res) { ok = 0; cc = -1; } #if DK4_HAVE_SHA512 if (6 == ntok) { res = dk4md_ctx_add(&ctx_sha512, data_buf, rdb, NULL); if (0 == res) { ok = 0; cc = -1; } } #endif if (0 == ok) { dk4app_log_3( app, dk_fic_msg, dk_fic_sz_msg, DK4_LL_ERROR, 9, 10, tok[ntok-1] ); } } else { $? ". no bytes read" cc = 0; if (0 != ferror(fipo)) { $? "! read error" cc = -1; /* ERROR Read attempt failed */ oldsourcename = dk4app_get_log_source_file(app); oldsourceline = dk4app_get_log_source_line(app); dk4app_set_log_source_file(app, tok[ntok-1]); dk4app_set_log_source_line(app, (dk4_um_t)0UL); dk4app_log_1( app, dk_fic_msg, dk_fic_sz_msg, DK4_LL_ERROR, 4 ); dk4app_set_log_source_file(app, oldsourcename); dk4app_set_log_source_line(app, oldsourceline); exval = EXIT_FAILURE; } } } else { $? "! signal" cc = -1; } } fclose(fipo); if (0 == cc) { $? ". end of loop ok" /* Retrieve results from message digest contexts. */ md5ptr = NULL; sha1ptr = NULL; ripemd160ptr = NULL; md5sz = 0; sha1sz = 0; ripemd160sz = 0; #if DK4_HAVE_SHA512 sha512ptr = NULL; $? ". sha512ptr NULL" sha512sz = 0; #endif res = dk4md_ctx_finish(&md5ptr, &md5sz, &ctx_md5, NULL); if (0 == res) { ok = 0; #if VERSION_BEFORE_20150821 cc = -1; #endif } res = dk4md_ctx_finish(&sha1ptr, &sha1sz, &ctx_sha1, NULL); if (0 == res) { ok = 0; #if VERSION_BEFORE_20150821 cc = -1; #endif } res = dk4md_ctx_finish( &ripemd160ptr,&ripemd160sz,&ctx_ripemd160,NULL ); if (0 == res) { ok = 0; #if VERSION_BEFORE_20150821 cc = -1; #endif } #if DK4_HAVE_SHA512 if (6 == ntok) { $? ". ntok=6" res = dk4md_ctx_finish( &sha512ptr, &sha512sz, &ctx_sha512, NULL ); $? ". sha512ptr %!8s", TR_8PTR(sha512ptr) if (0 == res) { ok = 0; #if VERSION_BEFORE_20150821 cc = -1; #endif } } #endif if (0 == ok) { dk4app_log_3( app, dk_fic_msg, dk_fic_sz_msg, DK4_LL_ERROR, 7, 8, tok[ntok-1] ); } /* Compare current results against input contents. */ if (0 != ok) { $? ". here" res = dk_fic_compare_checksums(md5ptr, tok[0], md5sz); if (0 == res) { $? "! md5 failed" ok = 0; } } if (0 != ok) { $? ". here" res = dk_fic_compare_checksums(sha1ptr, tok[1], sha1sz); if (0 == res) { $? "! sha-1 failed" ok = 0; } } if (0 != ok) { $? ". here" res = dk_fic_compare_checksums( ripemd160ptr, tok[2], ripemd160sz ); if (0 == res) { $? "! ripe failed" ok = 0; } } #if DK4_HAVE_SHA512 if (6 == ntok) { $? ". sha512" if (0 != ok) { $? ". here" res = dk_fic_compare_checksums( sha512ptr, tok[3], sha512sz ); if (0 == res) { $? "! sha512 failed" ok = 0; } } } #endif } else { $? "! loop end by error" ok = 0; } } else { $? "! fopen" } } else { $? "! ctx init" /* ERROR Failed to initialize MD contexts */ dk4app_log_3( app, dk_fic_msg, dk_fic_sz_msg, DK4_LL_ERROR, 11, 12, tok[ntok-1] ); } } else { $? "! files differ in size" } if (0 != ok) { $? ". PASSED" if (2 <= dk_fic_verbosity) { if (0 == dk4fputs(dk_fic_msg[2], stdout, NULL)) { exval = EXIT_FAILURE; dk_fic_write_error = 1; } if (0 == dk4fputs(tok[ntok-1], stdout, NULL)) { exval = EXIT_FAILURE; dk_fic_write_error = 1; } if (0 == dk4fputc(dkT('\n'), stdout, NULL)) { exval = EXIT_FAILURE; dk_fic_write_error = 1; } } } else { $? ". FAILED" exval = EXIT_FAILURE; if (1 <= dk_fic_verbosity) { if (0 == dk4fputs(dk_fic_msg[3], stdout, NULL)) { exval = EXIT_FAILURE; dk_fic_write_error = 1; } if (0 == dk4fputs(tok[ntok-1], stdout, NULL)) { exval = EXIT_FAILURE; dk_fic_write_error = 1; } if (0 == dk4fputc(dkT('\n'), stdout, NULL)) { exval = EXIT_FAILURE; dk_fic_write_error = 1; } } } } else { $? "! um" /* Syntax error */ dk4app_log_3( app, dk_fic_msg, dk_fic_sz_msg, DK4_LL_ERROR, 21, 22, tok[ntok-2] ); exval = EXIT_FAILURE; } } else { $? "! file size" /* ERROR: Failed to retrieve file size */ dk4app_log_3( app, dk_fic_msg, dk_fic_sz_msg, DK4_LL_ERROR, 15, 16, line ); exval = EXIT_FAILURE; } } else { $? "! fileinfo" /* ERROR: Failed to information about file, already reported */ exval = EXIT_FAILURE; } #if VERSION_BEFORE_20150821 back = DK4_TSP_RES_OK; #endif } else { $? "! ntok" /* Syntax error */ if (3 == dk_fic_num_mds) { dk4app_log_1(app, dk_fic_msg, dk_fic_sz_msg, DK4_LL_ERROR, 20); } else { dk4app_log_1(app, dk_fic_msg, dk_fic_sz_msg, DK4_LL_ERROR, 19); } exval = EXIT_FAILURE; } } $? "- verify_line_handler %d", back return back; } /** Process one file. @param fipo Opened file. @param fn File name. @param ie Expected input encoding. */ static void dk_fic_process_file(FILE *fipo, const dkChar *fn, int ie) { dk4_tspdk_t tsp; dk4_er_t er_en; dk4_er_t er_pr; dk4_er_t er; int res; int cc; int c; unsigned char ub; $? "+ dk_fic_process_file" dk4error_init(&er_en); dk4error_init(&er_pr); dk4error_init(&er); res = dk4tspdk_setup_line( &tsp, NULL, ( (0 != dk_fic_verify) ? dk_fic_verify_line_handler : dk_fic_create_line_handler ), ibuf_ptr, ibuf_sz, dk4app_get_encoding(app), ie, &er ); if (0 != res) { cc = 1; dk_fic_first_line = 1; dk_fic_num_mds = 4; do { if (dk_fic_can_continue()) { c = fgetc(fipo); if (EOF != c) { ub = (unsigned char)c; switch (dk4tspdk_add_one_byte(&tsp, ub)) { case DK4_TSP_RES_FATAL : { cc = -1; exval = EXIT_FAILURE; dk4tspdk_get_errors(&er_en, &er_pr, &tsp); dk_fic_report_errors(&er_en, &er_pr, fn); } break; case DK4_TSP_RES_ERROR : { exval = EXIT_FAILURE; dk4tspdk_get_errors(&er_en, &er_pr, &tsp); dk_fic_report_errors(&er_en, &er_pr, fn); } break; } } else { cc = 0; } } else { cc = -1; } } while (1 == cc); if (0 == cc) { switch (dk4tspdk_finish(&tsp)) { case DK4_TSP_RES_FATAL : { #if VERSION_BEFORE_20150821 cc = -1; #endif exval = EXIT_FAILURE; dk4tspdk_get_errors(&er_en, &er_pr, &tsp); dk_fic_report_errors(&er_en, &er_pr, fn); } break; case DK4_TSP_RES_ERROR : { exval = EXIT_FAILURE; dk4tspdk_get_errors(&er_en, &er_pr, &tsp); dk_fic_report_errors(&er_en, &er_pr, fn); } break; } } } else { /* ERROR: Failed to initialize text stream processor */ dk4app_log_1(app, dk_fic_msg, dk_fic_sz_msg, DK4_LL_ERROR, 23); exval = EXIT_FAILURE; } $? "- dk_fic_process_file" } static void dk_fic_process_filename(const dkChar *filename) { FILE *fipo; int tests = DK4_FOPEN_SC_IS_REGULAR; fipo = dk4fopen_app(filename, dk_fic_kwnl[5], tests, app); if (NULL != fipo) { dk_fic_process_file(fipo, filename, dk_fic_ie_file); fclose(fipo); } else { exval = EXIT_FAILURE; } } #if DK4_ON_WINDOWS static void dk_fic_expand_path_and_file( dkChar *bptr, size_t bsz, const dkChar *pth, const dkChar *shf ) { if (0 != dk4str_cpy_s(bptr, bsz, pth, NULL)) { if (0 != dk4str_cat_s(bptr, bsz, dk_fic_kwnl[6], NULL)) { if (0 != dk4str_cat_s(bptr, bsz, shf, NULL)) { dk_fic_process_filename(bptr); } else { /* ERROR: Name too long */ dk4app_log_base3(app, DK4_LL_ERROR, 98, 99, shf); exval = EXIT_FAILURE; } } else { /* ERROR: Name too long */ dk4app_log_base3(app, DK4_LL_ERROR, 98, 99, pth); exval = EXIT_FAILURE; } } else { /* ERROR: Name too long */ dk4app_log_base3(app, DK4_LL_ERROR, 98, 99, pth); exval = EXIT_FAILURE; } } static void dk_fic_expand_filename(const dkChar *pattern) { dkChar buf[DK4_MAX_PATH]; dk4_dir_t *fne; const dkChar *shf; const dkChar *pth; int any_file_found = 0; int can_continue; $? "+ dk_fic_expand_filename" fne = dk4app_fne_open(pattern, app); if (NULL != fne) { do { shf = dk4dir_next_file(fne); if (NULL != shf) { any_file_found = 1; can_continue = dk_fic_can_continue(); if (0 != can_continue) { pth = dk4dir_get_path(fne); if (NULL != pth) { dk_fic_expand_path_and_file(buf, DK4_SIZEOF(buf,dkChar), pth, shf); } else { dk_fic_process_filename(shf); } } } else { can_continue = 0; } } while (1 == can_continue); dk4dir_close(fne); if (0 == any_file_found) { /* ERROR: No matching file found */ dk4app_log_base3(app, DK4_LL_ERROR, 100, 107, pattern); exval = EXIT_FAILURE; } } else { exval = EXIT_FAILURE; } $? "- dk_fic_expand_filename" } #endif #endif /* if DK4_HAVE_... */ /** Process the command line arguments. */ static void dk_fic_run_for_command_line_args(void) { const dkChar *filename; int xargc; int i; #if DK4_HAVE_OPENSSL_MD5_H && DK4_HAVE_OPENSSL_SHA_H && DK4_HAVE_OPENSSL_RIPEMD_H #if DK4_ON_WINDOWS int oldmode; #endif if (0 == dk_fic_verify) { #if DK4_HAVE_SHA512 dk4fputs(dk_fic_kwnl[8], stdout, NULL); #else dk4fputs(dk_fic_kwnl[7], stdout, NULL); #endif } xargc = dk4app_get_argc(app); if (0 < xargc) { for (i = 0; ((0 != dk_fic_can_continue()) && (i < xargc)); i++) { filename = dk4app_get_argv(app, i); if (NULL != filename) { #if DK4_ON_WINDOWS if (0 != dk4path_must_expand(filename)) { dk_fic_expand_filename(filename); } else { #endif dk_fic_process_filename(filename); #if DK4_ON_WINDOWS } #endif } else { /* ERROR: Failed to obtain command line argument, bug! */ exval = EXIT_FAILURE; } } } else { #if DK4_ON_WINDOWS oldmode = _setmode(_fileno(stdin), _O_BINARY); #endif dk_fic_process_file(stdin, dk_fic_kwnl[4], dk_fic_ie_stdin); #if DK4_ON_WINDOWS _setmode(_fileno(stdin), oldmode); #endif } #else /* ERROR: At least one unsupported message digest type */ dk4app_log_1(app, dk_fic_msg, dk_fic_sz_msg, DK4_LL_ERROR, 24); exval = EXIT_FAILURE; #endif } /** Process options and preferences. */ static void dk_fic_normal_run(void) { dkChar encbuf[64]; size_t myll = 0; int res; /* Assume success. */ exval = EXIT_SUCCESS; /* Process command line options. */ if (0 != dk4app_opt_is_set_short(app, dkT('c'), NULL)) { dk_fic_verify = 1; } if (0 != dk4app_opt_is_set_short(app, dkT('q'), NULL)) { dk_fic_verbosity = 0; if (0 != dk4app_opt_is_set_short(app, dkT('v'), NULL)) { /* ERROR: Options -q an -v are mutually exclusve */ dk4app_log_1(app, dk_fic_msg, dk_fic_sz_msg, DK4_LL_ERROR, 25); exval = EXIT_FAILURE; goto finished; } } else { if (0 != dk4app_opt_is_set_short(app, dkT('v'), NULL)) { dk_fic_verbosity = 2; } } #if VERSION_BEFORE_20150821 res = 0; #endif if (0 != dk4app_opt_is_set_short(app, dkT('i'), NULL)) { res = dk4app_opt_get_string_short( encbuf, DK4_SIZEOF(encbuf,dkChar), app, dkT('i'), NULL ); if (0 != res) { res = dk4enc_find(&dk_fic_ie_stdin, NULL, encbuf, NULL); if (0 != res) { dk_fic_ie_file = dk_fic_ie_stdin; } else { /* ERROR: Encoding not found! */ dk4app_log_3( app, dk_fic_msg, dk_fic_sz_msg, DK4_LL_ERROR, 26, 27, encbuf ); exval = EXIT_FAILURE; goto finished; } } } res = 0; if (0 != dk4app_opt_is_set_short(app, dkT('l'), NULL)) { res = dk4app_opt_get_size_short(&myll, app, dkT('l'), NULL); } if ((0 == res) && (0 == dk4app_opt_is_set_short(app, dkT('R'), NULL))) { res = dk4app_pref_get_size(&myll, app, dk_fic_kwnl[3], 0); } if ((0 != res) && (DK4_SIZEOF(ibuf_def,dkChar) < myll)) { ibuf_all = dk4mem_new_app(dkChar, myll, app); if (NULL != ibuf_all) { ibuf_ptr = ibuf_all; ibuf_sz = myll; } else { goto finished; } } /* If no errors yet, process input. */ if (EXIT_SUCCESS == exval) { dk_fic_run_for_command_line_args(); } /* Clean up dynamic buffers, if any. */ finished: if (NULL != ibuf_all) { dk4mem_release(ibuf_all); } } /* SIGNAL HANDLING */ #if DK4_HAVE_SIGACTION /** Set signal handlers and run. */ static void dk_fic_run_with_signal_handlers(void) { #ifdef SIGPIPE struct sigaction opipe; #endif struct sigaction oint; #ifdef SIGPIPE struct sigaction npipe; #endif struct sigaction nint; struct sigaction oterm; struct sigaction nterm; int success = 0; #ifdef SIGPIPE /* Set up signal handling for SIGPIPE. */ DK4_MEMRES(&npipe, sizeof(npipe)); npipe.sa_handler = sig_handler_pipe; npipe.sa_flags = 0; if (0 != sigemptyset(&npipe.sa_mask)) { /* ERROR: Failed to set up masked signal set for SIGPIPE */ dk4app_log_1(app, dk_fic_msg, dk_fic_sz_msg, DK4_LL_ERROR, 0); goto finished; } if (0 != sigaddset(&npipe.sa_mask, SIGPIPE)) { /* ERROR: Failed to set up masked signal set for SIGPIPE */ dk4app_log_1(app, dk_fic_msg, dk_fic_sz_msg, DK4_LL_ERROR, 0); goto finished; } if (0 != sigaction(SIGPIPE, &npipe, &opipe)) { /* ERROR: Failed to set up signal handler for SIGPIPE */ dk4app_log_1(app, dk_fic_msg, dk_fic_sz_msg, DK4_LL_ERROR, 0); goto finished; } #endif /* Set up signal handling for SIGINT. */ DK4_MEMRES(&nint, sizeof(nint)); nint.sa_handler = sig_handler_int; nint.sa_flags = 0; if (0 != sigemptyset(&nint.sa_mask)) { /* ERROR: Failed to set up masked signal set for SIGINT */ dk4app_log_1(app, dk_fic_msg, dk_fic_sz_msg, DK4_LL_ERROR, 0); goto restore_old_pipe; } if (0 != sigaddset(&nint.sa_mask, SIGINT)) { /* ERROR: Failed to set up masked signal set for SIGINT */ dk4app_log_1(app, dk_fic_msg, dk_fic_sz_msg, DK4_LL_ERROR, 0); goto restore_old_pipe; } if (0 != sigaction(SIGINT, &nint, &oint)) { /* ERROR: Failed to set up signal handler for SIGINT */ dk4app_log_1(app, dk_fic_msg, dk_fic_sz_msg, DK4_LL_ERROR, 0); goto restore_old_pipe; } /* Set up signal handling for SIGTERM */ DK4_MEMRES(&nterm, sizeof(nterm)); nterm.sa_handler = sig_handler_term; nterm.sa_flags = 0; if (0 != sigemptyset(&nterm.sa_mask)) { /* ERROR: Failed to set up masked signal set for SIGTERM */ dk4app_log_1(app, dk_fic_msg, dk_fic_sz_msg, DK4_LL_ERROR, 0); goto restore_old_int; } if (0 != sigaddset(&nterm.sa_mask, SIGTERM)) { /* ERROR: Failed to set up masked signal set for SIGTERM */ dk4app_log_1(app, dk_fic_msg, dk_fic_sz_msg, DK4_LL_ERROR, 0); goto restore_old_int; } if (0 != sigaction(SIGTERM, &nterm, &oterm)) { /* ERROR: Failed to set up signal handler for SIGTERM */ dk4app_log_1(app, dk_fic_msg, dk_fic_sz_msg, DK4_LL_ERROR, 0); goto restore_old_int; } success = 1; dk_fic_normal_run(); /* Restore signal handling for SIGTERM. */ if (0 != sigaction(SIGTERM, &oterm, NULL)) { /* ERROR: Failed to restore old SIGTERM settings */ dk4app_log_1(app, dk_fic_msg, dk_fic_sz_msg, DK4_LL_ERROR, 1); success = 0; } /* Restore signal handling for SIGINT. */ restore_old_int: if (0 != sigaction(SIGINT, &oint, NULL)) { /* ERROR: Failed to restore old SIGPIPE settings */ dk4app_log_1(app, dk_fic_msg, dk_fic_sz_msg, DK4_LL_ERROR, 1); success = 0; } #ifdef SIGPIPE /* Restore signal handling for SIGPIPE. */ restore_old_pipe: if (0 != sigaction(SIGPIPE, &opipe, NULL)) { /* ERROR: Failed to restore old SIGPIPE settings */ dk4app_log_1(app, dk_fic_msg, dk_fic_sz_msg, DK4_LL_ERROR, 1); success = 0; } #endif /* Set exit status code if error occured. */ finished: if (0 == success) { exval = EXIT_FAILURE; } } #else #if DK4_HAVE_SIGSET /** Set signal handlers and run. */ static void dk_fic_run_with_signal_handlers(void) { #ifdef SIGPIPE dk4_sig_handler_t *oldpipe = NULL; #endif dk4_sig_handler_t *oldint = NULL; dk4_sig_handler_t *oldterm = NULL; #ifdef SIGPIPE oldpipe = sigset(SIGPIPE, sig_handler_pipe); #endif oldint = sigset(SIGINT, sig_handler_int); oldterm = sigset(SIGTERM, sig_handler_term); dk_fic_normal_run(); sigset(SIGTERM, oldterm); sigset(SIGINT, oldint); #ifdef SIGPIPE sigset(SIGPIPE, oldpipe); #endif } #else #if DK4_HAVE_SIGNAL /** Set signal handlers and run. */ static void dk_fic_run_with_signal_handlers(void) { #ifdef SIGPIPE dk4_sig_handler_t *oldpipe = NULL; #endif dk4_sig_handler_t *oldint = NULL; dk4_sig_handler_t *oldterm = NULL; #ifdef SIGPIPE oldpipe = signal(SIGPIPE, sig_handler_pipe); #endif oldint = signal(SIGINT, sig_handler_int); oldterm = signal(SIGTERM, sig_handler_term); dk_fic_normal_run(); signal(SIGTERM, oldterm); signal(SIGINT, oldint); #ifdef SIGPIPE signal(SIGPIPE, oldpipe); #endif } #else /** Set signal handlers and run. */ static void dk_fic_run_with_signal_handlers(void) { dk_fic_normal_run(); } #endif #endif #endif /** Main function. @param argc Number of command line arguments. @param argv Command line arguments array. @return 0 on success, all other values indicate errors. */ #if DK4_CHAR_SIZE > 1 int wmain(int argc, wchar_t *argv[]) #else int main(int argc, char *argv[]) #endif { $!trace-init dk-fic.deb $? "+ main" dk_fic_num_len = dk4numlength(sizeof(dk4_um_t), 0); dk4fput_initialize_stdout(); dk4fput_initialize_stderr(); app = dk4app_open_cmd( argc, argv, dk_fic_options, dk_fic_sz_options, dk_fic_kwnl[0], DKT_VERSION_DK, dk_fic_kwnl[1], dk_fic_help_text, dk_fic_license_text ); if (NULL != app) { dk_fic_sz_msg = dk4app_string_table_size(dk_fic_kw_def); dk_fic_msg = dk4app_string_table(app, dk_fic_kwnl[2], dk_fic_kw_def); dk_fic_ie_stdin = dk4app_get_stdin_encoding(app); dk_fic_ie_file = dk4app_get_file_in_encoding(app); if (0 != dk4app_can_run_normally(app)) { dk_fic_run_with_signal_handlers(); } else { if (0 != dk4app_help_version_license(app)) { exval = EXIT_SUCCESS; } } if (0 != dk_fic_write_error) { /* ERROR While writing output */ dk4app_log_1(app, dk_fic_msg, dk_fic_sz_msg, DK4_LL_ERROR, 28); exval = EXIT_FAILURE; } #ifdef SIGPIPE if (0 != sig_read_atomic(&sig_had_int)) { /* Signal PIPE */ dk4app_log_1(app, dk_fic_msg, dk_fic_sz_msg, DK4_LL_ERROR, 29); exval = EXIT_FAILURE; } #endif if (0 != sig_read_atomic(&sig_had_int)) { /* Signal INT */ dk4app_log_1(app, dk_fic_msg, dk_fic_sz_msg, DK4_LL_ERROR, 30); exval = EXIT_FAILURE; } if (0 != sig_read_atomic(&sig_had_term)) { /* Signal TERM */ dk4app_log_1(app, dk_fic_msg, dk_fic_sz_msg, DK4_LL_ERROR, 31); exval = EXIT_FAILURE; } dk4app_close(app); } fflush(stdout); fflush(stderr); dk4fput_cleanup_stderr(); dk4fput_cleanup_stdout(); $? "- main %d", exval $!trace-end exit (exval); return exval; }