From 04bed568528adf0460943f45643a94cb8b3e0a71 Mon Sep 17 00:00:00 2001 From: Georgi Tushev Date: Thu, 13 Sep 2018 15:07:17 +0200 Subject: [PATCH] fix seg.fault when wrong parameters --- config.c | 39 ++++++++++++++++++++------------------- config.h | 3 +-- main.c | 31 +++++++++++++------------------ 3 files changed, 34 insertions(+), 39 deletions(-) diff --git a/config.c b/config.c index 48c616b..76ffa69 100644 --- a/config.c +++ b/config.c @@ -11,7 +11,7 @@ const char VERSION[] = "v0.01"; const char PROGRAM_NAME[] = "PASSFinder"; -void aux_init(aux_config_t *aux) +void aux_parse(aux_config_t *aux, int argc, const char *argv[]) { /* initialize default auxiliary values */ aux->file_in = NULL; @@ -19,18 +19,21 @@ void aux_init(aux_config_t *aux) aux->polysize = AUX_DEFAULT_POLYSIZE; aux->masksize = AUX_DEFAULT_MASKSIZE; aux->mapq = AUX_DEFAULT_MAPQ; - - return; -} -int aux_parse(aux_config_t *aux, int argc, const char *argv[]) -{ + if ((argc != 2) && (argc < 4)) + { + fprintf(stderr, "Error: provide input, output and mask\n"); + help(); + } + + /* parse input pairs */ for(int i = 1; i < argc; i++) { + // current paramter length uint64_t parameterLength = strlen(argv[i]); - + // check each parameter and its value if((PARAMETER_CHECK("-h", 2, parameterLength)) || (PARAMETER_CHECK("--help", 6, parameterLength))) @@ -74,13 +77,11 @@ int aux_parse(aux_config_t *aux, int argc, const char *argv[]) else { fprintf(stderr, "Error:: Unknown parameter %s\n", argv[i]); - return EXIT_FAILURE; + help(); } - - } - return EXIT_SUCCESS; + return; } @@ -88,27 +89,27 @@ int aux_parse(aux_config_t *aux, int argc, const char *argv[]) void version(void) { fprintf(stderr, "PASSFinder %s\n", VERSION); - return; + exit(EXIT_SUCCESS); } void help(void) { - + fprintf(stderr, "\nTool: PASSFinder\n"); fprintf(stderr, "Version: %s\n", VERSION); fprintf(stderr, "Summary:\n"); fprintf(stderr, "\tCluster PolyA supported sites from 3'End sequencing.\n"); fprintf(stderr, "\nUsage:\t%s [OPTIONS] -i/--input -m/--maskpoly -o/--output \n", PROGRAM_NAME); fprintf(stderr, "Options:\n"); - fprintf(stderr, "\t-i/--input BAM/stdin file\n"); - fprintf(stderr, "\t-o/--output BED/stdout file\n"); - fprintf(stderr, "\t-m/--maskpoly BED/stdin file with genomic poly regions\n"); + fprintf(stderr, "\t-i/--input BAM/stdin file [required]\n"); + fprintf(stderr, "\t-o/--output BED/stdout file [required]\n"); + fprintf(stderr, "\t-m/--maskpoly BED/stdin file with genomic poly regions [required]\n"); fprintf(stderr, "\t-w/--window\tsliding window size\n"); fprintf(stderr, "\t-d/--depth\tminimum number of polyA reads to define PASS\n"); fprintf(stderr, "\t-p/--polysize\tminimum number of As for tail and Ts for head to define poly stretch\n"); fprintf(stderr, "\t-h/--help\tprint help message\n"); fprintf(stderr, "\t-v/--version\tprint current version\n"); - return; + exit(EXIT_SUCCESS); } int chomp(char *line_buf) @@ -117,6 +118,6 @@ int chomp(char *line_buf) line_length = strlen(line_buf) - 1; if(line_buf[line_length] == '\n') line_buf[line_length] = '\0'; - + return 0; -} \ No newline at end of file +} diff --git a/config.h b/config.h index 7d3776f..2ba9434 100644 --- a/config.h +++ b/config.h @@ -39,8 +39,7 @@ typedef struct _aux_config_t } aux_config_t; // function declarations -void aux_init(aux_config_t *); -int aux_parse(aux_config_t *, int, const char **); +void aux_parse(aux_config_t *, int, const char **); void help(void); void version(void); int chomp(char *); diff --git a/main.c b/main.c index f10e9c1..d6197a8 100644 --- a/main.c +++ b/main.c @@ -16,35 +16,30 @@ int main(int argc, const char * argv[]) { aux_config_t aux; hts_config_t hts; - - clock_t begin = clock(); - + + // parse auxiliary configuration - aux_init(&aux); - if (aux_parse(&aux, argc, argv)) - { - help(); - return EXIT_FAILURE; - } - + aux_parse(&aux, argc, argv); + + clock_t begin = clock(); + // initialize with hts hts_init(&hts, &aux); - + // parse with hts hts_parse(&hts, &aux); - + // dump hash hts_printout(&hts); - + // free hts_destroy(&hts); - - + clock_t end = clock(); - + double time_spent = (double)(end - begin) / CLOCKS_PER_SEC; - + fprintf(stderr, "Time: %.4f sec\n", time_spent); - + return 0; }