diff options
| author | 2020-12-13 00:55:33 +0100 | |
|---|---|---|
| committer | 2020-12-13 00:55:33 +0100 | |
| commit | 9c3dd915ea00ce33d6dee359bcef963a586aaf05 (patch) | |
| tree | 9111c69a3d27e606cb672b5ef6db0a96c111c6e3 /tools | |
| parent | 8c5ab80581e00c13cf852f378767a25d5df903b2 (diff) | |
| download | libimobiledevice-9c3dd915ea00ce33d6dee359bcef963a586aaf05.tar.gz libimobiledevice-9c3dd915ea00ce33d6dee359bcef963a586aaf05.tar.bz2 | |
idevicescreenshot: Remove unnecessary math functions to find a unique filename
Removes repeated malloc/free too, limits the maximum filenames to try to 65535
and fails if no unique filename could be found. I think this limit is VERY
generous. If there are really so many files to test against it will also cause
an unconveniently long delay, but I felt a lower limit could still be easily
reached.
Diffstat (limited to 'tools')
| -rw-r--r-- | tools/idevicescreenshot.c | 38 |
1 files changed, 23 insertions, 15 deletions
diff --git a/tools/idevicescreenshot.c b/tools/idevicescreenshot.c index f33e83e..62bb4a3 100644 --- a/tools/idevicescreenshot.c +++ b/tools/idevicescreenshot.c | |||
| @@ -29,7 +29,6 @@ | |||
| 29 | #include <string.h> | 29 | #include <string.h> |
| 30 | #include <stdlib.h> | 30 | #include <stdlib.h> |
| 31 | #include <errno.h> | 31 | #include <errno.h> |
| 32 | #include <math.h> | ||
| 33 | #include <time.h> | 32 | #include <time.h> |
| 34 | #include <unistd.h> | 33 | #include <unistd.h> |
| 35 | #ifndef WIN32 | 34 | #ifndef WIN32 |
| @@ -121,17 +120,21 @@ int main(int argc, char **argv) | |||
| 121 | uint64_t imgsize = 0; | 120 | uint64_t imgsize = 0; |
| 122 | if (screenshotr_take_screenshot(shotr, &imgdata, &imgsize) == SCREENSHOTR_E_SUCCESS) { | 121 | if (screenshotr_take_screenshot(shotr, &imgdata, &imgsize) == SCREENSHOTR_E_SUCCESS) { |
| 123 | get_image_filename(imgdata, &filename); | 122 | get_image_filename(imgdata, &filename); |
| 124 | FILE *f = fopen(filename, "wb"); | 123 | if (!filename) { |
| 125 | if (f) { | 124 | printf("FATAL: Could not find a unique filename!\n"); |
| 126 | if (fwrite(imgdata, 1, (size_t)imgsize, f) == (size_t)imgsize) { | 125 | } else { |
| 127 | printf("Screenshot saved to %s\n", filename); | 126 | FILE *f = fopen(filename, "wb"); |
| 128 | result = 0; | 127 | if (f) { |
| 128 | if (fwrite(imgdata, 1, (size_t)imgsize, f) == (size_t)imgsize) { | ||
| 129 | printf("Screenshot saved to %s\n", filename); | ||
| 130 | result = 0; | ||
| 131 | } else { | ||
| 132 | printf("Could not save screenshot to file %s!\n", filename); | ||
| 133 | } | ||
| 134 | fclose(f); | ||
| 129 | } else { | 135 | } else { |
| 130 | printf("Could not save screenshot to file %s!\n", filename); | 136 | printf("Could not open %s for writing: %s\n", filename, strerror(errno)); |
| 131 | } | 137 | } |
| 132 | fclose(f); | ||
| 133 | } else { | ||
| 134 | printf("Could not open %s for writing: %s\n", filename, strerror(errno)); | ||
| 135 | } | 138 | } |
| 136 | } else { | 139 | } else { |
| 137 | printf("Could not get screenshot!\n"); | 140 | printf("Could not get screenshot!\n"); |
| @@ -179,6 +182,7 @@ void get_image_filename(char *imgdata, char **filename) | |||
| 179 | basename = (char*)malloc(strlen(*filename) + 1); | 182 | basename = (char*)malloc(strlen(*filename) + 1); |
| 180 | strcpy(basename, *filename); | 183 | strcpy(basename, *filename); |
| 181 | free(*filename); | 184 | free(*filename); |
| 185 | *filename = NULL; | ||
| 182 | } else { | 186 | } else { |
| 183 | time_t now = time(NULL); | 187 | time_t now = time(NULL); |
| 184 | basename = (char*)malloc(32); | 188 | basename = (char*)malloc(32); |
| @@ -186,15 +190,19 @@ void get_image_filename(char *imgdata, char **filename) | |||
| 186 | } | 190 | } |
| 187 | 191 | ||
| 188 | // Ensure the filename is unique on disk. | 192 | // Ensure the filename is unique on disk. |
| 189 | char *unique_filename = (char*)malloc(strlen(basename) + strlen(fileext) + 1); | 193 | char *unique_filename = (char*)malloc(strlen(basename) + strlen(fileext) + 7); |
| 190 | sprintf(unique_filename, "%s%s", basename, fileext); | 194 | sprintf(unique_filename, "%s%s", basename, fileext); |
| 191 | int i; | 195 | int i; |
| 192 | for (i = 2; access(unique_filename, F_OK) != -1; i++) { | 196 | for (i = 2; i < (1 << 16); i++) { |
| 193 | free(unique_filename); | 197 | if (access(unique_filename, F_OK) == -1) { |
| 194 | unique_filename = (char*)malloc(strlen(basename) + strlen(fileext) + floor(log10(i)) + 3); | 198 | *filename = unique_filename; |
| 199 | break; | ||
| 200 | } | ||
| 195 | sprintf(unique_filename, "%s-%d%s", basename, i, fileext); | 201 | sprintf(unique_filename, "%s-%d%s", basename, i, fileext); |
| 196 | } | 202 | } |
| 197 | *filename = unique_filename; | 203 | if (!*filename) { |
| 204 | free(unique_filename); | ||
| 205 | } | ||
| 198 | free(basename); | 206 | free(basename); |
| 199 | } | 207 | } |
| 200 | 208 | ||
