2 * applywim.c - A program to extract the first image from a WIM file.
4 * Copyright 2022 Eric Biggers
6 * Permission is hereby granted, free of charge, to any person
7 * obtaining a copy of this software and associated documentation
8 * files (the "Software"), to deal in the Software without
9 * restriction, including without limitation the rights to use,
10 * copy, modify, merge, publish, distribute, sublicense, and/or sell
11 * copies of the Software, and to permit persons to whom the
12 * Software is furnished to do so, subject to the following
15 * The above copyright notice and this permission notice shall be
16 * included in all copies or substantial portions of the Software.
18 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
19 * EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES
20 * OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
21 * NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT
22 * HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY,
23 * WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING
24 * FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR
25 * OTHER DEALINGS IN THE SOFTWARE.
32 * Windows compatibility defines for string encoding. Applications using wimlib
33 * that need to run on both UNIX and Windows will need to do something similar
34 * to this, whereas applications that only need to run on one or the other can
35 * just use their platform's convention directly.
39 typedef wchar_t tchar;
46 #define TO_PERCENT(numerator, denominator) \
47 ((float)(((denominator) == 0) ? 0 : ((numerator) * 100 / (float)(denominator))))
49 static enum wimlib_progress_status
50 extract_progress(enum wimlib_progress_msg msg,
51 union wimlib_progress_info *info, void *progctx)
54 case WIMLIB_PROGRESS_MSG_EXTRACT_STREAMS:
55 printf("Extracting files: %.2f%% complete\n",
56 TO_PERCENT(info->extract.completed_bytes,
57 info->extract.total_bytes));
62 return WIMLIB_PROGRESS_STATUS_CONTINUE;
65 int main(int argc, tchar **argv)
68 WIMStruct *wim = NULL;
72 /* Check for the correct number of arguments. */
74 fprintf(stderr, "Usage: applywim WIM DIR\n");
81 /* Open the WIM file as a WIMStruct. */
82 ret = wimlib_open_wim(wimpath, /* Path of WIM file to open */
83 0, /* WIMLIB_OPEN_FLAG_* flags (0 means all defaults) */
84 &wim); /* Return the WIMStruct pointer in this location */
85 if (ret != 0) /* Always should check the error codes. */
88 /* Register our progress function. */
89 wimlib_register_progress_function(wim, extract_progress, NULL);
91 /* Extract the first image. */
92 ret = wimlib_extract_image(wim, /* WIMStruct from which to extract the image */
93 1, /* Image to extract */
94 destdir, /* Directory to extract the image to */
95 0); /* WIMLIB_EXTRACT_FLAG_* flags (0 means all defaults) */
98 /* Free the WIMStruct. Has no effect if the pointer to it is NULL. */
101 /* Check for error status. */
103 fprintf(stderr, "wimlib error %d: %" TS"\n",
104 ret, wimlib_get_error_string((enum wimlib_error_code)ret));
107 /* Free global memory (optional). */
108 wimlib_global_cleanup();