Skip to content

Commit

Permalink
Merge pull request #39 from uno20001/rework_ALLINONE
Browse files Browse the repository at this point in the history
#include changes and header files for multiple source files
  • Loading branch information
Rudde authored Apr 18, 2020
2 parents df0ccf8 + a4d343b commit 83aa516
Show file tree
Hide file tree
Showing 13 changed files with 108 additions and 91 deletions.
10 changes: 10 additions & 0 deletions export.h
Original file line number Diff line number Diff line change
@@ -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 */
15 changes: 5 additions & 10 deletions ftw.c
Original file line number Diff line number Diff line change
Expand Up @@ -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 <stdlib.h>
#include <errno.h>
#include <string.h>
Expand All @@ -26,17 +27,11 @@ Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA
#include <unistd.h>
#include <dirent.h>

#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;
Expand Down
6 changes: 3 additions & 3 deletions ftw.h
Original file line number Diff line number Diff line change
@@ -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 */
10 changes: 5 additions & 5 deletions hash.c
Original file line number Diff line number Diff line change
Expand Up @@ -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 <stdlib.h> /* exit() */
#include <sys/types.h> /* off_t */
#include <errno.h> /* errno */
#include <string.h> /* strerror() */
#include <stdio.h> /* printf() etc. */
Expand All @@ -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
Expand All @@ -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
Expand Down
9 changes: 9 additions & 0 deletions hash.h
Original file line number Diff line number Diff line change
@@ -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 */
30 changes: 15 additions & 15 deletions hash_pthreads.c
Original file line number Diff line number Diff line change
Expand Up @@ -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 <stdlib.h> /* exit(), malloc() */
#include <sys/types.h> /* off_t */
#include <errno.h> /* errno */
#include <string.h> /* strerror() */
#include <stdio.h> /* printf() etc. */
#include <fcntl.h> /* open() */
#include <unistd.h> /* access(), read(), close() */
#include <inttypes.h> /* PRId64 etc. */


#include <stdlib.h> /* exit(), malloc() */
#include <sys/types.h> /* off_t */
#include <errno.h> /* errno */
#include <string.h> /* strerror() */
#include <stdio.h> /* printf() etc. */
#include <fcntl.h> /* open() */
#include <unistd.h> /* read(), close() */
#include <inttypes.h> /* PRId64 etc. */
#include <pthread.h>

#ifdef USE_OPENSSL
#include <openssl/sha.h> /* SHA1() */
#include <openssl/sha.h> /* SHA1() */
#else
#include "sha1.h"
#endif
#include <pthread.h> /* 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
Expand All @@ -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;
Expand Down
9 changes: 5 additions & 4 deletions init.c
Original file line number Diff line number Diff line change
Expand Up @@ -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 <stdlib.h> /* exit() */
#include <sys/types.h> /* off_t */
#include <errno.h> /* errno */
Expand All @@ -27,21 +28,21 @@ Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA
#include <string.h> /* strcmp(), strlen(), strncpy() */
#include <strings.h> /* strcasecmp() */
#include <inttypes.h> /* PRId64 etc. */

#ifdef USE_LONG_OPTIONS
#include <getopt.h> /* 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;
Expand Down
9 changes: 9 additions & 0 deletions init.h
Original file line number Diff line number Diff line change
@@ -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 */
54 changes: 15 additions & 39 deletions main.c
Original file line number Diff line number Diff line change
Expand Up @@ -17,64 +17,38 @@ along with this program; if not, write to the Free Software
Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA
*/


#include <stdlib.h> /* exit() */
#include <sys/types.h> /* off_t */
#include <errno.h> /* errno */
#include <string.h> /* strerror() */
#include <stdio.h> /* printf() etc. */
#include <sys/stat.h> /* S_IRUSR, S_IWUSR, S_IRGRP, S_IROTH */
#include <fcntl.h> /* open() */

#ifdef ALLINONE
#include <sys/stat.h>
#include <unistd.h> /* access(), read(), close(), getcwd(), sysconf() */
#include <strings.h> /* strcasecmp() */
#include <inttypes.h> /* PRId64 etc. */
#include <ctype.h> /* isdigit */
#ifdef USE_LONG_OPTIONS
#include <getopt.h> /* getopt_long() */
#endif
#include <time.h> /* time() */
#include <dirent.h> /* opendir(), closedir(), readdir() etc. */
#ifdef USE_OPENSSL
#include <openssl/sha.h> /* SHA1(), SHA_DIGEST_LENGTH */
#else
#include <inttypes.h>
#endif
#ifdef USE_PTHREADS
#include <pthread.h> /* 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"
#else
#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
Expand All @@ -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
Expand Down
13 changes: 7 additions & 6 deletions output.c
Original file line number Diff line number Diff line change
Expand Up @@ -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 <sys/types.h> /* off_t */
#include <stdio.h> /* printf() etc. */
#include <stdio.h> /* fprintf() etc. */
#include <string.h> /* strlen() etc. */
#include <time.h> /* time() */

#ifdef USE_OPENSSL
#include <openssl/sha.h> /* SHA_DIGEST_LENGTH */
#else
#include <inttypes.h>
#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
Expand Down
12 changes: 12 additions & 0 deletions output.h
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
#ifndef MKTORRENT_OUTPUT_H
#define MKTORRENT_OUTPUT_H

#include <stdio.h> /* 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 */
8 changes: 4 additions & 4 deletions sha1.c
Original file line number Diff line number Diff line change
Expand Up @@ -16,16 +16,16 @@
/* #define SHA1_WIPE_VARS */
/* #define SHA1_VERBOSE */

#ifndef ALLINONE

#ifdef SHA1_TEST
#include <stdio.h>
#endif

#include <string.h>
#include <stdint.h>
#include <inttypes.h>

#define EXPORT
#endif /* ALLINONE */

#include "export.h"
#include "sha1.h"

#define rol(value, bits) (((value) << (bits)) | ((value) >> (32 - (bits))))
Expand Down
14 changes: 9 additions & 5 deletions sha1.h
Original file line number Diff line number Diff line change
@@ -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 <stdint.h> /* uintX_t */

#include "export.h" /* EXPORT */

typedef struct {
uint32_t state[5];
uint32_t count[2];
Expand All @@ -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 */

0 comments on commit 83aa516

Please sign in to comment.