]> wimlib.net Git - wimlib/blob - examples/applywim.c
COPYING: updates and clarifications
[wimlib] / examples / applywim.c
1 /*
2  * applywim.c - A program to extract the first image from a WIM file.
3  *
4  * The author dedicates this file to the public domain.
5  * You can do whatever you want with this file.
6  */
7
8 #include <wimlib.h>
9 #include <stdio.h>
10
11 #define TO_PERCENT(numerator, denominator) \
12         ((float)(((denominator) == 0) ? 0 : ((numerator) * 100 / (float)(denominator))))
13
14 static enum wimlib_progress_status
15 extract_progress(enum wimlib_progress_msg msg,
16                  union wimlib_progress_info *info, void *progctx)
17 {
18         switch (msg) {
19         case WIMLIB_PROGRESS_MSG_EXTRACT_STREAMS:
20                 printf("Extracting files: %.2f%% complete\n",
21                        TO_PERCENT(info->extract.completed_bytes,
22                                   info->extract.total_bytes));
23                 break;
24         default:
25                 break;
26         }
27         return WIMLIB_PROGRESS_STATUS_CONTINUE;
28 }
29
30 int main(int argc, char **argv)
31 {
32         int ret;
33         WIMStruct *wim = NULL;
34         const char *wimpath;
35         const char *destdir;
36
37         /* Check for the correct number of arguments.  */
38         if (argc != 3) {
39                 fprintf(stderr, "Usage: applywim WIM DIR\n");
40                 return 2;
41         }
42
43         wimpath = argv[1];
44         destdir = argv[2];
45
46         /* Open the WIM file as a WIMStruct.  */
47         ret = wimlib_open_wim(wimpath,  /* Path of WIM file to open  */
48                               0,        /* WIMLIB_OPEN_FLAG_* flags (0 means all defaults)  */
49                               &wim);    /* Return the WIMStruct pointer in this location  */
50         if (ret != 0) /* Always should check the error codes.  */
51                 goto out;
52
53         /* Register our progress function.  */
54         wimlib_register_progress_function(wim, extract_progress, NULL);
55
56         /* Extract the first image.  */
57         ret = wimlib_extract_image(wim,     /* WIMStruct from which to extract the image  */
58                                    1,       /* Image to extract  */
59                                    destdir, /* Directory to extract the image to  */
60                                    0);      /* WIMLIB_EXTRACT_FLAG_* flags (0 means all defaults)  */
61
62 out:
63         /* Free the WIMStruct.  Has no effect if the pointer to it is NULL.  */
64         wimlib_free(wim);
65
66         /* Check for error status.  */
67         if (ret != 0) {
68                 fprintf(stderr, "wimlib error %d: %s\n",
69                         ret, wimlib_get_error_string(ret));
70         }
71
72         /* Free global memory (optional).  */
73         wimlib_global_cleanup();
74
75         return ret;
76 }