File: tmp_full.c

package info (click to toggle)
lbcd 3.5.2-4
  • links: PTS, VCS
  • area: main
  • in suites: forky, sid, trixie
  • size: 1,708 kB
  • sloc: ansic: 11,073; sh: 1,816; perl: 503; makefile: 165
file content (64 lines) | stat: -rw-r--r-- 1,548 bytes parent folder | download | duplicates (4)
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
/*
 * Determine the percentage of free space in /tmp.
 *
 * A simple routine to obtain the percentage of free space in /tmp.  Machines
 * with a full tmp partition are often unusable.  Does not support Ultrix and
 * hard-codes the minfree values.  For unsupported systems, always reports 0%
 * full.
 *
 * Written by Larry Schwimmer
 * Copyright 1997, 2008, 2012
 *     The Board of Trustees of the Leland Stanford Junior University
 *
 * See LICENSE for licensing terms.
 */

#include <config.h>
#include <portable/statvfs.h>
#include <portable/system.h>

#include <server/internal.h>


/*
 * Returns the percent full of the file system at the given path.  Assume,
 * just to be safe, that the user can only use 95% of the available blocks.
 * (This number, minfree, may vary, but we don't have a good way of
 * determining it.)
 */
int
tmp_full(const char *path)
{
    struct statvfs info;
    int percent = 0;
    double total;

    if (chdir(path) == 0 && statvfs(".", &info) == 0) {
        if (info.f_bavail > info.f_blocks * 0.95)
            total = info.f_blocks;
        else
            total = info.f_blocks * 0.95;
        percent = (total - info.f_bavail) * 100.0 / total + 0.5;
        if (percent < 0)
            percent = 0;
        if (percent > 100)
            percent = 100;
    }
    return percent;
}


/*
 * Test routine.
 */
#ifdef MAIN
int
main(void)
{
    printf("%%%d percent tmp full.\n", tmp_full("/"));
# ifdef P_tmpdir
    printf("%%%d percent P_tmpdir full.\n", tmp_full(P_tmpdir));
# endif
    return 0;
}
#endif