Mercurial > hgrepos > Python2 > PyMuPDF
comparison mupdf-source/thirdparty/extract/src/sys.c @ 3:2c135c81b16c
MERGE: upstream PyMuPDF 1.26.4 with MuPDF 1.26.7
| author | Franz Glasner <fzglas.hg@dom66.de> |
|---|---|
| date | Mon, 15 Sep 2025 11:44:09 +0200 |
| parents | b50eed0cc0ef |
| children |
comparison
equal
deleted
inserted
replaced
| 0:6015a75abc2d | 3:2c135c81b16c |
|---|---|
| 1 #include "mem.h" | |
| 2 #include "outf.h" | |
| 3 #include "sys.h" | |
| 4 | |
| 5 #include <errno.h> | |
| 6 #include <stdarg.h> | |
| 7 | |
| 8 #include <sys/stat.h> | |
| 9 | |
| 10 | |
| 11 /* Define extract_APPLE_IOS if we are on iOS. */ | |
| 12 #undef extract_APPLE_IOS | |
| 13 #ifdef __APPLE__ | |
| 14 #include "TargetConditionals.h" | |
| 15 #ifdef TARGET_OS_IPHONE | |
| 16 #define extract_APPLE_IOS | |
| 17 #elif TARGET_IPHONE_SIMULATOR | |
| 18 #define extract_APPLE_IOS | |
| 19 #endif | |
| 20 #endif | |
| 21 | |
| 22 int extract_systemf(extract_alloc_t *alloc, const char *format, ...) | |
| 23 { | |
| 24 #ifdef extract_APPLE_IOS | |
| 25 /* system() not available on iOS. */ | |
| 26 (void) alloc; | |
| 27 (void) format; | |
| 28 errno = ENOTSUP; | |
| 29 return -1; | |
| 30 #else | |
| 31 | |
| 32 int e; | |
| 33 char *command; | |
| 34 va_list va; | |
| 35 | |
| 36 va_start(va, format); | |
| 37 e = extract_vasprintf(alloc, &command, format, va); | |
| 38 va_end(va); | |
| 39 if (e < 0) return e; | |
| 40 outf("running: %s", command); | |
| 41 e = system(command); | |
| 42 extract_free(alloc, &command); | |
| 43 if (e > 0) { | |
| 44 errno = EIO; | |
| 45 } | |
| 46 return e; | |
| 47 | |
| 48 #endif | |
| 49 } | |
| 50 | |
| 51 int extract_read_all(extract_alloc_t *alloc, FILE *in, char **o_out) | |
| 52 { | |
| 53 size_t len = 0; | |
| 54 size_t delta = 128; | |
| 55 for(;;) { | |
| 56 size_t n; | |
| 57 if (extract_realloc2(alloc, o_out, len, len + delta + 1)) { | |
| 58 extract_free(alloc, o_out); | |
| 59 return -1; | |
| 60 } | |
| 61 n = fread(*o_out + len, 1 /*size*/, delta /*nmemb*/, in); | |
| 62 len += n; | |
| 63 if (feof(in)) { | |
| 64 (*o_out)[len] = 0; | |
| 65 return 0; | |
| 66 } | |
| 67 if (ferror(in)) { | |
| 68 /* It's weird that fread() and ferror() don't set errno. */ | |
| 69 errno = EIO; | |
| 70 extract_free(alloc, o_out); | |
| 71 return -1; | |
| 72 } | |
| 73 } | |
| 74 } | |
| 75 | |
| 76 int extract_read_all_path(extract_alloc_t *alloc, const char *path, char **o_text) | |
| 77 { | |
| 78 int e = -1; | |
| 79 FILE *f = NULL; | |
| 80 f = fopen(path, "rb"); | |
| 81 if (!f) goto end; | |
| 82 if (extract_read_all(alloc, f, o_text)) goto end; | |
| 83 e = 0; | |
| 84 end: | |
| 85 if (f) fclose(f); | |
| 86 if (e) extract_free(alloc, o_text); | |
| 87 return e; | |
| 88 } | |
| 89 | |
| 90 int extract_write_all(const void *data, size_t data_size, const char *path) | |
| 91 { | |
| 92 int e = -1; | |
| 93 FILE *f = fopen(path, "w"); | |
| 94 if (!f) goto end; | |
| 95 if (fwrite(data, data_size, 1 /*nmemb*/, f) != 1) goto end; | |
| 96 e = 0; | |
| 97 end: | |
| 98 if (f) fclose(f); | |
| 99 return e; | |
| 100 } | |
| 101 | |
| 102 int extract_check_path_shell_safe(const char *path) | |
| 103 /* Returns -1 with errno=EINVAL if <path> contains sequences that could make it | |
| 104 unsafe in shell commands. */ | |
| 105 { | |
| 106 if (0 | |
| 107 || strstr(path, "..") | |
| 108 || strchr(path, '\'') | |
| 109 || strchr(path, '"') | |
| 110 || strchr(path, ' ') | |
| 111 ) { | |
| 112 errno = EINVAL; | |
| 113 return -1; | |
| 114 } | |
| 115 return 0; | |
| 116 } | |
| 117 int extract_remove_directory(extract_alloc_t *alloc, const char *path) | |
| 118 { | |
| 119 if (extract_check_path_shell_safe(path)) { | |
| 120 outf("path_out is unsafe: %s", path); | |
| 121 return -1; | |
| 122 } | |
| 123 return extract_systemf(alloc, "rm -r '%s'", path); | |
| 124 } | |
| 125 | |
| 126 #ifdef _WIN32 | |
| 127 #include <direct.h> | |
| 128 int extract_mkdir(const char *path, int mode) | |
| 129 { | |
| 130 (void) mode; | |
| 131 return _mkdir(path); | |
| 132 } | |
| 133 #else | |
| 134 int extract_mkdir(const char *path, int mode) | |
| 135 { | |
| 136 return mkdir(path, mode); | |
| 137 } | |
| 138 #endif |
