]> git.draconx.ca Git - scripts.git/blob - dpicalc.c
Add script to convert "JWK"-format RSA keys to normal.
[scripts.git] / dpicalc.c
1 /*
2  * Copyright © 2011 Nick Bowler
3  *
4  * License WTFPL2: Do What The Fuck You Want To Public License, version 2.
5  * This is free software: you are free to do what the fuck you want to.
6  * There is NO WARRANTY, to the extent permitted by law.
7  */
8
9 #include <stdio.h>
10 #include <stdlib.h>
11 #include <string.h>
12 #include <errno.h>
13 #include <math.h>
14
15 const char *progname = "dpicalc";
16
17 static unsigned long
18 simple_strtoul(const char *str, const char *name, int base)
19 {
20         unsigned long ret;
21         char *end;
22
23         errno = 0;
24         ret = strtoul(str, &end, base);
25         if (errno != 0) {
26                 fprintf(stderr, "%s: %s: invalid %s: %s\n",
27                                 progname, str, name, strerror(errno));
28                 exit(EXIT_FAILURE);
29         } else if (*end != '\0') {
30                 fprintf(stderr, "%s: %s: invalid %s: trailing garbage\n",
31                                 progname, str, name);
32                 exit(EXIT_FAILURE);
33         }
34
35         return ret;
36 }
37
38 int main(int argc, char **argv)
39 {
40         double diagonal, ratio, phys_width;
41         unsigned long w, h;
42
43         if (argc > 0)
44                 progname = argv[0];
45
46         if (argc < 4) {
47                 fprintf(stderr, "usage: %s diagonal pixel_w pixel_h\n",
48                                 progname);
49                 return EXIT_FAILURE;
50         }
51
52         diagonal = strtod(argv[1], NULL);
53         w = simple_strtoul(argv[2], "pixel width", 10);
54         h = simple_strtoul(argv[3], "pixel height", 10);
55
56         ratio = (double) h / w;
57         if (!isnormal(ratio)) {
58                 fprintf(stderr, "%s: nonsensical aspect ratio (%.2f)\n",
59                                 progname, ratio);
60         }
61
62         phys_width = sqrt(diagonal*diagonal / (1 + ratio*ratio));
63         printf("%.1f\n", w / phys_width);
64
65         return 0;
66 }