--- lbbs/src/test_file_loader.c 2025/05/25 06:48:31 1.5 +++ lbbs/src/test_file_loader.c 2025/11/19 03:12:58 1.14 @@ -1,29 +1,24 @@ -/*************************************************************************** - file_loader.c - description - ------------------- - Copyright : (C) 2004-2025 by Leaflet - Email : leaflet@leafok.com - ***************************************************************************/ - -/*************************************************************************** - * * - * This program is free software; you can redistribute it and/or modify * - * it under the terms of the GNU General Public License as published by * - * the Free Software Foundation; either version 3 of the License, or * - * (at your option) any later version. * - * * - ***************************************************************************/ +/* SPDX-License-Identifier: GPL-3.0-or-later */ +/* + * test_file_loader + * - tester for shared memory based file loader + * + * Copyright (C) 2004-2025 Leaflet + */ + +#ifdef HAVE_CONFIG_H +#include "config.h" +#endif #include "file_loader.h" -#include "trie_dict.h" #include "log.h" +#include +#include #include +#include #include -#include #include -#define TRIE_DICT_SHM_FILE "~trie_dict_shm.dat" - const char *files[] = { "../data/welcome.txt", "../data/copyright.txt", @@ -36,49 +31,32 @@ int files_cnt = 6; int main(int argc, char *argv[]) { - int ret; + char file_path_temp[FILE_PATH_LEN]; int i; - const void *p_shm; + void *p_shm; size_t data_len; long line_total; const void *p_data; const long *p_line_offsets; - FILE *fp; - if (log_begin("../log/bbsd.log", "../log/error.log") < 0) - { - printf("Open log error\n"); - return -1; - } - - log_std_redirect(STDOUT_FILENO); - log_err_redirect(STDERR_FILENO); + // Change current dir + strncpy(file_path_temp, argv[0], sizeof(file_path_temp) - 1); + file_path_temp[sizeof(file_path_temp) - 1] = '\0'; - if ((fp = fopen(TRIE_DICT_SHM_FILE, "w")) == NULL) + if (chdir(dirname(file_path_temp)) < 0) { - log_error("fopen(%s) error\n", TRIE_DICT_SHM_FILE); + fprintf(stderr, "chdir(%s) error: %d\n", dirname(file_path_temp), errno); return -1; } - fclose(fp); - if (trie_dict_init(TRIE_DICT_SHM_FILE) < 0) + if (log_begin("../log/bbsd.log", "../log/error.log") < 0) { - printf("trie_dict_init failed\n"); + printf("Open log error\n"); return -1; } - ret = file_loader_init(); - if (ret < 0) - { - printf("file_loader_init() error (%d)\n", ret); - return ret; - } - - ret = file_loader_init(); - if (ret == 0) - { - printf("Rerun file_loader_init() error\n"); - } + log_common_redir(STDOUT_FILENO); + log_error_redir(STDERR_FILENO); printf("Testing #1\n"); @@ -163,17 +141,6 @@ int main(int argc, char *argv[]) } } - file_loader_cleanup(); - file_loader_cleanup(); - - trie_dict_cleanup(); - - if (unlink(TRIE_DICT_SHM_FILE) < 0) - { - log_error("unlink(%s) error\n", TRIE_DICT_SHM_FILE); - return -1; - } - log_end(); return 0;