From a4d343b1fae2fe6c4f76cca7cc73cf0acfe60991 Mon Sep 17 00:00:00 2001 From: uno20001 Date: Fri, 17 Apr 2020 14:27:36 +0200 Subject: [PATCH] rework ALLINONE and includes, header files for all .c files --- export.h | 10 +++++++++ ftw.c | 15 +++++--------- ftw.h | 6 +++--- hash.c | 10 ++++----- hash.h | 9 +++++++++ hash_pthreads.c | 30 +++++++++++++-------------- init.c | 9 +++++---- init.h | 9 +++++++++ main.c | 54 ++++++++++++++----------------------------------- output.c | 13 ++++++------ output.h | 12 +++++++++++ sha1.c | 8 ++++---- sha1.h | 14 ++++++++----- 13 files changed, 108 insertions(+), 91 deletions(-) create mode 100644 export.h create mode 100644 hash.h create mode 100644 init.h create mode 100644 output.h diff --git a/export.h b/export.h new file mode 100644 index 0000000..47901d8 --- /dev/null +++ b/export.h @@ -0,0 +1,10 @@ +#ifndef MKTORRENT_EXPORT_H +#define MKTORRENT_EXPORT_H + +#ifdef ALLINONE +#define EXPORT static +#else +#define EXPORT +#endif /* ALLINONE */ + +#endif /* MKTORRENT_EXPORT_H */ diff --git a/ftw.c b/ftw.c index a7f013d..7939670 100644 --- a/ftw.c +++ b/ftw.c @@ -16,7 +16,8 @@ You should have received a copy of the GNU General Public License along with this program; if not, write to the Free Software Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA */ -#ifndef ALLINONE + + #include #include #include @@ -26,17 +27,11 @@ Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA #include #include -#ifdef _WIN32 -#define DIRSEP_CHAR '\\' -#else -#define DIRSEP_CHAR '/' -#endif /* _WIN32 */ - -#define EXPORT -#endif /* ALLINONE */ - +#include "export.h" +#include "mktorrent.h" /* DIRSEP_CHAR */ #include "ftw.h" + struct dir_state { struct dir_state *next; struct dir_state *prev; diff --git a/ftw.h b/ftw.h index 28b5417..4386230 100644 --- a/ftw.h +++ b/ftw.h @@ -1,13 +1,13 @@ #ifndef MKTORRENT_FTW_H #define MKTORRENT_FTW_H +#include "export.h" + typedef int (*file_tree_walk_cb)(const char *name, const struct stat *sbuf, void *data); -#ifndef ALLINONE -int file_tree_walk(const char *dirname, unsigned int nfds, +EXPORT int file_tree_walk(const char *dirname, unsigned int nfds, file_tree_walk_cb callback, void *data); -#endif /* ALLINONE */ #endif /* MKTORRENT_FTW_H */ diff --git a/hash.c b/hash.c index 62f8a1f..7967359 100644 --- a/hash.c +++ b/hash.c @@ -16,9 +16,9 @@ You should have received a copy of the GNU General Public License along with this program; if not, write to the Free Software Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA */ -#ifndef ALLINONE + + #include /* exit() */ -#include /* off_t */ #include /* errno */ #include /* strerror() */ #include /* printf() etc. */ @@ -32,10 +32,9 @@ Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA #include "sha1.h" #endif +#include "export.h" #include "mktorrent.h" - -#define EXPORT -#endif /* ALLINONE */ +#include "hash.h" #ifndef O_BINARY #define O_BINARY 0 @@ -47,6 +46,7 @@ Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA #define OPENFLAGS (O_RDONLY | O_BINARY) #endif + /* * go through the files in file_list, split their contents into pieces * of size piece_length and create the hash string, which is the diff --git a/hash.h b/hash.h new file mode 100644 index 0000000..02ff4b9 --- /dev/null +++ b/hash.h @@ -0,0 +1,9 @@ +#ifndef MKTORRENT_HASH_H +#define MKTORRENT_HASH_H + +#include "export.h" /* EXPORT */ +#include "mktorrent.h" /* struct metafile */ + +EXPORT unsigned char *make_hash(struct metafile *m); + +#endif /* MKTORRENT_HASH_H */ diff --git a/hash_pthreads.c b/hash_pthreads.c index dfd37ab..73e1b2f 100644 --- a/hash_pthreads.c +++ b/hash_pthreads.c @@ -16,28 +16,27 @@ You should have received a copy of the GNU General Public License along with this program; if not, write to the Free Software Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA */ -#ifndef ALLINONE -#include /* exit(), malloc() */ -#include /* off_t */ -#include /* errno */ -#include /* strerror() */ -#include /* printf() etc. */ -#include /* open() */ -#include /* access(), read(), close() */ -#include /* PRId64 etc. */ + + +#include /* exit(), malloc() */ +#include /* off_t */ +#include /* errno */ +#include /* strerror() */ +#include /* printf() etc. */ +#include /* open() */ +#include /* read(), close() */ +#include /* PRId64 etc. */ +#include #ifdef USE_OPENSSL -#include /* SHA1() */ +#include /* SHA1() */ #else #include "sha1.h" #endif -#include /* pthread functions and data structures */ +#include "export.h" #include "mktorrent.h" - -#define EXPORT -#endif /* ALLINONE */ - +#include "hash.h" #ifndef PROGRESS_PERIOD #define PROGRESS_PERIOD 200000 @@ -53,6 +52,7 @@ Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA #define OPENFLAGS (O_RDONLY | O_BINARY) #endif + struct piece { struct piece *next; unsigned char *dest; diff --git a/init.c b/init.c index f675147..08617ea 100644 --- a/init.c +++ b/init.c @@ -16,7 +16,8 @@ You should have received a copy of the GNU General Public License along with this program; if not, write to the Free Software Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA */ -#ifndef ALLINONE + + #include /* exit() */ #include /* off_t */ #include /* errno */ @@ -27,21 +28,21 @@ Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA #include /* strcmp(), strlen(), strncpy() */ #include /* strcasecmp() */ #include /* PRId64 etc. */ + #ifdef USE_LONG_OPTIONS #include /* getopt_long() */ #endif +#include "export.h" #include "mktorrent.h" #include "ftw.h" -#define EXPORT -#endif /* ALLINONE */ - #ifndef MAX_OPENFD #define MAX_OPENFD 100 /* Maximum number of file descriptors file_tree_walk() will open */ #endif + static void strip_ending_dirseps(char *s) { char *end = s; diff --git a/init.h b/init.h new file mode 100644 index 0000000..9cfa0a1 --- /dev/null +++ b/init.h @@ -0,0 +1,9 @@ +#ifndef MKTORRENT_INIT_H +#define MKTORRENT_INIT_H + +#include "export.h" /* EXPORT */ +#include "mktorrent.h" /* struct metafile */ + +EXPORT void init(struct metafile *m, int argc, char *argv[]); + +#endif /* MKTORRENT_INIT_H */ diff --git a/main.c b/main.c index 9856818..99ecaad 100644 --- a/main.c +++ b/main.c @@ -17,49 +17,24 @@ along with this program; if not, write to the Free Software Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA */ + #include /* exit() */ -#include /* off_t */ #include /* errno */ #include /* strerror() */ #include /* printf() etc. */ #include /* S_IRUSR, S_IWUSR, S_IRGRP, S_IROTH */ #include /* open() */ -#ifdef ALLINONE -#include -#include /* access(), read(), close(), getcwd(), sysconf() */ -#include /* strcasecmp() */ -#include /* PRId64 etc. */ -#include /* isdigit */ -#ifdef USE_LONG_OPTIONS -#include /* getopt_long() */ -#endif -#include /* time() */ -#include /* opendir(), closedir(), readdir() etc. */ -#ifdef USE_OPENSSL -#include /* SHA1(), SHA_DIGEST_LENGTH */ -#else -#include -#endif -#ifdef USE_PTHREADS -#include /* pthread functions and data structures */ -#endif - -#define EXPORT static -#else /* ALLINONE */ - -#define EXPORT -#endif /* ALLINONE */ - +#include "export.h" #include "mktorrent.h" +#include "init.h" +#include "hash.h" +#include "output.h" #ifdef ALLINONE -#include "ftw.c" -#include "init.c" +/* include all .c files in alphabetical order */ -#ifndef USE_OPENSSL -#include "sha1.c" -#endif +#include "ftw.c" #ifdef USE_PTHREADS #include "hash_pthreads.c" @@ -67,14 +42,13 @@ Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA #include "hash.c" #endif +#include "init.c" #include "output.c" -#else /* ALLINONE */ -/* init.c */ -extern void init(struct metafile *m, int argc, char *argv[]); -/* hash.c */ -extern unsigned char *make_hash(struct metafile *m); -/* output.c */ -extern void write_metainfo(FILE *f, struct metafile *m, unsigned char *hash_string); + +#ifndef USE_OPENSSL +#include "sha1.c" +#endif + #endif /* ALLINONE */ #ifndef O_BINARY @@ -84,10 +58,12 @@ extern void write_metainfo(FILE *f, struct metafile *m, unsigned char *hash_stri #ifndef S_IRGRP #define S_IRGRP 0 #endif + #ifndef S_IROTH #define S_IROTH 0 #endif + /* * create and open the metainfo file for writing and create a stream for it * we don't want to overwrite anything, so abort if the file is already there diff --git a/output.c b/output.c index 81d074f..5f8407b 100644 --- a/output.c +++ b/output.c @@ -16,22 +16,23 @@ You should have received a copy of the GNU General Public License along with this program; if not, write to the Free Software Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA */ -#ifndef ALLINONE + + #include /* off_t */ -#include /* printf() etc. */ +#include /* fprintf() etc. */ #include /* strlen() etc. */ #include /* time() */ + #ifdef USE_OPENSSL #include /* SHA_DIGEST_LENGTH */ #else -#include #include "sha1.h" #endif -#include "mktorrent.h" +#include "export.h" /* EXPORT */ +#include "mktorrent.h" /* struct metafile */ +#include "output.h" -#define EXPORT -#endif /* ALLINONE */ /* * write announce list diff --git a/output.h b/output.h new file mode 100644 index 0000000..edbb59f --- /dev/null +++ b/output.h @@ -0,0 +1,12 @@ +#ifndef MKTORRENT_OUTPUT_H +#define MKTORRENT_OUTPUT_H + +#include /* FILE */ + +#include "export.h" /* EXPORT */ +#include "mktorrent.h" /* struct metafile */ + +EXPORT void write_metainfo(FILE *f, struct metafile *m, + unsigned char *hash_string); + +#endif /* MKTORRENT_OUTPUT_H */ diff --git a/sha1.c b/sha1.c index 78f7988..f688953 100644 --- a/sha1.c +++ b/sha1.c @@ -16,16 +16,16 @@ /* #define SHA1_WIPE_VARS */ /* #define SHA1_VERBOSE */ -#ifndef ALLINONE + #ifdef SHA1_TEST #include #endif + #include +#include #include -#define EXPORT -#endif /* ALLINONE */ - +#include "export.h" #include "sha1.h" #define rol(value, bits) (((value) << (bits)) | ((value) >> (32 - (bits)))) diff --git a/sha1.h b/sha1.h index 9958c6e..567543c 100644 --- a/sha1.h +++ b/sha1.h @@ -1,9 +1,14 @@ /* Public API for Steve Reid's public domain SHA-1 implementation */ /* This file is in the public domain */ + #ifndef MKTORRENT_SHA1_H #define MKTORRENT_SHA1_H +#include /* uintX_t */ + +#include "export.h" /* EXPORT */ + typedef struct { uint32_t state[5]; uint32_t count[2]; @@ -12,10 +17,9 @@ typedef struct { #define SHA_DIGEST_LENGTH 20 -#ifndef ALLINONE -void SHA1_Init(SHA_CTX *context); -void SHA1_Update(SHA_CTX *context, const uint8_t *data, unsigned long len); -void SHA1_Final(uint8_t *digest, SHA_CTX *context); -#endif + +EXPORT void SHA1_Init(SHA_CTX *context); +EXPORT void SHA1_Update(SHA_CTX *context, const uint8_t *data, unsigned long len); +EXPORT void SHA1_Final(uint8_t *digest, SHA_CTX *context); #endif /* MKTORRENT_SHA1_H */