xfs/{111,137}: replace open-coded calls to repair with _scratch_xfs_repair
[xfstests-dev.git] / src / mkswap.c
1 /* mkswap(8) without any sanity checks */
2
3 #include <stdint.h>
4 #include <stdio.h>
5 #include <stdlib.h>
6 #include <string.h>
7 #include <unistd.h>
8 #include <sys/stat.h>
9 #include <sys/types.h>
10
11 struct swap_header {
12         char            bootbits[1024];
13         uint32_t        version;
14         uint32_t        last_page;
15         uint32_t        nr_badpages;
16         unsigned char   sws_uuid[16];
17         unsigned char   sws_volume[16];
18         uint32_t        padding[117];
19         uint32_t        badpages[1];
20 };
21
22 int main(int argc, char **argv)
23 {
24         struct swap_header *hdr;
25         FILE *file;
26         struct stat st;
27         long page_size;
28         int ret;
29
30         if (argc != 2) {
31                 fprintf(stderr, "usage: %s PATH\n", argv[0]);
32                 return EXIT_FAILURE;
33         }
34
35         page_size = sysconf(_SC_PAGESIZE);
36         if (page_size == -1) {
37                 perror("sysconf");
38                 return EXIT_FAILURE;
39         }
40
41         hdr = calloc(1, page_size);
42         if (!hdr) {
43                 perror("calloc");
44                 return EXIT_FAILURE;
45         }
46
47         file = fopen(argv[1], "r+");
48         if (!file) {
49                 perror("fopen");
50                 free(hdr);
51                 return EXIT_FAILURE;
52         }
53
54         ret = fstat(fileno(file), &st);
55         if (ret) {
56                 perror("fstat");
57                 free(hdr);
58                 fclose(file);
59                 return EXIT_FAILURE;
60         }
61
62         hdr->version = 1;
63         hdr->last_page = st.st_size / page_size - 1;
64         memset(&hdr->sws_uuid, 0x99, sizeof(hdr->sws_uuid));
65         memcpy((char *)hdr + page_size - 10, "SWAPSPACE2", 10);
66
67         if (fwrite(hdr, page_size, 1, file) != 1) {
68                 perror("fwrite");
69                 free(hdr);
70                 fclose(file);
71                 return EXIT_FAILURE;
72         }
73
74         if (fclose(file) == EOF) {
75                 perror("fwrite");
76                 free(hdr);
77                 return EXIT_FAILURE;
78         }
79
80         free(hdr);
81
82         return EXIT_SUCCESS;
83 }