From 189a2ed72389f3adcec1b624f4eea0b627ece29e Mon Sep 17 00:00:00 2001 From: Kevin Kragenbrink Date: Fri, 24 Aug 2012 21:15:12 -0700 Subject: [PATCH 1/3] Changing FileNames to include paths from the source argument. --- instrument.c | 13 +++++++++---- instrument.h | 3 ++- jscoverage.c | 4 +++- 3 files changed, 14 insertions(+), 6 deletions(-) diff --git a/instrument.c b/instrument.c index d19b690..bdc147a 100644 --- a/instrument.c +++ b/instrument.c @@ -70,7 +70,7 @@ static void check_contains_file(const char * file1, const char * file2) { } } -static void instrument_file(const char * source_file, const char * destination_file, const char * id, int instrumenting) { +static void instrument_file(const char * source_file, const char * destination_file, const char * id, int instrumenting, char * filepath) { if (g_verbose) { printf("Instrumenting file %s\n", id); } @@ -108,7 +108,7 @@ static void instrument_file(const char * source_file, const char * destination_f else if (result == JSCOVERAGE_ERROR_INVALID_BYTE_SEQUENCE) { fatal("error decoding %s in file %s", jscoverage_encoding, id); } - jscoverage_instrument_js(id, characters, num_characters, output_stream); + jscoverage_instrument_js(filepath, characters, num_characters, output_stream); free(characters); if (fwrite(output_stream->data, 1, output_stream->length, output) != output_stream->length) { @@ -135,7 +135,8 @@ void jscoverage_instrument(const char * source, char ** exclude, int num_exclude, char ** no_instrument, - int num_no_instrument) + int num_no_instrument, + char * path) { assert(source != NULL); assert(destination != NULL); @@ -215,9 +216,13 @@ void jscoverage_instrument(const char * source, free(ni); } - instrument_file(s, d, p->name, instrument_this); + int length = strlen(path) + strlen(p->name) + 1; + char * filepath = malloc(length); + sprintf(filepath, "%s/%s", path, p->name); + instrument_file(s, d, p->name, instrument_this, filepath); cleanup: + free(filepath); free(s); free(d); } diff --git a/instrument.h b/instrument.h index a8720b0..a260757 100644 --- a/instrument.h +++ b/instrument.h @@ -26,6 +26,7 @@ void jscoverage_instrument(const char * source, char ** exclude, int num_exclude, char ** no_instrument, - int num_no_instrument); + int num_no_instrument, + const char * path); #endif /* INSTRUMENT_H_ */ diff --git a/jscoverage.c b/jscoverage.c index f011148..501dc79 100644 --- a/jscoverage.c +++ b/jscoverage.c @@ -126,17 +126,19 @@ int main(int argc, char ** argv) { fatal_command_line("missing argument"); } + char * path = strdup(source); source = make_canonical_path(source); destination = make_canonical_path(destination); jscoverage_init(); - jscoverage_instrument(source, destination, verbose, exclude, num_exclude, no_instrument, num_no_instrument); + jscoverage_instrument(source, destination, verbose, exclude, num_exclude, no_instrument, num_no_instrument, path); jscoverage_cleanup(); free(source); free(destination); free(exclude); free(no_instrument); + free(path); exit(EXIT_SUCCESS); } From 83e5f7cc9e14a5663967a5e5ec675848ea3d6acd Mon Sep 17 00:00:00 2001 From: Kevin Kragenbrink Date: Fri, 24 Aug 2012 22:42:04 -0700 Subject: [PATCH 2/3] Fixing outstanding bugs and cleaning up extra slashes. --- instrument.c | 11 +++++++++-- instrument.h | 2 +- jscoverage.c | 3 ++- 3 files changed, 12 insertions(+), 4 deletions(-) diff --git a/instrument.c b/instrument.c index bdc147a..1ff50ed 100644 --- a/instrument.c +++ b/instrument.c @@ -216,9 +216,16 @@ void jscoverage_instrument(const char * source, free(ni); } - int length = strlen(path) + strlen(p->name) + 1; + /** make sure the path does not end in a slash. **/ + size_t length; + while (string_ends_with(path, "/")) { + length = strlen(path); + path[length-1] = '\0'; + } + + length = strlen(path) + strlen(p->name) + 1; char * filepath = malloc(length); - sprintf(filepath, "%s/%s", path, p->name); + snprintf(filepath, length, "%s/%s", path, p->name); instrument_file(s, d, p->name, instrument_this, filepath); cleanup: diff --git a/instrument.h b/instrument.h index a260757..0df539f 100644 --- a/instrument.h +++ b/instrument.h @@ -27,6 +27,6 @@ void jscoverage_instrument(const char * source, int num_exclude, char ** no_instrument, int num_no_instrument, - const char * path); + char * path); #endif /* INSTRUMENT_H_ */ diff --git a/jscoverage.c b/jscoverage.c index 501dc79..5fddd4a 100644 --- a/jscoverage.c +++ b/jscoverage.c @@ -126,7 +126,8 @@ int main(int argc, char ** argv) { fatal_command_line("missing argument"); } - char * path = strdup(source); + char * path; + path = strdup(source); source = make_canonical_path(source); destination = make_canonical_path(destination); From ee7038bad07027d70b00ea871df35c61e32b2361 Mon Sep 17 00:00:00 2001 From: Kevin Kragenbrink Date: Tue, 28 Aug 2012 13:32:53 -0700 Subject: [PATCH 3/3] Filenames were being truncated due to ignoring the null at the end of the line in the malloc. --- instrument.c | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/instrument.c b/instrument.c index 1ff50ed..81b0bf9 100644 --- a/instrument.c +++ b/instrument.c @@ -223,7 +223,7 @@ void jscoverage_instrument(const char * source, path[length-1] = '\0'; } - length = strlen(path) + strlen(p->name) + 1; + length = strlen(path) + strlen(p->name) + 2; char * filepath = malloc(length); snprintf(filepath, length, "%s/%s", path, p->name); instrument_file(s, d, p->name, instrument_this, filepath);