fileweight

Check-in [8c81447bf1]
Login

Many hyperlinks are disabled.
Use anonymous login to enable hyperlinks.

Overview
Comment:Transferred to Fossil
Downloads: Tarball | ZIP archive | SQL archive
Timelines: family | ancestors | trunk
Files: files | file ages | folders
SHA3-256: 8c81447bf1fcd67b6a6c6ee2d9af82702d6fae55fef1d06efbd44cd6a4aa3bab
User & Date: Cthulhux 2019-09-06 00:48:11
Context
2019-09-06
00:48
Transferred to Fossil Leaf check-in: 8c81447bf1 user: Cthulhux tags: trunk
00:45
initial empty check-in check-in: 1095669803 user: Cthulhux tags: trunk
Changes
Hide Diffs Unified Diffs Ignore Whitespace Patch

Added README.md.

























>
>
>
>
>
>
>
>
>
>
>
>
1
2
3
4
5
6
7
8
9
10
11
12
# How much does a file weigh?

Based on [IBM's research](http://gizmodo.com/5875674/ibm-figures-out-how-many-atoms-it-takes-to-hold-a-bit-hint-its-12), one bit takes between 1 and 62.5 million iron atoms on a normal hard disk. This application tries to estimate a medium weight for the input file, based on a value of `(62.5 + 1) / 2` = 31.75 million iron atoms per bit.

## Usage

    cc -o fileweight fileweight.c
    ./fileweight some-file

## License

Licensed under the terms of the [WTFPL v2](http://wtfpl.net/txt/copying).

Added fileweight.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
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
/*
 * How much does a file weigh?
 *
 * Based on IBM's research, one bit takes between
 * 1 and 62.5 million iron atoms on a normal hard
 * disk:
 * http://gizmodo.com/5875674/ibm-figures-out-how-many-atoms-it-takes-to-hold-a-bit-hint-its-12
 *
 * This application tries to estimate a medium
 * weight for the input file, based on a value of
 * (62.5+1)/2 = 31.75 million iron atoms per bit.
 *
 * Usage:
 *   cc -o fileweight fileweight.c
 *   ./fileweight some-file
 *
 * Licensed under the terms of the WTFPL v2.
 * http://wtfpl.net/txt/copying
 */

#include <stdio.h>
#include <stdlib.h>
#include <fcntl.h>
#include <limits.h>
#ifndef _WIN32
#include <unistd.h>
#include <sys/param.h> /* MAXPATHLEN */
#ifndef strlcpy
/* Linux has no concept of strlcpy(). */
#define strlcpy(x, y, z) snprintf((x), (z), "%s", (y))
#endif
#else /* ifdef WIN32 */
#include <io.h>        /* io.h does mostly replace unistd.h */
#include <sys/types.h> /* off_t support */

/* Windows does not have MAXPATHLEN. */
#define MAXPATHLEN _MAX_PATH

/* Nor does it have strlcpy(). */
#define strlcpy(x, y, z) strncpy_s((x), (z), (y), _TRUNCATE)
#endif


#define ATOMS_PER_BIT      31750000
#define MG_PER_IRON_ATOM   9.2732796E-20


void show_syntax(char *argv[]) {
    /* Someone made a mistake. */
    printf("Invalid number of parameters or invalid file parameter specified!\n\n");
    printf("Usage:\n");
    printf("   %s <filename>\n\n", argv[0]);
}

int main(int argc, char *argv[]) {
#ifdef __OpenBSD__
    if (-1 == pledge("stdio rpath", NULL)) {
        /* Don't waste priviledges. */
        return(EXIT_FAILURE);
    }
#endif

    if (argc != 2) {
        show_syntax(argv);
        return(EXIT_FAILURE);
    }

    char inputfile[MAXPATHLEN];
    off_t file_bytes;

    strlcpy(inputfile, argv[1], sizeof(inputfile));
    int fd_inputfile = open(inputfile, O_RDONLY);

    if (!fd_inputfile) {
        /* Couldn't open the file. */
        show_syntax(argv);
        return(EXIT_FAILURE);
    }

    file_bytes = lseek(fd_inputfile, 0, SEEK_END);
    close(fd_inputfile);

    long double weight = file_bytes * MG_PER_IRON_ATOM * ATOMS_PER_BIT / 1000 * CHAR_BIT;
    printf("The file %s weighs about %Le grams.\n\n", argv[1], weight);

    return(EXIT_SUCCESS);
}