diff options
| author | 2020-05-24 06:56:59 -0500 | |
|---|---|---|
| committer | 2020-05-24 07:54:09 -0500 | |
| commit | d97525644792cf0bceccde67be1bfd31dc7e2721 (patch) | |
| tree | 08fd172513b4124fbcb309b68078923813939c91 | |
| parent | 15f2eea66366ec692d4f9cd13c3302ea771ecad0 (diff) | |
| download | libplist-d97525644792cf0bceccde67be1bfd31dc7e2721.tar.gz libplist-d97525644792cf0bceccde67be1bfd31dc7e2721.tar.bz2 | |
plistutil: Added ability for files to be read from stdin
| -rw-r--r-- | tools/plistutil.c | 96 |
1 files changed, 71 insertions, 25 deletions
diff --git a/tools/plistutil.c b/tools/plistutil.c index a1a8c9c..4a4b75e 100644 --- a/tools/plistutil.c +++ b/tools/plistutil.c | |||
| @@ -28,6 +28,9 @@ | |||
| 28 | #include <string.h> | 28 | #include <string.h> |
| 29 | #include <sys/stat.h> | 29 | #include <sys/stat.h> |
| 30 | #include <errno.h> | 30 | #include <errno.h> |
| 31 | #include <unistd.h> | ||
| 32 | |||
| 33 | #define BUF_SIZE 2048 // Seems to be a decent start to cover most stdin files | ||
| 31 | 34 | ||
| 32 | #ifdef _MSC_VER | 35 | #ifdef _MSC_VER |
| 33 | #pragma warning(disable:4996) | 36 | #pragma warning(disable:4996) |
| @@ -45,8 +48,8 @@ static void print_usage(int argc, char *argv[]) | |||
| 45 | name = strrchr(argv[0], '/'); | 48 | name = strrchr(argv[0], '/'); |
| 46 | printf("Usage: %s -i|--infile FILE [-o|--outfile FILE] [-d|--debug]\n", (name ? name + 1: argv[0])); | 49 | printf("Usage: %s -i|--infile FILE [-o|--outfile FILE] [-d|--debug]\n", (name ? name + 1: argv[0])); |
| 47 | printf("Convert a plist FILE from binary to XML format or vice-versa.\n\n"); | 50 | printf("Convert a plist FILE from binary to XML format or vice-versa.\n\n"); |
| 48 | printf(" -i, --infile FILE\tThe FILE to convert from\n"); | 51 | printf(" -i, --infile FILE\tOptional FILE to convert from or stdin if - or not used\n"); |
| 49 | printf(" -o, --outfile FILE\tOptional FILE to convert to or stdout if not used\n"); | 52 | printf(" -o, --outfile FILE\tOptional FILE to convert to or stdout if - or not used\n"); |
| 50 | printf(" -d, --debug\t\tEnable extended debug output\n"); | 53 | printf(" -d, --debug\t\tEnable extended debug output\n"); |
| 51 | printf("\n"); | 54 | printf("\n"); |
| 52 | } | 55 | } |
| @@ -96,12 +99,6 @@ static options_t *parse_arguments(int argc, char *argv[]) | |||
| 96 | } | 99 | } |
| 97 | } | 100 | } |
| 98 | 101 | ||
| 99 | if (!options->in_file) | ||
| 100 | { | ||
| 101 | free(options); | ||
| 102 | return NULL; | ||
| 103 | } | ||
| 104 | |||
| 105 | return options; | 102 | return options; |
| 106 | } | 103 | } |
| 107 | 104 | ||
| @@ -122,28 +119,77 @@ int main(int argc, char *argv[]) | |||
| 122 | return 0; | 119 | return 0; |
| 123 | } | 120 | } |
| 124 | 121 | ||
| 125 | // read input file | 122 | if (!options->in_file || !strcmp(options->in_file, "-")) |
| 126 | iplist = fopen(options->in_file, "rb"); | 123 | { |
| 127 | if (!iplist) { | 124 | read_size = 0; |
| 128 | printf("ERROR: Could not open input file '%s': %s\n", options->in_file, strerror(errno)); | 125 | plist_entire = malloc(sizeof(char) * BUF_SIZE); |
| 129 | free(options); | 126 | if(plist_entire == NULL) |
| 130 | return 1; | 127 | { |
| 128 | printf("ERROR: Failed to allocate buffer to read from stdin"); | ||
| 129 | free(options); | ||
| 130 | return 1; | ||
| 131 | } | ||
| 132 | plist_entire[read_size] = '\0'; | ||
| 133 | char ch; | ||
| 134 | while(read(STDIN_FILENO, &ch, 1) > 0) | ||
| 135 | { | ||
| 136 | if (read_size >= BUF_SIZE) { | ||
| 137 | char *old = plist_entire; | ||
| 138 | plist_entire = realloc(plist_entire, sizeof(char) * (read_size + 1)); | ||
| 139 | if (plist_entire == NULL) | ||
| 140 | { | ||
| 141 | printf("ERROR: Failed to reallocate stdin buffer\n"); | ||
| 142 | free(old); | ||
| 143 | free(options); | ||
| 144 | return 1; | ||
| 145 | } | ||
| 146 | } | ||
| 147 | plist_entire[read_size] = ch; | ||
| 148 | read_size++; | ||
| 149 | } | ||
| 150 | plist_entire[read_size] = '\0'; | ||
| 151 | |||
| 152 | // Not positive we need this, but it doesnt seem to hurt lol | ||
| 153 | if(ferror(stdin)) | ||
| 154 | { | ||
| 155 | printf("ERROR: reading from stdin.\n"); | ||
| 156 | free(plist_entire); | ||
| 157 | free(options); | ||
| 158 | return 1; | ||
| 159 | } | ||
| 160 | |||
| 161 | if (read_size < 8) { | ||
| 162 | printf("ERROR: Input file is too small to contain valid plist data.\n"); | ||
| 163 | free(plist_entire); | ||
| 164 | free(options); | ||
| 165 | return 1; | ||
| 166 | } | ||
| 131 | } | 167 | } |
| 168 | else | ||
| 169 | { | ||
| 170 | // read input file | ||
| 171 | iplist = fopen(options->in_file, "rb"); | ||
| 172 | if (!iplist) { | ||
| 173 | printf("ERROR: Could not open input file '%s': %s\n", options->in_file, strerror(errno)); | ||
| 174 | free(options); | ||
| 175 | return 1; | ||
| 176 | } | ||
| 177 | |||
| 178 | memset(&filestats, '\0', sizeof(struct stat)); | ||
| 179 | fstat(fileno(iplist), &filestats); | ||
| 132 | 180 | ||
| 133 | memset(&filestats, '\0', sizeof(struct stat)); | 181 | if (filestats.st_size < 8) { |
| 134 | fstat(fileno(iplist), &filestats); | 182 | printf("ERROR: Input file is too small to contain valid plist data.\n"); |
| 183 | free(options); | ||
| 184 | fclose(iplist); | ||
| 185 | return -1; | ||
| 186 | } | ||
| 135 | 187 | ||
| 136 | if (filestats.st_size < 8) { | 188 | plist_entire = (char *) malloc(sizeof(char) * (filestats.st_size + 1)); |
| 137 | printf("ERROR: Input file is too small to contain valid plist data.\n"); | 189 | read_size = fread(plist_entire, sizeof(char), filestats.st_size, iplist); |
| 138 | free(options); | ||
| 139 | fclose(iplist); | 190 | fclose(iplist); |
| 140 | return -1; | ||
| 141 | } | 191 | } |
| 142 | 192 | ||
| 143 | plist_entire = (char *) malloc(sizeof(char) * (filestats.st_size + 1)); | ||
| 144 | read_size = fread(plist_entire, sizeof(char), filestats.st_size, iplist); | ||
| 145 | fclose(iplist); | ||
| 146 | |||
| 147 | // convert from binary to xml or vice-versa | 193 | // convert from binary to xml or vice-versa |
| 148 | if (plist_is_binary(plist_entire, read_size)) | 194 | if (plist_is_binary(plist_entire, read_size)) |
| 149 | { | 195 | { |
| @@ -160,7 +206,7 @@ int main(int argc, char *argv[]) | |||
| 160 | 206 | ||
| 161 | if (plist_out) | 207 | if (plist_out) |
| 162 | { | 208 | { |
| 163 | if (options->out_file != NULL) | 209 | if (options->out_file != NULL && strcmp(options->out_file, "-")) |
| 164 | { | 210 | { |
| 165 | FILE *oplist = fopen(options->out_file, "wb"); | 211 | FILE *oplist = fopen(options->out_file, "wb"); |
| 166 | if (!oplist) { | 212 | if (!oplist) { |
