-
Notifications
You must be signed in to change notification settings - Fork 0
/
alignsize.c
72 lines (65 loc) · 1.67 KB
/
alignsize.c
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
#include <stdint.h>
#include <unistd.h>
#include <err.h>
#include <stdlib.h>
#include <stdio.h>
#include "filesize.h"
static void
usage(void)
{
const char *usagestr = "usage: alignsize [-a alignbits] [-s sectorbits] [-e extrasize] <file> ...\n" \
"alignsize prints the size of file(s) or disk(s) in bytes or sectors,\n" \
"adding an optional addend an alignment\n" \
" -a alignbits align the result up to alignbits bits\n" \
" -s sectorbits print the output in sectors, with sectors of width 1<<sectorbits\n" \
" -e extrasize add extrasize bytes to the file size before alignment\n"\
" <file> ... files from which to sum sizes\n";
dprintf(2, usagestr);
_exit(1);
}
int
main(int argc, char **argv)
{
off_t sectors = 0;
off_t align = 0;
off_t slack = 0;
off_t sz;
char opt;
while ((opt = getopt(argc, argv, "a:s:e:h")) > 0) {
switch (opt) {
case 'h':
usage();
break;
case 'a':
align = (off_t)atoi(optarg);
if (align < 0)
errx(1, "negative alignment %lld", (long long)align);
break;
case 's':
sectors = (off_t)atoi(optarg);
if (sectors < 0)
errx(1, "negative sectors %lli", (long long)sectors);
break;
case 'e':
slack = (off_t)atoi(optarg);
if (slack < 0)
errx(1, "negative slack %lli", (long long)slack);
break;
default:
errx(1, "unexpected option %c\n", opt);
}
}
if (optind >= argc)
usage();
sz = 0;
for (int i=optind; i<argc; i++)
sz += getsize(argv[i]);
if (align)
sz = alignup(sz, align);
sz += slack;
if (align)
sz = alignup(sz, align);
if (sectors)
sz >>= sectors;
printf("%llu\n", (unsigned long long)sz);
}