Remove bb_ prefixes from xfuncs.c (and a few other places), consolidate
[platform/upstream/busybox.git] / util-linux / mkswap.c
1 /* vi: set sw=4 ts=4: */
2 /* mkswap.c - format swap device (Linux v1 only)
3  *
4  * Copyright 2006 Rob Landley <rob@landley.net>
5  *
6  * Licensed under GPLv2 or later, see file LICENSE in this tarball for details.
7  */
8
9 #include <busybox.h>
10
11 int mkswap_main(int argc, char *argv[])
12 {
13         int fd, pagesize;
14         off_t len;
15         unsigned int hdr[129];
16
17         // No options supported.
18
19         if (argc!=2) bb_show_usage();
20
21         // Figure out how big the device is and announce our intentions.
22         
23         fd = xopen(argv[1],O_RDWR);
24         len = fdlength(fd);
25         pagesize = getpagesize();
26         printf("Setting up swapspace version 1, size = %ld bytes\n", (long)(len-pagesize));
27
28         // Make a header.
29
30         memset(hdr, 0, 129 * sizeof(unsigned int));
31         hdr[0] = 1;
32         hdr[1] = (len / pagesize) - 1;
33
34         // Write the header.  Sync to disk because some kernel versions check
35         // signature on disk (not in cache) during swapon.
36
37         xlseek(fd, 1024, SEEK_SET);
38         xwrite(fd, hdr, 129 * sizeof(unsigned int));
39         xlseek(fd, pagesize-10, SEEK_SET);
40         xwrite(fd, "SWAPSPACE2", 10);
41         fsync(fd);
42
43         if (ENABLE_FEATURE_CLEAN_UP) close(fd);
44
45         return 0;
46 }