From cfd6915b206cc342600339f8ab7c1ea272ea7c69 Mon Sep 17 00:00:00 2001 From: discomfitor Date: Thu, 11 Aug 2011 00:41:21 -0400 Subject: [PATCH] move some initialization stuff out of main() --- lrzip.c | 40 ++++++++++++++++++++++++++++++++++++++++ lrzip.h | 1 + main.c | 37 ++----------------------------------- 3 files changed, 43 insertions(+), 35 deletions(-) diff --git a/lrzip.c b/lrzip.c index bd8c9d3..aa03de0 100644 --- a/lrzip.c +++ b/lrzip.c @@ -1143,3 +1143,43 @@ void compress_file(rzip_control *control) free(control->outfile); } + +void initialize_control(rzip_control *control) +{ + struct timeval tv; + char *eptr; /* for environment */ + + memset(control, 0, sizeof(rzip_control)); + control->msgout = stderr; + register_outputfile(control->msgout); + control->flags = FLAG_SHOW_PROGRESS | FLAG_KEEP_FILES | FLAG_THRESHOLD; + control->suffix = ".lrz"; + control->compression_level = 7; + control->ramsize = get_ram(); + /* for testing single CPU */ + control->threads = PROCESSORS; /* get CPUs for LZMA */ + control->page_size = PAGE_SIZE; + control->nice_val = 19; + + /* The first 5 bytes of the salt is the time in seconds. + * The next 2 bytes encode how many times to hash the password. + * The last 9 bytes are random data, making 16 bytes of salt */ + if (unlikely(gettimeofday(&tv, NULL))) + fatal("Failed to gettimeofday in main\n"); + control->secs = tv.tv_sec; + control->encloops = nloops(control->secs, control->salt, control->salt + 1); + get_rand(control->salt + 2, 6); + + /* Get Temp Dir */ + eptr = getenv("TMP"); + if (eptr != NULL) { + size_t len = strlen(eptr); + control->tmpdir = malloc(len+2); + if (control->tmpdir == NULL) + fatal("Failed to allocate for tmpdir\n"); + strcpy(control->tmpdir, eptr); + if (eptr[len - 2] != '/') + eptr[len - 2] = '/'; /* need a trailing slash */ + eptr[len - 1] = 0; + } +} diff --git a/lrzip.h b/lrzip.h index 1939957..e263f20 100644 --- a/lrzip.h +++ b/lrzip.h @@ -41,4 +41,5 @@ void close_tmpoutbuf(rzip_control *control); void clear_tmpinbuf(rzip_control *control); inline void clear_tmpinfile(rzip_control *control); void close_tmpinbuf(rzip_control *control); +void initialize_control(rzip_control *control); #endif diff --git a/main.c b/main.c index e96aacd..7812f54 100644 --- a/main.c +++ b/main.c @@ -353,7 +353,7 @@ out: int main(int argc, char *argv[]) { - struct timeval start_time, end_time, tv; + struct timeval start_time, end_time; struct sigaction handler; double seconds,total_time; // for timers int c, i; @@ -362,50 +362,17 @@ int main(int argc, char *argv[]) char *eptr; /* for environment */ control = &controlstaticvariablehaha; - memset(control, 0, sizeof(rzip_control)); - control->msgout = stderr; - register_outputfile(control->msgout); - control->flags = FLAG_SHOW_PROGRESS | FLAG_KEEP_FILES | FLAG_THRESHOLD; - control->suffix = ".lrz"; - control->outdir = NULL; - control->tmpdir = NULL; + initialize_control(control); if (strstr(argv[0], "lrunzip")) control->flags |= FLAG_DECOMPRESS; else if (strstr(argv[0], "lrzcat")) control->flags |= FLAG_DECOMPRESS | FLAG_STDOUT; - control->compression_level = 7; - control->ramsize = get_ram(); - /* for testing single CPU */ - control->threads = PROCESSORS; /* get CPUs for LZMA */ - control->page_size = PAGE_SIZE; - control->nice_val = 19; - - /* The first 5 bytes of the salt is the time in seconds. - * The next 2 bytes encode how many times to hash the password. - * The last 9 bytes are random data, making 16 bytes of salt */ - if (unlikely(gettimeofday(&tv, NULL))) - fatal("Failed to gettimeofday in main\n"); - control->secs = tv.tv_sec; - control->encloops = nloops(control->secs, control->salt, control->salt + 1); - get_rand(control->salt + 2, 6); - /* generate crc table */ CrcGenerateTable(); - /* Get Temp Dir */ - eptr = getenv("TMP"); - if (eptr != NULL) { - control->tmpdir = malloc(strlen(eptr)+2); - if (control->tmpdir == NULL) - fatal("Failed to allocate for tmpdir\n"); - strcpy(control->tmpdir, eptr); - if (strcmp(eptr+strlen(eptr) - 1, "/")) /* need a trailing slash */ - strcat(control->tmpdir, "/"); - } - /* Get Preloaded Defaults from lrzip.conf * Look in ., $HOME/.lrzip/, /etc/lrzip. * If LRZIP=NOCONFIG is set, then ignore config