diff --git a/recoverjpeg.c b/recoverjpeg.c index 9af717a..2270e37 100644 --- a/recoverjpeg.c +++ b/recoverjpeg.c @@ -191,17 +191,19 @@ file_name(const char *dir_format, const char *file_format, unsigned int index) exit(1); } strncat(dir_buffer, "/", sizeof dir_buffer - 1); - } else { + } + else { *dir_buffer = '\0'; } snprintf(file_buffer, sizeof file_buffer, file_format, index); - strncat(dir_buffer, file_buffer, sizeof dir_buffer - strlen(dir_buffer) - 1); + strncat(dir_buffer, file_buffer, + sizeof dir_buffer - strlen(dir_buffer) - 1); return dir_buffer; } int -main(int argc, const char * const argv[]) +main(int argc, const char *const argv[]) { int fd, fdout; size_t read_size, block_size; @@ -239,7 +241,7 @@ main(int argc, const char * const argv[]) max_size = atol_suffix(optarg); break; case 'o': - move_to(optarg); + record_chdir(optarg); break; case 'q': quiet = 1; @@ -278,6 +280,8 @@ main(int argc, const char * const argv[]) exit(1); } + perform_chdirs(); + page_size = getpagesize(); if (read_size % page_size || read_size < max_size) { if (read_size < max_size) { diff --git a/recoverjpeg.md b/recoverjpeg.md index 9bccfaa..ae3b391 100644 --- a/recoverjpeg.md +++ b/recoverjpeg.md @@ -1,6 +1,6 @@ % RECOVERJPEG(1) Recoverjpeg User Manuals % Samuel Tardieu -% September 22, 2015 +% November 12, 2016 # NAME recoverjpeg - recover jpeg pictures from a filesystem image @@ -66,7 +66,7 @@ it is discarded. The default is 6 MiB. -o *directory* : Change the working directory before restoring files. Use this option to restore files into a directory with enough space instead of the current -directory. +directory. This option can be repeated. -q : Be quiet and do not display anything. diff --git a/recovermov.cpp b/recovermov.cpp index 3d01113..0a55b6b 100644 --- a/recovermov.cpp +++ b/recovermov.cpp @@ -110,7 +110,7 @@ int main(int argc, char* const* argv) { mov_index = atoi(optarg); break; case 'o': - move_to(optarg); + record_chdir(optarg); break; case 'V': display_version_and_exit("recovermov"); @@ -130,6 +130,8 @@ int main(int argc, char* const* argv) { std::ifstream infile(infilename, std::ios_base::in | std::ios_base::binary); + perform_chdirs(); + size_t atom_size; std::string atom_type; diff --git a/recovermov.md b/recovermov.md index 4215c2f..05aff7a 100644 --- a/recovermov.md +++ b/recovermov.md @@ -1,6 +1,6 @@ % RECOVERMOV(1) Recovermov User Manuals % Jan Funke -% September 22, 2015 +% November 12, 2016 # NAME @@ -49,7 +49,7 @@ for example) at the expense of a much longer running time. -o *directory* : Change the working directory before restoring files. Use this option to restore files into a directory with enough space instead of the current -directory. +directory. This option can be repeated. -V : Display program version and exit. diff --git a/utils.c b/utils.c index 3e43e79..d8eeab5 100644 --- a/utils.c +++ b/utils.c @@ -16,6 +16,14 @@ #include #include "utils.h" +typedef struct move_s +{ + const char *target; + struct move_s *next; +} move_t; + +static move_t *chdirs = NULL; + size_t atol_suffix(char *arg) { @@ -54,12 +62,34 @@ display_version_and_exit(const char *program_name) } void -move_to(const char *directory) +record_chdir(const char *directory) { - if (chdir(directory) != 0) { - char buffer[128]; - snprintf(buffer, sizeof buffer, "cannot change directory to `%s'", directory); - perror(buffer); + move_t **ptr = &chdirs; + while (*ptr != NULL) { + ptr = &(*ptr)->next; + } + *ptr = malloc(sizeof(move_t)); + if (*ptr == NULL) { + perror("malloc"); exit(1); } + (*ptr)->target = directory; + (*ptr)->next = NULL; +} + +void +perform_chdirs() +{ + move_t *p = chdirs; + while (p != NULL) { + if (chdir(p->target) != 0) { + char buffer[512]; + snprintf(buffer, sizeof buffer, "cannot change directory to `%s'", p->target); + perror(buffer); + exit(1); + } + move_t *to_free = p; + p = p->next; + free(to_free); + } } diff --git a/utils.h b/utils.h index a742b76..0f6762c 100644 --- a/utils.h +++ b/utils.h @@ -24,7 +24,9 @@ extern "C" void display_version_and_exit(const char *program_name) __attribute__ ((noreturn)); - void move_to(const char *directory); + void record_chdir(const char *directory); + + void perform_chdirs(); #ifdef __cplusplus }